Skip to content

Commit

Permalink
Add Luxray-like network time advancing and resetting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koi-3088 committed Feb 16, 2021
1 parent 31a1283 commit 1567165
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 51 deletions.
81 changes: 69 additions & 12 deletions sys-botbase/source/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
#include <math.h>
#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};
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
}
12 changes: 8 additions & 4 deletions sys-botbase/source/commands.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <switch.h>
#include <time.h>
#define TOUCHPOLLMIN 15000000L // touch screen polling rate seems to be 15ms (no idea how to change)

extern Handle debughandle;
Expand Down Expand Up @@ -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);
void dateSkip(int resetTimeAfterSkips, int resetNTP);
void resetTime();
void resetTimeNTP();
void touch(HidTouchState* state, u64 sequentialCount, u64 holdTime, bool hold);
52 changes: 38 additions & 14 deletions sys-botbase/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
#include "args.h"
#include "util.h"
#include <poll.h>
#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];
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
57 changes: 57 additions & 0 deletions sys-botbase/source/ntp.c
Original file line number Diff line number Diff line change
@@ -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 <https://unlicense.org>
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 <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <switch.h>
#include <sys/socket.h>

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);
}
59 changes: 59 additions & 0 deletions sys-botbase/source/ntp.h
Original file line number Diff line number Diff line change
@@ -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 <https://unlicense.org>
Additionally, the ntp_packet struct uses code licensed under the BSD 3-clause. See LICENSE-THIRD-PARTY for more
information. */

#pragma once

#include <time.h>

#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();
Loading

0 comments on commit 1567165

Please sign in to comment.