2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
4 * @author: Abhay Agarwal <ay.agarwal@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include "wifi-location-plugin.h"
25 #include "wifi-location-plugin-util.h"
27 #define NAME "wifi-location-plugin"
28 #define AUTHOR "Samsung"
31 #define DETECTION_WINDOW_DEFAULT 55 /* 55 seconds */
33 #define DETECTION_STARTED 1
34 #define DETECTION_STOPPED 0
36 static unsigned int wifi_location_detection_window = DETECTION_WINDOW_DEFAULT;
37 static const uas_callbacks_t *uas_cbs = NULL;
38 static GSList *dev_list = NULL;
39 static int wifi_location_detection_type = 0;
40 static guint stop_scan_timer = 0;
42 static void __report_absence(gpointer data, gpointer user_data)
46 /* TODO: Implement this function */
51 static void __reset_devices(gpointer data, gpointer user_data)
53 /* TODO: Implement this function */
57 static int __stop_scan()
60 int ret = UAS_STATUS_SUCCESS;
62 g_slist_foreach(dev_list, __reset_devices, NULL);
65 if (UAS_STATUS_SUCCESS != ret) {
66 UA_WIFI_LOCATION_ERR("Fail to stop wifi_location scan [%d]", ret);
70 /* Send detection stopped */
71 if (uas_cbs && uas_cbs->detection_state_cb) {
72 uas_cbs->detection_state_cb(PLUGIN_ID, DETECTION_STOPPED);
74 UA_WIFI_LOCATION_ERR("uas_cbs not ready");
77 /* Reset detection flags */
78 wifi_location_detection_type = 0;
84 static gboolean __stop_scan_expired_timer_cb(gpointer user_data)
86 int ret = UAS_STATUS_SUCCESS;
90 /* Report absence for undetected devices */
91 g_slist_foreach(dev_list, __report_absence, NULL);
95 retv_if_with_log(UAS_STATUS_SUCCESS != ret, FALSE, "return %d", ret);
100 static int __start_scan(gpointer user_data, int scan_mode)
103 int ret = UAS_STATUS_SUCCESS;
105 ret = _wifi_location_plugin_start_scan(scan_mode);
106 retv_if_with_log(UAS_STATUS_SUCCESS != ret, ret, "return %d", ret);
108 if (0 < stop_scan_timer)
109 g_source_remove(stop_scan_timer);
110 stop_scan_timer = g_timeout_add_seconds(wifi_location_detection_window,
111 __stop_scan_expired_timer_cb, NULL);
113 /* Send detection started */
114 if (uas_cbs && uas_cbs->detection_state_cb) {
115 uas_cbs->detection_state_cb(PLUGIN_ID, DETECTION_STARTED);
117 UA_WIFI_LOCATION_ERR("uas_cbs not ready");
118 ret = UAS_STATUS_FAIL;
125 static int init(const uas_callbacks_t *callbacks)
128 int ret = UAS_STATUS_SUCCESS;
131 return UAS_STATUS_FAIL;
133 retv_if(NULL != uas_cbs, UAS_STATUS_ALREADY_DONE);
135 /* TODO: Check wifi location system support */
137 /* Initialize wifi-location */
138 ret = wifi_location_initialize();
139 retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
140 UAS_STATUS_FAIL, "return %d", ret);
145 return UAS_STATUS_SUCCESS;
148 static int deinit(void)
151 int ret = UAS_STATUS_SUCCESS;
154 g_slist_free_full(dev_list, g_free);
158 /* Deinitialize wifi-location */
159 ret = wifi_location_deinitialize();
160 retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
161 UAS_STATUS_FAIL, "return %d", ret);
166 return UAS_STATUS_SUCCESS;
169 static int get_state(int *state)
172 int ret = UAS_STATUS_SUCCESS;
174 retv_if(NULL == uas_cbs, UAS_STATUS_NOT_READY);
175 retv_if(NULL == state, UAS_STATUS_FAIL);
177 /* TODO: Get wifi-location state */
178 *state = UAS_STATE_READY;
184 static int get_capability(uas_capability_e *capability)
188 retv_if(NULL == uas_cbs, UAS_STATUS_NOT_READY);
189 retv_if(NULL == capability, UAS_STATUS_FAIL);
191 *capability = UAS_SUPPORT_USER;
194 return UAS_STATUS_SUCCESS;
197 static int start_detection(unsigned int detection_type, int scan_mode)
201 int ret = UAS_STATUS_SUCCESS;
203 retv_if(NULL == uas_cbs, UAS_STATUS_NOT_READY);
204 retv_if(detection_type == (wifi_location_detection_type & detection_type),
205 UAS_STATUS_ALREADY_DONE);
207 /* Check if detection already in progress */
208 if (0 != wifi_location_detection_type)
211 /* Start detection */
212 ret = __start_scan(NULL, scan_mode);
213 retv_if_with_log(UAS_STATUS_SUCCESS != ret, ret, "return %d", ret);
216 wifi_location_detection_type |= detection_type;
217 UA_WIFI_LOCATION_INFO_C("wifi_location_detection_type = 0x%8.8X", wifi_location_detection_type);
223 static uas_api_t wifi_location_api = {
226 .get_state = get_state,
227 .get_capability = get_capability,
228 .set_registered_devices = NULL,
230 .remove_device = NULL,
231 .start_detection = start_detection,
232 .stop_detection = NULL,
233 .set_low_power_mode = NULL,
234 .set_detection_window = NULL,
235 .set_detection_threshold = NULL,
236 .scan_active_devices = NULL,
237 .cancel_active_device_scan = NULL,
238 .add_ibeacon_adv = NULL,
241 static int module_init(uas_api_t **api)
245 *api = &wifi_location_api;
251 static int module_deinit(void)
259 UAS_MODULE_ADD(UAS_PLUGIN_ID_WIFI_LOCATION, NAME, AUTHOR, VERSION, module_init, module_deinit);