Skip to content

add-rtl8195am-feature-emac #6904

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

Merged
merged 6 commits into from
May 18, 2018
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
2 changes: 1 addition & 1 deletion TESTS/network/emac/template_mbed_app.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"echo-server-trace": {
"help": "Trace incoming messages on echo server",
"value": 0
},
},
"wifi-scan": {
"help": "Scan and list access points",
"value": 0
Expand Down
4 changes: 0 additions & 4 deletions targets/TARGET_Realtek/TARGET_AMEBA/.mbedignore

This file was deleted.

138 changes: 68 additions & 70 deletions targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

#include "RTWInterface.h"
#include "mbed_interface.h"
#include "rtw_emac.h"

#include "rtw_emac.h"
#include "EMAC.h"
#include "wifi_constants.h"
#include "wifi_conf.h"
#include "lwip_stack.h"

#include "OnboardNetworkStack.h"
#include "EMACMemoryManager.h"
#include "osdep_service.h"

typedef struct _wifi_scan_hdl {
Expand All @@ -42,14 +44,14 @@ static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_sca
{
wifi_scan_hdl *scan_handler = (wifi_scan_hdl *)malloced_scan_result->user_data;
if (malloced_scan_result->scan_complete != RTW_TRUE) {
if(scan_handler->ap_details && scan_handler->scan_num > scan_handler->ap_num){
if (scan_handler->ap_details && scan_handler->scan_num > scan_handler->ap_num) {
nsapi_wifi_ap_t ap;
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
memset((void*)&ap, 0x00, sizeof(nsapi_wifi_ap_t));
memcpy(ap.ssid, record->SSID.val, record->SSID.len);
memcpy(ap.bssid, record->BSSID.octet, 6);
switch (record->security){
switch (record->security) {
case RTW_SECURITY_OPEN:
ap.security = NSAPI_SECURITY_NONE;
break;
Expand Down Expand Up @@ -78,41 +80,30 @@ static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_sca
scan_handler->ap_details[scan_handler->ap_num] = WiFiAccessPoint(ap);
}
scan_handler->ap_num++;
} else{
} else {
// scan done
rtw_up_sema(&scan_handler->scan_sema);
}
return RTW_SUCCESS;
}

RTWInterface::RTWInterface(bool debug)
: _dhcp(true), _ssid(), _pass(), _ip_address(), _netmask(), _gateway()
RTWInterface::RTWInterface(RTW_EMAC &get_rtw_emac, OnboardNetworkStack &get_rtw_obn_stack) :
rtw_emac(get_rtw_emac),
rtw_obn_stack(get_rtw_obn_stack),
rtw_interface(NULL),
_dhcp(true),
_ip_address(),
_netmask(),
_gateway(),
_mac_address()
{
emac_interface_t *emac;
int ret;
extern u32 GlobalDebugEnable;

GlobalDebugEnable = debug?1:0;
emac = wlan_emac_init_interface();
if (!emac) {
printf("Error init RTWInterface!\r\n");
return;
}
emac->ops.power_up(emac);
if (_inited == false) {
ret = mbed_lwip_init(emac);
if (ret != 0) {
printf("Error init RTWInterface!(%d)\r\n", ret);
return;
}
_inited = true;
}
rtw_emac.power_up();
}

RTWInterface::~RTWInterface()
{
wlan_emac_link_change(false);
mbed_lwip_bringdown();
rtw_emac.wlan_emac_link_change(false);
rtw_interface->bringdown();
}

nsapi_error_t RTWInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
Expand All @@ -135,7 +126,7 @@ nsapi_error_t RTWInterface::set_dhcp(bool dhcp)
*/
nsapi_error_t RTWInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
if(!ssid || (strlen(ssid) == 0)) {
if (!ssid) {
return NSAPI_ERROR_PARAMETER;
}

Expand All @@ -144,14 +135,11 @@ nsapi_error_t RTWInterface::set_credentials(const char *ssid, const char *pass,
case NSAPI_SECURITY_WPA2:
case NSAPI_SECURITY_WPA_WPA2:
case NSAPI_SECURITY_WEP:
if((strlen(pass) < 8) || (strlen(pass) > 63)) { // 802.11 password 8-63 characters
if ((strlen(pass) < 8) || (strlen(pass) > 63)) { // 802.11 password 8-63 characters
return NSAPI_ERROR_PARAMETER;
}
break;
case NSAPI_SECURITY_NONE:
if(pass && strlen(pass) > 0) {
return NSAPI_ERROR_PARAMETER;
}
break;
default:
return NSAPI_ERROR_PARAMETER;
Expand All @@ -169,8 +157,7 @@ nsapi_error_t RTWInterface::connect()
int ret;
rtw_security_t sec;

if (!_ssid || (strlen(_ssid) == 0) ||
(!_pass && _security != NSAPI_SECURITY_NONE)) {
if (!_ssid || (!_pass && _security != NSAPI_SECURITY_NONE)) {
printf("Invalid credentials\r\n");
return NSAPI_ERROR_PARAMETER;
}
Expand All @@ -191,85 +178,94 @@ nsapi_error_t RTWInterface::connect()
return NSAPI_ERROR_PARAMETER;
}

if (_channel > 0 && _channel < 14) {
uint8_t pscan_config = PSCAN_ENABLE;
wifi_set_pscan_chan(&_channel, &pscan_config, 1);
}

ret = wifi_connect(_ssid, sec, _pass, strlen(_ssid), strlen(_pass), 0, (void *)NULL);
if (ret != RTW_SUCCESS) {
printf("failed: %d\r\n", ret);
return NSAPI_ERROR_NO_CONNECTION;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As Mika mentioned, you will still need a "link up" callback here after the "wifi_connect", to ensure the upcall occurs if this is the second connect, and the stack is already bound to you. That's the direct mirror of the "link down" callback from "disconnect".

The callback in "power_up" then handles the case of the first connect, when the stack wasn't attached to hear this link-up callback.

Copy link
Contributor Author

@M-ichae-l M-ichae-l May 17, 2018

Choose a reason for hiding this comment

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

Thanks. Will add rtw_emac.wlan_emac_link_change(true); after wifi_connect .


wlan_emac_link_change(true);
return mbed_lwip_bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0);
rtw_emac.wlan_emac_link_change(true);
if (!rtw_interface) {
nsapi_error_t err = rtw_obn_stack.add_ethernet_interface(rtw_emac, true, &rtw_interface);
if (err != NSAPI_ERROR_OK) {
rtw_interface = NULL;
return err;
}
}

int rtw_if_bringup = rtw_interface->bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0,
DEFAULT_STACK);
return rtw_if_bringup;
}

nsapi_error_t RTWInterface::scan(WiFiAccessPoint *res, unsigned count)
{
static wifi_scan_hdl scan_handler;
scan_handler.ap_num = 0;
if(!scan_handler.scan_sema)
if (!scan_handler.scan_sema) {
rtw_init_sema(&scan_handler.scan_sema, 0);
}
scan_handler.scan_num = count;
scan_handler.ap_details = res;
if(wifi_scan_networks(scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS){
if (wifi_scan_networks(scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS) {
printf("wifi scan failed\n\r");
return NSAPI_ERROR_DEVICE_ERROR;
}
if(rtw_down_timeout_sema( &scan_handler.scan_sema, MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
if (rtw_down_timeout_sema( &scan_handler.scan_sema, MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
printf("wifi scan timeout\r\n");
return NSAPI_ERROR_DEVICE_ERROR;
}
if(count <= 0 || count > scan_handler.ap_num)
if (count <= 0 || count > scan_handler.ap_num) {
count = scan_handler.ap_num;

}
return count;
}

nsapi_error_t RTWInterface::set_channel(uint8_t channel)
{
// Not supported for STA mode wifi driver
if (channel != 0)
return NSAPI_ERROR_UNSUPPORTED;

_channel = channel;
return NSAPI_ERROR_OK;
}

int8_t RTWInterface::get_rssi()
{
int rssi = 0;
if(wifi_get_rssi(&rssi) == 0)
if (wifi_get_rssi(&rssi) == 0) {
return (int8_t)rssi;
}
return NSAPI_ERROR_OK;
}

nsapi_error_t RTWInterface::connect(const char *ssid, const char *pass,
nsapi_security_t security, uint8_t channel)
{
nsapi_error_t ret;

ret = set_credentials(ssid, pass, security);
if(ret != NSAPI_ERROR_OK) return ret;

ret = set_channel(channel);
if(ret != NSAPI_ERROR_OK) return ret;

set_credentials(ssid, pass, security);
set_channel(channel);
return connect();
}

nsapi_error_t RTWInterface::disconnect()
{
char essid[33];

wlan_emac_link_change(false);
mbed_lwip_bringdown();
if(wifi_is_connected_to_ap() != RTW_SUCCESS)
rtw_emac.wlan_emac_link_change(false);
rtw_interface->bringdown();
if (wifi_is_connected_to_ap() != RTW_SUCCESS) {
return NSAPI_ERROR_NO_CONNECTION;
if(wifi_disconnect()<0){
}
if (wifi_disconnect()<0) {
return NSAPI_ERROR_DEVICE_ERROR;
}
while(1){
if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
while(1) {
if (wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
break;
}
}
Expand All @@ -278,40 +274,42 @@ nsapi_error_t RTWInterface::disconnect()

int RTWInterface::is_connected()
{
// wifi_is_connected_to_ap return 0 on connected
return !wifi_is_connected_to_ap();
}

const char *RTWInterface::get_mac_address()
{
return mbed_lwip_get_mac_address();
if (rtw_interface->get_mac_address(_mac_address, sizeof _mac_address)) {
return _mac_address;
}
return 0;
}

const char *RTWInterface::get_ip_address()
{
if (mbed_lwip_get_ip_address(_ip_address, sizeof _ip_address)) {
if (rtw_interface->get_ip_address(_ip_address, sizeof _ip_address)) {
return _ip_address;
}
return 0;
}

const char *RTWInterface::get_netmask()
{
if (mbed_lwip_get_netmask(_netmask, sizeof _netmask)) {
if (rtw_interface->get_netmask(_netmask, sizeof _netmask)) {
return _netmask;
}
return 0;
}

const char *RTWInterface::get_gateway()
{
if (mbed_lwip_get_gateway(_gateway, sizeof _gateway)) {
if (rtw_interface->get_gateway(_gateway, sizeof _gateway)) {
return _gateway;
}
return 0;
}

NetworkStack *RTWInterface::get_stack()
{
return nsapi_create_stack(&lwip_stack);
return &rtw_obn_stack;
}
22 changes: 17 additions & 5 deletions targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#include "netsocket/WiFiInterface.h"
#include "nsapi.h"
#include "rtos.h"
#include "lwip/netif.h"
#include "netif.h"
#include "rtw_emac.h"
#include "OnboardNetworkStack.h"
#include "LWIPStack.h"

// Forward declaration
class NetworkStack;
Expand All @@ -34,7 +37,10 @@ class RTWInterface: public WiFiInterface
public:
/** RTWWlanInterface lifetime
*/
RTWInterface(bool debug=false);
RTWInterface(
RTW_EMAC &rtw_emac = RTW_EMAC::get_instance(),
OnboardNetworkStack &rtw_lwip_stack = OnboardNetworkStack::get_default_instance());

~RTWInterface();

/** Set a static IP address
Expand Down Expand Up @@ -143,21 +149,27 @@ class RTWInterface: public WiFiInterface
*/
virtual const char *get_gateway();

RTW_EMAC &get_emac() const { return rtw_emac; }

virtual RTWInterface *rtwInterface() { return this; }

protected:
/** Provide access to the underlying stack
*
* @return The underlying network stack
*/
virtual NetworkStack *get_stack();

RTW_EMAC &rtw_emac;
OnboardNetworkStack &rtw_obn_stack;
OnboardNetworkStack::Interface *rtw_interface;
bool _dhcp;
char _ssid[256];
char _pass[256];
nsapi_security_t _security;
uint8_t _channel;
char _ip_address[IPADDR_STRLEN_MAX];
char _netmask[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];
char _mac_address[NSAPI_MAC_SIZE];
};

#endif
Loading