Skip to content

Commit

Permalink
Small fixes, including one to error handling that deconfuses error() …
Browse files Browse the repository at this point in the history
…fucntion used for two different things which led to confusing debug output.
  • Loading branch information
ropg committed Oct 12, 2018
1 parent 962803d commit ccaf420
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "git",
"url": "https://github.com/ropg/ezTime"
},
"version": "0.7.5",
"version": "0.7.6",
"framework": "arduino",
"platforms": "*",
"build": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ezTime
version=0.7.5
version=0.7.6
author=Rop Gonggrijp
maintainer=Rop Gonggrijp
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.
Expand Down
62 changes: 30 additions & 32 deletions src/ezTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace {
String _ntp_server = NTP_SERVER;
#endif

void error(const ezError_t err) {
void triggerError(const ezError_t err) {
_last_error = err;
if (_last_error) {
err(F("ERROR: "));
Expand Down Expand Up @@ -132,9 +132,7 @@ namespace ezt {
}
}



ezError_t error(const bool reset /* = false */) {
ezError_t error(const bool reset /* = false */) {
ezError_t tmp = _last_error;
if (reset) _last_error = NO_ERROR;
return tmp;
Expand Down Expand Up @@ -434,15 +432,12 @@ namespace ezt {
info(F(" ... "));

#ifndef EZTIME_ETHERNET
if (WiFi.status() != WL_CONNECTED) { error(NO_NETWORK); return false; }
if (WiFi.status() != WL_CONNECTED) { triggerError(NO_NETWORK); return false; }
WiFiUDP udp;
#else
EthernetUDP udp;
#endif

udp.flush();
udp.begin(NTP_LOCAL_PORT);

// Send NTP packet
byte buffer[NTP_PACKET_SIZE];
memset(buffer, 0, NTP_PACKET_SIZE);
Expand All @@ -454,19 +449,21 @@ namespace ezt {
buffer[12] = 'X'; // "kiss code", see RFC5905
buffer[13] = 'E'; // (codes starting with 'X' are not interpreted)
buffer[14] = 'Z';
buffer[15] = 'T';
buffer[15] = 'T';

udp.flush();
udp.begin(NTP_LOCAL_PORT);
unsigned long started = millis();
udp.beginPacket(server.c_str(), 123); //NTP requests are to port 123
udp.write(buffer, NTP_PACKET_SIZE);
udp.endPacket();

// Wait for packet or return false with timed out
unsigned long started = millis();
uint16_t packetsize = 0;
while (!udp.parsePacket()) {
delay (1);
if (millis() - started > NTP_TIMEOUT) {
udp.stop();
error(TIMEOUT);
triggerError(TIMEOUT);
return false;
}
}
Expand Down Expand Up @@ -504,7 +501,7 @@ namespace ezt {
if (WiFi.status() != WL_CONNECTED) {
info(F("Waiting for WiFi ... "));
while (WiFi.status() != WL_CONNECTED) {
if ( timeout && (millis() - start) / 1000 > timeout ) { error(TIMEOUT); return false;};
if ( timeout && (millis() - start) / 1000 > timeout ) { triggerError(TIMEOUT); return false;};
events();
delay(25);
}
Expand All @@ -515,7 +512,7 @@ namespace ezt {
if (!_time_status != timeSet) {
infoln(F("Waiting for time sync"));
while (_time_status != timeSet) {
if ( timeout && (millis() - start) / 1000 > timeout ) { error(TIMEOUT); return false;};
if ( timeout && (millis() - start) / 1000 > timeout ) { triggerError(TIMEOUT); return false;};
delay(250);
events();
}
Expand Down Expand Up @@ -551,7 +548,7 @@ Timezone::Timezone(const bool locked_to_UTC /* = false */) {
}

bool Timezone::setPosix(const String posix) {
if (_locked_to_UTC) { error(LOCKED_TO_UTC); return false; }
if (_locked_to_UTC) { triggerError(LOCKED_TO_UTC); return false; }
_posix = posix;
#ifdef EZTIME_NETWORK_ENABLE
_olsen = "";
Expand Down Expand Up @@ -776,32 +773,30 @@ String Timezone::getPosix() { return _posix; }
bool Timezone::setLocation(const String location /* = "GeoIP" */) {

info(F("Timezone lookup for: "));
infoln(location);
if (_locked_to_UTC) { error(LOCKED_TO_UTC); return false; }
info(location);
info(F(" ... "));
if (_locked_to_UTC) { triggerError(LOCKED_TO_UTC); return false; }

#ifndef EZTIME_ETHERNET
if (WiFi.status() != WL_CONNECTED) { error(NO_NETWORK); return false; }
if (WiFi.status() != WL_CONNECTED) { triggerError(NO_NETWORK); return false; }
WiFiUDP udp;
#else
EthernetUDP udp;
#endif

udp.flush();
udp.begin(TIMEZONED_LOCAL_PORT);

unsigned long started = millis();
udp.beginPacket(TIMEZONED_REMOTE_HOST, TIMEZONED_REMOTE_PORT);
udp.write((const uint8_t*)location.c_str(), location.length());
udp.endPacket();

// Wait for packet or return false with timed out
unsigned long started = millis();
uint16_t packetsize = 0;
while (!udp.parsePacket()) {
delay (1);
if (millis() - started > TIMEZONED_TIMEOUT) {
udp.stop();
error(TIMEOUT);
udp.stop();
triggerError(TIMEOUT);
return false;
}
}
Expand All @@ -810,9 +805,12 @@ String Timezone::getPosix() { return _posix; }
recv.reserve(60);
while (udp.available()) recv += (char)udp.read();
udp.stop();
info(F("(round-trip "));
info(millis() - started);
info(F(" ms) "));
if (recv.substring(0,6) == "ERROR ") {
_server_error = recv.substring(6);
error (SERVER_ERROR);
error (SERVER_ERROR);
return false;
}
if (recv.substring(0,3) == "OK ") {
Expand Down Expand Up @@ -852,7 +850,7 @@ String Timezone::getPosix() { return _posix; }
#ifdef EZTIME_CACHE_EEPROM
bool Timezone::setCache(const int16_t address) {
eepromBegin();
if (address + EEPROM_CACHE_LEN > eepromLength()) { error(CACHE_TOO_SMALL); return false; }
if (address + EEPROM_CACHE_LEN > eepromLength()) { triggerError(CACHE_TOO_SMALL); return false; }
_eeprom_address = address;
eepromEnd();
return setCache();
Expand Down Expand Up @@ -887,13 +885,13 @@ String Timezone::getPosix() { return _posix; }

#ifdef EZTIME_CACHE_EEPROM
eepromBegin();
if (_eeprom_address < 0) { error(NO_CACHE_SET); return; }
if (_eeprom_address < 0) { triggerError(NO_CACHE_SET); return; }
for (int16_t n = _eeprom_address; n < _eeprom_address + EEPROM_CACHE_LEN; n++) EEPROM.write(n, 0);
eepromEnd();
#endif

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

info(F("Caching timezone data "));
if (str.length() > MAX_CACHE_PAYLOAD) { error(CACHE_TOO_SMALL); return false; }
if (str.length() > MAX_CACHE_PAYLOAD) { triggerError(CACHE_TOO_SMALL); return false; }

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

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

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

#ifdef EZTIME_CACHE_NVS
if (_nvs_name == "" || _nvs_key == "") { error(NO_CACHE_SET); return false; }
if (_nvs_name == "" || _nvs_key == "") { triggerError(NO_CACHE_SET); return false; }

Preferences prefs;
prefs.begin(_nvs_name.c_str(), true);
Expand Down Expand Up @@ -1121,7 +1119,7 @@ uint8_t Timezone::setEvent(void (*function)(), time_t t /* = TIME_NOW */, const
return n + 1;
}
}
error(TOO_MANY_EVENTS);
triggerError(TOO_MANY_EVENTS);
return 0;
}

Expand Down

0 comments on commit ccaf420

Please sign in to comment.