Fix tizen build issue.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / calenwmonitor.c
1 /******************************************************************
2 *
3 * Copyright 2014 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 "caleinterface.h"
22
23 #include<stdio.h>
24 #include<stdlib.h>
25 #include<string.h>
26 #include<glib.h>
27 #include<arpa/inet.h>
28 #include<sys/types.h>
29 #include<sys/socket.h>
30 #include<netinet/in.h>
31
32 #include <bluetooth.h>
33 #include <bluetooth_internal.h>
34 #include <bluetooth_type.h>
35
36
37 #include "camutex.h"
38 #include "caleadapter.h"
39 #include "caadapterutils.h"
40
41 /**
42  * Logging tag for module name
43  */
44 #define TAG "OIC_CA_LE_MONITOR"
45
46 static GMainLoop *g_mainloop = NULL;
47 static ca_thread_pool_t g_threadPoolHandle = NULL;
48
49 /**
50  * Maintains the callback to be notified on device state changed.
51  */
52 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
53
54 /**
55  * Mutex to synchronize access to the deviceStateChanged Callback when the state
56  *           of the LE adapter gets change.
57  */
58 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
59
60 /**
61 * This is the callback which will be called when the adapter state gets changed.
62 *
63 * @param result         [IN] Result of the query done to the platform.
64 * @param adapter_state  [IN] State of the LE adapter.
65 * @param user_data      [IN] Any user_data passed by the caller when querying for the state changed cb.
66 *
67 * @return  None.
68 */
69 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
70                         void *user_data);
71
72 void *CALEMainLoopThread (void *param)
73 {
74     g_main_loop_run(g_mainloop);
75     return NULL;
76 }
77
78 CAResult_t CAInitializeLENetworkMonitor()
79 {
80     OIC_LOG(DEBUG, TAG, "IN");
81
82     if (NULL == g_bleDeviceStateChangedCbMutex)
83     {
84         g_bleDeviceStateChangedCbMutex = ca_mutex_new();
85         if (NULL == g_bleDeviceStateChangedCbMutex)
86         {
87             OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
88             return CA_STATUS_FAILED;
89         }
90     }
91     OIC_LOG(DEBUG, TAG, "OUT");
92
93     return CA_STATUS_OK;
94 }
95
96 void CATerminateLENetworkMonitor()
97 {
98     OIC_LOG(DEBUG, TAG, "IN");
99
100     ca_mutex_free(g_bleDeviceStateChangedCbMutex);
101     g_bleDeviceStateChangedCbMutex = NULL;
102
103     OIC_LOG(DEBUG, TAG, "OUT");
104 }
105
106 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
107 {
108     OIC_LOG(DEBUG, TAG, "IN");
109     g_threadPoolHandle = threadPool;
110     OIC_LOG(DEBUG, TAG, "OUT");
111     return CA_STATUS_OK;
112 }
113
114 CAResult_t CAStartLEAdapter()
115 {
116     OIC_LOG(DEBUG, TAG, "IN");
117     g_mainloop = g_main_loop_new(NULL, 0);
118     if(!g_mainloop)
119     {
120         OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
121         return CA_STATUS_FAILED;
122     }
123
124     if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL))
125     {
126         OIC_LOG(ERROR, TAG, "Failed to create thread!");
127         return CA_STATUS_FAILED;
128     }
129
130     int ret = bt_initialize();
131     if (0 != ret)
132     {
133         OIC_LOG(ERROR, TAG, "bt_initialize failed");
134         return CA_STATUS_FAILED;
135     }
136
137     ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
138     if (0 != ret)
139     {
140         OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
141         return CA_STATUS_FAILED;
142     }
143
144     ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
145     if (BT_ERROR_NONE != ret)
146     {
147         OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
148         return CA_STATUS_FAILED;
149     }
150
151     OIC_LOG(DEBUG, TAG, "OUT");
152     return CA_STATUS_OK;
153 }
154
155 CAResult_t CAStopLEAdapter()
156 {
157
158     int ret = bt_adapter_unset_state_changed_cb();
159     if (BT_ERROR_NONE != ret)
160     {
161         OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
162         return CA_STATUS_FAILED;
163     }
164
165     ret = bt_deinitialize();
166     if (0 != ret)
167     {
168         OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
169         return CA_STATUS_FAILED;
170     }
171
172     if (g_mainloop)
173     {
174         g_main_loop_quit(g_mainloop);
175     }
176     return CA_STATUS_OK;
177 }
178
179 CAResult_t CAGetLEAdapterState()
180 {
181     OIC_LOG(DEBUG, TAG, "IN");
182
183     bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
184
185     //Get Bluetooth adapter state
186     int ret = bt_adapter_get_state(&adapterState);
187     if (BT_ERROR_NONE != ret)
188     {
189         OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
190                   ret);
191         return CA_STATUS_FAILED;
192     }
193
194     if (BT_ADAPTER_ENABLED != adapterState)
195     {
196         OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
197         return CA_ADAPTER_NOT_ENABLED;
198     }
199
200     OIC_LOG(DEBUG, TAG, "OUT");
201     return CA_STATUS_OK;
202 }
203
204 CAResult_t CAGetLEAddress(char **local_address)
205 {
206     OIC_LOG(DEBUG, TAG, "IN");
207
208     VERIFY_NON_NULL(local_address, TAG, "local_address is null")
209
210     char *address = NULL;
211
212     int ret = bt_adapter_get_address(&address);
213     if (BT_ERROR_NONE != ret || !address)
214     {
215         OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
216                   ret);
217         return CA_STATUS_FAILED;
218     }
219
220     OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
221
222     *local_address = address;
223
224     OIC_LOG(DEBUG, TAG, "OUT");
225
226     return CA_STATUS_OK;
227 }
228
229 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
230 {
231     OIC_LOG(DEBUG, TAG, "IN");
232     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
233     g_bleDeviceStateChangedCallback = callback;
234     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
235     OIC_LOG(DEBUG, TAG, "OUT");
236     return CA_STATUS_OK;
237 }
238
239 CAResult_t CAUnSetLEAdapterStateChangedCb()
240 {
241     OIC_LOG(DEBUG, TAG, "IN");
242     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
243     g_bleDeviceStateChangedCallback = NULL;
244     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
245     OIC_LOG(DEBUG, TAG, "OUT");
246     return CA_STATUS_OK;
247 }
248
249 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
250                                           void *user_data)
251 {
252     OIC_LOG(DEBUG, TAG, "IN");
253
254     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
255
256     if (NULL == g_bleDeviceStateChangedCallback)
257     {
258         OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
259         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
260         return;
261     }
262
263     if (BT_ADAPTER_DISABLED == adapter_state)
264     {
265         OIC_LOG(DEBUG, TAG, "Adapter is disabled");
266         g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
267         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
268         return;
269     }
270
271     OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
272     g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
273     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
274
275     OIC_LOG(DEBUG, TAG, "OUT");
276 }