Skip to content

Commit 84bac28

Browse files
vmedcyArto Kinnunen
authored andcommitted
PSOC6: add WHD EMAC driver
1 parent 7a7873c commit 84bac28

File tree

9 files changed

+1650
-0
lines changed

9 files changed

+1650
-0
lines changed

features/netsocket/emac-drivers/TARGET_WHD/interface/WhdSTAInterface.cpp

Lines changed: 478 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/* WHD implementation of NetworkInterfaceAPI
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef WHD_STA_INTERFACE_H
18+
#define WHD_STA_INTERFACE_H
19+
20+
#include "mbed.h"
21+
#include "EthernetInterface.h"
22+
#include "netsocket/OnboardNetworkStack.h"
23+
#include "whd_emac.h"
24+
#include "whd_types_int.h"
25+
26+
struct ol_desc;
27+
28+
/** WhdSTAInterface class
29+
* Implementation of the NetworkStack for the WHD
30+
*/
31+
class WhdSTAInterface : public WiFiInterface, public EMACInterface
32+
{
33+
public:
34+
35+
class OlmInterface
36+
{
37+
public:
38+
/** Get the default OLM interface. */
39+
static OlmInterface &get_default_instance();
40+
41+
OlmInterface(struct ol_desc *list = NULL) {}
42+
43+
virtual int init_ols(void *whd, void *ip) { return 0; }
44+
virtual int sleep() { return 0; }
45+
virtual int wake() { return 0; }
46+
47+
virtual void deinit_ols(void) {}
48+
};
49+
50+
WhdSTAInterface(
51+
WHD_EMAC &emac = WHD_EMAC::get_instance(),
52+
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance(),
53+
OlmInterface &olm = OlmInterface::get_default_instance());
54+
55+
static WhdSTAInterface *get_default_instance();
56+
57+
/* Turn on the wifi device*/
58+
void wifi_on();
59+
60+
/** Start the interface
61+
*
62+
* Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
63+
* If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
64+
*
65+
* @return 0 on success, negative error code on failure
66+
*/
67+
nsapi_error_t connect();
68+
69+
/** Start the interface
70+
*
71+
* Attempts to connect to a WiFi network.
72+
*
73+
* @param ssid Name of the network to connect to
74+
* @param pass Security passphrase to connect to the network
75+
* @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
76+
* @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
77+
* @return 0 on success, or error code on failure
78+
*/
79+
nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, uint8_t channel = 0);
80+
81+
/** Stop the interface
82+
* @return 0 on success, negative on failure
83+
*/
84+
nsapi_error_t disconnect();
85+
86+
/** Set the WiFi network credentials
87+
*
88+
* @param ssid Name of the network to connect to
89+
* @param pass Security passphrase to connect to the network
90+
* @param security Type of encryption for connection
91+
* (defaults to NSAPI_SECURITY_NONE)
92+
* @return 0 on success, or error code on failure
93+
*/
94+
nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
95+
96+
/** Set the WiFi network channel - NOT SUPPORTED
97+
*
98+
* This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
99+
*
100+
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
101+
* @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
102+
*/
103+
nsapi_error_t set_channel(uint8_t channel) {
104+
if (channel != 0) {
105+
return NSAPI_ERROR_UNSUPPORTED;
106+
}
107+
108+
return 0;
109+
}
110+
111+
/** Gets the current radio signal strength for active connection
112+
*
113+
* @return Connection strength in dBm (negative value)
114+
*/
115+
int8_t get_rssi();
116+
117+
/** Scan for available networks
118+
*
119+
* This function will block.
120+
*
121+
* @param ap Pointer to allocated array to store discovered AP
122+
* @param count Size of allocated @a res array, or 0 to only count available AP
123+
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
124+
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
125+
* see @a nsapi_error
126+
*/
127+
int scan(WiFiAccessPoint *res, unsigned count);
128+
129+
/* is interface connected, if yes return WICED_SUCCESS else WICED_NOT_CONNECTED */
130+
int is_interface_connected();
131+
132+
/* get bssid of the AP if success return WICED_SUCCESS else WICED_ERROR */
133+
int get_bssid(uint8_t *bssid);
134+
135+
/* print WHD log (this routine will malloc/free a buffer
136+
* You need to enable printing with WHD_LOGGING_BUFFER_ENABLE
137+
*/
138+
int whd_log_print ( void );
139+
140+
/* read WHD log */
141+
int whd_log_read ( char *buffer, int buffer_size );
142+
143+
/* Get EDCF AC params */
144+
nsapi_error_t wifi_get_ac_params_sta(void * ac_param );
145+
146+
/* get iovar value */
147+
int wifi_get_iovar_value ( const char *iovar, uint32_t *value );
148+
149+
/* set iovar value */
150+
int wifi_set_iovar_value ( const char *iovar, uint32_t value );
151+
152+
/* set ioctl value */
153+
int wifi_set_ioctl_value( uint32_t ioctl, uint32_t value ) ;
154+
155+
/* set wifi interface up */
156+
int wifi_set_up (void );
157+
158+
/* set wifi interface down */
159+
int wifi_set_down (void );
160+
161+
/** Set Offload Manager Information
162+
* NOTE: Only allowed while disconnected
163+
*
164+
* @param olm Offload Manager info structure
165+
* @return true if completed successfully
166+
* false if Interface is connected
167+
*/
168+
int set_olm(OlmInterface *olm) {
169+
if (get_connection_status() == NSAPI_STATUS_DISCONNECTED)
170+
{
171+
_olm = olm;
172+
return true;
173+
}
174+
return false;
175+
}
176+
177+
/** Network stack is suspended
178+
*
179+
* @return 0 if successful
180+
*/
181+
int net_suspended() {
182+
int ret = _olm->sleep();
183+
return ret;
184+
}
185+
186+
/** Network stack is resuming
187+
*
188+
* @return 0 if successful
189+
*/
190+
int net_resuming() {
191+
int ret = _olm->wake();
192+
return ret;
193+
}
194+
private:
195+
196+
char _ssid[33]; /* The longest possible name (defined in 802.11) +1 for the \0 */
197+
char _pass[64]; /* The longest allowed passphrase + 1 */
198+
nsapi_security_t _security;
199+
WHD_EMAC& _whd_emac;
200+
OlmInterface *_olm;
201+
};
202+
203+
extern int wiced_leave_ap ( int interface );
204+
#endif
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* Wiced implementation of NetworkInterfaceAPI
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "nsapi.h"
18+
#include "lwipopts.h"
19+
#include "WhdSoftAPInterface.h"
20+
#include "nsapi.h"
21+
#include "lwipopts.h"
22+
#include "lwip/etharp.h"
23+
#include "lwip/ethip6.h"
24+
#include "rtos.h"
25+
#include "whd_emac.h"
26+
#include "whd_wifi_api.h"
27+
28+
extern int whd_toerror(whd_result_t res);
29+
extern nsapi_security_t whd_tosecurity(whd_security_t sec);
30+
extern whd_security_t whd_fromsecurity(nsapi_security_t sec);
31+
extern "C" void whd_emac_wifi_link_state_changed(bool state_up, whd_interface_t ifp);
32+
33+
34+
static const whd_event_num_t ap_client_events[] = { 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 };
35+
static uint16_t ap_event_entry = 2;
36+
37+
WhdSoftAPInterface::WhdSoftAPInterface(WHD_EMAC &emac, OnboardNetworkStack &stack)
38+
: EMACInterface(emac, stack),
39+
_whd_emac(emac)
40+
{
41+
42+
}
43+
44+
45+
int WhdSoftAPInterface::start(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel,
46+
bool start_dhcp_server, const whd_custom_ie_info_t* ie_info)
47+
{
48+
nsapi_error_t err;
49+
50+
/* set up our interface */
51+
if (!_interface) {
52+
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
53+
if (err != NSAPI_ERROR_OK) {
54+
_interface = NULL;
55+
return err;
56+
}
57+
_interface->attach(_connection_status_cb);
58+
}
59+
60+
// setup ssid
61+
whd_ssid_t whd_ssid;
62+
strncpy((char*)whd_ssid.value, ssid, SSID_NAME_SIZE);
63+
whd_ssid.value[SSID_NAME_SIZE-1] = '\0';
64+
whd_ssid.length = strlen((char*)whd_ssid.value);
65+
66+
// choose network security
67+
whd_security_t whd_security = whd_fromsecurity(security);
68+
69+
/* set up the AP info */
70+
err = whd_wifi_init_ap(_whd_emac.ifp, &whd_ssid, whd_security, (const uint8_t*)pass,
71+
strlen(pass), channel);
72+
if (err != NSAPI_ERROR_OK) {
73+
printf("whd_wifi_init_ap() ERROR: %d\n", err);
74+
return err;
75+
}
76+
77+
if (ie_info) {
78+
err = whd_wifi_manage_custom_ie(_whd_emac.ifp, WHD_ADD_CUSTOM_IE, (const uint8_t *)ie_info->oui,
79+
ie_info->subtype, (const void *)ie_info->data, ie_info->length, ie_info->which_packets);
80+
if (err != NSAPI_ERROR_OK) {
81+
printf("whd_wifi_manage_custom_ie() ERROR: %d\n", err);
82+
return err;
83+
}
84+
}
85+
86+
err = whd_wifi_start_ap(_whd_emac.ifp);
87+
if (err != NSAPI_ERROR_OK) {
88+
printf("whd_wifi_start_ap() ERROR: %d\n", err);
89+
return err;
90+
}
91+
92+
// Set static IP address for SoftAP and bring up
93+
set_dhcp(false);
94+
95+
if(whd_wifi_is_ready_to_transceive(_whd_emac.ifp) == WHD_SUCCESS) {
96+
whd_emac_wifi_link_state_changed(true, _whd_emac.ifp);
97+
}
98+
99+
// bring up
100+
err = _interface->bringup(_dhcp,
101+
_ip_address[0] ? _ip_address : 0,
102+
_netmask[0] ? _netmask : 0,
103+
_gateway[0] ? _gateway : 0,
104+
DEFAULT_STACK);
105+
if (err != NSAPI_ERROR_OK) {
106+
printf("bringup() ERROR: %d\n", err);
107+
}
108+
return err;
109+
}
110+
111+
112+
113+
int WhdSoftAPInterface::stop(void)
114+
{
115+
return whd_wifi_stop_ap(_whd_emac.ifp);
116+
}
117+
118+
int WhdSoftAPInterface::remove_custom_ie(const whd_custom_ie_info_t* ie_info)
119+
{
120+
return whd_wifi_manage_custom_ie(_whd_emac.ifp, WHD_REMOVE_CUSTOM_IE, (const uint8_t *)ie_info->oui,
121+
ie_info->subtype, (const void *)ie_info->data, ie_info->length, ie_info->which_packets);
122+
}
123+
124+
int WhdSoftAPInterface::get_associated_client_list(void* client_list_buffer, uint16_t buffer_length)
125+
{
126+
127+
return whd_wifi_get_associated_client_list(_whd_emac.ifp, client_list_buffer, buffer_length);
128+
}
129+
130+
int WhdSoftAPInterface::register_event_handler(whd_event_handler_t softap_event_handler)
131+
{
132+
return whd_management_set_event_handler(_whd_emac.ifp, ap_client_events, softap_event_handler, NULL, &ap_event_entry);
133+
}
134+
135+
int WhdSoftAPInterface::unregister_event_handler(void)
136+
{
137+
return whd_wifi_deregister_event_handler(_whd_emac.ifp, ap_event_entry);
138+
}

0 commit comments

Comments
 (0)