1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_wifi_product.h"
10 #include "hdf_wlan_chipdriver_manager.h"
11 #include "hdf_wlan_utils.h"
12 #include "securec.h"
13 
14 #define HDF_LOG_TAG HDF_WIFI_CORE
15 #define MAX_WLAN_DEVICE 3
16 /**
17  * @brief Defines the Product Data.
18  *
19  * @since 1.0
20  */
21 struct HdfWifiProductData {
22     char state;                     /* *< WLAN module state */
23     struct WifiModule module;       /* *< Structure of the WLAN module */
24     struct HdfDeviceObject *device; /* *< Structure of the Device Object */
25     struct HdfWlanDevice *wlanDevice[MAX_WLAN_DEVICE];
26 };
27 
28 static struct HdfWifiProductData *g_hdfWlanProductData = NULL;
29 
HdfWlanAddDevice(struct HdfWlanDevice * device)30 int HdfWlanAddDevice(struct HdfWlanDevice *device)
31 {
32     uint8_t i;
33     if (device == NULL) {
34         HDF_LOGE("%s:input is NULL!", __func__);
35         return HDF_FAILURE;
36     }
37     if (g_hdfWlanProductData == NULL) {
38         HDF_LOGE("%s:please Init product first!", __func__);
39         return HDF_FAILURE;
40     }
41     for (i = 0; i < MAX_WLAN_DEVICE; i++) {
42         if (g_hdfWlanProductData->wlanDevice[i] == NULL) {
43             g_hdfWlanProductData->wlanDevice[i] = device;
44             device->id = i;
45             HDF_LOGI("%s: HdfWlanAddDevice successful!", __func__);
46             return HDF_SUCCESS;
47         }
48     }
49     HDF_LOGE("%s: device list is full!", __func__);
50     return HDF_FAILURE;
51 }
52 
HdfWlanInitProduct(struct HdfDeviceObject * device,const struct HdfConfigWlanModuleConfig * config)53 int HdfWlanInitProduct(struct HdfDeviceObject *device, const struct HdfConfigWlanModuleConfig *config)
54 {
55     int ret;
56     if (g_hdfWlanProductData != NULL) {
57         HDF_LOGE("%s:already inited!", __func__);
58         return HDF_FAILURE;
59     }
60     g_hdfWlanProductData = OsalMemCalloc(sizeof(struct HdfWifiProductData));
61     if (g_hdfWlanProductData == NULL) {
62         HDF_LOGE("%s:oom", __func__);
63         return HDF_FAILURE;
64     }
65     ret = InitWifiModule(&(g_hdfWlanProductData->module), config);
66     if (ret != HDF_SUCCESS) {
67         HDF_LOGE("%s:InitWifiModule failed! ret=%d", __func__, ret);
68         OsalMemFree(g_hdfWlanProductData);
69         g_hdfWlanProductData = NULL;
70         return ret;
71     }
72     g_hdfWlanProductData->device = device;
73     HDF_LOGI("%s: HdfWlanInitProduct successful!", __func__);
74     return HDF_SUCCESS;
75 }
76 
HdfWlanSendBroadcastEvent(uint32_t id,const struct HdfSBuf * data)77 int HdfWlanSendBroadcastEvent(uint32_t id, const struct HdfSBuf *data)
78 {
79     if (g_hdfWlanProductData == NULL) {
80         return HDF_FAILURE;
81     }
82     return HdfDeviceSendEvent(g_hdfWlanProductData->device, id, data);
83 }
84 
HdfWlanGetModule(void)85 struct WifiModule *HdfWlanGetModule(void)
86 {
87     if (g_hdfWlanProductData == NULL) {
88         return NULL;
89     }
90     return &g_hdfWlanProductData->module;
91 }
92 
HdfWlanGetDevice(void)93 struct HdfDeviceObject *HdfWlanGetDevice(void)
94 {
95     if (g_hdfWlanProductData == NULL) {
96         return NULL;
97     }
98     return g_hdfWlanProductData->device;
99 }
100 
HdfWlanGetWlanDevice(uint8_t chipId)101 struct HdfWlanDevice *HdfWlanGetWlanDevice(uint8_t chipId)
102 {
103     if (chipId >= MAX_WLAN_DEVICE || g_hdfWlanProductData == NULL) {
104         HDF_LOGD("%s: HdfWlanGetWlanDevice is null!", __func__);
105         return NULL;
106     }
107     return g_hdfWlanProductData->wlanDevice[chipId];
108 }
109 
HdfWlanDeinitProduct(void)110 void HdfWlanDeinitProduct(void)
111 {
112     if (g_hdfWlanProductData != NULL) {
113         OsalMemFree(g_hdfWlanProductData);
114         g_hdfWlanProductData = NULL;
115     }
116 }
117