replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / include / srmutility.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 IOTVT_SRM_UTILITY_H
22 #define IOTVT_SRM_UTILITY_H
23
24 #include "ocstack.h"
25 #if defined (__TIZENRT__)
26 #include <apps/netutils/cJSON.h>
27 #else
28 #include "cJSON.h"
29 #endif
30 #include "securevirtualresourcetypes.h"
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #endif
35 #include <coap/uri.h>
36 #ifdef __cplusplus
37 }
38 #endif
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif // __cplusplus
43
44 typedef struct OicParseQueryIter OicParseQueryIter_t;
45
46 /**
47  * OicRestQueryIter data structure is used for book-keeping
48  * sub-REST query's attribute's and value's, starting location &
49  * length between calls to GetNextQuery(). This struct needs
50  * to be first initialized with ParseQueryIterInit().
51  *
52  */
53 struct OicParseQueryIter
54 {
55     unsigned char * attrPos;    /**< stating location of attribute. */
56     size_t attrLen;             /**< length of the attribute. */
57     unsigned char * valPos;     /**< starting location of value. */
58     size_t valLen;              /**< length of the value. */
59     coap_parse_iterator_t pi;   /**< coap struct for tokenizing the query.*/
60 };
61
62 typedef enum OicSecOtmEvent{
63     OIC_OTM_READY = 0,
64     OIC_OTM_STARTED = 1,
65     OIC_OTM_DONE = 2,
66     OIC_OTM_ERROR = 3
67 }OicSecOtmEvent_t;
68
69 /**
70  * Callback function to recevie the OTM event on server side.
71  *
72  * @param addr PT's address (address can be NULL in case of init state)
73  * @param port PT's port (It is meaningless in case of init state & BLE)
74  * @param uuid PT's UUID (UUID can be NULL in case of init state & coap reqest)
75  * @param event OTM state (@ref OicSecOtmEvent_t)
76  */
77 typedef void (*OicSecOtmEventHandler_t)(const char* addr, uint16_t port,
78                                         const char* uuid, int event);
79
80 /**
81  * Macro to verify success of operation.
82  * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR);
83  * @note Invoking function must define "exit:" label for goto functionality to work correctly.
84  */
85 #define VERIFY_SUCCESS(tag, op, logLevel) do{ if (!(op)) \
86             {OIC_LOG((logLevel), tag, #op " failed!!"); goto exit; } }while(0)
87
88 /**
89  * Macro to verify argument is not equal to NULL.
90  * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR);
91  * @note Invoking function must define "exit:" label for goto functionality to work correctly.
92  */
93 #define VERIFY_NON_NULL(tag, arg, logLevel) do{ if (NULL == (arg)) \
94             { OIC_LOG((logLevel), tag, #arg " is NULL"); goto exit; } }while(0)
95
96 /**
97  * This method initializes the @ref OicParseQueryIter_t struct.
98  *
99  * @param query is the REST query, to be parsed.
100  * @param parseIter is the @ref OicParseQueryIter_t struct, to be initialized based on the query.
101  */
102 void ParseQueryIterInit(const unsigned char * query, OicParseQueryIter_t * parseIter);
103
104 /**
105  * This method fills the @ref OicParseQueryIter_t struct with next REST query's
106  * attribute's and value's information.
107  *
108  * @param parseIter is the @ref OicParseQueryIter_t struct, has next query's attribute's
109  *  & value's info.
110  *
111  * @return reference to the @ref OicParseQueryIter_t if it has parsed query info, else
112  * NULL if it has no query to parse.
113  */
114 OicParseQueryIter_t * GetNextQuery(OicParseQueryIter_t * parseIter);
115
116 /**
117  * This method acts as a helper function for JSON unmarshalling by various SVR's.
118  *
119  * @param jsonRoot point to the root JSON node containing the OicUuid array.
120  * @param arrayItem is the name of the JSON OicUuid array item.
121  * @param numUuids is the pointer to the number of OicUuid's available in JSON array.
122  * @param uuids is the pointer to the array of OicUuid's.
123  *
124  * @return ::OC_STACK_OK on success, some other value upon failure.
125  */
126 OCStackResult AddUuidArray(const cJSON* jsonRoot, const char* arrayItem,
127                            size_t *numUuids, OicUuid_t** uuids);
128
129 /**
130  * Function to getting string of ownership transfer method
131  *
132  * @prarm oxmType ownership transfer method
133  *
134  * @return string value of ownership transfer method
135  */
136 const char* GetOxmString(OicSecOxm_t oxmType);
137
138 /*
139  * This method converts UUID to canonical format string.
140  *
141  * @param uuid Device UUID
142  * @param strUuid converted UUID in canonical format
143  * @return OC_STACK_OK for success.
144  *
145  * @note Caller needs to invoke OICFree after done using the return pointer
146  */
147 OCStackResult ConvertUuidToStr(const OicUuid_t* uuid, char** strUuid);
148
149
150 /*
151  * This method converts string UUID to OicUuid_t.
152  *
153  * @param strUuid Device UUID in string format
154  * @param uuid converted UUID in OicUuid_t format
155  * @return OC_STACK_OK for success.
156  *
157  */
158 OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid);
159
160
161 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
162 /**
163  * API to save the seed value to generate device UUID.
164  *
165  * @param seed buffer of seed value.
166  * @param seedSize byte length of seed
167  *
168  * @return ::OC_STACK_OK for Success, otherwise some error value.
169  */
170 OCStackResult SetDeviceIdSeed(const uint8_t* seed, size_t seedSize);
171
172 /**
173  * API to register OTM event handler
174  *
175  * @param otmEventHandler implementation of OTM event handler (@ref OicSecOtmEventHandler_t)
176  */
177 void SetOtmEventHandler(OicSecOtmEventHandler_t otmEventHandler);
178
179 /**
180  * Invoke OTM event handler to notify the OTM state.
181  *
182  * @param addr PT's address (address can be NULL in case of init state)
183  * @param port PT's port (It is meaningless in case of init state & BLE)
184  * @param uuid PT's UUID (UUID can be NULL in case of init state & coap reqest)
185  * @param event OTM state (@ref OicSecOtmEvent_t)
186  * @param result OTM result code
187  */
188 void InvokeOtmEventHandler(const char* addr, uint16_t port,
189                            const OicUuid_t* uuid, OicSecOtmEvent_t event);
190 #endif
191
192 #ifdef __cplusplus
193 }
194 #endif // __cplusplus
195
196 #endif //IOTVT_SRM_UTILITY_H