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