1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #ifndef __STDC_FORMAT_MACROS
25 #define __STDC_FORMAT_MACROS
27 #ifndef __STDC_LIMIT_MACROS
28 #define __STDC_LIMIT_MACROS
35 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
36 #include "securevirtualresourcetypes.h"
45 * Macro to verify the validity of cbor operation.
47 #define VERIFY_CBOR_SUCCESS(log_tag, err, log_message) \
48 if ((CborNoError != (CborError)(err)) && (CborErrorOutOfMemory != (CborError)(err))) \
50 if ((log_tag) && (log_message)) \
52 OIC_LOG_V(ERROR, (log_tag), "%s with cbor error: \'%s\'.", \
53 (log_message), (cbor_error_string((CborError)err))); \
58 #define VERIFY_PARAM_NON_NULL(log_tag, err, log_message) \
61 OIC_LOG_V(FATAL, (log_tag), "%s", (log_message)); \
66 typedef struct OCResource OCResource;
68 void OCPayloadDestroy(OCPayload* payload);
70 // Representation Payload
71 OCRepPayload* OCRepPayloadCreate();
73 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH]);
75 OCRepPayload* OCRepPayloadClone(const OCRepPayload* payload);
77 OCRepPayload* OCRepPayloadBatchClone(const OCRepPayload* repPayload);
79 void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child);
81 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri);
83 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType);
84 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* iface);
85 bool OCRepPayloadAddModelVersion(OCRepPayload* payload, const char* dmv);
87 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType);
88 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* iface);
90 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name);
91 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name);
93 bool OCRepPayloadSetPropInt(OCRepPayload* payload, const char* name, int64_t value);
94 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value);
96 bool OCRepPayloadSetPropDouble(OCRepPayload* payload, const char* name, double value);
97 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value);
100 * This function allocates memory for the byte string and sets it in the payload.
102 * @param payload Pointer to the payload to which byte string needs to be added.
103 * @param name Name of the byte string.
104 * @param value Byte string and it's length.
106 * @return true on success, false upon failure.
108 bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value);
111 * This function sets the byte string in the payload.
113 * @param payload Pointer to the payload to which byte string needs to be added.
114 * @param name Name of the byte string.
115 * @param value Byte string and it's length.
117 * @return true on success, false upon failure.
119 bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name,
120 OCByteString* value);
123 * This function gets the byte string from the payload.
125 * @param payload Pointer to the payload from which byte string needs to be retrieved.
126 * @param name Name of the byte string.
127 * @param value Byte string and it's length.
129 * @note: Caller needs to invoke OCFree on value.bytes after it is finished using the byte string.
131 * @return true on success, false upon failure.
133 bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name,
134 OCByteString* value);
136 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value);
137 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value);
138 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value);
140 bool OCRepPayloadSetPropBool(OCRepPayload* payload, const char* name, bool value);
141 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value);
143 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value);
144 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value);
145 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value);
148 bool OCRepPayloadSetPropPubDataType(OCRepPayload *payload, const char *name, const OicSecKey_t *value);
149 bool OCRepPayloadSetPropPubDataTypeAsOwner(OCRepPayload *payload, const char *name, const OicSecKey_t *value);
150 bool OCRepPayloadGetPropPubDataType(const OCRepPayload *payload, const char *name, OicSecKey_t *value);
154 * This function allocates memory for the byte string array and sets it in the payload.
156 * @param payload Pointer to the payload to which byte string array needs to be added.
157 * @param name Name of the byte string.
158 * @param array Byte string array.
159 * @param dimensions Number of byte strings in above array.
161 * @return true on success, false upon failure.
163 bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
164 OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
167 * This function sets the byte string array in the payload.
169 * @param payload Pointer to the payload to which byte string array needs to be added.
170 * @param name Name of the byte string.
171 * @param array Byte string array.
172 * @param dimensions Number of byte strings in above array.
174 * @return true on success, false upon failure.
176 bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
177 const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
180 * This function gets the byte string array from the payload.
182 * @param payload Pointer to the payload from which byte string array needs to be retrieved.
183 * @param name Name of the byte string array.
184 * @param value Byte string array.
185 * @param dimensions Number of byte strings in above array.
187 * @note: Caller needs to invoke OICFree on 'bytes' field of all array elements after it is
188 * finished using the byte string array.
190 * @return true on success, false upon failure.
192 bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
193 OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
195 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
196 int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
197 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
198 const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
199 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
200 int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
202 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
203 double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
204 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
205 const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
206 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
207 double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
209 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
210 char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
211 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
212 const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
213 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
214 char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
216 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
217 bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
218 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
219 const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
220 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
221 bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
223 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
224 OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
225 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
226 const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
227 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
228 OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
230 void OCRepPayloadDestroy(OCRepPayload* payload);
233 OCDiscoveryPayload* OCDiscoveryPayloadCreate();
235 OCSecurityPayload* OCSecurityPayloadCreate(const uint8_t* securityData, size_t size);
236 void OCSecurityPayloadDestroy(OCSecurityPayload* payload);
239 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
240 uint16_t securePort);
242 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
243 uint16_t securePort, uint16_t tcpPort);
245 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res);
246 bool OCResourcePayloadAddStringLL(OCStringLL **payload, const char* type);
248 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload);
249 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index);
251 void OCDiscoveryResourceDestroy(OCResourcePayload* payload);
252 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload);
256 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
257 OCPresenceTrigger trigger, const char* resourceType);
258 void OCPresencePayloadDestroy(OCPresencePayload* payload);
262 OCStringLL* CloneOCStringLL (OCStringLL* ll);
263 void OCFreeOCStringLL(OCStringLL* ll);
266 * This function creates a list from a string (with separated contents if several)
267 * @param text single string or CSV text fields
268 * @return newly allocated linked list
269 * @note separator is ',' (according to rfc4180, ';' is not valid)
271 OCStringLL* OCCreateOCStringLL(const char* text);
274 * This function creates a string from a list (with separated contents if several)
275 * @param ll Pointer to list
276 * @return newly allocated string. Caller takes ownership and must later free this memory with OICFree.
277 * @note separator is ',' (according to rfc4180)
279 char* OCCreateString(const OCStringLL* ll);
282 * This function copies contents (and allocates if necessary)
283 * @param dest existing bytestring (or null to allocate here)
284 * @param source existing bytestring
285 * @return true of success false on any errors
287 bool OCByteStringCopy(OCByteString *dest, const OCByteString *source);