Skip to content

Commit b16ba07

Browse files
authored
Merge pull request #3 from pilotak/master
set_server option
2 parents 21381f9 + b04c260 commit b16ba07

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

NTPClient.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#include "ntp-client/NTPClient.h"
22
#include "mbed.h"
33

4-
const char* NTPClient::NIST_SERVER_ADDRESS = "2.pool.ntp.org";
5-
const int NTPClient::NIST_SERVER_PORT = 123;
6-
74
NTPClient::NTPClient(NetworkInterface *iface) {
8-
this->iface = iface;
5+
this->iface = iface;
6+
7+
_nist_server_address = NTP_DEFULT_NIST_SERVER_ADDRESS;
8+
_nist_server_port = NTP_DEFULT_NIST_SERVER_PORT;
9+
}
10+
11+
void NTPClient::set_server(char* server, int port){
12+
_nist_server_address = server;
13+
_nist_server_port = port;
914
}
1015

1116
time_t NTPClient::get_timestamp(int timeout) {
@@ -14,14 +19,14 @@ time_t NTPClient::get_timestamp(int timeout) {
1419
int ntp_recv_values[12] = {0};
1520

1621
SocketAddress nist;
17-
int ret_gethostbyname = iface->gethostbyname(NIST_SERVER_ADDRESS, &nist);
22+
int ret_gethostbyname = iface->gethostbyname(_nist_server_address, &nist);
1823

1924
if (ret_gethostbyname < 0) {
2025
// Network error on DNS lookup
2126
return ret_gethostbyname;
2227
}
2328

24-
nist.set_port(NIST_SERVER_PORT);
29+
nist.set_port(_nist_server_port);
2530

2631
memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
2732
ntp_send_values[0] = '\x1b';

NTPClient.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#include "mbed.h"
22

3+
#define NTP_DEFULT_NIST_SERVER_ADDRESS "2.pool.ntp.org"
4+
#define NTP_DEFULT_NIST_SERVER_PORT 123
5+
36
class NTPClient {
47
public:
58
NTPClient(NetworkInterface *iface);
9+
void set_server(char* server, int port);
610
time_t get_timestamp(int timeout = 15000);
711

8-
static const char* NIST_SERVER_ADDRESS;
9-
static const int NIST_SERVER_PORT;
10-
1112
private:
1213
NetworkInterface *iface;
1314
uint32_t ntohl(uint32_t num);
15+
char* _nist_server_address;
16+
int _nist_server_port;
1417
};

0 commit comments

Comments
 (0)