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/objects.c@0:f9d13e09cf11, 2017-04-24 (annotated)
- Committer:
- terencez
- Date:
- Mon Apr 24 23:03:31 2017 +0000
- Revision:
- 0:f9d13e09cf11
- Child:
- 3:a280069151ac
The first compiled submit.
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 | * Toby Jaffey - Please refer to git log |
| terencez | 0:f9d13e09cf11 | 17 | * Benjamin Cabé - 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 | #ifdef LWM2M_CLIENT_MODE |
| terencez | 0:f9d13e09cf11 | 54 | #include <stdlib.h> |
| terencez | 0:f9d13e09cf11 | 55 | #include <string.h> |
| terencez | 0:f9d13e09cf11 | 56 | #include <stdio.h> |
| terencez | 0:f9d13e09cf11 | 57 | |
| terencez | 0:f9d13e09cf11 | 58 | |
| terencez | 0:f9d13e09cf11 | 59 | static lwm2m_object_t * prv_find_object(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 60 | uint16_t Id) |
| terencez | 0:f9d13e09cf11 | 61 | { |
| terencez | 0:f9d13e09cf11 | 62 | int i; |
| terencez | 0:f9d13e09cf11 | 63 | |
| terencez | 0:f9d13e09cf11 | 64 | for (i = 0 ; i < contextP->numObject ; i++) |
| terencez | 0:f9d13e09cf11 | 65 | { |
| terencez | 0:f9d13e09cf11 | 66 | if (contextP->objectList[i]->objID == Id) |
| terencez | 0:f9d13e09cf11 | 67 | { |
| terencez | 0:f9d13e09cf11 | 68 | return contextP->objectList[i]; |
| terencez | 0:f9d13e09cf11 | 69 | } |
| terencez | 0:f9d13e09cf11 | 70 | } |
| terencez | 0:f9d13e09cf11 | 71 | |
| terencez | 0:f9d13e09cf11 | 72 | return NULL; |
| terencez | 0:f9d13e09cf11 | 73 | } |
| terencez | 0:f9d13e09cf11 | 74 | |
| terencez | 0:f9d13e09cf11 | 75 | coap_status_t object_read(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 76 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 77 | char ** bufferP, |
| terencez | 0:f9d13e09cf11 | 78 | int * lengthP) |
| terencez | 0:f9d13e09cf11 | 79 | { |
| terencez | 0:f9d13e09cf11 | 80 | coap_status_t result; |
| terencez | 0:f9d13e09cf11 | 81 | |
| terencez | 0:f9d13e09cf11 | 82 | switch (uriP->objectId) |
| terencez | 0:f9d13e09cf11 | 83 | { |
| terencez | 0:f9d13e09cf11 | 84 | case LWM2M_SECURITY_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 85 | result = NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 86 | break; |
| terencez | 0:f9d13e09cf11 | 87 | |
| terencez | 0:f9d13e09cf11 | 88 | case LWM2M_SERVER_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 89 | result = object_server_read(contextP, uriP, bufferP, lengthP); |
| terencez | 0:f9d13e09cf11 | 90 | break; |
| terencez | 0:f9d13e09cf11 | 91 | |
| terencez | 0:f9d13e09cf11 | 92 | default: |
| terencez | 0:f9d13e09cf11 | 93 | { |
| terencez | 0:f9d13e09cf11 | 94 | lwm2m_object_t * targetP; |
| terencez | 0:f9d13e09cf11 | 95 | lwm2m_tlv_t * tlvP = NULL; |
| terencez | 0:f9d13e09cf11 | 96 | int size = 0; |
| terencez | 0:f9d13e09cf11 | 97 | |
| terencez | 0:f9d13e09cf11 | 98 | targetP = prv_find_object(contextP, uriP->objectId); |
| terencez | 0:f9d13e09cf11 | 99 | if (NULL == targetP) return NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 100 | if (NULL == targetP->readFunc) return METHOD_NOT_ALLOWED_4_05; |
| terencez | 0:f9d13e09cf11 | 101 | if (targetP->instanceList == NULL) |
| terencez | 0:f9d13e09cf11 | 102 | { |
| terencez | 0:f9d13e09cf11 | 103 | // this is a single instance object |
| terencez | 0:f9d13e09cf11 | 104 | if (LWM2M_URI_IS_SET_INSTANCE(uriP) && (uriP->instanceId != 0)) |
| terencez | 0:f9d13e09cf11 | 105 | { |
| terencez | 0:f9d13e09cf11 | 106 | return COAP_404_NOT_FOUND; |
| terencez | 0:f9d13e09cf11 | 107 | } |
| terencez | 0:f9d13e09cf11 | 108 | } |
| terencez | 0:f9d13e09cf11 | 109 | else |
| terencez | 0:f9d13e09cf11 | 110 | { |
| terencez | 0:f9d13e09cf11 | 111 | if (LWM2M_URI_IS_SET_INSTANCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 112 | { |
| terencez | 0:f9d13e09cf11 | 113 | if (NULL == lwm2m_list_find(targetP->instanceList, uriP->instanceId)) |
| terencez | 0:f9d13e09cf11 | 114 | { |
| terencez | 0:f9d13e09cf11 | 115 | return COAP_404_NOT_FOUND; |
| terencez | 0:f9d13e09cf11 | 116 | } |
| terencez | 0:f9d13e09cf11 | 117 | } |
| terencez | 0:f9d13e09cf11 | 118 | else |
| terencez | 0:f9d13e09cf11 | 119 | { |
| terencez | 0:f9d13e09cf11 | 120 | // multiple object instances read |
| terencez | 0:f9d13e09cf11 | 121 | lwm2m_list_t * instanceP; |
| terencez | 0:f9d13e09cf11 | 122 | int i; |
| terencez | 0:f9d13e09cf11 | 123 | |
| terencez | 0:f9d13e09cf11 | 124 | size = 0; |
| terencez | 0:f9d13e09cf11 | 125 | for (instanceP = targetP->instanceList; instanceP != NULL ; instanceP = instanceP->next) |
| terencez | 0:f9d13e09cf11 | 126 | { |
| terencez | 0:f9d13e09cf11 | 127 | size++; |
| terencez | 0:f9d13e09cf11 | 128 | } |
| terencez | 0:f9d13e09cf11 | 129 | |
| terencez | 0:f9d13e09cf11 | 130 | tlvP = lwm2m_tlv_new(size); |
| terencez | 0:f9d13e09cf11 | 131 | if (tlvP == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 132 | |
| terencez | 0:f9d13e09cf11 | 133 | result = COAP_205_CONTENT; |
| terencez | 0:f9d13e09cf11 | 134 | instanceP = targetP->instanceList; |
| terencez | 0:f9d13e09cf11 | 135 | i = 0; |
| terencez | 0:f9d13e09cf11 | 136 | while (instanceP != NULL && result == COAP_205_CONTENT) |
| terencez | 0:f9d13e09cf11 | 137 | { |
| terencez | 0:f9d13e09cf11 | 138 | result = targetP->readFunc(instanceP->id, (int*)&(tlvP[i].length), (lwm2m_tlv_t **)&(tlvP[i].value), targetP); |
| terencez | 0:f9d13e09cf11 | 139 | tlvP[i].type = LWM2M_TYPE_OBJECT_INSTANCE; |
| terencez | 0:f9d13e09cf11 | 140 | tlvP[i].id = instanceP->id; |
| terencez | 0:f9d13e09cf11 | 141 | i++; |
| terencez | 0:f9d13e09cf11 | 142 | instanceP = instanceP->next; |
| terencez | 0:f9d13e09cf11 | 143 | } |
| terencez | 0:f9d13e09cf11 | 144 | |
| terencez | 0:f9d13e09cf11 | 145 | if (result == COAP_205_CONTENT) |
| terencez | 0:f9d13e09cf11 | 146 | { |
| terencez | 0:f9d13e09cf11 | 147 | *lengthP = lwm2m_tlv_serialize(size, tlvP, bufferP); |
| terencez | 0:f9d13e09cf11 | 148 | if (*lengthP == 0) result = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 149 | } |
| terencez | 0:f9d13e09cf11 | 150 | lwm2m_tlv_free(size, tlvP); |
| terencez | 0:f9d13e09cf11 | 151 | |
| terencez | 0:f9d13e09cf11 | 152 | return result; |
| terencez | 0:f9d13e09cf11 | 153 | } |
| terencez | 0:f9d13e09cf11 | 154 | } |
| terencez | 0:f9d13e09cf11 | 155 | |
| terencez | 0:f9d13e09cf11 | 156 | // single instance read |
| terencez | 0:f9d13e09cf11 | 157 | if (LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 158 | { |
| terencez | 0:f9d13e09cf11 | 159 | size = 1; |
| terencez | 0:f9d13e09cf11 | 160 | tlvP = lwm2m_tlv_new(size); |
| terencez | 0:f9d13e09cf11 | 161 | if (tlvP == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 162 | |
| terencez | 0:f9d13e09cf11 | 163 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
| terencez | 0:f9d13e09cf11 | 164 | tlvP->flags = LWM2M_TLV_FLAG_TEXT_FORMAT; |
| terencez | 0:f9d13e09cf11 | 165 | tlvP->id = uriP->resourceId; |
| terencez | 0:f9d13e09cf11 | 166 | } |
| terencez | 0:f9d13e09cf11 | 167 | result = targetP->readFunc(uriP->instanceId, &size, &tlvP, targetP); |
| terencez | 0:f9d13e09cf11 | 168 | if (result == COAP_205_CONTENT) |
| terencez | 0:f9d13e09cf11 | 169 | { |
| terencez | 0:f9d13e09cf11 | 170 | if (size == 1 |
| terencez | 0:f9d13e09cf11 | 171 | && tlvP->type == LWM2M_TYPE_RESSOURCE |
| terencez | 0:f9d13e09cf11 | 172 | && (tlvP->flags && LWM2M_TLV_FLAG_TEXT_FORMAT) != 0 ) |
| terencez | 0:f9d13e09cf11 | 173 | { |
| terencez | 0:f9d13e09cf11 | 174 | *bufferP = (char *)malloc(tlvP->length); |
| terencez | 0:f9d13e09cf11 | 175 | if (*bufferP == NULL) |
| terencez | 0:f9d13e09cf11 | 176 | { |
| terencez | 0:f9d13e09cf11 | 177 | result = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 178 | } |
| terencez | 0:f9d13e09cf11 | 179 | else |
| terencez | 0:f9d13e09cf11 | 180 | { |
| terencez | 0:f9d13e09cf11 | 181 | memcpy(*bufferP, tlvP->value, tlvP->length); |
| terencez | 0:f9d13e09cf11 | 182 | *lengthP = tlvP->length; |
| terencez | 0:f9d13e09cf11 | 183 | } |
| terencez | 0:f9d13e09cf11 | 184 | } |
| terencez | 0:f9d13e09cf11 | 185 | else |
| terencez | 0:f9d13e09cf11 | 186 | { |
| terencez | 0:f9d13e09cf11 | 187 | *lengthP = lwm2m_tlv_serialize(size, tlvP, bufferP); |
| terencez | 0:f9d13e09cf11 | 188 | if (*lengthP == 0) result = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 189 | } |
| terencez | 0:f9d13e09cf11 | 190 | } |
| terencez | 0:f9d13e09cf11 | 191 | lwm2m_tlv_free(size, tlvP); |
| terencez | 0:f9d13e09cf11 | 192 | } |
| terencez | 0:f9d13e09cf11 | 193 | } |
| terencez | 0:f9d13e09cf11 | 194 | |
| terencez | 0:f9d13e09cf11 | 195 | return result; |
| terencez | 0:f9d13e09cf11 | 196 | } |
| terencez | 0:f9d13e09cf11 | 197 | |
| terencez | 0:f9d13e09cf11 | 198 | coap_status_t object_write(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 199 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 200 | char * buffer, |
| terencez | 0:f9d13e09cf11 | 201 | int length) |
| terencez | 0:f9d13e09cf11 | 202 | { |
| terencez | 0:f9d13e09cf11 | 203 | coap_status_t result; |
| terencez | 0:f9d13e09cf11 | 204 | |
| terencez | 0:f9d13e09cf11 | 205 | switch (uriP->objectId) |
| terencez | 0:f9d13e09cf11 | 206 | { |
| terencez | 0:f9d13e09cf11 | 207 | case LWM2M_SECURITY_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 208 | result = NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 209 | break; |
| terencez | 0:f9d13e09cf11 | 210 | |
| terencez | 0:f9d13e09cf11 | 211 | case LWM2M_SERVER_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 212 | result = object_server_write(contextP, uriP, buffer, length); |
| terencez | 0:f9d13e09cf11 | 213 | break; |
| terencez | 0:f9d13e09cf11 | 214 | |
| terencez | 0:f9d13e09cf11 | 215 | default: |
| terencez | 0:f9d13e09cf11 | 216 | { |
| terencez | 0:f9d13e09cf11 | 217 | lwm2m_object_t * targetP; |
| terencez | 0:f9d13e09cf11 | 218 | lwm2m_tlv_t * tlvP = NULL; |
| terencez | 0:f9d13e09cf11 | 219 | int size = 0; |
| terencez | 0:f9d13e09cf11 | 220 | |
| terencez | 0:f9d13e09cf11 | 221 | targetP = prv_find_object(contextP, uriP->objectId); |
| terencez | 0:f9d13e09cf11 | 222 | if (NULL == targetP) return NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 223 | if (NULL == targetP->writeFunc) return METHOD_NOT_ALLOWED_4_05; |
| terencez | 0:f9d13e09cf11 | 224 | |
| terencez | 0:f9d13e09cf11 | 225 | if (LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 226 | { |
| terencez | 0:f9d13e09cf11 | 227 | size = 1; |
| terencez | 0:f9d13e09cf11 | 228 | tlvP = lwm2m_tlv_new(size); |
| terencez | 0:f9d13e09cf11 | 229 | if (tlvP == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 230 | |
| terencez | 0:f9d13e09cf11 | 231 | tlvP->flags = LWM2M_TLV_FLAG_TEXT_FORMAT | LWM2M_TLV_FLAG_STATIC_DATA; |
| terencez | 0:f9d13e09cf11 | 232 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
| terencez | 0:f9d13e09cf11 | 233 | tlvP->id = uriP->resourceId; |
| terencez | 0:f9d13e09cf11 | 234 | tlvP->length = length; |
| terencez | 0:f9d13e09cf11 | 235 | tlvP->value = buffer; |
| terencez | 0:f9d13e09cf11 | 236 | } |
| terencez | 0:f9d13e09cf11 | 237 | else |
| terencez | 0:f9d13e09cf11 | 238 | { |
| terencez | 0:f9d13e09cf11 | 239 | size = lwm2m_tlv_parse(buffer, length, &tlvP); |
| terencez | 0:f9d13e09cf11 | 240 | if (size == 0) return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 241 | } |
| terencez | 0:f9d13e09cf11 | 242 | result = targetP->writeFunc(uriP->instanceId, size, tlvP, targetP); |
| terencez | 0:f9d13e09cf11 | 243 | lwm2m_tlv_free(size, tlvP); |
| terencez | 0:f9d13e09cf11 | 244 | } |
| terencez | 0:f9d13e09cf11 | 245 | } |
| terencez | 0:f9d13e09cf11 | 246 | |
| terencez | 0:f9d13e09cf11 | 247 | return result; |
| terencez | 0:f9d13e09cf11 | 248 | } |
| terencez | 0:f9d13e09cf11 | 249 | |
| terencez | 0:f9d13e09cf11 | 250 | coap_status_t object_execute(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 251 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 252 | char * buffer, |
| terencez | 0:f9d13e09cf11 | 253 | int length) |
| terencez | 0:f9d13e09cf11 | 254 | { |
| terencez | 0:f9d13e09cf11 | 255 | switch (uriP->objectId) |
| terencez | 0:f9d13e09cf11 | 256 | { |
| terencez | 0:f9d13e09cf11 | 257 | case LWM2M_SECURITY_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 258 | return NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 259 | |
| terencez | 0:f9d13e09cf11 | 260 | case LWM2M_SERVER_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 261 | return object_server_execute(contextP, uriP, buffer, length); |
| terencez | 0:f9d13e09cf11 | 262 | |
| terencez | 0:f9d13e09cf11 | 263 | default: |
| terencez | 0:f9d13e09cf11 | 264 | { |
| terencez | 0:f9d13e09cf11 | 265 | lwm2m_object_t * targetP; |
| terencez | 0:f9d13e09cf11 | 266 | |
| terencez | 0:f9d13e09cf11 | 267 | targetP = prv_find_object(contextP, uriP->objectId); |
| terencez | 0:f9d13e09cf11 | 268 | if (NULL == targetP) return NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 269 | if (NULL == targetP->executeFunc) return METHOD_NOT_ALLOWED_4_05; |
| terencez | 0:f9d13e09cf11 | 270 | |
| terencez | 0:f9d13e09cf11 | 271 | return targetP->executeFunc(uriP->instanceId, uriP->resourceId, buffer, length, targetP); |
| terencez | 0:f9d13e09cf11 | 272 | } |
| terencez | 0:f9d13e09cf11 | 273 | } |
| terencez | 0:f9d13e09cf11 | 274 | } |
| terencez | 0:f9d13e09cf11 | 275 | |
| terencez | 0:f9d13e09cf11 | 276 | coap_status_t object_create(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 277 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 278 | char * buffer, |
| terencez | 0:f9d13e09cf11 | 279 | int length) |
| terencez | 0:f9d13e09cf11 | 280 | { |
| terencez | 0:f9d13e09cf11 | 281 | if (length == 0 || buffer == 0) |
| terencez | 0:f9d13e09cf11 | 282 | { |
| terencez | 0:f9d13e09cf11 | 283 | return BAD_REQUEST_4_00; |
| terencez | 0:f9d13e09cf11 | 284 | } |
| terencez | 0:f9d13e09cf11 | 285 | |
| terencez | 0:f9d13e09cf11 | 286 | switch (uriP->objectId) |
| terencez | 0:f9d13e09cf11 | 287 | { |
| terencez | 0:f9d13e09cf11 | 288 | case LWM2M_SECURITY_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 289 | return object_security_create(contextP, uriP, buffer, length); |
| terencez | 0:f9d13e09cf11 | 290 | |
| terencez | 0:f9d13e09cf11 | 291 | case LWM2M_SERVER_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 292 | return object_server_create(contextP, uriP, buffer, length); |
| terencez | 0:f9d13e09cf11 | 293 | |
| terencez | 0:f9d13e09cf11 | 294 | default: |
| terencez | 0:f9d13e09cf11 | 295 | { |
| terencez | 0:f9d13e09cf11 | 296 | lwm2m_object_t * targetP; |
| terencez | 0:f9d13e09cf11 | 297 | lwm2m_tlv_t * tlvP = NULL; |
| terencez | 0:f9d13e09cf11 | 298 | int size = 0; |
| terencez | 0:f9d13e09cf11 | 299 | uint8_t result; |
| terencez | 0:f9d13e09cf11 | 300 | |
| terencez | 0:f9d13e09cf11 | 301 | targetP = prv_find_object(contextP, uriP->objectId); |
| terencez | 0:f9d13e09cf11 | 302 | if (NULL == targetP) return NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 303 | if (NULL == targetP->createFunc) return METHOD_NOT_ALLOWED_4_05; |
| terencez | 0:f9d13e09cf11 | 304 | |
| terencez | 0:f9d13e09cf11 | 305 | if (LWM2M_URI_IS_SET_INSTANCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 306 | { |
| terencez | 0:f9d13e09cf11 | 307 | if (NULL != lwm2m_list_find(targetP->instanceList, uriP->instanceId)) |
| terencez | 0:f9d13e09cf11 | 308 | { |
| terencez | 0:f9d13e09cf11 | 309 | // Instance already exists |
| terencez | 0:f9d13e09cf11 | 310 | return COAP_406_NOT_ACCEPTABLE; |
| terencez | 0:f9d13e09cf11 | 311 | } |
| terencez | 0:f9d13e09cf11 | 312 | } |
| terencez | 0:f9d13e09cf11 | 313 | else |
| terencez | 0:f9d13e09cf11 | 314 | { |
| terencez | 0:f9d13e09cf11 | 315 | uriP->instanceId = lwm2m_list_newId(targetP->instanceList); |
| terencez | 0:f9d13e09cf11 | 316 | uriP->flag |= LWM2M_URI_FLAG_INSTANCE_ID; |
| terencez | 0:f9d13e09cf11 | 317 | } |
| terencez | 0:f9d13e09cf11 | 318 | |
| terencez | 0:f9d13e09cf11 | 319 | targetP = prv_find_object(contextP, uriP->objectId); |
| terencez | 0:f9d13e09cf11 | 320 | if (NULL == targetP) return NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 321 | if (NULL == targetP->writeFunc) return METHOD_NOT_ALLOWED_4_05; |
| terencez | 0:f9d13e09cf11 | 322 | |
| terencez | 0:f9d13e09cf11 | 323 | size = lwm2m_tlv_parse(buffer, length, &tlvP); |
| terencez | 0:f9d13e09cf11 | 324 | if (size == 0) return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 325 | |
| terencez | 0:f9d13e09cf11 | 326 | result = targetP->createFunc(uriP->instanceId, size, tlvP, targetP); |
| terencez | 0:f9d13e09cf11 | 327 | lwm2m_tlv_free(size, tlvP); |
| terencez | 0:f9d13e09cf11 | 328 | |
| terencez | 0:f9d13e09cf11 | 329 | return result; |
| terencez | 0:f9d13e09cf11 | 330 | } |
| terencez | 0:f9d13e09cf11 | 331 | } |
| terencez | 0:f9d13e09cf11 | 332 | } |
| terencez | 0:f9d13e09cf11 | 333 | |
| terencez | 0:f9d13e09cf11 | 334 | coap_status_t object_delete(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 335 | lwm2m_uri_t * uriP) |
| terencez | 0:f9d13e09cf11 | 336 | { |
| terencez | 0:f9d13e09cf11 | 337 | switch (uriP->objectId) |
| terencez | 0:f9d13e09cf11 | 338 | { |
| terencez | 0:f9d13e09cf11 | 339 | case LWM2M_SECURITY_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 340 | return object_security_delete(contextP, uriP); |
| terencez | 0:f9d13e09cf11 | 341 | |
| terencez | 0:f9d13e09cf11 | 342 | case LWM2M_SERVER_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 343 | return object_server_delete(contextP, uriP); |
| terencez | 0:f9d13e09cf11 | 344 | |
| terencez | 0:f9d13e09cf11 | 345 | default: |
| terencez | 0:f9d13e09cf11 | 346 | { |
| terencez | 0:f9d13e09cf11 | 347 | lwm2m_object_t * targetP; |
| terencez | 0:f9d13e09cf11 | 348 | |
| terencez | 0:f9d13e09cf11 | 349 | targetP = prv_find_object(contextP, uriP->objectId); |
| terencez | 0:f9d13e09cf11 | 350 | if (NULL == targetP) return NOT_FOUND_4_04; |
| terencez | 0:f9d13e09cf11 | 351 | if (NULL == targetP->deleteFunc) return METHOD_NOT_ALLOWED_4_05; |
| terencez | 0:f9d13e09cf11 | 352 | |
| terencez | 0:f9d13e09cf11 | 353 | return targetP->deleteFunc(uriP->instanceId, targetP); |
| terencez | 0:f9d13e09cf11 | 354 | } |
| terencez | 0:f9d13e09cf11 | 355 | } |
| terencez | 0:f9d13e09cf11 | 356 | } |
| terencez | 0:f9d13e09cf11 | 357 | |
| terencez | 0:f9d13e09cf11 | 358 | bool object_isInstanceNew(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 359 | uint16_t objectId, |
| terencez | 0:f9d13e09cf11 | 360 | uint16_t instanceId) |
| terencez | 0:f9d13e09cf11 | 361 | { |
| terencez | 0:f9d13e09cf11 | 362 | switch (objectId) |
| terencez | 0:f9d13e09cf11 | 363 | { |
| terencez | 0:f9d13e09cf11 | 364 | case LWM2M_SECURITY_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 365 | case LWM2M_SERVER_OBJECT_ID: |
| terencez | 0:f9d13e09cf11 | 366 | if (NULL != lwm2m_list_find((lwm2m_list_t *)contextP->serverList, instanceId)) |
| terencez | 0:f9d13e09cf11 | 367 | { |
| terencez | 0:f9d13e09cf11 | 368 | return false; |
| terencez | 0:f9d13e09cf11 | 369 | } |
| terencez | 0:f9d13e09cf11 | 370 | break; |
| terencez | 0:f9d13e09cf11 | 371 | |
| terencez | 0:f9d13e09cf11 | 372 | default: |
| terencez | 0:f9d13e09cf11 | 373 | { |
| terencez | 0:f9d13e09cf11 | 374 | lwm2m_object_t * targetP; |
| terencez | 0:f9d13e09cf11 | 375 | |
| terencez | 0:f9d13e09cf11 | 376 | targetP = prv_find_object(contextP, objectId); |
| terencez | 0:f9d13e09cf11 | 377 | if (targetP != NULL) |
| terencez | 0:f9d13e09cf11 | 378 | { |
| terencez | 0:f9d13e09cf11 | 379 | if (NULL != lwm2m_list_find(targetP->instanceList, instanceId)) |
| terencez | 0:f9d13e09cf11 | 380 | { |
| terencez | 0:f9d13e09cf11 | 381 | return false; |
| terencez | 0:f9d13e09cf11 | 382 | } |
| terencez | 0:f9d13e09cf11 | 383 | } |
| terencez | 0:f9d13e09cf11 | 384 | } |
| terencez | 0:f9d13e09cf11 | 385 | break; |
| terencez | 0:f9d13e09cf11 | 386 | } |
| terencez | 0:f9d13e09cf11 | 387 | |
| terencez | 0:f9d13e09cf11 | 388 | return true; |
| terencez | 0:f9d13e09cf11 | 389 | } |
| terencez | 0:f9d13e09cf11 | 390 | |
| terencez | 0:f9d13e09cf11 | 391 | int prv_getRegisterPayload(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 392 | char * buffer, |
| terencez | 0:f9d13e09cf11 | 393 | size_t length) |
| terencez | 0:f9d13e09cf11 | 394 | { |
| terencez | 0:f9d13e09cf11 | 395 | int index; |
| terencez | 0:f9d13e09cf11 | 396 | int i; |
| terencez | 0:f9d13e09cf11 | 397 | int result; |
| terencez | 0:f9d13e09cf11 | 398 | |
| terencez | 0:f9d13e09cf11 | 399 | lwm2m_server_t * serverP; |
| terencez | 0:f9d13e09cf11 | 400 | |
| terencez | 0:f9d13e09cf11 | 401 | // index can not be greater than length |
| terencez | 0:f9d13e09cf11 | 402 | index = 0; |
| terencez | 0:f9d13e09cf11 | 403 | for (serverP = contextP->serverList; |
| terencez | 0:f9d13e09cf11 | 404 | serverP != NULL; |
| terencez | 0:f9d13e09cf11 | 405 | serverP = serverP->next) |
| terencez | 0:f9d13e09cf11 | 406 | { |
| terencez | 0:f9d13e09cf11 | 407 | result = snprintf(buffer + index, length - index, "</%hu/%hu>,", LWM2M_SERVER_OBJECT_ID, serverP->shortID); |
| terencez | 0:f9d13e09cf11 | 408 | if (result > 0 && result <= length - index) |
| terencez | 0:f9d13e09cf11 | 409 | { |
| terencez | 0:f9d13e09cf11 | 410 | index += result; |
| terencez | 0:f9d13e09cf11 | 411 | } |
| terencez | 0:f9d13e09cf11 | 412 | else |
| terencez | 0:f9d13e09cf11 | 413 | { |
| terencez | 0:f9d13e09cf11 | 414 | return 0; |
| terencez | 0:f9d13e09cf11 | 415 | } |
| terencez | 0:f9d13e09cf11 | 416 | } |
| terencez | 0:f9d13e09cf11 | 417 | |
| terencez | 0:f9d13e09cf11 | 418 | for (i = 0 ; i < contextP->numObject ; i++) |
| terencez | 0:f9d13e09cf11 | 419 | { |
| terencez | 0:f9d13e09cf11 | 420 | if (contextP->objectList[i]->instanceList == NULL) |
| terencez | 0:f9d13e09cf11 | 421 | { |
| terencez | 0:f9d13e09cf11 | 422 | result = snprintf(buffer + index, length - index, "</%hu>,", contextP->objectList[i]->objID); |
| terencez | 0:f9d13e09cf11 | 423 | if (result > 0 && result <= length - index) |
| terencez | 0:f9d13e09cf11 | 424 | { |
| terencez | 0:f9d13e09cf11 | 425 | index += result; |
| terencez | 0:f9d13e09cf11 | 426 | } |
| terencez | 0:f9d13e09cf11 | 427 | else |
| terencez | 0:f9d13e09cf11 | 428 | { |
| terencez | 0:f9d13e09cf11 | 429 | return 0; |
| terencez | 0:f9d13e09cf11 | 430 | } |
| terencez | 0:f9d13e09cf11 | 431 | } |
| terencez | 0:f9d13e09cf11 | 432 | else |
| terencez | 0:f9d13e09cf11 | 433 | { |
| terencez | 0:f9d13e09cf11 | 434 | lwm2m_list_t * targetP; |
| terencez | 0:f9d13e09cf11 | 435 | for (targetP = contextP->objectList[i]->instanceList ; targetP != NULL ; targetP = targetP->next) |
| terencez | 0:f9d13e09cf11 | 436 | { |
| terencez | 0:f9d13e09cf11 | 437 | int result; |
| terencez | 0:f9d13e09cf11 | 438 | |
| terencez | 0:f9d13e09cf11 | 439 | result = snprintf(buffer + index, length - index, "</%hu/%hu>,", contextP->objectList[i]->objID, targetP->id); |
| terencez | 0:f9d13e09cf11 | 440 | if (result > 0 && result <= length - index) |
| terencez | 0:f9d13e09cf11 | 441 | { |
| terencez | 0:f9d13e09cf11 | 442 | index += result; |
| terencez | 0:f9d13e09cf11 | 443 | } |
| terencez | 0:f9d13e09cf11 | 444 | else |
| terencez | 0:f9d13e09cf11 | 445 | { |
| terencez | 0:f9d13e09cf11 | 446 | return 0; |
| terencez | 0:f9d13e09cf11 | 447 | } |
| terencez | 0:f9d13e09cf11 | 448 | } |
| terencez | 0:f9d13e09cf11 | 449 | } |
| terencez | 0:f9d13e09cf11 | 450 | } |
| terencez | 0:f9d13e09cf11 | 451 | |
| terencez | 0:f9d13e09cf11 | 452 | if (index > 0) |
| terencez | 0:f9d13e09cf11 | 453 | { |
| terencez | 0:f9d13e09cf11 | 454 | index = index - 1; // remove trailing ',' |
| terencez | 0:f9d13e09cf11 | 455 | } |
| terencez | 0:f9d13e09cf11 | 456 | |
| terencez | 0:f9d13e09cf11 | 457 | buffer[index] = 0; |
| terencez | 0:f9d13e09cf11 | 458 | |
| terencez | 0:f9d13e09cf11 | 459 | return index; |
| terencez | 0:f9d13e09cf11 | 460 | } |
| terencez | 0:f9d13e09cf11 | 461 | #endif |