Skip to content

Commit 08489e4

Browse files
Archcady0xc0170
authored andcommitted
Fix on Wifi
1 parent 1141c7a commit 08489e4

File tree

7 files changed

+291
-392
lines changed

7 files changed

+291
-392
lines changed

targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.cpp

Lines changed: 132 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,100 @@
2727

2828
#include "osdep_service.h"
2929

30-
struct netif *xnetif[2];
31-
static struct netif lwap_netif;
32-
extern struct netif *netif_default;
30+
typedef struct _wifi_scan_hdl {
31+
int scan_num;
32+
WiFiAccessPoint *ap_details;
33+
} wifi_scan_hdl;
3334

34-
RTWInterface::RTWInterface()
35-
: _dhcp(true), _ip_address(), _netmask(), _gateway()
36-
{
37-
nsapi_error_t ret;
38-
ret = init();
39-
if (ret != NSAPI_ERROR_OK){
40-
printf("Error init RTWInterface!(%d)\r\n", ret);
41-
}
42-
}
43-
44-
static void *scan_sema;
45-
static const signed int maxApNum = 4;
46-
static signed int ApNum;
47-
static WiFiAccessPoint *SCANED_AP[maxApNum]; /*maximum store 15 APs*/
48-
49-
nsapi_error_t RTWInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
50-
{
51-
_dhcp = false;
52-
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
53-
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
54-
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
55-
return NSAPI_ERROR_OK;
56-
}
35+
#define MAX_SCAN_TIMEOUT (15000)
36+
static void *scan_sema = NULL;
37+
static signed int ApNum = 0;
5738

58-
nsapi_error_t RTWInterface::set_dhcp(bool dhcp)
39+
static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
5940
{
60-
_dhcp = dhcp;
61-
return NSAPI_ERROR_OK;
41+
if (malloced_scan_result->scan_complete != RTW_TRUE) {
42+
wifi_scan_hdl *scan_handler = (wifi_scan_hdl *)malloced_scan_result->user_data;
43+
if(scan_handler->ap_details && scan_handler->scan_num > ApNum){
44+
nsapi_wifi_ap_t ap;
45+
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
46+
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
47+
memset((void*)&ap, 0x00, sizeof(nsapi_wifi_ap_t));
48+
memcpy(ap.ssid, record->SSID.val, record->SSID.len);
49+
memcpy(ap.bssid, record->BSSID.octet, 6);
50+
switch (record->security){
51+
case RTW_SECURITY_OPEN:
52+
ap.security = NSAPI_SECURITY_NONE;
53+
break;
54+
case RTW_SECURITY_WEP_PSK:
55+
case RTW_SECURITY_WEP_SHARED:
56+
ap.security = NSAPI_SECURITY_WEP;
57+
break;
58+
case RTW_SECURITY_WPA_TKIP_PSK:
59+
case RTW_SECURITY_WPA_AES_PSK:
60+
ap.security = NSAPI_SECURITY_WPA;
61+
break;
62+
case RTW_SECURITY_WPA2_AES_PSK:
63+
case RTW_SECURITY_WPA2_TKIP_PSK:
64+
case RTW_SECURITY_WPA2_MIXED_PSK:
65+
ap.security = NSAPI_SECURITY_WPA2;
66+
break;
67+
case RTW_SECURITY_WPA_WPA2_MIXED:
68+
ap.security = NSAPI_SECURITY_WPA_WPA2;
69+
break;
70+
default:
71+
ap.security = NSAPI_SECURITY_UNKNOWN;
72+
break;
73+
}
74+
ap.rssi = record->signal_strength;
75+
ap.channel = record->channel;
76+
WiFiAccessPoint *accesspoint = new WiFiAccessPoint(ap);
77+
memcpy(&scan_handler->ap_details[ApNum], accesspoint, sizeof(WiFiAccessPoint));
78+
delete[] accesspoint;
79+
}
80+
ApNum++;
81+
} else{
82+
// scan done
83+
rtw_up_sema(&scan_sema);
84+
}
85+
return RTW_SUCCESS;
6286
}
6387

64-
nsapi_error_t RTWInterface::init()
88+
RTWInterface::RTWInterface()
89+
: _dhcp(true), _ip_address(), _netmask(), _gateway()
6590
{
6691
emac_interface_t *emac;
6792
int ret;
6893

69-
//printf("\r\nInitializing emac ...\r\n");
7094
emac = wlan_emac_init_interface();
7195
if (!emac) {
72-
return NSAPI_ERROR_DEVICE_ERROR;
96+
printf("Error init RTWInterface!\r\n");
7397
}
7498
emac->ops.power_up(emac);
75-
//printf("Initializing lwip ...\r\n");
7699
ret = mbed_lwip_init(emac);
77100
if (ret != 0) {
78-
return ret;
101+
printf("Error init RTWInterface!(%d)\r\n", ret);
79102
}
80-
xnetif[0] = netif_default;
103+
}
104+
105+
RTWInterface::~RTWInterface()
106+
{
107+
wlan_emac_link_change(false);
108+
mbed_lwip_bringdown();
109+
}
110+
111+
nsapi_error_t RTWInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
112+
{
113+
_dhcp = false;
114+
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
115+
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
116+
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
81117
return NSAPI_ERROR_OK;
82118
}
83119

84-
nsapi_error_t RTWInterface::deinit()
120+
nsapi_error_t RTWInterface::set_dhcp(bool dhcp)
85121
{
86-
return mbed_lwip_bringdown();
122+
_dhcp = dhcp;
123+
return NSAPI_ERROR_OK;
87124
}
88125

89126
/*
@@ -109,134 +146,102 @@ nsapi_error_t RTWInterface::connect()
109146
}
110147

111148
switch (_security) {
112-
case NSAPI_SECURITY_WPA2:
113-
sec = RTW_SECURITY_WPA2_MIXED_PSK;
114-
break;
115-
case NSAPI_SECURITY_NONE:
116-
default:
117-
sec = RTW_SECURITY_OPEN;
118-
break;
149+
case NSAPI_SECURITY_WPA:
150+
case NSAPI_SECURITY_WPA2:
151+
case NSAPI_SECURITY_WPA_WPA2:
152+
sec = RTW_SECURITY_WPA_WPA2_MIXED;
153+
break;
154+
case NSAPI_SECURITY_WEP:
155+
sec = RTW_SECURITY_WEP_PSK;
156+
break;
157+
case NSAPI_SECURITY_NONE:
158+
sec = RTW_SECURITY_OPEN;
159+
break;
160+
default:
161+
return NSAPI_ERROR_PARAMETER;
119162
}
120163

121-
printf("Connecting to: %s ... \r\n", _ssid);
164+
if(_channel > 0 && _channel < 14){
165+
uint8_t pscan_config = PSCAN_ENABLE;
166+
wifi_set_pscan_chan(&_channel, &pscan_config, 1);
167+
}
168+
169+
//printf("Connecting to: %s ... \r\n", _ssid);
122170
ret = wifi_connect(_ssid, sec, _pass, strlen(_ssid), strlen(_pass), 0, (void *)NULL);
123171
if (ret != RTW_SUCCESS) {
124172
printf("failed: %d\r\n", ret);
125173
return NSAPI_ERROR_NO_CONNECTION;
126174
}
127175
//printf("connected\r\n");
128176

129-
wlan_emac_link_up(0);
177+
wlan_emac_link_change(true);
130178
return mbed_lwip_bringup(_dhcp,
131179
_ip_address[0] ? _ip_address : 0,
132180
_netmask[0] ? _netmask : 0,
133181
_gateway[0] ? _gateway : 0);
134182
}
135183

136-
static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
137-
{
138-
if (malloced_scan_result->scan_complete != RTW_TRUE) {
139-
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
140-
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
141-
if(ApNum>maxApNum)
142-
return RTW_SUCCESS;
143-
nsapi_wifi_ap_t ap;
144-
145-
memset((void*)&ap, 0x00, sizeof(nsapi_wifi_ap_t));
146-
memcpy(ap.ssid, record->SSID.val, record->SSID.len);
147-
memcpy(ap.bssid, record->BSSID.octet, 6);
148-
ap.security = (record->security == RTW_SECURITY_OPEN)?NSAPI_SECURITY_NONE :
149-
(record->security == RTW_SECURITY_WEP_PSK)?NSAPI_SECURITY_WEP:
150-
(record->security == RTW_SECURITY_WPA_TKIP_PSK || record->security == RTW_SECURITY_WPA_AES_PSK)? NSAPI_SECURITY_WPA:
151-
(record->security == RTW_SECURITY_WPA2_AES_PSK || \
152-
record->security == RTW_SECURITY_WPA2_TKIP_PSK || \
153-
record->security == RTW_SECURITY_WPA2_MIXED_PSK)?NSAPI_SECURITY_WPA2:
154-
(record->security == RTW_SECURITY_WPA_WPA2_MIXED)?NSAPI_SECURITY_WPA_WPA2:NSAPI_SECURITY_UNKNOWN;
155-
ap.rssi = record->signal_strength;
156-
ap.channel = record->channel;
157-
158-
159-
SCANED_AP[ApNum++] = new WiFiAccessPoint(ap);
160-
161-
} else{
162-
// scan done
163-
164-
rtw_up_sema(&scan_sema);
165-
}
166-
return RTW_SUCCESS;
167-
}
168-
169184
nsapi_error_t RTWInterface::scan(WiFiAccessPoint *res, unsigned count)
170185
{
171-
// blocked
172-
if(count == 0){
173-
ApNum = 0;
174-
175-
rtw_init_sema(&scan_sema, 0);
176-
if(wifi_scan_networks(scan_result_handler, NULL) != RTW_SUCCESS){
177-
printf("wifi scan failed\n\r");
178-
//return NSAPI_ERROR_DEVICE_ERROR;
179-
goto error;
180-
}
181-
182-
if(rtw_down_timeout_sema( &scan_sema, 15000 ) == RTW_FALSE) {
183-
printf("wifi scan timeout\r\n");
184-
//return NSAPI_ERROR_DEVICE_ERROR;
185-
goto error;
186-
}
187-
rtw_free_sema(&scan_sema);
188-
return ApNum;
189-
}else if(count > 0 && res != NULL){
190-
count = count < maxApNum ? count : maxApNum;
191-
for(int i = 0; i < count; i++){
192-
memcpy(&res[i], SCANED_AP[i], sizeof(WiFiAccessPoint));
193-
delete[] SCANED_AP[i];
194-
}
195-
return (signed int)count;
196-
}
197-
return NSAPI_ERROR_OK;
198-
error:
199-
rtw_free_sema(&scan_sema);
200-
return NSAPI_ERROR_DEVICE_ERROR;
186+
static wifi_scan_hdl scan_handler;
187+
ApNum = 0;
188+
if(!scan_sema)
189+
rtw_init_sema(&scan_sema, 0);
190+
scan_handler.scan_num = count;
191+
scan_handler.ap_details = res;
192+
if(wifi_scan_networks(scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS){
193+
printf("wifi scan failed\n\r");
194+
return NSAPI_ERROR_DEVICE_ERROR;
195+
}
196+
if(rtw_down_timeout_sema( &scan_sema, MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
197+
printf("wifi scan timeout\r\n");
198+
return NSAPI_ERROR_DEVICE_ERROR;
199+
}
200+
if(count <= 0 || count > ApNum)
201+
count = ApNum;
202+
203+
return count;
201204
}
202205

203206
nsapi_error_t RTWInterface::set_channel(uint8_t channel)
204207
{
205-
if(wifi_set_channel(channel) == 0)
206-
return NSAPI_ERROR_OK;
207-
return NSAPI_ERROR_DEVICE_ERROR;
208+
_channel = channel;
209+
return NSAPI_ERROR_OK;
208210
}
209211

210212
int8_t RTWInterface::get_rssi()
211213
{
212-
return NSAPI_ERROR_UNSUPPORTED;
214+
int rssi = 0;
215+
if(wifi_get_rssi(&rssi) == 0)
216+
return (int8_t)rssi;
217+
return NSAPI_ERROR_OK;
213218
}
214219

215220
nsapi_error_t RTWInterface::connect(const char *ssid, const char *pass,
216221
nsapi_security_t security, uint8_t channel)
217222
{
218223
set_credentials(ssid, pass, security);
224+
set_channel(channel);
219225
return connect();
220226
}
221227

222228
nsapi_error_t RTWInterface::disconnect()
223229
{
224-
char essid[33];
230+
char essid[33];
225231

232+
wlan_emac_link_change(false);
226233
if(wifi_is_connected_to_ap() != RTW_SUCCESS)
227-
return NSAPI_ERROR_NO_CONNECTION;
228-
printf("Deassociating AP ...\r\n");
229-
if(wifi_disconnect()<0){
230-
return NSAPI_ERROR_DEVICE_ERROR;
231-
}
232-
233-
while(1){
234-
if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
235-
printf("WIFI disconnected\n\r");
236-
break;
237-
}
238-
}
239-
234+
return NSAPI_ERROR_NO_CONNECTION;
235+
//printf("Deassociating AP ...\r\n");
236+
if(wifi_disconnect()<0){
237+
return NSAPI_ERROR_DEVICE_ERROR;
238+
}
239+
while(1){
240+
if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
241+
//printf("WIFI disconnected\n\r");
242+
break;
243+
}
244+
}
240245
return NSAPI_ERROR_OK;
241246
}
242247

targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ class RTWInterface: public WiFiInterface
3535
/** RTWWlanInterface lifetime
3636
*/
3737
RTWInterface();
38-
39-
virtual nsapi_error_t init();
40-
virtual nsapi_error_t deinit();
38+
~RTWInterface();
4139

4240
/** Set a static IP address
4341
*
@@ -108,7 +106,7 @@ class RTWInterface: public WiFiInterface
108106
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
109107
* see @a nsapi_error
110108
*/
111-
virtual nsapi_error_t scan(WiFiAccessPoint *res, unsigned count);
109+
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, unsigned count);
112110

113111
virtual nsapi_error_t set_channel(uint8_t channel);
114112
virtual int8_t get_rssi();
@@ -156,6 +154,7 @@ class RTWInterface: public WiFiInterface
156154
char _ssid[256];
157155
char _pass[256];
158156
nsapi_security_t _security;
157+
uint8_t _channel;
159158
char _ip_address[IPADDR_STRLEN_MAX];
160159
char _netmask[NSAPI_IPv4_SIZE];
161160
char _gateway[NSAPI_IPv4_SIZE];

0 commit comments

Comments
 (0)