blob: 4e4ec6f0684289f5f76de3181d5f49a99415c642 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright (C) 2008 Tantric
* Copyright (C) 2012 Pedro Aguiar
* Copyright (C) 2017 hatkirby
*
* Originally part of the WiiTweet project, which was distributed under the
* GPL.
*/
#include "netinf.h"
#include <gccore.h>
#include <network.h>
#include <ogc/lwp_watchdog.h>
#include <sys/errno.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
static bool networkInit = false;
static lwp_t networkThread = LWP_THREAD_NULL;
static u8 netStack[32768] ATTRIBUTE_ALIGN(32);
static char wiiIP[16] = {0};
static bool netHalt = false;
static void* interface(void* arg)
{
static bool prevInit = false;
while (!netHalt)
{
int retry = 5;
int result = -1;
while ((retry > 0) && (!netHalt))
{
if (prevInit)
{
net_deinit();
for (int i=0; (i<400) && !netHalt; i++)
{
if (net_get_status() != -EBUSY)
{
usleep(2000);
net_wc24cleanup();
prevInit = false;
usleep(20000);
break;
}
usleep(20000);
}
}
usleep(2000);
if (net_init_async(NULL, NULL))
{
sleep(1);
retry--;
continue;
}
result = net_get_status();
for (int wait = 400; (wait > 0) && (result == -EBUSY) && !netHalt; wait++)
{
usleep(20000);
result = net_get_status();
}
if (result == 0)
{
break;
}
retry--;
usleep(2000);
}
if (result == 0)
{
struct in_addr hostip;
hostip.s_addr = net_gethostip();
if (hostip.s_addr)
{
strcpy(wiiIP, inet_ntoa(hostip));
networkInit = true;
prevInit = true;
}
}
if (!netHalt)
{
LWP_SuspendThread(networkThread);
}
}
return NULL;
}
static void startNetworkThread()
{
netHalt = false;
if (networkThread == LWP_THREAD_NULL)
{
LWP_CreateThread(&networkThread, interface, NULL, netStack, 8192, 40);
} else {
LWP_ResumeThread(networkThread);
}
}
static void stopNetworkThread()
{
if ((networkThread == LWP_THREAD_NULL)
|| (!LWP_ThreadIsSuspended(networkThread)))
{
return;
}
netHalt = true;
LWP_ResumeThread(networkThread);
LWP_JoinThread(networkThread, NULL);
networkThread = LWP_THREAD_NULL;
}
bool initializeNetwork()
{
stopNetworkThread();
if (networkInit && (net_gethostip() > 0))
{
return true;
}
networkInit = false;
printf("Initializing network...\n");
u64 start = gettime();
startNetworkThread();
while (!LWP_ThreadIsSuspended(networkThread))
{
usleep(50 * 1000);
if (diff_sec(start, gettime()) > 10)
{
break;
}
}
return networkInit;
}
|