Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Update CYSBSYSKIT_01 #13416

Merged
merged 5 commits into from Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,65 @@
/*
* 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 <cstdlib>
#include <utility>
#include "SclAccessPoint.h"

SclAccessPoint::SclAccessPoint(nsapi_wifi_ap_t ap, scl_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);
}
}

SclAccessPoint &SclAccessPoint::operator=(SclAccessPoint &&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;
}

scl_bss_type_t SclAccessPoint::get_bss_type() const
{
return _bss_type;
}

uint8_t *SclAccessPoint::get_ie_data() const
{
return _ie_ptr;
}

uint32_t SclAccessPoint::get_ie_len() const
{
return _ie_len;
}

SclAccessPoint::~SclAccessPoint()
{
if (_ie_ptr != NULL) {
free(_ie_ptr);
}
}
@@ -0,0 +1,74 @@
/*
* 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_ACCESS_POINT_H
#define SCL_ACCESS_POINT_H

#include "netsocket/WiFiAccessPoint.h"
#include "scl_types.h"

/* Enum for scan result type */
enum scan_result_type {
SRES_TYPE_WIFI_ACCESS_POINT,
SRES_TYPE_SCL_ACCESS_POINT
};

/** SclAccessPoint class
*
* Class that represents a Scl Access Point
* which contains additional Scl specific information
*/
class SclAccessPoint : public WiFiAccessPoint {
public:
SclAccessPoint() : WiFiAccessPoint() {};
SclAccessPoint(nsapi_wifi_ap_t ap, scl_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.
*/
SclAccessPoint &operator=(SclAccessPoint &&rhs);
SclAccessPoint &operator=(const SclAccessPoint &rhs) = delete;

/** Get SCL access point's bss type
*
* @return The scl_bss_type_t of the access point
*/
scl_bss_type_t get_bss_type() const;

/** Get SCL access point's IE data
*
* @return The pointer to ie data buffer
*/
uint8_t *get_ie_data() const;

/** Get SCL access point's IE length
*
* @return The ie data length
*/
uint32_t get_ie_len() const;

virtual ~SclAccessPoint();

private:
scl_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
Expand Up @@ -27,8 +27,8 @@
#include "scl_emac.h"
#include "scl_ipc.h"
#include "mbed_wait_api.h"


#include "SclAccessPoint.h"
#include "scl_buffer_api.h"
/** @file
* Provides SCL interface functions to be used with WiFiInterface or NetworkInterface Objects
*/
Expand All @@ -43,8 +43,31 @@ struct scl_tx_net_credentials {
const char *network_passphrase;
} scl_tx_network_credentials;


struct scl_scan_userdata {
rtos::Semaphore *sema;
scan_result_type sres_type;
WiFiAccessPoint *aps;
std::vector<scl_scan_result_t> *result_buff;
unsigned count;
unsigned offset;
bool scan_in_progress;
};

static scl_scan_userdata interal_scan_data;
static scl_scan_result_t internal_scan_result;
network_params_t network_parameter;

/* Internal scan callback that handles the scan results */
void scl_scan_handler(scl_scan_result_t *result_ptr,void *user_data, scl_scan_status_t status);

#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])))

int scl_toerror(scl_result_t res)
{
switch (res) {
Expand Down Expand Up @@ -93,14 +116,22 @@ nsapi_security_t scl_tosecurity(scl_security_t sec)
case SCL_SECURITY_WEP_SHARED:
return NSAPI_SECURITY_WEP;
case SCL_SECURITY_WPA_TKIP_PSK:
case SCL_SECURITY_WPA_AES_PSK:
case SCL_SECURITY_WPA_TKIP_ENT:
case SCL_SECURITY_WPA_AES_ENT:
case SCL_SECURITY_WPA_MIXED_ENT:
return NSAPI_SECURITY_WPA;
case SCL_SECURITY_WPA2_MIXED_PSK:
case SCL_SECURITY_WPA2_WPA_PSK:
case SCL_SECURITY_WPA2_WPA_TKIP_PSK:
return NSAPI_SECURITY_WPA_WPA2;
case SCL_SECURITY_WPA2_MIXED_ENT:
return NSAPI_SECURITY_WPA2_ENT;
case SCL_SECURITY_WPA2_AES_PSK:
case SCL_SECURITY_WPA2_AES_ENT:
case SCL_SECURITY_WPA2_FBT_PSK:
case SCL_SECURITY_WPA2_FBT_ENT:
case SCL_SECURITY_WPA2_TKIP_ENT:
return NSAPI_SECURITY_WPA2;
default:
return NSAPI_SECURITY_UNKNOWN;
Expand All @@ -125,12 +156,13 @@ scl_security_t scl_fromsecurity(nsapi_security_t sec)
}
}

SclSTAInterface::SclSTAInterface(SCL_EMAC &emac, OnboardNetworkStack &stack)
SclSTAInterface::SclSTAInterface(SCL_EMAC &emac, OnboardNetworkStack &stack, scl_interface_shared_info_t &shared)
: EMACInterface(emac, stack),
_ssid("\0"),
_pass("\0"),
_security(NSAPI_SECURITY_NONE),
_scl_emac(emac)
_scl_emac(emac),
_iface_shared(shared)
{
}

Expand Down Expand Up @@ -180,7 +212,7 @@ nsapi_error_t SclSTAInterface::connect()
uint32_t connection_status = 0;

scl_tx_network_credentials.network_ssid = _ssid;
if ((strlen(_ssid) < MAX_SSID_LENGTH) && (strlen(_ssid) > MIN_SSID_LENGTH)) {
if ((strlen(_ssid) < MAX_SSID_LENGTH) && (strlen(_ssid) > MIN_SSID_LENGTH) ) {
scl_tx_network_credentials.ssid_len = strlen(_ssid);
} else {
return NSAPI_ERROR_PARAMETER;
Expand Down Expand Up @@ -288,10 +320,106 @@ nsapi_error_t SclSTAInterface::disconnect()
return NSAPI_ERROR_OK;
}

int SclSTAInterface::scan(WiFiAccessPoint *res, unsigned count)
void scl_scan_handler(scl_scan_result_t *result_ptr,
void *user_data, scl_scan_status_t status)
{
scl_scan_userdata *data = (scl_scan_userdata *)&interal_scan_data;
scl_scan_result_t *record = result_ptr;
unsigned int i;
nsapi_wifi_ap ap;
uint8_t length;

/* 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 != SCL_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 SCL context,
Note that to call any SCL API, caller function should not in SCL context */
return;
}

for (i = 0; i < data->result_buff->size(); i++) {
if (memcmp(((*data->result_buff)[i].BSSID.octet),(record->BSSID.octet),sizeof(scl_mac_t)) == 0) {
return;
}
}

if (data->count > 0 && (data->aps != NULL)) {
// get ap stats
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 = scl_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_SCL_ACCESS_POINT) {
SclAccessPoint *aps_sres = static_cast<SclAccessPoint *>(data->aps);
aps_sres[data->offset] = std::move(SclAccessPoint(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 SclSTAInterface::internal_scan(WiFiAccessPoint *aps, unsigned count, scan_result_type sres_type)
{
ScopedMutexLock lock(_iface_shared.mutex);
scl_result_t scl_res;
int res;

// initialize wifi, this is noop if already init
if (!_scl_emac.powered_up) {
if(!_scl_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<scl_scan_result_t>();

scl_res = (scl_result_t)scl_wifi_scan(SCL_SCAN_TYPE_ACTIVE, SCL_BSS_TYPE_ANY,
NULL, NULL, NULL, NULL, scl_scan_handler, &internal_scan_result, &interal_scan_data);
if (scl_res != SCL_SUCCESS) {
res = scl_toerror(scl_res);
} else {
/* This semaphore will be released in scan callback once the scan is completed */
interal_scan_data.sema->acquire();
res = interal_scan_data.offset;
}
delete interal_scan_data.sema;
delete interal_scan_data.result_buff;
return res;
}

int SclSTAInterface::scan(WiFiAccessPoint *aps, unsigned count)
{
/* To Do */
return NSAPI_ERROR_UNSUPPORTED;
return internal_scan(aps, count, SRES_TYPE_WIFI_ACCESS_POINT);
}

int8_t SclSTAInterface::get_rssi()
Expand Down
Expand Up @@ -29,6 +29,8 @@
#include "scl_emac.h"
#include "scl_wifi_api.h"
#include "scl_types.h"
#include "SclAccessPoint.h"
#include "scl_interface.h"
#define MAX_SSID_LENGTH (33) /**< Maximum ssid length */
#define MAX_PASSWORD_LENGTH (64) /**< Maximum password length */

Expand All @@ -40,7 +42,8 @@ class SclSTAInterface : public WiFiInterface, public EMACInterface {

SclSTAInterface(
SCL_EMAC &emac = SCL_EMAC::get_instance(),
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance(),
scl_interface_shared_info_t &shared = scl_iface_shared);

/** Gets the current instance of the SclSTAInterface
*
Expand Down Expand Up @@ -127,11 +130,16 @@ class SclSTAInterface : public WiFiInterface, public EMACInterface {
*/
int8_t get_rssi();

/** Scans for available networks - NOT SUPPORTED
/** Scan for available networks in WiFiAccessPoint format
*
* @return NSAPI_ERROR_UNSUPPORTED
* This function will block.
*
* @param aps 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
* @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);
int scan(WiFiAccessPoint *aps, unsigned count);

/** This function is used to indicate if the device is connected to the network.
*
Expand All @@ -154,12 +162,15 @@ class SclSTAInterface : public WiFiInterface, public EMACInterface {
* @return SCL_SUCCESS if the Wi-Fi interface is set up successfully.
*/
int wifi_set_up(void);
protected:
int internal_scan(WiFiAccessPoint *aps, unsigned count, scan_result_type sres_type);

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 */
scl_interface_shared_info_t &_iface_shared;
};
#endif /* ifndef SCL_STA_INTERFACE_H */