Skip to content

Commit 3e6cd4e

Browse files
author
Cruz Monrreal
authored
Merge pull request #9501 from caixue1102/mbed-os-rda
Add WIFI support for RDA target UNO_91H
2 parents 0ccb4dd + d92e33d commit 3e6cd4e

33 files changed

+6066
-24
lines changed

TESTS/network/emac/main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
!defined(TARGET_MTB_ADV_WISE_1530) && \
3636
!defined(TARGET_MTB_USI_WM_BN_BM_22) && \
3737
!defined(TARGET_MTB_MXCHIP_EMW3166) && \
38-
!defined(TARGET_MTB_UBLOX_ODIN_W2)
38+
!defined(TARGET_MTB_UBLOX_ODIN_W2) && \
39+
!defined(TARGET_UNO_91H)
3940
#error [NOT_SUPPORTED] Wifi tests are not valid for the target
4041
#endif
4142
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/* Copyright (c) 2019 Unisoc Communications Inc.
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "WiFiInterface.h"
18+
#include "RdaWiFiInterface.h"
19+
#include "rda5991h_wland.h"
20+
#include "nsapi_types.h"
21+
#include "wland_types.h"
22+
#include "rda_sys_wrapper.h"
23+
24+
nsapi_error_t RDAWiFiInterface::set_channel(uint8_t channel)
25+
{
26+
int ret= 0;
27+
init();
28+
29+
if (channel > 13)
30+
return NSAPI_ERROR_PARAMETER;
31+
32+
if (channel == 0) {
33+
_channel = 0;
34+
return NSAPI_ERROR_OK;
35+
}
36+
37+
ret = rda5981_set_channel(channel);
38+
if (ret == 0) {
39+
_channel = channel;
40+
return NSAPI_ERROR_OK;
41+
} else {
42+
return NSAPI_ERROR_TIMEOUT;
43+
}
44+
}
45+
46+
int8_t RDAWiFiInterface::get_rssi()
47+
{
48+
return rda5981_get_rssi();
49+
}
50+
51+
nsapi_error_t RDAWiFiInterface::init()
52+
{
53+
if (!_interface) {
54+
if (!_emac.power_up()) {
55+
LWIP_DEBUGF(NETIF_DEBUG,"power up failed!\n");
56+
}
57+
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
58+
if (err != NSAPI_ERROR_OK) {
59+
_interface = NULL;
60+
return err;
61+
}
62+
_interface->attach(_connection_status_cb);
63+
}
64+
return NSAPI_ERROR_OK;
65+
}
66+
67+
nsapi_error_t RDAWiFiInterface::set_credentials(const char *ssid, const char *pass,
68+
nsapi_security_t security)
69+
{
70+
if (ssid == 0 || strlen(ssid) == 0) {
71+
return NSAPI_ERROR_PARAMETER;
72+
}
73+
if (security != NSAPI_SECURITY_NONE && (pass == 0 || strlen(pass) == 0)) {
74+
return NSAPI_ERROR_PARAMETER;
75+
}
76+
if (strlen(ssid) > 32 || strlen(pass) > 63) {
77+
return NSAPI_ERROR_PARAMETER;
78+
}
79+
memcpy((void*)_ssid, (void*)ssid, strlen(ssid));
80+
_ssid[strlen(ssid)] = '\0';
81+
memcpy((void*)_pass, (void*)pass, strlen(pass));
82+
_pass[strlen(pass)] = '\0';
83+
_security = security;
84+
return NSAPI_ERROR_OK;
85+
}
86+
87+
nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
88+
nsapi_security_t security, uint8_t channel)
89+
{
90+
rda_msg msg;
91+
bool find = false;
92+
int i = 0;
93+
rda5981_scan_result bss;
94+
int ret = 0;
95+
96+
if (ssid == NULL || ssid[0] == 0) {
97+
return NSAPI_ERROR_PARAMETER;
98+
}
99+
100+
init();
101+
102+
if(rda5981_check_scan_result_name(ssid) != 0) {
103+
for (i = 0; i< 5; i++) {
104+
rda5981_scan(NULL, 0, 0);
105+
if(rda5981_check_scan_result_name(ssid) == 0) {
106+
find = true;
107+
break;
108+
}
109+
}
110+
} else {
111+
find = true;
112+
}
113+
114+
if (find == false) {
115+
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
116+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
117+
}
118+
bss.channel = 15;
119+
rda5981_get_scan_result_name(&bss, ssid);
120+
if ((channel !=0) && (bss.channel != channel)) {
121+
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
122+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
123+
}
124+
125+
memcpy(gssid, ssid, strlen(ssid));
126+
if (pass[0] != 0) {
127+
memcpy(gpass, pass, strlen(pass));
128+
}
129+
memset(gbssid, 0, NSAPI_MAC_BYTES);
130+
gssid[strlen(ssid)] = gpass[strlen(pass)] = '\0';
131+
132+
msg.type = WLAND_CONNECT;
133+
rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever);
134+
ret = rda_sem_wait(wifi_auth_sem, 10000);
135+
if (ret) {
136+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
137+
}
138+
139+
ret = _interface->bringup(_dhcp,
140+
_ip_address[0] ? _ip_address : 0,
141+
_netmask[0] ? _netmask : 0,
142+
_gateway[0] ? _gateway : 0,
143+
DEFAULT_STACK,
144+
_blocking);
145+
146+
return ret;
147+
}
148+
149+
150+
nsapi_error_t RDAWiFiInterface::connect()
151+
{
152+
return connect(_ssid, _pass, _security, _channel);
153+
}
154+
155+
nsapi_error_t RDAWiFiInterface::disconnect()
156+
{
157+
rda_msg msg;
158+
159+
if(sta_state < 2) {
160+
return NSAPI_ERROR_NO_CONNECTION;
161+
}
162+
msg.type = WLAND_DISCONNECT;
163+
rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever);
164+
if (_interface) {
165+
return _interface->bringdown();
166+
}
167+
168+
return NSAPI_ERROR_NO_CONNECTION;
169+
}
170+
171+
nsapi_size_or_error_t RDAWiFiInterface::scan(WiFiAccessPoint *res, nsapi_size_t count)
172+
{
173+
int bss_num = 0, i;
174+
rda5981_scan_result *bss;
175+
nsapi_wifi_ap_t ap;
176+
177+
init();
178+
179+
rda5981_scan(NULL, 0, 0);
180+
bss_num = rda5981_get_scan_num();
181+
if (count != 0) {
182+
bss_num = (bss_num < count) ? bss_num : count;
183+
}
184+
if (res) {
185+
bss = (rda5981_scan_result *)malloc(bss_num * sizeof(rda5981_scan_result));
186+
rda5981_get_scan_result(bss, bss_num);
187+
for (i=0; i<bss_num; i++) {
188+
memset(&ap, 0, sizeof(nsapi_wifi_ap_t));
189+
memcpy(ap.bssid, bss[i].BSSID, 6);
190+
memcpy(ap.ssid, bss[i].SSID, bss[i].SSID_len);
191+
ap.channel = bss[i].channel;
192+
ap.rssi = bss[i].RSSI;
193+
if (bss[i].secure_type == ENCRYPT_NONE) {
194+
ap.security = NSAPI_SECURITY_NONE;
195+
} else if(bss[i].secure_type & ENCRYPT_WEP) {
196+
ap.security = NSAPI_SECURITY_WEP;
197+
} else if((bss[i].secure_type & (ENCRYPT_WPA_TKIP | ENCRYPT_WPA_CCMP)) && \
198+
(bss[i].secure_type & (ENCRYPT_WPA2_TKIP | ENCRYPT_WPA2_CCMP))) {
199+
ap.security = NSAPI_SECURITY_WPA_WPA2;
200+
} else if((bss[i].secure_type & (ENCRYPT_WPA_TKIP | ENCRYPT_WPA_CCMP))) {
201+
ap.security = NSAPI_SECURITY_WPA;
202+
} else {
203+
ap.security = NSAPI_SECURITY_WPA2;
204+
}
205+
WiFiAccessPoint ap_temp(ap);
206+
memcpy(&res[i], &ap_temp, sizeof(WiFiAccessPoint));
207+
}
208+
free(bss);
209+
}
210+
return bss_num;
211+
212+
}
213+
214+
WiFiInterface *WiFiInterface::get_default_instance() {
215+
static RDAWiFiInterface wifinet;
216+
return &wifinet;
217+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* LWIP implementation of NetworkInterfaceAPI
2+
* Copyright (c) 2019 Unisoc Communications Inc.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef RDA_WIFI_INTERFACE_H
19+
#define RDA_WIFI_INTERFACE_H
20+
21+
#include "nsapi.h"
22+
#include "rtos.h"
23+
#include "EMACInterface.h"
24+
#include "WiFiInterface.h"
25+
26+
27+
/** RDAWiFiInterface class
28+
* Implementation of the NetworkStack for an EMAC-based Ethernet driver
29+
*/
30+
class RDAWiFiInterface : public EMACInterface, public WiFiInterface
31+
{
32+
public:
33+
/** Create an EMAC-based ethernet interface.
34+
*
35+
* The default arguments obtain the default EMAC, which will be target-
36+
* dependent (and the target may have some JSON option to choose which
37+
* is the default, if there are multiple). The default stack is configured
38+
* by JSON option nsapi.default-stack.
39+
*
40+
* Due to inability to return errors from the constructor, no real
41+
* work is done until the first call to connect().
42+
*
43+
* @param emac Reference to EMAC to use
44+
* @param stack Reference to onboard-network stack to use
45+
*/
46+
RDAWiFiInterface(
47+
EMAC &emac = EMAC::get_default_instance(),
48+
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance()) : EMACInterface(emac, stack) {
49+
_ssid[0] = '\0';
50+
_pass[0] = '\0';
51+
_channel = 0;
52+
_security = NSAPI_SECURITY_NONE;
53+
}
54+
55+
//static RDAWiFiInterface *get_target_default_instance();
56+
57+
/** Set the WiFi network credentials
58+
*
59+
* @param ssid Name of the network to connect to
60+
* @param pass Security passphrase to connect to the network
61+
* @param security Type of encryption for connection
62+
* (defaults to NSAPI_SECURITY_NONE)
63+
* @return 0 on success, or error code on failure
64+
*/
65+
virtual nsapi_error_t set_credentials(const char *ssid, const char *pass,
66+
nsapi_security_t security = NSAPI_SECURITY_NONE);
67+
68+
/** Set the WiFi network channel
69+
*
70+
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
71+
* @return 0 on success, or error code on failure
72+
*/
73+
virtual nsapi_error_t set_channel(uint8_t channel);
74+
75+
/** Gets the current radio signal strength for active connection
76+
*
77+
* @return Connection strength in dBm (negative value),
78+
* or 0 if measurement impossible
79+
*/
80+
virtual int8_t get_rssi();
81+
82+
/** Start the interface
83+
*
84+
* Attempts to connect to a WiFi network.
85+
*
86+
* @param ssid Name of the network to connect to
87+
* @param pass Security passphrase to connect to the network
88+
* @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
89+
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
90+
* @return 0 on success, or error code on failure
91+
*/
92+
virtual nsapi_error_t connect(const char *ssid, const char *pass,
93+
nsapi_security_t security = NSAPI_SECURITY_NONE, uint8_t channel = 0);
94+
95+
/** Start the interface
96+
*
97+
* Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
98+
* If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
99+
*
100+
* @return 0 on success, negative error code on failure
101+
*/
102+
virtual nsapi_error_t connect();
103+
104+
/** Stop the interface
105+
*
106+
* @return 0 on success, or error code on failure
107+
*/
108+
virtual nsapi_error_t disconnect();
109+
110+
/** Scan for available networks
111+
*
112+
* This function will block. If the @a count is 0, function will only return count of available networks, so that
113+
* user can allocated necessary memory. If the \p count is grater than 0 and the a \p res is not NULL it'll be populated
114+
* with discovered networks up to value of \p count.
115+
*
116+
* @param res Pointer to allocated array to store discovered AP
117+
* @param count Size of allocated @a res array, or 0 to only count available AP
118+
* @return Number of entries in \p count, or if \p count was 0 number of available networks,
119+
* negative on error see @a nsapi_error
120+
*/
121+
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count);
122+
123+
virtual nsapi_size_or_error_t init();
124+
private:
125+
char _ssid[33];
126+
char _pass[65];
127+
uint8_t _channel;
128+
nsapi_security_t _security;
129+
130+
131+
};
132+
133+
#endif
134+
135+
136+
137+

0 commit comments

Comments
 (0)