Station API
Diff: NTPClient.h
- Revision:
- 2:a9d1a9c92927
- Parent:
- 1:a22e390c70b3
- Child:
- 5:3df13e2e928e
diff -r a22e390c70b3 -r a9d1a9c92927 NTPClient.h --- a/NTPClient.h Mon Dec 12 02:33:21 2011 +0000 +++ b/NTPClient.h Mon Dec 12 11:41:24 2011 +0000 @@ -22,11 +22,54 @@ #include "SimpleSocket.h" +/** + * NTP Client interface to adjust RTC + */ class NTPClient { public: - NTPClient(char *server = "pool.ntp.org", float timeout = 3.0) - : server(server), timeout(timeout) {} - + /** + * creates an NTPClient object + * + * @param server NTP server host name + * @param timeout max wait time for connection + */ + NTPClient(char *server = "pool.ntp.org", float timeout = 3.0) : timeout(timeout) { + strncpy(this->server, server, sizeof(this->server)); + } + + /** + * creates an NTPClient object from a config file + * + * @param filename name for the configuration file containing server host name and timeout + * @param verbose if true display debug info + */ + static NTPClient create(char *filename, bool verbose = false) { + char server[32] = "pool.ntp.org"; + float timeout = 3.0; + + if (filename) { + char path[32]; + LocalFileSystem local("local"); + sprintf(path, "/local/%s", filename); + if (FILE *fp = fopen(path, "r")) { + Utils::fgetValues(fp, "ntp-server:%s", server); + Utils::fgetValues(fp, "ntp-timeout:%f", &timeout); + fclose(fp); + if (verbose) { + printf("ntp-server:%s\n", server); + printf("ntp-timeout:%2.1f\n", timeout); + } + } + } + + return NTPClient(server, timeout); + } + + /** + * adjusts RTC + * + * @returns true if succeeded + */ bool setTime() { Timer timer; timer.start(); @@ -51,6 +94,6 @@ } private: - char *server; + char server[32]; float timeout; }; \ No newline at end of file