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