1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "camanagerleinterface.h"
22 #include "cabtpairinginterface.h"
23 #include "cautilinterface.h"
24 #include "cainterfacecontroller.h"
27 #include "caremotehandler.h"
29 #define TAG "OIC_CA_COMMON_UTILS"
31 CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB, CAConnectionStateChangedCB connStateCB)
33 OIC_LOG(DEBUG, TAG, "CARegisterNetworkMonitorHandler");
35 CASetNetworkMonitorCallbacks(adapterStateCB, connStateCB);
39 CAResult_t CAUnregisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB, CAConnectionStateChangedCB connStateCB)
41 OIC_LOG(DEBUG, TAG, "CAUnregisterNetworkMonitorHandler");
43 return CAUnsetNetworkMonitorCallbacks(adapterStateCB, connStateCB);
46 CAResult_t CASetAutoConnectionDeviceInfo(const char *address)
48 OIC_LOG(DEBUG, TAG, "CASetAutoConnectionDeviceInfo");
50 #if (defined(__ANDROID__) || defined(__APPLE__)) && defined(LE_ADAPTER)
51 return CASetLEClientAutoConnectionDeviceInfo(address);
54 return CA_NOT_SUPPORTED;
58 CAResult_t CAUnsetAutoConnectionDeviceInfo(const char *address)
60 OIC_LOG(DEBUG, TAG, "CAUnsetAutoConnectionDeviceInfo");
62 #if (defined(__ANDROID__) || defined(__APPLE__)) && defined(LE_ADAPTER)
63 return CAUnsetLEClientAutoConnectionDeviceInfo(address);
66 return CA_NOT_SUPPORTED;
72 CAResult_t CAUtilClientInitialize()
74 OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
76 CAResult_t res = CA_STATUS_OK;
78 if (CA_STATUS_OK != CAManagerLEClientInitialize()) {
79 OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
80 res = CA_STATUS_FAILED;
83 OIC_LOG(DEBUG, TAG, "it is not supported");
84 res = CA_NOT_SUPPORTED;
90 CAResult_t CAUtilClientTerminate()
92 OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
94 return CAManagerLEClientTerminate();
96 OIC_LOG(DEBUG, TAG, "it is not supported");
97 return CA_NOT_SUPPORTED;
101 CAResult_t CAUtilStopLEScan()
103 OIC_LOG(DEBUG, TAG, "CAUtilStopLEScan");
105 CAManagerLEStopScan();
108 OIC_LOG(DEBUG, TAG, "it is not supported");
109 return CA_NOT_SUPPORTED;
113 CAResult_t CAUtilStartLEScan()
115 OIC_LOG(DEBUG, TAG, "CAUtilStartLEScan");
117 CAManagerLEStartScan();
120 OIC_LOG(DEBUG, TAG, "it is not supported");
121 return CA_NOT_SUPPORTED;
125 CAResult_t CAUtilClientDisconnect()
127 OIC_LOG(DEBUG, TAG, "CAUtilClientDisconnect");
129 CAManagerLEDisconnect();
132 OIC_LOG(DEBUG, TAG, "it is not supported");
133 return CA_NOT_SUPPORTED;
139 CAResult_t CASetPortNumberToAssign(CATransportAdapter_t adapter, CATransportFlags_t flag, uint16_t port)
141 uint16_t *targetPort = 0;
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;
151 if (CA_IPV6 & flag) {
152 targetPort = &caglobals.ports.udp.u6;
153 } else if (CA_IPV4 & flag) {
154 targetPort = &caglobals.ports.udp.u4;
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;
173 return CA_NOT_SUPPORTED;
176 uint16_t CAGetAssignedPortNumber(CATransportAdapter_t adapter, CATransportFlags_t flag)
178 OIC_LOG(DEBUG, TAG, "CAGetAssignedPortNumber");
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;
188 if (CA_IPV6 & flag) {
189 return caglobals.ip.u6.port;
190 } else if (CA_IPV4 & flag) {
191 return caglobals.ip.u4.port;
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;
204 if (CA_IPV6 & flag) {
205 return caglobals.tcp.ipv6.port;
206 } else if (CA_IPV4 & flag) {
207 return caglobals.tcp.ipv4.port;
215 #if defined(__TIZEN__) && defined(LE_ADAPTER) && defined(BLE_CUSTOM_ADVERTISE)
216 CAResult_t CASetAdvertisementData(const char* data, int length)
218 return CAManagerLEServerSetAdvertisementData(data, length);
220 void CASetAdvertisementDataGetter(CAAdvertisementDataGetterCB getter)
222 CAManagerLEServerSetAdvertisementDataGetter(getter);
224 void CASetAutoAdvertisement(bool autoAdvertisement)
226 CAManagerLEServerSetAutoAdvertisement(autoAdvertisement);
228 CAResult_t CASetScanResponseData(const char* data, int length)
230 return CAManagerLEServerSetScanResponseData(data, length);
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.
241 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
243 OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
245 CAResult_t res = CA_STATUS_OK;
247 if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context)) {
248 OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
249 res = CA_STATUS_FAILED;
254 if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context)) {
255 OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
256 res = CA_STATUS_FAILED;
260 #if !defined(LE_ADAPTER) && !defined(EDR_ADAPTER)
269 * terminate client connection manager
270 * @param[in] env JNI interface pointer.
272 CAResult_t CAUtilClientTerminate(JNIEnv *env)
274 OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
276 return CAManagerLEClientTerminate(env);
278 OIC_LOG(DEBUG, TAG, "it is not supported");
280 return CA_NOT_SUPPORTED;
285 CAResult_t CAUtilStartScan(JNIEnv *env)
288 return CABTPairingStartScan(env);
290 OIC_LOG(DEBUG, TAG, "it is not supported");
292 return CA_NOT_SUPPORTED;
296 CAResult_t CAUtilStopScan(JNIEnv *env)
299 return CABTPairingStopScan(env);
301 OIC_LOG(DEBUG, TAG, "it is not supported");
303 return CA_NOT_SUPPORTED;
307 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
310 return CABTPairingCreateBond(env, device);
312 OIC_LOG(DEBUG, TAG, "it is not supported");
315 return CA_NOT_SUPPORTED;
319 void CAUtilSetFoundDeviceListener(jobject listener)
322 CABTPairingSetFoundDeviceListener(listener);
328 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount)
330 OIC_LOG(DEBUG, TAG, "CAUtilSetLEScanInterval");
332 CAManagerLESetScanInterval(intervalTime, workingCount);
337 OIC_LOG(DEBUG, TAG, "it is not supported");
338 return CA_NOT_SUPPORTED;
342 CAResult_t CAUtilStopLEScan()
344 OIC_LOG(DEBUG, TAG, "CAUtilStopLEScan");
346 CAManagerLEStopScan();
349 OIC_LOG(DEBUG, TAG, "it is not supported");
350 return CA_NOT_SUPPORTED;
353 #endif // __ANDROID__
355 CAResult_t CAUtilStartLEAdvertising()
357 OIC_LOG(DEBUG, TAG, "CAUtilStartLEAdvertising");
358 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
359 return CAManagerLEStartAdvertising();
361 OIC_LOG(DEBUG, TAG, "it is not supported");
362 return CA_NOT_SUPPORTED;
366 CAResult_t CAUtilStopLEAdvertising()
368 OIC_LOG(DEBUG, TAG, "CAUtilStopLEAdvertising");
369 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
370 return CAManagerLEStopAdvertising();
372 OIC_LOG(DEBUG, TAG, "it is not supported");
373 return CA_NOT_SUPPORTED;
377 CAResult_t CAUtilSetBTConfigure(CAUtilConfig_t config)
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);
386 OIC_LOG(DEBUG, TAG, "it is not supported");
387 return CA_NOT_SUPPORTED;
391 void CAUtilSetLogLevel(CAUtilLogLevel_t level, bool hidePrivateLogEntries)
393 OIC_LOG(DEBUG, TAG, "CAUtilSetLogLevel");
394 LogLevel logLevel = DEBUG;
396 case CA_LOG_LEVEL_ALL:
399 case CA_LOG_LEVEL_INFO:
407 OCSetLogLevel(logLevel, hidePrivateLogEntries);
410 CAResult_t CAUtilSetMulticastTTL(size_t ttl)
412 OIC_LOG(DEBUG, TAG, "CAUtilSetMulticastTTL");
415 OIC_LOG(ERROR, TAG, "TTL value is invalid");
416 return CA_STATUS_INVALID_PARAM;
419 OIC_LOG(INFO, TAG, "TTL value is not supported");
420 return CA_NOT_SUPPORTED;
422 return CASetMulticastTTL(ttl);
425 OIC_LOG(DEBUG, TAG, "it is not supported");
426 return CA_NOT_SUPPORTED;
430 CAResult_t CAUtilGetMulticastTTL(size_t *ttl)
432 OIC_LOG(DEBUG, TAG, "CAUtilGetMulticastTTL");
434 return CAGetMulticastTTL(ttl);
437 OIC_LOG(DEBUG, TAG, "it is not supported");
438 return CA_NOT_SUPPORTED;
442 CAResult_t CAUtilTCPDisconnectSession(const char *address, uint16_t port, CATransportFlags_t flags)
444 OIC_LOG(DEBUG, TAG, "CAUtilTCPDisconnectSession");
446 CARequestInfo_t reqInfo = {.info.event = CA_REQ_DISCONNECT};
447 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, CA_ADAPTER_TCP, address, port);
449 return CA_STATUS_FAILED;
452 CAResult_t caResult = CASendRequest(endpoint, &reqInfo);
453 if (CA_STATUS_OK != caResult) {
454 OIC_LOG(ERROR, TAG, "CASendRequest error");
456 CAFreeEndpoint(endpoint);
462 OIC_LOG(DEBUG, TAG, "it is not supported");
463 return CA_NOT_SUPPORTED;
467 CAResult_t CAUtilStartGattServer()
469 OIC_LOG(DEBUG, TAG, "CAUtilStartGattServer");
470 CAResult_t res = CA_STATUS_OK;
471 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
474 OIC_LOG(DEBUG, TAG, "it is not supported");
475 res = CA_NOT_SUPPORTED;
480 CAResult_t CAUtilStopGattServer()
482 OIC_LOG(DEBUG, TAG, "CAUtilStopGattServer");
483 CAResult_t res = CA_STATUS_OK;
484 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
487 OIC_LOG(DEBUG, TAG, "it is not supported");
488 res = CA_NOT_SUPPORTED;