// SPDX-License-Identifier: BSD-3-Clause OR Apache-2.0 #ifndef AAP2_UTIL_H_INCLUDED #define AAP2_UTIL_H_INCLUDED // Common functions for the AAP 2.0 Agent and Client. #include "aap2/aap2.pb.h" #include #include #include #include #ifdef __cplusplus extern "C" { #endif // __cplusplus // SENDING /** * @brief Send a message to the AAP socket. * * @param socket_fd Socket on which to send the message * @param fields Nanopb Protobuf message specification * @param src_struct Pointer to the Nanopb Protobuf message struct to send * @return 0 if successful, errno otherwise */ int aap2_send_message(const int socket_fd, const pb_msgdesc_t *const fields, const void *const src_struct); // RECEIVING /** * @brief A Nanopb-compatible callback function to receive data from a stream socket. * * @param stream Pointer to the Nanopb input stream (contains the socket fd) * @param buf A buffer provided by Nanopb to read the raw data into * @param count Length of the provided buffer * @return true if successful, false if not (evaluated by Nanopb) */ bool aap2_pb_recv_callback(pb_istream_t *stream, uint8_t *buf, size_t count); /** * @brief Receive a message from the AAP socket. * * @param stream Pointer to the Nanopb input stream (contains the socket fd) * @param fields Nanopb Protobuf message specification * @param dest_struct Pointer to the Nanopb Protobuf message struct to be populated * @return 0 if successful, -1 otherwise */ int aap2_recv_message(pb_istream_t *stream, const pb_msgdesc_t *const fields, void *dest_struct); // ADU FLAGS HANDLING /** * @brief Representation of the AAP 2.0 BundleADUFlags repeated enum field. */ struct aap2_bundle_adu_flags { /** * The payload of this ADU is a BIBE protocol data unit. */ bool is_bpdu; /** * The payload of this ADU is a bundle status report. */ bool is_status_report; /** * This ADU was or shall be authorized with administrative (bundle dispatch module) * permissions. Clients sending bundles with this flag set must authenticate with the * administrative (BDM) AAP 2.0 secret. Receivers of bundles with this flag set can assume * that it comes from a trusted source. Bundles with this flag may transmit, e.g., local * configuration and monitoring information. The flag does not leave the local node. */ bool with_bdm_auth; }; // Number of flags that may be set #define AAP2_BUNDLE_ADU_FLAGS_COUNT (_aap2_BundleADUFlags_MAX) #ifdef __cplusplus #define _Static_assert static_assert #endif // __cplusplus _Static_assert(AAP2_BUNDLE_ADU_FLAGS_COUNT == 3, "do not forget to update aap2.options"); /** * @brief Populate the given ADU flags field from the provided set of flags. * * @param flags_field The ADU flags array part of the BundleADU Nanopb message struct * @param flags The flags to be set * @return The number of flags set */ size_t aap2_set_adu_flags(aap2_BundleADUFlags *flags_field, struct aap2_bundle_adu_flags flags); /** * @brief Read the ADU flags from the provided Nanopb array. * * @param flags_field The ADU flags array part of the BundleADU Nanopb message struct * @param flags_count The number of entries set in the given array * @return A struct bundle_adu_flags containing the values of the individual ADU flags */ struct aap2_bundle_adu_flags aap2_get_adu_flags(const aap2_BundleADUFlags *flags_field, size_t flags_count); #ifdef __cplusplus } #endif // __cplusplus #endif // AAP2_UTIL_H_INCLUDED