RD client features in base layer
[platform/upstream/iotivity.git] / resource / csdk / stack / include / rdpayload.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 OC_RDPAYLOAD_H_
22 #define OC_RDPAYLOAD_H_
23
24 #include <cbor.h>
25 #include "octypes.h"
26 #include "logger.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif // __cplusplus
31
32 #define OIC_RD_PUBLISH_TTL 86400
33
34 #define OIC_RD_DEFAULT_RESOURCE 2
35
36 #define DEFAULT_MESSAGE_TYPE "application/json"
37
38 /**
39  * Converts RD payload from structure to CBOR format. It creates the outPayload
40  * which is then transmitted over the wire.
41  *
42  * @param rdPayload Contains structure holding values of OCRDPayload.
43  * @param outPayload The payload in the CBOR format converting OCRDPayload
44  * structure.
45  * @param size Length of the payload.
46  *
47  * @return ::CborNoError returns if successful and other Cbor error in  case of error.
48  * failed in creating CBOR.
49  */
50 int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size);
51
52 /**
53  * Converts CBOR to OCRDPayload.
54  *
55  * @param rdCBORPayload Payload received from other end in CBOR format.
56  * @param outPayload Parsing the values from CBOR into OCRDPayload structure.
57  *
58  * @return ::OC_STACK_OK returns if successful and OC_STACK_ERROR returns if
59  * failed in parsing CBOR.
60  */
61 OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPayload);
62
63 /**
64  * Initializes RD payload structure.
65  *
66  * @param payloadType Defines whether payload is RD_PAYLOAD_TYPE_DISCOVERY or
67  * RD_PAYLOAD_TYPE_PUBLISH.
68  *
69  * @return Allocated memory for the OCRDPayload and NULL in case if failed to
70  * allocate memory
71  */
72 OCRDPayload *OCRDPayloadCreate();
73
74 #ifdef RD_CLIENT
75 /**
76  * Initializes RD Publish payload structure.
77  *
78  * @param resourceHandles The handle of registered resource.
79  * @param nHandles The number of registered resource handles.
80  * @param ttl Time to live of the published resource.
81  *
82  * @return Allocated memory for the OCRDPayload and NULL in case if
83  * failed to allocate memory.
84  */
85 OCRDPayload *OCRDPublishPayloadCreate(OCResourceHandle resourceHandles[], uint8_t nHandles,
86                                       uint64_t ttl);
87 #endif
88
89 /**
90  * Initializes RD Discovery payload structure and sets the bias factor.
91  *
92  * @param name Name of the discovery device payload.
93  * @param identity Device identity of the discovery device.
94  * @param biasFactor Value specifies the selection factor. It is weigthage of
95  * CPU, Memory, Network, Load and Power capability of the RD server.
96  *
97  * @return Allocated memory for the OCRDDiscoveryPayload and NULL in case if
98  * failed to allocate memory.
99  */
100 OCRDDiscoveryPayload *OCRDDiscoveryPayloadCreate(const char *name, const char *identity, int biasFactor);
101
102 /**
103  * Free memory allocation of the RDPayload and its internal structure.
104  *
105  * @param payload Pointer to already allocated memory for OCRDPayload.
106  */
107 void OCRDPayloadDestroy(OCRDPayload *payload);
108
109 /**
110  * Copies tag paramter to creates OCTagsPayload.
111  *
112  * @param deviceName The device name as set during enrollment.
113  * @param id The device UUID.
114  * @param ttl Time to leave for the . Used only in resource directory.
115  *
116  * @retun Allocated memory for OCTagsPayload or else NULL in case of error.
117  */
118 OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, uint64_t ttl);
119
120 /**
121  * Copies link resource to create LinksPayload.
122  *
123  * @param href URI of the resource
124  * @param rel Relation
125  * @param rt Array of String pointing to resource types.
126  * @param itf Array of String pointing to interface
127  * @param p Whether to observe or not.
128  * @param title Title
129  * @param anchor URI
130  * @param ins Unique value per link.
131  * @param ttl Time to live for this link.
132  * @param mt Media Type
133
134  * @retun Allocated memory for OCLinksResource or else NULL in case of error.
135  */
136 OCLinksPayload* OCCopyLinksResources(const char *href, const char *rel, OCStringLL *rt,
137                                      OCStringLL *itf, uint8_t p, const char *title,
138                                      const char *anchor, uint8_t ins, uint64_t ttl,
139                                      OCStringLL *mt);
140
141 /**
142  * Creates a resource collection object.
143  *
144  * @param tags Pointer pointing to tags payload.
145  * @param links Pointer pointing to links payload.
146  *
147  * @return Memory allocation for OCResourceCollectionPayload, else NULL.
148  */
149 OCResourceCollectionPayload* OCCopyCollectionResource(OCTagsPayload *tags, OCLinksPayload *links);
150
151 /**
152  * Destroys tags payload including internal structure allocated
153  *
154  * @param tags - Allocated memory of the tags payload.
155  */
156 void OCFreeTagsResource(OCTagsPayload *tags);
157
158 /**
159  * Destroys allocated links payload including internal structure allocated.
160  *
161  * @param links - Allocated memory to the links payload.
162  */
163 void OCFreeLinksResource(OCLinksPayload *links);
164
165 /**
166  * ResourceCollection payload destroy. Includes free up tags and links structure.
167  *
168  * @param payload Pointer pointing to allocated memroy of ResourceCollection.
169  */
170 void OCFreeCollectionResource(OCResourceCollectionPayload *payload);
171
172 /**
173  * Discovery collection payload destroy includes internal structure OCResourceCollectionPayload.
174  *
175  * @param payload Pointer pointing to allocated memory of OCDiscoveryPayload.
176  */
177 void OCDiscoveryCollectionPayloadDestroy(OCDiscoveryPayload* payload);
178
179 #ifdef __cplusplus
180 }
181 #endif // __cplusplus
182
183 #endif /* OC_RDPAYLOAD_H_ */