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@15:d0f20339c1ad, 2017-05-07 (annotated)
- Committer:
- terencez
- Date:
- Sun May 07 03:00:16 2017 +0000
- Revision:
- 15:d0f20339c1ad
- Parent:
- 3:a280069151ac
- Child:
- 16:31c387e94b6d
Fix the "Get" operation fail issue, tested get information from device.
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 | { |
| terencez | 15:d0f20339c1ad | 185 | printf("dm_handleRequest: %d\n",__LINE__); |
| terence zhang | 3:a280069151ac | 186 | return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 187 | } |
| terencez | 15:d0f20339c1ad | 188 | |
| terencez | 15:d0f20339c1ad | 189 | printf("serverP->status %d, id= %d\n",serverP->status, serverP->shortID); |
| terence zhang | 3:a280069151ac | 190 | |
| terence zhang | 3:a280069151ac | 191 | if (serverP->status != STATE_REGISTERED |
| terence zhang | 3:a280069151ac | 192 | && serverP->status != STATE_REG_UPDATE_NEEDED |
| terence zhang | 3:a280069151ac | 193 | && serverP->status != STATE_REG_FULL_UPDATE_NEEDED |
| terence zhang | 3:a280069151ac | 194 | && serverP->status != STATE_REG_UPDATE_PENDING) |
| terence zhang | 3:a280069151ac | 195 | { |
| terencez | 15:d0f20339c1ad | 196 | printf("dm_handleRequest: %d\n",__LINE__); |
| terence zhang | 3:a280069151ac | 197 | return COAP_IGNORE; |
| terence zhang | 3:a280069151ac | 198 | } |
| terence zhang | 3:a280069151ac | 199 | |
| terence zhang | 3:a280069151ac | 200 | // TODO: check ACL |
| terencez | 0:f9d13e09cf11 | 201 | |
| terencez | 0:f9d13e09cf11 | 202 | switch (message->code) |
| terencez | 0:f9d13e09cf11 | 203 | { |
| terencez | 0:f9d13e09cf11 | 204 | case COAP_GET: |
| terencez | 0:f9d13e09cf11 | 205 | { |
| terence zhang | 3:a280069151ac | 206 | uint8_t * buffer = NULL; |
| terence zhang | 3:a280069151ac | 207 | size_t length = 0; |
| terence zhang | 3:a280069151ac | 208 | int res; |
| terencez | 0:f9d13e09cf11 | 209 | |
| terence zhang | 3:a280069151ac | 210 | if (IS_OPTION(message, COAP_OPTION_OBSERVE)) |
| terencez | 0:f9d13e09cf11 | 211 | { |
| terence zhang | 3:a280069151ac | 212 | lwm2m_data_t * dataP = NULL; |
| terence zhang | 3:a280069151ac | 213 | int size = 0; |
| terencez | 15:d0f20339c1ad | 214 | printf("dm_handleRequest: %d\n",__LINE__); |
| terencez | 15:d0f20339c1ad | 215 | |
| terence zhang | 3:a280069151ac | 216 | result = object_readData(contextP, uriP, &size, &dataP); |
| terence zhang | 3:a280069151ac | 217 | if (COAP_205_CONTENT == result) |
| terencez | 0:f9d13e09cf11 | 218 | { |
| terence zhang | 3:a280069151ac | 219 | result = observe_handleRequest(contextP, uriP, serverP, size, dataP, message, response); |
| terence zhang | 3:a280069151ac | 220 | if (COAP_205_CONTENT == result) |
| terence zhang | 3:a280069151ac | 221 | { |
| terence zhang | 3:a280069151ac | 222 | res = lwm2m_data_serialize(uriP, size, dataP, &format, &buffer); |
| terence zhang | 3:a280069151ac | 223 | if (res < 0) |
| terence zhang | 3:a280069151ac | 224 | { |
| terence zhang | 3:a280069151ac | 225 | result = COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 226 | } |
| terence zhang | 3:a280069151ac | 227 | else |
| terence zhang | 3:a280069151ac | 228 | { |
| terence zhang | 3:a280069151ac | 229 | length = (size_t)res; |
| terence zhang | 3:a280069151ac | 230 | LOG_ARG("Observe Request[/%d/%d/%d]: %.*s\n", uriP->objectId, uriP->instanceId, uriP->resourceId, length, buffer); |
| terence zhang | 3:a280069151ac | 231 | } |
| terence zhang | 3:a280069151ac | 232 | } |
| terence zhang | 3:a280069151ac | 233 | lwm2m_data_free(size, dataP); |
| terencez | 0:f9d13e09cf11 | 234 | } |
| terence zhang | 3:a280069151ac | 235 | } |
| terence zhang | 3:a280069151ac | 236 | else if (IS_OPTION(message, COAP_OPTION_ACCEPT) |
| terence zhang | 3:a280069151ac | 237 | && message->accept_num == 1 |
| terence zhang | 3:a280069151ac | 238 | && message->accept[0] == APPLICATION_LINK_FORMAT) |
| terence zhang | 3:a280069151ac | 239 | { |
| terencez | 15:d0f20339c1ad | 240 | printf("dm_handleRequest: %d\n",__LINE__); |
| terence zhang | 3:a280069151ac | 241 | format = LWM2M_CONTENT_LINK; |
| terence zhang | 3:a280069151ac | 242 | result = object_discover(contextP, uriP, serverP, &buffer, &length); |
| terence zhang | 3:a280069151ac | 243 | } |
| terence zhang | 3:a280069151ac | 244 | else |
| terence zhang | 3:a280069151ac | 245 | { |
| terence zhang | 3:a280069151ac | 246 | if (IS_OPTION(message, COAP_OPTION_ACCEPT)) |
| terencez | 0:f9d13e09cf11 | 247 | { |
| terence zhang | 3:a280069151ac | 248 | format = utils_convertMediaType(message->accept[0]); |
| terencez | 0:f9d13e09cf11 | 249 | } |
| terencez | 15:d0f20339c1ad | 250 | printf("dm_handleRequest: %d\n",__LINE__); |
| terencez | 15:d0f20339c1ad | 251 | |
| terence zhang | 3:a280069151ac | 252 | result = object_read(contextP, uriP, &format, &buffer, &length); |
| terence zhang | 3:a280069151ac | 253 | } |
| terence zhang | 3:a280069151ac | 254 | if (COAP_205_CONTENT == result) |
| terence zhang | 3:a280069151ac | 255 | { |
| terence zhang | 3:a280069151ac | 256 | coap_set_header_content_type(response, format); |
| terence zhang | 3:a280069151ac | 257 | coap_set_payload(response, buffer, length); |
| terence zhang | 3:a280069151ac | 258 | // lwm2m_handle_packet will free buffer |
| terence zhang | 3:a280069151ac | 259 | } |
| terence zhang | 3:a280069151ac | 260 | else |
| terence zhang | 3:a280069151ac | 261 | { |
| terence zhang | 3:a280069151ac | 262 | lwm2m_free(buffer); |
| terencez | 0:f9d13e09cf11 | 263 | } |
| terencez | 0:f9d13e09cf11 | 264 | } |
| terencez | 0:f9d13e09cf11 | 265 | break; |
| terence zhang | 3:a280069151ac | 266 | |
| terencez | 0:f9d13e09cf11 | 267 | case COAP_POST: |
| terencez | 0:f9d13e09cf11 | 268 | { |
| terencez | 0:f9d13e09cf11 | 269 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 270 | { |
| terence zhang | 3:a280069151ac | 271 | result = object_create(contextP, uriP, format, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 272 | if (result == COAP_201_CREATED) |
| terencez | 0:f9d13e09cf11 | 273 | { |
| terencez | 0:f9d13e09cf11 | 274 | //longest uri is /65535/65535 = 12 + 1 (null) chars |
| terencez | 0:f9d13e09cf11 | 275 | char location_path[13] = ""; |
| terencez | 0:f9d13e09cf11 | 276 | //instanceId expected |
| terencez | 0:f9d13e09cf11 | 277 | if ((uriP->flag & LWM2M_URI_FLAG_INSTANCE_ID) == 0) |
| terencez | 0:f9d13e09cf11 | 278 | { |
| terencez | 0:f9d13e09cf11 | 279 | result = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 280 | break; |
| terencez | 0:f9d13e09cf11 | 281 | } |
| terencez | 0:f9d13e09cf11 | 282 | |
| terencez | 0:f9d13e09cf11 | 283 | if (sprintf(location_path, "/%d/%d", uriP->objectId, uriP->instanceId) < 0) |
| terencez | 0:f9d13e09cf11 | 284 | { |
| terencez | 0:f9d13e09cf11 | 285 | result = COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 286 | break; |
| terencez | 0:f9d13e09cf11 | 287 | } |
| terencez | 0:f9d13e09cf11 | 288 | coap_set_header_location_path(response, location_path); |
| terence zhang | 3:a280069151ac | 289 | |
| terence zhang | 3:a280069151ac | 290 | lwm2m_update_registration(contextP, 0, true); |
| terencez | 0:f9d13e09cf11 | 291 | } |
| terencez | 0:f9d13e09cf11 | 292 | } |
| terencez | 0:f9d13e09cf11 | 293 | else if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 294 | { |
| terence zhang | 3:a280069151ac | 295 | result = object_write(contextP, uriP, format, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 296 | } |
| terencez | 0:f9d13e09cf11 | 297 | else |
| terencez | 0:f9d13e09cf11 | 298 | { |
| terencez | 0:f9d13e09cf11 | 299 | result = object_execute(contextP, uriP, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 300 | } |
| terencez | 0:f9d13e09cf11 | 301 | } |
| terencez | 0:f9d13e09cf11 | 302 | break; |
| terence zhang | 3:a280069151ac | 303 | |
| terencez | 0:f9d13e09cf11 | 304 | case COAP_PUT: |
| terencez | 0:f9d13e09cf11 | 305 | { |
| terence zhang | 3:a280069151ac | 306 | if (IS_OPTION(message, COAP_OPTION_URI_QUERY)) |
| terencez | 0:f9d13e09cf11 | 307 | { |
| terence zhang | 3:a280069151ac | 308 | lwm2m_attributes_t attr; |
| terence zhang | 3:a280069151ac | 309 | |
| terence zhang | 3:a280069151ac | 310 | if (0 != prv_readAttributes(message->uri_query, &attr)) |
| terence zhang | 3:a280069151ac | 311 | { |
| terence zhang | 3:a280069151ac | 312 | result = COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 313 | } |
| terence zhang | 3:a280069151ac | 314 | else |
| terence zhang | 3:a280069151ac | 315 | { |
| terence zhang | 3:a280069151ac | 316 | result = observe_setParameters(contextP, uriP, serverP, &attr); |
| terence zhang | 3:a280069151ac | 317 | } |
| terence zhang | 3:a280069151ac | 318 | } |
| terence zhang | 3:a280069151ac | 319 | else if (LWM2M_URI_IS_SET_INSTANCE(uriP)) |
| terence zhang | 3:a280069151ac | 320 | { |
| terence zhang | 3:a280069151ac | 321 | result = object_write(contextP, uriP, format, message->payload, message->payload_len); |
| terencez | 0:f9d13e09cf11 | 322 | } |
| terencez | 0:f9d13e09cf11 | 323 | else |
| terencez | 0:f9d13e09cf11 | 324 | { |
| terence zhang | 3:a280069151ac | 325 | result = COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 326 | } |
| terencez | 0:f9d13e09cf11 | 327 | } |
| terencez | 0:f9d13e09cf11 | 328 | break; |
| terence zhang | 3:a280069151ac | 329 | |
| terencez | 0:f9d13e09cf11 | 330 | case COAP_DELETE: |
| terencez | 0:f9d13e09cf11 | 331 | { |
| terence zhang | 3:a280069151ac | 332 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP) || LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 333 | { |
| terence zhang | 3:a280069151ac | 334 | result = COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 335 | } |
| terencez | 0:f9d13e09cf11 | 336 | else |
| terencez | 0:f9d13e09cf11 | 337 | { |
| terence zhang | 3:a280069151ac | 338 | result = object_delete(contextP, uriP); |
| terence zhang | 3:a280069151ac | 339 | if (result == COAP_202_DELETED) |
| terence zhang | 3:a280069151ac | 340 | { |
| terence zhang | 3:a280069151ac | 341 | lwm2m_update_registration(contextP, 0, true); |
| terence zhang | 3:a280069151ac | 342 | } |
| terencez | 0:f9d13e09cf11 | 343 | } |
| terencez | 0:f9d13e09cf11 | 344 | } |
| terencez | 0:f9d13e09cf11 | 345 | break; |
| terence zhang | 3:a280069151ac | 346 | |
| terencez | 0:f9d13e09cf11 | 347 | default: |
| terence zhang | 3:a280069151ac | 348 | result = COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 349 | break; |
| terencez | 0:f9d13e09cf11 | 350 | } |
| terencez | 0:f9d13e09cf11 | 351 | |
| terencez | 0:f9d13e09cf11 | 352 | return result; |
| terencez | 0:f9d13e09cf11 | 353 | } |
| terence zhang | 3:a280069151ac | 354 | |
| terencez | 0:f9d13e09cf11 | 355 | #endif |
| terencez | 0:f9d13e09cf11 | 356 | |
| terencez | 0:f9d13e09cf11 | 357 | #ifdef LWM2M_SERVER_MODE |
| terencez | 0:f9d13e09cf11 | 358 | |
| terencez | 0:f9d13e09cf11 | 359 | #define ID_AS_STRING_MAX_LEN 8 |
| terencez | 0:f9d13e09cf11 | 360 | |
| terence zhang | 3:a280069151ac | 361 | static void prv_resultCallback(lwm2m_transaction_t * transacP, |
| terencez | 0:f9d13e09cf11 | 362 | void * message) |
| terencez | 0:f9d13e09cf11 | 363 | { |
| terencez | 0:f9d13e09cf11 | 364 | dm_data_t * dataP = (dm_data_t *)transacP->userData; |
| terencez | 0:f9d13e09cf11 | 365 | |
| terencez | 0:f9d13e09cf11 | 366 | if (message == NULL) |
| terencez | 0:f9d13e09cf11 | 367 | { |
| terence zhang | 3:a280069151ac | 368 | dataP->callback(dataP->clientID, |
| terencez | 0:f9d13e09cf11 | 369 | &dataP->uri, |
| terencez | 0:f9d13e09cf11 | 370 | COAP_503_SERVICE_UNAVAILABLE, |
| terence zhang | 3:a280069151ac | 371 | LWM2M_CONTENT_TEXT, NULL, 0, |
| terencez | 0:f9d13e09cf11 | 372 | dataP->userData); |
| terencez | 0:f9d13e09cf11 | 373 | } |
| terencez | 0:f9d13e09cf11 | 374 | else |
| terencez | 0:f9d13e09cf11 | 375 | { |
| terencez | 0:f9d13e09cf11 | 376 | coap_packet_t * packet = (coap_packet_t *)message; |
| terencez | 0:f9d13e09cf11 | 377 | |
| terencez | 0:f9d13e09cf11 | 378 | //if packet is a CREATE response and the instanceId was assigned by the client |
| terencez | 0:f9d13e09cf11 | 379 | if (packet->code == COAP_201_CREATED |
| terencez | 0:f9d13e09cf11 | 380 | && packet->location_path != NULL) |
| terencez | 0:f9d13e09cf11 | 381 | { |
| terencez | 0:f9d13e09cf11 | 382 | char * locationString = NULL; |
| terencez | 0:f9d13e09cf11 | 383 | int result = 0; |
| terencez | 0:f9d13e09cf11 | 384 | lwm2m_uri_t locationUri; |
| terencez | 0:f9d13e09cf11 | 385 | |
| terencez | 0:f9d13e09cf11 | 386 | locationString = coap_get_multi_option_as_string(packet->location_path); |
| terencez | 0:f9d13e09cf11 | 387 | if (locationString == NULL) |
| terencez | 0:f9d13e09cf11 | 388 | { |
| terence zhang | 3:a280069151ac | 389 | LOG("Error: coap_get_multi_option_as_string() failed for Location_path option in prv_resultCallback()"); |
| terencez | 0:f9d13e09cf11 | 390 | return; |
| terencez | 0:f9d13e09cf11 | 391 | } |
| terencez | 0:f9d13e09cf11 | 392 | |
| terencez | 0:f9d13e09cf11 | 393 | result = lwm2m_stringToUri(locationString, strlen(locationString), &locationUri); |
| terencez | 0:f9d13e09cf11 | 394 | if (result == 0) |
| terencez | 0:f9d13e09cf11 | 395 | { |
| terence zhang | 3:a280069151ac | 396 | LOG("Error: lwm2m_stringToUri() failed for Location_path option in prv_resultCallback()"); |
| terencez | 0:f9d13e09cf11 | 397 | lwm2m_free(locationString); |
| terencez | 0:f9d13e09cf11 | 398 | return; |
| terencez | 0:f9d13e09cf11 | 399 | } |
| terencez | 0:f9d13e09cf11 | 400 | |
| terencez | 0:f9d13e09cf11 | 401 | ((dm_data_t*)transacP->userData)->uri.instanceId = locationUri.instanceId; |
| terencez | 0:f9d13e09cf11 | 402 | ((dm_data_t*)transacP->userData)->uri.flag = locationUri.flag; |
| terencez | 0:f9d13e09cf11 | 403 | |
| terencez | 0:f9d13e09cf11 | 404 | lwm2m_free(locationString); |
| terencez | 0:f9d13e09cf11 | 405 | } |
| terencez | 0:f9d13e09cf11 | 406 | |
| terence zhang | 3:a280069151ac | 407 | dataP->callback(dataP->clientID, |
| terencez | 0:f9d13e09cf11 | 408 | &dataP->uri, |
| terencez | 0:f9d13e09cf11 | 409 | packet->code, |
| terence zhang | 3:a280069151ac | 410 | utils_convertMediaType(packet->content_type), |
| terencez | 0:f9d13e09cf11 | 411 | packet->payload, |
| terencez | 0:f9d13e09cf11 | 412 | packet->payload_len, |
| terencez | 0:f9d13e09cf11 | 413 | dataP->userData); |
| terencez | 0:f9d13e09cf11 | 414 | } |
| terencez | 0:f9d13e09cf11 | 415 | lwm2m_free(dataP); |
| terencez | 0:f9d13e09cf11 | 416 | } |
| terencez | 0:f9d13e09cf11 | 417 | |
| terence zhang | 3:a280069151ac | 418 | static int prv_makeOperation(lwm2m_context_t * contextP, |
| terence zhang | 3:a280069151ac | 419 | uint16_t clientID, |
| terence zhang | 3:a280069151ac | 420 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 421 | coap_method_t method, |
| terence zhang | 3:a280069151ac | 422 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 423 | uint8_t * buffer, |
| terence zhang | 3:a280069151ac | 424 | int length, |
| terence zhang | 3:a280069151ac | 425 | lwm2m_result_callback_t callback, |
| terence zhang | 3:a280069151ac | 426 | void * userData) |
| terencez | 0:f9d13e09cf11 | 427 | { |
| terencez | 0:f9d13e09cf11 | 428 | lwm2m_client_t * clientP; |
| terencez | 0:f9d13e09cf11 | 429 | lwm2m_transaction_t * transaction; |
| terencez | 0:f9d13e09cf11 | 430 | dm_data_t * dataP; |
| terencez | 0:f9d13e09cf11 | 431 | |
| terencez | 0:f9d13e09cf11 | 432 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terencez | 0:f9d13e09cf11 | 433 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terencez | 0:f9d13e09cf11 | 434 | |
| terence zhang | 3:a280069151ac | 435 | transaction = transaction_new(clientP->sessionH, method, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); |
| terence zhang | 3:a280069151ac | 436 | if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 437 | |
| terence zhang | 3:a280069151ac | 438 | if (method == COAP_GET) |
| terencez | 0:f9d13e09cf11 | 439 | { |
| terence zhang | 3:a280069151ac | 440 | coap_set_header_accept(transaction->message, format); |
| terence zhang | 3:a280069151ac | 441 | } |
| terence zhang | 3:a280069151ac | 442 | else if (buffer != NULL) |
| terence zhang | 3:a280069151ac | 443 | { |
| terence zhang | 3:a280069151ac | 444 | coap_set_header_content_type(transaction->message, format); |
| terencez | 0:f9d13e09cf11 | 445 | // TODO: Take care of fragmentation |
| terencez | 0:f9d13e09cf11 | 446 | coap_set_payload(transaction->message, buffer, length); |
| terencez | 0:f9d13e09cf11 | 447 | } |
| terencez | 0:f9d13e09cf11 | 448 | |
| terencez | 0:f9d13e09cf11 | 449 | if (callback != NULL) |
| terencez | 0:f9d13e09cf11 | 450 | { |
| terencez | 0:f9d13e09cf11 | 451 | dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); |
| terencez | 0:f9d13e09cf11 | 452 | if (dataP == NULL) |
| terencez | 0:f9d13e09cf11 | 453 | { |
| terencez | 0:f9d13e09cf11 | 454 | transaction_free(transaction); |
| terencez | 0:f9d13e09cf11 | 455 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terencez | 0:f9d13e09cf11 | 456 | } |
| terencez | 0:f9d13e09cf11 | 457 | memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); |
| terence zhang | 3:a280069151ac | 458 | dataP->clientID = clientP->internalID; |
| terencez | 0:f9d13e09cf11 | 459 | dataP->callback = callback; |
| terencez | 0:f9d13e09cf11 | 460 | dataP->userData = userData; |
| terencez | 0:f9d13e09cf11 | 461 | |
| terence zhang | 3:a280069151ac | 462 | transaction->callback = prv_resultCallback; |
| terencez | 0:f9d13e09cf11 | 463 | transaction->userData = (void *)dataP; |
| terencez | 0:f9d13e09cf11 | 464 | } |
| terencez | 0:f9d13e09cf11 | 465 | |
| terencez | 0:f9d13e09cf11 | 466 | contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); |
| terencez | 0:f9d13e09cf11 | 467 | |
| terencez | 0:f9d13e09cf11 | 468 | return transaction_send(contextP, transaction); |
| terencez | 0:f9d13e09cf11 | 469 | } |
| terencez | 0:f9d13e09cf11 | 470 | |
| terencez | 0:f9d13e09cf11 | 471 | int lwm2m_dm_read(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 472 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 473 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 474 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 475 | void * userData) |
| terencez | 0:f9d13e09cf11 | 476 | { |
| terence zhang | 3:a280069151ac | 477 | lwm2m_client_t * clientP; |
| terence zhang | 3:a280069151ac | 478 | lwm2m_media_type_t format; |
| terence zhang | 3:a280069151ac | 479 | |
| terence zhang | 3:a280069151ac | 480 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 481 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 482 | |
| terence zhang | 3:a280069151ac | 483 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terence zhang | 3:a280069151ac | 484 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 485 | |
| terence zhang | 3:a280069151ac | 486 | if (clientP->supportJSON == true) |
| terence zhang | 3:a280069151ac | 487 | { |
| terence zhang | 3:a280069151ac | 488 | format = LWM2M_CONTENT_JSON; |
| terence zhang | 3:a280069151ac | 489 | } |
| terence zhang | 3:a280069151ac | 490 | else |
| terence zhang | 3:a280069151ac | 491 | { |
| terence zhang | 3:a280069151ac | 492 | format = LWM2M_CONTENT_TLV; |
| terence zhang | 3:a280069151ac | 493 | } |
| terence zhang | 3:a280069151ac | 494 | |
| terence zhang | 3:a280069151ac | 495 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 496 | COAP_GET, |
| terence zhang | 3:a280069151ac | 497 | format, |
| terence zhang | 3:a280069151ac | 498 | NULL, 0, |
| terence zhang | 3:a280069151ac | 499 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 500 | } |
| terencez | 0:f9d13e09cf11 | 501 | |
| terencez | 0:f9d13e09cf11 | 502 | int lwm2m_dm_write(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 503 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 504 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 505 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 506 | uint8_t * buffer, |
| terencez | 0:f9d13e09cf11 | 507 | int length, |
| terencez | 0:f9d13e09cf11 | 508 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 509 | void * userData) |
| terencez | 0:f9d13e09cf11 | 510 | { |
| terence zhang | 3:a280069151ac | 511 | LOG_ARG("clientID: %d, format: %s, length: %d", clientID, STR_MEDIA_TYPE(format), length); |
| terence zhang | 3:a280069151ac | 512 | LOG_URI(uriP); |
| terencez | 0:f9d13e09cf11 | 513 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP) |
| terencez | 0:f9d13e09cf11 | 514 | || length == 0) |
| terencez | 0:f9d13e09cf11 | 515 | { |
| terencez | 0:f9d13e09cf11 | 516 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 517 | } |
| terencez | 0:f9d13e09cf11 | 518 | |
| terencez | 0:f9d13e09cf11 | 519 | if (LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 520 | { |
| terence zhang | 3:a280069151ac | 521 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 522 | COAP_PUT, |
| terence zhang | 3:a280069151ac | 523 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 524 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 525 | } |
| terencez | 0:f9d13e09cf11 | 526 | else |
| terencez | 0:f9d13e09cf11 | 527 | { |
| terence zhang | 3:a280069151ac | 528 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 529 | COAP_POST, |
| terence zhang | 3:a280069151ac | 530 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 531 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 532 | } |
| terencez | 0:f9d13e09cf11 | 533 | } |
| terencez | 0:f9d13e09cf11 | 534 | |
| terencez | 0:f9d13e09cf11 | 535 | int lwm2m_dm_execute(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 536 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 537 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 538 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 539 | uint8_t * buffer, |
| terencez | 0:f9d13e09cf11 | 540 | int length, |
| terencez | 0:f9d13e09cf11 | 541 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 542 | void * userData) |
| terencez | 0:f9d13e09cf11 | 543 | { |
| terence zhang | 3:a280069151ac | 544 | LOG_ARG("clientID: %d, format: %s, length: %d", clientID, STR_MEDIA_TYPE(format), length); |
| terence zhang | 3:a280069151ac | 545 | LOG_URI(uriP); |
| terencez | 0:f9d13e09cf11 | 546 | if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 547 | { |
| terencez | 0:f9d13e09cf11 | 548 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 549 | } |
| terencez | 0:f9d13e09cf11 | 550 | |
| terence zhang | 3:a280069151ac | 551 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 552 | COAP_POST, |
| terence zhang | 3:a280069151ac | 553 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 554 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 555 | } |
| terencez | 0:f9d13e09cf11 | 556 | |
| terencez | 0:f9d13e09cf11 | 557 | int lwm2m_dm_create(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 558 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 559 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 560 | lwm2m_media_type_t format, |
| terence zhang | 3:a280069151ac | 561 | uint8_t * buffer, |
| terencez | 0:f9d13e09cf11 | 562 | int length, |
| terencez | 0:f9d13e09cf11 | 563 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 564 | void * userData) |
| terencez | 0:f9d13e09cf11 | 565 | { |
| terence zhang | 3:a280069151ac | 566 | LOG_ARG("clientID: %d, format: %s, length: %d", clientID, STR_MEDIA_TYPE(format), length); |
| terence zhang | 3:a280069151ac | 567 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 568 | |
| terence zhang | 3:a280069151ac | 569 | if (LWM2M_URI_IS_SET_INSTANCE(uriP) |
| terencez | 0:f9d13e09cf11 | 570 | || length == 0) |
| terencez | 0:f9d13e09cf11 | 571 | { |
| terencez | 0:f9d13e09cf11 | 572 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 573 | } |
| terencez | 0:f9d13e09cf11 | 574 | |
| terence zhang | 3:a280069151ac | 575 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 576 | COAP_POST, |
| terence zhang | 3:a280069151ac | 577 | format, buffer, length, |
| terencez | 0:f9d13e09cf11 | 578 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 579 | } |
| terencez | 0:f9d13e09cf11 | 580 | |
| terencez | 0:f9d13e09cf11 | 581 | int lwm2m_dm_delete(lwm2m_context_t * contextP, |
| terencez | 0:f9d13e09cf11 | 582 | uint16_t clientID, |
| terencez | 0:f9d13e09cf11 | 583 | lwm2m_uri_t * uriP, |
| terencez | 0:f9d13e09cf11 | 584 | lwm2m_result_callback_t callback, |
| terencez | 0:f9d13e09cf11 | 585 | void * userData) |
| terencez | 0:f9d13e09cf11 | 586 | { |
| terence zhang | 3:a280069151ac | 587 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 588 | LOG_URI(uriP); |
| terencez | 0:f9d13e09cf11 | 589 | if (!LWM2M_URI_IS_SET_INSTANCE(uriP) |
| terencez | 0:f9d13e09cf11 | 590 | || LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 0:f9d13e09cf11 | 591 | { |
| terencez | 0:f9d13e09cf11 | 592 | return COAP_400_BAD_REQUEST; |
| terencez | 0:f9d13e09cf11 | 593 | } |
| terencez | 0:f9d13e09cf11 | 594 | |
| terence zhang | 3:a280069151ac | 595 | return prv_makeOperation(contextP, clientID, uriP, |
| terence zhang | 3:a280069151ac | 596 | COAP_DELETE, |
| terence zhang | 3:a280069151ac | 597 | LWM2M_CONTENT_TEXT, NULL, 0, |
| terencez | 0:f9d13e09cf11 | 598 | callback, userData); |
| terencez | 0:f9d13e09cf11 | 599 | } |
| terence zhang | 3:a280069151ac | 600 | |
| terence zhang | 3:a280069151ac | 601 | int lwm2m_dm_write_attributes(lwm2m_context_t * contextP, |
| terence zhang | 3:a280069151ac | 602 | uint16_t clientID, |
| terence zhang | 3:a280069151ac | 603 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 604 | lwm2m_attributes_t * attrP, |
| terence zhang | 3:a280069151ac | 605 | lwm2m_result_callback_t callback, |
| terence zhang | 3:a280069151ac | 606 | void * userData) |
| terence zhang | 3:a280069151ac | 607 | { |
| terence zhang | 3:a280069151ac | 608 | #define _PRV_BUFFER_SIZE 32 |
| terence zhang | 3:a280069151ac | 609 | lwm2m_client_t * clientP; |
| terence zhang | 3:a280069151ac | 610 | lwm2m_transaction_t * transaction; |
| terence zhang | 3:a280069151ac | 611 | coap_packet_t * coap_pkt; |
| terence zhang | 3:a280069151ac | 612 | uint8_t buffer[_PRV_BUFFER_SIZE]; |
| terence zhang | 3:a280069151ac | 613 | size_t length; |
| terence zhang | 3:a280069151ac | 614 | |
| terence zhang | 3:a280069151ac | 615 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 616 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 617 | if (attrP == NULL) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 618 | |
| terence zhang | 3:a280069151ac | 619 | if (0 != (attrP->toSet & attrP->toClear)) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 620 | if (0 != (attrP->toSet & ATTR_FLAG_NUMERIC) && !LWM2M_URI_IS_SET_RESOURCE(uriP)) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 621 | if (ATTR_FLAG_NUMERIC == (attrP->toSet & ATTR_FLAG_NUMERIC) |
| terence zhang | 3:a280069151ac | 622 | && (attrP->lessThan + 2 * attrP->step >= attrP->greaterThan)) return COAP_400_BAD_REQUEST; |
| terence zhang | 3:a280069151ac | 623 | |
| terence zhang | 3:a280069151ac | 624 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terence zhang | 3:a280069151ac | 625 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 626 | |
| terence zhang | 3:a280069151ac | 627 | transaction = transaction_new(clientP->sessionH, COAP_PUT, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); |
| terence zhang | 3:a280069151ac | 628 | if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 629 | |
| terence zhang | 3:a280069151ac | 630 | if (callback != NULL) |
| terence zhang | 3:a280069151ac | 631 | { |
| terence zhang | 3:a280069151ac | 632 | dm_data_t * dataP; |
| terence zhang | 3:a280069151ac | 633 | |
| terence zhang | 3:a280069151ac | 634 | dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); |
| terence zhang | 3:a280069151ac | 635 | if (dataP == NULL) |
| terence zhang | 3:a280069151ac | 636 | { |
| terence zhang | 3:a280069151ac | 637 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 638 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 639 | } |
| terence zhang | 3:a280069151ac | 640 | memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); |
| terence zhang | 3:a280069151ac | 641 | dataP->clientID = clientP->internalID; |
| terence zhang | 3:a280069151ac | 642 | dataP->callback = callback; |
| terence zhang | 3:a280069151ac | 643 | dataP->userData = userData; |
| terence zhang | 3:a280069151ac | 644 | |
| terence zhang | 3:a280069151ac | 645 | transaction->callback = prv_resultCallback; |
| terence zhang | 3:a280069151ac | 646 | transaction->userData = (void *)dataP; |
| terence zhang | 3:a280069151ac | 647 | } |
| terence zhang | 3:a280069151ac | 648 | |
| terence zhang | 3:a280069151ac | 649 | coap_pkt = (coap_packet_t *)transaction->message; |
| terence zhang | 3:a280069151ac | 650 | free_multi_option(coap_pkt->uri_query); |
| terence zhang | 3:a280069151ac | 651 | if (attrP->toSet & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terence zhang | 3:a280069151ac | 652 | { |
| terence zhang | 3:a280069151ac | 653 | memcpy(buffer, ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 654 | length = utils_intToText(attrP->minPeriod, buffer + ATTR_MIN_PERIOD_LEN, _PRV_BUFFER_SIZE - ATTR_MIN_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 655 | if (length == 0) |
| terence zhang | 3:a280069151ac | 656 | { |
| terence zhang | 3:a280069151ac | 657 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 658 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 659 | } |
| terence zhang | 3:a280069151ac | 660 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_MIN_PERIOD_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 661 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 662 | } |
| terence zhang | 3:a280069151ac | 663 | if (attrP->toSet & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terence zhang | 3:a280069151ac | 664 | { |
| terence zhang | 3:a280069151ac | 665 | memcpy(buffer, ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 666 | length = utils_intToText(attrP->maxPeriod, buffer + ATTR_MAX_PERIOD_LEN, _PRV_BUFFER_SIZE - ATTR_MAX_PERIOD_LEN); |
| terence zhang | 3:a280069151ac | 667 | if (length == 0) |
| terence zhang | 3:a280069151ac | 668 | { |
| terence zhang | 3:a280069151ac | 669 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 670 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 671 | } |
| terence zhang | 3:a280069151ac | 672 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_MAX_PERIOD_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 673 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 674 | } |
| terence zhang | 3:a280069151ac | 675 | if (attrP->toSet & LWM2M_ATTR_FLAG_GREATER_THAN) |
| terence zhang | 3:a280069151ac | 676 | { |
| terence zhang | 3:a280069151ac | 677 | memcpy(buffer, ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN); |
| terence zhang | 3:a280069151ac | 678 | length = utils_floatToText(attrP->greaterThan, buffer + ATTR_GREATER_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_GREATER_THAN_LEN); |
| terence zhang | 3:a280069151ac | 679 | if (length == 0) |
| terence zhang | 3:a280069151ac | 680 | { |
| terence zhang | 3:a280069151ac | 681 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 682 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 683 | } |
| terence zhang | 3:a280069151ac | 684 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_GREATER_THAN_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 685 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 686 | } |
| terence zhang | 3:a280069151ac | 687 | if (attrP->toSet & LWM2M_ATTR_FLAG_LESS_THAN) |
| terence zhang | 3:a280069151ac | 688 | { |
| terence zhang | 3:a280069151ac | 689 | memcpy(buffer, ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN); |
| terence zhang | 3:a280069151ac | 690 | length = utils_floatToText(attrP->lessThan, buffer + ATTR_LESS_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_LESS_THAN_LEN); |
| terence zhang | 3:a280069151ac | 691 | if (length == 0) |
| terence zhang | 3:a280069151ac | 692 | { |
| terence zhang | 3:a280069151ac | 693 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 694 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 695 | } |
| terence zhang | 3:a280069151ac | 696 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_LESS_THAN_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 697 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 698 | } |
| terence zhang | 3:a280069151ac | 699 | if (attrP->toSet & LWM2M_ATTR_FLAG_STEP) |
| terence zhang | 3:a280069151ac | 700 | { |
| terence zhang | 3:a280069151ac | 701 | memcpy(buffer, ATTR_STEP_STR, ATTR_STEP_LEN); |
| terence zhang | 3:a280069151ac | 702 | length = utils_floatToText(attrP->step, buffer + ATTR_STEP_LEN, _PRV_BUFFER_SIZE - ATTR_STEP_LEN); |
| terence zhang | 3:a280069151ac | 703 | if (length == 0) |
| terence zhang | 3:a280069151ac | 704 | { |
| terence zhang | 3:a280069151ac | 705 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 706 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 707 | } |
| terence zhang | 3:a280069151ac | 708 | coap_add_multi_option(&(coap_pkt->uri_query), buffer, ATTR_STEP_LEN + length, 0); |
| terence zhang | 3:a280069151ac | 709 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 710 | } |
| terence zhang | 3:a280069151ac | 711 | if (attrP->toClear & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terence zhang | 3:a280069151ac | 712 | { |
| terence zhang | 3:a280069151ac | 713 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN -1, 0); |
| terence zhang | 3:a280069151ac | 714 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 715 | } |
| terence zhang | 3:a280069151ac | 716 | if (attrP->toClear & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terence zhang | 3:a280069151ac | 717 | { |
| terence zhang | 3:a280069151ac | 718 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 719 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 720 | } |
| terence zhang | 3:a280069151ac | 721 | if (attrP->toClear & LWM2M_ATTR_FLAG_GREATER_THAN) |
| terence zhang | 3:a280069151ac | 722 | { |
| terence zhang | 3:a280069151ac | 723 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 724 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 725 | } |
| terence zhang | 3:a280069151ac | 726 | if (attrP->toClear & LWM2M_ATTR_FLAG_LESS_THAN) |
| terence zhang | 3:a280069151ac | 727 | { |
| terence zhang | 3:a280069151ac | 728 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 729 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 730 | } |
| terence zhang | 3:a280069151ac | 731 | if (attrP->toClear & LWM2M_ATTR_FLAG_STEP) |
| terence zhang | 3:a280069151ac | 732 | { |
| terence zhang | 3:a280069151ac | 733 | coap_add_multi_option(&(coap_pkt->uri_query), ATTR_STEP_STR, ATTR_STEP_LEN - 1, 0); |
| terence zhang | 3:a280069151ac | 734 | SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); |
| terence zhang | 3:a280069151ac | 735 | } |
| terence zhang | 3:a280069151ac | 736 | |
| terence zhang | 3:a280069151ac | 737 | contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); |
| terence zhang | 3:a280069151ac | 738 | |
| terence zhang | 3:a280069151ac | 739 | return transaction_send(contextP, transaction); |
| terence zhang | 3:a280069151ac | 740 | } |
| terence zhang | 3:a280069151ac | 741 | |
| terence zhang | 3:a280069151ac | 742 | int lwm2m_dm_discover(lwm2m_context_t * contextP, |
| terence zhang | 3:a280069151ac | 743 | uint16_t clientID, |
| terence zhang | 3:a280069151ac | 744 | lwm2m_uri_t * uriP, |
| terence zhang | 3:a280069151ac | 745 | lwm2m_result_callback_t callback, |
| terence zhang | 3:a280069151ac | 746 | void * userData) |
| terence zhang | 3:a280069151ac | 747 | { |
| terence zhang | 3:a280069151ac | 748 | lwm2m_client_t * clientP; |
| terence zhang | 3:a280069151ac | 749 | lwm2m_transaction_t * transaction; |
| terence zhang | 3:a280069151ac | 750 | dm_data_t * dataP; |
| terence zhang | 3:a280069151ac | 751 | |
| terence zhang | 3:a280069151ac | 752 | LOG_ARG("clientID: %d", clientID); |
| terence zhang | 3:a280069151ac | 753 | LOG_URI(uriP); |
| terence zhang | 3:a280069151ac | 754 | clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); |
| terence zhang | 3:a280069151ac | 755 | if (clientP == NULL) return COAP_404_NOT_FOUND; |
| terence zhang | 3:a280069151ac | 756 | |
| terence zhang | 3:a280069151ac | 757 | transaction = transaction_new(clientP->sessionH, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); |
| terence zhang | 3:a280069151ac | 758 | if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 759 | |
| terence zhang | 3:a280069151ac | 760 | coap_set_header_accept(transaction->message, LWM2M_CONTENT_LINK); |
| terence zhang | 3:a280069151ac | 761 | |
| terence zhang | 3:a280069151ac | 762 | if (callback != NULL) |
| terence zhang | 3:a280069151ac | 763 | { |
| terence zhang | 3:a280069151ac | 764 | dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); |
| terence zhang | 3:a280069151ac | 765 | if (dataP == NULL) |
| terence zhang | 3:a280069151ac | 766 | { |
| terence zhang | 3:a280069151ac | 767 | transaction_free(transaction); |
| terence zhang | 3:a280069151ac | 768 | return COAP_500_INTERNAL_SERVER_ERROR; |
| terence zhang | 3:a280069151ac | 769 | } |
| terence zhang | 3:a280069151ac | 770 | memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); |
| terence zhang | 3:a280069151ac | 771 | dataP->clientID = clientP->internalID; |
| terence zhang | 3:a280069151ac | 772 | dataP->callback = callback; |
| terence zhang | 3:a280069151ac | 773 | dataP->userData = userData; |
| terence zhang | 3:a280069151ac | 774 | |
| terence zhang | 3:a280069151ac | 775 | transaction->callback = prv_resultCallback; |
| terence zhang | 3:a280069151ac | 776 | transaction->userData = (void *)dataP; |
| terence zhang | 3:a280069151ac | 777 | } |
| terence zhang | 3:a280069151ac | 778 | |
| terence zhang | 3:a280069151ac | 779 | contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); |
| terence zhang | 3:a280069151ac | 780 | |
| terence zhang | 3:a280069151ac | 781 | return transaction_send(contextP, transaction); |
| terence zhang | 3:a280069151ac | 782 | } |
| terence zhang | 3:a280069151ac | 783 | |
| terencez | 0:f9d13e09cf11 | 784 | #endif |