vbagx/source/networkop.cpp

259 lines
5.2 KiB
C++
Raw Permalink Normal View History

2008-12-24 08:52:35 +01:00
/****************************************************************************
* Visual Boy Advance GX
*
2021-01-06 21:13:12 +01:00
* Tantric 2008-2021
2008-12-24 08:52:35 +01:00
*
* networkop.cpp
*
* Network and SMB support routines
****************************************************************************/
#include <errno.h>
2008-12-24 08:52:35 +01:00
#include <network.h>
2010-08-18 02:15:41 +02:00
#include <malloc.h>
#include <ogc/lwp_watchdog.h>
2008-12-24 08:52:35 +01:00
#include <smb.h>
2010-03-22 00:49:59 +01:00
#include "vbagx.h"
2009-04-08 09:08:12 +02:00
#include "menu.h"
2008-12-24 08:52:35 +01:00
#include "fileop.h"
2009-06-12 09:43:19 +02:00
#include "filebrowser.h"
2008-12-24 08:52:35 +01:00
static bool networkInit = false;
static bool networkShareInit = false;
2010-08-18 02:15:41 +02:00
char wiiIP[16] = { 0 };
2011-02-03 04:44:53 +01:00
#ifdef HW_RVL
static int netHalt = 0;
2008-12-24 08:52:35 +01:00
/****************************************************************************
* InitializeNetwork
* Initializes the Wii/GameCube network interface
***************************************************************************/
2010-08-18 02:15:41 +02:00
static lwp_t networkthread = LWP_THREAD_NULL;
2011-02-06 06:38:24 +01:00
static u8 netstack[32768] ATTRIBUTE_ALIGN (32);
2010-08-18 02:15:41 +02:00
static void * netcb (void *arg)
{
2011-02-04 05:42:07 +01:00
s32 res=-1;
2010-08-18 02:15:41 +02:00
int retry;
2010-08-21 00:33:54 +02:00
int wait;
2011-03-13 06:49:30 +01:00
static bool prevInit = false;
2010-08-18 02:15:41 +02:00
while(netHalt != 2)
{
2011-02-04 05:42:07 +01:00
retry = 5;
while (retry>0 && (netHalt != 2))
2011-02-06 06:38:24 +01:00
{
2011-03-13 06:49:30 +01:00
if(prevInit)
2011-02-04 05:42:07 +01:00
{
int i;
2011-06-18 08:11:28 +02:00
net_deinit();
for(i=0; i < 400 && (netHalt != 2); i++) // 10 seconds to try to reset
2011-02-04 05:42:07 +01:00
{
res = net_get_status();
if(res != -EBUSY) // trying to init net so we can't kill the net
{
2011-02-06 06:38:24 +01:00
usleep(2000);
2011-02-04 05:42:07 +01:00
net_wc24cleanup(); //kill the net
2011-06-18 08:11:28 +02:00
prevInit=false; // net_wc24cleanup is called only once
usleep(20000);
2011-02-04 05:42:07 +01:00
break;
}
usleep(20000);
}
}
2011-03-13 06:49:30 +01:00
2011-02-06 06:38:24 +01:00
usleep(2000);
2010-08-21 00:33:54 +02:00
res = net_init_async(NULL, NULL);
if(res != 0)
2011-02-04 05:42:07 +01:00
{
sleep(1);
retry--;
continue;
}
2010-08-18 02:15:41 +02:00
res = net_get_status();
2011-02-06 06:38:24 +01:00
wait = 400; // only wait 8 sec
2011-02-04 05:42:07 +01:00
while (res == -EBUSY && wait > 0 && (netHalt != 2))
2010-08-18 02:15:41 +02:00
{
usleep(20000);
res = net_get_status();
2010-08-21 00:33:54 +02:00
wait--;
2010-08-18 02:15:41 +02:00
}
2011-02-04 05:42:07 +01:00
if(res==0) break;
2010-08-18 02:15:41 +02:00
retry--;
usleep(2000);
}
2010-08-21 00:33:54 +02:00
if (res == 0)
2010-08-18 02:15:41 +02:00
{
struct in_addr hostip;
hostip.s_addr = net_gethostip();
if (hostip.s_addr)
{
2010-08-18 02:15:41 +02:00
strcpy(wiiIP, inet_ntoa(hostip));
2011-03-13 06:49:30 +01:00
networkInit = true;
prevInit = true;
}
2010-08-18 02:15:41 +02:00
}
2011-02-04 05:42:07 +01:00
if(netHalt != 2) LWP_SuspendThread(networkthread);
2010-08-18 02:15:41 +02:00
}
return NULL;
}
/****************************************************************************
* StartNetworkThread
*
* Signals the network thread to resume, or creates a new thread
***************************************************************************/
void StartNetworkThread()
2008-12-24 08:52:35 +01:00
{
2010-08-18 02:15:41 +02:00
netHalt = 0;
if(networkthread == LWP_THREAD_NULL)
LWP_CreateThread(&networkthread, netcb, NULL, netstack, 8192, 40);
else
LWP_ResumeThread(networkthread);
}
/****************************************************************************
* StopNetworkThread
*
* Signals the network thread to stop
***************************************************************************/
void StopNetworkThread()
{
2011-02-06 06:38:24 +01:00
if(networkthread == LWP_THREAD_NULL || !LWP_ThreadIsSuspended(networkthread))
2008-12-24 08:52:35 +01:00
return;
2010-08-18 02:15:41 +02:00
netHalt = 2;
2011-02-06 06:38:24 +01:00
LWP_ResumeThread(networkthread);
2008-12-24 08:52:35 +01:00
2010-08-18 02:15:41 +02:00
// wait for thread to finish
LWP_JoinThread(networkthread, NULL);
networkthread = LWP_THREAD_NULL;
}
2009-04-08 09:08:12 +02:00
#endif
2010-08-18 02:15:41 +02:00
bool InitializeNetwork(bool silent)
{
#ifdef HW_RVL
2011-02-03 04:35:21 +01:00
StopNetworkThread();
if(networkInit && net_gethostip() > 0)
2010-08-18 02:15:41 +02:00
return true;
2008-12-24 08:52:35 +01:00
2011-02-03 04:35:21 +01:00
networkInit = false;
#else
if(networkInit)
return true;
#endif
2009-10-06 09:17:52 +02:00
2010-08-18 02:15:41 +02:00
int retry = 1;
2009-10-06 09:17:52 +02:00
2010-08-18 02:15:41 +02:00
while(retry)
{
ShowAction("Initializing network...");
#ifdef HW_RVL
2011-02-03 04:44:53 +01:00
u64 start = gettime();
2010-08-18 02:15:41 +02:00
StartNetworkThread();
while (!LWP_ThreadIsSuspended(networkthread))
{
usleep(50 * 1000);
2009-10-06 09:17:52 +02:00
2010-08-18 02:15:41 +02:00
if(diff_sec(start, gettime()) > 10) // wait for 10 seconds max for net init
break;
2008-12-24 08:52:35 +01:00
}
#else
networkInit = !(if_config(wiiIP, NULL, NULL, true, 10) < 0);
#endif
2009-10-06 09:17:52 +02:00
CancelAction();
2010-08-18 02:15:41 +02:00
2011-02-03 04:35:21 +01:00
if(networkInit || silent)
2010-08-18 02:15:41 +02:00
break;
retry = ErrorPromptRetry("Unable to initialize network!");
2011-02-04 05:42:07 +01:00
2011-02-10 19:08:24 +01:00
#ifdef HW_RVL
2011-02-04 05:42:07 +01:00
if(networkInit && net_gethostip() > 0)
2011-02-10 19:06:06 +01:00
#else
if(networkInit)
#endif
2011-02-04 05:42:07 +01:00
return true;
2010-08-18 02:15:41 +02:00
}
return networkInit;
2008-12-24 08:52:35 +01:00
}
void CloseShare()
{
if(networkShareInit)
2009-04-08 09:08:12 +02:00
smbClose("smb");
2008-12-24 08:52:35 +01:00
networkShareInit = false;
2010-03-15 08:46:11 +01:00
isMounted[DEVICE_SMB] = false;
2008-12-24 08:52:35 +01:00
}
/****************************************************************************
* Mount SMB Share
****************************************************************************/
bool
ConnectShare (bool silent)
{
2010-08-18 02:15:41 +02:00
if(!InitializeNetwork(silent))
return false;
2010-06-19 19:13:18 +02:00
if(networkShareInit)
return true;
2009-10-06 09:17:52 +02:00
int retry = 1;
2008-12-24 08:52:35 +01:00
int chkS = (strlen(GCSettings.smbshare) > 0) ? 0:1;
int chkI = (strlen(GCSettings.smbip) > 0) ? 0:1;
// check that all parameters have been set
2009-10-04 21:29:47 +02:00
if(chkS + chkI > 0)
2008-12-24 08:52:35 +01:00
{
if(!silent)
{
char msg[50];
char msg2[100];
2009-10-04 21:29:47 +02:00
if(chkS + chkI > 1) // more than one thing is wrong
2008-12-24 08:52:35 +01:00
sprintf(msg, "Check settings.xml.");
else if(chkS)
sprintf(msg, "Share name is blank.");
else if(chkI)
sprintf(msg, "Share IP is blank.");
sprintf(msg2, "Invalid network settings - %s", msg);
2009-04-08 09:08:12 +02:00
ErrorPrompt(msg2);
2008-12-24 08:52:35 +01:00
}
return false;
}
2009-10-06 09:17:52 +02:00
while(retry)
{
if(!silent)
ShowAction ("Connecting to network share...");
if(smbInit(GCSettings.smbuser, GCSettings.smbpwd, GCSettings.smbshare, GCSettings.smbip))
networkShareInit = true;
2009-10-06 09:17:52 +02:00
if(networkShareInit || silent)
break;
2008-12-24 08:52:35 +01:00
2009-10-06 09:17:52 +02:00
retry = ErrorPromptRetry("Failed to connect to network share.");
2008-12-24 08:52:35 +01:00
}
2009-10-06 09:17:52 +02:00
if(!silent)
CancelAction();
2008-12-24 08:52:35 +01:00
return networkShareInit;
}