Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
wakaama/liblwm2m.c@1:3549d3c3967c, 2017-04-28 (annotated)
- Committer:
- terencez
- Date:
- Fri Apr 28 08:16:22 2017 +0000
- Revision:
- 1:3549d3c3967c
- Parent:
- 0:f9d13e09cf11
- Child:
- 5:917e1705e5c2
The device can connect to Leshan server, registration success.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| terencez | 0:f9d13e09cf11 | 1 | /******************************************************************************* |
| terencez | 0:f9d13e09cf11 | 2 | * |
| terencez | 0:f9d13e09cf11 | 3 | * Copyright (c) 2013, 2014 Intel Corporation and others. |
| terencez | 0:f9d13e09cf11 | 4 | * All rights reserved. This program and the accompanying materials |
| terencez | 0:f9d13e09cf11 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| terencez | 0:f9d13e09cf11 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
| terencez | 0:f9d13e09cf11 | 7 | * |
| terencez | 0:f9d13e09cf11 | 8 | * The Eclipse Public License is available at |
| terencez | 0:f9d13e09cf11 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
| terencez | 0:f9d13e09cf11 | 10 | * The Eclipse Distribution License is available at |
| terencez | 0:f9d13e09cf11 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
| terencez | 0:f9d13e09cf11 | 12 | * |
| terencez | 0:f9d13e09cf11 | 13 | * Contributors: |
| terencez | 0:f9d13e09cf11 | 14 | * David Navarro, Intel Corporation - initial API and implementation |
| terencez | 0:f9d13e09cf11 | 15 | * Fabien Fleutot - Please refer to git log |
| terencez | 0:f9d13e09cf11 | 16 | * Simon Bernard - Please refer to git log |
| terencez | 0:f9d13e09cf11 | 17 | * Toby Jaffey - Please refer to git log |
| terencez | 0:f9d13e09cf11 | 18 | * |
| terencez | 0:f9d13e09cf11 | 19 | *******************************************************************************/ |
| terencez | 0:f9d13e09cf11 | 20 | |
| terencez | 0:f9d13e09cf11 | 21 | /* |
| terencez | 0:f9d13e09cf11 | 22 | Copyright (c) 2013, 2014 Intel Corporation |
| terencez | 0:f9d13e09cf11 | 23 | |
| terencez | 0:f9d13e09cf11 | 24 | Redistribution and use in source and binary forms, with or without modification, |
| terencez | 0:f9d13e09cf11 | 25 | are permitted provided that the following conditions are met: |
| terencez | 0:f9d13e09cf11 | 26 | |
| terencez | 0:f9d13e09cf11 | 27 | * Redistributions of source code must retain the above copyright notice, |
| terencez | 0:f9d13e09cf11 | 28 | this list of conditions and the following disclaimer. |
| terencez | 0:f9d13e09cf11 | 29 | * Redistributions in binary form must reproduce the above copyright notice, |
| terencez | 0:f9d13e09cf11 | 30 | this list of conditions and the following disclaimer in the documentation |
| terencez | 0:f9d13e09cf11 | 31 | and/or other materials provided with the distribution. |
| terencez | 0:f9d13e09cf11 | 32 | * Neither the name of Intel Corporation nor the names of its contributors |
| terencez | 0:f9d13e09cf11 | 33 | may be used to endorse or promote products derived from this software |
| terencez | 0:f9d13e09cf11 | 34 | without specific prior written permission. |
| terencez | 0:f9d13e09cf11 | 35 | |
| terencez | 0:f9d13e09cf11 | 36 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| terencez | 0:f9d13e09cf11 | 37 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| terencez | 0:f9d13e09cf11 | 38 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| terencez | 0:f9d13e09cf11 | 39 | IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| terencez | 0:f9d13e09cf11 | 40 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| terencez | 0:f9d13e09cf11 | 41 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| terencez | 0:f9d13e09cf11 | 42 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| terencez | 0:f9d13e09cf11 | 43 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| terencez | 0:f9d13e09cf11 | 44 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| terencez | 0:f9d13e09cf11 | 45 | THE POSSIBILITY OF SUCH DAMAGE. |
| terencez | 0:f9d13e09cf11 | 46 | |
| terencez | 0:f9d13e09cf11 | 47 | David Navarro <david.navarro@intel.com> |
| terencez | 0:f9d13e09cf11 | 48 | |
| terencez | 0:f9d13e09cf11 | 49 | */ |
| terencez | 0:f9d13e09cf11 | 50 | |
| terencez | 0:f9d13e09cf11 | 51 | #include "internals.h" |
| terencez | 0:f9d13e09cf11 | 52 | |
| terencez | 0:f9d13e09cf11 | 53 | #include <stdlib.h> |
| terencez | 0:f9d13e09cf11 | 54 | #include <string.h> |
| terencez | 0:f9d13e09cf11 | 55 | |
| terencez | 0:f9d13e09cf11 | 56 | #include <stdio.h> |
| terencez | 0:f9d13e09cf11 | 57 | |
| terencez | 0:f9d13e09cf11 | 58 | #ifndef LWM2M_EMBEDDED_MODE |
| terencez | 0:f9d13e09cf11 | 59 | int gettimeofday(struct timeval* t, void* timezone) |
| terencez | 0:f9d13e09cf11 | 60 | { |
| terencez | 0:f9d13e09cf11 | 61 | t->tv_sec = time(NULL); |
| terencez | 0:f9d13e09cf11 | 62 | t->tv_usec = 0; |
| terencez | 0:f9d13e09cf11 | 63 | return 0; /* 1sec precision only */ |
| terencez | 0:f9d13e09cf11 | 64 | } |
| terencez | 0:f9d13e09cf11 | 65 | #endif |
| terencez | 0:f9d13e09cf11 | 66 | |
| terencez | 0:f9d13e09cf11 | 67 | lwm2m_context_t * lwm2m_init(char * endpointName, |
| terencez | 0:f9d13e09cf11 | 68 | uint16_t numObject, |
| terencez | 0:f9d13e09cf11 | 69 | lwm2m_object_t * objectList[], |
| terencez | 0:f9d13e09cf11 | 70 | lwm2m_buffer_send_callback_t bufferSendCallback, |
| terencez | 0:f9d13e09cf11 | 71 | void * bufferSendUserData) |
| terencez | 0:f9d13e09cf11 | 72 | { |
| terencez | 0:f9d13e09cf11 | 73 | lwm2m_context_t * contextP; |
| terencez | 0:f9d13e09cf11 | 74 | |
| terencez | 0:f9d13e09cf11 | 75 | if (NULL == bufferSendCallback) |
| terencez | 0:f9d13e09cf11 | 76 | return NULL; |
| terencez | 0:f9d13e09cf11 | 77 | |
| terencez | 0:f9d13e09cf11 | 78 | #ifdef LWM2M_CLIENT_MODE |
| terencez | 0:f9d13e09cf11 | 79 | if (numObject != 0) |
| terencez | 0:f9d13e09cf11 | 80 | { |
| terencez | 0:f9d13e09cf11 | 81 | int i; |
| terencez | 0:f9d13e09cf11 | 82 | |
| terencez | 0:f9d13e09cf11 | 83 | for (i = 0 ; i < numObject ; i++) |
| terencez | 0:f9d13e09cf11 | 84 | { |
| terencez | 0:f9d13e09cf11 | 85 | if (objectList[i]->objID <= LWM2M_ACL_OBJECT_ID) |
| terencez | 0:f9d13e09cf11 | 86 | { |
| terencez | 0:f9d13e09cf11 | 87 | // Use of a reserved object ID |
| terencez | 0:f9d13e09cf11 | 88 | return NULL; |
| terencez | 0:f9d13e09cf11 | 89 | } |
| terencez | 0:f9d13e09cf11 | 90 | } |
| terencez | 0:f9d13e09cf11 | 91 | } |
| terencez | 0:f9d13e09cf11 | 92 | #endif |
| terencez | 0:f9d13e09cf11 | 93 | |
| terencez | 0:f9d13e09cf11 | 94 | contextP = (lwm2m_context_t *)lwm2m_malloc(sizeof(lwm2m_context_t)); |
| terencez | 0:f9d13e09cf11 | 95 | if (NULL != contextP) |
| terencez | 0:f9d13e09cf11 | 96 | { |
| terencez | 0:f9d13e09cf11 | 97 | memset(contextP, 0, sizeof(lwm2m_context_t)); |
| terencez | 0:f9d13e09cf11 | 98 | contextP->bufferSendCallback = bufferSendCallback; |
| terencez | 0:f9d13e09cf11 | 99 | contextP->bufferSendUserData = bufferSendUserData; |
| terencez | 0:f9d13e09cf11 | 100 | srand(time(NULL)); |
| terencez | 0:f9d13e09cf11 | 101 | contextP->nextMID = rand(); |
| terencez | 0:f9d13e09cf11 | 102 | #ifdef LWM2M_CLIENT_MODE |
| terencez | 0:f9d13e09cf11 | 103 | |
| terencez | 1:3549d3c3967c | 104 | contextP->endpointName = (char *)lwm2m_malloc(strlen(endpointName)); |
| terencez | 0:f9d13e09cf11 | 105 | if (contextP->endpointName == NULL) |
| terencez | 0:f9d13e09cf11 | 106 | { |
| terencez | 0:f9d13e09cf11 | 107 | lwm2m_free(contextP); |
| terencez | 0:f9d13e09cf11 | 108 | return NULL; |
| terencez | 0:f9d13e09cf11 | 109 | } |
| terencez | 1:3549d3c3967c | 110 | |
| terencez | 1:3549d3c3967c | 111 | strcpy(contextP->endpointName,endpointName); |
| terencez | 1:3549d3c3967c | 112 | printf("name: %s, %s, %d\n",endpointName,contextP->endpointName,strlen(endpointName)); |
| terencez | 1:3549d3c3967c | 113 | |
| terencez | 1:3549d3c3967c | 114 | //contextP->endpointName = strdup(endpointName); |
| terencez | 1:3549d3c3967c | 115 | |
| terencez | 0:f9d13e09cf11 | 116 | if (numObject != 0) |
| terencez | 0:f9d13e09cf11 | 117 | { |
| terencez | 0:f9d13e09cf11 | 118 | contextP->objectList = (lwm2m_object_t **)lwm2m_malloc(numObject * sizeof(lwm2m_object_t *)); |
| terencez | 0:f9d13e09cf11 | 119 | if (NULL != contextP->objectList) |
| terencez | 0:f9d13e09cf11 | 120 | { |
| terencez | 0:f9d13e09cf11 | 121 | memcpy(contextP->objectList, objectList, numObject * sizeof(lwm2m_object_t *)); |
| terencez | 0:f9d13e09cf11 | 122 | contextP->numObject = numObject; |
| terencez | 0:f9d13e09cf11 | 123 | } |
| terencez | 0:f9d13e09cf11 | 124 | else |
| terencez | 0:f9d13e09cf11 | 125 | { |
| terencez | 1:3549d3c3967c | 126 | lwm2m_free(contextP->endpointName); |
| terencez | 0:f9d13e09cf11 | 127 | lwm2m_free(contextP); |
| terencez | 0:f9d13e09cf11 | 128 | return NULL; |
| terencez | 0:f9d13e09cf11 | 129 | } |
| terencez | 0:f9d13e09cf11 | 130 | } |
| terencez | 0:f9d13e09cf11 | 131 | #endif |
| terencez | 0:f9d13e09cf11 | 132 | } |
| terencez | 0:f9d13e09cf11 | 133 | |
| terencez | 0:f9d13e09cf11 | 134 | return contextP; |
| terencez | 0:f9d13e09cf11 | 135 | } |
| terencez | 0:f9d13e09cf11 | 136 | |
| terencez | 0:f9d13e09cf11 | 137 | void lwm2m_close(lwm2m_context_t * contextP) |
| terencez | 0:f9d13e09cf11 | 138 | { |
| terencez | 0:f9d13e09cf11 | 139 | int i; |
| terencez | 0:f9d13e09cf11 | 140 | |
| terencez | 0:f9d13e09cf11 | 141 | #ifdef LWM2M_CLIENT_MODE |
| terencez | 0:f9d13e09cf11 | 142 | for (i = 0 ; i < contextP->numObject ; i++) |
| terencez | 0:f9d13e09cf11 | 143 | { |
| terencez | 0:f9d13e09cf11 | 144 | if (NULL != contextP->objectList[i]->closeFunc) |
| terencez | 0:f9d13e09cf11 | 145 | { |
| terencez | 0:f9d13e09cf11 | 146 | contextP->objectList[i]->closeFunc(contextP->objectList[i]); |
| terencez | 0:f9d13e09cf11 | 147 | } |
| terencez | 0:f9d13e09cf11 | 148 | lwm2m_free(contextP->objectList[i]); |
| terencez | 0:f9d13e09cf11 | 149 | } |
| terencez | 0:f9d13e09cf11 | 150 | |
| terencez | 0:f9d13e09cf11 | 151 | if (NULL != contextP->bootstrapServer) |
| terencez | 0:f9d13e09cf11 | 152 | { |
| terencez | 0:f9d13e09cf11 | 153 | if (NULL != contextP->bootstrapServer->uri) lwm2m_free (contextP->bootstrapServer->uri); |
| terencez | 0:f9d13e09cf11 | 154 | if (NULL != contextP->bootstrapServer->security.privateKey) lwm2m_free (contextP->bootstrapServer->security.privateKey); |
| terencez | 0:f9d13e09cf11 | 155 | if (NULL != contextP->bootstrapServer->security.publicKey) lwm2m_free (contextP->bootstrapServer->security.publicKey); |
| terencez | 0:f9d13e09cf11 | 156 | lwm2m_free(contextP->bootstrapServer); |
| terencez | 0:f9d13e09cf11 | 157 | } |
| terencez | 0:f9d13e09cf11 | 158 | |
| terencez | 0:f9d13e09cf11 | 159 | while (NULL != contextP->serverList) |
| terencez | 0:f9d13e09cf11 | 160 | { |
| terencez | 0:f9d13e09cf11 | 161 | lwm2m_server_t * targetP; |
| terencez | 0:f9d13e09cf11 | 162 | |
| terencez | 0:f9d13e09cf11 | 163 | targetP = contextP->serverList; |
| terencez | 0:f9d13e09cf11 | 164 | contextP->serverList = contextP->serverList->next; |
| terencez | 0:f9d13e09cf11 | 165 | |
| terencez | 0:f9d13e09cf11 | 166 | registration_deregister(contextP, targetP); |
| terencez | 0:f9d13e09cf11 | 167 | |
| terencez | 0:f9d13e09cf11 | 168 | if (NULL != targetP->location) lwm2m_free(targetP->location); |
| terencez | 0:f9d13e09cf11 | 169 | if (NULL != targetP->security.privateKey) lwm2m_free (targetP->security.privateKey); |
| terencez | 0:f9d13e09cf11 | 170 | if (NULL != targetP->security.publicKey) lwm2m_free (targetP->security.publicKey); |
| terencez | 0:f9d13e09cf11 | 171 | if (NULL != targetP->sms) lwm2m_free (targetP->sms); |
| terencez | 0:f9d13e09cf11 | 172 | lwm2m_free(targetP); |
| terencez | 0:f9d13e09cf11 | 173 | } |
| terencez | 0:f9d13e09cf11 | 174 | |
| terencez | 0:f9d13e09cf11 | 175 | while (NULL != contextP->observedList) |
| terencez | 0:f9d13e09cf11 | 176 | { |
| terencez | 0:f9d13e09cf11 | 177 | lwm2m_observed_t * targetP; |
| terencez | 0:f9d13e09cf11 | 178 | |
| terencez | 0:f9d13e09cf11 | 179 | targetP = contextP->observedList; |
| terencez | 0:f9d13e09cf11 | 180 | contextP->observedList = contextP->observedList->next; |
| terencez | 0:f9d13e09cf11 | 181 | |
| terencez | 0:f9d13e09cf11 | 182 | while (NULL != targetP->watcherList) |
| terencez | 0:f9d13e09cf11 | 183 | { |
| terencez | 0:f9d13e09cf11 | 184 | lwm2m_watcher_t * watcherP; |
| terencez | 0:f9d13e09cf11 | 185 | |
| terencez | 0:f9d13e09cf11 | 186 | watcherP = targetP->watcherList; |
| terencez | 0:f9d13e09cf11 | 187 | targetP->watcherList = targetP->watcherList->next; |
| terencez | 0:f9d13e09cf11 | 188 | lwm2m_free(watcherP); |
| terencez | 0:f9d13e09cf11 | 189 | } |
| terencez | 0:f9d13e09cf11 | 190 | lwm2m_free(targetP); |
| terencez | 0:f9d13e09cf11 | 191 | } |
| terencez | 0:f9d13e09cf11 | 192 | |
| terencez | 0:f9d13e09cf11 | 193 | if (NULL != contextP->objectList) |
| terencez | 0:f9d13e09cf11 | 194 | { |
| terencez | 0:f9d13e09cf11 | 195 | lwm2m_free(contextP->objectList); |
| terencez | 0:f9d13e09cf11 | 196 | } |
| terencez | 0:f9d13e09cf11 | 197 | |
| terencez | 0:f9d13e09cf11 | 198 | lwm2m_free(contextP->endpointName); |
| terencez | 0:f9d13e09cf11 | 199 | #endif |
| terencez | 0:f9d13e09cf11 | 200 | |
| terencez | 0:f9d13e09cf11 | 201 | #ifdef LWM2M_SERVER_MODE |
| terencez | 0:f9d13e09cf11 | 202 | while (NULL != contextP->clientList) |
| terencez | 0:f9d13e09cf11 | 203 | { |
| terencez | 0:f9d13e09cf11 | 204 | lwm2m_client_t * clientP; |
| terencez | 0:f9d13e09cf11 | 205 | |
| terencez | 0:f9d13e09cf11 | 206 | clientP = contextP->clientList; |
| terencez | 0:f9d13e09cf11 | 207 | contextP->clientList = contextP->clientList->next; |
| terencez | 0:f9d13e09cf11 | 208 | |
| terencez | 0:f9d13e09cf11 | 209 | prv_freeClient(clientP); |
| terencez | 0:f9d13e09cf11 | 210 | } |
| terencez | 0:f9d13e09cf11 | 211 | #endif |
| terencez | 0:f9d13e09cf11 | 212 | |
| terencez | 0:f9d13e09cf11 | 213 | while (NULL != contextP->transactionList) |
| terencez | 0:f9d13e09cf11 | 214 | { |
| terencez | 0:f9d13e09cf11 | 215 | lwm2m_transaction_t * transacP; |
| terencez | 0:f9d13e09cf11 | 216 | |
| terencez | 0:f9d13e09cf11 | 217 | transacP = contextP->transactionList; |
| terencez | 0:f9d13e09cf11 | 218 | contextP->transactionList = contextP->transactionList->next; |
| terencez | 0:f9d13e09cf11 | 219 | |
| terencez | 0:f9d13e09cf11 | 220 | transaction_free(transacP); |
| terencez | 0:f9d13e09cf11 | 221 | } |
| terencez | 0:f9d13e09cf11 | 222 | |
| terencez | 0:f9d13e09cf11 | 223 | lwm2m_free(contextP); |
| terencez | 0:f9d13e09cf11 | 224 | } |
| terencez | 0:f9d13e09cf11 | 225 | |
| terencez | 0:f9d13e09cf11 | 226 | #ifdef LWM2M_CLIENT_MODE |
| terencez | 0:f9d13e09cf11 | 227 | void lwm2m_set_bootstrap_server(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 228 | lwm2m_bootstrap_server_t * serverP) |
| terencez | 0:f9d13e09cf11 | 229 | { |
| terencez | 0:f9d13e09cf11 | 230 | if (NULL != contextP->bootstrapServer) |
| terencez | 0:f9d13e09cf11 | 231 | { |
| terencez | 0:f9d13e09cf11 | 232 | if (NULL != contextP->bootstrapServer->uri) lwm2m_free (contextP->bootstrapServer->uri); |
| terencez | 0:f9d13e09cf11 | 233 | if (NULL != contextP->bootstrapServer->security.privateKey) lwm2m_free (contextP->bootstrapServer->security.privateKey); |
| terencez | 0:f9d13e09cf11 | 234 | if (NULL != contextP->bootstrapServer->security.publicKey) lwm2m_free (contextP->bootstrapServer->security.publicKey); |
| terencez | 0:f9d13e09cf11 | 235 | lwm2m_free(contextP->bootstrapServer); |
| terencez | 0:f9d13e09cf11 | 236 | } |
| terencez | 0:f9d13e09cf11 | 237 | contextP->bootstrapServer = serverP; |
| terencez | 0:f9d13e09cf11 | 238 | } |
| terencez | 0:f9d13e09cf11 | 239 | |
| terencez | 0:f9d13e09cf11 | 240 | int lwm2m_add_server(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 241 | uint16_t shortID, |
| terencez | 0:f9d13e09cf11 | 242 | uint32_t lifetime, |
| terencez | 0:f9d13e09cf11 | 243 | char * sms, |
| terencez | 0:f9d13e09cf11 | 244 | lwm2m_binding_t binding, |
| terencez | 0:f9d13e09cf11 | 245 | void * sessionH, |
| terencez | 0:f9d13e09cf11 | 246 | lwm2m_security_t * securityP) |
| terencez | 0:f9d13e09cf11 | 247 | { |
| terencez | 0:f9d13e09cf11 | 248 | lwm2m_server_t * serverP; |
| terencez | 0:f9d13e09cf11 | 249 | int status = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 250 | |
| terencez | 0:f9d13e09cf11 | 251 | serverP = (lwm2m_server_t *)lwm2m_malloc(sizeof(lwm2m_server_t)); |
| terencez | 0:f9d13e09cf11 | 252 | if (serverP != NULL) |
| terencez | 0:f9d13e09cf11 | 253 | { |
| terencez | 0:f9d13e09cf11 | 254 | memset(serverP, 0, sizeof(lwm2m_server_t)); |
| terencez | 0:f9d13e09cf11 | 255 | memcpy(&(serverP->security), securityP, sizeof(lwm2m_security_t)); |
| terencez | 0:f9d13e09cf11 | 256 | serverP->shortID = shortID; |
| terencez | 0:f9d13e09cf11 | 257 | serverP->lifetime = lifetime; |
| terencez | 0:f9d13e09cf11 | 258 | serverP->binding = binding; |
| terencez | 0:f9d13e09cf11 | 259 | if (sms != NULL) |
| terencez | 0:f9d13e09cf11 | 260 | { |
| terencez | 0:f9d13e09cf11 | 261 | // copy the SMS number |
| terencez | 0:f9d13e09cf11 | 262 | int len = strlen(sms); |
| terencez | 0:f9d13e09cf11 | 263 | serverP->sms = (char*) lwm2m_malloc(strlen(sms)+1); |
| terencez | 0:f9d13e09cf11 | 264 | memcpy(serverP->sms, sms, len+1); |
| terencez | 0:f9d13e09cf11 | 265 | } |
| terencez | 0:f9d13e09cf11 | 266 | serverP->sessionH = sessionH; |
| terencez | 0:f9d13e09cf11 | 267 | contextP->serverList = (lwm2m_server_t*)LWM2M_LIST_ADD(contextP->serverList, serverP); |
| terencez | 0:f9d13e09cf11 | 268 | |
| terencez | 0:f9d13e09cf11 | 269 | status = COAP_NO_ERROR; |
| terencez | 0:f9d13e09cf11 | 270 | } |
| terencez | 0:f9d13e09cf11 | 271 | |
| terencez | 0:f9d13e09cf11 | 272 | return status; |
| terencez | 0:f9d13e09cf11 | 273 | } |
| terencez | 0:f9d13e09cf11 | 274 | #endif |
| terencez | 0:f9d13e09cf11 | 275 | |
| terencez | 0:f9d13e09cf11 | 276 | int lwm2m_step(lwm2m_context_t * contextP, struct timeval * timeoutP) |
| terencez | 0:f9d13e09cf11 | 277 | { |
| terencez | 0:f9d13e09cf11 | 278 | int ret =0; |
| terencez | 0:f9d13e09cf11 | 279 | lwm2m_transaction_t * transacP; |
| terencez | 0:f9d13e09cf11 | 280 | struct timeval tv; |
| terencez | 0:f9d13e09cf11 | 281 | //struct timezone tz; |
| terencez | 0:f9d13e09cf11 | 282 | #ifdef LWM2M_SERVER_MODE |
| terencez | 0:f9d13e09cf11 | 283 | lwm2m_client_t * clientP; |
| terencez | 0:f9d13e09cf11 | 284 | #endif |
| terencez | 0:f9d13e09cf11 | 285 | |
| terencez | 0:f9d13e09cf11 | 286 | |
| terencez | 0:f9d13e09cf11 | 287 | ret = lwm2m_gettimeofday(&tv,NULL); |
| terencez | 0:f9d13e09cf11 | 288 | if (0!=ret) |
| terencez | 0:f9d13e09cf11 | 289 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 290 | |
| terencez | 0:f9d13e09cf11 | 291 | transacP = contextP->transactionList; |
| terencez | 0:f9d13e09cf11 | 292 | while (transacP != NULL) |
| terencez | 0:f9d13e09cf11 | 293 | { |
| terencez | 0:f9d13e09cf11 | 294 | // transaction_send() may remove transaction from the linked list |
| terencez | 0:f9d13e09cf11 | 295 | lwm2m_transaction_t * nextP = transacP->next; |
| terencez | 0:f9d13e09cf11 | 296 | int removed = 0; |
| terencez | 0:f9d13e09cf11 | 297 | |
| terencez | 0:f9d13e09cf11 | 298 | if (transacP->retrans_time <= tv.tv_sec) |
| terencez | 0:f9d13e09cf11 | 299 | { |
| terencez | 0:f9d13e09cf11 | 300 | removed = transaction_send(contextP, transacP); |
| terencez | 0:f9d13e09cf11 | 301 | } |
| terencez | 0:f9d13e09cf11 | 302 | |
| terencez | 0:f9d13e09cf11 | 303 | if (0 == removed) |
| terencez | 0:f9d13e09cf11 | 304 | { |
| terencez | 0:f9d13e09cf11 | 305 | time_t interval; |
| terencez | 0:f9d13e09cf11 | 306 | |
| terencez | 0:f9d13e09cf11 | 307 | if (transacP->retrans_time > tv.tv_sec) |
| terencez | 0:f9d13e09cf11 | 308 | { |
| terencez | 0:f9d13e09cf11 | 309 | interval = transacP->retrans_time - tv.tv_sec; |
| terencez | 0:f9d13e09cf11 | 310 | } |
| terencez | 0:f9d13e09cf11 | 311 | else |
| terencez | 0:f9d13e09cf11 | 312 | { |
| terencez | 0:f9d13e09cf11 | 313 | interval = 1; |
| terencez | 0:f9d13e09cf11 | 314 | } |
| terencez | 0:f9d13e09cf11 | 315 | |
| terencez | 0:f9d13e09cf11 | 316 | if (timeoutP->tv_sec > interval) |
| terencez | 0:f9d13e09cf11 | 317 | { |
| terencez | 0:f9d13e09cf11 | 318 | timeoutP->tv_sec = interval; |
| terencez | 0:f9d13e09cf11 | 319 | } |
| terencez | 0:f9d13e09cf11 | 320 | } |
| terencez | 0:f9d13e09cf11 | 321 | |
| terencez | 0:f9d13e09cf11 | 322 | transacP = nextP; |
| terencez | 0:f9d13e09cf11 | 323 | } |
| terencez | 0:f9d13e09cf11 | 324 | |
| terencez | 0:f9d13e09cf11 | 325 | #ifdef LWM2M_SERVER_MODE |
| terencez | 0:f9d13e09cf11 | 326 | // monitor clients lifetime |
| terencez | 0:f9d13e09cf11 | 327 | clientP = contextP->clientList; |
| terencez | 0:f9d13e09cf11 | 328 | while (clientP != NULL) |
| terencez | 0:f9d13e09cf11 | 329 | { |
| terencez | 0:f9d13e09cf11 | 330 | lwm2m_client_t * nextP = clientP->next; |
| terencez | 0:f9d13e09cf11 | 331 | |
| terencez | 0:f9d13e09cf11 | 332 | if (clientP->endOfLife <= tv.tv_sec) |
| terencez | 0:f9d13e09cf11 | 333 | { |
| terencez | 0:f9d13e09cf11 | 334 | contextP->clientList = (lwm2m_client_t *)LWM2M_LIST_RM(contextP->clientList, clientP->internalID, NULL); |
| terencez | 0:f9d13e09cf11 | 335 | if (contextP->monitorCallback != NULL) |
| terencez | 0:f9d13e09cf11 | 336 | { |
| terencez | 0:f9d13e09cf11 | 337 | contextP->monitorCallback(clientP->internalID, NULL, DELETED_2_02, NULL, 0, contextP->monitorUserData); |
| terencez | 0:f9d13e09cf11 | 338 | } |
| terencez | 0:f9d13e09cf11 | 339 | prv_freeClient(clientP); |
| terencez | 0:f9d13e09cf11 | 340 | } |
| terencez | 0:f9d13e09cf11 | 341 | else |
| terencez | 0:f9d13e09cf11 | 342 | { |
| terencez | 0:f9d13e09cf11 | 343 | time_t interval; |
| terencez | 0:f9d13e09cf11 | 344 | |
| terencez | 0:f9d13e09cf11 | 345 | interval = clientP->endOfLife - tv.tv_sec; |
| terencez | 0:f9d13e09cf11 | 346 | |
| terencez | 0:f9d13e09cf11 | 347 | if (timeoutP->tv_sec > interval) |
| terencez | 0:f9d13e09cf11 | 348 | { |
| terencez | 0:f9d13e09cf11 | 349 | timeoutP->tv_sec = interval; |
| terencez | 0:f9d13e09cf11 | 350 | } |
| terencez | 0:f9d13e09cf11 | 351 | } |
| terencez | 0:f9d13e09cf11 | 352 | clientP = nextP; |
| terencez | 0:f9d13e09cf11 | 353 | } |
| terencez | 0:f9d13e09cf11 | 354 | #endif |
| terencez | 0:f9d13e09cf11 | 355 | |
| terencez | 0:f9d13e09cf11 | 356 | return 0; |
| terencez | 0:f9d13e09cf11 | 357 | } |