actually init pcap etc

This commit is contained in:
StapleButter 2017-12-17 23:09:48 +01:00
parent 17087a1ece
commit e44bc7743d
3 changed files with 74 additions and 5 deletions

View File

@ -41,6 +41,9 @@ void MP_DeInit();
int MP_SendPacket(u8* data, int len);
int MP_RecvPacket(u8* data, bool block);
bool LAN_Init();
void LAN_DeInit();
}
#endif // PLATFORM_H

View File

@ -81,6 +81,7 @@ int MPReplyTimer;
int MPNumReplies;
bool MPInited;
bool LANInited;
@ -89,9 +90,9 @@ bool MPInited;
// 2. IRQ7
// 3. send data
// 4. optional IRQ1
// 5. wait for client replies (duration: 112 + ((10 * CMD_REPLYTIME) * numclients))
// 5. wait for client replies (duration: 16 + ((10 * CMD_REPLYTIME) * numclients) + ack preamble (96))
// 6. IRQ7
// 7. send ack (16 bytes, 1Mbps)
// 7. send ack (32 bytes)
// 8. optional IRQ1, along with IRQ12 if the transfer was successful or if
// there's no time left for a retry
//
@ -123,6 +124,7 @@ bool MPInited;
bool Init()
{
MPInited = false;
LANInited = false;
return true;
}
@ -131,6 +133,8 @@ void DeInit()
{
if (MPInited)
Platform::MP_DeInit();
if (LANInited)
Platform::LAN_DeInit();
}
void Reset()
@ -1285,15 +1289,22 @@ void Write(u32 addr, u16 val)
// TODO: check whether this resets USCOUNT (and also which other events can reset it)
if ((IOPORT(W_PowerUS) & 0x0001) && !(val & 0x0001))
{
printf("WIFI ON\n");
NDS::ScheduleEvent(NDS::Event_Wifi, true, 33, USTimer, 0);
if (!MPInited)
{
Platform::MP_Init();
MPInited = true;
}
if (!LANInited)
{
Platform::LAN_Init();
LANInited = true;
}
}
else if (!(IOPORT(W_PowerUS) & 0x0001) && (val & 0x0001))
{
printf("WIFI OFF\n");
NDS::CancelEvent(NDS::Event_Wifi);
}
break;

View File

@ -86,6 +86,21 @@ const char* PCapLibNames[] =
NULL
};
void* PCapLib;
#define DECL_PCAP_FUNC(ret, name, args, args2) \
typedef ret (*type_##name) args; \
type_##name ptr_##name = NULL; \
ret name args { return ptr_##name args2; }
DECL_PCAP_FUNC(int, pcap_findalldevs, (pcap_if_t** alldevs, char* errbuf), (alldevs,errbuf))
DECL_PCAP_FUNC(void, pcap_freealldevs, (pcap_if_t* alldevs), (alldevs))
DECL_PCAP_FUNC(pcap_t*, pcap_open_live, (const char* src, int snaplen, int flags, int readtimeout, char* errbuf), (src,snaplen,flags,readtimeout,errbuf))
DECL_PCAP_FUNC(void, pcap_close, (pcap_t* dev), (dev))
DECL_PCAP_FUNC(int, pcap_setnonblock, (pcap_t* dev, int nonblock, char* errbuf), (dev,nonblock,errbuf))
DECL_PCAP_FUNC(int, pcap_sendpacket, (pcap_t* dev, const u_char* data, int len), (dev,data,len))
DECL_PCAP_FUNC(int, pcap_dispatch, (pcap_t* dev, int num, pcap_handler callback, u_char* data), (dev,num,callback,data))
void StopEmu()
{
@ -273,21 +288,61 @@ int MP_RecvPacket(u8* data, bool block)
// LAN interface. Currently powered by libpcap, may change.
#define LOAD_PCAP_FUNC(sym) \
ptr_##sym = (type_##sym)SDL_LoadFunction(lib, #sym); \
if (!ptr_##sym) return false;
bool TryLoadPCap(void* lib)
{
LOAD_PCAP_FUNC(pcap_findalldevs)
LOAD_PCAP_FUNC(pcap_freealldevs)
LOAD_PCAP_FUNC(pcap_open_live)
LOAD_PCAP_FUNC(pcap_close)
LOAD_PCAP_FUNC(pcap_setnonblock)
LOAD_PCAP_FUNC(pcap_sendpacket)
LOAD_PCAP_FUNC(pcap_dispatch)
return true;
}
bool LAN_Init()
{
PCapLib = NULL;
for (int i = 0; PCapLibNames[i]; i++)
{
void* lib = SDL_LoadObject(PCapLibNames[i]);
if (!lib) continue;
printf("loaded lib %s\n", PCapLibNames[i]);
SDL_UnloadObject(lib);
if (!TryLoadPCap(lib))
{
SDL_UnloadObject(lib);
continue;
}
printf("PCap: lib %s, init successful\n", PCapLibNames[i]);
PCapLib = lib;
break;
}
if (PCapLib == NULL)
{
printf("PCap: init failed\n");
return false;
}
//
return true;
}
void LAN_DeInit()
{
//
if (PCapLib)
{
SDL_UnloadObject(PCapLib);
PCapLib = NULL;
}
}