NSDL C library

Dependents:   NSDL_HelloWorld_WiFi UbloxModemNanoServiceClient IOT-NSDL_HelloWorld LWM2M_NanoService_Ethernet ... more

Fork of nsdl_lib by Tero Heinonen

Note that use of this software requires acceptance of the Sensinode EULA: http://mbed.org/teams/Sensinode/code/nsdl_lib/wiki/EULA

sn_nsdl_lib.h

Committer:
screamer
Date:
2013-10-10
Revision:
3:d8cb1afd7285
Parent:
2:ab50a2ab6ec9
Child:
4:0f845ba8adff

File content as of revision 3:d8cb1afd7285:

/**
 * \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;		/**< Endpoint name */
	uint8_t  	endpoint_name_len;

	uint8_t		*domain_name_ptr;		/**< Domain to register. If null, NSP uses default domain */
	uint8_t		domain_name_len;

	uint8_t 	*type_ptr;				/**< Endpoint type */
	uint8_t 	type_len;

	uint8_t 	*contex_ptr;
	uint8_t 	contex_len;

	uint8_t		*lifetime_ptr;			/**< Endpoint lifetime in seconds. eg. "1200" = 1200 seconds */
	uint8_t		lifetime_len;

} sn_nsdl_ep_parameters_s;

typedef struct sn_nsdl_sent_messages_
{
	uint16_t	msg_id_number;
	uint8_t		message_type;
} sn_nsdl_sent_messages_s;

typedef struct sn_nsdl_mem_
{
	void *(*sn_nsdl_alloc)(uint16_t);
	void (*sn_nsdl_free)(void *);
} sn_nsdl_mem_s;

typedef struct sn_grs_resource_
{
	uint8_t pathlen;
	uint8_t *path;
} sn_grs_resource_s;

typedef struct sn_grs_resource_list_
{
	uint8_t res_count;					/**< Number of resources */
	sn_grs_resource_s *res;
} sn_grs_resource_list_s;

/**
 * \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;

typedef struct sn_proto_info_
{
	sn_nsdl_capab_e proto;
} sn_proto_info_s;

/**
 * \brief Defines the resource mode
 */
typedef enum sn_nsdl_resource_mode_
{
	SN_GRS_STATIC,						/**< Static resources have some value that does not 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;
	uint16_t	resource_type_len;

	uint8_t		*interface_description_ptr;
	uint16_t	interface_description_len;

	uint8_t		coap_content_type;

	uint8_t		mime_content_type;

	uint8_t		observable;

	uint8_t		registered;

}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.. */

	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 */

	sn_grs_resource_acl_e 			access;

	uint8_t (*sn_grs_dyn_res_callback)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *);

} sn_nsdl_resource_info_s;

/**
 * \fn 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)
 *
 * \brief Initialization function for NSDL library. Initializes NSDL, GRS, HTTP and CoAP.
 *
 * \param *sn_nsdl_tx_callback 	A callback function for sending messages.
 *
 * \param *sn_nsdl_rx_callback 	A callback function for parsed messages. If received message is not CoAP protocol message (eg. ACK), message for GRS (GET, PUT, POST, DELETE) or
 * 								reply for some NSDL message (register message etc.), rx callback will be called.
 *
 * \param *sn_memory			Memory structure which includes function pointers to the allocation and free functions.
 *
 * \return Success = 0
 * \return Failed = -1
 */
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 Success = 0
 * \return Failed = -1
 */
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 Success = 0
 * \return Failed = -1
 */
int8_t sn_nsdl_unregister_endpoint(void);

/**
 * Update the registration with NSP.
 */
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);

/**
 * \fn extern int8_t sn_nsdl_is_ep_registered(void)
 *
 * \brief Checks if endpoint is registered.
 *
 * \return 1 if endpointi registration is done successfully
 * \return 0 if endpoint is not registered
 */
int8_t sn_nsdl_is_ep_registered(void);

/**
 * \fn 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);

/**
 * \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)
 *
 *
 * \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
 *
 * \return Success = 0
 * \return Failed = -1
 */
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);

/**
 * \brief Process an incoming CoAP message.
 */
int8_t sn_nsdl_process_coap(uint8_t *packet, uint16_t packet_len, sn_nsdl_addr_s *src);

/**
 * \brief Must be called periodically for the library to update retransmissions.
 */
int8_t sn_nsdl_exec(uint32_t time);

/**
 * \brief Adds a resource to the server.
 */
int8_t sn_nsdl_create_resource(sn_nsdl_resource_info_s *res);

/**
 * \brief Updates a resource on the server.
 */
int8_t sn_nsdl_update_resource(sn_nsdl_resource_info_s *res);

/**
 * \brief Removes a resource from the resource server.
 */
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);

/**
 * \brief Send an outgoing CoAP request.
 */
int8_t sn_nsdl_send_coap_message(sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr);

/**
 * \fn 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 on success,
 * \return -1 on false to indicate that NSDL internal address pointer is not allocated (call nsdl_init() first).
 */
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);

/*
 * \fn extern const char __code * sn_nsdl_get_library_version_info(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