Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WIFI support for RDA target UNO_91H #9501

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TESTS/network/emac/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
!defined(TARGET_MTB_ADV_WISE_1530) && \
!defined(TARGET_MTB_USI_WM_BN_BM_22) && \
!defined(TARGET_MTB_MXCHIP_EMW3166) && \
!defined(TARGET_MTB_UBLOX_ODIN_W2)
!defined(TARGET_MTB_UBLOX_ODIN_W2) && \
!defined(TARGET_UNO_91H)
#error [NOT_SUPPORTED] Wifi tests are not valid for the target
#endif
#endif
Expand Down
217 changes: 217 additions & 0 deletions features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/* Copyright (c) 2019 Unisoc Communications Inc.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "WiFiInterface.h"
#include "RdaWiFiInterface.h"
#include "rda5991h_wland.h"
#include "nsapi_types.h"
#include "wland_types.h"
#include "rda_sys_wrapper.h"

nsapi_error_t RDAWiFiInterface::set_channel(uint8_t channel)
{
int ret= 0;
init();

if (channel > 13)
return NSAPI_ERROR_PARAMETER;

if (channel == 0) {
_channel = 0;
return NSAPI_ERROR_OK;
}

ret = rda5981_set_channel(channel);
if (ret == 0) {
_channel = channel;
return NSAPI_ERROR_OK;
} else {
return NSAPI_ERROR_TIMEOUT;
}
}

int8_t RDAWiFiInterface::get_rssi()
{
return rda5981_get_rssi();
}

nsapi_error_t RDAWiFiInterface::init()
{
if (!_interface) {
if (!_emac.power_up()) {
LWIP_DEBUGF(NETIF_DEBUG,"power up failed!\n");
}
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
if (err != NSAPI_ERROR_OK) {
_interface = NULL;
return err;
}
_interface->attach(_connection_status_cb);
}
return NSAPI_ERROR_OK;
}

nsapi_error_t RDAWiFiInterface::set_credentials(const char *ssid, const char *pass,
nsapi_security_t security)
{
if (ssid == 0 || strlen(ssid) == 0) {
return NSAPI_ERROR_PARAMETER;
}
if (security != NSAPI_SECURITY_NONE && (pass == 0 || strlen(pass) == 0)) {
return NSAPI_ERROR_PARAMETER;
}
if (strlen(ssid) > 32 || strlen(pass) > 63) {
return NSAPI_ERROR_PARAMETER;
}
memcpy((void*)_ssid, (void*)ssid, strlen(ssid));
_ssid[strlen(ssid)] = '\0';
memcpy((void*)_pass, (void*)pass, strlen(pass));
_pass[strlen(pass)] = '\0';
_security = security;
return NSAPI_ERROR_OK;
}

nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
nsapi_security_t security, uint8_t channel)
{
rda_msg msg;
bool find = false;
int i = 0;
rda5981_scan_result bss;
int ret = 0;

if (ssid == NULL || ssid[0] == 0) {
return NSAPI_ERROR_PARAMETER;
}

init();

if(rda5981_check_scan_result_name(ssid) != 0) {
for (i = 0; i< 5; i++) {
rda5981_scan(NULL, 0, 0);
if(rda5981_check_scan_result_name(ssid) == 0) {
find = true;
break;
}
}
} else {
find = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our style guide requires one line if-else clauses to have brackets as well.

}

if (find == false) {
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}
bss.channel = 15;
rda5981_get_scan_result_name(&bss, ssid);
if ((channel !=0) && (bss.channel != channel)) {
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}

memcpy(gssid, ssid, strlen(ssid));
if (pass[0] != 0) {
memcpy(gpass, pass, strlen(pass));
}
memset(gbssid, 0, NSAPI_MAC_BYTES);
gssid[strlen(ssid)] = gpass[strlen(pass)] = '\0';

msg.type = WLAND_CONNECT;
rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever);
ret = rda_sem_wait(wifi_auth_sem, 10000);
if (ret) {
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}

ret = _interface->bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0,
DEFAULT_STACK,
_blocking);

return ret;
}


nsapi_error_t RDAWiFiInterface::connect()
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the point why this cannot call RDAWiFiInterface::connect(const char *ssid, const char *paswd, nsapi_security_t security, uint8_t channel)

return connect(_ssid, _pass, _security, _channel);
}

nsapi_error_t RDAWiFiInterface::disconnect()
{
rda_msg msg;

if(sta_state < 2) {
return NSAPI_ERROR_NO_CONNECTION;
}
msg.type = WLAND_DISCONNECT;
rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever);
if (_interface) {
return _interface->bringdown();
}

return NSAPI_ERROR_NO_CONNECTION;
}

nsapi_size_or_error_t RDAWiFiInterface::scan(WiFiAccessPoint *res, nsapi_size_t count)
{
int bss_num = 0, i;
rda5981_scan_result *bss;
nsapi_wifi_ap_t ap;

init();

rda5981_scan(NULL, 0, 0);
bss_num = rda5981_get_scan_num();
if (count != 0) {
bss_num = (bss_num < count) ? bss_num : count;
}
if (res) {
bss = (rda5981_scan_result *)malloc(bss_num * sizeof(rda5981_scan_result));
rda5981_get_scan_result(bss, bss_num);
for (i=0; i<bss_num; i++) {
memset(&ap, 0, sizeof(nsapi_wifi_ap_t));
memcpy(ap.bssid, bss[i].BSSID, 6);
memcpy(ap.ssid, bss[i].SSID, bss[i].SSID_len);
ap.channel = bss[i].channel;
ap.rssi = bss[i].RSSI;
if (bss[i].secure_type == ENCRYPT_NONE) {
ap.security = NSAPI_SECURITY_NONE;
} else if(bss[i].secure_type & ENCRYPT_WEP) {
ap.security = NSAPI_SECURITY_WEP;
} else if((bss[i].secure_type & (ENCRYPT_WPA_TKIP | ENCRYPT_WPA_CCMP)) && \
(bss[i].secure_type & (ENCRYPT_WPA2_TKIP | ENCRYPT_WPA2_CCMP))) {
ap.security = NSAPI_SECURITY_WPA_WPA2;
} else if((bss[i].secure_type & (ENCRYPT_WPA_TKIP | ENCRYPT_WPA_CCMP))) {
ap.security = NSAPI_SECURITY_WPA;
} else {
ap.security = NSAPI_SECURITY_WPA2;
}
WiFiAccessPoint ap_temp(ap);
memcpy(&res[i], &ap_temp, sizeof(WiFiAccessPoint));
}
free(bss);
}
return bss_num;

}

WiFiInterface *WiFiInterface::get_default_instance() {
static RDAWiFiInterface wifinet;
return &wifinet;
}
137 changes: 137 additions & 0 deletions features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* LWIP implementation of NetworkInterfaceAPI
* Copyright (c) 2019 Unisoc Communications Inc.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef RDA_WIFI_INTERFACE_H
#define RDA_WIFI_INTERFACE_H

#include "nsapi.h"
#include "rtos.h"
#include "EMACInterface.h"
#include "WiFiInterface.h"


/** RDAWiFiInterface class
* Implementation of the NetworkStack for an EMAC-based Ethernet driver
*/
class RDAWiFiInterface : public EMACInterface, public WiFiInterface
{
public:
/** Create an EMAC-based ethernet interface.
*
* The default arguments obtain the default EMAC, which will be target-
* dependent (and the target may have some JSON option to choose which
* is the default, if there are multiple). The default stack is configured
* by JSON option nsapi.default-stack.
*
* Due to inability to return errors from the constructor, no real
* work is done until the first call to connect().
*
* @param emac Reference to EMAC to use
* @param stack Reference to onboard-network stack to use
*/
RDAWiFiInterface(
EMAC &emac = EMAC::get_default_instance(),
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance()) : EMACInterface(emac, stack) {
_ssid[0] = '\0';
_pass[0] = '\0';
_channel = 0;
_security = NSAPI_SECURITY_NONE;
}

//static RDAWiFiInterface *get_target_default_instance();

/** Set the WiFi network credentials
*
* @param ssid Name of the network to connect to
* @param pass Security passphrase to connect to the network
* @param security Type of encryption for connection
* (defaults to NSAPI_SECURITY_NONE)
* @return 0 on success, or error code on failure
*/
virtual nsapi_error_t set_credentials(const char *ssid, const char *pass,
nsapi_security_t security = NSAPI_SECURITY_NONE);

/** Set the WiFi network channel
*
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
* @return 0 on success, or error code on failure
*/
virtual nsapi_error_t set_channel(uint8_t channel);

/** Gets the current radio signal strength for active connection
*
* @return Connection strength in dBm (negative value),
* or 0 if measurement impossible
*/
virtual int8_t get_rssi();

/** Start the interface
*
* Attempts to connect to a WiFi network.
*
* @param ssid Name of the network to connect to
* @param pass Security passphrase to connect to the network
* @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
* @return 0 on success, or error code on failure
*/
virtual nsapi_error_t connect(const char *ssid, const char *pass,
nsapi_security_t security = NSAPI_SECURITY_NONE, uint8_t channel = 0);

/** Start the interface
*
* Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
* If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
*
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t connect();

/** Stop the interface
*
* @return 0 on success, or error code on failure
*/
virtual nsapi_error_t disconnect();

/** Scan for available networks
*
* This function will block. If the @a count is 0, function will only return count of available networks, so that
* 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
* with discovered networks up to value of \p count.
*
* @param res Pointer to allocated array to store discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP
* @return Number of entries in \p count, or if \p count was 0 number of available networks,
* negative on error see @a nsapi_error
*/
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count);

virtual nsapi_size_or_error_t init();
private:
char _ssid[33];
char _pass[65];
uint8_t _channel;
nsapi_security_t _security;


};

#endif




Loading