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"
28 #define TAG "OIC_CA_COMMON_UTILS"
30 static CAAdapterStateChangedCB g_adapterStateCB = NULL;
31 static CAConnectionStateChangedCB g_connStateCB = NULL;
33 static void CAManagerAdapterMonitorHandler(CATransportAdapter_t adapter,
34 CANetworkStatus_t status)
36 if (CA_INTERFACE_DOWN == status)
40 g_adapterStateCB(adapter, false);
41 OIC_LOG(DEBUG, TAG, "Pass the disabled adapter state to upper layer");
44 else if (CA_INTERFACE_UP == status)
48 g_adapterStateCB(adapter, true);
49 OIC_LOG(DEBUG, TAG, "Pass the enabled adapter state to upper layer");
54 static void CAManagerConnectionMonitorHandler(const CAEndpoint_t *info, bool isConnected)
56 if (!info || !info->addr)
58 OIC_LOG(ERROR, TAG, "remoteAddress is NULL");
66 g_connStateCB(info->adapter, info->addr, isConnected);
67 OIC_LOG(DEBUG, TAG, "Pass the connected device info to upper layer");
74 g_connStateCB(info->adapter, info->addr, isConnected);
75 OIC_LOG(DEBUG, TAG, "Pass the disconnected device info to upper layer");
80 CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB,
81 CAConnectionStateChangedCB connStateCB)
83 OIC_LOG(DEBUG, TAG, "CARegisterNetworkMonitorHandler");
85 g_adapterStateCB = adapterStateCB;
86 g_connStateCB = connStateCB;
88 CASetNetworkMonitorCallbacks(CAManagerAdapterMonitorHandler,
89 CAManagerConnectionMonitorHandler);
93 CAResult_t CASetAutoConnectionDeviceInfo(const char *address)
95 OIC_LOG(DEBUG, TAG, "CASetAutoConnectionDeviceInfo");
97 #if defined(__ANDROID__) && defined(LE_ADAPTER)
98 return CASetLEClientAutoConnectionDeviceInfo(address);
101 return CA_NOT_SUPPORTED;
105 CAResult_t CAUnsetAutoConnectionDeviceInfo(const char *address)
107 OIC_LOG(DEBUG, TAG, "CAUnsetAutoConnectionDeviceInfo");
109 #if defined(__ANDROID__) && defined(LE_ADAPTER)
110 return CAUnsetLEClientAutoConnectionDeviceInfo(address);
113 return CA_NOT_SUPPORTED;
117 CAResult_t CASetPortNumberToAssign(CATransportAdapter_t adapter,
118 CATransportFlags_t flag, uint16_t port)
120 uint16_t *targetPort = 0;
122 if (CA_ADAPTER_IP & adapter)
124 if (CA_SECURE & flag)
128 targetPort = &caglobals.ports.udp.u6s;
130 else if (CA_IPV4 & flag)
132 targetPort = &caglobals.ports.udp.u4s;
139 targetPort = &caglobals.ports.udp.u6;
141 else if (CA_IPV4 & flag)
143 targetPort = &caglobals.ports.udp.u4;
148 if (CA_ADAPTER_TCP & adapter)
152 targetPort = &caglobals.ports.tcp.u6;
154 else if (CA_IPV4 & flag)
156 targetPort = &caglobals.ports.tcp.u4;
167 return CA_NOT_SUPPORTED;
170 uint16_t CAGetAssignedPortNumber(CATransportAdapter_t adapter, CATransportFlags_t flag)
172 OIC_LOG(DEBUG, TAG, "CAGetAssignedPortNumber");
174 if (CA_ADAPTER_IP & adapter)
176 if (CA_SECURE & flag)
180 return caglobals.ip.u6s.port;
182 else if (CA_IPV4 & flag)
184 return caglobals.ip.u4s.port;
191 return caglobals.ip.u6.port;
193 else if (CA_IPV4 & flag)
195 return caglobals.ip.u4.port;
200 if (CA_ADAPTER_TCP & adapter)
204 return caglobals.tcp.ipv6.port;
206 else if (CA_IPV4 & flag)
208 return caglobals.tcp.ipv4.port;
217 * initialize client connection manager
218 * @param[in] env JNI interface pointer.
219 * @param[in] jvm invocation inferface for JAVA virtual machine.
220 * @param[in] context application context.
222 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
224 OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
226 CAResult_t res = CA_STATUS_OK;
228 if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context))
230 OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
231 res = CA_STATUS_FAILED;
236 if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context))
238 OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
239 res = CA_STATUS_FAILED;
246 * terminate client connection manager
247 * @param[in] env JNI interface pointer.
249 CAResult_t CAUtilClientTerminate(JNIEnv *env)
251 OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
253 return CAManagerLEClientTerminate(env);
255 OIC_LOG(DEBUG, TAG, "it is not supported");
257 return CA_NOT_SUPPORTED;
262 CAResult_t CAUtilStartScan(JNIEnv *env)
265 return CABTPairingStartScan(env);
267 OIC_LOG(DEBUG, TAG, "it is not supported");
269 return CA_NOT_SUPPORTED;
273 CAResult_t CAUtilStopScan(JNIEnv *env)
276 return CABTPairingStopScan(env);
278 OIC_LOG(DEBUG, TAG, "it is not supported");
280 return CA_NOT_SUPPORTED;
284 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
287 return CABTPairingCreateBond(env, device);
289 OIC_LOG(DEBUG, TAG, "it is not supported");
292 return CA_NOT_SUPPORTED;
296 void CAUtilSetFoundDeviceListener(jobject listener)
299 CABTPairingSetFoundDeviceListener(listener);