[IOT-1671] Fixed base layer terminate logic
[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 CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB,
31                                            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,
40                                              CAConnectionStateChangedCB connStateCB)
41 {
42     OIC_LOG(DEBUG, TAG, "CAUnregisterNetworkMonitorHandler");
43
44     return CAUnsetNetworkMonitorCallbacks(adapterStateCB, connStateCB);
45 }
46
47 CAResult_t CASetAutoConnectionDeviceInfo(const char *address)
48 {
49     OIC_LOG(DEBUG, TAG, "CASetAutoConnectionDeviceInfo");
50
51 #if defined(__ANDROID__) && defined(LE_ADAPTER)
52     return CASetLEClientAutoConnectionDeviceInfo(address);
53 #else
54     (void)address;
55     return CA_NOT_SUPPORTED;
56 #endif
57 }
58
59 CAResult_t CAUnsetAutoConnectionDeviceInfo(const char *address)
60 {
61     OIC_LOG(DEBUG, TAG, "CAUnsetAutoConnectionDeviceInfo");
62
63 #if defined(__ANDROID__) && defined(LE_ADAPTER)
64     return CAUnsetLEClientAutoConnectionDeviceInfo(address);
65 #else
66     (void)address;
67     return CA_NOT_SUPPORTED;
68 #endif
69 }
70
71 CAResult_t CASetPortNumberToAssign(CATransportAdapter_t adapter,
72                                    CATransportFlags_t flag, uint16_t port)
73 {
74     uint16_t *targetPort = 0;
75
76     if (CA_ADAPTER_IP & adapter)
77     {
78         if (CA_SECURE & flag)
79         {
80             if (CA_IPV6 & flag)
81             {
82                 targetPort = &caglobals.ports.udp.u6s;
83             }
84             else if (CA_IPV4 & flag)
85             {
86                 targetPort = &caglobals.ports.udp.u4s;
87             }
88         }
89         else
90         {
91             if (CA_IPV6 & flag)
92             {
93                 targetPort = &caglobals.ports.udp.u6;
94             }
95             else if (CA_IPV4 & flag)
96             {
97                 targetPort = &caglobals.ports.udp.u4;
98             }
99         }
100     }
101 #ifdef TCP_ADAPTER
102     if (CA_ADAPTER_TCP & adapter)
103     {
104         if (CA_IPV6 & flag)
105         {
106             targetPort = &caglobals.ports.tcp.u6;
107         }
108         else if (CA_IPV4 & flag)
109         {
110             targetPort = &caglobals.ports.tcp.u4;
111         }
112     }
113 #endif
114
115     if (targetPort)
116     {
117         *targetPort = port;
118         return CA_STATUS_OK;
119     }
120
121     return CA_NOT_SUPPORTED;
122 }
123
124 uint16_t CAGetAssignedPortNumber(CATransportAdapter_t adapter, CATransportFlags_t flag)
125 {
126     OIC_LOG(DEBUG, TAG, "CAGetAssignedPortNumber");
127
128     if (CA_ADAPTER_IP & adapter)
129     {
130         if (CA_SECURE & flag)
131         {
132             if (CA_IPV6 & flag)
133             {
134                 return caglobals.ip.u6s.port;
135             }
136             else if (CA_IPV4 & flag)
137             {
138                 return caglobals.ip.u4s.port;
139             }
140         }
141         else
142         {
143             if (CA_IPV6 & flag)
144             {
145                 return caglobals.ip.u6.port;
146             }
147             else if (CA_IPV4 & flag)
148             {
149                 return caglobals.ip.u4.port;
150             }
151         }
152     }
153 #ifdef TCP_ADAPTER
154     if (CA_ADAPTER_TCP & adapter)
155     {
156         if (CA_IPV6 & flag)
157         {
158             return caglobals.tcp.ipv6.port;
159         }
160         else if (CA_IPV4 & flag)
161         {
162             return caglobals.tcp.ipv4.port;
163         }
164     }
165 #endif
166     return 0;
167 }
168
169 #ifdef __ANDROID__
170 /**
171  * initialize client connection manager
172  * @param[in]   env                   JNI interface pointer.
173  * @param[in]   jvm                   invocation inferface for JAVA virtual machine.
174  * @param[in]   context               application context.
175  */
176 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
177 {
178     OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
179
180     CAResult_t res = CA_STATUS_OK;
181 #ifdef LE_ADAPTER
182     if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context))
183     {
184         OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
185         res = CA_STATUS_FAILED;
186     }
187 #endif
188
189 #ifdef EDR_ADAPTER
190     if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context))
191     {
192         OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
193         res = CA_STATUS_FAILED;
194     }
195 #endif
196
197 #if !defined(LE_ADAPTER) && !defined(EDR_ADAPTER)
198     (void)env;
199     (void)jvm;
200     (void)context;
201 #endif
202     return res;
203 }
204
205 /**
206  * terminate client connection manager
207  * @param[in]   env                   JNI interface pointer.
208  */
209 CAResult_t CAUtilClientTerminate(JNIEnv *env)
210 {
211     OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
212 #ifdef LE_ADAPTER
213     return CAManagerLEClientTerminate(env);
214 #else
215     OIC_LOG(DEBUG, TAG, "it is not supported");
216     (void)env;
217     return CA_NOT_SUPPORTED;
218 #endif
219 }
220
221 // BT pairing
222 CAResult_t CAUtilStartScan(JNIEnv *env)
223 {
224 #ifdef EDR_ADAPTER
225     return CABTPairingStartScan(env);
226 #else
227     OIC_LOG(DEBUG, TAG, "it is not supported");
228     (void)env;
229     return CA_NOT_SUPPORTED;
230 #endif
231 }
232
233 CAResult_t CAUtilStopScan(JNIEnv *env)
234 {
235 #ifdef EDR_ADAPTER
236     return CABTPairingStopScan(env);
237 #else
238     OIC_LOG(DEBUG, TAG, "it is not supported");
239     (void)env;
240     return CA_NOT_SUPPORTED;
241 #endif
242 }
243
244 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
245 {
246 #ifdef EDR_ADAPTER
247     return CABTPairingCreateBond(env, device);
248 #else
249     OIC_LOG(DEBUG, TAG, "it is not supported");
250     (void)env;
251     (void)device;
252     return CA_NOT_SUPPORTED;
253 #endif
254 }
255
256 void CAUtilSetFoundDeviceListener(jobject listener)
257 {
258 #ifdef EDR_ADAPTER
259     CABTPairingSetFoundDeviceListener(listener);
260 #else
261     (void)listener;
262 #endif
263 }
264
265 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount)
266 {
267     OIC_LOG(DEBUG, TAG, "CAUtilSetLEScanInterval");
268 #ifdef LE_ADAPTER
269     CAManagerLESetScanInterval(intervalTime, workingCount);
270     return CA_STATUS_OK;
271 #else
272     (void)intervalTime;
273     (void)workingCount;
274     OIC_LOG(DEBUG, TAG, "it is not supported");
275     return CA_NOT_SUPPORTED;
276 #endif
277 }
278 #endif