pick up wakaama files from https://github.com/eclipse/wakaama

Committer:
terencez
Date:
Wed Apr 19 11:30:02 2017 +0000
Revision:
0:1fa43ab66921
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terencez 0:1fa43ab66921 1 /*******************************************************************************
terencez 0:1fa43ab66921 2 *
terencez 0:1fa43ab66921 3 * Copyright (c) 2013, 2014 Intel Corporation and others.
terencez 0:1fa43ab66921 4 * All rights reserved. This program and the accompanying materials
terencez 0:1fa43ab66921 5 * are made available under the terms of the Eclipse Public License v1.0
terencez 0:1fa43ab66921 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
terencez 0:1fa43ab66921 7 *
terencez 0:1fa43ab66921 8 * The Eclipse Public License is available at
terencez 0:1fa43ab66921 9 * http://www.eclipse.org/legal/epl-v10.html
terencez 0:1fa43ab66921 10 * The Eclipse Distribution License is available at
terencez 0:1fa43ab66921 11 * http://www.eclipse.org/org/documents/edl-v10.php.
terencez 0:1fa43ab66921 12 *
terencez 0:1fa43ab66921 13 * Contributors:
terencez 0:1fa43ab66921 14 * David Navarro, Intel Corporation - initial API and implementation
terencez 0:1fa43ab66921 15 * Fabien Fleutot - Please refer to git log
terencez 0:1fa43ab66921 16 * Simon Bernard - Please refer to git log
terencez 0:1fa43ab66921 17 * Toby Jaffey - Please refer to git log
terencez 0:1fa43ab66921 18 * Julien Vermillard - Please refer to git log
terencez 0:1fa43ab66921 19 * Bosch Software Innovations GmbH - Please refer to git log
terencez 0:1fa43ab66921 20 * Pascal Rieux - Please refer to git log
terencez 0:1fa43ab66921 21 *******************************************************************************/
terencez 0:1fa43ab66921 22
terencez 0:1fa43ab66921 23 /*
terencez 0:1fa43ab66921 24 Copyright (c) 2013, 2014 Intel Corporation
terencez 0:1fa43ab66921 25
terencez 0:1fa43ab66921 26 Redistribution and use in source and binary forms, with or without modification,
terencez 0:1fa43ab66921 27 are permitted provided that the following conditions are met:
terencez 0:1fa43ab66921 28
terencez 0:1fa43ab66921 29 * Redistributions of source code must retain the above copyright notice,
terencez 0:1fa43ab66921 30 this list of conditions and the following disclaimer.
terencez 0:1fa43ab66921 31 * Redistributions in binary form must reproduce the above copyright notice,
terencez 0:1fa43ab66921 32 this list of conditions and the following disclaimer in the documentation
terencez 0:1fa43ab66921 33 and/or other materials provided with the distribution.
terencez 0:1fa43ab66921 34 * Neither the name of Intel Corporation nor the names of its contributors
terencez 0:1fa43ab66921 35 may be used to endorse or promote products derived from this software
terencez 0:1fa43ab66921 36 without specific prior written permission.
terencez 0:1fa43ab66921 37
terencez 0:1fa43ab66921 38 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
terencez 0:1fa43ab66921 39 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
terencez 0:1fa43ab66921 40 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
terencez 0:1fa43ab66921 41 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
terencez 0:1fa43ab66921 42 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
terencez 0:1fa43ab66921 43 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
terencez 0:1fa43ab66921 44 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
terencez 0:1fa43ab66921 45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
terencez 0:1fa43ab66921 46 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
terencez 0:1fa43ab66921 47 THE POSSIBILITY OF SUCH DAMAGE.
terencez 0:1fa43ab66921 48
terencez 0:1fa43ab66921 49 David Navarro <david.navarro@intel.com>
terencez 0:1fa43ab66921 50
terencez 0:1fa43ab66921 51 */
terencez 0:1fa43ab66921 52
terencez 0:1fa43ab66921 53 #ifndef _LWM2M_CLIENT_H_
terencez 0:1fa43ab66921 54 #define _LWM2M_CLIENT_H_
terencez 0:1fa43ab66921 55
terencez 0:1fa43ab66921 56 #ifdef __cplusplus
terencez 0:1fa43ab66921 57 extern "C" {
terencez 0:1fa43ab66921 58 #endif
terencez 0:1fa43ab66921 59
terencez 0:1fa43ab66921 60 #include <stdint.h>
terencez 0:1fa43ab66921 61 #include <stddef.h>
terencez 0:1fa43ab66921 62 #include <stdbool.h>
terencez 0:1fa43ab66921 63 #include <time.h>
terencez 0:1fa43ab66921 64
terencez 0:1fa43ab66921 65 #ifdef LWM2M_SERVER_MODE
terencez 0:1fa43ab66921 66 #ifndef LWM2M_SUPPORT_JSON
terencez 0:1fa43ab66921 67 #define LWM2M_SUPPORT_JSON
terencez 0:1fa43ab66921 68 #endif
terencez 0:1fa43ab66921 69 #endif
terencez 0:1fa43ab66921 70
terencez 0:1fa43ab66921 71 #if defined(LWM2M_BOOTSTRAP) && defined(LWM2M_BOOTSTRAP_SERVER_MODE)
terencez 0:1fa43ab66921 72 #error "LWM2M_BOOTSTRAP and LWM2M_BOOTSTRAP_SERVER_MODE cannot be defined at the same time!"
terencez 0:1fa43ab66921 73 #endif
terencez 0:1fa43ab66921 74
terencez 0:1fa43ab66921 75 /*
terencez 0:1fa43ab66921 76 * Platform abstraction functions to be implemented by the user
terencez 0:1fa43ab66921 77 */
terencez 0:1fa43ab66921 78
terencez 0:1fa43ab66921 79 #ifndef LWM2M_MEMORY_TRACE
terencez 0:1fa43ab66921 80 // Allocate a block of size bytes of memory, returning a pointer to the beginning of the block.
terencez 0:1fa43ab66921 81 void * lwm2m_malloc(size_t s);
terencez 0:1fa43ab66921 82 // Deallocate a block of memory previously allocated by lwm2m_malloc() or lwm2m_strdup()
terencez 0:1fa43ab66921 83 void lwm2m_free(void * p);
terencez 0:1fa43ab66921 84 // Allocate a memory block, duplicate the string str in it and return a pointer to this new block.
terencez 0:1fa43ab66921 85 char * lwm2m_strdup(const char * str);
terencez 0:1fa43ab66921 86 #else
terencez 0:1fa43ab66921 87 // same functions as above with caller location for debugging purposes
terencez 0:1fa43ab66921 88 char * lwm2m_trace_strdup(const char * str, const char * file, const char * function, int lineno);
terencez 0:1fa43ab66921 89 void * lwm2m_trace_malloc(size_t size, const char * file, const char * function, int lineno);
terencez 0:1fa43ab66921 90 void lwm2m_trace_free(void * mem, const char * file, const char * function, int lineno);
terencez 0:1fa43ab66921 91
terencez 0:1fa43ab66921 92 #define lwm2m_strdup(S) lwm2m_trace_strdup(S, __FILE__, __FUNCTION__, __LINE__)
terencez 0:1fa43ab66921 93 #define lwm2m_malloc(S) lwm2m_trace_malloc(S, __FILE__, __FUNCTION__, __LINE__)
terencez 0:1fa43ab66921 94 #define lwm2m_free(M) lwm2m_trace_free(M, __FILE__, __FUNCTION__, __LINE__)
terencez 0:1fa43ab66921 95 #endif
terencez 0:1fa43ab66921 96 // Compare at most the n first bytes of s1 and s2, return 0 if they match
terencez 0:1fa43ab66921 97 int lwm2m_strncmp(const char * s1, const char * s2, size_t n);
terencez 0:1fa43ab66921 98 // This function must return the number of seconds elapsed since origin.
terencez 0:1fa43ab66921 99 // The origin (Epoch, system boot, etc...) does not matter as this
terencez 0:1fa43ab66921 100 // function is used only to determine the elapsed time since the last
terencez 0:1fa43ab66921 101 // call to it.
terencez 0:1fa43ab66921 102 // In case of error, this must return a negative value.
terencez 0:1fa43ab66921 103 // Per POSIX specifications, time_t is a signed integer.
terencez 0:1fa43ab66921 104 time_t lwm2m_gettime(void);
terencez 0:1fa43ab66921 105
terencez 0:1fa43ab66921 106 #ifdef LWM2M_WITH_LOGS
terencez 0:1fa43ab66921 107 // Same usage as C89 printf()
terencez 0:1fa43ab66921 108 void lwm2m_printf(const char * format, ...);
terencez 0:1fa43ab66921 109 #endif
terencez 0:1fa43ab66921 110
terencez 0:1fa43ab66921 111 // communication layer
terencez 0:1fa43ab66921 112 #ifdef LWM2M_CLIENT_MODE
terencez 0:1fa43ab66921 113 // Returns a session handle that MUST uniquely identify a peer.
terencez 0:1fa43ab66921 114 // secObjInstID: ID of the Securty Object instance to open a connection to
terencez 0:1fa43ab66921 115 // userData: parameter to lwm2m_init()
terencez 0:1fa43ab66921 116 void * lwm2m_connect_server(uint16_t secObjInstID, void * userData);
terencez 0:1fa43ab66921 117 // Close a session created by lwm2m_connect_server()
terencez 0:1fa43ab66921 118 // sessionH: session handle identifying the peer (opaque to the core)
terencez 0:1fa43ab66921 119 // userData: parameter to lwm2m_init()
terencez 0:1fa43ab66921 120 void lwm2m_close_connection(void * sessionH, void * userData);
terencez 0:1fa43ab66921 121 #endif
terencez 0:1fa43ab66921 122 // Send data to a peer
terencez 0:1fa43ab66921 123 // Returns COAP_NO_ERROR or a COAP_NNN error code
terencez 0:1fa43ab66921 124 // sessionH: session handle identifying the peer (opaque to the core)
terencez 0:1fa43ab66921 125 // buffer, length: data to send
terencez 0:1fa43ab66921 126 // userData: parameter to lwm2m_init()
terencez 0:1fa43ab66921 127 uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void * userData);
terencez 0:1fa43ab66921 128 // Compare two session handles
terencez 0:1fa43ab66921 129 // Returns true if the two sessions identify the same peer. false otherwise.
terencez 0:1fa43ab66921 130 // userData: parameter to lwm2m_init()
terencez 0:1fa43ab66921 131 bool lwm2m_session_is_equal(void * session1, void * session2, void * userData);
terencez 0:1fa43ab66921 132
terencez 0:1fa43ab66921 133 /*
terencez 0:1fa43ab66921 134 * Error code
terencez 0:1fa43ab66921 135 */
terencez 0:1fa43ab66921 136
terencez 0:1fa43ab66921 137 #define COAP_NO_ERROR (uint8_t)0x00
terencez 0:1fa43ab66921 138 #define COAP_IGNORE (uint8_t)0x01
terencez 0:1fa43ab66921 139
terencez 0:1fa43ab66921 140 #define COAP_201_CREATED (uint8_t)0x41
terencez 0:1fa43ab66921 141 #define COAP_202_DELETED (uint8_t)0x42
terencez 0:1fa43ab66921 142 #define COAP_204_CHANGED (uint8_t)0x44
terencez 0:1fa43ab66921 143 #define COAP_205_CONTENT (uint8_t)0x45
terencez 0:1fa43ab66921 144 #define COAP_231_CONTINUE (uint8_t)0x5F
terencez 0:1fa43ab66921 145 #define COAP_400_BAD_REQUEST (uint8_t)0x80
terencez 0:1fa43ab66921 146 #define COAP_401_UNAUTHORIZED (uint8_t)0x81
terencez 0:1fa43ab66921 147 #define COAP_402_BAD_OPTION (uint8_t)0x82
terencez 0:1fa43ab66921 148 #define COAP_404_NOT_FOUND (uint8_t)0x84
terencez 0:1fa43ab66921 149 #define COAP_405_METHOD_NOT_ALLOWED (uint8_t)0x85
terencez 0:1fa43ab66921 150 #define COAP_406_NOT_ACCEPTABLE (uint8_t)0x86
terencez 0:1fa43ab66921 151 #define COAP_408_REQ_ENTITY_INCOMPLETE (uint8_t)0x88
terencez 0:1fa43ab66921 152 #define COAP_412_PRECONDITION_FAILED (uint8_t)0x8C
terencez 0:1fa43ab66921 153 #define COAP_413_ENTITY_TOO_LARGE (uint8_t)0x8D
terencez 0:1fa43ab66921 154 #define COAP_500_INTERNAL_SERVER_ERROR (uint8_t)0xA0
terencez 0:1fa43ab66921 155 #define COAP_501_NOT_IMPLEMENTED (uint8_t)0xA1
terencez 0:1fa43ab66921 156 #define COAP_503_SERVICE_UNAVAILABLE (uint8_t)0xA3
terencez 0:1fa43ab66921 157
terencez 0:1fa43ab66921 158 /*
terencez 0:1fa43ab66921 159 * Standard Object IDs
terencez 0:1fa43ab66921 160 */
terencez 0:1fa43ab66921 161 #define LWM2M_SECURITY_OBJECT_ID 0
terencez 0:1fa43ab66921 162 #define LWM2M_SERVER_OBJECT_ID 1
terencez 0:1fa43ab66921 163 #define LWM2M_ACL_OBJECT_ID 2
terencez 0:1fa43ab66921 164 #define LWM2M_DEVICE_OBJECT_ID 3
terencez 0:1fa43ab66921 165 #define LWM2M_CONN_MONITOR_OBJECT_ID 4
terencez 0:1fa43ab66921 166 #define LWM2M_FIRMWARE_UPDATE_OBJECT_ID 5
terencez 0:1fa43ab66921 167 #define LWM2M_LOCATION_OBJECT_ID 6
terencez 0:1fa43ab66921 168 #define LWM2M_CONN_STATS_OBJECT_ID 7
terencez 0:1fa43ab66921 169
terencez 0:1fa43ab66921 170 /*
terencez 0:1fa43ab66921 171 * Ressource IDs for the LWM2M Security Object
terencez 0:1fa43ab66921 172 */
terencez 0:1fa43ab66921 173 #define LWM2M_SECURITY_URI_ID 0
terencez 0:1fa43ab66921 174 #define LWM2M_SECURITY_BOOTSTRAP_ID 1
terencez 0:1fa43ab66921 175 #define LWM2M_SECURITY_SECURITY_ID 2
terencez 0:1fa43ab66921 176 #define LWM2M_SECURITY_PUBLIC_KEY_ID 3
terencez 0:1fa43ab66921 177 #define LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID 4
terencez 0:1fa43ab66921 178 #define LWM2M_SECURITY_SECRET_KEY_ID 5
terencez 0:1fa43ab66921 179 #define LWM2M_SECURITY_SMS_SECURITY_ID 6
terencez 0:1fa43ab66921 180 #define LWM2M_SECURITY_SMS_KEY_PARAM_ID 7
terencez 0:1fa43ab66921 181 #define LWM2M_SECURITY_SMS_SECRET_KEY_ID 8
terencez 0:1fa43ab66921 182 #define LWM2M_SECURITY_SMS_SERVER_NUMBER_ID 9
terencez 0:1fa43ab66921 183 #define LWM2M_SECURITY_SHORT_SERVER_ID 10
terencez 0:1fa43ab66921 184 #define LWM2M_SECURITY_HOLD_OFF_ID 11
terencez 0:1fa43ab66921 185 #define LWM2M_SECURITY_BOOTSTRAP_TIMEOUT_ID 12
terencez 0:1fa43ab66921 186
terencez 0:1fa43ab66921 187 /*
terencez 0:1fa43ab66921 188 * Ressource IDs for the LWM2M Server Object
terencez 0:1fa43ab66921 189 */
terencez 0:1fa43ab66921 190 #define LWM2M_SERVER_SHORT_ID_ID 0
terencez 0:1fa43ab66921 191 #define LWM2M_SERVER_LIFETIME_ID 1
terencez 0:1fa43ab66921 192 #define LWM2M_SERVER_MIN_PERIOD_ID 2
terencez 0:1fa43ab66921 193 #define LWM2M_SERVER_MAX_PERIOD_ID 3
terencez 0:1fa43ab66921 194 #define LWM2M_SERVER_DISABLE_ID 4
terencez 0:1fa43ab66921 195 #define LWM2M_SERVER_TIMEOUT_ID 5
terencez 0:1fa43ab66921 196 #define LWM2M_SERVER_STORING_ID 6
terencez 0:1fa43ab66921 197 #define LWM2M_SERVER_BINDING_ID 7
terencez 0:1fa43ab66921 198 #define LWM2M_SERVER_UPDATE_ID 8
terencez 0:1fa43ab66921 199
terencez 0:1fa43ab66921 200 #define LWM2M_SECURITY_MODE_PRE_SHARED_KEY 0
terencez 0:1fa43ab66921 201 #define LWM2M_SECURITY_MODE_RAW_PUBLIC_KEY 1
terencez 0:1fa43ab66921 202 #define LWM2M_SECURITY_MODE_CERTIFICATE 2
terencez 0:1fa43ab66921 203 #define LWM2M_SECURITY_MODE_NONE 3
terencez 0:1fa43ab66921 204
terencez 0:1fa43ab66921 205
terencez 0:1fa43ab66921 206 /*
terencez 0:1fa43ab66921 207 * Utility functions for sorted linked list
terencez 0:1fa43ab66921 208 */
terencez 0:1fa43ab66921 209
terencez 0:1fa43ab66921 210 typedef struct _lwm2m_list_t
terencez 0:1fa43ab66921 211 {
terencez 0:1fa43ab66921 212 struct _lwm2m_list_t * next;
terencez 0:1fa43ab66921 213 uint16_t id;
terencez 0:1fa43ab66921 214 } lwm2m_list_t;
terencez 0:1fa43ab66921 215
terencez 0:1fa43ab66921 216 // defined in list.c
terencez 0:1fa43ab66921 217 // Add 'node' to the list 'head' and return the new list
terencez 0:1fa43ab66921 218 lwm2m_list_t * lwm2m_list_add(lwm2m_list_t * head, lwm2m_list_t * node);
terencez 0:1fa43ab66921 219 // Return the node with ID 'id' from the list 'head' or NULL if not found
terencez 0:1fa43ab66921 220 lwm2m_list_t * lwm2m_list_find(lwm2m_list_t * head, uint16_t id);
terencez 0:1fa43ab66921 221 // Remove the node with ID 'id' from the list 'head' and return the new list
terencez 0:1fa43ab66921 222 lwm2m_list_t * lwm2m_list_remove(lwm2m_list_t * head, uint16_t id, lwm2m_list_t ** nodeP);
terencez 0:1fa43ab66921 223 // Return the lowest unused ID in the list 'head'
terencez 0:1fa43ab66921 224 uint16_t lwm2m_list_newId(lwm2m_list_t * head);
terencez 0:1fa43ab66921 225 // Free a list. Do not use if nodes contain allocated pointers as it calls lwm2m_free on nodes only.
terencez 0:1fa43ab66921 226 // If the nodes of the list need to do more than just "free()" their instances, don't use lwm2m_list_free().
terencez 0:1fa43ab66921 227 void lwm2m_list_free(lwm2m_list_t * head);
terencez 0:1fa43ab66921 228
terencez 0:1fa43ab66921 229 #define LWM2M_LIST_ADD(H,N) lwm2m_list_add((lwm2m_list_t *)H, (lwm2m_list_t *)N);
terencez 0:1fa43ab66921 230 #define LWM2M_LIST_RM(H,I,N) lwm2m_list_remove((lwm2m_list_t *)H, I, (lwm2m_list_t **)N);
terencez 0:1fa43ab66921 231 #define LWM2M_LIST_FIND(H,I) lwm2m_list_find((lwm2m_list_t *)H, I)
terencez 0:1fa43ab66921 232 #define LWM2M_LIST_FREE(H) lwm2m_list_free((lwm2m_list_t *)H)
terencez 0:1fa43ab66921 233
terencez 0:1fa43ab66921 234 /*
terencez 0:1fa43ab66921 235 * URI
terencez 0:1fa43ab66921 236 *
terencez 0:1fa43ab66921 237 * objectId is always set
terencez 0:1fa43ab66921 238 * instanceId or resourceId are set according to the flag bit-field
terencez 0:1fa43ab66921 239 *
terencez 0:1fa43ab66921 240 */
terencez 0:1fa43ab66921 241
terencez 0:1fa43ab66921 242 #define LWM2M_MAX_ID ((uint16_t)0xFFFF)
terencez 0:1fa43ab66921 243
terencez 0:1fa43ab66921 244 #define LWM2M_URI_FLAG_OBJECT_ID (uint8_t)0x04
terencez 0:1fa43ab66921 245 #define LWM2M_URI_FLAG_INSTANCE_ID (uint8_t)0x02
terencez 0:1fa43ab66921 246 #define LWM2M_URI_FLAG_RESOURCE_ID (uint8_t)0x01
terencez 0:1fa43ab66921 247
terencez 0:1fa43ab66921 248 #define LWM2M_URI_IS_SET_INSTANCE(uri) (((uri)->flag & LWM2M_URI_FLAG_INSTANCE_ID) != 0)
terencez 0:1fa43ab66921 249 #define LWM2M_URI_IS_SET_RESOURCE(uri) (((uri)->flag & LWM2M_URI_FLAG_RESOURCE_ID) != 0)
terencez 0:1fa43ab66921 250
terencez 0:1fa43ab66921 251 typedef struct
terencez 0:1fa43ab66921 252 {
terencez 0:1fa43ab66921 253 uint8_t flag; // indicates which segments are set
terencez 0:1fa43ab66921 254 uint16_t objectId;
terencez 0:1fa43ab66921 255 uint16_t instanceId;
terencez 0:1fa43ab66921 256 uint16_t resourceId;
terencez 0:1fa43ab66921 257 } lwm2m_uri_t;
terencez 0:1fa43ab66921 258
terencez 0:1fa43ab66921 259
terencez 0:1fa43ab66921 260 #define LWM2M_STRING_ID_MAX_LEN 6
terencez 0:1fa43ab66921 261
terencez 0:1fa43ab66921 262 // Parse an URI in LWM2M format and fill the lwm2m_uri_t.
terencez 0:1fa43ab66921 263 // Return the number of characters read from buffer or 0 in case of error.
terencez 0:1fa43ab66921 264 // Valid URIs: /1, /1/, /1/2, /1/2/, /1/2/3
terencez 0:1fa43ab66921 265 // Invalid URIs: /, //, //2, /1//, /1//3, /1/2/3/, /1/2/3/4
terencez 0:1fa43ab66921 266 int lwm2m_stringToUri(const char * buffer, size_t buffer_len, lwm2m_uri_t * uriP);
terencez 0:1fa43ab66921 267
terencez 0:1fa43ab66921 268 /*
terencez 0:1fa43ab66921 269 * The lwm2m_data_t is used to store LWM2M resource values in a hierarchical way.
terencez 0:1fa43ab66921 270 * Depending on the type the value is different:
terencez 0:1fa43ab66921 271 * - LWM2M_TYPE_OBJECT, LWM2M_TYPE_OBJECT_INSTANCE, LWM2M_TYPE_MULTIPLE_RESOURCE: value.asChildren
terencez 0:1fa43ab66921 272 * - LWM2M_TYPE_STRING, LWM2M_TYPE_OPAQUE: value.asBuffer
terencez 0:1fa43ab66921 273 * - LWM2M_TYPE_INTEGER, LWM2M_TYPE_TIME: value.asInteger
terencez 0:1fa43ab66921 274 * - LWM2M_TYPE_FLOAT: value.asFloat
terencez 0:1fa43ab66921 275 * - LWM2M_TYPE_BOOLEAN: value.asBoolean
terencez 0:1fa43ab66921 276 *
terencez 0:1fa43ab66921 277 * LWM2M_TYPE_STRING is also used when the data is in text format.
terencez 0:1fa43ab66921 278 */
terencez 0:1fa43ab66921 279
terencez 0:1fa43ab66921 280 typedef enum
terencez 0:1fa43ab66921 281 {
terencez 0:1fa43ab66921 282 LWM2M_TYPE_UNDEFINED = 0,
terencez 0:1fa43ab66921 283 LWM2M_TYPE_OBJECT,
terencez 0:1fa43ab66921 284 LWM2M_TYPE_OBJECT_INSTANCE,
terencez 0:1fa43ab66921 285 LWM2M_TYPE_MULTIPLE_RESOURCE,
terencez 0:1fa43ab66921 286
terencez 0:1fa43ab66921 287 LWM2M_TYPE_STRING,
terencez 0:1fa43ab66921 288 LWM2M_TYPE_OPAQUE,
terencez 0:1fa43ab66921 289 LWM2M_TYPE_INTEGER,
terencez 0:1fa43ab66921 290 LWM2M_TYPE_FLOAT,
terencez 0:1fa43ab66921 291 LWM2M_TYPE_BOOLEAN,
terencez 0:1fa43ab66921 292
terencez 0:1fa43ab66921 293 LWM2M_TYPE_OBJECT_LINK
terencez 0:1fa43ab66921 294 } lwm2m_data_type_t;
terencez 0:1fa43ab66921 295
terencez 0:1fa43ab66921 296 typedef struct _lwm2m_data_t lwm2m_data_t;
terencez 0:1fa43ab66921 297
terencez 0:1fa43ab66921 298 struct _lwm2m_data_t
terencez 0:1fa43ab66921 299 {
terencez 0:1fa43ab66921 300 lwm2m_data_type_t type;
terencez 0:1fa43ab66921 301 uint16_t id;
terencez 0:1fa43ab66921 302 union
terencez 0:1fa43ab66921 303 {
terencez 0:1fa43ab66921 304 bool asBoolean;
terencez 0:1fa43ab66921 305 int64_t asInteger;
terencez 0:1fa43ab66921 306 double asFloat;
terencez 0:1fa43ab66921 307 struct
terencez 0:1fa43ab66921 308 {
terencez 0:1fa43ab66921 309 size_t length;
terencez 0:1fa43ab66921 310 uint8_t * buffer;
terencez 0:1fa43ab66921 311 } asBuffer;
terencez 0:1fa43ab66921 312 struct
terencez 0:1fa43ab66921 313 {
terencez 0:1fa43ab66921 314 size_t count;
terencez 0:1fa43ab66921 315 lwm2m_data_t * array;
terencez 0:1fa43ab66921 316 } asChildren;
terencez 0:1fa43ab66921 317 struct
terencez 0:1fa43ab66921 318 {
terencez 0:1fa43ab66921 319 uint16_t objectId;
terencez 0:1fa43ab66921 320 uint16_t objectInstanceId;
terencez 0:1fa43ab66921 321 } asObjLink;
terencez 0:1fa43ab66921 322 } value;
terencez 0:1fa43ab66921 323 };
terencez 0:1fa43ab66921 324
terencez 0:1fa43ab66921 325 typedef enum
terencez 0:1fa43ab66921 326 {
terencez 0:1fa43ab66921 327 LWM2M_CONTENT_TEXT = 0, // Also used as undefined
terencez 0:1fa43ab66921 328 LWM2M_CONTENT_LINK = 40,
terencez 0:1fa43ab66921 329 LWM2M_CONTENT_OPAQUE = 42,
terencez 0:1fa43ab66921 330 LWM2M_CONTENT_TLV_OLD = 1542, // Keep old value for backward-compatibility
terencez 0:1fa43ab66921 331 LWM2M_CONTENT_TLV = 11542,
terencez 0:1fa43ab66921 332 LWM2M_CONTENT_JSON_OLD = 1543, // Keep old value for backward-compatibility
terencez 0:1fa43ab66921 333 LWM2M_CONTENT_JSON = 11543
terencez 0:1fa43ab66921 334 } lwm2m_media_type_t;
terencez 0:1fa43ab66921 335
terencez 0:1fa43ab66921 336 lwm2m_data_t * lwm2m_data_new(int size);
terencez 0:1fa43ab66921 337 int lwm2m_data_parse(lwm2m_uri_t * uriP, uint8_t * buffer, size_t bufferLen, lwm2m_media_type_t format, lwm2m_data_t ** dataP);
terencez 0:1fa43ab66921 338 int lwm2m_data_serialize(lwm2m_uri_t * uriP, int size, lwm2m_data_t * dataP, lwm2m_media_type_t * formatP, uint8_t ** bufferP);
terencez 0:1fa43ab66921 339 void lwm2m_data_free(int size, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 340
terencez 0:1fa43ab66921 341 void lwm2m_data_encode_string(const char * string, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 342 void lwm2m_data_encode_nstring(const char * string, size_t length, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 343 void lwm2m_data_encode_opaque(uint8_t * buffer, size_t length, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 344 void lwm2m_data_encode_int(int64_t value, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 345 int lwm2m_data_decode_int(const lwm2m_data_t * dataP, int64_t * valueP);
terencez 0:1fa43ab66921 346 void lwm2m_data_encode_float(double value, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 347 int lwm2m_data_decode_float(const lwm2m_data_t * dataP, double * valueP);
terencez 0:1fa43ab66921 348 void lwm2m_data_encode_bool(bool value, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 349 int lwm2m_data_decode_bool(const lwm2m_data_t * dataP, bool * valueP);
terencez 0:1fa43ab66921 350 void lwm2m_data_encode_objlink(uint16_t objectId, uint16_t objectInstanceId, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 351 void lwm2m_data_encode_instances(lwm2m_data_t * subDataP, size_t count, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 352 void lwm2m_data_include(lwm2m_data_t * subDataP, size_t count, lwm2m_data_t * dataP);
terencez 0:1fa43ab66921 353
terencez 0:1fa43ab66921 354
terencez 0:1fa43ab66921 355 /*
terencez 0:1fa43ab66921 356 * Utility function to parse TLV buffers directly
terencez 0:1fa43ab66921 357 *
terencez 0:1fa43ab66921 358 * Returned value: number of bytes parsed
terencez 0:1fa43ab66921 359 * buffer: buffer to parse
terencez 0:1fa43ab66921 360 * buffer_len: length in bytes of buffer
terencez 0:1fa43ab66921 361 * oType: (OUT) type of the parsed TLV record. can be:
terencez 0:1fa43ab66921 362 * - LWM2M_TYPE_OBJECT
terencez 0:1fa43ab66921 363 * - LWM2M_TYPE_OBJECT_INSTANCE
terencez 0:1fa43ab66921 364 * - LWM2M_TYPE_MULTIPLE_RESOURCE
terencez 0:1fa43ab66921 365 * - LWM2M_TYPE_OPAQUE
terencez 0:1fa43ab66921 366 * oID: (OUT) ID of the parsed TLV record
terencez 0:1fa43ab66921 367 * oDataIndex: (OUT) index of the data of the parsed TLV record in the buffer
terencez 0:1fa43ab66921 368 * oDataLen: (OUT) length of the data of the parsed TLV record
terencez 0:1fa43ab66921 369 */
terencez 0:1fa43ab66921 370
terencez 0:1fa43ab66921 371 #define LWM2M_TLV_HEADER_MAX_LENGTH 6
terencez 0:1fa43ab66921 372
terencez 0:1fa43ab66921 373 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);
terencez 0:1fa43ab66921 374
terencez 0:1fa43ab66921 375
terencez 0:1fa43ab66921 376 /*
terencez 0:1fa43ab66921 377 * LWM2M Objects
terencez 0:1fa43ab66921 378 *
terencez 0:1fa43ab66921 379 * For the read callback, if *numDataP is not zero, *dataArrayP is pre-allocated
terencez 0:1fa43ab66921 380 * and contains the list of resources to read.
terencez 0:1fa43ab66921 381 *
terencez 0:1fa43ab66921 382 */
terencez 0:1fa43ab66921 383
terencez 0:1fa43ab66921 384 typedef struct _lwm2m_object_t lwm2m_object_t;
terencez 0:1fa43ab66921 385
terencez 0:1fa43ab66921 386 typedef uint8_t (*lwm2m_read_callback_t) (uint16_t instanceId, int * numDataP, lwm2m_data_t ** dataArrayP, lwm2m_object_t * objectP);
terencez 0:1fa43ab66921 387 typedef uint8_t (*lwm2m_discover_callback_t) (uint16_t instanceId, int * numDataP, lwm2m_data_t ** dataArrayP, lwm2m_object_t * objectP);
terencez 0:1fa43ab66921 388 typedef uint8_t (*lwm2m_write_callback_t) (uint16_t instanceId, int numData, lwm2m_data_t * dataArray, lwm2m_object_t * objectP);
terencez 0:1fa43ab66921 389 typedef uint8_t (*lwm2m_execute_callback_t) (uint16_t instanceId, uint16_t resourceId, uint8_t * buffer, int length, lwm2m_object_t * objectP);
terencez 0:1fa43ab66921 390 typedef uint8_t (*lwm2m_create_callback_t) (uint16_t instanceId, int numData, lwm2m_data_t * dataArray, lwm2m_object_t * objectP);
terencez 0:1fa43ab66921 391 typedef uint8_t (*lwm2m_delete_callback_t) (uint16_t instanceId, lwm2m_object_t * objectP);
terencez 0:1fa43ab66921 392
terencez 0:1fa43ab66921 393 struct _lwm2m_object_t
terencez 0:1fa43ab66921 394 {
terencez 0:1fa43ab66921 395 struct _lwm2m_object_t * next; // for internal use only.
terencez 0:1fa43ab66921 396 uint16_t objID;
terencez 0:1fa43ab66921 397 lwm2m_list_t * instanceList;
terencez 0:1fa43ab66921 398 lwm2m_read_callback_t readFunc;
terencez 0:1fa43ab66921 399 lwm2m_write_callback_t writeFunc;
terencez 0:1fa43ab66921 400 lwm2m_execute_callback_t executeFunc;
terencez 0:1fa43ab66921 401 lwm2m_create_callback_t createFunc;
terencez 0:1fa43ab66921 402 lwm2m_delete_callback_t deleteFunc;
terencez 0:1fa43ab66921 403 lwm2m_discover_callback_t discoverFunc;
terencez 0:1fa43ab66921 404 void * userData;
terencez 0:1fa43ab66921 405 };
terencez 0:1fa43ab66921 406
terencez 0:1fa43ab66921 407 /*
terencez 0:1fa43ab66921 408 * LWM2M Servers
terencez 0:1fa43ab66921 409 *
terencez 0:1fa43ab66921 410 * Since LWM2M Server Object instances are not accessible to LWM2M servers,
terencez 0:1fa43ab66921 411 * there is no need to store them as lwm2m_objects_t
terencez 0:1fa43ab66921 412 */
terencez 0:1fa43ab66921 413
terencez 0:1fa43ab66921 414 typedef enum
terencez 0:1fa43ab66921 415 {
terencez 0:1fa43ab66921 416 STATE_DEREGISTERED = 0, // not registered or boostrap not started
terencez 0:1fa43ab66921 417 STATE_REG_PENDING, // registration pending
terencez 0:1fa43ab66921 418 STATE_REGISTERED, // successfully registered
terencez 0:1fa43ab66921 419 STATE_REG_FAILED, // last registration failed
terencez 0:1fa43ab66921 420 STATE_REG_UPDATE_PENDING, // registration update pending
terencez 0:1fa43ab66921 421 STATE_REG_UPDATE_NEEDED, // registration update required
terencez 0:1fa43ab66921 422 STATE_REG_FULL_UPDATE_NEEDED, // registration update with objects required
terencez 0:1fa43ab66921 423 STATE_DEREG_PENDING, // deregistration pending
terencez 0:1fa43ab66921 424 STATE_BS_HOLD_OFF, // bootstrap hold off time
terencez 0:1fa43ab66921 425 STATE_BS_INITIATED, // bootstrap request sent
terencez 0:1fa43ab66921 426 STATE_BS_PENDING, // boostrap ongoing
terencez 0:1fa43ab66921 427 STATE_BS_FINISHING, // boostrap finish received
terencez 0:1fa43ab66921 428 STATE_BS_FINISHED, // bootstrap done
terencez 0:1fa43ab66921 429 STATE_BS_FAILING, // bootstrap error occurred
terencez 0:1fa43ab66921 430 STATE_BS_FAILED, // bootstrap failed
terencez 0:1fa43ab66921 431 } lwm2m_status_t;
terencez 0:1fa43ab66921 432
terencez 0:1fa43ab66921 433 typedef enum
terencez 0:1fa43ab66921 434 {
terencez 0:1fa43ab66921 435 BINDING_UNKNOWN = 0,
terencez 0:1fa43ab66921 436 BINDING_U, // UDP
terencez 0:1fa43ab66921 437 BINDING_UQ, // UDP queue mode
terencez 0:1fa43ab66921 438 BINDING_S, // SMS
terencez 0:1fa43ab66921 439 BINDING_SQ, // SMS queue mode
terencez 0:1fa43ab66921 440 BINDING_US, // UDP plus SMS
terencez 0:1fa43ab66921 441 BINDING_UQS // UDP queue mode plus SMS
terencez 0:1fa43ab66921 442 } lwm2m_binding_t;
terencez 0:1fa43ab66921 443
terencez 0:1fa43ab66921 444 /*
terencez 0:1fa43ab66921 445 * LWM2M block1 data
terencez 0:1fa43ab66921 446 *
terencez 0:1fa43ab66921 447 * Temporary data needed to handle block1 request.
terencez 0:1fa43ab66921 448 * Currently support only one block1 request by server.
terencez 0:1fa43ab66921 449 */
terencez 0:1fa43ab66921 450 typedef struct _lwm2m_block1_data_ lwm2m_block1_data_t;
terencez 0:1fa43ab66921 451
terencez 0:1fa43ab66921 452 struct _lwm2m_block1_data_
terencez 0:1fa43ab66921 453 {
terencez 0:1fa43ab66921 454 uint8_t * block1buffer; // data buffer
terencez 0:1fa43ab66921 455 size_t block1bufferSize; // buffer size
terencez 0:1fa43ab66921 456 uint16_t lastmid; // mid of the last message received
terencez 0:1fa43ab66921 457 };
terencez 0:1fa43ab66921 458
terencez 0:1fa43ab66921 459 typedef struct _lwm2m_server_
terencez 0:1fa43ab66921 460 {
terencez 0:1fa43ab66921 461 struct _lwm2m_server_ * next; // matches lwm2m_list_t::next
terencez 0:1fa43ab66921 462 uint16_t secObjInstID; // matches lwm2m_list_t::id
terencez 0:1fa43ab66921 463 uint16_t shortID; // servers short ID, may be 0 for bootstrap server
terencez 0:1fa43ab66921 464 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
terencez 0:1fa43ab66921 465 time_t registration; // date of the last registration in sec or end of client hold off time for bootstrap servers
terencez 0:1fa43ab66921 466 lwm2m_binding_t binding; // client connection mode with this server
terencez 0:1fa43ab66921 467 void * sessionH;
terencez 0:1fa43ab66921 468 lwm2m_status_t status;
terencez 0:1fa43ab66921 469 char * location;
terencez 0:1fa43ab66921 470 bool dirty;
terencez 0:1fa43ab66921 471 lwm2m_block1_data_t * block1Data; // buffer to handle block1 data, should be replace by a list to support several block1 transfer by server.
terencez 0:1fa43ab66921 472 } lwm2m_server_t;
terencez 0:1fa43ab66921 473
terencez 0:1fa43ab66921 474
terencez 0:1fa43ab66921 475 /*
terencez 0:1fa43ab66921 476 * LWM2M result callback
terencez 0:1fa43ab66921 477 *
terencez 0:1fa43ab66921 478 * When used with an observe, if 'data' is not nil, 'status' holds the observe counter.
terencez 0:1fa43ab66921 479 */
terencez 0:1fa43ab66921 480 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);
terencez 0:1fa43ab66921 481
terencez 0:1fa43ab66921 482 /*
terencez 0:1fa43ab66921 483 * LWM2M Observations
terencez 0:1fa43ab66921 484 *
terencez 0:1fa43ab66921 485 * Used to store observation of remote clients resources.
terencez 0:1fa43ab66921 486 * status STATE_REG_PENDING means the observe request was sent to the client but not yet answered.
terencez 0:1fa43ab66921 487 * status STATE_REGISTERED means the client acknowledged the observe request.
terencez 0:1fa43ab66921 488 * status STATE_DEREG_PENDING means the user canceled the request before the client answered it.
terencez 0:1fa43ab66921 489 */
terencez 0:1fa43ab66921 490
terencez 0:1fa43ab66921 491 typedef struct _lwm2m_observation_
terencez 0:1fa43ab66921 492 {
terencez 0:1fa43ab66921 493 struct _lwm2m_observation_ * next; // matches lwm2m_list_t::next
terencez 0:1fa43ab66921 494 uint16_t id; // matches lwm2m_list_t::id
terencez 0:1fa43ab66921 495 struct _lwm2m_client_ * clientP;
terencez 0:1fa43ab66921 496 lwm2m_uri_t uri;
terencez 0:1fa43ab66921 497 lwm2m_status_t status;
terencez 0:1fa43ab66921 498 lwm2m_result_callback_t callback;
terencez 0:1fa43ab66921 499 void * userData;
terencez 0:1fa43ab66921 500 } lwm2m_observation_t;
terencez 0:1fa43ab66921 501
terencez 0:1fa43ab66921 502 /*
terencez 0:1fa43ab66921 503 * LWM2M Link Attributes
terencez 0:1fa43ab66921 504 *
terencez 0:1fa43ab66921 505 * Used for observation parameters.
terencez 0:1fa43ab66921 506 *
terencez 0:1fa43ab66921 507 */
terencez 0:1fa43ab66921 508
terencez 0:1fa43ab66921 509 #define LWM2M_ATTR_FLAG_MIN_PERIOD (uint8_t)0x01
terencez 0:1fa43ab66921 510 #define LWM2M_ATTR_FLAG_MAX_PERIOD (uint8_t)0x02
terencez 0:1fa43ab66921 511 #define LWM2M_ATTR_FLAG_GREATER_THAN (uint8_t)0x04
terencez 0:1fa43ab66921 512 #define LWM2M_ATTR_FLAG_LESS_THAN (uint8_t)0x08
terencez 0:1fa43ab66921 513 #define LWM2M_ATTR_FLAG_STEP (uint8_t)0x10
terencez 0:1fa43ab66921 514
terencez 0:1fa43ab66921 515 typedef struct
terencez 0:1fa43ab66921 516 {
terencez 0:1fa43ab66921 517 uint8_t toSet;
terencez 0:1fa43ab66921 518 uint8_t toClear;
terencez 0:1fa43ab66921 519 uint32_t minPeriod;
terencez 0:1fa43ab66921 520 uint32_t maxPeriod;
terencez 0:1fa43ab66921 521 double greaterThan;
terencez 0:1fa43ab66921 522 double lessThan;
terencez 0:1fa43ab66921 523 double step;
terencez 0:1fa43ab66921 524 } lwm2m_attributes_t;
terencez 0:1fa43ab66921 525
terencez 0:1fa43ab66921 526 /*
terencez 0:1fa43ab66921 527 * LWM2M Clients
terencez 0:1fa43ab66921 528 *
terencez 0:1fa43ab66921 529 * Be careful not to mix lwm2m_client_object_t used to store list of objects of remote clients
terencez 0:1fa43ab66921 530 * and lwm2m_object_t describing objects exposed to remote servers.
terencez 0:1fa43ab66921 531 *
terencez 0:1fa43ab66921 532 */
terencez 0:1fa43ab66921 533
terencez 0:1fa43ab66921 534 typedef struct _lwm2m_client_object_
terencez 0:1fa43ab66921 535 {
terencez 0:1fa43ab66921 536 struct _lwm2m_client_object_ * next; // matches lwm2m_list_t::next
terencez 0:1fa43ab66921 537 uint16_t id; // matches lwm2m_list_t::id
terencez 0:1fa43ab66921 538 lwm2m_list_t * instanceList;
terencez 0:1fa43ab66921 539 } lwm2m_client_object_t;
terencez 0:1fa43ab66921 540
terencez 0:1fa43ab66921 541 typedef struct _lwm2m_client_
terencez 0:1fa43ab66921 542 {
terencez 0:1fa43ab66921 543 struct _lwm2m_client_ * next; // matches lwm2m_list_t::next
terencez 0:1fa43ab66921 544 uint16_t internalID; // matches lwm2m_list_t::id
terencez 0:1fa43ab66921 545 char * name;
terencez 0:1fa43ab66921 546 lwm2m_binding_t binding;
terencez 0:1fa43ab66921 547 char * msisdn;
terencez 0:1fa43ab66921 548 char * altPath;
terencez 0:1fa43ab66921 549 bool supportJSON;
terencez 0:1fa43ab66921 550 uint32_t lifetime;
terencez 0:1fa43ab66921 551 time_t endOfLife;
terencez 0:1fa43ab66921 552 void * sessionH;
terencez 0:1fa43ab66921 553 lwm2m_client_object_t * objectList;
terencez 0:1fa43ab66921 554 lwm2m_observation_t * observationList;
terencez 0:1fa43ab66921 555 } lwm2m_client_t;
terencez 0:1fa43ab66921 556
terencez 0:1fa43ab66921 557
terencez 0:1fa43ab66921 558 /*
terencez 0:1fa43ab66921 559 * LWM2M transaction
terencez 0:1fa43ab66921 560 *
terencez 0:1fa43ab66921 561 * Adaptation of Erbium's coap_transaction_t
terencez 0:1fa43ab66921 562 */
terencez 0:1fa43ab66921 563
terencez 0:1fa43ab66921 564 typedef struct _lwm2m_transaction_ lwm2m_transaction_t;
terencez 0:1fa43ab66921 565
terencez 0:1fa43ab66921 566 typedef void (*lwm2m_transaction_callback_t) (lwm2m_transaction_t * transacP, void * message);
terencez 0:1fa43ab66921 567
terencez 0:1fa43ab66921 568 struct _lwm2m_transaction_
terencez 0:1fa43ab66921 569 {
terencez 0:1fa43ab66921 570 lwm2m_transaction_t * next; // matches lwm2m_list_t::next
terencez 0:1fa43ab66921 571 uint16_t mID; // matches lwm2m_list_t::id
terencez 0:1fa43ab66921 572 void * peerH;
terencez 0:1fa43ab66921 573 uint8_t ack_received; // indicates, that the ACK was received
terencez 0:1fa43ab66921 574 time_t response_timeout; // timeout to wait for response, if token is used. When 0, use calculated acknowledge timeout.
terencez 0:1fa43ab66921 575 uint8_t retrans_counter;
terencez 0:1fa43ab66921 576 time_t retrans_time;
terencez 0:1fa43ab66921 577 char objStringID[LWM2M_STRING_ID_MAX_LEN];
terencez 0:1fa43ab66921 578 char instanceStringID[LWM2M_STRING_ID_MAX_LEN];
terencez 0:1fa43ab66921 579 char resourceStringID[LWM2M_STRING_ID_MAX_LEN];
terencez 0:1fa43ab66921 580 void * message;
terencez 0:1fa43ab66921 581 uint16_t buffer_len;
terencez 0:1fa43ab66921 582 uint8_t * buffer;
terencez 0:1fa43ab66921 583 lwm2m_transaction_callback_t callback;
terencez 0:1fa43ab66921 584 void * userData;
terencez 0:1fa43ab66921 585 };
terencez 0:1fa43ab66921 586
terencez 0:1fa43ab66921 587 /*
terencez 0:1fa43ab66921 588 * LWM2M observed resources
terencez 0:1fa43ab66921 589 */
terencez 0:1fa43ab66921 590 typedef struct _lwm2m_watcher_
terencez 0:1fa43ab66921 591 {
terencez 0:1fa43ab66921 592 struct _lwm2m_watcher_ * next;
terencez 0:1fa43ab66921 593
terencez 0:1fa43ab66921 594 bool active;
terencez 0:1fa43ab66921 595 bool update;
terencez 0:1fa43ab66921 596 lwm2m_server_t * server;
terencez 0:1fa43ab66921 597 lwm2m_attributes_t * parameters;
terencez 0:1fa43ab66921 598 uint8_t token[8];
terencez 0:1fa43ab66921 599 size_t tokenLen;
terencez 0:1fa43ab66921 600 time_t lastTime;
terencez 0:1fa43ab66921 601 uint32_t counter;
terencez 0:1fa43ab66921 602 uint16_t lastMid;
terencez 0:1fa43ab66921 603 union
terencez 0:1fa43ab66921 604 {
terencez 0:1fa43ab66921 605 int64_t asInteger;
terencez 0:1fa43ab66921 606 double asFloat;
terencez 0:1fa43ab66921 607 } lastValue;
terencez 0:1fa43ab66921 608 } lwm2m_watcher_t;
terencez 0:1fa43ab66921 609
terencez 0:1fa43ab66921 610 typedef struct _lwm2m_observed_
terencez 0:1fa43ab66921 611 {
terencez 0:1fa43ab66921 612 struct _lwm2m_observed_ * next;
terencez 0:1fa43ab66921 613
terencez 0:1fa43ab66921 614 lwm2m_uri_t uri;
terencez 0:1fa43ab66921 615 lwm2m_watcher_t * watcherList;
terencez 0:1fa43ab66921 616 } lwm2m_observed_t;
terencez 0:1fa43ab66921 617
terencez 0:1fa43ab66921 618 #ifdef LWM2M_CLIENT_MODE
terencez 0:1fa43ab66921 619
terencez 0:1fa43ab66921 620 typedef enum
terencez 0:1fa43ab66921 621 {
terencez 0:1fa43ab66921 622 STATE_INITIAL = 0,
terencez 0:1fa43ab66921 623 STATE_BOOTSTRAP_REQUIRED,
terencez 0:1fa43ab66921 624 STATE_BOOTSTRAPPING,
terencez 0:1fa43ab66921 625 STATE_REGISTER_REQUIRED,
terencez 0:1fa43ab66921 626 STATE_REGISTERING,
terencez 0:1fa43ab66921 627 STATE_READY
terencez 0:1fa43ab66921 628 } lwm2m_client_state_t;
terencez 0:1fa43ab66921 629
terencez 0:1fa43ab66921 630 #endif
terencez 0:1fa43ab66921 631 /*
terencez 0:1fa43ab66921 632 * LWM2M Context
terencez 0:1fa43ab66921 633 */
terencez 0:1fa43ab66921 634
terencez 0:1fa43ab66921 635 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
terencez 0:1fa43ab66921 636 // In all the following APIs, the session handle MUST uniquely identify a peer.
terencez 0:1fa43ab66921 637
terencez 0:1fa43ab66921 638 // LWM2M bootstrap callback
terencez 0:1fa43ab66921 639 // When a LWM2M client requests bootstrap information, the callback is called with status COAP_NO_ERROR, uriP is nil and
terencez 0:1fa43ab66921 640 // name is set. The callback must return a COAP_* error code. COAP_204_CHANGED for success.
terencez 0:1fa43ab66921 641 // After a lwm2m_bootstrap_delete() or a lwm2m_bootstrap_write(), the callback is called with the status returned by the
terencez 0:1fa43ab66921 642 // client, the URI of the operation (may be nil) and name is nil. The callback return value is ignored.
terencez 0:1fa43ab66921 643 typedef int (*lwm2m_bootstrap_callback_t) (void * sessionH, uint8_t status, lwm2m_uri_t * uriP, char * name, void * userData);
terencez 0:1fa43ab66921 644 #endif
terencez 0:1fa43ab66921 645
terencez 0:1fa43ab66921 646 typedef struct
terencez 0:1fa43ab66921 647 {
terencez 0:1fa43ab66921 648 #ifdef LWM2M_CLIENT_MODE
terencez 0:1fa43ab66921 649 lwm2m_client_state_t state;
terencez 0:1fa43ab66921 650 char * endpointName;
terencez 0:1fa43ab66921 651 char * msisdn;
terencez 0:1fa43ab66921 652 char * altPath;
terencez 0:1fa43ab66921 653 lwm2m_server_t * bootstrapServerList;
terencez 0:1fa43ab66921 654 lwm2m_server_t * serverList;
terencez 0:1fa43ab66921 655 lwm2m_object_t * objectList;
terencez 0:1fa43ab66921 656 lwm2m_observed_t * observedList;
terencez 0:1fa43ab66921 657 #endif
terencez 0:1fa43ab66921 658 #ifdef LWM2M_SERVER_MODE
terencez 0:1fa43ab66921 659 lwm2m_client_t * clientList;
terencez 0:1fa43ab66921 660 lwm2m_result_callback_t monitorCallback;
terencez 0:1fa43ab66921 661 void * monitorUserData;
terencez 0:1fa43ab66921 662 #endif
terencez 0:1fa43ab66921 663 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
terencez 0:1fa43ab66921 664 lwm2m_bootstrap_callback_t bootstrapCallback;
terencez 0:1fa43ab66921 665 void * bootstrapUserData;
terencez 0:1fa43ab66921 666 #endif
terencez 0:1fa43ab66921 667 uint16_t nextMID;
terencez 0:1fa43ab66921 668 lwm2m_transaction_t * transactionList;
terencez 0:1fa43ab66921 669 void * userData;
terencez 0:1fa43ab66921 670 } lwm2m_context_t;
terencez 0:1fa43ab66921 671
terencez 0:1fa43ab66921 672
terencez 0:1fa43ab66921 673 // initialize a liblwm2m context.
terencez 0:1fa43ab66921 674 lwm2m_context_t * lwm2m_init(void * userData);
terencez 0:1fa43ab66921 675 // close a liblwm2m context.
terencez 0:1fa43ab66921 676 void lwm2m_close(lwm2m_context_t * contextP);
terencez 0:1fa43ab66921 677
terencez 0:1fa43ab66921 678 // perform any required pending operation and adjust timeoutP to the maximal time interval to wait in seconds.
terencez 0:1fa43ab66921 679 int lwm2m_step(lwm2m_context_t * contextP, time_t * timeoutP);
terencez 0:1fa43ab66921 680 // dispatch received data to liblwm2m
terencez 0:1fa43ab66921 681 void lwm2m_handle_packet(lwm2m_context_t * contextP, uint8_t * buffer, int length, void * fromSessionH);
terencez 0:1fa43ab66921 682
terencez 0:1fa43ab66921 683 #ifdef LWM2M_CLIENT_MODE
terencez 0:1fa43ab66921 684 // configure the client side with the Endpoint Name, binding, MSISDN (can be nil), alternative path
terencez 0:1fa43ab66921 685 // for objects (can be nil) and a list of objects.
terencez 0:1fa43ab66921 686 // LWM2M Security Object (ID 0) must be present with either a bootstrap server or a LWM2M server and
terencez 0:1fa43ab66921 687 // its matching LWM2M Server Object (ID 1) instance
terencez 0:1fa43ab66921 688 int lwm2m_configure(lwm2m_context_t * contextP, const char * endpointName, const char * msisdn, const char * altPath, uint16_t numObject, lwm2m_object_t * objectList[]);
terencez 0:1fa43ab66921 689 int lwm2m_add_object(lwm2m_context_t * contextP, lwm2m_object_t * objectP);
terencez 0:1fa43ab66921 690 int lwm2m_remove_object(lwm2m_context_t * contextP, uint16_t id);
terencez 0:1fa43ab66921 691
terencez 0:1fa43ab66921 692 // send a registration update to the server specified by the server short identifier
terencez 0:1fa43ab66921 693 // or all if the ID is 0.
terencez 0:1fa43ab66921 694 // If withObjects is true, the registration update contains the object list.
terencez 0:1fa43ab66921 695 int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID, bool withObjects);
terencez 0:1fa43ab66921 696
terencez 0:1fa43ab66921 697 void lwm2m_resource_value_changed(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
terencez 0:1fa43ab66921 698 #endif
terencez 0:1fa43ab66921 699
terencez 0:1fa43ab66921 700 #ifdef LWM2M_SERVER_MODE
terencez 0:1fa43ab66921 701 // Clients registration/deregistration monitoring API.
terencez 0:1fa43ab66921 702 // When a LWM2M client registers, the callback is called with status COAP_201_CREATED.
terencez 0:1fa43ab66921 703 // When a LWM2M client deregisters, the callback is called with status COAP_202_DELETED.
terencez 0:1fa43ab66921 704 // clientID is the internal ID of the LWM2M Client.
terencez 0:1fa43ab66921 705 // The callback's parameters uri, data, dataLength are always NULL.
terencez 0:1fa43ab66921 706 // 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.
terencez 0:1fa43ab66921 707 void lwm2m_set_monitoring_callback(lwm2m_context_t * contextP, lwm2m_result_callback_t callback, void * userData);
terencez 0:1fa43ab66921 708
terencez 0:1fa43ab66921 709 // Device Management APIs
terencez 0:1fa43ab66921 710 int lwm2m_dm_read(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
terencez 0:1fa43ab66921 711 int lwm2m_dm_discover(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
terencez 0:1fa43ab66921 712 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);
terencez 0:1fa43ab66921 713 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);
terencez 0:1fa43ab66921 714 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);
terencez 0:1fa43ab66921 715 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);
terencez 0:1fa43ab66921 716 int lwm2m_dm_delete(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
terencez 0:1fa43ab66921 717
terencez 0:1fa43ab66921 718 // Information Reporting APIs
terencez 0:1fa43ab66921 719 int lwm2m_observe(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
terencez 0:1fa43ab66921 720 int lwm2m_observe_cancel(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
terencez 0:1fa43ab66921 721 #endif
terencez 0:1fa43ab66921 722
terencez 0:1fa43ab66921 723 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
terencez 0:1fa43ab66921 724 // Clients bootstrap request monitoring API.
terencez 0:1fa43ab66921 725 // When a LWM2M client sends a bootstrap request, the callback is called with the client's endpoint name.
terencez 0:1fa43ab66921 726 void lwm2m_set_bootstrap_callback(lwm2m_context_t * contextP, lwm2m_bootstrap_callback_t callback, void * userData);
terencez 0:1fa43ab66921 727
terencez 0:1fa43ab66921 728 // Boostrap Interface APIs
terencez 0:1fa43ab66921 729 // if uriP is nil, a "Delete /" is sent to the client
terencez 0:1fa43ab66921 730 int lwm2m_bootstrap_delete(lwm2m_context_t * contextP, void * sessionH, lwm2m_uri_t * uriP);
terencez 0:1fa43ab66921 731 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);
terencez 0:1fa43ab66921 732 int lwm2m_bootstrap_finish(lwm2m_context_t * contextP, void * sessionH);
terencez 0:1fa43ab66921 733
terencez 0:1fa43ab66921 734 #endif
terencez 0:1fa43ab66921 735
terencez 0:1fa43ab66921 736 #ifdef __cplusplus
terencez 0:1fa43ab66921 737 }
terencez 0:1fa43ab66921 738 #endif
terencez 0:1fa43ab66921 739
terencez 0:1fa43ab66921 740 #endif