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/management.c@3:a280069151ac, 2017-04-28 (annotated)
- Committer:
- terence zhang
- Date:
- Fri Apr 28 18:13:27 2017 +0800
- Revision:
- 3:a280069151ac
- Parent:
- 0:f9d13e09cf11
- Child:
- 15:d0f20339c1ad
update and merge wakaama to the latest version.
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 | * domedambrosio - Please refer to git log |
| terencez | 0:f9d13e09cf11 | 16 | * Toby Jaffey - Please refer to git log |
| terence zhang | 3:a280069151ac | 17 | * Bosch Software Innovations GmbH - Please refer to git log |
| terence zhang | 3:a280069151ac | 18 | * Pascal Rieux - Please refer to git log |
| 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 | #include <stdio.h> |
| terencez | 0:f9d13e09cf11 | 53 | |
| terencez | 0:f9d13e09cf11 | 54 | |
| terencez | 0:f9d13e09cf11 | 55 | #ifdef LWM2M_CLIENT_MODE |
| terence zhang | 3:a280069151ac | 56 | static int prv_readAttributes(multi_option_t * query, |
| terence zhang | 3:a280069151ac | 57 | lwm2m_attributes_t * attrP) |
| terence zhang | 3:a280069151ac | 58 | { |
| terence zhang | 3:a280069151ac | 59 | int64_t intValue; |
| terence zhang | 3:a280069151ac | 60 | double floatValue; |
| terence zhang | 3:a280069151ac | 61 | |
| terence zhang | 3:a280069151ac | 62 | memset(attrP, 0, sizeof(lwm2m_attributes_t)); |
| terence zhang | 3:a280069151ac | 63 | |
| terence zhang | 3:a280069151ac | 64 | while (query != NULL) |
| terence zhang | 3:a280069151ac | 65 | { |
| terence zhang | 3:a280069151ac | 66 | if (lwm2m_strncmp((char *)query->data, ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN) == 0) |
| terence zhang | 3:a280069151ac | 67 | { |
| terence zhang | 3:a280069151ac | 68 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_MIN_PERIOD)) return -1; |
| terence zhang | 3:a280069151ac | 69 | if (query->len == ATTR_MIN_PERIOD_LEN) return -1; |
| terence zhang | 3:a280069151ac | 70 | |
| terence zhang | 3:a280069151ac | 71 | if (1 != utils_plainTextToInt64(query->data + ATTR_MIN_PERIOD_LEN, query->len - ATTR_MIN_PERIOD_LEN, &intValue)) return -1; |
| terence zhang | 3:a280069151ac | 72 | if (intValue < 0) return -1; |
| terence zhang | 3:a280069151ac | 73 | |
| terence zhang | 3:a280069151ac | 74 | attrP->toSet |= LWM2M_ATTR_FLAG_MIN_PERIOD; |
| terence zhang | 3:a280069151ac | 75 | attrP->minPeriod = intValue; |
| terence zhang | 3:a280069151ac | 76 | } |
| terence zhang | 3:a280069151ac | 77 | else if (lwm2m_strncmp((char *)query->data, ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN - 1) == 0) |
| terence zhang | 3:a280069151ac | 78 | { |
| terence zhang | 3:a280069151ac | 79 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_MIN_PERIOD)) return -1; |
| terence zhang | 3:a280069151ac | 80 | if (query->len != ATTR_MIN_PERIOD_LEN - 1) return -1; |
| terence zhang | 3:a280069151ac | 81 | |
| terence zhang | 3:a280069151ac | 82 | attrP->toClear |= LWM2M_ATTR_FLAG_MIN_PERIOD; |
| terence zhang | 3:a280069151ac | 83 | } |
| terence zhang | 3:a280069151ac | 84 | else if (lwm2m_strncmp((char *)query->data, ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN) == 0) |
| terence zhang | 3:a280069151ac | 85 | { |
| terence zhang | 3:a280069151ac | 86 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_MAX_PERIOD)) return -1; |
| terence zhang | 3:a280069151ac | 87 | if (query->len == ATTR_MAX_PERIOD_LEN) return -1; |
| terence zhang | 3:a280069151ac | 88 | |
| terence zhang | 3:a280069151ac | 89 | if (1 != utils_plainTextToInt64(query->data + ATTR_MAX_PERIOD_LEN, query->len - ATTR_MAX_PERIOD_LEN, &intValue)) return -1; |
| terence zhang | 3:a280069151ac | 90 | if (intValue < 0) return -1; |
| terence zhang | 3:a280069151ac | 91 | |
| terence zhang | 3:a280069151ac | 92 | attrP->toSet |= LWM2M_ATTR_FLAG_MAX_PERIOD; |
| terence zhang | 3:a280069151ac | 93 | attrP->maxPeriod = intValue; |
| terence zhang | 3:a280069151ac | 94 | } |
| terence zhang | 3:a280069151ac | 95 | else if (lwm2m_strncmp((char *)query->data, ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN - 1) == 0) |
| terence zhang | 3:a280069151ac | 96 | { |
| terence zhang | 3:a280069151ac | 97 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_MAX_PERIOD)) return -1; |
| terence zhang | 3:a280069151ac | 98 | if (query->len != ATTR_MAX_PERIOD_LEN - 1) return -1; |
| terence zhang | 3:a280069151ac | 99 | |
| terence zhang | 3:a280069151ac | 100 | attrP->toClear |= LWM2M_ATTR_FLAG_MAX_PERIOD; |
| terence zhang | 3:a280069151ac | 101 | } |
| terence zhang | 3:a280069151ac | 102 | else if (lwm2m_strncmp((char *)query->data, ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN) == 0) |
| terence zhang | 3:a280069151ac | 103 | { |
| terence zhang | 3:a280069151ac | 104 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_GREATER_THAN)) return -1; |
| terence zhang | 3:a280069151ac | 105 | if (query->len == ATTR_GREATER_THAN_LEN) return -1; |
| terence zhang | 3:a280069151ac | 106 | |
| terence zhang | 3:a280069151ac | 107 | if (1 != utils_plainTextToFloat64(query->data + ATTR_GREATER_THAN_LEN, query->len - ATTR_GREATER_THAN_LEN, &floatValue)) return -1; |
| terence zhang | 3:a280069151ac | 108 | |
| terence zhang | 3:a280069151ac | 109 | attrP->toSet |= LWM2M_ATTR_FLAG_GREATER_THAN; |
| terence zhang | 3:a280069151ac | 110 | attrP->greaterThan = floatValue; |
| terence zhang | 3:a280069151ac | 111 | } |
| terence zhang | 3:a280069151ac | 112 | else if (lwm2m_strncmp((char *)query->data, ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN - 1) == 0) |
| terence zhang | 3:a280069151ac | 113 | { |
| terence zhang | 3:a280069151ac | 114 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_GREATER_THAN)) return -1; |
| terence zhang | 3:a280069151ac | 115 | if (query->len != ATTR_GREATER_THAN_LEN - 1) return -1; |
| terence zhang | 3:a280069151ac | 116 | |
| terence zhang | 3:a280069151ac | 117 | attrP->toClear |= LWM2M_ATTR_FLAG_GREATER_THAN; |
| terence zhang | 3:a280069151ac | 118 | } |
| terence zhang | 3:a280069151ac | 119 | else if (lwm2m_strncmp((char *)query->data, ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN) == 0) |
| terence zhang | 3:a280069151ac | 120 | { |
| terence zhang | 3:a280069151ac | 121 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_LESS_THAN)) return -1; |
| terence zhang | 3:a280069151ac | 122 | if (query->len == ATTR_LESS_THAN_LEN) return -1; |
| terence zhang | 3:a280069151ac | 123 | |
| terence zhang | 3:a280069151ac | 124 | if (1 != utils_plainTextToFloat64(query->data + ATTR_LESS_THAN_LEN, query->len - ATTR_LESS_THAN_LEN, &floatValue)) return -1; |
| terence zhang | 3:a280069151ac | 125 | |
| terence zhang | 3:a280069151ac | 126 | attrP->toSet |= LWM2M_ATTR_FLAG_LESS_THAN; |
| terence zhang | 3:a280069151ac | 127 | attrP->lessThan = floatValue; |
| terence zhang | 3:a280069151ac | 128 | } |
| terence zhang | 3:a280069151ac | 129 | else if (lwm2m_strncmp((char *)query->data, ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN - 1) == 0) |
| terence zhang | 3:a280069151ac | 130 | { |
| terence zhang | 3:a280069151ac | 131 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_LESS_THAN)) return -1; |
| terence zhang | 3:a280069151ac | 132 | if (query->len != ATTR_LESS_THAN_LEN - 1) return -1; |
| terence zhang | 3:a280069151ac | 133 | |
| terence zhang | 3:a280069151ac | 134 | attrP->toClear |= LWM2M_ATTR_FLAG_LESS_THAN; |
| terence zhang | 3:a280069151ac | 135 | } |
| terence zhang | 3:a280069151ac | 136 | else if (lwm2m_strncmp((char *)query->data, ATTR_STEP_STR, ATTR_STEP_LEN) == 0) |
| terence zhang | 3:a280069151ac | 137 | { |
| terence zhang | 3:a280069151ac | 138 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_STEP)) return -1; |
| terence zhang | 3:a280069151ac | 139 | if (query->len == ATTR_STEP_LEN) return -1; |
| terence zhang | 3:a280069151ac | 140 | |
| terence zhang | 3:a280069151ac | 141 | if (1 != utils_plainTextToFloat64(query->data + ATTR_STEP_LEN, query->len - ATTR_STEP_LEN, &floatValue)) return -1; |
| terence zhang | 3:a280069151ac | 142 | if (floatValue < 0) return -1; |
| terence zhang | 3:a280069151ac | 143 | |
| terence zhang | 3:a280069151ac | 144 | attrP->toSet |= LWM2M_ATTR_FLAG_STEP; |
| terence zhang | 3:a280069151ac | 145 | attrP->step = floatValue; |
| terence zhang | 3:a280069151ac | 146 | } |
| terence zhang | 3:a280069151ac | 147 | else if (lwm2m_strncmp((char *)query->data, ATTR_STEP_STR, ATTR_STEP_LEN - 1) == 0) |
| terence zhang | 3:a280069151ac | 148 | { |
| terence zhang | 3:a280069151ac | 149 | if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_STEP)) return -1; |
| terence zhang | 3:a280069151ac | 150 | if (query->len != ATTR_STEP_LEN - 1) return -1; |
| terence zhang | 3:a280069151ac | 151 | |
| terence zhang | 3:a280069151ac | 152 | attrP->toClear |= LWM2M_ATTR_FLAG_STEP; |
| terence zhang | 3:a280069151ac | 153 | } |
| terence zhang | 3:a280069151ac | 154 | else return -1; |
| terence zhang | 3:a280069151ac | 155 | |
| terence zhang | 3:a280069151ac | 156 | query = query->next; |
| terence zhang | 3:a280069151ac | 157 | } |
| terence zhang | 3:a280069151ac | 158 | |
| terence zhang | 3:a280069151ac | 159 | return 0; |
| terence zhang | 3:a280069151ac | 160 | } |
| terence zhang | 3:a280069151ac | 161 | |
| terence zhang | 3:a280069151ac | 162 | coap_status_t dm_handleRequest(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 163 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 164 | lwm2m_server_t * serverP, |
| terencez | 0:f9d13e09cf11 | 165 | coap_packet_t * message, |
| terencez | 0:f9d13e09cf11 | 166 | coap_packet_t * response) |
| terencez | 0:f9d13e09cf11 | 167 | { |
| terencez | 0:f9d13e09cf11 | 168 | coap_status_t result; |
| terence zhang | 3:a280069151ac | 169 | lwm2m_media_type_t format; |
| terence zhang | 3:a280069151ac | 170 | |
| terence zhang | 3:a280069151ac | 171 | LOG_ARG("Code: %02X, server status: %s", message->code, STR_STATUS(serverP->status)); |
| terence zhang | 3:a280069151ac | 172 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 173 | |
| terence zhang | 3:a280069151ac | 174 | if (IS_OPTION(message, COAP_OPTION_CONTENT_TYPE)) |
| terence zhang | 3:a280069151ac | 175 | { |
| terence zhang | 3:a280069151ac | 176 | format = utils_convertMediaType(message->content_type); |
| terence zhang | 3:a280069151ac | 177 | } |
| terence zhang | 3:a280069151ac | 178 | else |
| terence zhang | 3:a280069151ac | 179 | { |
| terence zhang | 3:a280069151ac | 180 | format = LWM2M_CONTENT_TLV; |
| terence zhang | 3:a280069151ac | 181 | } |
| terence zhang | 3:a280069151ac | 182 | |
| terence zhang | 3:a280069151ac | 183 | if (uriP->objectId == LWM2M_SECURITY_OBJECT_ID) |
| terence zhang | 3:a280069151ac | 184 | { |
| terence zhang | 3:a280069151ac | 185 | return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 186 | } |
| terence zhang | 3:a280069151ac | 187 | |
| terence zhang | 3:a280069151ac | 188 | if (serverP->status != STATE_REGISTERED |
| terence zhang | 3:a280069151ac | 189 | && serverP->status != STATE_REG_UPDATE_NEEDED |
| terence zhang | 3:a280069151ac | 190 | && serverP->status != STATE_REG_FULL_UPDATE_NEEDED |
| terence zhang | 3:a280069151ac | 191 | && serverP->status != STATE_REG_UPDATE_PENDING) |
| terence zhang | 3:a280069151ac | 192 | { |
| terence zhang | 3:a280069151ac | 193 | return COAP_IGNORE; |
| terence zhang | 3:a280069151ac | 194 | } |
| terence zhang | 3:a280069151ac | 195 | |
| terence zhang | 3:a280069151ac | 196 | // TODO: check ACL |
| terencez | 0:f9d13e09cf11 | 197 | |
| terencez | 0:f9d13e09cf11 | 198 | switch (message->code) |
| terencez | 0:f9d13e09cf11 | 199 | { |
| terencez | 0:f9d13e09cf11 | 200 | case COAP_GET: |
| terencez | 0:f9d13e09cf11 | 201 | { |
| terence zhang | 3:a280069151ac | 202 | uint8_t * buffer = NULL; |
| terence zhang | 3:a280069151ac | 203 | size_t length = 0; |
| terence zhang | 3:a280069151ac | 204 | int res; |
| terencez | 0:f9d13e09cf11 | 205 | |
| terence zhang | 3:a280069151ac | 206 | if (IS_OPTION(message, COAP_OPTION_OBSERVE)) |
| terencez | 0:f9d13e09cf11 | 207 | { |
| terence zhang | 3:a280069151ac | 208 | lwm2m_data_t * dataP = NULL; |
| terence zhang | 3:a280069151ac | 209 | int size = 0; |
| terence zhang | 3:a280069151ac | 210 | |
| terence zhang | 3:a280069151ac | 211 | result = object_readData(contextP, uriP, &size, &dataP); |
| terence zhang | 3:a280069151ac | 212 | if (COAP_205_CONTENT == result) |
| terencez | 0:f9d13e09cf11 | 213 | { |
| terence zhang | 3:a280069151ac | 214 | result = observe_handleRequest(contextP, uriP, serverP, size, dataP, message, response); |
| terence zhang | 3:a280069151ac | 215 | if (COAP_205_CONTENT == result) |
| terence zhang | 3:a280069151ac | 216 | { |
| terence zhang | 3:a280069151ac | 217 | res = lwm2m_data_serialize(uriP, size, dataP, &format, &buffer); |
| terence zhang | 3:a280069151ac | 218 | if (res < 0) |
| terence zhang | 3:a280069151ac | 219 | { |
| terence zhang | 3:a280069151ac | 220 | result = COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 221 | } |
| terence zhang | 3:a280069151ac | 222 | else |
| terence zhang | 3:a280069151ac | 223 | { |
| terence zhang | 3:a280069151ac | 224 | length = (size_t)res; |
| terence zhang | 3:a280069151ac | 225 | LOG_ARG("Observe Request[/%d/%d/%d]: %.*s\n", uriP->objectId, uriP->instanceId, uriP->resourceId, length, buffer); |
| terence zhang | 3:a280069151ac | 226 | } |
| terence zhang | 3:a280069151ac | 227 | } |
| terence zhang | 3:a280069151ac | 228 | lwm2m_data_free(size, dataP); |
| terencez | 0:f9d13e09cf11 | 229 | } |
| terence zhang | 3:a280069151ac | 230 | } |
| terence zhang | 3:a280069151ac | 231 | else if (IS_OPTION(message, COAP_OPTION_ACCEPT) |
| terence zhang | 3:a280069151ac | 232 | && message->accept_num == 1 |
| terence zhang | 3:a280069151ac | 233 | && message->accept[0] == APPLICATION_LINK_FORMAT) |
| terence zhang | 3:a280069151ac | 234 | { |
| terence zhang | 3:a280069151ac | 235 | format = LWM2M_CONTENT_LINK; |
| terence zhang | 3:a280069151ac | 236 | result = object_discover(contextP, uriP, serverP, &buffer, &length); |
| terence zhang | 3:a280069151ac | 237 | } |
| terence zhang | 3:a280069151ac | 238 | else |
| terence zhang | 3:a280069151ac | 239 | { |
| terence zhang | 3:a280069151ac | 240 | if (IS_OPTION(message, COAP_OPTION_ACCEPT)) |
| terencez | 0:f9d13e09cf11 | 241 | { |
| terence zhang | 3:a280069151ac | 242 | format = utils_convertMediaType(message->accept[0]); |
| terencez | 0:f9d13e09cf11 | 243 | } |
| terence zhang | 3:a280069151ac | 244 | |
| terence zhang | 3:a280069151ac | 245 | result = object_read(contextP, uriP, &format, &buffer, &length); |
| terence zhang | 3:a280069151ac | 246 | } |
| terence zhang | 3:a280069151ac | 247 | if (COAP_205_CONTENT == result) |
| terence zhang | 3:a280069151ac | 248 | { |
| terence zhang | 3:a280069151ac | 249 | coap_set_header_content_type(response, format); |
| terence zhang | 3:a280069151ac | 250 | coap_set_payload(response, buffer, length); |
| terence zhang | 3:a280069151ac | 251 | // lwm2m_handle_packet will free buffer |
| terence zhang | 3:a280069151ac | 252 | } |
| terence zhang | 3:a280069151ac | 253 | else |
| terence zhang | 3:a280069151ac | 254 | { |
| terence zhang | 3:a280069151ac | 255 | lwm2m_free(buffer); |
| terencez | 0:f9d13e09cf11 | 256 | } |
| terencez | 0:f9d13e09cf11 | 257 | } |
| terencez | 0:f9d13e09cf11 | 258 | break; |
| terence zhang | 3:a280069151ac | 259 | |
| terencez | 0:f9d13e09cf11 | 260 | case COAP_POST: |
| terencez | 0:f9d13e09cf11 | 261 | { |
| terencez | 0:f9d13e09cf11 | 262 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 263 | { |
| terence zhang | 3:a280069151ac | 264 | result = object_create(contextP, uriP, format, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 265 | if (result == COAP_201_CREATED) |
| terencez | 0:f9d13e09cf11 | 266 | { |
| terencez | 0:f9d13e09cf11 | 267 | //longest uri is /65535/65535 = 12 + 1 (null) chars |
| terencez | 0:f9d13e09cf11 | 268 | char location_path[13] = ""; |
| terencez | 0:f9d13e09cf11 | 269 | //instanceId expected |
| terencez | 0:f9d13e09cf11 | 270 | if ((uriP->flag & LWM2M_URI_FLAG_INSTANCE_ID) == 0) |
| terencez | 0:f9d13e09cf11 | 271 | { |
| terencez | 0:f9d13e09cf11 | 272 | result = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 273 | break; |
| terencez | 0:f9d13e09cf11 | 274 | } |
| terencez | 0:f9d13e09cf11 | 275 | |
| terencez | 0:f9d13e09cf11 | 276 | if (sprintf(location_path, "/%d/%d", uriP->objectId, uriP->instanceId) < 0) |
| terencez | 0:f9d13e09cf11 | 277 | { |
| terencez | 0:f9d13e09cf11 | 278 | result = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 279 | break; |
| terencez | 0:f9d13e09cf11 | 280 | } |
| terencez | 0:f9d13e09cf11 | 281 | coap_set_header_location_path(response, location_path); |
| terence zhang | 3:a280069151ac | 282 | |
| terence zhang | 3:a280069151ac | 283 | lwm2m_update_registration(contextP, 0, true); |
| terencez | 0:f9d13e09cf11 | 284 | } |
| terencez | 0:f9d13e09cf11 | 285 | } |
| terencez | 0:f9d13e09cf11 | 286 | else if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 287 | { |
| terence zhang | 3:a280069151ac | 288 | result = object_write(contextP, uriP, format, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 289 | } |
| terencez | 0:f9d13e09cf11 | 290 | else |
| terencez | 0:f9d13e09cf11 | 291 | { |
| terencez | 0:f9d13e09cf11 | 292 | result = object_execute(contextP, uriP, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 293 | } |
| terencez | 0:f9d13e09cf11 | 294 | } |
| terencez | 0:f9d13e09cf11 | 295 | break; |
| terence zhang | 3:a280069151ac | 296 | |
| terencez | 0:f9d13e09cf11 | 297 | case COAP_PUT: |
| terencez | 0:f9d13e09cf11 | 298 | { |
| terence zhang | 3:a280069151ac | 299 | if (IS_OPTION(message, COAP_OPTION_URI_QUERY)) |
| terencez | 0:f9d13e09cf11 | 300 | { |
| terence zhang | 3:a280069151ac | 301 | lwm2m_attributes_t attr; |
| terence zhang | 3:a280069151ac | 302 | |
| terence zhang | 3:a280069151ac | 303 | if (0 != prv_readAttributes(message->uri_query, &attr)) |
| terence zhang | 3:a280069151ac | 304 | { |
| terence zhang | 3:a280069151ac | 305 | result = COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 306 | } |
| terence zhang | 3:a280069151ac | 307 | else |
| terence zhang | 3:a280069151ac | 308 | { |
| terence zhang | 3:a280069151ac | 309 | result = observe_setParameters(contextP, uriP, serverP, &attr); |
| terence zhang | 3:a280069151ac | 310 | } |
| terence zhang | 3:a280069151ac | 311 | } |
| terence zhang | 3:a280069151ac | 312 | else if (LWM2M_URI_IS_SET_INSTANCE(uriP)) |
| terence zhang | 3:a280069151ac | 313 | { |
| terence zhang | 3:a280069151ac | 314 | result = object_write(contextP, uriP, format, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 315 | } |
| terencez | 0:f9d13e09cf11 | 316 | else |
| terencez | 0:f9d13e09cf11 | 317 | { |
| terence zhang | 3:a280069151ac | 318 | result = COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 319 | } |
| terencez | 0:f9d13e09cf11 | 320 | } |
| terencez | 0:f9d13e09cf11 | 321 | break; |
| terence zhang | 3:a280069151ac | 322 | |
| terencez | 0:f9d13e09cf11 | 323 | case COAP_DELETE: |
| terencez | 0:f9d13e09cf11 | 324 | { |
| terence zhang | 3:a280069151ac | 325 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP) || LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 326 | { |
| terence zhang | 3:a280069151ac | 327 | result = COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 328 | } |
| terencez | 0:f9d13e09cf11 | 329 | else |
| terencez | 0:f9d13e09cf11 | 330 | { |
| terence zhang | 3:a280069151ac | 331 | result = object_delete(contextP, uriP); |
| terence zhang | 3:a280069151ac | 332 | if (result == COAP_202_DELETED) |
| terence zhang | 3:a280069151ac | 333 | { |
| terence zhang | 3:a280069151ac | 334 | lwm2m_update_registration(contextP, 0, true); |
| terence zhang | 3:a280069151ac | 335 | } |
| terencez | 0:f9d13e09cf11 | 336 | } |
| terencez | 0:f9d13e09cf11 | 337 | } |
| terencez | 0:f9d13e09cf11 | 338 | break; |
| terence zhang | 3:a280069151ac | 339 | |
| terencez | 0:f9d13e09cf11 | 340 | default: |
| terence zhang | 3:a280069151ac | 341 | result = COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 342 | break; |
| terencez | 0:f9d13e09cf11 | 343 | } |
| terencez | 0:f9d13e09cf11 | 344 | |
| terencez | 0:f9d13e09cf11 | 345 | return result; |
| terencez | 0:f9d13e09cf11 | 346 | } |
| terence zhang | 3:a280069151ac | 347 | |
| terencez | 0:f9d13e09cf11 | 348 | #endif |
| terencez | 0:f9d13e09cf11 | 349 | |
| terencez | 0:f9d13e09cf11 | 350 | #ifdef LWM2M_SERVER_MODE |
| terencez | 0:f9d13e09cf11 | 351 | |
| terencez | 0:f9d13e09cf11 | 352 | #define ID_AS_STRING_MAX_LEN 8 |
| terencez | 0:f9d13e09cf11 | 353 | |
| terence zhang | 3:a280069151ac | 354 | static void prv_resultCallback(lwm2m_transaction_t * transacP, |
| terencez | 0:f9d13e09cf11 | 355 | void * message) |
| terencez | 0:f9d13e09cf11 | 356 | { |
| terencez | 0:f9d13e09cf11 | 357 | dm_data_t * dataP = (dm_data_t *)transacP->userData; |
| terencez | 0:f9d13e09cf11 | 358 | |
| terencez | 0:f9d13e09cf11 | 359 | if (message == NULL) |
| terencez | 0:f9d13e09cf11 | 360 | { |
| terence zhang | 3:a280069151ac | 361 | dataP->callback(dataP->clientID, |
| terencez | 0:f9d13e09cf11 | 362 | &dataP->uri, |
| terencez | 0:f9d13e09cf11 | 363 | COAP_503_SERVICE_UNAVAILABLE, |
| terence zhang | 3:a280069151ac | 364 | LWM2M_CONTENT_TEXT, NULL, 0, |
| terencez | 0:f9d13e09cf11 | 365 | dataP->userData); |
| terencez | 0:f9d13e09cf11 | 366 | } |
| terencez | 0:f9d13e09cf11 | 367 | else |
| terencez | 0:f9d13e09cf11 | 368 | { |
| terencez | 0:f9d13e09cf11 | 369 | coap_packet_t * packet = (coap_packet_t *)message; |
| terencez | 0:f9d13e09cf11 | 370 | |
| terencez | 0:f9d13e09cf11 | 371 | //if packet is a CREATE response and the instanceId was assigned by the client |
| terencez | 0:f9d13e09cf11 | 372 | if (packet->code == COAP_201_CREATED |
| terencez | 0:f9d13e09cf11 | 373 | && packet->location_path != NULL) |
| terencez | 0:f9d13e09cf11 | 374 | { |
| terencez | 0:f9d13e09cf11 | 375 | char * locationString = NULL; |
| terencez | 0:f9d13e09cf11 | 376 | int result = 0; |
| terencez | 0:f9d13e09cf11 | 377 | lwm2m_uri_t locationUri; |
| terencez | 0:f9d13e09cf11 | 378 | |
| terencez | 0:f9d13e09cf11 | 379 | locationString = coap_get_multi_option_as_string(packet->location_path); |
| terencez | 0:f9d13e09cf11 | 380 | if (locationString == NULL) |
| terencez | 0:f9d13e09cf11 | 381 | { |
| terence zhang | 3:a280069151ac | 382 | LOG("Error: coap_get_multi_option_as_string() failed for Location_path option in prv_resultCallback()"); |
| terencez | 0:f9d13e09cf11 | 383 | return; |
| terencez | 0:f9d13e09cf11 | 384 | } |
| terencez | 0:f9d13e09cf11 | 385 | |
| terencez | 0:f9d13e09cf11 | 386 | result = lwm2m_stringToUri(locationString, strlen(locationString), &locationUri); |
| terencez | 0:f9d13e09cf11 | 387 | if (result == 0) |
| terencez | 0:f9d13e09cf11 | 388 | { |
| terence zhang | 3:a280069151ac | 389 | LOG("Error: lwm2m_stringToUri() failed for Location_path option in prv_resultCallback()"); |
| terencez | 0:f9d13e09cf11 | 390 | lwm2m_free(locationString); |
| terencez | 0:f9d13e09cf11 | 391 | return; |
| terencez | 0:f9d13e09cf11 | 392 | } |
| terencez | 0:f9d13e09cf11 | 393 | |
| terencez | 0:f9d13e09cf11 | 394 | ((dm_data_t*)transacP->userData)->uri.instanceId = locationUri.instanceId; |
| terencez | 0:f9d13e09cf11 | 395 | ((dm_data_t*)transacP->userData)->uri.flag = locationUri.flag; |
| terencez | 0:f9d13e09cf11 | 396 | |
| terencez | 0:f9d13e09cf11 | 397 | lwm2m_free(locationString); |
| terencez | 0:f9d13e09cf11 | 398 | } |
| terencez | 0:f9d13e09cf11 | 399 | |
| terence zhang | 3:a280069151ac | 400 | dataP->callback(dataP->clientID, |
| terencez | 0:f9d13e09cf11 | 401 | &dataP->uri, |
| terencez | 0:f9d13e09cf11 | 402 | packet->code, |
| terence zhang | 3:a280069151ac | 403 | utils_convertMediaType(packet->content_type), |
| terencez | 0:f9d13e09cf11 | 404 | packet->payload, |
| terencez | 0:f9d13e09cf11 | 405 | packet->payload_len, |
| terencez | 0:f9d13e09cf11 | 406 | dataP->userData); |
| terencez | 0:f9d13e09cf11 | 407 | } |
| terencez | 0:f9d13e09cf11 | 408 | lwm2m_free(dataP); |
| terencez | 0:f9d13e09cf11 | 409 | } |
| terencez | 0:f9d13e09cf11 | 410 | |
| terence zhang | 3:a280069151ac | 411 | static int prv_makeOperation(lwm2m_context_t * contextP, |
| terence zhang | 3:a280069151ac | 412 | uint16_t clientID, |
| terence zhang | 3:a280069151ac | 413 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 414 | coap_method_t method, |
| terence zhang | 3:a280069151ac | 415 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 416 | uint8_t * buffer, |
| terence zhang | 3:a280069151ac | 417 | int length, |
| terence zhang | 3:a280069151ac | 418 | lwm2m_result_callback_t callback, |
| terence zhang | 3:a280069151ac | 419 | void * userData) |
| terencez | 0:f9d13e09cf11 | 420 | { |
| terencez | 0:f9d13e09cf11 | 421 | lwm2m_client_t * clientP; |
| terencez | 0:f9d13e09cf11 | 422 | lwm2m_transaction_t * transaction; |
| terencez | 0:f9d13e09cf11 | 423 | dm_data_t * dataP; |
| terencez | 0:f9d13e09cf11 | 424 | |
| terencez | 0:f9d13e09cf11 | 425 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terencez | 0:f9d13e09cf11 | 426 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terencez | 0:f9d13e09cf11 | 427 | |
| terence zhang | 3:a280069151ac | 428 | transaction = transaction_new(clientP->sessionH, method, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); |
| terence zhang | 3:a280069151ac | 429 | if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 430 | |
| terence zhang | 3:a280069151ac | 431 | if (method == COAP_GET) |
| terencez | 0:f9d13e09cf11 | 432 | { |
| terence zhang | 3:a280069151ac | 433 | coap_set_header_accept(transaction->message, format); |
| terence zhang | 3:a280069151ac | 434 | } |
| terence zhang | 3:a280069151ac | 435 | else if (buffer != NULL) |
| terence zhang | 3:a280069151ac | 436 | { |
| terence zhang | 3:a280069151ac | 437 | coap_set_header_content_type(transaction->message, format); |
| terencez | 0:f9d13e09cf11 | 438 | // TODO: Take care of fragmentation |
| terencez | 0:f9d13e09cf11 | 439 | coap_set_payload(transaction->message, buffer, length); |
| terencez | 0:f9d13e09cf11 | 440 | } |
| terencez | 0:f9d13e09cf11 | 441 | |
| terencez | 0:f9d13e09cf11 | 442 | if (callback != NULL) |
| terencez | 0:f9d13e09cf11 | 443 | { |
| terencez | 0:f9d13e09cf11 | 444 | dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); |
| terencez | 0:f9d13e09cf11 | 445 | if (dataP == NULL) |
| terencez | 0:f9d13e09cf11 | 446 | { |
| terencez | 0:f9d13e09cf11 | 447 | transaction_free(transaction); |
| terencez | 0:f9d13e09cf11 | 448 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 449 | } |
| terencez | 0:f9d13e09cf11 | 450 | memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); |
| terence zhang | 3:a280069151ac | 451 | dataP->clientID = clientP->internalID; |
| terencez | 0:f9d13e09cf11 | 452 | dataP->callback = callback; |
| terencez | 0:f9d13e09cf11 | 453 | dataP->userData = userData; |
| terencez | 0:f9d13e09cf11 | 454 | |
| terence zhang | 3:a280069151ac | 455 | transaction->callback = prv_resultCallback; |
| terencez | 0:f9d13e09cf11 | 456 | transaction->userData = (void *)dataP; |
| terencez | 0:f9d13e09cf11 | 457 | } |
| terencez | 0:f9d13e09cf11 | 458 | |
| terencez | 0:f9d13e09cf11 | 459 | contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); |
| terencez | 0:f9d13e09cf11 | 460 | |
| terencez | 0:f9d13e09cf11 | 461 | return transaction_send(contextP, transaction); |
| terencez | 0:f9d13e09cf11 | 462 | } |
| terencez | 0:f9d13e09cf11 | 463 | |
| terencez | 0:f9d13e09cf11 | 464 | int lwm2m_dm_read(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 465 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 466 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 467 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 468 | void * userData) |
| terencez | 0:f9d13e09cf11 | 469 | { |
| terence zhang | 3:a280069151ac | 470 | lwm2m_client_t * clientP; |
| terence zhang | 3:a280069151ac | 471 | lwm2m_media_type_t format; |
| terence zhang | 3:a280069151ac | 472 | |
| terence zhang | 3:a280069151ac | 473 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 474 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 475 | |
| terence zhang | 3:a280069151ac | 476 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terence zhang | 3:a280069151ac | 477 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 478 | |
| terence zhang | 3:a280069151ac | 479 | if (clientP->supportJSON == true) |
| terence zhang | 3:a280069151ac | 480 | { |
| terence zhang | 3:a280069151ac | 481 | format = LWM2M_CONTENT_JSON; |
| terence zhang | 3:a280069151ac | 482 | } |
| terence zhang | 3:a280069151ac | 483 | else |
| terence zhang | 3:a280069151ac | 484 | { |
| terence zhang | 3:a280069151ac | 485 | format = LWM2M_CONTENT_TLV; |
| terence zhang | 3:a280069151ac | 486 | } |
| terence zhang | 3:a280069151ac | 487 | |
| terence zhang | 3:a280069151ac | 488 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 489 | COAP_GET, |
| terence zhang | 3:a280069151ac | 490 | format, |
| terence zhang | 3:a280069151ac | 491 | NULL, 0, |
| terence zhang | 3:a280069151ac | 492 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 493 | } |
| terencez | 0:f9d13e09cf11 | 494 | |
| terencez | 0:f9d13e09cf11 | 495 | int lwm2m_dm_write(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 496 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 497 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 498 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 499 | uint8_t * buffer, |
| terencez | 0:f9d13e09cf11 | 500 | int length, |
| terencez | 0:f9d13e09cf11 | 501 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 502 | void * userData) |
| terencez | 0:f9d13e09cf11 | 503 | { |
| terence zhang | 3:a280069151ac | 504 | LOG_ARG("clientID: %d, format: %s, length: %d", clientID, STR_MEDIA_TYPE(format), length); |
| terence zhang | 3:a280069151ac | 505 | LOG_URI(uriP); |
| terencez | 0:f9d13e09cf11 | 506 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP) |
| terencez | 0:f9d13e09cf11 | 507 | || length == 0) |
| terencez | 0:f9d13e09cf11 | 508 | { |
| terencez | 0:f9d13e09cf11 | 509 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 510 | } |
| terencez | 0:f9d13e09cf11 | 511 | |
| terencez | 0:f9d13e09cf11 | 512 | if (LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 513 | { |
| terence zhang | 3:a280069151ac | 514 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 515 | COAP_PUT, |
| terence zhang | 3:a280069151ac | 516 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 517 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 518 | } |
| terencez | 0:f9d13e09cf11 | 519 | else |
| terencez | 0:f9d13e09cf11 | 520 | { |
| terence zhang | 3:a280069151ac | 521 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 522 | COAP_POST, |
| terence zhang | 3:a280069151ac | 523 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 524 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 525 | } |
| terencez | 0:f9d13e09cf11 | 526 | } |
| terencez | 0:f9d13e09cf11 | 527 | |
| terencez | 0:f9d13e09cf11 | 528 | int lwm2m_dm_execute(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 529 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 530 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 531 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 532 | uint8_t * buffer, |
| terencez | 0:f9d13e09cf11 | 533 | int length, |
| terencez | 0:f9d13e09cf11 | 534 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 535 | void * userData) |
| terencez | 0:f9d13e09cf11 | 536 | { |
| terence zhang | 3:a280069151ac | 537 | LOG_ARG("clientID: %d, format: %s, length: %d", clientID, STR_MEDIA_TYPE(format), length); |
| terence zhang | 3:a280069151ac | 538 | LOG_URI(uriP); |
| terencez | 0:f9d13e09cf11 | 539 | if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 540 | { |
| terencez | 0:f9d13e09cf11 | 541 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 542 | } |
| terencez | 0:f9d13e09cf11 | 543 | |
| terence zhang | 3:a280069151ac | 544 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 545 | COAP_POST, |
| terence zhang | 3:a280069151ac | 546 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 547 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 548 | } |
| terencez | 0:f9d13e09cf11 | 549 | |
| terencez | 0:f9d13e09cf11 | 550 | int lwm2m_dm_create(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 551 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 552 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 553 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 554 | uint8_t * buffer, |
| terencez | 0:f9d13e09cf11 | 555 | int length, |
| terencez | 0:f9d13e09cf11 | 556 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 557 | void * userData) |
| terencez | 0:f9d13e09cf11 | 558 | { |
| terence zhang | 3:a280069151ac | 559 | LOG_ARG("clientID: %d, format: %s, length: %d", clientID, STR_MEDIA_TYPE(format), length); |
| terence zhang | 3:a280069151ac | 560 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 561 | |
| terence zhang | 3:a280069151ac | 562 | if (LWM2M_URI_IS_SET_INSTANCE(uriP) |
| terencez | 0:f9d13e09cf11 | 563 | || length == 0) |
| terencez | 0:f9d13e09cf11 | 564 | { |
| terencez | 0:f9d13e09cf11 | 565 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 566 | } |
| terencez | 0:f9d13e09cf11 | 567 | |
| terence zhang | 3:a280069151ac | 568 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 569 | COAP_POST, |
| terence zhang | 3:a280069151ac | 570 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 571 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 572 | } |
| terencez | 0:f9d13e09cf11 | 573 | |
| terencez | 0:f9d13e09cf11 | 574 | int lwm2m_dm_delete(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 575 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 576 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 577 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 578 | void * userData) |
| terencez | 0:f9d13e09cf11 | 579 | { |
| terence zhang | 3:a280069151ac | 580 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 581 | LOG_URI(uriP); |
| terencez | 0:f9d13e09cf11 | 582 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP) |
| terencez | 0:f9d13e09cf11 | 583 | || LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 584 | { |
| terencez | 0:f9d13e09cf11 | 585 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 586 | } |
| terencez | 0:f9d13e09cf11 | 587 | |
| terence zhang | 3:a280069151ac | 588 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 589 | COAP_DELETE, |
| terence zhang | 3:a280069151ac | 590 | LWM2M_CONTENT_TEXT, NULL, 0, |
| terencez | 0:f9d13e09cf11 | 591 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 592 | } |
| terence zhang | 3:a280069151ac | 593 | |
| terence zhang | 3:a280069151ac | 594 | int lwm2m_dm_write_attributes(lwm2m_context_t * contextP, |
| terence zhang | 3:a280069151ac | 595 | uint16_t clientID, |
| terence zhang | 3:a280069151ac | 596 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 597 | lwm2m_attributes_t * attrP, |
| terence zhang | 3:a280069151ac | 598 | lwm2m_result_callback_t callback, |
| terence zhang | 3:a280069151ac | 599 | void * userData) |
| terence zhang | 3:a280069151ac | 600 | { |
| terence zhang | 3:a280069151ac | 601 | #define _PRV_BUFFER_SIZE 32 |
| terence zhang | 3:a280069151ac | 602 | lwm2m_client_t * clientP; |
| terence zhang | 3:a280069151ac | 603 | lwm2m_transaction_t * transaction; |
| terence zhang | 3:a280069151ac | 604 | coap_packet_t * coap_pkt; |
| terence zhang | 3:a280069151ac | 605 | uint8_t buffer[_PRV_BUFFER_SIZE]; |
| terence zhang | 3:a280069151ac | 606 | size_t length; |
| terence zhang | 3:a280069151ac | 607 | |
| terence zhang | 3:a280069151ac | 608 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 609 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 610 | if (attrP == NULL) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 611 | |
| terence zhang | 3:a280069151ac | 612 | if (0 != (attrP->toSet & attrP->toClear)) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 613 | if (0 != (attrP->toSet & ATTR_FLAG_NUMERIC) && !LWM2M_URI_IS_SET_RESOURCE(uriP)) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 614 | if (ATTR_FLAG_NUMERIC == (attrP->toSet & ATTR_FLAG_NUMERIC) |
| terence zhang | 3:a280069151ac | 615 | && (attrP->lessThan + 2 * attrP->step >= attrP->greaterThan)) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 616 | |
| terence zhang | 3:a280069151ac | 617 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terence zhang | 3:a280069151ac | 618 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 619 | |
| terence zhang | 3:a280069151ac | 620 | transaction = transaction_new(clientP->sessionH, COAP_PUT, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); |
| terence zhang | 3:a280069151ac | 621 | if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 622 | |
| terence zhang | 3:a280069151ac | 623 | if (callback != NULL) |
| terence zhang | 3:a280069151ac | 624 | { |
| terence zhang | 3:a280069151ac | 625 | dm_data_t * dataP; |
| terence zhang | 3:a280069151ac | 626 | |
| terence zhang | 3:a280069151ac | 627 | dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); |
| terence zhang | 3:a280069151ac | 628 | if (dataP == NULL) |
| terence zhang | 3:a280069151ac | 629 | { |
| terence zhang | 3:a280069151ac | 630 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 631 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 632 | } |
| terence zhang | 3:a280069151ac | 633 | memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); |
| terence zhang | 3:a280069151ac | 634 | dataP->clientID = clientP->internalID; |
| terence zhang | 3:a280069151ac | 635 | dataP->callback = callback; |
| terence zhang | 3:a280069151ac | 636 | dataP->userData = userData; |
| terence zhang | 3:a280069151ac | 637 | |
| terence zhang | 3:a280069151ac | 638 | transaction->callback = prv_resultCallback; |
| terence zhang | 3:a280069151ac | 639 | transaction->userData = (void *)dataP; |
| terence zhang | 3:a280069151ac | 640 | } |
| terence zhang | 3:a280069151ac | 641 | |
| terence zhang | 3:a280069151ac | 642 | coap_pkt = (coap_packet_t *)transaction->message; |
| terence zhang | 3:a280069151ac | 643 | free_multi_option(coap_pkt->uri_query); |
| terence zhang | 3:a280069151ac | 644 | if (attrP->toSet & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terence zhang | 3:a280069151ac | 645 | { |
| terence zhang | 3:a280069151ac | 646 | memcpy(buffer, ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 647 | length = utils_intToText(attrP->minPeriod, buffer + ATTR_MIN_PERIOD_LEN, _PRV_BUFFER_SIZE - ATTR_MIN_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 648 | if (length == 0) |
| terence zhang | 3:a280069151ac | 649 | { |
| terence zhang | 3:a280069151ac | 650 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 651 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 652 | } |
| terence zhang | 3:a280069151ac | 653 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_MIN_PERIOD_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 654 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 655 | } |
| terence zhang | 3:a280069151ac | 656 | if (attrP->toSet & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terence zhang | 3:a280069151ac | 657 | { |
| terence zhang | 3:a280069151ac | 658 | memcpy(buffer, ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 659 | length = utils_intToText(attrP->maxPeriod, buffer + ATTR_MAX_PERIOD_LEN, _PRV_BUFFER_SIZE - ATTR_MAX_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 660 | if (length == 0) |
| terence zhang | 3:a280069151ac | 661 | { |
| terence zhang | 3:a280069151ac | 662 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 663 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 664 | } |
| terence zhang | 3:a280069151ac | 665 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_MAX_PERIOD_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 666 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 667 | } |
| terence zhang | 3:a280069151ac | 668 | if (attrP->toSet & LWM2M_ATTR_FLAG_GREATER_THAN) |
| terence zhang | 3:a280069151ac | 669 | { |
| terence zhang | 3:a280069151ac | 670 | memcpy(buffer, ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN); |
| terence zhang | 3:a280069151ac | 671 | length = utils_floatToText(attrP->greaterThan, buffer + ATTR_GREATER_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_GREATER_THAN_LEN); |
| terence zhang | 3:a280069151ac | 672 | if (length == 0) |
| terence zhang | 3:a280069151ac | 673 | { |
| terence zhang | 3:a280069151ac | 674 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 675 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 676 | } |
| terence zhang | 3:a280069151ac | 677 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_GREATER_THAN_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 678 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 679 | } |
| terence zhang | 3:a280069151ac | 680 | if (attrP->toSet & LWM2M_ATTR_FLAG_LESS_THAN) |
| terence zhang | 3:a280069151ac | 681 | { |
| terence zhang | 3:a280069151ac | 682 | memcpy(buffer, ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN); |
| terence zhang | 3:a280069151ac | 683 | length = utils_floatToText(attrP->lessThan, buffer + ATTR_LESS_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_LESS_THAN_LEN); |
| terence zhang | 3:a280069151ac | 684 | if (length == 0) |
| terence zhang | 3:a280069151ac | 685 | { |
| terence zhang | 3:a280069151ac | 686 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 687 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 688 | } |
| terence zhang | 3:a280069151ac | 689 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_LESS_THAN_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 690 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 691 | } |
| terence zhang | 3:a280069151ac | 692 | if (attrP->toSet & LWM2M_ATTR_FLAG_STEP) |
| terence zhang | 3:a280069151ac | 693 | { |
| terence zhang | 3:a280069151ac | 694 | memcpy(buffer, ATTR_STEP_STR, ATTR_STEP_LEN); |
| terence zhang | 3:a280069151ac | 695 | length = utils_floatToText(attrP->step, buffer + ATTR_STEP_LEN, _PRV_BUFFER_SIZE - ATTR_STEP_LEN); |
| terence zhang | 3:a280069151ac | 696 | if (length == 0) |
| terence zhang | 3:a280069151ac | 697 | { |
| terence zhang | 3:a280069151ac | 698 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 699 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 700 | } |
| terence zhang | 3:a280069151ac | 701 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_STEP_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 702 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 703 | } |
| terence zhang | 3:a280069151ac | 704 | if (attrP->toClear & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terence zhang | 3:a280069151ac | 705 | { |
| terence zhang | 3:a280069151ac | 706 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN -1, 0); |
| terence zhang | 3:a280069151ac | 707 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 708 | } |
| terence zhang | 3:a280069151ac | 709 | if (attrP->toClear & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terence zhang | 3:a280069151ac | 710 | { |
| terence zhang | 3:a280069151ac | 711 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 712 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 713 | } |
| terence zhang | 3:a280069151ac | 714 | if (attrP->toClear & LWM2M_ATTR_FLAG_GREATER_THAN) |
| terence zhang | 3:a280069151ac | 715 | { |
| terence zhang | 3:a280069151ac | 716 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 717 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 718 | } |
| terence zhang | 3:a280069151ac | 719 | if (attrP->toClear & LWM2M_ATTR_FLAG_LESS_THAN) |
| terence zhang | 3:a280069151ac | 720 | { |
| terence zhang | 3:a280069151ac | 721 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 722 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 723 | } |
| terence zhang | 3:a280069151ac | 724 | if (attrP->toClear & LWM2M_ATTR_FLAG_STEP) |
| terence zhang | 3:a280069151ac | 725 | { |
| terence zhang | 3:a280069151ac | 726 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_STEP_STR, ATTR_STEP_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 727 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 728 | } |
| terence zhang | 3:a280069151ac | 729 | |
| terence zhang | 3:a280069151ac | 730 | contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); |
| terence zhang | 3:a280069151ac | 731 | |
| terence zhang | 3:a280069151ac | 732 | return transaction_send(contextP, transaction); |
| terence zhang | 3:a280069151ac | 733 | } |
| terence zhang | 3:a280069151ac | 734 | |
| terence zhang | 3:a280069151ac | 735 | int lwm2m_dm_discover(lwm2m_context_t * contextP, |
| terence zhang | 3:a280069151ac | 736 | uint16_t clientID, |
| terence zhang | 3:a280069151ac | 737 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 738 | lwm2m_result_callback_t callback, |
| terence zhang | 3:a280069151ac | 739 | void * userData) |
| terence zhang | 3:a280069151ac | 740 | { |
| terence zhang | 3:a280069151ac | 741 | lwm2m_client_t * clientP; |
| terence zhang | 3:a280069151ac | 742 | lwm2m_transaction_t * transaction; |
| terence zhang | 3:a280069151ac | 743 | dm_data_t * dataP; |
| terence zhang | 3:a280069151ac | 744 | |
| terence zhang | 3:a280069151ac | 745 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 746 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 747 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terence zhang | 3:a280069151ac | 748 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 749 | |
| terence zhang | 3:a280069151ac | 750 | transaction = transaction_new(clientP->sessionH, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); |
| terence zhang | 3:a280069151ac | 751 | if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 752 | |
| terence zhang | 3:a280069151ac | 753 | coap_set_header_accept(transaction->message, LWM2M_CONTENT_LINK); |
| terence zhang | 3:a280069151ac | 754 | |
| terence zhang | 3:a280069151ac | 755 | if (callback != NULL) |
| terence zhang | 3:a280069151ac | 756 | { |
| terence zhang | 3:a280069151ac | 757 | dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); |
| terence zhang | 3:a280069151ac | 758 | if (dataP == NULL) |
| terence zhang | 3:a280069151ac | 759 | { |
| terence zhang | 3:a280069151ac | 760 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 761 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 762 | } |
| terence zhang | 3:a280069151ac | 763 | memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); |
| terence zhang | 3:a280069151ac | 764 | dataP->clientID = clientP->internalID; |
| terence zhang | 3:a280069151ac | 765 | dataP->callback = callback; |
| terence zhang | 3:a280069151ac | 766 | dataP->userData = userData; |
| terence zhang | 3:a280069151ac | 767 | |
| terence zhang | 3:a280069151ac | 768 | transaction->callback = prv_resultCallback; |
| terence zhang | 3:a280069151ac | 769 | transaction->userData = (void *)dataP; |
| terence zhang | 3:a280069151ac | 770 | } |
| terence zhang | 3:a280069151ac | 771 | |
| terence zhang | 3:a280069151ac | 772 | contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); |
| terence zhang | 3:a280069151ac | 773 | |
| terence zhang | 3:a280069151ac | 774 | return transaction_send(contextP, transaction); |
| terence zhang | 3:a280069151ac | 775 | } |
| terence zhang | 3:a280069151ac | 776 | |
| terencez | 0:f9d13e09cf11 | 777 | #endif |