|
| 1 | +/*------------------------------------------------------------- |
| 2 | +
|
| 3 | +system_report.c -- Formatted output to serial port |
| 4 | +
|
| 5 | +Copyright (C) 2025 Extrems' Corner.org |
| 6 | +
|
| 7 | +This software is provided 'as-is', without any express or implied |
| 8 | +warranty. In no event will the authors be held liable for any |
| 9 | +damages arising from the use of this software. |
| 10 | +
|
| 11 | +Permission is granted to anyone to use this software for any |
| 12 | +purpose, including commercial applications, and to alter it and |
| 13 | +redistribute it freely, subject to the following restrictions: |
| 14 | +
|
| 15 | +1. The origin of this software must not be misrepresented; you |
| 16 | +must not claim that you wrote the original software. If you use |
| 17 | +this software in a product, an acknowledgment in the product |
| 18 | +documentation would be appreciated but is not required. |
| 19 | +
|
| 20 | +2. Altered source versions must be plainly marked as such, and |
| 21 | +must not be misrepresented as being the original software. |
| 22 | +
|
| 23 | +3. This notice may not be removed or altered from any source |
| 24 | +distribution. |
| 25 | +
|
| 26 | +-------------------------------------------------------------*/ |
| 27 | + |
| 28 | +#include <gctypes.h> |
| 29 | +#include <ogc/system.h> |
| 30 | +#include <ogc/usbgecko.h> |
| 31 | +#include <stdarg.h> |
| 32 | +#include <stdio.h> |
| 33 | + |
| 34 | +static FILE *fp = NULL; |
| 35 | +static s32 Chan = -1; |
| 36 | +static bool Safe = true; |
| 37 | + |
| 38 | +extern s32 InitializeUART(void); |
| 39 | +extern s32 WriteUARTN(void *buf, u32 len); |
| 40 | + |
| 41 | +static int WriteReport(void *c, const char *buf, int n) |
| 42 | +{ |
| 43 | + if (usb_isgeckoalive(Chan)) { |
| 44 | + if (Safe) |
| 45 | + return usb_sendbuffer_safe(Chan, buf, n); |
| 46 | + else |
| 47 | + return usb_sendbuffer(Chan, buf, n); |
| 48 | + } else { |
| 49 | + if (InitializeUART() != 0) |
| 50 | + return -1; |
| 51 | + if (WriteUARTN((void *)buf, n) != 0) |
| 52 | + return -1; |
| 53 | + } |
| 54 | + |
| 55 | + return n; |
| 56 | +} |
| 57 | + |
| 58 | +static void __attribute__((constructor)) __SYS_InitReport(void) |
| 59 | +{ |
| 60 | + fp = fwopen(NULL, WriteReport); |
| 61 | + setlinebuf(fp); |
| 62 | +} |
| 63 | + |
| 64 | +static void __attribute__((destructor)) __SYS_DeinitReport(void) |
| 65 | +{ |
| 66 | + fclose(fp); |
| 67 | + fp = NULL; |
| 68 | +} |
| 69 | + |
| 70 | +void __attribute__((weak)) SYS_Report(const char *msg, ...) |
| 71 | +{ |
| 72 | + va_list list; |
| 73 | + va_start(list, msg); |
| 74 | + vfprintf(fp, msg, list); |
| 75 | + va_end(list); |
| 76 | +} |
| 77 | + |
| 78 | +void __attribute__((weak)) SYS_Reportv(const char *msg, va_list list) |
| 79 | +{ |
| 80 | + vfprintf(fp, msg, list); |
| 81 | +} |
| 82 | + |
| 83 | +void SYS_EnableGecko(s32 chan, bool safe) |
| 84 | +{ |
| 85 | + flockfile(fp); |
| 86 | + Chan = chan; |
| 87 | + Safe = safe; |
| 88 | + funlockfile(fp); |
| 89 | +} |
0 commit comments