| 1 |
/* |
| 2 |
* Copyright (c) 2023 Valtteri Koskivuori <vkoskiv@gmail.com> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef _MACNTP_H |
| 18 |
#define _MACNTP_H |
| 19 |
|
| 20 |
enum MacNTPError { |
| 21 |
Success = 0, |
| 22 |
MacTCPInitFailed, |
| 23 |
MacTCPNotFound, |
| 24 |
InvalidURL, |
| 25 |
BadNtpStructSize, |
| 26 |
UDPCreateFailed, |
| 27 |
UDPSendFailed, |
| 28 |
UDPRcvFailed, |
| 29 |
InvalidNTPResponse, |
| 30 |
OriginTimestampMismatch, |
| 31 |
DNSResolveFailed, |
| 32 |
GestaltMissing, |
| 33 |
// Below only for SetSystemTime |
| 34 |
ClockWriteFailed, |
| 35 |
ClockReadFailed, |
| 36 |
UDPRcvTimedOut, |
| 37 |
PacketParamNull |
| 38 |
}; |
| 39 |
|
| 40 |
struct ntp_ts { |
| 41 |
unsigned long upper; /* Seconds since 1904-01-01 */ |
| 42 |
unsigned long lower; /* FIXME: These values seem to be wrong */ |
| 43 |
}; |
| 44 |
|
| 45 |
struct ntp_packet { |
| 46 |
unsigned char li_vn_mode; |
| 47 |
unsigned char stratum; |
| 48 |
unsigned char poll; |
| 49 |
char precision; |
| 50 |
|
| 51 |
unsigned long root_delay; |
| 52 |
unsigned long root_dispersion; |
| 53 |
unsigned long reference_id; |
| 54 |
|
| 55 |
struct ntp_ts reference_timestamp; |
| 56 |
struct ntp_ts origin_timestamp; |
| 57 |
struct ntp_ts receive_timestamp; |
| 58 |
struct ntp_ts transmit_timestamp; |
| 59 |
}; |
| 60 |
|
| 61 |
typedef long minutes_t; |
| 62 |
|
| 63 |
enum MacNTPError MacNTPFetchTime(char *ntp_url, struct ntp_packet *packet, OSErr *supplemental, minutes_t utc_offset); |
| 64 |
|
| 65 |
// For convenience, you can just pass ntp_packet from above to set the system time |
| 66 |
// Optionally, you can care about the contents of ntp_packet and do things with it. |
| 67 |
enum MacNTPError MacNTPSetSystemTime(struct ntp_packet *ntp, minutes_t utc_offset); |
| 68 |
|
| 69 |
#endif |