Skip to content

Commit ccaf420

Browse files
committed
Small fixes, including one to error handling that deconfuses error() fucntion used for two different things which led to confusing debug output.
1 parent 962803d commit ccaf420

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"type": "git",
1212
"url": "https://github.com/ropg/ezTime"
1313
},
14-
"version": "0.7.5",
14+
"version": "0.7.6",
1515
"framework": "arduino",
1616
"platforms": "*",
1717
"build": {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ezTime
2-
version=0.7.5
2+
version=0.7.6
33
author=Rop Gonggrijp
44
maintainer=Rop Gonggrijp
55
sentence=ezTime - pronounced "Easy Time" - is a very easy to use Arduino time and date library that provides NTP network time lookups, extensive timezone support, formatted time and date strings, user events, millisecond precision and more.

src/ezTime.cpp

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace {
7979
String _ntp_server = NTP_SERVER;
8080
#endif
8181

82-
void error(const ezError_t err) {
82+
void triggerError(const ezError_t err) {
8383
_last_error = err;
8484
if (_last_error) {
8585
err(F("ERROR: "));
@@ -132,9 +132,7 @@ namespace ezt {
132132
}
133133
}
134134

135-
136-
137-
ezError_t error(const bool reset /* = false */) {
135+
ezError_t error(const bool reset /* = false */) {
138136
ezError_t tmp = _last_error;
139137
if (reset) _last_error = NO_ERROR;
140138
return tmp;
@@ -434,15 +432,12 @@ namespace ezt {
434432
info(F(" ... "));
435433

436434
#ifndef EZTIME_ETHERNET
437-
if (WiFi.status() != WL_CONNECTED) { error(NO_NETWORK); return false; }
435+
if (WiFi.status() != WL_CONNECTED) { triggerError(NO_NETWORK); return false; }
438436
WiFiUDP udp;
439437
#else
440438
EthernetUDP udp;
441439
#endif
442440

443-
udp.flush();
444-
udp.begin(NTP_LOCAL_PORT);
445-
446441
// Send NTP packet
447442
byte buffer[NTP_PACKET_SIZE];
448443
memset(buffer, 0, NTP_PACKET_SIZE);
@@ -454,19 +449,21 @@ namespace ezt {
454449
buffer[12] = 'X'; // "kiss code", see RFC5905
455450
buffer[13] = 'E'; // (codes starting with 'X' are not interpreted)
456451
buffer[14] = 'Z';
457-
buffer[15] = 'T';
452+
buffer[15] = 'T';
453+
454+
udp.flush();
455+
udp.begin(NTP_LOCAL_PORT);
456+
unsigned long started = millis();
458457
udp.beginPacket(server.c_str(), 123); //NTP requests are to port 123
459458
udp.write(buffer, NTP_PACKET_SIZE);
460459
udp.endPacket();
461460

462461
// Wait for packet or return false with timed out
463-
unsigned long started = millis();
464-
uint16_t packetsize = 0;
465462
while (!udp.parsePacket()) {
466463
delay (1);
467464
if (millis() - started > NTP_TIMEOUT) {
468465
udp.stop();
469-
error(TIMEOUT);
466+
triggerError(TIMEOUT);
470467
return false;
471468
}
472469
}
@@ -504,7 +501,7 @@ namespace ezt {
504501
if (WiFi.status() != WL_CONNECTED) {
505502
info(F("Waiting for WiFi ... "));
506503
while (WiFi.status() != WL_CONNECTED) {
507-
if ( timeout && (millis() - start) / 1000 > timeout ) { error(TIMEOUT); return false;};
504+
if ( timeout && (millis() - start) / 1000 > timeout ) { triggerError(TIMEOUT); return false;};
508505
events();
509506
delay(25);
510507
}
@@ -515,7 +512,7 @@ namespace ezt {
515512
if (!_time_status != timeSet) {
516513
infoln(F("Waiting for time sync"));
517514
while (_time_status != timeSet) {
518-
if ( timeout && (millis() - start) / 1000 > timeout ) { error(TIMEOUT); return false;};
515+
if ( timeout && (millis() - start) / 1000 > timeout ) { triggerError(TIMEOUT); return false;};
519516
delay(250);
520517
events();
521518
}
@@ -551,7 +548,7 @@ Timezone::Timezone(const bool locked_to_UTC /* = false */) {
551548
}
552549

553550
bool Timezone::setPosix(const String posix) {
554-
if (_locked_to_UTC) { error(LOCKED_TO_UTC); return false; }
551+
if (_locked_to_UTC) { triggerError(LOCKED_TO_UTC); return false; }
555552
_posix = posix;
556553
#ifdef EZTIME_NETWORK_ENABLE
557554
_olsen = "";
@@ -776,32 +773,30 @@ String Timezone::getPosix() { return _posix; }
776773
bool Timezone::setLocation(const String location /* = "GeoIP" */) {
777774

778775
info(F("Timezone lookup for: "));
779-
infoln(location);
780-
if (_locked_to_UTC) { error(LOCKED_TO_UTC); return false; }
776+
info(location);
777+
info(F(" ... "));
778+
if (_locked_to_UTC) { triggerError(LOCKED_TO_UTC); return false; }
781779

782780
#ifndef EZTIME_ETHERNET
783-
if (WiFi.status() != WL_CONNECTED) { error(NO_NETWORK); return false; }
781+
if (WiFi.status() != WL_CONNECTED) { triggerError(NO_NETWORK); return false; }
784782
WiFiUDP udp;
785783
#else
786784
EthernetUDP udp;
787785
#endif
788-
786+
789787
udp.flush();
790788
udp.begin(TIMEZONED_LOCAL_PORT);
791-
789+
unsigned long started = millis();
792790
udp.beginPacket(TIMEZONED_REMOTE_HOST, TIMEZONED_REMOTE_PORT);
793791
udp.write((const uint8_t*)location.c_str(), location.length());
794792
udp.endPacket();
795793

796794
// Wait for packet or return false with timed out
797-
unsigned long started = millis();
798-
uint16_t packetsize = 0;
799795
while (!udp.parsePacket()) {
800796
delay (1);
801797
if (millis() - started > TIMEZONED_TIMEOUT) {
802798
udp.stop();
803-
error(TIMEOUT);
804-
udp.stop();
799+
triggerError(TIMEOUT);
805800
return false;
806801
}
807802
}
@@ -810,9 +805,12 @@ String Timezone::getPosix() { return _posix; }
810805
recv.reserve(60);
811806
while (udp.available()) recv += (char)udp.read();
812807
udp.stop();
808+
info(F("(round-trip "));
809+
info(millis() - started);
810+
info(F(" ms) "));
813811
if (recv.substring(0,6) == "ERROR ") {
814812
_server_error = recv.substring(6);
815-
error (SERVER_ERROR);
813+
error (SERVER_ERROR);
816814
return false;
817815
}
818816
if (recv.substring(0,3) == "OK ") {
@@ -852,7 +850,7 @@ String Timezone::getPosix() { return _posix; }
852850
#ifdef EZTIME_CACHE_EEPROM
853851
bool Timezone::setCache(const int16_t address) {
854852
eepromBegin();
855-
if (address + EEPROM_CACHE_LEN > eepromLength()) { error(CACHE_TOO_SMALL); return false; }
853+
if (address + EEPROM_CACHE_LEN > eepromLength()) { triggerError(CACHE_TOO_SMALL); return false; }
856854
_eeprom_address = address;
857855
eepromEnd();
858856
return setCache();
@@ -887,13 +885,13 @@ String Timezone::getPosix() { return _posix; }
887885

888886
#ifdef EZTIME_CACHE_EEPROM
889887
eepromBegin();
890-
if (_eeprom_address < 0) { error(NO_CACHE_SET); return; }
888+
if (_eeprom_address < 0) { triggerError(NO_CACHE_SET); return; }
891889
for (int16_t n = _eeprom_address; n < _eeprom_address + EEPROM_CACHE_LEN; n++) EEPROM.write(n, 0);
892890
eepromEnd();
893891
#endif
894892

895893
#ifdef EZTIME_CACHE_NVS
896-
if (_nvs_name == "" || _nvs_key == "") { error(NO_CACHE_SET); return; }
894+
if (_nvs_name == "" || _nvs_key == "") { triggerError(NO_CACHE_SET); return; }
897895
Preferences prefs;
898896
prefs.begin(_nvs_name.c_str(), false);
899897
if (delete_section) {
@@ -913,7 +911,7 @@ String Timezone::getPosix() { return _posix; }
913911
if (_eeprom_address < 0) return false;
914912

915913
info(F("Caching timezone data "));
916-
if (str.length() > MAX_CACHE_PAYLOAD) { error(CACHE_TOO_SMALL); return false; }
914+
if (str.length() > MAX_CACHE_PAYLOAD) { triggerError(CACHE_TOO_SMALL); return false; }
917915

918916
uint16_t last_byte = _eeprom_address + EEPROM_CACHE_LEN - 1;
919917
uint16_t addr = _eeprom_address;
@@ -984,7 +982,7 @@ String Timezone::getPosix() { return _posix; }
984982
bool Timezone::readCache(String &olsen, String &posix, uint8_t &months_since_jan_2018) {
985983

986984
#ifdef EZTIME_CACHE_EEPROM
987-
if (_eeprom_address < 0) { error(NO_CACHE_SET); return false; }
985+
if (_eeprom_address < 0) { triggerError(NO_CACHE_SET); return false; }
988986
eepromBegin();
989987
uint16_t last_byte = _eeprom_address + EEPROM_CACHE_LEN - 1;
990988

@@ -1049,7 +1047,7 @@ String Timezone::getPosix() { return _posix; }
10491047
#endif
10501048

10511049
#ifdef EZTIME_CACHE_NVS
1052-
if (_nvs_name == "" || _nvs_key == "") { error(NO_CACHE_SET); return false; }
1050+
if (_nvs_name == "" || _nvs_key == "") { triggerError(NO_CACHE_SET); return false; }
10531051

10541052
Preferences prefs;
10551053
prefs.begin(_nvs_name.c_str(), true);
@@ -1121,7 +1119,7 @@ uint8_t Timezone::setEvent(void (*function)(), time_t t /* = TIME_NOW */, const
11211119
return n + 1;
11221120
}
11231121
}
1124-
error(TOO_MANY_EVENTS);
1122+
triggerError(TOO_MANY_EVENTS);
11251123
return 0;
11261124
}
11271125

0 commit comments

Comments
 (0)