terence zhang / wakaama
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers liblwm2m.h Source File

liblwm2m.h

00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2013, 2014 Intel Corporation and others.
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * The Eclipse Distribution License is available at
00011  *    http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    David Navarro, Intel Corporation - initial API and implementation
00015  *    Fabien Fleutot - Please refer to git log
00016  *    Simon Bernard - Please refer to git log
00017  *    Toby Jaffey - Please refer to git log
00018  *    Julien Vermillard - Please refer to git log
00019  *    Bosch Software Innovations GmbH - Please refer to git log
00020  *    Pascal Rieux - Please refer to git log
00021  *******************************************************************************/
00022 
00023 /*
00024  Copyright (c) 2013, 2014 Intel Corporation
00025 
00026  Redistribution and use in source and binary forms, with or without modification,
00027  are permitted provided that the following conditions are met:
00028 
00029      * Redistributions of source code must retain the above copyright notice,
00030        this list of conditions and the following disclaimer.
00031      * Redistributions in binary form must reproduce the above copyright notice,
00032        this list of conditions and the following disclaimer in the documentation
00033        and/or other materials provided with the distribution.
00034      * Neither the name of Intel Corporation nor the names of its contributors
00035        may be used to endorse or promote products derived from this software
00036        without specific prior written permission.
00037 
00038  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00039  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00040  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00041  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00042  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00043  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00044  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00045  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00046  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00047  THE POSSIBILITY OF SUCH DAMAGE.
00048 
00049  David Navarro <david.navarro@intel.com>
00050 
00051 */
00052 
00053 #ifndef _LWM2M_CLIENT_H_
00054 #define _LWM2M_CLIENT_H_
00055 
00056 #ifdef __cplusplus
00057 extern "C" {
00058 #endif
00059 
00060 #include <stdint.h>
00061 #include <stddef.h>
00062 #include <stdbool.h>
00063 #include <time.h>
00064 
00065 #ifdef LWM2M_SERVER_MODE
00066 #ifndef LWM2M_SUPPORT_JSON
00067 #define LWM2M_SUPPORT_JSON
00068 #endif
00069 #endif
00070 
00071 #if defined(LWM2M_BOOTSTRAP) && defined(LWM2M_BOOTSTRAP_SERVER_MODE)
00072 #error "LWM2M_BOOTSTRAP and LWM2M_BOOTSTRAP_SERVER_MODE cannot be defined at the same time!"
00073 #endif
00074 
00075 /*
00076  * Platform abstraction functions to be implemented by the user
00077  */
00078 
00079 #ifndef LWM2M_MEMORY_TRACE
00080 // Allocate a block of size bytes of memory, returning a pointer to the beginning of the block.
00081 void * lwm2m_malloc(size_t s);
00082 // Deallocate a block of memory previously allocated by lwm2m_malloc() or lwm2m_strdup()
00083 void lwm2m_free(void * p);
00084 // Allocate a memory block, duplicate the string str in it and return a pointer to this new block.
00085 char * lwm2m_strdup(const char * str);
00086 #else
00087 // same functions as above with caller location for debugging purposes
00088 char * lwm2m_trace_strdup(const char * str, const char * file, const char * function, int lineno);
00089 void * lwm2m_trace_malloc(size_t size, const char * file, const char * function, int lineno);
00090 void    lwm2m_trace_free(void * mem, const char * file, const char * function, int lineno);
00091 
00092 #define lwm2m_strdup(S) lwm2m_trace_strdup(S, __FILE__, __FUNCTION__, __LINE__)
00093 #define lwm2m_malloc(S) lwm2m_trace_malloc(S, __FILE__, __FUNCTION__, __LINE__)
00094 #define lwm2m_free(M)   lwm2m_trace_free(M, __FILE__, __FUNCTION__, __LINE__)
00095 #endif
00096 // Compare at most the n first bytes of s1 and s2, return 0 if they match
00097 int lwm2m_strncmp(const char * s1, const char * s2, size_t n);
00098 // This function must return the number of seconds elapsed since origin.
00099 // The origin (Epoch, system boot, etc...) does not matter as this
00100 // function is used only to determine the elapsed time since the last
00101 // call to it.
00102 // In case of error, this must return a negative value.
00103 // Per POSIX specifications, time_t is a signed integer.
00104 time_t lwm2m_gettime(void);
00105 
00106 #ifdef LWM2M_WITH_LOGS
00107 // Same usage as C89 printf()
00108 void lwm2m_printf(const char * format, ...);
00109 #endif
00110 
00111 // communication layer
00112 #ifdef LWM2M_CLIENT_MODE
00113 // Returns a session handle that MUST uniquely identify a peer.
00114 // secObjInstID: ID of the Securty Object instance to open a connection to
00115 // userData: parameter to lwm2m_init()
00116 void * lwm2m_connect_server(uint16_t secObjInstID, void * userData);
00117 // Close a session created by lwm2m_connect_server()
00118 // sessionH: session handle identifying the peer (opaque to the core)
00119 // userData: parameter to lwm2m_init()
00120 void lwm2m_close_connection(void * sessionH, void * userData);
00121 #endif
00122 // Send data to a peer
00123 // Returns COAP_NO_ERROR or a COAP_NNN error code
00124 // sessionH: session handle identifying the peer (opaque to the core)
00125 // buffer, length: data to send
00126 // userData: parameter to lwm2m_init()
00127 uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void * userData);
00128 // Compare two session handles
00129 // Returns true if the two sessions identify the same peer. false otherwise.
00130 // userData: parameter to lwm2m_init()
00131 bool lwm2m_session_is_equal(void * session1, void * session2, void * userData);
00132 
00133 /*
00134  * Error code
00135  */
00136 
00137 #define COAP_NO_ERROR                   (uint8_t)0x00
00138 #define COAP_IGNORE                     (uint8_t)0x01
00139 
00140 #define COAP_201_CREATED                (uint8_t)0x41
00141 #define COAP_202_DELETED                (uint8_t)0x42
00142 #define COAP_204_CHANGED                (uint8_t)0x44
00143 #define COAP_205_CONTENT                (uint8_t)0x45
00144 #define COAP_231_CONTINUE               (uint8_t)0x5F
00145 #define COAP_400_BAD_REQUEST            (uint8_t)0x80
00146 #define COAP_401_UNAUTHORIZED           (uint8_t)0x81
00147 #define COAP_402_BAD_OPTION             (uint8_t)0x82
00148 #define COAP_404_NOT_FOUND              (uint8_t)0x84
00149 #define COAP_405_METHOD_NOT_ALLOWED     (uint8_t)0x85
00150 #define COAP_406_NOT_ACCEPTABLE         (uint8_t)0x86
00151 #define COAP_408_REQ_ENTITY_INCOMPLETE  (uint8_t)0x88
00152 #define COAP_412_PRECONDITION_FAILED    (uint8_t)0x8C
00153 #define COAP_413_ENTITY_TOO_LARGE       (uint8_t)0x8D
00154 #define COAP_500_INTERNAL_SERVER_ERROR  (uint8_t)0xA0
00155 #define COAP_501_NOT_IMPLEMENTED        (uint8_t)0xA1
00156 #define COAP_503_SERVICE_UNAVAILABLE    (uint8_t)0xA3
00157 
00158 /*
00159  * Standard Object IDs
00160  */
00161 #define LWM2M_SECURITY_OBJECT_ID            0
00162 #define LWM2M_SERVER_OBJECT_ID              1
00163 #define LWM2M_ACL_OBJECT_ID                 2
00164 #define LWM2M_DEVICE_OBJECT_ID              3
00165 #define LWM2M_CONN_MONITOR_OBJECT_ID        4
00166 #define LWM2M_FIRMWARE_UPDATE_OBJECT_ID     5
00167 #define LWM2M_LOCATION_OBJECT_ID            6
00168 #define LWM2M_CONN_STATS_OBJECT_ID          7
00169 
00170 /*
00171  * Ressource IDs for the LWM2M Security Object
00172  */
00173 #define LWM2M_SECURITY_URI_ID                 0
00174 #define LWM2M_SECURITY_BOOTSTRAP_ID           1
00175 #define LWM2M_SECURITY_SECURITY_ID            2
00176 #define LWM2M_SECURITY_PUBLIC_KEY_ID          3
00177 #define LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID   4
00178 #define LWM2M_SECURITY_SECRET_KEY_ID          5
00179 #define LWM2M_SECURITY_SMS_SECURITY_ID        6
00180 #define LWM2M_SECURITY_SMS_KEY_PARAM_ID       7
00181 #define LWM2M_SECURITY_SMS_SECRET_KEY_ID      8
00182 #define LWM2M_SECURITY_SMS_SERVER_NUMBER_ID   9
00183 #define LWM2M_SECURITY_SHORT_SERVER_ID        10
00184 #define LWM2M_SECURITY_HOLD_OFF_ID            11
00185 #define LWM2M_SECURITY_BOOTSTRAP_TIMEOUT_ID   12
00186 
00187 /*
00188  * Ressource IDs for the LWM2M Server Object
00189  */
00190 #define LWM2M_SERVER_SHORT_ID_ID    0
00191 #define LWM2M_SERVER_LIFETIME_ID    1
00192 #define LWM2M_SERVER_MIN_PERIOD_ID  2
00193 #define LWM2M_SERVER_MAX_PERIOD_ID  3
00194 #define LWM2M_SERVER_DISABLE_ID     4
00195 #define LWM2M_SERVER_TIMEOUT_ID     5
00196 #define LWM2M_SERVER_STORING_ID     6
00197 #define LWM2M_SERVER_BINDING_ID     7
00198 #define LWM2M_SERVER_UPDATE_ID      8
00199 
00200 #define LWM2M_SECURITY_MODE_PRE_SHARED_KEY  0
00201 #define LWM2M_SECURITY_MODE_RAW_PUBLIC_KEY  1
00202 #define LWM2M_SECURITY_MODE_CERTIFICATE     2
00203 #define LWM2M_SECURITY_MODE_NONE            3
00204 
00205 
00206 /*
00207  * Utility functions for sorted linked list
00208  */
00209 
00210 typedef struct _lwm2m_list_t
00211 {
00212     struct _lwm2m_list_t * next;
00213     uint16_t    id;
00214 } lwm2m_list_t;
00215 
00216 // defined in list.c
00217 // Add 'node' to the list 'head' and return the new list
00218 lwm2m_list_t * lwm2m_list_add(lwm2m_list_t * head, lwm2m_list_t * node);
00219 // Return the node with ID 'id' from the list 'head' or NULL if not found
00220 lwm2m_list_t * lwm2m_list_find(lwm2m_list_t * head, uint16_t id);
00221 // Remove the node with ID 'id' from the list 'head' and return the new list
00222 lwm2m_list_t * lwm2m_list_remove(lwm2m_list_t * head, uint16_t id, lwm2m_list_t ** nodeP);
00223 // Return the lowest unused ID in the list 'head'
00224 uint16_t lwm2m_list_newId(lwm2m_list_t * head);
00225 // Free a list. Do not use if nodes contain allocated pointers as it calls lwm2m_free on nodes only.
00226 // If the nodes of the list need to do more than just "free()" their instances, don't use lwm2m_list_free().
00227 void lwm2m_list_free(lwm2m_list_t * head);
00228 
00229 #define LWM2M_LIST_ADD(H,N) lwm2m_list_add((lwm2m_list_t *)H, (lwm2m_list_t *)N);
00230 #define LWM2M_LIST_RM(H,I,N) lwm2m_list_remove((lwm2m_list_t *)H, I, (lwm2m_list_t **)N);
00231 #define LWM2M_LIST_FIND(H,I) lwm2m_list_find((lwm2m_list_t *)H, I)
00232 #define LWM2M_LIST_FREE(H) lwm2m_list_free((lwm2m_list_t *)H)
00233 
00234 /*
00235  * URI
00236  *
00237  * objectId is always set
00238  * instanceId or resourceId are set according to the flag bit-field
00239  *
00240  */
00241 
00242 #define LWM2M_MAX_ID   ((uint16_t)0xFFFF)
00243 
00244 #define LWM2M_URI_FLAG_OBJECT_ID    (uint8_t)0x04
00245 #define LWM2M_URI_FLAG_INSTANCE_ID  (uint8_t)0x02
00246 #define LWM2M_URI_FLAG_RESOURCE_ID  (uint8_t)0x01
00247 
00248 #define LWM2M_URI_IS_SET_INSTANCE(uri) (((uri)->flag & LWM2M_URI_FLAG_INSTANCE_ID) != 0)
00249 #define LWM2M_URI_IS_SET_RESOURCE(uri) (((uri)->flag & LWM2M_URI_FLAG_RESOURCE_ID) != 0)
00250 
00251 typedef struct
00252 {
00253     uint8_t     flag;           // indicates which segments are set
00254     uint16_t    objectId;
00255     uint16_t    instanceId;
00256     uint16_t    resourceId;
00257 } lwm2m_uri_t;
00258 
00259 
00260 #define LWM2M_STRING_ID_MAX_LEN 6
00261 
00262 // Parse an URI in LWM2M format and fill the lwm2m_uri_t.
00263 // Return the number of characters read from buffer or 0 in case of error.
00264 // Valid URIs: /1, /1/, /1/2, /1/2/, /1/2/3
00265 // Invalid URIs: /, //, //2, /1//, /1//3, /1/2/3/, /1/2/3/4
00266 int lwm2m_stringToUri(const char * buffer, size_t buffer_len, lwm2m_uri_t * uriP);
00267 
00268 /*
00269  * The lwm2m_data_t is used to store LWM2M resource values in a hierarchical way.
00270  * Depending on the type the value is different:
00271  * - LWM2M_TYPE_OBJECT, LWM2M_TYPE_OBJECT_INSTANCE, LWM2M_TYPE_MULTIPLE_RESOURCE: value.asChildren
00272  * - LWM2M_TYPE_STRING, LWM2M_TYPE_OPAQUE: value.asBuffer
00273  * - LWM2M_TYPE_INTEGER, LWM2M_TYPE_TIME: value.asInteger
00274  * - LWM2M_TYPE_FLOAT: value.asFloat
00275  * - LWM2M_TYPE_BOOLEAN: value.asBoolean
00276  *
00277  * LWM2M_TYPE_STRING is also used when the data is in text format.
00278  */
00279 
00280 typedef enum
00281 {
00282     LWM2M_TYPE_UNDEFINED = 0,
00283     LWM2M_TYPE_OBJECT,
00284     LWM2M_TYPE_OBJECT_INSTANCE,
00285     LWM2M_TYPE_MULTIPLE_RESOURCE,
00286 
00287     LWM2M_TYPE_STRING,
00288     LWM2M_TYPE_OPAQUE,
00289     LWM2M_TYPE_INTEGER,
00290     LWM2M_TYPE_FLOAT,
00291     LWM2M_TYPE_BOOLEAN,
00292 
00293     LWM2M_TYPE_OBJECT_LINK
00294 } lwm2m_data_type_t;
00295 
00296 typedef struct _lwm2m_data_t lwm2m_data_t;
00297 
00298 struct _lwm2m_data_t
00299 {
00300     lwm2m_data_type_t type;
00301     uint16_t    id;
00302     union
00303     {
00304         bool        asBoolean;
00305         int64_t     asInteger;
00306         double      asFloat;
00307         struct
00308         {
00309             size_t    length;
00310             uint8_t * buffer;
00311         } asBuffer;
00312         struct
00313         {
00314             size_t         count;
00315             lwm2m_data_t * array;
00316         } asChildren;
00317         struct
00318         {
00319             uint16_t objectId;
00320             uint16_t objectInstanceId;
00321         } asObjLink;
00322     } value;
00323 };
00324 
00325 typedef enum
00326 {
00327     LWM2M_CONTENT_TEXT      = 0,        // Also used as undefined
00328     LWM2M_CONTENT_LINK      = 40,
00329     LWM2M_CONTENT_OPAQUE    = 42,
00330     LWM2M_CONTENT_TLV_OLD   = 1542,     // Keep old value for backward-compatibility
00331     LWM2M_CONTENT_TLV       = 11542,
00332     LWM2M_CONTENT_JSON_OLD  = 1543,     // Keep old value for backward-compatibility
00333     LWM2M_CONTENT_JSON      = 11543
00334 } lwm2m_media_type_t;
00335 
00336 lwm2m_data_t * lwm2m_data_new(int size);
00337 int lwm2m_data_parse(lwm2m_uri_t * uriP, uint8_t * buffer, size_t bufferLen, lwm2m_media_type_t format, lwm2m_data_t ** dataP);
00338 int lwm2m_data_serialize(lwm2m_uri_t * uriP, int size, lwm2m_data_t * dataP, lwm2m_media_type_t * formatP, uint8_t ** bufferP);
00339 void lwm2m_data_free(int size, lwm2m_data_t * dataP);
00340 
00341 void lwm2m_data_encode_string(const char * string, lwm2m_data_t * dataP);
00342 void lwm2m_data_encode_nstring(const char * string, size_t length, lwm2m_data_t * dataP);
00343 void lwm2m_data_encode_opaque(uint8_t * buffer, size_t length, lwm2m_data_t * dataP);
00344 void lwm2m_data_encode_int(int64_t value, lwm2m_data_t * dataP);
00345 int lwm2m_data_decode_int(const lwm2m_data_t * dataP, int64_t * valueP);
00346 void lwm2m_data_encode_float(double value, lwm2m_data_t * dataP);
00347 int lwm2m_data_decode_float(const lwm2m_data_t * dataP, double * valueP);
00348 void lwm2m_data_encode_bool(bool value, lwm2m_data_t * dataP);
00349 int lwm2m_data_decode_bool(const lwm2m_data_t * dataP, bool * valueP);
00350 void lwm2m_data_encode_objlink(uint16_t objectId, uint16_t objectInstanceId, lwm2m_data_t * dataP);
00351 void lwm2m_data_encode_instances(lwm2m_data_t * subDataP, size_t count, lwm2m_data_t * dataP);
00352 void lwm2m_data_include(lwm2m_data_t * subDataP, size_t count, lwm2m_data_t * dataP);
00353 
00354 
00355 /*
00356  * Utility function to parse TLV buffers directly
00357  *
00358  * Returned value: number of bytes parsed
00359  * buffer: buffer to parse
00360  * buffer_len: length in bytes of buffer
00361  * oType: (OUT) type of the parsed TLV record. can be:
00362  *          - LWM2M_TYPE_OBJECT
00363  *          - LWM2M_TYPE_OBJECT_INSTANCE
00364  *          - LWM2M_TYPE_MULTIPLE_RESOURCE
00365  *          - LWM2M_TYPE_OPAQUE
00366  * oID: (OUT) ID of the parsed TLV record
00367  * oDataIndex: (OUT) index of the data of the parsed TLV record in the buffer
00368  * oDataLen: (OUT) length of the data of the parsed TLV record
00369  */
00370 
00371 #define LWM2M_TLV_HEADER_MAX_LENGTH 6
00372 
00373 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);
00374 
00375 
00376 /*
00377  * LWM2M Objects
00378  *
00379  * For the read callback, if *numDataP is not zero, *dataArrayP is pre-allocated
00380  * and contains the list of resources to read.
00381  *
00382  */
00383 
00384 typedef struct _lwm2m_object_t lwm2m_object_t;
00385 
00386 typedef uint8_t (*lwm2m_read_callback_t) (uint16_t instanceId, int * numDataP, lwm2m_data_t ** dataArrayP, lwm2m_object_t * objectP);
00387 typedef uint8_t (*lwm2m_discover_callback_t) (uint16_t instanceId, int * numDataP, lwm2m_data_t ** dataArrayP, lwm2m_object_t * objectP);
00388 typedef uint8_t (*lwm2m_write_callback_t) (uint16_t instanceId, int numData, lwm2m_data_t * dataArray, lwm2m_object_t * objectP);
00389 typedef uint8_t (*lwm2m_execute_callback_t) (uint16_t instanceId, uint16_t resourceId, uint8_t * buffer, int length, lwm2m_object_t * objectP);
00390 typedef uint8_t (*lwm2m_create_callback_t) (uint16_t instanceId, int numData, lwm2m_data_t * dataArray, lwm2m_object_t * objectP);
00391 typedef uint8_t (*lwm2m_delete_callback_t) (uint16_t instanceId, lwm2m_object_t * objectP);
00392 
00393 struct _lwm2m_object_t
00394 {
00395     struct _lwm2m_object_t * next;           // for internal use only.
00396     uint16_t       objID;
00397     lwm2m_list_t * instanceList;
00398     lwm2m_read_callback_t     readFunc;
00399     lwm2m_write_callback_t    writeFunc;
00400     lwm2m_execute_callback_t  executeFunc;
00401     lwm2m_create_callback_t   createFunc;
00402     lwm2m_delete_callback_t   deleteFunc;
00403     lwm2m_discover_callback_t discoverFunc;
00404     void * userData;
00405 };
00406 
00407 /*
00408  * LWM2M Servers
00409  *
00410  * Since LWM2M Server Object instances are not accessible to LWM2M servers,
00411  * there is no need to store them as lwm2m_objects_t
00412  */
00413 
00414 typedef enum
00415 {
00416     STATE_DEREGISTERED = 0,        // not registered or boostrap not started
00417     STATE_REG_PENDING,             // registration pending
00418     STATE_REGISTERED,              // successfully registered
00419     STATE_REG_FAILED,              // last registration failed
00420     STATE_REG_UPDATE_PENDING,      // registration update pending
00421     STATE_REG_UPDATE_NEEDED,       // registration update required
00422     STATE_REG_FULL_UPDATE_NEEDED,  // registration update with objects required
00423     STATE_DEREG_PENDING,           // deregistration pending
00424     STATE_BS_HOLD_OFF,             // bootstrap hold off time
00425     STATE_BS_INITIATED,            // bootstrap request sent
00426     STATE_BS_PENDING,              // boostrap ongoing
00427     STATE_BS_FINISHING,            // boostrap finish received
00428     STATE_BS_FINISHED,             // bootstrap done
00429     STATE_BS_FAILING,              // bootstrap error occurred
00430     STATE_BS_FAILED,               // bootstrap failed
00431 } lwm2m_status_t;
00432 
00433 typedef enum
00434 {
00435     BINDING_UNKNOWN = 0,
00436     BINDING_U,   // UDP
00437     BINDING_UQ,  // UDP queue mode
00438     BINDING_S,   // SMS
00439     BINDING_SQ,  // SMS queue mode
00440     BINDING_US,  // UDP plus SMS
00441     BINDING_UQS  // UDP queue mode plus SMS
00442 } lwm2m_binding_t;
00443 
00444 /*
00445  * LWM2M block1 data
00446  *
00447  * Temporary data needed to handle block1 request.
00448  * Currently support only one block1 request by server.
00449  */
00450 typedef struct _lwm2m_block1_data_ lwm2m_block1_data_t;
00451 
00452 struct _lwm2m_block1_data_
00453 {
00454     uint8_t *             block1buffer;     // data buffer
00455     size_t                block1bufferSize; // buffer size
00456     uint16_t              lastmid;          // mid of the last message received
00457 };
00458 
00459 typedef struct _lwm2m_server_
00460 {
00461     struct _lwm2m_server_ * next;         // matches lwm2m_list_t::next
00462     uint16_t                secObjInstID; // matches lwm2m_list_t::id
00463     uint16_t                shortID;      // servers short ID, may be 0 for bootstrap server
00464     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
00465     time_t                  registration; // date of the last registration in sec or end of client hold off time for bootstrap servers
00466     lwm2m_binding_t         binding;      // client connection mode with this server
00467     void *                  sessionH;
00468     lwm2m_status_t          status;
00469     char *                  location;
00470     bool                    dirty;
00471     lwm2m_block1_data_t *   block1Data;   // buffer to handle block1 data, should be replace by a list to support several block1 transfer by server.
00472 } lwm2m_server_t;
00473 
00474 
00475 /*
00476  * LWM2M result callback
00477  *
00478  * When used with an observe, if 'data' is not nil, 'status' holds the observe counter.
00479  */
00480 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);
00481 
00482 /*
00483  * LWM2M Observations
00484  *
00485  * Used to store observation of remote clients resources.
00486  * status STATE_REG_PENDING means the observe request was sent to the client but not yet answered.
00487  * status STATE_REGISTERED means the client acknowledged the observe request.
00488  * status STATE_DEREG_PENDING means the user canceled the request before the client answered it.
00489  */
00490 
00491 typedef struct _lwm2m_observation_
00492 {
00493     struct _lwm2m_observation_ * next;  // matches lwm2m_list_t::next
00494     uint16_t                     id;    // matches lwm2m_list_t::id
00495     struct _lwm2m_client_ * clientP;
00496     lwm2m_uri_t             uri;
00497     lwm2m_status_t          status;
00498     lwm2m_result_callback_t callback;
00499     void *                  userData;
00500 } lwm2m_observation_t;
00501 
00502 /*
00503  * LWM2M Link Attributes
00504  *
00505  * Used for observation parameters.
00506  *
00507  */
00508 
00509 #define LWM2M_ATTR_FLAG_MIN_PERIOD      (uint8_t)0x01
00510 #define LWM2M_ATTR_FLAG_MAX_PERIOD      (uint8_t)0x02
00511 #define LWM2M_ATTR_FLAG_GREATER_THAN    (uint8_t)0x04
00512 #define LWM2M_ATTR_FLAG_LESS_THAN       (uint8_t)0x08
00513 #define LWM2M_ATTR_FLAG_STEP            (uint8_t)0x10
00514 
00515 typedef struct
00516 {
00517     uint8_t     toSet;
00518     uint8_t     toClear;
00519     uint32_t    minPeriod;
00520     uint32_t    maxPeriod;
00521     double      greaterThan;
00522     double      lessThan;
00523     double      step;
00524 } lwm2m_attributes_t;
00525 
00526 /*
00527  * LWM2M Clients
00528  *
00529  * Be careful not to mix lwm2m_client_object_t used to store list of objects of remote clients
00530  * and lwm2m_object_t describing objects exposed to remote servers.
00531  *
00532  */
00533 
00534 typedef struct _lwm2m_client_object_
00535 {
00536     struct _lwm2m_client_object_ * next; // matches lwm2m_list_t::next
00537     uint16_t                 id;         // matches lwm2m_list_t::id
00538     lwm2m_list_t *           instanceList;
00539 } lwm2m_client_object_t;
00540 
00541 typedef struct _lwm2m_client_
00542 {
00543     struct _lwm2m_client_ * next;       // matches lwm2m_list_t::next
00544     uint16_t                internalID; // matches lwm2m_list_t::id
00545     char *                  name;
00546     lwm2m_binding_t         binding;
00547     char *                  msisdn;
00548     char *                  altPath;
00549     bool                    supportJSON;
00550     uint32_t                lifetime;
00551     time_t                  endOfLife;
00552     void *                  sessionH;
00553     lwm2m_client_object_t * objectList;
00554     lwm2m_observation_t *   observationList;
00555 } lwm2m_client_t;
00556 
00557 
00558 /*
00559  * LWM2M transaction
00560  *
00561  * Adaptation of Erbium's coap_transaction_t
00562  */
00563 
00564 typedef struct _lwm2m_transaction_ lwm2m_transaction_t;
00565 
00566 typedef void (*lwm2m_transaction_callback_t) (lwm2m_transaction_t * transacP, void * message);
00567 
00568 struct _lwm2m_transaction_
00569 {
00570     lwm2m_transaction_t * next;  // matches lwm2m_list_t::next
00571     uint16_t              mID;   // matches lwm2m_list_t::id
00572     void *                peerH;
00573     uint8_t               ack_received; // indicates, that the ACK was received
00574     time_t                response_timeout; // timeout to wait for response, if token is used. When 0, use calculated acknowledge timeout.
00575     uint8_t  retrans_counter;
00576     time_t   retrans_time;
00577     char objStringID[LWM2M_STRING_ID_MAX_LEN];
00578     char instanceStringID[LWM2M_STRING_ID_MAX_LEN];
00579     char resourceStringID[LWM2M_STRING_ID_MAX_LEN];
00580     void * message;
00581     uint16_t buffer_len;
00582     uint8_t * buffer;
00583     lwm2m_transaction_callback_t callback;
00584     void * userData;
00585 };
00586 
00587 /*
00588  * LWM2M observed resources
00589  */
00590 typedef struct _lwm2m_watcher_
00591 {
00592     struct _lwm2m_watcher_ * next;
00593 
00594     bool active;
00595     bool update;
00596     lwm2m_server_t * server;
00597     lwm2m_attributes_t * parameters;
00598     uint8_t token[8];
00599     size_t tokenLen;
00600     time_t lastTime;
00601     uint32_t counter;
00602     uint16_t lastMid;
00603     union
00604     {
00605         int64_t asInteger;
00606         double  asFloat;
00607     } lastValue;
00608 } lwm2m_watcher_t;
00609 
00610 typedef struct _lwm2m_observed_
00611 {
00612     struct _lwm2m_observed_ * next;
00613 
00614     lwm2m_uri_t uri;
00615     lwm2m_watcher_t * watcherList;
00616 } lwm2m_observed_t;
00617 
00618 #ifdef LWM2M_CLIENT_MODE
00619 
00620 typedef enum
00621 {
00622     STATE_INITIAL = 0,
00623     STATE_BOOTSTRAP_REQUIRED,
00624     STATE_BOOTSTRAPPING,
00625     STATE_REGISTER_REQUIRED,
00626     STATE_REGISTERING,
00627     STATE_READY
00628 } lwm2m_client_state_t;
00629 
00630 #endif
00631 /*
00632  * LWM2M Context
00633  */
00634 
00635 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
00636 // In all the following APIs, the session handle MUST uniquely identify a peer.
00637 
00638 // LWM2M bootstrap callback
00639 // When a LWM2M client requests bootstrap information, the callback is called with status COAP_NO_ERROR, uriP is nil and
00640 // name is set. The callback must return a COAP_* error code. COAP_204_CHANGED for success.
00641 // After a lwm2m_bootstrap_delete() or a lwm2m_bootstrap_write(), the callback is called with the status returned by the
00642 // client, the URI of the operation (may be nil) and name is nil. The callback return value is ignored.
00643 typedef int (*lwm2m_bootstrap_callback_t) (void * sessionH, uint8_t status, lwm2m_uri_t * uriP, char * name, void * userData);
00644 #endif
00645 
00646 typedef struct
00647 {
00648 #ifdef LWM2M_CLIENT_MODE
00649     lwm2m_client_state_t state;
00650     char *               endpointName;
00651     char *               msisdn;
00652     char *               altPath;
00653     lwm2m_server_t *     bootstrapServerList;
00654     lwm2m_server_t *     serverList;
00655     lwm2m_object_t *     objectList;
00656     lwm2m_observed_t *   observedList;
00657 #endif
00658 #ifdef LWM2M_SERVER_MODE
00659     lwm2m_client_t *        clientList;
00660     lwm2m_result_callback_t monitorCallback;
00661     void *                  monitorUserData;
00662 #endif
00663 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
00664     lwm2m_bootstrap_callback_t bootstrapCallback;
00665     void *                     bootstrapUserData;
00666 #endif
00667     uint16_t                nextMID;
00668     lwm2m_transaction_t *   transactionList;
00669     void *                  userData;
00670 } lwm2m_context_t;
00671 
00672 
00673 // initialize a liblwm2m context.
00674 lwm2m_context_t * lwm2m_init(void * userData);
00675 // close a liblwm2m context.
00676 void lwm2m_close(lwm2m_context_t * contextP);
00677 
00678 // perform any required pending operation and adjust timeoutP to the maximal time interval to wait in seconds.
00679 int lwm2m_step(lwm2m_context_t * contextP, time_t * timeoutP);
00680 // dispatch received data to liblwm2m
00681 void lwm2m_handle_packet(lwm2m_context_t * contextP, uint8_t * buffer, int length, void * fromSessionH);
00682 
00683 #ifdef LWM2M_CLIENT_MODE
00684 // configure the client side with the Endpoint Name, binding, MSISDN (can be nil), alternative path
00685 // for objects (can be nil) and a list of objects.
00686 // LWM2M Security Object (ID 0) must be present with either a bootstrap server or a LWM2M server and
00687 // its matching LWM2M Server Object (ID 1) instance
00688 int lwm2m_configure(lwm2m_context_t * contextP, const char * endpointName, const char * msisdn, const char * altPath, uint16_t numObject, lwm2m_object_t * objectList[]);
00689 int lwm2m_add_object(lwm2m_context_t * contextP, lwm2m_object_t * objectP);
00690 int lwm2m_remove_object(lwm2m_context_t * contextP, uint16_t id);
00691 
00692 // send a registration update to the server specified by the server short identifier
00693 // or all if the ID is 0.
00694 // If withObjects is true, the registration update contains the object list.
00695 int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID, bool withObjects);
00696 
00697 void lwm2m_resource_value_changed(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
00698 #endif
00699 
00700 #ifdef LWM2M_SERVER_MODE
00701 // Clients registration/deregistration monitoring API.
00702 // When a LWM2M client registers, the callback is called with status COAP_201_CREATED.
00703 // When a LWM2M client deregisters, the callback is called with status COAP_202_DELETED.
00704 // clientID is the internal ID of the LWM2M Client.
00705 // The callback's parameters uri, data, dataLength are always NULL.
00706 // 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.
00707 void lwm2m_set_monitoring_callback(lwm2m_context_t * contextP, lwm2m_result_callback_t callback, void * userData);
00708 
00709 // Device Management APIs
00710 int lwm2m_dm_read(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
00711 int lwm2m_dm_discover(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
00712 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);
00713 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);
00714 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);
00715 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);
00716 int lwm2m_dm_delete(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
00717 
00718 // Information Reporting APIs
00719 int lwm2m_observe(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
00720 int lwm2m_observe_cancel(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
00721 #endif
00722 
00723 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
00724 // Clients bootstrap request monitoring API.
00725 // When a LWM2M client sends a bootstrap request, the callback is called with the client's endpoint name.
00726 void lwm2m_set_bootstrap_callback(lwm2m_context_t * contextP, lwm2m_bootstrap_callback_t callback, void * userData);
00727 
00728 // Boostrap Interface APIs
00729 // if uriP is nil, a "Delete /" is sent to the client
00730 int lwm2m_bootstrap_delete(lwm2m_context_t * contextP, void * sessionH, lwm2m_uri_t * uriP);
00731 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);
00732 int lwm2m_bootstrap_finish(lwm2m_context_t * contextP, void * sessionH);
00733 
00734 #endif
00735 
00736 #ifdef __cplusplus
00737 }
00738 #endif
00739 
00740 #endif