Improve gecko logging over WiFi

This commit is contained in:
wiidev 2021-02-04 16:27:18 +00:00
parent bd9e707e6c
commit 27bce4d461

View File

@ -23,21 +23,27 @@
* *
* for WiiXplorer 2010 * for WiiXplorer 2010
***************************************************************************/ ***************************************************************************/
#include <errno.h>
#include <network.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <network.h>
#define DESTINATION_IP "192.168.1.255" #define DESTINATION_IP "192.168.1.255"
#define DESTINATION_PORT 4405 #define DESTINATION_PORT 4405
#define GPRINTF_SIZE 256
#define WIFIGECKO_SIZE 1024
char wifigeckobuffer[WIFIGECKO_SIZE];
static int connection = -1; static int connection = -1;
void WifiGecko_Close() void WifiGecko_Close()
{ {
if(connection >= 0) if (connection >= 0)
net_close(connection); net_close(connection);
connection = -1; connection = -1;
@ -45,7 +51,7 @@ void WifiGecko_Close()
int WifiGecko_Connect() int WifiGecko_Connect()
{ {
if(connection >= 0) if (connection >= 0)
return connection; return connection;
connection = net_socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); connection = net_socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
@ -58,7 +64,7 @@ int WifiGecko_Connect()
connect_addr.sin_port = htons(DESTINATION_PORT); connect_addr.sin_port = htons(DESTINATION_PORT);
inet_aton(DESTINATION_IP, &connect_addr.sin_addr); inet_aton(DESTINATION_IP, &connect_addr.sin_addr);
if(net_connect(connection, (struct sockaddr*)&connect_addr, sizeof(connect_addr)) < 0) if (net_connect(connection, (struct sockaddr *)&connect_addr, sizeof(connect_addr)) < 0)
{ {
WifiGecko_Close(); WifiGecko_Close();
return -1; return -1;
@ -67,47 +73,33 @@ int WifiGecko_Connect()
return connection; return connection;
} }
int WifiGecko_Send(const char * data, int datasize) int WifiGecko_Send(const char *data, int datasize)
{ {
if(WifiGecko_Connect() < 0) if ((strlen(wifigeckobuffer) + datasize) < WIFIGECKO_SIZE)
strcat(wifigeckobuffer, data);
if (WifiGecko_Connect() < 0)
return connection; return connection;
int ret = 0, done = 0, blocksize = 1024; u32 sendsize = strlen(wifigeckobuffer);
while (done < datasize) while (net_get_status() == -EBUSY)
{ usleep(10000);
if(blocksize > datasize-done) int ret = net_send(connection, wifigeckobuffer, sendsize, 0);
blocksize = datasize-done; if (ret < 0)
WifiGecko_Close();
ret = net_send(connection, data+done, blocksize, 0);
if (ret < 0)
{
WifiGecko_Close();
return ret;
}
else if(ret == 0)
{
break;
}
done += ret;
usleep (1000);
}
memset(wifigeckobuffer, 0, WIFIGECKO_SIZE);
return ret; return ret;
} }
void wifi_printf(const char * format, ...) void wifi_printf(const char *format, ...)
{ {
char * tmp = NULL; char gprintfBuffer[GPRINTF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) size_t len = vsnprintf(gprintfBuffer, GPRINTF_SIZE - 1, format, va);
{
WifiGecko_Send(tmp, strlen(tmp));
}
va_end(va); va_end(va);
if (len)
if(tmp) WifiGecko_Send(gprintfBuffer, len);
free(tmp);
} }