0365cf2f14b7eee1f316a3a37f2cf18276eb4609
[platform/core/connectivity/ua-plugin-wifi-location.git] / src / wifi-location-plugin-scan.c
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * @author: Abhay Agarwal <ay.agarwal@samsung.com>
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24
25 #include <ua-plugin.h>
26
27 #include "wifi-location-plugin.h"
28 #include "wifi-location-plugin-util.h"
29
30 static wifi_location_request_h g_request;
31
32 int _wifi_location_plugin_initialize(void)
33 {
34         FUNC_ENTER;
35         int ret = UAS_STATUS_SUCCESS;
36
37         ret = wifi_location_initialize();
38         retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
39                         UAS_STATUS_FAIL, "return %d", ret);
40
41         g_request = NULL;
42
43         FUNC_EXIT;
44         return UAS_STATUS_SUCCESS;
45 }
46
47 int _wifi_location_plugin_deinitialize(void)
48 {
49         FUNC_ENTER;
50         int ret = UAS_STATUS_SUCCESS;
51
52         if (g_request) {
53                 ret = wifi_location_request_destroy(g_request);
54                 if (WIFI_LOCATION_ERROR_NONE != ret)
55                         UA_WIFI_LOCATION_ERR(
56                                 "wifi_location_request_destroy failed");
57         }
58
59         ret = wifi_location_deinitialize();
60         retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
61                         UAS_STATUS_FAIL, "return %d", ret);
62
63         FUNC_EXIT;
64         return UAS_STATUS_SUCCESS;
65 }
66
67 gboolean _wifi_location_plugin_add_device(gpointer data)
68 {
69         FUNC_ENTER;
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;
75
76         retv_if(NULL == dev, FALSE);
77
78         if (!g_request) {
79                 /* create request */
80                 ret = wifi_location_request_create(&g_request);
81
82                 if(WIFI_LOCATION_ERROR_NONE != ret || !g_request) {
83                         UA_WIFI_LOCATION_ERR(
84                                 "wifi_location_request_create failed");
85                         status = UAS_STATUS_FAIL;
86                         goto done;
87                 }
88         }
89
90         /* Add device */
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;
95         else
96                 UA_WIFI_LOCATION_ERR(
97                         "wifi_location_request_add_peer failed");
98
99 done:
100         _wifi_location_plugin_handle_device_added(status, dev);
101         g_free(dev);
102         FUNC_EXIT;
103         return FALSE;
104 }
105
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,
110         void *user_data)
111 {
112         FUNC_ENTER;
113         unsigned char *mac = NULL;
114         int distance_mm = 0;
115         int ret;
116
117         if(WIFI_LOCATION_ERROR_NONE != error) {
118                 UA_WIFI_LOCATION_ERR("failed error %d", error);
119                 return;
120         }
121
122         if (!result) {
123                 UA_WIFI_LOCATION_ERR("No result found");
124                 return;
125         }
126
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);
131         else
132                 UA_WIFI_LOCATION_DBG("Result received for mac %s", mac);
133
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);
138         else
139                 UA_WIFI_LOCATION_DBG("Result distance(mm) %d", distance_mm);
140
141         FUNC_EXIT;
142 }
143
144 int _wifi_location_plugin_start_scan(int scan_mode) {
145         FUNC_ENTER;
146         int ret = UAS_STATUS_SUCCESS;
147
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);
152
153         FUNC_EXIT;
154         return ret;
155 }
156
157 int _wifi_location_plugin_stop_scan(void) {
158         FUNC_ENTER;
159         int ret = UAS_STATUS_SUCCESS;
160
161         ret = wifi_location_cancel(g_request);
162         retv_if_with_log(WIFI_LOCATION_ERROR_NONE != ret,
163                 UAS_STATUS_FAIL, "return %d", ret);
164
165         FUNC_EXIT;
166         return ret;
167 }