[IOT-1569] Use OICMalloc/OICFree consistently
[platform/upstream/iotivity.git] / resource / csdk / stack / include / ocpayload.h
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef OCPAYLOAD_H_
22 #define OCPAYLOAD_H_
23
24 #ifndef __STDC_FORMAT_MACROS
25 #define __STDC_FORMAT_MACROS
26 #endif
27 #ifndef __STDC_LIMIT_MACROS
28 #define __STDC_LIMIT_MACROS
29 #endif
30
31 #include <stdbool.h>
32 #include <inttypes.h>
33 #include "octypes.h"
34
35 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
36 #include "securevirtualresourcetypes.h"
37 #endif
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43
44 /**
45  * Macro to verify the validity of cbor operation.
46  */
47 #define VERIFY_CBOR_SUCCESS(log_tag, err, log_message) \
48     if ((CborNoError != (err)) && (CborErrorOutOfMemory != (err))) \
49     { \
50         if ((log_tag) && (log_message)) \
51         { \
52             OIC_LOG_V(ERROR, (log_tag), "%s with cbor error: \'%s\'.", \
53                     (log_message), (cbor_error_string(err))); \
54         } \
55         goto exit; \
56     } \
57
58 #define VERIFY_PARAM_NON_NULL(log_tag, err, log_message) \
59     if (NULL == (err)) \
60     { \
61         OIC_LOG_V(FATAL, (log_tag), "%s", (log_message)); \
62         goto exit;\
63     } \
64
65
66 typedef struct OCResource OCResource;
67
68 void OCPayloadDestroy(OCPayload* payload);
69
70 // Representation Payload
71 OCRepPayload* OCRepPayloadCreate();
72
73 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH]);
74
75 OCRepPayload* OCRepPayloadClone(const OCRepPayload* payload);
76
77 void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child);
78
79 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri);
80
81 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType);
82 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* iface);
83 bool OCRepPayloadAddModelVersion(OCRepPayload* payload, const char* dmv);
84
85 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType);
86 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* iface);
87
88 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name);
89 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name);
90
91 bool OCRepPayloadSetPropInt(OCRepPayload* payload, const char* name, int64_t value);
92 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value);
93
94 bool OCRepPayloadSetPropDouble(OCRepPayload* payload, const char* name, double value);
95 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value);
96
97 /**
98  * This function allocates memory for the byte string and sets it in the payload.
99  *
100  * @param payload      Pointer to the payload to which byte string needs to be added.
101  * @param name         Name of the byte string.
102  * @param value        Byte string and it's length.
103  *
104  * @return true on success, false upon failure.
105  */
106 bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value);
107
108 /**
109  * This function sets the byte string in the payload.
110  *
111  * @param payload      Pointer to the payload to which byte string needs to be added.
112  * @param name         Name of the byte string.
113  * @param value        Byte string and it's length.
114  *
115  * @return true on success, false upon failure.
116  */
117 bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name,
118         OCByteString* value);
119
120 /**
121  * This function gets the byte string from the payload.
122  *
123  * @param payload      Pointer to the payload from which byte string needs to be retrieved.
124  * @param name         Name of the byte string.
125  * @param value        Byte string and it's length.
126  *
127  * @note: Caller needs to invoke OCFree on value.bytes after it is finished using the byte string.
128  *
129  * @return true on success, false upon failure.
130  */
131 bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name,
132         OCByteString* value);
133
134 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value);
135 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value);
136 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value);
137
138 bool OCRepPayloadSetPropBool(OCRepPayload* payload, const char* name, bool value);
139 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value);
140
141 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value);
142 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value);
143 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value);
144
145 #ifdef __WITH_TLS__
146 bool OCRepPayloadSetPropPubDataType(OCRepPayload *payload, const char *name, const OicSecKey_t *value);
147 bool OCRepPayloadSetPropPubDataTypeAsOwner(OCRepPayload *payload, const char *name, const OicSecKey_t *value);
148 bool OCRepPayloadGetPropPubDataType(const OCRepPayload *payload, const char *name, OicSecKey_t *value);
149 #endif
150
151 /**
152  * This function allocates memory for the byte string array and sets it in the payload.
153  *
154  * @param payload      Pointer to the payload to which byte string array needs to be added.
155  * @param name         Name of the byte string.
156  * @param array        Byte string array.
157  * @param dimensions   Number of byte strings in above array.
158  *
159  * @return true on success, false upon failure.
160  */
161 bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
162         OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
163
164 /**
165  * This function sets the byte string array in the payload.
166  *
167  * @param payload      Pointer to the payload to which byte string array needs to be added.
168  * @param name         Name of the byte string.
169  * @param array        Byte string array.
170  * @param dimensions   Number of byte strings in above array.
171  *
172  * @return true on success, false upon failure.
173  */
174 bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
175         const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
176
177 /**
178  * This function gets the byte string array from the payload.
179  *
180  * @param payload      Pointer to the payload from which byte string array needs to be retrieved.
181  * @param name         Name of the byte string array.
182  * @param value        Byte string array.
183  * @param dimensions   Number of byte strings in above array.
184  *
185  * @note: Caller needs to invoke OICFree on 'bytes' field of all array elements after it is
186  *        finished using the byte string array.
187  *
188  * @return true on success, false upon failure.
189  */
190 bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
191         OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
192
193 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
194         int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
195 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
196         const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
197 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
198         int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
199
200 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
201         double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
202 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
203         const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
204 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
205         double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
206
207 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
208         char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
209 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
210         const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
211 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
212         char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
213
214 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
215         bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
216 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
217         const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
218 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
219         bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
220
221 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
222         OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
223 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
224         const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
225 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
226         OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
227
228 void OCRepPayloadDestroy(OCRepPayload* payload);
229
230 // Discovery Payload
231 OCDiscoveryPayload* OCDiscoveryPayloadCreate();
232
233 OCSecurityPayload* OCSecurityPayloadCreate(const uint8_t* securityData, size_t size);
234 void OCSecurityPayloadDestroy(OCSecurityPayload* payload);
235
236 #ifndef TCP_ADAPTER
237 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
238                                    uint16_t securePort);
239 #else
240 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
241                                    uint16_t securePort, uint16_t tcpPort);
242 #endif
243 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res);
244 bool OCResourcePayloadAddStringLL(OCStringLL **payload, const char* type);
245
246 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload);
247 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index);
248
249 void OCDiscoveryResourceDestroy(OCResourcePayload* payload);
250 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload);
251
252 // Device Payload
253 OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
254         const OCStringLL *types, const char* specVer, const char* dmVer);
255 void OCDevicePayloadDestroy(OCDevicePayload* payload);
256
257 // Platform Payload
258 OCPlatformPayload* OCPlatformPayloadCreate(const OCPlatformInfo* platformInfo);
259 OCPlatformPayload* OCPlatformPayloadCreateAsOwner(OCPlatformInfo* platformInfo);
260 void OCPlatformInfoDestroy(OCPlatformInfo *info);
261 void OCPlatformPayloadDestroy(OCPlatformPayload* payload);
262
263 // Presence Payload
264 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
265         OCPresenceTrigger trigger, const char* resourceType);
266 void OCPresencePayloadDestroy(OCPresencePayload* payload);
267
268 // Helper API
269 OCStringLL* CloneOCStringLL (OCStringLL* ll);
270 void OCFreeOCStringLL(OCStringLL* ll);
271
272 /**
273  * This function creates a list from a string (with separated contents if several)
274  * @param text         single string or CSV text fields
275  * @return newly allocated linked list
276  * @note separator is ',' (according to rfc4180, ';' is not valid)
277  **/
278 OCStringLL* OCCreateOCStringLL(const char* text);
279
280 /**
281  * This function creates a string from a list (with separated contents if several)
282  * @param ll           Pointer to list
283  * @return newly allocated string. Caller takes ownership and must later free this memory with OICFree.
284  * @note separator is ',' (according to rfc4180)
285  **/
286 char* OCCreateString(const OCStringLL* ll);
287
288 /**
289  * This function copies contents (and allocates if necessary)
290  * @param dest existing bytestring (or null to allocate here)
291  * @param source existing bytestring
292  * @return true of success false on any errors
293  **/
294 bool OCByteStringCopy(OCByteString *dest, const OCByteString *source);
295
296 #ifdef __cplusplus
297 }
298 #endif
299
300 #endif