4a6b7df1ac49b9616e6480e8235f4de6753dfee2
[platform/upstream/iotivity.git] / service / resource-directory / include / rd_payload.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 _RESOURCE_DIRECTORY_PAYLOAD_H_
22 #define _RESOURCE_DIRECTORY_PAYLOAD_H_
23
24 #include <cbor.h>
25
26 #include "rd_types.h"
27 #include "logger.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif // __cplusplus
32
33 /**
34  * Converts RD payload from structure to CBOR format. It creates the outPayload
35  * which is then transmitted over the wire.
36  *
37  * @param rdPayload Contains structure holding values of OCRDPayload.
38  * @param outPayload The payload in the CBOR format converting OCRDPayload
39  * structure.
40  * @param size Length of the payload.
41  *
42  * @return ::OC_STACK_OK returns if successful and OC_STACK_ERROR returns if
43  * failed in creating CBOR.
44  */
45 OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload,
46                                 uint8_t *outPayload, size_t *size);
47
48 /**
49  * Converts CBOR to OCRDPayload.
50  *
51  * @param rdCBORPayload Payload received from other end in CBOR format.
52  * @param outPayload Parsing the values from CBOR into OCRDPayload structure.
53  *
54  * @return ::OC_STACK_OK returns if successful and OC_STACK_ERROR returns if
55  * failed in parsing CBOR.
56  */
57 OCStackResult OCRDCborToPayload(const CborValue *rdCBORPayload, OCPayload **outPayload);
58
59 /**
60  * Initializes RD payload structure.
61  *
62  * @param payloadType Defines whether payload is RD_PAYLOAD_TYPE_DISCOVERY or
63  * RD_PAYLOAD_TYPE_PUBLISH.
64  *
65  * @return Allocated memory for the OCRDPayload and NULL in case if failed to
66  * allocate memory
67  */
68 OCRDPayload *OCRDPayloadCreate(OCRDPayloadType payloadType);
69
70 /**
71  * Initializes RD Discovery payload structure and sets the bias factor.
72  *
73  * @param biasFactor Value specifies the selection factor. It is weigthage of
74  * CPU, Memory, Network, Load and Power capability of the RD server.
75  *
76  * @return Allocated memory for the OCRDDiscoveryPayload and NULL in case if
77  * failed to allocate memory.
78  */
79 OCRDDiscoveryPayload *OCRDDiscoveryPayloadCreate(int biasFactor);
80
81 /**
82  * Creates the links payload, which is then embedded inside OCRDPublishPayload.
83  *
84  * @param uri  The Web Link Address of the resource.
85  * @param rt   The resource type of the published resource.
86  * @param itf  The interface type of the published resource.
87  * @param linkPayload The address of the allocated memory or NULL in case if failed
88  *                    to allocate memory.
89  */
90 void OCRDLinksPayloadCreate(const char *uri, const char *rt, const char *itf,
91         OCRDLinksPayload **linkPayload);
92
93 /**
94  * Creates the links payload, which is then embedded inside OCRDPublishPayload.
95  *
96  * @param ttl  Time to live of the published resource..
97  * @param linkPayload  The link payload with uri, rt and itf.
98  *
99  * @return  Allocated memory of OCRDPublishPayload or NULL in case if failed
100  *          to allocate memory.
101  */
102 OCRDPublishPayload *OCRDPublishPayloadCreate(const int ttl,
103         OCRDLinksPayload *linkPayload);
104
105 /**
106  * Free memory allocation of the RDPayload and its internal structure.
107  *
108  * @param payload Pointer to already allocated memory for OCRDPayload.
109  */
110 void OCRDPayloadDestroy(OCRDPayload *payload);
111
112 /**
113  * Logs the content of the OCRDPayload.
114  *
115  * @param level Log level DEBUG or INFO or ERROR.
116  * @param tag File specific tag to use.
117  * @param payload Pointer to already allocated memory for OCRDPayload.
118  */
119 void OCRDPayloadLog(LogLevel level, const char *tag, const OCRDPayload *payload);
120
121 /**
122  * Logs the subset of the OCRDPayload, prints separately OCRDPublish.
123  *
124  * @param level Log level DEBUG or INFO or ERROR.
125  * @param tag File specific tag to use.
126  * @param payload Pointer to already allocated memory for OCRDPublish.
127  */
128 void OCRDPublishPayloadLog(LogLevel level, const char *tag,
129         const OCRDPublishPayload *payload);
130
131 #ifdef __cplusplus
132 }
133 #endif // __cplusplus
134
135 #endif