1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Portions copyright (C) 2023 Broadcom Limited
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 #include <stdint.h>
20 #include <fcntl.h>
21 #include <sys/socket.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/ctrl.h>
25 #include <linux/rtnetlink.h>
26 #include <netpacket/packet.h>
27 #include <linux/filter.h>
28 #include <linux/errqueue.h>
29 
30 #include <linux/pkt_sched.h>
31 #include <netlink/object-api.h>
32 #include <netlink/netlink.h>
33 #include <netlink/socket.h>
34 #include <netlink/handlers.h>
35 
36 #include "sync.h"
37 
38 #define LOG_TAG  "WifiHAL"
39 //#define LOG_NDEBUG 0         //uncomment to enable verbose logging
40 
41 #include <log/log.h>
42 
43 #include <hardware_legacy/wifi_hal.h>
44 #include "common.h"
45 #include "cpp_bindings.h"
46 
47 typedef enum {
48 
49     GSCAN_ATTRIBUTE_NUM_BUCKETS = 10,
50     GSCAN_ATTRIBUTE_BASE_PERIOD,
51     GSCAN_ATTRIBUTE_BUCKETS_BAND,
52     GSCAN_ATTRIBUTE_BUCKET_ID,
53     GSCAN_ATTRIBUTE_BUCKET_PERIOD,
54     GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS,
55     GSCAN_ATTRIBUTE_BUCKET_CHANNELS,
56     GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN,
57     GSCAN_ATTRIBUTE_REPORT_THRESHOLD,
58     GSCAN_ATTRIBUTE_NUM_SCANS_TO_CACHE,
59     GSCAN_ATTRIBUTE_BAND = GSCAN_ATTRIBUTE_BUCKETS_BAND,
60 
61     GSCAN_ATTRIBUTE_ENABLE_FEATURE = 20,
62     GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE,              /* indicates no more results */
63     GSCAN_ATTRIBUTE_FLUSH_FEATURE,                      /* Flush all the configs */
64     GSCAN_ENABLE_FULL_SCAN_RESULTS,
65     GSCAN_ATTRIBUTE_REPORT_EVENTS,
66 
67     /* remaining reserved for additional attributes */
68     GSCAN_ATTRIBUTE_NUM_OF_RESULTS = 30,
69     GSCAN_ATTRIBUTE_FLUSH_RESULTS,
70     GSCAN_ATTRIBUTE_SCAN_RESULTS,                       /* flat array of wifi_scan_result */
71     GSCAN_ATTRIBUTE_SCAN_ID,                            /* indicates scan number */
72     GSCAN_ATTRIBUTE_SCAN_FLAGS,                         /* indicates if scan was aborted */
73     GSCAN_ATTRIBUTE_AP_FLAGS,                           /* flags on significant change event */
74     GSCAN_ATTRIBUTE_NUM_CHANNELS,
75     GSCAN_ATTRIBUTE_CHANNEL_LIST,
76     GSCAN_ATTRIBUTE_CH_BUCKET_BITMASK,
77     /* remaining reserved for additional attributes */
78 
79     GSCAN_ATTRIBUTE_SSID = 40,
80     GSCAN_ATTRIBUTE_BSSID,
81     GSCAN_ATTRIBUTE_CHANNEL,
82     GSCAN_ATTRIBUTE_RSSI,
83     GSCAN_ATTRIBUTE_TIMESTAMP,
84     GSCAN_ATTRIBUTE_RTT,
85     GSCAN_ATTRIBUTE_RTTSD,
86 
87     /* remaining reserved for additional attributes */
88 
89     GSCAN_ATTRIBUTE_HOTLIST_BSSIDS = 50,
90     GSCAN_ATTRIBUTE_RSSI_LOW,
91     GSCAN_ATTRIBUTE_RSSI_HIGH,
92     GSCAN_ATTRIBUTE_HOTLIST_ELEM,
93     GSCAN_ATTRIBUTE_HOTLIST_FLUSH,
94     GSCAN_ATTRIBUTE_HOTLIST_BSSID_COUNT,
95 
96     /* remaining reserved for additional attributes */
97     GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE = 60,
98     GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE,
99     GSCAN_ATTRIBUTE_MIN_BREACHING,
100     GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS,
101     GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_FLUSH,
102 
103     /* EPNO */
104     GSCAN_ATTRIBUTE_EPNO_SSID_LIST = 70,
105     GSCAN_ATTRIBUTE_EPNO_SSID,
106     GSCAN_ATTRIBUTE_EPNO_SSID_LEN,
107     GSCAN_ATTRIBUTE_EPNO_RSSI,
108     GSCAN_ATTRIBUTE_EPNO_FLAGS,
109     GSCAN_ATTRIBUTE_EPNO_AUTH,
110     GSCAN_ATTRIBUTE_EPNO_SSID_NUM,
111     GSCAN_ATTRIBUTE_EPNO_FLUSH,
112 
113     /* remaining reserved for additional attributes */
114 
115     GSCAN_ATTRIBUTE_WHITELIST_SSID = 80,
116     GSCAN_ATTRIBUTE_NUM_WL_SSID,
117     GSCAN_ATTRIBUTE_WL_SSID_LEN,
118     GSCAN_ATTRIBUTE_WL_SSID_FLUSH,
119     GSCAN_ATTRIBUTE_WHITELIST_SSID_ELEM,
120     GSCAN_ATTRIBUTE_NUM_BSSID,
121     GSCAN_ATTRIBUTE_BSSID_PREF_LIST,
122     GSCAN_ATTRIBUTE_BSSID_PREF_FLUSH,
123     GSCAN_ATTRIBUTE_BSSID_PREF,
124     GSCAN_ATTRIBUTE_RSSI_MODIFIER,
125 
126     /* remaining reserved for additional attributes */
127 
128     GSCAN_ATTRIBUTE_A_BAND_BOOST_THRESHOLD = 90,
129     GSCAN_ATTRIBUTE_A_BAND_PENALTY_THRESHOLD,
130     GSCAN_ATTRIBUTE_A_BAND_BOOST_FACTOR,
131     GSCAN_ATTRIBUTE_A_BAND_PENALTY_FACTOR,
132     GSCAN_ATTRIBUTE_A_BAND_MAX_BOOST,
133     GSCAN_ATTRIBUTE_LAZY_ROAM_HYSTERESIS,
134     GSCAN_ATTRIBUTE_ALERT_ROAM_RSSI_TRIGGER,
135     GSCAN_ATTRIBUTE_LAZY_ROAM_ENABLE,
136 
137     /* BSSID blacklist */
138     GSCAN_ATTRIBUTE_BSSID_BLACKLIST_FLUSH = 100,
139     GSCAN_ATTRIBUTE_BLACKLIST_BSSID,
140 
141     /* ANQPO */
142     GSCAN_ATTRIBUTE_ANQPO_HS_LIST = 110,
143     GSCAN_ATTRIBUTE_ANQPO_HS_LIST_SIZE,
144     GSCAN_ATTRIBUTE_ANQPO_HS_NETWORK_ID,
145     GSCAN_ATTRIBUTE_ANQPO_HS_NAI_REALM,
146     GSCAN_ATTRIBUTE_ANQPO_HS_ROAM_CONSORTIUM_ID,
147     GSCAN_ATTRIBUTE_ANQPO_HS_PLMN,
148 
149     /* Adaptive scan attributes */
150     GSCAN_ATTRIBUTE_BUCKET_STEP_COUNT = 120,
151     GSCAN_ATTRIBUTE_BUCKET_MAX_PERIOD,
152 
153     /* ePNO cfg */
154     GSCAN_ATTRIBUTE_EPNO_5G_RSSI_THR = 130,
155     GSCAN_ATTRIBUTE_EPNO_2G_RSSI_THR,
156     GSCAN_ATTRIBUTE_EPNO_INIT_SCORE_MAX,
157     GSCAN_ATTRIBUTE_EPNO_CUR_CONN_BONUS,
158     GSCAN_ATTRIBUTE_EPNO_SAME_NETWORK_BONUS,
159     GSCAN_ATTRIBUTE_EPNO_SECURE_BONUS,
160     GSCAN_ATTRIBUTE_EPNO_5G_BONUS,
161 
162     /* Roaming features */
163     GSCAN_ATTRIBUTE_ROAM_STATE_SET = 140,
164     GSCAN_ATTRIBUTE_MAX
165 
166 } GSCAN_ATTRIBUTE;
167 
168 typedef struct {
169     int num_bssid;                          // number of blacklisted BSSIDs
170     mac_addr bssids[MAX_BLACKLIST_BSSID];   // blacklisted BSSIDs
171 } wifi_bssid_params;
172 
173 // helper methods
174 wifi_error wifi_enable_full_scan_results(wifi_request_id id, wifi_interface_handle iface,
175          wifi_scan_result_handler handler);
176 wifi_error wifi_disable_full_scan_results(wifi_request_id id, wifi_interface_handle iface);
177 int wifi_handle_full_scan_event(wifi_request_id id, WifiEvent& event,
178          wifi_scan_result_handler handler);
179 void convert_to_hal_result(wifi_scan_result *to, wifi_gscan_result_t *from);
180 
181 
convert_to_hal_result(wifi_scan_result * to,wifi_gscan_result_t * from)182 void convert_to_hal_result(wifi_scan_result *to, wifi_gscan_result_t *from)
183 {
184     to->ts = from->ts;
185     to->channel = from->channel;
186     to->rssi = from->rssi;
187     to->rtt = from->rtt;
188     to->rtt_sd = from->rtt_sd;
189     to->beacon_period = from->beacon_period;
190     to->capability = from->capability;
191     memcpy(to->ssid, from->ssid, (DOT11_MAX_SSID_LEN+1));
192     memcpy(&to->bssid, &from->bssid, sizeof(mac_addr));
193 }
194 
195 /////////////////////////////////////////////////////////////////////////////
196 
197 class GetCapabilitiesCommand : public WifiCommand
198 {
199     void *mCapabilities;
200     uint16_t mRequesttype;
201     int mRequestsize;
202     public:
GetCapabilitiesCommand(wifi_interface_handle iface,void * capabitlites,uint16_t request_type,int request_size)203     GetCapabilitiesCommand(wifi_interface_handle iface, void *capabitlites, uint16_t request_type,
204             int request_size)
205         : WifiCommand("GetGscanCapabilitiesCommand", iface, 0), mCapabilities(capabitlites), mRequesttype(request_type),
206         mRequestsize(request_size)
207     {
208         memset(mCapabilities, 0, mRequestsize);
209     }
210 
create()211     virtual int create() {
212         ALOGV("Creating message to get scan capablities; iface = %d", mIfaceInfo->id);
213 
214         int ret = mMsg.create(GOOGLE_OUI, mRequesttype);
215         if (ret < 0) {
216             return ret;
217         }
218 
219         return ret;
220     }
221 
222     protected:
handleResponse(WifiEvent & reply)223     virtual int handleResponse(WifiEvent& reply) {
224 
225         ALOGV("In GetCapabilities::handleResponse");
226 
227         if (reply.get_cmd() != NL80211_CMD_VENDOR) {
228             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
229             return NL_SKIP;
230         }
231 
232         int id = reply.get_vendor_id();
233         int subcmd = reply.get_vendor_subcmd();
234 
235         void *data = reply.get_vendor_data();
236         int len = reply.get_vendor_data_len();
237 
238         ALOGV("Id = %0x, subcmd = 0x%x, len = %d, expected len = %d", id, subcmd, len,
239             mRequestsize);
240 
241         memcpy(mCapabilities, data, min(len, mRequestsize));
242 
243         return NL_OK;
244     }
245 };
246 
wifi_get_gscan_capabilities(wifi_interface_handle handle,wifi_gscan_capabilities * capabilities)247 wifi_error wifi_get_gscan_capabilities(wifi_interface_handle handle,
248         wifi_gscan_capabilities *capabilities)
249 {
250     GetCapabilitiesCommand command(handle, capabilities, GSCAN_SUBCMD_GET_CAPABILITIES,
251             (int)sizeof(wifi_gscan_capabilities));
252     return (wifi_error) command.requestResponse();
253 }
254 
255 /* Function to get chipset supported roaming capabilities */
wifi_get_roaming_capabilities(wifi_interface_handle handle,wifi_roaming_capabilities * capabilities)256 wifi_error wifi_get_roaming_capabilities(wifi_interface_handle handle,
257         wifi_roaming_capabilities *capabilities)
258 {
259     GetCapabilitiesCommand command(handle, capabilities, WIFI_SUBCMD_ROAM_CAPABILITY,
260             (int)sizeof(wifi_roaming_capabilities));
261     return (wifi_error) command.requestResponse();
262 }
263 
264 class GetChannelListCommand : public WifiCommand
265 {
266     wifi_channel *channels;
267     int max_channels;
268     int *num_channels;
269     int band;
270 public:
GetChannelListCommand(wifi_interface_handle iface,wifi_channel * channel_buf,int * ch_num,int num_max_ch,int band)271     GetChannelListCommand(wifi_interface_handle iface, wifi_channel *channel_buf, int *ch_num,
272         int num_max_ch, int band)
273         : WifiCommand("GetChannelListCommand", iface, 0), channels(channel_buf),
274             max_channels(num_max_ch), num_channels(ch_num), band(band)
275     {
276         memset(channels, 0, sizeof(wifi_channel) * max_channels);
277     }
create()278     virtual int create() {
279         ALOGV("Creating message to get channel list; iface = %d", mIfaceInfo->id);
280 
281         int ret = mMsg.create(GOOGLE_OUI, GSCAN_SUBCMD_GET_CHANNEL_LIST);
282         if (ret < 0) {
283             return ret;
284         }
285 
286         nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
287         ret = mMsg.put_u32(GSCAN_ATTRIBUTE_BAND, band);
288         if (ret < 0) {
289             return ret;
290         }
291 
292         mMsg.attr_end(data);
293 
294         return ret;
295     }
296 
297 protected:
handleResponse(WifiEvent & reply)298     virtual int handleResponse(WifiEvent& reply) {
299 
300         ALOGV("In GetChannelList::handleResponse");
301 
302         if (reply.get_cmd() != NL80211_CMD_VENDOR) {
303             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
304             return NL_SKIP;
305         }
306 
307         int id = reply.get_vendor_id();
308         int subcmd = reply.get_vendor_subcmd();
309         int num_channels_to_copy = 0;
310 
311         nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
312         int len = reply.get_vendor_data_len();
313 
314         ALOGV("Id = %0x, subcmd = %d, len = %d", id, subcmd, len);
315         if (vendor_data == NULL || len == 0) {
316             ALOGE("no vendor data in GetChannelList response; ignoring it");
317             return NL_SKIP;
318         }
319 
320         for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
321             if (it.get_type() == GSCAN_ATTRIBUTE_NUM_CHANNELS) {
322                 num_channels_to_copy = it.get_u32();
323                 ALOGI("Got channel list with %d channels", num_channels_to_copy);
324                 if(num_channels_to_copy > max_channels) {
325                     num_channels_to_copy = max_channels;
326                 }
327                 *num_channels = num_channels_to_copy;
328             } else if (it.get_type() == GSCAN_ATTRIBUTE_CHANNEL_LIST && num_channels_to_copy) {
329                 memcpy(channels, it.get_data(), sizeof(wifi_channel) * num_channels_to_copy);
330                 for (int i = 0; i < num_channels_to_copy; i++) {
331                         ALOGD("count %d: channel %d MHz\n", i, channels[i]);
332                 }
333             } else {
334                 ALOGW("Ignoring invalid attribute type = %d, size = %d",
335                         it.get_type(), it.get_len());
336             }
337         }
338 
339         return NL_OK;
340     }
341 };
342 
wifi_get_valid_channels(wifi_interface_handle handle,int band,int max_channels,wifi_channel * channels,int * num_channels)343 wifi_error wifi_get_valid_channels(wifi_interface_handle handle,
344         int band, int max_channels, wifi_channel *channels, int *num_channels)
345 {
346     GetChannelListCommand command(handle, channels, num_channels,
347                                         max_channels, band);
348     return (wifi_error) command.requestResponse();
349 }
350 /////////////////////////////////////////////////////////////////////////////
351 
352 /* helper functions */
createFeatureRequest(WifiRequest & request,int subcmd,int enable)353 int createFeatureRequest(WifiRequest& request, int subcmd, int enable) {
354 
355     int result = request.create(GOOGLE_OUI, subcmd);
356     if (result < 0) {
357         return result;
358     }
359 
360     nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
361     result = request.put_u32(GSCAN_ATTRIBUTE_ENABLE_FEATURE, enable);
362     if (result < 0) {
363         return result;
364     }
365 
366     request.attr_end(data);
367     return WIFI_SUCCESS;
368 }
369 
370 /////////////////////////////////////////////////////////////////////////////
371 class FullScanResultsCommand : public WifiCommand
372 {
373     int *mParams;
374     wifi_scan_result_handler mHandler;
375 public:
FullScanResultsCommand(wifi_interface_handle iface,int id,int * params,wifi_scan_result_handler handler)376     FullScanResultsCommand(wifi_interface_handle iface, int id, int *params,
377                 wifi_scan_result_handler handler)
378         : WifiCommand("FullScanResultsCommand", iface, id), mParams(params), mHandler(handler)
379     {*mParams = 0;}
380 
createRequest(WifiRequest & request,int subcmd,int enable)381     int createRequest(WifiRequest& request, int subcmd, int enable) {
382         int result = request.create(GOOGLE_OUI, subcmd);
383         if (result < 0) {
384             return result;
385         }
386 
387         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
388         result = request.put_u32(GSCAN_ENABLE_FULL_SCAN_RESULTS, enable);
389         if (result < 0) {
390             return result;
391         }
392 
393         request.attr_end(data);
394         return WIFI_SUCCESS;
395 
396     }
397 
start()398     int start() {
399         ALOGV("Enabling Full scan results");
400         WifiRequest request(familyId(), ifaceId());
401         int result = createRequest(request, GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS, 1);
402         if (result != WIFI_SUCCESS) {
403             ALOGE("failed to create request; result = %d", result);
404             return result;
405         }
406 
407         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);
408 
409         result = requestResponse(request);
410         if (result != WIFI_SUCCESS) {
411             unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);
412             ALOGE("failed to enable full scan results; result = %d", result);
413             return result;
414         }
415 
416         return result;
417     }
418 
cancel()419     virtual int cancel() {
420         ALOGV("Disabling Full scan results");
421 
422         WifiRequest request(familyId(), ifaceId());
423         int result = createRequest(request, GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS, 0);
424         if (result != WIFI_SUCCESS) {
425             ALOGE("failed to create request; result = %d", result);
426         } else {
427             result = requestResponse(request);
428             if (result != WIFI_SUCCESS) {
429                 ALOGE("failed to disable full scan results;result = %d", result);
430             }
431         }
432 
433         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);
434         return WIFI_SUCCESS;
435     }
436 
handleResponse(WifiEvent & reply)437     virtual int handleResponse(WifiEvent& reply) {
438          ALOGD("Request complete!");
439         /* Nothing to do on response! */
440         return NL_SKIP;
441     }
442 
handleEvent(WifiEvent & event)443     virtual int handleEvent(WifiEvent& event) {
444         ALOGV("Full scan results:  Got an event");
445         return wifi_handle_full_scan_event(id(), event, mHandler);
446     }
447 
448 };
449 /////////////////////////////////////////////////////////////////////////////
450 
451 class ScanCommand : public WifiCommand
452 {
453     wifi_scan_cmd_params *mParams;
454     wifi_scan_result_handler mHandler;
455 public:
ScanCommand(wifi_interface_handle iface,int id,wifi_scan_cmd_params * params,wifi_scan_result_handler handler)456     ScanCommand(wifi_interface_handle iface, int id, wifi_scan_cmd_params *params,
457                 wifi_scan_result_handler handler)
458         : WifiCommand("ScanCommand", iface, id), mParams(params), mHandler(handler)
459     { }
460 
createSetupRequest(WifiRequest & request)461     int createSetupRequest(WifiRequest& request) {
462         int i;
463         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_CONFIG);
464         if (result < 0) {
465             return result;
466         }
467 
468         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
469         result = request.put_u32(GSCAN_ATTRIBUTE_BASE_PERIOD, mParams->base_period);
470         if (result < 0) {
471             return result;
472         }
473 
474         result = request.put_u32(GSCAN_ATTRIBUTE_NUM_BUCKETS, mParams->num_buckets);
475         if (result < 0) {
476             return result;
477         }
478 
479         for (i = 0; i < (int)mParams->num_buckets; i++) {
480             nlattr * bucket = request.attr_start(i);    // next bucket
481             result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_ID, mParams->buckets[i].bucket);
482             if (result < 0) {
483                 return result;
484             }
485             result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_PERIOD, mParams->buckets[i].period);
486             if (result < 0) {
487                 return result;
488             }
489             result = request.put_u32(GSCAN_ATTRIBUTE_BUCKETS_BAND,
490                     mParams->buckets[i].band);
491             if (result < 0) {
492                 return result;
493             }
494             result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_STEP_COUNT,
495                     mParams->buckets[i].step_count);
496             if (result < 0) {
497                 return result;
498             }
499             result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_MAX_PERIOD,
500                     mParams->buckets[i].max_period);
501             if (result < 0) {
502                 return result;
503             }
504             result = request.put_u32(GSCAN_ATTRIBUTE_REPORT_EVENTS,
505                     mParams->buckets[i].report_events);
506             if (result < 0) {
507                 return result;
508             }
509 
510             result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS,
511                     mParams->buckets[i].num_channels);
512             if (result < 0) {
513                 return result;
514             }
515 
516             if (mParams->buckets[i].num_channels) {
517                 nlattr *channels = request.attr_start(GSCAN_ATTRIBUTE_BUCKET_CHANNELS);
518                 ALOGV(" channels: ");
519                 for (int j = 0; j < mParams->buckets[i].num_channels; j++) {
520                     result = request.put_u32(j, mParams->buckets[i].channels[j].channel);
521                     ALOGV(" %u", mParams->buckets[i].channels[j].channel);
522 
523                     if (result < 0) {
524                         return result;
525                     }
526                 }
527                 request.attr_end(channels);
528             }
529 
530             request.attr_end(bucket);
531         }
532 
533         request.attr_end(data);
534         return WIFI_SUCCESS;
535     }
536 
createScanConfigRequest(WifiRequest & request)537     int createScanConfigRequest(WifiRequest& request) {
538         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_SCAN_CONFIG);
539         if (result < 0) {
540             return result;
541         }
542 
543         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
544         result = request.put_u32(GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN, mParams->max_ap_per_scan);
545         if (result < 0) {
546             return result;
547         }
548 
549         result = request.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD,
550                 mParams->report_threshold_percent);
551         if (result < 0) {
552             return result;
553         }
554 
555         int num_scans = mParams->report_threshold_num_scans;
556 
557         result = request.put_u32(GSCAN_ATTRIBUTE_NUM_SCANS_TO_CACHE, num_scans);
558         if (result < 0) {
559             return result;
560         }
561 
562         request.attr_end(data);
563         return WIFI_SUCCESS;
564     }
565 
createStartRequest(WifiRequest & request)566     int createStartRequest(WifiRequest& request) {
567         return createFeatureRequest(request, GSCAN_SUBCMD_ENABLE_GSCAN, 1);
568     }
569 
createStopRequest(WifiRequest & request)570     int createStopRequest(WifiRequest& request) {
571         return createFeatureRequest(request, GSCAN_SUBCMD_ENABLE_GSCAN, 0);
572     }
573 
unregisterVendorHandlerAll()574     void unregisterVendorHandlerAll() {
575         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);
576         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);
577         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);
578     }
579 
start()580     int start() {
581         ALOGV("GSCAN start");
582         WifiRequest request(familyId(), ifaceId());
583         int result = createSetupRequest(request);
584         if (result != WIFI_SUCCESS) {
585             ALOGE("failed to create setup request; result = %d", result);
586             return result;
587         }
588 
589         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);
590         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);
591         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);
592 
593         result = requestResponse(request);
594         if (result != WIFI_SUCCESS) {
595             unregisterVendorHandlerAll();
596             ALOGE("failed to configure setup; result = %d", result);
597             return result;
598         }
599 
600         request.destroy();
601 
602         result = createScanConfigRequest(request);
603         if (result != WIFI_SUCCESS) {
604             unregisterVendorHandlerAll();
605             ALOGE("failed to create scan config request; result = %d", result);
606             return result;
607         }
608 
609         result = requestResponse(request);
610         if (result != WIFI_SUCCESS) {
611             unregisterVendorHandlerAll();
612             ALOGE("failed to configure scan; result = %d", result);
613             return result;
614         }
615 
616         ALOGV(" ....starting scan");
617 
618         result = createStartRequest(request);
619         if (result != WIFI_SUCCESS) {
620             unregisterVendorHandlerAll();
621             ALOGE("failed to create start request; result = %d", result);
622             return result;
623         }
624 
625         result = requestResponse(request);
626         if (result != WIFI_SUCCESS) {
627             unregisterVendorHandlerAll();
628             ALOGE("failed to start scan; result = %d", result);
629             return result;
630         }
631         return result;
632     }
633 
cancel()634     virtual int cancel() {
635         ALOGV("Stopping scan");
636 
637         WifiRequest request(familyId(), ifaceId());
638         int result = createStopRequest(request);
639         if (result != WIFI_SUCCESS) {
640             ALOGE("failed to create stop request; result = %d", result);
641         } else {
642             result = requestResponse(request);
643             if (result != WIFI_SUCCESS) {
644                 ALOGE("failed to stop scan; result = %d", result);
645             }
646         }
647 
648         unregisterVendorHandlerAll();
649         return WIFI_SUCCESS;
650     }
651 
handleResponse(WifiEvent & reply)652     virtual int handleResponse(WifiEvent& reply) {
653         /* Nothing to do on response! */
654         return NL_SKIP;
655     }
656 
handleEvent(WifiEvent & event)657     virtual int handleEvent(WifiEvent& event) {
658         ALOGV("Got a scan results event");
659         //event.log();
660 
661         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
662         int len = event.get_vendor_data_len();
663         int event_id = event.get_vendor_subcmd();
664 
665         if ((event_id == GSCAN_EVENT_COMPLETE_SCAN) ||
666             (event_id == GSCAN_EVENT_SCAN_RESULTS_AVAILABLE)) {
667             if (vendor_data == NULL || len != 4) {
668                 ALOGI("Bad event data!");
669                 return NL_SKIP;
670             }
671             wifi_scan_event evt_type;
672             evt_type = (wifi_scan_event) event.get_u32(NL80211_ATTR_VENDOR_DATA);
673             ALOGV("Received event type %d", evt_type);
674             if(*mHandler.on_scan_event)
675                 (*mHandler.on_scan_event)(id(), evt_type);
676         } else if (event_id == GSCAN_EVENT_FULL_SCAN_RESULTS) {
677             wifi_handle_full_scan_event(id(), event, mHandler);
678         }
679         return NL_SKIP;
680     }
681 };
682 
wifi_start_gscan(wifi_request_id id,wifi_interface_handle iface,wifi_scan_cmd_params params,wifi_scan_result_handler handler)683 wifi_error wifi_start_gscan(
684         wifi_request_id id,
685         wifi_interface_handle iface,
686         wifi_scan_cmd_params params,
687         wifi_scan_result_handler handler)
688 {
689     wifi_handle handle = getWifiHandle(iface);
690 
691     ALOGV("Starting GScan, halHandle = %p", handle);
692 
693     ScanCommand *cmd = new ScanCommand(iface, id, &params, handler);
694     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
695     wifi_error result = wifi_register_cmd(handle, id, cmd);
696     if (result != WIFI_SUCCESS) {
697         cmd->releaseRef();
698         return result;
699     }
700     result = (wifi_error)cmd->start();
701     if (result != WIFI_SUCCESS) {
702         wifi_unregister_cmd(handle, id);
703         cmd->releaseRef();
704         return result;
705     }
706     return result;
707 }
708 
wifi_stop_gscan(wifi_request_id id,wifi_interface_handle iface)709 wifi_error wifi_stop_gscan(wifi_request_id id, wifi_interface_handle iface)
710 {
711     wifi_handle handle = getWifiHandle(iface);
712     ALOGV("Stopping GScan, wifi_request_id = %d, halHandle = %p", id, handle);
713 
714     if (id == -1) {
715         wifi_scan_result_handler handler;
716         wifi_scan_cmd_params dummy_params;
717         memset(&handler, 0, sizeof(handler));
718 
719         ScanCommand *cmd = new ScanCommand(iface, id, &dummy_params, handler);
720         NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
721         cmd->cancel();
722         cmd->releaseRef();
723         return WIFI_SUCCESS;
724     }
725 
726     return wifi_cancel_cmd(id, iface);
727 }
728 
wifi_enable_full_scan_results(wifi_request_id id,wifi_interface_handle iface,wifi_scan_result_handler handler)729 wifi_error wifi_enable_full_scan_results(
730         wifi_request_id id,
731         wifi_interface_handle iface,
732         wifi_scan_result_handler handler)
733 {
734     wifi_handle handle = getWifiHandle(iface);
735     int params_dummy;
736 
737     ALOGV("Enabling full scan results, halHandle = %p", handle);
738 
739     FullScanResultsCommand *cmd = new FullScanResultsCommand(iface, id, &params_dummy, handler);
740     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
741     wifi_error result = wifi_register_cmd(handle, id, cmd);
742     if (result != WIFI_SUCCESS) {
743         cmd->releaseRef();
744         return result;
745     }
746     result = (wifi_error)cmd->start();
747     if (result != WIFI_SUCCESS) {
748         wifi_unregister_cmd(handle, id);
749         cmd->releaseRef();
750         return result;
751     }
752     return result;
753 }
754 
wifi_handle_full_scan_event(wifi_request_id id,WifiEvent & event,wifi_scan_result_handler handler)755 int wifi_handle_full_scan_event(
756         wifi_request_id id,
757         WifiEvent& event,
758         wifi_scan_result_handler handler)
759 {
760     nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
761     unsigned int len = event.get_vendor_data_len();
762 
763     if (vendor_data == NULL || len < sizeof(wifi_gscan_full_result_t)) {
764         ALOGI("Full scan results: No scan results found");
765         return NL_SKIP;
766     }
767 
768     wifi_gscan_full_result_t *drv_res = (wifi_gscan_full_result_t *)event.get_vendor_data();
769     /* To protect against corrupted data, put a ceiling */
770     int ie_len = min(MAX_PROBE_RESP_IE_LEN, drv_res->ie_length);
771     wifi_scan_result *full_scan_result;
772     wifi_gscan_result_t *fixed = &drv_res->fixed;
773 
774     if ((ie_len + offsetof(wifi_gscan_full_result_t, ie_data)) > len) {
775         ALOGE("BAD event data, len %d ie_len %d fixed length %lu!\n", len,
776             ie_len, offsetof(wifi_gscan_full_result_t, ie_data));
777         return NL_SKIP;
778     }
779     full_scan_result = (wifi_scan_result *) malloc((ie_len + offsetof(wifi_scan_result, ie_data)));
780     if (!full_scan_result) {
781         ALOGE("Full scan results: Can't malloc!\n");
782         return NL_SKIP;
783     }
784     convert_to_hal_result(full_scan_result, fixed);
785     full_scan_result->ie_length = ie_len;
786     memcpy(full_scan_result->ie_data, drv_res->ie_data, ie_len);
787     if(handler.on_full_scan_result)
788         handler.on_full_scan_result(id, full_scan_result, drv_res->scan_ch_bucket);
789 
790     ALOGV("Full scan result: %-32s %02x:%02x:%02x:%02x:%02x:%02x %d %d %ld %lx %lx %x %d\n",
791         fixed->ssid, fixed->bssid[0], fixed->bssid[1], fixed->bssid[2], fixed->bssid[3],
792         fixed->bssid[4], fixed->bssid[5], fixed->rssi, fixed->channel, fixed->ts,
793         fixed->rtt, fixed->rtt_sd, drv_res->scan_ch_bucket, drv_res->ie_length);
794     free(full_scan_result);
795     return NL_SKIP;
796 }
797 
798 
wifi_disable_full_scan_results(wifi_request_id id,wifi_interface_handle iface)799 wifi_error wifi_disable_full_scan_results(wifi_request_id id, wifi_interface_handle iface)
800 {
801     ALOGV("Disabling full scan results");
802 
803     if(id == -1) {
804         wifi_scan_result_handler handler;
805         int params_dummy;
806 
807         memset(&handler, 0, sizeof(handler));
808         FullScanResultsCommand *cmd = new FullScanResultsCommand(iface, 0, &params_dummy, handler);
809         NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
810         cmd->cancel();
811         cmd->releaseRef();
812         return WIFI_SUCCESS;
813     }
814 
815     return wifi_cancel_cmd(id, iface);
816 }
817 
818 
819 /////////////////////////////////////////////////////////////////////////////
820 
821 class GetScanResultsCommand : public WifiCommand {
822     wifi_cached_scan_results *mScans;
823     int mMax;
824     int *mNum;
825     int mRetrieved;
826     byte mFlush;
827     int mCompleted;
828 public:
GetScanResultsCommand(wifi_interface_handle iface,byte flush,wifi_cached_scan_results * results,int max,int * num)829     GetScanResultsCommand(wifi_interface_handle iface, byte flush,
830             wifi_cached_scan_results *results, int max, int *num)
831         : WifiCommand("GetScanResultsCommand", iface, -1), mScans(results), mMax(max), mNum(num),
832                 mRetrieved(0), mFlush(flush), mCompleted(0)
833     { }
834 
createRequest(WifiRequest & request,int num,byte flush)835     int createRequest(WifiRequest& request, int num, byte flush) {
836         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_GET_SCAN_RESULTS);
837         if (result < 0) {
838             return result;
839         }
840 
841         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
842         result = request.put_u32(GSCAN_ATTRIBUTE_NUM_OF_RESULTS, num);
843         if (result < 0) {
844             return result;
845         }
846 
847         result = request.put_u8(GSCAN_ATTRIBUTE_FLUSH_RESULTS, flush);
848         if (result < 0) {
849             return result;
850         }
851 
852         request.attr_end(data);
853         return WIFI_SUCCESS;
854     }
855 
execute()856     int execute() {
857         WifiRequest request(familyId(), ifaceId());
858         ALOGV("retrieving %d scan results", mMax);
859 
860         for (int i = 0; i < 10 && mRetrieved < mMax; i++) {
861             int num_to_retrieve = mMax - mRetrieved;
862             // ALOGI("retrieving %d scan results in one shot", num_to_retrieve);
863             int result = createRequest(request, num_to_retrieve, mFlush);
864             if (result < 0) {
865                 ALOGE("failed to create request");
866                 return result;
867             }
868 
869             int prev_retrieved = mRetrieved;
870 
871             result = requestResponse(request);
872 
873             if (result != WIFI_SUCCESS) {
874                 ALOGE("failed to retrieve scan results; result = %d", result);
875                 return result;
876             }
877 
878             if (mRetrieved == prev_retrieved || mCompleted) {
879                 /* no more items left to retrieve */
880                 break;
881             }
882 
883             request.destroy();
884         }
885 
886         ALOGV("GetScanResults read %d results", mRetrieved);
887         *mNum = mRetrieved;
888         return WIFI_SUCCESS;
889     }
890 
handleResponse(WifiEvent & reply)891     virtual int handleResponse(WifiEvent& reply) {
892         ALOGV("In GetScanResultsCommand::handleResponse");
893 
894         if (reply.get_cmd() != NL80211_CMD_VENDOR) {
895             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
896             return NL_SKIP;
897         }
898 
899         int id = reply.get_vendor_id();
900         int subcmd = reply.get_vendor_subcmd();
901 
902         ALOGV("Id = %0x, subcmd = %d", id, subcmd);
903 
904         /*
905         if (subcmd != GSCAN_SUBCMD_SCAN_RESULTS) {
906             ALOGE("Invalid response to GetScanResultsCommand; ignoring it");
907             return NL_SKIP;
908         }
909         */
910 
911         nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
912         int len = reply.get_vendor_data_len();
913 
914         if (vendor_data == NULL || len == 0) {
915             ALOGE("no vendor data in GetScanResults response; ignoring it");
916             return NL_SKIP;
917         }
918 
919         for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
920             if (it.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE) {
921                 mCompleted = it.get_u8();
922                 ALOGV("retrieved mCompleted flag : %d", mCompleted);
923             } else if (it.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS || it.get_type() == 0) {
924                 int scan_id = 0, flags = 0, num = 0, scan_ch_bucket_mask = 0;
925                 for (nl_iterator it2(it.get()); it2.has_next(); it2.next()) {
926                     if (it2.get_type() == GSCAN_ATTRIBUTE_SCAN_ID) {
927                         scan_id = it2.get_u32();
928                         ALOGV("retrieved scan_id : 0x%0x", scan_id);
929                     } else if (it2.get_type() == GSCAN_ATTRIBUTE_SCAN_FLAGS) {
930                         flags = it2.get_u8();
931                         ALOGV("retrieved scan_flags : 0x%0x", flags);
932                     } else if (it2.get_type() == GSCAN_ATTRIBUTE_NUM_OF_RESULTS) {
933                         num = it2.get_u32();
934                         ALOGV("retrieved num_results: %d", num);
935                     } else if (it2.get_type() == GSCAN_ATTRIBUTE_CH_BUCKET_BITMASK) {
936                         scan_ch_bucket_mask = it2.get_u32();
937                         ALOGD("retrieved scan_ch_bucket_mask: %x", scan_ch_bucket_mask);
938                     } else if (it2.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS && num) {
939                         if (mRetrieved >= mMax) {
940                             ALOGW("Stored %d scans, ignoring excess results", mRetrieved);
941                             break;
942                         }
943                         num = min(num, (int)(it2.get_len()/sizeof(wifi_gscan_result)));
944                         num = min(num, (int)MAX_AP_CACHE_PER_SCAN);
945                         ALOGV("Copying %d scan results", num);
946                         wifi_gscan_result_t *results = (wifi_gscan_result_t *)it2.get_data();
947                         wifi_scan_result *mScanResults = mScans[mRetrieved].results;
948 
949                         for (int i = 0; i < num; i++) {
950                             wifi_gscan_result_t *result = &results[i];
951                             convert_to_hal_result(&mScanResults[i], result);
952                             mScanResults[i].ie_length = 0;
953                             ALOGV("%02d  %-32s  %02x:%02x:%02x:%02x:%02x:%02x  %04d", i,
954                                 result->ssid, result->bssid[0], result->bssid[1], result->bssid[2],
955                                 result->bssid[3], result->bssid[4], result->bssid[5],
956                                 result->rssi);
957                         }
958                         mScans[mRetrieved].scan_id = scan_id;
959                         mScans[mRetrieved].flags = flags;
960                         mScans[mRetrieved].num_results = num;
961                         mScans[mRetrieved].buckets_scanned = scan_ch_bucket_mask;
962                         ALOGV("Setting result of scan_id : 0x%0x", mScans[mRetrieved].scan_id);
963                         mRetrieved++;
964                     } else {
965                         ALOGW("Ignoring invalid attribute type = %d, size = %d",
966                                 it.get_type(), it.get_len());
967                     }
968                 }
969             } else {
970                 ALOGW("Ignoring invalid attribute type = %d, size = %d",
971                         it.get_type(), it.get_len());
972             }
973         }
974         ALOGV("GetScanResults read %d results", mRetrieved);
975         return NL_OK;
976     }
977 };
978 
wifi_get_cached_gscan_results(wifi_interface_handle iface,byte flush,int max,wifi_cached_scan_results * results,int * num)979 wifi_error wifi_get_cached_gscan_results(wifi_interface_handle iface, byte flush,
980         int max, wifi_cached_scan_results *results, int *num) {
981     ALOGV("Getting cached scan results, iface handle = %p, num = %d", iface, *num);
982 
983     GetScanResultsCommand *cmd = new GetScanResultsCommand(iface, flush, results, max, num);
984     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
985     wifi_error err = (wifi_error)cmd->execute();
986     cmd->releaseRef();
987     return err;
988 }
989 
990 /////////////////////////////////////////////////////////////////////////////
991 
992 class BssidHotlistCommand : public WifiCommand
993 {
994 private:
995     wifi_bssid_hotlist_params mParams;
996     wifi_hotlist_ap_found_handler mHandler;
997     static const int MAX_RESULTS = 64;
998     wifi_scan_result mResults[MAX_RESULTS];
999 public:
BssidHotlistCommand(wifi_interface_handle handle,int id,wifi_bssid_hotlist_params params,wifi_hotlist_ap_found_handler handler)1000     BssidHotlistCommand(wifi_interface_handle handle, int id,
1001             wifi_bssid_hotlist_params params, wifi_hotlist_ap_found_handler handler)
1002         : WifiCommand("BssidHotlistCommand", handle, id), mParams(params), mHandler(handler)
1003     { }
1004 
createSetupRequest(WifiRequest & request)1005     int createSetupRequest(WifiRequest& request) {
1006         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_HOTLIST);
1007         if (result < 0) {
1008             return result;
1009         }
1010 
1011         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1012         result = request.put_u8(GSCAN_ATTRIBUTE_HOTLIST_FLUSH, 1);
1013         if (result < 0) {
1014             return result;
1015         }
1016 
1017         result = request.put_u32(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE, mParams.lost_ap_sample_size);
1018         if (result < 0) {
1019             return result;
1020         }
1021 
1022         result = request.put_u32(GSCAN_ATTRIBUTE_HOTLIST_BSSID_COUNT, mParams.num_bssid);
1023         if (result < 0) {
1024             return result;
1025         }
1026 
1027         struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS);
1028         for (int i = 0; i < mParams.num_bssid; i++) {
1029             nlattr *attr2 = request.attr_start(GSCAN_ATTRIBUTE_HOTLIST_ELEM);
1030             if (attr2 == NULL) {
1031                 return WIFI_ERROR_OUT_OF_MEMORY;
1032             }
1033             result = request.put_addr(GSCAN_ATTRIBUTE_BSSID, mParams.ap[i].bssid);
1034             if (result < 0) {
1035                 return result;
1036             }
1037             result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH, mParams.ap[i].high);
1038             if (result < 0) {
1039                 return result;
1040             }
1041             result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW, mParams.ap[i].low);
1042             if (result < 0) {
1043                 return result;
1044             }
1045             request.attr_end(attr2);
1046         }
1047 
1048         request.attr_end(attr);
1049         request.attr_end(data);
1050         return result;
1051     }
1052 
createTeardownRequest(WifiRequest & request)1053     int createTeardownRequest(WifiRequest& request) {
1054         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_HOTLIST);
1055         if (result < 0) {
1056             return result;
1057         }
1058 
1059         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1060         result = request.put_u8(GSCAN_ATTRIBUTE_HOTLIST_FLUSH, 1);
1061         if (result < 0) {
1062             return result;
1063         }
1064 
1065         struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS);
1066         request.attr_end(attr);
1067         request.attr_end(data);
1068         return result;
1069     }
1070 
unregisterVendorHandlerAll()1071     void unregisterVendorHandlerAll() {
1072         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);
1073         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);
1074     }
1075 
start()1076     int start() {
1077         ALOGI("Executing hotlist setup request, num = %d", mParams.num_bssid);
1078         WifiRequest request(familyId(), ifaceId());
1079         int result = createSetupRequest(request);
1080         if (result < 0) {
1081             return result;
1082         }
1083 
1084         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);
1085         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);
1086 
1087         result = requestResponse(request);
1088         if (result < 0) {
1089             unregisterVendorHandlerAll();
1090             ALOGI("Failed to execute hotlist setup request, result = %d", result);
1091             return result;
1092         }
1093 
1094         ALOGI("Successfully set %d APs in the hotlist ", mParams.num_bssid);
1095         result = createFeatureRequest(request, GSCAN_SUBCMD_ENABLE_GSCAN, 1);
1096         if (result < 0) {
1097             unregisterVendorHandlerAll();
1098             return result;
1099         }
1100 
1101         result = requestResponse(request);
1102         if (result < 0) {
1103             unregisterVendorHandlerAll();
1104             return result;
1105         }
1106 
1107         ALOGI("successfully restarted the scan");
1108         return result;
1109     }
1110 
cancel()1111     virtual int cancel() {
1112         /* unregister event handler */
1113         unregisterVendorHandlerAll();
1114         /* create set hotlist message with empty hotlist */
1115         WifiRequest request(familyId(), ifaceId());
1116         int result = createTeardownRequest(request);
1117         if (result < 0) {
1118             return result;
1119         }
1120 
1121         result = requestResponse(request);
1122         if (result < 0) {
1123             return result;
1124         }
1125 
1126         ALOGI("Successfully reset APs in current hotlist");
1127         return result;
1128     }
1129 
handleResponse(WifiEvent & reply)1130     virtual int handleResponse(WifiEvent& reply) {
1131         /* Nothing to do on response! */
1132         return NL_SKIP;
1133     }
1134 
handleEvent(WifiEvent & event)1135     virtual int handleEvent(WifiEvent& event) {
1136         ALOGI("Hotlist AP event");
1137         int event_id = event.get_vendor_subcmd();
1138         // event.log();
1139 
1140         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1141         int len = event.get_vendor_data_len();
1142 
1143         if (vendor_data == NULL || len == 0) {
1144             ALOGI("No scan results found");
1145             return NL_SKIP;
1146         }
1147 
1148         memset(mResults, 0, sizeof(wifi_scan_result) * MAX_RESULTS);
1149 
1150         int num = len / sizeof(wifi_gscan_result_t);
1151         wifi_gscan_result_t *inp = (wifi_gscan_result_t *)event.get_vendor_data();
1152         num = min(MAX_RESULTS, num);
1153         for (int i = 0; i < num; i++, inp++) {
1154             convert_to_hal_result(&(mResults[i]), inp);
1155         }
1156 
1157         if (event_id == GSCAN_EVENT_HOTLIST_RESULTS_FOUND) {
1158             ALOGI("FOUND %d hotlist APs", num);
1159             if (*mHandler.on_hotlist_ap_found)
1160                 (*mHandler.on_hotlist_ap_found)(id(), num, mResults);
1161         } else if (event_id == GSCAN_EVENT_HOTLIST_RESULTS_LOST) {
1162             ALOGI("LOST %d hotlist APs", num);
1163             if (*mHandler.on_hotlist_ap_lost)
1164                 (*mHandler.on_hotlist_ap_lost)(id(), num, mResults);
1165         }
1166         return NL_SKIP;
1167     }
1168 };
1169 
1170 class ePNOCommand : public WifiCommand
1171 {
1172 private:
1173     wifi_epno_params epno_params;
1174     wifi_epno_handler mHandler;
1175     wifi_scan_result mResults[MAX_EPNO_NETWORKS];
1176 public:
ePNOCommand(wifi_interface_handle handle,int id,const wifi_epno_params * params,wifi_epno_handler handler)1177     ePNOCommand(wifi_interface_handle handle, int id,
1178             const wifi_epno_params *params, wifi_epno_handler handler)
1179         : WifiCommand("ePNOCommand", handle, id), mHandler(handler)
1180     {
1181         if (params != NULL) {
1182             memcpy(&epno_params, params, sizeof(wifi_epno_params));
1183         } else {
1184             memset(&epno_params, 0, sizeof(wifi_epno_params));
1185         }
1186     }
createSetupRequest(WifiRequest & request)1187     int createSetupRequest(WifiRequest& request) {
1188         char tmp_buf[DOT11_MAX_SSID_LEN + 1];
1189         if (epno_params.num_networks > MAX_EPNO_NETWORKS) {
1190             ALOGE("wrong epno num_networks:%d", epno_params.num_networks);
1191             return WIFI_ERROR_INVALID_ARGS;
1192         }
1193         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_EPNO_SSID);
1194         if (result < 0) {
1195             return result;
1196         }
1197         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1198         result = request.put_u8(GSCAN_ATTRIBUTE_EPNO_FLUSH, 1);
1199         if (result < 0) {
1200             return result;
1201         }
1202 
1203         result = request.put_u8(GSCAN_ATTRIBUTE_EPNO_5G_RSSI_THR,
1204                             (u8)epno_params.min5GHz_rssi);
1205         if (result < 0) {
1206             return result;
1207         }
1208         result = request.put_u8(GSCAN_ATTRIBUTE_EPNO_2G_RSSI_THR,
1209                             (u8)epno_params.min24GHz_rssi);
1210         if (result < 0) {
1211             return result;
1212         }
1213         result = request.put_u16(GSCAN_ATTRIBUTE_EPNO_INIT_SCORE_MAX,
1214                             epno_params.initial_score_max);
1215         if (result < 0) {
1216             return result;
1217         }
1218         result = request.put_u16(GSCAN_ATTRIBUTE_EPNO_CUR_CONN_BONUS,
1219                             epno_params.current_connection_bonus);
1220         if (result < 0) {
1221             return result;
1222         }
1223         result = request.put_u16(GSCAN_ATTRIBUTE_EPNO_SAME_NETWORK_BONUS,
1224                             epno_params.same_network_bonus);
1225         if (result < 0) {
1226             return result;
1227         }
1228         result = request.put_u16(GSCAN_ATTRIBUTE_EPNO_SECURE_BONUS,
1229                             epno_params.secure_bonus);
1230         if (result < 0) {
1231             return result;
1232         }
1233         result = request.put_u16(GSCAN_ATTRIBUTE_EPNO_5G_BONUS,
1234                             epno_params.band5GHz_bonus);
1235         if (result < 0) {
1236             return result;
1237         }
1238         result = request.put_u8(GSCAN_ATTRIBUTE_EPNO_SSID_NUM,
1239                         epno_params.num_networks);
1240         if (result < 0) {
1241             return result;
1242         }
1243         struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_EPNO_SSID_LIST);
1244         wifi_epno_network *ssid_list = epno_params.networks;
1245         for (int i = 0; i < epno_params.num_networks; i++) {
1246             nlattr *attr2 = request.attr_start(i);
1247             if (attr2 == NULL) {
1248                 return WIFI_ERROR_OUT_OF_MEMORY;
1249             }
1250             strlcpy(tmp_buf, ssid_list[i].ssid, sizeof(tmp_buf));
1251             result = request.put(GSCAN_ATTRIBUTE_EPNO_SSID, tmp_buf,
1252                 strlen(tmp_buf));
1253             ALOGI("PNO network: SSID %s flags %x auth %x", tmp_buf,
1254                 ssid_list[i].flags,
1255                 ssid_list[i].auth_bit_field);
1256             if (result < 0) {
1257                 return result;
1258             }
1259             result = request.put_u32(GSCAN_ATTRIBUTE_EPNO_SSID_LEN,
1260                 strlen(tmp_buf));
1261             if (result < 0) {
1262                 return result;
1263             }
1264             result = request.put_u32(GSCAN_ATTRIBUTE_EPNO_FLAGS, ssid_list[i].flags);
1265             if (result < 0) {
1266                 return result;
1267             }
1268             result = request.put_u32(GSCAN_ATTRIBUTE_EPNO_AUTH, ssid_list[i].auth_bit_field);
1269             if (result < 0) {
1270                 return result;
1271             }
1272             request.attr_end(attr2);
1273         }
1274         request.attr_end(attr);
1275         request.attr_end(data);
1276         return result;
1277     }
1278 
createTeardownRequest(WifiRequest & request)1279     int createTeardownRequest(WifiRequest& request) {
1280         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_EPNO_SSID);
1281         if (result < 0) {
1282             return result;
1283         }
1284 
1285         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1286         result = request.put_u8(GSCAN_ATTRIBUTE_EPNO_FLUSH, 1);
1287         if (result < 0) {
1288             return result;
1289         }
1290         request.attr_end(data);
1291         return result;
1292     }
1293 
start()1294     int start() {
1295         ALOGI("Executing ePNO setup request, num = %d", epno_params.num_networks);
1296         WifiRequest request(familyId(), ifaceId());
1297         int result = createSetupRequest(request);
1298         if (result < 0) {
1299             return result;
1300         }
1301 
1302         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_EPNO_EVENT);
1303         result = requestResponse(request);
1304         if (result < 0) {
1305             unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_EPNO_EVENT);
1306             ALOGI("Failed to execute ePNO setup request, result = %d", result);
1307             return result;
1308         }
1309 
1310         ALOGI("Successfully set %d SSIDs for ePNO", epno_params.num_networks);
1311         ALOGI("successfully restarted the scan");
1312         return result;
1313     }
1314 
cancel()1315     virtual int cancel() {
1316         /* unregister event handler */
1317         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_EPNO_EVENT);
1318         /* create set hotlist message with empty hotlist */
1319         WifiRequest request(familyId(), ifaceId());
1320         int result = createTeardownRequest(request);
1321         if (result < 0) {
1322             return result;
1323         }
1324 
1325         result = requestResponse(request);
1326         if (result < 0) {
1327             return result;
1328         }
1329 
1330         ALOGI("Successfully reset APs in current hotlist");
1331         return result;
1332     }
1333 
handleResponse(WifiEvent & reply)1334     virtual int handleResponse(WifiEvent& reply) {
1335         /* Nothing to do on response! */
1336         return NL_SKIP;
1337     }
1338 
handleEvent(WifiEvent & event)1339     virtual int handleEvent(WifiEvent& event) {
1340         ALOGI("ePNO event");
1341         // event.log();
1342 
1343         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1344         int len = event.get_vendor_data_len();
1345 
1346         if (vendor_data == NULL || len == 0) {
1347             ALOGI("No scan results found");
1348             return NL_SKIP;
1349         }
1350 
1351         memset(mResults, 0, sizeof(wifi_scan_result) * MAX_EPNO_NETWORKS);
1352 
1353         unsigned int num = len / sizeof(wifi_pno_result_t);
1354         unsigned int i;
1355         num = min(MAX_EPNO_NETWORKS, num);
1356         wifi_pno_result_t *res = (wifi_pno_result_t *) event.get_vendor_data();
1357         for (i = 0; i < num; i++) {
1358             if (res[i].flags == PNO_SSID_FOUND) {
1359                 memcpy(mResults[i].ssid, res[i].ssid, res[i].ssid_len);
1360                 memcpy(mResults[i].bssid, res[i].bssid, sizeof(mac_addr));
1361 
1362                 mResults[i].ssid[res[i].ssid_len] = '\0';
1363                 mResults[i].channel = res[i].channel;
1364                 mResults[i].rssi = res[i].rssi;
1365             }
1366         }
1367         if (*mHandler.on_network_found)
1368             (*mHandler.on_network_found)(id(), num, mResults);
1369         return NL_SKIP;
1370     }
1371 };
1372 
wifi_set_bssid_hotlist(wifi_request_id id,wifi_interface_handle iface,wifi_bssid_hotlist_params params,wifi_hotlist_ap_found_handler handler)1373 wifi_error wifi_set_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface,
1374         wifi_bssid_hotlist_params params, wifi_hotlist_ap_found_handler handler)
1375 {
1376     wifi_handle handle = getWifiHandle(iface);
1377 
1378     BssidHotlistCommand *cmd = new BssidHotlistCommand(iface, id, params, handler);
1379     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
1380     wifi_error result = wifi_register_cmd(handle, id, cmd);
1381     if (result != WIFI_SUCCESS) {
1382         cmd->releaseRef();
1383         return result;
1384     }
1385     result = (wifi_error)cmd->start();
1386     if (result != WIFI_SUCCESS) {
1387         wifi_unregister_cmd(handle, id);
1388         cmd->releaseRef();
1389         return result;
1390     }
1391     return result;
1392 }
1393 
wifi_reset_bssid_hotlist(wifi_request_id id,wifi_interface_handle iface)1394 wifi_error wifi_reset_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface)
1395 {
1396     return wifi_cancel_cmd(id, iface);
1397 }
1398 
1399 
1400 /////////////////////////////////////////////////////////////////////////////
1401 
1402 class SignificantWifiChangeCommand : public WifiCommand
1403 {
1404     typedef struct {
1405         mac_addr bssid;                     // BSSID
1406         wifi_channel channel;               // channel frequency in MHz
1407         int num_rssi;                       // number of rssi samples
1408         wifi_rssi rssi[8];                   // RSSI history in db
1409     } wifi_significant_change_result_internal;
1410 
1411 private:
1412     wifi_significant_change_params mParams;
1413     wifi_significant_change_handler mHandler;
1414     static const int MAX_RESULTS = 64;
1415     wifi_significant_change_result_internal mResultsBuffer[MAX_RESULTS];
1416     wifi_significant_change_result *mResults[MAX_RESULTS];
1417 public:
SignificantWifiChangeCommand(wifi_interface_handle handle,int id,wifi_significant_change_params params,wifi_significant_change_handler handler)1418     SignificantWifiChangeCommand(wifi_interface_handle handle, int id,
1419             wifi_significant_change_params params, wifi_significant_change_handler handler)
1420         : WifiCommand("SignificantWifiChangeCommand", handle, id), mParams(params),
1421             mHandler(handler)
1422     { }
1423 
createSetupRequest(WifiRequest & request)1424     int createSetupRequest(WifiRequest& request) {
1425         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG);
1426         if (result < 0) {
1427             return result;
1428         }
1429 
1430         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1431         result = request.put_u8(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_FLUSH, 1);
1432         if (result < 0) {
1433             return result;
1434         }
1435         result = request.put_u16(GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE, mParams.rssi_sample_size);
1436         if (result < 0) {
1437             return result;
1438         }
1439         result = request.put_u16(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE, mParams.lost_ap_sample_size);
1440         if (result < 0) {
1441             return result;
1442         }
1443         result = request.put_u16(GSCAN_ATTRIBUTE_MIN_BREACHING, mParams.min_breaching);
1444         if (result < 0) {
1445             return result;
1446         }
1447         result = request.put_u16(GSCAN_ATTRIBUTE_NUM_BSSID, mParams.num_bssid);
1448         if (result < 0) {
1449             return result;
1450         }
1451         if (mParams.num_bssid != 0) {
1452             nlattr* attr = request.attr_start(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS);
1453             if (attr == NULL) {
1454                 return WIFI_ERROR_OUT_OF_MEMORY;
1455             }
1456 
1457             for (int i = 0; i < mParams.num_bssid; i++) {
1458                 nlattr* attr2 = request.attr_start(i);
1459                 if (attr2 == NULL) {
1460                     return WIFI_ERROR_OUT_OF_MEMORY;
1461                 }
1462                 result = request.put_addr(GSCAN_ATTRIBUTE_BSSID, mParams.ap[i].bssid);
1463                 if (result < 0) {
1464                     return result;
1465                 }
1466                 result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH, mParams.ap[i].high);
1467                 if (result < 0) {
1468                     return result;
1469                 }
1470                 result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW, mParams.ap[i].low);
1471                 if (result < 0) {
1472                     return result;
1473                 }
1474                 request.attr_end(attr2);
1475             }
1476 
1477             request.attr_end(attr);
1478         }
1479         request.attr_end(data);
1480 
1481         return result;
1482     }
1483 
createTeardownRequest(WifiRequest & request)1484     int createTeardownRequest(WifiRequest& request) {
1485         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG);
1486         if (result < 0) {
1487             return result;
1488         }
1489 
1490         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1491         result = request.put_u16(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_FLUSH, 1);
1492         if (result < 0) {
1493             return result;
1494         }
1495 
1496         request.attr_end(data);
1497         return result;
1498     }
1499 
start()1500     int start() {
1501         ALOGI("Set significant wifi change config");
1502         WifiRequest request(familyId(), ifaceId());
1503 
1504         int result = createSetupRequest(request);
1505         if (result < 0) {
1506             return result;
1507         }
1508 
1509         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);
1510 
1511         result = requestResponse(request);
1512         if (result < 0) {
1513             unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);
1514             ALOGI("failed to set significant wifi change config %d", result);
1515             return result;
1516         }
1517 
1518         ALOGI("successfully set significant wifi change config");
1519 
1520         result = createFeatureRequest(request, GSCAN_SUBCMD_ENABLE_GSCAN, 1);
1521         if (result < 0) {
1522             unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);
1523             return result;
1524         }
1525 
1526         result = requestResponse(request);
1527         if (result < 0) {
1528             unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);
1529             return result;
1530         }
1531 
1532         ALOGI("successfully restarted the scan");
1533         return result;
1534     }
1535 
cancel()1536     virtual int cancel() {
1537         /* unregister event handler */
1538         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);
1539 
1540         /* create set significant change monitor message with empty hotlist */
1541         WifiRequest request(familyId(), ifaceId());
1542 
1543         int result = createTeardownRequest(request);
1544         if (result < 0) {
1545             return result;
1546         }
1547 
1548         result = requestResponse(request);
1549         if (result < 0) {
1550             return result;
1551         }
1552 
1553         ALOGI("successfully reset significant wifi change config");
1554         return result;
1555     }
1556 
handleResponse(WifiEvent & reply)1557     virtual int handleResponse(WifiEvent& reply) {
1558         /* Nothing to do on response! */
1559         return NL_SKIP;
1560     }
1561 
handleEvent(WifiEvent & event)1562     virtual int handleEvent(WifiEvent& event) {
1563         ALOGV("Got a significant wifi change event");
1564 
1565         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1566         int len = event.get_vendor_data_len();
1567 
1568         if (vendor_data == NULL || len == 0) {
1569             ALOGI("No scan results found");
1570             return NL_SKIP;
1571         }
1572 
1573         typedef struct {
1574             uint16_t flags;
1575             uint16_t channel;
1576             mac_addr bssid;
1577             s8 rssi_history[8];
1578         } ChangeInfo;
1579 
1580         int num = min(len / sizeof(ChangeInfo), MAX_RESULTS);
1581         ChangeInfo *ci = (ChangeInfo *)event.get_vendor_data();
1582 
1583         for (int i = 0; i < num; i++) {
1584             memcpy(mResultsBuffer[i].bssid, ci[i].bssid, sizeof(mac_addr));
1585             mResultsBuffer[i].channel = ci[i].channel;
1586             mResultsBuffer[i].num_rssi = 8;
1587             for (int j = 0; j < mResultsBuffer[i].num_rssi; j++)
1588                 mResultsBuffer[i].rssi[j] = (int) ci[i].rssi_history[j];
1589             mResults[i] = reinterpret_cast<wifi_significant_change_result *>(&(mResultsBuffer[i]));
1590         }
1591 
1592         ALOGV("Retrieved %d scan results", num);
1593 
1594         if (num != 0) {
1595             (*mHandler.on_significant_change)(id(), num, mResults);
1596         } else {
1597             ALOGW("No significant change reported");
1598         }
1599 
1600         return NL_SKIP;
1601     }
1602 };
1603 
wifi_set_significant_change_handler(wifi_request_id id,wifi_interface_handle iface,wifi_significant_change_params params,wifi_significant_change_handler handler)1604 wifi_error wifi_set_significant_change_handler(wifi_request_id id, wifi_interface_handle iface,
1605         wifi_significant_change_params params, wifi_significant_change_handler handler)
1606 {
1607     wifi_handle handle = getWifiHandle(iface);
1608 
1609     SignificantWifiChangeCommand *cmd = new SignificantWifiChangeCommand(
1610             iface, id, params, handler);
1611     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
1612     wifi_error result = wifi_register_cmd(handle, id, cmd);
1613     if (result != WIFI_SUCCESS) {
1614         cmd->releaseRef();
1615         return result;
1616     }
1617     result = (wifi_error)cmd->start();
1618     if (result != WIFI_SUCCESS) {
1619         wifi_unregister_cmd(handle, id);
1620         cmd->releaseRef();
1621         return result;
1622     }
1623     return result;
1624 }
1625 
wifi_reset_significant_change_handler(wifi_request_id id,wifi_interface_handle iface)1626 wifi_error wifi_reset_significant_change_handler(wifi_request_id id, wifi_interface_handle iface)
1627 {
1628     return wifi_cancel_cmd(id, iface);
1629 }
1630 
wifi_reset_epno_list(wifi_request_id id,wifi_interface_handle iface)1631 wifi_error wifi_reset_epno_list(wifi_request_id id, wifi_interface_handle iface)
1632 {
1633     if (id == -1) {
1634         wifi_epno_handler handler;
1635 
1636         memset(&handler, 0, sizeof(handler));
1637         ePNOCommand *cmd = new ePNOCommand(iface, id, NULL, handler);
1638         NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
1639         cmd->cancel();
1640         cmd->releaseRef();
1641         return WIFI_SUCCESS;
1642     }
1643     return wifi_cancel_cmd(id, iface);
1644 }
1645 
wifi_set_epno_list(wifi_request_id id,wifi_interface_handle iface,const wifi_epno_params * params,wifi_epno_handler handler)1646 wifi_error wifi_set_epno_list(wifi_request_id id, wifi_interface_handle iface,
1647         const wifi_epno_params *params, wifi_epno_handler handler)
1648 {
1649     wifi_handle handle = getWifiHandle(iface);
1650     if (handler.on_network_found == NULL) {
1651         return WIFI_ERROR_INVALID_ARGS;
1652     }
1653 
1654     ePNOCommand *cmd = new ePNOCommand(iface, id, params, handler);
1655     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
1656     wifi_error result = wifi_register_cmd(handle, id, cmd);
1657     if (result != WIFI_SUCCESS) {
1658         cmd->releaseRef();
1659         return result;
1660     }
1661     result = (wifi_error)cmd->start();
1662     if (result != WIFI_SUCCESS) {
1663         wifi_unregister_cmd(handle, id);
1664         cmd->releaseRef();
1665         return result;
1666     }
1667     return result;
1668 }
1669 
1670 class BssidBlacklistCommand : public WifiCommand
1671 {
1672     private:
1673         wifi_bssid_params *mParams;
1674     public:
BssidBlacklistCommand(wifi_interface_handle handle,int id,wifi_bssid_params * params)1675         BssidBlacklistCommand(wifi_interface_handle handle, int id,
1676                 wifi_bssid_params *params)
1677             : WifiCommand("BssidBlacklistCommand", handle, id), mParams(params)
1678         { }
createRequest(WifiRequest & request)1679         int createRequest(WifiRequest& request) {
1680             if ((mParams->num_bssid < 0) || (mParams->num_bssid > MAX_BLACKLIST_BSSID)) {
1681                 return WIFI_ERROR_INVALID_ARGS;
1682             }
1683             int result = request.create(GOOGLE_OUI, WIFI_SUBCMD_SET_BSSID_BLACKLIST);
1684             if (result < 0) {
1685                 return result;
1686             }
1687 
1688             nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1689             if (!mParams->num_bssid) {
1690                 result = request.put_u32(GSCAN_ATTRIBUTE_BSSID_BLACKLIST_FLUSH, 1);
1691                 if (result < 0) {
1692                     return result;
1693                 }
1694             } else {
1695                 result = request.put_u32(GSCAN_ATTRIBUTE_NUM_BSSID, mParams->num_bssid);
1696                 if (result < 0) {
1697                     return result;
1698                 }
1699                 for (int i = 0; i < mParams->num_bssid; i++) {
1700                     result = request.put_addr(GSCAN_ATTRIBUTE_BLACKLIST_BSSID, mParams->bssids[i]);
1701                     if (result < 0) {
1702                         return result;
1703                     }
1704                }
1705             }
1706             request.attr_end(data);
1707             return result;
1708         }
1709 
start()1710         int start() {
1711             ALOGV("Executing bssid blacklist request, num = %d", mParams->num_bssid);
1712             WifiRequest request(familyId(), ifaceId());
1713             int result = createRequest(request);
1714             if (result < 0) {
1715                 return result;
1716             }
1717 
1718             result = requestResponse(request);
1719             if (result < 0) {
1720                 ALOGE("Failed to execute bssid blacklist request, result = %d", result);
1721                 return result;
1722             }
1723 
1724             ALOGI("Successfully added %d blacklist bssids", mParams->num_bssid);
1725             return result;
1726         }
1727 
handleResponse(WifiEvent & reply)1728         virtual int handleResponse(WifiEvent& reply) {
1729             /* Nothing to do on response! */
1730             return NL_SKIP;
1731         }
1732 };
1733 
wifi_set_bssid_blacklist(wifi_request_id id,wifi_interface_handle iface,wifi_bssid_params params)1734 wifi_error wifi_set_bssid_blacklist(wifi_request_id id, wifi_interface_handle iface,
1735         wifi_bssid_params params)
1736 {
1737     BssidBlacklistCommand *cmd = new BssidBlacklistCommand(iface, id, &params);
1738     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
1739     wifi_error result = (wifi_error)cmd->start();
1740     //release the reference of command as well
1741     cmd->releaseRef();
1742     return result;
1743 }
1744 
wifi_configure_roaming(wifi_interface_handle iface,wifi_roaming_config * roam_config)1745 wifi_error wifi_configure_roaming(wifi_interface_handle iface,
1746 		wifi_roaming_config *roam_config)
1747 {
1748     wifi_error ret;
1749     wifi_bssid_params bssid_params;
1750     unsigned int i;
1751     wifi_request_id id = 0;
1752 
1753     /* Set bssid blacklist */
1754     if (roam_config->num_blacklist_bssid == 0) {
1755         /* Flush request */
1756         ALOGI("%s: num_blacklist_bssid == 0 (flush)", __FUNCTION__);
1757     }
1758 
1759     bssid_params.num_bssid = roam_config->num_blacklist_bssid;
1760 
1761     for (i = 0; i < roam_config->num_blacklist_bssid; i++) {
1762         mac_addr &addr1 = roam_config->blacklist_bssid[i];
1763         memcpy(&bssid_params.bssids[i], &roam_config->blacklist_bssid[i],
1764             sizeof(mac_addr));
1765         ALOGI("%02x:%02x:%02x:%02x:%02x:%02x\n", addr1[0],
1766             addr1[1], addr1[2], addr1[3], addr1[4], addr1[5]);
1767     }
1768     ret = wifi_set_bssid_blacklist(id, iface, bssid_params);
1769     if (ret != WIFI_SUCCESS) {
1770         ALOGE("%s: Failed to configure blacklist bssids", __FUNCTION__);
1771         return ret;
1772     }
1773 
1774     return ret;
1775 }
1776 
1777 class FirmwareRoamingStateCommand : public WifiCommand
1778 {
1779     private:
1780         fw_roaming_state_t roam_state;
1781     public:
FirmwareRoamingStateCommand(wifi_interface_handle handle,fw_roaming_state_t state)1782         FirmwareRoamingStateCommand(wifi_interface_handle handle,
1783                 fw_roaming_state_t state)
1784             : WifiCommand("FirmwareRoamingStateCommand", handle, -1), roam_state(state)
1785         { }
createRequest(WifiRequest & request)1786         int createRequest(WifiRequest& request) {
1787             int result = request.create(GOOGLE_OUI, WIFI_SUBCMD_FW_ROAM_POLICY);
1788             if (result < 0) {
1789                 return result;
1790             }
1791 
1792             nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1793             result = request.put_u32(GSCAN_ATTRIBUTE_ROAM_STATE_SET, roam_state);
1794             if (result < 0) {
1795                 return result;
1796             }
1797             request.attr_end(data);
1798             return result;
1799         }
1800 
start()1801         int start() {
1802             ALOGV("Executing firmware roam state set, state = %d", roam_state);
1803             WifiRequest request(familyId(), ifaceId());
1804             int result = createRequest(request);
1805             if (result < 0) {
1806                 return result;
1807             }
1808 
1809             result = requestResponse(request);
1810             if (result < 0) {
1811                 ALOGE("Failed to execute firmware roam state set, result = %d", result);
1812                 return result;
1813             }
1814 
1815             ALOGI("Successfully set firmware roam state - %d", roam_state);
1816             return result;
1817         }
1818 
handleResponse(WifiEvent & reply)1819         virtual int handleResponse(WifiEvent& reply) {
1820             /* Nothing to do on response! */
1821             return NL_SKIP;
1822         }
1823 };
1824 
wifi_enable_firmware_roaming(wifi_interface_handle iface,fw_roaming_state_t state)1825 wifi_error wifi_enable_firmware_roaming(wifi_interface_handle iface,
1826             fw_roaming_state_t state)
1827 {
1828     /* Set firmware roaming state */
1829     FirmwareRoamingStateCommand *cmd = new FirmwareRoamingStateCommand(iface, state);
1830     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
1831     wifi_error result = (wifi_error)cmd->start();
1832     //release the reference of command as well
1833     cmd->releaseRef();
1834     return result;
1835 }
1836 
1837 ////////////////////////////////////////////////////////////////////////////////
1838 
1839 class AnqpoConfigureCommand : public WifiCommand
1840 {
1841     int num_hs;
1842     wifi_passpoint_network *mNetworks;
1843     wifi_passpoint_event_handler mHandler;
1844     wifi_scan_result *mResult;
1845 public:
AnqpoConfigureCommand(wifi_request_id id,wifi_interface_handle iface,int num,wifi_passpoint_network * hs_list,wifi_passpoint_event_handler handler)1846     AnqpoConfigureCommand(wifi_request_id id, wifi_interface_handle iface,
1847         int num, wifi_passpoint_network *hs_list, wifi_passpoint_event_handler handler)
1848         : WifiCommand("AnqpoConfigureCommand", iface, id), num_hs(num), mNetworks(hs_list),
1849             mHandler(handler)
1850     {
1851         mResult = NULL;
1852     }
1853 
createRequest(WifiRequest & request,int val)1854     int createRequest(WifiRequest& request, int val) {
1855 
1856         int result = request.create(GOOGLE_OUI, GSCAN_SUBCMD_ANQPO_CONFIG);
1857         result = request.put_u32(GSCAN_ATTRIBUTE_ANQPO_HS_LIST_SIZE, num_hs);
1858         if (result < 0) {
1859             return result;
1860         }
1861 
1862         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1863 
1864         struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_ANQPO_HS_LIST);
1865         for (int i = 0; i < num_hs; i++) {
1866             nlattr *attr2 = request.attr_start(i);
1867             if (attr2 == NULL) {
1868                 return WIFI_ERROR_OUT_OF_MEMORY;
1869             }
1870             result = request.put_u32(GSCAN_ATTRIBUTE_ANQPO_HS_NETWORK_ID, mNetworks[i].id);
1871             if (result < 0) {
1872                 return result;
1873             }
1874             result = request.put(GSCAN_ATTRIBUTE_ANQPO_HS_NAI_REALM, mNetworks[i].realm, 256);
1875             if (result < 0) {
1876                 return result;
1877             }
1878             result = request.put(GSCAN_ATTRIBUTE_ANQPO_HS_ROAM_CONSORTIUM_ID,
1879                          mNetworks[i].roamingConsortiumIds, 128);
1880             if (result < 0) {
1881                 return result;
1882             }
1883             result = request.put(GSCAN_ATTRIBUTE_ANQPO_HS_PLMN, mNetworks[i].plmn, 3);
1884             if (result < 0) {
1885                 return result;
1886             }
1887 
1888             request.attr_end(attr2);
1889         }
1890 
1891         request.attr_end(attr);
1892         request.attr_end(data);
1893 
1894         return WIFI_SUCCESS;
1895     }
1896 
start()1897     int start() {
1898 
1899         WifiRequest request(familyId(), ifaceId());
1900         int result = createRequest(request, num_hs);
1901         if (result != WIFI_SUCCESS) {
1902             ALOGE("failed to create request; result = %d", result);
1903             return result;
1904         }
1905 
1906         registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_ANQPO_HOTSPOT_MATCH);
1907 
1908         result = requestResponse(request);
1909         if (result != WIFI_SUCCESS) {
1910             unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_ANQPO_HOTSPOT_MATCH);
1911             ALOGE("failed to set ANQPO networks; result = %d", result);
1912             return result;
1913         }
1914 
1915         return result;
1916     }
1917 
cancel()1918     virtual int cancel() {
1919 
1920         WifiRequest request(familyId(), ifaceId());
1921         int result = createRequest(request, 0);
1922         if (result != WIFI_SUCCESS) {
1923             ALOGE("failed to create request; result = %d", result);
1924         } else {
1925             result = requestResponse(request);
1926             if (result != WIFI_SUCCESS) {
1927                 ALOGE("failed to reset ANQPO networks;result = %d", result);
1928             }
1929         }
1930 
1931         unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_ANQPO_HOTSPOT_MATCH);
1932         return WIFI_SUCCESS;
1933     }
1934 
handleResponse(WifiEvent & reply)1935     virtual int handleResponse(WifiEvent& reply) {
1936          ALOGD("Request complete!");
1937         /* Nothing to do on response! */
1938         return NL_SKIP;
1939     }
1940 
handleEvent(WifiEvent & event)1941     virtual int handleEvent(WifiEvent& event) {
1942         typedef struct {
1943             u16 channel;        /* channel of GAS protocol */
1944             u8  dialog_token;   /* GAS dialog token */
1945             u8  fragment_id;    /* fragment id */
1946             u16 status_code;    /* status code on GAS completion */
1947             u16 data_len;       /* length of data to follow */
1948             u8  data[1];        /* variable length specified by data_len */
1949         } wifi_anqp_gas_resp;
1950 
1951         ALOGI("ANQPO hotspot matched event!");
1952 
1953         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1954         unsigned int len = event.get_vendor_data_len();
1955 
1956         if (vendor_data == NULL || len < sizeof(wifi_scan_result)) {
1957             ALOGI("No scan results found");
1958             return NL_SKIP;
1959         }
1960         mResult = (wifi_scan_result *)malloc(sizeof(wifi_scan_result));
1961         if (!mResult) {
1962             return NL_SKIP;
1963         }
1964 
1965         wifi_gscan_full_result_t *drv_res = (wifi_gscan_full_result_t *)event.get_vendor_data();
1966         /* To protect against corrupted data, put a ceiling */
1967         int ie_len = min(MAX_PROBE_RESP_IE_LEN, drv_res->ie_length);
1968         if ((ie_len + offsetof(wifi_gscan_full_result_t, ie_data)) > len) {
1969             ALOGE("BAD event data, len %d ie_len %d fixed length %lu!\n", len,
1970                 ie_len, offsetof(wifi_gscan_full_result_t, ie_data));
1971             return NL_SKIP;
1972         }
1973 
1974         wifi_gscan_result_t *fixed = &drv_res->fixed;
1975         convert_to_hal_result(mResult, fixed);
1976 
1977         byte *anqp = (byte *)drv_res + offsetof(wifi_gscan_full_result_t, ie_data) + drv_res->ie_length;
1978         wifi_anqp_gas_resp *gas = (wifi_anqp_gas_resp *)anqp;
1979         int anqp_len = offsetof(wifi_anqp_gas_resp, data) + gas->data_len;
1980         int networkId = *(int *)((byte *)anqp + anqp_len);
1981 
1982         ALOGI("%-32s\t", mResult->ssid);
1983 
1984         ALOGI("%02x:%02x:%02x:%02x:%02x:%02x ", mResult->bssid[0], mResult->bssid[1],
1985                 mResult->bssid[2], mResult->bssid[3], mResult->bssid[4], mResult->bssid[5]);
1986 
1987         ALOGI("rssi:%d\t", mResult->rssi);
1988         ALOGI("channel:%d\t", mResult->channel);
1989         ALOGI("ts:0x%jx\t", mResult->ts);
1990         ALOGI("rtt:0x%jx\t", mResult->rtt);
1991         ALOGI("rtt_sd:0x%jx\n", mResult->rtt_sd);
1992 
1993         if(*mHandler.on_passpoint_network_found)
1994             (*mHandler.on_passpoint_network_found)(id(), networkId, mResult, anqp_len, anqp);
1995         free(mResult);
1996         return NL_SKIP;
1997     }
1998 };
1999 
wifi_set_passpoint_list(wifi_request_id id,wifi_interface_handle iface,int num,wifi_passpoint_network * networks,wifi_passpoint_event_handler handler)2000 wifi_error wifi_set_passpoint_list(wifi_request_id id, wifi_interface_handle iface, int num,
2001         wifi_passpoint_network *networks, wifi_passpoint_event_handler handler)
2002 {
2003     wifi_handle handle = getWifiHandle(iface);
2004 
2005     AnqpoConfigureCommand *cmd = new AnqpoConfigureCommand(id, iface, num, networks, handler);
2006     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
2007     wifi_error result = wifi_register_cmd(handle, id, cmd);
2008     if (result != WIFI_SUCCESS) {
2009         cmd->releaseRef();
2010         return result;
2011     }
2012     result = (wifi_error)cmd->start();
2013     if (result != WIFI_SUCCESS) {
2014         wifi_unregister_cmd(handle, id);
2015         cmd->releaseRef();
2016         return result;
2017     }
2018     return result;
2019 }
2020 
wifi_reset_passpoint_list(wifi_request_id id,wifi_interface_handle iface)2021 wifi_error wifi_reset_passpoint_list(wifi_request_id id, wifi_interface_handle iface)
2022 {
2023     return wifi_cancel_cmd(id, iface);
2024 }
2025