terence zhang / wakaama-core
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers internals.h Source File

internals.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  *    Toby Jaffey - Please refer to git log
00017  *    Bosch Software Innovations GmbH - Please refer to git log
00018  *    Pascal Rieux - Please refer to git log
00019  *    
00020  *******************************************************************************/
00021 /*
00022  Copyright (c) 2013, 2014 Intel Corporation
00023 
00024  Redistribution and use in source and binary forms, with or without modification,
00025  are permitted provided that the following conditions are met:
00026 
00027      * Redistributions of source code must retain the above copyright notice,
00028        this list of conditions and the following disclaimer.
00029      * Redistributions in binary form must reproduce the above copyright notice,
00030        this list of conditions and the following disclaimer in the documentation
00031        and/or other materials provided with the distribution.
00032      * Neither the name of Intel Corporation nor the names of its contributors
00033        may be used to endorse or promote products derived from this software
00034        without specific prior written permission.
00035 
00036  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00037  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00038  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00039  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00040  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00041  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00042  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00043  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00044  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00045  THE POSSIBILITY OF SUCH DAMAGE.
00046 
00047  David Navarro <david.navarro@intel.com>
00048 
00049 */
00050 
00051 #ifndef _LWM2M_INTERNALS_H_
00052 #define _LWM2M_INTERNALS_H_
00053 
00054 #include "liblwm2m.h"
00055 
00056 #include <stdlib.h>
00057 #include <string.h>
00058 #include <stdio.h>
00059 #include <stdint.h>
00060 #include <stddef.h>
00061 #include <stdbool.h>
00062 
00063 #include "er-coap-13/er-coap-13.h"
00064 
00065 #ifdef LWM2M_WITH_LOGS
00066 #include <inttypes.h>
00067 #define LOG(STR) lwm2m_printf("[%s:%d] " STR "\r\n", __func__ , __LINE__)
00068 #define LOG_ARG(FMT, ...) lwm2m_printf("[%s:%d] " FMT "\r\n", __func__ , __LINE__ , __VA_ARGS__)
00069 #define LOG_URI(URI)                                                                \
00070 {                                                                                   \
00071     if ((URI) == NULL) lwm2m_printf("[%s:%d] NULL\r\n", __func__ , __LINE__);     \
00072     else                                                                            \
00073     {                                                                               \
00074         lwm2m_printf("[%s:%d] /%d", __func__ , __LINE__ , (URI)->objectId);       \
00075         if (LWM2M_URI_IS_SET_INSTANCE(URI)) lwm2m_printf("/%d", (URI)->instanceId); \
00076         if (LWM2M_URI_IS_SET_RESOURCE(URI)) lwm2m_printf("/%d", (URI)->resourceId); \
00077         lwm2m_printf("\r\n");                                                       \
00078     }                                                                               \
00079 }
00080 #define STR_STATUS(S)                                           \
00081 ((S) == STATE_DEREGISTERED ? "STATE_DEREGISTERED" :             \
00082 ((S) == STATE_REG_PENDING ? "STATE_REG_PENDING" :               \
00083 ((S) == STATE_REGISTERED ? "STATE_REGISTERED" :                 \
00084 ((S) == STATE_REG_FAILED ? "STATE_REG_FAILED" :                 \
00085 ((S) == STATE_REG_UPDATE_PENDING ? "STATE_REG_UPDATE_PENDING" : \
00086 ((S) == STATE_REG_UPDATE_NEEDED ? "STATE_REG_UPDATE_NEEDED" :   \
00087 ((S) == STATE_REG_FULL_UPDATE_NEEDED ? "STATE_REG_FULL_UPDATE_NEEDED" :   \
00088 ((S) == STATE_DEREG_PENDING ? "STATE_DEREG_PENDING" :           \
00089 ((S) == STATE_BS_HOLD_OFF ? "STATE_BS_HOLD_OFF" :               \
00090 ((S) == STATE_BS_INITIATED ? "STATE_BS_INITIATED" :             \
00091 ((S) == STATE_BS_PENDING ? "STATE_BS_PENDING" :                 \
00092 ((S) == STATE_BS_FINISHED ? "STATE_BS_FINISHED" :               \
00093 ((S) == STATE_BS_FINISHING ? "STATE_BS_FINISHING" :             \
00094 ((S) == STATE_BS_FAILING ? "STATE_BS_FAILING" :                 \
00095 ((S) == STATE_BS_FAILED ? "STATE_BS_FAILED" :                   \
00096 "Unknown")))))))))))))))
00097 #define STR_MEDIA_TYPE(M)                                \
00098 ((M) == LWM2M_CONTENT_TEXT ? "LWM2M_CONTENT_TEXT" :      \
00099 ((M) == LWM2M_CONTENT_LINK ? "LWM2M_CONTENT_LINK" :      \
00100 ((M) == LWM2M_CONTENT_OPAQUE ? "LWM2M_CONTENT_OPAQUE" :  \
00101 ((M) == LWM2M_CONTENT_TLV ? "LWM2M_CONTENT_TLV" :        \
00102 ((M) == LWM2M_CONTENT_JSON ? "LWM2M_CONTENT_JSON" :      \
00103 "Unknown")))))
00104 #define STR_STATE(S)                                \
00105 ((S) == STATE_INITIAL ? "STATE_INITIAL" :      \
00106 ((S) == STATE_BOOTSTRAP_REQUIRED ? "STATE_BOOTSTRAP_REQUIRED" :      \
00107 ((S) == STATE_BOOTSTRAPPING ? "STATE_BOOTSTRAPPING" :  \
00108 ((S) == STATE_REGISTER_REQUIRED ? "STATE_REGISTER_REQUIRED" :        \
00109 ((S) == STATE_REGISTERING ? "STATE_REGISTERING" :      \
00110 ((S) == STATE_READY ? "STATE_READY" :      \
00111 "Unknown"))))))
00112 #else
00113 #define LOG_ARG(FMT, ...)
00114 #define LOG(STR)
00115 #define LOG_URI(URI)
00116 #endif
00117 
00118 #define LWM2M_DEFAULT_LIFETIME  86400
00119 
00120 #ifdef LWM2M_SUPPORT_JSON
00121 #define REG_LWM2M_RESOURCE_TYPE     ">;rt=\"oma.lwm2m\";ct=11543,"
00122 #define REG_LWM2M_RESOURCE_TYPE_LEN 25
00123 #else
00124 #define REG_LWM2M_RESOURCE_TYPE     ">;rt=\"oma.lwm2m\","
00125 #define REG_LWM2M_RESOURCE_TYPE_LEN 17
00126 #endif
00127 #define REG_START           "<"
00128 #define REG_DEFAULT_PATH    "/"
00129 
00130 #define REG_OBJECT_MIN_LEN  5   // "</n>,"
00131 #define REG_PATH_END        ">,"
00132 #define REG_PATH_SEPARATOR  "/"
00133 
00134 #define REG_OBJECT_PATH             "<%s/%hu>,"
00135 #define REG_OBJECT_INSTANCE_PATH    "<%s/%hu/%hu>,"
00136 
00137 #define URI_REGISTRATION_SEGMENT        "rd"
00138 #define URI_REGISTRATION_SEGMENT_LEN    2
00139 #define URI_BOOTSTRAP_SEGMENT           "bs"
00140 #define URI_BOOTSTRAP_SEGMENT_LEN       2
00141 
00142 #define QUERY_STARTER       "?"
00143 #define QUERY_NAME          "ep="
00144 #define QUERY_NAME_LEN      3       // strlen("ep=")
00145 #define QUERY_SMS           "sms="
00146 #define QUERY_SMS_LEN       4
00147 #define QUERY_LIFETIME      "lt="
00148 #define QUERY_LIFETIME_LEN  3
00149 #define QUERY_VERSION       "lwm2m="
00150 #define QUERY_VERSION_LEN   6
00151 #define QUERY_BINDING       "b="
00152 #define QUERY_BINDING_LEN   2
00153 #define QUERY_DELIMITER     "&"
00154 
00155 #define LWM2M_VERSION      "1.0"
00156 #define LWM2M_VERSION_LEN  3
00157 
00158 #define QUERY_VERSION_FULL      QUERY_VERSION LWM2M_VERSION
00159 #define QUERY_VERSION_FULL_LEN  QUERY_VERSION_LEN+LWM2M_VERSION_LEN
00160 
00161 #define REG_URI_START       '<'
00162 #define REG_URI_END         '>'
00163 #define REG_DELIMITER       ','
00164 #define REG_ATTR_SEPARATOR  ';'
00165 #define REG_ATTR_EQUALS     '='
00166 #define REG_ATTR_TYPE_KEY           "rt"
00167 #define REG_ATTR_TYPE_KEY_LEN       2
00168 #define REG_ATTR_TYPE_VALUE         "\"oma.lwm2m\""
00169 #define REG_ATTR_TYPE_VALUE_LEN     11
00170 #define REG_ATTR_CONTENT_KEY        "ct"
00171 #define REG_ATTR_CONTENT_KEY_LEN    2
00172 #define REG_ATTR_CONTENT_JSON       "11543"   // Temporary value
00173 #define REG_ATTR_CONTENT_JSON_LEN   5
00174 
00175 #define ATTR_SERVER_ID_STR       "ep="
00176 #define ATTR_SERVER_ID_LEN       3
00177 #define ATTR_MIN_PERIOD_STR      "pmin="
00178 #define ATTR_MIN_PERIOD_LEN      5
00179 #define ATTR_MAX_PERIOD_STR      "pmax="
00180 #define ATTR_MAX_PERIOD_LEN      5
00181 #define ATTR_GREATER_THAN_STR    "gt="
00182 #define ATTR_GREATER_THAN_LEN    3
00183 #define ATTR_LESS_THAN_STR       "lt="
00184 #define ATTR_LESS_THAN_LEN       3
00185 #define ATTR_STEP_STR            "stp="
00186 #define ATTR_STEP_LEN            4
00187 #define ATTR_DIMENSION_STR       "dim="
00188 #define ATTR_DIMENSION_LEN       4
00189 
00190 #define URI_MAX_STRING_LEN    18      // /65535/65535/65535
00191 #define _PRV_64BIT_BUFFER_SIZE 8
00192 
00193 #define LINK_ITEM_START             "<"
00194 #define LINK_ITEM_START_SIZE        1
00195 #define LINK_ITEM_END               ">,"
00196 #define LINK_ITEM_END_SIZE          2
00197 #define LINK_ITEM_DIM_START         ">;dim="
00198 #define LINK_ITEM_DIM_START_SIZE    6
00199 #define LINK_ITEM_ATTR_END          ","
00200 #define LINK_ITEM_ATTR_END_SIZE     1
00201 #define LINK_URI_SEPARATOR          "/"
00202 #define LINK_URI_SEPARATOR_SIZE     1
00203 #define LINK_ATTR_SEPARATOR         ";"
00204 #define LINK_ATTR_SEPARATOR_SIZE    1
00205 
00206 #define ATTR_FLAG_NUMERIC (uint8_t)(LWM2M_ATTR_FLAG_LESS_THAN | LWM2M_ATTR_FLAG_GREATER_THAN | LWM2M_ATTR_FLAG_STEP)
00207 
00208 #define LWM2M_URI_FLAG_DM           (uint8_t)0x00
00209 #define LWM2M_URI_FLAG_DELETE_ALL   (uint8_t)0x10
00210 #define LWM2M_URI_FLAG_REGISTRATION (uint8_t)0x20
00211 #define LWM2M_URI_FLAG_BOOTSTRAP    (uint8_t)0x40
00212 
00213 #define LWM2M_URI_MASK_TYPE (uint8_t)0x70
00214 #define LWM2M_URI_MASK_ID   (uint8_t)0x07
00215 
00216 typedef struct
00217 {
00218     uint16_t clientID;
00219     lwm2m_uri_t uri;
00220     lwm2m_result_callback_t callback;
00221     void * userData;
00222 } dm_data_t;
00223 
00224 typedef enum
00225 {
00226     URI_DEPTH_OBJECT,
00227     URI_DEPTH_OBJECT_INSTANCE,
00228     URI_DEPTH_RESOURCE,
00229     URI_DEPTH_RESOURCE_INSTANCE
00230 } uri_depth_t;
00231 
00232 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
00233 typedef struct
00234 {
00235     bool        isUri;
00236     lwm2m_uri_t uri;
00237     lwm2m_bootstrap_callback_t callback;
00238     void *      userData;
00239 } bs_data_t;
00240 #endif
00241 
00242 // defined in uri.c
00243 lwm2m_uri_t * uri_decode(char * altPath, multi_option_t *uriPath);
00244 int uri_getNumber(uint8_t * uriString, size_t uriLength);
00245 int uri_toString(lwm2m_uri_t * uriP, uint8_t * buffer, size_t bufferLen, uri_depth_t * depthP);
00246 
00247 // defined in objects.c
00248 coap_status_t object_readData(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, int * sizeP, lwm2m_data_t ** dataP);
00249 coap_status_t object_read(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_media_type_t * formatP, uint8_t ** bufferP, size_t * lengthP);
00250 coap_status_t object_write(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, size_t length);
00251 coap_status_t object_create(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, size_t length);
00252 coap_status_t object_execute(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, uint8_t * buffer, size_t length);
00253 coap_status_t object_delete(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
00254 coap_status_t object_discover(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, uint8_t ** bufferP, size_t * lengthP);
00255 uint8_t object_checkReadable(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
00256 uint8_t object_checkNumeric(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
00257 bool object_isInstanceNew(lwm2m_context_t * contextP, uint16_t objectId, uint16_t instanceId);
00258 int object_getRegisterPayload(lwm2m_context_t * contextP, uint8_t * buffer, size_t length);
00259 int object_getServers(lwm2m_context_t * contextP);
00260 coap_status_t object_createInstance(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_data_t * dataP);
00261 coap_status_t object_writeInstance(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_data_t * dataP);
00262 
00263 // defined in transaction.c
00264 lwm2m_transaction_t * transaction_new(void * sessionH, coap_method_t method, char * altPath, lwm2m_uri_t * uriP, uint16_t mID, uint8_t token_len, uint8_t* token);
00265 int transaction_send(lwm2m_context_t * contextP, lwm2m_transaction_t * transacP);
00266 void transaction_free(lwm2m_transaction_t * transacP);
00267 void transaction_remove(lwm2m_context_t * contextP, lwm2m_transaction_t * transacP);
00268 bool transaction_handleResponse(lwm2m_context_t * contextP, void * fromSessionH, coap_packet_t * message, coap_packet_t * response);
00269 void transaction_step(lwm2m_context_t * contextP, time_t currentTime, time_t * timeoutP);
00270 
00271 // defined in management.c
00272 coap_status_t dm_handleRequest(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, coap_packet_t * message, coap_packet_t * response);
00273 
00274 // defined in observe.c
00275 coap_status_t observe_handleRequest(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, int size, lwm2m_data_t * dataP, coap_packet_t * message, coap_packet_t * response);
00276 void observe_cancel(lwm2m_context_t * contextP, uint16_t mid, void * fromSessionH);
00277 coap_status_t observe_setParameters(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, lwm2m_attributes_t * attrP);
00278 void observe_step(lwm2m_context_t * contextP, time_t currentTime, time_t * timeoutP);
00279 bool observe_handleNotify(lwm2m_context_t * contextP, void * fromSessionH, coap_packet_t * message, coap_packet_t * response);
00280 void observe_remove(lwm2m_observation_t * observationP);
00281 lwm2m_observed_t * observe_findByUri(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
00282 
00283 // defined in registration.c
00284 coap_status_t registration_handleRequest(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, void * fromSessionH, coap_packet_t * message, coap_packet_t * response);
00285 void registration_deregister(lwm2m_context_t * contextP, lwm2m_server_t * serverP);
00286 void registration_freeClient(lwm2m_client_t * clientP);
00287 uint8_t registration_start(lwm2m_context_t * contextP);
00288 void registration_step(lwm2m_context_t * contextP, time_t currentTime, time_t * timeoutP);
00289 lwm2m_status_t registration_getStatus(lwm2m_context_t * contextP);
00290 
00291 // defined in packet.c
00292 coap_status_t message_send(lwm2m_context_t * contextP, coap_packet_t * message, void * sessionH);
00293 
00294 // defined in bootstrap.c
00295 void bootstrap_step(lwm2m_context_t * contextP, uint32_t currentTime, time_t* timeoutP);
00296 coap_status_t bootstrap_handleCommand(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, coap_packet_t * message, coap_packet_t * response);
00297 coap_status_t bootstrap_handleDeleteAll(lwm2m_context_t * context, void * fromSessionH);
00298 coap_status_t bootstrap_handleFinish(lwm2m_context_t * context, void * fromSessionH);
00299 uint8_t bootstrap_handleRequest(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, void * fromSessionH, coap_packet_t * message, coap_packet_t * response);
00300 void bootstrap_start(lwm2m_context_t * contextP);
00301 lwm2m_status_t bootstrap_getStatus(lwm2m_context_t * contextP);
00302 
00303 // defined in tlv.c
00304 int tlv_parse(uint8_t * buffer, size_t bufferLen, lwm2m_data_t ** dataP);
00305 int tlv_serialize(bool isResourceInstance, int size, lwm2m_data_t * dataP, uint8_t ** bufferP);
00306 
00307 // defined in json.c
00308 #ifdef LWM2M_SUPPORT_JSON
00309 int json_parse(lwm2m_uri_t * uriP, uint8_t * buffer, size_t bufferLen, lwm2m_data_t ** dataP);
00310 int json_serialize(lwm2m_uri_t * uriP, int size, lwm2m_data_t * tlvP, uint8_t ** bufferP);
00311 #endif
00312 
00313 // defined in discover.c
00314 int discover_serialize(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, int size, lwm2m_data_t * dataP, uint8_t ** bufferP);
00315 
00316 // defined in block1.c
00317 coap_status_t coap_block1_handler(lwm2m_block1_data_t ** block1Data, uint16_t mid, uint8_t * buffer, size_t length, uint16_t blockSize, uint32_t blockNum, bool blockMore, uint8_t ** outputBuffer, size_t * outputLength);
00318 void free_block1_buffer(lwm2m_block1_data_t * block1Data);
00319 
00320 // defined in utils.c
00321 lwm2m_data_type_t utils_depthToDatatype(uri_depth_t depth);
00322 lwm2m_binding_t utils_stringToBinding(uint8_t *buffer, size_t length);
00323 lwm2m_media_type_t utils_convertMediaType(coap_content_type_t type);
00324 int utils_isAltPathValid(const char * altPath);
00325 int utils_stringCopy(char * buffer, size_t length, const char * str);
00326 int utils_intCopy(char * buffer, size_t length, int32_t value);
00327 size_t utils_intToText(int64_t data, uint8_t * string, size_t length);
00328 size_t utils_floatToText(double data, uint8_t * string, size_t length);
00329 int utils_plainTextToInt64(uint8_t * buffer, int length, int64_t * dataP);
00330 int utils_plainTextToFloat64(uint8_t * buffer, int length, double * dataP);
00331 size_t utils_int64ToPlainText(int64_t data, uint8_t ** bufferP);
00332 size_t utils_float64ToPlainText(double data, uint8_t ** bufferP);
00333 size_t utils_boolToPlainText(bool data, uint8_t ** bufferP);
00334 void utils_copyValue(void * dst, const void * src, size_t len);
00335 int utils_opaqueToInt(const uint8_t * buffer, size_t buffer_len, int64_t * dataP);
00336 int utils_opaqueToFloat(const uint8_t * buffer, size_t buffer_len, double * dataP);
00337 size_t utils_encodeInt(int64_t data, uint8_t data_buffer[_PRV_64BIT_BUFFER_SIZE]);
00338 size_t utils_encodeFloat(double data, uint8_t data_buffer[_PRV_64BIT_BUFFER_SIZE]);
00339 size_t utils_base64ToOpaque(uint8_t * dataP, size_t dataLen, uint8_t ** bufferP);
00340 size_t utils_opaqueToBase64(uint8_t * dataP, size_t dataLen, uint8_t ** bufferP);
00341 size_t utils_base64Encode(uint8_t * dataP, size_t dataLen, uint8_t * bufferP, size_t bufferLen);
00342 #ifdef LWM2M_CLIENT_MODE
00343 lwm2m_server_t * utils_findServer(lwm2m_context_t * contextP, void * fromSessionH);
00344 lwm2m_server_t * utils_findBootstrapServer(lwm2m_context_t * contextP, void * fromSessionH);
00345 #endif
00346 
00347 #endif