Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: wakaama/liblwm2m.h
- Revision:
- 3:a280069151ac
- Parent:
- 0:f9d13e09cf11
- Child:
- 10:df97539c6ddd
--- a/wakaama/liblwm2m.h Fri Apr 28 09:53:26 2017 +0000 +++ b/wakaama/liblwm2m.h Fri Apr 28 18:13:27 2017 +0800 @@ -16,6 +16,8 @@ * Simon Bernard - Please refer to git log * Toby Jaffey - Please refer to git log * Julien Vermillard - Please refer to git log + * Bosch Software Innovations GmbH - Please refer to git log + * Pascal Rieux - Please refer to git log *******************************************************************************/ /* @@ -51,15 +53,18 @@ #ifndef _LWM2M_CLIENT_H_ #define _LWM2M_CLIENT_H_ +#ifdef __cplusplus +extern "C" { +#endif + #include <stdint.h> #include <stddef.h> #include <stdbool.h> +#include <time.h> + #include <stdlib.h> #include <string.h> -#include <time.h> -//#include <sys/time.h> -//#include <EthernetInterface/lwip/include/lwip/sockets.h> ////////////////////////////////////////////////// #define MBED_OS_EXAMPLE_WAKAAMA @@ -99,64 +104,147 @@ void lwm2m_free(void *p); #endif + +#ifdef LWM2M_SERVER_MODE +#ifndef LWM2M_SUPPORT_JSON +#define LWM2M_SUPPORT_JSON +#endif +#endif + +#if defined(LWM2M_BOOTSTRAP) && defined(LWM2M_BOOTSTRAP_SERVER_MODE) +#error "LWM2M_BOOTSTRAP and LWM2M_BOOTSTRAP_SERVER_MODE cannot be defined at the same time!" +#endif + +/* + * Platform abstraction functions to be implemented by the user + */ + +#ifndef LWM2M_MEMORY_TRACE +// Allocate a block of size bytes of memory, returning a pointer to the beginning of the block. +void * lwm2m_malloc(size_t s); +// Deallocate a block of memory previously allocated by lwm2m_malloc() or lwm2m_strdup() +void lwm2m_free(void * p); +// Allocate a memory block, duplicate the string str in it and return a pointer to this new block. +char * lwm2m_strdup(const char * str); +#else +// same functions as above with caller location for debugging purposes +char * lwm2m_trace_strdup(const char * str, const char * file, const char * function, int lineno); +void * lwm2m_trace_malloc(size_t size, const char * file, const char * function, int lineno); +void lwm2m_trace_free(void * mem, const char * file, const char * function, int lineno); + +#define lwm2m_strdup(S) lwm2m_trace_strdup(S, __FILE__, __FUNCTION__, __LINE__) +#define lwm2m_malloc(S) lwm2m_trace_malloc(S, __FILE__, __FUNCTION__, __LINE__) +#define lwm2m_free(M) lwm2m_trace_free(M, __FILE__, __FUNCTION__, __LINE__) +#endif +// Compare at most the n first bytes of s1 and s2, return 0 if they match +int lwm2m_strncmp(const char * s1, const char * s2, size_t n); +// This function must return the number of seconds elapsed since origin. +// The origin (Epoch, system boot, etc...) does not matter as this +// function is used only to determine the elapsed time since the last +// call to it. +// In case of error, this must return a negative value. +// Per POSIX specifications, time_t is a signed integer. +time_t lwm2m_gettime(void); + +#ifdef LWM2M_WITH_LOGS +// Same usage as C89 printf() +void lwm2m_printf(const char * format, ...); +#endif + +// communication layer +#ifdef LWM2M_CLIENT_MODE +// Returns a session handle that MUST uniquely identify a peer. +// secObjInstID: ID of the Securty Object instance to open a connection to +// userData: parameter to lwm2m_init() +void * lwm2m_connect_server(uint16_t secObjInstID, void * userData); +// Close a session created by lwm2m_connect_server() +// sessionH: session handle identifying the peer (opaque to the core) +// userData: parameter to lwm2m_init() +void lwm2m_close_connection(void * sessionH, void * userData); +#endif +// Send data to a peer +// Returns COAP_NO_ERROR or a COAP_NNN error code +// sessionH: session handle identifying the peer (opaque to the core) +// buffer, length: data to send +// userData: parameter to lwm2m_init() +uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void * userData); +// Compare two session handles +// Returns true if the two sessions identify the same peer. false otherwise. +// userData: parameter to lwm2m_init() +bool lwm2m_session_is_equal(void * session1, void * session2, void * userData); + /* * Error code */ -#if 0 //To be sync with enum. -typedef enum { - NO_ERROR = 0, - - CREATED_2_01 = 65, /* CREATED */ - DELETED_2_02 = 66, /* DELETED */ - VALID_2_03 = 67, /* NOT_MODIFIED */ - CHANGED_2_04 = 68, /* CHANGED */ - CONTENT_2_05 = 69, /* OK */ - - BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */ - UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */ - BAD_OPTION_4_02 = 130, /* BAD_OPTION */ - FORBIDDEN_4_03 = 131, /* FORBIDDEN */ - NOT_FOUND_4_04 = 132, /* NOT_FOUND */ - METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */ - NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */ - PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */ - REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */ - UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */ - - INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */ - NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */ - BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */ - SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */ - GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */ - PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */ - - /* Erbium errors */ - MEMORY_ALLOCATION_ERROR = 192, - PACKET_SERIALIZATION_ERROR, - - /* Erbium hooks */ - MANUAL_RESPONSE - - -#endif - - #define COAP_NO_ERROR (uint8_t)0x00 +#define COAP_IGNORE (uint8_t)0x01 #define COAP_201_CREATED (uint8_t)0x41 #define COAP_202_DELETED (uint8_t)0x42 #define COAP_204_CHANGED (uint8_t)0x44 #define COAP_205_CONTENT (uint8_t)0x45 +#define COAP_231_CONTINUE (uint8_t)0x5F #define COAP_400_BAD_REQUEST (uint8_t)0x80 #define COAP_401_UNAUTHORIZED (uint8_t)0x81 +#define COAP_402_BAD_OPTION (uint8_t)0x82 #define COAP_404_NOT_FOUND (uint8_t)0x84 #define COAP_405_METHOD_NOT_ALLOWED (uint8_t)0x85 #define COAP_406_NOT_ACCEPTABLE (uint8_t)0x86 +#define COAP_408_REQ_ENTITY_INCOMPLETE (uint8_t)0x88 +#define COAP_412_PRECONDITION_FAILED (uint8_t)0x8C +#define COAP_413_ENTITY_TOO_LARGE (uint8_t)0x8D #define COAP_500_INTERNAL_SERVER_ERROR (uint8_t)0xA0 #define COAP_501_NOT_IMPLEMENTED (uint8_t)0xA1 #define COAP_503_SERVICE_UNAVAILABLE (uint8_t)0xA3 +/* + * Standard Object IDs + */ +#define LWM2M_SECURITY_OBJECT_ID 0 +#define LWM2M_SERVER_OBJECT_ID 1 +#define LWM2M_ACL_OBJECT_ID 2 +#define LWM2M_DEVICE_OBJECT_ID 3 +#define LWM2M_CONN_MONITOR_OBJECT_ID 4 +#define LWM2M_FIRMWARE_UPDATE_OBJECT_ID 5 +#define LWM2M_LOCATION_OBJECT_ID 6 +#define LWM2M_CONN_STATS_OBJECT_ID 7 + +/* + * Ressource IDs for the LWM2M Security Object + */ +#define LWM2M_SECURITY_URI_ID 0 +#define LWM2M_SECURITY_BOOTSTRAP_ID 1 +#define LWM2M_SECURITY_SECURITY_ID 2 +#define LWM2M_SECURITY_PUBLIC_KEY_ID 3 +#define LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID 4 +#define LWM2M_SECURITY_SECRET_KEY_ID 5 +#define LWM2M_SECURITY_SMS_SECURITY_ID 6 +#define LWM2M_SECURITY_SMS_KEY_PARAM_ID 7 +#define LWM2M_SECURITY_SMS_SECRET_KEY_ID 8 +#define LWM2M_SECURITY_SMS_SERVER_NUMBER_ID 9 +#define LWM2M_SECURITY_SHORT_SERVER_ID 10 +#define LWM2M_SECURITY_HOLD_OFF_ID 11 +#define LWM2M_SECURITY_BOOTSTRAP_TIMEOUT_ID 12 + +/* + * Ressource IDs for the LWM2M Server Object + */ +#define LWM2M_SERVER_SHORT_ID_ID 0 +#define LWM2M_SERVER_LIFETIME_ID 1 +#define LWM2M_SERVER_MIN_PERIOD_ID 2 +#define LWM2M_SERVER_MAX_PERIOD_ID 3 +#define LWM2M_SERVER_DISABLE_ID 4 +#define LWM2M_SERVER_TIMEOUT_ID 5 +#define LWM2M_SERVER_STORING_ID 6 +#define LWM2M_SERVER_BINDING_ID 7 +#define LWM2M_SERVER_UPDATE_ID 8 + +#define LWM2M_SECURITY_MODE_PRE_SHARED_KEY 0 +#define LWM2M_SECURITY_MODE_RAW_PUBLIC_KEY 1 +#define LWM2M_SECURITY_MODE_CERTIFICATE 2 +#define LWM2M_SECURITY_MODE_NONE 3 + /* * Utility functions for sorted linked list @@ -177,95 +265,20 @@ lwm2m_list_t * lwm2m_list_remove(lwm2m_list_t * head, uint16_t id, lwm2m_list_t ** nodeP); // Return the lowest unused ID in the list 'head' uint16_t lwm2m_list_newId(lwm2m_list_t * head); +// Free a list. Do not use if nodes contain allocated pointers as it calls lwm2m_free on nodes only. +// If the nodes of the list need to do more than just "free()" their instances, don't use lwm2m_list_free(). +void lwm2m_list_free(lwm2m_list_t * head); #define LWM2M_LIST_ADD(H,N) lwm2m_list_add((lwm2m_list_t *)H, (lwm2m_list_t *)N); #define LWM2M_LIST_RM(H,I,N) lwm2m_list_remove((lwm2m_list_t *)H, I, (lwm2m_list_t **)N); - - -/* - * Resource values - */ - -// defined in utils.c -int lwm2m_PlainTextToInt64(char * buffer, int length, int64_t * dataP); - -/* - * These utility functions allocate a new buffer storing the plain text - * representation of data. They return the size in bytes of the buffer - * or 0 in case of error. - * There is no trailing '\0' character in the buffer. - */ -int lwm2m_int8ToPlainText(int8_t data, char ** bufferP); -int lwm2m_int16ToPlainText(int16_t data, char ** bufferP); -int lwm2m_int32ToPlainText(int32_t data, char ** bufferP); -int lwm2m_int64ToPlainText(int64_t data, char ** bufferP); -int lwm2m_float32ToPlainText(float data, char ** bufferP); -int lwm2m_float64ToPlainText(double data, char ** bufferP); -int lwm2m_boolToPlainText(bool data, char ** bufferP); - - -/* - * TLV - */ - -#define LWM2M_TLV_HEADER_MAX_LENGTH 6 - -#define LWM2M_TYPE_RESSOURCE 0x00 -#define LWM2M_TYPE_MULTIPLE_RESSOURCE 0x01 -#define LWM2M_TYPE_RESSOURCE_INSTANCE 0x02 -#define LWM2M_TYPE_OBJECT_INSTANCE 0x03 - -/* - * Bitmask for the lwm2m_tlv_t::flag - * LWM2M_TLV_FLAG_STATIC_DATA specifies that lwm2m_tlv_t::value - * points to static memory and must no be freeed by the caller. - * LWM2M_TLV_FLAG_TEXT_FORMAT specifies that lwm2m_tlv_t::value - * is expressed or requested in plain text format. - */ -#define LWM2M_TLV_FLAG_STATIC_DATA 0x01 -#define LWM2M_TLV_FLAG_TEXT_FORMAT 0x02 - -typedef enum -{ - TLV_OBJECT_INSTANCE = LWM2M_TYPE_OBJECT_INSTANCE, - TLV_RESSOURCE_INSTANCE = LWM2M_TYPE_RESSOURCE_INSTANCE, - TLV_MULTIPLE_INSTANCE = LWM2M_TYPE_MULTIPLE_RESSOURCE, - TLV_RESSOURCE = LWM2M_TYPE_RESSOURCE -} lwm2m_tlv_type_t; - -typedef struct -{ - uint8_t flags; - uint8_t type; - uint16_t id; - size_t length; - uint8_t * value; -} lwm2m_tlv_t; - -lwm2m_tlv_t * lwm2m_tlv_new(int size); -int lwm2m_tlv_parse(char * buffer, size_t bufferLen, lwm2m_tlv_t ** dataP); -int lwm2m_tlv_serialize(int size, lwm2m_tlv_t * tlvP, char ** bufferP); -void lwm2m_tlv_free(int size, lwm2m_tlv_t * tlvP); - -void lwm2m_tlv_encode_int(int64_t data, lwm2m_tlv_t * tlvP); -int lwm2m_tlv_decode_int(lwm2m_tlv_t * tlvP, int64_t * dataP); - -/* - * These utility functions fill the buffer with a TLV record containing - * the data. They return the size in bytes of the TLV record, 0 in case - * of error. - */ -int lwm2m_intToTLV(lwm2m_tlv_type_t type, int64_t data, uint16_t id, char * buffer, size_t buffer_len); -int lwm2m_boolToTLV(lwm2m_tlv_type_t type, bool value, uint16_t id, char * buffer, size_t buffer_len); -int lwm2m_opaqueToTLV(lwm2m_tlv_type_t type, uint8_t * dataP, size_t data_len, uint16_t id, char * buffer, size_t buffer_len); -int lwm2m_decodeTLV(char * buffer, size_t buffer_len, lwm2m_tlv_type_t * oType, uint16_t * oID, size_t * oDataIndex, size_t * oDataLen); -int lwm2m_opaqueToInt(char * buffer, size_t buffer_len, int64_t * dataP); +#define LWM2M_LIST_FIND(H,I) lwm2m_list_find((lwm2m_list_t *)H, I) +#define LWM2M_LIST_FREE(H) lwm2m_list_free((lwm2m_list_t *)H) /* * URI * * objectId is always set - * if instanceId or resourceId is greater than LWM2M_URI_MAX_ID, it means it is not specified + * instanceId or resourceId are set according to the flag bit-field * */ @@ -275,8 +288,8 @@ #define LWM2M_URI_FLAG_INSTANCE_ID (uint8_t)0x02 #define LWM2M_URI_FLAG_RESOURCE_ID (uint8_t)0x01 -#define LWM2M_URI_IS_SET_INSTANCE(uri) ((uri->flag & LWM2M_URI_FLAG_INSTANCE_ID) != 0) -#define LWM2M_URI_IS_SET_RESOURCE(uri) ((uri->flag & LWM2M_URI_FLAG_RESOURCE_ID) != 0) +#define LWM2M_URI_IS_SET_INSTANCE(uri) (((uri)->flag & LWM2M_URI_FLAG_INSTANCE_ID) != 0) +#define LWM2M_URI_IS_SET_RESOURCE(uri) (((uri)->flag & LWM2M_URI_FLAG_RESOURCE_ID) != 0) typedef struct { @@ -293,7 +306,114 @@ // Return the number of characters read from buffer or 0 in case of error. // Valid URIs: /1, /1/, /1/2, /1/2/, /1/2/3 // Invalid URIs: /, //, //2, /1//, /1//3, /1/2/3/, /1/2/3/4 -int lwm2m_stringToUri(char * buffer, size_t buffer_len, lwm2m_uri_t * uriP); +int lwm2m_stringToUri(const char * buffer, size_t buffer_len, lwm2m_uri_t * uriP); + +/* + * The lwm2m_data_t is used to store LWM2M resource values in a hierarchical way. + * Depending on the type the value is different: + * - LWM2M_TYPE_OBJECT, LWM2M_TYPE_OBJECT_INSTANCE, LWM2M_TYPE_MULTIPLE_RESOURCE: value.asChildren + * - LWM2M_TYPE_STRING, LWM2M_TYPE_OPAQUE: value.asBuffer + * - LWM2M_TYPE_INTEGER, LWM2M_TYPE_TIME: value.asInteger + * - LWM2M_TYPE_FLOAT: value.asFloat + * - LWM2M_TYPE_BOOLEAN: value.asBoolean + * + * LWM2M_TYPE_STRING is also used when the data is in text format. + */ + +typedef enum +{ + LWM2M_TYPE_UNDEFINED = 0, + LWM2M_TYPE_OBJECT, + LWM2M_TYPE_OBJECT_INSTANCE, + LWM2M_TYPE_MULTIPLE_RESOURCE, + + LWM2M_TYPE_STRING, + LWM2M_TYPE_OPAQUE, + LWM2M_TYPE_INTEGER, + LWM2M_TYPE_FLOAT, + LWM2M_TYPE_BOOLEAN, + + LWM2M_TYPE_OBJECT_LINK +} lwm2m_data_type_t; + +typedef struct _lwm2m_data_t lwm2m_data_t; + +struct _lwm2m_data_t +{ + lwm2m_data_type_t type; + uint16_t id; + union + { + bool asBoolean; + int64_t asInteger; + double asFloat; + struct + { + size_t length; + uint8_t * buffer; + } asBuffer; + struct + { + size_t count; + lwm2m_data_t * array; + } asChildren; + struct + { + uint16_t objectId; + uint16_t objectInstanceId; + } asObjLink; + } value; +}; + +typedef enum +{ + LWM2M_CONTENT_TEXT = 0, // Also used as undefined + LWM2M_CONTENT_LINK = 40, + LWM2M_CONTENT_OPAQUE = 42, + LWM2M_CONTENT_TLV_OLD = 1542, // Keep old value for backward-compatibility + LWM2M_CONTENT_TLV = 11542, + LWM2M_CONTENT_JSON_OLD = 1543, // Keep old value for backward-compatibility + LWM2M_CONTENT_JSON = 11543 +} lwm2m_media_type_t; + +lwm2m_data_t * lwm2m_data_new(int size); +int lwm2m_data_parse(lwm2m_uri_t * uriP, uint8_t * buffer, size_t bufferLen, lwm2m_media_type_t format, lwm2m_data_t ** dataP); +int lwm2m_data_serialize(lwm2m_uri_t * uriP, int size, lwm2m_data_t * dataP, lwm2m_media_type_t * formatP, uint8_t ** bufferP); +void lwm2m_data_free(int size, lwm2m_data_t * dataP); + +void lwm2m_data_encode_string(const char * string, lwm2m_data_t * dataP); +void lwm2m_data_encode_nstring(const char * string, size_t length, lwm2m_data_t * dataP); +void lwm2m_data_encode_opaque(uint8_t * buffer, size_t length, lwm2m_data_t * dataP); +void lwm2m_data_encode_int(int64_t value, lwm2m_data_t * dataP); +int lwm2m_data_decode_int(const lwm2m_data_t * dataP, int64_t * valueP); +void lwm2m_data_encode_float(double value, lwm2m_data_t * dataP); +int lwm2m_data_decode_float(const lwm2m_data_t * dataP, double * valueP); +void lwm2m_data_encode_bool(bool value, lwm2m_data_t * dataP); +int lwm2m_data_decode_bool(const lwm2m_data_t * dataP, bool * valueP); +void lwm2m_data_encode_objlink(uint16_t objectId, uint16_t objectInstanceId, lwm2m_data_t * dataP); +void lwm2m_data_encode_instances(lwm2m_data_t * subDataP, size_t count, lwm2m_data_t * dataP); +void lwm2m_data_include(lwm2m_data_t * subDataP, size_t count, lwm2m_data_t * dataP); + + +/* + * Utility function to parse TLV buffers directly + * + * Returned value: number of bytes parsed + * buffer: buffer to parse + * buffer_len: length in bytes of buffer + * oType: (OUT) type of the parsed TLV record. can be: + * - LWM2M_TYPE_OBJECT + * - LWM2M_TYPE_OBJECT_INSTANCE + * - LWM2M_TYPE_MULTIPLE_RESOURCE + * - LWM2M_TYPE_OPAQUE + * oID: (OUT) ID of the parsed TLV record + * oDataIndex: (OUT) index of the data of the parsed TLV record in the buffer + * oDataLen: (OUT) length of the data of the parsed TLV record + */ + +#define LWM2M_TLV_HEADER_MAX_LENGTH 6 + +int lwm2m_decode_TLV(const uint8_t * buffer, size_t buffer_len, lwm2m_data_type_t * oType, uint16_t * oID, size_t * oDataIndex, size_t * oDataLen); /* @@ -306,25 +426,25 @@ typedef struct _lwm2m_object_t lwm2m_object_t; -typedef uint8_t (*lwm2m_read_callback_t) (uint16_t instanceId, int * numDataP, lwm2m_tlv_t ** dataArrayP, lwm2m_object_t * objectP); -typedef uint8_t (*lwm2m_write_callback_t) (uint16_t instanceId, int numData, lwm2m_tlv_t * dataArray, lwm2m_object_t * objectP); -typedef uint8_t (*lwm2m_execute_callback_t) (uint16_t instanceId, uint16_t resourceId, char * buffer, int length, lwm2m_object_t * objectP); -typedef uint8_t (*lwm2m_create_callback_t) (uint16_t instanceId, int numData, lwm2m_tlv_t * dataArray, lwm2m_object_t * objectP); +typedef uint8_t (*lwm2m_read_callback_t) (uint16_t instanceId, int * numDataP, lwm2m_data_t ** dataArrayP, lwm2m_object_t * objectP); +typedef uint8_t (*lwm2m_discover_callback_t) (uint16_t instanceId, int * numDataP, lwm2m_data_t ** dataArrayP, lwm2m_object_t * objectP); +typedef uint8_t (*lwm2m_write_callback_t) (uint16_t instanceId, int numData, lwm2m_data_t * dataArray, lwm2m_object_t * objectP); +typedef uint8_t (*lwm2m_execute_callback_t) (uint16_t instanceId, uint16_t resourceId, uint8_t * buffer, int length, lwm2m_object_t * objectP); +typedef uint8_t (*lwm2m_create_callback_t) (uint16_t instanceId, int numData, lwm2m_data_t * dataArray, lwm2m_object_t * objectP); typedef uint8_t (*lwm2m_delete_callback_t) (uint16_t instanceId, lwm2m_object_t * objectP); -typedef void (*lwm2m_close_callback_t) (lwm2m_object_t * objectP); - struct _lwm2m_object_t { - uint16_t objID; - lwm2m_list_t * instanceList; - lwm2m_read_callback_t readFunc; - lwm2m_write_callback_t writeFunc; - lwm2m_execute_callback_t executeFunc; - lwm2m_create_callback_t createFunc; - lwm2m_delete_callback_t deleteFunc; - lwm2m_close_callback_t closeFunc; - void * userData; + struct _lwm2m_object_t * next; // for internal use only. + uint16_t objID; + lwm2m_list_t * instanceList; + lwm2m_read_callback_t readFunc; + lwm2m_write_callback_t writeFunc; + lwm2m_execute_callback_t executeFunc; + lwm2m_create_callback_t createFunc; + lwm2m_delete_callback_t deleteFunc; + lwm2m_discover_callback_t discoverFunc; + void * userData; }; /* @@ -336,28 +456,21 @@ typedef enum { - SEC_NONE = 0, - SEC_PRE_SHARED_KEY, - SEC_RAW_PUBLIC_KEY, - SEC_CERTIFICATE -} lwm2m_security_mode_t; - -typedef struct -{ - lwm2m_security_mode_t mode; - size_t publicKeyLength; - uint8_t * publicKey; - size_t privateKeyLength; - uint8_t * privateKey; -} lwm2m_security_t; - -typedef enum -{ - STATE_UNKNOWN = 0, - STATE_REG_PENDING, - STATE_REGISTERED, - STATE_REG_UPDATE_PENDING, - STATE_DEREG_PENDING + STATE_DEREGISTERED = 0, // not registered or boostrap not started + STATE_REG_PENDING, // registration pending + STATE_REGISTERED, // successfully registered + STATE_REG_FAILED, // last registration failed + STATE_REG_UPDATE_PENDING, // registration update pending + STATE_REG_UPDATE_NEEDED, // registration update required + STATE_REG_FULL_UPDATE_NEEDED, // registration update with objects required + STATE_DEREG_PENDING, // deregistration pending + STATE_BS_HOLD_OFF, // bootstrap hold off time + STATE_BS_INITIATED, // bootstrap request sent + STATE_BS_PENDING, // boostrap ongoing + STATE_BS_FINISHING, // boostrap finish received + STATE_BS_FINISHED, // bootstrap done + STATE_BS_FAILING, // bootstrap error occurred + STATE_BS_FAILED, // bootstrap failed } lwm2m_status_t; typedef enum @@ -371,33 +484,43 @@ BINDING_UQS // UDP queue mode plus SMS } lwm2m_binding_t; +/* + * LWM2M block1 data + * + * Temporary data needed to handle block1 request. + * Currently support only one block1 request by server. + */ +typedef struct _lwm2m_block1_data_ lwm2m_block1_data_t; + +struct _lwm2m_block1_data_ +{ + uint8_t * block1buffer; // data buffer + size_t block1bufferSize; // buffer size + uint16_t lastmid; // mid of the last message received +}; + typedef struct _lwm2m_server_ { - struct _lwm2m_server_ * next; // matches lwm2m_list_t::next - uint16_t shortID; // matches lwm2m_list_t::id - uint32_t lifetime; // lifetime of the registration in sec or 0 if default value (86400 sec) - char * sms; // SMS MSISDN (phone number) for this server to send SMS - lwm2m_binding_t binding; // client connection mode with this server - lwm2m_security_t security; - void * sessionH; - lwm2m_status_t status; - char * location; - uint16_t mid; + struct _lwm2m_server_ * next; // matches lwm2m_list_t::next + uint16_t secObjInstID; // matches lwm2m_list_t::id + uint16_t shortID; // servers short ID, may be 0 for bootstrap server + time_t lifetime; // lifetime of the registration in sec or 0 if default value (86400 sec), also used as hold off time for bootstrap servers + time_t registration; // date of the last registration in sec or end of client hold off time for bootstrap servers + lwm2m_binding_t binding; // client connection mode with this server + void * sessionH; + lwm2m_status_t status; + char * location; + bool dirty; + lwm2m_block1_data_t * block1Data; // buffer to handle block1 data, should be replace by a list to support several block1 transfer by server. } lwm2m_server_t; -typedef struct -{ - char * uri; - lwm2m_security_t security; - uint32_t holdOffTime; -} lwm2m_bootstrap_server_t; /* * LWM2M result callback * * When used with an observe, if 'data' is not nil, 'status' holds the observe counter. */ -typedef void (*lwm2m_result_callback_t) (uint16_t clientID, lwm2m_uri_t * uriP, int status, uint8_t * data, int dataLength, void * userData); +typedef void (*lwm2m_result_callback_t) (uint16_t clientID, lwm2m_uri_t * uriP, int status, lwm2m_media_type_t format, uint8_t * data, int dataLength, void * userData); /* * LWM2M Observations @@ -405,6 +528,7 @@ * Used to store observation of remote clients resources. * status STATE_REG_PENDING means the observe request was sent to the client but not yet answered. * status STATE_REGISTERED means the client acknowledged the observe request. + * status STATE_DEREG_PENDING means the user canceled the request before the client answered it. */ typedef struct _lwm2m_observation_ @@ -413,11 +537,36 @@ uint16_t id; // matches lwm2m_list_t::id struct _lwm2m_client_ * clientP; lwm2m_uri_t uri; + lwm2m_status_t status; lwm2m_result_callback_t callback; void * userData; } lwm2m_observation_t; /* + * LWM2M Link Attributes + * + * Used for observation parameters. + * + */ + +#define LWM2M_ATTR_FLAG_MIN_PERIOD (uint8_t)0x01 +#define LWM2M_ATTR_FLAG_MAX_PERIOD (uint8_t)0x02 +#define LWM2M_ATTR_FLAG_GREATER_THAN (uint8_t)0x04 +#define LWM2M_ATTR_FLAG_LESS_THAN (uint8_t)0x08 +#define LWM2M_ATTR_FLAG_STEP (uint8_t)0x10 + +typedef struct +{ + uint8_t toSet; + uint8_t toClear; + uint32_t minPeriod; + uint32_t maxPeriod; + double greaterThan; + double lessThan; + double step; +} lwm2m_attributes_t; + +/* * LWM2M Clients * * Be careful not to mix lwm2m_client_object_t used to store list of objects of remote clients @@ -439,6 +588,8 @@ char * name; lwm2m_binding_t binding; char * msisdn; + char * altPath; + bool supportJSON; uint32_t lifetime; time_t endOfLife; void * sessionH; @@ -453,14 +604,6 @@ * Adaptation of Erbium's coap_transaction_t */ -typedef enum -{ - ENDPOINT_UNKNOWN = 0, - ENDPOINT_CLIENT, - ENDPOINT_SERVER, - ENDPOINT_BOOTSTRAP -} lwm2m_endpoint_type_t; - typedef struct _lwm2m_transaction_ lwm2m_transaction_t; typedef void (*lwm2m_transaction_callback_t) (lwm2m_transaction_t * transacP, void * message); @@ -469,8 +612,9 @@ { lwm2m_transaction_t * next; // matches lwm2m_list_t::next uint16_t mID; // matches lwm2m_list_t::id - lwm2m_endpoint_type_t peerType; - void * peerP; + void * peerH; + uint8_t ack_received; // indicates, that the ACK was received + time_t response_timeout; // timeout to wait for response, if token is used. When 0, use calculated acknowledge timeout. uint8_t retrans_counter; time_t retrans_time; char objStringID[LWM2M_STRING_ID_MAX_LEN]; @@ -490,15 +634,23 @@ { struct _lwm2m_watcher_ * next; + bool active; + bool update; lwm2m_server_t * server; + lwm2m_attributes_t * parameters; uint8_t token[8]; size_t tokenLen; + time_t lastTime; uint32_t counter; uint16_t lastMid; + union + { + int64_t asInteger; + double asFloat; + } lastValue; } lwm2m_watcher_t; typedef struct _lwm2m_observed_ - { struct _lwm2m_observed_ * next; @@ -506,67 +658,92 @@ lwm2m_watcher_t * watcherList; } lwm2m_observed_t; +#ifdef LWM2M_CLIENT_MODE +typedef enum +{ + STATE_INITIAL = 0, + STATE_BOOTSTRAP_REQUIRED, + STATE_BOOTSTRAPPING, + STATE_REGISTER_REQUIRED, + STATE_REGISTERING, + STATE_READY +} lwm2m_client_state_t; + +#endif /* * LWM2M Context */ -// The session handle MUST uniquely identify a peer. -typedef uint8_t (*lwm2m_buffer_send_callback_t)(void * sessionH, uint8_t * buffer, size_t length, void * userData); +#ifdef LWM2M_BOOTSTRAP_SERVER_MODE +// In all the following APIs, the session handle MUST uniquely identify a peer. +// LWM2M bootstrap callback +// When a LWM2M client requests bootstrap information, the callback is called with status COAP_NO_ERROR, uriP is nil and +// name is set. The callback must return a COAP_* error code. COAP_204_CHANGED for success. +// After a lwm2m_bootstrap_delete() or a lwm2m_bootstrap_write(), the callback is called with the status returned by the +// client, the URI of the operation (may be nil) and name is nil. The callback return value is ignored. +typedef int (*lwm2m_bootstrap_callback_t) (void * sessionH, uint8_t status, lwm2m_uri_t * uriP, char * name, void * userData); +#endif typedef struct { - int socket; #ifdef LWM2M_CLIENT_MODE - char * endpointName; - lwm2m_bootstrap_server_t * bootstrapServer; - lwm2m_server_t * serverList; - lwm2m_object_t ** objectList; - uint16_t numObject; - lwm2m_observed_t * observedList; + lwm2m_client_state_t state; + char * endpointName; + char * msisdn; + char * altPath; + lwm2m_server_t * bootstrapServerList; + lwm2m_server_t * serverList; + lwm2m_object_t * objectList; + lwm2m_observed_t * observedList; #endif #ifdef LWM2M_SERVER_MODE lwm2m_client_t * clientList; lwm2m_result_callback_t monitorCallback; void * monitorUserData; #endif - uint16_t nextMID; - lwm2m_transaction_t * transactionList; - // buffer send callback - lwm2m_buffer_send_callback_t bufferSendCallback; - void * bufferSendUserData; +#ifdef LWM2M_BOOTSTRAP_SERVER_MODE + lwm2m_bootstrap_callback_t bootstrapCallback; + void * bootstrapUserData; +#endif + uint16_t nextMID; + lwm2m_transaction_t * transactionList; + void * userData; } lwm2m_context_t; -// initialize a liblwm2m context. endpointName, numObject and objectList are ignored for pure servers. -lwm2m_context_t * lwm2m_init(char * endpointName, uint16_t numObject, lwm2m_object_t * objectList[], lwm2m_buffer_send_callback_t bufferSendCallback, void * bufferSendUserData); +// initialize a liblwm2m context. +lwm2m_context_t * lwm2m_init(void * userData); // close a liblwm2m context. void lwm2m_close(lwm2m_context_t * contextP); -// perform any required pending operation and adjust timeoutP to the maximal time interval to wait. -int lwm2m_step(lwm2m_context_t * contextP, struct timeval * timeoutP); +// perform any required pending operation and adjust timeoutP to the maximal time interval to wait in seconds. +int lwm2m_step(lwm2m_context_t * contextP, time_t * timeoutP); // dispatch received data to liblwm2m void lwm2m_handle_packet(lwm2m_context_t * contextP, uint8_t * buffer, int length, void * fromSessionH); #ifdef LWM2M_CLIENT_MODE -void lwm2m_set_bootstrap_server(lwm2m_context_t * contextP, lwm2m_bootstrap_server_t * serverP); -int lwm2m_add_server(lwm2m_context_t * contextP, uint16_t shortID, uint32_t lifetime, char * sms, lwm2m_binding_t binding, void * sessionH, lwm2m_security_t * securityP); - -// send registration message to all known LWM2M Servers. -int lwm2m_register(lwm2m_context_t * contextP); +// configure the client side with the Endpoint Name, binding, MSISDN (can be nil), alternative path +// for objects (can be nil) and a list of objects. +// LWM2M Security Object (ID 0) must be present with either a bootstrap server or a LWM2M server and +// its matching LWM2M Server Object (ID 1) instance +int lwm2m_configure(lwm2m_context_t * contextP, const char * endpointName, const char * msisdn, const char * altPath, uint16_t numObject, lwm2m_object_t * objectList[]); +int lwm2m_add_object(lwm2m_context_t * contextP, lwm2m_object_t * objectP); +int lwm2m_remove_object(lwm2m_context_t * contextP, uint16_t id); // send a registration update to the server specified by the server short identifier -int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID); +// or all if the ID is 0. +// If withObjects is true, the registration update contains the object list. +int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID, bool withObjects); -// inform liblwm2m that a resource value has changed. void lwm2m_resource_value_changed(lwm2m_context_t * contextP, lwm2m_uri_t * uriP); #endif #ifdef LWM2M_SERVER_MODE // Clients registration/deregistration monitoring API. -// When a LWM2M client registers, the callback is called with status CREATED_2_01. -// When a LWM2M client deregisters, the callback is called with status DELETED_2_02. +// When a LWM2M client registers, the callback is called with status COAP_201_CREATED. +// When a LWM2M client deregisters, the callback is called with status COAP_202_DELETED. // clientID is the internal ID of the LWM2M Client. // The callback's parameters uri, data, dataLength are always NULL. // The lwm2m_client_t is present in the lwm2m_context_t's clientList when the callback is called. On a deregistration, it deleted when the callback returns. @@ -574,9 +751,11 @@ // Device Management APIs int lwm2m_dm_read(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData); -int lwm2m_dm_write(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, char * buffer, int length, lwm2m_result_callback_t callback, void * userData); -int lwm2m_dm_execute(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, char * buffer, int length, lwm2m_result_callback_t callback, void * userData); -int lwm2m_dm_create(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, char * buffer, int length, lwm2m_result_callback_t callback, void * userData); +int lwm2m_dm_discover(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData); +int lwm2m_dm_write(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, int length, lwm2m_result_callback_t callback, void * userData); +int lwm2m_dm_write_attributes(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_attributes_t * attrP, lwm2m_result_callback_t callback, void * userData); +int lwm2m_dm_execute(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, int length, lwm2m_result_callback_t callback, void * userData); +int lwm2m_dm_create(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, int length, lwm2m_result_callback_t callback, void * userData); int lwm2m_dm_delete(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData); // Information Reporting APIs @@ -584,4 +763,21 @@ int lwm2m_observe_cancel(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData); #endif +#ifdef LWM2M_BOOTSTRAP_SERVER_MODE +// Clients bootstrap request monitoring API. +// When a LWM2M client sends a bootstrap request, the callback is called with the client's endpoint name. +void lwm2m_set_bootstrap_callback(lwm2m_context_t * contextP, lwm2m_bootstrap_callback_t callback, void * userData); + +// Boostrap Interface APIs +// if uriP is nil, a "Delete /" is sent to the client +int lwm2m_bootstrap_delete(lwm2m_context_t * contextP, void * sessionH, lwm2m_uri_t * uriP); +int lwm2m_bootstrap_write(lwm2m_context_t * contextP, void * sessionH, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, size_t length); +int lwm2m_bootstrap_finish(lwm2m_context_t * contextP, void * sessionH); + #endif + +#ifdef __cplusplus +} +#endif + +#endif