Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
net_cc3220/CC32xxWiFi.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_NET_C32XX_CC32XXWIFI_HXX_
35#define _FREERTOS_DRIVERS_NET_C32XX_CC32XXWIFI_HXX_
36
37#include <functional>
38#include <vector>
39#include <string>
40
41#include "os/OS.hxx"
42#include "utils/Singleton.hxx"
44#include "freertos_drivers/common/WifiDefs.hxx"
45
46class CC32xxSocket;
47class NetworkSpace;
48
52{
53public:
62
63protected:
67 {
68 }
69
77 virtual void wlan_setup_ap(const char *ssid, const char *security_key,
78 SecurityType security_type) = 0;
79
84 virtual int wlan_profile_del(int index) = 0;
85};
86
91class CC32xxWiFi : public CC32xxWiFiInterface, public Singleton<CC32xxWiFi>
92{
93public:
94
96 static constexpr int PROFILE_DELETE_ALL = 0xFF;
97
100 static constexpr int NUM_PROFILES = 7;
101
103 static constexpr unsigned IPPROTO_TCP_TLS = 254;
104
106 static constexpr unsigned SO_SIMPLELINK_SD = 65537;
107
109 struct WlanEvent;
110
112 struct NetAppEvent;
113
115 struct SockEvent;
116
118 struct SockTriggerEvent;
119
121 struct HttpServerEvent;
122
124 struct HttpServerResponse;
125
127 struct NetAppRequest;
128
130 struct NetAppResponse;
131
133 struct FatalErrorEvent;
134
144
155
159 {
160 string ssid;
162 int rssi;
163 };
164
179 using PostFunction = std::function<void(uint16_t handle,
180 uint32_t content_length, const uint8_t *md, size_t md_len,
181 const uint8_t *payload, size_t payload_len, bool has_more)>;
182
185 CC32xxWiFi();
186
190 {
191 }
192
197 void test_mode_start();
198
203 void start(WlanRole role = WlanRole::STA,
205 WlanConnectionPolicy connection_policy = WLAN_CONNECTION_NO_CHANGE);
206
210 void stop();
211
216 {
217 return started;
218 }
219
226 WlanConnectResult wlan_connect(const char *ssid, const char *security_key,
227 SecurityType security_type);
228
230 void wlan_disconnect();
231
235
241 void wlan_setup_ap(const char *ssid, const char *security_key,
242 SecurityType security_type) override;
243
248 void wlan_get_ap_config(string *ssid, SecurityType *security_type);
249
255
259 {
260 return (connected || wlanRole == WlanRole::AP) && ipAcquired;
261 }
262
266 WlanRole wlan_role()
267 {
268 return wlanRole;
269 }
270
276 void wlan_set_role(WlanRole new_role);
277
280 {
281 if (!connected && wlanRole != WlanRole::AP)
282 {
283 if (securityFailure)
284 {
285 return WlanState::WRONG_PASSWORD;
286 }
287 return WlanState::NOT_ASSOCIATED;
288 }
289 if (!ipAcquired)
290 {
291 return WlanState::NO_IP;
292 }
293 return WlanState::OK;
294 }
295
296#ifdef GTEST
303 void TEST_set_state(
304 bool conn, bool has_ip, bool wrong_password, const string &ssid)
305 {
306 connected = conn ? 1 : 0;
307 ipAcquired = has_ip ? 1 : 0;
308 securityFailure = wrong_password ? 1 : 0;
309 strcpy(this->ssid, ssid.c_str());
310 }
311
315 void TEST_set_ip(uint32_t ip)
316 {
317 ipAddress = ip;
318 }
319#endif
320
324
328 CountryCode wlan_country_code_get();
329
336 int wlan_country_code_set(CountryCode cc, bool restart = false);
337
343 void wlan_set_scan_params(int mask, int min_rssi);
344
354 int wlan_profile_add(const char *ssid, SecurityType sec_type,
355 const char *key, unsigned priority);
356
361 int wlan_profile_del(const char *ssid);
362
367 int wlan_profile_del(int index) override;
368
376 int wlan_profile_get(int index, char ssid[], SecurityType *sec_type,
377 uint32_t *priority);
378
383
389
395
401
407 int wlan_network_list_get(WlanNetworkEntry *entries, size_t count);
408
411 void wlan_rescan();
412
416 void wlan_mac(uint8_t mac[6]);
417
425 void wlan_set_mac(uint8_t mac[6]);
426
430 uint32_t wlan_ip()
431 {
432 return ipAcquired ? ipAddress : 0;
433 }
434
440 const char *wlan_ssid()
441 {
442 ssid[32] = '\0';
443 return ssid;
444 }
445
450 {
451 // the RSSI value is only reliable when associatedd
452 return wlan_startup_state() == WlanState::NOT_ASSOCIATED ? 0 : rssi;
453 }
454
455 void set_ip_acquired_callback(std::function<void(bool)> callback)
456 {
457 ipAcquiredCallback_ = callback;
458 }
459
462 void run_on_network_thread(std::function<void()> callback);
463
506 const char *token_name, std::function<std::string()> callback)
507 {
508 OSMutexLock l(&lock_);
509 httpGetTokenCallbacks_.emplace_back(token_name, std::move(callback));
510 }
511
517 void add_http_post_callback(const char *uri, PostFunction callback)
518 {
519 OSMutexLock l(&lock_);
520 httpPostCallbacks_.emplace_back(uri, std::move(callback));
521 }
522
532 bool get_post_data(uint16_t handle, void *buf, size_t *len);
533
540 void send_post_respose(uint16_t handle, uint16_t http_status = 204,
541 const string &redirect = "");
542
547 void wlan_event_handler(WlanEvent *event);
548
554 void net_app_event_handler(NetAppEvent *event);
555
560 void sock_event_handler(SockEvent *event);
561
566 void trigger_event_handler(SockTriggerEvent *event);
567
573 void http_server_callback(HttpServerEvent *event,
574 HttpServerResponse *response);
575
582 NetAppRequest *request, NetAppResponse *response);
583
588 void fatal_error_callback(FatalErrorEvent* event);
589
592 static std::string get_version();
593
594private:
595 friend class ::NetworkSpace;
596
601 static uint8_t security_type_to_simplelink(SecurityType sec_type);
602
607 static SecurityType security_type_from_simplelink(uint8_t sec_type);
608
614 static SecurityType security_type_from_scan(unsigned sec_type);
615
618 void set_default_state();
619
623 static void* wlan_task_entry(void *context)
624 {
625 instance()->wlan_task();
626 return nullptr;
627 }
628
631 void wlan_task();
632
635 void select_wakeup();
636
641 void fd_remove(int16_t sd);
642
646 void fd_set_read(int16_t sd);
647
651 void fd_set_write(int16_t sd);
652
657 {
658 return ipv4_to_string(ipAddress);
659 }
660
661 uint32_t ipAddress;
662 char ssid[33];
665 std::function<void(bool)> ipAcquiredCallback_;
666
668 std::vector<std::function<void()> > callbacks_;
669
671 std::vector<std::pair<const char *, std::function<std::string()>>>
673
675 std::vector<std::pair<const char *, PostFunction>> httpPostCallbacks_;
676
679
680 int wakeup;
682 int16_t rssi;
684 WlanRole wlanRole;
688 unsigned started : 1;
689 unsigned connected : 1;
690 unsigned connectionFailed : 1;
691 unsigned ipAcquired : 1;
692 unsigned ipLeased : 1;
693 unsigned smartConfigStart : 1;
694 unsigned securityFailure : 1;
697 friend class CC32xxSocket;
698
700};
701
702#endif /* _FREERTOS_DRIVERS_NET_C32XX_CC32XXWIFI_HXX_ */
Socket implementation for the CC32xx class WiFi devices.
Interface that aids in unit testing.
@ SEC_WEP
WEP security mode.
@ SEC_OPEN
open (no security)
@ SEC_WPA2
WPA2 security mode.
virtual void wlan_setup_ap(const char *ssid, const char *security_key, SecurityType security_type)=0
Setup access point role credentials.
virtual int wlan_profile_del(int index)=0
Delete a saved WLAN profile.
virtual ~CC32xxWiFiInterface()
Destructor.
Provides the startup and mantainance methods for configuring and using the CC32xx Wi-Fi stack.
string http_get_ip_address()
Get the IP address for a http request.
bool wlan_profile_test_none()
Test if there are any saved profiles.
unsigned securityFailure
Disconnected due to wrong password.
void net_app_event_handler(NetAppEvent *event)
This function handles network events such as IP acquisition, IP leased, IP released etc.
void trigger_event_handler(SockTriggerEvent *event)
Notifies the service about a wifi asynchronous socket event callback.
int wlan_power_policy_set(WlanPowerPolicy wpp)
Set the power policy.
int wakeup
loopback socket to wakeup select()
void wlan_connection_policy_set(WlanConnectionPolicy policy)
Sets connection policy to auto connect.
std::vector< std::pair< const char *, PostFunction > > httpPostCallbacks_
List of callbacks for http post handlers.
void netapp_request_callback(NetAppRequest *request, NetAppResponse *response)
This function handles netapp request callbacks.
void stop()
Stops the Wi-Fi in preparation for a reboot.
void set_default_state()
Set the CC32xx to its default state, including station mode.
CC32xxWiFi()
Constructor.
bool is_started()
Get the started state of the network processor.
std::function< void(bool)> ipAcquiredCallback_
Callback for when IP is acquired.
void wlan_setup_ap(const char *ssid, const char *security_key, SecurityType security_type) override
Setup access point role credentials.
void wlan_set_scan_params(int mask, int min_rssi)
Sets the scan parameters.
void wlan_event_handler(WlanEvent *event)
This function handles WLAN events.
void wlan_get_ap_config(string *ssid, SecurityType *security_type)
Retrieve current AP config.
unsigned started
network processor started
void wlan_mac(uint8_t mac[6])
Get the device MAC address.
int16_t rssi
receive signal strength indicator
int wlan_get_ap_station_count()
Retrieves how many stations are connected to the wifi in AP mode.
void fatal_error_callback(FatalErrorEvent *event)
This Function Handles the Fatal errors.
void wlan_set_role(WlanRole new_role)
Change the default Wlan Role.
int wlan_profile_del(const char *ssid)
Delete a saved WLAN profile.
char ssid[33]
SSID of AP, or AP we are connected to.
unsigned smartConfigStart
Smart config in progress.
void add_http_post_callback(const char *uri, PostFunction callback)
Registers a handler for an HTTP POST operation.
void fd_remove(int16_t sd)
Remove a socket from the known sockets that might be part of the sl_Select fdset.
WlanRole wlanRole
the Wi-Fi role we are in
OSMutex lock_
Protects callbacks_ vector.
void connecting_update_blinker()
Updates the blinker based on connection state.
uint32_t ipAddress
assigned IP adress
void sock_event_handler(SockEvent *event)
This function handles socket events indication.
void run_on_network_thread(std::function< void()> callback)
Executes the given function on the network thread.
static constexpr int PROFILE_DELETE_ALL
the value passed to wlan_profile_del() to remove all profiles
unsigned connected
AP connected state.
WlanConnectionPolicy
The Wlan reconnect policy.
@ WLAN_CONNECTION_FAST_RECONNECT
Reconnect to the last connected AP.
@ WLAN_CONNECTION_SCAN
Scan for wifi networks and connect to the nearest that is stored in the profiles (by profile priority...
void fd_set_read(int16_t sd)
Add socket to the read fd set.
uint32_t wlan_ip()
Get the assigned IP address.
CountryCode wlan_country_code_get()
Get the current country code.
int wlan_rssi()
Get the receive signal strength indicator.
int wlan_profile_add(const char *ssid, SecurityType sec_type, const char *key, unsigned priority)
Add a saved WLAN profile.
static SecurityType security_type_from_simplelink(uint8_t sec_type)
Translates the SimpleLink code to SecurityType enum.
void start(WlanRole role=WlanRole::STA, WlanPowerPolicy power_policy=WLAN_NO_CHANGE_POLICY, WlanConnectionPolicy connection_policy=WLAN_CONNECTION_NO_CHANGE)
Startup the Wi-Fi.
std::vector< std::function< void()> > callbacks_
List of callbacks to execute on the network thread.
int wlan_profile_get(int index, char ssid[], SecurityType *sec_type, uint32_t *priority)
Get a saved WLAN profile by index.
std::function< void(uint16_t handle, uint32_t content_length, const uint8_t *md, size_t md_len, const uint8_t *payload, size_t payload_len, bool has_more)> PostFunction
This function type is used for POST callback operations to the application.
WlanConnectionPolicy connectionPolicy
scan or reconnect to last AP
int wlan_network_list_get(WlanNetworkEntry *entries, size_t count)
Get a list of available networks.
static uint8_t security_type_to_simplelink(SecurityType sec_type)
Translates the SecurityType enum to the internal SimpleLink code.
void fd_set_write(int16_t sd)
Add socket to the write fd set.
void wlan_task()
Thread that will manage the WLAN connection inside object context.
WlanRole wlan_role()
Get the current Wi-Fi role.
void http_server_callback(HttpServerEvent *event, HttpServerResponse *response)
This function handles http server callback indication.
void select_wakeup()
Asynchronously wakeup the select call.
const char * wlan_ssid()
Get the SSID of the access point we are connected to.
unsigned ipAcquired
IP address aquired state.
WlanPowerPolicy wlanPowerPolicy
the desired power policy
static constexpr unsigned IPPROTO_TCP_TLS
Pass this option as protocol to socket to create a secure socket.
static std::string get_version()
Returns a string contianing the version numbers of the network interface.
static constexpr int NUM_PROFILES
What is the maximum number of profiles in the CC32xx.
void wlan_rescan()
Initiate rescanning available networks.
static void * wlan_task_entry(void *context)
Thread that will manage the WLAN connection.
void test_mode_start()
Startup the Wi-Fi in test mode.
void wlan_wps_pbc_initiate()
Initiate a WPS Push Button Control connection.
WlanConnectResult wlan_connect(const char *ssid, const char *security_key, SecurityType security_type)
Connect to access point.
WlanPowerPolicy
The WLAN power policy.
@ WLAN_ALWAYS_ON_POLICY
WLAN power policy always on.
@ WLAN_NORMAL_POLICY
WLAN power policy normal.
@ WLAN_LOW_LATENCY_POLICY
WLAN power policy low latency.
@ WLAN_LOW_POWER_POLICY
WLAN power policy low power.
@ WLAN_NO_CHANGE_POLICY
WLAN power policy to be left alone.
void wlan_set_mac(uint8_t mac[6])
Sets the device MAC address.
void add_http_get_token_callback(const char *token_name, std::function< std::string()> callback)
Add an HTTP get token callback.
~CC32xxWiFi()
Destructor.
void send_post_respose(uint16_t handle, uint16_t http_status=204, const string &redirect="")
Sends a POST response.
int wlan_country_code_set(CountryCode cc, bool restart=false)
Set the current country code.
WlanState wlan_startup_state()
static constexpr unsigned SO_SIMPLELINK_SD
Retrieves the socket descriptor for setting TLS parameters.
static SecurityType security_type_from_scan(unsigned sec_type)
Translates the SimpleLink code from the network scan to SecurityType enum.
unsigned connectionFailed
Connection attempt failed status.
std::vector< std::pair< const char *, std::function< std::string()> > > httpGetTokenCallbacks_
List of callbacks for http get tokens.
unsigned ipLeased
IP address leased to a client(AP mode)
bool get_post_data(uint16_t handle, void *buf, size_t *len)
Retrieves additional payload for http POST operations.
void wlan_disconnect()
Disconnects from the current AP.
int wlan_power_policy_get(WlanPowerPolicy *wpp)
Get the power policy.
Class to allow convenient locking and unlocking of mutexes in a C context.
Definition OS.hxx:494
This class provides a mutex API.
Definition OS.hxx:427
Singleton class.
Definition Singleton.hxx:65
static CC32xxWiFi * instance()
Definition Singleton.hxx:77
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
metadata for a WLAN netowrk entry.
int rssi
receive signal strength indicator of the AP
SecurityType sec_type
security type of the AP