Merging security-M3 to master
[platform/upstream/iotivity.git] / resource / csdk / connectivity / api / cainterface.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 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 /**
22  * @file
23  *
24  * This file contains the APIs for Resource Model to use.
25  */
26
27 #ifndef CA_INTERFACE_H_
28 #define CA_INTERFACE_H_
29
30 /**
31  * Connectivity Abstraction Interface APIs.
32  */
33 #include "cacommon.h"
34
35 #ifdef __WITH_DTLS__
36 #include "ocsecurityconfig.h"
37 #endif
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43
44 /**
45  * @brief   Callback function type for request delivery.
46  * @param   object      [OUT] Endpoint object from which the request is received. It contains
47  *                            endpoint address based on the connectivity type.
48  * @param   requestInfo [OUT] Info for resource model to understand about the request.
49  * @return  NONE
50  */
51 typedef void (*CARequestCallback)(const CARemoteEndpoint_t *object,
52                                   const CARequestInfo_t *requestInfo);
53
54 /**
55  * @brief   Callback function type for response delivery.
56  * @param   object          [OUT] Endpoint object from which the response is received.
57  * @param   responseInfo    [OUT] Identifier which needs to be mapped with response.
58  * @return  NONE
59  */
60 typedef void (*CAResponseCallback)(const CARemoteEndpoint_t *object,
61                                    const CAResponseInfo_t *responseInfo);
62 /**
63  * @brief   Callback function type for error
64  * @param   object          [OUT] remote device information
65  * @param   errorInfo       [OUT] CA Error information
66  * @return  NONE
67  */
68 typedef void (*CAErrorCallback)(const CARemoteEndpoint_t *object,
69                                 const CAErrorInfo_t *errorInfo);
70
71 #ifdef __WITH_DTLS__
72
73 /**
74  * Binary blob containing device identity and the credentials for all devices
75  * trusted by this device.
76  */
77 typedef struct
78 {
79    unsigned char identity[DTLS_PSK_ID_LEN]; /** identity of self */
80    uint32_t num;                            /** number of credentials in this blob */
81    OCDtlsPskCreds *creds;                   /** list of credentials. Size of this
82                                                 array is determined by 'num' variable. */
83 } CADtlsPskCredsBlob_t;
84
85 /**
86  * @brief   Callback function type for getting DTLS credentials.
87  * @param   credInfo          [OUT] DTLS credentials info. Handler has to allocate new memory for
88  *                                  both credInfo and credInfo->creds which is then freed by CA
89  * @return  NONE
90  */
91 typedef void (*CAGetDTLSCredentialsHandler)(CADtlsPskCredsBlob_t **credInfo);
92 #endif //__WITH_DTLS__
93
94 /**
95  * @brief   Initialize the connectivity abstraction module.
96  *          It will initialize adapters, thread pool and other modules based on the platform
97  *          compilation options.
98  *
99  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
100  */
101 CAResult_t CAInitialize();
102
103 /**
104  * @brief   Terminate the connectivity abstraction module.
105  *          All threads, data structures are destroyed for next initializations.
106  * @return  NONE
107  */
108 void CATerminate();
109
110 /**
111  * @brief   Starts listening servers.
112  *          This API is used by resource hosting server for listening multicast requests.
113  *          Based on the adapters configurations, different kinds of servers are started.
114  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED
115  */
116 CAResult_t CAStartListeningServer();
117
118 /**
119  * @brief   Starts discovery servers.
120  *          This API is used by resource required clients for listening multicast requests.
121  *          Based on the adapters configurations, different kinds of servers are started.
122  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED
123  */
124 CAResult_t CAStartDiscoveryServer();
125
126 /**
127  * @brief   Register request callbacks and response callbacks.
128  *          Requests and responses are delivered these callbacks .
129  * @param   ReqHandler   [IN] Request callback ( for GET,PUT ..etc)
130  * @param   RespHandler  [IN] Response Handler Callback
131  * @see     CARequestCallback
132  * @see     CAResponseCallback
133  * @see     CAErrorCallback
134  * @return  NONE
135  */
136 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
137                        CAErrorCallback ErrorHandler);
138
139 #ifdef __WITH_DTLS__
140 /**
141  * @brief   Register callback to get DTLS PSK credentials.
142  * @param   GetDTLSCredentials   [IN] GetDTLS Credetials callback
143  * @return  #CA_STATUS_OK
144  */
145 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentials);
146 #endif //__WITH_DTLS__
147
148 /**
149  * @brief   Create a Remote endpoint if the URI is available already.
150  *          This is a Helper function which can be used before calling
151  *          CASendRequest / CASendNotification.
152  * @param   uri                 [IN]  Absolute URI of the resource to be used to generate the
153  *                                    Remote endpoint
154  *                                    \n For ex : coap://10.11.12.13:4545/resource_uri ( for IP)
155  *                                    \n coap://10:11:12:13:45:45/resource_uri ( for BT)
156  * @param   transportType    [IN]  Transport type of the endpoint
157  * @param   object              [OUT] Endpoint object which contains the above parsed data
158  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED
159  * @remark  The created Remote endpoint can be freed using CADestroyRemoteEndpoint() API.
160  * @see     CADestroyRemoteEndpoint
161  */
162 CAResult_t CACreateRemoteEndpoint(const CAURI_t uri,
163                                   const CATransportType_t transportType,
164                                   CARemoteEndpoint_t **object);
165
166 /**
167  * @brief   Destroy the remote endpoint created
168  * @param   object  [IN] Remote Endpoint object created with CACreateRemoteEndpoint
169  * @return  NONE
170  */
171 void CADestroyRemoteEndpoint(CARemoteEndpoint_t *object);
172
173 /**
174  * @brief   Generating the token for matching the request and response.
175  * @param   token          [OUT] Token for the request
176  * @param   tokenLength    [IN]  length of the token
177  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
178  *          or #CA_STATUS_NOT_INITIALIZED
179  * @remark  Token memory is destroyed by the caller using CADestroyToken().
180  * @see     CADestroyToken
181  */
182 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength);
183
184 /**
185  * @brief   Destroy the token generated by CAGenerateToken
186  * @param   token   [IN] token to be freed
187  * @return  NONE
188  */
189 void CADestroyToken(CAToken_t token);
190
191 /**
192  * @brief   Find the resource in the network. This API internally sends multicast messages on all
193  *          selected connectivity adapters. Responses are delivered via response callbacks.
194  *
195  * @param   resourceUri [IN] Uri to send multicast search request. Must contain only relative
196  *                           path of Uri to be search.
197  * @param   token       [IN] Token for the request
198  * @param   tokenLength [IN]  length of the token
199  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_STATUS_NOT_INITIALIZED
200  */
201 CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token, uint8_t tokenLength);
202
203 /**
204  * @brief   Send control Request on a resource
205  * @param   object      [IN] Remote Endpoint where the payload need to be sent.
206  *                           This Remote endpoint is delivered with Request or response callback.
207  * @param   requestInfo [IN] Information for the request.
208  * @return  #CA_STATUS_OK #CA_STATUS_FAILED #CA_MEMORY_ALLOC_FAILED
209  */
210 CAResult_t CASendRequest(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
211
212 /**
213  * @brief   Send control Request on a resource to multicast group
214  * @param   object      [IN] Group Endpoint where the payload need to be sent.
215  *                           This Remote endpoint is delivered with Request or response callback.
216  * @param   requestInfo [IN] Information for the request.
217  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
218  */
219 CAResult_t CASendRequestToAll(const CAGroupEndpoint_t *object,
220                               const CARequestInfo_t *requestInfo);
221
222 /**
223  * @brief   Send the response
224  * @param   object          [IN] Remote Endpoint where the payload need to be sent.
225  *                               This Remote endpoint is delivered with Request or response callback
226  * @param   responseInfo    [IN] Information for the response
227  * @return  #CA_STATUS_OK or  #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
228  */
229 CAResult_t CASendResponse(const CARemoteEndpoint_t *object,
230                 const CAResponseInfo_t *responseInfo);
231
232 /**
233  * @brief   Send notification to the remote object
234  * @param   object          [IN] Remote Endpoint where the payload need to be sent.
235  *                               This Remote endpoint is delivered with Request or response callback.
236  * @param   responseInfo    [IN] Information for the response.
237  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
238  */
239 CAResult_t CASendNotification(const CARemoteEndpoint_t *object,
240                       const  CAResponseInfo_t *responseInfo);
241
242 /**
243  * @brief   To advertise the resource
244  * @param   resourceUri [IN] URI to be advertised
245  * @param   token       [IN] Token for the request
246  * @param   tokenLength [IN] length of the token
247  * @param   options     [IN] Header options information
248  * @param   numOptions  [IN] Number of options
249  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or
250  *          #CA_MEMORY_ALLOC_FAILED or #CA_STATUS_NOT_INITIALIZED
251  */
252 CAResult_t CAAdvertiseResource(const CAURI_t resourceUri,const CAToken_t token,
253                                uint8_t tokenLength, const CAHeaderOption_t *options,
254                                const uint8_t numOptions);
255
256 /**
257  * @brief   Select network to use
258  * @param   interestedNetwork   [IN] Connectivity Type enum
259  * @return  #CA_STATUS_OK or #CA_NOT_SUPPORTED or #CA_STATUS_FAILED or #CA_NOT_SUPPORTED
260  */
261 CAResult_t CASelectNetwork(const uint32_t interestedNetwork);
262
263 /**
264  * @brief   Select network to unuse
265  * @param   nonInterestedNetwork    [IN] Connectivity Type enum
266  * @return  #CA_STATUS_OK or #CA_NOT_SUPPORTED or #CA_STATUS_FAILED
267  */
268 CAResult_t CAUnSelectNetwork(const uint32_t nonInterestedNetwork);
269
270 /**
271  * @brief   Get network information
272  *          It should be destroyed by the caller as it Get Information.
273  * @param   info    [OUT] LocalConnectivity objects
274  * @param   size    [OUT] No Of Array objects
275  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_STATUS_INVALID_PARAM or
276 *                #CA_MEMORY_ALLOC_FAILED
277  */
278 CAResult_t CAGetNetworkInformation(CALocalConnectivity_t **info, uint32_t *size);
279
280 /**
281  * @brief    To Handle the Request or Response
282  * @return   #CA_STATUS_OK
283  */
284 CAResult_t CAHandleRequestResponse();
285
286
287 #ifdef __WITH_DTLS__
288
289 /**
290  * Select the cipher suite for dtls handshake
291  *
292  * @param[IN] cipher  cipher suite (Note : Make sure endianness)
293  *                               0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA
294  *                               0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
295  *                               0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
296  *
297  * @retval  CA_STATUS_OK    Successful
298  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
299  * @retval  CA_STATUS_FAILED Operation failed
300  */
301 CAResult_t CASelectCipherSuite(const uint16_t cipher);
302
303 /**
304  * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls
305  *
306  * @param[IN] enable  TRUE/FALSE enables/disables anonymous cipher suite
307  *
308  * @retval  CA_STATUS_OK    Successful
309  * @retval  CA_STATUS_FAILED Operation failed
310  *
311  * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
312  */
313 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
314
315
316 /**
317  * Generate ownerPSK using PRF
318  * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
319  *                                    'ID of new device(Resource Server)',
320  *                                    'ID of owner smart-phone(Provisioning Server)')
321  *
322  * @param[IN] addrInfo  information of network address
323  * @param[IN] transportType  transport type
324  * @param[IN] label  Ownership transfer method e.g)"oic.sec.doxm.jw"
325  * @param[IN] labelLen  Byte length of label
326  * @param[IN] rsrcServerDeviceID  ID of new device(Resource Server)
327  * @param[IN] rsrcServerDeviceIDLen  Byte length of rsrcServerDeviceID
328  * @param[IN] provServerDeviceID  label of previous owner
329  * @param[IN] provServerDeviceIDLen  byte length of provServerDeviceID
330  * @param[IN,OUT] ownerPSK  Output buffer for owner PSK
331  * @param[IN] ownerPSKSize  Byte length of the ownerPSK to be generated
332  *
333  * @retval  CA_STATUS_OK    Successful
334  * @retval  CA_STATUS_FAILED Operation failed
335  */
336 CAResult_t CAGenerateOwnerPSK(const CAAddress_t* addrInfo,
337                               const CATransportType_t transportType,
338                               const uint8_t* label, const size_t labelLen,
339                               const uint8_t* rsrcServerDeviceID,
340                               const size_t rsrcServerDeviceIDLen,
341                               const uint8_t* provServerDeviceID,
342                               const size_t provServerDeviceIDLen,
343                               uint8_t* ownerPSK, const size_t ownerPSKSize);
344
345 /**
346  * Initiate DTLS handshake with selected cipher suite
347  *
348  * @param[IN] addrInfo    information of network address
349  * @param[IN] transportType  transport type
350  *
351  * @retval  CA_STATUS_OK    Successful
352  * @retval  CA_STATUS_FAILED Operation failed
353  */
354 CAResult_t CAInitiateHandshake(const CAAddress_t* addrInfo,
355                                const CATransportType_t transportType);
356
357 /**
358  * Close the DTLS session
359  *
360  * @param[IN] addrInfo       information of network address
361  * @param[IN] transportType  transport type
362  *
363  * @retval  CA_STATUS_OK    Successful
364  * @retval  CA_STATUS_FAILED Operation failed
365  */
366 CAResult_t CACloseDtlsSession(const CAAddress_t* addrInfo,
367                               const CATransportType_t transportType);
368
369
370 #endif /* __WITH_DTLS__ */
371
372 #ifdef __cplusplus
373 } /* extern "C" */
374 #endif
375
376 #endif /* CA_INTERFACE_H_ */
377