Import Mbed OS hard-float snapshot
This commit is contained in:
@@ -0,0 +1,349 @@
|
||||
/*
|
||||
* Copyright 2018-2020 Cypress Semiconductor Corporation
|
||||
* 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 <cstring>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include "SclSTAInterface.h"
|
||||
#include "nsapi.h"
|
||||
#include "lwipopts.h"
|
||||
#include "lwip/etharp.h"
|
||||
#include "lwip/ethip6.h"
|
||||
#include "rtos.h"
|
||||
#include "scl_emac.h"
|
||||
#include "scl_ipc.h"
|
||||
#include "mbed_wait_api.h"
|
||||
|
||||
|
||||
/** @file
|
||||
* Provides SCL interface functions to be used with WiFiInterface or NetworkInterface Objects
|
||||
*/
|
||||
#define MIN_SSID_LENGTH (0)
|
||||
#define MIN_PASSWORD_LENGTH (0)
|
||||
|
||||
struct scl_tx_net_credentials {
|
||||
nsapi_security_t network_security_type;
|
||||
int ssid_len;
|
||||
int pass_len;
|
||||
const char *network_ssid;
|
||||
const char *network_passphrase;
|
||||
} scl_tx_network_credentials;
|
||||
|
||||
network_params_t network_parameter;
|
||||
|
||||
int scl_toerror(scl_result_t res)
|
||||
{
|
||||
switch (res) {
|
||||
case SCL_SUCCESS:
|
||||
return NSAPI_ERROR_OK;
|
||||
case SCL_UNSUPPORTED:
|
||||
return NSAPI_ERROR_UNSUPPORTED;
|
||||
case SCL_BADARG:
|
||||
return NSAPI_ERROR_PARAMETER;
|
||||
case SCL_INVALID_JOIN_STATUS:
|
||||
return NSAPI_ERROR_NO_CONNECTION;
|
||||
case SCL_BUFFER_UNAVAILABLE_PERMANENT:
|
||||
case SCL_BUFFER_UNAVAILABLE_TEMPORARY:
|
||||
case SCL_RX_BUFFER_ALLOC_FAIL:
|
||||
case SCL_BUFFER_ALLOC_FAIL:
|
||||
case SCL_MALLOC_FAILURE:
|
||||
return NSAPI_ERROR_NO_MEMORY;
|
||||
case SCL_ACCESS_POINT_NOT_FOUND:
|
||||
case SCL_NETWORK_NOT_FOUND:
|
||||
return NSAPI_ERROR_NO_SSID;
|
||||
case SCL_NOT_AUTHENTICATED:
|
||||
case SCL_INVALID_KEY:
|
||||
case SCL_NOT_KEYED:
|
||||
return NSAPI_ERROR_AUTH_FAILURE;
|
||||
case SCL_PENDING:
|
||||
case SCL_JOIN_IN_PROGRESS:
|
||||
return NSAPI_ERROR_IN_PROGRESS;
|
||||
case SCL_CONNECTION_LOST:
|
||||
return NSAPI_ERROR_CONNECTION_LOST;
|
||||
case SCL_TIMEOUT:
|
||||
case SCL_EAPOL_KEY_PACKET_M1_TIMEOUT:
|
||||
case SCL_EAPOL_KEY_PACKET_M3_TIMEOUT:
|
||||
case SCL_EAPOL_KEY_PACKET_G1_TIMEOUT:
|
||||
return NSAPI_ERROR_CONNECTION_TIMEOUT;
|
||||
default:
|
||||
return -res;
|
||||
}
|
||||
}
|
||||
|
||||
nsapi_security_t scl_tosecurity(scl_security_t sec)
|
||||
{
|
||||
switch (sec) {
|
||||
case SCL_SECURITY_OPEN:
|
||||
return NSAPI_SECURITY_NONE;
|
||||
case SCL_SECURITY_WEP_PSK:
|
||||
case SCL_SECURITY_WEP_SHARED:
|
||||
return NSAPI_SECURITY_WEP;
|
||||
case SCL_SECURITY_WPA_TKIP_PSK:
|
||||
case SCL_SECURITY_WPA_TKIP_ENT:
|
||||
return NSAPI_SECURITY_WPA;
|
||||
case SCL_SECURITY_WPA2_MIXED_PSK:
|
||||
return NSAPI_SECURITY_WPA_WPA2;
|
||||
case SCL_SECURITY_WPA2_AES_PSK:
|
||||
case SCL_SECURITY_WPA2_AES_ENT:
|
||||
case SCL_SECURITY_WPA2_FBT_PSK:
|
||||
case SCL_SECURITY_WPA2_FBT_ENT:
|
||||
return NSAPI_SECURITY_WPA2;
|
||||
default:
|
||||
return NSAPI_SECURITY_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
scl_security_t scl_fromsecurity(nsapi_security_t sec)
|
||||
{
|
||||
switch (sec) {
|
||||
case NSAPI_SECURITY_NONE:
|
||||
return SCL_SECURITY_OPEN;
|
||||
case NSAPI_SECURITY_WEP:
|
||||
return SCL_SECURITY_WEP_PSK;
|
||||
case NSAPI_SECURITY_WPA:
|
||||
return SCL_SECURITY_WPA_MIXED_PSK;
|
||||
case NSAPI_SECURITY_WPA2:
|
||||
return SCL_SECURITY_WPA2_AES_PSK;
|
||||
case NSAPI_SECURITY_WPA_WPA2:
|
||||
return SCL_SECURITY_WPA2_MIXED_PSK;
|
||||
default:
|
||||
return SCL_SECURITY_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
SclSTAInterface::SclSTAInterface(SCL_EMAC &emac, OnboardNetworkStack &stack)
|
||||
: EMACInterface(emac, stack),
|
||||
_ssid("\0"),
|
||||
_pass("\0"),
|
||||
_security(NSAPI_SECURITY_NONE),
|
||||
_scl_emac(emac)
|
||||
{
|
||||
}
|
||||
|
||||
nsapi_error_t SclSTAInterface::connect(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel)
|
||||
{
|
||||
int err = set_channel(channel);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = set_credentials(ssid, pass, security);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return connect();
|
||||
}
|
||||
|
||||
nsapi_error_t SclSTAInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
|
||||
{
|
||||
if ((ssid == NULL) ||
|
||||
(strlen(ssid) == 0) ||
|
||||
(pass == NULL && (security != NSAPI_SECURITY_NONE)) ||
|
||||
(strlen(pass) == 0 && (security != NSAPI_SECURITY_NONE)) ||
|
||||
(strlen(pass) > 63 && (security == NSAPI_SECURITY_WPA2 || security == NSAPI_SECURITY_WPA || security == NSAPI_SECURITY_WPA_WPA2))
|
||||
) {
|
||||
return NSAPI_ERROR_PARAMETER;
|
||||
}
|
||||
|
||||
memset(_ssid, 0, sizeof(_ssid));
|
||||
strncpy(_ssid, ssid, sizeof(_ssid));
|
||||
|
||||
memset(_pass, 0, sizeof(_pass));
|
||||
strncpy(_pass, pass, sizeof(_pass));
|
||||
|
||||
_security = security;
|
||||
|
||||
return NSAPI_ERROR_OK;
|
||||
}
|
||||
|
||||
nsapi_error_t SclSTAInterface::connect()
|
||||
{
|
||||
|
||||
uint32_t delay_timeout = 0;
|
||||
scl_result_t ret_val;
|
||||
nsapi_error_t interface_status;
|
||||
uint32_t connection_status = 0;
|
||||
|
||||
scl_tx_network_credentials.network_ssid = _ssid;
|
||||
if ((strlen(_ssid) < MAX_SSID_LENGTH) && (strlen(_ssid) > MIN_SSID_LENGTH)) {
|
||||
scl_tx_network_credentials.ssid_len = strlen(_ssid);
|
||||
} else {
|
||||
return NSAPI_ERROR_PARAMETER;
|
||||
}
|
||||
scl_tx_network_credentials.network_passphrase = _pass;
|
||||
if (((strlen(_pass) < MAX_PASSWORD_LENGTH) && (strlen(_pass) > MIN_PASSWORD_LENGTH)) || (_security == NSAPI_SECURITY_NONE)) {
|
||||
scl_tx_network_credentials.pass_len = strlen(_pass);
|
||||
} else {
|
||||
return NSAPI_ERROR_PARAMETER;
|
||||
}
|
||||
scl_tx_network_credentials.network_security_type = _security;
|
||||
|
||||
ret_val = scl_send_data(SCL_TX_CONNECT, (char *)&scl_tx_network_credentials, TIMER_DEFAULT_VALUE);
|
||||
|
||||
if (ret_val == SCL_SUCCESS) {
|
||||
SCL_LOG(("wifi provisioning in progress\r\n"));
|
||||
}
|
||||
|
||||
network_parameter.connection_status = NSAPI_STATUS_DISCONNECTED;
|
||||
|
||||
|
||||
//Get the network parameter from NP
|
||||
while ((network_parameter.connection_status != NSAPI_STATUS_GLOBAL_UP) && delay_timeout < NW_CONNECT_TIMEOUT) {
|
||||
ret_val = scl_get_nw_parameters(&network_parameter);
|
||||
wait_us(NW_DELAY_TIME_US);
|
||||
delay_timeout++;
|
||||
}
|
||||
|
||||
if (delay_timeout >= NW_CONNECT_TIMEOUT || ret_val != SCL_SUCCESS) {
|
||||
return NSAPI_ERROR_NO_CONNECTION;
|
||||
}
|
||||
|
||||
if (!_scl_emac.powered_up) {
|
||||
_scl_emac.power_up();
|
||||
}
|
||||
|
||||
if (!_interface) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (!scl_wifi_is_ready_to_transceive()) {
|
||||
scl_emac_wifi_link_state_changed(true);
|
||||
}
|
||||
|
||||
interface_status = _interface->bringup(false,
|
||||
network_parameter.ip_address,
|
||||
network_parameter.netmask,
|
||||
network_parameter.gateway,
|
||||
DEFAULT_STACK);
|
||||
|
||||
if (interface_status == NSAPI_ERROR_OK) {
|
||||
scl_send_data(SCL_TX_CONNECTION_STATUS, (char *)&connection_status, TIMER_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
return interface_status;
|
||||
}
|
||||
|
||||
void SclSTAInterface::wifi_on()
|
||||
{
|
||||
if (!_scl_emac.powered_up) {
|
||||
_scl_emac.power_up();
|
||||
}
|
||||
}
|
||||
|
||||
nsapi_error_t SclSTAInterface::disconnect()
|
||||
{
|
||||
scl_result_t ret_val;
|
||||
nsapi_error_t disconnect_status;
|
||||
uint32_t delay_timeout = 0;
|
||||
|
||||
ret_val = scl_send_data(SCL_TX_DISCONNECT, (char *)&disconnect_status, TIMER_DEFAULT_VALUE);
|
||||
|
||||
if (ret_val == SCL_ERROR) {
|
||||
return NSAPI_ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (!_interface) {
|
||||
return NSAPI_ERROR_NO_CONNECTION;
|
||||
}
|
||||
|
||||
// block till disconnected from network
|
||||
while ((network_parameter.connection_status != NSAPI_STATUS_DISCONNECTED) && delay_timeout < NW_DISCONNECT_TIMEOUT) {
|
||||
ret_val = scl_get_nw_parameters(&network_parameter);
|
||||
wait_us(NW_DELAY_TIME_US);
|
||||
delay_timeout++;
|
||||
}
|
||||
|
||||
if (delay_timeout >= NW_DISCONNECT_TIMEOUT) {
|
||||
return NSAPI_ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
// bring down
|
||||
int err = _interface->bringdown();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
scl_emac_wifi_link_state_changed(false);
|
||||
|
||||
return NSAPI_ERROR_OK;
|
||||
}
|
||||
|
||||
int SclSTAInterface::scan(WiFiAccessPoint *res, unsigned count)
|
||||
{
|
||||
/* To Do */
|
||||
return NSAPI_ERROR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int8_t SclSTAInterface::get_rssi()
|
||||
{
|
||||
int32_t rssi;
|
||||
scl_result_t res;
|
||||
|
||||
if (!_scl_emac.powered_up) {
|
||||
_scl_emac.power_up();
|
||||
}
|
||||
|
||||
res = (scl_result_t) scl_wifi_get_rssi(&rssi);
|
||||
if (res == SCL_ERROR) {
|
||||
return SCL_ERROR;
|
||||
}
|
||||
|
||||
return (int8_t)rssi;
|
||||
}
|
||||
|
||||
int SclSTAInterface::is_interface_connected(void)
|
||||
{
|
||||
if (scl_wifi_is_ready_to_transceive() == SCL_SUCCESS) {
|
||||
return SCL_SUCCESS;
|
||||
} else {
|
||||
return SCL_CONNECTION_LOST;
|
||||
}
|
||||
}
|
||||
|
||||
int SclSTAInterface::get_bssid(uint8_t *bssid)
|
||||
{
|
||||
scl_mac_t ap_mac;
|
||||
scl_result_t res = SCL_SUCCESS;
|
||||
|
||||
if (bssid == NULL) {
|
||||
return SCL_BADARG;
|
||||
}
|
||||
|
||||
memset(&ap_mac, 0, sizeof(ap_mac));
|
||||
if (scl_wifi_is_ready_to_transceive() == SCL_SUCCESS) {
|
||||
res = (scl_result_t) scl_wifi_get_bssid(&ap_mac);
|
||||
if (res == SCL_SUCCESS) {
|
||||
memcpy(bssid, ap_mac.octet, sizeof(ap_mac.octet));
|
||||
}
|
||||
} else {
|
||||
return SCL_CONNECTION_LOST;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int SclSTAInterface::wifi_set_up(void)
|
||||
{
|
||||
int res = SCL_SUCCESS;
|
||||
res = scl_wifi_set_up();
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright 2018-2020 Cypress Semiconductor Corporation
|
||||
* 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 SCL_STA_INTERFACE_H
|
||||
#define SCL_STA_INTERFACE_H
|
||||
|
||||
|
||||
/** @file
|
||||
* Provides SCL interface functions to be used with WiFiInterface or NetworkInterface Objects
|
||||
*/
|
||||
|
||||
#include "netsocket/WiFiInterface.h"
|
||||
#include "netsocket/EMACInterface.h"
|
||||
#include "netsocket/OnboardNetworkStack.h"
|
||||
#include "scl_emac.h"
|
||||
#include "scl_wifi_api.h"
|
||||
#include "scl_types.h"
|
||||
#define MAX_SSID_LENGTH (33) /**< Maximum ssid length */
|
||||
#define MAX_PASSWORD_LENGTH (64) /**< Maximum password length */
|
||||
|
||||
/** SclSTAInterface class
|
||||
* Implementation of the Network Stack for the SCL
|
||||
*/
|
||||
class SclSTAInterface : public WiFiInterface, public EMACInterface {
|
||||
public:
|
||||
|
||||
SclSTAInterface(
|
||||
SCL_EMAC &emac = SCL_EMAC::get_instance(),
|
||||
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
|
||||
|
||||
/** Gets the current instance of the SclSTAInterface
|
||||
*
|
||||
* @return Pointer to the object of class SclSTAInterface.
|
||||
*/
|
||||
static SclSTAInterface *get_default_instance();
|
||||
|
||||
/** Turns on the Wi-Fi device
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
void wifi_on();
|
||||
|
||||
/** Starts the interface
|
||||
*
|
||||
* Attempts to connect to a Wi-Fi 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.
|
||||
*/
|
||||
nsapi_error_t connect();
|
||||
|
||||
/** Starts the interface
|
||||
*
|
||||
* Attempts to connect to a Wi-Fi 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 This parameter is not supported, setting it to a value other than 0 will result in NSAPI_ERROR_UNSUPPORTED.
|
||||
* @return 0 on success, negative error code on failure.
|
||||
*/
|
||||
nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, uint8_t channel = 0);
|
||||
|
||||
/** Disconnects the interface
|
||||
*
|
||||
* @return 0 on success, negative error code on failure.
|
||||
*/
|
||||
nsapi_error_t disconnect();
|
||||
|
||||
/** Set the Wi-Fi 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, negative error code on failure.
|
||||
*/
|
||||
nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
|
||||
|
||||
/** Sets the Wi-Fi network channel - NOT SUPPORTED
|
||||
*
|
||||
* This function is not supported and will return NSAPI_ERROR_UNSUPPORTED.
|
||||
*
|
||||
* @param channel Channel on which the connection is to be made (Default: 0).
|
||||
* @return Not supported, returns NSAPI_ERROR_UNSUPPORTED.
|
||||
*/
|
||||
nsapi_error_t set_channel(uint8_t channel)
|
||||
{
|
||||
if (channel != 0) {
|
||||
return NSAPI_ERROR_UNSUPPORTED;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Set blocking status of interface.
|
||||
* Nonblocking mode is not supported.
|
||||
*
|
||||
* @param blocking True if connect is blocking
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
nsapi_error_t set_blocking(bool blocking)
|
||||
{
|
||||
if (blocking) {
|
||||
_blocking = blocking;
|
||||
return NSAPI_ERROR_OK;
|
||||
} else {
|
||||
return NSAPI_ERROR_UNSUPPORTED;
|
||||
}
|
||||
}
|
||||
/** Gets the current radio signal strength for active connection
|
||||
*
|
||||
* @return Connection strength in dBm (negative value).
|
||||
*/
|
||||
int8_t get_rssi();
|
||||
|
||||
/** Scans for available networks - NOT SUPPORTED
|
||||
*
|
||||
* @return NSAPI_ERROR_UNSUPPORTED
|
||||
*/
|
||||
int scan(WiFiAccessPoint *res, unsigned count);
|
||||
|
||||
/** This function is used to indicate if the device is connected to the network.
|
||||
*
|
||||
* @return SCL_SUCCESS if device is connected.
|
||||
*/
|
||||
int is_interface_connected();
|
||||
|
||||
/** Gets the BSSID (MAC address of device connected to).
|
||||
*
|
||||
* @param bssid Pointer to the BSSID value.
|
||||
* @return SCL_SUCCESS if BSSID is obtained successfully.
|
||||
* @return SCL_BADARG if input parameter is NULL.
|
||||
* @return SCL_ERROR if unable to fetch BSSID.
|
||||
*/
|
||||
int get_bssid(uint8_t *bssid);
|
||||
|
||||
/** This function is used to set up the Wi-Fi interface.
|
||||
* This function should be used after the wifi_on.
|
||||
*
|
||||
* @return SCL_SUCCESS if the Wi-Fi interface is set up successfully.
|
||||
*/
|
||||
int wifi_set_up(void);
|
||||
|
||||
private:
|
||||
|
||||
char _ssid[MAX_SSID_LENGTH]; /**< The longest possible name (defined in 802.11) +1 for the \0 */
|
||||
char _pass[MAX_PASSWORD_LENGTH]; /**< The longest allowed passphrase + 1 */
|
||||
nsapi_security_t _security; /**< Security type */
|
||||
SCL_EMAC &_scl_emac; /**< SCL_EMAC object */
|
||||
};
|
||||
#endif /* ifndef SCL_STA_INTERFACE_H */
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2018-2020 Cypress Semiconductor Corporation
|
||||
* 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 "SclSTAInterface.h"
|
||||
|
||||
/** @file
|
||||
* Provides function definition to override get_target_default_intance of WiFiInterface and NetworkInterface classes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the WiFiInterface Object
|
||||
* This function can be called using WiFiInterface or NetworkInterface objects
|
||||
*
|
||||
* @return pointer to WiFiInterface object.
|
||||
*/
|
||||
|
||||
WiFiInterface *WiFiInterface::get_target_default_instance()
|
||||
{
|
||||
static SclSTAInterface wifi;
|
||||
return &wifi;
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* Copyright 2018-2020 Cypress Semiconductor Corporation
|
||||
* 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 <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cmsis_os.h"
|
||||
#include "scl_emac.h"
|
||||
#include "lwip/etharp.h"
|
||||
#include "lwip/ethip6.h"
|
||||
#include "events/mbed_shared_queues.h"
|
||||
#include "scl_buffer_api.h"
|
||||
#include "cy_result.h"
|
||||
#include "cy_pdl.h"
|
||||
#include "scl_ipc.h"
|
||||
|
||||
/** @file
|
||||
* Provides EMAC interface functions to be used with the SCL_EMAC object
|
||||
*/
|
||||
|
||||
SCL_EMAC::SCL_EMAC(scl_interface_role_t role)
|
||||
: interface_type(role)
|
||||
{
|
||||
}
|
||||
|
||||
SCL_EMAC::SCL_EMAC()
|
||||
: interface_type(SCL_STA_ROLE)
|
||||
{
|
||||
}
|
||||
|
||||
SCL_EMAC &SCL_EMAC::get_instance()
|
||||
{
|
||||
return get_instance(SCL_STA_ROLE);
|
||||
}
|
||||
|
||||
SCL_EMAC &SCL_EMAC::get_instance(scl_interface_role_t role)
|
||||
{
|
||||
static SCL_EMAC emac_sta(SCL_STA_ROLE);
|
||||
static SCL_EMAC emac_ap(SCL_AP_ROLE);
|
||||
return role == SCL_AP_ROLE ? emac_ap : emac_sta;
|
||||
}
|
||||
|
||||
uint32_t SCL_EMAC::get_mtu_size() const
|
||||
{
|
||||
return SCL_PAYLOAD_MTU;
|
||||
}
|
||||
|
||||
uint32_t SCL_EMAC::get_align_preference() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SCL_EMAC::add_multicast_group(const uint8_t *addr)
|
||||
{
|
||||
memcpy(multicast_addr.octet, addr, sizeof(multicast_addr.octet));
|
||||
scl_wifi_register_multicast_address(&multicast_addr);
|
||||
}
|
||||
|
||||
void SCL_EMAC::remove_multicast_group(const uint8_t *address)
|
||||
{
|
||||
/* To Do */
|
||||
}
|
||||
void SCL_EMAC::set_all_multicast(bool all)
|
||||
{
|
||||
/* No-op at this stage */
|
||||
}
|
||||
|
||||
void SCL_EMAC::power_down()
|
||||
{
|
||||
/* No-op at this stage */
|
||||
}
|
||||
|
||||
bool SCL_EMAC::power_up()
|
||||
{
|
||||
if (!powered_up) {
|
||||
#ifdef MBED_TEST_MODE
|
||||
scl_init();
|
||||
#endif
|
||||
if (!scl_wifi_on()) {
|
||||
SCL_LOG(("WiFi failed to turn on\r\n"));
|
||||
return false;
|
||||
}
|
||||
powered_up = true;
|
||||
if (link_state && emac_link_state_cb) {
|
||||
emac_link_state_cb(link_state);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SCL_EMAC::get_hwaddr(uint8_t *addr) const
|
||||
{
|
||||
scl_mac_t mac;
|
||||
scl_result_t res = scl_wifi_get_mac_address(&mac);
|
||||
if (res == SCL_SUCCESS) {
|
||||
memcpy(addr, mac.octet, sizeof(mac.octet));
|
||||
return true;
|
||||
} else {
|
||||
SCL_LOG(("Unable to get MAC address\r\n"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SCL_EMAC::set_hwaddr(const uint8_t *addr)
|
||||
{
|
||||
/* No-op at this stage */
|
||||
}
|
||||
|
||||
uint8_t SCL_EMAC::get_hwaddr_size() const
|
||||
{
|
||||
scl_mac_t mac;
|
||||
return sizeof(mac.octet);
|
||||
}
|
||||
|
||||
void SCL_EMAC::set_link_input_cb(emac_link_input_cb_t input_cb)
|
||||
{
|
||||
emac_link_input_cb = input_cb;
|
||||
}
|
||||
|
||||
void SCL_EMAC::set_link_state_cb(emac_link_state_change_cb_t state_cb)
|
||||
{
|
||||
emac_link_state_cb = state_cb;
|
||||
}
|
||||
|
||||
void SCL_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr)
|
||||
{
|
||||
memory_manager = &mem_mngr;
|
||||
}
|
||||
|
||||
bool SCL_EMAC::link_out(emac_mem_buf_t *buf)
|
||||
{
|
||||
scl_result_t retval;
|
||||
scl_tx_buf_t scl_tx_data;
|
||||
scl_tx_data.size = memory_manager->get_total_len(buf);
|
||||
scl_tx_data.buffer = buf;
|
||||
if (buf == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
retval = scl_network_send_ethernet_data(scl_tx_data); //Buffer is copied on Network Processor
|
||||
if (retval != SCL_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
memory_manager->free(buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SCL_EMAC::get_ifname(char *name, uint8_t size) const
|
||||
{
|
||||
if (name != NULL) {
|
||||
memcpy(name, "scl", size);
|
||||
}
|
||||
}
|
||||
|
||||
void SCL_EMAC::set_activity_cb(mbed::Callback<void(bool)> cb)
|
||||
{
|
||||
activity_cb = cb;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void scl_network_process_ethernet_data(scl_buffer_t buffer)
|
||||
{
|
||||
emac_mem_buf_t *mem_buf = NULL;
|
||||
|
||||
SCL_EMAC &emac = SCL_EMAC::get_instance(SCL_STA_ROLE);
|
||||
|
||||
if (!emac.powered_up && !emac.emac_link_input_cb) {
|
||||
scl_buffer_release(buffer, SCL_NETWORK_RX);
|
||||
return;
|
||||
}
|
||||
mem_buf = buffer;
|
||||
if (emac.activity_cb) {
|
||||
emac.activity_cb(false);
|
||||
}
|
||||
emac.emac_link_input_cb(mem_buf);
|
||||
}
|
||||
|
||||
void scl_emac_wifi_link_state_changed(bool state_up)
|
||||
{
|
||||
SCL_EMAC &emac = SCL_EMAC::get_instance(SCL_STA_ROLE);
|
||||
|
||||
emac.link_state = state_up;
|
||||
if (emac.emac_link_state_cb) {
|
||||
emac.emac_link_state_cb(state_up);
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright 2018-2020 Cypress Semiconductor Corporation
|
||||
* 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 SCL_EMAC_H_
|
||||
#define SCL_EMAC_H_
|
||||
|
||||
/** @file
|
||||
* Provides EMAC interface functions to be used with the SCL_EMAC object
|
||||
*
|
||||
*/
|
||||
#include "EMAC.h"
|
||||
#include "EMACInterface.h"
|
||||
#include "WiFiInterface.h"
|
||||
#include "scl_common.h"
|
||||
#include "rtos/Semaphore.h"
|
||||
#include "rtos/Mutex.h"
|
||||
#include "scl_wifi_api.h"
|
||||
class SCL_EMAC : public EMAC {
|
||||
public:
|
||||
SCL_EMAC();
|
||||
SCL_EMAC(scl_interface_role_t itype);
|
||||
|
||||
/**
|
||||
* Get the EMAC instance
|
||||
*
|
||||
* @return Reference to SCL_EMAC object.
|
||||
*/
|
||||
static SCL_EMAC &get_instance();
|
||||
|
||||
/**
|
||||
* Get the EMAC instance
|
||||
*
|
||||
* @param role Interface type.
|
||||
*
|
||||
* @return Reference to SCL_EMAC object.
|
||||
*/
|
||||
static SCL_EMAC &get_instance(scl_interface_role_t role);
|
||||
|
||||
/**
|
||||
* Returns the maximum transmission unit
|
||||
*
|
||||
* @return MTU in bytes.
|
||||
*/
|
||||
virtual uint32_t get_mtu_size() const;
|
||||
|
||||
/**
|
||||
* Gets the memory buffer alignment preference
|
||||
*
|
||||
* Gets the preferred memory buffer alignment of the EMAC device. IP stack may or may not
|
||||
* align with the link out memory buffer chains using the alignment.
|
||||
*
|
||||
* @return Memory alignment requirement in bytes.
|
||||
*/
|
||||
virtual uint32_t get_align_preference() const;
|
||||
|
||||
/**
|
||||
* Returns the interface name
|
||||
*
|
||||
* @param name Pointer to the location where the name should be written.
|
||||
* @param size Maximum number of characters to copy.
|
||||
*/
|
||||
virtual void get_ifname(char *name, uint8_t size) const;
|
||||
|
||||
/**
|
||||
* Returns the size of the underlying interface HW address size.
|
||||
*
|
||||
* @return HW address size in bytes.
|
||||
*/
|
||||
virtual uint8_t get_hwaddr_size() const;
|
||||
|
||||
/**
|
||||
* Returns the interface supplied HW address
|
||||
* Copies the HW address to the provided memory
|
||||
* @param addr HW address of the underlying interface. It must be of correct size. See @a get_hwaddr_size.
|
||||
* @return True if HW address is available.
|
||||
*/
|
||||
virtual bool get_hwaddr(uint8_t *addr) const;
|
||||
|
||||
/**
|
||||
* Set HW address for the interface
|
||||
*
|
||||
* Provided address must be of correct size. See @a get_hwaddr_size.
|
||||
*
|
||||
* Called to set the MAC address to be used - if @a get_hwaddr is provided
|
||||
* the stack would normally use that, but it could be overridden for test
|
||||
* purposes.
|
||||
*
|
||||
* @param addr Address to be set
|
||||
*/
|
||||
virtual void set_hwaddr(const uint8_t *addr);
|
||||
|
||||
/**
|
||||
* Sends the packet over the link
|
||||
*
|
||||
* This cannot be called from an interrupt context.
|
||||
*
|
||||
* @param buf Packet to be sent.
|
||||
* @return True if the packet was sent 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();
|
||||
|
||||
/**
|
||||
* De-initializes the HW
|
||||
*/
|
||||
virtual void power_down();
|
||||
|
||||
/**
|
||||
* Sets a callback that is called for packets received for a given interface
|
||||
*
|
||||
* @param input_cb Function to be registered as a callback.
|
||||
*/
|
||||
virtual void set_link_input_cb(emac_link_input_cb_t input_cb);
|
||||
|
||||
/**
|
||||
* Sets a callback that is called on changes in the link status for a given interface
|
||||
*
|
||||
* @param state_cb Function to be registered as a callback.
|
||||
*/
|
||||
virtual void set_link_state_cb(emac_link_state_change_cb_t state_cb);
|
||||
|
||||
/** Adds a device to a multicast group
|
||||
*
|
||||
* @param address A multicast group hardware address.
|
||||
*/
|
||||
virtual void add_multicast_group(const uint8_t *address);
|
||||
|
||||
/** Removes a device from a multicast group
|
||||
*
|
||||
* @param address A multicast group hardware address.
|
||||
*/
|
||||
virtual void remove_multicast_group(const uint8_t *address);
|
||||
|
||||
/** Requests 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 used to handle memory buffers
|
||||
*
|
||||
* @param mem_mngr Pointer to memory manager.
|
||||
*/
|
||||
virtual void set_memory_manager(EMACMemoryManager &mem_mngr);
|
||||
|
||||
/** Sets callback to receive EMAC activity events
|
||||
*
|
||||
* @param activity_cb The callback for activity events.
|
||||
*/
|
||||
virtual void set_activity_cb(mbed::Callback<void(bool is_tx_activity)> activity_cb);
|
||||
|
||||
emac_link_input_cb_t emac_link_input_cb = NULL; /**< Callback for incoming data */
|
||||
emac_link_state_change_cb_t emac_link_state_cb = NULL; /**< Callback for network connection status */
|
||||
EMACMemoryManager *memory_manager; /**< Pointer to hold memory manager object */
|
||||
bool powered_up = false; /**< Flag for Wi-Fi power on status */
|
||||
bool link_state = false; /**< Flag for network connection status */
|
||||
scl_interface_role_t interface_type; /**< Type of the interface */
|
||||
scl_mac_t multicast_addr; /**< Multicast address */
|
||||
mbed::Callback<void(bool)> activity_cb; /**< Callback for activity on network */
|
||||
|
||||
};
|
||||
/** Sends the change in network connection state to network stack
|
||||
*
|
||||
* @param state_up Connection status.
|
||||
*/
|
||||
extern "C" void scl_emac_wifi_link_state_changed(bool state_up);
|
||||
|
||||
#endif /* SCL_EMAC_H_ */
|
||||
Reference in New Issue
Block a user