|
| 1 | +// Copyright © 2016 The Things Network |
| 2 | +// Use of this source code is governed by the MIT license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +#include "Arduino.h" |
| 5 | +#include "TheThingsNetwork.h" |
| 6 | + |
| 7 | +void TheThingsNetwork::init(Stream& modemStream, Stream& debugStream) { |
| 8 | + this->modemStream = &modemStream; |
| 9 | + this->debugStream = &debugStream; |
| 10 | +} |
| 11 | + |
| 12 | +String TheThingsNetwork::readLine(int waitTime) { |
| 13 | + unsigned long start = millis(); |
| 14 | + while (millis() < start + waitTime) { |
| 15 | + String line = modemStream->readStringUntil('\n'); |
| 16 | + if (line.length() > 0) { |
| 17 | + return line.substring(0, line.length() - 1); |
| 18 | + } |
| 19 | + } |
| 20 | + return ""; |
| 21 | +} |
| 22 | + |
| 23 | +bool TheThingsNetwork::waitForOK(int waitTime, String okMessage) { |
| 24 | + String line = readLine(waitTime); |
| 25 | + if (line == "") { |
| 26 | + debugPrintLn("Wait for OK time-out expired"); |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + if (line != okMessage) { |
| 31 | + debugPrintLn("Response is not OK"); |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +String TheThingsNetwork::readValue(String cmd) { |
| 39 | + modemStream->println(cmd); |
| 40 | + return readLine(); |
| 41 | +} |
| 42 | + |
| 43 | +bool TheThingsNetwork::sendCommand(String cmd, int waitTime) { |
| 44 | + debugPrintLn("Sending: " + cmd); |
| 45 | + |
| 46 | + modemStream->println(cmd); |
| 47 | + |
| 48 | + return waitForOK(waitTime); |
| 49 | +} |
| 50 | + |
| 51 | +bool TheThingsNetwork::sendCommand(String cmd, String value, int waitTime) { |
| 52 | + int l = value.length(); |
| 53 | + byte buf[l]; |
| 54 | + value.getBytes(buf, l); |
| 55 | + |
| 56 | + return sendCommand(cmd, buf, l, waitTime); |
| 57 | +} |
| 58 | + |
| 59 | +char btohexa_high(unsigned char b) { |
| 60 | + b >>= 4; |
| 61 | + return (b > 0x9u) ? b + 'A' - 10 : b + '0'; |
| 62 | +} |
| 63 | + |
| 64 | +char btohexa_low(unsigned char b) { |
| 65 | + b &= 0x0F; |
| 66 | + return (b > 0x9u) ? b + 'A' - 10 : b + '0'; |
| 67 | +} |
| 68 | + |
| 69 | +bool TheThingsNetwork::sendCommand(String cmd, const byte *buf, int length, int waitTime) { |
| 70 | + debugPrintLn("Sending: " + cmd + " with " + String(length) + " bytes"); |
| 71 | + |
| 72 | + modemStream->print(cmd + " "); |
| 73 | + |
| 74 | + for (int i = 0; i < length; i++) { |
| 75 | + modemStream->print(btohexa_high(buf[i])); |
| 76 | + modemStream->print(btohexa_low(buf[i])); |
| 77 | + } |
| 78 | + modemStream->println(); |
| 79 | + |
| 80 | + return waitForOK(waitTime); |
| 81 | +} |
| 82 | + |
| 83 | +void TheThingsNetwork::reset(bool adr, int sf, int fsb) { |
| 84 | + modemStream->println("sys reset"); |
| 85 | + String version = readLine(3000); |
| 86 | + if (version == "") { |
| 87 | + debugPrintLn("Invalid version"); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + model = version.substring(0, version.indexOf(' ')); |
| 92 | + debugPrintLn("Version is " + version + ", model is " + model); |
| 93 | + |
| 94 | + sendCommand("mac set adr " + String(adr ? "on" : "off")); |
| 95 | + |
| 96 | + int dr = -1; |
| 97 | + if (model == "RN2483") { |
| 98 | + sendCommand("mac set pwridx " + String(PWRIDX_868)); |
| 99 | + |
| 100 | + switch (sf) { |
| 101 | + case 7: |
| 102 | + dr = 5; |
| 103 | + break; |
| 104 | + case 8: |
| 105 | + dr = 4; |
| 106 | + break; |
| 107 | + case 9: |
| 108 | + dr = 3; |
| 109 | + break; |
| 110 | + case 10: |
| 111 | + dr = 2; |
| 112 | + break; |
| 113 | + case 11: |
| 114 | + dr = 1; |
| 115 | + break; |
| 116 | + case 12: |
| 117 | + dr = 0; |
| 118 | + break; |
| 119 | + default: |
| 120 | + debugPrintLn("Invalid SF") |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + else if (model == "RN2903") { |
| 125 | + sendCommand("mac set pwridx " + String(PWRIDX_915)); |
| 126 | + enableFsbChannels(fsb); |
| 127 | + |
| 128 | + switch (sf) { |
| 129 | + case 7: |
| 130 | + dr = 3; |
| 131 | + break; |
| 132 | + case 8: |
| 133 | + dr = 2; |
| 134 | + break; |
| 135 | + case 9: |
| 136 | + dr = 1; |
| 137 | + break; |
| 138 | + case 10: |
| 139 | + dr = 0; |
| 140 | + break; |
| 141 | + default: |
| 142 | + debugPrintLn("Invalid SF") |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (dr > -1) |
| 148 | + sendCommand("mac set dr " + String(dr)); |
| 149 | +} |
| 150 | + |
| 151 | +bool TheThingsNetwork::enableFsbChannels(int fsb) { |
| 152 | + int chLow = fsb > 0 ? (fsb - 1) * 8 : 0; |
| 153 | + int chHigh = fsb > 0 ? chLow + 7 : 71; |
| 154 | + |
| 155 | + for (int i = 0; i < 72; i++) |
| 156 | + if (i == 70 || chLow <= i && i <= chHigh) |
| 157 | + sendCommand("mac set ch status " + String(i) + " on"); |
| 158 | + else |
| 159 | + sendCommand("mac set ch status " + String(i) + " off"); |
| 160 | + return true; |
| 161 | +} |
| 162 | + |
| 163 | +bool TheThingsNetwork::personalize(const byte devAddr[4], const byte nwkSKey[16], const byte appSKey[16]) { |
| 164 | + sendCommand("mac set devaddr", devAddr, 4); |
| 165 | + sendCommand("mac set nwkskey", nwkSKey, 16); |
| 166 | + sendCommand("mac set appskey", appSKey, 16); |
| 167 | + sendCommand("mac join abp"); |
| 168 | + |
| 169 | + if (readLine() != "accepted") { |
| 170 | + debugPrintLn("Personalize not accepted"); |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + debugPrintLn("Personalize accepted. Status: " + readValue("mac get status")); |
| 175 | + return true; |
| 176 | +} |
| 177 | + |
| 178 | +bool TheThingsNetwork::join(const byte appEui[8], const byte appKey[16]) { |
| 179 | + String devEui = readValue("sys get hweui"); |
| 180 | + sendCommand("mac set appeui", appEui, 8); |
| 181 | + sendCommand("mac set deveui " + devEui); |
| 182 | + sendCommand("mac set appkey", appKey, 16); |
| 183 | + sendCommand("mac join otaa"); |
| 184 | + |
| 185 | + String response = readLine(10000); |
| 186 | + if (response != "accepted") { |
| 187 | + debugPrintLn("Join not accepted"); |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + debugPrintLn("Join accepted: " + response + ". Status: " + readValue("mac get status")); |
| 192 | + return true; |
| 193 | +} |
| 194 | + |
| 195 | +void TheThingsNetwork::sendBytes(const byte* buffer, int length, int port, bool confirm) { |
| 196 | + if (!sendCommand("mac tx " + String(confirm ? "cnf" : "uncnf") + " " + String(port), buffer, length)) { |
| 197 | + debugPrintLn("Send command failed"); |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + String response = readLine(10000); |
| 202 | + if (response == "") |
| 203 | + debugPrintLn("Time-out") |
| 204 | + else if (response == "mac_tx_ok") |
| 205 | + debugPrintLn("Successful transmission") |
| 206 | + //else if (response starts with "mac_rx") // TODO: Handle downlink |
| 207 | + else |
| 208 | + debugPrintLn("Unexpected response: " + response); |
| 209 | +} |
| 210 | + |
| 211 | +void TheThingsNetwork::sendString(String message, int port, bool confirm) { |
| 212 | + int l = message.length(); |
| 213 | + byte buf[l + 1]; |
| 214 | + message.getBytes(buf, l + 1); |
| 215 | + |
| 216 | + return sendBytes(buf, l, port, confirm); |
| 217 | +} |
| 218 | + |
| 219 | +void TheThingsNetwork::showStatus() { |
| 220 | + debugPrintLn("EUI: " + readValue("sys get hweui")); |
| 221 | + debugPrintLn("Battery: " + readValue("sys get vdd")); |
| 222 | + debugPrintLn("AppEUI: " + readValue("mac get appeui")); |
| 223 | + debugPrintLn("DevEUI: " + readValue("mac get deveui")); |
| 224 | + debugPrintLn("DevAddr: " + readValue("mac get devaddr")); |
| 225 | + |
| 226 | + if (this->model == "RN2483") { |
| 227 | + debugPrintLn("Band: " + readValue("mac get band")); |
| 228 | + } |
| 229 | + |
| 230 | + debugPrintLn("Data Rate: " + readValue("mac get dr")); |
| 231 | + debugPrintLn("RX Delay 1: " + readValue("mac get rxdelay1")); |
| 232 | + debugPrintLn("RX Delay 2: " + readValue("mac get rxdelay2")); |
| 233 | +} |
0 commit comments