From: ljsebald <ljs...@us...> - 2023-11-30 20:03:32
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "A pseudo Operating System for the Dreamcast.". The branch, master has been updated via 25be1fb3ae7ae6267106bac673c2297c659c8930 (commit) via 594915b3dcefe438b70a3ce5688f070522381db7 (commit) via bc5d28aa769e44dcced5e0d27a7e8391d8f74e60 (commit) via 5c5a00eeaa1c26bc9d9d773ae0c380484c6899e5 (commit) from e1e1bb214155a01edafbda8e43bde747787955b0 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 25be1fb3ae7ae6267106bac673c2297c659c8930 Merge: e1e1bb2 594915b Author: Lawrence Sebald <ljs...@us...> Date: Thu Nov 30 15:03:02 2023 -0500 Merge pull request #392 from KallistiOS/ntp_example Add network example for setting system clock via NTP commit 594915b3dcefe438b70a3ce5688f070522381db7 Author: Lawrence Sebald <ljs...@us...> Date: Thu Nov 30 15:02:05 2023 -0500 Update examples/dreamcast/network/ntp/ntp.c Fix spacing. commit bc5d28aa769e44dcced5e0d27a7e8391d8f74e60 Author: darcagn <da...@pr...> Date: Thu Nov 30 13:59:18 2023 -0600 Apply suggestions from code review Co-authored-by: Lawrence Sebald <ljs...@us...> commit 5c5a00eeaa1c26bc9d9d773ae0c380484c6899e5 Author: darc <da...@pr...> Date: Wed Nov 29 14:57:18 2023 -0600 Add network example for setting system clock via NTP ----------------------------------------------------------------------- Summary of changes: examples/dreamcast/network/Makefile | 3 + .../dreamcast/network/{udpecho6 => ntp}/Makefile | 12 +-- examples/dreamcast/network/ntp/ntp.c | 119 +++++++++++++++++++++ 3 files changed, 128 insertions(+), 6 deletions(-) copy examples/dreamcast/network/{udpecho6 => ntp}/Makefile (74%) create mode 100644 examples/dreamcast/network/ntp/ntp.c diff --git a/examples/dreamcast/network/Makefile b/examples/dreamcast/network/Makefile index ddd39b1..b9627dc 100644 --- a/examples/dreamcast/network/Makefile +++ b/examples/dreamcast/network/Makefile @@ -12,6 +12,7 @@ all: $(KOS_MAKE) -C dns-client $(KOS_MAKE) -C httpd $(KOS_MAKE) -C isp-settings + $(KOS_MAKE) -C ntp clean: $(KOS_MAKE) -C basic clean @@ -21,6 +22,7 @@ clean: $(KOS_MAKE) -C dns-client clean $(KOS_MAKE) -C httpd clean $(KOS_MAKE) -C isp-settings clean + $(KOS_MAKE) -C ntp clean dist: $(KOS_MAKE) -C basic dist @@ -30,3 +32,4 @@ dist: $(KOS_MAKE) -C dns-client dist $(KOS_MAKE) -C httpd dist $(KOS_MAKE) -C isp-settings dist + $(KOS_MAKE) -C ntp dist diff --git a/examples/dreamcast/network/udpecho6/Makefile b/examples/dreamcast/network/ntp/Makefile similarity index 74% copy from examples/dreamcast/network/udpecho6/Makefile copy to examples/dreamcast/network/ntp/Makefile index 4aa9fc6..07741d8 100644 --- a/examples/dreamcast/network/udpecho6/Makefile +++ b/examples/dreamcast/network/ntp/Makefile @@ -1,13 +1,14 @@ +# KallistiOS ##version## +# +# examples/dreamcast/network/ntp/Makefile +# Copyright (C) 2023 Eric Fradella # -# Basic KallistiOS skeleton / test program -# (c)2001 Megan Potter -# # Put the filename of the output binary here -TARGET = echo.elf +TARGET = ntp.elf # List all of your C files here, but change the extension to ".o" -OBJS = echo.o +OBJS = ntp.o all: rm-elf $(TARGET) @@ -28,4 +29,3 @@ run: $(TARGET) dist: $(TARGET) -rm -f $(OBJS) $(KOS_STRIP) $(TARGET) - diff --git a/examples/dreamcast/network/ntp/ntp.c b/examples/dreamcast/network/ntp/ntp.c new file mode 100644 index 0000000..a706928 --- /dev/null +++ b/examples/dreamcast/network/ntp/ntp.c @@ -0,0 +1,119 @@ +/* KallistiOS ##version## + * + * ntp.c + * Copyright (C) 2023 Eric Fradella + * + * This example demonstrates how to set the Dreamcast's + * real-time clock to current UTC time via an NTP server. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <netdb.h> + +#include <arch/arch.h> +#include <arch/rtc.h> +#include <kos/dbgio.h> +#include <kos/net.h> + +#define NTP_PORT "123" +#define NTP_SERVER "us.pool.ntp.org" +#define NTP_DELTA 2208988800ULL + +#define ERR_EXIT() { thd_sleep(2000); exit(EXIT_FAILURE); } + +KOS_INIT_FLAGS(INIT_DEFAULT | INIT_NET); + +/* Structure for 48-byte NTP packet */ +typedef struct ntp_packet { + uint8_t leap_ver_mode; /* First 2 bits are leap indicator, next + three bits NTP version, last 3 bits mode */ + uint8_t stratum; + uint8_t poll_interval; + uint8_t precision; + uint32_t root_delay; + uint32_t root_dispersion; + uint32_t ref_id; + uint32_t ref_time_s; + uint32_t ref_time_f; + uint32_t orig_time_s; + uint32_t orig_time_f; + uint32_t rcv_time_s; + uint32_t rcv_time_f; + uint32_t trns_time_s; + uint32_t trns_time_f; +} ntp_packet_t; + +int main(int argc, char **argv) { + ntp_packet_t packet; + int sockfd; + struct addrinfo *ai; + time_t ntp_time, dc_time; + + /* Set the framebuffer as the output device for dbgio. */ + dbgio_dev_select("fb"); + + /* Create NTP packet and clear it */ + memset(&packet, 0, sizeof(ntp_packet_t)); + + /* Leave leap indicator blank, set version number to 4,and + set client mode to 3. 0x23 = 00 100 011 = 0, 4, 3 */ + packet.leap_ver_mode = 0x23; + + /* Create a new UDP socket */ + if((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + printf("Error opening socket!\n"); + ERR_EXIT(); + } + + /* Retrieve IP address for our specified hostname */ + if(getaddrinfo(NTP_SERVER, NTP_PORT, NULL, &ai)) { + printf("Error resolving host!\n"); + ERR_EXIT(); + } + + if(connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { + printf("Error connecting to server!\n"); + ERR_EXIT(); + } + + freeaddrinfo(ai); + + /* Send the NTP packet we constructed */ + if(write(sockfd, &packet, sizeof(ntp_packet_t)) < 0) { + printf("Error writing to socket!\n"); + ERR_EXIT(); + } + + /* Receive the packet back from the server, + now filled out with the current time */ + if(read(sockfd, &packet, sizeof(ntp_packet_t)) < 0) { + printf("Error reading response from socket!\n"); + ERR_EXIT(); + } + + /* Grab time from the structure, and subtract 70 years to convert + from NTP's 1900 epoch to Unix time's 1970 epoch */ + ntp_time = (ntohl(packet.trns_time_s) - NTP_DELTA); + printf("The current NTP time is...\n %s\n", ctime(&ntp_time)); + + /* Print the current system time */ + dc_time = rtc_unix_secs(); + printf("Dreamcast system time is...\n %s\n", ctime(&dc_time)); + + /* Set the system time to the NTP time and read it back */ + printf("Setting Dreamcast clock's time to NTP time...\n\n"); + rtc_set_unix_secs(ntp_time); + dc_time = rtc_unix_secs(); + printf("Dreamcast system time is now...\n %s\n", ctime(&dc_time)); + + /* Wait 10 seconds for the user to see what's on the screen before we clear + it during the exit back to the loader */ + thd_sleep(10 * 1000); + + return 0; +} + hooks/post-receive -- A pseudo Operating System for the Dreamcast. |