• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "wlan_callback_impl.h"
17 #include <securec.h>
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include <osal_mem.h>
21 
22 #define WLAN_EID_SSID 0
23 #define MAX_SSID_LEN 32
24 
25 struct ElementHeader {
26     uint8_t id;
27     uint8_t datalen;
28 };
29 
WlanCallbackResetDriver(struct IWlanCallback * self,uint32_t event,int32_t code,const char * ifName)30 static int32_t WlanCallbackResetDriver(struct IWlanCallback *self, uint32_t event, int32_t code, const char *ifName)
31 {
32     (void)self;
33     HDF_LOGE("WlanCallbackResetDriver: receive resetStatus=%{public}d", code);
34     return HDF_SUCCESS;
35 }
36 
PrintSsid(const uint8_t * ie,uint32_t len)37 static void PrintSsid(const uint8_t *ie, uint32_t len)
38 {
39     char ssid[MAX_SSID_LEN] = {0};
40     uint8_t *pos = NULL;
41     struct ElementHeader *hdr = (struct ElementHeader *)ie;
42 
43     if (ie == NULL || len < sizeof(struct ElementHeader)) {
44         return;
45     }
46     while ((ie + len) >= ((uint8_t *)hdr + sizeof(*hdr) + hdr->datalen)) {
47         pos = (uint8_t *)hdr + sizeof(*hdr);
48         if (hdr->id == WLAN_EID_SSID) {
49             if (hdr->datalen < MAX_SSID_LEN && memcpy_s(ssid, MAX_SSID_LEN, pos, hdr->datalen) == EOK) {
50                 HDF_LOGE("ssid: %{public}s", ssid);
51             }
52             return;
53         }
54         hdr = (struct ElementHeader *)(pos + hdr->datalen);
55     }
56 }
57 
WlanCallbackScanResult(struct IWlanCallback * self,uint32_t event,const struct HdfWifiScanResult * scanResult,const char * ifName)58 static int32_t WlanCallbackScanResult(struct IWlanCallback *self, uint32_t event,
59     const struct HdfWifiScanResult *scanResult, const char *ifName)
60 {
61     (void)self;
62     if (scanResult == NULL || ifName == NULL) {
63         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
64         return HDF_ERR_INVALID_PARAM;
65     }
66     HDF_LOGE("HdiProcessScanResult: flags=%{public}d, caps=%{public}d, freq=%{public}d, beaconInt=%{public}d",
67         scanResult->flags, scanResult->caps, scanResult->freq, scanResult->beaconInt);
68     HDF_LOGE("HdiProcessScanResult: qual=%{public}d, beaconIeLen=%{public}d, level=%{public}d", scanResult->qual,
69         scanResult->beaconIeLen, scanResult->level);
70     HDF_LOGE("HdiProcessScanResult: age=%{public}d, ieLen=%{public}d", scanResult->age, scanResult->ieLen);
71     PrintSsid(scanResult->ie, scanResult->ieLen);
72     return HDF_SUCCESS;
73 }
74 
WlanCallbackScanResults(struct IWlanCallback * self,uint32_t event,const struct HdfWifiScanResults * scanResults,const char * ifName)75 static int32_t WlanCallbackScanResults(struct IWlanCallback *self, uint32_t event,
76     const struct HdfWifiScanResults *scanResults, const char *ifName)
77 {
78     uint32_t i;
79     (void)self;
80     if (scanResults == NULL || ifName == NULL) {
81         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
82         return HDF_ERR_INVALID_PARAM;
83     }
84     HDF_LOGI("%{public}s: Receive %u scan results!", __func__, scanResults->resLen);
85     for (i = 0; i < scanResults->resLen; i++) {
86         struct HdfWifiScanResultExt *scanResult = &scanResults->res[i];
87         HDF_LOGI("HdiProcessScanResult: flags=%{public}d, caps=%{public}d, freq=%{public}d, beaconInt=%{public}d",
88             scanResult->flags, scanResult->caps, scanResult->freq, scanResult->beaconInt);
89         HDF_LOGI("HdiProcessScanResult: qual=%{public}d, beaconIeLen=%{public}d, level=%{public}d", scanResult->qual,
90             scanResult->beaconIeLen, scanResult->level);
91         HDF_LOGI("HdiProcessScanResult: age=%{public}d, ieLen=%{public}d", scanResult->age, scanResult->ieLen);
92         PrintSsid(scanResult->ie, scanResult->ieLen);
93     }
94     return HDF_SUCCESS;
95 }
96 
WlanCallbackNetlinkMessage(struct IWlanCallback * self,const uint8_t * msg,uint32_t msgLen)97 static int32_t WlanCallbackNetlinkMessage(struct IWlanCallback *self, const uint8_t *msg, uint32_t msgLen)
98 {
99     uint32_t i;
100     (void)self;
101     if (msg == NULL) {
102         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
103         return HDF_ERR_INVALID_PARAM;
104     }
105 
106     HDF_LOGI("%{public}s: receive message from netlink", __func__);
107     for (i = 0; i < msgLen; i++) {
108         HDF_LOGI("%02x", msg[i]);
109     }
110     return HDF_SUCCESS;
111 }
112 
WlanCallbackServiceGet(void)113 struct IWlanCallback *WlanCallbackServiceGet(void)
114 {
115     struct WlanCallbackService *service =
116         (struct WlanCallbackService *)OsalMemCalloc(sizeof(struct WlanCallbackService));
117     if (service == NULL) {
118         HDF_LOGE("%{public}s: malloc WlanCallbackService obj failed!", __func__);
119         return NULL;
120     }
121 
122     service->interface.ResetDriverResult = WlanCallbackResetDriver;
123     service->interface.ScanResult = WlanCallbackScanResult;
124     service->interface.WifiNetlinkMessage = WlanCallbackNetlinkMessage;
125     service->interface.ScanResults = WlanCallbackScanResults;
126     return &service->interface;
127 }
128 
WlanCallbackServiceRelease(struct IWlanCallback * instance)129 void WlanCallbackServiceRelease(struct IWlanCallback *instance)
130 {
131     struct WlanCallbackService *service = (struct WlanCallbackService *)instance;
132     if (service == NULL) {
133         return;
134     }
135 
136     OsalMemFree(service);
137 }
138