summaryrefslogtreecommitdiff
path: root/resource/csdk/security/include/srmutility.h
blob: a48d322b4f7c142d6c616d2b6057f888b641a330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//******************************************************************
//
// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#ifndef IOTVT_SRM_UTILITY_H
#define IOTVT_SRM_UTILITY_H

#include "ocstack.h"
#if defined (__TIZENRT__)
#include <apps/netutils/cJSON.h>
#else
#include "cJSON.h"
#endif
#include "securevirtualresourcetypes.h"
#ifdef __cplusplus
extern "C"
{
#endif
#include <coap/uri.h>
#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

typedef struct OicParseQueryIter OicParseQueryIter_t;

/**
 * OicRestQueryIter data structure is used for book-keeping
 * sub-REST query's attribute's and value's, starting location &
 * length between calls to GetNextQuery(). This struct needs
 * to be first initialized with ParseQueryIterInit().
 *
 */
struct OicParseQueryIter
{
    unsigned char * attrPos;    /**< stating location of attribute. */
    size_t attrLen;             /**< length of the attribute. */
    unsigned char * valPos;     /**< starting location of value. */
    size_t valLen;              /**< length of the value. */
    coap_parse_iterator_t pi;   /**< coap struct for tokenizing the query.*/
};

typedef enum OicSecOtmEvent{
    OIC_OTM_READY = 0,
    OIC_OTM_STARTED = 1,
    OIC_OTM_DONE = 2,
    OIC_OTM_ERROR = 3
}OicSecOtmEvent_t;

/**
 * Callback function to recevie the OTM event on server side.
 *
 * @param addr PT's address (address can be NULL in case of init state)
 * @param port PT's port (It is meaningless in case of init state & BLE)
 * @param uuid PT's UUID (UUID can be NULL in case of init state & coap reqest)
 * @param event OTM state (@ref OicSecOtmEvent_t)
 */
typedef void (*OicSecOtmEventHandler_t)(const char* addr, uint16_t port,
                                        const char* uuid, int event);

/**
 * Macro to verify success of operation.
 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR);
 * @note Invoking function must define "exit:" label for goto functionality to work correctly.
 */
#define VERIFY_SUCCESS(tag, op, logLevel) do{ if (!(op)) \
            {OIC_LOG((logLevel), tag, #op " failed!!"); goto exit; } }while(0)

/**
 * Macro to verify argument is not equal to NULL.
 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR);
 * @note Invoking function must define "exit:" label for goto functionality to work correctly.
 */
#define VERIFY_NON_NULL(tag, arg, logLevel) do{ if (NULL == (arg)) \
            { OIC_LOG((logLevel), tag, #arg " is NULL"); goto exit; } }while(0)

/**
 * This method initializes the @ref OicParseQueryIter_t struct.
 *
 * @param query is the REST query, to be parsed.
 * @param parseIter is the @ref OicParseQueryIter_t struct, to be initialized based on the query.
 */
void ParseQueryIterInit(const unsigned char * query, OicParseQueryIter_t * parseIter);

/**
 * This method fills the @ref OicParseQueryIter_t struct with next REST query's
 * attribute's and value's information.
 *
 * @param parseIter is the @ref OicParseQueryIter_t struct, has next query's attribute's
 *  & value's info.
 *
 * @return reference to the @ref OicParseQueryIter_t if it has parsed query info, else
 * NULL if it has no query to parse.
 */
OicParseQueryIter_t * GetNextQuery(OicParseQueryIter_t * parseIter);

/**
 * This method acts as a helper function for JSON unmarshalling by various SVR's.
 *
 * @param jsonRoot point to the root JSON node containing the OicUuid array.
 * @param arrayItem is the name of the JSON OicUuid array item.
 * @param numUuids is the pointer to the number of OicUuid's available in JSON array.
 * @param uuids is the pointer to the array of OicUuid's.
 *
 * @return ::OC_STACK_OK on success, some other value upon failure.
 */
OCStackResult AddUuidArray(const cJSON* jsonRoot, const char* arrayItem,
                           size_t *numUuids, OicUuid_t** uuids);

/**
 * Function to getting string of ownership transfer method
 *
 * @prarm oxmType ownership transfer method
 *
 * @return string value of ownership transfer method
 */
const char* GetOxmString(OicSecOxm_t oxmType);

/*
 * This method converts UUID to canonical format string.
 *
 * @param uuid Device UUID
 * @param strUuid converted UUID in canonical format
 * @return OC_STACK_OK for success.
 *
 * @note Caller needs to invoke OICFree after done using the return pointer
 */
OCStackResult ConvertUuidToStr(const OicUuid_t* uuid, char** strUuid);


/*
 * This method converts string UUID to OicUuid_t.
 *
 * @param strUuid Device UUID in string format
 * @param uuid converted UUID in OicUuid_t format
 * @return OC_STACK_OK for success.
 *
 */
OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid);


#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
/**
 * API to save the seed value to generate device UUID.
 *
 * @param seed buffer of seed value.
 * @param seedSize byte length of seed
 *
 * @return ::OC_STACK_OK for Success, otherwise some error value.
 */
OCStackResult SetDeviceIdSeed(const uint8_t* seed, size_t seedSize);

/**
 * API to register OTM event handler
 *
 * @param otmEventHandler implementation of OTM event handler (@ref OicSecOtmEventHandler_t)
 */
void SetOtmEventHandler(OicSecOtmEventHandler_t otmEventHandler);

/**
 * Invoke OTM event handler to notify the OTM state.
 *
 * @param addr PT's address (address can be NULL in case of init state)
 * @param port PT's port (It is meaningless in case of init state & BLE)
 * @param uuid PT's UUID (UUID can be NULL in case of init state & coap reqest)
 * @param event OTM state (@ref OicSecOtmEvent_t)
 * @param result OTM result code
 */
void InvokeOtmEventHandler(const char* addr, uint16_t port,
                           const OicUuid_t* uuid, OicSecOtmEvent_t event);
#endif
/**
 * OicUuid_t to Nil UUID {.id={0000000000000000}}
 *
 * @return true if the OicUuid_t is the Nil UUID
 */
bool IsNilUuid(const OicUuid_t *uuid);

#ifdef __cplusplus
}
#endif // __cplusplus

/**
 * cbor default init
 */
#ifndef OC_DEFAULT_CBOR_VALUE
#   if (__STDC_VERSION__ >= 199901L)
#       define OC_DEFAULT_CBOR_VALUE {.parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0}
#   else
#       define OC_DEFAULT_CBOR_VALUE {NULL, NULL, 0, 0, 0, 0}
#   endif
#endif

#ifndef OC_DEFAULT_CBOR_PARSER
#   if (__STDC_VERSION__ >= 199901L)
#       define OC_DEFAULT_CBOR_PARSER {.end = NULL, .flags = 0}
#   else
#       define OC_DEFAULT_CBOR_PARSER {NULL, 0}
#   endif
#endif

#ifndef OC_DEFAULT_OICUUID
#   if (__STDC_VERSION__ >= 199901L)
#   define OC_DEFAULT_OICUUID {.id= { 0 } }
#   else
#   define OC_DEFAULT_OICUUID {{ 0 }}
#   endif
#endif

#endif //IOTVT_SRM_UTILITY_H