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