Add links and rep in the response for the collection
[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 OCRepPayload* OCRepPayloadBatchClone(const OCRepPayload* repPayload);
78
79 void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child);
80
81 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri);
82
83 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType);
84 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* iface);
85 bool OCRepPayloadAddModelVersion(OCRepPayload* payload, const char* dmv);
86
87 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType);
88 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* iface);
89
90 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name);
91 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name);
92
93 bool OCRepPayloadSetPropInt(OCRepPayload* payload, const char* name, int64_t value);
94 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value);
95
96 bool OCRepPayloadSetPropDouble(OCRepPayload* payload, const char* name, double value);
97 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value);
98
99 /**
100  * This function allocates memory for the byte string and sets it in the payload.
101  *
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.
105  *
106  * @return true on success, false upon failure.
107  */
108 bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value);
109
110 /**
111  * This function sets the byte string in the payload.
112  *
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.
116  *
117  * @return true on success, false upon failure.
118  */
119 bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name,
120         OCByteString* value);
121
122 /**
123  * This function gets the byte string from the payload.
124  *
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.
128  *
129  * @note: Caller needs to invoke OCFree on value.bytes after it is finished using the byte string.
130  *
131  * @return true on success, false upon failure.
132  */
133 bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name,
134         OCByteString* value);
135
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);
139
140 bool OCRepPayloadSetPropBool(OCRepPayload* payload, const char* name, bool value);
141 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value);
142
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);
146
147 #ifdef __WITH_TLS__
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);
151 #endif
152
153 /**
154  * This function allocates memory for the byte string array and sets it in the payload.
155  *
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.
160  *
161  * @return true on success, false upon failure.
162  */
163 bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
164         OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
165
166 /**
167  * This function sets the byte string array in the payload.
168  *
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.
173  *
174  * @return true on success, false upon failure.
175  */
176 bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
177         const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
178
179 /**
180  * This function gets the byte string array from the payload.
181  *
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.
186  *
187  * @note: Caller needs to invoke OICFree on 'bytes' field of all array elements after it is
188  *        finished using the byte string array.
189  *
190  * @return true on success, false upon failure.
191  */
192 bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
193         OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
194
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]);
201
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]);
208
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]);
215
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]);
222
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]);
229
230 void OCRepPayloadDestroy(OCRepPayload* payload);
231
232 // Discovery Payload
233 OCDiscoveryPayload* OCDiscoveryPayloadCreate();
234
235 OCSecurityPayload* OCSecurityPayloadCreate(const uint8_t* securityData, size_t size);
236 void OCSecurityPayloadDestroy(OCSecurityPayload* payload);
237
238 #ifndef TCP_ADAPTER
239 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
240                                    uint16_t securePort);
241 #else
242 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
243                                    uint16_t securePort, uint16_t tcpPort);
244 #endif
245 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res);
246 bool OCResourcePayloadAddStringLL(OCStringLL **payload, const char* type);
247
248 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload);
249 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index);
250
251 void OCDiscoveryResourceDestroy(OCResourcePayload* payload);
252 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload);
253
254 // Presence Payload
255 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
256         OCPresenceTrigger trigger, const char* resourceType);
257 void OCPresencePayloadDestroy(OCPresencePayload* payload);
258
259 // Helper API
260 OCStringLL* CloneOCStringLL (OCStringLL* ll);
261 void OCFreeOCStringLL(OCStringLL* ll);
262
263 /**
264  * This function creates a list from a string (with separated contents if several)
265  * @param text         single string or CSV text fields
266  * @return newly allocated linked list
267  * @note separator is ',' (according to rfc4180, ';' is not valid)
268  **/
269 OCStringLL* OCCreateOCStringLL(const char* text);
270
271 /**
272  * This function creates a string from a list (with separated contents if several)
273  * @param ll           Pointer to list
274  * @return newly allocated string. Caller takes ownership and must later free this memory with OICFree.
275  * @note separator is ',' (according to rfc4180)
276  **/
277 char* OCCreateString(const OCStringLL* ll);
278
279 /**
280  * This function copies contents (and allocates if necessary)
281  * @param dest existing bytestring (or null to allocate here)
282  * @param source existing bytestring
283  * @return true of success false on any errors
284  **/
285 bool OCByteStringCopy(OCByteString *dest, const OCByteString *source);
286
287 #ifdef __cplusplus
288 }
289 #endif
290
291 #endif