0a03b117e8e494b306ae9c06c499f064972cba38
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / cautilinterface.c
1 /* ****************************************************************
2  *
3  * Copyright 2016 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 "camanagerleinterface.h"
22 #include "cabtpairinginterface.h"
23 #include "cautilinterface.h"
24 #include "cainterfacecontroller.h"
25 #include "cacommon.h"
26 #include "logger.h"
27 #include "caremotehandler.h"
28 #ifdef TCP_ADAPTER
29 #include "catcpadapter.h"
30 #endif
31 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
32 #include "ca_adapter_net_ssl.h"
33 #endif
34
35 #define TAG "OIC_CA_COMMON_UTILS"
36
37 #if defined(__ANDROID__)
38
39 void CASetLEScanUuid(char* uuid, int uuid_type)
40 {
41         OIC_LOG(DEBUG, TAG, "CASetLEScanUuid");
42 #ifdef LE_ADAPTER
43         CASetScanUuid(uuid, uuid_type);
44 #else
45         OIC_LOG(DEBUG, TAG, "it is not supported");
46 #endif
47         return;
48 }
49
50 #endif
51
52 CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB, CAConnectionStateChangedCB connStateCB)
53 {
54         OIC_LOG(DEBUG, TAG, "CARegisterNetworkMonitorHandler");
55
56         CASetNetworkMonitorCallbacks(adapterStateCB, connStateCB);
57         return CA_STATUS_OK;
58 }
59
60 CAResult_t CAUnregisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB, CAConnectionStateChangedCB connStateCB)
61 {
62         OIC_LOG(DEBUG, TAG, "CAUnregisterNetworkMonitorHandler");
63
64         return CAUnsetNetworkMonitorCallbacks(adapterStateCB, connStateCB);
65 }
66
67 CAResult_t CASetAutoConnectionDeviceInfo(const char *address)
68 {
69         OIC_LOG(DEBUG, TAG, "CASetAutoConnectionDeviceInfo");
70
71 #if (defined(__ANDROID__) || defined(__APPLE__)) && defined(LE_ADAPTER)
72         return CASetLEClientAutoConnectionDeviceInfo(address);
73 #else
74         (void)address;
75         return CA_NOT_SUPPORTED;
76 #endif
77 }
78
79 CAResult_t CAUnsetAutoConnectionDeviceInfo(const char *address)
80 {
81         OIC_LOG(DEBUG, TAG, "CAUnsetAutoConnectionDeviceInfo");
82
83 #if (defined(__ANDROID__) || defined(__APPLE__))  && defined(LE_ADAPTER)
84         return CAUnsetLEClientAutoConnectionDeviceInfo(address);
85 #else
86         (void)address;
87         return CA_NOT_SUPPORTED;
88 #endif
89 }
90
91 #ifdef __APPLE__
92
93 CAResult_t CAUtilClientInitialize()
94 {
95         OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
96
97         CAResult_t res = CA_STATUS_OK;
98 #ifdef LE_ADAPTER
99         if (CA_STATUS_OK != CAManagerLEClientInitialize()) {
100                 OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
101                 res = CA_STATUS_FAILED;
102         }
103 #else
104         OIC_LOG(DEBUG, TAG, "it is not supported");
105         res = CA_NOT_SUPPORTED;
106 #endif
107
108         return res;
109 }
110
111 CAResult_t CAUtilClientTerminate()
112 {
113         OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
114 #ifdef LE_ADAPTER
115         return CAManagerLEClientTerminate();
116 #else
117         OIC_LOG(DEBUG, TAG, "it is not supported");
118         return CA_NOT_SUPPORTED;
119 #endif
120 }
121
122 CAResult_t CAUtilStopLEScan()
123 {
124         OIC_LOG(DEBUG, TAG, "CAUtilStopLEScan");
125 #ifdef LE_ADAPTER
126         CAManagerLEStopScan();
127         return CA_STATUS_OK;
128 #else
129         OIC_LOG(DEBUG, TAG, "it is not supported");
130         return CA_NOT_SUPPORTED;
131 #endif
132 }
133
134 CAResult_t CAUtilStartLEScan()
135 {
136         OIC_LOG(DEBUG, TAG, "CAUtilStartLEScan");
137 #ifdef LE_ADAPTER
138         CAManagerLEStartScan();
139         return CA_STATUS_OK;
140 #else
141         OIC_LOG(DEBUG, TAG, "it is not supported");
142         return CA_NOT_SUPPORTED;
143 #endif
144 }
145
146 CAResult_t CAUtilClientDisconnect()
147 {
148         OIC_LOG(DEBUG, TAG, "CAUtilClientDisconnect");
149 #ifdef LE_ADAPTER
150         CAManagerLEDisconnect();
151         return CA_STATUS_OK;
152 #else
153         OIC_LOG(DEBUG, TAG, "it is not supported");
154         return CA_NOT_SUPPORTED;
155 #endif
156 }
157
158 #endif
159
160 CAResult_t CASetPortNumberToAssign(CATransportAdapter_t adapter, CATransportFlags_t flag, uint16_t port)
161 {
162         uint16_t *targetPort = 0;
163
164         if (CA_ADAPTER_IP & adapter) {
165                 if (CA_SECURE & flag) {
166                         if (CA_IPV6 & flag) {
167                                 targetPort = &caglobals.ports.udp.u6s;
168                         } else if (CA_IPV4 & flag) {
169                                 targetPort = &caglobals.ports.udp.u4s;
170                         }
171                 } else {
172                         if (CA_IPV6 & flag) {
173                                 targetPort = &caglobals.ports.udp.u6;
174                         } else if (CA_IPV4 & flag) {
175                                 targetPort = &caglobals.ports.udp.u4;
176                         }
177                 }
178         }
179 #ifdef TCP_ADAPTER
180         if (CA_ADAPTER_TCP & adapter) {
181                 if (CA_IPV6 & flag) {
182                         targetPort = &caglobals.ports.tcp.u6;
183                 } else if (CA_IPV4 & flag) {
184                         targetPort = &caglobals.ports.tcp.u4;
185                 }
186         }
187 #endif
188
189         if (targetPort) {
190                 *targetPort = port;
191                 return CA_STATUS_OK;
192         }
193
194         return CA_NOT_SUPPORTED;
195 }
196
197 uint16_t CAGetAssignedPortNumber(CATransportAdapter_t adapter, CATransportFlags_t flag)
198 {
199         OIC_LOG(DEBUG, TAG, "CAGetAssignedPortNumber");
200
201         if (CA_ADAPTER_IP & adapter) {
202                 if (CA_SECURE & flag) {
203                         if (CA_IPV6 & flag) {
204                                 return caglobals.ip.u6s.port;
205                         } else if (CA_IPV4 & flag) {
206                                 return caglobals.ip.u4s.port;
207                         }
208                 } else {
209                         if (CA_IPV6 & flag) {
210                                 return caglobals.ip.u6.port;
211                         } else if (CA_IPV4 & flag) {
212                                 return caglobals.ip.u4.port;
213                         }
214                 }
215         }
216 #ifdef TCP_ADAPTER
217         if (CA_ADAPTER_TCP & adapter) {
218                 if (CA_SECURE & flag) {
219                         if (CA_IPV6 & flag) {
220                                 return caglobals.tcp.ipv6s.port;
221                         } else if (CA_IPV4 & flag) {
222                                 return caglobals.tcp.ipv4s.port;
223                         }
224                 } else {
225                         if (CA_IPV6 & flag) {
226                                 return caglobals.tcp.ipv6.port;
227                         } else if (CA_IPV4 & flag) {
228                                 return caglobals.tcp.ipv4.port;
229                         }
230                 }
231         }
232 #endif
233         return 0;
234 }
235
236 #if defined(__TIZEN__) && defined(LE_ADAPTER) && defined(BLE_CUSTOM_ADVERTISE)
237 CAResult_t CASetAdvertisementData(const char* data, int length)
238 {
239         return CAManagerLEServerSetAdvertisementData(data, length);
240 }
241 void CASetAdvertisementDataGetter(CAAdvertisementDataGetterCB getter)
242 {
243         CAManagerLEServerSetAdvertisementDataGetter(getter);
244 }
245 void CASetAutoAdvertisement(bool autoAdvertisement)
246 {
247         CAManagerLEServerSetAutoAdvertisement(autoAdvertisement);
248 }
249 CAResult_t CASetScanResponseData(const char* data, int length)
250 {
251         return CAManagerLEServerSetScanResponseData(data, length);
252 }
253 #endif
254
255 #ifdef __ANDROID__
256 /**
257  * initialize client connection manager
258  * @param[in]   env                   JNI interface pointer.
259  * @param[in]   jvm                   invocation inferface for JAVA virtual machine.
260  * @param[in]   context               application context.
261  */
262 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
263 {
264         OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
265
266         CAResult_t res = CA_STATUS_OK;
267 #ifdef LE_ADAPTER
268         if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context)) {
269                 OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
270                 res = CA_STATUS_FAILED;
271         }
272 #endif
273
274 #ifdef EDR_ADAPTER
275         if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context)) {
276                 OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
277                 res = CA_STATUS_FAILED;
278         }
279 #endif
280
281 #if !defined(LE_ADAPTER) && !defined(EDR_ADAPTER)
282         (void)env;
283         (void)jvm;
284         (void)context;
285 #endif
286         return res;
287 }
288
289 /**
290  * terminate client connection manager
291  * @param[in]   env                   JNI interface pointer.
292  */
293 CAResult_t CAUtilClientTerminate(JNIEnv *env)
294 {
295         OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
296 #ifdef LE_ADAPTER
297         return CAManagerLEClientTerminate(env);
298 #else
299         OIC_LOG(DEBUG, TAG, "it is not supported");
300         (void)env;
301         return CA_NOT_SUPPORTED;
302 #endif
303 }
304
305 // BT pairing
306 CAResult_t CAUtilStartScan(JNIEnv *env)
307 {
308 #ifdef EDR_ADAPTER
309         return CABTPairingStartScan(env);
310 #else
311         OIC_LOG(DEBUG, TAG, "it is not supported");
312         (void)env;
313         return CA_NOT_SUPPORTED;
314 #endif
315 }
316
317 CAResult_t CAUtilStopScan(JNIEnv *env)
318 {
319 #ifdef EDR_ADAPTER
320         return CABTPairingStopScan(env);
321 #else
322         OIC_LOG(DEBUG, TAG, "it is not supported");
323         (void)env;
324         return CA_NOT_SUPPORTED;
325 #endif
326 }
327
328 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
329 {
330 #ifdef EDR_ADAPTER
331         return CABTPairingCreateBond(env, device);
332 #else
333         OIC_LOG(DEBUG, TAG, "it is not supported");
334         (void)env;
335         (void)device;
336         return CA_NOT_SUPPORTED;
337 #endif
338 }
339
340 void CAUtilSetFoundDeviceListener(jobject listener)
341 {
342 #ifdef EDR_ADAPTER
343         CABTPairingSetFoundDeviceListener(listener);
344 #else
345         (void)listener;
346 #endif
347 }
348
349 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount)
350 {
351         OIC_LOG(DEBUG, TAG, "CAUtilSetLEScanInterval");
352 #ifdef LE_ADAPTER
353         CAManagerLESetScanInterval(intervalTime, workingCount);
354         return CA_STATUS_OK;
355 #else
356         (void)intervalTime;
357         (void)workingCount;
358         OIC_LOG(DEBUG, TAG, "it is not supported");
359         return CA_NOT_SUPPORTED;
360 #endif
361 }
362
363 CAResult_t CAUtilStopLEScan()
364 {
365         OIC_LOG(DEBUG, TAG, "CAUtilStopLEScan");
366 #ifdef LE_ADAPTER
367         CAManagerLEStopScan();
368         return CA_STATUS_OK;
369 #else
370         OIC_LOG(DEBUG, TAG, "it is not supported");
371         return CA_NOT_SUPPORTED;
372 #endif
373 }
374 #endif // __ANDROID__
375
376 CAResult_t CAUtilStartLEAdvertising()
377 {
378         OIC_LOG(DEBUG, TAG, "CAUtilStartLEAdvertising");
379 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
380         return CAManagerLEStartAdvertising();
381 #else
382         OIC_LOG(DEBUG, TAG, "it is not supported");
383         return CA_NOT_SUPPORTED;
384 #endif
385 }
386
387 CAResult_t CAUtilStopLEAdvertising()
388 {
389         OIC_LOG(DEBUG, TAG, "CAUtilStopLEAdvertising");
390 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
391         return CAManagerLEStopAdvertising();
392 #else
393         OIC_LOG(DEBUG, TAG, "it is not supported");
394         return CA_NOT_SUPPORTED;
395 #endif
396 }
397
398 CAResult_t CAUtilSetBTConfigure(CAUtilConfig_t config)
399 {
400         OIC_LOG_V(DEBUG, TAG, "CAUtilSetConfigure");
401 #if (defined(__ANDROID__) && defined(LE_ADAPTER))
402         OIC_LOG_V(DEBUG, TAG, " - bleFlag [%d]", config.bleFlags);
403         CAManagerSetConfigure(config);
404         return CA_STATUS_OK;
405 #else
406         (void) config;
407         OIC_LOG(DEBUG, TAG, "it is not supported");
408         return CA_NOT_SUPPORTED;
409 #endif
410 }
411
412 void CAUtilSetLogLevel(CAUtilLogLevel_t level, bool hidePrivateLogEntries)
413 {
414         OIC_LOG(DEBUG, TAG, "CAUtilSetLogLevel");
415         LogLevel logLevel = DEBUG;
416         switch (level) {
417         case CA_LOG_LEVEL_ALL:
418                 logLevel = DEBUG;
419                 break;
420         case CA_LOG_LEVEL_INFO:
421                 logLevel = INFO;
422                 break;
423         default:
424                 logLevel = DEBUG;
425                 break;
426         }
427
428         OCSetLogLevel(logLevel, hidePrivateLogEntries);
429 }
430
431 CAResult_t CAUtilSetMulticastTTL(size_t ttl)
432 {
433         OIC_LOG(DEBUG, TAG, "CAUtilSetMulticastTTL");
434 #ifdef IP_ADAPTER
435         if (ttl > 255) {
436                 OIC_LOG(ERROR, TAG, "TTL value is invalid");
437                 return CA_STATUS_INVALID_PARAM;
438         }
439         if (ttl > 32) {
440                 OIC_LOG(INFO, TAG, "TTL value is not supported");
441                 return CA_NOT_SUPPORTED;
442         }
443         return CASetMulticastTTL(ttl);
444 #else
445         (void) ttl;
446         OIC_LOG(DEBUG, TAG, "it is not supported");
447         return CA_NOT_SUPPORTED;
448 #endif
449 }
450
451 CAResult_t CAUtilGetMulticastTTL(size_t *ttl)
452 {
453         OIC_LOG(DEBUG, TAG, "CAUtilGetMulticastTTL");
454 #ifdef IP_ADAPTER
455         return CAGetMulticastTTL(ttl);
456 #else
457         (void) ttl;
458         OIC_LOG(DEBUG, TAG, "it is not supported");
459         return CA_NOT_SUPPORTED;
460 #endif
461 }
462
463 CAResult_t CAUtilTCPDisconnectSession(const char *address, uint16_t port, CATransportFlags_t flags)
464 {
465         OIC_LOG(DEBUG, TAG, "CAUtilTCPDisconnectSession");
466 #ifdef TCP_ADAPTER
467         CARequestInfo_t reqInfo = {.info.event = CA_REQ_DISCONNECT};
468         CAEndpoint_t *endpoint = CACreateEndpointObject(flags, CA_ADAPTER_TCP, address, port);
469         if (!endpoint) {
470                 return CA_STATUS_FAILED;
471         }
472
473         CAResult_t caResult = CASendRequest(endpoint, &reqInfo);
474         if (CA_STATUS_OK != caResult) {
475                 OIC_LOG(ERROR, TAG, "CASendRequest error");
476         }
477         CAFreeEndpoint(endpoint);
478         return caResult;
479 #else
480         (void) address;
481         (void) port;
482         (void) flags;
483         OIC_LOG(DEBUG, TAG, "it is not supported");
484         return CA_NOT_SUPPORTED;
485 #endif
486 }
487
488 void CAUtilSkipTCPCloseOnInterfaceDown(bool state)
489 {
490 #ifdef TCP_ADAPTER
491     CATCPSkipCloseOnInterfaceDown(state);
492 #else
493     (void) state;
494     OIC_LOG(DEBUG, TAG, "Not supported!");
495 #endif
496 }
497
498 CAResult_t CASetSecureEndpointUuid(const CAEndpoint_t *peer, const char *uuid)
499 {
500     OIC_LOG(DEBUG, TAG, "CASetSecureEndpointUuid");
501 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
502     return SetCASecureEndpointUuid(peer, uuid);
503 #else
504     (void) peer;
505     (void) uuid;
506     OIC_LOG(DEBUG, TAG, "it is not supported");
507     return CA_NOT_SUPPORTED;
508 #endif
509 }
510
511 CAResult_t CAUtilStartGattServer()
512 {
513         OIC_LOG(DEBUG, TAG, "CAUtilStartGattServer");
514         CAResult_t res = CA_STATUS_OK;
515 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
516         CAStartGattServer();
517 #else
518         OIC_LOG(DEBUG, TAG, "it is not supported");
519         res = CA_NOT_SUPPORTED;
520 #endif
521         return res;
522 }
523
524 CAResult_t CAUtilStopGattServer()
525 {
526         OIC_LOG(DEBUG, TAG, "CAUtilStopGattServer");
527         CAResult_t res = CA_STATUS_OK;
528 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
529         CAStopGattServer();
530 #else
531         OIC_LOG(DEBUG, TAG, "it is not supported");
532         res = CA_NOT_SUPPORTED;
533 #endif
534         return res;
535 }
536
537 #if defined(__TIZEN__)
538 CAResult_t CAGetTCPIPHeader(CATransportAdapter_t adapter, int flag, TCPHeaderInfo* info)
539 {
540     CAResult_t res = CA_STATUS_OK;
541 #ifdef TCP_ADAPTER
542     if (CA_ADAPTER_TCP & adapter) {
543         res = CAGetTCPIPHeaderInfo(flag,info);
544     }
545     else
546         res = CA_NOT_SUPPORTED;
547 #else
548     res = CA_NOT_SUPPORTED;
549     OIC_LOG(DEBUG, TAG, "Not supported!");
550 #endif
551     return res;
552 }
553 #endif