Skip to content

Commit ceadd30

Browse files
pjsgmarcelstoer
authored andcommitted
Add support for the wifi monitor mode in the SDK (#2204)
1 parent 77fe510 commit ceadd30

File tree

9 files changed

+1352
-0
lines changed

9 files changed

+1352
-0
lines changed

app/include/sys/network_80211.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#define FRAME_TYPE_MANAGEMENT 0
2+
#define FRAME_TYPE_CONTROL 1
3+
#define FRAME_TYPE_DATA 2
4+
#define FRAME_SUBTYPE_ASSOC_REQUEST 0x00
5+
#define FRAME_SUBTYPE_ASSOC_RESPONSE 0x01
6+
#define FRAME_SUBTYPE_REASSOC_REQUEST 0x02
7+
#define FRAME_SUBTYPE_REASSOC_RESPONSE 0x03
8+
#define FRAME_SUBTYPE_PROBE_REQUEST 0x04
9+
#define FRAME_SUBTYPE_PROBE_RESPONSE 0x05
10+
#define FRAME_SUBTYPE_BEACON 0x08
11+
#define FRAME_SUBTYPE_ATIM 0x09
12+
#define FRAME_SUBTYPE_DISASSOCIATION 0x0a
13+
#define FRAME_SUBTYPE_AUTHENTICATION 0x0b
14+
#define FRAME_SUBTYPE_DEAUTHENTICATION 0x0c
15+
#define FRAME_SUBTYPE_DATA 0x14
16+
typedef struct framectrl_80211
17+
{
18+
//buf[0]
19+
u8 Protocol:2;
20+
u8 Type:2;
21+
u8 Subtype:4;
22+
//buf[1]
23+
u8 ToDS:1;
24+
u8 FromDS:1;
25+
u8 MoreFlag:1;
26+
u8 Retry:1;
27+
u8 PwrMgmt:1;
28+
u8 MoreData:1;
29+
u8 Protectedframe:1;
30+
u8 Order:1;
31+
} framectrl_80211,*lpframectrl_80211;
32+
33+
typedef struct management_80211
34+
{
35+
struct framectrl_80211 framectrl;
36+
uint16 duration;
37+
uint8 rdaddr[6];
38+
uint8 tsaddr[6];
39+
uint8 bssid[6];
40+
uint16 number;
41+
} management_request_t;
42+
43+
typedef struct
44+
{
45+
management_request_t hdr;
46+
uint8 timestamp[8];
47+
uint16 beacon_interval;
48+
uint16 capability_info;
49+
} wifi_beacon_t;
50+
51+
typedef struct tagged_parameter
52+
{
53+
/* SSID parameter */
54+
uint8 tag_number;
55+
uint8 tag_length;
56+
} tagged_parameter, *ptagged_parameter;
57+
58+
struct RxControl {
59+
signed rssi:8;//表示该包的信号强度
60+
unsigned rate:4;
61+
unsigned is_group:1;
62+
unsigned:1;
63+
unsigned sig_mode:2;//表示该包是否是11n的包,0表示非11n,非0表示11n
64+
unsigned legacy_length:12;//如果不是11n的包,它表示包的长度
65+
unsigned damatch0:1;
66+
unsigned damatch1:1;
67+
unsigned bssidmatch0:1;
68+
unsigned bssidmatch1:1;
69+
unsigned MCS:7;//如果是11n的包,它表示包的调制编码序列,有效值:0-76
70+
unsigned CWB:1;//如果是11n的包,它表示是否为HT40的包
71+
unsigned HT_length:16;//如果是11n的包,它表示包的长度
72+
unsigned Smoothing:1;
73+
unsigned Not_Sounding:1;
74+
unsigned:1;
75+
unsigned Aggregation:1;
76+
unsigned STBC:2;
77+
unsigned FEC_CODING:1;//如果是11n的包,它表示是否为LDPC的包
78+
unsigned SGI:1;
79+
unsigned rxend_state:8;
80+
unsigned ampdu_cnt:8;
81+
unsigned channel:4;//表示该包所在的信道
82+
unsigned:12;
83+
};
84+
85+
struct sniffer_buf2{
86+
struct RxControl rx_ctrl;
87+
u8 buf[112];//包含ieee80211包头
88+
u16 cnt;//包的个数
89+
u16 len[1];//包的长度
90+
};
91+
92+
struct sniffer_buf{
93+
struct RxControl rx_ctrl;
94+
u8 buf[48];//包含ieee80211包头
95+
u16 cnt;//包的个数
96+
u16 len[1];//包的长度
97+
};
98+

app/include/user_modules.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
//#define LUA_USE_MODULES_UCG
7878
//#define LUA_USE_MODULES_WEBSOCKET
7979
#define LUA_USE_MODULES_WIFI
80+
//#define LUA_USE_MODULES_WIFI_MONITOR
8081
//#define LUA_USE_MODULES_WPS
8182
//#define LUA_USE_MODULES_WS2801
8283
//#define LUA_USE_MODULES_WS2812

app/modules/wifi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,9 @@ static const LUA_REG_TYPE wifi_map[] = {
18231823
{ LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) },
18241824
#if defined(WIFI_SDK_EVENT_MONITOR_ENABLE)
18251825
{ LSTRKEY( "eventmon" ), LROVAL( wifi_event_monitor_map ) }, //declared in wifi_eventmon.c
1826+
#endif
1827+
#if defined(LUA_USE_MODULES_WIFI_MONITOR)
1828+
{ LSTRKEY( "monitor" ), LROVAL( wifi_monitor_map ) }, //declared in wifi_monitor.c
18261829
#endif
18271830
{ LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) },
18281831
{ LSTRKEY( "STATION" ), LNUMVAL( STATION_MODE ) },
@@ -1898,6 +1901,9 @@ int luaopen_wifi( lua_State *L )
18981901
}
18991902
#if defined(WIFI_SDK_EVENT_MONITOR_ENABLE)
19001903
wifi_eventmon_init();
1904+
#endif
1905+
#if defined(LUA_USE_MODULES_WIFI_MONITOR)
1906+
wifi_monitor_init(L);
19011907
#endif
19021908
return 0;
19031909
}

app/modules/wifi_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,9 @@ enum wifi_suspension_state
6767
int wifi_event_monitor_register(lua_State* L);
6868
#endif
6969

70+
#ifdef LUA_USE_MODULES_WIFI_MONITOR
71+
extern const LUA_REG_TYPE wifi_monitor_map[];
72+
int wifi_monitor_init(lua_State *L);
73+
#endif
74+
7075
#endif /* APP_MODULES_WIFI_COMMON_H_ */

app/modules/wifi_eventmon.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ static evt_queue_t *wifi_event_queue_head; //pointer to beginning of queue
3131
static evt_queue_t *wifi_event_queue_tail; //pointer to end of queue
3232
static int wifi_event_cb_ref[EVENT_MAX+1] = { [0 ... EVENT_MAX] = LUA_NOREF}; //holds references to registered Lua callbacks
3333

34+
#ifdef LUA_USE_MODULES_WIFI_MONITOR
35+
static int (*hook_fn)(System_Event_t *);
36+
37+
void wifi_event_monitor_register_hook(int (*fn)(System_Event_t*)) {
38+
hook_fn = fn;
39+
}
40+
#endif
41+
3442
// wifi.eventmon.register()
3543
int wifi_event_monitor_register(lua_State* L)
3644
{
@@ -58,6 +66,12 @@ static void wifi_event_monitor_handle_event_cb(System_Event_t *evt)
5866
{
5967
EVENT_DBG("\n\twifi_event_monitor_handle_event_cb is called\n");
6068

69+
#ifdef LUA_USE_MODULES_WIFI_MONITOR
70+
if (hook_fn && hook_fn(evt)) {
71+
return;
72+
}
73+
#endif
74+
6175
if((wifi_event_cb_ref[evt->event] != LUA_NOREF) || ((wifi_event_cb_ref[EVENT_MAX] != LUA_NOREF) &&
6276
!(evt->event == EVENT_STAMODE_CONNECTED || evt->event == EVENT_STAMODE_DISCONNECTED ||
6377
evt->event == EVENT_STAMODE_AUTHMODE_CHANGE || evt->event == EVENT_STAMODE_GOT_IP ||

0 commit comments

Comments
 (0)