Imported Upstream version 1.1.1
[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)
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->adapter, info->addr, 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->adapter, info->addr, 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 #ifdef __ANDROID__
118 /**
119  * initialize client connection manager
120  * @param[in]   env                   JNI interface pointer.
121  * @param[in]   jvm                   invocation inferface for JAVA virtual machine.
122  * @param[in]   context               application context.
123  */
124 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
125 {
126     OIC_LOG(DEBUG, TAG, "CAUtilClientInitialize");
127
128     CAResult_t res = CA_STATUS_OK;
129 #ifdef LE_ADAPTER
130     if (CA_STATUS_OK != CAManagerLEClientInitialize(env, jvm, context))
131     {
132         OIC_LOG(ERROR, TAG, "CAManagerLEClientInitialize has failed");
133         res = CA_STATUS_FAILED;
134     }
135 #endif
136
137 #ifdef EDR_ADAPTER
138     if (CA_STATUS_OK != CABTPairingInitialize(env, jvm, context))
139     {
140         OIC_LOG(ERROR, TAG, "CABTPairingInitialize has failed");
141         res = CA_STATUS_FAILED;
142     }
143 #endif
144     return res;
145 }
146
147 /**
148  * terminate client connection manager
149  * @param[in]   env                   JNI interface pointer.
150  */
151 CAResult_t CAUtilClientTerminate(JNIEnv *env)
152 {
153     OIC_LOG(DEBUG, TAG, "CAUtilClientTerminate");
154 #ifdef LE_ADAPTER
155     return CAManagerLEClientTerminate(env);
156 #else
157     OIC_LOG(DEBUG, TAG, "it is not supported");
158     (void)env;
159     return CA_NOT_SUPPORTED;
160 #endif
161 }
162
163 // BT pairing
164 CAResult_t CAUtilStartScan(JNIEnv *env)
165 {
166 #ifdef EDR_ADAPTER
167     return CABTPairingStartScan(env);
168 #else
169     OIC_LOG(DEBUG, TAG, "it is not supported");
170     (void)env;
171     return CA_NOT_SUPPORTED;
172 #endif
173 }
174
175 CAResult_t CAUtilStopScan(JNIEnv *env)
176 {
177 #ifdef EDR_ADAPTER
178     return CABTPairingStopScan(env);
179 #else
180     OIC_LOG(DEBUG, TAG, "it is not supported");
181     (void)env;
182     return CA_NOT_SUPPORTED;
183 #endif
184 }
185
186 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device)
187 {
188 #ifdef EDR_ADAPTER
189     return CABTPairingCreateBond(env, device);
190 #else
191     OIC_LOG(DEBUG, TAG, "it is not supported");
192     (void)env;
193     (void)device;
194     return CA_NOT_SUPPORTED;
195 #endif
196 }
197
198 void CAUtilSetFoundDeviceListener(jobject listener)
199 {
200 #ifdef EDR_ADAPTER
201     CABTPairingSetFoundDeviceListener(listener);
202 #else
203     (void)listener;
204 #endif
205 }
206 #endif