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 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     return res;
189 }
190
191 /**
192  * terminate client connection manager
193  * @param[in]   env                   JNI interface pointer.
194  */
195 CAResult_t CAUtilClientTerminate(JNIEnv *env)
196 {
197     OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
198 #ifdef LE_ADAPTER
199     return CAManagerLEClientTerminate(env);
200 #else
201     OIC_LOG(DEBUG, TAG, "it is not supported");
202     (void)env;
203     return CA_NOT_SUPPORTED;
204 #endif
205 }
206
207 // BT pairing
208 CAResult_t CAUtilStartScan(JNIEnv *env)
209 {
210 #ifdef EDR_ADAPTER
211     return CABTPairingStartScan(env);
212 #else
213     OIC_LOG(DEBUG, TAG, "it is not supported");
214     (void)env;
215     return CA_NOT_SUPPORTED;
216 #endif
217 }
218
219 CAResult_t CAUtilStopScan(JNIEnv *env)
220 {
221 #ifdef EDR_ADAPTER
222     return CABTPairingStopScan(env);
223 #else
224     OIC_LOG(DEBUG, TAG, "it is not supported");
225     (void)env;
226     return CA_NOT_SUPPORTED;
227 #endif
228 }
229
230 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
231 {
232 #ifdef EDR_ADAPTER
233     return CABTPairingCreateBond(env, device);
234 #else
235     OIC_LOG(DEBUG, TAG, "it is not supported");
236     (void)env;
237     (void)device;
238     return CA_NOT_SUPPORTED;
239 #endif
240 }
241
242 void CAUtilSetFoundDeviceListener(jobject listener)
243 {
244 #ifdef EDR_ADAPTER
245     CABTPairingSetFoundDeviceListener(listener);
246 #else
247     (void)listener;
248 #endif
249 }
250
251 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount)
252 {
253     OIC_LOG(DEBUG, TAG, "CAUtilSetLEScanInterval");
254 #ifdef LE_ADAPTER
255     CAManagerLESetScanInterval(intervalTime, workingCount);
256     return CA_STATUS_OK;
257 #else
258     OIC_LOG(DEBUG, TAG, "it is not supported");
259     return CA_NOT_SUPPORTED;
260 #endif
261 }
262 #endif