diff --git a/TESTS/network/emac/template_mbed_app.txt b/TESTS/network/emac/template_mbed_app.txt index 45856bd700..2306894a5a 100644 --- a/TESTS/network/emac/template_mbed_app.txt +++ b/TESTS/network/emac/template_mbed_app.txt @@ -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 diff --git a/targets/TARGET_Realtek/TARGET_AMEBA/.mbedignore b/targets/TARGET_Realtek/TARGET_AMEBA/.mbedignore deleted file mode 100644 index ad1d2bea66..0000000000 --- a/targets/TARGET_Realtek/TARGET_AMEBA/.mbedignore +++ /dev/null @@ -1,4 +0,0 @@ -rtw_emac.cpp -RTWInterface.cpp -sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.c -sdk/common/api/wifi/* diff --git a/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.cpp b/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.cpp index 6b64f8609b..6ae8b8306d 100644 --- a/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.cpp +++ b/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.cpp @@ -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 { @@ -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; @@ -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) @@ -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; } @@ -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; @@ -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; } @@ -191,69 +178,77 @@ 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; } - 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(); } @@ -261,15 +256,16 @@ 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; } } @@ -278,18 +274,20 @@ 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; @@ -297,7 +295,7 @@ const char *RTWInterface::get_ip_address() 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; @@ -305,7 +303,7 @@ const char *RTWInterface::get_netmask() 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; @@ -313,5 +311,5 @@ const char *RTWInterface::get_gateway() NetworkStack *RTWInterface::get_stack() { - return nsapi_create_stack(&lwip_stack); + return &rtw_obn_stack; } diff --git a/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.h b/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.h index f221efc744..26ae69cdfe 100644 --- a/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.h +++ b/targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.h @@ -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; @@ -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 @@ -143,13 +149,19 @@ public: */ 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]; @@ -157,7 +169,7 @@ protected: 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 diff --git a/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.cpp b/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.cpp index 7374c3c983..c79e0a4119 100644 --- a/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.cpp +++ b/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.cpp @@ -20,9 +20,10 @@ #include "mbed_assert.h" #include "mbed_events.h" -#include "emac_api.h" -#include "rtos.h" +#include "rtw_emac.h" +#include "EMACMemoryManager.h" +#include "rtos.h" #include "lwip/pbuf.h" #include "netif/etharp.h" @@ -32,39 +33,43 @@ #define RTW_EMAC_MTU_SIZE (1500U) -static emac_interface_t *_emac; -static emac_link_input_fn link_input_cb; -static emac_link_state_change_fn link_state_cb; -static void *link_input_data; -static void *link_state_data; +RTW_EMAC::RTW_EMAC() +{ + set_callback_func((emac_callback)(&RTW_EMAC::wlan_emac_recv), this); +} -static uint32_t wlan_get_mtu_size(emac_interface_t *emac) +uint32_t RTW_EMAC::get_mtu_size() const { return RTW_EMAC_MTU_SIZE; } -static void wlan_get_ifname(emac_interface_t *emac, char *name, uint8_t size) +uint32_t RTW_EMAC::get_align_preference() const +{ + return true; +} + +void RTW_EMAC::get_ifname(char *name, uint8_t size) const { MBED_ASSERT(name != NULL); strncpy(name, "r0", size); } -static uint8_t wlan_get_hwaddr_size(emac_interface_t *emac) +uint8_t RTW_EMAC::get_hwaddr_size() const { return ETHARP_HWADDR_LEN; } -static void wlan_get_hwaddr(emac_interface_t *emac, uint8_t *addr) +bool RTW_EMAC::get_hwaddr(uint8_t *addr) const { char mac[20]; int val[6]; int i; if (RTW_SUCCESS == wifi_get_mac_address(mac)) { - if (sscanf(mac, "%x:%x:%x:%x:%x:%x", - &val[0], &val[1], &val[2], &val[3], &val[4], &val[5]) != 6) + if (sscanf(mac, "%x:%x:%x:%x:%x:%x", + &val[0], &val[1], &val[2], &val[3], &val[4], &val[5]) != 6) { printf("Get HW address failed\r\n"); - + } for (i = 0; i < 6; i++) { addr[i] = (unsigned char) val[i]; } @@ -73,80 +78,93 @@ static void wlan_get_hwaddr(emac_interface_t *emac, uint8_t *addr) } } -static void wlan_set_hwaddr(emac_interface_t *emac, uint8_t *addr) +void RTW_EMAC::set_hwaddr(const uint8_t *addr) { - } - -static bool wlan_link_out(emac_interface_t *emac, emac_stack_mem_t *buf) +bool RTW_EMAC::link_out(emac_mem_buf_t *buf) { - struct eth_drv_sg * sg_list=0; + struct eth_drv_sg *sg_list; int sg_len = 0; int tot_len; - struct pbuf *p; + emac_mem_buf_t *p; bool ret = true; - if (!rltk_wlan_running(0)) { + memory_manager->free(buf); return false; } sg_list = (struct eth_drv_sg *)malloc(sizeof(struct eth_drv_sg)*MAX_ETH_DRV_SG); - if(sg_list == 0){//malloc fail + if (sg_list == 0) { + memory_manager->free(buf); return false; } - emac_stack_mem_ref(emac, buf); - p = (struct pbuf *)buf; - tot_len = p->tot_len; - for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = p->next) { - sg_list[sg_len].buf = (uint32_t) p->payload; - sg_list[sg_len].len = p->len; - sg_len++; + p = buf; + tot_len = memory_manager->get_total_len(p); + for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = memory_manager->get_next(p)) { + sg_list[sg_len].buf = (unsigned int)(static_cast(memory_manager->get_ptr(p))); + sg_list[sg_len].len = memory_manager->get_len(p); + sg_len++; } - if (sg_len) { if (rltk_wlan_send(0, sg_list, sg_len, tot_len) != 0) { ret = false; } } - emac_stack_mem_free(emac, buf); + memory_manager->free(buf); free(sg_list); return ret; } -static bool wlan_power_up(emac_interface_t *emac) +bool RTW_EMAC::power_up() { wifi_on(RTW_MODE_STA); wait_ms(1000); + wlan_emac_link_change(true); return true; } -static void wlan_power_down(emac_interface_t *emac) +void RTW_EMAC::power_down() { wifi_off(); } -static void wlan_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn cb, void *data) +void RTW_EMAC::set_link_input_cb(emac_link_input_cb_t input_cb) { - link_input_cb = cb; - link_input_data = data; + emac_link_input_cb = input_cb; } -static void wlan_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn cb, void *data) +void RTW_EMAC::set_link_state_cb(emac_link_state_change_cb_t state_cb) { - link_state_cb = cb; - link_state_data = data; + emac_link_state_cb = state_cb; } -void wlan_emac_recv(struct netif *netif, int len) +void RTW_EMAC::add_multicast_group(const uint8_t *addr) { - struct eth_drv_sg sg_list[MAX_ETH_DRV_SG]; - emac_stack_mem_t *buf; - struct pbuf *p; +} + +void RTW_EMAC::remove_multicast_group(const uint8_t *addr) +{ +} + +void RTW_EMAC::set_all_multicast(bool all) +{ +} + +void RTW_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr) +{ + memory_manager = &mem_mngr; +} + +void RTW_EMAC::wlan_emac_recv(void *param, struct netif *netif, uint32_t len) +{ + struct eth_drv_sg sg_list[MAX_ETH_DRV_SG] = {0}; + emac_mem_buf_t *buf; + RTW_EMAC *enet = static_cast(param); + emac_mem_buf_t *p; int sg_len = 0; - if (!rltk_wlan_running(0)) { return; } @@ -155,48 +173,33 @@ void wlan_emac_recv(struct netif *netif, int len) len = MAX_ETH_MSG; } - buf = emac_stack_mem_alloc(NULL, len, 0); + buf = enet->memory_manager->alloc_heap(len, 0); if (buf == NULL) { return; - } + } - p = (struct pbuf *)buf; - for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = p->next) { - sg_list[sg_len].buf = (uint32_t) p->payload; - sg_list[sg_len].len = p->len; + enet->memory_manager->set_len(buf, len); + p = buf; + for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = enet->memory_manager->get_next(p)) { + sg_list[sg_len].buf = (unsigned int)(static_cast(enet->memory_manager->get_ptr(p))); + sg_list[sg_len].len = enet->memory_manager->get_len(p); sg_len++; } + rltk_wlan_recv(0, sg_list, sg_len); - - if (link_input_cb) { - link_input_cb(link_input_data, buf); + if (enet->emac_link_input_cb) { + enet->emac_link_input_cb(buf); } - return; } -const emac_interface_ops_t wlan_emac_interface = { - .get_mtu_size = wlan_get_mtu_size, - .get_ifname = wlan_get_ifname, - .get_hwaddr_size = wlan_get_hwaddr_size, - .get_hwaddr = wlan_get_hwaddr, - .set_hwaddr = wlan_set_hwaddr, - .link_out = wlan_link_out, - .power_up = wlan_power_up, - .power_down = wlan_power_down, - .set_link_input_cb = wlan_set_link_input_cb, - .set_link_state_cb = wlan_set_link_state_cb -}; - void mbed_default_mac_address(char *mac) { unsigned char RTK_mac_addr[3] = {0x00, 0xE0, 0x4C}; // default Realtek mac address - mac[0] = RTK_mac_addr[0]; mac[1] = RTK_mac_addr[1]; mac[2] = RTK_mac_addr[2]; mac[3] = 0x87; mac[4] = 0x00; mac[5] = 0x01; - return; } @@ -205,12 +208,11 @@ void mbed_mac_address(char *mac) char hwaddr[20]; int val[6]; int i; - if (RTW_SUCCESS == wifi_get_mac_address(hwaddr)) { if (sscanf(hwaddr, "%x:%x:%x:%x:%x:%x", - &val[0], &val[1], &val[2], &val[3], &val[4], &val[5]) != 6) + &val[0], &val[1], &val[2], &val[3], &val[4], &val[5]) != 6) { printf("Get HW address failed\r\n"); - + } for (i = 0; i < 6; i++) { mac[i] = (unsigned char) val[i]; } @@ -220,22 +222,19 @@ void mbed_mac_address(char *mac) } } -void wlan_emac_link_change(bool up) +void RTW_EMAC::wlan_emac_link_change(bool up) { - if (link_state_cb) { - link_state_cb(link_state_data, up); + if (emac_link_state_cb) { + emac_link_state_cb(up); } } -emac_interface_t *wlan_emac_init_interface() -{ - - if (_emac == NULL) { - _emac = (emac_interface_t*) malloc(sizeof(emac_interface_t)); - MBED_ASSERT(_emac); - _emac->hw = NULL; - memcpy((void*)&_emac->ops, &wlan_emac_interface, sizeof(wlan_emac_interface)); - } - return _emac; +RTW_EMAC &RTW_EMAC::get_instance() { + static RTW_EMAC rtw_emac; + return rtw_emac; +} +// Weak so a module can override +MBED_WEAK EMAC &EMAC::get_default_instance() { + return RTW_EMAC::get_instance(); } #endif diff --git a/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.h b/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.h index 1e6165b49d..516bc35182 100644 --- a/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.h +++ b/targets/TARGET_Realtek/TARGET_AMEBA/rtw_emac.h @@ -13,13 +13,153 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MBED_WLAN_EMAC_H -#define MBED_WLAN_EMAC_H -#include "emac_api.h" +#ifndef RTW_EMAC_H +#define RTW_EMAC_H -extern void wlan_emac_link_change(bool up); -extern emac_interface_t *wlan_emac_init_interface(); -extern void wlan_emac_recv(struct netif *netif, uint32_t len); +#include "EMAC.h" +#include "rtos/Semaphore.h" +#include "rtos/Mutex.h" +#include "netif.h" +#include "EMACMemoryManager.h" + + +class RTW_EMAC : public EMAC { +public: + RTW_EMAC(); + + static RTW_EMAC &get_instance(); + + /** + * Return maximum transmission unit + * + * @return MTU in bytes + */ + virtual uint32_t get_mtu_size() const; + + /** + * Gets memory buffer alignment preference + * + * Gets preferred memory buffer alignment of the Emac device. IP stack may or may not + * align link out memory buffer chains using the alignment. + * + * @return Memory alignment requirement in bytes + */ + virtual uint32_t get_align_preference() const; + + /** + * Return interface name + * + * @param name Pointer to where the name should be written + * @param size Maximum number of character to copy + */ + virtual void get_ifname(char *name, uint8_t size) const; + + /** + * Returns size of the underlying interface HW address size. + * + * @return HW address size in bytes + */ + virtual uint8_t get_hwaddr_size() const; + + /** + * Return interface-supplied HW address + * + * Copies HW address to provided memory, @param addr has to be of correct size see @a get_hwaddr_size + * + * HW address need not be provided if this interface does not have its own HW + * address configuration; stack will choose address from central system + * configuration if the function returns false and does not write to addr. + * + * @param addr HW address for underlying interface + * @return true if HW address is available + */ + virtual bool get_hwaddr(uint8_t *addr) const; + + /** + * Set HW address for interface + * + * Provided address has to be of correct size, see @a get_hwaddr_size + * + * Called to set the MAC address to actually use - if @a get_hwaddr is provided + * the stack would normally use that, but it could be overridden, eg for test + * purposes. + * + * @param addr Address to be set + */ + virtual void set_hwaddr(const uint8_t *addr); + + /** + * Sends the packet over the link + * + * That can not be called from an interrupt context. + * + * @param buf Packet to be send + * @return True if the packet was send successfully, False otherwise + */ + virtual bool link_out(emac_mem_buf_t *buf); + + /** + * Initializes the HW + * + * @return True on success, False in case of an error. + */ + virtual bool power_up(); + + /** + * Deinitializes the HW + * + */ + virtual void power_down(); + + /** + * Sets a callback that needs to be called for packets received for that interface + * + * @param input_cb Function to be register as a callback + */ + virtual void set_link_input_cb(emac_link_input_cb_t input_cb); + + /** + * Sets a callback that needs to be called on link status changes for given interface + * + * @param state_cb Function to be register as a callback + */ + virtual void set_link_state_cb(emac_link_state_change_cb_t state_cb); + + /** Add device to a multicast group + * + * @param address A multicast group hardware address + */ + virtual void add_multicast_group(const uint8_t *address); + + /** Remove device from a multicast group + * + * @param address A multicast group hardware address + */ + virtual void remove_multicast_group(const uint8_t *address); + + /** Request reception of all multicast packets + * + * @param all True to receive all multicasts + * False to receive only multicasts addressed to specified groups + */ + virtual void set_all_multicast(bool all); + + /** Sets memory manager that is used to handle memory buffers + * + * @param mem_mngr Pointer to memory manager + */ + virtual void set_memory_manager(EMACMemoryManager &mem_mngr); + + virtual void wlan_emac_link_change(bool up); + +private: + static void wlan_emac_recv(void *param, struct netif *netif, uint32_t len); + void *emac_link_input_data; + void *emac_link_state_data; + emac_link_input_cb_t emac_link_input_cb; + emac_link_state_change_cb_t emac_link_state_cb; + EMACMemoryManager *memory_manager; +}; +#endif /* RTW_EMAC_H_ */ -#endif diff --git a/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.c b/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.c index f70150561b..9ab5a593e9 100644 --- a/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.c +++ b/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.c @@ -19,10 +19,12 @@ #include #include #include + #if !DEVICE_EMAC #include #include #endif + #include #include @@ -31,18 +33,17 @@ //----- ------------------------------------------------------------------ #if (CONFIG_LWIP_LAYER == 1) #if DEVICE_EMAC - extern struct netif *xnetif[]; + extern struct netif *xnetif[]; #else - extern struct netif xnetif[]; //LWIP netif + extern struct netif xnetif[]; //LWIP netif #endif #endif - /** * rltk_wlan_set_netif_info - set netif hw address and register dev pointer to netif device * @idx_wlan: netif index - * 0 for STA only or SoftAP only or STA in STA+SoftAP concurrent mode, - * 1 for SoftAP in STA+SoftAP concurrent mode + * 0 for STA only or SoftAP only or STA in STA+SoftAP concurrent mode, + * 1 for SoftAP in STA+SoftAP concurrent mode * @dev: register netdev pointer to LWIP. Reserved. * @dev_addr: set netif hw address * @@ -52,11 +53,11 @@ void rltk_wlan_set_netif_info(int idx_wlan, void * dev, unsigned char * dev_addr { #if (CONFIG_LWIP_LAYER == 1) #if DEVICE_EMAC - //rtw_memcpy(xnetif[idx_wlan]->hwaddr, dev_addr, 6); - //set netif hwaddr later + //rtw_memcpy(xnetif[idx_wlan]->hwaddr, dev_addr, 6); + //set netif hwaddr later #else - rtw_memcpy(xnetif[idx_wlan].hwaddr, dev_addr, 6); - xnetif[idx_wlan].state = dev; + rtw_memcpy(xnetif[idx_wlan].hwaddr, dev_addr, 6); + xnetif[idx_wlan].state = dev; #endif #endif } @@ -73,35 +74,35 @@ void rltk_wlan_set_netif_info(int idx_wlan, void * dev, unsigned char * dev_addr int rltk_wlan_send(int idx, struct eth_drv_sg *sg_list, int sg_len, int total_len) { #if (CONFIG_LWIP_LAYER == 1) - struct eth_drv_sg *last_sg; + struct eth_drv_sg *last_sg; struct sk_buff *skb = NULL; int ret = 0; - if(idx == -1){ - DBG_ERR("netif is DOWN"); - return -1; - } - DBG_TRACE("%s is called", __FUNCTION__); + if (idx == -1) { + DBG_ERR("netif is DOWN"); + return -1; + } + DBG_TRACE("%s is called", __FUNCTION__); save_and_cli(); - if(rltk_wlan_check_isup(idx)) - rltk_wlan_tx_inc(idx); - else { - DBG_ERR("netif is DOWN"); - restore_flags(); - return -1; - } + if (rltk_wlan_check_isup(idx)) { + rltk_wlan_tx_inc(idx); + } else { + DBG_ERR("netif is DOWN"); + restore_flags(); + return -1; + } restore_flags(); skb = rltk_wlan_alloc_skb(total_len); if (skb == NULL) { - //DBG_ERR("rltk_wlan_alloc_skb() for data len=%d failed!", total_len); + //DBG_ERR("rltk_wlan_alloc_skb() for data len=%d failed!", total_len); ret = -1; goto exit; } - for (last_sg = &sg_list[sg_len]; sg_list < last_sg; ++sg_list) { - rtw_memcpy(skb->tail, (void *)(sg_list->buf), sg_list->len); + for (last_sg = &sg_list[sg_len]; sg_list < last_sg; ++sg_list) { + rtw_memcpy(skb->tail, (void *)(sg_list->buf), sg_list->len); skb_put(skb, sg_list->len); } @@ -130,16 +131,16 @@ void rltk_wlan_recv(int idx, struct eth_drv_sg *sg_list, int sg_len) struct sk_buff *skb; DBG_TRACE("%s is called", __FUNCTION__); - + if (!rltk_wlan_check_isup(idx)) { return; } - + if (idx == -1) { DBG_ERR("skb is NULL"); return; } - + skb = rltk_wlan_get_recv_skb(idx); DBG_ASSERT(skb, "No pending rx skb"); @@ -171,25 +172,28 @@ int netif_is_valid_IP(int idx, unsigned char *ip_dest) #endif addr.addr = *ip_dest_addr; - if(pnetif->ip_addr.addr == 0) + if (pnetif->ip_addr.addr == 0) { return 1; - - if(ip_addr_ismulticast(&addr) || ip_addr_isbroadcast(&addr,pnetif)){ - return 1; - } - - //if(ip_addr_netcmp(&(pnetif->ip_addr), &addr, &(pnetif->netmask))) //addr&netmask - // return 1; + } - if(ip_addr_cmp(&(pnetif->ip_addr),&addr)) - return 1; + if (ip_addr_ismulticast(&addr) || ip_addr_isbroadcast(&addr,pnetif)) { + return 1; + } - DBG_TRACE("invalid IP: %d.%d.%d.%d ",ip_dest[0],ip_dest[1],ip_dest[2],ip_dest[3]); + //if(ip_addr_netcmp(&(pnetif->ip_addr), &addr, &(pnetif->netmask))) //addr&netmask + // return 1; + + if (ip_addr_cmp(&(pnetif->ip_addr),&addr)) { + return 1; + } + + DBG_TRACE("invalid IP: %d.%d.%d.%d ",ip_dest[0],ip_dest[1],ip_dest[2],ip_dest[3]); #endif #ifdef CONFIG_DONT_CARE_TP - if(pnetif->flags & NETIF_FLAG_IPSWITCH) + if (pnetif->flags & NETIF_FLAG_IPSWITCH) { return 1; - else + } + else #endif return 0; #endif @@ -201,42 +205,50 @@ int netif_is_valid_IP(int idx, unsigned char *ip_dest) int netif_get_idx(struct netif *pnetif) { #if (CONFIG_LWIP_LAYER == 1) - int idx = pnetif - xnetif; + int idx = pnetif - xnetif; - switch(idx) { - case 0: - return 0; - case 1: - return 1; - default: - return -1; - } -#else - return -1; + switch (idx) { + case 0: + return 0; + case 1: + return 1; + default: + return -1; + } +#else + return -1; #endif } unsigned char *netif_get_hwaddr(int idx_wlan) { #if (CONFIG_LWIP_LAYER == 1) - return xnetif[idx_wlan].hwaddr; + return xnetif[idx_wlan].hwaddr; #else - return NULL; + return NULL; #endif } #endif +emac_callback emac_callback_func = NULL; +void *emac_callback_data = NULL; +void set_callback_func(emac_callback p, void *data) { + emac_callback_func = p; + emac_callback_data = data; +} + void netif_rx(int idx, unsigned int len) { #if (CONFIG_LWIP_LAYER == 1) #if DEVICE_EMAC - wlan_emac_recv(NULL, len); + emac_callback_func(emac_callback_data, NULL, len); #else ethernetif_recv(&xnetif[idx], len); #endif #endif + #if (CONFIG_INIC_EN == 1) - inic_netif_rx(idx, len); + inic_netif_rx(idx, len); #endif } @@ -245,7 +257,7 @@ void netif_post_sleep_processing(void) #if (CONFIG_LWIP_LAYER == 1) #if DEVICE_EMAC #else - lwip_POST_SLEEP_PROCESSING(); //For FreeRTOS tickless to enable Lwip ARP timer when leaving IPS - Alex Fang + lwip_POST_SLEEP_PROCESSING(); //For FreeRTOS tickless to enable Lwip ARP timer when leaving IPS - Alex Fang #endif #endif } @@ -255,7 +267,7 @@ void netif_pre_sleep_processing(void) #if (CONFIG_LWIP_LAYER == 1) #if DEVICE_EMAC #else - lwip_PRE_SLEEP_PROCESSING(); + lwip_PRE_SLEEP_PROCESSING(); #endif #endif } @@ -263,9 +275,9 @@ void netif_pre_sleep_processing(void) #ifdef CONFIG_WOWLAN unsigned char *rltk_wlan_get_ip(int idx){ #if (CONFIG_LWIP_LAYER == 1) - return LwIP_GetIP(&xnetif[idx]); + return LwIP_GetIP(&xnetif[idx]); #else - return NULL; + return NULL; #endif } #endif diff --git a/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.h b/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.h index 2553c9906d..233335f933 100644 --- a/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.h +++ b/targets/TARGET_Realtek/TARGET_AMEBA/sdk/common/drivers/wlan/realtek/src/osdep/lwip_intf.h @@ -36,7 +36,6 @@ struct eth_drv_sg { #define MAX_ETH_DRV_SG 32 #define MAX_ETH_MSG 1540 -extern void wlan_emac_recv(struct netif *netif, int len); #else #include "ethernetif.h" // moved to ethernetif.h by jimmy 12/2/2015 #endif @@ -53,6 +52,8 @@ void rltk_wlan_send_skb(int idx, struct sk_buff *skb); //struct sk_buff as defin int rltk_wlan_send(int idx, struct eth_drv_sg *sg_list, int sg_len, int total_len); void rltk_wlan_recv(int idx, struct eth_drv_sg *sg_list, int sg_len); unsigned char rltk_wlan_running(unsigned char idx); // interface is up. 0: interface is down +typedef void (*emac_callback)(void *param, struct netif *netif, unsigned int len); +void set_callback_func(emac_callback p, void *data); //----- ------------------------------------------------------------------ // Network Interface provided diff --git a/targets/targets.json b/targets/targets.json index 872ea43ff5..3d105b2447 100755 --- a/targets/targets.json +++ b/targets/targets.json @@ -3922,10 +3922,10 @@ "default_toolchain": "GCC_ARM", "inherits": ["Target"], "detect_code": ["4600"], - "extra_labels": ["Realtek", "AMEBA", "RTL8195A"], + "extra_labels": ["Realtek", "AMEBA", "RTL8195A", "RTW_EMAC"], "macros": ["__RTL8195A__","CONFIG_PLATFORM_8195A","CONFIG_MBED_ENABLED","PLATFORM_CMSIS_RTOS","MBED_FAULT_HANDLER_DISABLED"], "supported_toolchains": ["GCC_ARM", "ARM", "IAR"], - "device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SPI", "TRNG", "FLASH"], + "device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SPI", "TRNG", "FLASH", "EMAC"], "features": ["LWIP"], "post_binary_hook": { "function": "RTL8195ACode.binary_hook", diff --git a/tools/test_configs/RealtekInterface.json b/tools/test_configs/RealtekInterface.json index ee016d2e26..8e9f5419d7 100644 --- a/tools/test_configs/RealtekInterface.json +++ b/tools/test_configs/RealtekInterface.json @@ -8,8 +8,8 @@ "value" : "new RTWInterface()" }, "connect-statement" : { - "help" : "Disabled until EMAC updated", - "value" : null + "help" : "Must use 'net' variable name, replace WIFI_SSID, WIFI_PASSWORD, WIFI_SECURITY, WIFI_CHANNEL with your WiFi settings", + "value" : "net->wifiInterface()->connect(WIFI_SSID, WIFI_PASSWORD, WIFI_SECURITY, WIFI_CHANNEL)" }, "echo-server-addr" : { "help" : "IP address of echo server",