Skip to content

Commit 88cf7df

Browse files
committed
1, Restrict access to 1st 4K of flash
2, Merge UNISOC change for auto reconnect patch
1 parent 0a2d843 commit 88cf7df

File tree

4 files changed

+158
-8
lines changed

4 files changed

+158
-8
lines changed

features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.cpp

Lines changed: 145 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,45 @@
2121
#include "wland_types.h"
2222
#include "rda_sys_wrapper.h"
2323

24+
typedef enum {
25+
WIFI_CONNECTED,
26+
WIFI_DISCONNECTED,
27+
}WIFI_STATE;
28+
29+
static WIFI_STATE wifi_state = WIFI_DISCONNECTED;
30+
31+
void daemon(void *para)
32+
{
33+
void *main_msgQ = NULL;
34+
rda_msg msg;
35+
int ret;
36+
RDAWiFiInterface *wifi = (RDAWiFiInterface *)para;
37+
main_msgQ = rda_mail_create(10, sizeof(unsigned int)*4);
38+
wifi->set_msg_queue(main_msgQ);
39+
while(1){
40+
rda_mail_get(main_msgQ, (void*)&msg, osWaitForever);
41+
switch(msg.type)
42+
{
43+
case MAIN_RECONNECT:
44+
printf("wifi disconnect!\r\n");
45+
ret = wifi->disconnect();
46+
if(ret != 0){
47+
printf("disconnect failed!\r\n");
48+
break;
49+
}
50+
ret = wifi->reconnect();
51+
while(ret != 0){
52+
osDelay(5*1000);
53+
ret = wifi->reconnect();
54+
};
55+
break;
56+
default:
57+
printf("unknown msg\r\n");
58+
break;
59+
}
60+
}
61+
}
62+
2463
nsapi_error_t RDAWiFiInterface::set_channel(uint8_t channel)
2564
{
2665
int ret= 0;
@@ -52,14 +91,15 @@ nsapi_error_t RDAWiFiInterface::init()
5291
{
5392
if (!_interface) {
5493
if (!_emac.power_up()) {
55-
LWIP_DEBUGF(NETIF_DEBUG,"power up failed!\n");
94+
LWIP_DEBUGF(NETIF_DEBUG,("power up failed!\n"));
5695
}
5796
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
5897
if (err != NSAPI_ERROR_OK) {
5998
_interface = NULL;
6099
return err;
61100
}
62101
_interface->attach(_connection_status_cb);
102+
rda_thread_new("daemon", daemon, this, DEFAULT_THREAD_STACKSIZE*4, osPriorityNormal);
63103
}
64104
return NSAPI_ERROR_OK;
65105
}
@@ -93,10 +133,17 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
93133
rda5981_scan_result bss;
94134
int ret = 0;
95135

136+
if(wifi_state == WIFI_CONNECTED) {
137+
return NSAPI_ERROR_IS_CONNECTED;
138+
}
139+
96140
if (ssid == NULL || ssid[0] == 0) {
97141
return NSAPI_ERROR_PARAMETER;
98142
}
99-
143+
144+
set_credentials(ssid, pass, security);
145+
set_channel(channel);
146+
100147
init();
101148

102149
if(rda5981_check_scan_result_name(ssid) != 0) {
@@ -112,13 +159,13 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
112159
}
113160

114161
if (find == false) {
115-
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
116-
return NSAPI_ERROR_CONNECTION_TIMEOUT;
162+
LWIP_DEBUGF(NETIF_DEBUG,("can not find the ap.\r\n"));
163+
return NSAPI_ERROR_NO_SSID;
117164
}
118165
bss.channel = 15;
119166
rda5981_get_scan_result_name(&bss, ssid);
120167
if ((channel !=0) && (bss.channel != channel)) {
121-
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
168+
LWIP_DEBUGF(NETIF_DEBUG, ("invalid channel\r\n"));
122169
return NSAPI_ERROR_CONNECTION_TIMEOUT;
123170
}
124171

@@ -142,6 +189,15 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
142189
_gateway[0] ? _gateway : 0,
143190
DEFAULT_STACK,
144191
_blocking);
192+
LWIP_DEBUGF(NETIF_DEBUG,("Interface bringup up status:%d\r\n",ret));
193+
194+
if( ret == NSAPI_ERROR_OK || ret == NSAPI_ERROR_IS_CONNECTED ) {
195+
ret = NSAPI_ERROR_OK;
196+
wifi_state = WIFI_CONNECTED;
197+
}
198+
else if( ret == NSAPI_ERROR_DHCP_FAILURE) {
199+
disconnect();
200+
}
145201

146202
return ret;
147203
}
@@ -156,9 +212,10 @@ nsapi_error_t RDAWiFiInterface::disconnect()
156212
{
157213
rda_msg msg;
158214

159-
if(sta_state < 2) {
215+
if(wifi_state == WIFI_DISCONNECTED) {
160216
return NSAPI_ERROR_NO_CONNECTION;
161217
}
218+
wifi_state = WIFI_DISCONNECTED;
162219
void* wifi_disconnect_sem = rda_sem_create(0);
163220
msg.type = WLAND_DISCONNECT;
164221
msg.arg1 = (unsigned int)wifi_disconnect_sem;
@@ -172,6 +229,81 @@ nsapi_error_t RDAWiFiInterface::disconnect()
172229
return NSAPI_ERROR_NO_CONNECTION;
173230
}
174231

232+
nsapi_error_t RDAWiFiInterface::reconnect()
233+
{
234+
rda_msg msg;
235+
bool find = false;
236+
int i = 0;
237+
rda5981_scan_result bss;
238+
int ret = 0;
239+
240+
if (_ssid == NULL || _ssid[0] == 0) {
241+
return NSAPI_ERROR_PARAMETER;
242+
}
243+
244+
rda5981_del_scan_all_result();
245+
if(rda5981_check_scan_result_name(_ssid) != 0) {
246+
for (i = 0; i< 5; i++) {
247+
rda5981_scan(NULL, 0, 0);
248+
if(rda5981_check_scan_result_name(_ssid) == 0) {
249+
find = true;
250+
break;
251+
}
252+
}
253+
} else {
254+
find = true;
255+
}
256+
257+
if (find == false) {
258+
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
259+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
260+
}
261+
bss.channel = 15;
262+
rda5981_get_scan_result_name(&bss, _ssid);
263+
if ((_channel !=0) && (bss.channel != _channel)) {
264+
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
265+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
266+
}
267+
268+
memcpy(gssid, _ssid, strlen(_ssid));
269+
if (_pass[0] != 0) {
270+
memcpy(gpass, _pass, strlen(_pass));
271+
}
272+
memset(gbssid, 0, NSAPI_MAC_BYTES);
273+
gssid[strlen(_ssid)] = gpass[strlen(_pass)] = '\0';
274+
275+
msg.type = WLAND_CONNECT;
276+
rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever);
277+
ret = rda_sem_wait(wifi_auth_sem, 10000);
278+
if (ret) {
279+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
280+
}
281+
282+
if(_dhcp) {
283+
memset(_ip_address, 0, sizeof(_ip_address));
284+
memset(_netmask, 0, sizeof(_netmask));
285+
memset(_gateway, 0, sizeof(_gateway));
286+
}
287+
288+
ret = _interface->bringup(_dhcp,
289+
_ip_address[0] ? _ip_address : 0,
290+
_netmask[0] ? _netmask : 0,
291+
_gateway[0] ? _gateway : 0,
292+
DEFAULT_STACK,
293+
_blocking);
294+
LWIP_DEBUGF(NETIF_DEBUG,("Interface bringup up status:%d\r\n",ret));
295+
296+
if( ret == NSAPI_ERROR_OK || ret == NSAPI_ERROR_IS_CONNECTED ) {
297+
ret = NSAPI_ERROR_OK;
298+
wifi_state = WIFI_CONNECTED;
299+
}
300+
else if( ret == NSAPI_ERROR_DHCP_FAILURE) {
301+
disconnect();
302+
}
303+
return ret;
304+
}
305+
306+
175307
nsapi_size_or_error_t RDAWiFiInterface::scan(WiFiAccessPoint *res, nsapi_size_t count)
176308
{
177309
int bss_num = 0, i;
@@ -219,3 +351,10 @@ WiFiInterface *WiFiInterface::get_default_instance() {
219351
static RDAWiFiInterface wifinet;
220352
return &wifinet;
221353
}
354+
355+
nsapi_size_or_error_t RDAWiFiInterface::set_msg_queue(void *queue)
356+
{
357+
//TO_DO: No need for 1st stage since application already control the logic.
358+
//rda5981_set_main_queue(queue);
359+
return 0;
360+
}

features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ class RDAWiFiInterface : public EMACInterface, public WiFiInterface
107107
*/
108108
virtual nsapi_error_t disconnect();
109109

110+
/** Restart the interface
111+
*
112+
* Attempts to reconnect to a WiFi network. Ssid and passphrase has been stored.
113+
*
114+
* @return 0 on success, negative error code on failure
115+
*/
116+
virtual nsapi_error_t reconnect();
117+
110118
/** Scan for available networks
111119
*
112120
* This function will block. If the @a count is 0, function will only return count of available networks, so that
@@ -121,6 +129,9 @@ class RDAWiFiInterface : public EMACInterface, public WiFiInterface
121129
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count);
122130

123131
virtual nsapi_size_or_error_t init();
132+
133+
virtual nsapi_size_or_error_t set_msg_queue(void *queue);
134+
124135
private:
125136
char _ssid[33];
126137
char _pass[65];

targets/TARGET_RDA/TARGET_UNO_91H/flash_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define MBED_FLASH_SIZE 0x100000
2323
#else
2424
//there is 4K BOOTROM at beginning of the flash
25-
#define MBED_FLASH_SIZE (MBED_ROM_SIZE+0x1000)
25+
#define MBED_FLASH_SIZE MBED_ROM_SIZE
2626
#endif
2727

2828

@@ -75,7 +75,7 @@ static const sector_info_t sectors_info[] = {
7575

7676
static const flash_target_config_t flash_target_config = {
7777
.page_size = 0x100,
78-
.flash_start = 0x18000000,
78+
.flash_start = 0x18001000,
7979
.flash_size = MBED_FLASH_SIZE,
8080
.sectors = sectors_info,
8181
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)

0 commit comments

Comments
 (0)