[7570] wlbt: Backout WiFi HAL changes for LLS
[GitHub/MotorolaMobilityLLC/hardware-samsung_slsi-scsc_wifibt-wifi_hal.git] / gscan.cpp
1
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <fcntl.h>
5 #include <sys/socket.h>
6 #include <netlink/genl/genl.h>
7 #include <netlink/genl/family.h>
8 #include <netlink/genl/ctrl.h>
9 #include <linux/rtnetlink.h>
10 #include <netpacket/packet.h>
11 #include <linux/filter.h>
12 #include <linux/errqueue.h>
13
14 #include <linux/pkt_sched.h>
15 #include <netlink/object-api.h>
16 #include <netlink/netlink.h>
17 #include <netlink/socket.h>
18 #include <netlink/handlers.h>
19
20 #include "sync.h"
21
22 #define LOG_TAG "WifiHAL"
23
24 #include <utils/Log.h>
25
26 #include "wifi_hal.h"
27 #include "common.h"
28 #include "cpp_bindings.h"
29
30 typedef enum {
31
32 GSCAN_ATTRIBUTE_NUM_BUCKETS = 10,
33 GSCAN_ATTRIBUTE_BASE_PERIOD,
34 GSCAN_ATTRIBUTE_BUCKETS_BAND,
35 GSCAN_ATTRIBUTE_BUCKET_ID,
36 GSCAN_ATTRIBUTE_BUCKET_PERIOD,
37 GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS,
38 GSCAN_ATTRIBUTE_BUCKET_CHANNELS,
39 GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN,
40 GSCAN_ATTRIBUTE_REPORT_THRESHOLD,
41 GSCAN_ATTRIBUTE_NUM_SCANS_TO_CACHE,
42 GSCAN_ATTRIBUTE_REPORT_THRESHOLD_NUM_SCANS,
43 GSCAN_ATTRIBUTE_BAND = GSCAN_ATTRIBUTE_BUCKETS_BAND,
44
45 GSCAN_ATTRIBUTE_ENABLE_FEATURE = 20,
46 GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE, /* indicates no more results */
47 GSCAN_ATTRIBUTE_REPORT_EVENTS,
48
49 /* remaining reserved for additional attributes */
50 GSCAN_ATTRIBUTE_NUM_OF_RESULTS = 30,
51 GSCAN_ATTRIBUTE_SCAN_RESULTS, /* flat array of wifi_scan_result */
52 GSCAN_ATTRIBUTE_NUM_CHANNELS,
53 GSCAN_ATTRIBUTE_CHANNEL_LIST,
54 GSCAN_ATTRIBUTE_SCAN_ID,
55 GSCAN_ATTRIBUTE_SCAN_FLAGS,
56
57 /* remaining reserved for additional attributes */
58
59 GSCAN_ATTRIBUTE_SSID = 40,
60 GSCAN_ATTRIBUTE_BSSID,
61 GSCAN_ATTRIBUTE_CHANNEL,
62 GSCAN_ATTRIBUTE_RSSI,
63 GSCAN_ATTRIBUTE_TIMESTAMP,
64 GSCAN_ATTRIBUTE_RTT,
65 GSCAN_ATTRIBUTE_RTTSD,
66
67 /* remaining reserved for additional attributes */
68
69 GSCAN_ATTRIBUTE_HOTLIST_BSSIDS = 50,
70 GSCAN_ATTRIBUTE_RSSI_LOW,
71 GSCAN_ATTRIBUTE_RSSI_HIGH,
72 GSCAN_ATTRIBUTE_HOTLIST_ELEM,
73 GSCAN_ATTRIBUTE_HOTLIST_FLUSH,
74 GSCAN_ATTRIBUTE_CHANNEL_NUMBER,
75
76 /* remaining reserved for additional attributes */
77 GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE = 60,
78 GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE,
79 GSCAN_ATTRIBUTE_MIN_BREACHING,
80 GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS,
81
82 GSCAN_ATTRIBUTE_BUCKET_STEP_COUNT = 70,
83 GSCAN_ATTRIBUTE_BUCKET_EXPONENT,
84 GSCAN_ATTRIBUTE_BUCKET_MAX_PERIOD,
85
86 GSCAN_ATTRIBUTE_NUM_BSSID,
87 GSCAN_ATTRIBUTE_BLACKLIST_BSSID,
88
89 GSCAN_ATTRIBUTE_MAX
90
91 } GSCAN_ATTRIBUTE;
92
93 typedef enum {
94 EPNO_ATTRIBUTE_SSID_LIST,
95 EPNO_ATTRIBUTE_SSID_NUM,
96 EPNO_ATTRIBUTE_SSID,
97 EPNO_ATTRIBUTE_SSID_LEN,
98 EPNO_ATTRIBUTE_RSSI,
99 EPNO_ATTRIBUTE_FLAGS,
100 EPNO_ATTRIBUTE_AUTH,
101 EPNO_ATTRIBUTE_MAX
102 } EPNO_ATTRIBUTE;
103
104 typedef enum {
105 EPNO_ATTRIBUTE_HS_PARAM_LIST,
106 EPNO_ATTRIBUTE_HS_NUM,
107 EPNO_ATTRIBUTE_HS_ID,
108 EPNO_ATTRIBUTE_HS_REALM,
109 EPNO_ATTRIBUTE_HS_CONSORTIUM_IDS,
110 EPNO_ATTRIBUTE_HS_PLMN,
111 EPNO_ATTRIBUTE_HS_MAX
112 } EPNO_HS_ATTRIBUTE;
113
114
115 class GetCapabilitiesCommand : public WifiCommand
116 {
117 wifi_gscan_capabilities *mCapabilities;
118 public:
119 GetCapabilitiesCommand(wifi_interface_handle iface, wifi_gscan_capabilities *capabitlites)
120 : WifiCommand(iface, 0), mCapabilities(capabitlites)
121 {
122 memset(mCapabilities, 0, sizeof(*mCapabilities));
123 }
124
125 virtual int create() {
126 ALOGD("Creating message to get scan capablities; iface = %d", mIfaceInfo->id);
127
128 int ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_CAPABILITIES);
129 if (ret < 0) {
130 ALOGD("NL message creation failed");
131 return ret;
132 }
133
134 return ret;
135 }
136
137 protected:
138 virtual int handleResponse(WifiEvent& reply) {
139
140 ALOGD("In GetCapabilities::handleResponse");
141
142 if (reply.get_cmd() != NL80211_CMD_VENDOR) {
143 ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
144 return NL_SKIP;
145 }
146
147 int id = reply.get_vendor_id();
148 int subcmd = reply.get_vendor_subcmd();
149
150 void *data = reply.get_vendor_data();
151 int len = reply.get_vendor_data_len();
152
153 ALOGD("Id = %0x, subcmd = %d, len = %d, expected len = %d", id, subcmd, len,
154 sizeof(*mCapabilities));
155
156 memcpy(mCapabilities, data, min(len, (int) sizeof(*mCapabilities)));
157
158 return NL_OK;
159 }
160 };
161
162
163 wifi_error wifi_get_gscan_capabilities(wifi_interface_handle handle,
164 wifi_gscan_capabilities *capabilities)
165 {
166 GetCapabilitiesCommand command(handle, capabilities);
167 return (wifi_error) command.requestResponse();
168 }
169
170 class GetChannelListCommand : public WifiCommand
171 {
172 wifi_channel *channels;
173 int max_channels;
174 int *num_channels;
175 int band;
176 public:
177 GetChannelListCommand(wifi_interface_handle iface, wifi_channel *channel_buf, int *ch_num,
178 int num_max_ch, int band)
179 : WifiCommand(iface, 0), channels(channel_buf), max_channels(num_max_ch), num_channels(ch_num),
180 band(band)
181 {
182 memset(channels, 0, sizeof(wifi_channel) * max_channels);
183 }
184 virtual int create() {
185 ALOGD("Creating message to get channel list; iface = %d", mIfaceInfo->id);
186
187 int ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_VALID_CHANNELS);
188 if (ret < 0) {
189 return ret;
190 }
191
192 nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
193 ret = mMsg.put_u32(GSCAN_ATTRIBUTE_BAND, band);
194 if (ret < 0) {
195 return ret;
196 }
197
198 mMsg.attr_end(data);
199
200 return ret;
201 }
202
203 protected:
204 virtual int handleResponse(WifiEvent& reply) {
205
206 ALOGD("In GetChannelList::handleResponse");
207
208 if (reply.get_cmd() != NL80211_CMD_VENDOR) {
209 ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
210 return NL_SKIP;
211 }
212
213 int id = reply.get_vendor_id();
214 int subcmd = reply.get_vendor_subcmd();
215 int num_channels_to_copy = 0;
216
217 nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
218 int len = reply.get_vendor_data_len();
219
220 ALOGD("Id = %0x, subcmd = %d, len = %d", id, subcmd, len);
221 if (vendor_data == NULL || len == 0) {
222 ALOGE("no vendor data in GetChannelList response; ignoring it");
223 return NL_SKIP;
224 }
225
226 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
227 if (it.get_type() == GSCAN_ATTRIBUTE_NUM_CHANNELS) {
228 num_channels_to_copy = it.get_u32();
229 ALOGD("Got channel list with %d channels", num_channels_to_copy);
230 if(num_channels_to_copy > max_channels)
231 num_channels_to_copy = max_channels;
232 *num_channels = num_channels_to_copy;
233 } else if (it.get_type() == GSCAN_ATTRIBUTE_CHANNEL_LIST && num_channels_to_copy) {
234 memcpy(channels, it.get_data(), sizeof(int) * num_channels_to_copy);
235 } else {
236 ALOGW("Ignoring invalid attribute type = %d, size = %d",
237 it.get_type(), it.get_len());
238 }
239 }
240
241 return NL_OK;
242 }
243 };
244
245 wifi_error wifi_get_valid_channels(wifi_interface_handle handle,
246 int band, int max_channels, wifi_channel *channels, int *num_channels)
247 {
248 GetChannelListCommand command(handle, channels, num_channels,
249 max_channels, band);
250 return (wifi_error) command.requestResponse();
251 }
252 /////////////////////////////////////////////////////////////////////////////
253
254 /* helper functions */
255
256 static int parseScanResults(wifi_scan_result *results, int num, nlattr *attr)
257 {
258 memset(results, 0, sizeof(wifi_scan_result) * num);
259
260 int i = 0;
261 for (nl_iterator it(attr); it.has_next() && i < num; it.next(), i++) {
262
263 int index = it.get_type();
264 ALOGD("retrieved scan result %d", index);
265 nlattr *sc_data = (nlattr *) it.get_data();
266 wifi_scan_result *result = results + i;
267
268 for (nl_iterator it2(sc_data); it2.has_next(); it2.next()) {
269 int type = it2.get_type();
270 if (type == GSCAN_ATTRIBUTE_SSID) {
271 strncpy(result->ssid, (char *) it2.get_data(), it2.get_len());
272 result->ssid[it2.get_len()] = 0;
273 } else if (type == GSCAN_ATTRIBUTE_BSSID) {
274 memcpy(result->bssid, (byte *) it2.get_data(), sizeof(mac_addr));
275 } else if (type == GSCAN_ATTRIBUTE_TIMESTAMP) {
276 result->ts = it2.get_u64();
277 } else if (type == GSCAN_ATTRIBUTE_CHANNEL) {
278 result->ts = it2.get_u16();
279 } else if (type == GSCAN_ATTRIBUTE_RSSI) {
280 result->rssi = it2.get_u8();
281 } else if (type == GSCAN_ATTRIBUTE_RTT) {
282 result->rtt = it2.get_u64();
283 } else if (type == GSCAN_ATTRIBUTE_RTTSD) {
284 result->rtt_sd = it2.get_u64();
285 }
286 }
287
288 }
289
290 if (i >= num) {
291 ALOGE("Got too many results; skipping some");
292 }
293
294 return i;
295 }
296
297 int createFeatureRequest(WifiRequest& request, int subcmd) {
298
299 int result = request.create(GOOGLE_OUI, subcmd);
300 if (result < 0) {
301 return result;
302 }
303
304 return WIFI_SUCCESS;
305 }
306
307 class ScanCommand : public WifiCommand
308 {
309 wifi_scan_cmd_params *mParams;
310 wifi_scan_result_handler mHandler;
311 static unsigned mGlobalFullScanBuckets;
312 bool mLocalFullScanBuckets;
313 public:
314 ScanCommand(wifi_interface_handle iface, int id, wifi_scan_cmd_params *params,
315 wifi_scan_result_handler handler)
316 : WifiCommand(iface, id), mParams(params), mHandler(handler),
317 mLocalFullScanBuckets(0)
318 { }
319
320 int createSetupRequest(WifiRequest& request) {
321 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN);
322 if (result < 0) {
323 return result;
324 }
325
326 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
327 result = request.put_u32(GSCAN_ATTRIBUTE_BASE_PERIOD, mParams->base_period);
328 if (result < 0) {
329 return result;
330 }
331
332 result = request.put_u32(GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN, mParams->max_ap_per_scan);
333 if (result < 0) {
334 return result;
335 }
336
337 result = request.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD, mParams->report_threshold_percent);
338 if (result < 0) {
339 return result;
340 }
341
342 result = request.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD_NUM_SCANS, mParams->report_threshold_num_scans);
343 if (result < 0) {
344 return result;
345 }
346
347 result = request.put_u32(GSCAN_ATTRIBUTE_NUM_BUCKETS, mParams->num_buckets);
348 if (result < 0) {
349 return result;
350 }
351
352 for (int i = 0; i < mParams->num_buckets; i++) {
353 nlattr * bucket = request.attr_start(i); // next bucket
354 result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_ID, mParams->buckets[i].bucket);
355 if (result < 0) {
356 return result;
357 }
358 result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_PERIOD, mParams->buckets[i].period);
359 if (result < 0) {
360 return result;
361 }
362 result = request.put_u32(GSCAN_ATTRIBUTE_BUCKETS_BAND,
363 mParams->buckets[i].band);
364 if (result < 0) {
365 return result;
366 }
367
368 result = request.put_u32(GSCAN_ATTRIBUTE_REPORT_EVENTS,
369 mParams->buckets[i].report_events);
370 if (result < 0) {
371 return result;
372 }
373
374 result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS,
375 mParams->buckets[i].num_channels);
376 if (result < 0) {
377 return result;
378 }
379
380 result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_EXPONENT,
381 mParams->buckets[i].exponent);
382 if (result < 0) {
383 return result;
384 }
385
386 result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_MAX_PERIOD,
387 mParams->buckets[i].max_period);
388 if (result < 0) {
389 return result;
390 }
391
392 result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_STEP_COUNT,
393 mParams->buckets[i].step_count);
394 if (result < 0) {
395 return result;
396 }
397
398 if (mParams->buckets[i].num_channels) {
399 nlattr *channels = request.attr_start(GSCAN_ATTRIBUTE_BUCKET_CHANNELS);
400 for (int j = 0; j < mParams->buckets[i].num_channels; j++) {
401 result = request.put_u32(j, mParams->buckets[i].channels[j].channel);
402 if (result < 0) {
403 return result;
404 }
405 }
406 request.attr_end(channels);
407 }
408
409 request.attr_end(bucket);
410 }
411
412 request.attr_end(data);
413 return WIFI_SUCCESS;
414 }
415
416 int createStartRequest(WifiRequest& request) {
417 return createFeatureRequest(request, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN);
418 }
419
420 int createStopRequest(WifiRequest& request) {
421 return createFeatureRequest(request, SLSI_NL80211_VENDOR_SUBCMD_DEL_GSCAN);
422 }
423
424 int start() {
425 ALOGD(" sending scan req to driver");
426 WifiRequest request(familyId(), ifaceId());
427 int result = createSetupRequest(request);
428 if (result != WIFI_SUCCESS) {
429 ALOGE("failed to create setup request; result = %d", result);
430 return result;
431 }
432 ALOGD("Starting scan");
433
434 registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);
435 registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);
436
437 int nBuckets = 0;
438 for (int i = 0; i < mParams->num_buckets; i++) {
439 if (mParams->buckets[i].report_events == 2) {
440 nBuckets++;
441 }
442 }
443
444 if (nBuckets != 0) {
445 ALOGI("Full scan requested with nBuckets = %d", nBuckets);
446 registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);
447 }
448 result = requestResponse(request);
449 if (result != WIFI_SUCCESS) {
450 ALOGE("failed to start scan; result = %d", result);
451 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);
452 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);
453 return result;
454 }
455
456
457 return result;
458 }
459
460 virtual int cancel() {
461 ALOGD("Stopping scan");
462
463 WifiRequest request(familyId(), ifaceId());
464 int result = createStopRequest(request);
465 if (result != WIFI_SUCCESS) {
466 ALOGE("failed to create stop request; result = %d", result);
467 } else {
468 result = requestResponse(request);
469 if (result != WIFI_SUCCESS) {
470 ALOGE("failed to stop scan; result = %d", result);
471 }
472 }
473
474 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);
475 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);
476 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);
477
478 return WIFI_SUCCESS;
479 }
480
481 virtual int handleResponse(WifiEvent& reply) {
482 /* Nothing to do on response! */
483 return NL_SKIP;
484 }
485
486 virtual int handleEvent(WifiEvent& event) {
487 ALOGD("Got a scan results event");
488
489 event.log();
490
491 nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
492 unsigned int len = event.get_vendor_data_len();
493 int event_id = event.get_vendor_subcmd();
494 ALOGD("handleEvent, event_id = %d", event_id);
495
496 if(event_id == GSCAN_EVENT_COMPLETE_SCAN) {
497 if (vendor_data == NULL || len != 4) {
498 ALOGD("Scan complete type not mentioned!");
499 return NL_SKIP;
500 }
501 wifi_scan_event evt_type;
502
503 evt_type = (wifi_scan_event) event.get_u32(NL80211_ATTR_VENDOR_DATA);
504 ALOGD("Scan complete: Received event type %d", evt_type);
505 if(*mHandler.on_scan_event)
506 (*mHandler.on_scan_event)(evt_type, evt_type);
507 } else if(event_id == GSCAN_EVENT_FULL_SCAN_RESULTS) {
508 if (vendor_data == NULL || len < sizeof(wifi_scan_result)) {
509 ALOGD("No scan results found");
510 return NL_SKIP;
511 }
512 wifi_scan_result *result = (wifi_scan_result *)event.get_vendor_data();
513
514 if(*mHandler.on_full_scan_result)
515 (*mHandler.on_full_scan_result)(id(), result);
516
517 ALOGD("%-32s\t", result->ssid);
518
519 ALOGD("%02x:%02x:%02x:%02x:%02x:%02x ", result->bssid[0], result->bssid[1],
520 result->bssid[2], result->bssid[3], result->bssid[4], result->bssid[5]);
521
522 ALOGD("%d\t", result->rssi);
523 ALOGD("%d\t", result->channel);
524 ALOGD("%lld\t", result->ts);
525 ALOGD("%lld\t", result->rtt);
526 ALOGD("%lld\n", result->rtt_sd);
527 } else {
528
529 if (vendor_data == NULL || len != 4) {
530 ALOGD("No scan results found");
531 return NL_SKIP;
532 }
533
534 int num = event.get_u32(NL80211_ATTR_VENDOR_DATA);
535 ALOGD("Found %d scan results", num);
536 if(*mHandler.on_scan_results_available)
537 (*mHandler.on_scan_results_available)(id(), num);
538 }
539 return NL_SKIP;
540 }
541 };
542
543 unsigned ScanCommand::mGlobalFullScanBuckets = 0;
544
545 wifi_error wifi_start_gscan(
546 wifi_request_id id,
547 wifi_interface_handle iface,
548 wifi_scan_cmd_params params,
549 wifi_scan_result_handler handler)
550 {
551 wifi_handle handle = getWifiHandle(iface);
552
553 ALOGD("Starting GScan, halHandle = %p", handle);
554
555 ScanCommand *cmd = new ScanCommand(iface, id, &params, handler);
556 wifi_register_cmd(handle, id, cmd);
557 return (wifi_error)cmd->start();
558 }
559
560 wifi_error wifi_stop_gscan(wifi_request_id id, wifi_interface_handle iface)
561 {
562 ALOGD("Stopping GScan");
563 wifi_handle handle = getWifiHandle(iface);
564
565 if(id == -1) {
566 wifi_scan_result_handler handler;
567 wifi_scan_cmd_params dummy_params;
568 wifi_handle handle = getWifiHandle(iface);
569 memset(&handler, 0, sizeof(handler));
570
571 ScanCommand *cmd = new ScanCommand(iface, id, &dummy_params, handler);
572 cmd->cancel();
573 cmd->releaseRef();
574 return WIFI_SUCCESS;
575 }
576
577
578 WifiCommand *cmd = wifi_unregister_cmd(handle, id);
579 if (cmd) {
580 cmd->cancel();
581 cmd->releaseRef();
582 return WIFI_SUCCESS;
583 }
584
585 return WIFI_ERROR_INVALID_ARGS;
586 }
587
588 class GetScanResultsCommand : public WifiCommand {
589 wifi_cached_scan_results *mScans;
590 int mMax;
591 int *mNum;
592 int mRetrieved;
593 byte mFlush;
594 int mCompleted;
595 static const int MAX_RESULTS = 320;
596 wifi_scan_result mScanResults[MAX_RESULTS];
597 int mNextScanResult;
598 public:
599 GetScanResultsCommand(wifi_interface_handle iface, byte flush,
600 wifi_cached_scan_results *results, int max, int *num)
601 : WifiCommand(iface, -1), mScans(results), mMax(max), mNum(num),
602 mRetrieved(0), mFlush(flush), mCompleted(0)
603 { }
604
605 int createRequest(WifiRequest& request, int num, byte flush) {
606 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_SCAN_RESULTS);
607 if (result < 0) {
608 return result;
609 }
610
611 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
612 result = request.put_u32(GSCAN_ATTRIBUTE_NUM_OF_RESULTS, num);
613 if (result < 0) {
614 return result;
615 }
616
617 request.attr_end(data);
618 return WIFI_SUCCESS;
619 }
620
621 int execute() {
622 WifiRequest request(familyId(), ifaceId());
623 ALOGD("retrieving %d scan results", mMax);
624
625 for (int i = 0; i < 10 && mRetrieved < mMax; i++) {
626 int result = createRequest(request, (mMax - mRetrieved), mFlush);
627 if (result < 0) {
628 ALOGE("failed to create request");
629 return result;
630 }
631
632 int prev_retrieved = mRetrieved;
633
634 result = requestResponse(request);
635
636 if (result != WIFI_SUCCESS) {
637 ALOGE("failed to retrieve scan results; result = %d", result);
638 return result;
639 }
640
641 if (mRetrieved == prev_retrieved || mCompleted) {
642 /* no more items left to retrieve */
643 break;
644 }
645
646 request.destroy();
647 }
648
649 ALOGE("GetScanResults read %d results", mRetrieved);
650 *mNum = mRetrieved;
651 return WIFI_SUCCESS;
652 }
653
654 virtual int handleResponse(WifiEvent& reply) {
655 ALOGD("In GetScanResultsCommand::handleResponse");
656
657 if (reply.get_cmd() != NL80211_CMD_VENDOR) {
658 ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
659 return NL_SKIP;
660 }
661
662 int id = reply.get_vendor_id();
663 int subcmd = reply.get_vendor_subcmd();
664
665 ALOGD("Id = %0x, subcmd = %d", id, subcmd);
666
667 nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
668 int len = reply.get_vendor_data_len();
669
670 if (vendor_data == NULL || len == 0) {
671 ALOGE("no vendor data in GetScanResults response; ignoring it");
672 return NL_SKIP;
673 }
674
675 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
676 if (it.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE) {
677 mCompleted = it.get_u8();
678 ALOGD("retrieved mCompleted flag : %d", mCompleted);
679 } else if (it.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS || it.get_type() == 0) {
680 int scan_id = 0, flags = 0, num = 0;
681 for (nl_iterator it2(it.get()); it2.has_next(); it2.next()) {
682 if (it2.get_type() == GSCAN_ATTRIBUTE_SCAN_ID) {
683 scan_id = it2.get_u32();
684 ALOGD("retrieved scan_id : 0x%0x", scan_id);
685 } else if (it2.get_type() == GSCAN_ATTRIBUTE_SCAN_FLAGS) {
686 flags = it2.get_u8();
687 ALOGD("retrieved scan_flags : 0x%0x", flags);
688 } else if (it2.get_type() == GSCAN_ATTRIBUTE_NUM_OF_RESULTS) {
689 num = it2.get_u32();
690 ALOGD("retrieved num_results: %d", num);
691 } else if (it2.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS) {
692 if (mRetrieved >= mMax) {
693 ALOGW("Stored %d scans, ignoring excess results", mRetrieved);
694 break;
695 }
696 num = it2.get_len() / sizeof(wifi_scan_result);
697 num = min(MAX_RESULTS - mNextScanResult, num);
698 num = min((int)MAX_AP_CACHE_PER_SCAN, num);
699 memcpy(mScanResults + mNextScanResult, it2.get_data(),
700 sizeof(wifi_scan_result) * num);
701 ALOGD("Retrieved %d scan results", num);
702 wifi_scan_result *results = (wifi_scan_result *)it2.get_data();
703 for (int i = 0; i < num; i++) {
704 wifi_scan_result *result = results + i;
705 ALOGD("%02d %-32s %02x:%02x:%02x:%02x:%02x:%02x %04d", i,
706 result->ssid, result->bssid[0], result->bssid[1], result->bssid[2],
707 result->bssid[3], result->bssid[4], result->bssid[5],
708 result->rssi);
709 }
710 mScans[mRetrieved].scan_id = scan_id;
711 mScans[mRetrieved].flags = flags;
712 mScans[mRetrieved].num_results = num;
713 ALOGD("Setting result of scan_id : 0x%0x", mScans[mRetrieved].scan_id);
714 memcpy(mScans[mRetrieved].results,
715 &(mScanResults[mNextScanResult]), num * sizeof(wifi_scan_result));
716 mNextScanResult += num;
717 mRetrieved++;
718 } else {
719 ALOGW("Ignoring invalid attribute type = %d, size = %d",
720 it.get_type(), it.get_len());
721 }
722 }
723 } else {
724 ALOGW("Ignoring invalid attribute type = %d, size = %d",
725 it.get_type(), it.get_len());
726 }
727 }
728
729 return NL_OK;
730 }
731 };
732
733 wifi_error wifi_get_cached_gscan_results(wifi_interface_handle iface, byte flush,
734 int max, wifi_cached_scan_results *results, int *num) {
735 ALOGD("Getting cached scan results, iface handle = %p, num = %d", iface, *num);
736
737 GetScanResultsCommand *cmd = new GetScanResultsCommand(iface, flush, results, max, num);
738 return (wifi_error)cmd->execute();
739 }
740
741 /////////////////////////////////////////////////////////////////////////////
742
743 class BssidHotlistCommand : public WifiCommand
744 {
745 private:
746 wifi_bssid_hotlist_params mParams;
747 wifi_hotlist_ap_found_handler mHandler;
748 static const int MAX_RESULTS = 64;
749 wifi_scan_result mResults[MAX_RESULTS];
750 public:
751 BssidHotlistCommand(wifi_interface_handle handle, int id,
752 wifi_bssid_hotlist_params params, wifi_hotlist_ap_found_handler handler)
753 : WifiCommand(handle, id), mParams(params), mHandler(handler)
754 { }
755
756 int createSetupRequest(WifiRequest& request) {
757 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_BSSID_HOTLIST);
758 if (result < 0) {
759 return result;
760 }
761
762 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
763
764 result = request.put_u32(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE, mParams.lost_ap_sample_size);
765 if (result < 0) {
766 return result;
767 }
768
769 struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS);
770 for (int i = 0; i < mParams.num_bssid; i++) {
771 nlattr *attr2 = request.attr_start(GSCAN_ATTRIBUTE_HOTLIST_ELEM);
772 if (attr2 == NULL) {
773 return WIFI_ERROR_OUT_OF_MEMORY;
774 }
775 result = request.put_addr(GSCAN_ATTRIBUTE_BSSID, mParams.ap[i].bssid);
776 if (result < 0) {
777 return result;
778 }
779 result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH, mParams.ap[i].high);
780 if (result < 0) {
781 return result;
782 }
783 result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW, mParams.ap[i].low);
784 if (result < 0) {
785 return result;
786 }
787 request.attr_end(attr2);
788 }
789
790 request.attr_end(attr);
791 request.attr_end(data);
792 return result;
793 }
794
795 int createTeardownRequest(WifiRequest& request) {
796 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_RESET_BSSID_HOTLIST);
797 if (result < 0) {
798 return result;
799 }
800
801 return result;
802 }
803
804 int start() {
805 ALOGD("Executing hotlist setup request, num = %d", mParams.num_bssid);
806 WifiRequest request(familyId(), ifaceId());
807 int result = createSetupRequest(request);
808 if (result < 0) {
809 return result;
810 }
811
812 result = requestResponse(request);
813 if (result < 0) {
814 ALOGD("Failed to execute hotlist setup request, result = %d", result);
815 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);
816 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);
817 return result;
818 }
819
820 ALOGD("Successfully set %d APs in the hotlist", mParams.num_bssid);
821
822 registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);
823 registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);
824
825 return result;
826 }
827
828 virtual int cancel() {
829 /* unregister event handler */
830 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);
831 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);
832 /* create set hotlist message with empty hotlist */
833 WifiRequest request(familyId(), ifaceId());
834 int result = createTeardownRequest(request);
835 if (result < 0) {
836 return result;
837 }
838
839 result = requestResponse(request);
840 if (result < 0) {
841 return result;
842 }
843
844 ALOGD("Successfully reset APs in current hotlist");
845 return result;
846 }
847
848 virtual int handleResponse(WifiEvent& reply) {
849 /* Nothing to do on response! */
850 return NL_SKIP;
851 }
852
853 virtual int handleEvent(WifiEvent& event) {
854 ALOGD("Hotlist AP event");
855 int event_id = event.get_vendor_subcmd();
856 event.log();
857
858 nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
859 int len = event.get_vendor_data_len();
860
861 if (vendor_data == NULL || len == 0) {
862 ALOGD("No scan results found");
863 return NL_SKIP;
864 }
865
866 memset(mResults, 0, sizeof(wifi_scan_result) * MAX_RESULTS);
867
868 int num = len / sizeof(wifi_scan_result);
869 num = min(MAX_RESULTS, num);
870 memcpy(mResults, event.get_vendor_data(), num * sizeof(wifi_scan_result));
871
872 if (event_id == GSCAN_EVENT_HOTLIST_RESULTS_FOUND) {
873 ALOGD("FOUND %d hotlist APs", num);
874 if (*mHandler.on_hotlist_ap_found)
875 (*mHandler.on_hotlist_ap_found)(id(), num, mResults);
876 } else if (event_id == GSCAN_EVENT_HOTLIST_RESULTS_LOST) {
877 ALOGD("LOST %d hotlist APs", num);
878 if (*mHandler.on_hotlist_ap_lost)
879 (*mHandler.on_hotlist_ap_lost)(id(), num, mResults);
880 }
881 return NL_SKIP;
882 }
883 };
884
885 wifi_error wifi_set_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface,
886 wifi_bssid_hotlist_params params, wifi_hotlist_ap_found_handler handler)
887 {
888 wifi_handle handle = getWifiHandle(iface);
889
890 BssidHotlistCommand *cmd = new BssidHotlistCommand(iface, id, params, handler);
891 wifi_register_cmd(handle, id, cmd);
892 return (wifi_error)cmd->start();
893 }
894
895 wifi_error wifi_reset_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface)
896 {
897 wifi_handle handle = getWifiHandle(iface);
898
899 WifiCommand *cmd = wifi_unregister_cmd(handle, id);
900 if (cmd) {
901 cmd->cancel();
902 cmd->releaseRef();
903 return WIFI_SUCCESS;
904 }
905
906 return WIFI_ERROR_INVALID_ARGS;
907 }
908
909
910 /////////////////////////////////////////////////////////////////////////////
911
912 class SignificantWifiChangeCommand : public WifiCommand
913 {
914 typedef struct {
915 mac_addr bssid; // BSSID
916 wifi_channel channel; // channel frequency in MHz
917 int num_rssi; // number of rssi samples
918 wifi_rssi rssi[8]; // RSSI history in db
919 } wifi_significant_change_result_internal;
920
921 private:
922 wifi_significant_change_params mParams;
923 wifi_significant_change_handler mHandler;
924 static const int MAX_RESULTS = 64;
925 wifi_significant_change_result_internal mResultsBuffer[MAX_RESULTS];
926 wifi_significant_change_result *mResults[MAX_RESULTS];
927 public:
928 SignificantWifiChangeCommand(wifi_interface_handle handle, int id,
929 wifi_significant_change_params params, wifi_significant_change_handler handler)
930 : WifiCommand(handle, id), mParams(params), mHandler(handler)
931 { }
932
933 int createSetupRequest(WifiRequest& request) {
934 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_SIGNIFICANT_CHANGE);
935 if (result < 0) {
936 return result;
937 }
938
939 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
940
941 result = request.put_u16(GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE, mParams.rssi_sample_size);
942 if (result < 0) {
943 return result;
944 }
945 result = request.put_u16(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE, mParams.lost_ap_sample_size);
946 if (result < 0) {
947 return result;
948 }
949 result = request.put_u16(GSCAN_ATTRIBUTE_MIN_BREACHING, mParams.min_breaching);
950 if (result < 0) {
951 return result;
952 }
953
954 struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS);
955
956 for (int i = 0; i < mParams.num_bssid; i++) {
957
958 nlattr *attr2 = request.attr_start(i);
959 if (attr2 == NULL) {
960 return WIFI_ERROR_OUT_OF_MEMORY;
961 }
962 result = request.put_addr(GSCAN_ATTRIBUTE_BSSID, mParams.ap[i].bssid);
963 if (result < 0) {
964 return result;
965 }
966 result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH, mParams.ap[i].high);
967 if (result < 0) {
968 return result;
969 }
970 result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW, mParams.ap[i].low);
971 if (result < 0) {
972 return result;
973 }
974 request.attr_end(attr2);
975 }
976
977 request.attr_end(attr);
978 request.attr_end(data);
979
980 return result;
981 }
982
983 int createTeardownRequest(WifiRequest& request) {
984 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_RESET_SIGNIFICANT_CHANGE);
985 if (result < 0) {
986 return result;
987 }
988
989 return result;
990 }
991
992 int start() {
993 ALOGD("Set significant wifi change");
994 WifiRequest request(familyId(), ifaceId());
995
996 int result = createSetupRequest(request);
997 if (result < 0) {
998 return result;
999 }
1000
1001 result = requestResponse(request);
1002 if (result < 0) {
1003 ALOGD("failed to set significant wifi change %d", result);
1004 return result;
1005 }
1006 registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);
1007
1008 return result;
1009 }
1010
1011 virtual int cancel() {
1012 /* unregister event handler */
1013 unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);
1014
1015 /* create set significant change monitor message with empty hotlist */
1016 WifiRequest request(familyId(), ifaceId());
1017
1018 int result = createTeardownRequest(request);
1019 if (result < 0) {
1020 return result;
1021 }
1022
1023 result = requestResponse(request);
1024 if (result < 0) {
1025 return result;
1026 }
1027
1028 ALOGD("successfully reset significant wifi change");
1029 return result;
1030 }
1031
1032 virtual int handleResponse(WifiEvent& reply) {
1033 /* Nothing to do on response! */
1034 return NL_SKIP;
1035 }
1036
1037 virtual int handleEvent(WifiEvent& event) {
1038 ALOGD("Got a significant wifi change event");
1039
1040 nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1041 int len = event.get_vendor_data_len();
1042
1043 if (vendor_data == NULL || len == 0) {
1044 ALOGD("No scan results found");
1045 return NL_SKIP;
1046 }
1047
1048 typedef struct {
1049 uint16_t channel;
1050 mac_addr bssid;
1051 int16_t rssi_history[8];
1052 } ChangeInfo;
1053
1054 int num = min(len / sizeof(ChangeInfo), MAX_RESULTS);
1055 ChangeInfo *ci = (ChangeInfo *)event.get_vendor_data();
1056
1057 for (int i = 0; i < num; i++) {
1058 memcpy(mResultsBuffer[i].bssid, ci[i].bssid, sizeof(mac_addr));
1059 mResultsBuffer[i].channel = ci[i].channel;
1060 /* Driver sends N samples and the rest 8-N are filled 0x7FFF
1061 * N = no of rssi samples to average sent in significant change request. */
1062 int num_rssi = 0;
1063 for (int j = 0; j < 8; j++) {
1064 if (ci[i].rssi_history[j] == 0x7FFF) {
1065 num_rssi = j;
1066 break;
1067 }
1068 mResultsBuffer[i].rssi[j] = (int) ci[i].rssi_history[j];
1069 }
1070 mResultsBuffer[i].num_rssi = num_rssi;
1071 mResults[i] = reinterpret_cast<wifi_significant_change_result *>(&(mResultsBuffer[i]));
1072 }
1073
1074 ALOGD("Retrieved %d scan results", num);
1075
1076 if (num != 0) {
1077 (*mHandler.on_significant_change)(id(), num, mResults);
1078 } else {
1079 ALOGW("No significant change reported");
1080 }
1081
1082 return NL_SKIP;
1083 }
1084 };
1085
1086 wifi_error wifi_set_significant_change_handler(wifi_request_id id, wifi_interface_handle iface,
1087 wifi_significant_change_params params, wifi_significant_change_handler handler)
1088 {
1089 wifi_handle handle = getWifiHandle(iface);
1090
1091 SignificantWifiChangeCommand *cmd = new SignificantWifiChangeCommand(
1092 iface, id, params, handler);
1093 wifi_register_cmd(handle, id, cmd);
1094 return (wifi_error)cmd->start();
1095 }
1096
1097 wifi_error wifi_reset_significant_change_handler(wifi_request_id id, wifi_interface_handle iface)
1098 {
1099 wifi_handle handle = getWifiHandle(iface);
1100
1101 WifiCommand *cmd = wifi_unregister_cmd(handle, id);
1102 if (cmd) {
1103 cmd->cancel();
1104 cmd->releaseRef();
1105 return WIFI_SUCCESS;
1106 }
1107
1108 return WIFI_ERROR_INVALID_ARGS;
1109 }
1110
1111 class ePNOCommand : public WifiCommand
1112 {
1113 private:
1114 wifi_epno_network *ssid_list;
1115 int num_ssid;
1116 wifi_epno_handler mHandler;
1117 wifi_scan_result mResults;
1118 public:
1119 ePNOCommand(wifi_interface_handle handle, int id,
1120 int num_networks, wifi_epno_network *networks, wifi_epno_handler handler)
1121 : WifiCommand(handle, id), mHandler(handler)
1122 {
1123 ssid_list = networks;
1124 num_ssid = num_networks;
1125 }
1126
1127 int createSetupRequest(WifiRequest& request) {
1128 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_EPNO_LIST);
1129 if (result < 0) {
1130 return result;
1131 }
1132
1133 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1134
1135 result = request.put_u8(EPNO_ATTRIBUTE_SSID_NUM, num_ssid);
1136 if (result < 0) {
1137 return result;
1138 }
1139
1140 struct nlattr * attr = request.attr_start(EPNO_ATTRIBUTE_SSID_LIST);
1141 for (int i = 0; i < num_ssid; i++) {
1142 nlattr *attr2 = request.attr_start(i);
1143 if (attr2 == NULL) {
1144 return WIFI_ERROR_OUT_OF_MEMORY;
1145 }
1146 result = request.put(EPNO_ATTRIBUTE_SSID, ssid_list[i].ssid, 32);
1147 ALOGI("ePNO [SSID:%s rssi_thresh:%d flags:%d auth:%d]", ssid_list[i].ssid,
1148 (signed char)ssid_list[i].rssi_threshold, ssid_list[i].flags,
1149 ssid_list[i].auth_bit_field);
1150 if (result < 0) {
1151 return result;
1152 }
1153 result = request.put_u8(EPNO_ATTRIBUTE_SSID_LEN, strlen(ssid_list[i].ssid));
1154 if (result < 0) {
1155 return result;
1156 }
1157
1158 result = request.put_u8(EPNO_ATTRIBUTE_RSSI, ssid_list[i].rssi_threshold);
1159 if (result < 0) {
1160 return result;
1161 }
1162 result = request.put_u8(EPNO_ATTRIBUTE_FLAGS, ssid_list[i].flags);
1163 if (result < 0) {
1164 return result;
1165 }
1166 result = request.put_u8(EPNO_ATTRIBUTE_AUTH, ssid_list[i].auth_bit_field);
1167 if (result < 0) {
1168 return result;
1169 }
1170 request.attr_end(attr2);
1171 }
1172
1173 request.attr_end(attr);
1174 request.attr_end(data);
1175 return result;
1176 }
1177
1178 int start() {
1179 ALOGI("ePNO num_network=%d", num_ssid);
1180 WifiRequest request(familyId(), ifaceId());
1181 int result = createSetupRequest(request);
1182 if (result < 0) {
1183 return result;
1184 }
1185
1186 result = requestResponse(request);
1187 if (result < 0) {
1188 ALOGI("Failed: ePNO setup request, result = %d", result);
1189 unregisterVendorHandler(GOOGLE_OUI, WIFI_EPNO_EVENT);
1190 return result;
1191 }
1192
1193 ALOGI("Successfully set %d SSIDs for ePNO", num_ssid);
1194 registerVendorHandler(GOOGLE_OUI, WIFI_EPNO_EVENT);
1195 return result;
1196 }
1197
1198 virtual int cancel() {
1199 /* unregister event handler */
1200 unregisterVendorHandler(GOOGLE_OUI, WIFI_EPNO_EVENT);
1201 return 0;
1202 }
1203
1204 virtual int handleResponse(WifiEvent& reply) {
1205 /* Nothing to do on response! */
1206 return NL_SKIP;
1207 }
1208
1209 virtual int handleEvent(WifiEvent& event) {
1210 ALOGI("ePNO event");
1211 int event_id = event.get_vendor_subcmd();
1212 // event.log();
1213
1214 nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1215 int len = event.get_vendor_data_len();
1216
1217 if (vendor_data == NULL || len == 0) {
1218 ALOGI("No scan results found");
1219 return NL_SKIP;
1220 }
1221
1222 mResults = *(wifi_scan_result *) event.get_vendor_data();
1223 if (*mHandler.on_network_found)
1224 (*mHandler.on_network_found)(id(), 1, &mResults);
1225 return NL_SKIP;
1226 }
1227 };
1228
1229 wifi_error wifi_set_epno_list(wifi_request_id id,
1230 wifi_interface_handle iface,
1231 int num_networks,
1232 wifi_epno_network * networks,
1233 wifi_epno_handler handler)
1234 {
1235 wifi_handle handle = getWifiHandle(iface);
1236
1237 ePNOCommand *cmd = new ePNOCommand(iface, id, num_networks, networks, handler);
1238 wifi_register_cmd(handle, id, cmd);
1239 wifi_error result = (wifi_error)cmd->start();
1240 if (result != WIFI_SUCCESS) {
1241 wifi_unregister_cmd(handle, id);
1242 }
1243 return result;
1244 }
1245
1246 class HsListCommand : public WifiCommand
1247 {
1248 int num_hs;
1249 wifi_passpoint_network *mNetworks;
1250 wifi_passpoint_event_handler mHandler;
1251 public:
1252 HsListCommand(wifi_request_id id, wifi_interface_handle iface,
1253 int num, wifi_passpoint_network *hs_list, wifi_passpoint_event_handler handler)
1254 : WifiCommand(iface, id), num_hs(num), mNetworks(hs_list),
1255 mHandler(handler)
1256 {
1257 }
1258
1259 HsListCommand(wifi_request_id id, wifi_interface_handle iface,
1260 int num)
1261 : WifiCommand(iface, id), num_hs(num), mNetworks(NULL)
1262 {
1263 }
1264
1265 int createRequest(WifiRequest& request, int val) {
1266 int result;
1267
1268 if (val) {
1269 result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_HS_LIST);
1270 result = request.put_u32(EPNO_ATTRIBUTE_HS_NUM, num_hs);
1271 if (result < 0) {
1272 return result;
1273 }
1274 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1275
1276 struct nlattr * attr = request.attr_start(EPNO_ATTRIBUTE_HS_PARAM_LIST);
1277 for (int i = 0; i < num_hs; i++) {
1278 nlattr *attr2 = request.attr_start(i);
1279 if (attr2 == NULL) {
1280 return WIFI_ERROR_OUT_OF_MEMORY;
1281 }
1282 result = request.put_u32(EPNO_ATTRIBUTE_HS_ID, mNetworks[i].id);
1283 if (result < 0) {
1284 return result;
1285 }
1286 result = request.put(EPNO_ATTRIBUTE_HS_REALM, mNetworks[i].realm, 256);
1287 if (result < 0) {
1288 return result;
1289 }
1290 result = request.put(EPNO_ATTRIBUTE_HS_CONSORTIUM_IDS, mNetworks[i].roamingConsortiumIds, 128);
1291 if (result < 0) {
1292 return result;
1293 }
1294 result = request.put(EPNO_ATTRIBUTE_HS_PLMN, mNetworks[i].plmn, 3);
1295 if (result < 0) {
1296 return result;
1297 }
1298 request.attr_end(attr2);
1299 }
1300 request.attr_end(attr);
1301 request.attr_end(data);
1302 }else {
1303 result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_RESET_HS_LIST);
1304 if (result < 0) {
1305 return result;
1306 }
1307 }
1308
1309 return WIFI_SUCCESS;
1310 }
1311
1312 int start() {
1313
1314 WifiRequest request(familyId(), ifaceId());
1315 int result = createRequest(request, num_hs);
1316 if (result != WIFI_SUCCESS) {
1317 ALOGE("failed to create request; result = %d", result);
1318 return result;
1319 }
1320
1321 registerVendorHandler(GOOGLE_OUI, WIFI_HOTSPOT_MATCH);
1322
1323 result = requestResponse(request);
1324 if (result != WIFI_SUCCESS) {
1325 ALOGE("failed to set ANQPO networks; result = %d", result);
1326 unregisterVendorHandler(GOOGLE_OUI, WIFI_HOTSPOT_MATCH);
1327 return result;
1328 }
1329
1330 return result;
1331 }
1332
1333 virtual int cancel() {
1334
1335 WifiRequest request(familyId(), ifaceId());
1336 int result = createRequest(request, 0);
1337 if (result != WIFI_SUCCESS) {
1338 ALOGE("failed to create request; result = %d", result);
1339 } else {
1340 result = requestResponse(request);
1341 if (result != WIFI_SUCCESS) {
1342 ALOGE("failed to reset ANQPO networks;result = %d", result);
1343 }
1344 }
1345
1346 unregisterVendorHandler(GOOGLE_OUI, WIFI_HOTSPOT_MATCH);
1347 return WIFI_SUCCESS;
1348 }
1349
1350 virtual int handleResponse(WifiEvent& reply) {
1351 ALOGD("Request complete!");
1352 /* Nothing to do on response! */
1353 return NL_SKIP;
1354 }
1355
1356 virtual int handleEvent(WifiEvent& event) {
1357
1358 ALOGI("hotspot matched event");
1359 nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1360 unsigned int len = event.get_vendor_data_len();
1361 if (vendor_data == NULL || len < sizeof(wifi_scan_result)) {
1362 ALOGE("ERROR: No scan results found");
1363 return NL_SKIP;
1364 }
1365
1366 wifi_scan_result *result = (wifi_scan_result *)event.get_vendor_data();
1367 byte *anqp = (byte *)result + offsetof(wifi_scan_result, ie_data) + result->ie_length;
1368 int networkId = *(int *)anqp;
1369 anqp += sizeof(int);
1370 int anqp_len = *(u16 *)anqp;
1371 anqp += sizeof(u16);
1372
1373 ALOGI("%-32s\t", result->ssid);
1374
1375 ALOGI("%02x:%02x:%02x:%02x:%02x:%02x ", result->bssid[0], result->bssid[1],
1376 result->bssid[2], result->bssid[3], result->bssid[4], result->bssid[5]);
1377
1378 ALOGI("%d\t", result->rssi);
1379 ALOGI("%d\t", result->channel);
1380 ALOGI("%lld\t", result->ts);
1381 ALOGI("%lld\t", result->rtt);
1382 ALOGI("%lld\n", result->rtt_sd);
1383
1384 if(*mHandler.on_passpoint_network_found)
1385 (*mHandler.on_passpoint_network_found)(id(), networkId, result, anqp_len, anqp);
1386
1387 return NL_SKIP;
1388 }
1389 };
1390
1391 wifi_error wifi_set_passpoint_list(wifi_request_id id, wifi_interface_handle iface, int num,
1392 wifi_passpoint_network *networks, wifi_passpoint_event_handler handler)
1393 {
1394 wifi_handle handle = getWifiHandle(iface);
1395 HsListCommand *cmd = new HsListCommand(id, iface, num, networks, handler);
1396
1397 wifi_register_cmd(handle, id, cmd);
1398 wifi_error result = (wifi_error)cmd->start();
1399 if (result != WIFI_SUCCESS) {
1400 wifi_unregister_cmd(handle, id);
1401 }
1402 return result;
1403 }
1404
1405 wifi_error wifi_reset_passpoint_list(wifi_request_id id, wifi_interface_handle iface)
1406 {
1407 wifi_handle handle = getWifiHandle(iface);
1408 wifi_error result;
1409 HsListCommand *cmd = (HsListCommand *)(wifi_get_cmd(handle, id));
1410
1411 if (cmd == NULL) {
1412 cmd = new HsListCommand(id, iface, 0);
1413 wifi_register_cmd(handle, id, cmd);
1414 }
1415 result = (wifi_error)cmd->cancel();
1416 wifi_unregister_cmd(handle, id);
1417 return result;
1418 }
1419 class BssidBlacklistCommand : public WifiCommand
1420 {
1421 private:
1422 wifi_bssid_params *mParams;
1423 public:
1424 BssidBlacklistCommand(wifi_interface_handle handle, int id,
1425 wifi_bssid_params *params)
1426 : WifiCommand(handle, id), mParams(params)
1427 { }
1428 int createRequest(WifiRequest& request) {
1429 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_BSSID_BLACKLIST);
1430 if (result < 0) {
1431 return result;
1432 }
1433
1434 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1435 result = request.put_u32(GSCAN_ATTRIBUTE_NUM_BSSID, mParams->num_bssid);
1436 if (result < 0) {
1437 return result;
1438 }
1439
1440 for (int i = 0; i < mParams->num_bssid; i++) {
1441 result = request.put_addr(GSCAN_ATTRIBUTE_BLACKLIST_BSSID, mParams->bssids[i]);
1442 if (result < 0) {
1443 return result;
1444 }
1445 }
1446 request.attr_end(data);
1447 return result;
1448 }
1449
1450 int start() {
1451 ALOGD("Executing bssid blacklist request, num = %d", mParams->num_bssid);
1452 WifiRequest request(familyId(), ifaceId());
1453 int result = createRequest(request);
1454 if (result < 0) {
1455 return result;
1456 }
1457
1458 result = requestResponse(request);
1459 if (result < 0) {
1460 ALOGE("Failed to execute bssid blacklist request, result = %d", result);
1461 return result;
1462 }
1463
1464 ALOGI("Successfully added %d blacklist bssids", mParams->num_bssid);
1465 return result;
1466 }
1467
1468
1469 virtual int handleResponse(WifiEvent& reply) {
1470 /* Nothing to do on response! */
1471 return NL_SKIP;
1472 }
1473 };
1474
1475 wifi_error wifi_set_bssid_blacklist(wifi_request_id id, wifi_interface_handle iface,
1476 wifi_bssid_params params)
1477 {
1478 wifi_handle handle = getWifiHandle(iface);
1479
1480 BssidBlacklistCommand *cmd = new BssidBlacklistCommand(iface, id, &params);
1481 wifi_error result = (wifi_error)cmd->start();
1482 //release the reference of command as well
1483 cmd->releaseRef();
1484 return result;
1485 }