From 7ba2186ac10cc40e580ae63366b46a9aa629ef41 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 5 Mar 2020 01:30:07 +0100 Subject: [PATCH 01/10] timesync.cpp: minor code beautifications --- src/timesync.cpp | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/timesync.cpp b/src/timesync.cpp index b6a41ea2..97da7171 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -20,13 +20,15 @@ You may use timesync option 2 if you do not want or cannot accept this. // Local logging tag static const char TAG[] = __FILE__; +static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO); +#define WRAP(v, top) (v++ > top ? 0 : v) + // timesync option 1: use external timeserver (for LoRAWAN < 1.0.3) #if (TIME_SYNC_LORASERVER) && (HAS_LORA) static TaskHandle_t timeSyncReqTask = NULL; static bool timeSyncPending = false; -static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO); static uint8_t sample_idx = 0; static uint32_t timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0}; @@ -89,10 +91,7 @@ void IRAM_ATTR process_timesync_req(void *taskparameter) { timesync_timestamp[sample_idx][timesync_tx]; // increment and maybe wrap around seqNo, keeping it in time port range - time_sync_seqNo++; - if (time_sync_seqNo > TIMEREQUEST_MAX_SEQNO) { - time_sync_seqNo = 0; - } + WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO); // increment index for timestamp array sample_idx++; @@ -243,12 +242,15 @@ void timesync_init() { // send time request message void send_timesync_req(void) { - LMIC_requestNetworkTime(process_timesync_req, NULL); + LMIC_requestNetworkTime(process_timesync_req, &time_sync_seqNo); } -void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess) { - // Explicit conversion from void* to uint32_t* to avoid compiler errors - time_t *pUserUTCTime = (time_t *)pVoidUserUTCTime; +void IRAM_ATTR process_timesync_req(void *pUserData, int flagSuccess) { + // Explicit conversion from void* to uint8_t* to avoid compiler errors + uint8_t *seqNo = (uint8_t *)pUserData; + + // mask application irq to ensure accurate timing + mask_user_IRQ(); // A struct that will be populated by LMIC_getNetworkTimeReference. // It contains the following fields: @@ -258,32 +260,35 @@ void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess) { // the gateway received the time request lmic_time_reference_t lmicTime; - if (flagSuccess != 1) { + uint32_t networkTimeSec; + uint16_t requestDelaymSec; + + if ((flagSuccess != 1) || (time_sync_seqNo != *seqNo)) { ESP_LOGW(TAG, "LoRaWAN network did not answer time request"); - return; + goto Finish; } // Populate lmic_time_reference flagSuccess = LMIC_getNetworkTimeReference(&lmicTime); if (flagSuccess != 1) { ESP_LOGW(TAG, "LoRaWAN time request failed"); - return; + goto Finish; } - // mask application irq to ensure accurate timing - mask_user_IRQ(); - - // Update networkUTCTime, considering the difference between GPS and UTC time - uint32_t networkTimeSec = lmicTime.tNetwork + GPS_UTC_DIFF; + // Calculate UTCTime, considering the difference between GPS and UTC time + networkTimeSec = lmicTime.tNetwork + GPS_UTC_DIFF; // Add delay between the instant the time was transmitted and the current time - uint16_t requestDelaymSec = - osticks2ms(os_getTime() - lmicTime.tLocal); - + requestDelaymSec = osticks2ms(os_getTime() - lmicTime.tLocal); // Update system time with time read from the network setMyTime(networkTimeSec, requestDelaymSec, _lora); +Finish: + // end of time critical section: release app irq lock unmask_user_IRQ(); + // increment and maybe wrap around seqNo, keeping it in time port range + WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO); + } // user_request_network_time_callback -#endif // TIME_SYNC_LORAWAN \ No newline at end of file +#endif // TIME_SYNC_LORAWAN From f1b13f11fadb03eb122d7fc0f97199a221100b26 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 6 Mar 2020 19:23:51 +0100 Subject: [PATCH 02/10] timeserver: removed timezone format --- src/Timeserver/Nodered-Timeserver.json | 288 ++++++++++++------------- src/Timeserver/timeserver.java | 19 +- 2 files changed, 154 insertions(+), 153 deletions(-) diff --git a/src/Timeserver/Nodered-Timeserver.json b/src/Timeserver/Nodered-Timeserver.json index 085b132d..0f324ba7 100644 --- a/src/Timeserver/Nodered-Timeserver.json +++ b/src/Timeserver/Nodered-Timeserver.json @@ -1,61 +1,4 @@ [ - { - "id": "9b4f492d.fbfd18", - "type": "change", - "z": "449c1517.e25f4c", - "name": "Payload", - "rules": [ - { - "t": "change", - "p": "topic", - "pt": "msg", - "from": "up", - "fromt": "str", - "to": "down", - "tot": "str" - }, - { - "t": "set", - "p": "payload.confirmed", - "pt": "msg", - "to": "false", - "tot": "bool" - }, - { - "t": "set", - "p": "payload.schedule", - "pt": "msg", - "to": "replace", - "tot": "str" - }, - { - "t": "move", - "p": "payload", - "pt": "msg", - "to": "payload.payload_raw", - "tot": "msg" - }, - { - "t": "move", - "p": "port", - "pt": "msg", - "to": "payload.port", - "tot": "msg" - } - ], - "action": "", - "property": "", - "from": "", - "to": "", - "reg": false, - "x": 220, - "y": 520, - "wires": [ - [ - "53a85e2c.2728d" - ] - ] - }, { "id": "9c105726.613a58", "type": "mqtt in", @@ -72,19 +15,6 @@ ] ] }, - { - "id": "1c9a7438.6e38ec", - "type": "mqtt out", - "z": "449c1517.e25f4c", - "name": "send", - "topic": "", - "qos": "", - "retain": "", - "broker": "2a15ab6f.ab2244", - "x": 710, - "y": 520, - "wires": [] - }, { "id": "113ef524.57edeb", "type": "json", @@ -126,22 +56,6 @@ ] ] }, - { - "id": "90e76b02.6298f8", - "type": "json", - "z": "449c1517.e25f4c", - "name": "Convert", - "property": "payload", - "action": "", - "pretty": false, - "x": 560, - "y": 520, - "wires": [ - [ - "1c9a7438.6e38ec" - ] - ] - }, { "id": "d6f27e8e.93242", "type": "base64", @@ -158,28 +72,13 @@ ] ] }, - { - "id": "53a85e2c.2728d", - "type": "base64", - "z": "449c1517.e25f4c", - "name": "Encode", - "action": "", - "property": "payload.payload_raw", - "x": 400, - "y": 520, - "wires": [ - [ - "90e76b02.6298f8" - ] - ] - }, { "id": "15980d22.6f4663", "type": "comment", "z": "449c1517.e25f4c", - "name": "LoRaWAN Timeserver v1.21", + "name": "LoRaWAN Timeserver v1.3", "info": "PLEASE NOTE: There is a patent filed for the time sync algorithm used in the\ncode of this file. The shown implementation example is covered by the\nrepository's licencse, but you may not be eligible to deploy the applied\nalgorithm in applications without granted license by the patent holder.", - "x": 160, + "x": 150, "y": 47, "wires": [] }, @@ -188,7 +87,7 @@ "type": "function", "z": "449c1517.e25f4c", "name": "Timeserver Logic", - "func": "/* LoRaWAN Timeserver\n\nconstruct 7 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n1 sequence number (taken from node's time_sync_req)\n2 timezone in 15 minutes steps\n3..6 current second (from epoch time 1970)\n7 1/250ths fractions of current second\n\n*/\n\nfunction timecompare(a, b) {\n \n const timeA = a.time;\n const timeB = b.time;\n\n let comparison = 0;\n if (timeA > timeB) {\n comparison = 1;\n } else if (timeA < timeB) {\n comparison = -1;\n }\n return comparison;\n}\n\nlet confidence = 2000; // max millisecond diff gateway time to server time\n\n// guess if we have received a valid time_sync_req command\nif (msg.payload.payload_raw.length != 1)\n return;\n\nvar deviceMsg = { payload: msg.payload.dev_id };\nvar seqNo = msg.payload.payload_raw[0];\nvar seqNoMsg = { payload: seqNo };\nvar gateway_list = msg.payload.metadata.gateways;\n\n// filter all gateway timestamps that have milliseconds part (which we assume have a \".\")\nvar gateways = gateway_list.filter(function (element) {\n return (element.time.includes(\".\"));\n});\n\nvar gateway_time = gateways.map(gw => {\n return {\n time: new Date(gw.time),\n eui: gw.gtw_id,\n }\n });\nvar server_time = new Date(msg.payload.metadata.time);\n\n// validate all gateway timestamps against lorawan server_time (which is assumed to be recent)\nvar gw_timestamps = gateway_time.filter(function (element) {\n return ((element.time > (server_time - confidence) && element.time <= server_time));\n});\n\n// if no timestamp left, we have no valid one and exit\nif (gw_timestamps.length === 0) {\n var notavailMsg = { payload: \"n/a\" };\n var notimeMsg = { payload: 0xff }; \n var buf2 = Buffer.alloc(1);\n msg.payload = new Buffer(buf2.fill(0xff));\n msg.port = 9; // Paxcounter TIMEPORT\n return [notavailMsg, notavailMsg, deviceMsg, seqNoMsg, msg];}\n\n// sort time array in ascending order to find most recent timestamp for time answer\ngw_timestamps.sort(timecompare);\n\nvar timestamp = gw_timestamps[0].time;\nvar eui = gw_timestamps[0].eui;\nvar offset = server_time - timestamp;\n\nvar seconds = Math.floor(timestamp/1000);\nvar fractions = (timestamp % 1000) / 4;\n\nlet buf = new ArrayBuffer(7);\nnew DataView(buf).setUint8(0, seqNo);\n// Timezone (in 15min steps)\nvar timezone = 8; // CET = UTC+2h\nnew DataView(buf).setUint8(1, timezone);\nnew DataView(buf).setUint32(2, seconds);\nnew DataView(buf).setUint8(6, fractions);\n\nmsg.payload = new Buffer(new Uint8Array(buf));\nmsg.port = 9; // Paxcounter TIMEPORT\nvar euiMsg = { payload: eui };\nvar offsetMsg = { payload: offset };\n\nreturn [euiMsg, offsetMsg, deviceMsg, seqNoMsg, msg];", + "func": "/* LoRaWAN Timeserver\n\nVERSION: 1.3\n\nconstruct 6 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n1 sequence number (taken from node's time_sync_req)\n2..5 current second (from GPS epoch starting 1980)\n6 1/250ths fractions of current second\n\n*/\n\nfunction timecompare(a, b) {\n \n const timeA = a.time;\n const timeB = b.time;\n\n let comparison = 0;\n if (timeA > timeB) {\n comparison = 1;\n } else if (timeA < timeB) {\n comparison = -1;\n }\n return comparison;\n}\n\nlet confidence = 1000; // max millisecond diff gateway time to server time\n\n// guess if we have received a valid time_sync_req command\nif (msg.payload.payload_raw.length != 1)\n return;\n\nvar deviceMsg = { payload: msg.payload.dev_id };\nvar seqNo = msg.payload.payload_raw[0];\nvar seqNoMsg = { payload: seqNo };\nvar gateway_list = msg.payload.metadata.gateways;\n\n// filter all gateway timestamps that have milliseconds part (which we assume have a \".\")\nvar gateways = gateway_list.filter(function (element) {\n return (element.time.includes(\".\"));\n});\n\nvar gateway_time = gateways.map(gw => {\n return {\n time: new Date(gw.time),\n eui: gw.gtw_id,\n }\n });\nvar server_time = new Date(msg.payload.metadata.time);\n\n// validate all gateway timestamps against lorawan server_time (which is assumed to be recent)\nvar gw_timestamps = gateway_time.filter(function (element) {\n return ((element.time > (server_time - confidence) && element.time <= server_time));\n});\n\n// if no timestamp left, we have no valid one and exit\nif (gw_timestamps.length === 0) {\n var notavailMsg = { payload: \"n/a\" };\n var notimeMsg = { payload: 0xff }; \n var buf2 = Buffer.alloc(1);\n msg.payload = new Buffer(buf2.fill(0xff));\n msg.port = 9; // Paxcounter TIMEPORT\n return [notavailMsg, notavailMsg, deviceMsg, seqNoMsg, msg];}\n\n// sort time array in ascending order to find most recent timestamp for time answer\ngw_timestamps.sort(timecompare);\n\nvar timestamp = gw_timestamps[0].time;\nvar eui = gw_timestamps[0].eui;\nvar offset = server_time - timestamp;\n\nvar seconds = Math.floor(timestamp/1000);\nvar fractions = (timestamp % 1000) / 4;\n\nlet buf = new ArrayBuffer(6);\nnew DataView(buf).setUint8(0, seqNo);\n// Timezone (in 15min steps) -> deprecated\n//var timezone = 8; // CET = UTC+2h\n//new DataView(buf).setUint8(1, timezone);\nnew DataView(buf).setUint32(2, seconds);\nnew DataView(buf).setUint8(6, fractions);\n\nmsg.payload = new Buffer(new Uint8Array(buf));\nmsg.port = 9; // Paxcounter TIMEPORT\nvar euiMsg = { payload: eui };\nvar offsetMsg = { payload: offset };\n\nreturn [euiMsg, offsetMsg, deviceMsg, seqNoMsg, msg];", "outputs": 5, "noerr": 0, "x": 330, @@ -251,32 +150,6 @@ "y": 307, "wires": [] }, - { - "id": "de908e66.b6fd3", - "type": "ui_gauge", - "z": "449c1517.e25f4c", - "name": "Timeserver offset", - "group": "edb7cc8d.a3817", - "order": 2, - "width": 0, - "height": 0, - "gtype": "gage", - "title": "Offset gateway to server", - "label": "milliseconds", - "format": "{{value}}", - "min": 0, - "max": "2000", - "colors": [ - "#00b500", - "#e6e600", - "#ca3838" - ], - "seg1": "", - "seg2": "", - "x": 690, - "y": 387, - "wires": [] - }, { "id": "6aeb3720.a89618", "type": "ui_text", @@ -309,6 +182,147 @@ ] ] }, + { + "id": "cc245719.3c4cd8", + "type": "debug", + "z": "449c1517.e25f4c", + "name": "", + "active": true, + "tosidebar": true, + "console": false, + "tostatus": false, + "complete": "true", + "x": 860, + "y": 140, + "wires": [] + }, + { + "id": "9b4f492d.fbfd18", + "type": "change", + "z": "449c1517.e25f4c", + "name": "Payload", + "rules": [ + { + "t": "change", + "p": "topic", + "pt": "msg", + "from": "up", + "fromt": "str", + "to": "down", + "tot": "str" + }, + { + "t": "set", + "p": "payload.confirmed", + "pt": "msg", + "to": "false", + "tot": "bool" + }, + { + "t": "set", + "p": "payload.schedule", + "pt": "msg", + "to": "replace", + "tot": "str" + }, + { + "t": "move", + "p": "payload", + "pt": "msg", + "to": "payload.payload_raw", + "tot": "msg" + }, + { + "t": "move", + "p": "port", + "pt": "msg", + "to": "payload.port", + "tot": "msg" + } + ], + "action": "", + "property": "", + "from": "", + "to": "", + "reg": false, + "x": 220, + "y": 520, + "wires": [ + [ + "53a85e2c.2728d" + ] + ] + }, + { + "id": "1c9a7438.6e38ec", + "type": "mqtt out", + "z": "449c1517.e25f4c", + "name": "send", + "topic": "", + "qos": "", + "retain": "", + "broker": "2a15ab6f.ab2244", + "x": 710, + "y": 520, + "wires": [] + }, + { + "id": "90e76b02.6298f8", + "type": "json", + "z": "449c1517.e25f4c", + "name": "Convert", + "property": "payload", + "action": "", + "pretty": false, + "x": 560, + "y": 520, + "wires": [ + [ + "1c9a7438.6e38ec" + ] + ] + }, + { + "id": "53a85e2c.2728d", + "type": "base64", + "z": "449c1517.e25f4c", + "name": "Encode", + "action": "", + "property": "payload.payload_raw", + "x": 400, + "y": 520, + "wires": [ + [ + "90e76b02.6298f8" + ] + ] + }, + { + "id": "de908e66.b6fd3", + "type": "ui_gauge", + "z": "449c1517.e25f4c", + "name": "Timeserver offset", + "group": "edb7cc8d.a3817", + "order": 2, + "width": 0, + "height": 0, + "gtype": "gage", + "title": "Offset gateway to server", + "label": "milliseconds", + "format": "{{value}}", + "min": 0, + "max": "2000", + "colors": [ + "#00b500", + "#e6e600", + "#ca3838" + ], + "seg1": "", + "seg2": "", + "x": 690, + "y": 387, + "wires": [] + }, { "id": "d5a35bab.44cb18", "type": "ui_text", @@ -341,20 +355,6 @@ "y": 467, "wires": [] }, - { - "id": "cc245719.3c4cd8", - "type": "debug", - "z": "449c1517.e25f4c", - "name": "", - "active": true, - "tosidebar": true, - "console": false, - "tostatus": false, - "complete": "true", - "x": 860, - "y": 140, - "wires": [] - }, { "id": "2a15ab6f.ab2244", "type": "mqtt-broker", diff --git a/src/Timeserver/timeserver.java b/src/Timeserver/timeserver.java index 0896b23f..66e4f94a 100644 --- a/src/Timeserver/timeserver.java +++ b/src/Timeserver/timeserver.java @@ -1,12 +1,13 @@ /* LoRaWAN Timeserver -construct 7 byte timesync_answer from gateway timestamp and node's time_sync_req +VERSION: 1.3 + +construct 6 byte timesync_answer from gateway timestamp and node's time_sync_req byte meaning 1 sequence number (taken from node's time_sync_req) -2 timezone in 15 minutes steps -3..6 current second (from epoch time 1970) -7 1/250ths fractions of current second +2..5 current second (from GPS epoch starting 1980) +6 1/250ths fractions of current second */ @@ -24,7 +25,7 @@ function timecompare(a, b) { return comparison; } -let confidence = 2000; // max millisecond diff gateway time to server time +let confidence = 1000; // max millisecond diff gateway time to server time // guess if we have received a valid time_sync_req command if (msg.payload.payload_raw.length != 1) @@ -72,11 +73,11 @@ var offset = server_time - timestamp; var seconds = Math.floor(timestamp/1000); var fractions = (timestamp % 1000) / 4; -let buf = new ArrayBuffer(7); +let buf = new ArrayBuffer(6); new DataView(buf).setUint8(0, seqNo); -// Timezone (in 15min steps) -var timezone = 8; // CET = UTC+2h -new DataView(buf).setUint8(1, timezone); +// Timezone (in 15min steps) -> deprecated +//var timezone = 8; // CET = UTC+2h +//new DataView(buf).setUint8(1, timezone); new DataView(buf).setUint32(2, seconds); new DataView(buf).setUint8(6, fractions); From 73a612779b2e517d8e12a547ccba3a330e7934e4 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 6 Mar 2020 19:24:58 +0100 Subject: [PATCH 03/10] deeply reworked timesync code --- include/lorawan.h | 30 ++++---- include/timesync.h | 15 ++-- src/lorawan.cpp | 179 ++++++++++++++++++++++--------------------- src/main.cpp | 2 +- src/timekeeper.cpp | 32 ++++---- src/timesync.cpp | 187 +++++++++++++++++++++++---------------------- 6 files changed, 233 insertions(+), 212 deletions(-) diff --git a/include/lorawan.h b/include/lorawan.h index d57a69fe..c04b0341 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -20,13 +20,6 @@ extern TaskHandle_t lmicTask, lorasendTask; -// table of LORAWAN MAC commands -typedef struct { - const uint8_t opcode; - const char cmdname[20]; - const uint8_t params; -} mac_t; - esp_err_t lora_stack_init(bool do_join); void lora_setupForNetwork(bool preJoin); void lmictask(void *pvParameters); @@ -36,19 +29,28 @@ void get_hard_deveui(uint8_t *pdeveui); void os_getDevKey(u1_t *buf); void os_getArtEui(u1_t *buf); void os_getDevEui(u1_t *buf); -void showLoraKeys(void); void lora_send(void *pvParameters); void lora_enqueuedata(MessageBuffer_t *message); void lora_queuereset(void); void IRAM_ATTR myEventCallback(void *pUserData, ev_t ev); -void IRAM_ATTR myRxCallback(void *pUserData, uint8_t port, - const uint8_t *pMsg, size_t nMsg); -//void IRAM_ATTR myTxCallback(void *pUserData, int fSuccess); -void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], - const uint8_t tablesize); -//u1_t os_getBattLevel(void); +void IRAM_ATTR myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, + size_t nMsg); +void IRAM_ATTR myTxCallback(void *pUserData, int fSuccess); const char *getSfName(rps_t rps); const char *getBwName(rps_t rps); const char *getCrName(rps_t rps); +// u1_t os_getBattLevel(void); + +#if VERBOSE +// a table for storage of LORAWAN MAC commands +typedef struct { + const uint8_t opcode; + const char cmdname[20]; + const uint8_t params; +} mac_t; +void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], + const uint8_t tablesize); +void showLoraKeys(void); +#endif #endif \ No newline at end of file diff --git a/include/timesync.h b/include/timesync.h index 40356847..656ce41c 100644 --- a/include/timesync.h +++ b/include/timesync.h @@ -17,15 +17,18 @@ enum timesync_t { timesync_rx, gwtime_sec, gwtime_msec, - gwtime_tzsec, no_of_timestamps }; void timesync_init(void); -void send_timesync_req(void); -int recv_timesync_ans(const uint8_t buf[], uint8_t buf_len); -void store_timestamp(uint32_t timestamp, timesync_t timestamp_type); -void IRAM_ATTR process_timesync_req(void *taskparameter); -void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess); +void timesync_sendReq(void); +void timesync_storeReq(uint32_t timestamp, timesync_t timestamp_type); +void IRAM_ATTR timesync_processReq(void *taskparameter); + +#if (TIME_SYNC_LORASERVER) +int recv_timeserver_ans(const uint8_t buf[], uint8_t buf_len); +#elif (TIME_SYNC_LORAWAN) +void IRAM_ATTR DevTimeAns_Cb(void *pUserData, int flagSuccess); +#endif #endif diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 853832aa..515e5ac9 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -23,6 +23,7 @@ RTC_NOINIT_ATTR int RTCseqnoUp, RTCseqnoDn; QueueHandle_t LoraSendQueue; TaskHandle_t lmicTask = NULL, lorasendTask = NULL; +#if (VERBOSE) // table of LORAWAN MAC messages sent by the network to the device // format: opcode, cmdname (max 19 chars), #bytes params // source: LoRaWAN 1.1 Specification (October 11, 2017) @@ -46,6 +47,10 @@ static const mac_t MACup_table[] = { {0x0B, "RekeyInd", 1}, {0x0C, "ADRParamSetupAns", 0}, {0x0D, "DeviceTimeReq", 0}, {0x0F, "RejoinParamSetupAns", 1}}; +static const uint8_t MACdn_tSize = sizeof(MACdn_table) / sizeof(MACdn_table[0]), + MACup_tSize = sizeof(MACup_table) / sizeof(MACup_table[0]); +#endif // VERBOSE + class MyHalConfig_t : public Arduino_LMIC::HalConfiguration_t { public: @@ -265,18 +270,16 @@ void lora_send(void *pvParameters) { SendBuffer.MessageSize, (cfg.countermode & 0x02))) { - // switch (LMIC_sendWithCallback_strict( - // SendBuffer.MessagePort, SendBuffer.Message, - // SendBuffer.MessageSize, (cfg.countermode & 0x02), myTxCallback, - // &SendBuffer.MessagePort)) { - case LMIC_ERROR_SUCCESS: + // save current Fcnt to RTC RAM + RTCseqnoUp = LMIC.seqnoUp; + RTCseqnoDn = LMIC.seqnoDn; #if (TIME_SYNC_LORASERVER) // if last packet sent was a timesync request, store TX timestamp if (SendBuffer.MessagePort == TIMEPORT) // store LMIC time when we started transmit of timesync request - store_timestamp(osticks2ms(os_getTime()), timesync_tx); + timesync_storeReq(osticks2ms(os_getTime()), timesync_tx); #endif ESP_LOGI(TAG, "%d byte(s) sent to LORA", SendBuffer.MessageSize); @@ -429,6 +432,15 @@ void myEventCallback(void *pUserData, ev_t ev) { // process current event message switch (ev) { + + case EV_TXCOMPLETE: + // -> processed in lora_send() + break; + + case EV_RXCOMPLETE: + // -> processed in myRxCallback() + break; + case EV_JOINING: // do the network-specific setup prior to join. lora_setupForNetwork(true); @@ -439,12 +451,6 @@ void myEventCallback(void *pUserData, ev_t ev) { lora_setupForNetwork(false); break; - case EV_TXCOMPLETE: - // save current Fcnt to RTC RAM - RTCseqnoUp = LMIC.seqnoUp; - RTCseqnoDn = LMIC.seqnoDn; - break; - case EV_JOIN_TXCOMPLETE: // replace descriptor from library with more descriptive term snprintf(lmic_event_msg, LMIC_EVENTMSG_LEN, "%-16s", "JOIN_WAIT"); @@ -462,113 +468,79 @@ void myEventCallback(void *pUserData, ev_t ev) { ESP_LOGD(TAG, "%s", lmic_event_msg); } -// receive message handler +// event EV_RXCOMPLETE message handler void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, size_t nMsg) { - // display type of received data + // calculate if we have encapsulated MAC commands + uint8_t nMac = pMsg - &LMIC.frame[0]; + if (port != MACPORT) + --nMac; + + // display amount of received data if (nMsg) ESP_LOGI(TAG, "Received %u byte(s) of payload on port %u", nMsg, port); else if (port) ESP_LOGI(TAG, "Received empty message on port %u", port); - // list MAC messages, if any - uint8_t nMac = pMsg - &LMIC.frame[0]; - if (port != MACPORT) - --nMac; - if (nMac) { - ESP_LOGI(TAG, "%u byte(s) downlink MAC commands", nMac); - // NOT WORKING YET - // whe need to unwrap the MAC command from LMIC.frame here - // mac_decode(LMIC.frame, nMac, MACdn_table, sizeof(MACdn_table) / - // sizeof(MACdn_table[0])); - } - - if (LMIC.pendMacLen) { - ESP_LOGI(TAG, "%u byte(s) uplink MAC commands", LMIC.pendMacLen); - mac_decode(LMIC.pendMacData, LMIC.pendMacLen, MACup_table, - sizeof(MACup_table) / sizeof(MACup_table[0])); - } - switch (port) { - // ignore mac messages +// decode unpiggybacked mac messages if we want to print those +#if (VERBOSE) case MACPORT: - break; + // decode downlink MAC commands + // mac_decode(LMIC.frame, nMac, MACdn_table, MACdn_tSize); + // decode uplink MAC commands + if (LMIC.pendMacLen) + mac_decode(LMIC.pendMacData, LMIC.pendMacLen, MACup_table, MACup_tSize); + break; // do not fallthrough to default, we are done +#endif // rcommand received -> call interpreter case RCMDPORT: rcommand(pMsg, nMsg); - break; + +// timeserver answer -> call timesync processor +#if (TIME_SYNC_LORASERVER) + case TIMEPORT: + // store LMIC time when we received the timesync answer + timesync_storeReq(osticks2ms(os_getTime()), timesync_rx); + // get and store gwtime from payload + recv_timeserver_ans(pMsg, nMsg); +#endif default: -#if (TIME_SYNC_LORASERVER) - // valid timesync answer -> call timesync processor - if (port == TIMEPORT) { - // store LMIC time when we received the timesync answer - store_timestamp(osticks2ms(os_getTime()), timesync_rx); - // get and store gwtime from payload - recv_timesync_ans(pMsg, nMsg); - break; +// decode any piggybacked downlink MAC commands +#if (VERBOSE) + if (nMac) { + // decoding yet to come + // -> whe need to unwrap the MAC command from LMIC.frame before calling + // mac_decode(LMIC.frame, nMac, MACdn_table, MACdn_tSize); } -#endif +#endif // VERBOSE - // unknown port -> display info - ESP_LOGI(TAG, "Received data on unsupported port %u", port); break; } // switch } /* -// event TRANSMIT COMPLETE message handler +// event EV_TXCOMPLETE message handler void myTxCallback(void *pUserData, int fSuccess) { - uint8_t *const sendport = (uint8_t *)pUserData; + uint8_t *const pMsg = (uint8_t *)pUserData; + // LMIC did successful transmit data if (fSuccess) { - // LMIC did tx on *sendport -> nothing yet to do here + RTCseqnoUp = LMIC.seqnoUp; + RTCseqnoDn = LMIC.seqnoDn; } else { - // LMIC could not tx on *sendport -> error handling yet to come + // LMIC could not transmit data + // -> error handling yet to come } } */ -// decode LORAWAN MAC message -void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], - const uint8_t tablesize) { - - if (!cmdlen) - return; - - uint8_t foundcmd[cmdlen], cursor = 0; - - while (cursor < cmdlen) { - - int i = tablesize; // number of commands in table - - while (i--) { - if (cmd[cursor] == table[i].opcode) { // lookup command in opcode table - cursor++; // strip 1 byte opcode - if ((cursor + table[i].params) <= cmdlen) { - memmove(foundcmd, cmd + cursor, - table[i].params); // strip opcode from cmd array - cursor += table[i].params; - ESP_LOGD(TAG, "MAC command %s", table[i].cmdname); - } else - ESP_LOGD(TAG, "MAC command 0x%02X with missing parameter(s)", - table[i].opcode); - break; // command found -> exit table lookup loop - } // end of command validation - } // end of command table lookup loop - if (i < 0) { // command not found -> skip it - ESP_LOGD(TAG, "Unknown MAC command 0x%02X", cmd[cursor]); - cursor++; - } - } // command parsing loop - -} // mac_decode() - const char *getSfName(rps_t rps) { const char *const t[] = {"FSK", "SF7", "SF8", "SF9", "SF10", "SF11", "SF12", "SF?"}; @@ -611,4 +583,41 @@ u1_t os_getBattLevel() { } // getBattLevel() */ +#if (VERBOSE) +// decode LORAWAN MAC message +void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], + const uint8_t tablesize) { + + if (!cmdlen) + return; + + uint8_t foundcmd[cmdlen], cursor = 0; + + while (cursor < cmdlen) { + + int i = tablesize; // number of commands in table + + while (i--) { + if (cmd[cursor] == table[i].opcode) { // lookup command in opcode table + cursor++; // strip 1 byte opcode + if ((cursor + table[i].params) <= cmdlen) { + memmove(foundcmd, cmd + cursor, + table[i].params); // strip opcode from cmd array + cursor += table[i].params; + ESP_LOGD(TAG, "MAC command %s", table[i].cmdname); + } else + ESP_LOGD(TAG, "MAC command 0x%02X with missing parameter(s)", + table[i].opcode); + break; // command found -> exit table lookup loop + } // end of command validation + } // end of command table lookup loop + if (i < 0) { // command not found -> skip it + ESP_LOGD(TAG, "Unknown MAC command 0x%02X", cmd[cursor]); + cursor++; + } + } // command parsing loop + +} // mac_decode() +#endif // VERBOSE + #endif // HAS_LORA \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 60e57127..ae8560b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -457,7 +457,7 @@ void setup() { clock_init(); #endif -#if (TIME_SYNC_LORASERVER) +#if (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN) timesync_init(); // create loraserver time sync task #endif diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 0af5e956..cf7f3bac 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -28,6 +28,21 @@ void calibrateTime(void) { time_t t = 0; uint16_t t_msec = 0; + // kick off asychronous lora timesync if we have +#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN) + timesync_sendReq(); +#endif + +// has RTC -> fallback to RTC time +#ifdef HAS_RTC + t = get_rtctime(); + if (t) { + timeSource = _rtc; + goto finish; + } +#endif + +// no RTC -> fallback to GPS time #if (HAS_GPS) // fetch recent time from last NMEA record t = fetch_gpsTime(&t_msec); @@ -37,21 +52,8 @@ void calibrateTime(void) { } #endif -// kick off asychronous lora timesync if we have -#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN) - send_timesync_req(); -#endif - -// no time from GPS -> fallback to RTC time while trying lora sync -#ifdef HAS_RTC - t = get_rtctime(); - if (t) { - timeSource = _rtc; - goto finish; - } -#endif - - goto finish; + // no local time source -> don't set time + return; finish: diff --git a/src/timesync.cpp b/src/timesync.cpp index 97da7171..dbe62574 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -11,46 +11,59 @@ You may use timesync option 2 if you do not want or cannot accept this. */ -#include "timesync.h" +#if (HAS_LORA) -#if (TIME_SYNC_LORASERVER) && (TIME_SYNC_LORAWAN) && (HAS_LORA) +#if (TIME_SYNC_LORASERVER) && (TIME_SYNC_LORAWAN) #error Duplicate timesync method selected. You must select either LORASERVER or LORAWAN timesync. #endif +#include "timesync.h" + +#define WRAP(v, top) (v++ > top ? 0 : v) + // Local logging tag static const char TAG[] = __FILE__; -static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO); -#define WRAP(v, top) (v++ > top ? 0 : v) - -// timesync option 1: use external timeserver (for LoRAWAN < 1.0.3) - -#if (TIME_SYNC_LORASERVER) && (HAS_LORA) - -static TaskHandle_t timeSyncReqTask = NULL; static bool timeSyncPending = false; -static uint8_t sample_idx = 0; -static uint32_t timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0}; +static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO), + sample_idx; +static uint16_t timestamp_msec; +static uint32_t timestamp_sec, + timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps]; +static TaskHandle_t timeSyncReqTask = NULL; -// send time request message -void send_timesync_req(void) { +// create task for timeserver handshake processing, called from main.cpp +void timesync_init() { + xTaskCreatePinnedToCore(timesync_processReq, // task function + "timesync_req", // name of task + 2048, // stack size of task + (void *)1, // task parameter + 3, // priority of the task + &timeSyncReqTask, // task handle + 1); // CPU core +} + +// kickoff asnychronous timesync handshake +void timesync_sendReq(void) { // if a timesync handshake is pending then exit if (timeSyncPending) return; - // else unblock timesync task + // else clear array and unblock timesync task else { - ESP_LOGI(TAG, "[%0.3f] Timeserver sync request started", millis() / 1000.0); + ESP_LOGI(TAG, "[%0.3f] Timeserver sync request seqNo#%d started", + millis() / 1000.0, time_sync_seqNo); + sample_idx = 0; xTaskNotifyGive(timeSyncReqTask); } } -// task for sending time sync requests -void IRAM_ATTR process_timesync_req(void *taskparameter) { +// task for processing time sync request +void IRAM_ATTR timesync_processReq(void *taskparameter) { uint32_t rcv_seq_no = TIMEREQUEST_FINISH, time_offset_ms; // this task is an endless loop, waiting in blocked mode, until it is - // unblocked by send_timesync_req(). It then waits to be notified from + // unblocked by timesync_sendReq(). It then waits to be notified from // recv_timesync_ans(), which is called from RX callback in lorawan.cpp, each // time a timestamp from timeserver arrived. @@ -68,14 +81,26 @@ void IRAM_ATTR process_timesync_req(void *taskparameter) { vTaskDelay(pdMS_TO_TICKS(5000)); } - // trigger and collect timestamp samples + // clear timestamp array + timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0}; + + // trigger and collect samples in timestamp array for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) { - // send timesync request to timeserver + +// send timesync request to timeserver or networkserver +#if (TIME_SYNC_LORASERVER) + // timesync option 1: use external timeserver (for LoRAWAN < 1.0.3) payload.reset(); payload.addByte(time_sync_seqNo); SendPayload(TIMEPORT, prio_high); +#elif (TIME_SYNC_LORAWAN) + // timesync option 2: use LoRAWAN network time (requires LoRAWAN >= 1.0.3) + LMIC_requestNetworkTime(DevTimeAns_Cb, &time_sync_seqNo); + // open a receive window to trigger DevTimeAns + LMIC_sendAlive(); +#endif - // wait until recv_timesync_ans() signals a timestamp was received + // wait until a timestamp was received while (rcv_seq_no != time_sync_seqNo) { if (xTaskNotifyWait(0x00, ULONG_MAX, &rcv_seq_no, pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == @@ -86,26 +111,27 @@ void IRAM_ATTR process_timesync_req(void *taskparameter) { } } - // calculate time diff from collected timestamps + // calculate time diff from received timestamp time_offset_ms += timesync_timestamp[sample_idx][timesync_rx] - timesync_timestamp[sample_idx][timesync_tx]; // increment and maybe wrap around seqNo, keeping it in time port range WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO); - // increment index for timestamp array sample_idx++; - // if last cycle, send finish char for closing timesync handshake, - // else wait until time has come for next cycle + // if last cycle, finish after, else pause until next cycle if (i < TIME_SYNC_SAMPLES - 1) { // wait for next cycle vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000)); - } else { // finish timesync handshake + } else { +#if (TIME_SYNC_LORASERVER) + // send finish char for closing timesync handshake payload.reset(); payload.addByte(TIMEREQUEST_FINISH); SendPayload(RCMDPORT, prio_high); // open a receive window to get last time_sync_answer instantly LMIC_sendAlive(); +#endif } } // end of for loop to collect timestamp samples @@ -137,8 +163,8 @@ void IRAM_ATTR process_timesync_req(void *taskparameter) { } // infinite while(1) } -// called from lorawan.cpp -void store_timestamp(uint32_t timestamp, timesync_t timestamp_type) { +// store incoming timestamps +void timesync_storeReq(uint32_t timestamp, timesync_t timestamp_type) { ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: timestamp(t%d)=%d", millis() / 1000.0, time_sync_seqNo, sample_idx, timestamp_type, timestamp); @@ -146,17 +172,18 @@ void store_timestamp(uint32_t timestamp, timesync_t timestamp_type) { timesync_timestamp[sample_idx][timestamp_type] = timestamp; } -// process timeserver timestamp answer, called by myRxCallback() in lorawan.cpp -int recv_timesync_ans(const uint8_t buf[], const uint8_t buf_len) { +#if (TIME_SYNC_LORASERVER) +// evaluate timerserver's timestamp answer, called by myRxCallback() in +// lorawan.cpp +int recv_timeserver_ans(const uint8_t buf[], const uint8_t buf_len) { /* - parse 7 byte timesync_answer: + parse 6 byte timesync_answer: byte meaning 1 sequence number (taken from node's time_sync_req) - 2 timezone in 15 minutes steps - 3..6 current second (from epoch time 1970) - 7 1/250ths fractions of current second + 2..5 current second (from epoch time 1970) + 6 1/250ths fractions of current second */ // if no timesync handshake is pending then exit @@ -164,12 +191,12 @@ int recv_timesync_ans(const uint8_t buf[], const uint8_t buf_len) { return 0; // failure // extract 1 byte timerequest sequence number from payload - uint8_t seq_no = buf[0]; + uint8_t seqNo = buf[0]; buf++; // if no time is available or spurious buffer then exit if (buf_len != TIME_SYNC_FRAME_LENGTH) { - if (seq_no == 0xff) + if (seqNo == 0xff) ESP_LOGI(TAG, "[%0.3f] Timeserver error: no confident time available", millis() / 1000.0); else @@ -183,11 +210,6 @@ int recv_timesync_ans(const uint8_t buf[], const uint8_t buf_len) { // pointers to 4 bytes msb order uint32_t timestamp_sec, *timestamp_ptr; - // extract 1 byte containing timezone offset - // one step being 15min * 60sec = 900sec - uint32_t timestamp_tzsec = buf[0] * 900; // timezone offset in secs - buf++; - // extract 4 bytes containing gateway time in UTC seconds since unix // epoch and convert it to uint32_t, octet order is big endian timestamp_ptr = (uint32_t *)buf; @@ -204,15 +226,14 @@ int recv_timesync_ans(const uint8_t buf[], const uint8_t buf_len) { // we guess timepoint is recent if it is newer than code compile date if (timeIsValid(t)) { ESP_LOGD(TAG, "[%0.3f] Timesync request seq#%d rcvd at %0.3f", - millis() / 1000.0, seq_no, osticks2ms(os_getTime()) / 1000.0); + millis() / 1000.0, seqNo, osticks2ms(os_getTime()) / 1000.0); // store time received from gateway - store_timestamp(timestamp_sec, gwtime_sec); - store_timestamp(timestamp_msec, gwtime_msec); - store_timestamp(timestamp_tzsec, gwtime_tzsec); + timesync_storeReq(timestamp_sec, gwtime_sec); + timesync_storeReq(timestamp_msec, gwtime_msec); // inform processing task - xTaskNotify(timeSyncReqTask, seq_no, eSetBits); + xTaskNotify(timeSyncReqTask, seqNo, eSetBits); return 1; // success } else { @@ -223,29 +244,9 @@ int recv_timesync_ans(const uint8_t buf[], const uint8_t buf_len) { } } -// create task for timeserver handshake processing, called from main.cpp -void timesync_init() { - xTaskCreatePinnedToCore(process_timesync_req, // task function - "timesync_req", // name of task - 2048, // stack size of task - (void *)1, // task parameter - 3, // priority of the task - &timeSyncReqTask, // task handle - 1); // CPU core -} +#elif (TIME_SYNC_LORAWAN) -#endif - -// timesync option 2: use LoRAWAN network time (requires LoRAWAN >= 1.0.3) - -#if (TIME_SYNC_LORAWAN) && (HAS_LORA) - -// send time request message -void send_timesync_req(void) { - LMIC_requestNetworkTime(process_timesync_req, &time_sync_seqNo); -} - -void IRAM_ATTR process_timesync_req(void *pUserData, int flagSuccess) { +void IRAM_ATTR DevTimeAns_Cb(void *pUserData, int flagSuccess) { // Explicit conversion from void* to uint8_t* to avoid compiler errors uint8_t *seqNo = (uint8_t *)pUserData; @@ -260,35 +261,39 @@ void IRAM_ATTR process_timesync_req(void *pUserData, int flagSuccess) { // the gateway received the time request lmic_time_reference_t lmicTime; - uint32_t networkTimeSec; - uint16_t requestDelaymSec; + if (flagSuccess != 1) { + ESP_LOGW(TAG, "Network did not answer time request"); + goto Finish; + } - if ((flagSuccess != 1) || (time_sync_seqNo != *seqNo)) { - ESP_LOGW(TAG, "LoRaWAN network did not answer time request"); + if (time_sync_seqNo != *seqNo) { + ESP_LOGW(TAG, "Network timesync handshake failed, seqNo#%u, *seqNo"); goto Finish; } // Populate lmic_time_reference - flagSuccess = LMIC_getNetworkTimeReference(&lmicTime); - if (flagSuccess != 1) { - ESP_LOGW(TAG, "LoRaWAN time request failed"); - goto Finish; - } + if ((LMIC_getNetworkTimeReference(&lmicTime)) != 1) { + ESP_LOGW(TAG, "Network time request failed"); + goto Finish; +} - // Calculate UTCTime, considering the difference between GPS and UTC time - networkTimeSec = lmicTime.tNetwork + GPS_UTC_DIFF; - // Add delay between the instant the time was transmitted and the current time - requestDelaymSec = osticks2ms(os_getTime() - lmicTime.tLocal); - // Update system time with time read from the network - setMyTime(networkTimeSec, requestDelaymSec, _lora); +// Calculate UTCTime, considering the difference between GPS and UTC time +timestamp_sec = lmicTime.tNetwork + GPS_UTC_DIFF; +// Add delay between the instant the time was transmitted and the current time +timestamp_msec = osticks2ms(os_getTime() - lmicTime.tLocal); -Finish: +// store time received from gateway +timesync_storeReq(timestamp_sec, gwtime_sec); +timesync_storeReq(timestamp_msec, gwtime_msec); - // end of time critical section: release app irq lock - unmask_user_IRQ(); +// inform processing task +xTaskNotify(timeSyncReqTask, *seqNo, eSetBits); - // increment and maybe wrap around seqNo, keeping it in time port range - WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO); +Finish : + // end of time critical section: release app irq lock + unmask_user_IRQ(); +} -} // user_request_network_time_callback -#endif // TIME_SYNC_LORAWAN +#endif + +#endif // HAS_LORA \ No newline at end of file From 40f56e02d2cf02dd0ad461abfddd8243b1d73e85 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 6 Mar 2020 19:25:30 +0100 Subject: [PATCH 04/10] v1.9.94 --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index c38c501c..8ef64c50 100644 --- a/platformio.ini +++ b/platformio.ini @@ -45,7 +45,7 @@ description = Paxcounter is a device for metering passenger flows in realtime. I [common] ; for release_version use max. 10 chars total, use any decimal format like "a.b.c" -release_version = 1.9.93 +release_version = 1.9.94 ; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running! ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose debug_level = 3 From 3187fb0d514439b28590e98706dad9d5f81b05f7 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 6 Mar 2020 19:27:11 +0100 Subject: [PATCH 05/10] paxcounter.conf: timesync settings restructured --- src/paxcounter.conf | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 3cc7f2e3..4c0a6061 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -6,7 +6,7 @@ // // Note: After editing, before "build", use "clean" button in PlatformIO! -// Verbose enables serial output +// Verbose enables additional serial debug output #define VERBOSE 0 // set to 0 to silence the device, for mute use build option // Payload send cycle and encoding @@ -74,15 +74,13 @@ // settings for syncing time of node with a time source (network / gps / rtc / timeserver) #define TIME_SYNC_LORAWAN 1 // set to 1 to use LORA network as time source, 0 means off [default = 1] -#define TIME_SYNC_INTERVAL 60 // sync time attempt each .. minutes from time source (GPS/LORA/RTC) [default = 60], 0 means off -#define TIME_SYNC_INTERVAL_RETRY 10 // retry time sync after lost sync each .. minutes [default = 10], 0 means off -#define TIME_SYNC_COMPILEDATE 0 // set to 1 to use compile date to initialize RTC after power outage [default = 0] - -// specific settings for syncing time of node with a timeserver #define TIME_SYNC_LORASERVER 0 // set to 1 to use LORA timeserver as time source, 0 means off [default = 0] +#define TIME_SYNC_INTERVAL 60 // sync time attempt each .. minutes from time source [default = 60], 0 means off +#define TIME_SYNC_INTERVAL_RETRY 10 // retry time sync after lost sync each .. minutes [default = 10], 0 means off #define TIME_SYNC_SAMPLES 1 // number of time requests for averaging, max. 255 #define TIME_SYNC_CYCLE 60 // delay between two time samples [seconds] #define TIME_SYNC_TIMEOUT 300 // timeout waiting for timeserver answer [seconds] +#define TIME_SYNC_COMPILEDATE 0 // set to 1 to use compile date to initialize RTC after power outage [default = 0] // time zone, see https://github.com/JChristensen/Timezone/blob/master/examples/WorldClock/WorldClock.ino #define DAYLIGHT_TIME {"CEST", Last, Sun, Mar, 2, 120} // Central European Summer Time From 63fd681c3b7dd667646dbafbb7474868fe585540 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 6 Mar 2020 22:22:58 +0100 Subject: [PATCH 06/10] MAC command mac_decode calls --- src/lorawan.cpp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 515e5ac9..51934620 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -472,11 +472,6 @@ void myEventCallback(void *pUserData, ev_t ev) { void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, size_t nMsg) { - // calculate if we have encapsulated MAC commands - uint8_t nMac = pMsg - &LMIC.frame[0]; - if (port != MACPORT) - --nMac; - // display amount of received data if (nMsg) ESP_LOGI(TAG, "Received %u byte(s) of payload on port %u", nMsg, port); @@ -485,11 +480,12 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, switch (port) { -// decode unpiggybacked mac messages if we want to print those +// decode mac messages if we want to print those #if (VERBOSE) case MACPORT: // decode downlink MAC commands - // mac_decode(LMIC.frame, nMac, MACdn_table, MACdn_tSize); + if (LMIC.dataBeg) + mac_decode(LMIC.frame, LMIC.dataBeg, MACdn_table, MACdn_tSize); // decode uplink MAC commands if (LMIC.pendMacLen) mac_decode(LMIC.pendMacData, LMIC.pendMacLen, MACup_table, MACup_tSize); @@ -509,15 +505,11 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, recv_timeserver_ans(pMsg, nMsg); #endif + // decode any piggybacked downlink MAC commands if we want to print those default: - -// decode any piggybacked downlink MAC commands #if (VERBOSE) - if (nMac) { - // decoding yet to come - // -> whe need to unwrap the MAC command from LMIC.frame before calling - // mac_decode(LMIC.frame, nMac, MACdn_table, MACdn_tSize); - } + if (LMIC.dataBeg > 1) + mac_decode(LMIC.frame, LMIC.dataBeg - 1, MACdn_table, MACdn_tSize); #endif // VERBOSE break; From c79a36ef3b359b58692f8009e60eba3f14f30f0e Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 7 Mar 2020 00:03:05 +0100 Subject: [PATCH 07/10] mac_decode code sanitized --- include/lorawan.h | 38 +++++++++++++++++++++++---- src/lmic_config.h | 3 --- src/lorawan.cpp | 65 ++++++++++++++++------------------------------- 3 files changed, 55 insertions(+), 51 deletions(-) diff --git a/include/lorawan.h b/include/lorawan.h index c04b0341..91305048 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -41,16 +41,44 @@ const char *getBwName(rps_t rps); const char *getCrName(rps_t rps); // u1_t os_getBattLevel(void); -#if VERBOSE +#if (VERBOSE) + // a table for storage of LORAWAN MAC commands typedef struct { - const uint8_t opcode; + const uint8_t cid; const char cmdname[20]; const uint8_t params; } mac_t; -void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], - const uint8_t tablesize); + +// table of LORAWAN MAC messages sent by the network to the device +// format: CDI, Command (max 19 chars), #parameters (bytes) +// source: LoRaWAN 1.1 Specification (October 11, 2017) +static const mac_t MACdn_table[] = { + {0x01, "ResetConf", 1}, {0x02, "LinkCheckAns", 2}, + {0x03, "LinkADRReq", 4}, {0x04, "DutyCycleReq", 1}, + {0x05, "RXParamSetupReq", 4}, {0x06, "DevStatusReq", 0}, + {0x07, "NewChannelReq", 5}, {0x08, "RxTimingSetupReq", 1}, + {0x09, "TxParamSetupReq", 1}, {0x0A, "DlChannelReq", 4}, + {0x0B, "RekeyConf", 1}, {0x0C, "ADRParamSetupReq", 1}, + {0x0D, "DeviceTimeAns", 5}, {0x0E, "ForceRejoinReq", 2}, + {0x0F, "RejoinParamSetupReq", 1}}; + +static const uint8_t MACdn_tSize = sizeof(MACdn_table) / sizeof(MACdn_table[0]); + +// table of LORAWAN MAC messages sent by the device to the network +static const mac_t MACup_table[] = { + {0x01, "ResetInd", 1}, {0x02, "LinkCheckReq", 0}, + {0x03, "LinkADRAns", 1}, {0x04, "DutyCycleAns", 0}, + {0x05, "RXParamSetupAns", 1}, {0x06, "DevStatusAns", 2}, + {0x07, "NewChannelAns", 1}, {0x08, "RxTimingSetupAns", 0}, + {0x09, "TxParamSetupAns", 0}, {0x0A, "DlChannelAns", 1}, + {0x0B, "RekeyInd", 1}, {0x0C, "ADRParamSetupAns", 0}, + {0x0D, "DeviceTimeReq", 0}, {0x0F, "RejoinParamSetupAns", 1}}; + +static const uint8_t MACup_tSize = sizeof(MACup_table) / sizeof(MACup_table[0]); + +void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, bool is_down); void showLoraKeys(void); -#endif +#endif // VERBOSE #endif \ No newline at end of file diff --git a/src/lmic_config.h b/src/lmic_config.h index d8a88c7a..f888ac0e 100644 --- a/src/lmic_config.h +++ b/src/lmic_config.h @@ -23,9 +23,6 @@ // time sync via LoRaWAN network, note: not supported by TTNv2 #define LMIC_ENABLE_DeviceTimeReq 1 -// use callback event handlers, not onEvent() reference -#define LMIC_ENABLE_onEvent 0 - // This tells LMIC to make the receive windows bigger, in case your clock is // faster or slower. This causes the transceiver to be earlier switched on, // so consuming more power. You may sharpen (reduce) this value if you are diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 51934620..98807eb4 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -23,34 +23,6 @@ RTC_NOINIT_ATTR int RTCseqnoUp, RTCseqnoDn; QueueHandle_t LoraSendQueue; TaskHandle_t lmicTask = NULL, lorasendTask = NULL; -#if (VERBOSE) -// table of LORAWAN MAC messages sent by the network to the device -// format: opcode, cmdname (max 19 chars), #bytes params -// source: LoRaWAN 1.1 Specification (October 11, 2017) -static const mac_t MACdn_table[] = { - {0x01, "ResetConf", 1}, {0x02, "LinkCheckAns", 2}, - {0x03, "LinkADRReq", 4}, {0x04, "DutyCycleReq", 1}, - {0x05, "RXParamSetupReq", 4}, {0x06, "DevStatusReq", 0}, - {0x07, "NewChannelReq", 5}, {0x08, "RxTimingSetupReq", 1}, - {0x09, "TxParamSetupReq", 1}, {0x0A, "DlChannelReq", 4}, - {0x0B, "RekeyConf", 1}, {0x0C, "ADRParamSetupReq", 1}, - {0x0D, "DeviceTimeAns", 5}, {0x0E, "ForceRejoinReq", 2}, - {0x0F, "RejoinParamSetupReq", 1}}; - -// table of LORAWAN MAC messages sent by the device to the network -static const mac_t MACup_table[] = { - {0x01, "ResetInd", 1}, {0x02, "LinkCheckReq", 0}, - {0x03, "LinkADRAns", 1}, {0x04, "DutyCycleAns", 0}, - {0x05, "RXParamSetupAns", 1}, {0x06, "DevStatusAns", 2}, - {0x07, "NewChannelAns", 1}, {0x08, "RxTimingSetupAns", 0}, - {0x09, "TxParamSetupAns", 0}, {0x0A, "DlChannelAns", 1}, - {0x0B, "RekeyInd", 1}, {0x0C, "ADRParamSetupAns", 0}, - {0x0D, "DeviceTimeReq", 0}, {0x0F, "RejoinParamSetupAns", 1}}; - -static const uint8_t MACdn_tSize = sizeof(MACdn_table) / sizeof(MACdn_table[0]), - MACup_tSize = sizeof(MACup_table) / sizeof(MACup_table[0]); -#endif // VERBOSE - class MyHalConfig_t : public Arduino_LMIC::HalConfiguration_t { public: @@ -485,10 +457,10 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, case MACPORT: // decode downlink MAC commands if (LMIC.dataBeg) - mac_decode(LMIC.frame, LMIC.dataBeg, MACdn_table, MACdn_tSize); + mac_decode(LMIC.frame, LMIC.dataBeg, true); // decode uplink MAC commands if (LMIC.pendMacLen) - mac_decode(LMIC.pendMacData, LMIC.pendMacLen, MACup_table, MACup_tSize); + mac_decode(LMIC.pendMacData, LMIC.pendMacLen, false); break; // do not fallthrough to default, we are done #endif @@ -509,7 +481,7 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, default: #if (VERBOSE) if (LMIC.dataBeg > 1) - mac_decode(LMIC.frame, LMIC.dataBeg - 1, MACdn_table, MACdn_tSize); + mac_decode(LMIC.frame, LMIC.dataBeg - 1, true); #endif // VERBOSE break; @@ -577,34 +549,41 @@ u1_t os_getBattLevel() { #if (VERBOSE) // decode LORAWAN MAC message -void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], - const uint8_t tablesize) { +void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, bool is_down) { if (!cmdlen) return; uint8_t foundcmd[cmdlen], cursor = 0; + // select CID resolve table + const mac_t *p; + p = is_down ? MACdn_table : MACup_table; + const int tablesize = is_down ? MACdn_tSize : MACup_tSize; + const String MACdir = is_down ? "DN" : "UP"; + while (cursor < cmdlen) { - int i = tablesize; // number of commands in table + // get number of commands in CID table + int i = tablesize; + // lookup cmd in CID table while (i--) { - if (cmd[cursor] == table[i].opcode) { // lookup command in opcode table - cursor++; // strip 1 byte opcode - if ((cursor + table[i].params) <= cmdlen) { + if (cmd[cursor] == (p + i)->cid) { // lookup command in CID table + cursor++; // strip 1 byte CID + if ((cursor + (p + i)->params) <= cmdlen) { memmove(foundcmd, cmd + cursor, - table[i].params); // strip opcode from cmd array - cursor += table[i].params; - ESP_LOGD(TAG, "MAC command %s", table[i].cmdname); + (p + i)->params); // strip opcode from cmd array + cursor += (p + i)->params; + ESP_LOGD(TAG, "%s MAC command %s", MACdir, (p + i)->cmdname); } else - ESP_LOGD(TAG, "MAC command 0x%02X with missing parameter(s)", - table[i].opcode); + ESP_LOGD(TAG, "%s MAC command 0x%02X with missing parameter(s)", MACdir, + (p + i)->cid); break; // command found -> exit table lookup loop } // end of command validation } // end of command table lookup loop if (i < 0) { // command not found -> skip it - ESP_LOGD(TAG, "Unknown MAC command 0x%02X", cmd[cursor]); + ESP_LOGD(TAG, "%s Unknown MAC command 0x%02X", MACdir, cmd[cursor]); cursor++; } } // command parsing loop From a7f0ee61b3c102c319e461d560c90bcc22d6a0e7 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 7 Mar 2020 00:06:15 +0100 Subject: [PATCH 08/10] rcommand.cpp small fix --- src/rcommand.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 3f3093de..adb61a49 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -343,7 +343,7 @@ void set_flush(uint8_t val[]) { // format: opcode, function, #bytes params, // flag (true = do make settings persistent / false = don't) // -static cmd_t table[] = { +static const cmd_t table[] = { {0x01, set_rssi, 1, true}, {0x02, set_countmode, 1, true}, {0x03, set_gps, 1, true}, {0x04, set_display, 1, true}, {0x05, set_loradr, 1, true}, {0x06, set_lorapower, 1, true}, From 3bc5d5a13fb147ae0d5a0c154252984709116036 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 7 Mar 2020 12:52:46 +0100 Subject: [PATCH 09/10] fix fCounter display offset by 1 bug --- src/display.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 10ea61d1..bb8b296a 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -378,8 +378,9 @@ start: LMIC.datarate); dp_printf(0, 5, FONT_SMALL, 0, "ChMsk:%04X Nonce:%04X", LMIC.channelMap, LMIC.devNonce); - dp_printf(0, 6, FONT_SMALL, 0, "CUp:%-6d CDn:%-6d", LMIC.seqnoUp, - LMIC.seqnoDn); + dp_printf(0, 6, FONT_SMALL, 0, "fUp:%-6d fDn:%-6d", + LMIC.seqnoUp ? LMIC.seqnoUp - 1 : 0, + LMIC.seqnoDn ? LMIC.seqnoDn - 1 : 0); dp_printf(0, 7, FONT_SMALL, 0, "SNR:%-5d RSSI:%-5d", LMIC.snr / 4, LMIC.rssi); break; // page5 From cf29505c84abb58986d932b74f57e218d629f808 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 7 Mar 2020 13:00:22 +0100 Subject: [PATCH 10/10] fix SNR display bug --- src/display.cpp | 2 +- src/lorawan.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index bb8b296a..0765b452 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -381,7 +381,7 @@ start: dp_printf(0, 6, FONT_SMALL, 0, "fUp:%-6d fDn:%-6d", LMIC.seqnoUp ? LMIC.seqnoUp - 1 : 0, LMIC.seqnoDn ? LMIC.seqnoDn - 1 : 0); - dp_printf(0, 7, FONT_SMALL, 0, "SNR:%-5d RSSI:%-5d", LMIC.snr / 4, + dp_printf(0, 7, FONT_SMALL, 0, "SNR:%-5d RSSI:%-5d", (LMIC.snr + 2) / 4, LMIC.rssi); break; // page5 #else // don't show blank page if we are unattended diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 98807eb4..39c7ed17 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -77,7 +77,7 @@ void lora_setupForNetwork(bool preJoin) { // show current devaddr ESP_LOGI(TAG, "DEVaddr: 0x%08X | Network ID: 0x%06X | Network Type: %d", LMIC.devaddr, LMIC.netid & 0x001FFFFF, LMIC.netid & 0x00E00000); - ESP_LOGI(TAG, "RSSI: %d | SNR: %d", LMIC.rssi, LMIC.snr / 4); + ESP_LOGI(TAG, "RSSI: %d | SNR: %d", LMIC.rssi, (LMIC.snr + 2) / 4); ESP_LOGI(TAG, "Radio parameters: %s | %s | %s", getSfName(updr2rps(LMIC.datarate)), getBwName(updr2rps(LMIC.datarate)), @@ -560,7 +560,7 @@ void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, bool is_down) { const mac_t *p; p = is_down ? MACdn_table : MACup_table; const int tablesize = is_down ? MACdn_tSize : MACup_tSize; - const String MACdir = is_down ? "DN" : "UP"; + const String MACdir = is_down ? "-->" : "<--"; while (cursor < cmdlen) { @@ -577,8 +577,8 @@ void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, bool is_down) { cursor += (p + i)->params; ESP_LOGD(TAG, "%s MAC command %s", MACdir, (p + i)->cmdname); } else - ESP_LOGD(TAG, "%s MAC command 0x%02X with missing parameter(s)", MACdir, - (p + i)->cid); + ESP_LOGD(TAG, "%s MAC command 0x%02X with missing parameter(s)", + MACdir, (p + i)->cid); break; // command found -> exit table lookup loop } // end of command validation } // end of command table lookup loop