summaryrefslogtreecommitdiff
path: root/wcn6740/qcwcn/wifi_hal/radio_mode.cpp
blob: 267d3f466d9fc26506f429548fe8688fb5ccf2cb (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
/* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of The Linux Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 * Changes from Qualcomm Innovation Center are provided under the following license:

 *  Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
 *  SPDX-License-Identifier: BSD-3-Clause-Clear
 */

#include "sync.h"

#include <utils/Log.h>

#include "wifi_hal.h"
#include "common.h"
#include "cpp_bindings.h"
#include "radio_mode.h"
#include "vendor_definitions.h"
#include <netlink/genl/genl.h>
#include <string.h>
#include <net/if.h>

/* Used to handle radio command events from driver/firmware. */
typedef struct radio_event_handler_s {
    RADIOModeCommand* mRADIOModeCommandInstance;
} radio_event_handlers;

wifi_error initializeRadioHandler(hal_info *info)
{
    info->radio_handlers = (radio_event_handlers *)malloc(
            sizeof(radio_event_handlers));
    if (info->radio_handlers) {
        memset(info->radio_handlers, 0, sizeof(radio_event_handlers));
    } else {
        ALOGE("%s: Allocation of radio event handlers failed",
                __FUNCTION__);
        return WIFI_ERROR_OUT_OF_MEMORY;
    }
    return WIFI_SUCCESS;
}

wifi_error cleanupRadioHandler(hal_info *info) {
    radio_event_handlers* event_handlers;
    if (info && info->radio_handlers) {
        event_handlers = (radio_event_handlers*) info->radio_handlers;
        if (event_handlers->mRADIOModeCommandInstance) {
            delete event_handlers->mRADIOModeCommandInstance;
        }
        memset(event_handlers, 0, sizeof(radio_event_handlers));
        free(info->radio_handlers);
        info->radio_handlers = NULL;
        return WIFI_SUCCESS;
    }
    ALOGE ("%s: info or info->radio_handlers NULL", __FUNCTION__);
    return WIFI_ERROR_UNKNOWN;
}

/* Used to handle radio mode command events from driver/firmware.*/
void RADIOModeCommand::setCallbackHandler(wifi_radio_mode_change_handler handler)
{
    mHandler = handler;
}

void RADIOModeCommand::setReqId(wifi_request_id id)
{
    mreqId =  id;
}

RADIOModeCommand::RADIOModeCommand(wifi_handle handle, int id,
                                   u32 vendor_id, u32 subcmd)
        : WifiVendorCommand(handle, id, vendor_id, subcmd)
{
    memset(&mHandler, 0, sizeof(mHandler));
    if (registerVendorHandler(vendor_id, subcmd)) {
        /* Error case should not happen print log */
        ALOGE("%s: Unable to register Vendor Handler Vendor Id=0x%x subcmd=%u",
              __FUNCTION__, vendor_id, subcmd);
    }
}

RADIOModeCommand::~RADIOModeCommand()
{
    unregisterVendorHandler(mVendor_id, mSubcmd);
}


RADIOModeCommand* RADIOModeCommand::instance(wifi_handle handle,
                                             wifi_request_id id)
{
    if (handle == NULL) {
        ALOGE("Interface Handle is invalid");
        return NULL;
    }
    hal_info *info = getHalInfo(handle);
    if (!info) {
        ALOGE("hal_info is invalid");
        return NULL;
    }
    RADIOModeCommand* instance = info->radio_handlers->mRADIOModeCommandInstance;
    if (instance) {
        if (handle != getWifiHandle(instance->mInfo)) {
            ALOGV("%s - Handle different, update the handle", __FUNCTION__);
            instance->mInfo = (hal_info *)handle;
        }
        instance->setReqId(id);
    } else {
        info->radio_handlers->mRADIOModeCommandInstance =
            new RADIOModeCommand(handle, id, OUI_QCA,
                QCA_NL80211_VENDOR_SUBCMD_WLAN_MAC_INFO);
        instance = info->radio_handlers->mRADIOModeCommandInstance;
    }
    return instance;
}

/* This function will be the main handler for incoming event.
 * Call the appropriate callback handler after parsing the vendor data.
 */
int RADIOModeCommand::handleEvent(WifiEvent &event)
{
    wifi_error ret = WIFI_ERROR_UNKNOWN;
    int num_of_mac = 0;
    wifi_mac_info mode_info;
    memset(&mode_info, 0, sizeof(mode_info));

    WifiVendorCommand::handleEvent(event);

    /* Parse the vendordata and get the attribute */
    switch(mSubcmd)
    {
        case QCA_NL80211_VENDOR_SUBCMD_WLAN_MAC_INFO:
        {
            struct nlattr *mtb_vendor[QCA_WLAN_VENDOR_ATTR_MAC_MAX + 1];
            struct nlattr *modeInfo;
            int rem;

            nla_parse(mtb_vendor, QCA_WLAN_VENDOR_ATTR_MAC_MAX,
                      (struct nlattr *)mVendorData,
                      mDataLen, NULL);

            if (mtb_vendor[QCA_WLAN_VENDOR_ATTR_MAC_INFO])
            {
                for (modeInfo = (struct nlattr *) nla_data(mtb_vendor[QCA_WLAN_VENDOR_ATTR_MAC_INFO]),
                     rem = nla_len(mtb_vendor[QCA_WLAN_VENDOR_ATTR_MAC_INFO]);
                     nla_ok(modeInfo, rem);modeInfo = nla_next(modeInfo, &(rem))) {

                     struct nlattr *tb2[QCA_WLAN_VENDOR_ATTR_MAC_INFO_MAX+ 1];
                     nla_parse(tb2, QCA_WLAN_VENDOR_ATTR_MAC_INFO_MAX,
                                (struct nlattr *) nla_data(modeInfo), nla_len(modeInfo), NULL);
                     if (!tb2[QCA_WLAN_VENDOR_ATTR_MAC_INFO_MAC_ID])
                     {
                        ALOGE("%s: QCA_WLAN_VENDOR_ATTR_MAC_INFO_MAC_ID"
                               " not found", __FUNCTION__);
                        ret = WIFI_ERROR_INVALID_ARGS;
                        goto cleanup;
                     }
                     mode_info.wlan_mac_id = nla_get_u32(tb2[QCA_WLAN_VENDOR_ATTR_MAC_INFO_MAC_ID]);
                     ALOGV("mac_id[%d]: %d ", num_of_mac, mode_info.wlan_mac_id);

                     if (!tb2[QCA_WLAN_VENDOR_ATTR_MAC_INFO_BAND])
                     {
                         ALOGE("%s: QCA_WLAN_VENDOR_ATTR_MAC_INFO_BAND"
                               " NOT FOUND", __FUNCTION__);
                         ret = WIFI_ERROR_INVALID_ARGS;
                         goto cleanup;
                     }
                     mode_info.mac_band = (wlan_mac_band) nla_get_u32(tb2[QCA_WLAN_VENDOR_ATTR_MAC_INFO_BAND]);
                     ALOGV("mac_band[%d]: %d ", num_of_mac, mode_info.mac_band);

                     if (tb2[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO])
                     {
                       int num_of_iface = 0;
                       struct nlattr *tb_iface;
                       int rem_info;

                       for (tb_iface = (struct nlattr *) nla_data(tb2[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO]),
                            rem_info = nla_len(tb2[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO]);
                            nla_ok(tb_iface, rem_info);tb_iface = nla_next(tb_iface, &(rem_info))) {

                            struct nlattr *tb3[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_MAX+ 1];
                            wifi_iface_info miface_info;

                            nla_parse(tb3, QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_MAX,
                                      (struct nlattr *) nla_data(tb_iface), nla_len(tb_iface), NULL);

                            if (!tb3[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_IFINDEX])
                            {
                                ALOGE("%s: QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_IFINDEX"
                                      " NOT FOUND", __FUNCTION__);
                                ret = WIFI_ERROR_INVALID_ARGS;
                                goto cleanup;
                            }
                            if (if_indextoname(nla_get_u32(tb3[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_IFINDEX]),
                                               miface_info.iface_name) == NULL)
                            {
                                ALOGE("%s: Failed to convert %d IFINDEX to IFNAME", __FUNCTION__,
                                      nla_get_u32(tb3[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_IFINDEX]));
                            }
                            ALOGV("ifname[%d]: %s ", num_of_iface, miface_info.iface_name);

                            if (!tb3[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_FREQ])
                            {
                                ALOGE("%s: QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_FREQ"
                                      " NOT FOUND", __FUNCTION__);
                                ret = WIFI_ERROR_INVALID_ARGS;
                                goto cleanup;
                            }
                            miface_info.channel = nla_get_u32(tb3[QCA_WLAN_VENDOR_ATTR_MAC_IFACE_INFO_FREQ]);
                            ALOGV("channel[%d]: %d ", num_of_iface, miface_info.channel);

                            if (!num_of_iface)
                               mode_info.iface_info = (wifi_iface_info *)
                                         malloc(sizeof(wifi_iface_info));
                            else
                               mode_info.iface_info = (wifi_iface_info *)
                                         realloc(mode_info.iface_info, (num_of_iface + 1) * sizeof(wifi_iface_info));

                            if (mode_info.iface_info != NULL) {
                                memcpy(&mode_info.iface_info[num_of_iface], &miface_info, sizeof(wifi_iface_info));
                                num_of_iface++;
                                mode_info.num_iface = num_of_iface;
                            }
                       }
                    }
                    if (!num_of_mac)
                       mwifi_iface_mac_info = (wifi_mac_info *)
                          malloc(sizeof(wifi_mac_info));
                    else
                       mwifi_iface_mac_info = (wifi_mac_info *)
                          realloc(mwifi_iface_mac_info, (num_of_mac + 1) * (sizeof(wifi_mac_info)));

                    if (mwifi_iface_mac_info != NULL) {
                        memcpy(&mwifi_iface_mac_info[num_of_mac], &mode_info, sizeof(wifi_mac_info));
                        num_of_mac++;
                    }
                }
            }

            if (mHandler.on_radio_mode_change && num_of_mac) {
                (*mHandler.on_radio_mode_change)(mreqId, num_of_mac, mwifi_iface_mac_info);
            }
            else {
                  ALOGE("No Callback registered: on radio mode change");
                  ret = WIFI_ERROR_UNKNOWN;
                  goto cleanup;
            }
            ret = WIFI_SUCCESS;
        }
        break;

        default:
            /* Error case should not happen print log */
            ALOGE("%s: Wrong subcmd received %d", __FUNCTION__, mSubcmd);
    }

cleanup:
    if (mode_info.iface_info != NULL) {
       free(mode_info.iface_info);
       mode_info.iface_info = NULL;
    }
    if (mwifi_iface_mac_info != NULL) {
       free(mwifi_iface_mac_info);
       mwifi_iface_mac_info = NULL;
    }

    return ret;
}

wifi_error wifi_set_radio_mode_change_handler(wifi_request_id id,
                                      wifi_interface_handle iface,
                                      wifi_radio_mode_change_handler eh)
{
    wifi_error ret;
    WifiVendorCommand *vCommand = NULL;
    wifi_handle wifiHandle = getWifiHandle(iface);
    RADIOModeCommand *radiomodeCommand;

    ret = initialize_vendor_cmd(iface, id,
                                QCA_NL80211_VENDOR_SUBCMD_WLAN_MAC_INFO,
                                &vCommand);
    if (ret != WIFI_SUCCESS) {
        ALOGE("%s: Initialization failed", __FUNCTION__);
        return ret;
    }

    radiomodeCommand = RADIOModeCommand::instance(wifiHandle, id);
    if (radiomodeCommand == NULL) {
        ALOGE("%s: Error RadioModeCommand NULL", __FUNCTION__);
        ret = WIFI_ERROR_OUT_OF_MEMORY;
        goto cleanup;
    }
    radiomodeCommand->setCallbackHandler(eh);
    radiomodeCommand->setReqId(id);

cleanup:
    delete vCommand;
    return mapKernelErrortoWifiHalError(ret);
}