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 CAResult_t CASetScanResponseData(const char* data, int length)
222 return CAManagerLEServerSetScanResponseData(data, length);
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.
233 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
235 OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
237 CAResult_t res = CA_STATUS_OK;
239 if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context)) {
240 OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
241 res = CA_STATUS_FAILED;
246 if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context)) {
247 OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
248 res = CA_STATUS_FAILED;
252 #if !defined(LE_ADAPTER) && !defined(EDR_ADAPTER)
261 * terminate client connection manager
262 * @param[in] env JNI interface pointer.
264 CAResult_t CAUtilClientTerminate(JNIEnv *env)
266 OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
268 return CAManagerLEClientTerminate(env);
270 OIC_LOG(DEBUG, TAG, "it is not supported");
272 return CA_NOT_SUPPORTED;
277 CAResult_t CAUtilStartScan(JNIEnv *env)
280 return CABTPairingStartScan(env);
282 OIC_LOG(DEBUG, TAG, "it is not supported");
284 return CA_NOT_SUPPORTED;
288 CAResult_t CAUtilStopScan(JNIEnv *env)
291 return CABTPairingStopScan(env);
293 OIC_LOG(DEBUG, TAG, "it is not supported");
295 return CA_NOT_SUPPORTED;
299 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
302 return CABTPairingCreateBond(env, device);
304 OIC_LOG(DEBUG, TAG, "it is not supported");
307 return CA_NOT_SUPPORTED;
311 void CAUtilSetFoundDeviceListener(jobject listener)
314 CABTPairingSetFoundDeviceListener(listener);
320 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount)
322 OIC_LOG(DEBUG, TAG, "CAUtilSetLEScanInterval");
324 CAManagerLESetScanInterval(intervalTime, workingCount);
329 OIC_LOG(DEBUG, TAG, "it is not supported");
330 return CA_NOT_SUPPORTED;
334 CAResult_t CAUtilStopLEScan()
336 OIC_LOG(DEBUG, TAG, "CAUtilStopLEScan");
338 CAManagerLEStopScan();
341 OIC_LOG(DEBUG, TAG, "it is not supported");
342 return CA_NOT_SUPPORTED;
345 #endif // __ANDROID__
347 CAResult_t CAUtilStartLEAdvertising()
349 OIC_LOG(DEBUG, TAG, "CAUtilStartLEAdvertising");
350 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
351 return CAManagerLEStartAdvertising();
353 OIC_LOG(DEBUG, TAG, "it is not supported");
354 return CA_NOT_SUPPORTED;
358 CAResult_t CAUtilStopLEAdvertising()
360 OIC_LOG(DEBUG, TAG, "CAUtilStopLEAdvertising");
361 #if (defined(__ANDROID__) || defined(__TIZEN__)) && defined(LE_ADAPTER)
362 return CAManagerLEStopAdvertising();
364 OIC_LOG(DEBUG, TAG, "it is not supported");
365 return CA_NOT_SUPPORTED;
369 CAResult_t CAUtilSetBTConfigure(CAUtilConfig_t config)
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);
378 OIC_LOG(DEBUG, TAG, "it is not supported");
379 return CA_NOT_SUPPORTED;
383 void CAUtilSetLogLevel(CAUtilLogLevel_t level, bool hidePrivateLogEntries)
385 OIC_LOG(DEBUG, TAG, "CAUtilSetLogLevel");
386 LogLevel logLevel = DEBUG;
388 case CA_LOG_LEVEL_ALL:
391 case CA_LOG_LEVEL_INFO:
399 OCSetLogLevel(logLevel, hidePrivateLogEntries);
402 CAResult_t CAUtilSetMulticastTTL(size_t ttl)
404 OIC_LOG(DEBUG, TAG, "CAUtilSetMulticastTTL");
407 OIC_LOG(ERROR, TAG, "TTL value is invalid");
408 return CA_STATUS_INVALID_PARAM;
411 OIC_LOG(INFO, TAG, "TTL value is not supported");
412 return CA_NOT_SUPPORTED;
414 return CASetMulticastTTL(ttl);
417 OIC_LOG(DEBUG, TAG, "it is not supported");
418 return CA_NOT_SUPPORTED;
422 CAResult_t CAUtilGetMulticastTTL(size_t *ttl)
424 OIC_LOG(DEBUG, TAG, "CAUtilGetMulticastTTL");
426 return CAGetMulticastTTL(ttl);
429 OIC_LOG(DEBUG, TAG, "it is not supported");
430 return CA_NOT_SUPPORTED;
434 CAResult_t CAUtilTCPDisconnectSession(const char *address, uint16_t port, CATransportFlags_t flags)
436 OIC_LOG(DEBUG, TAG, "CAUtilTCPDisconnectSession");
438 CARequestInfo_t reqInfo = {.info.event = CA_REQ_DISCONNECT};
439 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, CA_ADAPTER_TCP, address, port);
441 return CA_STATUS_FAILED;
444 CAResult_t caResult = CASendRequest(endpoint, &reqInfo);
445 if (CA_STATUS_OK != caResult) {
446 OIC_LOG(ERROR, TAG, "CASendRequest error");
448 CAFreeEndpoint(endpoint);
454 OIC_LOG(DEBUG, TAG, "it is not supported");
455 return CA_NOT_SUPPORTED;
459 CAResult_t CAUtilStartGattServer()
461 OIC_LOG(DEBUG, TAG, "CAUtilStartGattServer");
462 CAResult_t res = CA_STATUS_OK;
463 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
466 OIC_LOG(DEBUG, TAG, "it is not supported");
467 res = CA_NOT_SUPPORTED;
472 CAResult_t CAUtilStopGattServer()
474 OIC_LOG(DEBUG, TAG, "CAUtilStopGattServer");
475 CAResult_t res = CA_STATUS_OK;
476 #if (defined(__TIZEN__) && defined(LE_ADAPTER))
479 OIC_LOG(DEBUG, TAG, "it is not supported");
480 res = CA_NOT_SUPPORTED;