DSWifi
Loading...
Searching...
No Matches
Internet mode guide

DSWifi supports Internet communications. However, DSi mode WiFi isn't supported yet, so it is restricted to open and WEP-encrypted networks. DSWifi includes the library sgIP to handle connections. It supports IPv4, as well as TCP and UDP.

1. Initialization

You can initialize the library in autoconnect mode or in manual connection mode. With autoconnect mode DSWifi will check the WiFi networks stored in the firmware of the console and it will try to connect to them:

bool Wifi_InitDefault(bool useFirmwareSettings)
Initializes WiFi library.
#define WFC_CONNECT
Init library and try to connect to firmware AP. Used by Wifi_InitDefault().
Definition dswifi9.h:52

However, this isn't very flexible. The function will simply wait until the connection works (or until the library times out). It will also make it impossible to switch between local multiplayer and Internet modes.

It's generally better to initialize the library in manual mode and to give the user the option to do different things. Initialize the library with this (it starts by default in Internet mode, not multiplayer mode):

#define INIT_ONLY
Init library only, don't try to connect to AP. Used by Wifi_InitDefault().
Definition dswifi9.h:54

Hardware timer 3 will be used by the WiFi library after this call.

1.1 Connect to WFC settings

You can start autoconnect mode by calling:

void Wifi_AutoConnect(void)
Connect to an Access Point specified by the WFC data in the firmware.

1.2 Manually look for access points

You can manually look for access points like this, for example:

// Set the library in scan mode
while (1)
{
swiWaitForVBlank();
// Get find out how many APs there are in the area
int count = Wifi_GetNumAP();
printf("Number of AP: %d\n", count);
printf("\n");
for (int i = 0; i < count; i++)
{
Wifi_GetAPData(i, &ap);
const char *security = "Open";
if (ap.flags & WFLAG_APDATA_WPA)
security = "WPA ";
else if (ap.flags & WFLAG_APDATA_WEP)
security = "WEP ";
// WPA isn't supported!
printf("[%.24s]\n", ap.ssid)
printf("%s | Channel %2d | RSSI %u\n", security, ap.channel, ap.rssi);
printf("\n");
}
}
int Wifi_GetNumAP(void)
Returns the current number of APs that are known and tracked internally.
int Wifi_GetAPData(int apnum, Wifi_AccessPoint *apdata)
Grabs data from internal structures for user code (always succeeds).
void Wifi_ScanMode(void)
Makes the ARM7 go into scan mode and list Internet APs.
Structure that defines how to connect to an access point.
Definition dswifi_common.h:197
u16 flags
Flags indicating various parameters for the AP. [not required, but the WFLAG_APDATA_ADHOC flag will b...
Definition dswifi_common.h:220
char ssid[33]
AP's SSID. Zero terminated is not necessary. If ssid[0] is zero, the SSID will be ignored in trying t...
Definition dswifi_common.h:200

After you have decided which AP to connect to:

// Setting everything to 0 will make DHCP determine the IP address. You can also
// set the settings manually if you want.
Wifi_SetIP(0, 0, 0, 0, 0);
// If the access point requires a WEP password, ask the user to provide it
if (AccessPoint.flags & WFLAG_APDATA_WEP)
{
// For 5 character long passwords, use WEPMODE_40BIT. For 13 character long
// passwords, use WEPMODE_128BIT.
int wepmode = WEPMODE_128BIT;
const char *password = "MyPassword123";
Wifi_ConnectAP(&AccessPoint, wepmode, 0, (u8 *)password);
}
else
{
Wifi_ConnectAP(&AccessPoint, WEPMODE_NONE, 0, 0);
}
@ WEPMODE_NONE
No WEP security is used.
Definition dswifi_common.h:79
@ WEPMODE_128BIT
13 ASCII characters.
Definition dswifi_common.h:81
int Wifi_ConnectAP(Wifi_AccessPoint *apdata, int wepmode, int wepkeyid, unsigned char *wepkey)
Connect to an Access Point.
void Wifi_SetIP(u32 IPaddr, u32 gateway, u32 subnetmask, u32 dns1, u32 dns2)
Set the DS's IP address and other IP configuration information.

Wifi_DisconnectAP() expects WEP keys as a string (usually called "ASCII" key). If you have an hexadecimal key you have to save it as a string of hexadecimal values. For example, 6162636465 needs to be passed to the function as { 0x61, 0x62, 0x63, 0x64, 0x65 }, not as "6162636465".

1.3 Wait until you connect to the AP

Regardless of which mode you used to start the connection to the AP, wait for the connection to be completed (or to fail!):

while (1)
{
swiWaitForVBlank();
int status = Wifi_AssocStatus();
{
// We can't connect to this host, try to connect to a different one!
}
if (status == ASSOCSTATUS_ASSOCIATED)
{
// Success!
}
}
int Wifi_AssocStatus(void)
Returns information about the status of connection to an AP.
@ ASSOCSTATUS_CANNOTCONNECT
Error in connecting... (COMPLETE if Wifi_ConnectAP was called to start)
Definition dswifi9.h:222
@ ASSOCSTATUS_ASSOCIATED
Connected! (COMPLETE if Wifi_ConnectAP was called to start)
Definition dswifi9.h:220

1.4 Disconnect from the AP

Eventually, when you want to leave this AP:

int Wifi_DisconnectAP(void)
Disassociate from the Access Point.

If you are done using wireless mode you can disable it to save power by calling:

void Wifi_DisableWifi(void)
Instructs the ARM7 to disengage wireless and stop receiving or transmitting.

2. Get connection settings

You can get information like the IP address like this:

struct in_addr ip, gateway, mask, dns1, dns2;
ip = Wifi_GetIPInfo(&gateway, &mask, &dns1, &dns2);
printf("\n");
printf("Connection information:\n");
printf("\n");
printf("IP: %s\n", inet_ntoa(ip));
printf("Gateway: %s\n", inet_ntoa(gateway));
printf("Mask: %s\n", inet_ntoa(mask));
printf("DNS1: %s\n", inet_ntoa(dns1));
printf("DNS2: %s\n", inet_ntoa(dns2));
printf("\n");
struct in_addr Wifi_GetIPInfo(struct in_addr *pGateway, struct in_addr *pSnmask, struct in_addr *pDns1, struct in_addr *pDns2)
Returns IP information.

3. Use libc to access the Internet

3.1 Resolve host IPs

You can obtain the IP of a host like this:

const char *url = "www.wikipedia.com";
struct hostent *host = gethostbyname(url);
if (host)
printf("IP: %s\n", inet_ntoa(*(struct in_addr *)host->h_addr_list[0]));
else
printf("Could not get IP\n");

3.2 Communicate using sockets

You can create TCP or UDP sockets with:

socket(AF_INET, SOCK_STREAM, 0); // TCP
socket(AF_INET, SOCK_DGRAM, 0); // UDP

Then, you can use connect(), send(), recv(), shutdown() and closesocket().

Note that, by default, sockets start in blocking mode. You can switch to non-blocking mode with:

int opt = 1;
int rc = ioctl(my_socket, FIONBIO, (char *)&opt);