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