#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
#include <stdarg.h>
#include <unistd.h>
void print_constatus()
{
{
printf("udsGetConnectionStatus() returned 0x%08x.\n", (unsigned int)ret);
}
else
{
printf(
"constatus:\nstatus=0x%x\n", (
unsigned int)constatus.
status);
printf(
"1=0x%x\n", (
unsigned int)constatus.
unk_x4);
printf(
"cur_NetworkNodeID=0x%x\n", (
unsigned int)constatus.
cur_NetworkNodeID);
printf(
"unk_xa=0x%x\n", (
unsigned int)constatus.
unk_xa);
for(pos=0; pos<(0x20>>2); pos++)printf(
"%u=0x%x ", (
unsigned int)pos+3, (
unsigned int)constatus.
unk_xc[pos]);
printf(
"\ntotal_nodes=0x%x\n", (
unsigned int)constatus.
total_nodes);
printf(
"max_nodes=0x%x\n", (
unsigned int)constatus.
max_nodes);
printf("node_bitmask=0x%x\n", (unsigned int)constatus.total_nodes);
}
}
void uds_test()
{
size_t tmpbuf_size;
size_t total_networks = 0;
u32 wlancommID = 0x48425710;
char *passphrase = "udsdemo passphrase c186093cd2652741";
udsConnectionType conntype = UDSCONTYPE_Client;
u32 transfer_data, prev_transfer_data = 0;
size_t actual_size;
u8 appdata[0x14] = {0x69, 0x8a, 0x05, 0x5c};
char tmpstr[256];
strncpy((char*)&appdata[4], "Test appdata.", sizeof(appdata)-4);
printf("Successfully initialized.\n");
tmpbuf_size = 0x4000;
tmpbuf = malloc(tmpbuf_size);
if(tmpbuf==NULL)
{
printf("Failed to allocate tmpbuf for beacon data.\n");
return;
}
for(pos=0; pos<10; pos++)
{
total_networks = 0;
memset(tmpbuf, 0, sizeof(tmpbuf_size));
ret =
udsScanBeacons(tmpbuf, tmpbuf_size, &networks, &total_networks, wlancommID, 0, NULL,
false);
printf("udsScanBeacons() returned 0x%08x.\ntotal_networks=%u.\n", (unsigned int)ret, (unsigned int)total_networks);
if(total_networks)break;
}
free(tmpbuf);
tmpbuf = NULL;
if(total_networks)
{
network = &networks[0];
printf(
"network: total nodes = %u.\n", (
unsigned int)network->
network.
total_nodes);
{
memset(tmpstr, 0, sizeof(tmpstr));
{
printf("udsGetNodeInfoUsername() returned 0x%08x.\n", (unsigned int)ret);
free(networks);
return;
}
printf("node%u username: %s\n", (unsigned int)pos, tmpstr);
}
actual_size = 0;
if(
R_FAILED(ret) || actual_size!=
sizeof(out_appdata))
{
printf("udsGetNetworkStructApplicationData() returned 0x%08x. actual_size = 0x%x.\n", (unsigned int)ret, actual_size);
free(networks);
return;
}
memset(tmpstr, 0, sizeof(tmpstr));
if(memcmp(out_appdata, appdata, 4)!=0)
{
printf("The first 4-bytes of appdata is invalid.\n");
free(networks);
return;
}
strncpy(tmpstr, (char*)&out_appdata[4], sizeof(out_appdata)-5);
tmpstr[sizeof(out_appdata)-6]='\0';
printf("String from network appdata: %s\n", (char*)&out_appdata[4]);
{
conntype = UDSCONTYPE_Spectator;
printf("Connecting to the network as a spectator...\n");
}
else
{
printf("Connecting to the network as a client...\n");
}
for(pos=0; pos<10; pos++)
{
{
printf("udsConnectNetwork() returned 0x%08x.\n", (unsigned int)ret);
}
else
{
break;
}
}
free(networks);
if(pos==10)return;
printf("Connected.\n");
tmp = 0;
printf("udsGetChannel() returned 0x%08x. channel = %u.\n", (unsigned int)ret, (unsigned int)tmp);
{
return;
}
memset(out_appdata, 0, sizeof(out_appdata));
actual_size = 0;
if(
R_FAILED(ret) || actual_size!=
sizeof(out_appdata))
{
printf("udsGetApplicationData() returned 0x%08x. actual_size = 0x%x.\n", (unsigned int)ret, actual_size);
return;
}
memset(tmpstr, 0, sizeof(tmpstr));
if(memcmp(out_appdata, appdata, 4)!=0)
{
printf("The first 4-bytes of appdata is invalid.\n");
return;
}
strncpy(tmpstr, (char*)&out_appdata[4], sizeof(out_appdata)-5);
tmpstr[sizeof(out_appdata)-6]='\0';
printf("String from appdata: %s\n", (char*)&out_appdata[4]);
con_type = 1;
}
else
{
printf("Creating the network...\n");
ret =
udsCreateNetwork(&networkstruct, passphrase, strlen(passphrase)+1, &bindctx, data_channel, recv_buffer_size);
{
printf("udsCreateNetwork() returned 0x%08x.\n", (unsigned int)ret);
return;
}
{
printf("udsSetApplicationData() returned 0x%08x.\n", (unsigned int)ret);
return;
}
tmp = 0;
printf("udsGetChannel() returned 0x%08x. channel = %u.\n", (unsigned int)ret, (unsigned int)tmp);
{
return;
}
con_type = 0;
}
{
printf("Constatus event signaled.\n");
print_constatus();
}
printf("Press A to stop data transfer.\n");
tmpbuf = malloc(tmpbuf_size);
if(tmpbuf==NULL)
{
printf("Failed to allocate tmpbuf for receiving data.\n");
if(con_type)
{
}
else
{
}
return;
}
while(1)
{
prev_transfer_data = transfer_data;
if(transfer_data != prev_transfer_data && conntype!=UDSCONTYPE_Spectator)
{
{
printf("udsSendTo() returned 0x%08x.\n", (unsigned int)ret);
break;
}
}
{
memset(tmpbuf, 0, tmpbuf_size);
actual_size = 0;
src_NetworkNodeID = 0;
ret =
udsPullPacket(&bindctx, tmpbuf, tmpbuf_size, &actual_size, &src_NetworkNodeID);
{
printf("udsPullPacket() returned 0x%08x.\n", (unsigned int)ret);
break;
}
if(actual_size)
{
printf("Received 0x%08x size=0x%08x from node 0x%x.\n", (unsigned int)tmpbuf[0], actual_size, (unsigned int)src_NetworkNodeID);
}
}
{
{
printf("udsGetNodeInformation() returned 0x%08x.\n", (unsigned int)ret);
}
else
{
memset(tmpstr, 0, sizeof(tmpstr));
{
printf("udsGetNodeInfoUsername() returned 0x%08x for udsGetNodeInfoUsername.\n", (unsigned int)ret);
}
else
{
printf("node username: %s\n", tmpstr);
printf(
"node unk_x1c=0x%x\n", (
unsigned int)tmpnode.
unk_x1c);
printf(
"node flag=0x%x\n", (
unsigned int)tmpnode.
flag);
printf(
"node pad_x1f=0x%x\n", (
unsigned int)tmpnode.
pad_x1f);
printf(
"node NetworkNodeID=0x%x\n", (
unsigned int)tmpnode.
NetworkNodeID);
printf(
"node word_x24=0x%x\n", (
unsigned int)tmpnode.
word_x24);
}
}
}
{
printf("udsSetNewConnectionsBlocked() for enabling blocking returned 0x%08x.\n", (unsigned int)ret);
}
{
printf("udsSetNewConnectionsBlocked() for disabling blocking returned 0x%08x.\n", (unsigned int)ret);
}
{
printf("udsEjectSpectator() returned 0x%08x.\n", (unsigned int)ret);
}
{
printf("udsAllowSpectators() returned 0x%08x.\n", (unsigned int)ret);
}
{
printf("Constatus event signaled.\n");
print_constatus();
}
}
free(tmpbuf);
tmpbuf = NULL;
if(con_type)
{
}
else
{
}
}
int main()
{
printf("libctru UDS local-WLAN demo.\n");
{
printf("udsInit failed: 0x%08x.\n", (unsigned int)ret);
}
else
{
uds_test();
}
printf("Press START to exit.\n");
{
break;
}
return 0;
}
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
void gfxSwapBuffers(void)
Updates the configuration of both screens.
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
@ GFX_TOP
Top screen.
Definition: gfx.h:26
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
@ KEY_B
B.
Definition: hid.h:13
@ KEY_X
X.
Definition: hid.h:22
@ KEY_Y
Y.
Definition: hid.h:23
@ KEY_START
Start.
Definition: hid.h:15
@ KEY_R
R.
Definition: hid.h:20
@ KEY_A
A.
Definition: hid.h:12
@ KEY_L
L.
Definition: hid.h:21
u32 hidKeysHeld(void)
Returns a bitmask of held buttons.
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
void hidScanInput(void)
Scans HID for input data.
#define R_FAILED(res)
Checks whether a result code indicates failure.
Definition: result.h:11
Connection status struct.
Definition: uds.h:48
Output structure generated from host scanning output.
Definition: uds.h:134
Network struct stored as big-endian.
Definition: uds.h:61
Node info struct.
Definition: uds.h:26
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
s32 Result
Function result.
Definition: types.h:42
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
uint32_t u32
32-bit unsigned integer
Definition: types.h:23
Result udsUnbind(udsBindContext *bindcontext)
Remove a bind.
#define UDS_BROADCAST_NETWORKNODEID
Broadcast value for NetworkNodeID / alias for all NetworkNodeIDs.
Definition: uds.h:11
void udsExit(void)
Exits UDS.
Result udsPullPacket(const udsBindContext *bindcontext, void *buf, size_t size, size_t *actual_size, u16 *src_NetworkNodeID)
Receives data over the network.
Result udsCreateNetwork(const udsNetworkStruct *network, const void *passphrase, size_t passphrase_size, udsBindContext *context, u8 data_channel, u32 recv_buffer_size)
Starts hosting a new network.
Result udsSendTo(u16 dst_NetworkNodeID, u8 data_channel, u8 flags, const void *buf, size_t size)
Sends data over the network.
Result udsScanBeacons(void *outbuf, size_t maxsize, udsNetworkScanInfo **networks, size_t *total_networks, u32 wlancommID, u8 id8, const u8 *host_macaddress, bool connected)
Scans for networks via beacon-scanning.
Result udsDisconnectNetwork(void)
Disconnect this client device from the network.
bool udsWaitConnectionStatusEvent(bool nextEvent, bool wait)
Waits for the ConnectionStatus event to occur, or checks if the event was signaled.
Result udsDestroyNetwork(void)
Stop hosting the network.
Result udsSetNewConnectionsBlocked(bool block, bool clients, bool flag)
This uses udsUpdateNetworkAttribute() for (un)blocking new connections to this host.
Result udsEjectSpectator(void)
This can be used by the host to force-disconnect the spectators.
Result udsConnectNetwork(const udsNetworkStruct *network, const void *passphrase, size_t passphrase_size, udsBindContext *context, u16 recv_NetworkNodeID, udsConnectionType connection_type, u8 data_channel, u32 recv_buffer_size)
Connect to a network.
Result udsGetChannel(u8 *channel)
Gets the wifi channel currently being used.
Result udsSetApplicationData(const void *buf, size_t size)
This can be used by the host to set the appdata contained in the broadcasted beacons.
Result udsAllowSpectators(void)
This uses udsUpdateNetworkAttribute() for unblocking new spectator connections to this host.
Result udsInit(size_t sharedmem_size, const char *username)
Initializes UDS.
Result udsGetConnectionStatus(udsConnectionStatus *output)
This loads the current ConnectionStatus struct.
bool udsCheckNodeInfoInitialized(const udsNodeInfo *nodeinfo)
Checks whether a NodeInfo struct was initialized by NWM-module(not any output from udsGenerateNodeInf...
#define UDS_DATAFRAME_MAXSIZE
Max size of user data-frames.
Definition: uds.h:20
#define UDS_CHECK_SENDTO_FATALERROR(x)
Check whether a fatal udsSendTo error occured(some error(s) from udsSendTo() can be ignored,...
Definition: uds.h:23
Result udsGetNodeInformation(u16 NetworkNodeID, udsNodeInfo *output)
This loads a NodeInfo struct for the specified NetworkNodeID.
Result udsGetApplicationData(void *buf, size_t size, size_t *actual_size)
This can be used while on a network(host/client) to get the appdata from the current beacon.
Result udsGetNodeInfoUsername(const udsNodeInfo *nodeinfo, char *username)
Loads the UTF-16 username stored in the input NodeInfo struct, converted to UTF-8.
#define UDS_DEFAULT_RECVBUFSIZE
Default recv_buffer_size that can be used for udsBind() input / code which uses udsBind() internally.
Definition: uds.h:17
Result udsGetNetworkStructApplicationData(const udsNetworkStruct *network, void *buf, size_t size, size_t *actual_size)
This can be used with a NetworkStruct, from udsScanBeacons() mainly, for getting the appdata.
void udsGenerateDefaultNetworkStruct(udsNetworkStruct *network, u32 wlancommID, u8 id8, u8 max_nodes)
Generates a default NetworkStruct for creating networks.
#define UDS_MAXNODES
Maximum number of nodes(devices) that can be connected to the network.
Definition: uds.h:8