NSDL C library
Dependents: NSDL_HelloWorld_WiFi UbloxModemNanoServiceClient IOT-NSDL_HelloWorld LWM2M_NanoService_Ethernet ... more
Fork of nsdl_lib by
Note that use of this software requires acceptance of the Sensinode EULA: http://mbed.org/teams/Sensinode/code/nsdl_lib/wiki/EULA
Revision 5:da1db64e7fb9, committed 2013-10-14
- Comitter:
- terohoo
- Date:
- Mon Oct 14 11:00:30 2013 +0000
- Parent:
- 0:58c4f13c4b9a
- Child:
- 6:1caf76131c9a
- Commit message:
- - New library build; - Header cleaning
Changed in this revision
Binary file nsdl_lib.ar has changed
--- a/sn_coap_header.h Wed Oct 09 09:20:29 2013 +0000 +++ b/sn_coap_header.h Mon Oct 14 11:00:30 2013 +0000 @@ -16,31 +16,33 @@ extern "C" { #endif -/* * * * * * * * * * * */ -/* * * * DEFINES * * * */ -/* * * * * * * * * * * */ - /* * * * * * * * * * * * * * */ /* * * * ENUMERATIONS * * * */ /* * * * * * * * * * * * * * */ -/* Enumeration for CoAP Version */ +/** + * \brief Enumeration for CoAP Version + */ typedef enum coap_version_ { COAP_VERSION_1 = 0x40, COAP_VERSION_UNKNOWN = 0xFF } coap_version_e; -/* Enumeration for CoAP Message type, used in CoAP Header */ +/** + * \brief Enumeration for CoAP Message type, used in CoAP Header + */ typedef enum sn_coap_msg_type_ { - COAP_MSG_TYPE_CONFIRMABLE = 0x00, /* User uses this for Reliable Request messages */ - COAP_MSG_TYPE_NON_CONFIRMABLE = 0x10, /* User uses this for Non-reliable Request and Response messages */ - COAP_MSG_TYPE_ACKNOWLEDGEMENT = 0x20, /* User uses this for Response to a Confirmable Request */ - COAP_MSG_TYPE_RESET = 0x30 /* User uses this to answer a Bad Request */ + COAP_MSG_TYPE_CONFIRMABLE = 0x00, /**< Reliable Request messages */ + COAP_MSG_TYPE_NON_CONFIRMABLE = 0x10, /**< Non-reliable Request and Response messages */ + COAP_MSG_TYPE_ACKNOWLEDGEMENT = 0x20, /**< Response to a Confirmable Request */ + COAP_MSG_TYPE_RESET = 0x30 /**< Answer a Bad Request */ } sn_coap_msg_type_e; -/* Enumeration for CoAP Message code, used in CoAP Header */ +/** + * \brief Enumeration for CoAP Message code, used in CoAP Header + */ typedef enum sn_coap_msg_code_ { COAP_MSG_CODE_EMPTY = 0, @@ -61,7 +63,7 @@ COAP_MSG_CODE_RESPONSE_NOT_FOUND = 132, COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED = 133, COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE = 134, - COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE = 136, /* Block */ + COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE = 136, COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED = 140, COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE = 141, COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT = 143, @@ -73,7 +75,9 @@ COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED = 165 } sn_coap_msg_code_e; -/* Enumeration for CoAP Option number, used in CoAP Header */ +/** + * \brief Enumeration for CoAP Option number, used in CoAP Header + */ typedef enum sn_coap_option_numbers_ { COAP_OPTION_IF_MATCH = 1, @@ -99,7 +103,9 @@ // 136 = (Reserved) } sn_coap_option_numbers_e; -/* Enumeration for CoAP Content Format codes */ +/** + * \brief Enumeration for CoAP Content Format codes + */ typedef enum sn_coap_content_format_ { COAP_CT_NONE = -1, @@ -111,18 +117,20 @@ COAP_CT_JSON = 50, } sn_coap_content_format_e; -/* Enumeration for CoAP status, used in CoAP Header */ +/** + * \brief Enumeration for CoAP status, used in CoAP Header + */ typedef enum sn_coap_status_ { - COAP_STATUS_OK = 0, /* Default value is OK */ - COAP_STATUS_PARSER_ERROR_IN_HEADER = 1, /* CoAP will send Reset message to invalid message sender */ - COAP_STATUS_PARSER_DUPLICATED_MSG = 2, /* CoAP will send Acknowledgement message to duplicated message sender */ - COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING = 3, /* User will get whole message after all message blocks received. - User must release messages with this status. */ - COAP_STATUS_PARSER_BLOCKWISE_ACK = 4, /* Acknowledgement for sent Blockwise message received */ - COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED = 5, /* Blockwise message received but not supported by compiling switch */ - COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED = 6 /* Blockwise message fully received and returned to app. */ - /* User must take care of releasing whole payload of the blockwise messages */ + COAP_STATUS_OK = 0, /**< Default value is OK */ + COAP_STATUS_PARSER_ERROR_IN_HEADER = 1, /**< CoAP will send Reset message to invalid message sender */ + COAP_STATUS_PARSER_DUPLICATED_MSG = 2, /**< CoAP will send Acknowledgement message to duplicated message sender */ + COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING = 3, /**< User will get whole message after all message blocks received. + User must release messages with this status. */ + COAP_STATUS_PARSER_BLOCKWISE_ACK = 4, /**< Acknowledgement for sent Blockwise message received */ + COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED = 5, /**< Blockwise message received but not supported by compiling switch */ + COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED = 6 /**< Blockwise message fully received and returned to app. + User must take care of releasing whole payload of the blockwise messages */ } sn_coap_status_e; @@ -130,67 +138,69 @@ /* * * * STRUCTURES * * * */ /* * * * * * * * * * * * * */ -/* Structure for CoAP Options */ +/** + * \brief Structure for CoAP Options + */ typedef struct sn_coap_options_list_ { - /* If-Match */ - /* If-None-Match */ - /* Size */ - /* Proxy-Scheme */ + uint8_t max_age_len; /**< 0-4 bytes. */ + uint8_t *max_age_ptr; /**< Must be set to NULL if not used */ - uint8_t max_age_len; /* 0-4 bytes. */ - uint8_t *max_age_ptr; /* Must be set to NULL if not used */ + uint16_t proxy_uri_len; /**< 1-1034 bytes. */ + uint8_t *proxy_uri_ptr; /**< Must be set to NULL if not used */ - uint16_t proxy_uri_len; /* 1-1034 bytes. */ - uint8_t *proxy_uri_ptr; /* Must be set to NULL if not used */ + uint8_t etag_len; /**< 1-8 bytes. Repeatable */ + uint8_t *etag_ptr; /**< Must be set to NULL if not used */ - uint8_t etag_len; /* 1-8 bytes. Repeatable */ - uint8_t *etag_ptr; /* Must be set to NULL if not used */ + uint16_t uri_host_len; /**< 1-255 bytes. */ + uint8_t *uri_host_ptr; /**< Must be set to NULL if not used */ - uint16_t uri_host_len; /* 1-255 bytes. */ - uint8_t *uri_host_ptr; /* Must be set to NULL if not used */ + uint16_t location_path_len; /**< 0-255 bytes. Repeatable */ + uint8_t *location_path_ptr; /**< Must be set to NULL if not used */ - uint16_t location_path_len; /* 0-255 bytes. Repeatable */ - uint8_t *location_path_ptr; /* Must be set to NULL if not used */ + uint8_t uri_port_len; /**< 0-2 bytes. */ + uint8_t *uri_port_ptr; /**< Must be set to NULL if not used */ - uint8_t uri_port_len; /* 0-2 bytes. */ - uint8_t *uri_port_ptr; /* Must be set to NULL if not used */ - - uint16_t location_query_len; /* 0-255 bytes. Repeatable */ - uint8_t *location_query_ptr; /* Must be set to NULL if not used */ + uint16_t location_query_len; /**< 0-255 bytes. Repeatable */ + uint8_t *location_query_ptr; /**< Must be set to NULL if not used */ uint8_t observe; - uint8_t observe_len; /* 0-2 bytes. */ - uint8_t *observe_ptr; /* Must be set to NULL if not used */ + uint8_t observe_len; /**< 0-2 bytes. */ + uint8_t *observe_ptr; /**< Must be set to NULL if not used */ - uint8_t accept_len; /* 0-2 bytes. Repeatable */ - uint8_t *accept_ptr; /* Must be set to NULL if not used */ + uint8_t accept_len; /**< 0-2 bytes. Repeatable */ + uint8_t *accept_ptr; /**< Must be set to NULL if not used */ - uint16_t uri_query_len; /* 1-255 bytes. Repeatable */ - uint8_t *uri_query_ptr; /* Must be set to NULL if not used */ + uint16_t uri_query_len; /**< 1-255 bytes. Repeatable */ + uint8_t *uri_query_ptr; /**< Must be set to NULL if not used */ - uint8_t block1_len; /* 0-3 bytes. */ - uint8_t *block1_ptr; /* Not for User */ + uint8_t block1_len; /**< 0-3 bytes. */ + uint8_t *block1_ptr; /**< Not for User */ - uint8_t block2_len; /* 0-3 bytes. */ - uint8_t *block2_ptr; /* Not for User */ + uint8_t block2_len; /**< 0-3 bytes. */ + uint8_t *block2_ptr; /**< Not for User */ } sn_coap_options_list_s; + /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ /* !!! Main CoAP message struct !!! */ /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +/** + * \brief Main CoAP message struct + */ typedef struct sn_coap_hdr_ { - sn_coap_status_e coap_status; /* Used for telling to User special cases when parsing message */ + sn_coap_status_e coap_status; /**< Used for telling to User special cases when parsing message */ /* * * * * * * * * * * */ /* * * * Header * * * */ /* * * * * * * * * * * */ - sn_coap_msg_type_e msg_type; /* Confirmable, Non-Confirmable, Acknowledgement or Reset */ - sn_coap_msg_code_e msg_code; /* Empty: 0; Requests: 1-31; Responses: 64-191 */ - uint16_t msg_id; /* Message ID. Parser sets parsed message ID, builder sets message ID of built coap message */ + sn_coap_msg_type_e msg_type; /**< Confirmable, Non-Confirmable, Acknowledgement or Reset */ + sn_coap_msg_code_e msg_code; /**< Empty: 0; Requests: 1-31; Responses: 64-191 */ + uint16_t msg_id; /**< Message ID. Parser sets parsed message ID, builder sets message ID of built coap message */ /* * * * * * * * * * * */ /* * * * Options * * * */ @@ -198,38 +208,63 @@ /* Here are most often used Options */ - uint16_t uri_path_len; /* 0-255 bytes. Repeatable. */ - uint8_t *uri_path_ptr; /* Must be set to NULL if not used. E.g: temp1/temp2 */ + uint16_t uri_path_len; /**< 0-255 bytes. Repeatable. */ + uint8_t *uri_path_ptr; /**< Must be set to NULL if not used. E.g: temp1/temp2 */ - uint8_t token_len; /* 1-8 bytes. */ - uint8_t *token_ptr; /* Must be set to NULL if not used */ + uint8_t token_len; /**< 1-8 bytes. */ + uint8_t *token_ptr; /**< Must be set to NULL if not used */ - /* todo: COAP12 - content type ptr as a content_type_e */ - uint8_t content_type_len; /* 0-2 bytes. */ - uint8_t *content_type_ptr; /* Must be set to NULL if not used */ + uint8_t content_type_len; /**< 0-2 bytes. */ + uint8_t *content_type_ptr; /**< Must be set to NULL if not used */ /* Here are not so often used Options */ - sn_coap_options_list_s *options_list_ptr; /* Must be set to NULL if not used */ + sn_coap_options_list_s *options_list_ptr; /**< Must be set to NULL if not used */ /* * * * * * * * * * * */ /* * * * Payload * * * */ /* * * * * * * * * * * */ - uint16_t payload_len; /* Must be set to zero if not used */ - uint8_t *payload_ptr; /* Must be set to NULL if not used */ + uint16_t payload_len; /**< Must be set to zero if not used */ + uint8_t *payload_ptr; /**< Must be set to NULL if not used */ } sn_coap_hdr_s; /* * * * * * * * * * * * * * * * * * * * * * */ /* * * * EXTERNAL FUNCTION PROTOTYPES * * * */ /* * * * * * * * * * * * * * * * * * * * * * */ +/** + * \brief Sets the memory allocation and deallocation functions the library will use, and must be called first. + */ extern void sn_coap_builder_and_parser_init(void* (*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void*)); + +/** + * \brief Parses an incoming message buffer to a CoAP header structure. + */ extern sn_coap_hdr_s *sn_coap_parser(uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr); + +/** + * \brief Releases any memory allocated by a CoAP message structure. + */ +extern void sn_coap_parser_release_allocated_coap_msg_mem(sn_coap_hdr_s *freed_coap_msg_ptr); + +/** + * \brief Builds an outgoing message buffer from a CoAP header structure. + */ extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr); + +/** + * \brief Calculates the needed message buffer size from a CoAP message structure. + */ extern uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr); -extern void sn_coap_builder_release_allocated_send_msg_mem(sn_nsdl_transmit_s *freed_send_msg_ptr); + +/** + * \brief Automates the building of a response to an incoming request. + */ extern sn_coap_hdr_s *sn_coap_build_response(sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code); -extern void sn_coap_parser_release_allocated_coap_msg_mem(sn_coap_hdr_s *freed_coap_msg_ptr); + +/** + * \brief CoAP packet debugging. + */ extern void sn_coap_packet_debug(sn_coap_hdr_s *coap_packet_ptr); #endif /* SN_COAP_HEADER_H_ */
--- a/sn_coap_protocol.h Wed Oct 09 09:20:29 2013 +0000 +++ b/sn_coap_protocol.h Mon Oct 14 11:00:30 2013 +0000 @@ -15,76 +15,67 @@ #ifndef SN_COAP_PROTOCOL_H_ #define SN_COAP_PROTOCOL_H_ -/* * * * * * * * * * * */ -/* * * * DEFINES * * * */ -/* * * * * * * * * * * */ - -/* Maximum time in seconds of messages to be stored for Acknowledging. This time tells */ -/* how long time User of CoAP C-library have time to send Piggy-backed acknowledgement */ -/* message to Request sender. */ -#define SN_COAP_ACK_INFO_MAX_TIME_MSGS_STORED 20 -#define SN_COAP_ACK_INFO_MAX_COUNT_MESSAGES_SAVED 10 - -/* * For Message resending * */ - -/* Init value for maximum count of ongoing active resending messages */ -/* This value depends on available memory: If there is restricted count of memory, use little value e.g. 1 */ -/* Setting of this value to 0 will disable re-sending and also reduce use of ROM memory */ -#define SN_COAP_RESENDING_MAX_COUNT 0 -/* Default value for re-sending buffer size */ -#define SN_COAP_RESENDING_BUFFER_MAX_SIZE 0 - -/* These parameters sets maximum values application can set with API */ -/* Maximum allowed count of re-sending */ -#define SN_COAP_MAX_ALLOWED_RESENDING_COUNT 6 -/* Maximum allowed number of saved re-sending messages */ -#define SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE 6 - -/* * For Message duplication detecting * */ - -/* Init value for the maximum count of messages to be stored for duplication detection */ -/* Setting of this value to 0 will disable duplication check, also reduce use of ROM memory */ -#define SN_COAP_DUPLICATION_MAX_MSGS_COUNT 0 -/* Maximum allowed number of saved messages for duplicate searching */ -#define SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT 6 - -/* Maximum time in seconds of messages to be stored for duplication detection */ -#define SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED 60 /* RESPONSE_TIMEOUT * RESPONSE_RANDOM_FACTOR * (2 ^ MAX_RETRANSMIT - 1) + the expected maximum round trip time */ - -/* * For Message blockwising * */ - -/* Init value for the maximum payload size to be sent and received at one blockwise message */ -/* Setting of this value to 0 will disable this feature, and also reduce use of ROM memory */ -/* Note: Current Coap implementation supports Blockwise transfers specification version draft-ietf-core-block-03 */ -/* Note: This define is common for both received and sent Blockwise messages */ -#ifndef SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE -#define SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE 0 /* Must be 2^x and x is at least 4. Suitable values: 0, 16, 32, 64, 128, 256, 512 and 1024 */ -#endif - - -/* Maximum time in seconds of data (messages and payload) to be stored for blockwising */ -#ifndef SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED -#define SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED 10 -#endif - /* * * * * * * * * * * * * * * * * * * * * * */ /* * * * EXTERNAL FUNCTION PROTOTYPES * * * */ /* * * * * * * * * * * * * * * * * * * * * * */ +/** + * \brief This function sets the memory allocation and deallocation functions and used TX callback function pointer the library will use, and must be called first. + */ extern int8_t sn_coap_protocol_init(void* (*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void*), uint8_t (*used_tx_callback_ptr)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *)); + +/** + * \brief Frees all allocated memory in libCoap protocol part. + */ extern int8_t sn_coap_protocol_destroy(void); + +/** + * \brief Use to build an outgoing message buffer from a CoAP header structure. + */ extern int16_t sn_coap_protocol_build(sn_nsdl_addr_s *dst_addr_ptr, uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr); + +/** + * \brief Use to parse an incoming message buffer to a CoAP header structure. + */ extern sn_coap_hdr_s *sn_coap_protocol_parse(sn_nsdl_addr_s *src_addr_ptr, uint16_t packet_data_len, uint8_t *packet_data_ptr); + +/** + * \brief Called periodically to allow the protocol to update retransmission timers and destroy unneeded data. + */ extern int8_t sn_coap_protocol_exec(uint32_t current_time); + +/** + * \brief If block transfer is enabled, this function changes the block size. + */ extern int8_t sn_coap_protocol_set_block_size(uint16_t block_size); + +/** + * \brief If dublicate message detection is enabled, this function changes buffer size. + */ extern int8_t sn_coap_protocol_set_duplicate_buffer_size(uint8_t message_count); + +/** + * \brief If re-transmissions are enabled, this function changes resending count and buffer size. + */ extern int8_t sn_coap_protocol_set_retransmission(uint8_t resending_count, uint8_t buffer_size); -/* NSP registration functions */ +/* NSP manual registration functions */ + +/** + * \brief Create an NSP registration message. + */ extern int8_t sn_coap_register(sn_coap_hdr_s *coap_hdr_ptr, registration_info_t *endpoint_info_ptr); + +/** + * \brief Create an NSP update message. + */ extern int8_t sn_coap_register_update(sn_coap_hdr_s *coap_hdr_ptr, uint8_t *location, uint8_t length); + +/** + * \brief Create an NSP de-registration message. + */ extern int8_t sn_coap_deregister(sn_coap_hdr_s *coap_hdr_ptr, uint8_t *location, uint8_t length); #endif /* SN_COAP_PROTOCOL_H_ */
--- a/sn_nsdl.h Wed Oct 09 09:20:29 2013 +0000 +++ b/sn_nsdl.h Mon Oct 14 11:00:30 2013 +0000 @@ -26,7 +26,7 @@ #endif #ifndef SN_NSDL_HAVE_HTTPS_CAPABILITY -#define SN_NSDL_HAVE_HTTPS_CAPABILITY 0 +#define SN_NSDL_HAVE_HTTPS_CAPABILITY 0 #endif #ifndef SN_NSDL_HAVE_COAP_CAPABILITY @@ -43,32 +43,35 @@ /* * * * * * * * * * * * * * */ +/** + * \brief Used protocol + */ typedef enum sn_nsdl_capab_ { - SN_NSDL_PROTOCOL_HTTP = 0x01, - SN_NSDL_PROTOCOL_HTTPS = 0x02, - SN_NSDL_PROTOCOL_COAP = 0x04 + SN_NSDL_PROTOCOL_HTTP = 0x01, /**< Unsupported */ + SN_NSDL_PROTOCOL_HTTPS = 0x02, /**< Unsupported */ + SN_NSDL_PROTOCOL_COAP = 0x04 /**< Supported */ } sn_nsdl_capab_e; +/** + * \brief Address type of given address + */ typedef enum sn_nsdl_addr_type_ { - SN_NSDL_ADDRESS_TYPE_IPV6 = 0x01, - SN_NSDL_ADDRESS_TYPE_IPV4 = 0x02, - SN_NSDL_ADDRESS_TYPE_HOSTNAME = 0x03, + SN_NSDL_ADDRESS_TYPE_IPV6 = 0x01, /**< Supported */ + SN_NSDL_ADDRESS_TYPE_IPV4 = 0x02, /**< Supported */ + SN_NSDL_ADDRESS_TYPE_HOSTNAME = 0x03, /**< Unsupported */ SN_NSDL_ADDRESS_TYPE_NONE = 0xFF } sn_nsdl_addr_type_e; -#define SN_NDSL_RESOURCE_NOT_REGISTERED 0 -#define SN_NDSL_RESOURCE_REGISTERING 1 -#define SN_NDSL_RESOURCE_REGISTERED 2 - - /* * * * * * * * * * * * * */ /* * * * STRUCTURES * * * */ /* * * * * * * * * * * * * */ -/* Address structure of Packet data */ +/** + * \brief Address structure of Packet data + */ typedef struct sn_nsdl_addr_ { sn_nsdl_addr_type_e type; @@ -82,32 +85,24 @@ } sn_nsdl_addr_s; -/* This structure is returned by sn_coap_exec() for sending */ -typedef struct sn_nsdl_transmit_ -{ - sn_nsdl_addr_s *dst_addr_ptr; - - sn_nsdl_capab_e protocol; - - uint16_t packet_len; - uint8_t *packet_ptr; -} sn_nsdl_transmit_s; - +/** + * \brief Used for creating manually NSP registration message with sn_coap_register() + */ typedef struct registration_info_ { - uint8_t *endpoint_ptr; + uint8_t *endpoint_ptr; /**< Endpoint name */ uint8_t endpoint_len; - uint8_t *endpoint_type_ptr; + uint8_t *endpoint_type_ptr; /**< Endpoint type */ uint8_t endpoint_type_len; - uint8_t *links_ptr; + uint8_t *links_ptr; /**< Resource registration string */ uint16_t links_len; -}registration_info_t; +} registration_info_t; #endif /* SN_NSDL_H_ */ #ifdef __cplusplus } -#endif \ No newline at end of file +#endif
--- a/sn_nsdl_lib.h Wed Oct 09 09:20:29 2013 +0000 +++ b/sn_nsdl_lib.h Mon Oct 14 11:00:30 2013 +0000 @@ -1,124 +1,109 @@ +/** + * \file sn_nsdl_lib.h + * + * \brief NanoService Devices Library header file + * + * Created on: Aug 23, 2011 + * Author: tero + * + */ + #ifdef __cplusplus extern "C" { #endif #define SN_NSDL_CONST_MEMORY_ATTRIBUTE -#define RESOURCE_DIR_LEN 2 -#define RESOURCE_DIR_PATH {'r','d'} - -#define EP_NAME_PARAMETERS_LEN 2 -#define EP_NAME_PARAMETERS {'h','='} - -#define RT_PARAMETER_LEN 3 -#define RT_PARAMETER {'r','t','='} - -#define IF_PARAMETER_LEN 3 -#define IF_PARAMETER {'i','f','='} - -#define CON_PARAMETER_LEN 4 -#define CON_PARAMETER {'c','o','n','='} - -#define LT_PARAMETER_LEN 3 -#define LT_PARAMETER {'l','t','='} - -#define OBS_PARAMETER_LEN 3 -#define OBS_PARAMETER {'o','b','s'} - -#define COAP_CON_PARAMETER_LEN 3 -#define COAP_CON_PARAMETER {'c','t','='} - -#define EVENT_PATH_LEN 6 -#define EVENT_PATH {'e','v','e','n','t','/'} - -#define WELLKNOWN_PATH_LEN 16 -#define WELLKNOWN_PATH (".well-known/core") - -#define SN_NSDL_EP_REGISTER_MESSAGE 1 -#define SN_NSDL_EP_UPDATE_MESSAGE 2 - -#define SN_NSDL_MSG_NO_TYPE 0 -#define SN_NSDL_MSG_REGISTER 1 -#define SN_NSDL_MSG_UNREGISTER 2 -#define SN_NSDL_MSG_UPDATE 3 -#define SN_NSDL_MSG_EVENT 4 - -#define SN_NSDL_MAX_MESSAGE_COUNT 1 - #define SN_NSDL_ENDPOINT_NOT_REGISTERED 0 #define SN_NSDL_ENDPOINT_IS_REGISTERED 1 +/** + * \brief Endpoint registration parameters + */ typedef struct sn_nsdl_ep_parameters_ { - uint8_t *endpoint_name_ptr; + uint8_t *endpoint_name_ptr; /**< Endpoint name */ uint8_t endpoint_name_len; - uint8_t *domain_name_ptr; + uint8_t *domain_name_ptr; /**< Domain to register. If null, NSP uses default domain */ uint8_t domain_name_len; - uint8_t *type_ptr; + uint8_t *type_ptr; /**< Endpoint type */ uint8_t type_len; - uint8_t *contex_ptr; - uint8_t contex_len; - - uint8_t *lifetime_ptr; + uint8_t *lifetime_ptr; /**< Endpoint lifetime in seconds. eg. "1200" = 1200 seconds */ uint8_t lifetime_len; } sn_nsdl_ep_parameters_s; +/** + * \brief For internal use + */ typedef struct sn_nsdl_sent_messages_ { uint16_t msg_id_number; uint8_t message_type; -}sn_nsdl_sent_messages_s; +} sn_nsdl_sent_messages_s; +/** + * \brief Function pointers used for memory allocation and freeing + */ typedef struct sn_nsdl_mem_ { void *(*sn_nsdl_alloc)(uint16_t); void (*sn_nsdl_free)(void *); -}sn_nsdl_mem_s; +} sn_nsdl_mem_s; +/** + * \brief Includes resource path + */ typedef struct sn_grs_resource_ { uint8_t pathlen; uint8_t *path; -}sn_grs_resource_s; +} sn_grs_resource_s; +/** + * \brief Table of created resources + */ typedef struct sn_grs_resource_list_ { - uint8_t res_count; /* Number of resources */ + uint8_t res_count; /**< Number of resources */ sn_grs_resource_s *res; -}sn_grs_resource_list_s; +} sn_grs_resource_list_s; -typedef enum sn_nsdl_resource_mode_ -{ - SN_GRS_STATIC, - SN_GRS_DYNAMIC, - SN_GRS_DIRECTORY -}sn_nsdl_resource_mode_e; - -typedef enum sn_grs_resource_mutable_ -{ - SN_GRS_GET = 0x01, - SN_GRS_POST = 0x02, - SN_GRS_PUT = 0x04, - SN_GRS_DELETE = 0x08 -}sn_grs_resource_mutable_e; - +/** + * \brief Resource access rights + */ typedef enum sn_grs_resource_acl_ { SN_GRS_GET_ALLOWED = 0x01 , SN_GRS_PUT_ALLOWED = 0x02, SN_GRS_POST_ALLOWED = 0x04, SN_GRS_DELETE_ALLOWED = 0x08 -}sn_grs_resource_acl_e; +} sn_grs_resource_acl_e; +/** + * \brief Used protocol + */ typedef struct sn_proto_info_ { - sn_nsdl_capab_e proto; -}sn_proto_info_s; + sn_nsdl_capab_e proto; /**< Only COAP is supported */ +} sn_proto_info_s; +/** + * \brief Defines the resource mode + */ +typedef enum sn_nsdl_resource_mode_ +{ + SN_GRS_STATIC, /**< Static resources have some value that doesn't change */ + SN_GRS_DYNAMIC, /**< Dynamic resources are handled in application. Therefore one must give function callback pointer to them */ + SN_GRS_DIRECTORY /**< Directory resources are unused and unsupported */ +} sn_nsdl_resource_mode_e; + +/** + * \brief Resource registration parameters + */ typedef struct sn_nsdl_resource_parameters_ { uint8_t *resource_type_ptr; @@ -137,17 +122,20 @@ }sn_nsdl_resource_parameters_s; +/** + * \brief Defines parameters for the resource. + */ typedef struct sn_nsdl_resource_info_ { sn_nsdl_resource_parameters_s *resource_parameters_ptr; - sn_nsdl_resource_mode_e mode; // STATIC etc.. + sn_nsdl_resource_mode_e mode; /**< STATIC etc.. */ - uint16_t pathlen; // Address + uint16_t pathlen; /**< Address */ uint8_t *path; - uint16_t resourcelen; // 0 if dynamic resource, resource information in static resource - uint8_t *resource; // NULL if dynamic resource + uint16_t resourcelen; /**< 0 if dynamic resource, resource information in static resource */ + uint8_t *resource; /**< NULL if dynamic resource */ sn_grs_resource_acl_e access; @@ -160,7 +148,6 @@ * uint8_t (*sn_nsdl_rx_cb)(sn_coap_hdr_s *, sn_nsdl_addr_s *), * sn_nsdl_mem_s *sn_memory) * - * * \brief Initialization function for NSDL library. Initializes NSDL, GRS, HTTP and CoAP. * * \param *sn_nsdl_tx_callback A callback function for sending messages. @@ -170,97 +157,274 @@ * * \param *sn_memory Memory structure which includes function pointers to the allocation and free functions. * - * \return SN_NSDL_SUCCESS = 0, Failed = -1 + * \return 0 Success + * \return -1 Failure */ -int8_t sn_nsdl_init (uint8_t (*sn_nsdl_tx_cb)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *), +extern int8_t sn_nsdl_init(uint8_t (*sn_nsdl_tx_cb)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *), uint8_t (*sn_nsdl_rx_cb)(sn_coap_hdr_s *, sn_nsdl_addr_s *), sn_nsdl_mem_s *sn_memory); /** * \fn extern uint8_t sn_nsdl_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_info_ptr) * - * * \brief Registers endpoint to NSP server. * * \param *endpoint_info_ptr Contains endpoint information. * - * \return SN_NSDL_SUCCESS = 0, Failed = -1 + * \return 0 Success + * \return -1 Failure */ -int8_t sn_nsdl_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_info_ptr); +extern int8_t sn_nsdl_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_info_ptr); /** * \fn extern int8_t sn_nsdl_unregister_endpoint(void) * - * * \brief Sends unregister-message to NSP server. * - * \return SN_NSDL_SUCCESS = 0, Failed = -1 + * \return 0 Success + * \return -1 Failure */ -int8_t sn_nsdl_unregister_endpoint(void); -int8_t sn_nsdl_update_registration (sn_nsdl_ep_parameters_s *endpoint_parameters_ptr); -int8_t sn_nsdl_send_eventing_message (uint8_t *event_name_ptr, uint16_t event_name_len, uint8_t *message_body_ptr, uint16_t message_body_len); +extern int8_t sn_nsdl_unregister_endpoint(void); + +/** + * \fn extern int8_t sn_nsdl_update_registration(sn_nsdl_ep_parameters_s *endpoint_parameters_ptr); + * + * \brief Update the registration with NSP. + * + * \param *endpoint_info_ptr Contains endpoint information. + * + * \return 0 Success + * \return -1 Failure + */ +extern int8_t sn_nsdl_update_registration(sn_nsdl_ep_parameters_s *endpoint_parameters_ptr); /** * \fn extern int8_t sn_nsdl_is_ep_registered(void) * - * * \brief Checks if endpoint is registered. * - * \return 1 if endpointi registration is done SN_NSDL_SUCCESSfully, 0 if endpoint is not registered + * \return 1 Endpoint registration is done successfully + * \return 0 Endpoint is not registered */ -int8_t sn_nsdl_is_ep_registered(void); +extern int8_t sn_nsdl_is_ep_registered(void); -/* - * A function to inform NSDL-C library if application detects a fault in NSP registration. After calling this function - * , sn_nsdl_is_ep_registered() will return "not registered". +/** + * \fn extern void sn_nsdl_nsp_lost(void); + * + * \brief A function to inform NSDL-C library if application detects a fault in NSP registration. + * + * After calling this function sn_nsdl_is_ep_registered() will return "not registered". */ -void sn_nsdl_nsp_lost(void); +extern void sn_nsdl_nsp_lost(void); + /** - * \fn extern int8_t sn_nsdl_send_observation_notification(uint8_t *token_ptr, uint8_t token_len, - * uint8_t *payload_ptr, uint16_t payload_len, - * uint8_t *observe_ptr, uint8_t observe_len) + * \fn extern uint16_t sn_nsdl_send_observation_notification(uint8_t *token_ptr, uint8_t token_len, + * uint8_t *payload_ptr, uint16_t payload_len, + * uint8_t *observe_ptr, uint8_t observe_len, + * sn_coap_msg_type_e message_type, uint8_t content_type) * * * \brief Sends observation message to NSP server * - * \param *token_ptr Pointer to token to be used - * \param token_len Token length - * \param *payload_ptr Pointer to payload to be sent - * \param payload_len Payload length - * \param *observe_ptr Pointer to observe number to be sent - * \param observe_len Observe number len + * \param *token_ptr Pointer to token to be used + * \param token_len Token length + * \param *payload_ptr Pointer to payload to be sent + * \param payload_len Payload length + * \param *observe_ptr Pointer to observe number to be sent + * \param observe_len Observe number len + * \param message_type Observation message type (confirmable or non-confirmable) + * \param contetnt_type Observation message payload contetnt type * - * \return SN_NSDL_SUCCESS = 0, Failed = -1 + * \return !0 Success, observation messages message ID + * \return 0 Failure + */ +extern uint16_t sn_nsdl_send_observation_notification(uint8_t *token_ptr, uint8_t token_len, + uint8_t *payload_ptr, uint16_t payload_len, + uint8_t *observe_ptr, uint8_t observe_len, + sn_coap_msg_type_e message_type, uint8_t content_type); + +/** + * \fn extern int16_t sn_nsdl_get_capability(void) + * + * \brief Capability query function. + * + * Used to retrieve the list of supported protocols from the NSDL module. + * + * \return >0 Success, supported capabilities reported using bitmask with definitions from sn_nsdl_capab_t + * \return 0 Success, no supported capabilities */ -extern uint16_t sn_nsdl_send_observation_notification(uint8_t *token_ptr, uint8_t token_len, uint8_t *payload_ptr, uint16_t payload_len, uint8_t *observe_ptr, uint8_t observe_len, sn_coap_msg_type_e message_type, uint8_t content_type); -int16_t sn_nsdl_get_capability(void); -uint32_t sn_nsdl_get_version(void); -int8_t sn_nsdl_process_http(uint8_t *packet, uint16_t *packet_len, sn_nsdl_addr_s *src); -int8_t sn_nsdl_process_coap(uint8_t *packet, uint16_t packet_len, sn_nsdl_addr_s *src); -int8_t sn_nsdl_exec(uint32_t time); -int8_t sn_nsdl_create_resource(sn_nsdl_resource_info_s *res); -int8_t sn_nsdl_update_resource(sn_nsdl_resource_info_s *res); -int8_t sn_nsdl_delete_resource(uint8_t pathlen, uint8_t *path); -sn_nsdl_resource_info_s *sn_nsdl_get_resource(uint8_t pathlen, uint8_t *path); -sn_grs_resource_list_s *sn_nsdl_list_resource(uint8_t pathlen, uint8_t *path); -int8_t sn_nsdl_send_coap_message(sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr); +extern int16_t sn_nsdl_get_capability(void); + +/** + * \fn extern uint32_t sn_nsdl_get_version(void) + * + * \brief Version query function. + * + * Used to retrieve the version information structure from the NSDL library. + * + * \return !0 MSB 2 bytes major version, LSB 2 bytes minor version. + * \return 0 Failure +*/ +extern uint32_t sn_nsdl_get_version(void); + +/** + * \fn extern int8_t sn_nsdl_process_http(uint8_t *packet, uint16_t *packet_len, sn_nsdl_addr_s *src) + * + * \brief Currently HTTP is not supported + * + * \return -1 Failure + */ +extern int8_t sn_nsdl_process_http(uint8_t *packet, uint16_t *packet_len, sn_nsdl_addr_s *src); + /** - * \brief This function is used to set the NSP address given by an application. - * @return 0 on success, -1 on false to indicate that NSDL internal address pointer is not allocated (call nsdl_init() first). + * \fn extern int8_t sn_nsdl_process_coap(uint8_t *packet, uint16_t packet_len, sn_nsdl_addr_s *src) + * + * \brief To push CoAP packet to NSDL library + * + * Used to push an CoAP packet to NSDL library for processing. + * + * \param *packet Pointer to a uint8_t array containing the packet (including the CoAP headers). + * After successful execution this array may contain the response packet. + * + * \param *packet_len Pointer to length of the packet. After successful execution this array may contain the length + * of the response packet. + * + * \param *src Pointer to packet source address information. After successful execution this array may contain + * the destination address of the response packet. + * + * \return 0 Success + * \return -1 Failure */ -int8_t set_NSP_address(uint8_t *NSP_address, uint16_t port, sn_nsdl_addr_type_e address_type); +extern int8_t sn_nsdl_process_coap(uint8_t *packet, uint16_t packet_len, sn_nsdl_addr_s *src); + +/** + * \fn extern int8_t sn_nsdl_exec(uint32_t time); + * + * \brief CoAP retransmission function. + * + * Used to give execution time for the NSDL (CoAP) library for retransmissions. The NSDL library + * will call the exec functions of all enabled protocol modules. + * + * \param time Time in seconds. + * + * \return 0 Success + * \return -1 Failure + */ +extern int8_t sn_nsdl_exec(uint32_t time); /** + * \fn extern int8_t sn_nsdl_create_resource(sn_nsdl_resource_info_s *res) + * + * \brief Resource creating function. + * + * Used to create a static or dynamic HTTP(S) or CoAP resource. + * + * \param *res Pointer to a structure of type sn_nsdl_resource_info_t that contains the information + * about the resource. + * + * \return 0 Success + * \return -1 Failure + * \return -2 Resource already exists + * \return -3 Invalid path + * \return -4 List adding failure + */ +extern int8_t sn_nsdl_create_resource(sn_nsdl_resource_info_s *res); + +/** + * \fn extern int8_t sn_nsdl_update_resource(sn_nsdl_resource_info_s *res) + * + * \brief Resource updating function. + * + * Used to update the direct value of a static resource, the callback function pointer of a dynamic resource + * and access rights of the recource. + * + * \param *res Pointer to a structure of type sn_nsdl_resource_info_t that contains the information + * about the resource. Only the pathlen and path elements are evaluated along with + * either resourcelen and resource or the function pointer. + * + * \return 0 Success + * \return -1 Failure + */ +extern int8_t sn_nsdl_update_resource(sn_nsdl_resource_info_s *res); + +/** + * \fn extern int8_t sn_nsdl_delete_resource(uint8_t pathlen, uint8_t *path) + * + * \brief Resource delete function. + * + * Used to delete a resource. If resource has a subresources, these all must also be removed. + * + * \param pathlen Contains the length of the path that is to be deleted (excluding possible trailing "\0"). + * + * \param *path_ptr A pointer to an array containing the path. + * + * \return 0 Success + * \return -1 Failure (No such resource) + */ +extern int8_t sn_nsdl_delete_resource(uint8_t pathlen, uint8_t *path); + +/** + * \fn extern sn_nsdl_resource_info_s *sn_nsdl_get_resource(uint16_t pathlen, uint8_t *path) + * + * \brief Resource get function. + * + * Used to get a resource. + * + * \param pathlen Contains the length of the path that is to be returned (excluding possible trailing '\0'). + * + * \param *path A pointer to an array containing the path. + * + * \return !NULL Success, pointer to a sn_nsdl_resource_info_s that contains the resource information\n + * \return NULL Failure + */ +extern sn_nsdl_resource_info_s *sn_nsdl_get_resource(uint16_t pathlen, uint8_t *path); + +/** + * \fn extern sn_grs_resource_list_s *sn_nsdl_list_resource(uint16_t pathlen, uint8_t *path) + * + * \brief Resource list function. + * + * \param pathlen Contains the length of the target path (excluding possible trailing '\0'). + * The length value is not examined if the path itself is a NULL pointer. + * + * \param *path A pointer to an array containing the path or a NULL pointer. + * + * \return !NULL A pointer to a sn_grs_resource_list_s structure containing the resource listing. + * \return NULL Failure with an unspecified error + */ +extern sn_grs_resource_list_s *sn_nsdl_list_resource(uint16_t pathlen, uint8_t *path); + +/** + * \fn extern int8_t sn_nsdl_send_coap_message(sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr); + * + * \brief Send an outgoing CoAP request. + * + * \param *address_ptr Pointer to source address struct + * + * \param *coap_hdr_ptr Pointer to CoAP message to be sent + * + * \return 0 Success + * \return -1 Failure + */ +extern int8_t sn_nsdl_send_coap_message(sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr); + +/** + * \fn extern int8_t set_NSP_address(uint8_t *NSP_address, uint16_t port, sn_nsdl_addr_type_e address_type); + * + * \brief This function is used to set the NSP address given by an application. + * + * \return 0 Success + * \return -1 Failed to indicate that NSDL internal address pointer is not allocated (call nsdl_init() first). + */ +extern int8_t set_NSP_address(uint8_t *NSP_address, uint16_t port, sn_nsdl_addr_type_e address_type); + +/** + * \fn extern int8_t sn_nsdl_destroy(void); + * * \brief This function releases all allocated memory in nsdl and grs modules. */ extern int8_t sn_nsdl_destroy(void); -/* - * \brief A function to request SN internal version information out of NSDL library in case of "error reporting" or similar. - * @return A string with \0 in the end. A human readable format. Please deliver this item to Sensinode in case if you're to report of errors. - */ -//extern const char __code * sn_nsdl_get_library_version_info(void); - #ifdef __cplusplus } #endif