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/discover.c@14:ec9e195830ff, 2017-05-06 (annotated)
- Committer:
- terencez
- Date:
- Sat May 06 11:21:27 2017 +0000
- Revision:
- 14:ec9e195830ff
First compile version, registration success but could not handle request.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| terencez | 14:ec9e195830ff | 1 | /******************************************************************************* |
| terencez | 14:ec9e195830ff | 2 | * |
| terencez | 14:ec9e195830ff | 3 | * Copyright (c) 2015 Intel Corporation and others. |
| terencez | 14:ec9e195830ff | 4 | * All rights reserved. This program and the accompanying materials |
| terencez | 14:ec9e195830ff | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| terencez | 14:ec9e195830ff | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
| terencez | 14:ec9e195830ff | 7 | * |
| terencez | 14:ec9e195830ff | 8 | * The Eclipse Public License is available at |
| terencez | 14:ec9e195830ff | 9 | * http://www.eclipse.org/legal/epl-v10.html |
| terencez | 14:ec9e195830ff | 10 | * The Eclipse Distribution License is available at |
| terencez | 14:ec9e195830ff | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
| terencez | 14:ec9e195830ff | 12 | * |
| terencez | 14:ec9e195830ff | 13 | * Contributors: |
| terencez | 14:ec9e195830ff | 14 | * David Navarro, Intel Corporation - initial API and implementation |
| terencez | 14:ec9e195830ff | 15 | * |
| terencez | 14:ec9e195830ff | 16 | *******************************************************************************/ |
| terencez | 14:ec9e195830ff | 17 | |
| terencez | 14:ec9e195830ff | 18 | |
| terencez | 14:ec9e195830ff | 19 | #include "internals.h" |
| terencez | 14:ec9e195830ff | 20 | |
| terencez | 14:ec9e195830ff | 21 | #define PRV_LINK_BUFFER_SIZE 1024 |
| terencez | 14:ec9e195830ff | 22 | |
| terencez | 14:ec9e195830ff | 23 | |
| terencez | 14:ec9e195830ff | 24 | #define PRV_CONCAT_STR(buf, len, index, str, str_len) \ |
| terencez | 14:ec9e195830ff | 25 | { \ |
| terencez | 14:ec9e195830ff | 26 | if ((len)-(index) < (str_len)) return -1; \ |
| terencez | 14:ec9e195830ff | 27 | memcpy((buf)+(index), (str), (str_len)); \ |
| terencez | 14:ec9e195830ff | 28 | (index) += (str_len); \ |
| terencez | 14:ec9e195830ff | 29 | } |
| terencez | 14:ec9e195830ff | 30 | |
| terencez | 14:ec9e195830ff | 31 | |
| terencez | 14:ec9e195830ff | 32 | #ifdef LWM2M_CLIENT_MODE |
| terencez | 14:ec9e195830ff | 33 | |
| terencez | 14:ec9e195830ff | 34 | static lwm2m_attributes_t * prv_findAttributes(lwm2m_context_t * contextP, |
| terencez | 14:ec9e195830ff | 35 | lwm2m_uri_t * uriP, |
| terencez | 14:ec9e195830ff | 36 | lwm2m_server_t * serverP) |
| terencez | 14:ec9e195830ff | 37 | { |
| terencez | 14:ec9e195830ff | 38 | lwm2m_observed_t * observedP; |
| terencez | 14:ec9e195830ff | 39 | lwm2m_watcher_t * watcherP; |
| terencez | 14:ec9e195830ff | 40 | lwm2m_attributes_t * paramP; |
| terencez | 14:ec9e195830ff | 41 | |
| terencez | 14:ec9e195830ff | 42 | paramP = NULL; |
| terencez | 14:ec9e195830ff | 43 | |
| terencez | 14:ec9e195830ff | 44 | if (contextP == NULL) return NULL; |
| terencez | 14:ec9e195830ff | 45 | if (serverP == NULL) return NULL; |
| terencez | 14:ec9e195830ff | 46 | |
| terencez | 14:ec9e195830ff | 47 | observedP = observe_findByUri(contextP, uriP); |
| terencez | 14:ec9e195830ff | 48 | if (observedP == NULL || observedP->watcherList == NULL) return NULL; |
| terencez | 14:ec9e195830ff | 49 | |
| terencez | 14:ec9e195830ff | 50 | for (watcherP = observedP->watcherList; watcherP != NULL; watcherP = watcherP->next) |
| terencez | 14:ec9e195830ff | 51 | { |
| terencez | 14:ec9e195830ff | 52 | if (watcherP->server == serverP) |
| terencez | 14:ec9e195830ff | 53 | { |
| terencez | 14:ec9e195830ff | 54 | paramP = watcherP->parameters; |
| terencez | 14:ec9e195830ff | 55 | } |
| terencez | 14:ec9e195830ff | 56 | } |
| terencez | 14:ec9e195830ff | 57 | |
| terencez | 14:ec9e195830ff | 58 | return paramP; |
| terencez | 14:ec9e195830ff | 59 | } |
| terencez | 14:ec9e195830ff | 60 | |
| terencez | 14:ec9e195830ff | 61 | static int prv_serializeAttributes(lwm2m_context_t * contextP, |
| terencez | 14:ec9e195830ff | 62 | lwm2m_uri_t * uriP, |
| terencez | 14:ec9e195830ff | 63 | lwm2m_server_t * serverP, |
| terencez | 14:ec9e195830ff | 64 | lwm2m_attributes_t * objectParamP, |
| terencez | 14:ec9e195830ff | 65 | uint8_t * buffer, |
| terencez | 14:ec9e195830ff | 66 | size_t uriLen, |
| terencez | 14:ec9e195830ff | 67 | size_t bufferLen) |
| terencez | 14:ec9e195830ff | 68 | { |
| terencez | 14:ec9e195830ff | 69 | int head; |
| terencez | 14:ec9e195830ff | 70 | int res; |
| terencez | 14:ec9e195830ff | 71 | lwm2m_attributes_t * paramP; |
| terencez | 14:ec9e195830ff | 72 | |
| terencez | 14:ec9e195830ff | 73 | head = 0; |
| terencez | 14:ec9e195830ff | 74 | |
| terencez | 14:ec9e195830ff | 75 | paramP = prv_findAttributes(contextP, uriP, serverP); |
| terencez | 14:ec9e195830ff | 76 | if (paramP == NULL) paramP = objectParamP; |
| terencez | 14:ec9e195830ff | 77 | |
| terencez | 14:ec9e195830ff | 78 | if (paramP != NULL) |
| terencez | 14:ec9e195830ff | 79 | { |
| terencez | 14:ec9e195830ff | 80 | head = uriLen; |
| terencez | 14:ec9e195830ff | 81 | |
| terencez | 14:ec9e195830ff | 82 | if (paramP->toSet & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terencez | 14:ec9e195830ff | 83 | { |
| terencez | 14:ec9e195830ff | 84 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 85 | PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN); |
| terencez | 14:ec9e195830ff | 86 | |
| terencez | 14:ec9e195830ff | 87 | res = utils_intToText(paramP->minPeriod, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 88 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 89 | head += res; |
| terencez | 14:ec9e195830ff | 90 | } |
| terencez | 14:ec9e195830ff | 91 | else if (objectParamP->toSet & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terencez | 14:ec9e195830ff | 92 | { |
| terencez | 14:ec9e195830ff | 93 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 94 | PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_MIN_PERIOD_STR, ATTR_MIN_PERIOD_LEN); |
| terencez | 14:ec9e195830ff | 95 | |
| terencez | 14:ec9e195830ff | 96 | res = utils_intToText(objectParamP->minPeriod, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 97 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 98 | head += res; |
| terencez | 14:ec9e195830ff | 99 | } |
| terencez | 14:ec9e195830ff | 100 | |
| terencez | 14:ec9e195830ff | 101 | if (paramP->toSet & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terencez | 14:ec9e195830ff | 102 | { |
| terencez | 14:ec9e195830ff | 103 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 104 | PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN); |
| terencez | 14:ec9e195830ff | 105 | |
| terencez | 14:ec9e195830ff | 106 | res = utils_intToText(paramP->maxPeriod, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 107 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 108 | head += res; |
| terencez | 14:ec9e195830ff | 109 | } |
| terencez | 14:ec9e195830ff | 110 | else if (objectParamP->toSet & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terencez | 14:ec9e195830ff | 111 | { |
| terencez | 14:ec9e195830ff | 112 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 113 | PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_MAX_PERIOD_STR, ATTR_MAX_PERIOD_LEN); |
| terencez | 14:ec9e195830ff | 114 | |
| terencez | 14:ec9e195830ff | 115 | res = utils_intToText(objectParamP->maxPeriod, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 116 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 117 | head += res; |
| terencez | 14:ec9e195830ff | 118 | } |
| terencez | 14:ec9e195830ff | 119 | |
| terencez | 14:ec9e195830ff | 120 | if (paramP->toSet & LWM2M_ATTR_FLAG_GREATER_THAN) |
| terencez | 14:ec9e195830ff | 121 | { |
| terencez | 14:ec9e195830ff | 122 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 123 | PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN); |
| terencez | 14:ec9e195830ff | 124 | |
| terencez | 14:ec9e195830ff | 125 | res = utils_floatToText(paramP->greaterThan, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 126 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 127 | head += res; |
| terencez | 14:ec9e195830ff | 128 | } |
| terencez | 14:ec9e195830ff | 129 | if (paramP->toSet & LWM2M_ATTR_FLAG_LESS_THAN) |
| terencez | 14:ec9e195830ff | 130 | { |
| terencez | 14:ec9e195830ff | 131 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 132 | PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN); |
| terencez | 14:ec9e195830ff | 133 | |
| terencez | 14:ec9e195830ff | 134 | res = utils_floatToText(paramP->lessThan, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 135 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 136 | head += res; |
| terencez | 14:ec9e195830ff | 137 | } |
| terencez | 14:ec9e195830ff | 138 | if (paramP->toSet & LWM2M_ATTR_FLAG_STEP) |
| terencez | 14:ec9e195830ff | 139 | { |
| terencez | 14:ec9e195830ff | 140 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 141 | PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_STEP_STR, ATTR_STEP_LEN); |
| terencez | 14:ec9e195830ff | 142 | |
| terencez | 14:ec9e195830ff | 143 | res = utils_floatToText(paramP->step, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 144 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 145 | head += res; |
| terencez | 14:ec9e195830ff | 146 | } |
| terencez | 14:ec9e195830ff | 147 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ITEM_ATTR_END, LINK_ITEM_ATTR_END_SIZE); |
| terencez | 14:ec9e195830ff | 148 | } |
| terencez | 14:ec9e195830ff | 149 | |
| terencez | 14:ec9e195830ff | 150 | if (head > 0) head -= uriLen + 1; |
| terencez | 14:ec9e195830ff | 151 | |
| terencez | 14:ec9e195830ff | 152 | return head; |
| terencez | 14:ec9e195830ff | 153 | } |
| terencez | 14:ec9e195830ff | 154 | |
| terencez | 14:ec9e195830ff | 155 | static int prv_serializeLinkData(lwm2m_context_t * contextP, |
| terencez | 14:ec9e195830ff | 156 | lwm2m_data_t * tlvP, |
| terencez | 14:ec9e195830ff | 157 | lwm2m_server_t * serverP, |
| terencez | 14:ec9e195830ff | 158 | lwm2m_attributes_t * objectParamP, |
| terencez | 14:ec9e195830ff | 159 | lwm2m_uri_t * parentUriP, |
| terencez | 14:ec9e195830ff | 160 | uint8_t * parentUriStr, |
| terencez | 14:ec9e195830ff | 161 | size_t parentUriLen, |
| terencez | 14:ec9e195830ff | 162 | uint8_t * buffer, |
| terencez | 14:ec9e195830ff | 163 | size_t bufferLen) |
| terencez | 14:ec9e195830ff | 164 | { |
| terencez | 14:ec9e195830ff | 165 | int head; |
| terencez | 14:ec9e195830ff | 166 | int res; |
| terencez | 14:ec9e195830ff | 167 | lwm2m_uri_t uri; |
| terencez | 14:ec9e195830ff | 168 | |
| terencez | 14:ec9e195830ff | 169 | head = 0; |
| terencez | 14:ec9e195830ff | 170 | |
| terencez | 14:ec9e195830ff | 171 | switch (tlvP->type) |
| terencez | 14:ec9e195830ff | 172 | { |
| terencez | 14:ec9e195830ff | 173 | case LWM2M_TYPE_UNDEFINED: |
| terencez | 14:ec9e195830ff | 174 | case LWM2M_TYPE_STRING: |
| terencez | 14:ec9e195830ff | 175 | case LWM2M_TYPE_OPAQUE: |
| terencez | 14:ec9e195830ff | 176 | case LWM2M_TYPE_INTEGER: |
| terencez | 14:ec9e195830ff | 177 | case LWM2M_TYPE_FLOAT: |
| terencez | 14:ec9e195830ff | 178 | case LWM2M_TYPE_BOOLEAN: |
| terencez | 14:ec9e195830ff | 179 | case LWM2M_TYPE_OBJECT_LINK: |
| terencez | 14:ec9e195830ff | 180 | case LWM2M_TYPE_MULTIPLE_RESOURCE: |
| terencez | 14:ec9e195830ff | 181 | if (bufferLen < LINK_ITEM_START_SIZE) return -1; |
| terencez | 14:ec9e195830ff | 182 | memcpy(buffer + head, LINK_ITEM_START, LINK_ITEM_START_SIZE); |
| terencez | 14:ec9e195830ff | 183 | head = LINK_ITEM_START_SIZE; |
| terencez | 14:ec9e195830ff | 184 | |
| terencez | 14:ec9e195830ff | 185 | if (parentUriLen > 0) |
| terencez | 14:ec9e195830ff | 186 | { |
| terencez | 14:ec9e195830ff | 187 | if (bufferLen - head < parentUriLen) return -1; |
| terencez | 14:ec9e195830ff | 188 | memcpy(buffer + head, parentUriStr, parentUriLen); |
| terencez | 14:ec9e195830ff | 189 | head += parentUriLen; |
| terencez | 14:ec9e195830ff | 190 | } |
| terencez | 14:ec9e195830ff | 191 | |
| terencez | 14:ec9e195830ff | 192 | if (bufferLen - head < LINK_URI_SEPARATOR_SIZE) return -1; |
| terencez | 14:ec9e195830ff | 193 | memcpy(buffer + head, LINK_URI_SEPARATOR, LINK_URI_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 194 | head += LINK_URI_SEPARATOR_SIZE; |
| terencez | 14:ec9e195830ff | 195 | |
| terencez | 14:ec9e195830ff | 196 | res = utils_intToText(tlvP->id, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 197 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 198 | head += res; |
| terencez | 14:ec9e195830ff | 199 | |
| terencez | 14:ec9e195830ff | 200 | if (tlvP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) |
| terencez | 14:ec9e195830ff | 201 | { |
| terencez | 14:ec9e195830ff | 202 | if (bufferLen - head < LINK_ITEM_DIM_START_SIZE) return -1; |
| terencez | 14:ec9e195830ff | 203 | memcpy(buffer + head, LINK_ITEM_DIM_START, LINK_ITEM_DIM_START_SIZE); |
| terencez | 14:ec9e195830ff | 204 | head += LINK_ITEM_DIM_START_SIZE; |
| terencez | 14:ec9e195830ff | 205 | |
| terencez | 14:ec9e195830ff | 206 | res = utils_intToText(tlvP->value.asChildren.count, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 207 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 208 | head += res; |
| terencez | 14:ec9e195830ff | 209 | |
| terencez | 14:ec9e195830ff | 210 | if (bufferLen - head < LINK_ITEM_ATTR_END_SIZE) return -1; |
| terencez | 14:ec9e195830ff | 211 | memcpy(buffer + head, LINK_ITEM_ATTR_END, LINK_ITEM_ATTR_END_SIZE); |
| terencez | 14:ec9e195830ff | 212 | head += LINK_ITEM_ATTR_END_SIZE; |
| terencez | 14:ec9e195830ff | 213 | } |
| terencez | 14:ec9e195830ff | 214 | else |
| terencez | 14:ec9e195830ff | 215 | { |
| terencez | 14:ec9e195830ff | 216 | if (bufferLen - head < LINK_ITEM_END_SIZE) return -1; |
| terencez | 14:ec9e195830ff | 217 | memcpy(buffer + head, LINK_ITEM_END, LINK_ITEM_END_SIZE); |
| terencez | 14:ec9e195830ff | 218 | head += LINK_ITEM_END_SIZE; |
| terencez | 14:ec9e195830ff | 219 | } |
| terencez | 14:ec9e195830ff | 220 | |
| terencez | 14:ec9e195830ff | 221 | if (serverP != NULL) |
| terencez | 14:ec9e195830ff | 222 | { |
| terencez | 14:ec9e195830ff | 223 | memcpy(&uri, parentUriP, sizeof(lwm2m_uri_t)); |
| terencez | 14:ec9e195830ff | 224 | uri.resourceId = tlvP->id; |
| terencez | 14:ec9e195830ff | 225 | uri.flag |= LWM2M_URI_FLAG_RESOURCE_ID; |
| terencez | 14:ec9e195830ff | 226 | res = prv_serializeAttributes(contextP, &uri, serverP, objectParamP, buffer, head - 1, bufferLen); |
| terencez | 14:ec9e195830ff | 227 | if (res < 0) return -1; // careful, 0 is valid |
| terencez | 14:ec9e195830ff | 228 | if (res > 0) head += res; |
| terencez | 14:ec9e195830ff | 229 | } |
| terencez | 14:ec9e195830ff | 230 | break; |
| terencez | 14:ec9e195830ff | 231 | |
| terencez | 14:ec9e195830ff | 232 | case LWM2M_TYPE_OBJECT_INSTANCE: |
| terencez | 14:ec9e195830ff | 233 | { |
| terencez | 14:ec9e195830ff | 234 | uint8_t uriStr[URI_MAX_STRING_LEN]; |
| terencez | 14:ec9e195830ff | 235 | size_t uriLen; |
| terencez | 14:ec9e195830ff | 236 | size_t index; |
| terencez | 14:ec9e195830ff | 237 | |
| terencez | 14:ec9e195830ff | 238 | if (parentUriLen > 0) |
| terencez | 14:ec9e195830ff | 239 | { |
| terencez | 14:ec9e195830ff | 240 | if (URI_MAX_STRING_LEN < parentUriLen) return -1; |
| terencez | 14:ec9e195830ff | 241 | memcpy(uriStr, parentUriStr, parentUriLen); |
| terencez | 14:ec9e195830ff | 242 | uriLen = parentUriLen; |
| terencez | 14:ec9e195830ff | 243 | } |
| terencez | 14:ec9e195830ff | 244 | else |
| terencez | 14:ec9e195830ff | 245 | { |
| terencez | 14:ec9e195830ff | 246 | uriLen = 0; |
| terencez | 14:ec9e195830ff | 247 | } |
| terencez | 14:ec9e195830ff | 248 | |
| terencez | 14:ec9e195830ff | 249 | if (URI_MAX_STRING_LEN - uriLen < LINK_URI_SEPARATOR_SIZE) return -1; |
| terencez | 14:ec9e195830ff | 250 | memcpy(uriStr + uriLen, LINK_URI_SEPARATOR, LINK_URI_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 251 | uriLen += LINK_URI_SEPARATOR_SIZE; |
| terencez | 14:ec9e195830ff | 252 | |
| terencez | 14:ec9e195830ff | 253 | res = utils_intToText(tlvP->id, uriStr + uriLen, URI_MAX_STRING_LEN - uriLen); |
| terencez | 14:ec9e195830ff | 254 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 255 | uriLen += res; |
| terencez | 14:ec9e195830ff | 256 | |
| terencez | 14:ec9e195830ff | 257 | memcpy(&uri, parentUriP, sizeof(lwm2m_uri_t)); |
| terencez | 14:ec9e195830ff | 258 | uri.instanceId = tlvP->id; |
| terencez | 14:ec9e195830ff | 259 | uri.flag |= LWM2M_URI_FLAG_INSTANCE_ID; |
| terencez | 14:ec9e195830ff | 260 | |
| terencez | 14:ec9e195830ff | 261 | head = 0; |
| terencez | 14:ec9e195830ff | 262 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ITEM_START, LINK_ITEM_START_SIZE); |
| terencez | 14:ec9e195830ff | 263 | PRV_CONCAT_STR(buffer, bufferLen, head, uriStr, uriLen); |
| terencez | 14:ec9e195830ff | 264 | PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ITEM_END, LINK_ITEM_END_SIZE); |
| terencez | 14:ec9e195830ff | 265 | if (serverP != NULL) |
| terencez | 14:ec9e195830ff | 266 | { |
| terencez | 14:ec9e195830ff | 267 | res = prv_serializeAttributes(contextP, &uri, serverP, NULL, buffer, head - 1, bufferLen); |
| terencez | 14:ec9e195830ff | 268 | if (res < 0) return -1; // careful, 0 is valid |
| terencez | 14:ec9e195830ff | 269 | if (res == 0) head = 0; // rewind |
| terencez | 14:ec9e195830ff | 270 | else head += res; |
| terencez | 14:ec9e195830ff | 271 | } |
| terencez | 14:ec9e195830ff | 272 | for (index = 0; index < tlvP->value.asChildren.count; index++) |
| terencez | 14:ec9e195830ff | 273 | { |
| terencez | 14:ec9e195830ff | 274 | res = prv_serializeLinkData(contextP, tlvP->value.asChildren.array + index, serverP, objectParamP, &uri, uriStr, uriLen, buffer + head, bufferLen - head); |
| terencez | 14:ec9e195830ff | 275 | if (res < 0) return -1; |
| terencez | 14:ec9e195830ff | 276 | head += res; |
| terencez | 14:ec9e195830ff | 277 | } |
| terencez | 14:ec9e195830ff | 278 | } |
| terencez | 14:ec9e195830ff | 279 | break; |
| terencez | 14:ec9e195830ff | 280 | |
| terencez | 14:ec9e195830ff | 281 | case LWM2M_TYPE_OBJECT: |
| terencez | 14:ec9e195830ff | 282 | default: |
| terencez | 14:ec9e195830ff | 283 | return -1; |
| terencez | 14:ec9e195830ff | 284 | } |
| terencez | 14:ec9e195830ff | 285 | |
| terencez | 14:ec9e195830ff | 286 | return head; |
| terencez | 14:ec9e195830ff | 287 | } |
| terencez | 14:ec9e195830ff | 288 | |
| terencez | 14:ec9e195830ff | 289 | int discover_serialize(lwm2m_context_t * contextP, |
| terencez | 14:ec9e195830ff | 290 | lwm2m_uri_t * uriP, |
| terencez | 14:ec9e195830ff | 291 | lwm2m_server_t * serverP, |
| terencez | 14:ec9e195830ff | 292 | int size, |
| terencez | 14:ec9e195830ff | 293 | lwm2m_data_t * dataP, |
| terencez | 14:ec9e195830ff | 294 | uint8_t ** bufferP) |
| terencez | 14:ec9e195830ff | 295 | { |
| terencez | 14:ec9e195830ff | 296 | uint8_t bufferLink[PRV_LINK_BUFFER_SIZE]; |
| terencez | 14:ec9e195830ff | 297 | uint8_t baseUriStr[URI_MAX_STRING_LEN]; |
| terencez | 14:ec9e195830ff | 298 | int baseUriLen; |
| terencez | 14:ec9e195830ff | 299 | int index; |
| terencez | 14:ec9e195830ff | 300 | size_t head; |
| terencez | 14:ec9e195830ff | 301 | int res; |
| terencez | 14:ec9e195830ff | 302 | lwm2m_uri_t parentUri; |
| terencez | 14:ec9e195830ff | 303 | lwm2m_attributes_t * paramP; |
| terencez | 14:ec9e195830ff | 304 | lwm2m_attributes_t mergedParam; |
| terencez | 14:ec9e195830ff | 305 | |
| terencez | 14:ec9e195830ff | 306 | LOG_ARG("size: %d", size); |
| terencez | 14:ec9e195830ff | 307 | LOG_URI(uriP); |
| terencez | 14:ec9e195830ff | 308 | |
| terencez | 14:ec9e195830ff | 309 | head = 0; |
| terencez | 14:ec9e195830ff | 310 | memset(&parentUri, 0, sizeof(lwm2m_uri_t)); |
| terencez | 14:ec9e195830ff | 311 | parentUri.objectId = uriP->objectId; |
| terencez | 14:ec9e195830ff | 312 | parentUri.flag = LWM2M_URI_FLAG_OBJECT_ID; |
| terencez | 14:ec9e195830ff | 313 | |
| terencez | 14:ec9e195830ff | 314 | if (LWM2M_URI_IS_SET_RESOURCE(uriP)) |
| terencez | 14:ec9e195830ff | 315 | { |
| terencez | 14:ec9e195830ff | 316 | lwm2m_uri_t tempUri; |
| terencez | 14:ec9e195830ff | 317 | lwm2m_attributes_t * objParamP; |
| terencez | 14:ec9e195830ff | 318 | lwm2m_attributes_t * instParamP; |
| terencez | 14:ec9e195830ff | 319 | |
| terencez | 14:ec9e195830ff | 320 | memset(&parentUri, 0, sizeof(lwm2m_uri_t)); |
| terencez | 14:ec9e195830ff | 321 | tempUri.objectId = uriP->objectId; |
| terencez | 14:ec9e195830ff | 322 | tempUri.flag = LWM2M_URI_FLAG_OBJECT_ID; |
| terencez | 14:ec9e195830ff | 323 | |
| terencez | 14:ec9e195830ff | 324 | // get object level attributes |
| terencez | 14:ec9e195830ff | 325 | objParamP = prv_findAttributes(contextP, &tempUri, serverP); |
| terencez | 14:ec9e195830ff | 326 | |
| terencez | 14:ec9e195830ff | 327 | // get object instance level attributes |
| terencez | 14:ec9e195830ff | 328 | tempUri.instanceId = uriP->instanceId; |
| terencez | 14:ec9e195830ff | 329 | tempUri.flag = LWM2M_URI_FLAG_INSTANCE_ID; |
| terencez | 14:ec9e195830ff | 330 | instParamP = prv_findAttributes(contextP, &tempUri, serverP); |
| terencez | 14:ec9e195830ff | 331 | |
| terencez | 14:ec9e195830ff | 332 | if (objParamP != NULL) |
| terencez | 14:ec9e195830ff | 333 | { |
| terencez | 14:ec9e195830ff | 334 | if (instParamP != NULL) |
| terencez | 14:ec9e195830ff | 335 | { |
| terencez | 14:ec9e195830ff | 336 | memset(&mergedParam, 0, sizeof(lwm2m_attributes_t)); |
| terencez | 14:ec9e195830ff | 337 | mergedParam.toSet = objParamP->toSet | instParamP->toSet; |
| terencez | 14:ec9e195830ff | 338 | if (mergedParam.toSet & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terencez | 14:ec9e195830ff | 339 | { |
| terencez | 14:ec9e195830ff | 340 | if (instParamP->toSet & LWM2M_ATTR_FLAG_MIN_PERIOD) |
| terencez | 14:ec9e195830ff | 341 | { |
| terencez | 14:ec9e195830ff | 342 | mergedParam.minPeriod = instParamP->minPeriod; |
| terencez | 14:ec9e195830ff | 343 | } |
| terencez | 14:ec9e195830ff | 344 | else |
| terencez | 14:ec9e195830ff | 345 | { |
| terencez | 14:ec9e195830ff | 346 | mergedParam.minPeriod = objParamP->minPeriod; |
| terencez | 14:ec9e195830ff | 347 | } |
| terencez | 14:ec9e195830ff | 348 | } |
| terencez | 14:ec9e195830ff | 349 | if (mergedParam.toSet & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terencez | 14:ec9e195830ff | 350 | { |
| terencez | 14:ec9e195830ff | 351 | if (instParamP->toSet & LWM2M_ATTR_FLAG_MAX_PERIOD) |
| terencez | 14:ec9e195830ff | 352 | { |
| terencez | 14:ec9e195830ff | 353 | mergedParam.maxPeriod = instParamP->maxPeriod; |
| terencez | 14:ec9e195830ff | 354 | } |
| terencez | 14:ec9e195830ff | 355 | else |
| terencez | 14:ec9e195830ff | 356 | { |
| terencez | 14:ec9e195830ff | 357 | mergedParam.maxPeriod = objParamP->maxPeriod; |
| terencez | 14:ec9e195830ff | 358 | } |
| terencez | 14:ec9e195830ff | 359 | } |
| terencez | 14:ec9e195830ff | 360 | paramP = &mergedParam; |
| terencez | 14:ec9e195830ff | 361 | } |
| terencez | 14:ec9e195830ff | 362 | else |
| terencez | 14:ec9e195830ff | 363 | { |
| terencez | 14:ec9e195830ff | 364 | paramP = objParamP; |
| terencez | 14:ec9e195830ff | 365 | } |
| terencez | 14:ec9e195830ff | 366 | } |
| terencez | 14:ec9e195830ff | 367 | else |
| terencez | 14:ec9e195830ff | 368 | { |
| terencez | 14:ec9e195830ff | 369 | paramP = instParamP; |
| terencez | 14:ec9e195830ff | 370 | } |
| terencez | 14:ec9e195830ff | 371 | uriP->flag &= ~LWM2M_URI_FLAG_RESOURCE_ID; |
| terencez | 14:ec9e195830ff | 372 | } |
| terencez | 14:ec9e195830ff | 373 | else |
| terencez | 14:ec9e195830ff | 374 | { |
| terencez | 14:ec9e195830ff | 375 | paramP = NULL; |
| terencez | 14:ec9e195830ff | 376 | |
| terencez | 14:ec9e195830ff | 377 | if (LWM2M_URI_IS_SET_INSTANCE(uriP)) |
| terencez | 14:ec9e195830ff | 378 | { |
| terencez | 14:ec9e195830ff | 379 | PRV_CONCAT_STR(bufferLink, PRV_LINK_BUFFER_SIZE, head, LINK_ITEM_START, LINK_ITEM_START_SIZE); |
| terencez | 14:ec9e195830ff | 380 | PRV_CONCAT_STR(bufferLink, PRV_LINK_BUFFER_SIZE, head, LINK_URI_SEPARATOR, LINK_URI_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 381 | res = utils_intToText(uriP->objectId, bufferLink + head, PRV_LINK_BUFFER_SIZE - head); |
| terencez | 14:ec9e195830ff | 382 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 383 | head += res; |
| terencez | 14:ec9e195830ff | 384 | PRV_CONCAT_STR(bufferLink, PRV_LINK_BUFFER_SIZE, head, LINK_URI_SEPARATOR, LINK_URI_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 385 | res = utils_intToText(uriP->instanceId, bufferLink + head, PRV_LINK_BUFFER_SIZE - head); |
| terencez | 14:ec9e195830ff | 386 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 387 | head += res; |
| terencez | 14:ec9e195830ff | 388 | PRV_CONCAT_STR(bufferLink, PRV_LINK_BUFFER_SIZE, head, LINK_ITEM_END, LINK_ITEM_END_SIZE); |
| terencez | 14:ec9e195830ff | 389 | parentUri.instanceId = uriP->instanceId; |
| terencez | 14:ec9e195830ff | 390 | parentUri.flag = LWM2M_URI_FLAG_INSTANCE_ID; |
| terencez | 14:ec9e195830ff | 391 | if (serverP != NULL) |
| terencez | 14:ec9e195830ff | 392 | { |
| terencez | 14:ec9e195830ff | 393 | res = prv_serializeAttributes(contextP, &parentUri, serverP, NULL, bufferLink, head - 1, PRV_LINK_BUFFER_SIZE); |
| terencez | 14:ec9e195830ff | 394 | if (res < 0) return -1; // careful, 0 is valid |
| terencez | 14:ec9e195830ff | 395 | } |
| terencez | 14:ec9e195830ff | 396 | else |
| terencez | 14:ec9e195830ff | 397 | { |
| terencez | 14:ec9e195830ff | 398 | res = 0; |
| terencez | 14:ec9e195830ff | 399 | } |
| terencez | 14:ec9e195830ff | 400 | head += res; |
| terencez | 14:ec9e195830ff | 401 | } |
| terencez | 14:ec9e195830ff | 402 | else |
| terencez | 14:ec9e195830ff | 403 | { |
| terencez | 14:ec9e195830ff | 404 | PRV_CONCAT_STR(bufferLink, PRV_LINK_BUFFER_SIZE, head, LINK_ITEM_START, LINK_ITEM_START_SIZE); |
| terencez | 14:ec9e195830ff | 405 | PRV_CONCAT_STR(bufferLink, PRV_LINK_BUFFER_SIZE, head, LINK_URI_SEPARATOR, LINK_URI_SEPARATOR_SIZE); |
| terencez | 14:ec9e195830ff | 406 | res = utils_intToText(uriP->objectId, bufferLink + head, PRV_LINK_BUFFER_SIZE - head); |
| terencez | 14:ec9e195830ff | 407 | if (res <= 0) return -1; |
| terencez | 14:ec9e195830ff | 408 | head += res; |
| terencez | 14:ec9e195830ff | 409 | PRV_CONCAT_STR(bufferLink, PRV_LINK_BUFFER_SIZE, head, LINK_ITEM_END, LINK_ITEM_END_SIZE); |
| terencez | 14:ec9e195830ff | 410 | |
| terencez | 14:ec9e195830ff | 411 | if (serverP != NULL) |
| terencez | 14:ec9e195830ff | 412 | { |
| terencez | 14:ec9e195830ff | 413 | res = prv_serializeAttributes(contextP, &parentUri, serverP, NULL, bufferLink, head - 1, PRV_LINK_BUFFER_SIZE); |
| terencez | 14:ec9e195830ff | 414 | if (res < 0) return -1; // careful, 0 is valid |
| terencez | 14:ec9e195830ff | 415 | head += res; |
| terencez | 14:ec9e195830ff | 416 | } |
| terencez | 14:ec9e195830ff | 417 | } |
| terencez | 14:ec9e195830ff | 418 | } |
| terencez | 14:ec9e195830ff | 419 | |
| terencez | 14:ec9e195830ff | 420 | baseUriLen = uri_toString(uriP, baseUriStr, URI_MAX_STRING_LEN, NULL); |
| terencez | 14:ec9e195830ff | 421 | if (baseUriLen < 0) return -1; |
| terencez | 14:ec9e195830ff | 422 | baseUriLen -= 1; |
| terencez | 14:ec9e195830ff | 423 | |
| terencez | 14:ec9e195830ff | 424 | for (index = 0; index < size && head < PRV_LINK_BUFFER_SIZE; index++) |
| terencez | 14:ec9e195830ff | 425 | { |
| terencez | 14:ec9e195830ff | 426 | res = prv_serializeLinkData(contextP, dataP + index, serverP, paramP, uriP, baseUriStr, baseUriLen, bufferLink + head, PRV_LINK_BUFFER_SIZE - head); |
| terencez | 14:ec9e195830ff | 427 | if (res < 0) return -1; |
| terencez | 14:ec9e195830ff | 428 | head += res; |
| terencez | 14:ec9e195830ff | 429 | } |
| terencez | 14:ec9e195830ff | 430 | |
| terencez | 14:ec9e195830ff | 431 | if (head > 0) |
| terencez | 14:ec9e195830ff | 432 | { |
| terencez | 14:ec9e195830ff | 433 | head -= 1; |
| terencez | 14:ec9e195830ff | 434 | |
| terencez | 14:ec9e195830ff | 435 | *bufferP = (uint8_t *)lwm2m_malloc(head); |
| terencez | 14:ec9e195830ff | 436 | if (*bufferP == NULL) return 0; |
| terencez | 14:ec9e195830ff | 437 | memcpy(*bufferP, bufferLink, head); |
| terencez | 14:ec9e195830ff | 438 | } |
| terencez | 14:ec9e195830ff | 439 | |
| terencez | 14:ec9e195830ff | 440 | return (int)head; |
| terencez | 14:ec9e195830ff | 441 | } |
| terencez | 14:ec9e195830ff | 442 | #endif |