12#include "../../ThreadDriver.h"
13#include "../../Time.h"
15#define NETWORK_THREAD_STACKSIZE (8 * 1024)
16#define NETWORK_BRINGUP_TIMEOUT_SECS 10
19static Thread networkThread;
20static bool networkIdle =
false;
21static bool networkUp =
false;
22static bool netPrevInit =
false;
23static void (*onUpCallback)() =
nullptr;
25static void WakeNetworkThread()
28 NetSync().workCond.signal();
38static void * NetworkThreadEntry(
void *)
40 NetSync().mutex.lock();
41 while(!networkThread.stopRequested())
43 NetSync().mutex.unlock();
46 bool success = (net_gethostip() > 0);
53 while(retry > 0 && !networkThread.stopRequested())
58 for(
int i = 0; i < 400 && !networkThread.stopRequested(); i++)
60 res = net_get_status();
74 res = net_init_async(NULL, NULL);
83 res = net_get_status();
85 while(res == -EBUSY && wait > 0 && !networkThread.stopRequested())
88 res = net_get_status();
101 struct in_addr hostip;
102 hostip.s_addr = net_gethostip();
111 void (*firedCallback)() =
nullptr;
113 NetSync().mutex.lock();
116 NetSync().idleCond.signal();
118 if(success && onUpCallback)
120 firedCallback = onUpCallback;
121 onUpCallback =
nullptr;
126 NetSync().mutex.unlock();
128 NetSync().mutex.lock();
131 while(networkIdle && !networkThread.stopRequested())
132 NetSync().workCond.wait(NetSync().mutex);
134 NetSync().mutex.unlock();
140 return net_gethostip() > 0;
149 if(net_gethostip() > 0)
152 if(!networkThread.isRunning())
158 networkThread.start(NetworkThreadEntry,
nullptr, NETWORK_THREAD_STACKSIZE, ThreadPriority::Low, WakeNetworkThread);
164 NetSync().workCond.signal();
178 return up && net_gethostip() > 0;
180 if(cancel && cancel())
192 onUpCallback = callback;
196 if(!networkThread.isRunning())
197 networkThread.start(NetworkThreadEntry,
nullptr, NETWORK_THREAD_STACKSIZE, ThreadPriority::Low, WakeNetworkThread);
206 onUpCallback =
nullptr;
208 networkThread.requestStop();
209 networkThread.join();
RAII lock guard - locks on construction, unlocks on destruction.
static uint32_t diffSecs(Ticks start, Ticks end)
static void notifyWhenUp(void(*callback)())
Registers callback to run exactly once.
static void shutdown()
Stops and joins the background bring-up thread.
static bool isUp()
Non-blocking: true if the console currently has a usable IP.
static bool ensureUp(bool(*cancel)(void)=nullptr)
Brings the network interface up if it isn't already, or confirms it's still alive if it is.
Bundles the mutex and pair of condition variables used by the common producer/consumer handshake betw...