Merge branch 'master' into cloud-interface
[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
28 #define TAG "OIC_CA_COMMON_UTILS"
29
30 static CAAdapterStateChangedCB g_adapterStateCB = NULL;
31 static CAConnectionStateChangedCB g_connStateCB = NULL;
32
33 static void CAManagerAdapterMonitorHandler(CATransportAdapter_t adapter,
34                                            CANetworkStatus_t status)
35 {
36     if (CA_INTERFACE_DOWN == status)
37     {
38         if (g_adapterStateCB)
39         {
40             g_adapterStateCB(adapter, false);
41             OIC_LOG(DEBUG, TAG, "Pass the disabled adapter state to upper layer");
42         }
43     }
44     else if (CA_INTERFACE_UP == status)
45     {
46         if (g_adapterStateCB)
47         {
48             g_adapterStateCB(adapter, true);
49             OIC_LOG(DEBUG, TAG, "Pass the enabled adapter state to upper layer");
50         }
51     }
52 }
53
54 static void CAManagerConnectionMonitorHandler(const CAEndpoint_t *info, bool isConnected)
55 {
56     if (!info || !info->addr[0])
57     {
58         OIC_LOG(ERROR, TAG, "remoteAddress is NULL");
59         return;
60     }
61
62     if (isConnected)
63     {
64         if (g_connStateCB)
65         {
66             g_connStateCB(info, isConnected);
67             OIC_LOG(DEBUG, TAG, "Pass the connected device info to upper layer");
68         }
69     }
70     else
71     {
72         if (g_connStateCB)
73         {
74             g_connStateCB(info, isConnected);
75             OIC_LOG(DEBUG, TAG, "Pass the disconnected device info to upper layer");
76         }
77     }
78 }
79
80 CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB,
81                                            CAConnectionStateChangedCB connStateCB)
82 {
83     OIC_LOG(DEBUG, TAG, "CARegisterNetworkMonitorHandler");
84
85     g_adapterStateCB = adapterStateCB;
86     g_connStateCB = connStateCB;
87
88     CASetNetworkMonitorCallbacks(CAManagerAdapterMonitorHandler,
89                                  CAManagerConnectionMonitorHandler);
90     return CA_STATUS_OK;
91 }
92
93 CAResult_t CASetAutoConnectionDeviceInfo(const char *address)
94 {
95     OIC_LOG(DEBUG, TAG, "CASetAutoConnectionDeviceInfo");
96
97 #if defined(__ANDROID__) && defined(LE_ADAPTER)
98     return CASetLEClientAutoConnectionDeviceInfo(address);
99 #else
100     (void)address;
101     return CA_NOT_SUPPORTED;
102 #endif
103 }
104
105 CAResult_t CAUnsetAutoConnectionDeviceInfo(const char *address)
106 {
107     OIC_LOG(DEBUG, TAG, "CAUnsetAutoConnectionDeviceInfo");
108
109 #if defined(__ANDROID__) && defined(LE_ADAPTER)
110     return CAUnsetLEClientAutoConnectionDeviceInfo(address);
111 #else
112     (void)address;
113     return CA_NOT_SUPPORTED;
114 #endif
115 }
116
117 CAResult_t CASetPortNumberToAssign(CATransportAdapter_t adapter,
118                                    CATransportFlags_t flag, uint16_t port)
119 {
120     uint16_t *targetPort = 0;
121
122     if (CA_ADAPTER_IP & adapter)
123     {
124         if (CA_SECURE & flag)
125         {
126             if (CA_IPV6 & flag)
127             {
128                 targetPort = &caglobals.ports.udp.u6s;
129             }
130             else if (CA_IPV4 & flag)
131             {
132                 targetPort = &caglobals.ports.udp.u4s;
133             }
134         }
135         else
136         {
137             if (CA_IPV6 & flag)
138             {
139                 targetPort = &caglobals.ports.udp.u6;
140             }
141             else if (CA_IPV4 & flag)
142             {
143                 targetPort = &caglobals.ports.udp.u4;
144             }
145         }
146     }
147 #ifdef TCP_ADAPTER
148     if (CA_ADAPTER_TCP & adapter)
149     {
150         if (CA_IPV6 & flag)
151         {
152             targetPort = &caglobals.ports.tcp.u6;
153         }
154         else if (CA_IPV4 & flag)
155         {
156             targetPort = &caglobals.ports.tcp.u4;
157         }
158     }
159 #endif
160
161     if (targetPort)
162     {
163         *targetPort = port;
164         return CA_STATUS_OK;
165     }
166
167     return CA_NOT_SUPPORTED;
168 }
169
170 uint16_t CAGetAssignedPortNumber(CATransportAdapter_t adapter, CATransportFlags_t flag)
171 {
172     OIC_LOG(DEBUG, TAG, "CAGetAssignedPortNumber");
173
174     if (CA_ADAPTER_IP & adapter)
175     {
176         if (CA_SECURE & flag)
177         {
178             if (CA_IPV6 & flag)
179             {
180                 return caglobals.ip.u6s.port;
181             }
182             else if (CA_IPV4 & flag)
183             {
184                 return caglobals.ip.u4s.port;
185             }
186         }
187         else
188         {
189             if (CA_IPV6 & flag)
190             {
191                 return caglobals.ip.u6.port;
192             }
193             else if (CA_IPV4 & flag)
194             {
195                 return caglobals.ip.u4.port;
196             }
197         }
198     }
199 #ifdef TCP_ADAPTER
200     if (CA_ADAPTER_TCP & adapter)
201     {
202         if (CA_IPV6 & flag)
203         {
204             return caglobals.tcp.ipv6.port;
205         }
206         else if (CA_IPV4 & flag)
207         {
208             return caglobals.tcp.ipv4.port;
209         }
210     }
211 #endif
212     return 0;
213 }
214
215 #ifdef __ANDROID__
216 /**
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.
221  */
222 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
223 {
224     OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
225
226     CAResult_t res = CA_STATUS_OK;
227 #ifdef LE_ADAPTER
228     if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context))
229     {
230         OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
231         res = CA_STATUS_FAILED;
232     }
233 #endif
234
235 #ifdef EDR_ADAPTER
236     if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context))
237     {
238         OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
239         res = CA_STATUS_FAILED;
240     }
241 #endif
242     return res;
243 }
244
245 /**
246  * terminate client connection manager
247  * @param[in]   env                   JNI interface pointer.
248  */
249 CAResult_t CAUtilClientTerminate(JNIEnv *env)
250 {
251     OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
252 #ifdef LE_ADAPTER
253     return CAManagerLEClientTerminate(env);
254 #else
255     OIC_LOG(DEBUG, TAG, "it is not supported");
256     (void)env;
257     return CA_NOT_SUPPORTED;
258 #endif
259 }
260
261 // BT pairing
262 CAResult_t CAUtilStartScan(JNIEnv *env)
263 {
264 #ifdef EDR_ADAPTER
265     return CABTPairingStartScan(env);
266 #else
267     OIC_LOG(DEBUG, TAG, "it is not supported");
268     (void)env;
269     return CA_NOT_SUPPORTED;
270 #endif
271 }
272
273 CAResult_t CAUtilStopScan(JNIEnv *env)
274 {
275 #ifdef EDR_ADAPTER
276     return CABTPairingStopScan(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 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
285 {
286 #ifdef EDR_ADAPTER
287     return CABTPairingCreateBond(env, device);
288 #else
289     OIC_LOG(DEBUG, TAG, "it is not supported");
290     (void)env;
291     (void)device;
292     return CA_NOT_SUPPORTED;
293 #endif
294 }
295
296 void CAUtilSetFoundDeviceListener(jobject listener)
297 {
298 #ifdef EDR_ADAPTER
299     CABTPairingSetFoundDeviceListener(listener);
300 #else
301     (void)listener;
302 #endif
303 }
304
305 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount)
306 {
307     OIC_LOG(DEBUG, TAG, "CAUtilSetLEScanInterval");
308 #ifdef LE_ADAPTER
309     CAManagerLESetScanInterval(intervalTime, workingCount);
310     return CA_STATUS_OK;
311 #else
312     OIC_LOG(DEBUG, TAG, "it is not supported");
313     return CA_NOT_SUPPORTED;
314 #endif
315 }
316 #endif