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