From 1567165b9d9a4442fe1522828eca9e31281bb6d7 Mon Sep 17 00:00:00 2001 From: Koi-3088 Date: Tue, 16 Feb 2021 01:53:02 -0600 Subject: [PATCH] Add Luxray-like network time advancing and resetting. --- sys-botbase/source/commands.c | 81 +++++++++++++++++++++++++++++------ sys-botbase/source/commands.h | 12 ++++-- sys-botbase/source/main.c | 52 ++++++++++++++++------ sys-botbase/source/ntp.c | 57 ++++++++++++++++++++++++ sys-botbase/source/ntp.h | 59 +++++++++++++++++++++++++ sys-botbase/source/util.c | 40 ++++++++--------- sys-botbase/source/util.h | 2 +- 7 files changed, 252 insertions(+), 51 deletions(-) create mode 100644 sys-botbase/source/ntp.c create mode 100644 sys-botbase/source/ntp.h diff --git a/sys-botbase/source/commands.c b/sys-botbase/source/commands.c index 7f4c1d6..a54ba5a 100644 --- a/sys-botbase/source/commands.c +++ b/sys-botbase/source/commands.c @@ -5,11 +5,16 @@ #include #include "commands.h" #include "util.h" +#include "time.h" +#include "ntp.h" Mutex actionLock; //Controller: bool bControllerIsInitialised = false; +time_t curTime = 0; +time_t origTime = 0; +int resetSkips = 0; HiddbgHdlsHandle controllerHandle = {0}; HiddbgHdlsDeviceInfo controllerDevice = {0}; HiddbgHdlsState controllerState = {0}; @@ -174,21 +179,21 @@ void peek(u64 offset, u64 size) free(out); } -void click(HidControllerKeys btn) +void click(HidNpadButton btn) { initController(); press(btn); svcSleepThread(buttonClickSleepTime * 1e+6L); release(btn); } -void press(HidControllerKeys btn) +void press(HidNpadButton btn) { initController(); controllerState.buttons |= btn; hiddbgSetHdlsState(controllerHandle, &controllerState); } -void release(HidControllerKeys btn) +void release(HidNpadButton btn) { initController(); controllerState.buttons &= ~btn; @@ -198,16 +203,16 @@ void release(HidControllerKeys btn) void setStickState(int side, int dxVal, int dyVal) { initController(); - if (side == JOYSTICK_LEFT) - { + if(side == 0) + { controllerState.analog_stick_l.x = dxVal; - controllerState.analog_stick_l.y = dyVal; - } - else - { - controllerState.analog_stick_r.x = dxVal; - controllerState.analog_stick_r.y = dyVal; - } + controllerState.analog_stick_l.y = dyVal; + } + if(side == 1) + { + controllerState.analog_stick_r.x = dxVal; + controllerState.analog_stick_r.y = dyVal; + } hiddbgSetHdlsState(controllerHandle, &controllerState); } @@ -233,4 +238,56 @@ void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold) } hiddbgUnsetTouchScreenAutoPilotState(); +} + +void dateSkip(int resetTimeAfterSkips, int resetNTP) +{ + if(origTime == 0) + { + Result ot = timeGetCurrentTime(TimeType_UserSystemClock, (u64*)&origTime); + if(R_FAILED(ot)) + fatalThrow(ot); + } + + Result tg = timeGetCurrentTime(TimeType_UserSystemClock, (u64*)&curTime); //Current system time + if(R_FAILED(tg)) + fatalThrow(tg); + + Result ts = timeSetCurrentTime(TimeType_NetworkSystemClock, (uint64_t)(curTime + 86400)); //Set new time + if(R_FAILED(ts)) + fatalThrow(ts); + + resetSkips++; + if(resetNTP == 0 && resetTimeAfterSkips != 0 && (resetTimeAfterSkips == resetSkips)) //Reset time after # of skips + resetTime(); + else if(resetNTP != 0 && resetTimeAfterSkips != 0 && (resetTimeAfterSkips == resetSkips)) + resetTimeNTP(); +} + +void resetTime() +{ + if(curTime == 0) + { + Result ct = timeGetCurrentTime(TimeType_UserSystemClock, (u64*)&curTime); //Current system time + if(R_FAILED(ct)) + fatalThrow(ct); + } + + resetSkips = 0; + struct tm currentTime = *localtime(&curTime); + struct tm timeReset = *localtime(&origTime); + timeReset.tm_hour = currentTime.tm_hour; + timeReset.tm_min = currentTime.tm_min; + timeReset.tm_sec = currentTime.tm_sec; + Result rt = timeSetCurrentTime(TimeType_NetworkSystemClock, mktime(&timeReset)); + if(R_FAILED(rt)) + fatalThrow(rt); +} + +void resetTimeNTP() +{ + resetSkips = 0; + Result ts = timeSetCurrentTime(TimeType_NetworkSystemClock, ntpGetTime()); + if(R_FAILED(ts)) + fatalThrow(ts); } \ No newline at end of file diff --git a/sys-botbase/source/commands.h b/sys-botbase/source/commands.h index 2cd0726..2b49983 100644 --- a/sys-botbase/source/commands.h +++ b/sys-botbase/source/commands.h @@ -1,4 +1,5 @@ #include +#include #define TOUCHPOLLMIN 15000000L // touch screen polling rate seems to be 15ms (no idea how to change) extern Handle debughandle; @@ -26,8 +27,11 @@ MetaData getMetaData(void); void poke(u64 offset, u64 size, u8* val); void peek(u64 offset, u64 size); -void click(HidControllerKeys btn); -void press(HidControllerKeys btn); -void release(HidControllerKeys btn); +void click(HidNpadButton btn); +void press(HidNpadButton btn); +void release(HidNpadButton btn); void setStickState(int side, int dxVal, int dyVal); -void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold); \ No newline at end of file +void dateSkip(int resetTimeAfterSkips, int resetNTP); +void resetTime(); +void resetTimeNTP(); +void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold); diff --git a/sys-botbase/source/main.c b/sys-botbase/source/main.c index 0676039..7b95cea 100644 --- a/sys-botbase/source/main.c +++ b/sys-botbase/source/main.c @@ -12,12 +12,14 @@ #include "args.h" #include "util.h" #include +#include "time.h" #define TITLE_ID 0x430000000000000B #define HEAP_SIZE 0x000540000 // we aren't an applet u32 __nx_applet_type = AppletType_None; +TimeServiceType __nx_time_service_type = TimeServiceType_System; // setup a fake heap (we don't need the heap anyway) char fake_heap[HEAP_SIZE]; @@ -40,6 +42,9 @@ void __appInit(void) rc = smInitialize(); if (R_FAILED(rc)) fatalThrow(rc); + rc = apmInitialize(); + if(R_FAILED(rc)) + fatalThrow(rc); if (hosversionGet() == 0) { rc = setsysInitialize(); if (R_SUCCEEDED(rc)) { @@ -58,19 +63,25 @@ void __appInit(void) fatalThrow(rc); rc = timeInitialize(); if (R_FAILED(rc)) - fatalThrow(rc); + { + timeExit(); + __nx_time_service_type = TimeServiceType_User; + rc = timeInitialize(); + if(R_FAILED(rc)) + fatalThrow(rc); + } rc = pmdmntInitialize(); - if (R_FAILED(rc)) { + if (R_FAILED(rc)) { fatalThrow(rc); - } + } rc = ldrDmntInitialize(); - if (R_FAILED(rc)) { - fatalThrow(rc); - } + if (R_FAILED(rc)) { + fatalThrow(rc); + } rc = pminfoInitialize(); - if (R_FAILED(rc)) { - fatalThrow(rc); - } + if (R_FAILED(rc)) { + fatalThrow(rc); + } rc = socketInitializeDefault(); if (R_FAILED(rc)) fatalThrow(rc); @@ -182,7 +193,7 @@ int argmain(int argc, char **argv) { if(argc != 2) return 0; - HidControllerKeys key = parseStringToButton(argv[1]); + HidNpadButton key = parseStringToButton(argv[1]); click(key); } @@ -191,7 +202,7 @@ int argmain(int argc, char **argv) { if(argc != 2) return 0; - HidControllerKeys key = parseStringToButton(argv[1]); + HidNpadButton key = parseStringToButton(argv[1]); press(key); } @@ -200,7 +211,7 @@ int argmain(int argc, char **argv) { if(argc != 2) return 0; - HidControllerKeys key = parseStringToButton(argv[1]); + HidNpadButton key = parseStringToButton(argv[1]); release(key); } @@ -212,9 +223,9 @@ int argmain(int argc, char **argv) int side = 0; if(!strcmp(argv[1], "LEFT")){ - side = JOYSTICK_LEFT; + side = 0; }else if(!strcmp(argv[1], "RIGHT")){ - side = JOYSTICK_RIGHT; + side = 1; }else{ return 0; } @@ -383,6 +394,19 @@ int argmain(int argc, char **argv) touch(state, count, TOUCHPOLLMIN * 2, true); } + if(!strcmp(argv[0], "daySkip")) + { + int resetTimeAfterSkips = parseStringToInt(argv[1]); + int resetNTP = parseStringToInt(argv[2]); + dateSkip(resetTimeAfterSkips, resetNTP); + } + + if(!strcmp(argv[0], "resetTime")) + resetTime(); + + if(!strcmp(argv[0], "resetTimeNTP")) + resetTimeNTP(); + return 0; } diff --git a/sys-botbase/source/ntp.c b/sys-botbase/source/ntp.c new file mode 100644 index 0000000..2bbd52f --- /dev/null +++ b/sys-botbase/source/ntp.c @@ -0,0 +1,57 @@ +/* This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +For more information, please refer to +Additionally, the ntp_packet struct uses code licensed under the BSD 3-clause. See LICENSE-THIRD-PARTY for more +information. */ + +#define _BSD_SOURCE + +#include "ntp.h" +#include +#include +#include +#include +#include +#include + +time_t ntpGetTime() { + static const char* SERVER_NAME = "0.pool.ntp.org"; + int sockfd = -1; + sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + struct hostent* server; + server = gethostbyname(SERVER_NAME); + + struct sockaddr_in serv_addr; + memset(&serv_addr, 0, sizeof(struct sockaddr_in)); + serv_addr.sin_family = AF_INET; + memcpy((char*)&serv_addr.sin_addr.s_addr, (char*)server->h_addr_list[0], 4); + serv_addr.sin_port = htons(123); + connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); + + ntp_packet packet; + memset(&packet, 0, sizeof(ntp_packet)); + packet.li_vn_mode = (0 << 6) | (4 << 3) | 3; // LI 0 | Client version 4 | Mode 3 + packet.txTm_s = htonl(NTP_TIMESTAMP_DELTA + time(NULL)); // Current networktime on the console + send(sockfd, (char*)&packet, sizeof(ntp_packet), 0); + (size_t)(recv(sockfd, (char*)&packet, sizeof(ntp_packet), 0)); + packet.txTm_s = ntohl(packet.txTm_s); + + return (time_t)(packet.txTm_s - NTP_TIMESTAMP_DELTA); +} \ No newline at end of file diff --git a/sys-botbase/source/ntp.h b/sys-botbase/source/ntp.h new file mode 100644 index 0000000..af8d02c --- /dev/null +++ b/sys-botbase/source/ntp.h @@ -0,0 +1,59 @@ +/* This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +For more information, please refer to +Additionally, the ntp_packet struct uses code licensed under the BSD 3-clause. See LICENSE-THIRD-PARTY for more +information. */ + +#pragma once + +#include + +#define NTP_TIMESTAMP_DELTA 2208988800ull + +/* ------------- BEGIN BSD 3-CLAUSE LICENSED CODE-------------------- */ +// https://www.cisco.com/c/en/us/about/press/internet-protocol-journal/back-issues/table-contents-58/154-ntp.html +// Struct adapted from https://github.com/lettier/ntpclient , see LICENSE-THIRD-PARTY for more information. +typedef struct { + uint8_t li_vn_mode; // li: two bits, leap indicator. vn: three bits, protocol version number. mode: three bits, + // client mode. + + uint8_t stratum; // Stratum level of the local clock. + uint8_t poll; // Maximum interval between successive messages. + uint8_t precision; // Precision of the local clock. + + uint32_t rootDelay; // Total round trip delay time. + uint32_t rootDispersion; // Max error allowed from primary clock source. + uint32_t refId; // Reference clock identifier. + + uint32_t refTm_s; // Reference time-stamp seconds. + uint32_t refTm_f; // Reference time-stamp fraction of a second. + + uint32_t origTm_s; // Originate time-stamp seconds. + uint32_t origTm_f; // Originate time-stamp fraction of a second. + + uint32_t rxTm_s; // Received time-stamp seconds. + uint32_t rxTm_f; // Received time-stamp fraction of a second. + + uint32_t txTm_s; // Transmit time-stamp seconds. + uint32_t txTm_f; // Transmit time-stamp fraction of a second. +} ntp_packet; +/* ------------- END BSD 3-CLAUSE LICENSED CODE-------------------- */ + +time_t ntpGetTime(); \ No newline at end of file diff --git a/sys-botbase/source/util.c b/sys-botbase/source/util.c index dc938cc..105808d 100644 --- a/sys-botbase/source/util.c +++ b/sys-botbase/source/util.c @@ -87,81 +87,81 @@ u8* parseStringToByteBuffer(char* arg, u64* size) return buffer; } -HidControllerKeys parseStringToButton(char* arg) +HidNpadButton parseStringToButton(char* arg) { if (strcmp(arg, "A") == 0) { - return KEY_A; + return HidNpadButton_A; } else if (strcmp(arg, "B") == 0) { - return KEY_B; + return HidNpadButton_B; } else if (strcmp(arg, "X") == 0) { - return KEY_X; + return HidNpadButton_X; } else if (strcmp(arg, "Y") == 0) { - return KEY_Y; + return HidNpadButton_Y; } else if (strcmp(arg, "RSTICK") == 0) { - return KEY_RSTICK; + return HidNpadButton_StickR; } else if (strcmp(arg, "LSTICK") == 0) { - return KEY_LSTICK; + return HidNpadButton_StickL; } else if (strcmp(arg, "L") == 0) { - return KEY_L; + return HidNpadButton_L; } else if (strcmp(arg, "R") == 0) { - return KEY_R; + return HidNpadButton_R; } else if (strcmp(arg, "ZL") == 0) { - return KEY_ZL; + return HidNpadButton_ZL; } else if (strcmp(arg, "ZR") == 0) { - return KEY_ZR; + return HidNpadButton_ZR; } else if (strcmp(arg, "PLUS") == 0) { - return KEY_PLUS; + return HidNpadButton_Plus; } else if (strcmp(arg, "MINUS") == 0) { - return KEY_MINUS; + return HidNpadButton_Minus; } else if (strcmp(arg, "DLEFT") == 0) { - return KEY_DLEFT; + return HidNpadButton_Left; } else if (strcmp(arg, "DUP") == 0) { - return KEY_DUP; + return HidNpadButton_Up; } else if (strcmp(arg, "DRIGHT") == 0) { - return KEY_DRIGHT; + return HidNpadButton_Right; } else if (strcmp(arg, "DDOWN") == 0) { - return KEY_DDOWN; + return HidNpadButton_Down; } else if (strcmp(arg, "HOME") == 0) { - return KEY_HOME; + return HiddbgNpadButton_Home; } else if (strcmp(arg, "CAPTURE") == 0) { - return KEY_CAPTURE; + return HiddbgNpadButton_Capture; } - return KEY_A; //I guess lol + return HidNpadButton_A; //I guess lol } Result capsscCaptureForDebug(void *buffer, size_t buffer_size, u64 *size) { diff --git a/sys-botbase/source/util.h b/sys-botbase/source/util.h index 01ba018..528d185 100644 --- a/sys-botbase/source/util.h +++ b/sys-botbase/source/util.h @@ -7,5 +7,5 @@ extern bool debugResultCodes; int setupServerSocket(); u64 parseStringToInt(char* arg); u8* parseStringToByteBuffer(char* arg, u64* size); -HidControllerKeys parseStringToButton(char* arg); +HidNpadButton parseStringToButton(char* arg); Result capsscCaptureForDebug(void *buffer, size_t buffer_size, u64 *size); //big thanks to Behemoth from the Reswitched Discord! \ No newline at end of file