summaryrefslogtreecommitdiffstats
path: root/service/lib/gscan.cpp
blob: f6ce57b0b5c2bda150285d0e63e15af226f71255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

#include <stdint.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <linux/rtnetlink.h>
#include <netpacket/packet.h>
#include <linux/filter.h>
#include <linux/errqueue.h>

#include <linux/pkt_sched.h>
#include <netlink/object-api.h>
#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <netlink-types.h>

#include <linux/nl80211.h>

#include "sync.h"

#define LOG_TAG  "WifiHAL"

#include <utils/Log.h>

#include "wifi_hal.h"
#include "common.h"
#include "cpp_bindings.h"

/* TODO: define vendor subcommands */
typedef enum {

    GSCAN_SUBCMD_START_GSCAN = ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START,
    GSCAN_SUBCMD_STOP_GSCAN,
    GSCAN_SUBCMD_GSCAN_RESULTS,

    GSCAN_SUBCMD_SET_HOTLIST,
    GSCAN_SUBCMD_HOTLIST_RESULTS,

    GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_MONITOR,
    GSCAN_SUBCMD_SIGNIFICANT_CHANGE_RESULTS,

    /* Add more sub commands here */

    GSCAN_SUBCMD_MAX

} GSCAN_SUB_COMMAND;

typedef enum {
    GSCAN_ATTRIBUTE_SCAN_CHANNELS = 10,
    GSCAN_ATTRIBUTE_SCAN_SINGLE_SHOT,
    GSCAN_ATTRIBUTE_SCAN_FREQUENCY,

    /* remaining reserved for additional attributes */

    GSCAN_ATTRIBUTE_HOTLIST_BSSIDS = 20,

    /* remaining reserved for additional attributes */

    GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_ENABLE = 30,


    GSCAN_ATTRIBUTE_MAX

} GSCAN_ATTRIBUTE;

/////////////////////////////////////////////////////////////////////////////


class ScanCommand : public WifiCommand
{
private:
    static const unsigned int MAX_CHANNELS = 64;
    int mChannels[MAX_CHANNELS];
    int mNumChannels;
    IScanResultsHandler mHandler;
    static const unsigned int MAX_RESULTS = 64;
    wifi_scan_result mResults[MAX_RESULTS];
public:
    ScanCommand(wifi_handle handle, int id, int *channels, unsigned n,
                IScanResultsHandler handler)
        : WifiCommand(handle, id), mHandler(handler)
    {
        mNumChannels = n > MAX_CHANNELS ? MAX_CHANNELS : n;
        for (int i = 0; i < mNumChannels; i++) {
            mChannels[i] = channels[i];
        }
    }

    virtual int create() {
        int ret = mMsg.create(NL80211_CMD_START_SCHED_SCAN, 0, 0);
        if (ret < 0) {
            return ret;
        }
        struct nlattr * attr = mMsg.attr_start(NL80211_ATTR_SCAN_FREQUENCIES);
        for (int i = 0; i < mNumChannels; i++) {
            ret = mMsg.put_u32(i + 1, mChannels[i]);
            if (ret < 0) {
                return ret;
            }
        }
        mMsg.attr_end(attr);
        mMsg.put_u32(NL80211_ATTR_SCAN_FLAGS, NL80211_SCAN_FLAG_FLUSH);
        return ret;
    }

    int start() {
        return requestEvent(NL80211_CMD_SCHED_SCAN_RESULTS);
    }

    virtual int cancel() {
        /* TODO: send another command to the driver to cancel the scan */
        wifi_unregister_handler(mInfo, NL80211_CMD_SCHED_SCAN_RESULTS);
        return WIFI_SUCCESS;
    }

    virtual int handleResponse(WifiEvent reply) {
        /* Nothing to do on response! */
        return NL_SKIP;
    }

    virtual int handleEvent(WifiEvent event) {
        ALOGI("Got a scan results event");

        int rem = 0, i = 0;

        nl_iterator it(event.get_attribute(NL80211_ATTR_SCAN_SSIDS));
        for ( ; it.has_next(); it.next()) {
            struct nlattr *attr = it.get();
            wifi_scan_result *result = &mResults[i];
            char *ssid = (char *)nla_data(attr);
            int len = nla_len(attr);
            memcpy(result->ssid, ssid, len);
            ssid[len] = 0;
        }

        (*mHandler.on_scan_results)(id(), i, mResults);
        return NL_SKIP;
    }
};

wifi_error wifi_start_gscan(
        wifi_request_id id,
        wifi_interface_handle iface,
        wifi_scan_cmd_params params,
        IScanResultsHandler handler)
{
    interface_info *iinfo = (interface_info *)iface;
    wifi_handle handle = iinfo->handle;
    hal_info *info = (hal_info *)handle;

    ScanCommand *cmd = new ScanCommand(handle, id, params.channels, params.num_channels, handler);
    wifi_register_cmd(handle, id, cmd);
    return (wifi_error)cmd->start();
}

wifi_error wifi_stop_gscan(wifi_request_id id, wifi_interface_handle iface)
{
    interface_info *iinfo = (interface_info *)iface;
    wifi_handle handle = iinfo->handle;
    hal_info *info = (hal_info *)handle;

    WifiCommand *cmd = wifi_unregister_cmd(handle, id);
    if (cmd) {
        cmd->cancel();
        delete cmd;
        return WIFI_SUCCESS;
    }

    return WIFI_ERROR_INVALID_ARGS;
}


/////////////////////////////////////////////////////////////////////////////

class BssidHotlistCommand : public WifiCommand
{
private:
    static const uint32_t VENDOR_OUI = GOOGLE_OUI;
    int mNum;
    mac_addr *mBssids;
    wifi_hotlist_ap_found_handler mHandler;
    static const unsigned int MAX_RESULTS = 64;
    wifi_scan_result mResults[MAX_RESULTS];
public:
    BssidHotlistCommand(wifi_handle handle, int id,
            mac_addr bssid[], int num, wifi_hotlist_ap_found_handler handler)
        : WifiCommand(handle, id), mNum(num), mBssids(bssid), mHandler(handler)
    { }

    virtual int create() {
        int ret = mMsg.create(VENDOR_OUI, GSCAN_SUBCMD_SET_HOTLIST);
        if (ret < 0) {
            return ret;
        }
        struct nlattr * attr = mMsg.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS);
        for (int i = 0; i < mNum; i++) {
            ret = mMsg.put_addr(i + 1, mBssids[i]);
            if (ret < 0) {
                return ret;
            }
        }
        mMsg.attr_end(attr);
        return ret;
    }

    int start() {
        registerVendorHandler(VENDOR_OUI, GSCAN_SUBCMD_HOTLIST_RESULTS);
        int res = requestResponse();
        mMsg.destroy();
        return res;
    }

    virtual int cancel() {
        /* unregister event handler */
        unregisterVendorHandler(VENDOR_OUI, GSCAN_SUBCMD_HOTLIST_RESULTS);

        /* create set hotlist message with empty hotlist */
        int ret = mMsg.create(VENDOR_OUI, GSCAN_SUBCMD_SET_HOTLIST);
        if (ret < 0) {
            return WIFI_ERROR_OUT_OF_MEMORY;
        }

        struct nlattr * attr = mMsg.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS);
        if (attr == NULL) {
            return WIFI_ERROR_OUT_OF_MEMORY;
        }

        mMsg.attr_end(attr);
        return requestResponse();
    }

    virtual int handleResponse(WifiEvent reply) {
        /* Nothing to do on response! */
        return NL_SKIP;
    }

    virtual int handleEvent(WifiEvent event) {
        ALOGI("Got a scan results event");

        int rem = 0, i = 0;

        nl_iterator it(event.get_attribute(NL80211_ATTR_SCAN_SSIDS));
        for ( ; it.has_next(); it.next()) {
            struct nlattr *attr = it.get();
            wifi_scan_result *result = &mResults[i];
            char *ssid = (char *)nla_data(attr);
            int len = nla_len(attr);
            memcpy(result->ssid, ssid, len);
            ssid[len] = 0;
        }

        (*mHandler.on_hotlist_ap_found)(id(), i, mResults);
        return NL_SKIP;
    }
};

wifi_error wifi_set_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface,
        int num_bssid, mac_addr bssid[], wifi_hotlist_ap_found_handler handler)
{
    interface_info *iinfo = (interface_info *)iface;
    wifi_handle handle = iinfo->handle;
    hal_info *info = (hal_info *)handle;

    BssidHotlistCommand *cmd = new BssidHotlistCommand(handle, id, bssid, num_bssid, handler);
    wifi_register_cmd(handle, id, cmd);
    return (wifi_error)cmd->start();
}

wifi_error wifi_reset_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface)
{
    interface_info *iinfo = (interface_info *)iface;
    wifi_handle handle = iinfo->handle;
    hal_info *info = (hal_info *)handle;

    WifiCommand *cmd = wifi_unregister_cmd(handle, id);
    if (cmd) {
        cmd->cancel();
        delete cmd;
        return WIFI_SUCCESS;
    }

    return WIFI_ERROR_INVALID_ARGS;
}


/////////////////////////////////////////////////////////////////////////////


class SignificantWifiChangeCommand : public WifiCommand
{
private:
    static const uint32_t VENDOR_OUI = GOOGLE_OUI;
    wifi_significant_change_handler mHandler;
    static const unsigned int MAX_RESULTS = 64;
    wifi_scan_result mResults[MAX_RESULTS];
public:
    SignificantWifiChangeCommand(wifi_handle handle, int id,
            wifi_significant_change_handler handler)
        : WifiCommand(handle, id), mHandler(handler)
    { }

    virtual int create() {
        int ret = mMsg.create(VENDOR_OUI, GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_MONITOR);
        if (ret < 0) {
            return ret;
        }
        mMsg.put_u8(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_ENABLE, 1);
        return ret;
    }

    int start() {
        registerVendorHandler(VENDOR_OUI, GSCAN_SUBCMD_SIGNIFICANT_CHANGE_RESULTS);
        int res = requestResponse();
        mMsg.destroy();
        return res;
    }

    virtual int cancel() {
        /* unregister event handler */
        unregisterVendorHandler(VENDOR_OUI, GSCAN_SUBCMD_SIGNIFICANT_CHANGE_RESULTS);

        /* create set significant change monitor message with empty hotlist */
        int ret = mMsg.create(VENDOR_OUI, GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_MONITOR);
        if (ret < 0) {
            return WIFI_ERROR_OUT_OF_MEMORY;
        }
        mMsg.put_u8(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_ENABLE, 0);
        return requestResponse();
    }

    virtual int handleResponse(WifiEvent reply) {
        /* Nothing to do on response! */
        return NL_SKIP;
    }

    virtual int handleEvent(WifiEvent event) {
        ALOGI("Got a scan results event");

        int rem = 0, i = 0;

        nl_iterator it(event.get_attribute(NL80211_ATTR_SCAN_SSIDS));
        for ( ; it.has_next(); it.next()) {
            struct nlattr *attr = it.get();
            wifi_scan_result *result = &mResults[i];
            char *ssid = (char *)nla_data(attr);
            int len = nla_len(attr);
            memcpy(result->ssid, ssid, len);
            ssid[len] = 0;
        }

        (*mHandler.on_significant_change)(id(), i, mResults);
        return NL_SKIP;
    }
};

wifi_error wifi_set_significant_change_handler(wifi_request_id id, wifi_interface_handle iface,
        wifi_significant_change_handler handler)
{
    interface_info *iinfo = (interface_info *)iface;
    wifi_handle handle = iinfo->handle;
    hal_info *info = (hal_info *)handle;

    SignificantWifiChangeCommand *cmd = new SignificantWifiChangeCommand(handle, id, handler);
    wifi_register_cmd(handle, id, cmd);
    return (wifi_error)cmd->start();
}

wifi_error wifi_reset_significant_change_handler(wifi_request_id id, wifi_interface_handle iface)
{
    interface_info *iinfo = (interface_info *)iface;
    wifi_handle handle = iinfo->handle;
    hal_info *info = (hal_info *)handle;

    WifiCommand *cmd = wifi_unregister_cmd(handle, id);
    if (cmd) {
        cmd->cancel();
        delete cmd;
        return WIFI_SUCCESS;
    }

    return WIFI_ERROR_INVALID_ARGS;
}