2727#include " scl_emac.h"
2828#include " scl_ipc.h"
2929#include " mbed_wait_api.h"
30-
31-
30+ # include " SclAccessPoint.h "
31+ # include " scl_buffer_api.h "
3232/* * @file
3333 * Provides SCL interface functions to be used with WiFiInterface or NetworkInterface Objects
3434 */
@@ -43,8 +43,31 @@ struct scl_tx_net_credentials {
4343 const char *network_passphrase;
4444} scl_tx_network_credentials;
4545
46+
47+ struct scl_scan_userdata {
48+ rtos::Semaphore *sema;
49+ scan_result_type sres_type;
50+ WiFiAccessPoint *aps;
51+ std::vector<scl_scan_result_t > *result_buff;
52+ unsigned count;
53+ unsigned offset;
54+ bool scan_in_progress;
55+ };
56+
57+ static scl_scan_userdata interal_scan_data;
58+ static scl_scan_result_t internal_scan_result;
4659network_params_t network_parameter;
4760
61+ /* Internal scan callback that handles the scan results */
62+ void scl_scan_handler (scl_scan_result_t *result_ptr,void *user_data, scl_scan_status_t status);
63+
64+ #define CMP_MAC ( a, b ) (((((unsigned char *)a)[0 ])==(((unsigned char *)b)[0 ]))&& \
65+ ((((unsigned char *)a)[1 ])==(((unsigned char *)b)[1 ]))&& \
66+ ((((unsigned char *)a)[2 ])==(((unsigned char *)b)[2 ]))&& \
67+ ((((unsigned char *)a)[3 ])==(((unsigned char *)b)[3 ]))&& \
68+ ((((unsigned char *)a)[4 ])==(((unsigned char *)b)[4 ]))&& \
69+ ((((unsigned char *)a)[5 ])==(((unsigned char *)b)[5 ])))
70+
4871int scl_toerror (scl_result_t res)
4972{
5073 switch (res) {
@@ -93,14 +116,22 @@ nsapi_security_t scl_tosecurity(scl_security_t sec)
93116 case SCL_SECURITY_WEP_SHARED:
94117 return NSAPI_SECURITY_WEP;
95118 case SCL_SECURITY_WPA_TKIP_PSK:
119+ case SCL_SECURITY_WPA_AES_PSK:
96120 case SCL_SECURITY_WPA_TKIP_ENT:
121+ case SCL_SECURITY_WPA_AES_ENT:
122+ case SCL_SECURITY_WPA_MIXED_ENT:
97123 return NSAPI_SECURITY_WPA;
98124 case SCL_SECURITY_WPA2_MIXED_PSK:
125+ case SCL_SECURITY_WPA2_WPA_PSK:
126+ case SCL_SECURITY_WPA2_WPA_TKIP_PSK:
99127 return NSAPI_SECURITY_WPA_WPA2;
128+ case SCL_SECURITY_WPA2_MIXED_ENT:
129+ return NSAPI_SECURITY_WPA2_ENT;
100130 case SCL_SECURITY_WPA2_AES_PSK:
101131 case SCL_SECURITY_WPA2_AES_ENT:
102132 case SCL_SECURITY_WPA2_FBT_PSK:
103133 case SCL_SECURITY_WPA2_FBT_ENT:
134+ case SCL_SECURITY_WPA2_TKIP_ENT:
104135 return NSAPI_SECURITY_WPA2;
105136 default :
106137 return NSAPI_SECURITY_UNKNOWN;
@@ -125,12 +156,13 @@ scl_security_t scl_fromsecurity(nsapi_security_t sec)
125156 }
126157}
127158
128- SclSTAInterface::SclSTAInterface (SCL_EMAC &emac, OnboardNetworkStack &stack)
159+ SclSTAInterface::SclSTAInterface (SCL_EMAC &emac, OnboardNetworkStack &stack, scl_interface_shared_info_t &shared )
129160 : EMACInterface(emac, stack),
130161 _ssid(" \0 " ),
131162 _pass(" \0 " ),
132163 _security(NSAPI_SECURITY_NONE),
133- _scl_emac(emac)
164+ _scl_emac(emac),
165+ _iface_shared(shared)
134166{
135167}
136168
@@ -180,7 +212,7 @@ nsapi_error_t SclSTAInterface::connect()
180212 uint32_t connection_status = 0 ;
181213
182214 scl_tx_network_credentials.network_ssid = _ssid;
183- if ((strlen (_ssid) < MAX_SSID_LENGTH) && (strlen (_ssid) > MIN_SSID_LENGTH)) {
215+ if ((strlen (_ssid) < MAX_SSID_LENGTH) && (strlen (_ssid) > MIN_SSID_LENGTH) ) {
184216 scl_tx_network_credentials.ssid_len = strlen (_ssid);
185217 } else {
186218 return NSAPI_ERROR_PARAMETER;
@@ -288,10 +320,106 @@ nsapi_error_t SclSTAInterface::disconnect()
288320 return NSAPI_ERROR_OK;
289321}
290322
291- int SclSTAInterface::scan (WiFiAccessPoint *res, unsigned count)
323+ void scl_scan_handler (scl_scan_result_t *result_ptr,
324+ void *user_data, scl_scan_status_t status)
325+ {
326+ scl_scan_userdata *data = (scl_scan_userdata *)&interal_scan_data;
327+ scl_scan_result_t *record = result_ptr;
328+ unsigned int i;
329+ nsapi_wifi_ap ap;
330+ uint8_t length;
331+
332+ /* Even after stopping scan, some results will still come as results are already present in the queue */
333+ if (data->scan_in_progress == false ) {
334+ return ;
335+ }
336+
337+ // finished scan, either succesfully or through an abort
338+ if (status != SCL_SCAN_INCOMPLETE) {
339+ data->scan_in_progress = false ;
340+ data->sema ->release ();
341+ return ;
342+ }
343+
344+ // can't really keep anymore scan results
345+ if (data->count > 0 && data->offset >= data->count ) {
346+ /* We can not abort the scan as this function is getting executed in SCL context,
347+ Note that to call any SCL API, caller function should not in SCL context */
348+ return ;
349+ }
350+
351+ for (i = 0 ; i < data->result_buff ->size (); i++) {
352+ if (memcmp (((*data->result_buff )[i].BSSID .octet ),(record->BSSID .octet ),sizeof (scl_mac_t )) == 0 ) {
353+ return ;
354+ }
355+ }
356+
357+ if (data->count > 0 && (data->aps != NULL )) {
358+ // get ap stats
359+ length = record->SSID .length ;
360+ if (length < (sizeof (ap.ssid ) - 1 )) {
361+ length = sizeof (ap.ssid ) - 1 ;
362+ }
363+ memcpy (ap.ssid , record->SSID .value , length);
364+ ap.ssid [length] = ' \0 ' ;
365+
366+ memcpy (ap.bssid , record->BSSID .octet , sizeof (ap.bssid ));
367+
368+ ap.security = scl_tosecurity (record->security );
369+ ap.rssi = record->signal_strength ;
370+ ap.channel = record->channel ;
371+ if (data->sres_type == SRES_TYPE_WIFI_ACCESS_POINT) {
372+ data->aps [data->offset ] = WiFiAccessPoint (ap);
373+ } else if (data->sres_type == SRES_TYPE_SCL_ACCESS_POINT) {
374+ SclAccessPoint *aps_sres = static_cast <SclAccessPoint *>(data->aps );
375+ aps_sres[data->offset ] = std::move (SclAccessPoint (ap, record->bss_type ,
376+ record->ie_ptr , record->ie_len ));
377+ }
378+ }
379+
380+ // store to result_buff for future duplication removal
381+ data->result_buff ->push_back (*record);
382+ data->offset = data->result_buff ->size ();
383+ }
384+
385+ int SclSTAInterface::internal_scan (WiFiAccessPoint *aps, unsigned count, scan_result_type sres_type)
386+ {
387+ ScopedMutexLock lock (_iface_shared.mutex );
388+ scl_result_t scl_res;
389+ int res;
390+
391+ // initialize wifi, this is noop if already init
392+ if (!_scl_emac.powered_up ) {
393+ if (!_scl_emac.power_up ()) {
394+ return NSAPI_ERROR_DEVICE_ERROR;
395+ }
396+ }
397+
398+ interal_scan_data.sema = new Semaphore ();
399+ interal_scan_data.sres_type = sres_type;
400+ interal_scan_data.aps = aps;
401+ interal_scan_data.count = count;
402+ interal_scan_data.offset = 0 ;
403+ interal_scan_data.scan_in_progress = true ;
404+ interal_scan_data.result_buff = new std::vector<scl_scan_result_t >();
405+
406+ scl_res = (scl_result_t )scl_wifi_scan (SCL_SCAN_TYPE_ACTIVE, SCL_BSS_TYPE_ANY,
407+ NULL , NULL , NULL , NULL , scl_scan_handler, &internal_scan_result, &interal_scan_data);
408+ if (scl_res != SCL_SUCCESS) {
409+ res = scl_toerror (scl_res);
410+ } else {
411+ /* This semaphore will be released in scan callback once the scan is completed */
412+ interal_scan_data.sema ->acquire ();
413+ res = interal_scan_data.offset ;
414+ }
415+ delete interal_scan_data.sema ;
416+ delete interal_scan_data.result_buff ;
417+ return res;
418+ }
419+
420+ int SclSTAInterface::scan (WiFiAccessPoint *aps, unsigned count)
292421{
293- /* To Do */
294- return NSAPI_ERROR_UNSUPPORTED;
422+ return internal_scan (aps, count, SRES_TYPE_WIFI_ACCESS_POINT);
295423}
296424
297425int8_t SclSTAInterface::get_rssi ()
0 commit comments