1 /*
2 * Copyright (c) 2021 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 "wifi_hal_base_feature.h"
17 #include "hdf_base.h"
18 #include "hdf_log.h"
19 #include "securec.h"
20 #include "wifi_hal_cmd.h"
21 #include "wifi_hal_util.h"
22
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28
GetNetworkIfaceNameInner(const struct IWiFiBaseFeature * baseFeature)29 static const char *GetNetworkIfaceNameInner(const struct IWiFiBaseFeature *baseFeature)
30 {
31 if (baseFeature == NULL) {
32 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
33 return NULL;
34 }
35 return baseFeature->ifName;
36 }
37
GetFeatureTypeInner(const struct IWiFiBaseFeature * baseFeature)38 static int32_t GetFeatureTypeInner(const struct IWiFiBaseFeature *baseFeature)
39 {
40 if (baseFeature == NULL) {
41 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
42 return HDF_FAILURE;
43 }
44 return baseFeature->type;
45 }
46
SetMacAddressInner(const struct IWiFiBaseFeature * baseFeature,unsigned char * mac,uint8_t len)47 static int32_t SetMacAddressInner(const struct IWiFiBaseFeature *baseFeature, unsigned char *mac, uint8_t len)
48 {
49 if (baseFeature == NULL || mac == NULL || len != WIFI_MAC_ADDR_LENGTH) {
50 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
51 return HDF_ERR_INVALID_PARAM;
52 }
53 return HalCmdSetMacAddr(baseFeature->ifName, mac, len);
54 }
55
GetDeviceMacAddressInner(const struct IWiFiBaseFeature * baseFeature,unsigned char * mac,uint8_t len)56 static int32_t GetDeviceMacAddressInner(const struct IWiFiBaseFeature *baseFeature, unsigned char *mac, uint8_t len)
57 {
58 if (baseFeature == NULL || mac == NULL || len != WIFI_MAC_ADDR_LENGTH) {
59 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
60 return HDF_ERR_INVALID_PARAM;
61 }
62 return HalCmdGetDevMacAddr(baseFeature->ifName, baseFeature->type, mac, len);
63 }
64
GetValidFreqsWithBandInner(const struct IWiFiBaseFeature * baseFeature,int32_t band,int32_t * freqs,uint32_t size,uint32_t * num)65 static int32_t GetValidFreqsWithBandInner(const struct IWiFiBaseFeature *baseFeature,
66 int32_t band, int32_t *freqs, uint32_t size, uint32_t *num)
67 {
68 if (baseFeature == NULL || freqs == NULL || size < MAX_CHANNEL_NUM || num == NULL) {
69 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
70 return HDF_ERR_INVALID_PARAM;
71 }
72 return HalCmdGetValidFreqWithBand(baseFeature->ifName, band, freqs, size, num);
73 }
74
SetTxPowerInner(const struct IWiFiBaseFeature * baseFeature,int32_t power)75 static int32_t SetTxPowerInner(const struct IWiFiBaseFeature *baseFeature, int32_t power)
76 {
77 if (baseFeature == NULL || power <= 0) {
78 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
79 return HDF_ERR_INVALID_PARAM;
80 }
81 return HalCmdSetTxPower(baseFeature->ifName, power);
82 }
83
GetChipIdInner(const struct IWiFiBaseFeature * baseFeature,uint8_t * chipId)84 static int32_t GetChipIdInner(const struct IWiFiBaseFeature *baseFeature, uint8_t *chipId)
85 {
86 if (baseFeature == NULL || chipId == NULL) {
87 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
88 return HDF_ERR_INVALID_PARAM;
89 }
90 return HalCmdGetChipId(baseFeature->ifName, chipId);
91 }
92
GetIfNamesByChipIdInner(const uint8_t chipId,char ** ifNames,uint32_t * num)93 static int32_t GetIfNamesByChipIdInner(const uint8_t chipId, char **ifNames, uint32_t *num)
94 {
95 if (ifNames == NULL || num == NULL) {
96 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
97 return HDF_ERR_INVALID_PARAM;
98 }
99 return HalCmdGetIfNamesByChipId(chipId, ifNames, num);
100 }
101
GetNetworkIfaceName(const struct IWiFiBaseFeature * baseFeature)102 static const char *GetNetworkIfaceName(const struct IWiFiBaseFeature *baseFeature)
103 {
104 HalMutexLock();
105 const char *ifName = GetNetworkIfaceNameInner(baseFeature);
106 HalMutexUnlock();
107 return ifName;
108 }
109
GetFeatureType(const struct IWiFiBaseFeature * baseFeature)110 static int32_t GetFeatureType(const struct IWiFiBaseFeature *baseFeature)
111 {
112 HalMutexLock();
113 int32_t type = GetFeatureTypeInner(baseFeature);
114 HalMutexUnlock();
115 return type;
116 }
117
SetMacAddress(const struct IWiFiBaseFeature * baseFeature,unsigned char * mac,uint8_t len)118 static int32_t SetMacAddress(const struct IWiFiBaseFeature *baseFeature, unsigned char *mac, uint8_t len)
119 {
120 HalMutexLock();
121 int32_t ret = SetMacAddressInner(baseFeature, mac, len);
122 HalMutexUnlock();
123 return ret;
124 }
125
GetDeviceMacAddress(const struct IWiFiBaseFeature * baseFeature,unsigned char * mac,uint8_t len)126 static int32_t GetDeviceMacAddress(const struct IWiFiBaseFeature *baseFeature, unsigned char *mac, uint8_t len)
127 {
128 HalMutexLock();
129 int32_t ret = GetDeviceMacAddressInner(baseFeature, mac, len);
130 HalMutexUnlock();
131 return ret;
132 }
133
GetValidFreqsWithBand(const struct IWiFiBaseFeature * baseFeature,int32_t band,int32_t * freqs,uint32_t size,uint32_t * num)134 static int32_t GetValidFreqsWithBand(const struct IWiFiBaseFeature *baseFeature,
135 int32_t band, int32_t *freqs, uint32_t size, uint32_t *num)
136 {
137 HalMutexLock();
138 int32_t ret = GetValidFreqsWithBandInner(baseFeature, band, freqs, size, num);
139 HalMutexUnlock();
140 return ret;
141 }
142
HalSetTxPower(const struct IWiFiBaseFeature * baseFeature,int32_t power)143 static int32_t HalSetTxPower(const struct IWiFiBaseFeature *baseFeature, int32_t power)
144 {
145 HalMutexLock();
146 int32_t ret = SetTxPowerInner(baseFeature, power);
147 HalMutexUnlock();
148 return ret;
149 }
150
HalGetChipId(const struct IWiFiBaseFeature * baseFeature,uint8_t * chipId)151 static int32_t HalGetChipId(const struct IWiFiBaseFeature *baseFeature, uint8_t *chipId)
152 {
153 HalMutexLock();
154 int32_t ret = GetChipIdInner(baseFeature, chipId);
155 HalMutexUnlock();
156 return ret;
157 }
158
HalGetIfNamesByChipId(const uint8_t chipId,char ** ifNames,uint32_t * num)159 static int32_t HalGetIfNamesByChipId(const uint8_t chipId, char **ifNames, uint32_t *num)
160 {
161 HalMutexLock();
162 int32_t ret = GetIfNamesByChipIdInner(chipId, ifNames, num);
163 HalMutexUnlock();
164 return ret;
165 }
166
InitBaseFeature(struct IWiFiBaseFeature ** fe)167 int32_t InitBaseFeature(struct IWiFiBaseFeature **fe)
168 {
169 if (fe == NULL || *fe == NULL) {
170 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
171 return HDF_ERR_INVALID_PARAM;
172 }
173 (*fe)->getNetworkIfaceName = GetNetworkIfaceName;
174 (*fe)->getFeatureType = GetFeatureType;
175 (*fe)->setMacAddress = SetMacAddress;
176 (*fe)->getDeviceMacAddress = GetDeviceMacAddress;
177 (*fe)->getValidFreqsWithBand = GetValidFreqsWithBand;
178 (*fe)->setTxPower = HalSetTxPower;
179 (*fe)->getChipId = HalGetChipId;
180 (*fe)->getIfNamesByChipId = HalGetIfNamesByChipId;
181 return HDF_SUCCESS;
182 }
183
184
185 #ifdef __cplusplus
186 #if __cplusplus
187 }
188 #endif
189 #endif