Imported Upstream version 0.9.2
[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 CAEndpoint_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 CAEndpoint_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 CAEndpoint_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 an endpoint description
150  * @param   flags               [IN]  how the adapter should be used
151  * @param   adapter             [IN]  which adapter to use
152  * @param   addr                [IN]  string representation of address
153  * @param   port                [IN]  port (for IP_ADAPTER)
154  * @param   endpoint            [OUT] Endpoint which contains the above
155  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED
156  * @remark  The created Remote endpoint can be freed using CADestroyEndpoint().
157  * @see     CADestroyEndpoint
158  */
159 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
160                             CATransportAdapter_t adapter,
161                             const char *addr,
162                             uint16_t port,
163                             CAEndpoint_t **object);
164
165 /**
166  * @brief   Destroy the remote endpoint created
167  * @param   object  [IN] Remote Endpoint object created with CACreateEndpoint
168  * @return  NONE
169  */
170 void CADestroyEndpoint(CAEndpoint_t *object);
171
172 /**
173  * @brief   Generating the token for matching the request and response.
174  * @param   token          [OUT] Token for the request
175  * @param   tokenLength    [IN]  length of the token
176  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
177  *          or #CA_STATUS_NOT_INITIALIZED
178  * @remark  Token memory is destroyed by the caller using CADestroyToken().
179  * @see     CADestroyToken
180  */
181 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength);
182
183 /**
184  * @brief   Destroy the token generated by CAGenerateToken
185  * @param   token   [IN] token to be freed
186  * @return  NONE
187  */
188 void CADestroyToken(CAToken_t token);
189
190 /**
191  * @brief   Send control Request on a resource
192  * @param   object      [IN] Endpoint where the payload need to be sent.
193  *                           This endpoint is delivered with Request or response callback.
194  * @param   requestInfo [IN] Information for the request.
195  * @return  #CA_STATUS_OK #CA_STATUS_FAILED #CA_MEMORY_ALLOC_FAILED
196  */
197 CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo);
198
199 /**
200  * @brief   Send the response
201  * @param   object          [IN] Endpoint where the payload need to be sent.
202  *                               This endpoint is delivered with Request or response callback
203  * @param   responseInfo    [IN] Information for the response
204  * @return  #CA_STATUS_OK or  #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
205  */
206 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo);
207
208 /**
209  * @brief   Send notification to the remote object
210  * @param   object          [IN] Endpoint where the payload need to be sent.
211  *                               This endpoint is delivered with Request or response callback.
212  * @param   responseInfo    [IN] Information for the response.
213  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED
214  */
215 CAResult_t CASendNotification(const CAEndpoint_t *object,
216                       const  CAResponseInfo_t *responseInfo);
217
218 /**
219  * @brief   Select network to use
220  * @param   interestedNetwork   [IN] Connectivity Type enum
221  * @return  #CA_STATUS_OK or #CA_NOT_SUPPORTED or #CA_STATUS_FAILED or #CA_NOT_SUPPORTED
222  */
223 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork);
224
225 /**
226  * @brief   Select network to unuse
227  * @param   nonInterestedNetwork    [IN] Connectivity Type enum
228  * @return  #CA_STATUS_OK or #CA_NOT_SUPPORTED or #CA_STATUS_FAILED
229  */
230 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork);
231
232 /**
233  * @brief   Get network information
234  *          It should be destroyed by the caller as it Get Information.
235  * @param   info    [OUT] LocalConnectivity objects
236  * @param   size    [OUT] No Of Array objects
237  * @return  #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_STATUS_INVALID_PARAM or
238 *                #CA_MEMORY_ALLOC_FAILED
239  */
240 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size);
241
242 /**
243  * @brief    To Handle the Request or Response
244  * @return   #CA_STATUS_OK
245  */
246 CAResult_t CAHandleRequestResponse();
247
248 #ifdef RA_ADAPTER
249 /**
250  * @brief   Set Remote Access information for XMPP Client.
251  * @param   caraInfo          [IN] remote access info.
252  *
253  * @return  #CA_STATUS_OK
254  */
255 CAResult_t CASetRAInfo(const CARAInfo_t *caraInfo);
256 #endif
257
258
259 #ifdef __WITH_DTLS__
260
261 /**
262  * Select the cipher suite for dtls handshake
263  *
264  * @param[IN] cipher  cipher suite (Note : Make sure endianness)
265  *                               0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA
266  *                               0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
267  *                               0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
268  *
269  * @retval  CA_STATUS_OK    Successful
270  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
271  * @retval  CA_STATUS_FAILED Operation failed
272  */
273 CAResult_t CASelectCipherSuite(const uint16_t cipher);
274
275 /**
276  * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls
277  *
278  * @param[IN] enable  TRUE/FALSE enables/disables anonymous cipher suite
279  *
280  * @retval  CA_STATUS_OK    Successful
281  * @retval  CA_STATUS_FAILED Operation failed
282  *
283  * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
284  */
285 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
286
287
288 /**
289  * Generate ownerPSK using PRF
290  * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
291  *                                    'ID of new device(Resource Server)',
292  *                                    'ID of owner smart-phone(Provisioning Server)')
293  *
294  * @param[IN] endpoint  information of network address
295  * @param[IN] label  Ownership transfer method e.g)"oic.sec.doxm.jw"
296  * @param[IN] labelLen  Byte length of label
297  * @param[IN] rsrcServerDeviceID  ID of new device(Resource Server)
298  * @param[IN] rsrcServerDeviceIDLen  Byte length of rsrcServerDeviceID
299  * @param[IN] provServerDeviceID  label of previous owner
300  * @param[IN] provServerDeviceIDLen  byte length of provServerDeviceID
301  * @param[IN,OUT] ownerPSK  Output buffer for owner PSK
302  * @param[IN] ownerPSKSize  Byte length of the ownerPSK to be generated
303  *
304  * @retval  CA_STATUS_OK    Successful
305  * @retval  CA_STATUS_FAILED Operation failed
306  */
307 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t *endpoint,
308                               const uint8_t* label, const size_t labelLen,
309                               const uint8_t* rsrcServerDeviceID,
310                               const size_t rsrcServerDeviceIDLen,
311                               const uint8_t* provServerDeviceID,
312                               const size_t provServerDeviceIDLen,
313                               uint8_t* ownerPSK, const size_t ownerPSKSize);
314
315 /**
316  * Initiate DTLS handshake with selected cipher suite
317  *
318  * @param[IN] endpoint  information of network address
319  *
320  * @retval  CA_STATUS_OK    Successful
321  * @retval  CA_STATUS_FAILED Operation failed
322  */
323 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint);
324
325 /**
326  * Close the DTLS session
327  *
328  * @param[IN] endpoint  information of network address
329  *
330  * @retval  CA_STATUS_OK    Successful
331  * @retval  CA_STATUS_FAILED Operation failed
332  */
333 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint);
334
335 #endif /* __WITH_DTLS__ */
336
337 #ifdef __cplusplus
338 } /* extern "C" */
339 #endif
340
341 #endif /* CA_INTERFACE_H_ */
342