VS2013/15 aggregate changes of Intel and Microsoft
[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 OC_EXPORT void OCPayloadDestroy(OCPayload* payload);
64
65 // Representation Payload
66 OC_EXPORT OCRepPayload* OCRepPayloadCreate();
67
68 OC_EXPORT size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH]);
69
70 OC_EXPORT OCRepPayload* OCRepPayloadClone(const OCRepPayload* payload);
71
72 OC_EXPORT void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child);
73
74 OC_EXPORT bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri);
75
76 OC_EXPORT bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType);
77 OC_EXPORT bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* iface);
78
79 OC_EXPORT bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType);
80 OC_EXPORT bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* iface);
81
82 OC_EXPORT bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name);
83 OC_EXPORT bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name);
84
85 OC_EXPORT bool OCRepPayloadSetPropInt(OCRepPayload* payload, const char* name, int64_t value);
86 OC_EXPORT bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value);
87
88 OC_EXPORT bool OCRepPayloadSetPropDouble(OCRepPayload* payload, const char* name, double value);
89 OC_EXPORT bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value);
90
91 /**
92  * This function allocates memory for the byte string and sets it in the payload.
93  *
94  * @param payload      Pointer to the payload to which byte string needs to be added.
95  * @param name         Name of the byte string.
96  * @param value        Byte string and it's length.
97  *
98  * @return true on success, false upon failure.
99  */
100 OC_EXPORT bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value);
101
102 /**
103  * This function sets the byte string in the payload.
104  *
105  * @param payload      Pointer to the payload to which byte string needs to be added.
106  * @param name         Name of the byte string.
107  * @param value        Byte string and it's length.
108  *
109  * @return true on success, false upon failure.
110  */
111 OC_EXPORT bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name,
112         OCByteString* value);
113
114 /**
115  * This function gets the byte string from the payload.
116  *
117  * @param payload      Pointer to the payload from which byte string needs to be retrieved.
118  * @param name         Name of the byte string.
119  * @param value        Byte string and it's length.
120  *
121  * @note: Caller needs to invoke OCFree on value.bytes after it is finished using the byte string.
122  *
123  * @return true on success, false upon failure.
124  */
125 OC_EXPORT bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name,
126         OCByteString* value);
127
128 OC_EXPORT bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value);
129 OC_EXPORT bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value);
130 OC_EXPORT bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value);
131
132 OC_EXPORT bool OCRepPayloadSetPropBool(OCRepPayload* payload, const char* name, bool value);
133 OC_EXPORT bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value);
134
135 OC_EXPORT bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value);
136 OC_EXPORT bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value);
137 OC_EXPORT bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value);
138
139 /**
140  * This function allocates memory for the byte string array and sets it in the payload.
141  *
142  * @param payload      Pointer to the payload to which byte string array needs to be added.
143  * @param name         Name of the byte string.
144  * @param array        Byte string array.
145  * @param dimensions   Number of byte strings in above array.
146  *
147  * @return true on success, false upon failure.
148  */
149 OC_EXPORT bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
150         OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
151
152 /**
153  * This function sets the byte string array 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 OC_EXPORT bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
163         const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
164
165 /**
166  * This function gets the byte string array from the payload.
167  *
168  * @param payload      Pointer to the payload from which byte string array needs to be retrieved.
169  * @param name         Name of the byte string array.
170  * @param value        Byte string array.
171  * @param dimensions   Number of byte strings in above array.
172  *
173  * @note: Caller needs to invoke OICFree on 'bytes' field of all array elements after it is
174  *        finished using the byte string array.
175  *
176  * @return true on success, false upon failure.
177  */
178 OC_EXPORT bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
179         OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
180
181 OC_EXPORT bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
182         int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
183 OC_EXPORT bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
184         const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
185 OC_EXPORT bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
186         int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
187
188 OC_EXPORT bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
189         double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
190 OC_EXPORT bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
191         const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
192 OC_EXPORT bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
193         double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
194
195 OC_EXPORT bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
196         char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
197 OC_EXPORT bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
198         const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
199 OC_EXPORT bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
200         char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
201
202 OC_EXPORT bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
203         bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
204 OC_EXPORT bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
205         const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
206 OC_EXPORT bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
207         bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
208
209 OC_EXPORT bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
210         OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
211 OC_EXPORT bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
212         const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
213 OC_EXPORT bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
214         OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH]);
215
216 OC_EXPORT void OCRepPayloadDestroy(OCRepPayload* payload);
217
218 // Discovery Payload
219 OC_EXPORT OCDiscoveryPayload* OCDiscoveryPayloadCreate();
220
221 OC_EXPORT OCSecurityPayload* OCSecurityPayloadCreate(const uint8_t* securityData, size_t size);
222 OC_EXPORT void OCSecurityPayloadDestroy(OCSecurityPayload* payload);
223
224 OC_EXPORT void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
225                                    uint16_t securePort, uint16_t tcpPort);
226 OC_EXPORT void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res);
227 OC_EXPORT bool OCResourcePayloadAddStringLL(OCStringLL **payload, const char* type);
228
229 OC_EXPORT size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload);
230 OC_EXPORT OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index);
231
232 OC_EXPORT void OCDiscoveryResourceDestroy(OCResourcePayload* payload);
233 OC_EXPORT void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload);
234
235 // Device Payload
236 OC_EXPORT OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
237         const OCStringLL *types, const char* specVer, const char* dmVer);
238 OC_EXPORT void OCDevicePayloadDestroy(OCDevicePayload* payload);
239
240 // Platform Payload
241 OC_EXPORT OCPlatformPayload* OCPlatformPayloadCreate(const OCPlatformInfo* platformInfo);
242 OC_EXPORT OCPlatformPayload* OCPlatformPayloadCreateAsOwner(OCPlatformInfo* platformInfo);
243 OC_EXPORT void OCPlatformInfoDestroy(OCPlatformInfo *info);
244 OC_EXPORT void OCPlatformPayloadDestroy(OCPlatformPayload* payload);
245
246 // Presence Payload
247 OC_EXPORT OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
248         OCPresenceTrigger trigger, const char* resourceType);
249 OC_EXPORT void OCPresencePayloadDestroy(OCPresencePayload* payload);
250
251 // Helper API
252 OC_EXPORT OCStringLL* CloneOCStringLL (OCStringLL* ll);
253 OC_EXPORT void OCFreeOCStringLL(OCStringLL* ll);
254
255 #ifdef __cplusplus
256 }
257 #endif
258
259 #endif