Import Mbed OS hard-float snapshot

This commit is contained in:
Beslan
2026-06-01 20:15:04 +03:00
commit d3738e2f89
16278 changed files with 10628036 additions and 0 deletions

View File

@@ -0,0 +1,454 @@
/*
* Copyright (c) 2018-2019 ARM Limited
* 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 "CyDhcpServer.h"
#include "cy_utils.h"
#include "Callback.h"
#include "def.h"
#include "whd_types.h"
#ifdef DHCP_EXTENSIVE_DEBUG
extern "C" void dhcp_server_print_header_info(dhcp_packet_t *header, uint32_t datalen, const char *title);
#endif
/* UDP port numbers for DHCP server and client */
#define IP_PORT_DHCP_SERVER (67)
#define IP_PORT_DHCP_CLIENT (68)
/* BOOTP operations */
#define BOOTP_OP_REQUEST (1)
#define BOOTP_OP_REPLY (2)
/* DCHP message types */
#define DHCP_MSG_TYPE_DISCOVER (1)
#define DHCP_MSG_TYPE_OFFER (2)
#define DHCP_MSG_TYPE_REQUEST (3)
#define DHCP_MSG_TYPE_DECLINE (4)
#define DHCP_MSG_TYPE_ACK (5)
#define DHCP_MSG_TYPE_NACK (6)
#define DHCP_MSG_TYPE_RELEASE (7)
#define DHCP_MSG_TYPE_INFORM (8)
#define DHCP_MSG_TYPE_INVALID (255)
#define DHCP_MSG_MAGIC_COOKIE (0x63825363)
#define DHCP_STACK_SIZE (8*1024)
/********************* Options manipulation functions ***********************************/
static void addOption(dhcp_packet_t *dhcp, uint32_t &index, uint8_t optype)
{
if (index + sizeof(dhcp_packet_t) - 1 + 1 >= DHCP_PACKET_SIZE) {
printf("DHCP ERROR: Option index %d (Optype: %d) written to exceeds size of the packet", (int)index, (int)optype);
return;
}
dhcp->Options[index++] = optype;
return;
}
static void addOption(dhcp_packet_t *dhcp, uint32_t &index, uint8_t optype, uint8_t value)
{
if (index + sizeof(dhcp_packet_t) - 1 + 3 >= DHCP_PACKET_SIZE) {
printf("DHCP ERROR: Option index %d (Optype: %d) written to exceeds size of the packet", (int)index, (int)optype);
return;
}
dhcp->Options[index++] = optype;
dhcp->Options[index++] = 0x01;
dhcp->Options[index++] = value;
return;
}
static void addOption(dhcp_packet_t *dhcp, uint32_t &index, uint8_t optype, uint16_t value)
{
if (index + sizeof(dhcp_packet_t) - 1 + 4 >= DHCP_PACKET_SIZE) {
printf("DHCP ERROR: Option index %d (Optype: %d) written to exceeds size of the packet", (int)index, (int)optype);
return;
}
dhcp->Options[index++] = optype;
dhcp->Options[index++] = 0x02;
dhcp->Options[index++] = static_cast<uint8_t>((value >> 0) & 0xFF);
dhcp->Options[index++] = static_cast<uint8_t>((value >> 8) & 0xFF);
return;
}
static void addOption(dhcp_packet_t *dhcp, uint32_t &index, uint8_t optype, uint32_t value)
{
if (index + sizeof(dhcp_packet_t) - 1 + 6 >= DHCP_PACKET_SIZE) {
printf("DHCP ERROR: Option index %d (Optype: %d) written to exceeds size of the packet", (int)index, (int)optype);
return;
}
dhcp->Options[index++] = optype;
dhcp->Options[index++] = 0x04;
dhcp->Options[index++] = static_cast<uint8_t>((value >> 0) & 0xFF);
dhcp->Options[index++] = static_cast<uint8_t>((value >> 8) & 0xFF);
dhcp->Options[index++] = static_cast<uint8_t>((value >> 16) & 0xFF);
dhcp->Options[index++] = static_cast<uint8_t>((value >> 24) & 0xFF);
return;
}
static void addOption(dhcp_packet_t *dhcp, uint32_t &index, uint8_t optype, uint8_t *value, uint32_t size)
{
if (index + sizeof(dhcp_packet_t) - 1 + 2 + size >= DHCP_PACKET_SIZE) {
printf("DHCP ERROR: Option index %d (Optype: %d) written to exceeds size of the packet", (int)index, (int)optype);
return;
}
dhcp->Options[index++] = optype;
dhcp->Options[index++] = size;
memcpy(&dhcp->Options[index], value, size);
index += size;
return;
}
static const uint8_t *findOption(const dhcp_packet_t *request, uint8_t option_num)
{
const uint8_t *option_ptr = request->Options;
while ((option_ptr[0] != DHCP_END_OPTION_CODE) &&
(option_ptr[0] != option_num) &&
(option_ptr < ((const uint8_t *)request) + DHCP_PACKET_SIZE)) {
option_ptr += option_ptr[1] + 2;
}
/* Was the option found? */
if (option_ptr[0] == option_num) {
return &option_ptr[2];
}
return NULL;
}
static void addCommonOptions(dhcp_packet_t *dhcp, uint32_t &index, const uint32_t server_addr, const uint32_t netmask)
{
/* Prepare the Web proxy auto discovery URL */
char wpad_sample_url[] = "http://xxx.xxx.xxx.xxx/wpad.dat";
char ip_str[16];
ipv4_to_string(ip_str, htonl(server_addr));
memcpy(&wpad_sample_url[7], &ip_str[0], 15);
/* Server identifier */
addOption(dhcp, index, DHCP_SERVER_IDENTIFIER_OPTION_CODE, server_addr);
/* Lease Time */
addOption(dhcp, index, DHCP_LEASETIME_OPTION_CODE, static_cast<uint32_t>(0x00015180));
/* Subnet Mask */
addOption(dhcp, index, DHCP_SUBNETMASK_OPTION_CODE, htonl(netmask));
/* Web proxy auto discovery URL */
addOption(dhcp, index, DHCP_WPAD_OPTION_CODE, (uint8_t *)&wpad_sample_url[0], strlen(wpad_sample_url));
/* Router (gateway) */
addOption(dhcp, index, DHCP_ROUTER_OPTION_CODE, htonl(server_addr));
/* DNS server */
addOption(dhcp, index, DHCP_DNS_SERVER_OPTION_CODE, htonl(server_addr));
/* Interface MTU */
addOption(dhcp, index, DHCP_MTU_OPTION_CODE, static_cast<uint16_t>(WHD_PAYLOAD_MTU));
}
static void sendPacket(UDPSocket *socket, dhcp_packet_t *dhcp, uint32_t size)
{
nsapi_size_or_error_t err;
uint32_t broadcast_ip = 0xFFFFFFFF;
char string_addr[16];
ipv4_to_string(string_addr, htonl(broadcast_ip));
SocketAddress sock_addr(string_addr, IP_PORT_DHCP_CLIENT);
err = socket->sendto(sock_addr, reinterpret_cast<uint8_t *>(dhcp), size);
if (err < 0) {
printf("DHCP ERROR: Packet send failure with error %d.", err);
} else if (err != (int)size) {
printf("DHCP ERROR: Could not send entire packet. Only %d bytes were sent.", err);
}
}
/********************* Cache utility functions ***********************************/
void CyDhcpServer::setAddress(const cy_mac_addr_t &mac_id, const cy_ip_addr_t &addr)
{
uint32_t a;
uint32_t first_empty_slot;
uint32_t cached_slot;
char empty_cache[NSAPI_IPv6_SIZE] = "";
/* Search for empty slot in cache */
for (a = 0, first_empty_slot = DHCP_IP_ADDRESS_CACHE_MAX, cached_slot = DHCP_IP_ADDRESS_CACHE_MAX; a < DHCP_IP_ADDRESS_CACHE_MAX; a++) {
/* Check for matching MAC address */
if (memcmp(&_mac_addr_cache[a], &mac_id, sizeof(mac_id)) == 0) {
/* Cached device found */
cached_slot = a;
break;
} else if (first_empty_slot == DHCP_IP_ADDRESS_CACHE_MAX && memcmp(&_mac_addr_cache[a], &empty_cache, sizeof(cy_mac_addr_t)) == 0) {
/* Device not found in cache. Return the first empty slot */
first_empty_slot = a;
}
}
if (cached_slot != DHCP_IP_ADDRESS_CACHE_MAX) {
/* Update IP address of cached device */
_ip_addr_cache[cached_slot] = addr;
} else if (first_empty_slot != DHCP_IP_ADDRESS_CACHE_MAX) {
/* Add device to the first empty slot */
_mac_addr_cache[first_empty_slot] = mac_id;
_ip_addr_cache[first_empty_slot] = addr;
} else {
/* Cache is full. Add device to slot 0 */
_mac_addr_cache[0] = mac_id;
_ip_addr_cache [0] = addr;
}
}
bool CyDhcpServer::lookupAddress(const cy_mac_addr_t &mac_id, cy_ip_addr_t &addr)
{
/* Check whether device is already cached */
for (uint32_t a = 0; a < DHCP_IP_ADDRESS_CACHE_MAX; a++) {
if (memcmp(&_mac_addr_cache[a], &mac_id, sizeof(mac_id)) == 0) {
addr = _ip_addr_cache[a];
return true;
}
}
return false;
}
void CyDhcpServer::freeAddress(const cy_mac_addr_t &mac_id)
{
/* Check whether device is already cached */
for (uint32_t a = 0; a < DHCP_IP_ADDRESS_CACHE_MAX; a++) {
if (memcmp(&_mac_addr_cache[a], &mac_id, sizeof(mac_id)) == 0) {
memset(&_mac_addr_cache[a], 0, sizeof(_mac_addr_cache[a]));
memset(&_ip_addr_cache[a], 0, sizeof(_ip_addr_cache[a]));
}
}
}
void CyDhcpServer::handleDiscover(dhcp_packet_t *dhcp)
{
#ifdef DHCP_EXTENSIVE_DEBUG
dhcp_server_print_header_info(dhcp, DHCP_PACKET_SIZE, "\n\nDHCP DISCOVER RECEIVED");
#endif
uint32_t index;
cy_mac_addr_t client_mac;
cy_ip_addr_t client_ip;
memcpy(&client_mac, dhcp->ClientHwAddr, sizeof(client_mac));
if (!lookupAddress(client_mac, client_ip)) {
client_ip = _available_addr;
}
memset(&dhcp->Legacy, 0, sizeof(dhcp->Legacy));
memset(&dhcp->Options[0], 0, DHCP_PACKET_SIZE - sizeof(dhcp_packet_t) + 3);
dhcp->Opcode = BOOTP_OP_REPLY;
dhcp->YourIpAddr = htonl(client_ip.addrv4.addr);
dhcp->MagicCookie = htonl(static_cast<uint32_t>(DHCP_MSG_MAGIC_COOKIE));
/* Add options */
index = 0;
addOption(dhcp, index, DHCP_MESSAGETYPE_OPTION_CODE, static_cast<uint8_t>(DHCP_MSG_TYPE_OFFER));
addCommonOptions(dhcp, index, _server_addr.addrv4.addr, _netmask.addrv4.addr);
addOption(dhcp, index, static_cast<uint8_t>(DHCP_END_OPTION_CODE));
uint32_t size = sizeof(dhcp_packet_t) + index - 1;
CY_ASSERT(size <= DHCP_PACKET_SIZE);
#ifdef DHCP_EXTENSIVE_DEBUG
dhcp_server_print_header_info(dhcp, size, "\n\nDHCP OFFER SENT");
#endif
sendPacket(&_socket, dhcp, size);
}
void CyDhcpServer::handleRequest(dhcp_packet_t *dhcp)
{
#ifdef DHCP_EXTENSIVE_DEBUG
dhcp_server_print_header_info(dhcp, DHCP_PACKET_SIZE, "\n\nDHCP REQUEST RECEIVED");
#endif
cy_mac_addr_t client_mac;
cy_ip_addr_t client_ip;
cy_ip_addr_t req_ip;
bool increment = false;
uint32_t index;
/* Check that the REQUEST is for this server */
uint32_t *server_id_req = (uint32_t *)findOption(dhcp, DHCP_SERVER_IDENTIFIER_OPTION_CODE);
if ((server_id_req == NULL) || ((server_id_req != NULL) && (_server_addr.addrv4.addr != *server_id_req))) {
return; /* Server ID was not found or does not match local IP address */
}
/* Locate the requested address in the options and keep requested address */
req_ip.addrv4.addr = ntohl(*(uint32_t *)findOption(dhcp, DHCP_REQUESTED_IP_ADDRESS_OPTION_CODE));
memcpy(&client_mac, dhcp->ClientHwAddr, sizeof(client_mac));
if (!lookupAddress(client_mac, client_ip)) {
client_ip = _available_addr;
increment = true;
}
memset(&dhcp->Legacy, 0, sizeof(dhcp->Legacy));
memset(&dhcp->Options[0], 0, DHCP_PACKET_SIZE - sizeof(dhcp_packet_t) + 3);
dhcp->Opcode = BOOTP_OP_REPLY;
dhcp->MagicCookie = htonl(static_cast<uint32_t>(DHCP_MSG_MAGIC_COOKIE));
index = 0;
/* Check if the requested IP address matches one we have assigned */
if (req_ip.addrv4.addr != client_ip.addrv4.addr) {
/* Request is not for the assigned IP - force client to take next available IP by sending NAK */
addOption(dhcp, index, DHCP_MESSAGETYPE_OPTION_CODE, static_cast<uint8_t>(DHCP_MSG_TYPE_NACK));
addOption(dhcp, index, DHCP_SERVER_IDENTIFIER_OPTION_CODE, _server_addr.addrv4.addr);
printf("\n\nDHCP_THREAD: %d REQUEST NAK\n", __LINE__);
} else {
dhcp->YourIpAddr = htonl(client_ip.addrv4.addr);
addOption(dhcp, index, DHCP_MESSAGETYPE_OPTION_CODE, static_cast<uint8_t>(DHCP_MSG_TYPE_ACK));
addCommonOptions(dhcp, index, _server_addr.addrv4.addr, _netmask.addrv4.addr);
if (increment) {
uint32_t ip_mask = ~(_netmask.addrv4.addr);
uint32_t subnet = _server_addr.addrv4.addr & _netmask.addrv4.addr;
do {
_available_addr.addrv4.addr = subnet | ((_available_addr.addrv4.addr + 1) & ip_mask);
} while (_available_addr.addrv4.addr == _server_addr.addrv4.addr);
}
setAddress(client_mac, client_ip);
}
addOption(dhcp, index, static_cast<uint8_t>(DHCP_END_OPTION_CODE));
uint32_t size = sizeof(dhcp_packet_t) + index - 1;
CY_ASSERT(size <= DHCP_PACKET_SIZE);
#ifdef DHCP_EXTENSIVE_DEBUG
dhcp_server_print_header_info(dhcp, DHCP_PACKET_SIZE, "\n\nDHCP REQUEST REPLY SENT");
#endif
sendPacket(&_socket, dhcp, size);
}
void CyDhcpServer::runServer(void)
{
nsapi_size_or_error_t err_or_size;
_running = true;
/* Create receive DHCP socket */
_socket.open(_nstack);
char iface_name[8] = {0};
_niface->get_interface_name(iface_name);
_socket.setsockopt(NSAPI_SOCKET, NSAPI_BIND_TO_DEVICE, iface_name, strlen(iface_name));
_socket.bind((uint16_t)IP_PORT_DHCP_SERVER);
/* Save the current netmask to be sent in DHCP packets as the 'subnet mask option' */
SocketAddress sock_addr;
_niface->get_ip_address(&sock_addr);
_server_addr.addrv4.addr = string_to_ipv4(sock_addr.get_ip_address());
_niface->get_netmask(&sock_addr);
_netmask.addrv4.addr = string_to_ipv4(sock_addr.get_ip_address());
#ifdef DHCP_EXTENSIVE_DEBUG
printf("DHCP Server started.\n");
printf("DHCP Server: IP : %s\n", _niface->get_ip_address());
printf("DHCP Server: Netmask: %s\n", _niface->get_netmask());
printf("DHCP Server: Gateway: %s\n", _niface->get_gateway());
printf("DHCP Server: MAC : %s\n\n", _niface->get_mac_address());
#endif
/* Calculate the first available IP address which will be served - based on the netmask and the local IP */
uint32_t ip_mask = ~(_netmask.addrv4.addr);
uint32_t subnet = _server_addr.addrv4.addr & _netmask.addrv4.addr;
_available_addr.addrv4.addr = subnet | ((_server_addr.addrv4.addr + 1) & ip_mask);
while (_running) {
/* Sleep until data is received from socket. */
err_or_size = _socket.recv(_buff, DHCP_PACKET_SIZE);
/* Options field in DHCP header is variable length. We are looking for option "DHCP Message Type" that is 3 octets in size (code, length and type) */
/* If the return value is <0, it is an error; if it is >=0, it is the received length */
if (err_or_size < 0 || err_or_size < (int32_t)sizeof(dhcp_packet_t)) {
continue;
}
dhcp_packet_t *dhcp = reinterpret_cast<dhcp_packet_t *>(_buff);
/* Check if the option in the dhcp header is "DHCP Message Type", code value for option "DHCP Message Type" is 53 as per rfc2132 */
if (dhcp->Options[0] != DHCP_MESSAGETYPE_OPTION_CODE) {
printf("%d: %s received option code wrong: %d != %d\n", __LINE__, __func__, dhcp->Options[0], DHCP_MESSAGETYPE_OPTION_CODE);
continue;
}
uint8_t msg_type = dhcp->Options[2];
switch (msg_type) {
case DHCP_MSG_TYPE_DISCOVER:
handleDiscover(dhcp);
break;
case DHCP_MSG_TYPE_REQUEST:
handleRequest(dhcp);
break;
default:
printf("DHCP ERROR: Unhandled dhcp packet type, %d", msg_type);
break;
}
}
}
void CyDhcpServer::threadWrapper(CyDhcpServer *obj)
{
obj->runServer();
}
CyDhcpServer::CyDhcpServer(NetworkStack *nstack, NetworkInterface *niface)
: _nstack(nstack),
_niface(niface),
_thread(osPriorityNormal, DHCP_STACK_SIZE, NULL, "DHCPserver") {}
CyDhcpServer::~CyDhcpServer()
{
stop();
}
cy_rslt_t CyDhcpServer::start(void)
{
cy_rslt_t result = CY_RSLT_SUCCESS;
if (!_running) {
CY_ASSERT(_nstack != NULL);
/* Clear cache */
memset(_mac_addr_cache, 0, sizeof(_mac_addr_cache));
memset(_ip_addr_cache, 0, sizeof(_ip_addr_cache));
/* Start DHCP server */
if (osOK != _thread.start(mbed::callback(threadWrapper, this))) {
result = CY_DHCP_THREAD_CREATION_FAILED;
}
}
return result;
}
cy_rslt_t CyDhcpServer::stop(void)
{
cy_rslt_t result = CY_RSLT_SUCCESS;
if (_running) {
_running = false;
if (NSAPI_ERROR_OK != _socket.close()) {
printf("DHCP ERROR: DHCP socket closure failed.\n");
result = CY_DHCP_STOP_FAILED;
}
if (osOK != _thread.join()) {
printf("DHCP ERROR: DHCP thread join failed.\n");
result = CY_DHCP_STOP_FAILED;
}
}
return result;
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (c) 2018-2019 ARM Limited
* 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 WHD_DHCP_SERVER_H
#define WHD_DHCP_SERVER_H
#include "cy_result.h"
#include "cy_syslib.h"
#include "cynetwork_utils.h"
#include "UDPSocket.h"
#include "netsocket/NetworkInterface.h"
#include "netsocket/NetworkStack.h"
#include "rtos.h"
/* DHCP data structure */
typedef struct {
uint8_t Opcode; /* packet opcode type */
uint8_t HwType; /* hardware addr type */
uint8_t HwLen; /* hardware addr length */
uint8_t Hops; /* gateway hops */
uint32_t TransactionId; /* transaction ID */
uint16_t SecsElapsed; /* seconds since boot began */
uint16_t Flags;
uint32_t ClientIpAddr; /* client IP address */
uint32_t YourIpAddr; /* 'your' IP address */
uint32_t ServerIpAddr; /* server IP address */
uint32_t GatewayIpAddr; /* gateway IP address */
uint8_t ClientHwAddr[16]; /* client hardware address */
uint8_t Legacy[192]; /* SName, File */
uint32_t MagicCookie;
uint8_t Options[3]; /* options area */
/* as of RFC2131 it is variable length */
} dhcp_packet_t;
#define DHCP_SUBNETMASK_OPTION_CODE (1)
#define DHCP_ROUTER_OPTION_CODE (3)
#define DHCP_DNS_SERVER_OPTION_CODE (6)
#define DHCP_HOST_NAME_OPTION_CODE (12)
#define DHCP_MTU_OPTION_CODE (26)
#define DHCP_REQUESTED_IP_ADDRESS_OPTION_CODE (50)
#define DHCP_LEASETIME_OPTION_CODE (51)
#define DHCP_MESSAGETYPE_OPTION_CODE (53)
#define DHCP_SERVER_IDENTIFIER_OPTION_CODE (54)
#define DHCP_PARAM_REQUEST_LIST_OPTION_CODE (55)
#define DHCP_WPAD_OPTION_CODE (252)
#define DHCP_END_OPTION_CODE (255)
#define DHCP_IP_ADDRESS_CACHE_MAX (5)
#define ADDITIONAL_OPTION_BYTES (272)
#define DHCP_PACKET_SIZE (sizeof(dhcp_packet_t) + ADDITIONAL_OPTION_BYTES)
/** DHCP thread could not be started */
#define CY_DHCP_THREAD_CREATION_FAILED CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_MIDDLEWARE_BASE, 0)
/** Error while trying to stop the DHCP server */
#define CY_DHCP_STOP_FAILED CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_MIDDLEWARE_BASE, 1)
/**
* Implementation of a DHCP sever
*/
class CyDhcpServer {
public:
/**
* Create a DHCP server.
*/
CyDhcpServer(NetworkStack *nstack, NetworkInterface *niface);
/**
* Delete the DHCP server.
*/
virtual ~CyDhcpServer();
/**
* Start a DHCP server instance.
* @return CY_RSLT_SUCCESS on success otherwise error.
*/
cy_rslt_t start(void);
/**
* Stop a DHCP server instance.
* @return CY_RSLT_SUCCESS on success otherwise error.
*/
cy_rslt_t stop(void);
private:
NetworkStack *_nstack = NULL;
NetworkInterface *_niface = NULL;
UDPSocket _socket;
Thread _thread;
bool _running = false;
cy_ip_addr_t _available_addr;
cy_ip_addr_t _server_addr;
cy_ip_addr_t _netmask;
cy_mac_addr_t _mac_addr_cache[DHCP_IP_ADDRESS_CACHE_MAX];
cy_ip_addr_t _ip_addr_cache[DHCP_IP_ADDRESS_CACHE_MAX];
uint8_t _buff[DHCP_PACKET_SIZE];
static void threadWrapper(CyDhcpServer *obj);
void runServer(void);
void setAddress(const cy_mac_addr_t &mac_id, const cy_ip_addr_t &addr);
bool lookupAddress(const cy_mac_addr_t &mac_id, cy_ip_addr_t &addr);
void freeAddress(const cy_mac_addr_t &mac_id);
void handleDiscover(dhcp_packet_t *dhcp);
void handleRequest(dhcp_packet_t *dhcp);
};
#endif /* WHD_DHCP_SERVER_H */

View File

@@ -0,0 +1,65 @@
/* WHD Access Point Interface Implementation
* Copyright (c) 2018-2019 ARM Limited
* 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 <cstdlib>
#include <utility>
#include "WhdAccessPoint.h"
WhdAccessPoint::WhdAccessPoint(nsapi_wifi_ap_t ap, whd_bss_type_t bss_type, uint8_t *ie_ptr, uint32_t ie_len) :
WiFiAccessPoint(ap), _bss_type(bss_type)
{
_ie_ptr = (uint8_t *)malloc(ie_len * sizeof(uint8_t));
if (_ie_ptr != NULL) {
_ie_len = ie_len;
memcpy(_ie_ptr, ie_ptr, ie_len * sizeof(uint8_t));
}
}
WhdAccessPoint &WhdAccessPoint::operator=(WhdAccessPoint &&rhs)
{
if (this != &rhs) {
WiFiAccessPoint::operator=(rhs);
_bss_type = rhs._bss_type;
_ie_ptr = rhs._ie_ptr;
_ie_len = rhs._ie_len;
rhs._ie_ptr = NULL;
rhs._ie_len = 0;
}
return *this;
}
whd_bss_type_t WhdAccessPoint::get_bss_type() const
{
return _bss_type;
}
uint8_t *WhdAccessPoint::get_ie_data() const
{
return _ie_ptr;
}
uint32_t WhdAccessPoint::get_ie_len() const
{
return _ie_len;
}
WhdAccessPoint::~WhdAccessPoint()
{
if (_ie_ptr != NULL) {
free(_ie_ptr);
}
}

View File

@@ -0,0 +1,74 @@
/* WHD Access Point Interface
* Copyright (c) 2017-2019 ARM Limited
* 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 WHD_ACCESS_POINT_H
#define WHD_ACCESS_POINT_H
#include "netsocket/WiFiAccessPoint.h"
#include "whd_types.h"
/* Enum for scan result type */
enum scan_result_type {
SRES_TYPE_WIFI_ACCESS_POINT,
SRES_TYPE_WHD_ACCESS_POINT
};
/** WhdAccessPoint class
*
* Class that represents a Whd Access Point
* which contains additional Whd specific information
*/
class WhdAccessPoint : public WiFiAccessPoint {
public:
WhdAccessPoint() : WiFiAccessPoint() {};
WhdAccessPoint(nsapi_wifi_ap_t ap, whd_bss_type_t bss_type, uint8_t *ie_ptr, uint32_t ie_len);
/** Define move assignment and prevent copy-assignment
*
* Due to IE element data could have large memory footprint,
* only move assignment is allowed.
*/
WhdAccessPoint &operator=(WhdAccessPoint &&rhs);
WhdAccessPoint &operator=(const WhdAccessPoint &rhs) = delete;
/** Get WHD access point's bss type
*
* @return The whd_bss_type_t of the access point
*/
whd_bss_type_t get_bss_type() const;
/** Get WHD access point's IE data
*
* @return The pointer to ie data buffer
*/
uint8_t *get_ie_data() const;
/** Get WHD access point's IE length
*
* @return The ie data length
*/
uint32_t get_ie_len() const;
virtual ~WhdAccessPoint();
private:
whd_bss_type_t _bss_type;
uint8_t *_ie_ptr; /**< Pointer to received Beacon/Probe Response IE(Information Element) */
uint32_t _ie_len; /**< Length of IE(Information Element) */
};
#endif

View File

@@ -0,0 +1,665 @@
/* WHD STAION implementation of NetworkInterfaceAPI
* Copyright (c) 2017-2019 ARM Limited
* 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 "WhdSTAInterface.h"
#include "nsapi.h"
#include "lwipopts.h"
#include "lwip/etharp.h"
#include "lwip/ethip6.h"
#include "rtos.h"
#include "whd_emac.h"
#include "whd_wifi_api.h"
#include "whd_wlioctl.h"
#define CMP_MAC( a, b ) (((((unsigned char*)a)[0])==(((unsigned char*)b)[0]))&& \
((((unsigned char*)a)[1])==(((unsigned char*)b)[1]))&& \
((((unsigned char*)a)[2])==(((unsigned char*)b)[2]))&& \
((((unsigned char*)a)[3])==(((unsigned char*)b)[3]))&& \
((((unsigned char*)a)[4])==(((unsigned char*)b)[4]))&& \
((((unsigned char*)a)[5])==(((unsigned char*)b)[5])))
struct whd_scan_userdata {
rtos::Semaphore *sema;
scan_result_type sres_type;
WiFiAccessPoint *aps;
std::vector<whd_scan_result_t> *result_buff;
unsigned count;
unsigned offset;
bool scan_in_progress;
};
static whd_scan_userdata interal_scan_data;
static whd_scan_result_t internal_scan_result;
static uint16_t sta_link_update_entry = 0xFF;
static const whd_event_num_t sta_link_change_events[] = {
WLC_E_SET_SSID, WLC_E_LINK, WLC_E_AUTH, WLC_E_ASSOC, WLC_E_DEAUTH_IND, WLC_E_DISASSOC_IND, WLC_E_DISASSOC,
WLC_E_REASSOC, WLC_E_PSK_SUP, WLC_E_ACTION_FRAME_COMPLETE, WLC_E_NONE
};
extern "C" void whd_emac_wifi_link_state_changed(whd_interface_t ifp, whd_bool_t state_up);
int whd_toerror(whd_result_t res)
{
switch (res) {
case WHD_SUCCESS:
return NSAPI_ERROR_OK;
case WHD_UNSUPPORTED:
case WHD_WLAN_UNSUPPORTED:
case WHD_WLAN_ACM_NOTSUPPORTED:
return NSAPI_ERROR_UNSUPPORTED;
case WHD_BADARG:
case WHD_WLAN_BADARG:
return NSAPI_ERROR_PARAMETER;
case WHD_WLAN_NOTASSOCIATED:
case WHD_INVALID_JOIN_STATUS:
return NSAPI_ERROR_NO_CONNECTION;
case WHD_BUFFER_UNAVAILABLE_PERMANENT:
case WHD_BUFFER_UNAVAILABLE_TEMPORARY:
case WHD_RX_BUFFER_ALLOC_FAIL:
case WHD_BUFFER_ALLOC_FAIL:
case WHD_WLAN_NOMEM:
case WHD_MALLOC_FAILURE:
return NSAPI_ERROR_NO_MEMORY;
case WHD_ACCESS_POINT_NOT_FOUND:
case WHD_NETWORK_NOT_FOUND:
return NSAPI_ERROR_NO_SSID;
case WHD_NOT_AUTHENTICATED:
case WHD_INVALID_KEY:
case WHD_NOT_KEYED:
return NSAPI_ERROR_AUTH_FAILURE;
case WHD_PENDING:
case WHD_JOIN_IN_PROGRESS:
return NSAPI_ERROR_IN_PROGRESS;
case WHD_CONNECTION_LOST:
return NSAPI_ERROR_CONNECTION_LOST;
case WHD_TIMEOUT:
case WHD_EAPOL_KEY_PACKET_M1_TIMEOUT:
case WHD_EAPOL_KEY_PACKET_M3_TIMEOUT:
case WHD_EAPOL_KEY_PACKET_G1_TIMEOUT:
return NSAPI_ERROR_CONNECTION_TIMEOUT;
case WHD_WLAN_BUSY:
return NSAPI_ERROR_BUSY;
case WHD_WLAN_NODEVICE:
return NSAPI_ERROR_DEVICE_ERROR;
default:
return -res;
}
}
static nsapi_security_t whd_tosecurity(whd_security_t sec)
{
switch (sec) {
case WHD_SECURITY_OPEN:
return NSAPI_SECURITY_NONE;
case WHD_SECURITY_WEP_PSK:
case WHD_SECURITY_WEP_SHARED:
return NSAPI_SECURITY_WEP;
case WHD_SECURITY_WPA_MIXED_PSK:
case WHD_SECURITY_WPA_TKIP_PSK:
case WHD_SECURITY_WPA_TKIP_ENT:
return NSAPI_SECURITY_WPA;
case WHD_SECURITY_WPA2_WPA_AES_PSK:
case WHD_SECURITY_WPA2_WPA_MIXED_PSK:
return NSAPI_SECURITY_WPA_WPA2;
case WHD_SECURITY_WPA2_MIXED_ENT:
return NSAPI_SECURITY_WPA2_ENT;
case WHD_SECURITY_WPA2_MIXED_PSK:
case WHD_SECURITY_WPA2_AES_PSK:
case WHD_SECURITY_WPA2_AES_ENT:
case WHD_SECURITY_WPA2_FBT_PSK:
case WHD_SECURITY_WPA2_FBT_ENT:
return NSAPI_SECURITY_WPA2;
case WHD_SECURITY_WPA3_SAE:
return NSAPI_SECURITY_WPA3;
case WHD_SECURITY_WPA3_WPA2_PSK:
return NSAPI_SECURITY_WPA3_WPA2;
default:
return NSAPI_SECURITY_UNKNOWN;
}
}
whd_security_t whd_fromsecurity(nsapi_security_t sec)
{
switch (sec) {
case NSAPI_SECURITY_NONE:
return WHD_SECURITY_OPEN;
case NSAPI_SECURITY_WEP:
return WHD_SECURITY_WEP_PSK;
case NSAPI_SECURITY_WPA:
return WHD_SECURITY_WPA_MIXED_PSK;
case NSAPI_SECURITY_WPA2:
return WHD_SECURITY_WPA2_AES_PSK;
case NSAPI_SECURITY_WPA_WPA2:
return WHD_SECURITY_WPA2_MIXED_PSK;
case NSAPI_SECURITY_WPA2_ENT:
return WHD_SECURITY_WPA2_MIXED_ENT;
case NSAPI_SECURITY_WPA3:
return WHD_SECURITY_WPA3_SAE;
case NSAPI_SECURITY_WPA3_WPA2:
return WHD_SECURITY_WPA3_WPA2_PSK;
default:
return WHD_SECURITY_UNKNOWN;
}
}
static void *whd_wifi_link_state_change_handler(whd_interface_t ifp,
const whd_event_header_t *event_header,
const uint8_t *event_data,
void *handler_user_data)
{
UNUSED_PARAMETER(event_data);
if (event_header->bsscfgidx >= WHD_INTERFACE_MAX) {
WPRINT_WHD_DEBUG(("%s: event_header: Bad interface\n", __FUNCTION__));
return NULL;
}
if ((event_header->event_type == WLC_E_DEAUTH_IND) ||
(event_header->event_type == WLC_E_DISASSOC_IND) ||
((event_header->event_type == WLC_E_PSK_SUP) &&
(event_header->status == WLC_SUP_KEYED) &&
(event_header->reason == WLC_E_SUP_DEAUTH))) {
whd_emac_wifi_link_state_changed(ifp, WHD_FALSE);
return handler_user_data;
}
if (((event_header->event_type == WLC_E_PSK_SUP) &&
(event_header->status == WLC_SUP_KEYED) &&
(event_header->reason == WLC_E_SUP_OTHER)) ||
(whd_wifi_is_ready_to_transceive(ifp) == WHD_SUCCESS)) {
whd_emac_wifi_link_state_changed(ifp, WHD_TRUE);
return handler_user_data;
}
return handler_user_data;
}
MBED_WEAK WhdSTAInterface::OlmInterface &WhdSTAInterface::OlmInterface::get_default_instance()
{
static OlmInterface olm;
return olm;
}
WhdSTAInterface::WhdSTAInterface(WHD_EMAC &emac, OnboardNetworkStack &stack, OlmInterface &olm, whd_interface_shared_info_t &shared)
: EMACInterface(emac, stack),
_ssid("\0"),
_pass("\0"),
_security(NSAPI_SECURITY_NONE),
_whd_emac(emac),
_olm(&olm),
_iface_shared(shared)
{
}
nsapi_error_t WhdSTAInterface::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 WhdSTAInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
if ((ssid == NULL) ||
(strlen(ssid) == 0) ||
(pass == NULL && (security != NSAPI_SECURITY_NONE && security != NSAPI_SECURITY_WPA2_ENT)) ||
(strlen(pass) == 0 && (security != NSAPI_SECURITY_NONE && security != NSAPI_SECURITY_WPA2_ENT)) ||
(strlen(pass) > 63 && (security == NSAPI_SECURITY_WPA2 || security == NSAPI_SECURITY_WPA ||
security == NSAPI_SECURITY_WPA_WPA2 || security == NSAPI_SECURITY_WPA3 || security == NSAPI_SECURITY_WPA3_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 WhdSTAInterface::connect()
{
ScopedMutexLock lock(_iface_shared.mutex);
#define MAX_RETRY_COUNT ( 5 )
int i;
whd_result_t res;
// initialize wiced, this is noop if already init
if (!_whd_emac.powered_up) {
if(!_whd_emac.power_up()) {
return NSAPI_ERROR_DEVICE_ERROR;
}
}
res = whd_management_set_event_handler(_whd_emac.ifp, sta_link_change_events,
whd_wifi_link_state_change_handler, NULL, &sta_link_update_entry);
if (res != WHD_SUCCESS) {
return whd_toerror(res);
}
_iface_shared.if_status_flags |= IF_STATUS_STA_UP;
_iface_shared.default_if_cfg = DEFAULT_IF_STA;
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);
_iface_shared.iface_sta = _interface;
} else {
_stack.set_default_interface(_interface);
}
// Initialize the Offload Manager
if (_olm != NULL) {
_olm->init_ols(_whd_emac.ifp, this);
}
if ((_ssid == NULL) ||
(strlen(_ssid) == 0)) {
return NSAPI_ERROR_PARAMETER;
}
// setup ssid
whd_ssid_t ssid;
strncpy((char *)ssid.value, _ssid, SSID_NAME_SIZE);
ssid.value[SSID_NAME_SIZE - 1] = '\0';
ssid.length = strlen((char *)ssid.value);
// choose network security
whd_security_t security = whd_fromsecurity(_security);
#if defined MBED_CONF_APP_WIFI_PASSWORD_WPA2PSK
/* Set PSK password for WPA3_WPA2 */
if (security == WHD_SECURITY_WPA3_WPA2_PSK) {
res = (whd_result_t)whd_wifi_enable_sup_set_passphrase( _whd_emac.ifp, (const uint8_t *)MBED_CONF_APP_WIFI_PASSWORD_WPA2PSK,
strlen(MBED_CONF_APP_WIFI_PASSWORD_WPA2PSK), WHD_SECURITY_WPA3_WPA2_PSK );
}
#else
/* Set PSK password for WPA3_WPA2 */
if (security == WHD_SECURITY_WPA3_WPA2_PSK) {
res = (whd_result_t)whd_wifi_enable_sup_set_passphrase( _whd_emac.ifp, (const uint8_t *)_pass,
strlen(_pass), WHD_SECURITY_WPA3_WPA2_PSK );
}
#endif
// join the network
for (i = 0; i < MAX_RETRY_COUNT; i++) {
res = (whd_result_t)whd_wifi_join(_whd_emac.ifp,
&ssid,
security,
(const uint8_t *)_pass, strlen(_pass));
if (res == WHD_SUCCESS) {
break;
}
}
if (res != WHD_SUCCESS) {
return whd_toerror(res);
}
if (whd_wifi_is_ready_to_transceive(_whd_emac.ifp) == WHD_SUCCESS) {
whd_emac_wifi_link_state_changed(_whd_emac.ifp, WHD_TRUE);
}
// bring up
return _interface->bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0,
DEFAULT_STACK);
}
void WhdSTAInterface::wifi_on()
{
if (!_whd_emac.powered_up) {
if(!_whd_emac.power_up()) {
CY_ASSERT(false);
}
}
}
nsapi_error_t WhdSTAInterface::disconnect()
{
ScopedMutexLock lock(_iface_shared.mutex);
if (!_interface) {
return NSAPI_STATUS_DISCONNECTED;
}
// bring down
int err = _interface->bringdown();
if (err) {
return err;
}
_iface_shared.if_status_flags &= ~IF_STATUS_STA_UP;
if (_iface_shared.if_status_flags & IF_STATUS_SOFT_AP_UP) {
_iface_shared.default_if_cfg = DEFAULT_IF_SOFT_AP;
_stack.set_default_interface(_iface_shared.iface_softap);
} else {
_iface_shared.default_if_cfg = DEFAULT_IF_NOT_SET;
}
// leave network
whd_result_t res = whd_wifi_leave(_whd_emac.ifp);
if (res != WHD_SUCCESS) {
return whd_toerror(res);
}
whd_emac_wifi_link_state_changed(_whd_emac.ifp, WHD_FALSE);
// remove the interface added in connect
if (_interface) {
nsapi_error_t err = _stack.remove_ethernet_interface(&_interface);
if (err != NSAPI_ERROR_OK) {
return err;
}
_iface_shared.iface_sta = NULL;
}
res = whd_wifi_deregister_event_handler(_whd_emac.ifp, sta_link_update_entry);
if (res != WHD_SUCCESS) {
return whd_toerror(res);
}
// de-init Offload Manager
if (_olm != NULL) {
_olm->deinit_ols();
}
return NSAPI_ERROR_OK;
}
int8_t WhdSTAInterface::get_rssi()
{
int32_t rssi;
whd_result_t res;
if (!_whd_emac.powered_up) {
if(!_whd_emac.power_up()) {
CY_ASSERT(false);
}
}
res = (whd_result_t)whd_wifi_get_rssi(_whd_emac.ifp, &rssi);
if (res != 0) {
/* The network GT tests expect that this function should return 0 in case of an error and not assert */
return 0;
}
return (int8_t)rssi;
}
static void whd_scan_handler(whd_scan_result_t **result_ptr,
void *user_data, whd_scan_status_t status)
{
whd_scan_userdata *data = (whd_scan_userdata *)user_data;
/* Even after stopping scan, some results will still come as results are already present in the queue */
if (data->scan_in_progress == false) {
return;
}
// finished scan, either succesfully or through an abort
if (status != WHD_SCAN_INCOMPLETE) {
data->scan_in_progress = false;
data->sema->release();
return;
}
// can't really keep anymore scan results
if (data->count > 0 && data->offset >= data->count) {
/* We can not abort the scan as this function is getting executed in WHD context,
Note that to call any WHD API, caller function should not in WHD context */
return;
}
whd_scan_result_t *record = *result_ptr;
for (unsigned int i = 0; i < data->result_buff->size(); i++) {
if (CMP_MAC((*data->result_buff)[i].BSSID.octet, record->BSSID.octet)) {
return;
}
}
if (data->count > 0 && data->aps != NULL) {
// get ap stats
nsapi_wifi_ap ap;
uint8_t length = record->SSID.length;
if (length < sizeof(ap.ssid) - 1) {
length = sizeof(ap.ssid) - 1;
}
memcpy(ap.ssid, record->SSID.value, length);
ap.ssid[length] = '\0';
memcpy(ap.bssid, record->BSSID.octet, sizeof(ap.bssid));
ap.security = whd_tosecurity(record->security);
ap.rssi = record->signal_strength;
ap.channel = record->channel;
if (data->sres_type == SRES_TYPE_WIFI_ACCESS_POINT) {
data->aps[data->offset] = WiFiAccessPoint(ap);
} else if (data->sres_type == SRES_TYPE_WHD_ACCESS_POINT) {
WhdAccessPoint *aps_sres = static_cast<WhdAccessPoint *>(data->aps);
aps_sres[data->offset] = std::move(WhdAccessPoint(ap, record->bss_type,
record->ie_ptr, record->ie_len));
}
}
// store to result_buff for future duplication removal
data->result_buff->push_back(*record);
data->offset = data->result_buff->size();
}
int WhdSTAInterface::internal_scan(WiFiAccessPoint *aps, unsigned count, scan_result_type sres_type)
{
ScopedMutexLock lock(_iface_shared.mutex);
// initialize wiced, this is noop if already init
if (!_whd_emac.powered_up) {
if(!_whd_emac.power_up()) {
return NSAPI_ERROR_DEVICE_ERROR;
}
}
interal_scan_data.sema = new Semaphore();
interal_scan_data.sres_type = sres_type;
interal_scan_data.aps = aps;
interal_scan_data.count = count;
interal_scan_data.offset = 0;
interal_scan_data.scan_in_progress = true;
interal_scan_data.result_buff = new std::vector<whd_scan_result_t>();
whd_result_t whd_res;
int res;
whd_res = (whd_result_t)whd_wifi_scan(_whd_emac.ifp, WHD_SCAN_TYPE_ACTIVE, WHD_BSS_TYPE_ANY,
NULL, NULL, NULL, NULL, whd_scan_handler, &internal_scan_result, &interal_scan_data);
if (whd_res != WHD_SUCCESS) {
res = whd_toerror(whd_res);
} else {
interal_scan_data.sema->acquire();
res = interal_scan_data.offset;
}
delete interal_scan_data.sema;
delete interal_scan_data.result_buff;
return res;
}
int WhdSTAInterface::scan(WiFiAccessPoint *aps, unsigned count)
{
return internal_scan(aps, count, SRES_TYPE_WIFI_ACCESS_POINT);
}
int WhdSTAInterface::scan_whd(WhdAccessPoint *aps, unsigned count)
{
return internal_scan(aps, count, SRES_TYPE_WHD_ACCESS_POINT);
}
int WhdSTAInterface::is_interface_connected(void)
{
if (!_whd_emac.ifp) {
return WHD_INTERFACE_NOT_UP;
}
_whd_emac.ifp->role = WHD_STA_ROLE;
if ((whd_wifi_is_ready_to_transceive(_whd_emac.ifp) == WHD_SUCCESS)) {
return WHD_SUCCESS;
} else {
return WHD_CONNECTION_LOST;
}
}
int WhdSTAInterface::get_bssid(uint8_t *bssid)
{
whd_mac_t ap_mac;
whd_result_t res = WHD_SUCCESS;
memset(&ap_mac, 0, sizeof(ap_mac));
_whd_emac.ifp->role = WHD_STA_ROLE;
if ((whd_wifi_is_ready_to_transceive(_whd_emac.ifp) == WHD_SUCCESS)) {
res = whd_wifi_get_bssid(_whd_emac.ifp, &ap_mac);
if (res == WHD_SUCCESS) {
memcpy(bssid, ap_mac.octet, sizeof(ap_mac.octet));
}
}
return res;
}
int WhdSTAInterface::whd_log_print(void)
{
return whd_wifi_print_whd_log(_whd_emac.drvp);
}
int WhdSTAInterface::whd_log_read(char *buffer, int buffer_size)
{
whd_result_t res = WHD_SUCCESS;
if (buffer != NULL) {
res = whd_wifi_read_wlan_log(_whd_emac.drvp, buffer, buffer_size);
}
return res;
}
nsapi_error_t WhdSTAInterface::wifi_get_ac_params_sta(void *acp)
{
whd_result_t res = WHD_SUCCESS;
edcf_acparam_t *ac_param = (edcf_acparam_t *)acp;
res = whd_wifi_get_acparams(_whd_emac.ifp, ac_param);
if (res != WHD_SUCCESS) {
return res;
}
return res;
}
int WhdSTAInterface::wifi_set_iovar_value(const char *iovar, uint32_t value)
{
whd_result_t res = WHD_SUCCESS;
_whd_emac.ifp->role = WHD_STA_ROLE;
res = whd_wifi_set_iovar_value(_whd_emac.ifp, iovar, value);
return res;
}
int WhdSTAInterface::wifi_get_iovar_value(const char *iovar, uint32_t *value)
{
int res = WHD_SUCCESS;
_whd_emac.ifp->role = WHD_STA_ROLE;
res = whd_wifi_get_iovar_value(_whd_emac.ifp, iovar, value);
return res;
}
int WhdSTAInterface::wifi_set_ioctl_value(uint32_t ioctl, uint32_t value)
{
int res = WHD_SUCCESS;
_whd_emac.ifp->role = WHD_STA_ROLE;
res = whd_wifi_set_ioctl_value(_whd_emac.ifp, ioctl, value);
return res;
}
int WhdSTAInterface::wifi_get_ioctl_value(uint32_t ioctl, uint32_t *value )
{
int res = WHD_SUCCESS;
_whd_emac.ifp->role = WHD_STA_ROLE;
res = whd_wifi_get_ioctl_value(_whd_emac.ifp, ioctl, value);
return res;
}
int WhdSTAInterface::wifi_get_ioctl_buffer(uint32_t ioctl, uint8_t *buffer, uint16_t len)
{
int res = WHD_SUCCESS;
_whd_emac.ifp->role = WHD_STA_ROLE;
res = whd_wifi_get_ioctl_buffer(_whd_emac.ifp, ioctl, buffer, len);
return res;
}
int WhdSTAInterface::wifi_set_ioctl_buffer(uint32_t ioctl, uint8_t *buffer, uint16_t len)
{
int res = WHD_SUCCESS;
_whd_emac.ifp->role = WHD_STA_ROLE;
res = whd_wifi_set_ioctl_buffer(_whd_emac.ifp, ioctl, buffer, len);
return res;
}
int WhdSTAInterface::wifi_get_ifp(whd_interface_t *ifp)
{
int res = WHD_SUCCESS;
*ifp = _whd_emac.ifp;
return res;
}
int WhdSTAInterface::wifi_set_up(void)
{
int res = WHD_SUCCESS;
res = whd_wifi_set_up(_whd_emac.ifp);
return res;
}
int WhdSTAInterface::wifi_set_down(void)
{
int res = WHD_SUCCESS;
res = whd_wifi_set_down(_whd_emac.ifp);
return res;
}
int WhdSTAInterface::wifi_set_coex_config(whd_coex_config_t *coex_config)
{
int res = WHD_SUCCESS;
res = whd_wifi_set_coex_config(_whd_emac.ifp, coex_config);
return res;
}

View File

@@ -0,0 +1,263 @@
/* WHD implementation of NetworkInterfaceAPI
* Copyright (c) 2017-2019 ARM Limited
* 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 WHD_STA_INTERFACE_H
#define WHD_STA_INTERFACE_H
#include "netsocket/WiFiInterface.h"
#include "netsocket/EMACInterface.h"
#include "netsocket/OnboardNetworkStack.h"
#include "WhdAccessPoint.h"
#include "whd_emac.h"
#include "whd_interface.h"
#include "whd_types_int.h"
struct ol_desc;
/** WhdSTAInterface class
* Implementation of the NetworkStack for the WHD
*/
class WhdSTAInterface : public WiFiInterface, public EMACInterface {
public:
class OlmInterface {
public:
/** Get the default OLM interface. */
static OlmInterface &get_default_instance();
OlmInterface(struct ol_desc *list = NULL) {}
virtual int init_ols(void *whd, void *ip)
{
return 0;
}
virtual int sleep()
{
return 0;
}
virtual int wake()
{
return 0;
}
virtual void deinit_ols(void) {}
};
WhdSTAInterface(
WHD_EMAC &emac = WHD_EMAC::get_instance(),
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance(),
OlmInterface &olm = OlmInterface::get_default_instance(),
whd_interface_shared_info_t &shared = whd_iface_shared);
static WhdSTAInterface *get_default_instance();
/* Turn on the wifi device*/
void wifi_on();
/** 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
*/
nsapi_error_t connect();
/** 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 This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
* @return 0 on success, or 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);
/** Stop the interface
* @return 0 on success, negative on failure
*/
nsapi_error_t disconnect();
/** 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
*/
nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
/** Set the WiFi 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, or 0 for any (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 unsupported.
*
* @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();
/** Scan for available networks in WiFiAccessPoint format
*
* This function will block.
*
* @param ap Pointer to allocated array of WiFiAccessPoint format for discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
*/
int scan(WiFiAccessPoint *res, unsigned count);
/** Scan for available networks in WhdAccessPoint format
*
* This function will block.
*
* @param ap Pointer to allocated array of WhdAccessPoint format for discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
*/
int scan_whd(WhdAccessPoint *res, unsigned count);
/* is interface connected, if yes return WICED_SUCCESS else WICED_NOT_CONNECTED */
int is_interface_connected();
/* get bssid of the AP if success return WICED_SUCCESS else WICED_ERROR */
int get_bssid(uint8_t *bssid);
/* print WHD log (this routine will malloc/free a buffer
* You need to enable printing with WHD_LOGGING_BUFFER_ENABLE
*/
int whd_log_print(void);
/* read WHD log */
int whd_log_read(char *buffer, int buffer_size);
/* Get EDCF AC params */
nsapi_error_t wifi_get_ac_params_sta(void *ac_param);
/* get iovar value */
int wifi_get_iovar_value(const char *iovar, uint32_t *value);
/* set iovar value */
int wifi_set_iovar_value(const char *iovar, uint32_t value);
/* set ioctl value */
int wifi_set_ioctl_value(uint32_t ioctl, uint32_t value) ;
/* get ioctl value */
int wifi_get_ioctl_value(uint32_t ioctl, uint32_t *value);
/* get ioctl buffer */
int wifi_get_ioctl_buffer(uint32_t ioctl, uint8_t *buffer, uint16_t len);
/* set ioctl buffer */
int wifi_set_ioctl_buffer(uint32_t ioctl, uint8_t *buffer, uint16_t len);
/* get WHD ifp value */
int wifi_get_ifp(whd_interface_t *ifp);
/* set wifi interface up */
int wifi_set_up(void);
/* set wifi interface down */
int wifi_set_down(void);
/* set wifi coex configuration */
int wifi_set_coex_config(whd_coex_config_t *coex_config);
/** Set Offload Manager Information
* NOTE: Only allowed while disconnected
*
* @param olm Offload Manager info structure
* @return true if completed successfully
* false if Interface is connected
*/
int set_olm(OlmInterface *olm)
{
if (get_connection_status() == NSAPI_STATUS_DISCONNECTED) {
_olm = olm;
return true;
}
return false;
}
/** Network stack is suspended
*
* @return 0 if successful
*/
int net_suspended()
{
int ret = _olm->sleep();
return ret;
}
/** Network stack is resuming
*
* @return 0 if successful
*/
int net_resuming()
{
int ret = _olm->wake();
return ret;
}
protected:
int internal_scan(WiFiAccessPoint *aps, unsigned count, scan_result_type sres_type);
private:
char _ssid[33]; /* The longest possible name (defined in 802.11) +1 for the \0 */
char _pass[64]; /* The longest allowed passphrase + 1 */
nsapi_security_t _security;
WHD_EMAC &_whd_emac;
OlmInterface *_olm;
whd_interface_shared_info_t &_iface_shared;
};
#endif

View File

@@ -0,0 +1,237 @@
/*
* Copyright (c) 2018-2019 ARM Limited
* 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 "nsapi.h"
#include "lwipopts.h"
#include "WhdSoftAPInterface.h"
#include "nsapi.h"
#include "lwipopts.h"
#include "lwip/etharp.h"
#include "lwip/ethip6.h"
#include "rtos.h"
#include "whd_emac.h"
#include "whd_wifi_api.h"
extern int whd_toerror(whd_result_t res);
extern nsapi_security_t whd_tosecurity(whd_security_t sec);
extern whd_security_t whd_fromsecurity(nsapi_security_t sec);
extern "C" void whd_emac_wifi_link_state_changed(whd_interface_t ifp, whd_bool_t state_up);
static const whd_event_num_t ap_events[] = { WLC_E_LINK, WLC_E_IF, WLC_E_DEAUTH, WLC_E_DEAUTH_IND, WLC_E_DISASSOC, WLC_E_DISASSOC_IND, WLC_E_ASSOC_IND, WLC_E_REASSOC_IND, WLC_E_NONE };
static void *whd_default_handle_softap_events(whd_interface_t ifp, const whd_event_header_t *event_header,
const uint8_t *event_data, void *handler_user_data)
{
whd_driver_t whd_driver = ifp->whd_driver;
UNUSED_PARAMETER(event_header);
UNUSED_PARAMETER(event_data);
UNUSED_PARAMETER(handler_user_data);
WHD_IOCTL_LOG_ADD_EVENT(whd_driver, event_header->event_type, event_header->flags, event_header->reason);
if ((event_header->event_type == (whd_event_num_t)WLC_E_LINK) ||
(event_header->event_type == WLC_E_IF)) {
if (osSemaphoreGetCount(whd_driver->ap_info.whd_wifi_sleep_flag) < 1) {
osStatus_t result = osSemaphoreRelease(whd_driver->ap_info.whd_wifi_sleep_flag);
if (result != osOK) {
printf("Release whd_wifi_sleep_flag ERROR: %d", result);
}
}
}
return handler_user_data;
}
WhdSoftAPInterface::WhdSoftAPInterface(WHD_EMAC &emac, OnboardNetworkStack &stack, whd_interface_shared_info_t &shared)
: EMACInterface(emac, stack),
_whd_emac(emac),
_iface_shared(shared)
{
}
int WhdSoftAPInterface::start(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel,
bool start_dhcp_server, const whd_custom_ie_info_t *ie_info, bool ap_sta_concur)
{
ScopedMutexLock lock(_iface_shared.mutex);
nsapi_error_t err;
// power up primary emac interface first
if (ap_sta_concur) {
WHD_EMAC &emac_prime = WHD_EMAC::get_instance(WHD_STA_ROLE);
if (!emac_prime.power_up()) {
printf("Primary interface power up ERROR!\n");
return NSAPI_ERROR_DEVICE_ERROR;
}
}
// set concurrency mode and power up secondary, the bsp init is done by primary emac
_whd_emac.ap_sta_concur = ap_sta_concur;
if (!_whd_emac.power_up()) {
printf("Secondary interface power up ERROR!\n");
return NSAPI_ERROR_DEVICE_ERROR;
}
// setup ssid
whd_ssid_t whd_ssid;
strncpy((char *)whd_ssid.value, ssid, SSID_NAME_SIZE);
whd_ssid.value[SSID_NAME_SIZE - 1] = '\0';
whd_ssid.length = strlen((char *)whd_ssid.value);
// choose network security
whd_security_t whd_security = whd_fromsecurity(security);
/* set up the AP info */
err = whd_wifi_init_ap(_whd_emac.ifp, &whd_ssid, whd_security, (const uint8_t *)pass,
strlen(pass), channel);
if (err != NSAPI_ERROR_OK) {
printf("whd_wifi_init_ap() ERROR: %d\n", err);
return err;
}
// update default softap interface event handler
err = unregister_event_handler();
if (err != NSAPI_ERROR_OK) {
printf("unregister_event_handler() ERROR: %d\n", err);
return err;
}
err = register_event_handler(whd_default_handle_softap_events);
if (err != NSAPI_ERROR_OK) {
printf("register_event_handler() ERROR: %d\n", err);
return err;
}
_iface_shared.if_status_flags |= IF_STATUS_SOFT_AP_UP;
if (!ap_sta_concur || (_iface_shared.default_if_cfg == DEFAULT_IF_NOT_SET)) {
_iface_shared.default_if_cfg = DEFAULT_IF_SOFT_AP;
}
if (!_interface) {
nsapi_error_t err = _stack.add_ethernet_interface(_whd_emac, _iface_shared.default_if_cfg == DEFAULT_IF_SOFT_AP, &_interface);
if (err != NSAPI_ERROR_OK) {
_interface = NULL;
return err;
}
_interface->attach(_connection_status_cb);
_iface_shared.iface_softap = _interface;
} else if (_iface_shared.default_if_cfg == DEFAULT_IF_SOFT_AP) {
_stack.set_default_interface(_interface);
}
if (ie_info) {
err = whd_wifi_manage_custom_ie(_whd_emac.ifp, WHD_ADD_CUSTOM_IE, (const uint8_t *)ie_info->oui,
ie_info->subtype, (const void *)ie_info->data, ie_info->length, ie_info->which_packets);
if (err != NSAPI_ERROR_OK) {
printf("whd_wifi_manage_custom_ie() ERROR: %d\n", err);
return err;
}
}
err = whd_wifi_start_ap(_whd_emac.ifp);
if (err != NSAPI_ERROR_OK) {
printf("whd_wifi_start_ap() ERROR: %d\n", err);
return err;
}
// Set static IP address for SoftAP and bring up
set_dhcp(false);
if (whd_wifi_is_ready_to_transceive(_whd_emac.ifp) == WHD_SUCCESS) {
whd_emac_wifi_link_state_changed(_whd_emac.ifp, WHD_TRUE);
}
err = _interface->bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0,
DEFAULT_STACK);
if (err != NSAPI_ERROR_OK) {
printf("bringup() ERROR: %d\n", err);
}
if (start_dhcp_server) {
_dhcp_server = std::make_unique<CyDhcpServer>(get_stack(), this);
if (CY_RSLT_SUCCESS != _dhcp_server->start()) {
err = NSAPI_ERROR_DHCP_FAILURE;
}
}
return err;
}
int WhdSoftAPInterface::stop(void)
{
ScopedMutexLock lock(_iface_shared.mutex);
if (_dhcp_server && CY_RSLT_SUCCESS != _dhcp_server->stop()) {
return NSAPI_ERROR_DHCP_FAILURE;
}
_dhcp_server.reset();
// bring down
int err = _interface->bringdown();
if (err) {
return err;
}
_iface_shared.if_status_flags &= ~IF_STATUS_SOFT_AP_UP;
if ((_iface_shared.if_status_flags & IF_STATUS_STA_UP) == 0) {
_iface_shared.default_if_cfg = DEFAULT_IF_NOT_SET;
}
// stop softap
whd_result_t res = whd_wifi_stop_ap(_whd_emac.ifp);
if (res != WHD_SUCCESS) {
return whd_toerror(res);
}
// remove the interface added in start
if (_interface) {
nsapi_error_t err = _stack.remove_ethernet_interface(&_interface);
if (err != NSAPI_ERROR_OK) {
return err;
}
_iface_shared.iface_softap = NULL;
}
return NSAPI_ERROR_OK;
}
int WhdSoftAPInterface::remove_custom_ie(const whd_custom_ie_info_t *ie_info)
{
return whd_wifi_manage_custom_ie(_whd_emac.ifp, WHD_REMOVE_CUSTOM_IE, (const uint8_t *)ie_info->oui,
ie_info->subtype, (const void *)ie_info->data, ie_info->length, ie_info->which_packets);
}
int WhdSoftAPInterface::get_associated_client_list(void *client_list_buffer, uint16_t buffer_length)
{
return whd_wifi_get_associated_client_list(_whd_emac.ifp, client_list_buffer, buffer_length);
}
int WhdSoftAPInterface::register_event_handler(whd_event_handler_t softap_event_handler)
{
uint16_t ap_events_entry = _whd_emac.ifp->event_reg_list[WHD_AP_EVENT_ENTRY];
return whd_management_set_event_handler(_whd_emac.ifp, ap_events, softap_event_handler, NULL, &ap_events_entry);
}
int WhdSoftAPInterface::unregister_event_handler(void)
{
return whd_wifi_deregister_event_handler(_whd_emac.ifp, _whd_emac.ifp->event_reg_list[WHD_AP_EVENT_ENTRY]);
}

View File

@@ -0,0 +1,158 @@
/*
* Copyright (c) 2018-2019 ARM Limited
* 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 WHD_SOFTAP_INTERFACE_H
#define WHD_SOFTAP_INTERFACE_H
#include "netsocket/EMACInterface.h"
#include "netsocket/OnboardNetworkStack.h"
#include "whd_emac.h"
#include "CyDhcpServer.h"
#include "whd_interface.h"
#include <memory>
/**
* Vendor IE details
*/
typedef struct {
uint8_t oui[WIFI_IE_OUI_LENGTH]; /**< Unique identifier for the IE */
uint8_t subtype; /**< Sub-type of the IE */
void *data; /**< Pointer to IE data */
uint16_t length; /**< IE data length */
uint16_t which_packets; /**< Mask of the packet in which this IE details to be included */
} whd_custom_ie_info_t;
/** WhdSoftAPInterface class
* Implementation of the SoftAPInterface for the Whd
*/
class WhdSoftAPInterface : public EMACInterface {
public:
/** Construct SoftAP interface
* @return pointer to default WhdSoftAPInterface instance
*/
WhdSoftAPInterface(WHD_EMAC &emac = WHD_EMAC::get_instance(WHD_AP_ROLE),
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance(),
whd_interface_shared_info_t &shared = whd_iface_shared);
/** Get the default WhdSoftAPInterface instance.
* @return pointer to default WhdSoftAPInterface instance
*/
static WhdSoftAPInterface *get_default_instance();
/** Set static IP address for SoftAP
*
* Configures the static IP address of SoftAP
* Requires that the network is stopped.
*
* @param ip_address Null-terminated representation of the local IP address
* @param netmask Null-terminated representation of the local network mask
* @param gateway Null-terminated representation of the local gateway
* @return 0 on success, negative error code on failure
* int set_network(const char *ip_address, const char *netmask, const char *gateway);
*/
/** Start a SoftAP
*
* @param ssid Name of the SoftAP to create
* @param pass Security passphrase for connection to SoftAP
* @param security Type of encryption for connection
* @param channel Channel for SoftAP
* @param start_dhcp_server Start dhcp server for connection
* @param whd_custom_ie Optional Custom IE
* @param ap_sta_concur Enable STA+AP concurrency mode
*
* @return 0 on success, or error code on failure
* see @a nsapi_error
*/
int start(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel,
bool start_dhcp_server = true, const whd_custom_ie_info_t *ie_info = NULL, bool ap_sta_concur = false);
/**
* Remove Wi-Fi custom IE
*
* @param[in] ie_info : Pointer to the structure which contains custom IE information
*
* @return 0 on success, or error code on failure
* see @a nsapi_error
*/
int remove_custom_ie(const whd_custom_ie_info_t *ie_info);
/** Stop the Software Access Point
*
* @return 0 on success, or error code on failure
* see @a nsapi_error
*/
int stop(void);
/**
* Gets information about associated clients.
*
* @note Only applicable if softAP interface is up
*
* @param[out] client_list_buffer : pointer to a buffer that will be populated with a variable length structure defined by @ref whd_maclist_t
* @param[in] buffer_length : length of the buffer
*
* @return 0 on success, or error code on failure
* see @a nsapi_error
*/
int get_associated_client_list(void *client_list_buffer, uint16_t buffer_length);
/**
* Register soft AP event handler
*
* @param[in] softap_event_handler : A function pointer to the event handler
*
* @return 0 on success, or error code on failure
* see @a nsapi_error
*/
int register_event_handler(whd_event_handler_t softap_event_handler);
/**
* Unregister soft AP event handler
*
* @return 0 on success, or error code on failure
* see @a nsapi_error
*/
int unregister_event_handler(void);
/** Set blocking status of interface.
* Nonblocking mode unsupported.
*
* @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;
}
}
protected:
WHD_EMAC &_whd_emac;
std::unique_ptr<CyDhcpServer> _dhcp_server;
whd_interface_shared_info_t &_iface_shared;
};
#endif

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2019 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 INCLUDED_EMAC_EAPOL_H_
#define INCLUDED_EMAC_EAPOL_H_
#include "whd.h"
#ifdef __cplusplus
extern "C"
{
#endif
/******************************************************
* Macros
******************************************************/
/******************************************************
* Constants
******************************************************/
#define EAPOL_PACKET_TYPE (0x888E)
/******************************************************
* Enumerations
******************************************************/
/******************************************************
* Type Definitions
******************************************************/
typedef whd_buffer_t whd_eapol_packet_t;
typedef void (*eapol_packet_handler_t)(whd_interface_t interface, whd_buffer_t buffer);
/******************************************************
* Structures
******************************************************/
/******************************************************
* Global Variables
******************************************************/
/******************************************************
* Function Declarations
******************************************************/
whd_result_t emac_register_eapol_packet_handler(eapol_packet_handler_t eapol_packet_handler);
void emac_unregister_eapol_packet_handler(void);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* ifndef INCLUDED_EMAC_EAPOL_H_ */

View File

@@ -0,0 +1,298 @@
/*
* Copyright (c) 2018-2019 ARM Limited
* 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 "whd_emac.h"
#include "lwip/etharp.h"
#include "lwip/ethip6.h"
#include "events/mbed_shared_queues.h"
#include "whd_wlioctl.h"
#include "whd_buffer_api.h"
#include "cybsp_wifi.h"
#include "emac_eapol.h"
#include "cy_result.h"
#define NULL_MAC(a) ( ( ( ( (unsigned char *)a )[0] ) == 0 ) && \
( ( ( (unsigned char *)a )[1] ) == 0 ) && \
( ( ( (unsigned char *)a )[2] ) == 0 ) && \
( ( ( (unsigned char *)a )[3] ) == 0 ) && \
( ( ( (unsigned char *)a )[4] ) == 0 ) && \
( ( ( (unsigned char *)a )[5] ) == 0 ) )
extern "C"
{
eapol_packet_handler_t emac_eapol_packet_handler = NULL;
void whd_emac_wifi_link_state_changed(whd_interface_t ifp, whd_bool_t state_up);
} // extern "C"
WHD_EMAC::WHD_EMAC(whd_interface_role_t role, const uint8_t *mac_addr)
: interface_type(role)
{
if (mac_addr) {
set_hwaddr(mac_addr);
}
}
WHD_EMAC &WHD_EMAC::get_instance(whd_interface_role_t role, const uint8_t *mac_addr)
{
static WHD_EMAC emac_sta(WHD_STA_ROLE, mac_addr);
static WHD_EMAC emac_ap(WHD_AP_ROLE, mac_addr);
return role == WHD_AP_ROLE ? emac_ap : emac_sta;
}
uint32_t WHD_EMAC::get_mtu_size() const
{
return WHD_PAYLOAD_MTU;
}
uint32_t WHD_EMAC::get_align_preference() const
{
return 0;
}
void WHD_EMAC::add_multicast_group(const uint8_t *addr)
{
memcpy(multicast_addr.octet, addr, sizeof(multicast_addr.octet));
whd_wifi_register_multicast_address(ifp, &multicast_addr);
}
void WHD_EMAC::remove_multicast_group(const uint8_t *addr)
{
whd_wifi_unregister_multicast_address(ifp, &multicast_addr);
}
void WHD_EMAC::set_all_multicast(bool all)
{
/* No-op at this stage */
}
void WHD_EMAC::power_down()
{
if (powered_up) {
powered_up = false;
whd_wifi_off(ifp);
whd_deinit(ifp);
}
}
bool WHD_EMAC::power_up()
{
if (!powered_up) {
cy_rslt_t res = CY_RSLT_SUCCESS;
if (ap_sta_concur && interface_type == WHD_AP_ROLE) {
WHD_EMAC &emac_prime = WHD_EMAC::get_instance(WHD_STA_ROLE);
if (NULL_MAC(unicast_addr.octet)) {
emac_prime.get_hwaddr(unicast_addr.octet);
// Generated mac will set locally administered bit 1 of first byte
unicast_addr.octet[0] |= (1 << 1);
}
// Note: This assumes that the primary interface initializes the
// wifi driver and turns on the wifi chip.
res = cybsp_wifi_init_secondary(&ifp /* Out */, &unicast_addr);
} else {
WHD_EMAC &emac_other = WHD_EMAC::get_instance(interface_type == WHD_STA_ROLE ? WHD_AP_ROLE :
WHD_STA_ROLE);
if (!emac_other.powered_up) {
res = cybsp_wifi_init_primary(&ifp /* OUT */);
} else {
ifp = emac_other.ifp;
}
}
if (CY_RSLT_SUCCESS == res) {
drvp = cybsp_get_wifi_driver();
powered_up = true;
if (link_state && emac_link_state_cb) {
emac_link_state_cb(link_state);
}
} else {
return false;
}
}
return true;
}
bool WHD_EMAC::get_hwaddr(uint8_t *addr) const
{
if (!NULL_MAC(unicast_addr.octet)) {
memcpy(addr, unicast_addr.octet, sizeof(unicast_addr.octet));
} else {
whd_mac_t mac;
whd_result_t res = whd_wifi_get_mac_address(ifp, &mac);
MBED_ASSERT(res == WHD_SUCCESS);
memcpy(addr, mac.octet, sizeof(mac.octet));
}
return true;
}
void WHD_EMAC::set_hwaddr(const uint8_t *addr)
{
memcpy(unicast_addr.octet, addr, sizeof(unicast_addr.octet));
}
uint8_t WHD_EMAC::get_hwaddr_size() const
{
whd_mac_t mac;
return sizeof(mac.octet);
}
void WHD_EMAC::set_link_input_cb(emac_link_input_cb_t input_cb)
{
emac_link_input_cb = input_cb;
}
void WHD_EMAC::set_link_state_cb(emac_link_state_change_cb_t state_cb)
{
emac_link_state_cb = state_cb;
}
void WHD_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr)
{
memory_manager = &mem_mngr;
}
bool WHD_EMAC::link_out(emac_mem_buf_t *buf)
{
uint16_t offset = 64;
whd_buffer_t buffer;
uint16_t size = memory_manager->get_total_len(buf);
whd_result_t res = whd_host_buffer_get(drvp, &buffer, WHD_NETWORK_TX, size + offset, WHD_TRUE);
if (res != WHD_SUCCESS) {
memory_manager->free(buf);
return true;
}
MBED_ASSERT(res == WHD_SUCCESS);
whd_buffer_add_remove_at_front(drvp, &buffer, offset);
void *dest = whd_buffer_get_current_piece_data_pointer(drvp, buffer);
memory_manager->copy_from_buf(dest, size, buf);
if (activity_cb) {
activity_cb(true);
}
whd_network_send_ethernet_data(ifp, buffer);
memory_manager->free(buf);
return true;
}
void WHD_EMAC::get_ifname(char *name, uint8_t size) const
{
switch (interface_type) {
case WHD_STA_ROLE:
memcpy(name, "st", size);
break;
case WHD_AP_ROLE:
memcpy(name, "ap", size);
break;
default:
memcpy(name, "wh", size);
}
}
void WHD_EMAC::set_activity_cb(mbed::Callback<void(bool)> cb)
{
activity_cb = cb;
}
extern "C"
{
static void emac_receive_eapol_packet(whd_interface_t interface, whd_buffer_t buffer)
{
if (buffer != NULL) {
if (emac_eapol_packet_handler != NULL) {
emac_eapol_packet_handler(interface, buffer);
} else {
whd_buffer_release(interface->whd_driver, buffer, WHD_NETWORK_RX);
}
}
}
whd_result_t emac_register_eapol_packet_handler(eapol_packet_handler_t eapol_packet_handler)
{
if (emac_eapol_packet_handler == NULL) {
emac_eapol_packet_handler = eapol_packet_handler;
return WHD_SUCCESS;
}
return WHD_HANDLER_ALREADY_REGISTERED;
}
void emac_unregister_eapol_packet_handler(void)
{
emac_eapol_packet_handler = NULL;
}
void cy_network_process_ethernet_data(whd_interface_t ifp, whd_buffer_t buffer)
{
emac_mem_buf_t *mem_buf = NULL;
WHD_EMAC &emac = WHD_EMAC::get_instance(ifp->role);
if (!emac.powered_up || !emac.emac_link_input_cb) {
// ignore any trailing packets
whd_buffer_release(emac.drvp, buffer, WHD_NETWORK_RX);
return;
}
uint8_t *data = whd_buffer_get_current_piece_data_pointer(emac.drvp, buffer);
uint16_t size = whd_buffer_get_current_piece_size(emac.drvp, buffer);
if (size > WHD_ETHERNET_SIZE) {
uint16_t ethertype;
ethertype = (uint16_t)(data[12] << 8 | data[13]);
if (ethertype == EAPOL_PACKET_TYPE) {
/* pass it to the EAP layer, but do not release the packet */
emac_receive_eapol_packet(ifp, buffer);
} else {
mem_buf = buffer;
if (emac.activity_cb) {
emac.activity_cb(false);
}
emac.emac_link_input_cb(mem_buf);
}
}
}
void whd_emac_wifi_link_state_changed(whd_interface_t ifp, whd_bool_t state_up)
{
WHD_EMAC &emac = WHD_EMAC::get_instance(ifp->role);
emac.link_state = state_up;
if (emac.emac_link_state_cb) {
emac.emac_link_state_cb(state_up);
}
}
} // extern "C"

View File

@@ -0,0 +1,176 @@
/*
* Copyright (c) 2018-2019 ARM Limited
* 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 WHD_EMAC_H_
#define WHD_EMAC_H_
#include "EMAC.h"
#include "EMACInterface.h"
#include "WiFiInterface.h"
#include "whd_int.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h"
class WHD_EMAC : public EMAC {
public:
WHD_EMAC(whd_interface_role_t itype = WHD_STA_ROLE, const uint8_t *mac_addr = NULL);
static WHD_EMAC &get_instance(whd_interface_role_t role = WHD_STA_ROLE, const uint8_t *mac_addr = NULL);
/**
* 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);
/** Set 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;
EMACMemoryManager *memory_manager;
bool powered_up = false;
bool link_state = false;
bool ap_sta_concur = false;
whd_interface_role_t interface_type;
whd_driver_t drvp = NULL;
whd_interface_t ifp = NULL;
whd_mac_t unicast_addr;
whd_mac_t multicast_addr;
mbed::Callback<void(bool)> activity_cb;
};
#endif /* WHD_EMAC_H_ */

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018-2019, Arm Limited and affiliates.
* 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 "WhdSTAInterface.h"
#include "WhdSoftAPInterface.h"
#include "whd_interface.h"
whd_interface_shared_info_t whd_iface_shared;
WiFiInterface *WiFiInterface::get_target_default_instance()
{
static WhdSTAInterface wifi;
return &wifi;
}
WhdSoftAPInterface *WhdSoftAPInterface::get_default_instance()
{
static WhdSoftAPInterface softap;
return &softap;
}

View File

@@ -0,0 +1,51 @@
/* WHD implementation of NetworkInterfaceAPI
* Copyright (c) 2017-2019 ARM Limited
* 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 WHD_INTERFACE_H
#define WHD_INTERFACE_H
#include "rtos/Mutex.h"
#include "OnboardNetworkStack.h"
/** WhdSTAInterface class
* Shared information
*/
#define IF_STATUS_ALL_IF_DOWN 0x0
#define IF_STATUS_STA_UP 0x1
#define IF_STATUS_SOFT_AP_UP 0x2
enum whd_default_interface_config
{
DEFAULT_IF_NOT_SET,
DEFAULT_IF_STA,
DEFAULT_IF_SOFT_AP
};
struct whd_interface_shared_info_t {
rtos::Mutex mutex;
whd_default_interface_config default_if_cfg;
uint32_t if_status_flags;
OnboardNetworkStack::Interface *iface_sta;
OnboardNetworkStack::Interface *iface_softap;
whd_interface_shared_info_t() : default_if_cfg(DEFAULT_IF_NOT_SET), if_status_flags(IF_STATUS_ALL_IF_DOWN),
iface_sta(NULL), iface_softap(NULL)
{}
};
extern whd_interface_shared_info_t whd_iface_shared;
#endif