SGDK
A free and open development kit for the Sega Mega Drive
Loading...
Searching...
No Matches
megawifi.h
Go to the documentation of this file.
1/************************************************************************/
26
27#ifndef _MEGAWIFI_H_
28#define _MEGAWIFI_H_
29
30#include "ext/mw/mw-msg.h"
31#include "ext/mw/lsd.h"
32#include "ext/mw/comm.h"
33
35#define MW_API_VERSION_MAJOR 1
36
38#define MW_API_VERSION_MINOR 5
39
41#define MW_COMMAND_TOUT_MS 2000
43#define MW_CONNECT_TOUT_MS 10000
45#define MW_HTTP_OPEN_TOUT_MS 10000
47#define MW_SCAN_TOUT_MS 10000
49#define MW_ASSOC_TOUT_MS 20000
51#define MW_ASSOC_WAIT_SLEEP_MS 5000
53#define MW_UPGRADE_TOUT_MS 600000
55#define MW_PING_TOUT_MS 30000
57#define MW_STAT_POLL_MS 250
58
59// Should consider if console is PAL or NTSC
60
61#define MW_COMMAND_TOUT MW_MS_TO_FRAMES(MW_COMMAND_TOUT_MS)
62#define MW_CONNECT_TOUT MW_MS_TO_FRAMES(MW_CONNECT_TOUT_MS)
63#define MW_SCAN_TOUT MW_MS_TO_FRAMES(MW_SCAN_TOUT_MS)
64#define MW_ASSOC_TOUT MW_MS_TO_FRAMES(MW_ASSOC_TOUT_MS)
65#define MW_ASSOC_WAIT_SLEEP MW_MS_TO_FRAMES(MW_ASSOC_WAIT_SLEEP_MS)
66#define MW_STAT_POLL_TOUT MW_MS_TO_FRAMES(MW_STAT_POLL_MS)
67#define MW_HTTP_OPEN_TOUT MW_MS_TO_FRAMES(MW_HTTP_OPEN_TOUT_MS)
68#define MW_UPGRADE_TOUT MW_MS_TO_FRAMES(MW_UPGRADE_TOUT_MS)
69
80
95
97#define MW_SSID_MAXLEN 32
99#define MW_PASS_MAXLEN 64
101#define MW_NTP_POOL_MAXLEN 80
103#define MW_NUM_CFG_SLOTS 3
105#define MW_NUM_DNS_SERVERS 2
107#define MW_FSM_QUEUE_LEN 8
109#define MW_MAX_SOCK 3
111#define MW_CTRL_CH 0
113#define MW_HTTP_CH LSD_MAX_CH - 1
114
118#define MW_CMD_MIN_BUFLEN 168
119
120struct mw_data {
121 mw_cmd *cmd __attribute__((unused));
122 uint16_t buf_len __attribute__((unused));
123 uint8_t mw_ready __attribute__((unused));
124};
125
129 uint8_t channel;
130 int8_t rssi;
131 uint8_t ssid_len;
132 char *ssid;
133};
134
141
142/************************************************************************/
151int16_t mw_init(uint16_t *cmd_buf, uint16_t buf_len);
152
153/************************************************************************/
161void mw_process(void);
162
163/************************************************************************/
173enum mw_err mw_detect(uint8_t *major, uint8_t *minor, char **variant);
174
175/************************************************************************/
183enum mw_err mw_version_get(uint8_t version[3], char **variant);
184
185/************************************************************************/
192uint8_t *mw_bssid_get(enum mw_if_type interface_type);
193
194/************************************************************************/
203
204/************************************************************************/
219enum mw_err mw_ap_cfg_set(uint8_t slot, const char *ssid, const char *pass,
220 enum mw_phy_type phy_type);
221
222/************************************************************************/
236enum mw_err mw_ap_cfg_get(uint8_t slot, char **ssid, char **pass,
237 enum mw_phy_type *phy_type);
238
239/************************************************************************/
250enum mw_err mw_ip_cfg_set(uint8_t slot, const struct mw_ip_cfg *ip);
251
252/************************************************************************/
260enum mw_err mw_ip_cfg_get(uint8_t slot, struct mw_ip_cfg **ip);
261
262/************************************************************************/
279
280/************************************************************************/
286
287/************************************************************************/
293
294/************************************************************************/
302
303/************************************************************************/
314int16_t mw_ap_scan(enum mw_phy_type phy_type, char **ap_data, uint8_t *aps);
315
316/************************************************************************/
332int16_t mw_ap_fill_next(const char *ap_data, uint16_t pos,
333 struct mw_ap_data *apd, uint16_t data_len);
334
335/************************************************************************/
342enum mw_err mw_ap_assoc(uint8_t slot);
343
344/************************************************************************/
354enum mw_err mw_ap_assoc_wait(int16_t tout_frames);
355
356/************************************************************************/
366enum mw_err mw_def_ap_cfg(uint8_t slot);
367
368/************************************************************************/
374
375/************************************************************************/
380int16_t mw_def_ap_cfg_get(void);
381
382/************************************************************************/
393enum mw_err mw_tcp_connect(uint8_t ch, const char *dst_addr,
394 const char *dst_port, const char *src_port);
395
396/************************************************************************/
406enum mw_err mw_close(uint8_t ch);
407
409#define mw_tcp_disconnect(ch) mw_close(ch)
410
411/************************************************************************/
423enum mw_err mw_udp_set(uint8_t ch, const char *dst_addr, const char *dst_port,
424 const char *src_port);
425
427#define mw_udp_unset(ch) mw_close(ch)
428
429/************************************************************************/
439enum mw_err mw_tcp_bind(uint8_t ch, uint16_t port);
440
441/************************************************************************/
451enum mw_err mw_sock_conn_wait(uint8_t ch, int16_t tout_frames);
452
453/************************************************************************/
463static inline enum lsd_status mw_recv(char *buf, int16_t len, void *ctx,
464 lsd_recv_cb recv_cb)
465{
466 return lsd_recv(buf, len, ctx, recv_cb);
467}
468
469/************************************************************************/
480static inline enum lsd_status mw_udp_reuse_recv(struct mw_reuse_payload *data,
481 int16_t len, void *ctx, lsd_recv_cb recv_cb)
482{
483 return lsd_recv((char*)data, len, ctx, recv_cb);
484}
485
486/************************************************************************/
498static inline enum lsd_status mw_udp_reuse_send(uint8_t ch,
499 const struct mw_reuse_payload *data, int16_t len, void *ctx,
500 lsd_send_cb send_cb)
501{
502 return lsd_send(ch, (const char*)data, len, ctx, send_cb);
503}
504
505/************************************************************************/
523static inline enum lsd_status mw_send(uint8_t ch, const char *data, int16_t len,
524 void *ctx, lsd_send_cb send_cb)
525{
526 return lsd_send(ch, data, len, ctx, send_cb);
527}
528
529/************************************************************************/
543enum mw_err mw_recv_sync(uint8_t *ch, char *buf, int16_t *buf_len,
544 int16_t tout_frames);
545
546/************************************************************************/
561enum mw_err mw_send_sync(uint8_t ch, const char *data, uint16_t len,
562 int16_t tout_frames);
563
564/************************************************************************/
570
571/************************************************************************/
579
580/************************************************************************/
592enum mw_err mw_sntp_cfg_set(const char *tz_str, const char *server[3]);
593
594/************************************************************************/
603enum mw_err mw_sntp_cfg_get(char **tz_str, char *server[3]);
604
605/************************************************************************/
615char *mw_date_time_get(uint32_t dt_bin[2]);
616
617/************************************************************************/
625enum mw_err mw_flash_id_get(uint8_t *man_id, uint16_t *dev_id);
626
627/************************************************************************/
635enum mw_err mw_flash_sector_erase(uint16_t sect);
636
637/************************************************************************/
646enum mw_err mw_flash_write(uint32_t addr, uint8_t *data, uint16_t data_len);
647
648/************************************************************************/
656uint8_t *mw_flash_read(uint32_t addr, uint16_t data_len);
657
658/************************************************************************/
669enum mw_err mw_gamertag_set(uint8_t slot, const struct mw_gamertag *gamertag);
670
671/************************************************************************/
678struct mw_gamertag *mw_gamertag_get(uint8_t slot);
679
680/************************************************************************/
687enum mw_err mw_log(const char *msg);
688
689/************************************************************************/
696
697/************************************************************************/
703void mw_power_off(void);
704
705/************************************************************************/
710void mw_sleep(int16_t frames);
711
712/************************************************************************/
719enum mw_err mw_http_url_set(const char *url);
720
721/************************************************************************/
729
730/************************************************************************/
738enum mw_err mw_http_header_add(const char *key, const char *value);
739
740/************************************************************************/
747enum mw_err mw_http_header_del(const char *key);
748
749/************************************************************************/
761enum mw_err mw_http_open(uint32_t content_len);
762
763/************************************************************************/
781int16_t mw_http_finish(uint32_t *content_len, int16_t tout_frames);
782
783/************************************************************************/
789uint32_t mw_http_cert_query(void);
790
791/************************************************************************/
808enum mw_err mw_http_cert_set(uint32_t cert_hash, const char *cert,
809 uint16_t cert_len);
810
811/************************************************************************/
816int16_t mw_http_cleanup(void);
817
818/************************************************************************/
824
825/************************************************************************/
835enum mw_err mw_def_server_set(const char *server_url);
836
837/************************************************************************/
845uint8_t *mw_hrng_get(uint16_t rnd_len);
846
847/************************************************************************/
859enum mw_err mw_ga_endpoint_set(const char *endpoint, const char *priv_key);
860
861/************************************************************************/
879enum mw_err mw_ga_key_value_add(const char **key, const char **value,
880 uint16_t num_pairs);
881
882/************************************************************************/
912int16_t mw_ga_request(enum mw_http_method method, const char **path,
913 uint8_t num_paths, const char **key, const char **value,
914 uint8_t num_kv_pairs, uint32_t *content_len,
915 int16_t tout_frames);
916
917/************************************************************************/
929enum mw_err mw_fw_list_upgrades(uint8_t page, uint8_t size, uint8_t offset, char **listUpgrades, uint8_t *len, uint8_t *total);
930
931/************************************************************************/
939enum mw_err mw_fw_upgrade(const char *name);
940
941/************************************************************************/
951struct mw_ping_response *mw_ping(const char* domain, u8 retries);
952
953/****** THE FOLLOWING COMMANDS ARE LOWER LEVEL AND USUALLY NOT NEEDED ******/
954
955/************************************************************************/
964static inline enum lsd_status mw_cmd_send(mw_cmd *cmd, void *ctx,
965 lsd_send_cb send_cb)
966{
967 // Send data on control channel (0).
968 return lsd_send(MW_CTRL_CH, cmd->packet, cmd->data_len + 4,
969 ctx, send_cb);
970}
971
972/************************************************************************/
981static inline enum lsd_status mw_cmd_recv(mw_cmd *rep, void *ctx,
982 lsd_recv_cb recv_cb) {
983 return lsd_recv(rep->packet, sizeof(mw_cmd), ctx, recv_cb);
984}
985
986struct mw_data* mw_get_data(void);
987
988uint16_t concat_strings(const char **str, uint8_t num_strs,
989 char *output, uint16_t max_len);
990
991uint16_t concat_kv_pairs(const char **key, const char **value,
992 uint8_t num_pairs, char *output, uint16_t max_len);
993
994int16_t fill_addr(const char *dst_addr, const char *dst_port,
995 const char *src_port, struct mw_msg_in_addr *in_addr);
996
997#endif /*_MEGAWIFI_H_*/
998
1000
lsd_status
Return status codes for LSD functions.
Definition lsd.h:65
void(* lsd_send_cb)(enum lsd_status stat, void *ctx)
Callback for the asynchronous lsd_send() function.
Definition lsd.h:76
enum lsd_status lsd_send(uint8_t ch, const char *data, int16_t len, void *ctx, lsd_send_cb send_cb)
Asynchronously sends data through a previously enabled channel.
enum lsd_status lsd_recv(char *buf, int16_t len, void *ctx, lsd_recv_cb recv_cb)
Asyncrhonously Receives a frame using LSD protocol.
void(* lsd_recv_cb)(enum lsd_status stat, uint8_t ch, char *data, uint16_t len, void *ctx)
Callback for the asynchronous lsd_recv() function.
Definition lsd.h:78
enum mw_err mw_wifi_adv_cfg_set(const struct mw_wifi_adv_cfg *wifi)
Set advanced WiFi configuration.
struct mw_gamertag * mw_gamertag_get(uint8_t slot)
Get gamertag information for one slot.
void mw_process(void)
Processes sends/receives pending data.
enum mw_err mw_ip_cfg_set(uint8_t slot, const struct mw_ip_cfg *ip)
Set IPv4 configuration.
enum mw_sock_stat mw_sock_stat_get(uint8_t ch)
Get socket status.
char * mw_def_server_get(void)
Get the default server used for MegaWiFi connections.
int16_t mw_def_ap_cfg_get(void)
Gets default AP/IP configuration slot.
enum mw_err mw_sock_conn_wait(uint8_t ch, int16_t tout_frames)
Polls a socket until it is ready to transfer data. Typical use of this function is after a successful...
enum mw_err mw_ap_assoc(uint8_t slot)
Tries associating to an AP. If successful, also configures IPv4.
enum mw_err mw_close(uint8_t ch)
Closes and disconnects a socket from specified channel.
#define MW_CTRL_CH
Control channel used for LSD protocol.
Definition megawifi.h:111
enum mw_err mw_def_server_set(const char *server_url)
Set the default server used for MegaWiFi connections.
enum mw_err mw_sntp_cfg_get(char **tz_str, char *server[3])
Get SNTP parameters and timezone configuration.
union mw_msg_sys_stat * mw_sys_stat_get(void)
Get system status.
enum mw_err mw_flash_sector_erase(uint16_t sect)
Erase a 4 KiB Flash sector. Every byte of an erased sector will be read as 0xFF.
enum mw_err mw_ap_cfg_set(uint8_t slot, const char *ssid, const char *pass, enum mw_phy_type phy_type)
Set access point configuration (SSID and password).
enum mw_err mw_ap_cfg_get(uint8_t slot, char **ssid, char **pass, enum mw_phy_type *phy_type)
Gets access point configuration (SSID and password).
int16_t mw_ap_scan(enum mw_phy_type phy_type, char **ap_data, uint8_t *aps)
Scan for access points.
enum mw_err mw_http_cert_set(uint32_t cert_hash, const char *cert, uint16_t cert_len)
Set the PEM certificate to use on HTTPS requests.
enum mw_err mw_ap_assoc_wait(int16_t tout_frames)
Polls the module status until it reports device is associated to AP or timeout occurs.
enum mw_err mw_detect(uint8_t *major, uint8_t *minor, char **variant)
Performs the startup sequence for the WiFi module, and tries detecting it by requesting the version d...
enum mw_err mw_http_header_add(const char *key, const char *value)
Add an HTTP header.
enum mw_err mw_http_method_set(enum mw_http_method method)
Set method for HTTP requests.
enum mw_err mw_def_ap_cfg(uint8_t slot)
Sets default AP/IP configuration.
enum mw_err mw_default_cfg_set(void)
Set default module configuration (AKA factory settings).
int16_t mw_http_cleanup(void)
Clean-up an HTTP request, freeing associated resources.
uint8_t * mw_bssid_get(enum mw_if_type interface_type)
Gets the module BSSID (the MAC address) for the specified interface.
uint32_t mw_http_cert_query(void)
Query the X.509 hash of the installed PEM certificate.
enum mw_err mw_fw_upgrade(const char *name)
Over-The-Air upgrade WiFi module firmware.
enum mw_err mw_send_sync(uint8_t ch, const char *data, uint16_t len, int16_t tout_frames)
Sends data through a socket, using a previously allocated channel. Synchronous interface.
uint8_t * mw_flash_read(uint32_t addr, uint16_t data_len)
Read data from specified flash address.
mw_err
Error codes for MegaWiFi API functions.
Definition megawifi.h:71
int16_t mw_http_finish(uint32_t *content_len, int16_t tout_frames)
Finish an opened HTTP request.
enum mw_err mw_http_open(uint32_t content_len)
Open HTTP connection.
enum mw_err mw_ip_cfg_get(uint8_t slot, struct mw_ip_cfg **ip)
Get IPv4 configuration.
int16_t mw_ga_request(enum mw_http_method method, const char **path, uint8_t num_paths, const char **key, const char **value, uint8_t num_kv_pairs, uint32_t *content_len, int16_t tout_frames)
Perform a GameAPI request, with the previously set endpoint and key/value pairs.
enum mw_err mw_tcp_bind(uint8_t ch, uint16_t port)
Binds a socket to a port, and listens to connections on the port. If a connection request is received...
enum mw_err mw_http_url_set(const char *url)
Set URL for HTTP requests.
enum mw_err mw_cfg_save(void)
Saves changed configuration parameters to non-volatile memory.
enum mw_err mw_fw_list_upgrades(uint8_t page, uint8_t size, uint8_t offset, char **listUpgrades, uint8_t *len, uint8_t *total)
List available upgrades WiFi module firmware.
enum mw_err mw_gamertag_set(uint8_t slot, const struct mw_gamertag *gamertag)
Set gamertag information for one slot.
enum mw_err mw_factory_settings(void)
Set factory default configuration.
enum mw_err mw_ga_key_value_add(const char **key, const char **value, uint16_t num_pairs)
Add parameters to the Game API request in key/value format.
enum mw_err mw_sntp_cfg_set(const char *tz_str, const char *server[3])
Configure SNTP parameters and timezone.
int16_t mw_init(uint16_t *cmd_buf, uint16_t buf_len)
Module initialization. Must be called once before using any other function. It also initializes de UA...
mw_if_type
Interface type for the mw_bssid_get() function.
Definition megawifi.h:136
mw_http_method
Supported HTTP methods.
Definition megawifi.h:82
void mw_power_off(void)
Powers off the WiFi module.
uint8_t * mw_hrng_get(uint16_t rnd_len)
Get random numbers.
enum mw_err mw_ip_current(struct mw_ip_cfg **ip)
Get current IP configuration, of the joined AP.
enum mw_err mw_flash_id_get(uint8_t *man_id, uint16_t *dev_id)
Get the identifiers of the flash chip in the WiFi module.
enum mw_err mw_udp_set(uint8_t ch, const char *dst_addr, const char *dst_port, const char *src_port)
Configures a UDP socket to send/receive data.
enum mw_err mw_tcp_connect(uint8_t ch, const char *dst_addr, const char *dst_port, const char *src_port)
Tries establishing a TCP connection with specified server.
enum mw_err mw_http_header_del(const char *key)
Delete a previously added HTTP header.
struct mw_ping_response * mw_ping(const char *domain, u8 retries)
Run ICMP requesto to domain.
enum mw_err mw_log(const char *msg)
Write a message to the WiFi module log trace.
void mw_sleep(int16_t frames)
Sleep the specified amount of frames.
int16_t mw_ap_fill_next(const char *ap_data, uint16_t pos, struct mw_ap_data *apd, uint16_t data_len)
Parses received AP data and fills information of the AP at "pos". Useful to extract AP information fr...
enum mw_err mw_ap_disassoc(void)
Dissasociates from a previously associated AP.
enum mw_err mw_ga_endpoint_set(const char *endpoint, const char *priv_key)
Set endpoint for Game API.
enum mw_err mw_flash_write(uint32_t addr, uint8_t *data, uint16_t data_len)
Write data to specified flash address.
enum mw_err mw_recv_sync(uint8_t *ch, char *buf, int16_t *buf_len, int16_t tout_frames)
Receive data, syncrhonous interface.
struct mw_wifi_adv_cfg * mw_wifi_adv_cfg_get(void)
Get advanced WiFi configuration.
char * mw_date_time_get(uint32_t dt_bin[2])
Get date and time.
enum mw_err mw_version_get(uint8_t version[3], char **variant)
Obtain module version numbers and string.
@ MW_ERR_NONE
No error (success).
Definition megawifi.h:72
@ MW_ERR_NOT_READY
Not ready to run command.
Definition megawifi.h:74
@ MW_ERR_RECV
Error receiving data.
Definition megawifi.h:78
@ MW_ERR_BUFFER_TOO_SHORT
Command buffer is too small.
Definition megawifi.h:75
@ MW_ERR
General error.
Definition megawifi.h:73
@ MW_ERR_SEND
Error sending data.
Definition megawifi.h:77
@ MW_ERR_PARAM
Input parameter out of range.
Definition megawifi.h:76
@ MW_IF_SOFTAP
Access Point interface.
Definition megawifi.h:138
@ MW_IF_STATION
Station interface.
Definition megawifi.h:137
@ MW_IF_MAX
Number of supported interface types.
Definition megawifi.h:139
@ MW_HTTP_METHOD_NOTIFY
HTTP NOTIFY Method.
Definition megawifi.h:89
@ MW_HTTP_METHOD_GET
HTTP GET Method.
Definition megawifi.h:83
@ MW_HTTP_METHOD_UNSUBSCRIBE
HTTP UNSUBSCRIBE.
Definition megawifi.h:91
@ MW_HTTP_METHOD_SUBSCRIBE
HTTP SUBSCRIBE Method.
Definition megawifi.h:90
@ MW_HTTP_METHOD_OPTIONS
HTTP OPTIONS.
Definition megawifi.h:92
@ MW_HTTP_METHOD_POST
HTTP POST Method.
Definition megawifi.h:84
@ MW_HTTP_METHOD_PUT
HTTP PUT Method.
Definition megawifi.h:85
@ MW_HTTP_METHOD_PATCH
HTTP PATCH Method.
Definition megawifi.h:86
@ MW_HTTP_METHOD_HEAD
HTTP HEAD Method.
Definition megawifi.h:88
@ MW_HTTP_METHOD_DELETE
HTTP DELETE Method.
Definition megawifi.h:87
mw_sock_stat
Socket status.
Definition mw-msg.h:277
enum PACKED mw_phy_type
WiFi PHY configuration for the connection to the AP.
Definition mw-msg.h:138
enum PACKED mw_security
Supported security protocols.
Definition mw-msg.h:122
MegaWiFi command message definitions.
Access Point data.
Definition megawifi.h:127
uint8_t channel
WiFi channel.
Definition megawifi.h:129
enum mw_security auth
Security type.
Definition megawifi.h:128
char * ssid
SSID string (not NULL terminated).
Definition megawifi.h:132
uint8_t ssid_len
Length of ssid field.
Definition megawifi.h:131
int8_t rssi
Signal strength.
Definition megawifi.h:130
Definition megawifi.h:120
Gamertag data.
Definition mw-msg.h:241
IP configuration parameters.
Definition mw-msg.h:160
TCP/UDP address message.
Definition mw-msg.h:151
Definition mw-msg.h:327
Payload with remote IP and port.
Definition mw-msg.h:385
Advanced WiFi configuration.
Definition mw-msg.h:224
unsigned char u8
Definition types.h:95
Command sent to system FSM.
Definition mw-msg.h:340
uint16_t data_len
Data length.
Definition mw-msg.h:344
char packet[MW_CMD_MAX_BUFLEN+2 *sizeof(uint16_t)]
Packet raw data.
Definition mw-msg.h:341
System status.
Definition mw-msg.h:285