Skip to content

Commit

Permalink
- Make network init asynchronous.
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed May 14, 2024
1 parent 5f8d400 commit d840edd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 49 deletions.
2 changes: 2 additions & 0 deletions cube/swiss/include/bba.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ extern struct in_addr bba_netmask;
extern struct in_addr bba_gateway;
extern u32 bba_location;

void wait_network();
void init_network();
void init_network_async();
void init_wiiload_thread();
bool bba_exists(u32 location);

Expand Down
108 changes: 67 additions & 41 deletions cube/swiss/source/bba.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <malloc.h>
#include <network.h>
#include <ogcsys.h>
#include "bba.h"
#include "exi.h"
#include "httpd.h"
#include "swiss.h"
#include "deviceHandler.h"

Expand All @@ -14,54 +16,78 @@ struct in_addr bba_netmask;
struct in_addr bba_gateway;
u32 bba_location = LOC_UNK;

// Init the GC net interface (bba)
void init_network(void *args) {

int res = 0;
static lwp_t net_thread = LWP_THREAD_NULL;

if(!net_initialized) {
inet_aton(swissSettings.bbaLocalIp, &bba_localip);
bba_netmask.s_addr = INADDR_BROADCAST << (32 - swissSettings.bbaNetmask);
inet_aton(swissSettings.bbaGateway, &bba_gateway);
static void *net_thread_func(void *arg)
{
inet_aton(swissSettings.bbaLocalIp, &bba_localip);
bba_netmask.s_addr = INADDR_BROADCAST << (32 - swissSettings.bbaNetmask);
inet_aton(swissSettings.bbaGateway, &bba_gateway);

res = if_configex(&bba_localip, &bba_netmask, &bba_gateway, swissSettings.bbaUseDhcp);
if(res >= 0) {
strcpy(swissSettings.bbaLocalIp, inet_ntoa(bba_localip));
swissSettings.bbaNetmask = (32 - __builtin_ctz(bba_netmask.s_addr));
strcpy(swissSettings.bbaGateway, inet_ntoa(bba_gateway));
net_initialized = 1;
}
else {
net_initialized = 0;
}
if (if_configex(&bba_localip, &bba_netmask, &bba_gateway, swissSettings.bbaUseDhcp) == -1) {
net_initialized = 0;
return NULL;
}

char ifname[4];
if (if_indextoname(1, ifname)) {
if (ifname[0] == 'E') {
switch (ifname[1]) {
case 'A': bba_location = LOC_MEMCARD_SLOT_A; break;
case 'B': bba_location = LOC_MEMCARD_SLOT_B; break;
case '1': bba_location = LOC_SERIAL_PORT_1; break;
case '2': bba_location = LOC_SERIAL_PORT_2; break;
}
} else if (ifname[0] == 'e')
bba_location = LOC_SERIAL_PORT_1;
strcpy(swissSettings.bbaLocalIp, inet_ntoa(bba_localip));
swissSettings.bbaNetmask = (32 - __builtin_ctz(bba_netmask.s_addr));
strcpy(swissSettings.bbaGateway, inet_ntoa(bba_gateway));

if (ifname[0] == 'E' && strchr("2AB", ifname[1])) {
if (__device_gcloader.features & FEAT_PATCHES)
__device_gcloader.emulable |= EMU_ETHERNET;
__device_ata_c.emulable |= EMU_ETHERNET;
__device_sd_c.emulable |= EMU_ETHERNET;
__device_sd_a.emulable |= EMU_ETHERNET;
__device_sd_b.emulable |= EMU_ETHERNET;
__device_ata_a.emulable |= EMU_ETHERNET;
__device_ata_b.emulable |= EMU_ETHERNET;
char ifname[4];
if (if_indextoname(1, ifname)) {
if (ifname[0] == 'E') {
switch (ifname[1]) {
case 'A': bba_location = LOC_MEMCARD_SLOT_A; break;
case 'B': bba_location = LOC_MEMCARD_SLOT_B; break;
case '1': bba_location = LOC_SERIAL_PORT_1; break;
case '2': bba_location = LOC_SERIAL_PORT_2; break;
}
} else if (ifname[0] == 'e')
bba_location = LOC_SERIAL_PORT_1;

if (ifname[0] == 'E' && strchr("2AB", ifname[1])) {
if (__device_gcloader.features & FEAT_PATCHES)
__device_gcloader.emulable |= EMU_ETHERNET;
__device_ata_c.emulable |= EMU_ETHERNET;
__device_sd_c.emulable |= EMU_ETHERNET;
__device_sd_a.emulable |= EMU_ETHERNET;
__device_sd_b.emulable |= EMU_ETHERNET;
__device_ata_a.emulable |= EMU_ETHERNET;
__device_ata_b.emulable |= EMU_ETHERNET;
}
}

deviceHandler_setDeviceAvailable(&__device_smb, deviceHandler_SMB_test());
deviceHandler_setDeviceAvailable(&__device_ftp, deviceHandler_FTP_test());
deviceHandler_setDeviceAvailable(&__device_fsp, deviceHandler_FSP_test());
deviceHandler_setDeviceAvailable(&__device_smb, deviceHandler_SMB_test());
deviceHandler_setDeviceAvailable(&__device_ftp, deviceHandler_FTP_test());
deviceHandler_setDeviceAvailable(&__device_fsp, deviceHandler_FSP_test());

net_initialized = 1;
init_httpd_thread();
init_wiiload_thread();
return NULL;
}

// Init the GC net interface (bba)
void init_network_async(void)
{
if (!net_initialized && net_thread == LWP_THREAD_NULL)
LWP_CreateThread(&net_thread, net_thread_func, NULL, NULL, 0, LWP_PRIO_NORMAL);
}

void init_network(void)
{
init_network_async();
wait_network();
}

void wait_network(void)
{
lwp_t thread = net_thread;
net_thread = LWP_THREAD_NULL;
if (thread != LWP_THREAD_NULL) {
uiDrawObj_t *msgBox = DrawPublish(DrawProgressBar(true, 0, "Initialising Network"));
LWP_JoinThread(thread, NULL);
DrawDispose(msgBox);
}
}

Expand Down
3 changes: 2 additions & 1 deletion cube/swiss/source/gui/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ uiDrawObj_t * info_draw_page(int page_num) {
DrawAddChild(container, DrawStyledLabel(640/2, 146, getHwNameByLocation(LOC_MEMCARD_SLOT_B), 0.75f, true, defaultColor));
DrawAddChild(container, DrawStyledLabel(640/2, 170, (char*)"SERIAL PORT 1", 0.65f, true, defaultColor));
if(bba_exists(LOC_SERIAL_PORT_1)) {
sprintf(topStr, "%s (%s)", getHwNameByLocation(LOC_SERIAL_PORT_1), !net_initialized ? "Not initialised" : inet_ntoa(bba_localip));
bba_localip.s_addr = net_gethostip();
sprintf(topStr, "%s (%s)", getHwNameByLocation(LOC_SERIAL_PORT_1), bba_localip.s_addr == INADDR_ANY ? "Not initialised" : inet_ntoa(bba_localip));
}
else {
strcpy(topStr, getHwNameByLocation(LOC_SERIAL_PORT_1));
Expand Down
2 changes: 1 addition & 1 deletion cube/swiss/source/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int is_httpd_in_use() {
}

void init_httpd_thread() {
if(net_initialized) {
if (net_initialized && httpd_handle == LWP_THREAD_NULL) {
LWP_CreateThread( &httpd_handle, /* thread handle */
httpd, /* code */
NULL, /* arg pointer for thread */
Expand Down
6 changes: 1 addition & 5 deletions cube/swiss/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ int main(int argc, char *argv[])
swissSettings.initNetworkAtStart |= bba_exists(LOC_MEMCARD_SLOT_A | LOC_MEMCARD_SLOT_B | LOC_SERIAL_PORT_2);
if(swissSettings.initNetworkAtStart) {
// Start up the BBA if it exists
uiDrawObj_t *msgBox = DrawPublish(DrawProgressBar(true, 0, "Initialising Network"));
init_network();
init_httpd_thread();
init_wiiload_thread();
DrawDispose(msgBox);
init_network_async();
}

DEVICEHANDLER_INTERFACE *device = getDeviceByLocation(LOC_DVD_CONNECTOR);
Expand Down
2 changes: 1 addition & 1 deletion cube/swiss/source/wiiload.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,6 @@ static void *thread_func(void *arg)

void init_wiiload_thread(void)
{
if (net_initialized)
if (net_initialized && thread == LWP_THREAD_NULL)
LWP_CreateThread(&thread, thread_func, NULL, NULL, 0, LWP_PRIO_NORMAL);
}

0 comments on commit d840edd

Please sign in to comment.