Any-Region-Changer-ModMii-E.../source/gecko.c

160 lines
3.0 KiB
C
Raw Permalink Normal View History

2022-04-21 02:12:38 +02:00
#include <gccore.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/iosupport.h>
#include <ogcsys.h>
#include <dirent.h>
#include <sys/stat.h> //for mkdir
//#include "fat2.h"
//#include "install.h"
/* init-globals */
static bool geckoinit = false;
char DirPath[64] = "Fat:/debug";
char SavePath[128];
int ret = 0;
int vasprintf(char **, const char *, va_list);
//#ifndef NO_DEBUG
#include <stdarg.h>
void gprintf(const char *format, ...)
{
if (!geckoinit)
2022-04-21 03:38:39 +02:00
return;
char *tmp = NULL;
2022-04-21 02:12:38 +02:00
va_list va;
va_start(va, format);
2022-04-21 03:38:39 +02:00
if ((vasprintf(&tmp, format, va) >= 0) && tmp)
2022-04-21 02:12:38 +02:00
{
2022-04-21 03:38:39 +02:00
usb_sendbuffer(1, tmp, strlen(tmp));
2022-04-21 02:12:38 +02:00
}
va_end(va);
2022-04-21 03:38:39 +02:00
if (tmp)
free(tmp);
2022-04-21 02:12:38 +02:00
}
bool InitGecko()
{
2022-04-21 03:38:39 +02:00
u32 geckoattached = usb_isgeckoalive(EXI_CHANNEL_1);
if (geckoattached)
{
usb_flush(EXI_CHANNEL_1);
geckoinit = true;
return true;
}
return false;
2022-04-21 02:12:38 +02:00
}
2022-04-21 03:38:39 +02:00
void gecko_log(const char *format, ...)
{
// ret = Fat_MakeDir(DirPath);
// void *f = NULL;
2022-04-21 02:12:38 +02:00
gprintf("\n\n Fat Make Dir ret[%i] \n\n", ret);
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
if (!geckoinit)
2022-04-21 03:38:39 +02:00
return;
char *tmp = NULL;
2022-04-21 02:12:38 +02:00
va_list va;
va_start(va, format);
2022-04-21 03:38:39 +02:00
if ((vasprintf(&tmp, format, va) >= 0) && tmp)
2022-04-21 02:12:38 +02:00
{
sprintf(SavePath, "%s/debug.log", DirPath);
2022-04-21 03:38:39 +02:00
// f = &tmp;
// Fat_SaveFilelog(SavePath, f, strlen(tmp));
// usb_sendbuffer(1, tmp, strlen(tmp));
2022-04-21 02:12:38 +02:00
}
va_end(va);
2022-04-21 03:38:39 +02:00
// if(tmp)
// free(tmp);
2022-04-21 02:12:38 +02:00
tmp = NULL;
2022-04-21 03:38:39 +02:00
// f = NULL;
2022-04-21 02:12:38 +02:00
}
char ascii2(char s)
{
2022-04-21 03:38:39 +02:00
if (s < 0x20)
return '.';
if (s > 0x7E)
return '.';
return s;
2022-04-21 02:12:38 +02:00
}
void hexdump2(void *d, int len)
{
2022-04-21 03:38:39 +02:00
u8 *data;
int i, off;
data = (u8 *)d;
gprintf("\n 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF");
gprintf("\n==== =============================================== ================\n");
for (off = 0; off < len; off += 16)
{
gprintf("%04x ", off);
for (i = 0; i < 16; i++)
if ((i + off) >= len)
gprintf(" ");
else
gprintf("%02x ", data[off + i]);
gprintf(" ");
for (i = 0; i < 16; i++)
if ((i + off) >= len)
gprintf(" ");
else
gprintf("%c", ascii2(data[off + i]));
gprintf("\n");
}
2022-04-21 02:12:38 +02:00
}
static ssize_t __out_write(struct _reent *r, void *fd, const char *ptr, size_t len)
2022-04-21 02:12:38 +02:00
{
2022-04-21 03:38:39 +02:00
if (geckoinit && ptr)
{
usb_sendbuffer(1, ptr, len);
2022-04-21 02:12:38 +02:00
}
return len;
}
static const devoptab_t gecko_out = {
2022-04-21 03:38:39 +02:00
"stdout", // device name
0, // size of file structure
NULL, // device open
NULL, // device close
__out_write, // device write
NULL, // device read
NULL, // device seek
NULL, // device fstat
NULL, // device stat
NULL, // device link
NULL, // device unlink
NULL, // device chdir
NULL, // device rename
NULL, // device mkdir
0, // dirStateSize
NULL, // device diropen_r
NULL, // device dirreset_r
NULL, // device dirnext_r
NULL, // device dirclose_r
NULL, // device statvfs_r
NULL, // device ftruncate_r
NULL, // device fsync_r
NULL, // device deviceData
NULL, // device chmod_r
NULL, // device fchmod_r
2022-04-21 02:12:38 +02:00
};
void USBGeckoOutput()
{
devoptab_list[STD_OUT] = &gecko_out;
devoptab_list[STD_ERR] = &gecko_out;
}
//#endif /* NO_DEBUG */