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>
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>
22 #include <utils/Log.h>
26 #include "cpp_bindings.h"
29 EPNO_ATTRIBUTE_MINIMUM_5G_RSSI
,
30 EPNO_ATTRIBUTE_MINIMUM_2G_RSSI
,
31 EPNO_ATTRIBUTE_INITIAL_SCORE_MAX
,
32 EPNO_ATTRIBUTE_CUR_CONN_BONUS
,
33 EPNO_ATTRIBUTE_SAME_NETWORK_BONUS
,
34 EPNO_ATTRIBUTE_SECURE_BONUS
,
35 EPNO_ATTRIBUTE_5G_BONUS
,
36 EPNO_ATTRIBUTE_SSID_NUM
,
37 EPNO_ATTRIBUTE_SSID_LIST
,
39 EPNO_ATTRIBUTE_SSID_LEN
,
46 EPNO_ATTRIBUTE_HS_PARAM_LIST
,
47 EPNO_ATTRIBUTE_HS_NUM
,
49 EPNO_ATTRIBUTE_HS_REALM
,
50 EPNO_ATTRIBUTE_HS_CONSORTIUM_IDS
,
51 EPNO_ATTRIBUTE_HS_PLMN
,
56 class GetCapabilitiesCommand
: public WifiCommand
58 wifi_gscan_capabilities
*mCapabilities
;
60 GetCapabilitiesCommand(wifi_interface_handle iface
, wifi_gscan_capabilities
*capabitlites
)
61 : WifiCommand(iface
, 0), mCapabilities(capabitlites
)
63 memset(mCapabilities
, 0, sizeof(*mCapabilities
));
66 virtual int create() {
67 int ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_CAPABILITIES
);
69 ALOGE("NL message creation failed");
77 virtual int handleResponse(WifiEvent
& reply
) {
79 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
80 ALOGE("Ignoring reply with cmd = %d", reply
.get_cmd());
84 int id
= reply
.get_vendor_id();
85 int subcmd
= reply
.get_vendor_subcmd();
87 void *data
= reply
.get_vendor_data();
88 int len
= reply
.get_vendor_data_len();
90 memcpy(mCapabilities
, data
, min(len
, (int) sizeof(*mCapabilities
)));
97 wifi_error
wifi_get_gscan_capabilities(wifi_interface_handle handle
,
98 wifi_gscan_capabilities
*capabilities
)
100 GetCapabilitiesCommand
command(handle
, capabilities
);
101 return (wifi_error
) command
.requestResponse();
104 class GetChannelListCommand
: public WifiCommand
106 wifi_channel
*channels
;
111 GetChannelListCommand(wifi_interface_handle iface
, wifi_channel
*channel_buf
, int *ch_num
,
112 int num_max_ch
, int band
)
113 : WifiCommand(iface
, 0), channels(channel_buf
), max_channels(num_max_ch
), num_channels(ch_num
),
116 memset(channels
, 0, sizeof(wifi_channel
) * max_channels
);
118 virtual int create() {
119 int ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_VALID_CHANNELS
);
124 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
125 ret
= mMsg
.put_u32(GSCAN_ATTRIBUTE_BAND
, band
);
136 virtual int handleResponse(WifiEvent
& reply
) {
138 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
139 ALOGE("Ignoring reply with cmd = %d", reply
.get_cmd());
143 int id
= reply
.get_vendor_id();
144 int subcmd
= reply
.get_vendor_subcmd();
145 int num_channels_to_copy
= 0;
147 nlattr
*vendor_data
= reply
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
148 int len
= reply
.get_vendor_data_len();
150 if (vendor_data
== NULL
|| len
== 0) {
151 ALOGE("no vendor data in GetChannelList response; ignoring it");
155 for (nl_iterator
it(vendor_data
); it
.has_next(); it
.next()) {
156 if (it
.get_type() == GSCAN_ATTRIBUTE_NUM_CHANNELS
) {
157 num_channels_to_copy
= it
.get_u32();
158 /*ALOGD("Got channel list with %d channels", num_channels_to_copy);*/
159 if(num_channels_to_copy
> max_channels
)
160 num_channels_to_copy
= max_channels
;
161 *num_channels
= num_channels_to_copy
;
162 } else if (it
.get_type() == GSCAN_ATTRIBUTE_CHANNEL_LIST
&& num_channels_to_copy
) {
163 memcpy(channels
, it
.get_data(), sizeof(int) * num_channels_to_copy
);
165 ALOGW("Ignoring invalid attribute type = %d, size = %d",
166 it
.get_type(), it
.get_len());
174 wifi_error
wifi_get_valid_channels(wifi_interface_handle handle
,
175 int band
, int max_channels
, wifi_channel
*channels
, int *num_channels
)
177 GetChannelListCommand
command(handle
, channels
, num_channels
,
179 return (wifi_error
) command
.requestResponse();
181 /////////////////////////////////////////////////////////////////////////////
183 /* helper functions */
185 static int parseScanResults(wifi_scan_result
*results
, int num
, nlattr
*attr
)
187 memset(results
, 0, sizeof(wifi_scan_result
) * num
);
190 for (nl_iterator
it(attr
); it
.has_next() && i
< num
; it
.next(), i
++) {
192 int index
= it
.get_type();
193 nlattr
*sc_data
= (nlattr
*) it
.get_data();
194 wifi_scan_result
*result
= results
+ i
;
196 for (nl_iterator
it2(sc_data
); it2
.has_next(); it2
.next()) {
197 int type
= it2
.get_type();
198 if (type
== GSCAN_ATTRIBUTE_SSID
) {
199 strncpy(result
->ssid
, (char *) it2
.get_data(), it2
.get_len());
200 result
->ssid
[it2
.get_len()] = 0;
201 } else if (type
== GSCAN_ATTRIBUTE_BSSID
) {
202 memcpy(result
->bssid
, (byte
*) it2
.get_data(), sizeof(mac_addr
));
203 } else if (type
== GSCAN_ATTRIBUTE_TIMESTAMP
) {
204 result
->ts
= it2
.get_u64();
205 } else if (type
== GSCAN_ATTRIBUTE_CHANNEL
) {
206 result
->ts
= it2
.get_u16();
207 } else if (type
== GSCAN_ATTRIBUTE_RSSI
) {
208 result
->rssi
= it2
.get_u8();
209 } else if (type
== GSCAN_ATTRIBUTE_RTT
) {
210 result
->rtt
= it2
.get_u64();
211 } else if (type
== GSCAN_ATTRIBUTE_RTTSD
) {
212 result
->rtt_sd
= it2
.get_u64();
219 ALOGE("Got too many results; skipping some");
225 int createFeatureRequest(WifiRequest
& request
, int subcmd
) {
227 int result
= request
.create(GOOGLE_OUI
, subcmd
);
235 class ScanCommand
: public WifiCommand
237 wifi_scan_cmd_params
*mParams
;
238 wifi_scan_result_handler mHandler
;
239 static unsigned mGlobalFullScanBuckets
;
240 bool mLocalFullScanBuckets
;
242 ScanCommand(wifi_interface_handle iface
, int id
, wifi_scan_cmd_params
*params
,
243 wifi_scan_result_handler handler
)
244 : WifiCommand(iface
, id
), mParams(params
), mHandler(handler
),
245 mLocalFullScanBuckets(0)
248 int createSetupRequest(WifiRequest
& request
) {
249 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN
);
254 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
255 result
= request
.put_u32(GSCAN_ATTRIBUTE_BASE_PERIOD
, mParams
->base_period
);
260 result
= request
.put_u32(GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN
, mParams
->max_ap_per_scan
);
265 result
= request
.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD
, mParams
->report_threshold_percent
);
270 result
= request
.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD_NUM_SCANS
, mParams
->report_threshold_num_scans
);
275 result
= request
.put_u32(GSCAN_ATTRIBUTE_NUM_BUCKETS
, mParams
->num_buckets
);
280 for (int i
= 0; i
< mParams
->num_buckets
; i
++) {
281 nlattr
* bucket
= request
.attr_start(i
); // next bucket
282 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_ID
, mParams
->buckets
[i
].bucket
);
286 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_PERIOD
, mParams
->buckets
[i
].period
);
290 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKETS_BAND
,
291 mParams
->buckets
[i
].band
);
296 result
= request
.put_u32(GSCAN_ATTRIBUTE_REPORT_EVENTS
,
297 mParams
->buckets
[i
].report_events
);
302 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS
,
303 mParams
->buckets
[i
].num_channels
);
308 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_EXPONENT
,
309 mParams
->buckets
[i
].base
);
314 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_MAX_PERIOD
,
315 mParams
->buckets
[i
].max_period
);
320 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_STEP_COUNT
,
321 mParams
->buckets
[i
].step_count
);
326 if (mParams
->buckets
[i
].num_channels
) {
327 nlattr
*channels
= request
.attr_start(GSCAN_ATTRIBUTE_BUCKET_CHANNELS
);
328 for (int j
= 0; j
< mParams
->buckets
[i
].num_channels
; j
++) {
329 result
= request
.put_u32(j
, mParams
->buckets
[i
].channels
[j
].channel
);
334 request
.attr_end(channels
);
337 request
.attr_end(bucket
);
340 request
.attr_end(data
);
344 int createStartRequest(WifiRequest
& request
) {
345 return createFeatureRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN
);
348 int createStopRequest(WifiRequest
& request
) {
349 return createFeatureRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_DEL_GSCAN
);
353 ALOGD("starting Gscan");
354 WifiRequest
request(familyId(), ifaceId());
355 int result
= createSetupRequest(request
);
356 if (result
!= WIFI_SUCCESS
) {
357 ALOGE("failed to create setup request; result = %d", result
);
361 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE
);
362 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_COMPLETE_SCAN
);
365 for (int i
= 0; i
< mParams
->num_buckets
; i
++) {
366 if (mParams
->buckets
[i
].report_events
== 2) {
372 ALOGI("Full scan requested with nBuckets = %d", nBuckets
);
373 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_FULL_SCAN_RESULTS
);
375 result
= requestResponse(request
);
376 if (result
!= WIFI_SUCCESS
) {
377 ALOGE("failed to start scan; result = %d", result
);
378 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_COMPLETE_SCAN
);
379 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE
);
387 virtual int cancel() {
388 ALOGD("Stopping Gscan");
390 WifiRequest
request(familyId(), ifaceId());
391 int result
= createStopRequest(request
);
392 if (result
!= WIFI_SUCCESS
) {
393 ALOGE("failed to create stop request; result = %d", result
);
395 result
= requestResponse(request
);
396 if (result
!= WIFI_SUCCESS
) {
397 ALOGE("failed to stop scan; result = %d", result
);
401 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_COMPLETE_SCAN
);
402 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE
);
403 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_FULL_SCAN_RESULTS
);
408 virtual int handleResponse(WifiEvent
& reply
) {
409 /* Nothing to do on response! */
413 virtual int handleEvent(WifiEvent
& event
) {
416 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
417 unsigned int len
= event
.get_vendor_data_len();
418 int event_id
= event
.get_vendor_subcmd();
420 if(event_id
== GSCAN_EVENT_COMPLETE_SCAN
) {
421 if (vendor_data
== NULL
|| len
!= 4) {
422 ALOGE("Scan complete type not mentioned!");
425 wifi_scan_event evt_type
;
427 evt_type
= (wifi_scan_event
) event
.get_u32(NL80211_ATTR_VENDOR_DATA
);
428 if(*mHandler
.on_scan_event
)
429 (*mHandler
.on_scan_event
)(evt_type
, evt_type
);
430 } else if(event_id
== GSCAN_EVENT_FULL_SCAN_RESULTS
) {
431 uint32_t bucket_scanned
= 0;
432 wifi_scan_result
*scan_result
= NULL
;
433 for (nl_iterator
it(vendor_data
); it
.has_next(); it
.next()) {
434 if (it
.get_type() == GSCAN_ATTRIBUTE_SCAN_BUCKET_BIT
) {
435 bucket_scanned
= it
.get_u32();
436 } else if (it
.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS
) {
437 if (it
.get_len() >= (int)sizeof(*scan_result
))
438 scan_result
= (wifi_scan_result
*)it
.get_data();
442 if(*mHandler
.on_full_scan_result
)
443 (*mHandler
.on_full_scan_result
)(id(), scan_result
, bucket_scanned
);
445 ALOGD("%-32s\t", scan_result->ssid);
446 ALOGD("%02x:%02x:%02x:%02x:%02x:%02x ", scan_result->bssid[0], scan_result->bssid[1],
447 scan_result->bssid[2], scan_result->bssid[3], scan_result->bssid[4], scan_result->bssid[5]);
448 ALOGD("%d\t", scan_result->rssi);
449 ALOGD("%d\t", scan_result->channel);
450 ALOGD("%lld\t", scan_result->ts);
451 ALOGD("%lld\t", scan_result->rtt);
452 ALOGD("%lld\n", scan_result->rtt_sd);
460 unsigned ScanCommand::mGlobalFullScanBuckets
= 0;
462 wifi_error
wifi_start_gscan(
464 wifi_interface_handle iface
,
465 wifi_scan_cmd_params params
,
466 wifi_scan_result_handler handler
)
468 wifi_handle handle
= getWifiHandle(iface
);
470 ScanCommand
*cmd
= new ScanCommand(iface
, id
, ¶ms
, handler
);
471 wifi_register_cmd(handle
, id
, cmd
);
472 return (wifi_error
)cmd
->start();
475 wifi_error
wifi_stop_gscan(wifi_request_id id
, wifi_interface_handle iface
)
477 wifi_handle handle
= getWifiHandle(iface
);
480 wifi_scan_result_handler handler
;
481 wifi_scan_cmd_params dummy_params
;
482 wifi_handle handle
= getWifiHandle(iface
);
483 memset(&handler
, 0, sizeof(handler
));
485 ScanCommand
*cmd
= new ScanCommand(iface
, id
, &dummy_params
, handler
);
492 WifiCommand
*cmd
= wifi_unregister_cmd(handle
, id
);
499 return WIFI_ERROR_INVALID_ARGS
;
502 class GetScanResultsCommand
: public WifiCommand
{
503 wifi_cached_scan_results
*mScans
;
509 static const int MAX_RESULTS
= 320;
510 wifi_scan_result mScanResults
[MAX_RESULTS
];
513 GetScanResultsCommand(wifi_interface_handle iface
, byte flush
,
514 wifi_cached_scan_results
*results
, int max
, int *num
)
515 : WifiCommand(iface
, -1), mScans(results
), mMax(max
), mNum(num
),
516 mRetrieved(0), mFlush(flush
), mCompleted(0)
518 memset(mScanResults
,0,sizeof(mScanResults
));
522 int createRequest(WifiRequest
& request
, int num
, byte flush
) {
523 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_SCAN_RESULTS
);
528 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
529 result
= request
.put_u32(GSCAN_ATTRIBUTE_NUM_OF_RESULTS
, num
);
534 request
.attr_end(data
);
539 WifiRequest
request(familyId(), ifaceId());
541 for (int i
= 0; i
< 10 && mRetrieved
< mMax
; i
++) {
542 int result
= createRequest(request
, (mMax
- mRetrieved
), mFlush
);
544 ALOGE("failed to create request");
548 int prev_retrieved
= mRetrieved
;
550 result
= requestResponse(request
);
552 if (result
!= WIFI_SUCCESS
) {
553 ALOGE("failed to retrieve scan results; result = %d", result
);
557 if (mRetrieved
== prev_retrieved
|| mCompleted
) {
558 /* no more items left to retrieve */
565 ALOGE("GetScanResults read %d results", mRetrieved
);
570 virtual int handleResponse(WifiEvent
& reply
) {
572 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
573 ALOGE("Ignoring reply with cmd = %d", reply
.get_cmd());
577 int id
= reply
.get_vendor_id();
578 int subcmd
= reply
.get_vendor_subcmd();
580 nlattr
*vendor_data
= reply
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
581 int len
= reply
.get_vendor_data_len();
583 if (vendor_data
== NULL
|| len
== 0) {
584 ALOGE("no vendor data in GetScanResults response; ignoring it");
588 for (nl_iterator
it(vendor_data
); it
.has_next(); it
.next()) {
589 if (it
.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE
) {
590 mCompleted
= it
.get_u8();
591 //ALOGD("retrieved mCompleted flag : %d", mCompleted);
592 } else if (it
.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS
|| it
.get_type() == 0) {
593 int scan_id
= 0, flags
= 0, num
= 0;
594 for (nl_iterator
it2(it
.get()); it2
.has_next(); it2
.next()) {
595 if (it2
.get_type() == GSCAN_ATTRIBUTE_SCAN_ID
) {
596 scan_id
= it2
.get_u32();
597 //ALOGD("retrieved scan_id : 0x%0x", scan_id);
598 } else if (it2
.get_type() == GSCAN_ATTRIBUTE_SCAN_FLAGS
) {
599 flags
= it2
.get_u8();
600 //ALOGD("retrieved scan_flags : 0x%0x", flags);
601 } else if (it2
.get_type() == GSCAN_ATTRIBUTE_NUM_OF_RESULTS
) {
603 //ALOGD("retrieved num_results: %d", num);
604 } else if (it2
.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS
) {
605 if (mRetrieved
>= mMax
) {
606 ALOGW("Stored %d scans, ignoring excess results", mRetrieved
);
609 num
= it2
.get_len() / sizeof(wifi_scan_result
);
610 num
= min(MAX_RESULTS
- mNextScanResult
, num
);
611 num
= min((int)MAX_AP_CACHE_PER_SCAN
, num
);
612 memcpy(mScanResults
+ mNextScanResult
, it2
.get_data(),
613 sizeof(wifi_scan_result
) * num
);
614 wifi_scan_result
*results
= (wifi_scan_result
*)it2
.get_data();
616 for (int i = 0; i < num; i++) {
617 wifi_scan_result *result = results + i;
618 ALOGD("%02d %-32s %02x:%02x:%02x:%02x:%02x:%02x %04d", i,
619 result->ssid, result->bssid[0], result->bssid[1], result->bssid[2],
620 result->bssid[3], result->bssid[4], result->bssid[5],
623 mScans
[mRetrieved
].scan_id
= scan_id
;
624 mScans
[mRetrieved
].flags
= flags
;
625 mScans
[mRetrieved
].num_results
= num
;
626 //ALOGD("Setting result of scan_id : 0x%0x", mScans[mRetrieved].scan_id);
627 memcpy(mScans
[mRetrieved
].results
,
628 &(mScanResults
[mNextScanResult
]), num
* sizeof(wifi_scan_result
));
629 mNextScanResult
+= num
;
632 ALOGW("Ignoring invalid attribute type = %d, size = %d",
633 it
.get_type(), it
.get_len());
637 ALOGW("Ignoring invalid attribute type = %d, size = %d",
638 it
.get_type(), it
.get_len());
646 wifi_error
wifi_get_cached_gscan_results(wifi_interface_handle iface
, byte flush
,
647 int max
, wifi_cached_scan_results
*results
, int *num
) {
648 GetScanResultsCommand
*cmd
= new GetScanResultsCommand(iface
, flush
, results
, max
, num
);
649 return (wifi_error
)cmd
->execute();
652 /////////////////////////////////////////////////////////////////////////////
654 class BssidHotlistCommand
: public WifiCommand
657 wifi_bssid_hotlist_params mParams
;
658 wifi_hotlist_ap_found_handler mHandler
;
659 static const int MAX_RESULTS
= 64;
660 wifi_scan_result mResults
[MAX_RESULTS
];
662 BssidHotlistCommand(wifi_interface_handle handle
, int id
,
663 wifi_bssid_hotlist_params params
, wifi_hotlist_ap_found_handler handler
)
664 : WifiCommand(handle
, id
), mParams(params
), mHandler(handler
)
666 memset(mResults
, 0, sizeof(mResults
));
669 int createSetupRequest(WifiRequest
& request
) {
670 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_BSSID_HOTLIST
);
675 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
677 result
= request
.put_u32(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE
, mParams
.lost_ap_sample_size
);
682 struct nlattr
* attr
= request
.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS
);
683 for (int i
= 0; i
< mParams
.num_bssid
; i
++) {
684 nlattr
*attr2
= request
.attr_start(GSCAN_ATTRIBUTE_HOTLIST_ELEM
);
686 return WIFI_ERROR_OUT_OF_MEMORY
;
688 result
= request
.put_addr(GSCAN_ATTRIBUTE_BSSID
, mParams
.ap
[i
].bssid
);
692 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH
, mParams
.ap
[i
].high
);
696 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW
, mParams
.ap
[i
].low
);
700 request
.attr_end(attr2
);
703 request
.attr_end(attr
);
704 request
.attr_end(data
);
708 int createTeardownRequest(WifiRequest
& request
) {
709 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_RESET_BSSID_HOTLIST
);
718 WifiRequest
request(familyId(), ifaceId());
719 int result
= createSetupRequest(request
);
724 result
= requestResponse(request
);
726 ALOGE("Failed to execute hotlist setup request, result = %d", result
);
727 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_FOUND
);
728 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_LOST
);
732 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_FOUND
);
733 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_LOST
);
738 virtual int cancel() {
739 /* unregister event handler */
740 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_FOUND
);
741 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_LOST
);
742 /* create set hotlist message with empty hotlist */
743 WifiRequest
request(familyId(), ifaceId());
744 int result
= createTeardownRequest(request
);
749 result
= requestResponse(request
);
757 virtual int handleResponse(WifiEvent
& reply
) {
758 /* Nothing to do on response! */
762 virtual int handleEvent(WifiEvent
& event
) {
763 int event_id
= event
.get_vendor_subcmd();
766 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
767 int len
= event
.get_vendor_data_len();
769 if (vendor_data
== NULL
|| len
== 0) {
770 ALOGE("No scan results found");
775 int num
= len
/ sizeof(wifi_scan_result
);
776 num
= min(MAX_RESULTS
, num
);
777 memcpy(mResults
, event
.get_vendor_data(), num
* sizeof(wifi_scan_result
));
779 if (event_id
== GSCAN_EVENT_HOTLIST_RESULTS_FOUND
) {
780 ALOGD("FOUND %d hotlist APs", num
);
781 if (*mHandler
.on_hotlist_ap_found
)
782 (*mHandler
.on_hotlist_ap_found
)(id(), num
, mResults
);
783 } else if (event_id
== GSCAN_EVENT_HOTLIST_RESULTS_LOST
) {
784 ALOGD("LOST %d hotlist APs", num
);
785 if (*mHandler
.on_hotlist_ap_lost
)
786 (*mHandler
.on_hotlist_ap_lost
)(id(), num
, mResults
);
792 wifi_error
wifi_set_bssid_hotlist(wifi_request_id id
, wifi_interface_handle iface
,
793 wifi_bssid_hotlist_params params
, wifi_hotlist_ap_found_handler handler
)
795 wifi_handle handle
= getWifiHandle(iface
);
797 BssidHotlistCommand
*cmd
= new BssidHotlistCommand(iface
, id
, params
, handler
);
798 wifi_register_cmd(handle
, id
, cmd
);
799 return (wifi_error
)cmd
->start();
802 wifi_error
wifi_reset_bssid_hotlist(wifi_request_id id
, wifi_interface_handle iface
)
804 wifi_handle handle
= getWifiHandle(iface
);
806 WifiCommand
*cmd
= wifi_unregister_cmd(handle
, id
);
813 return WIFI_ERROR_INVALID_ARGS
;
817 /////////////////////////////////////////////////////////////////////////////
819 class SignificantWifiChangeCommand
: public WifiCommand
822 mac_addr bssid
; // BSSID
823 wifi_channel channel
; // channel frequency in MHz
824 int num_rssi
; // number of rssi samples
825 wifi_rssi rssi
[8]; // RSSI history in db
826 } wifi_significant_change_result_internal
;
829 wifi_significant_change_params mParams
;
830 wifi_significant_change_handler mHandler
;
831 static const int MAX_RESULTS
= 64;
832 wifi_significant_change_result_internal mResultsBuffer
[MAX_RESULTS
];
833 wifi_significant_change_result
*mResults
[MAX_RESULTS
];
835 SignificantWifiChangeCommand(wifi_interface_handle handle
, int id
,
836 wifi_significant_change_params params
, wifi_significant_change_handler handler
)
837 : WifiCommand(handle
, id
), mParams(params
), mHandler(handler
)
839 memset(mResultsBuffer
,0,sizeof(mResultsBuffer
));
840 memset(mResults
,0,sizeof(mResults
));
843 int createSetupRequest(WifiRequest
& request
) {
844 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_SIGNIFICANT_CHANGE
);
849 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
851 result
= request
.put_u16(GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE
, mParams
.rssi_sample_size
);
855 result
= request
.put_u16(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE
, mParams
.lost_ap_sample_size
);
859 result
= request
.put_u16(GSCAN_ATTRIBUTE_MIN_BREACHING
, mParams
.min_breaching
);
864 struct nlattr
* attr
= request
.attr_start(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS
);
866 for (int i
= 0; i
< mParams
.num_bssid
; i
++) {
868 nlattr
*attr2
= request
.attr_start(i
);
870 return WIFI_ERROR_OUT_OF_MEMORY
;
872 result
= request
.put_addr(GSCAN_ATTRIBUTE_BSSID
, mParams
.ap
[i
].bssid
);
876 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH
, mParams
.ap
[i
].high
);
880 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW
, mParams
.ap
[i
].low
);
884 request
.attr_end(attr2
);
887 request
.attr_end(attr
);
888 request
.attr_end(data
);
893 int createTeardownRequest(WifiRequest
& request
) {
894 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_RESET_SIGNIFICANT_CHANGE
);
903 WifiRequest
request(familyId(), ifaceId());
905 int result
= createSetupRequest(request
);
910 result
= requestResponse(request
);
912 ALOGE("failed to set significant wifi change %d", result
);
915 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS
);
920 virtual int cancel() {
921 /* unregister event handler */
922 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS
);
924 /* create set significant change monitor message with empty hotlist */
925 WifiRequest
request(familyId(), ifaceId());
927 int result
= createTeardownRequest(request
);
932 result
= requestResponse(request
);
940 virtual int handleResponse(WifiEvent
& reply
) {
941 /* Nothing to do on response! */
945 virtual int handleEvent(WifiEvent
& event
) {
946 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
947 int len
= event
.get_vendor_data_len();
949 if (vendor_data
== NULL
|| len
== 0) {
950 ALOGE("No scan results found");
957 int16_t rssi_history
[8];
960 int num
= min(len
/ sizeof(ChangeInfo
), MAX_RESULTS
);
961 ChangeInfo
*ci
= (ChangeInfo
*)event
.get_vendor_data();
963 for (int i
= 0; i
< num
; i
++) {
964 memcpy(mResultsBuffer
[i
].bssid
, ci
[i
].bssid
, sizeof(mac_addr
));
965 mResultsBuffer
[i
].channel
= ci
[i
].channel
;
966 /* Driver sends N samples and the rest 8-N are filled 0x7FFF
967 * N = no of rssi samples to average sent in significant change request. */
969 for (int j
= 0; j
< 8; j
++) {
970 if (ci
[i
].rssi_history
[j
] == 0x7FFF) {
974 mResultsBuffer
[i
].rssi
[j
] = (int) ci
[i
].rssi_history
[j
];
976 mResultsBuffer
[i
].num_rssi
= num_rssi
;
977 mResults
[i
] = reinterpret_cast<wifi_significant_change_result
*>(&(mResultsBuffer
[i
]));
981 (*mHandler
.on_significant_change
)(id(), num
, mResults
);
983 ALOGW("No significant change reported");
990 wifi_error
wifi_set_significant_change_handler(wifi_request_id id
, wifi_interface_handle iface
,
991 wifi_significant_change_params params
, wifi_significant_change_handler handler
)
993 wifi_handle handle
= getWifiHandle(iface
);
995 SignificantWifiChangeCommand
*cmd
= new SignificantWifiChangeCommand(
996 iface
, id
, params
, handler
);
997 wifi_register_cmd(handle
, id
, cmd
);
998 return (wifi_error
)cmd
->start();
1001 wifi_error
wifi_reset_significant_change_handler(wifi_request_id id
, wifi_interface_handle iface
)
1003 wifi_handle handle
= getWifiHandle(iface
);
1005 WifiCommand
*cmd
= wifi_unregister_cmd(handle
, id
);
1009 return WIFI_SUCCESS
;
1012 return WIFI_ERROR_INVALID_ARGS
;
1015 class ePNOCommand
: public WifiCommand
1018 wifi_epno_params
*epno_params
;
1019 wifi_epno_handler mHandler
;
1020 wifi_scan_result mResults
;
1022 ePNOCommand(wifi_interface_handle handle
, int id
,
1023 wifi_epno_params
*params
, wifi_epno_handler handler
)
1024 : WifiCommand(handle
, id
), mHandler(handler
)
1026 epno_params
= params
;
1027 memset(&mResults
,0,sizeof(wifi_scan_result
));
1030 int createSetupRequest(WifiRequest
& request
) {
1031 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_EPNO_LIST
);
1036 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
1037 if (epno_params
== NULL
) {
1038 result
= request
.put_u8(EPNO_ATTRIBUTE_SSID_NUM
, 0);
1042 request
.attr_end(data
);
1045 result
= request
.put_u16(EPNO_ATTRIBUTE_MINIMUM_5G_RSSI
, epno_params
->min5GHz_rssi
);
1049 result
= request
.put_u16(EPNO_ATTRIBUTE_MINIMUM_2G_RSSI
, epno_params
->min24GHz_rssi
);
1053 result
= request
.put_u16(EPNO_ATTRIBUTE_INITIAL_SCORE_MAX
, epno_params
->initial_score_max
);
1057 result
= request
.put_u8(EPNO_ATTRIBUTE_CUR_CONN_BONUS
, epno_params
->current_connection_bonus
);
1061 result
= request
.put_u8(EPNO_ATTRIBUTE_SAME_NETWORK_BONUS
, epno_params
->same_network_bonus
);
1065 result
= request
.put_u8(EPNO_ATTRIBUTE_SECURE_BONUS
, epno_params
->secure_bonus
);
1069 result
= request
.put_u8(EPNO_ATTRIBUTE_5G_BONUS
, epno_params
->band5GHz_bonus
);
1073 result
= request
.put_u8(EPNO_ATTRIBUTE_SSID_NUM
, epno_params
->num_networks
);
1078 ALOGI("ePNO [min5GHz_rssi:%d min24GHz_rssi:%d initial_score_max:%d current_connection_bonus:%d same_network_bonus:%d secure_bonus:%d band5GHz_bonus:%d num_networks:%d]",
1079 epno_params
->min5GHz_rssi
,
1080 epno_params
->min24GHz_rssi
,
1081 epno_params
->initial_score_max
,
1082 epno_params
->current_connection_bonus
,
1083 epno_params
->same_network_bonus
,
1084 epno_params
->secure_bonus
,
1085 epno_params
->band5GHz_bonus
,
1086 epno_params
->num_networks
);
1088 struct nlattr
* attr
= request
.attr_start(EPNO_ATTRIBUTE_SSID_LIST
);
1089 for (int i
= 0; i
< epno_params
->num_networks
; i
++) {
1090 nlattr
*attr2
= request
.attr_start(i
);
1091 if (attr2
== NULL
) {
1092 return WIFI_ERROR_OUT_OF_MEMORY
;
1094 result
= request
.put_u16(EPNO_ATTRIBUTE_FLAGS
, epno_params
->networks
[i
].flags
);
1098 result
= request
.put_u8(EPNO_ATTRIBUTE_AUTH
, epno_params
->networks
[i
].auth_bit_field
);
1102 result
= request
.put_u8(EPNO_ATTRIBUTE_SSID_LEN
, strlen(epno_params
->networks
[i
].ssid
));
1106 result
= request
.put(EPNO_ATTRIBUTE_SSID
, epno_params
->networks
[i
].ssid
, strlen(epno_params
->networks
[i
].ssid
));
1110 request
.attr_end(attr2
);
1113 request
.attr_end(attr
);
1114 request
.attr_end(data
);
1119 ALOGI("ePNO num_network=%d", epno_params
? epno_params
->num_networks
: 0);
1120 WifiRequest
request(familyId(), ifaceId());
1121 int result
= createSetupRequest(request
);
1126 result
= requestResponse(request
);
1128 ALOGI("Failed: ePNO setup request, result = %d", result
);
1129 unregisterVendorHandler(GOOGLE_OUI
, WIFI_EPNO_EVENT
);
1134 registerVendorHandler(GOOGLE_OUI
, WIFI_EPNO_EVENT
);
1139 virtual int cancel() {
1140 /* unregister event handler */
1141 unregisterVendorHandler(GOOGLE_OUI
, WIFI_EPNO_EVENT
);
1145 virtual int handleResponse(WifiEvent
& reply
) {
1146 /* Nothing to do on response! */
1150 virtual int handleEvent(WifiEvent
& event
) {
1151 int event_id
= event
.get_vendor_subcmd();
1154 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
1155 int len
= event
.get_vendor_data_len();
1157 if (vendor_data
== NULL
|| len
== 0) {
1158 ALOGI("No scan results found");
1163 mResults
= *(wifi_scan_result
*) event
.get_vendor_data();
1164 if (*mHandler
.on_network_found
)
1165 (*mHandler
.on_network_found
)(id(), 1, &mResults
);
1170 wifi_error
wifi_set_epno_list(wifi_request_id id
,
1171 wifi_interface_handle iface
,
1172 const wifi_epno_params
*epno_params
,
1173 wifi_epno_handler handler
)
1175 wifi_handle handle
= getWifiHandle(iface
);
1176 ePNOCommand
*cmd
= new ePNOCommand(iface
, id
, (wifi_epno_params
*)epno_params
, handler
);
1177 wifi_register_cmd(handle
, id
, cmd
);
1178 wifi_error result
= (wifi_error
)cmd
->start();
1179 if (result
!= WIFI_SUCCESS
) {
1180 wifi_unregister_cmd(handle
, id
);
1185 wifi_error
wifi_reset_epno_list(wifi_request_id id
, wifi_interface_handle iface
)
1187 wifi_handle handle
= getWifiHandle(iface
);
1188 wifi_epno_handler handler
;
1190 handler
.on_network_found
= NULL
;
1191 ePNOCommand
*cmd
= new ePNOCommand(iface
, id
, NULL
, handler
);
1192 wifi_register_cmd(handle
, id
, cmd
);
1193 wifi_error result
= (wifi_error
)cmd
->start();
1194 if (result
!= WIFI_SUCCESS
) {
1195 wifi_unregister_cmd(handle
, id
);
1200 class HsListCommand
: public WifiCommand
1203 wifi_passpoint_network
*mNetworks
;
1204 wifi_passpoint_event_handler mHandler
;
1206 HsListCommand(wifi_request_id id
, wifi_interface_handle iface
,
1207 int num
, wifi_passpoint_network
*hs_list
, wifi_passpoint_event_handler handler
)
1208 : WifiCommand(iface
, id
), num_hs(num
), mNetworks(hs_list
),
1213 HsListCommand(wifi_request_id id
, wifi_interface_handle iface
,
1215 : WifiCommand(iface
, id
), num_hs(num
), mNetworks(NULL
)
1217 mHandler
.on_passpoint_network_found
= NULL
;
1220 int createRequest(WifiRequest
& request
, int val
) {
1224 result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_HS_LIST
);
1225 result
= request
.put_u32(EPNO_ATTRIBUTE_HS_NUM
, num_hs
);
1229 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
1231 struct nlattr
* attr
= request
.attr_start(EPNO_ATTRIBUTE_HS_PARAM_LIST
);
1232 for (int i
= 0; i
< num_hs
; i
++) {
1233 nlattr
*attr2
= request
.attr_start(i
);
1234 if (attr2
== NULL
) {
1235 return WIFI_ERROR_OUT_OF_MEMORY
;
1237 result
= request
.put_u32(EPNO_ATTRIBUTE_HS_ID
, mNetworks
[i
].id
);
1241 result
= request
.put(EPNO_ATTRIBUTE_HS_REALM
, mNetworks
[i
].realm
, 256);
1245 result
= request
.put(EPNO_ATTRIBUTE_HS_CONSORTIUM_IDS
, mNetworks
[i
].roamingConsortiumIds
, 128);
1249 result
= request
.put(EPNO_ATTRIBUTE_HS_PLMN
, mNetworks
[i
].plmn
, 3);
1253 request
.attr_end(attr2
);
1255 request
.attr_end(attr
);
1256 request
.attr_end(data
);
1258 result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_RESET_HS_LIST
);
1264 return WIFI_SUCCESS
;
1269 WifiRequest
request(familyId(), ifaceId());
1270 int result
= createRequest(request
, num_hs
);
1271 if (result
!= WIFI_SUCCESS
) {
1272 ALOGE("failed to create request; result = %d", result
);
1276 registerVendorHandler(GOOGLE_OUI
, WIFI_HOTSPOT_MATCH
);
1278 result
= requestResponse(request
);
1279 if (result
!= WIFI_SUCCESS
) {
1280 ALOGE("failed to set ANQPO networks; result = %d", result
);
1281 unregisterVendorHandler(GOOGLE_OUI
, WIFI_HOTSPOT_MATCH
);
1288 virtual int cancel() {
1290 WifiRequest
request(familyId(), ifaceId());
1291 int result
= createRequest(request
, 0);
1292 if (result
!= WIFI_SUCCESS
) {
1293 ALOGE("failed to create request; result = %d", result
);
1295 result
= requestResponse(request
);
1296 if (result
!= WIFI_SUCCESS
) {
1297 ALOGE("failed to reset ANQPO networks;result = %d", result
);
1301 unregisterVendorHandler(GOOGLE_OUI
, WIFI_HOTSPOT_MATCH
);
1302 return WIFI_SUCCESS
;
1305 virtual int handleResponse(WifiEvent
& reply
) {
1306 /* Nothing to do on response! */
1310 virtual int handleEvent(WifiEvent
& event
) {
1311 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
1312 unsigned int len
= event
.get_vendor_data_len();
1313 if (vendor_data
== NULL
|| len
< sizeof(wifi_scan_result
)) {
1314 ALOGE("ERROR: No scan results found");
1318 wifi_scan_result
*result
= (wifi_scan_result
*)event
.get_vendor_data();
1319 byte
*anqp
= (byte
*)result
+ offsetof(wifi_scan_result
, ie_data
) + result
->ie_length
;
1320 int networkId
= *(int *)anqp
;
1321 anqp
+= sizeof(int);
1322 int anqp_len
= *(u16
*)anqp
;
1323 anqp
+= sizeof(u16
);
1325 if(*mHandler
.on_passpoint_network_found
)
1326 (*mHandler
.on_passpoint_network_found
)(id(), networkId
, result
, anqp_len
, anqp
);
1332 wifi_error
wifi_set_passpoint_list(wifi_request_id id
, wifi_interface_handle iface
, int num
,
1333 wifi_passpoint_network
*networks
, wifi_passpoint_event_handler handler
)
1335 wifi_handle handle
= getWifiHandle(iface
);
1336 HsListCommand
*cmd
= new HsListCommand(id
, iface
, num
, networks
, handler
);
1338 wifi_register_cmd(handle
, id
, cmd
);
1339 wifi_error result
= (wifi_error
)cmd
->start();
1340 if (result
!= WIFI_SUCCESS
) {
1341 wifi_unregister_cmd(handle
, id
);
1346 wifi_error
wifi_reset_passpoint_list(wifi_request_id id
, wifi_interface_handle iface
)
1348 wifi_handle handle
= getWifiHandle(iface
);
1350 HsListCommand
*cmd
= (HsListCommand
*)(wifi_get_cmd(handle
, id
));
1353 cmd
= new HsListCommand(id
, iface
, 0);
1354 wifi_register_cmd(handle
, id
, cmd
);
1356 result
= (wifi_error
)cmd
->cancel();
1357 wifi_unregister_cmd(handle
, id
);