12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775 |
- /*
- * Copyright (c) 2006-2023, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-08-06 tyx the first version
- * 2023-12-12 Evlers add the wlan join scan function
- */
- #include <rthw.h>
- #include <rtthread.h>
- #include <wlan_dev.h>
- #include <wlan_cfg.h>
- #include <wlan_mgnt.h>
- #include <wlan_prot.h>
- #include <wlan_workqueue.h>
- // #define RT_WLAN_MGNT_DEBUG
- #define DBG_TAG "WLAN.mgnt"
- #ifdef RT_WLAN_MGNT_DEBUG
- #define DBG_LVL DBG_LOG
- #else
- #define DBG_LVL DBG_INFO
- #endif /* RT_WLAN_MGNT_DEBUG */
- #include <rtdbg.h>
- #ifdef RT_WLAN_MANAGE_ENABLE
- #ifndef RT_WLAN_DEVICE
- #define RT_WLAN_DEVICE(__device) ((struct rt_wlan_device *)__device)
- #endif
- #define RT_WLAN_LOG_D(_fmt, ...) LOG_D("L:%d "_fmt"", __LINE__, ##__VA_ARGS__)
- #define RT_WLAN_LOG_I(...) LOG_I(__VA_ARGS__)
- #define RT_WLAN_LOG_W(_fmt, ...) LOG_W("F:%s L:%d "_fmt"", __FUNCTION__, __LINE__, ##__VA_ARGS__)
- #define RT_WLAN_LOG_E(_fmt, ...) LOG_E("F:%s L:%d "_fmt"", __FUNCTION__, __LINE__, ##__VA_ARGS__)
- #define STA_DEVICE() (_sta_mgnt.device)
- #define AP_DEVICE() (_ap_mgnt.device)
- #define STAINFO_LOCK() (rt_mutex_take(&sta_info_mutex, RT_WAITING_FOREVER))
- #define STAINFO_UNLOCK() (rt_mutex_release(&sta_info_mutex))
- #define MGNT_LOCK() (rt_mutex_take(&mgnt_mutex, RT_WAITING_FOREVER))
- #define MGNT_UNLOCK() (rt_mutex_release(&mgnt_mutex))
- #define COMPLETE_LOCK() (rt_mutex_take(&complete_mutex, RT_WAITING_FOREVER))
- #define COMPLETE_UNLOCK() (rt_mutex_release(&complete_mutex))
- #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
- #define TIME_STOP() (rt_timer_stop(&reconnect_time))
- #define TIME_START() (rt_timer_start(&reconnect_time))
- static rt_uint32_t id = 0;
- #else
- #define TIME_STOP()
- #define TIME_START()
- #endif
- #if RT_WLAN_EBOX_NUM < 1
- #error "event box num Too few"
- #endif
- struct rt_wlan_mgnt_des
- {
- struct rt_wlan_device *device;
- struct rt_wlan_info info;
- struct rt_wlan_key key;
- rt_uint8_t state;
- rt_uint8_t flags;
- };
- struct rt_wlan_event_desc
- {
- rt_wlan_event_handler handler;
- void *parameter;
- };
- struct rt_wlan_sta_list
- {
- struct rt_wlan_sta_list *next;
- struct rt_wlan_info info;
- };
- struct rt_wlan_sta_des
- {
- int num;
- struct rt_wlan_sta_list *node;
- };
- struct rt_wlan_msg
- {
- rt_int32_t event;
- rt_int32_t len;
- void *buff;
- };
- struct rt_wlan_complete_des
- {
- struct rt_event complete;
- rt_uint32_t event_flag;
- int index;
- };
- static struct rt_mutex mgnt_mutex;
- static struct rt_wlan_mgnt_des _sta_mgnt;
- static struct rt_wlan_mgnt_des _ap_mgnt;
- static struct rt_wlan_sta_des sta_info;
- static struct rt_mutex sta_info_mutex;
- static struct rt_wlan_event_desc event_tab[RT_WLAN_EVT_MAX];
- static struct rt_wlan_complete_des *complete_tab[5];
- static struct rt_mutex complete_mutex;
- #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
- static struct rt_timer reconnect_time;
- #endif
- rt_inline int _sta_is_null(void)
- {
- if (_sta_mgnt.device == RT_NULL)
- {
- return 1;
- }
- return 0;
- }
- rt_inline int _ap_is_null(void)
- {
- if (_ap_mgnt.device == RT_NULL)
- {
- return 1;
- }
- return 0;
- }
- rt_inline rt_bool_t _is_do_connect(void)
- {
- if ((rt_wlan_get_autoreconnect_mode() == RT_FALSE) ||
- (rt_wlan_is_connected() == RT_TRUE) ||
- (_sta_mgnt.state & RT_WLAN_STATE_CONNECTING))
- {
- return RT_FALSE;
- }
- return RT_TRUE;
- }
- #ifdef RT_WLAN_WORK_THREAD_ENABLE
- static void rt_wlan_mgnt_work(void *parameter)
- {
- struct rt_wlan_msg *msg = parameter;
- void *user_parameter;
- rt_wlan_event_handler handler = RT_NULL;
- struct rt_wlan_buff user_buff = { 0 };
- rt_base_t level;
- /* Get user callback */
- if (msg->event < RT_WLAN_EVT_MAX)
- {
- level = rt_hw_interrupt_disable();
- handler = event_tab[msg->event].handler;
- user_parameter = event_tab[msg->event].parameter;
- rt_hw_interrupt_enable(level);
- }
- /* run user callback fun */
- if (handler)
- {
- user_buff.data = msg->buff;
- user_buff.len = msg->len;
- RT_WLAN_LOG_D("wlan work thread run user callback, event:%d", msg->event);
- handler(msg->event, &user_buff, user_parameter);
- }
- switch (msg->event)
- {
- case RT_WLAN_EVT_STA_CONNECTED:
- {
- struct rt_wlan_cfg_info cfg_info;
- rt_memset(&cfg_info, 0, sizeof(cfg_info));
- /* save config */
- if (rt_wlan_is_connected() == RT_TRUE)
- {
- rt_enter_critical();
- cfg_info.info = _sta_mgnt.info;
- cfg_info.key = _sta_mgnt.key;
- rt_exit_critical();
- RT_WLAN_LOG_D("run save config! ssid:%s len%d", _sta_mgnt.info.ssid.val, _sta_mgnt.info.ssid.len);
- #ifdef RT_WLAN_CFG_ENABLE
- rt_wlan_cfg_save(&cfg_info);
- #endif
- }
- break;
- }
- default :
- break;
- }
- rt_free(msg);
- }
- static rt_err_t rt_wlan_send_to_thread(rt_wlan_event_t event, void *buff, int len)
- {
- struct rt_wlan_msg *msg;
- RT_WLAN_LOG_D("F:%s is run event:%d", __FUNCTION__, event);
- /* Event packing */
- msg = rt_malloc(sizeof(struct rt_wlan_msg) + len);
- if (msg == RT_NULL)
- {
- RT_WLAN_LOG_E("wlan mgnt send msg err! No memory");
- return -RT_ENOMEM;
- }
- rt_memset(msg, 0, sizeof(struct rt_wlan_msg) + len);
- msg->event = event;
- if (len != 0)
- {
- msg->buff = (void *)&msg[1];
- rt_memcpy(msg->buff, buff, len);
- msg->len = len;
- }
- /* send event to wlan thread */
- if (rt_wlan_workqueue_dowork(rt_wlan_mgnt_work, msg) != RT_EOK)
- {
- rt_free(msg);
- RT_WLAN_LOG_E("wlan mgnt do work fail");
- return -RT_ERROR;
- }
- return RT_EOK;
- }
- #endif
- static rt_err_t rt_wlan_sta_info_add(struct rt_wlan_info *info, int timeout)
- {
- struct rt_wlan_sta_list *sta_list;
- rt_err_t err = RT_EOK;
- if (_ap_is_null() || (info == RT_NULL)) return RT_EOK;
- err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout));
- if (err == RT_EOK)
- {
- /* malloc memory */
- sta_list = rt_malloc(sizeof(struct rt_wlan_sta_list));
- if (sta_list == RT_NULL)
- {
- rt_mutex_release(&sta_info_mutex);
- RT_WLAN_LOG_E("sta list malloc failed!");
- return -RT_ENOMEM;
- }
- sta_list->next = RT_NULL;
- sta_list->info = *info;
- /* Append sta info */
- sta_list->next = sta_info.node;
- sta_info.node = sta_list;
- /* num++ */
- sta_info.num ++;
- rt_mutex_release(&sta_info_mutex);
- RT_WLAN_LOG_I("sta associated mac:%02x:%02x:%02x:%02x:%02x:%02x",
- info->bssid[0], info->bssid[1], info->bssid[2],
- info->bssid[3], info->bssid[4], info->bssid[5]);
- }
- return err;
- }
- static rt_err_t rt_wlan_sta_info_del(struct rt_wlan_info *info, int timeout)
- {
- struct rt_wlan_sta_list *sta_list, *sta_prve;
- rt_err_t err = RT_EOK;
- if (_ap_is_null() || (info == RT_NULL)) return RT_EOK;
- err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout));
- if (err == RT_EOK)
- {
- /* traversing the list */
- for (sta_list = sta_info.node, sta_prve = RT_NULL; sta_list != RT_NULL;
- sta_prve = sta_list, sta_list = sta_list->next)
- {
- /* find mac addr */
- if (rt_memcmp(&sta_list->info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)
- {
- if (sta_prve == RT_NULL)
- {
- sta_info.node = sta_list->next;
- }
- else
- {
- sta_prve->next = sta_list->next;
- }
- sta_info.num --;
- rt_free(sta_list);
- break;
- }
- }
- rt_mutex_release(&sta_info_mutex);
- RT_WLAN_LOG_I("sta exit mac:%02x:%02x:%02x:%02x:%02x:%02x",
- info->bssid[0], info->bssid[1], info->bssid[2],
- info->bssid[3], info->bssid[4], info->bssid[5]);
- }
- return err;
- }
- static rt_err_t rt_wlan_sta_info_del_all(int timeout)
- {
- struct rt_wlan_sta_list *sta_list, *sta_next;
- rt_err_t err = RT_EOK;
- err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout));
- if (err == RT_EOK)
- {
- /* traversing the list */
- for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_next)
- {
- sta_next = sta_list->next;
- sta_info.num --;
- rt_free(sta_list);
- }
- rt_mutex_release(&sta_info_mutex);
- }
- if (sta_info.num != 0)
- {
- RT_WLAN_LOG_W("\n\n!!!Program runing exception!!!\n\n");
- }
- sta_info.num = 0;
- sta_info.node = RT_NULL;
- return err;
- }
- #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
- static void rt_wlan_auto_connect_run(struct rt_work *work, void *parameter)
- {
- struct rt_wlan_cfg_info cfg_info;
- char *password = RT_NULL;
- rt_base_t level;
- RT_WLAN_LOG_D("F:%s is run", __FUNCTION__);
- if (rt_mutex_take(&mgnt_mutex, 0) != RT_EOK)
- goto exit;
- /* auto connect status is disable or wifi is connect or connecting, exit */
- if (_is_do_connect() == RT_FALSE)
- {
- id = 0;
- RT_WLAN_LOG_D("not connection");
- goto exit;
- }
- /* Read the next configuration */
- rt_memset(&cfg_info, 0, sizeof(struct rt_wlan_cfg_info));
- if (rt_wlan_cfg_read_index(&cfg_info, id ++) == 0)
- {
- RT_WLAN_LOG_D("read cfg fail");
- id = 0;
- goto exit;
- }
- if (id >= rt_wlan_cfg_get_num()) id = 0;
- if ((cfg_info.key.len > 0) && (cfg_info.key.len <= RT_WLAN_PASSWORD_MAX_LENGTH))
- {
- cfg_info.key.val[cfg_info.key.len] = '\0';
- password = (char *)(&cfg_info.key.val[0]);
- }
- rt_wlan_connect((char *)cfg_info.info.ssid.val, password);
- exit:
- rt_mutex_release(&mgnt_mutex);
- level = rt_hw_interrupt_disable();
- rt_memset(work, 0, sizeof(struct rt_work));
- rt_hw_interrupt_enable(level);
- }
- static void rt_wlan_cyclic_check(void *parameter)
- {
- static struct rt_work work;
- rt_base_t level;
- if ((_is_do_connect() == RT_TRUE) && (work.work_func == RT_NULL))
- {
- level = rt_hw_interrupt_disable();
- rt_work_init(&work, rt_wlan_auto_connect_run, RT_NULL);
- rt_hw_interrupt_enable(level);
- if(rt_work_submit(&work,RT_TICK_PER_SECOND) != RT_EOK)
- {
- level = rt_hw_interrupt_disable();
- rt_memset(&work, 0, sizeof(struct rt_work));
- rt_hw_interrupt_enable(level);
- }
- }
- }
- #endif
- static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter)
- {
- rt_err_t err = RT_NULL;
- rt_wlan_event_t user_event = RT_WLAN_EVT_MAX;
- int i;
- struct rt_wlan_buff user_buff = { 0 };
- if (buff)
- {
- user_buff = *buff;
- }
- /* Event Handle */
- switch (event)
- {
- case RT_WLAN_DEV_EVT_CONNECT:
- {
- RT_WLAN_LOG_D("event: CONNECT");
- #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
- id = 0;
- #endif
- _sta_mgnt.state |= RT_WLAN_STATE_CONNECT;
- _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
- user_event = RT_WLAN_EVT_STA_CONNECTED;
- TIME_STOP();
- user_buff.data = &_sta_mgnt.info;
- user_buff.len = sizeof(struct rt_wlan_info);
- RT_WLAN_LOG_I("wifi connect success ssid:%s", &_sta_mgnt.info.ssid.val[0]);
- #ifdef RT_WLAN_CFG_ENABLE
- {
- struct rt_wlan_cfg_info cfg_info;
- rt_memset(&cfg_info, 0, sizeof(cfg_info));
- /* save config */
- if (rt_wlan_is_connected() == RT_TRUE)
- {
- rt_enter_critical();
- cfg_info.info = _sta_mgnt.info;
- cfg_info.key = _sta_mgnt.key;
- rt_exit_critical();
- RT_WLAN_LOG_D("run save config! ssid:%s len%d", _sta_mgnt.info.ssid.val, _sta_mgnt.info.ssid.len);
- rt_wlan_cfg_save(&cfg_info);
- }
- }
- #endif
- break;
- }
- case RT_WLAN_DEV_EVT_CONNECT_FAIL:
- {
- RT_WLAN_LOG_D("event: CONNECT_FAIL");
- _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECT;
- _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
- _sta_mgnt.state &= ~RT_WLAN_STATE_READY;
- user_event = RT_WLAN_EVT_STA_CONNECTED_FAIL;
- user_buff.data = &_sta_mgnt.info;
- user_buff.len = sizeof(struct rt_wlan_info);
- if (rt_wlan_get_autoreconnect_mode())
- {
- TIME_START();
- }
- break;
- }
- case RT_WLAN_DEV_EVT_DISCONNECT:
- {
- RT_WLAN_LOG_D("event: DISCONNECT");
- _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECT;
- _sta_mgnt.state &= ~RT_WLAN_STATE_READY;
- user_event = RT_WLAN_EVT_STA_DISCONNECTED;
- user_buff.data = &_sta_mgnt.info;
- user_buff.len = sizeof(struct rt_wlan_info);
- if (rt_wlan_get_autoreconnect_mode())
- {
- TIME_START();
- }
- break;
- }
- case RT_WLAN_DEV_EVT_AP_START:
- {
- RT_WLAN_LOG_D("event: AP_START");
- _ap_mgnt.state |= RT_WLAN_STATE_ACTIVE;
- user_event = RT_WLAN_EVT_AP_START;
- user_buff.data = &_ap_mgnt.info;
- user_buff.len = sizeof(struct rt_wlan_info);
- break;
- }
- case RT_WLAN_DEV_EVT_AP_STOP:
- {
- RT_WLAN_LOG_D("event: AP_STOP");
- _ap_mgnt.state &= ~RT_WLAN_STATE_ACTIVE;
- user_event = RT_WLAN_EVT_AP_STOP;
- err = rt_wlan_sta_info_del_all(RT_WAITING_FOREVER);
- if (err != RT_NULL)
- {
- RT_WLAN_LOG_W("AP_STOP event handle fail");
- }
- user_buff.data = &_ap_mgnt.info;
- user_buff.len = sizeof(struct rt_wlan_info);
- break;
- }
- case RT_WLAN_DEV_EVT_AP_ASSOCIATED:
- {
- RT_WLAN_LOG_D("event: ASSOCIATED");
- user_event = RT_WLAN_EVT_AP_ASSOCIATED;
- if (user_buff.len != sizeof(struct rt_wlan_info))
- break;
- err = rt_wlan_sta_info_add(user_buff.data, RT_WAITING_FOREVER);
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_W("AP_ASSOCIATED event handle fail");
- }
- break;
- }
- case RT_WLAN_DEV_EVT_AP_DISASSOCIATED:
- {
- RT_WLAN_LOG_D("event: DISASSOCIATED");
- user_event = RT_WLAN_EVT_AP_DISASSOCIATED;
- if (user_buff.len != sizeof(struct rt_wlan_info))
- break;
- err = rt_wlan_sta_info_del(user_buff.data, RT_WAITING_FOREVER);
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_W("AP_DISASSOCIATED event handle fail");
- }
- break;
- }
- case RT_WLAN_DEV_EVT_AP_ASSOCIATE_FAILED:
- {
- RT_WLAN_LOG_D("event: AP_ASSOCIATE_FAILED");
- break;
- }
- case RT_WLAN_DEV_EVT_SCAN_REPORT:
- {
- RT_WLAN_LOG_D("event: SCAN_REPORT");
- user_event = RT_WLAN_EVT_SCAN_REPORT;
- break;
- }
- case RT_WLAN_DEV_EVT_SCAN_DONE:
- {
- RT_WLAN_LOG_D("event: SCAN_DONE");
- user_event = RT_WLAN_EVT_SCAN_DONE;
- break;
- }
- default :
- {
- RT_WLAN_LOG_D("event: UNKNOWN");
- return;
- }
- }
- /* send event */
- COMPLETE_LOCK();
- for (i = 0; i < sizeof(complete_tab) / sizeof(complete_tab[0]); i++)
- {
- if ((complete_tab[i] != RT_NULL))
- {
- complete_tab[i]->event_flag |= 0x1 << event;
- rt_event_send(&complete_tab[i]->complete, 0x1 << event);
- RT_WLAN_LOG_D("&complete_tab[i]->complete:0x%08x", &complete_tab[i]->complete);
- }
- }
- COMPLETE_UNLOCK();
- #ifdef RT_WLAN_WORK_THREAD_ENABLE
- rt_wlan_send_to_thread(user_event, user_buff.data, user_buff.len);
- #else
- {
- void *user_parameter;
- rt_wlan_event_handler handler = RT_NULL;
- rt_base_t level;
- /* Get user callback */
- if (user_event < RT_WLAN_EVT_MAX)
- {
- level = rt_hw_interrupt_disable();
- handler = event_tab[user_event].handler;
- user_parameter = event_tab[user_event].parameter;
- rt_hw_interrupt_enable(level);
- }
- /* run user callback fun */
- if (handler)
- {
- RT_WLAN_LOG_D("unknown thread run user callback, event:%d", user_event);
- handler(user_event, &user_buff, user_parameter);
- }
- }
- #endif
- }
- static struct rt_wlan_complete_des *rt_wlan_complete_create(const char *name)
- {
- struct rt_wlan_complete_des *complete;
- int i;
- complete = rt_malloc(sizeof(struct rt_wlan_complete_des));
- if (complete == RT_NULL)
- {
- RT_WLAN_LOG_E("complete event create failed");
- MGNT_UNLOCK();
- return complete;
- }
- rt_event_init(&complete->complete, name, RT_IPC_FLAG_FIFO);
- complete->event_flag = 0;
- //protect
- COMPLETE_LOCK();
- for (i = 0; i < sizeof(complete_tab) / sizeof(complete_tab[0]); i++)
- {
- if (complete_tab[i] == RT_NULL)
- {
- complete->index = i;
- complete_tab[i] = complete;
- break;
- }
- }
- COMPLETE_UNLOCK();
- if (i >= sizeof(complete_tab) / sizeof(complete_tab[0]))
- {
- rt_event_detach(&complete->complete);
- rt_free(complete);
- complete = RT_NULL;
- }
- return complete;
- }
- static rt_err_t rt_wlan_complete_wait(struct rt_wlan_complete_des *complete, rt_uint32_t event,
- rt_uint32_t timeout, rt_uint32_t *recved)
- {
- if (complete == RT_NULL)
- {
- return -RT_ERROR;
- }
- /* Check whether there is a waiting event */
- if (complete->event_flag & event)
- {
- *recved = complete->event_flag;
- return RT_EOK;
- }
- else
- {
- return rt_event_recv(&complete->complete, event, RT_EVENT_FLAG_OR,
- rt_tick_from_millisecond(timeout), recved);
- }
- }
- static void rt_wlan_complete_delete(struct rt_wlan_complete_des *complete)
- {
- if (complete == RT_NULL)
- {
- return;
- }
- COMPLETE_LOCK();
- complete_tab[complete->index] = RT_NULL;
- COMPLETE_UNLOCK();
- rt_event_detach(&complete->complete);
- rt_free(complete);
- }
- rt_err_t rt_wlan_set_mode(const char *dev_name, rt_wlan_mode_t mode)
- {
- rt_device_t device = RT_NULL;
- rt_err_t err;
- rt_int8_t up_event_flag = 0;
- rt_wlan_dev_event_handler handler = RT_NULL;
- if ((dev_name == RT_NULL) || (mode >= RT_WLAN_MODE_MAX))
- {
- RT_WLAN_LOG_E("Parameter Wrongful name:%s mode:%d", dev_name, mode);
- return -RT_EINVAL;
- }
- RT_WLAN_LOG_D("%s is run dev_name:%s mode:%s%s%s", __FUNCTION__, dev_name,
- mode == RT_WLAN_NONE ? "NONE" : "",
- mode == RT_WLAN_STATION ? "STA" : "",
- mode == RT_WLAN_AP ? "AP" : ""
- );
- /* find device */
- device = rt_device_find(dev_name);
- if (device == RT_NULL)
- {
- RT_WLAN_LOG_E("not find device, set mode failed! name:%s", dev_name);
- return -RT_EIO;
- }
- MGNT_LOCK();
- if (RT_WLAN_DEVICE(device)->mode == mode)
- {
- RT_WLAN_LOG_D("L:%d this device mode is set");
- MGNT_UNLOCK();
- return RT_EOK;
- }
- if ((mode == RT_WLAN_STATION) &&
- (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_AP_ONLY))
- {
- RT_WLAN_LOG_I("this device ap mode only");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- else if ((mode == RT_WLAN_AP) &&
- (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_STA_ONLY))
- {
- RT_WLAN_LOG_I("this device sta mode only");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- /*
- * device == sta and change to ap, should deinit
- * device == ap and change to sta, should deinit
- */
- if (((mode == RT_WLAN_STATION) && (RT_WLAN_DEVICE(device) == AP_DEVICE())) ||
- ((mode == RT_WLAN_AP) && (RT_WLAN_DEVICE(device) == STA_DEVICE())))
- {
- err = rt_wlan_set_mode(dev_name, RT_WLAN_NONE);
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_E("change mode failed!");
- MGNT_UNLOCK();
- return err;
- }
- }
- /* init device */
- err = rt_wlan_dev_init(RT_WLAN_DEVICE(device), mode);
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_E("F:%s L:%d wlan init failed", __FUNCTION__, __LINE__);
- MGNT_UNLOCK();
- return err;
- }
- /* the mode is none */
- if (mode == RT_WLAN_NONE)
- {
- if (_sta_mgnt.device == RT_WLAN_DEVICE(device))
- {
- _sta_mgnt.device = RT_NULL;
- _sta_mgnt.state = 0;
- up_event_flag = 1;
- handler = RT_NULL;
- }
- else if (_ap_mgnt.device == RT_WLAN_DEVICE(device))
- {
- _ap_mgnt.state = 0;
- _ap_mgnt.device = RT_NULL;
- up_event_flag = 1;
- handler = RT_NULL;
- }
- }
- /* save sta device */
- else if (mode == RT_WLAN_STATION)
- {
- up_event_flag = 1;
- handler = rt_wlan_event_dispatch;
- _sta_mgnt.device = RT_WLAN_DEVICE(device);
- }
- /* save ap device */
- else if (mode == RT_WLAN_AP)
- {
- up_event_flag = 1;
- handler = rt_wlan_event_dispatch;
- _ap_mgnt.device = RT_WLAN_DEVICE(device);
- }
- /* update dev event handle */
- if (up_event_flag == 1)
- {
- if (handler)
- {
- if (mode == RT_WLAN_STATION)
- {
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_CONNECT, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_CONNECT_FAIL, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_DISCONNECT, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_SCAN_REPORT, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_SCAN_DONE, handler, RT_NULL);
- }
- else if (mode == RT_WLAN_AP)
- {
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_START, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_STOP, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_ASSOCIATED, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_DISASSOCIATED, handler, RT_NULL);
- rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_ASSOCIATE_FAILED, handler, RT_NULL);
- }
- }
- else
- {
- rt_wlan_dev_event_t event;
- handler = rt_wlan_event_dispatch;
- for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event++)
- {
- rt_wlan_dev_unregister_event_handler(RT_WLAN_DEVICE(device), event, handler);
- }
- }
- }
- MGNT_UNLOCK();
- /* Mount protocol */
- #if defined(RT_WLAN_PROT_ENABLE) && defined(RT_WLAN_DEFAULT_PROT)
- if (err == RT_EOK)
- {
- rt_wlan_prot_attach(dev_name, RT_WLAN_DEFAULT_PROT);
- }
- #endif
- return err;
- }
- rt_wlan_mode_t rt_wlan_get_mode(const char *dev_name)
- {
- rt_device_t device = RT_NULL;
- rt_wlan_mode_t mode;
- if (dev_name == RT_NULL)
- {
- RT_WLAN_LOG_E("name is null");
- return RT_WLAN_NONE;
- }
- /* find device */
- device = rt_device_find(dev_name);
- if (device == RT_NULL)
- {
- RT_WLAN_LOG_E("device not find! name:%s", dev_name);
- return RT_WLAN_NONE;
- }
- /* get mode */
- mode = RT_WLAN_DEVICE(device)->mode;
- RT_WLAN_LOG_D("%s is run dev_name:%s mode:%s%s%s", __FUNCTION__, dev_name,
- mode == RT_WLAN_NONE ? "NONE" : "",
- mode == RT_WLAN_STATION ? "STA" : "",
- mode == RT_WLAN_AP ? "AP" : "");
- return mode;
- }
- #ifdef RT_WLAN_JOIN_SCAN_BY_MGNT
- static void rt_wlan_join_scan_callback(int event, struct rt_wlan_buff *buff, void *parameter)
- {
- struct rt_wlan_info *info = RT_NULL;
- struct rt_wlan_info *tgt_info = RT_NULL;
- RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT);
- RT_ASSERT(buff != RT_NULL);
- RT_ASSERT(parameter != RT_NULL);
- info = (struct rt_wlan_info *)buff->data;
- tgt_info = (struct rt_wlan_info *)parameter;
- RT_WLAN_LOG_D("%s info len:%d tgt info len:%d", __FUNCTION__,info->ssid.len,tgt_info->ssid.len);
- RT_WLAN_LOG_D("%s info ssid:%s tgt info ssid:%s", __FUNCTION__,&info->ssid.val[0],&tgt_info->ssid.val[0]);
- if(rt_memcmp(&info->ssid.val[0], &tgt_info->ssid.val[0], info->ssid.len) == 0 &&
- info->ssid.len == tgt_info->ssid.len)
- {
- /*Get the rssi the max ap*/
- if((info->rssi > tgt_info->rssi) || (tgt_info->rssi == 0))
- {
- tgt_info->security = info->security;
- tgt_info->band = info->band;
- tgt_info->datarate = info->datarate;
- tgt_info->channel = info->channel;
- tgt_info->rssi = info->rssi;
- tgt_info->hidden = info->hidden;
- /* hwaddr */
- rt_memcpy(tgt_info->bssid,info->bssid,RT_WLAN_BSSID_MAX_LENGTH);
- }
- }
- }
- #endif
- rt_err_t rt_wlan_connect(const char *ssid, const char *password)
- {
- rt_err_t err = RT_EOK;
- int ssid_len = 0;
- struct rt_wlan_info info;
- struct rt_wlan_complete_des *complete;
- rt_uint32_t set = 0, recved = 0;
- /* sta dev Can't be NULL */
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
- if (ssid == RT_NULL)
- {
- RT_WLAN_LOG_E("ssid is null!");
- return -RT_EINVAL;
- }
- ssid_len = rt_strlen(ssid);
- if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
- {
- RT_WLAN_LOG_E("ssid is to long! ssid:%s len:%d", ssid, ssid_len);
- return -RT_EINVAL;
- }
- if ((rt_wlan_is_connected() == RT_TRUE) &&
- (rt_strcmp((char *)&_sta_mgnt.info.ssid.val[0], ssid) == 0))
- {
- RT_WLAN_LOG_I("wifi is connect ssid:%s", ssid);
- return RT_EOK;
- }
- /* get info from cache */
- INVALID_INFO(&info);
- MGNT_LOCK();
- rt_memcpy(&info.ssid.val[0],ssid,rt_strlen(ssid));
- info.ssid.len = rt_strlen(ssid);
- #ifdef RT_WLAN_JOIN_SCAN_BY_MGNT
- err = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,rt_wlan_join_scan_callback,&info);
- if(err != RT_EOK)
- {
- LOG_E("Scan register user callback error:%d!\n",err);
- return err;
- }
- err = rt_wlan_scan_with_info(&info);
- if(err != RT_EOK)
- {
- LOG_E("Scan with info error:%d!\n",err);
- return err;
- }
- if (info.channel <= 0)
- {
- RT_WLAN_LOG_W("not find ap! ssid:%s,info.ssid.len=%d", ssid,info.ssid.len);
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- RT_WLAN_LOG_D("find best info ssid:%s mac: %02x %02x %02x %02x %02x %02x",
- info.ssid.val, info.bssid[0], info.bssid[1], info.bssid[2], info.bssid[3], info.bssid[4], info.bssid[5]);
- #endif
- /* create event wait complete */
- complete = rt_wlan_complete_create("join");
- if (complete == RT_NULL)
- {
- MGNT_UNLOCK();
- return -RT_ENOMEM;
- }
- /* run connect adv */
- err = rt_wlan_connect_adv(&info, password);
- if (err != RT_EOK)
- {
- rt_wlan_complete_delete(complete);
- MGNT_UNLOCK();
- return err;
- }
- /* Initializing events that need to wait */
- set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT;
- set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT_FAIL;
- /* Check whether there is a waiting event */
- rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
- rt_wlan_complete_delete(complete);
- /* check event */
- set = 0x1 << RT_WLAN_DEV_EVT_CONNECT;
- if (!(recved & set))
- {
- RT_WLAN_LOG_I("wifi connect failed!");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password)
- {
- int password_len = 0;
- rt_err_t err = RT_EOK;
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- if (info == RT_NULL)
- {
- RT_WLAN_LOG_E("info is null!");
- return -RT_EINVAL;
- }
- RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, info->ssid.val, password);
- /* Parameter checking */
- if (password != RT_NULL)
- {
- password_len = rt_strlen(password);
- if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
- {
- RT_WLAN_LOG_E("password is to long! password:%s len:%d", password, password_len);
- return -RT_EINVAL;
- }
- }
- if (info->ssid.len == 0 || info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
- {
- RT_WLAN_LOG_E("ssid is zero or to long! ssid:%s len:%d", info->ssid.val, info->ssid.len);
- return -RT_EINVAL;
- }
- /* is connect ? */
- MGNT_LOCK();
- if (rt_wlan_is_connected())
- {
- if ((_sta_mgnt.info.ssid.len == info->ssid.len) &&
- (_sta_mgnt.key.len == password_len) &&
- (rt_memcmp(&_sta_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
- (rt_memcmp(&_sta_mgnt.info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0) &&
- (rt_memcmp(&_sta_mgnt.key.val[0], password, password_len) == 0))
- {
- RT_WLAN_LOG_I("wifi Already Connected");
- MGNT_UNLOCK();
- return RT_EOK;
- }
- err = rt_wlan_disconnect();
- if (err != RT_EOK)
- {
- MGNT_UNLOCK();
- return err;
- }
- }
- /* save info */
- rt_enter_critical();
- _sta_mgnt.info = *info;
- rt_memcpy(&_sta_mgnt.key.val, password, password_len);
- _sta_mgnt.key.len = password_len;
- _sta_mgnt.key.val[password_len] = '\0';
- rt_exit_critical();
- /* run wifi connect */
- _sta_mgnt.state |= RT_WLAN_STATE_CONNECTING;
- err = rt_wlan_dev_fast_connect(_sta_mgnt.device, info, password, password_len);
- if(err != RT_EOK)
- {
- err = rt_wlan_dev_connect(_sta_mgnt.device, info, password, password_len);
- if (err != RT_EOK)
- {
- rt_enter_critical();
- rt_memset(&_sta_mgnt.info, 0, sizeof(struct rt_wlan_ssid));
- rt_memset(&_sta_mgnt.key, 0, sizeof(struct rt_wlan_key));
- rt_exit_critical();
- _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
- MGNT_UNLOCK();
- return err;
- }
- }
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_disconnect(void)
- {
- rt_err_t err;
- struct rt_wlan_complete_des *complete;
- rt_uint32_t recved = 0, set = 0;
- /* ap dev Can't be empty */
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- /* run disconnect */
- MGNT_LOCK();
- /* create event wait complete */
- complete = rt_wlan_complete_create("disc");
- if (complete == RT_NULL)
- {
- MGNT_UNLOCK();
- return -RT_ENOMEM;
- }
- err = rt_wlan_dev_disconnect(_sta_mgnt.device);
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_E("wifi disconnect fail");
- rt_wlan_complete_delete(complete);
- MGNT_UNLOCK();
- return err;
- }
- /* Initializing events that need to wait */
- set |= 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
- /* Check whether there is a waiting event */
- rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
- rt_wlan_complete_delete(complete);
- /* check event */
- set = 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
- if (!(recved & set))
- {
- RT_WLAN_LOG_E("disconnect failed!");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- RT_WLAN_LOG_I("disconnect success!");
- MGNT_UNLOCK();
- return err;
- }
- rt_bool_t rt_wlan_is_connected(void)
- {
- rt_bool_t _connect;
- if (_sta_is_null())
- {
- return RT_FALSE;
- }
- _connect = _sta_mgnt.state & RT_WLAN_STATE_CONNECT ? RT_TRUE : RT_FALSE;
- RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _connect ? "connect" : "disconnect");
- return _connect;
- }
- rt_bool_t rt_wlan_is_ready(void)
- {
- rt_bool_t _ready;
- if (_sta_is_null())
- {
- return RT_FALSE;
- }
- _ready = _sta_mgnt.state & RT_WLAN_STATE_READY ? RT_TRUE : RT_FALSE;
- RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _ready ? "ready" : "not ready");
- return _ready;
- }
- rt_err_t rt_wlan_set_mac(rt_uint8_t mac[6])
- {
- rt_err_t err = RT_EOK;
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
- __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
- MGNT_LOCK();
- err = rt_wlan_dev_set_mac(STA_DEVICE(), mac);
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_E("set sta mac addr fail");
- MGNT_UNLOCK();
- return err;
- }
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_get_mac(rt_uint8_t mac[6])
- {
- rt_err_t err = RT_EOK;
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- MGNT_LOCK();
- err = rt_wlan_dev_get_mac(STA_DEVICE(), mac);
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_E("get sta mac addr fail");
- MGNT_UNLOCK();
- return err;
- }
- RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
- __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_get_info(struct rt_wlan_info *info)
- {
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- if (rt_wlan_is_connected() == RT_TRUE)
- {
- *info = _sta_mgnt.info;
- info->rssi = rt_wlan_get_rssi();
- return RT_EOK;
- }
- return -RT_ERROR;
- }
- int rt_wlan_get_rssi(void)
- {
- int rssi = 0;
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- MGNT_LOCK();
- rssi = rt_wlan_dev_get_rssi(STA_DEVICE());
- RT_WLAN_LOG_D("%s is run rssi:%d", __FUNCTION__, rssi);
- MGNT_UNLOCK();
- return rssi;
- }
- rt_err_t rt_wlan_start_ap(const char *ssid, const char *password)
- {
- rt_err_t err = RT_EOK;
- int ssid_len = 0;
- struct rt_wlan_info info;
- struct rt_wlan_complete_des *complete;
- rt_uint32_t set = 0, recved = 0;
- if (_ap_is_null())
- {
- return -RT_EIO;
- }
- if (ssid == RT_NULL) return -RT_EINVAL;
- rt_memset(&info, 0, sizeof(struct rt_wlan_info));
- RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
- if (password)
- {
- info.security = SECURITY_WPA2_AES_PSK;
- }
- ssid_len = rt_strlen(ssid);
- if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
- {
- RT_WLAN_LOG_E("ssid is to long! len:%d", ssid_len);
- }
- /* copy info */
- rt_memcpy(&info.ssid.val, ssid, ssid_len);
- info.ssid.len = ssid_len;
- info.channel = 6;
- info.band = RT_802_11_BAND_2_4GHZ;
- /* Initializing events that need to wait */
- MGNT_LOCK();
- /* create event wait complete */
- complete = rt_wlan_complete_create("start_ap");
- if (complete == RT_NULL)
- {
- MGNT_UNLOCK();
- return -RT_ENOMEM;
- }
- /* start ap */
- err = rt_wlan_start_ap_adv(&info, password);
- if (err != RT_EOK)
- {
- rt_wlan_complete_delete(complete);
- RT_WLAN_LOG_I("start ap failed!");
- MGNT_UNLOCK();
- return err;
- }
- /* Initializing events that need to wait */
- set |= 0x1 << RT_WLAN_DEV_EVT_AP_START;
- set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
- /* Check whether there is a waiting event */
- rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
- rt_wlan_complete_delete(complete);
- /* check event */
- set = 0x1 << RT_WLAN_DEV_EVT_AP_START;
- if (!(recved & set))
- {
- RT_WLAN_LOG_I("start ap failed!");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- RT_WLAN_LOG_I("start ap successs!");
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_start_ap_adv(struct rt_wlan_info *info, const char *password)
- {
- rt_err_t err = RT_EOK;
- int password_len = 0;
- if (_ap_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- if (password != RT_NULL)
- {
- password_len = rt_strlen(password);
- }
- if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
- {
- RT_WLAN_LOG_E("key is to long! len:%d", password_len);
- return -RT_EINVAL;
- }
- /* is start up ? */
- MGNT_LOCK();
- if (rt_wlan_ap_is_active())
- {
- if ((_ap_mgnt.info.ssid.len == info->ssid.len) &&
- (_ap_mgnt.info.security == info->security) &&
- (_ap_mgnt.info.channel == info->channel) &&
- (_ap_mgnt.info.hidden == info->hidden) &&
- (_ap_mgnt.key.len == password_len) &&
- (rt_memcmp(&_ap_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
- (rt_memcmp(&_ap_mgnt.key.val[0], password, password_len)))
- {
- RT_WLAN_LOG_D("wifi Already Start");
- MGNT_UNLOCK();
- return RT_EOK;
- }
- }
- err = rt_wlan_dev_ap_start(AP_DEVICE(), info, password, password_len);
- if (err != RT_EOK)
- {
- MGNT_UNLOCK();
- return err;
- }
- rt_memcpy(&_ap_mgnt.info, info, sizeof(struct rt_wlan_info));
- rt_memcpy(&_ap_mgnt.key.val, password, password_len);
- _ap_mgnt.key.len = password_len;
- MGNT_UNLOCK();
- return err;
- }
- rt_bool_t rt_wlan_ap_is_active(void)
- {
- rt_bool_t _active = RT_FALSE;
- if (_ap_is_null())
- {
- return RT_FALSE;
- }
- _active = _ap_mgnt.state & RT_WLAN_STATE_ACTIVE ? RT_TRUE : RT_FALSE;
- RT_WLAN_LOG_D("%s is run active:%s", __FUNCTION__, _active ? "Active" : "Inactive");
- return _active;
- }
- rt_err_t rt_wlan_ap_stop(void)
- {
- rt_err_t err = RT_EOK;
- struct rt_wlan_complete_des *complete;
- rt_uint32_t set = 0, recved = 0;
- if (_ap_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- MGNT_LOCK();
- /* create event wait complete */
- complete = rt_wlan_complete_create("stop_ap");
- if (complete == RT_NULL)
- {
- MGNT_UNLOCK();
- return -RT_ENOMEM;
- }
- err = rt_wlan_dev_ap_stop(AP_DEVICE());
- if (err != RT_EOK)
- {
- RT_WLAN_LOG_E("ap stop fail");
- rt_wlan_complete_delete(complete);
- MGNT_UNLOCK();
- return err;
- }
- /* Initializing events that need to wait */
- set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
- /* Check whether there is a waiting event */
- rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
- rt_wlan_complete_delete(complete);
- /* check event */
- set = 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
- if (!(recved & set))
- {
- RT_WLAN_LOG_I("ap stop failed!");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- RT_WLAN_LOG_I("ap stop success!");
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_ap_get_info(struct rt_wlan_info *info)
- {
- if (_ap_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- if (rt_wlan_ap_is_active() == RT_TRUE)
- {
- *info = _ap_mgnt.info;
- return RT_EOK;
- }
- return -RT_ERROR;
- }
- /* get sta number */
- int rt_wlan_ap_get_sta_num(void)
- {
- int sta_num = 0;
- STAINFO_LOCK();
- sta_num = sta_info.num;
- STAINFO_UNLOCK();
- RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, sta_num);
- return sta_num;
- }
- /* get sta info */
- int rt_wlan_ap_get_sta_info(struct rt_wlan_info *info, int num)
- {
- int sta_num = 0, i = 0;
- struct rt_wlan_sta_list *sta_list;
- STAINFO_LOCK();
- /* sta_num = min(sta_info.num, num) */
- sta_num = sta_info.num > num ? num : sta_info.num;
- for (sta_list = sta_info.node; sta_list != RT_NULL && i < sta_num; sta_list = sta_list->next)
- {
- info[i] = sta_list->info;
- i ++;
- }
- STAINFO_UNLOCK();
- RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, i);
- return i;
- }
- /* deauth sta */
- rt_err_t rt_wlan_ap_deauth_sta(rt_uint8_t *mac)
- {
- rt_err_t err = RT_EOK;
- struct rt_wlan_sta_list *sta_list;
- rt_bool_t find_flag = RT_FALSE;
- if (_ap_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x:%d",
- __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
- if (mac == RT_NULL)
- {
- RT_WLAN_LOG_E("mac addr is null");
- return -RT_EINVAL;
- }
- MGNT_LOCK();
- if (sta_info.node == RT_NULL || sta_info.num == 0)
- {
- RT_WLAN_LOG_E("No AP");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- STAINFO_LOCK();
- /* Search for MAC address from sta list */
- for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_list->next)
- {
- if (rt_memcmp(&sta_list->info.bssid[0], &mac[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)
- {
- find_flag = RT_TRUE;
- break;
- }
- }
- STAINFO_UNLOCK();
- /* No MAC address was found. return */
- if (find_flag != RT_TRUE)
- {
- RT_WLAN_LOG_E("Not find mac addr");
- MGNT_UNLOCK();
- return -RT_ERROR;
- }
- /* Kill STA */
- err = rt_wlan_dev_ap_deauth(AP_DEVICE(), mac);
- if (err != RT_NULL)
- {
- RT_WLAN_LOG_E("deauth sta failed");
- MGNT_UNLOCK();
- return err;
- }
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_ap_set_country(rt_country_code_t country_code)
- {
- rt_err_t err = RT_EOK;
- if (_ap_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
- MGNT_LOCK();
- err = rt_wlan_dev_set_country(AP_DEVICE(), country_code);
- MGNT_UNLOCK();
- return err;
- }
- rt_country_code_t rt_wlan_ap_get_country(void)
- {
- rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
- if (_ap_is_null())
- {
- return country_code;
- }
- MGNT_LOCK();
- country_code = rt_wlan_dev_get_country(AP_DEVICE());
- RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
- MGNT_UNLOCK();
- return country_code;
- }
- void rt_wlan_config_autoreconnect(rt_bool_t enable)
- {
- #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
- RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
- MGNT_LOCK();
- if (enable)
- {
- TIME_START();
- _sta_mgnt.flags |= RT_WLAN_STATE_AUTOEN;
- }
- else
- {
- TIME_STOP();
- _sta_mgnt.flags &= ~RT_WLAN_STATE_AUTOEN;
- }
- MGNT_UNLOCK();
- #endif
- }
- rt_bool_t rt_wlan_get_autoreconnect_mode(void)
- {
- #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
- rt_bool_t enable = 0;
- enable = _sta_mgnt.flags & RT_WLAN_STATE_AUTOEN ? 1 : 0;
- RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
- return enable;
- #else
- return RT_FALSE;
- #endif
- }
- /* Call the underlying scan function, which is asynchronous.
- The hotspots scanned are returned by callbacks */
- rt_err_t rt_wlan_scan(void)
- {
- rt_err_t err = RT_EOK;
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- MGNT_LOCK();
- err = rt_wlan_dev_scan(STA_DEVICE(), RT_NULL);
- MGNT_UNLOCK();
- return err;
- }
- rt_err_t rt_wlan_scan_with_info(struct rt_wlan_info *info)
- {
- rt_err_t err = RT_EOK;
- struct rt_wlan_complete_des *complete;
- rt_uint32_t set = 0, recved = 0;
- if (_sta_is_null())
- {
- return -RT_EINVAL;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- if (info != RT_NULL && info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
- {
- RT_WLAN_LOG_E("ssid is to long!");
- return -RT_EINVAL;
- }
- /* Create an event that needs to wait. */
- MGNT_LOCK();
- complete = rt_wlan_complete_create("scan");
- if (complete == RT_NULL)
- {
- MGNT_UNLOCK();
- return -RT_EIO;
- }
- /* run scan */
- err = rt_wlan_dev_scan(STA_DEVICE(), info);
- if (err != RT_EOK)
- {
- rt_wlan_complete_delete(complete);
- MGNT_UNLOCK();
- RT_WLAN_LOG_E("scan sync fail");
- return -RT_ERROR;
- }
- /* Initializing events that need to wait */
- set |= 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
- /* Check whether there is a waiting event */
- rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
- rt_wlan_complete_delete(complete);
- /* check event */
- set = 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
- if (!(recved & set))
- {
- MGNT_UNLOCK();
- RT_WLAN_LOG_E("scan wait timeout!");
- return -RT_ETIMEOUT;
- }
- MGNT_UNLOCK();
- return RT_EOK;
- }
- rt_err_t rt_wlan_set_powersave(int level)
- {
- rt_err_t err = RT_EOK;
- if (_sta_is_null())
- {
- return -RT_EIO;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- MGNT_LOCK();
- err = rt_wlan_dev_set_powersave(STA_DEVICE(), level);
- MGNT_UNLOCK();
- return err;
- }
- int rt_wlan_get_powersave(void)
- {
- int level;
- if (_sta_is_null())
- {
- return -1;
- }
- RT_WLAN_LOG_D("%s is run", __FUNCTION__);
- MGNT_LOCK();
- level = rt_wlan_dev_get_powersave(STA_DEVICE());
- MGNT_UNLOCK();
- return level;
- }
- rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter)
- {
- rt_base_t level;
- if (event >= RT_WLAN_EVT_MAX)
- {
- return -RT_EINVAL;
- }
- RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
- MGNT_LOCK();
- /* Registering Callbacks */
- level = rt_hw_interrupt_disable();
- event_tab[event].handler = handler;
- event_tab[event].parameter = parameter;
- rt_hw_interrupt_enable(level);
- MGNT_UNLOCK();
- return RT_EOK;
- }
- rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event)
- {
- rt_base_t level;
- if (event >= RT_WLAN_EVT_MAX)
- {
- return -RT_EINVAL;
- }
- RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
- MGNT_LOCK();
- /* unregister*/
- level = rt_hw_interrupt_disable();
- event_tab[event].handler = RT_NULL;
- event_tab[event].parameter = RT_NULL;
- rt_hw_interrupt_enable(level);
- MGNT_UNLOCK();
- return RT_EOK;
- }
- void rt_wlan_mgnt_lock(void)
- {
- MGNT_LOCK();
- }
- void rt_wlan_mgnt_unlock(void)
- {
- MGNT_UNLOCK();
- }
- int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff)
- {
- rt_base_t level;
- if ((wlan == RT_NULL) || (_sta_mgnt.device != wlan) ||
- (!(_sta_mgnt.state & RT_WLAN_STATE_CONNECT)))
- {
- return -1;
- }
- if (_sta_mgnt.state & RT_WLAN_STATE_READY)
- {
- return 0;
- }
- level = rt_hw_interrupt_disable();
- _sta_mgnt.state |= RT_WLAN_STATE_READY;
- rt_hw_interrupt_enable(level);
- #ifdef RT_WLAN_WORK_THREAD_ENABLE
- rt_wlan_send_to_thread(RT_WLAN_EVT_READY, buff->data, buff->len);
- #else
- {
- void *user_parameter;
- rt_wlan_event_handler handler = RT_NULL;
- level = rt_hw_interrupt_disable();
- handler = event_tab[RT_WLAN_EVT_READY].handler;
- user_parameter = event_tab[RT_WLAN_EVT_READY].parameter;
- rt_hw_interrupt_enable(level);
- if (handler)
- {
- handler(RT_WLAN_EVT_READY, buff, user_parameter);
- }
- }
- #endif
- return 0;
- }
- int rt_wlan_init(void)
- {
- static rt_int8_t _init_flag = 0;
- /* Execute only once */
- if (_init_flag == 0)
- {
- rt_memset(&_sta_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
- rt_memset(&_ap_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
- rt_memset(&sta_info, 0, sizeof(struct rt_wlan_sta_des));
- rt_mutex_init(&mgnt_mutex, "mgnt", RT_IPC_FLAG_FIFO);
- rt_mutex_init(&sta_info_mutex, "sta", RT_IPC_FLAG_FIFO);
- rt_mutex_init(&complete_mutex, "complete", RT_IPC_FLAG_FIFO);
- #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
- rt_timer_init(&reconnect_time, "wifi_tim", rt_wlan_cyclic_check, RT_NULL,
- rt_tick_from_millisecond(AUTO_CONNECTION_PERIOD_MS),
- RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
- #endif
- _init_flag = 1;
- }
- return 0;
- }
- INIT_PREV_EXPORT(rt_wlan_init);
- #endif
|