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