OCMulticast start and stop APIs
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caconnectivitymanager.c
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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <stdbool.h>
25
26 #include "cainterface.h"
27 #include "caremotehandler.h"
28 #include "camessagehandler.h"
29 #include "caprotocolmessage.h"
30 #include "canetworkconfigurator.h"
31 #include "cainterfacecontroller.h"
32 #include "logger.h"
33 #ifdef __WITH_DTLS__
34 #include "caadapternetdtls.h"
35 #endif
36
37 #ifdef TCP_ADAPTER
38 #include "catcpadapter.h"
39 #endif
40
41 CAGlobals_t caglobals = { 0 };
42
43 #define TAG "CA_CONN_MGR"
44
45 static bool g_isInitialized = false;
46
47 #ifdef __WITH_DTLS__
48 // CAAdapterNetDTLS will register the callback.
49 // Taking callback all the way through adapters not the right approach, hence calling here.
50 extern void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
51 #endif
52
53 CAResult_t CAInitialize()
54 {
55     OIC_LOG(DEBUG, TAG, "CAInitialize");
56
57     if (!g_isInitialized)
58     {
59         CAResult_t res = CAInitializeMessageHandler();
60         if (res != CA_STATUS_OK)
61         {
62             OIC_LOG(ERROR, TAG, "CAInitialize has failed");
63             return res;
64         }
65         g_isInitialized = true;
66     }
67     return CA_STATUS_OK;
68 }
69
70 void CATerminate()
71 {
72     OIC_LOG(DEBUG, TAG, "CATerminate");
73
74     if (g_isInitialized)
75     {
76         CATerminateMessageHandler();
77         CATerminateNetworkType();
78
79         g_isInitialized = false;
80     }
81 }
82
83 CAResult_t CAStartListeningServer()
84 {
85     OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
86
87     if(!g_isInitialized)
88     {
89         return CA_STATUS_NOT_INITIALIZED;
90     }
91
92     return CAStartListeningServerAdapters();
93 }
94
95 CAResult_t CAStopListeningServer()
96 {
97     OIC_LOG(DEBUG, TAG, "CAStopListeningServer");
98
99     if(!g_isInitialized)
100     {
101         return CA_STATUS_NOT_INITIALIZED;
102     }
103
104     return CAStopListeningServerAdapters();
105 }
106
107 CAResult_t CAStartDiscoveryServer()
108 {
109     OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
110
111     if(!g_isInitialized)
112     {
113         return CA_STATUS_NOT_INITIALIZED;
114     }
115
116     return CAStartDiscoveryServerAdapters();
117 }
118
119 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
120                        CAErrorCallback ErrorHandler)
121 {
122     OIC_LOG(DEBUG, TAG, "CARegisterHandler");
123
124     if(!g_isInitialized)
125     {
126         OIC_LOG(DEBUG, TAG, "CA is not initialized");
127         return;
128     }
129
130     CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
131 }
132
133 #ifdef __WITH_DTLS__
134 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentialsHandler)
135 {
136     OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
137
138     if(!g_isInitialized)
139     {
140         return CA_STATUS_NOT_INITIALIZED;
141     }
142
143     CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
144     return CA_STATUS_OK;
145 }
146 #endif //__WITH_DTLS__
147
148 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
149                             CATransportAdapter_t adapter,
150                             const char *addr,
151                             uint16_t port,
152                             CAEndpoint_t **object)
153 {
154     if (!object)
155     {
156         OIC_LOG(ERROR, TAG, "Invalid Parameter");
157         return CA_STATUS_INVALID_PARAM;
158     }
159
160     CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
161     if (!endpoint)
162     {
163         return CA_STATUS_FAILED;
164     }
165     *object = endpoint;
166     return CA_STATUS_OK;
167 }
168
169 void CADestroyEndpoint(CAEndpoint_t *rep)
170 {
171     OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
172
173     CAFreeEndpoint(rep);
174 }
175
176 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
177 {
178     OIC_LOG(DEBUG, TAG, "CAGenerateToken");
179
180     return CAGenerateTokenInternal(token, tokenLength);
181 }
182
183 void CADestroyToken(CAToken_t token)
184 {
185     OIC_LOG(DEBUG, TAG, "CADestroyToken");
186
187     CADestroyTokenInternal(token);
188
189     OIC_LOG(DEBUG, TAG, "OUT");
190 }
191
192 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
193 {
194     OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
195
196     if(!g_isInitialized)
197     {
198         return CA_STATUS_NOT_INITIALIZED;
199     }
200
201     return CAGetNetworkInformationInternal(info, size);
202 }
203
204 CAResult_t CASendRequest(const CAEndpoint_t *object,const CARequestInfo_t *requestInfo)
205 {
206     OIC_LOG(DEBUG, TAG, "CASendGetRequest");
207
208     if(!g_isInitialized)
209     {
210         return CA_STATUS_NOT_INITIALIZED;
211     }
212
213     return CADetachRequestMessage(object, requestInfo);
214 }
215
216 CAResult_t CASendNotification(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
217 {
218     OIC_LOG(DEBUG, TAG, "CASendNotification");
219
220     if(!g_isInitialized)
221     {
222         return CA_STATUS_NOT_INITIALIZED;
223     }
224
225     return CADetachResponseMessage(object, responseInfo);
226 }
227
228 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
229 {
230     OIC_LOG(DEBUG, TAG, "CASendResponse");
231
232     if(!g_isInitialized)
233     {
234         return CA_STATUS_NOT_INITIALIZED;
235     }
236
237     return CADetachResponseMessage(object, responseInfo);
238 }
239
240 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
241 {
242     OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
243
244     if(!g_isInitialized)
245     {
246         return CA_STATUS_NOT_INITIALIZED;
247     }
248
249     CAResult_t res = CA_STATUS_OK;
250
251     if (interestedNetwork & CA_ADAPTER_IP)
252     {
253         res = CAAddNetworkType(CA_ADAPTER_IP);
254         OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
255     }
256     else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
257     {
258         res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
259         OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
260     }
261     else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
262     {
263         res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
264         OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
265     }
266
267 #ifdef RA_ADAPTER
268     else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
269     {
270         res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
271         OIC_LOG_V(DEBUG, TAG,
272                   "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
273     }
274 #endif
275
276 #ifdef TCP_ADAPTER
277     else if (interestedNetwork & CA_ADAPTER_TCP)
278     {
279         res = CAAddNetworkType(CA_ADAPTER_TCP);
280         OIC_LOG_V(DEBUG, TAG,
281                   "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
282     }
283 #endif
284
285     else
286     {
287         res = CA_NOT_SUPPORTED;
288     }
289     return res;
290 }
291
292 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
293 {
294     OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
295
296     if(!g_isInitialized)
297     {
298         return CA_STATUS_NOT_INITIALIZED;
299     }
300
301     CAResult_t res = CA_STATUS_OK;
302
303     if (nonInterestedNetwork & CA_ADAPTER_IP)
304     {
305         res = CARemoveNetworkType(CA_ADAPTER_IP);
306         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
307     }
308     else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
309     {
310         res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
311         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
312     }
313     else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
314     {
315         res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
316         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
317     }
318 #ifdef RA_ADAPTER
319     else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
320     {
321         res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
322         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
323                   res);
324     }
325 #endif
326
327
328 #ifdef TCP_ADAPTER
329     else if (nonInterestedNetwork & CA_ADAPTER_TCP)
330     {
331         res = CARemoveNetworkType(CA_ADAPTER_TCP);
332         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
333                   res);
334     }
335 #endif
336
337     else
338     {
339         res = CA_STATUS_FAILED;
340     }
341     return res;
342 }
343
344 CAResult_t CAHandleRequestResponse()
345 {
346     if (!g_isInitialized)
347     {
348         OIC_LOG(ERROR, TAG, "not initialized");
349         return CA_STATUS_NOT_INITIALIZED;
350     }
351
352     CAHandleRequestResponseCallbacks();
353
354     return CA_STATUS_OK;
355 }
356
357 #ifdef __WITH_DTLS__
358
359 CAResult_t CASelectCipherSuite(const uint16_t cipher)
360 {
361     OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
362
363     return CADtlsSelectCipherSuite(cipher);
364 }
365
366 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
367 {
368     OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
369
370     return CADtlsEnableAnonECDHCipherSuite(enable);
371 }
372
373 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
374                     const uint8_t* label, const size_t labelLen,
375                     const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
376                     const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
377                     uint8_t* ownerPSK, const size_t ownerPSKSize)
378 {
379     OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
380
381     CAResult_t res = CA_STATUS_OK;
382
383     //newOwnerLabel and prevOwnerLabe can be NULL
384     if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
385     {
386         return CA_STATUS_INVALID_PARAM;
387     }
388
389     res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
390                                   rsrcServerDeviceID, rsrcServerDeviceIDLen,
391                                   provServerDeviceID, provServerDeviceIDLen,
392                                   ownerPSK, ownerPSKSize);
393     if (CA_STATUS_OK != res)
394     {
395         OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
396     }
397
398     OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
399
400     return res;
401 }
402
403 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
404 {
405     OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
406     CAResult_t res = CA_STATUS_OK;
407
408     if (!endpoint)
409     {
410         return CA_STATUS_INVALID_PARAM;
411     }
412
413     res = CADtlsInitiateHandshake(endpoint);
414     if (CA_STATUS_OK != res)
415     {
416         OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
417     }
418
419     OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
420
421     return res;
422 }
423
424 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
425 {
426     OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
427     CAResult_t res = CA_STATUS_OK;
428
429     if (!endpoint)
430     {
431         return CA_STATUS_INVALID_PARAM;
432     }
433
434     res = CADtlsClose(endpoint);
435     if (CA_STATUS_OK != res)
436     {
437         OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
438     }
439
440     OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
441
442     return res;
443 }
444
445 #endif /* __WITH_DTLS__ */