1 /* 2 * Copyright (C) Huawei Technologies Co., Ltd. 2024. All rights reserved. 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 #ifndef __WIFI_HAL_COMMON_H__ 17 #define __WIFI_HAL_COMMON_H__ 18 19 #include "wifi_hal.h" 20 #include <linux/nl80211.h> 21 #include "sync.h" 22 #include <hdf_log.h> 23 #include <map> 24 #include <mutex> 25 #include <shared_mutex> 26 27 #define SOCKET_BUFFER_SIZE (32768U) 28 constexpr int32_t DEFAULT_EVENT_CB_SIZE = 64; 29 constexpr int32_t DEFAULT_CMD_SIZE = 64; 30 31 const uint32_t HAL_OUI = 0x001A11; 32 const uint32_t BRCM_OUI = 0x001018; 33 const uint32_t OUI_QCA = 0x001374; 34 35 typedef int (*nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg); 36 typedef enum { 37 /* don't use 0 as a valid subcommand */ 38 VENDOR_NL80211_SUBCMD_UNSPECIFIED, 39 VENDOR_NL80211_SUBCMD_RANGE_START = 0x0001, 40 VENDOR_NL80211_SUBCMD_RANGE_END = 0x0FFF, 41 NL80211_SUBCMD_GSCAN_RANGE_START = 0x1000, 42 NL80211_SUBCMD_GSCAN_RANGE_END = 0x10FF, 43 NL80211_SUBCMD_NBD_RANGE_START = 0x1100, 44 NL80211_SUBCMD_NBD_RANGE_END = 0x11FF, 45 NL80211_SUBCMD_RTT_RANGE_START = 0x1100, 46 NL80211_SUBCMD_RTT_RANGE_END = 0x11FF, 47 NL80211_SUBCMD_LSTATS_RANGE_START = 0x1200, 48 NL80211_SUBCMD_LSTATS_RANGE_END = 0x12FF, 49 NL80211_SUBCMD_DEBUG_RANGE_START = 0x1400, 50 NL80211_SUBCMD_DEBUG_RANGE_END = 0x14FF, 51 NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START = 0x1600, 52 NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_END = 0x16FF, 53 NL80211_SUBCMD_NAN_RANGE_START = 0x1700, 54 NL80211_SUBCMD_NAN_RANGE_END = 0x17FF, 55 NL80211_SUBCMD_PKT_FILTER_RANGE_START = 0x1800, 56 NL80211_SUBCMD_PKT_FILTER_RANGE_END = 0x18FF, 57 NL80211_SUBCMD_TX_POWER_RANGE_START = 0x1900, 58 NL80211_SUBCMD_TX_POWER_RANGE_END = 0x1910, 59 NL80211_SUBCMD_MITIGATION_RANGE_START = 0x1920, 60 NL80211_SUBCMD_MITIGATION_RANGE_END = 0x192F, 61 NL80211_SUBCMD_DSCP_RANGE_START = 0x2000, 62 NL80211_SUBCMD_DSCP_RANGE_END = 0x20FF, 63 NL80211_SUBCMD_CHAVOID_RANGE_START = 0x2100, 64 NL80211_SUBCMD_CHAVOID_RANGE_END = 0x211F, 65 NL80211_SUBCMD_OTA_DOWNLOAD_START = 0x2120, 66 NL80211_SUBCMD_OTA_DOWNLOAD_END = 0x212F, 67 NL80211_SUBCMD_VIOP_MODE_START = 0x2130, 68 NL80211_SUBCMD_VIOP_MODE_END = 0x213F, 69 NL80211_SUBCMD_TWT_START = 0x2140, 70 NL80211_SUBCMD_TWT_END = 0x214F, 71 NL80211_SUBCMD_USABLE_CHANNEL_START = 0x2150, 72 NL80211_SUBCMD_USABLE_CHANNEL_END = 0x215F, 73 NL80211_SUBCMD_INIT_DEINIT_RANGE_START = 0x2160, 74 NL80211_SUBCMD_INIT_DEINIT_RANGE_END = 0x216F, 75 }VENDOR_SUB_COMMAND; 76 77 typedef enum { 78 GSCAN_SUBCMD_GET_CAPABILITIES = NL80211_SUBCMD_GSCAN_RANGE_START, 79 GSCAN_SUBCMD_SET_CONFIG, 80 GSCAN_SUBCMD_SET_SCAN_CONFIG, 81 GSCAN_SUBCMD_ENABLE_GSCAN, 82 GSCAN_SUBCMD_GET_SCAN_RESULTS, 83 GSCAN_SUBCMD_SCAN_RESULTS, 84 GSCAN_SUBCMD_SET_HOTLIST, 85 GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG, 86 GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS, 87 GSCAN_SUBCMD_GET_CHANNEL_LIST, 88 WIFI_SUBCMD_GET_FEATURE_SET, 89 WIFI_SUBCMD_GET_FEATURE_SET_MATRIX, 90 WIFI_SUBCMD_SET_PNO_RANDOM_MAC_OUI, 91 WIFI_SUBCMD_NODFS_SET, 92 WIFI_SUBCMD_SET_COUNTRY_CODE, 93 GSCAN_SUBCMD_SET_EPNO_SSID, 94 WIFI_SUBCMD_SET_SSID_WHITE_LIST, 95 WIFI_SUBCMD_SET_ROAM_PARAMS, 96 WIFI_SUBCMD_ENABLE_LAZY_ROAM, 97 WIFI_SUBCMD_SET_BSSID_PREF, 98 WIFI_SUBCMD_SET_BSSID_AVOID, 99 GSCAN_SUBCMD_ANQPO_CONFIG, 100 WIFI_SUBCMD_SET_RSSI_MONITOR, 101 WIFI_SUBCMD_CONFIG_ND_OFFLOAD, 102 WIFI_SUBCMD_CONFIG_TCPACK_SUP, 103 WIFI_SUBCMD_FW_ROAM_POLICY, 104 WIFI_SUBCMD_ROAM_CAPABILITY, 105 WIFI_SUBCMD_SET_LATENCY_MODE, 106 WIFI_SUBCMD_SET_MULTISTA_PRIMARY_CONNECTION, 107 WIFI_SUBCMD_SET_MULTISTA_USE_CASE, 108 WIFI_SUBCMD_SET_DTIM_CONFIG, 109 GSCAN_SUBCMD_MAX, 110 NAN_SUBCMD_ENABLE = NL80211_SUBCMD_NAN_RANGE_START, 111 NAN_SUBCMD_DISABLE, 112 NAN_SUBCMD_PUBLISH, 113 NAN_SUBCMD_SUBSCRIBE, 114 NAN_SUBCMD_PUBLISH_CANCEL, 115 NAN_SUBCMD_SUBSCRIBE_CANCEL, 116 NAN_SUBCMD_TRANSMIT_FOLLOWUP, 117 NAN_SUBCMD_CONFIG, 118 NAN_SUBCMD_TCA, 119 NAN_SUBCMD_STATS, 120 NAN_SUBCMD_GET_CAPABILITIES, 121 NAN_SUBCMD_DATA_PATH_IFACE_CREATE, 122 NAN_SUBCMD_DATA_PATH_IFACE_DELETE, 123 NAN_SUBCMD_DATA_PATH_REQUEST, 124 NAN_SUBCMD_DATA_PATH_RESPONSE, 125 NAN_SUBCMD_DATA_PATH_END, 126 NAN_SUBCMD_DATA_PATH_SEC_INFO, 127 NAN_SUBCMD_VERSION_INFO, 128 NAN_SUBCMD_ENABLE_MERGE, 129 APF_SUBCMD_GET_CAPABILITIES = NL80211_SUBCMD_PKT_FILTER_RANGE_START, 130 APF_SUBCMD_SET_FILTER, 131 APF_SUBCMD_READ_FILTER, 132 WIFI_SUBCMD_TX_POWER_SCENARIO = NL80211_SUBCMD_TX_POWER_RANGE_START, 133 WIFI_SUBCMD_THERMAL_MITIGATION = NL80211_SUBCMD_MITIGATION_RANGE_START, 134 DSCP_SUBCMD_SET_TABLE = NL80211_SUBCMD_DSCP_RANGE_START, 135 DSCP_SUBCMD_RESET_TABLE, 136 CHAVOID_SUBCMD_SET_CONFIG = NL80211_SUBCMD_CHAVOID_RANGE_START, 137 TWT_SUBCMD_GETCAPABILITY = NL80211_SUBCMD_TWT_START, 138 TWT_SUBCMD_SETUP_REQUEST, 139 TWT_SUBCMD_TEAR_DOWN_REQUEST, 140 TWT_SUBCMD_INFO_FRAME_REQUEST, 141 TWT_SUBCMD_GETSTATS, 142 TWT_SUBCMD_CLR_STATS, 143 WIFI_SUBCMD_CONFIG_VOIP_MODE = NL80211_SUBCMD_VIOP_MODE_START, 144 WIFI_SUBCMD_GET_OTA_CURRUNT_INFO = NL80211_SUBCMD_OTA_DOWNLOAD_START, 145 WIFI_SUBCMD_OTA_UPDATE, 146 WIFI_SUBCMD_USABLE_CHANNEL = NL80211_SUBCMD_USABLE_CHANNEL_START, 147 WIFI_SUBCMD_TRIGGER_SSR = NL80211_SUBCMD_INIT_DEINIT_RANGE_START, 148 } WIFI_SUB_COMMAND; 149 150 typedef enum { 151 BRCM_RESERVED1 = 0, 152 BRCM_RESERVED2 = 1, 153 GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS = 2, 154 GSCAN_EVENT_HOTLIST_RESULTS_FOUND = 3, 155 GSCAN_EVENT_SCAN_RESULTS_AVAILABLE = 4, 156 GSCAN_EVENT_FULL_SCAN_RESULTS = 5, 157 RTT_EVENT_COMPLETE = 6, 158 GSCAN_EVENT_COMPLETE_SCAN = 7, 159 GSCAN_EVENT_HOTLIST_RESULTS_LOST = 8, 160 GSCAN_EVENT_EPNO_EVENT = 9, 161 GSCAN_DEBUG_RING_EVENT = 10, 162 GSCAN_DEBUG_MEM_DUMP_EVENT = 11, 163 GSCAN_EVENT_ANQPO_HOTSPOT_MATCH = 12, 164 GSCAN_RSSI_MONITOR_EVENT = 13, 165 GSCAN_MKEEP_ALIVE = 14, 166 NAN_EVENT_ENABLED = 15, 167 NAN_EVENT_DISABLED = 16, 168 NAN_EVENT_SUBSCRIBE_MATCH = 17, 169 NAN_EVENT_PUBLISH_REPLIED_IND = 18, 170 NAN_EVENT_PUBLISH_TERMINATED = 19, 171 NAN_EVENT_SUBSCRIBE_TERMINATED = 20, 172 NAN_EVENT_DE_EVENT = 21, 173 NAN_EVENT_FOLLOWUP = 22, 174 NAN_EVENT_TRANSMIT_FOLLOWUP_IND = 23, 175 NAN_EVENT_DATA_REQUEST = 24, 176 NAN_EVENT_DATA_CONFIRMATION = 25, 177 NAN_EVENT_DATA_END = 26, 178 NAN_EVENT_BEACON = 27, 179 NAN_EVENT_SDF = 28, 180 NAN_EVENT_TCA = 29, 181 NAN_EVENT_SUBSCRIBE_UNMATCH = 30, 182 NAN_EVENT_UNKNOWN = 31, 183 NAN_EVENT_RESPONSE = 32, 184 BRCM_VENDOR_EVENT_HANGED = 33, 185 ROAM_EVENT_START, 186 GSCAN_FILE_DUMP_EVENT = 37, 187 NAN_ASYNC_RESPONSE_DISABLED = 40, 188 BRCM_IDSUP_STATUS = 42, 189 BRCM_VENDOR_EVENT_TWT = 43, 190 BRCM_TPUT_DUMP_EVENT = 44, 191 NAN_EVENT_MATCH_EXPIRY = 45 192 } WIFI_EVENT; 193 194 typedef void (*WifiInternalEventHandler) (wifiHandle handle, int events); 195 196 class WifiCommand; 197 198 typedef struct { 199 int nlCmd; 200 uint32_t vendorId; 201 int vendorSubcmd; 202 nl_recvmsg_msg_cb_t cbFunc; 203 void *cbArg; 204 } CbInfo; 205 206 typedef struct { 207 int id; 208 WifiCommand *cmd; 209 } CmdInfo; 210 211 typedef struct { 212 wifiHandle handle; 213 char name[IFNAMSIZ + 1]; 214 int id; 215 bool isVirtual; 216 } InterfaceInfo; 217 218 typedef struct { 219 struct nl_sock *cmdSock; 220 struct nl_sock *eventSock; 221 int nl80211FamilyId; 222 int cleanupSocks[2]; 223 bool inEventLoop; 224 bool cleanUp; 225 WifiInternalEventHandler eventHandler; 226 VendorHalExitHandler CleanedUpHandler; 227 CbInfo *eventCb; 228 int numEventCb; 229 int allocEventCb; 230 pthread_mutex_t cbLock; 231 CmdInfo *cmd; 232 int numCmd; 233 int allocCmd; 234 InterfaceInfo **interfaces; 235 int numInterfaces; 236 int maxNumInterfaces; 237 VendorHalRestartHandler restartHandler; 238 WifiCallbackHandler ifaceCallBack; 239 } HalInfo; 240 241 WifiError WifiRegisterHandler(wifiHandle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg); 242 WifiError WifiRegisterVendorHandler(wifiHandle handle, 243 uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg); 244 void WifiUnregisterHandler(wifiHandle handle, int cmd); 245 void WifiUnregisterVendorHandlerWithoutLock(wifiHandle handle, uint32_t id, int subcmd); 246 void WifiUnregisterVendorHandler(wifiHandle handle, uint32_t id, int subcmd); 247 WifiError WifiRegisterCmd(wifiHandle handle, int id, WifiCommand *cmd); 248 WifiCommand *WifiUnregisterCmd(wifiHandle handle, int id); 249 void WifiUnregisterCmd(wifiHandle handle, WifiCommand *cmd); 250 InterfaceInfo *GetIfaceInfo(wifiInterfaceHandle); 251 wifiHandle GetWifiHandle(wifiInterfaceHandle handle); 252 HalInfo *GetHalInfo(wifiHandle handle); 253 HalInfo *GetHalInfo(wifiInterfaceHandle handle); 254 wifiHandle GetWifiHandle(HalInfo *info); 255 wifiInterfaceHandle GetIfaceHandle(InterfaceInfo *info); 256 wifiInterfaceHandle WifiGetWlanInterface(wifiHandle info, 257 wifiInterfaceHandle *ifaceHandles, int numIfaceHandles); 258 void SetHautilMode(bool halutilMode); 259 bool GetGHalutilMode(); 260 WifiError TriggerVendorHalRestart(wifiHandle handle); 261 std::shared_lock<std::shared_mutex> ReadLockData(); 262 std::unique_lock<std::shared_mutex> WriteLock(); 263 264 #define MIN(x, y) ((x) < (y) ? (x) : (y)) 265 #define MAX(x, y) ((x) > (y) ? (x) : (y)) 266 267 #define NULL_CHECK_RETURN(ptr, str, ret) \ 268 do { \ 269 if (!(ptr)) { \ 270 HDF_LOGE("%s(): null pointer - #ptr (%s)\n", __FUNCTION__, str); \ 271 return ret; \ 272 } \ 273 } while (0) 274 275 #endif 276