• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. 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 /*****************************************************************************
17 *
18 * File Name : wm_wifi.c
19 *
20 * Description: wm wifi
21 *
22 * Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd.
23 * All rights reserved.
24 *
25 * Author :
26 *
27 * Date : 2022-3-10
28 *****************************************************************************/
29 
30 #include <string.h>
31 #include "securec.h"
32 #include "wm_params.h"
33 #include "wm_mem.h"
34 #include "wm_ram_config.h"
35 #include "wm_debug.h"
36 #include "wm_wifi.h"
37 
38 static struct tls_wifi_netif_status_event wifi_netif_status_event;
39 static u8 wifi_inited_ok = 0;
wifi_status_changed(u8 status)40 static void wifi_status_changed(u8 status)
41 {
42     struct tls_wifi_netif_status_event *status_event;
43 
44     dl_list_for_each(status_event, &wifi_netif_status_event.list, struct tls_wifi_netif_status_event, list)
45     {
46         if (status_event->status_callback != NULL)
47         {
48             switch (status)
49             {
50                 case WIFI_JOIN_SUCCESS:
51                     status_event->status_callback(NETIF_WIFI_JOIN_SUCCESS);
52                     break;
53                 case WIFI_JOIN_FAILED:
54                     status_event->status_callback(NETIF_WIFI_JOIN_FAILED);
55                     break;
56                 case WIFI_DISCONNECTED:
57                     status_event->status_callback(NETIF_WIFI_DISCONNECTED);
58                     break;
59 #if TLS_CONFIG_AP
60                 case WIFI_SOFTAP_SUCCESS:
61                     status_event->status_callback(NETIF_WIFI_SOFTAP_SUCCESS);
62                     break;
63                 case WIFI_SOFTAP_FAILED:
64                     status_event->status_callback(NETIF_WIFI_SOFTAP_FAILED);
65                     break;
66                 case WIFI_SOFTAP_CLOSED:
67                     status_event->status_callback(NETIF_WIFI_SOFTAP_CLOSED);
68                     break;
69 #endif
70                 default:
71                     break;
72             }
73         }
74     }
75 }
76 
77 /**
78  * @brief          This function is used to initialize wifi
79  *
80  * @param[in]      None
81  *
82  * @return         0-success
83  *
84  * @note           None
85  */
tls_wifi_init(void)86 int tls_wifi_init(void)
87 {
88     extern u8 tx_gain_group[];
89     u8 mac_addr[6] = {0x00, 0x25, 0x08, 0x09, 0x01, 0x0F};
90     if (wifi_inited_ok == 1) {
91         return 0;
92     }
93 
94     tls_get_tx_gain(&tx_gain_group[0]);
95     if (tls_wifi_mem_cfg(WIFI_MEM_START_ADDR, 5, 3)) { /* wifi tx&rx mem customized interface */ // 5:txcnt, 3:rxcnt
96         TLS_DBGPRT_INFO("wl mem initial failured\n");
97         return -1;
98     }
99 
100     tls_get_mac_addr(&mac_addr[0]);
101     TLS_DBGPRT_INFO("%02x%02x%02x%02x%02x%02x\n", mac_addr[0], mac_addr[1], mac_addr[2], // 2:array element
102         mac_addr[3], mac_addr[4], mac_addr[5]); // 3:array element, 4:array element, 5:array element
103     if (tls_wl_init(NULL, &mac_addr[0], NULL) == NULL) {
104         TLS_DBGPRT_INFO("wl driver initial failured\n");
105         return -1;
106     }
107     if (wpa_supplicant_init(mac_addr)) {
108         TLS_DBGPRT_INFO("supplicant initial failured\n");
109         return -1;
110     }
111     tls_wifi_status_change_cb_register(wifi_status_changed);
112     wifi_inited_ok = 1;
113     return 0;
114 }
115 
116 /**
117  * @brief           This function is used to initialize wifi netif event list
118  *
119  * @param[in]       None
120  *
121  * @return           0-success
122  *
123  * @note           None
124  */
tls_wifi_netif_event_init(void)125 void tls_wifi_netif_event_init(void)
126 {
127     dl_list_init(&wifi_netif_status_event.list);
128 }
129 
130 /**
131  * @brief          This function is used to add wifi event function
132  *
133  * @param[in]      None
134  *
135  * @return         0-success
136  *
137  * @note           None
138  */
tls_wifi_netif_add_status_event(tls_wifi_netif_status_event_fn event_fn)139 int tls_wifi_netif_add_status_event(tls_wifi_netif_status_event_fn event_fn)
140 {
141     u32 cpu_sr;
142     struct tls_wifi_netif_status_event *evt;
143     // if exist, remove from event list first.
144     tls_wifi_netif_remove_status_event(event_fn);
145     evt = tls_mem_alloc(sizeof(struct tls_wifi_netif_status_event));
146     if (evt==NULL) {
147         return -1;
148     }
149     memset_s(evt, sizeof(struct tls_wifi_netif_status_event), 0, sizeof(struct tls_wifi_netif_status_event));
150     evt->status_callback = event_fn;
151     cpu_sr = tls_os_set_critical();
152     dl_list_add_tail(&wifi_netif_status_event.list, &evt->list);
153     tls_os_release_critical(cpu_sr);
154 
155     return 0;
156 }
157 
158 /**
159  * @brief          This function is used to remove wifi event function
160  *
161  * @param[in]      None
162  *
163  * @return         0-success
164  *
165  * @note           None
166  */
tls_wifi_netif_remove_status_event(tls_wifi_netif_status_event_fn event_fn)167 int tls_wifi_netif_remove_status_event(tls_wifi_netif_status_event_fn event_fn)
168 {
169     struct tls_wifi_netif_status_event *status_event;
170     bool is_exist = FALSE;
171     u32 cpu_sr;
172     if (dl_list_empty(&wifi_netif_status_event.list))
173         return 0;
174     dl_list_for_each(status_event, &wifi_netif_status_event.list, struct tls_wifi_netif_status_event, list)
175     {
176         if (status_event->status_callback == event_fn) {
177             is_exist = TRUE;
178             break;
179         }
180     }
181     if (is_exist) {
182         cpu_sr = tls_os_set_critical();
183         dl_list_del(&status_event->list);
184         tls_os_release_critical(cpu_sr);
185         tls_mem_free(status_event);
186     }
187     return 0;
188 }