replace : iotivity -> iotivity-sec
[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 CAResult_t CASetScanResponseData(const char* data, int length)
221 {
222         return CAManagerLEServerSetScanResponseData(data, length);
223 }
224 #endif
225
226 #ifdef __ANDROID__
227 /**
228  * initialize client connection manager
229  * @param[in]   env                   JNI interface pointer.
230  * @param[in]   jvm                   invocation inferface for JAVA virtual machine.
231  * @param[in]   context               application context.
232  */
233 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
234 {
235         OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
236
237         CAResult_t res = CA_STATUS_OK;
238 #ifdef LE_ADAPTER
239         if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context)) {
240                 OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
241                 res = CA_STATUS_FAILED;
242         }
243 #endif
244
245 #ifdef EDR_ADAPTER
246         if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context)) {
247                 OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
248                 res = CA_STATUS_FAILED;
249         }
250 #endif
251
252 #if !defined(LE_ADAPTER) && !defined(EDR_ADAPTER)
253         (void)env;
254         (void)jvm;
255         (void)context;
256 #endif
257         return res;
258 }
259
260 /**
261  * terminate client connection manager
262  * @param[in]   env                   JNI interface pointer.
263  */
264 CAResult_t CAUtilClientTerminate(JNIEnv *env)
265 {
266         OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
267 #ifdef LE_ADAPTER
268         return CAManagerLEClientTerminate(env);
269 #else
270         OIC_LOG(DEBUG, TAG, "it is not supported");
271         (void)env;
272         return CA_NOT_SUPPORTED;
273 #endif
274 }
275
276 // BT pairing
277 CAResult_t CAUtilStartScan(JNIEnv *env)
278 {
279 #ifdef EDR_ADAPTER
280         return CABTPairingStartScan(env);
281 #else
282         OIC_LOG(DEBUG, TAG, "it is not supported");
283         (void)env;
284         return CA_NOT_SUPPORTED;
285 #endif
286 }
287
288 CAResult_t CAUtilStopScan(JNIEnv *env)
289 {
290 #ifdef EDR_ADAPTER
291         return CABTPairingStopScan(env);
292 #else
293         OIC_LOG(DEBUG, TAG, "it is not supported");
294         (void)env;
295         return CA_NOT_SUPPORTED;
296 #endif
297 }
298
299 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
300 {
301 #ifdef EDR_ADAPTER
302         return CABTPairingCreateBond(env, device);
303 #else
304         OIC_LOG(DEBUG, TAG, "it is not supported");
305         (void)env;
306         (void)device;
307         return CA_NOT_SUPPORTED;
308 #endif
309 }
310
311 void CAUtilSetFoundDeviceListener(jobject listener)
312 {
313 #ifdef EDR_ADAPTER
314         CABTPairingSetFoundDeviceListener(listener);
315 #else
316         (void)listener;
317 #endif
318 }
319
320 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount)
321 {
322         OIC_LOG(DEBUG, TAG, "CAUtilSetLEScanInterval");
323 #ifdef LE_ADAPTER
324         CAManagerLESetScanInterval(intervalTime, workingCount);
325         return CA_STATUS_OK;
326 #else
327         (void)intervalTime;
328         (void)workingCount;
329         OIC_LOG(DEBUG, TAG, "it is not supported");
330         return CA_NOT_SUPPORTED;
331 #endif
332 }
333
334 CAResult_t CAUtilStopLEScan()
335 {
336         OIC_LOG(DEBUG, TAG, "CAUtilStopLEScan");
337 #ifdef LE_ADAPTER
338         CAManagerLEStopScan();
339         return CA_STATUS_OK;
340 #else
341         OIC_LOG(DEBUG, TAG, "it is not supported");
342         return CA_NOT_SUPPORTED;
343 #endif
344 }
345 #endif // __ANDROID__
346
347 CAResult_t CAUtilStartLEAdvertising()
348 {
349         OIC_LOG(DEBUG, TAG, "CAUtilStartLEAdvertising");
350 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
351         return CAManagerLEStartAdvertising();
352 #else
353         OIC_LOG(DEBUG, TAG, "it is not supported");
354         return CA_NOT_SUPPORTED;
355 #endif
356 }
357
358 CAResult_t CAUtilStopLEAdvertising()
359 {
360         OIC_LOG(DEBUG, TAG, "CAUtilStopLEAdvertising");
361 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
362         return CAManagerLEStopAdvertising();
363 #else
364         OIC_LOG(DEBUG, TAG, "it is not supported");
365         return CA_NOT_SUPPORTED;
366 #endif
367 }
368
369 CAResult_t CAUtilSetBTConfigure(CAUtilConfig_t config)
370 {
371         OIC_LOG_V(DEBUG, TAG, "CAUtilSetConfigure");
372 #if (defined(__ANDROID__) && defined(LE_ADAPTER))
373         OIC_LOG_V(DEBUG, TAG, " - bleFlag [%d]", config.bleFlags);
374         CAManagerSetConfigure(config);
375         return CA_STATUS_OK;
376 #else
377         (void) config;
378         OIC_LOG(DEBUG, TAG, "it is not supported");
379         return CA_NOT_SUPPORTED;
380 #endif
381 }
382
383 void CAUtilSetLogLevel(CAUtilLogLevel_t level, bool hidePrivateLogEntries)
384 {
385         OIC_LOG(DEBUG, TAG, "CAUtilSetLogLevel");
386         LogLevel logLevel = DEBUG;
387         switch (level) {
388         case CA_LOG_LEVEL_ALL:
389                 logLevel = DEBUG;
390                 break;
391         case CA_LOG_LEVEL_INFO:
392                 logLevel = INFO;
393                 break;
394         default:
395                 logLevel = DEBUG;
396                 break;
397         }
398
399         OCSetLogLevel(logLevel, hidePrivateLogEntries);
400 }
401
402 CAResult_t CAUtilSetMulticastTTL(size_t ttl)
403 {
404         OIC_LOG(DEBUG, TAG, "CAUtilSetMulticastTTL");
405 #ifdef IP_ADAPTER
406         if (ttl > 255) {
407                 OIC_LOG(ERROR, TAG, "TTL value is invalid");
408                 return CA_STATUS_INVALID_PARAM;
409         }
410         if (ttl > 32) {
411                 OIC_LOG(INFO, TAG, "TTL value is not supported");
412                 return CA_NOT_SUPPORTED;
413         }
414         return CASetMulticastTTL(ttl);
415 #else
416         (void) ttl;
417         OIC_LOG(DEBUG, TAG, "it is not supported");
418         return CA_NOT_SUPPORTED;
419 #endif
420 }
421
422 CAResult_t CAUtilGetMulticastTTL(size_t *ttl)
423 {
424         OIC_LOG(DEBUG, TAG, "CAUtilGetMulticastTTL");
425 #ifdef IP_ADAPTER
426         return CAGetMulticastTTL(ttl);
427 #else
428         (void) ttl;
429         OIC_LOG(DEBUG, TAG, "it is not supported");
430         return CA_NOT_SUPPORTED;
431 #endif
432 }
433
434 CAResult_t CAUtilTCPDisconnectSession(const char *address, uint16_t port, CATransportFlags_t flags)
435 {
436         OIC_LOG(DEBUG, TAG, "CAUtilTCPDisconnectSession");
437 #ifdef TCP_ADAPTER
438         CARequestInfo_t reqInfo = {.info.event = CA_REQ_DISCONNECT};
439         CAEndpoint_t *endpoint = CACreateEndpointObject(flags, CA_ADAPTER_TCP, address, port);
440         if (!endpoint) {
441                 return CA_STATUS_FAILED;
442         }
443
444         CAResult_t caResult = CASendRequest(endpoint, &reqInfo);
445         if (CA_STATUS_OK != caResult) {
446                 OIC_LOG(ERROR, TAG, "CASendRequest error");
447         }
448         CAFreeEndpoint(endpoint);
449         return caResult;
450 #else
451         (void) address;
452         (void) port;
453         (void) flags;
454         OIC_LOG(DEBUG, TAG, "it is not supported");
455         return CA_NOT_SUPPORTED;
456 #endif
457 }
458
459 CAResult_t CAUtilStartGattServer()
460 {
461         OIC_LOG(DEBUG, TAG, "CAUtilStartGattServer");
462         CAResult_t res = CA_STATUS_OK;
463 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
464         CAStartGattServer();
465 #else
466         OIC_LOG(DEBUG, TAG, "it is not supported");
467         res = CA_NOT_SUPPORTED;
468 #endif
469         return res;
470 }
471
472 CAResult_t CAUtilStopGattServer()
473 {
474         OIC_LOG(DEBUG, TAG, "CAUtilStopGattServer");
475         CAResult_t res = CA_STATUS_OK;
476 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
477         CAStopGattServer();
478 #else
479         OIC_LOG(DEBUG, TAG, "it is not supported");
480         res = CA_NOT_SUPPORTED;
481 #endif
482         return res;
483 }