OCMulticast start and stop APIs
[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  * Callback function type for request delivery.
46  * @param[out]   object       Endpoint object from which the request is received.
47  *                            It contains endpoint address based on the connectivity type.
48  * @param[out]   requestInfo  Info for resource model to understand about the request.
49  */
50 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
51                                   const CARequestInfo_t *requestInfo);
52
53 /**
54  * Callback function type for response delivery.
55  * @param[out]   object           Endpoint object from which the response is received.
56  * @param[out]   responseInfo     Identifier which needs to be mapped with response.
57  */
58 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
59                                    const CAResponseInfo_t *responseInfo);
60 /**
61  * Callback function type for error.
62  * @param[out]   object           remote device information.
63  * @param[out]   errorInfo        CA Error information.
64  */
65 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
66                                 const CAErrorInfo_t *errorInfo);
67
68 #ifdef __WITH_DTLS__
69
70 /**
71  * Binary blob containing device identity and the credentials for all devices
72  * trusted by this device.
73  */
74 typedef struct
75 {
76    unsigned char identity[DTLS_PSK_ID_LEN]; /** identity of self. */
77    uint32_t num;                            /** number of credentials in this blob. */
78    OCDtlsPskCreds *creds;                   /** list of credentials. Size of this
79                                                 array is determined by 'num' variable. */
80 } CADtlsPskCredsBlob_t;
81
82 /**
83  * Callback function type for getting DTLS credentials.
84  * @param[out]   credInfo     DTLS credentials info. Handler has to allocate new memory for.
85  *                            both credInfo and credInfo->creds which is then freed by CA.
86  */
87 typedef void (*CAGetDTLSCredentialsHandler)(CADtlsPskCredsBlob_t **credInfo);
88 #endif //__WITH_DTLS__
89
90 /**
91  * Initialize the connectivity abstraction module.
92  * It will initialize adapters, thread pool and other modules based on the platform
93  * compilation options.
94  *
95  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_MEMORY_ALLOC_FAILED
96  */
97 CAResult_t CAInitialize();
98
99 /**
100  * Terminate the connectivity abstraction module.
101  * All threads, data structures are destroyed for next initializations.
102  */
103 void CATerminate();
104
105 /**
106  * Starts listening servers.
107  * This API is used by resource hosting server for listening multicast requests.
108  * Based on the adapters configurations, different kinds of servers are started.
109  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED
110  */
111 CAResult_t CAStartListeningServer();
112
113 /**
114  * Stops the server from receiving the multicast traffic. This is used by sleeping
115  * device to not receives the multicast traffic.
116  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED
117  */
118 CAResult_t CAStopListeningServer();
119
120 /**
121  * Starts discovery servers.
122  * This API is used by resource required clients for listening multicast requests.
123  * Based on the adapters configurations, different kinds of servers are started.
124  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED
125  */
126 CAResult_t CAStartDiscoveryServer();
127
128 /**
129  * Register request callbacks and response callbacks.
130  *          Requests and responses are delivered these callbacks.
131  * @param[in]   ReqHandler    Request callback ( for GET,PUT ..etc).
132  * @param[in]   RespHandler   Response Handler Callback.
133  * @see     CARequestCallback
134  * @see     CAResponseCallback
135  * @see     CAErrorCallback
136  */
137 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
138                        CAErrorCallback ErrorHandler);
139
140 #ifdef __WITH_DTLS__
141 /**
142  * Register callback to get DTLS PSK credentials.
143  * @param[in]   GetDTLSCredentials    GetDTLS Credetials callback.
144  * @return  ::CA_STATUS_OK
145  */
146 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentials);
147 #endif //__WITH_DTLS__
148
149 /**
150  * Create an endpoint description.
151  * @param[in]   flags                 how the adapter should be used.
152  * @param[in]   adapter               which adapter to use.
153  * @param[in]   addr                  string representation of address.
154  * @param[in]   port                  port (for IP_ADAPTER).
155  * @param[in]   endpoint              Endpoint which contains the above.
156  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED
157  * @remark  The created Remote endpoint can be freed using CADestroyEndpoint().
158  * @see     CADestroyEndpoint
159  */
160 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
161                             CATransportAdapter_t adapter,
162                             const char *addr,
163                             uint16_t port,
164                             CAEndpoint_t **object);
165
166 /**
167  * Destroy the remote endpoint created.
168  * @param[in]   object   Remote Endpoint object created with CACreateEndpoint.
169  */
170 void CADestroyEndpoint(CAEndpoint_t *object);
171
172 /**
173  * Generating the token for matching the request and response.
174  * @param[in]   token            Token for the request.
175  * @param[in]   tokenLength      length of the token.
176  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or
177  *          ::CA_MEMORY_ALLOC_FAILED 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  * Destroy the token generated by CAGenerateToken.
185  * @param[in]   token    token to be freed.
186  */
187 void CADestroyToken(CAToken_t token);
188
189 /**
190  * Send control Request on a resource.
191  * @param[in]   object       Endpoint where the payload need to be sent.
192  *                           This endpoint is delivered with Request or response callback.
193  * @param[in]   requestInfo  Information for the request.
194  * @return  ::CA_STATUS_OK ::CA_STATUS_FAILED ::CA_MEMORY_ALLOC_FAILED
195  */
196 CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo);
197
198 /**
199  * Send the response.
200  * @param[in]   object           Endpoint where the payload need to be sent.
201  *                               This endpoint is delivered with Request or response callback.
202  * @param[in]   responseInfo     Information for the response.
203  * @return  ::CA_STATUS_OK or  ::CA_STATUS_FAILED or ::CA_MEMORY_ALLOC_FAILED
204  */
205 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo);
206
207 /**
208  * Send notification to the remote object.
209  * @param[in]   object           Endpoint where the payload need to be sent.
210  *                               This endpoint is delivered with Request or response callback.
211  * @param[in]   responseInfo     Information for the response.
212  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_MEMORY_ALLOC_FAILED
213  */
214 CAResult_t CASendNotification(const CAEndpoint_t *object,
215                       const  CAResponseInfo_t *responseInfo);
216
217 /**
218  * Select network to use.
219  * @param[in]   interestedNetwork    Connectivity Type enum.
220  * @return  ::CA_STATUS_OK or ::CA_NOT_SUPPORTED or
221  *          ::CA_STATUS_FAILED or ::CA_NOT_SUPPORTED
222  */
223 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork);
224
225 /**
226  * Select network to unuse.
227  * @param[in]   nonInterestedNetwork     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  * Get network information.
234  * It should be destroyed by the caller as it Get Information.
235  * @param[out]   info     LocalConnectivity objects
236  * @param[out]   size     No Of Array objects
237  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or
238  *          ::CA_STATUS_INVALID_PARAM or ::CA_MEMORY_ALLOC_FAILED
239  */
240 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size);
241
242 /**
243  * To Handle the Request or Response.
244  * @return   ::CA_STATUS_OK
245  */
246 CAResult_t CAHandleRequestResponse();
247
248 #ifdef RA_ADAPTER
249 /**
250  * Set Remote Access information for XMPP Client.
251  * @param[in]   caraInfo          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 arguments.
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