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