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