4 #include <sys/socket.h>
5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <linux/rtnetlink.h>
9 #include <netpacket/packet.h>
10 #include <linux/filter.h>
11 #include <linux/errqueue.h>
13 #include <linux/pkt_sched.h>
14 #include <netlink/object-api.h>
15 #include <netlink/netlink.h>
16 #include <netlink/socket.h>
17 #include <netlink/handlers.h>
21 #define LOG_TAG "WifiHAL"
23 #include <utils/Log.h>
27 #include "cpp_bindings.h"
31 GSCAN_ATTRIBUTE_NUM_BUCKETS
= 10,
32 GSCAN_ATTRIBUTE_BASE_PERIOD
,
33 GSCAN_ATTRIBUTE_BUCKETS_BAND
,
34 GSCAN_ATTRIBUTE_BUCKET_ID
,
35 GSCAN_ATTRIBUTE_BUCKET_PERIOD
,
36 GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS
,
37 GSCAN_ATTRIBUTE_BUCKET_CHANNELS
,
38 GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN
,
39 GSCAN_ATTRIBUTE_REPORT_THRESHOLD
,
40 GSCAN_ATTRIBUTE_NUM_SCANS_TO_CACHE
,
41 GSCAN_ATTRIBUTE_REPORT_THRESHOLD_NUM_SCANS
,
42 GSCAN_ATTRIBUTE_BAND
= GSCAN_ATTRIBUTE_BUCKETS_BAND
,
44 GSCAN_ATTRIBUTE_ENABLE_FEATURE
= 20,
45 GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE
, /* indicates no more results */
46 GSCAN_ATTRIBUTE_REPORT_EVENTS
,
48 /* remaining reserved for additional attributes */
49 GSCAN_ATTRIBUTE_NUM_OF_RESULTS
= 30,
50 GSCAN_ATTRIBUTE_SCAN_RESULTS
, /* flat array of wifi_scan_result */
51 GSCAN_ATTRIBUTE_NUM_CHANNELS
,
52 GSCAN_ATTRIBUTE_CHANNEL_LIST
,
53 GSCAN_ATTRIBUTE_SCAN_ID
,
54 GSCAN_ATTRIBUTE_SCAN_FLAGS
,
56 /* remaining reserved for additional attributes */
58 GSCAN_ATTRIBUTE_SSID
= 40,
59 GSCAN_ATTRIBUTE_BSSID
,
60 GSCAN_ATTRIBUTE_CHANNEL
,
62 GSCAN_ATTRIBUTE_TIMESTAMP
,
64 GSCAN_ATTRIBUTE_RTTSD
,
66 /* remaining reserved for additional attributes */
68 GSCAN_ATTRIBUTE_HOTLIST_BSSIDS
= 50,
69 GSCAN_ATTRIBUTE_RSSI_LOW
,
70 GSCAN_ATTRIBUTE_RSSI_HIGH
,
71 GSCAN_ATTRIBUTE_HOTLIST_ELEM
,
72 GSCAN_ATTRIBUTE_HOTLIST_FLUSH
,
73 GSCAN_ATTRIBUTE_CHANNEL_NUMBER
,
75 /* remaining reserved for additional attributes */
76 GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE
= 60,
77 GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE
,
78 GSCAN_ATTRIBUTE_MIN_BREACHING
,
79 GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS
,
86 class GetCapabilitiesCommand
: public WifiCommand
88 wifi_gscan_capabilities
*mCapabilities
;
90 GetCapabilitiesCommand(wifi_interface_handle iface
, wifi_gscan_capabilities
*capabitlites
)
91 : WifiCommand(iface
, 0), mCapabilities(capabitlites
)
93 memset(mCapabilities
, 0, sizeof(*mCapabilities
));
96 virtual int create() {
97 ALOGD("Creating message to get scan capablities; iface = %d", mIfaceInfo
->id
);
99 int ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_CAPABILITIES
);
101 ALOGD("NL message creation failed");
109 virtual int handleResponse(WifiEvent
& reply
) {
111 ALOGD("In GetCapabilities::handleResponse");
113 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
114 ALOGD("Ignoring reply with cmd = %d", reply
.get_cmd());
118 int id
= reply
.get_vendor_id();
119 int subcmd
= reply
.get_vendor_subcmd();
121 void *data
= reply
.get_vendor_data();
122 int len
= reply
.get_vendor_data_len();
124 ALOGD("Id = %0x, subcmd = %d, len = %d, expected len = %d", id
, subcmd
, len
,
125 sizeof(*mCapabilities
));
127 memcpy(mCapabilities
, data
, min(len
, (int) sizeof(*mCapabilities
)));
134 wifi_error
wifi_get_gscan_capabilities(wifi_interface_handle handle
,
135 wifi_gscan_capabilities
*capabilities
)
137 GetCapabilitiesCommand
command(handle
, capabilities
);
138 return (wifi_error
) command
.requestResponse();
141 class GetChannelListCommand
: public WifiCommand
143 wifi_channel
*channels
;
148 GetChannelListCommand(wifi_interface_handle iface
, wifi_channel
*channel_buf
, int *ch_num
,
149 int num_max_ch
, int band
)
150 : WifiCommand(iface
, 0), channels(channel_buf
), max_channels(num_max_ch
), num_channels(ch_num
),
153 memset(channels
, 0, sizeof(wifi_channel
) * max_channels
);
155 virtual int create() {
156 ALOGD("Creating message to get channel list; iface = %d", mIfaceInfo
->id
);
158 int ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_VALID_CHANNELS
);
163 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
164 ret
= mMsg
.put_u32(GSCAN_ATTRIBUTE_BAND
, band
);
175 virtual int handleResponse(WifiEvent
& reply
) {
177 ALOGD("In GetChannelList::handleResponse");
179 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
180 ALOGD("Ignoring reply with cmd = %d", reply
.get_cmd());
184 int id
= reply
.get_vendor_id();
185 int subcmd
= reply
.get_vendor_subcmd();
186 int num_channels_to_copy
= 0;
188 nlattr
*vendor_data
= reply
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
189 int len
= reply
.get_vendor_data_len();
191 ALOGD("Id = %0x, subcmd = %d, len = %d", id
, subcmd
, len
);
192 if (vendor_data
== NULL
|| len
== 0) {
193 ALOGE("no vendor data in GetChannelList response; ignoring it");
197 for (nl_iterator
it(vendor_data
); it
.has_next(); it
.next()) {
198 if (it
.get_type() == GSCAN_ATTRIBUTE_NUM_CHANNELS
) {
199 num_channels_to_copy
= it
.get_u32();
200 ALOGD("Got channel list with %d channels", num_channels_to_copy
);
201 if(num_channels_to_copy
> max_channels
)
202 num_channels_to_copy
= max_channels
;
203 *num_channels
= num_channels_to_copy
;
204 } else if (it
.get_type() == GSCAN_ATTRIBUTE_CHANNEL_LIST
&& num_channels_to_copy
) {
205 memcpy(channels
, it
.get_data(), sizeof(int) * num_channels_to_copy
);
207 ALOGW("Ignoring invalid attribute type = %d, size = %d",
208 it
.get_type(), it
.get_len());
216 wifi_error
wifi_get_valid_channels(wifi_interface_handle handle
,
217 int band
, int max_channels
, wifi_channel
*channels
, int *num_channels
)
219 GetChannelListCommand
command(handle
, channels
, num_channels
,
221 return (wifi_error
) command
.requestResponse();
223 /////////////////////////////////////////////////////////////////////////////
225 /* helper functions */
227 static int parseScanResults(wifi_scan_result
*results
, int num
, nlattr
*attr
)
229 memset(results
, 0, sizeof(wifi_scan_result
) * num
);
232 for (nl_iterator
it(attr
); it
.has_next() && i
< num
; it
.next(), i
++) {
234 int index
= it
.get_type();
235 ALOGD("retrieved scan result %d", index
);
236 nlattr
*sc_data
= (nlattr
*) it
.get_data();
237 wifi_scan_result
*result
= results
+ i
;
239 for (nl_iterator
it2(sc_data
); it2
.has_next(); it2
.next()) {
240 int type
= it2
.get_type();
241 if (type
== GSCAN_ATTRIBUTE_SSID
) {
242 strncpy(result
->ssid
, (char *) it2
.get_data(), it2
.get_len());
243 result
->ssid
[it2
.get_len()] = 0;
244 } else if (type
== GSCAN_ATTRIBUTE_BSSID
) {
245 memcpy(result
->bssid
, (byte
*) it2
.get_data(), sizeof(mac_addr
));
246 } else if (type
== GSCAN_ATTRIBUTE_TIMESTAMP
) {
247 result
->ts
= it2
.get_u64();
248 } else if (type
== GSCAN_ATTRIBUTE_CHANNEL
) {
249 result
->ts
= it2
.get_u16();
250 } else if (type
== GSCAN_ATTRIBUTE_RSSI
) {
251 result
->rssi
= it2
.get_u8();
252 } else if (type
== GSCAN_ATTRIBUTE_RTT
) {
253 result
->rtt
= it2
.get_u64();
254 } else if (type
== GSCAN_ATTRIBUTE_RTTSD
) {
255 result
->rtt_sd
= it2
.get_u64();
262 ALOGE("Got too many results; skipping some");
268 int createFeatureRequest(WifiRequest
& request
, int subcmd
) {
270 int result
= request
.create(GOOGLE_OUI
, subcmd
);
278 class ScanCommand
: public WifiCommand
280 wifi_scan_cmd_params
*mParams
;
281 wifi_scan_result_handler mHandler
;
282 static unsigned mGlobalFullScanBuckets
;
283 bool mLocalFullScanBuckets
;
285 ScanCommand(wifi_interface_handle iface
, int id
, wifi_scan_cmd_params
*params
,
286 wifi_scan_result_handler handler
)
287 : WifiCommand(iface
, id
), mParams(params
), mHandler(handler
),
288 mLocalFullScanBuckets(0)
291 int createSetupRequest(WifiRequest
& request
) {
292 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN
);
297 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
298 result
= request
.put_u32(GSCAN_ATTRIBUTE_BASE_PERIOD
, mParams
->base_period
);
303 result
= request
.put_u32(GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN
, mParams
->max_ap_per_scan
);
308 result
= request
.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD
, mParams
->report_threshold_percent
);
313 result
= request
.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD_NUM_SCANS
, mParams
->report_threshold_num_scans
);
318 result
= request
.put_u32(GSCAN_ATTRIBUTE_NUM_BUCKETS
, mParams
->num_buckets
);
323 for (int i
= 0; i
< mParams
->num_buckets
; i
++) {
324 nlattr
* bucket
= request
.attr_start(i
); // next bucket
325 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_ID
, mParams
->buckets
[i
].bucket
);
329 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_PERIOD
, mParams
->buckets
[i
].period
);
333 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKETS_BAND
,
334 mParams
->buckets
[i
].band
);
339 result
= request
.put_u32(GSCAN_ATTRIBUTE_REPORT_EVENTS
,
340 mParams
->buckets
[i
].report_events
);
345 result
= request
.put_u32(GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS
,
346 mParams
->buckets
[i
].num_channels
);
351 if (mParams
->buckets
[i
].num_channels
) {
352 nlattr
*channels
= request
.attr_start(GSCAN_ATTRIBUTE_BUCKET_CHANNELS
);
353 for (int j
= 0; j
< mParams
->buckets
[i
].num_channels
; j
++) {
354 result
= request
.put_u32(j
, mParams
->buckets
[i
].channels
[j
].channel
);
359 request
.attr_end(channels
);
362 request
.attr_end(bucket
);
365 request
.attr_end(data
);
369 int createStartRequest(WifiRequest
& request
) {
370 return createFeatureRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN
);
373 int createStopRequest(WifiRequest
& request
) {
374 return createFeatureRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_DEL_GSCAN
);
378 ALOGD(" sending scan req to driver");
379 WifiRequest
request(familyId(), ifaceId());
380 int result
= createSetupRequest(request
);
381 if (result
!= WIFI_SUCCESS
) {
382 ALOGE("failed to create setup request; result = %d", result
);
385 ALOGD("Starting scan");
387 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE
);
388 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_COMPLETE_SCAN
);
391 for (int i
= 0; i
< mParams
->num_buckets
; i
++) {
392 if (mParams
->buckets
[i
].report_events
== 2) {
398 ALOGI("Full scan requested with nBuckets = %d", nBuckets
);
399 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_FULL_SCAN_RESULTS
);
401 result
= requestResponse(request
);
402 if (result
!= WIFI_SUCCESS
) {
403 ALOGE("failed to start scan; result = %d", result
);
404 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_COMPLETE_SCAN
);
405 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE
);
413 virtual int cancel() {
414 ALOGD("Stopping scan");
416 WifiRequest
request(familyId(), ifaceId());
417 int result
= createStopRequest(request
);
418 if (result
!= WIFI_SUCCESS
) {
419 ALOGE("failed to create stop request; result = %d", result
);
421 result
= requestResponse(request
);
422 if (result
!= WIFI_SUCCESS
) {
423 ALOGE("failed to stop scan; result = %d", result
);
427 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_COMPLETE_SCAN
);
428 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE
);
429 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_FULL_SCAN_RESULTS
);
434 virtual int handleResponse(WifiEvent
& reply
) {
435 /* Nothing to do on response! */
439 virtual int handleEvent(WifiEvent
& event
) {
440 ALOGD("Got a scan results event");
444 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
445 unsigned int len
= event
.get_vendor_data_len();
446 int event_id
= event
.get_vendor_subcmd();
447 ALOGD("handleEvent, event_id = %d", event_id
);
449 if(event_id
== GSCAN_EVENT_COMPLETE_SCAN
) {
450 if (vendor_data
== NULL
|| len
!= 4) {
451 ALOGD("Scan complete type not mentioned!");
454 wifi_scan_event evt_type
;
456 evt_type
= (wifi_scan_event
) event
.get_u32(NL80211_ATTR_VENDOR_DATA
);
457 ALOGD("Scan complete: Received event type %d", evt_type
);
458 if(*mHandler
.on_scan_event
)
459 (*mHandler
.on_scan_event
)(evt_type
, evt_type
);
460 } else if(event_id
== GSCAN_EVENT_FULL_SCAN_RESULTS
) {
461 if (vendor_data
== NULL
|| len
< sizeof(wifi_scan_result
)) {
462 ALOGD("No scan results found");
465 wifi_scan_result
*result
= (wifi_scan_result
*)event
.get_vendor_data();
467 if(*mHandler
.on_full_scan_result
)
468 (*mHandler
.on_full_scan_result
)(id(), result
);
470 ALOGD("%-32s\t", result
->ssid
);
472 ALOGD("%02x:%02x:%02x:%02x:%02x:%02x ", result
->bssid
[0], result
->bssid
[1],
473 result
->bssid
[2], result
->bssid
[3], result
->bssid
[4], result
->bssid
[5]);
475 ALOGD("%d\t", result
->rssi
);
476 ALOGD("%d\t", result
->channel
);
477 ALOGD("%lld\t", result
->ts
);
478 ALOGD("%lld\t", result
->rtt
);
479 ALOGD("%lld\n", result
->rtt_sd
);
482 if (vendor_data
== NULL
|| len
!= 4) {
483 ALOGD("No scan results found");
487 int num
= event
.get_u32(NL80211_ATTR_VENDOR_DATA
);
488 ALOGD("Found %d scan results", num
);
489 if(*mHandler
.on_scan_results_available
)
490 (*mHandler
.on_scan_results_available
)(id(), num
);
496 unsigned ScanCommand::mGlobalFullScanBuckets
= 0;
498 wifi_error
wifi_start_gscan(
500 wifi_interface_handle iface
,
501 wifi_scan_cmd_params params
,
502 wifi_scan_result_handler handler
)
504 wifi_handle handle
= getWifiHandle(iface
);
506 ALOGD("Starting GScan, halHandle = %p", handle
);
508 ScanCommand
*cmd
= new ScanCommand(iface
, id
, ¶ms
, handler
);
509 wifi_register_cmd(handle
, id
, cmd
);
510 return (wifi_error
)cmd
->start();
513 wifi_error
wifi_stop_gscan(wifi_request_id id
, wifi_interface_handle iface
)
515 ALOGD("Stopping GScan");
516 wifi_handle handle
= getWifiHandle(iface
);
519 wifi_scan_result_handler handler
;
520 wifi_scan_cmd_params dummy_params
;
521 wifi_handle handle
= getWifiHandle(iface
);
522 memset(&handler
, 0, sizeof(handler
));
524 ScanCommand
*cmd
= new ScanCommand(iface
, id
, &dummy_params
, handler
);
531 WifiCommand
*cmd
= wifi_unregister_cmd(handle
, id
);
538 return WIFI_ERROR_INVALID_ARGS
;
541 class GetScanResultsCommand
: public WifiCommand
{
542 wifi_cached_scan_results
*mScans
;
548 static const int MAX_RESULTS
= 320;
549 wifi_scan_result mScanResults
[MAX_RESULTS
];
552 GetScanResultsCommand(wifi_interface_handle iface
, byte flush
,
553 wifi_cached_scan_results
*results
, int max
, int *num
)
554 : WifiCommand(iface
, -1), mScans(results
), mMax(max
), mNum(num
),
555 mRetrieved(0), mFlush(flush
), mCompleted(0)
558 int createRequest(WifiRequest
& request
, int num
, byte flush
) {
559 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_SCAN_RESULTS
);
564 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
565 result
= request
.put_u32(GSCAN_ATTRIBUTE_NUM_OF_RESULTS
, num
);
570 request
.attr_end(data
);
575 WifiRequest
request(familyId(), ifaceId());
576 ALOGD("retrieving %d scan results", mMax
);
578 for (int i
= 0; i
< 10 && mRetrieved
< mMax
; i
++) {
579 int result
= createRequest(request
, (mMax
- mRetrieved
), mFlush
);
581 ALOGE("failed to create request");
585 int prev_retrieved
= mRetrieved
;
587 result
= requestResponse(request
);
589 if (result
!= WIFI_SUCCESS
) {
590 ALOGE("failed to retrieve scan results; result = %d", result
);
594 if (mRetrieved
== prev_retrieved
|| mCompleted
) {
595 /* no more items left to retrieve */
602 ALOGE("GetScanResults read %d results", mRetrieved
);
607 virtual int handleResponse(WifiEvent
& reply
) {
608 ALOGD("In GetScanResultsCommand::handleResponse");
610 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
611 ALOGD("Ignoring reply with cmd = %d", reply
.get_cmd());
615 int id
= reply
.get_vendor_id();
616 int subcmd
= reply
.get_vendor_subcmd();
618 ALOGD("Id = %0x, subcmd = %d", id
, subcmd
);
620 nlattr
*vendor_data
= reply
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
621 int len
= reply
.get_vendor_data_len();
623 if (vendor_data
== NULL
|| len
== 0) {
624 ALOGE("no vendor data in GetScanResults response; ignoring it");
628 for (nl_iterator
it(vendor_data
); it
.has_next(); it
.next()) {
629 if (it
.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE
) {
630 mCompleted
= it
.get_u8();
631 ALOGD("retrieved mCompleted flag : %d", mCompleted
);
632 } else if (it
.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS
|| it
.get_type() == 0) {
633 int scan_id
= 0, flags
= 0, num
= 0;
634 for (nl_iterator
it2(it
.get()); it2
.has_next(); it2
.next()) {
635 if (it2
.get_type() == GSCAN_ATTRIBUTE_SCAN_ID
) {
636 scan_id
= it2
.get_u32();
637 ALOGD("retrieved scan_id : 0x%0x", scan_id
);
638 } else if (it2
.get_type() == GSCAN_ATTRIBUTE_SCAN_FLAGS
) {
639 flags
= it2
.get_u8();
640 ALOGD("retrieved scan_flags : 0x%0x", flags
);
641 } else if (it2
.get_type() == GSCAN_ATTRIBUTE_NUM_OF_RESULTS
) {
643 ALOGD("retrieved num_results: %d", num
);
644 } else if (it2
.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS
) {
645 if (mRetrieved
>= mMax
) {
646 ALOGW("Stored %d scans, ignoring excess results", mRetrieved
);
649 num
= it2
.get_len() / sizeof(wifi_scan_result
);
650 num
= min(MAX_RESULTS
- mNextScanResult
, num
);
651 num
= min((int)MAX_AP_CACHE_PER_SCAN
, num
);
652 memcpy(mScanResults
+ mNextScanResult
, it2
.get_data(),
653 sizeof(wifi_scan_result
) * num
);
654 ALOGD("Retrieved %d scan results", num
);
655 wifi_scan_result
*results
= (wifi_scan_result
*)it2
.get_data();
656 for (int i
= 0; i
< num
; i
++) {
657 wifi_scan_result
*result
= results
+ i
;
658 ALOGD("%02d %-32s %02x:%02x:%02x:%02x:%02x:%02x %04d", i
,
659 result
->ssid
, result
->bssid
[0], result
->bssid
[1], result
->bssid
[2],
660 result
->bssid
[3], result
->bssid
[4], result
->bssid
[5],
663 mScans
[mRetrieved
].scan_id
= scan_id
;
664 mScans
[mRetrieved
].flags
= flags
;
665 mScans
[mRetrieved
].num_results
= num
;
666 ALOGD("Setting result of scan_id : 0x%0x", mScans
[mRetrieved
].scan_id
);
667 memcpy(mScans
[mRetrieved
].results
,
668 &(mScanResults
[mNextScanResult
]), num
* sizeof(wifi_scan_result
));
669 mNextScanResult
+= num
;
672 ALOGW("Ignoring invalid attribute type = %d, size = %d",
673 it
.get_type(), it
.get_len());
677 ALOGW("Ignoring invalid attribute type = %d, size = %d",
678 it
.get_type(), it
.get_len());
686 wifi_error
wifi_get_cached_gscan_results(wifi_interface_handle iface
, byte flush
,
687 int max
, wifi_cached_scan_results
*results
, int *num
) {
688 ALOGD("Getting cached scan results, iface handle = %p, num = %d", iface
, *num
);
690 GetScanResultsCommand
*cmd
= new GetScanResultsCommand(iface
, flush
, results
, max
, num
);
691 return (wifi_error
)cmd
->execute();
694 /////////////////////////////////////////////////////////////////////////////
696 class BssidHotlistCommand
: public WifiCommand
699 wifi_bssid_hotlist_params mParams
;
700 wifi_hotlist_ap_found_handler mHandler
;
701 static const int MAX_RESULTS
= 64;
702 wifi_scan_result mResults
[MAX_RESULTS
];
704 BssidHotlistCommand(wifi_interface_handle handle
, int id
,
705 wifi_bssid_hotlist_params params
, wifi_hotlist_ap_found_handler handler
)
706 : WifiCommand(handle
, id
), mParams(params
), mHandler(handler
)
709 int createSetupRequest(WifiRequest
& request
) {
710 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_BSSID_HOTLIST
);
715 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
717 result
= request
.put_u32(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE
, mParams
.lost_ap_sample_size
);
722 struct nlattr
* attr
= request
.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS
);
723 for (int i
= 0; i
< mParams
.num_bssid
; i
++) {
724 nlattr
*attr2
= request
.attr_start(GSCAN_ATTRIBUTE_HOTLIST_ELEM
);
726 return WIFI_ERROR_OUT_OF_MEMORY
;
728 result
= request
.put_addr(GSCAN_ATTRIBUTE_BSSID
, mParams
.ap
[i
].bssid
);
732 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH
, mParams
.ap
[i
].high
);
736 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW
, mParams
.ap
[i
].low
);
740 request
.attr_end(attr2
);
743 request
.attr_end(attr
);
744 request
.attr_end(data
);
748 int createTeardownRequest(WifiRequest
& request
) {
749 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_RESET_BSSID_HOTLIST
);
758 ALOGD("Executing hotlist setup request, num = %d", mParams
.num_bssid
);
759 WifiRequest
request(familyId(), ifaceId());
760 int result
= createSetupRequest(request
);
765 result
= requestResponse(request
);
767 ALOGD("Failed to execute hotlist setup request, result = %d", result
);
768 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_FOUND
);
769 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_LOST
);
773 ALOGD("Successfully set %d APs in the hotlist", mParams
.num_bssid
);
775 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_FOUND
);
776 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_LOST
);
781 virtual int cancel() {
782 /* unregister event handler */
783 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_FOUND
);
784 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_HOTLIST_RESULTS_LOST
);
785 /* create set hotlist message with empty hotlist */
786 WifiRequest
request(familyId(), ifaceId());
787 int result
= createTeardownRequest(request
);
792 result
= requestResponse(request
);
797 ALOGD("Successfully reset APs in current hotlist");
801 virtual int handleResponse(WifiEvent
& reply
) {
802 /* Nothing to do on response! */
806 virtual int handleEvent(WifiEvent
& event
) {
807 ALOGD("Hotlist AP event");
808 int event_id
= event
.get_vendor_subcmd();
811 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
812 int len
= event
.get_vendor_data_len();
814 if (vendor_data
== NULL
|| len
== 0) {
815 ALOGD("No scan results found");
819 memset(mResults
, 0, sizeof(wifi_scan_result
) * MAX_RESULTS
);
821 int num
= len
/ sizeof(wifi_scan_result
);
822 num
= min(MAX_RESULTS
, num
);
823 memcpy(mResults
, event
.get_vendor_data(), num
* sizeof(wifi_scan_result
));
825 if (event_id
== GSCAN_EVENT_HOTLIST_RESULTS_FOUND
) {
826 ALOGD("FOUND %d hotlist APs", num
);
827 if (*mHandler
.on_hotlist_ap_found
)
828 (*mHandler
.on_hotlist_ap_found
)(id(), num
, mResults
);
829 } else if (event_id
== GSCAN_EVENT_HOTLIST_RESULTS_LOST
) {
830 ALOGD("LOST %d hotlist APs", num
);
831 if (*mHandler
.on_hotlist_ap_lost
)
832 (*mHandler
.on_hotlist_ap_lost
)(id(), num
, mResults
);
838 wifi_error
wifi_set_bssid_hotlist(wifi_request_id id
, wifi_interface_handle iface
,
839 wifi_bssid_hotlist_params params
, wifi_hotlist_ap_found_handler handler
)
841 wifi_handle handle
= getWifiHandle(iface
);
843 BssidHotlistCommand
*cmd
= new BssidHotlistCommand(iface
, id
, params
, handler
);
844 wifi_register_cmd(handle
, id
, cmd
);
845 return (wifi_error
)cmd
->start();
848 wifi_error
wifi_reset_bssid_hotlist(wifi_request_id id
, wifi_interface_handle iface
)
850 wifi_handle handle
= getWifiHandle(iface
);
852 WifiCommand
*cmd
= wifi_unregister_cmd(handle
, id
);
859 return WIFI_ERROR_INVALID_ARGS
;
863 /////////////////////////////////////////////////////////////////////////////
865 class SignificantWifiChangeCommand
: public WifiCommand
868 mac_addr bssid
; // BSSID
869 wifi_channel channel
; // channel frequency in MHz
870 int num_rssi
; // number of rssi samples
871 wifi_rssi rssi
[8]; // RSSI history in db
872 } wifi_significant_change_result_internal
;
875 wifi_significant_change_params mParams
;
876 wifi_significant_change_handler mHandler
;
877 static const int MAX_RESULTS
= 64;
878 wifi_significant_change_result_internal mResultsBuffer
[MAX_RESULTS
];
879 wifi_significant_change_result
*mResults
[MAX_RESULTS
];
881 SignificantWifiChangeCommand(wifi_interface_handle handle
, int id
,
882 wifi_significant_change_params params
, wifi_significant_change_handler handler
)
883 : WifiCommand(handle
, id
), mParams(params
), mHandler(handler
)
886 int createSetupRequest(WifiRequest
& request
) {
887 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_SIGNIFICANT_CHANGE
);
892 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
894 result
= request
.put_u16(GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE
, mParams
.rssi_sample_size
);
898 result
= request
.put_u16(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE
, mParams
.lost_ap_sample_size
);
902 result
= request
.put_u16(GSCAN_ATTRIBUTE_MIN_BREACHING
, mParams
.min_breaching
);
907 struct nlattr
* attr
= request
.attr_start(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS
);
909 for (int i
= 0; i
< mParams
.num_bssid
; i
++) {
911 nlattr
*attr2
= request
.attr_start(i
);
913 return WIFI_ERROR_OUT_OF_MEMORY
;
915 result
= request
.put_addr(GSCAN_ATTRIBUTE_BSSID
, mParams
.ap
[i
].bssid
);
919 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH
, mParams
.ap
[i
].high
);
923 result
= request
.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW
, mParams
.ap
[i
].low
);
927 request
.attr_end(attr2
);
930 request
.attr_end(attr
);
931 request
.attr_end(data
);
936 int createTeardownRequest(WifiRequest
& request
) {
937 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_RESET_SIGNIFICANT_CHANGE
);
946 ALOGD("Set significant wifi change");
947 WifiRequest
request(familyId(), ifaceId());
949 int result
= createSetupRequest(request
);
954 result
= requestResponse(request
);
956 ALOGD("failed to set significant wifi change %d", result
);
959 registerVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS
);
964 virtual int cancel() {
965 /* unregister event handler */
966 unregisterVendorHandler(GOOGLE_OUI
, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS
);
968 /* create set significant change monitor message with empty hotlist */
969 WifiRequest
request(familyId(), ifaceId());
971 int result
= createTeardownRequest(request
);
976 result
= requestResponse(request
);
981 ALOGD("successfully reset significant wifi change");
985 virtual int handleResponse(WifiEvent
& reply
) {
986 /* Nothing to do on response! */
990 virtual int handleEvent(WifiEvent
& event
) {
991 ALOGD("Got a significant wifi change event");
993 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
994 int len
= event
.get_vendor_data_len();
996 if (vendor_data
== NULL
|| len
== 0) {
997 ALOGD("No scan results found");
1004 int16_t rssi_history
[8];
1007 int num
= min(len
/ sizeof(ChangeInfo
), MAX_RESULTS
);
1008 ChangeInfo
*ci
= (ChangeInfo
*)event
.get_vendor_data();
1010 for (int i
= 0; i
< num
; i
++) {
1011 memcpy(mResultsBuffer
[i
].bssid
, ci
[i
].bssid
, sizeof(mac_addr
));
1012 mResultsBuffer
[i
].channel
= ci
[i
].channel
;
1013 /* Driver sends N samples and the rest 8-N are filled 0x7FFF
1014 * N = no of rssi samples to average sent in significant change request. */
1016 for (int j
= 0; j
< 8; j
++) {
1017 if (ci
[i
].rssi_history
[j
] == 0x7FFF) {
1021 mResultsBuffer
[i
].rssi
[j
] = (int) ci
[i
].rssi_history
[j
];
1023 mResultsBuffer
[i
].num_rssi
= num_rssi
;
1024 mResults
[i
] = reinterpret_cast<wifi_significant_change_result
*>(&(mResultsBuffer
[i
]));
1027 ALOGD("Retrieved %d scan results", num
);
1030 (*mHandler
.on_significant_change
)(id(), num
, mResults
);
1032 ALOGW("No significant change reported");
1039 wifi_error
wifi_set_significant_change_handler(wifi_request_id id
, wifi_interface_handle iface
,
1040 wifi_significant_change_params params
, wifi_significant_change_handler handler
)
1042 wifi_handle handle
= getWifiHandle(iface
);
1044 SignificantWifiChangeCommand
*cmd
= new SignificantWifiChangeCommand(
1045 iface
, id
, params
, handler
);
1046 wifi_register_cmd(handle
, id
, cmd
);
1047 return (wifi_error
)cmd
->start();
1050 wifi_error
wifi_reset_significant_change_handler(wifi_request_id id
, wifi_interface_handle iface
)
1052 wifi_handle handle
= getWifiHandle(iface
);
1054 WifiCommand
*cmd
= wifi_unregister_cmd(handle
, id
);
1058 return WIFI_SUCCESS
;
1061 return WIFI_ERROR_INVALID_ARGS
;