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.
25 #include <ua-plugin.h>
27 #include "wifi-location-plugin.h"
28 #include "wifi-location-plugin-util.h"
30 static wifi_location_request_h g_request;
32 int _wifi_location_plugin_initialize(void)
35 int ret = UAS_STATUS_SUCCESS;
37 ret = wifi_location_initialize();
38 retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
39 UAS_STATUS_FAIL, "return %d", ret);
44 return UAS_STATUS_SUCCESS;
47 int _wifi_location_plugin_deinitialize(void)
50 int ret = UAS_STATUS_SUCCESS;
53 ret = wifi_location_request_destroy(g_request);
54 if (WIFI_LOCATION_ERROR_NONE != ret)
56 "wifi_location_request_destroy failed");
59 ret = wifi_location_deinitialize();
60 retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
61 UAS_STATUS_FAIL, "return %d", ret);
64 return UAS_STATUS_SUCCESS;
67 gboolean _wifi_location_plugin_add_device(gpointer data)
70 uas_wifi_location_info_t *dev = data;
71 int status = UAS_STATUS_FAIL;
72 int ret = WIFI_LOCATION_ERROR_NONE;
73 wifi_location_peer_type_e peer_type =
74 WIFI_LOCATION_PEER_TYPE_WIFI_AWARE;
76 retv_if(NULL == dev, FALSE);
80 ret = wifi_location_request_create(&g_request);
82 if(WIFI_LOCATION_ERROR_NONE != ret || !g_request) {
84 "wifi_location_request_create failed");
85 status = UAS_STATUS_FAIL;
91 ret = wifi_location_request_add_peer(g_request,
92 (unsigned char*)dev->mac_addr, peer_type);
93 if (WIFI_LOCATION_ERROR_NONE == ret)
94 status = UAS_STATUS_SUCCESS;
97 "wifi_location_request_add_peer failed");
100 _wifi_location_plugin_handle_device_added(status, dev);
106 static void __wifi_location_result_received_cb(
107 wifi_location_request_h request,
108 wifi_location_error_e error,
109 wifi_location_result_h result,
113 unsigned char *mac = NULL;
117 if(WIFI_LOCATION_ERROR_NONE != error) {
118 UA_WIFI_LOCATION_ERR("failed error %d", error);
123 UA_WIFI_LOCATION_ERR("No result found");
127 ret = wifi_location_result_get_mac(result, &mac);
128 if(WIFI_LOCATION_ERROR_NONE != ret)
129 UA_WIFI_LOCATION_ERR("wifi_location_result_get_mac " \
130 "failed error %d", ret);
132 UA_WIFI_LOCATION_DBG("Result received for mac %s", mac);
134 ret = wifi_location_result_get_distance_mm(result, &distance_mm);
135 if(WIFI_LOCATION_ERROR_NONE != ret)
136 UA_WIFI_LOCATION_ERR("wifi_location_result_get_distance_mm " \
137 "failed error %d", ret);
139 UA_WIFI_LOCATION_DBG("Result distance(mm) %d", distance_mm);
144 int _wifi_location_plugin_start_scan(int scan_mode) {
146 int ret = UAS_STATUS_SUCCESS;
148 ret = wifi_location_start(g_request,
149 __wifi_location_result_received_cb, NULL);
150 retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
151 UAS_STATUS_FAIL, "return %d", ret);
157 int _wifi_location_plugin_stop_scan(void) {
159 int ret = UAS_STATUS_SUCCESS;
161 ret = wifi_location_cancel(g_request);
162 retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
163 UAS_STATUS_FAIL, "return %d", ret);