AllWize Library
AllWize.cpp
Go to the documentation of this file.
1 /*
2 
3 AllWize Library
4 
5 Copyright (C) 2018-2019 by AllWize <github@allwize.io>
6 
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 */
21 
27 #include "AllWize.h"
28 #include <assert.h>
29 
30 // -----------------------------------------------------------------------------
31 // Init
32 // -----------------------------------------------------------------------------
33 
40 AllWize::AllWize(HardwareSerial *serial, uint8_t reset_gpio, uint8_t config_gpio) : _stream(serial), _hw_serial(serial), _reset_gpio(reset_gpio), _config_gpio(config_gpio) {
41  _init();
42 }
43 
44 #if not defined(ARDUINO_ARCH_SAMD) && not defined(ARDUINO_ARCH_ESP32)
45 
51 AllWize::AllWize(SoftwareSerial *serial, uint8_t reset_gpio, uint8_t config_gpio) : _stream(serial), _sw_serial(serial), _reset_gpio(reset_gpio), _config_gpio(config_gpio) {
52  _init();
53 }
54 #endif
55 
63 AllWize::AllWize(uint8_t rx, uint8_t tx, uint8_t reset_gpio, uint8_t config_gpio) : _rx(rx), _tx(tx), _reset_gpio(reset_gpio), _config_gpio(config_gpio) {
64 #if defined(ARDUINO_ARCH_SAMD)
65  // Software serial not implemented for SAMD
66  assert(false);
67 #elif defined(ARDUINO_ARCH_ESP32)
68  _stream = _hw_serial = new HardwareSerial(HARDWARE_SERIAL_PORT);
69 #else
70  _stream = _sw_serial = new SoftwareSerial(_rx, _tx);
71 #endif
72  _init();
73 }
74 
76  if (GPIO_NONE != _reset_gpio) {
77  pinMode(_reset_gpio, OUTPUT);
78  digitalWrite(_reset_gpio, HIGH);
79  }
80  if (GPIO_NONE != _config_gpio) {
81  pinMode(_config_gpio, OUTPUT);
82  digitalWrite(_config_gpio, LOW);
83  }
84  randomSeed(analogRead(0));
85  _access_number = random(0, 256);
86 }
87 
91 void AllWize::begin(uint8_t baudrate) {
92 
93  _baudrate = BAUDRATES[baudrate-1];
94  reset();
95  delay(200);
96 
97  // Figure out module type
98  _readModel();
99  String part_number = getPartNumber();
100  if (part_number.equals("RC1701HP-MBUS4")) {
102  } else if (part_number.equals("RC1701HP-OSP")) {
104  } else if (part_number.equals("RC1701HP-WIZE")) {
106  _ci = CI_WIZE;
107  } else {
109  }
110 
114 
115 }
116 
121 
122  if (_hw_serial) {
123 
124  _hw_serial->end();
125 #if defined(ARDUINO_ARCH_ESP32)
126  if ((_rx != -1) && (_tx != -1)) {
127  pinMode(_rx, FUNCTION_4);
128  pinMode(_tx, FUNCTION_4);
129  _hw_serial->begin(_baudrate, SERIAL_8N1, _rx, _tx);
130  } else {
131  _hw_serial->begin(_baudrate);
132  }
133 #else
134  _hw_serial->begin(_baudrate);
135 #endif
136 
137  } else {
138 
139 #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD)
140  // It should never hit this block
141  assert(false);
142 #else
143  _sw_serial->end();
144  _sw_serial->begin(_baudrate);
145 #endif
146 
147  }
148 
149  _flush();
150 
151  // Cache memory
152  #if USE_MEMORY_CACHE
154  #endif
155 
156 }
157 
163  if (GPIO_NONE == _reset_gpio) {
164  _reset_serial();
165  delay(100);
167  if (_setConfig(true)) {
168  _send('@');
169  _send('R');
170  _send('R');
171  delay(100);
172  if (GPIO_NONE != _config_gpio) {
173  digitalWrite(_config_gpio, LOW);
174  }
175  _config = false;
176  _reset_serial();
177  return true;
178  }
179  } else {
180  digitalWrite(_reset_gpio, LOW);
181  delay(1);
182  digitalWrite(_reset_gpio, HIGH);
183  delay(100);
184  if (GPIO_NONE != _config_gpio) {
185  digitalWrite(_config_gpio, LOW);
186  }
187  _config = false;
188  _reset_serial();
189  return true;
190  }
191  return false;
192 }
193 
198  if (_setConfig(true)) _setConfig(false);
199  /*
200  if (_send(CMD_ENTER_CONFIG) == 1) {
201  _flush();
202  _send(CMD_EXIT_CONFIG);
203  }
204  delay(10);
205  */
206 }
207 
213  _reset_serial();
214  delay(100);
216  if (_setConfig(true)) {
217  _send('@');
218  _send('R');
219  _send('C');
220  delay(100);
221  if (GPIO_NONE != _config_gpio) {
222  digitalWrite(_config_gpio, LOW);
223  }
224  _config = false;
225  _reset_serial();
226  return true;
227  }
228  return false;
229 }
230 
235  setMode(DEFAULT_MBUS_MODE, true);
242  setAppendRSSI(true);
244 }
245 
250  setMode(DEFAULT_MBUS_MODE, true);
255 }
256 
261  setMode(DEFAULT_MBUS_MODE, true);
263 }
264 
269  if (!_setConfig(true)) return;
270  _send(CMD_SLEEP);
271 }
272 
277  _send(CMD_AWAKE);
278  delay(5);
279  ready();
280 }
281 
286  bool response = _setConfig(true);
287  if (response) _setConfig(false);
288  return response;
289 }
290 
294 bool AllWize::waitForReady(uint32_t timeout) {
295  uint32_t start = millis();
296  while (millis() - start < timeout) {
297  if (ready()) return true;
298  delay(100);
299  }
300  return false;
301 }
302 
307 void AllWize::dump(Stream &debug) {
308 
309  #if not USE_MEMORY_CACHE
310  uint8_t _memory[0x100] = {0xFF};
311  bool _ready = _cacheMemory(_memory);
312  #endif
313 
314  if (!_ready) {
315  debug.println("Error doing memory dump...");
316  return;
317  }
318 
319  char ch[10];
320  char ascii[17] = {0};
321  uint8_t address = 0;
322  ascii[16] = 0;
323 
324  debug.println();
325  debug.print(" ");
326  for (address = 0; address <= 0x0F; address++) {
327  snprintf(ch, sizeof(ch), "%02X ", address);
328  debug.print(ch);
329  }
330  debug.println();
331  debug.print("------------------------------------------------------");
332 
333  address = 0;
334  while (true) {
335 
336  if ((address % 16) == 0) {
337  if (address > 0)
338  debug.print(ascii);
339  snprintf(ch, sizeof(ch), "\n0x%02X: ", address);
340  debug.print(ch);
341  }
342  if ((31 < _memory[address]) && (_memory[address] < 127)) {
343  ascii[address % 16] = (char)_memory[address];
344  } else {
345  ascii[address % 16] = ' ';
346  }
347  snprintf(ch, sizeof(ch), "%02X ", (uint8_t)_memory[address]);
348  debug.print(ch);
349 
350  if (0xFF == address) break;
351  address++;
352 
353  }
354 
355  debug.println();
356  debug.println();
357 
358 }
359 
366 bool AllWize::send(uint8_t *buffer, uint8_t len) {
367 
368  // Check we are in IDLE mode
369  if (_config) return false;
370 
371  // Clean line
372  softReset();
373 
374  // Send no response message in len is 0
375  if (0 == len) return (1 == _send(0xFE));
376 
377  // Wize transport layer
378  bool send_wize_transport_layer = (MODULE_WIZE == _module) && (CI_WIZE == _ci);
379 
380  // message length is payload length + 1 (CI) + 2 (for timestamp if wize) + 6 (wize transport layer if wize)
381  uint8_t message_len = len + 1;
382  if (MODULE_WIZE == _module) message_len += 2;
383  if (send_wize_transport_layer) message_len += 6;
384 
385  // max payload size is 0xF6 bytes
386  if (message_len > 0xF6) return false;
387 
388  // length
389  if (1 != _send(message_len)) return false;
390 
391  // control information field
392  if (1 != _send(_ci)) return false;
393 
394  // transport layer
395  if (send_wize_transport_layer) {
396  _send(_wize_control); // Wize Control
397  _send((_wize_operator_id >> 0) & 0xFF); // Operator ID HIGH
398  _send((_wize_operator_id >> 8) & 0xFF); // Operator ID HIGH
399  _send((_counter >> 0) & 0xFF); // Frame counter LOW
400  _send((_counter >> 8) & 0xFF); // Frame counter HIGH
401  _send(_wize_application); // Wize app indicator
402  }
403 
404  // application payload
405  if (len != _send(buffer, len)) return false;
406 
407  // timestamp, TODO: add option to provide a timestamp
408  if (MODULE_WIZE == _module) {
409  _send(0);
410  _send(0);
411  }
412 
413  _access_number++;
414  _counter++;
415  return true;
416 
417 }
418 
424 bool AllWize::send(const char *buffer) {
425  return send((uint8_t *)buffer, strlen(buffer));
426 }
427 
432 bool AllWize::ack() {
433  if (_config) return false;
435  return send("ACK");
436 }
437 
443 bool AllWize::enableRX(bool enable) {
444  if (_config) return false;
445  if (enable) {
447  } else {
449  }
450  return true;
451 }
452 
459 
460  bool response = false;
461 
462  static uint32_t when = millis();
463 
464  while (_stream->available() && _pointer < RX_BUFFER_SIZE) {
465 
466  uint8_t ch = _stream->read();
467 
468  #if defined(ALLWIZE_DEBUG_PORT)
469  {
470  char buffer[10];
471  snprintf(buffer, sizeof(buffer), "r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
472  ALLWIZE_DEBUG_PRINTLN(buffer);
473  }
474  #endif
475 
476  _buffer[_pointer++] = ch;
477  when = millis();
478 
479  #if defined(ARDUINO_ARCH_ESP8266)
480  yield();
481  #endif
482 
483  }
484 
485  // Check if message finished and decode it
486  if ((_pointer > 0) && (millis() - when > 100)) {
487 
488  response = _decode();
489  _pointer = 0;
490 
491  // If we don't soft-reset the line the RX channel gets stalled
492  softReset();
493 
494  }
495 
496  return response;
497 
498 }
499 
505  return _message;
506 }
507 
513 bool AllWize::setWizeControl(uint8_t wize_control) {
514  if (wize_control > 14) return false;
515  _wize_control = wize_control;
516  return true;
517 }
518 
523 void AllWize::setWizeOperatorId(uint16_t wize_operator_id) {
524  _wize_operator_id = wize_operator_id;
525 }
526 
531 void AllWize::setWizeApplication(uint8_t wize_application) {
532  _wize_application = wize_application;
533 }
534 
539 void AllWize::setCounter(uint16_t counter) {
540  _counter = counter;
541 }
542 
547 uint16_t AllWize::getCounter() {
548  return _counter;
549 }
550 
551 // -----------------------------------------------------------------------------
552 // Configuration
553 // -----------------------------------------------------------------------------
554 
560  _ci = ci;
561 }
562 
568  return _ci;
569 }
570 
576 void AllWize::setChannel(uint8_t channel, bool persist) {
577  if (channel > 41) return;
578  if (persist) {
579  _setSlot(MEM_CHANNEL, channel);
580  if (MODULE_WIZE == _module) {
581  _setSlot(MEM_CHANNEL_RX, channel);
582  }
583  }
584  _sendCommand(CMD_CHANNEL, channel);
585 }
586 
592  return _getSlot(MEM_CHANNEL);
593 }
594 
600 void AllWize::setPower(uint8_t power, bool persist) {
601  if (0 < power && power < 6) {
602  if (persist) {
603  _setSlot(MEM_RF_POWER, power);
604  }
605  _sendCommand(CMD_RF_POWER, power);
606  }
607 }
608 
613 uint8_t AllWize::getPower() {
614  return _getSlot(MEM_RF_POWER);
615 }
616 
621 void AllWize::setDataRate(uint8_t dr) {
622 
623  if (_module == MODULE_MBUS4) return;
624  if (dr < 1) return;
625  if (_module == MODULE_OSP) {
626  if (DATARATE_6400bps == dr) {
628  }
629  if (dr > 5) return;
630  }
631  if (_module == MODULE_WIZE) {
632  if (dr > 3) return;
633  }
634 
635  _setSlot(MEM_DATA_RATE, dr);
636  if (MODULE_WIZE == _module) {
638  }
639 
640 }
641 
647  return _getSlot(MEM_DATA_RATE);
648 }
649 
655 void AllWize::setMode(uint8_t mode, bool persist) {
656 
657  // Wize FW accepts only modes 0x10 and 0x11
658  if (MODULE_WIZE == _module) {
659  if ((MBUS_MODE_N1 != mode) && (MBUS_MODE_N2 != mode)) return;
660  }
661 
662  // Only OSP FW accepts mode 0x12
663  if ((MBUS_MODE_OSP == mode) && (MODULE_OSP != _module)) return;
664 
665  if (persist) {
666  _setSlot(MEM_MBUS_MODE, mode);
667  }
669  _mbus_mode = mode;
670 
671 }
672 
677 uint8_t AllWize::getMode() {
678  return _mbus_mode;
679 }
680 
685 void AllWize::setSleepMode(uint8_t mode) {
686  _setSlot(MEM_SLEEP_MODE, mode);
687 }
688 
694  return _getSlot(MEM_SLEEP_MODE);
695 }
696 
701 void AllWize::setAppendRSSI(bool value) {
702  if (value == 1) {
704  } else {
706  }
707  _append_rssi = value;
708 }
709 
715  return _append_rssi;
716 }
717 
722 void AllWize::setPreamble(uint8_t preamble) {
723  if (PREAMBLE_FORMAT_A == preamble || PREAMBLE_FORMAT_B == preamble) {
724  _setSlot(MEM_PREAMBLE_LENGTH, preamble);
725  }
726 }
727 
734 }
735 
740 void AllWize::setTimeout(uint16_t ms) {
741  if (ms > 4080) return;
742  uint8_t timeout = (ms / 16) - 1;
743  _setSlot(MEM_TIMEOUT, timeout);
744 }
745 
751  uint8_t timeout = _getSlot(MEM_TIMEOUT);
752  return 16 * (uint16_t) (timeout + 1);
753 }
754 
759 void AllWize::setNetworkRole(uint8_t role) {
760  _setSlot(MEM_NETWORK_ROLE, role);
761 }
762 
768  return _getSlot(MEM_NETWORK_ROLE);
769 }
770 
775 void AllWize::setLEDControl(uint8_t value) {
776  if (value > 3) return;
777  _setSlot(MEM_LED_CONTROL, value);
778 }
779 
785  return _getSlot(MEM_LED_CONTROL);
786 }
787 
792 void AllWize::setDataInterface(uint8_t value) {
793  if (value <= 0x0C) {
795  _data_interface = value;
796  }
797 }
798 
804  return _data_interface;
805 }
806 
812 void AllWize::setControlField(uint8_t value, bool persist) {
813  if (persist) {
814  _setSlot(MEM_CONTROL_FIELD, value);
815  }
817 }
818 
824  return _getSlot(MEM_CONTROL_FIELD);
825 }
826 
832 void AllWize::setInstallMode(uint8_t mode, bool persist) {
833  if (mode <= 2) {
834  if (persist) {
835  _setSlot(MEM_INSTALL_MODE, mode);
836  }
838  }
839 }
840 
846  return _getSlot(MEM_INSTALL_MODE);
847 }
848 
853 void AllWize::setEncryptFlag(uint8_t flag) {
854  if (0 == flag || 1 == flag || 3 == flag) {
855  _setSlot(MEM_ENCRYPT_FLAG, flag);
856  }
857 }
858 
864  return _getSlot(MEM_ENCRYPT_FLAG);
865 }
866 
871 void AllWize::setDecryptFlag(uint8_t flag) {
872  _setSlot(MEM_DECRYPT_FLAG, flag);
873 }
874 
880  return _getSlot(MEM_DECRYPT_FLAG);
881 }
882 
888 void AllWize::setKey(uint8_t reg, const uint8_t *key) {
889  if (reg > 128) return;
890  uint8_t data[17];
891  data[0] = reg;
892  memcpy(&data[1], key, 16);
893  _sendCommand(CMD_KEY_REGISTER, data, 17);
894 }
895 
900 void AllWize::setDefaultKey(const uint8_t *key) {
901  _setSlot(MEM_DEFAULT_KEY, (uint8_t *)key, 16);
902 }
903 
908 void AllWize::getDefaultKey(uint8_t *key) {
909  _getSlot(MEM_DEFAULT_KEY, key, 16);
910 }
911 
916 void AllWize::setAccessNumber(uint8_t value) {
918 }
919 
924 void AllWize::setBaudRate(uint8_t value) {
925  if ((0 < value) & (value < 12)) {
926  if (ready()) _setSlot(MEM_UART_BAUD_RATE, value);
927  _baudrate = BAUDRATES[value-1];
928  }
929 }
930 
937 }
938 
944 uint32_t AllWize::getBaudRateSpeed(uint8_t value) {
945  if ((0 < value) & (value < 12)) {
946  return BAUDRATES[value-1];
947  }
948  return 0;
949 }
950 
951 // -----------------------------------------------------------------------------
952 
959  uint8_t response = _sendCommand(CMD_RSSI);
960  if (response > 0) return -0.5 * (float) _buffer[0];
961  return 0;
962 }
963 
969  uint8_t response = _sendCommand(CMD_TEMPERATURE);
970  uint8_t ret_val = 0;
971 
972  if (response > 0) {
973  ret_val = _buffer[0] - 128;
974  } else {
975  ret_val = 0;
976  }
977 
978  return ret_val;
979 }
980 
986  uint8_t response = _sendCommand(CMD_VOLTAGE);
987  uint16_t ret_val;
988  if (response > 0) {
989  ret_val = 30 * _buffer[0];
990  } else {
991  ret_val = 0;
992  }
993 
994  return ret_val;
995 }
996 
1003 }
1004 
1009 bool AllWize::setMID(uint16_t mid) {
1010  uint8_t buffer[2];
1011  buffer[0] = (mid >> 8) & 0xFF;
1012  buffer[1] = (mid >> 0) & 0xFF;
1013  return _setSlot(MEM_MANUFACTURER_ID, buffer, 2);
1014 }
1015 
1022 }
1023 
1028 bool AllWize::setUID(uint32_t uid) {
1029  uint8_t buffer[4];
1030  buffer[0] = (uid >> 24) & 0xFF;
1031  buffer[1] = (uid >> 16) & 0xFF;
1032  buffer[2] = (uid >> 8) & 0xFF;
1033  buffer[3] = (uid >> 0) & 0xFF;
1034  return _setSlot(MEM_UNIQUE_ID, buffer, 4);
1035 }
1036 
1042  return _getSlot(MEM_VERSION);
1043 }
1044 
1049 void AllWize::setVersion(uint8_t version) {
1050  _setSlot(MEM_VERSION, version);
1051 }
1052 
1058  return _getSlot(MEM_DEVICE);
1059 }
1060 
1065 void AllWize::setDevice(uint8_t type) {
1066  _setSlot(MEM_DEVICE, type);
1067 }
1068 
1074  return _model;
1075 }
1076 
1082  return _hw;
1083 }
1084 
1090  return _fw;
1091 }
1092 
1099 }
1100 
1106  return _module;
1107 }
1108 
1114  switch (_module) {
1115  case MODULE_MBUS4: return String("MBUS4");
1116  case MODULE_OSP: return String("OSP");
1117  case MODULE_WIZE: return String("WIZE");
1118  }
1119  return String("Unknown");
1120 }
1121 
1122 
1128 double AllWize::getFrequency(uint8_t channel) {
1129  if (channel < 7) {
1130  return 169.40625 + 0.0125 * (channel - 1);
1131  } else if (channel == 7) {
1132  return 169.41250;
1133  } else if (channel == 8) {
1134  return 169.43750;
1135  } else if (channel == 9) {
1136  return 169.46250;
1137  } else if (channel == 10) {
1138  return 169.43750;
1139  } else if (channel < 38) {
1140  return 169.48125 + 0.0125 * (channel - 11);
1141  } else if (channel < 42) {
1142  return 169.62500 + 0.0500 * (channel - 38);
1143  } else {
1144  return 0;
1145  }
1146 }
1147 
1153 uint16_t AllWize::getDataRateSpeed(uint8_t dr) {
1154  if (dr == DATARATE_6400bps_OSP) dr = DATARATE_6400bps;
1155  if ((0 < dr) && (dr < 5)) {
1156  return DATARATES[dr-1];
1157  }
1158  return 0;
1159 }
1160 
1161 // -----------------------------------------------------------------------------
1162 // Protected
1163 // -----------------------------------------------------------------------------
1164 
1171 bool AllWize::_setConfig(bool value) {
1172  if (value != _config) {
1173  _flush();
1174  if (value) {
1175  if (GPIO_NONE != _config_gpio) {
1176  digitalWrite(_config_gpio, HIGH);
1177  _config = true;
1178  } else {
1180  }
1181  } else {
1182  if (GPIO_NONE != _config_gpio) {
1183  digitalWrite(_config_gpio, LOW);
1184  }
1186  delay(5);
1187  _config = false;
1188  }
1189  }
1190  return _config;
1191 }
1192 
1200 int8_t AllWize::_sendCommand(uint8_t command, uint8_t *data, uint8_t len) {
1201  int8_t response = -1;
1202  if (!_setConfig(true)) return response;
1203  if (_sendAndReceive(command) != -1) {
1204  response = _sendAndReceive(data, len);
1205  }
1206  _setConfig(false);
1207  return response;
1208 }
1209 
1217 int8_t AllWize::_sendCommand(uint8_t command, uint8_t data) {
1218  int8_t response = -1;
1219  if (!_setConfig(true)) return response;
1220  if (_sendAndReceive(command) != -1) {
1221  response = _sendAndReceive(data);
1222  }
1223  _setConfig(false);
1224  return response;
1225 }
1226 
1233 int8_t AllWize::_sendCommand(uint8_t command) {
1234  int8_t response = -1;
1235  if (!_setConfig(true)) return response;
1236  response = _sendAndReceive(command);
1237  _setConfig(false);
1238  return response;
1239 }
1240 
1241 // ------------------------------------------------------------------------------------------------
1242 
1249 bool AllWize::_cacheMemory(uint8_t * buffer) {
1250 
1251  // Read memory
1252  _setConfig(true);
1254  bool ret = (256 == _readBytes((char *) buffer, 256));
1255  _setConfig(false);
1256  return ret;
1257 
1258 }
1259 
1265 
1266  #if not USE_MEMORY_CACHE
1267  uint8_t _memory[0x100] = {0xFF};
1268  bool _ready = _cacheMemory(_memory);
1269  if (!_ready) return;
1270  #endif
1271 
1272  // Look for the part_number
1273  bool found = false;
1274  uint8_t index = 0;
1275  uint8_t len = strlen(MODULE_SIGNATURE);
1276  for (index=0; index<0xFF-32; index++) {
1277  if (memcmp(&_memory[index], (uint8_t *) MODULE_SIGNATURE, len) == 0) {
1278  found = true;
1279  break;
1280  }
1281  }
1282 
1283  // Parse signature
1284  if (found) {
1285 
1286  String part_number = String((char *) &_memory[index]);
1287  part_number.substring(0, 31);
1288  part_number.trim();
1289 
1290  uint8_t end = part_number.indexOf(",");
1291  _model = part_number.substring(0, end);
1292  uint8_t start = end + 1;
1293  end = part_number.indexOf(",", start);
1294  _hw = part_number.substring(start, end);
1295  _fw = part_number.substring(end + 1, 32);
1296  _fw.trim();
1297 
1298  }
1299 
1300 }
1301 
1310 uint8_t AllWize::_getMemory(uint8_t address, uint8_t *buffer, uint8_t len) {
1311  #if USE_MEMORY_CACHE
1312  if (!_ready) return 0;
1313  memcpy(buffer, &_memory[address], len);
1314  return len;
1315  #else
1316  uint8_t count = 0;
1317  if (_setConfig(true)) {
1318  for (uint8_t i=0; i<len; i++) {
1319  if (_sendAndReceive(CMD_READ_MEMORY) == -1) break;
1320  if (_sendAndReceive(address + i) != 1) break;
1321  count++;
1322  buffer[i] = _buffer[0];
1323  }
1324  _setConfig(false);
1325  }
1326  return count;
1327  #endif
1328 }
1329 
1336 uint8_t AllWize::_getMemory(uint8_t address) {
1337  #if USE_MEMORY_CACHE
1338  if (!_ready) return 0;
1339  return _memory[address];
1340  #else
1341  uint8_t response = _sendCommand(CMD_READ_MEMORY, address);
1342  if (response > 0) return _buffer[0];
1343  return 0;
1344  #endif
1345 }
1346 
1354 bool AllWize::_setMemory(uint8_t address, uint8_t data) {
1355 
1356  // Check cached data
1357  #if USE_MEMORY_CACHE
1358  if (_memory[address] == data) return true;
1359  #endif
1360 
1361  // Build query buffer
1362  uint8_t buffer[3] = {address, data, (uint8_t)CMD_EXIT_MEMORY};
1363 
1364  // Execute command
1365  bool ret = (_sendCommand(CMD_WRITE_MEMORY, buffer, 3) != -1);
1366 
1367  // Update cached memory
1368  #if USE_MEMORY_CACHE
1369  if (ret) _memory[address] = data;
1370  #endif
1371 
1372  return ret;
1373 
1374 }
1375 
1384 bool AllWize::_setMemory(uint8_t address, uint8_t *data, uint8_t len) {
1385 
1386  // Check cached data
1387  #if USE_MEMORY_CACHE
1388  if (memcmp(&_memory[address], data, len) == 0) return true;
1389  #endif
1390 
1391  // Build query buffer
1392  uint8_t buffer[len * 2 + 1];
1393  for (uint8_t i = 0; i < len; i++) {
1394  buffer[i * 2] = address + i;
1395  buffer[i * 2 + 1] = data[i];
1396  }
1397  buffer[len * 2] = CMD_EXIT_MEMORY;
1398 
1399  // Execute command
1400  bool ret = (_sendCommand(CMD_WRITE_MEMORY, buffer, len * 2 + 1) != -1);
1401 
1402  // Update cached memory
1403  #if USE_MEMORY_CACHE
1404  if (ret) memcpy(&_memory[address], data, len);
1405  #endif
1406 
1407  return ret;
1408 
1409 }
1410 
1411 // ------------------------------------------------------------------------------------------------
1412 
1419 uint8_t AllWize::_getAddress(uint8_t slot) {
1420  if ((slot >= MEM_MAX_SLOTS) || (MODULE_UNKNOWN == _module)) {
1421  return 0xFF;
1422  }
1423  return MEM_ADDRESS[_module-1][slot];
1424 }
1425 
1434 bool AllWize::_setSlot(uint8_t slot, uint8_t *data, uint8_t len) {
1435  uint8_t address = _getAddress(slot);
1436  if (0xFF == address) return false;
1437  return _setMemory(address, data, len);
1438 }
1439 
1447 bool AllWize::_setSlot(uint8_t slot, uint8_t data) {
1448  uint8_t address = _getAddress(slot);
1449  if (0xFF == address) return false;
1450  return _setMemory(address, data);
1451 }
1452 
1461 uint8_t AllWize::_getSlot(uint8_t slot, uint8_t *buffer, uint8_t len) {
1462  uint8_t address = _getAddress(slot);
1463  if (0xFF == address) return 0;
1464  return _getMemory(address, buffer, len);
1465 }
1466 
1473 uint8_t AllWize::_getSlot(uint8_t slot) {
1474  uint8_t address = _getAddress(slot);
1475  if (0xFF == address) return 0;
1476  return _getMemory(address);
1477 }
1478 
1486 String AllWize::_getSlotAsHexString(uint8_t slot, uint8_t len) {
1487  uint8_t bin[len];
1488  char hex[2 * len + 1];
1489  hex[0] = 0;
1490  if (len == _getSlot(slot, bin, len)) {
1491  _bin2hex(bin, hex, len);
1492  }
1493  return String(hex);
1494 }
1495 
1503 String AllWize::_getSlotAsString(uint8_t slot, uint8_t len) {
1504  uint8_t bin[len];
1505  char hex[len + 1];
1506  hex[0] = 0;
1507  if (len == _getSlot(slot, bin, len)) {
1508  memcpy(hex, bin, len);
1509  hex[len - 1] = 0;
1510  }
1511  return String(hex);
1512 }
1513 
1514 // ------------------------------------------------------------------------------------------------
1515 
1543 
1544  #if defined(ALLWIZE_DEBUG_PORT)
1545  {
1546  char ch[4];
1547  ALLWIZE_DEBUG_PRINT("recv:");
1548  for (uint8_t i = 0; i < _pointer; i++) {
1549  snprintf(ch, sizeof(ch), " %02X", _buffer[i]);
1550  ALLWIZE_DEBUG_PRINT(ch);
1551  }
1553  }
1554  #endif
1555 
1556  // Local copy of the buffer
1557  uint8_t _local[RX_BUFFER_SIZE];
1558  memcpy(_local, _buffer, RX_BUFFER_SIZE);
1559 
1560  // Get current values
1561  uint8_t mbus_mode = getMode();
1562  uint8_t data_interface = getDataInterface();
1563  bool has_start = (data_interface & 0x04) == 0x04;
1564  bool has_header = (mbus_mode != MBUS_MODE_OSP) & ((data_interface & 0x01) == 0x00);
1565  bool has_rssi = getAppendRSSI();
1566  bool has_crc = (data_interface & 0x08) == 0x08;
1567  uint8_t bytes_not_in_len = has_start ? 3 : 1;
1568  uint8_t bytes_not_in_app = (has_header ? 9 : 0) + 1 + (has_rssi ? 1 : 0) + (has_crc ? 2 : 0);
1569 
1570  // This variable will contain the pointer to the current reading position
1571  uint8_t in = 0;
1572 
1573  // Start byte
1574  if (has_start) {
1575  if (START_BYTE != _local[in]) return false;
1576  in += 1;
1577  };
1578 
1579  // Get and check buffer length
1580  uint8_t len = _local[in];
1581  if (_pointer != len + bytes_not_in_len) return false;
1582  in += 1;
1583 
1584  if (has_header) {
1585 
1586  // C-field
1587  _message.c = _local[in];
1588  in += 1;
1589 
1590  // Manufacturer
1591  uint16_t man = (_local[in + 1] << 8) + _local[in];
1592  _message.man[0] = ((man >> 10) & 0x001F) + 64;
1593  _message.man[1] = ((man >> 5) & 0x001F) + 64;
1594  _message.man[2] = ((man >> 0) & 0x001F) + 64;
1595  _message.man[3] = 0;
1596  in += 2;
1597 
1598  // Address
1599  _message.address[0] = _local[in + 3];
1600  _message.address[1] = _local[in + 2];
1601  _message.address[2] = _local[in + 1];
1602  _message.address[3] = _local[in + 0];
1603 
1604  // Type
1605  _message.type = _local[in + 5];
1606 
1607  // Version
1608  _message.version = _local[in + 4];
1609 
1610  in += 6;
1611 
1612  } else {
1613  _message.c = 0xFF;
1614  _message.type = 0;
1615  _message.version = 0;
1616  _message.man[0] = 0;
1617  memset(_message.address, 0, 6);
1618  }
1619 
1620  // Control information
1621  _message.ci = _buffer[in];
1622  in += 1;
1623 
1624  // Wize transport layer
1625  if ((MODULE_WIZE == _module) && (_message.ci == CI_WIZE)) {
1626 
1627  bytes_not_in_app += 6;
1628 
1629  // Wize control
1631  in += 1;
1632 
1633  // Wize operator ID
1634  _message.wize_operator_id = (_local[in + 1] << 8) + _local[in];
1635  in += 2;
1636 
1637  // Wize counter
1638  _message.wize_counter = (_local[in + 1] << 8) + _local[in];
1639  in += 2;
1640 
1641  // Wize application
1643  in += 1;
1644 
1645  }
1646 
1647  // Application data
1648  _message.len = len - bytes_not_in_app;
1649  memcpy(_message.data, &_local[in], _message.len);
1650  _message.data[_message.len] = 0;
1651  in += _message.len;
1652 
1653  // RSSI
1654  if (has_rssi) {
1655  _message.rssi = _local[in];
1656  in += 1;
1657  } else {
1658  _message.rssi = 0xFF;
1659  }
1660 
1661  // CRC
1662  if (has_crc) {
1663  in += 2;
1664  }
1665 
1666  // Stop byte
1667  if (has_start) {
1668  if (STOP_BYTE != _local[in]) return false;
1669  }
1670 
1671  return true;
1672 
1673 }
1674 
1675 // -----------------------------------------------------------------------------
1676 
1682 
1683  // Flush TX line
1684  _stream->flush();
1685 
1686  // Flush RX line
1687  while (_stream->available()) _stream->read();
1688 
1689 }
1690 
1697 uint8_t AllWize::_send(uint8_t ch) {
1698  #if defined(ALLWIZE_DEBUG_PORT)
1699  {
1700  char buffer[10];
1701  snprintf(buffer, sizeof(buffer), "w %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1702  ALLWIZE_DEBUG_PRINTLN(buffer);
1703  }
1704  #endif
1705  return _stream->write(ch);
1706 }
1707 
1715 uint8_t AllWize::_send(uint8_t *buffer, uint8_t len) {
1716  uint8_t n = 0;
1717  for (uint8_t i = 0; i < len; i++) {
1718  if (_send(buffer[i])) n++;
1719  }
1720  return n;
1721 }
1722 
1730 }
1731 
1739 int8_t AllWize::_sendAndReceive(uint8_t *buffer, uint8_t len) {
1740  if (_send(buffer, len) != len) return -1;
1741  return _receive();
1742 }
1743 
1750 int8_t AllWize::_sendAndReceive(uint8_t ch) {
1751  if (_send(ch) != 1) return -1;
1752  return _receive();
1753 }
1754 
1761 
1762  uint32_t _start = millis();
1763  int ch = -1;
1764  while (millis() - _start < _timeout) {
1765 #if defined(ARDUINO_ARCH_ESP8266)
1766  yield();
1767 #endif
1768  ch = _stream->read();
1769  if (ch >= 0) break;
1770  };
1771 
1772  #if defined(ALLWIZE_DEBUG_PORT)
1773  /*
1774  {
1775  if (ch < 0) {
1776  ALLWIZE_DEBUG_PRINTLN("r TIMEOUT");
1777  } else {
1778  char buffer[10];
1779  snprintf(buffer, sizeof(buffer), "r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1780  ALLWIZE_DEBUG_PRINTLN(buffer);
1781  }
1782  }
1783  */
1784  #endif
1785 
1786  return ch;
1787 }
1788 
1796 int AllWize::_readBytes(char * data, uint16_t len) {
1797 
1798  if (len < 1) return 0;
1799 
1800  uint16_t index = 0;
1801  while (index < len) {
1802  int ch = _timedRead();
1803  if (ch < 0) return -1;
1804  *data++ = (char)ch;
1805  index++;
1806  }
1807 
1808  return index;
1809 
1810 }
1811 
1820 int AllWize::_readBytesUntil(char terminator, char *data, uint16_t len) {
1821 
1822  if (len < 1) return 0;
1823 
1824  uint16_t index = 0;
1825  while (index < len) {
1826  int ch = _timedRead();
1827  if (ch < 0) return -1;
1828  if (ch == terminator) break;
1829  *data++ = (char) ch;
1830  index++;
1831  }
1832 
1833  return index;
1834 
1835 }
1836 
1844 void AllWize::_hex2bin(char *hex, uint8_t *bin, uint8_t len) {
1845  for (uint8_t i = 0; i < len; i += 2) {
1846  bin[i / 2] = ((hex[i] - '0') * 16 + (hex[i + 1] - '0')) & 0xFF;
1847  }
1848 }
1849 
1857 void AllWize::_bin2hex(uint8_t *bin, char *hex, uint8_t len) {
1858  for (uint8_t i = 0; i < len; i++) {
1859  sprintf(&hex[i * 2], "%02X", bin[i]);
1860  }
1861 }
uint8_t _data_interface
Definition: AllWize.h:258
String getModuleTypeName()
Returns the module type.
Definition: AllWize.cpp:1113
#define STOP_BYTE
Definition: RC1701HP.h:25
uint8_t _access_number
Definition: AllWize.h:260
#define CMD_VOLTAGE
Definition: RC1701HP.h:57
uint8_t getChannel()
Gets the channel stored in non-volatile memory.
Definition: AllWize.cpp:591
#define MBUS_MODE_OSP
Definition: RC1701HP.h:211
void setInstallMode(uint8_t mode, bool persist=false)
Sets the module in one of the available operations modes.
Definition: AllWize.cpp:832
uint8_t rssi
Definition: AllWize.h:63
uint8_t data[RX_BUFFER_SIZE]
Definition: AllWize.h:62
#define DATARATE_6400bps
Definition: RC1701HP.h:188
bool enableRX(bool enable)
Enables or disables RF recever.
Definition: AllWize.cpp:443
#define PREAMBLE_FORMAT_A
Definition: RC1701HP.h:250
uint16_t wize_operator_id
Definition: AllWize.h:65
uint8_t version
Definition: AllWize.h:59
String _model
Definition: AllWize.h:269
bool setUID(uint32_t uid)
Saved the UID into the module memory.
Definition: AllWize.cpp:1028
bool ack()
Sends an ACK.
Definition: AllWize.cpp:432
void setEncryptFlag(uint8_t flag)
Sets the encrypt flag setting.
Definition: AllWize.cpp:853
uint8_t getSleepMode()
Gets the sleep mode stored in non-volatile memory.
Definition: AllWize.cpp:693
#define MODULE_SIGNATURE
Definition: RC1701HP.h:7
uint8_t getLEDControl()
Gets the current LED control.
Definition: AllWize.cpp:784
uint8_t getPower()
Gets the RF power stored in non-volatile memory.
Definition: AllWize.cpp:613
#define DATA_INTERFACE_START_STOP
Definition: RC1701HP.h:245
uint8_t len
Definition: AllWize.h:61
void setWizeOperatorId(uint16_t wize_operator_id)
Sets the wize operator ID field in the transpoprt layer.
Definition: AllWize.cpp:523
#define CMD_INSTALL_MODE
Definition: RC1701HP.h:45
String _fw
Definition: AllWize.h:271
int _readBytes(char *buffer, uint16_t len)
Reads the stream buffer up to a number of bytes.
Definition: AllWize.cpp:1796
#define CI_WIZE
Definition: OMS.h:22
void setWizeApplication(uint8_t wize_application)
Sets the wize applicaton field in the transpoprt layer.
Definition: AllWize.cpp:531
uint8_t getEncryptFlag()
Gets the encrypt flag setting.
Definition: AllWize.cpp:863
void setVersion(uint8_t version)
Sets the device version.
Definition: AllWize.cpp:1049
void slave()
Sets the module in slave mode.
Definition: AllWize.cpp:249
allwize_message_t _message
Definition: AllWize.h:280
void setPower(uint8_t power, bool persist=false)
Sets the RF power.
Definition: AllWize.cpp:600
char man[4]
Definition: AllWize.h:57
#define INSTALL_MODE_HOST
Definition: RC1701HP.h:216
void setNetworkRole(uint8_t role)
Sets the network role.
Definition: AllWize.cpp:759
uint8_t _wize_application
Definition: AllWize.h:276
#define HARDWARE_SERIAL_PORT
Definition: AllWize.h:47
uint32_t getBaudRateSpeed(uint8_t value)
Gets the UART baud rate speed in bps.
Definition: AllWize.cpp:944
bool getAppendRSSI()
Gets the current RSSI mode value.
Definition: AllWize.cpp:714
bool _decode()
Decodes the current RX buffer contents.
Definition: AllWize.cpp:1542
#define CMD_EXIT_CONFIG
Definition: RC1701HP.h:29
#define CMD_KEY_REGISTER
Definition: RC1701HP.h:46
uint8_t getVersion()
Returns the device version from non-volatile memory.
Definition: AllWize.cpp:1041
void setControlField(uint8_t value, bool persist=false)
Sets the control field value.
Definition: AllWize.cpp:812
void begin(uint8_t baudrate=MODEM_DEFAULT_BAUDRATE)
Inits the module communications.
Definition: AllWize.cpp:91
int8_t _sendCommand(uint8_t command, uint8_t *data, uint8_t len)
Sends a command with the given data.
Definition: AllWize.cpp:1200
void setMode(uint8_t mode, bool persist=false)
Sets the module in one of the available MBus modes.
Definition: AllWize.cpp:655
void dump(Stream &debug)
Dumps the current memory configuration to the given stream.
Definition: AllWize.cpp:307
#define CMD_CHANNEL
Definition: RC1701HP.h:40
#define CMD_TEMPERATURE
Definition: RC1701HP.h:56
#define ALLWIZE_DEBUG_PRINT(...)
Definition: AllWize.h:83
uint8_t address[4]
Definition: AllWize.h:60
String getRequiredHardwareVersion()
Returns the minimum required hardware version to run the current firmware.
Definition: AllWize.cpp:1081
void setPreamble(uint8_t preamble)
Sets the preamble length frame format.
Definition: AllWize.cpp:722
#define NETWORK_ROLE_REPEATER
Definition: RC1701HP.h:228
uint8_t getControlField()
Gets the control field value stored in non-volatile memory.
Definition: AllWize.cpp:823
uint16_t _counter
Definition: AllWize.h:277
uint8_t getPreamble()
Gets the preamble length frame format.
Definition: AllWize.cpp:732
int8_t _receive()
Listens to incoming data from the module until timeout or END_OF_RESPONSE.
Definition: AllWize.cpp:1728
uint16_t _wize_operator_id
Definition: AllWize.h:275
bool send(uint8_t *buffer, uint8_t len)
Sends a byte array.
Definition: AllWize.cpp:366
#define CMD_READ_MEMORY
Definition: RC1701HP.h:59
void setCounter(uint16_t counter)
Sets the wize couonter field in the transpoprt layer.
Definition: AllWize.cpp:539
void softReset()
Cleans the RX/TX line.
Definition: AllWize.cpp:197
bool _ready
Definition: AllWize.h:265
uint8_t _getSlot(uint8_t slot)
Returns the contents of single-byte memory slot.
Definition: AllWize.cpp:1473
#define CMD_ACCESS_NUMBER
Definition: RC1701HP.h:49
uint8_t _buffer[RX_BUFFER_SIZE]
Definition: AllWize.h:281
String getUID()
Returns the Unique ID string.
Definition: AllWize.cpp:1020
#define CMD_AWAKE
Definition: RC1701HP.h:34
uint16_t wize_counter
Definition: AllWize.h:66
uint8_t getTemperature()
Returns the internal temperature of the module.
Definition: AllWize.cpp:968
#define DEFAULT_MBUS_MODE
Definition: AllWize.h:48
#define MBUS_MODE_N2
Definition: RC1701HP.h:209
bool ready()
Test whether the radio module is ready or not.
Definition: AllWize.cpp:285
void setSleepMode(uint8_t mode)
Sets the sleep mode.
Definition: AllWize.cpp:685
#define POWER_20dBm
Definition: RC1701HP.h:197
uint8_t getDecryptFlag()
Gets the decrypt flag setting.
Definition: AllWize.cpp:879
void setControlInformation(uint8_t ci)
Sets the control information byte.
Definition: AllWize.cpp:559
void wakeup()
Wakes up the radio from sleep mode.
Definition: AllWize.cpp:276
void master()
Sets the module in master mode.
Definition: AllWize.cpp:234
bool available()
Returns true if a new message has been received and decoded This method has to be called in the main ...
Definition: AllWize.cpp:458
String _getSlotAsHexString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as an HEX String.
Definition: AllWize.cpp:1486
#define CMD_RSSI
Definition: RC1701HP.h:54
uint8_t getDevice()
Returns the device type from non-volatile memory.
Definition: AllWize.cpp:1057
#define CMD_TEST_MODE_0
Definition: RC1701HP.h:62
void setLEDControl(uint8_t value)
Sets the LED control.
Definition: AllWize.cpp:775
#define CMD_CONTROL_FIELD
Definition: RC1701HP.h:43
int8_t _sendAndReceive(uint8_t *buffer, uint8_t len)
Sends a binary buffer and waits for response. Returns the number of bytes received and stored in the ...
Definition: AllWize.cpp:1739
uint8_t _pointer
Definition: AllWize.h:282
#define CMD_IDLE_DISABLE_RF
Definition: RC1701HP.h:33
void setDecryptFlag(uint8_t flag)
Sets the decrypt flag setting.
Definition: AllWize.cpp:871
uint32_t _baudrate
Definition: AllWize.h:254
String getPartNumber()
Returns the module part number.
Definition: AllWize.cpp:1073
uint8_t _wize_control
Definition: AllWize.h:274
uint8_t ci
Definition: AllWize.h:56
#define CMD_MBUS_MODE
Definition: RC1701HP.h:44
uint8_t getModuleType()
Returns the module type.
Definition: AllWize.cpp:1105
void _reset_serial()
Resets the serial object.
Definition: AllWize.cpp:120
#define GPIO_NONE
Definition: AllWize.h:44
static const uint32_t DATARATES[4]
Definition: RC1701HP.h:192
void repeater()
Sets the module in repeater mode.
Definition: AllWize.cpp:260
bool waitForReady(uint32_t timeout=DEFAULT_TIMEOUT)
Waits for timeout millis for the module to be ready.
Definition: AllWize.cpp:294
void setTimeout(uint16_t ms)
Sets the buffer timeout (also used for auto sleep modes)
Definition: AllWize.cpp:740
uint8_t getNetworkRole()
Gets the current network role.
Definition: AllWize.cpp:767
bool setWizeControl(uint8_t wize_control)
Sets the wize control field in the transpoprt layer.
Definition: AllWize.cpp:513
uint8_t wize_control
Definition: AllWize.h:64
bool _config
Definition: AllWize.h:252
int8_t _tx
Definition: AllWize.h:238
String _getSlotAsString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as a String object.
Definition: AllWize.cpp:1503
int8_t _rx
Definition: AllWize.h:237
static const uint32_t BAUDRATES[11]
Definition: RC1701HP.h:266
uint32_t _timeout
Definition: AllWize.h:253
#define END_OF_RESPONSE
Definition: RC1701HP.h:23
uint16_t getCounter()
Gets the current wize counter.
Definition: AllWize.cpp:547
#define START_BYTE
Definition: RC1701HP.h:24
AllWize(HardwareSerial *serial, uint8_t reset_gpio=GPIO_NONE, uint8_t config_gpio=GPIO_NONE)
AllWize object constructor.
Definition: AllWize.cpp:40
int _readBytesUntil(char terminator, char *buffer, uint16_t len)
Reads the stream buffer up to a certain char or times out.
Definition: AllWize.cpp:1820
uint8_t _getMemory(uint8_t address)
Returns the contents of memory address.
Definition: AllWize.cpp:1336
void sleep()
Sets the radio module in sleep mode.
Definition: AllWize.cpp:268
String getFirmwareVersion()
Returns the module firmware revision.
Definition: AllWize.cpp:1089
#define C_SND_NR
Definition: OMS.h:14
#define CMD_SLEEP
Definition: RC1701HP.h:60
bool _cacheMemory(uint8_t *buffer)
Reads and caches the module memory.
Definition: AllWize.cpp:1249
#define CMD_IDLE_ENABLE_RF
Definition: RC1701HP.h:32
#define NETWORK_ROLE_MASTER
Definition: RC1701HP.h:227
#define CMD_WRITE_MEMORY
Definition: RC1701HP.h:48
bool _setConfig(bool value)
Sets or unsets config mode.
Definition: AllWize.cpp:1171
#define CMD_EXIT_MEMORY
Definition: RC1701HP.h:35
double getFrequency(uint8_t channel)
Returns the frequency for the given channel.
Definition: AllWize.cpp:1128
String getMID()
Returns the Manufacturer ID.
Definition: AllWize.cpp:1001
void setChannel(uint8_t channel, bool persist=false)
Sets the communications channel (for MBUS_MODE_R2 only)
Definition: AllWize.cpp:576
void _init()
Definition: AllWize.cpp:75
#define NETWORK_ROLE_SLAVE
Definition: RC1701HP.h:226
void setAppendRSSI(bool value)
Sets the RSSI mode value.
Definition: AllWize.cpp:701
uint8_t wize_application
Definition: AllWize.h:67
String _hw
Definition: AllWize.h:270
#define CMD_RF_POWER
Definition: RC1701HP.h:51
String getSerialNumber()
Returns the module serial number.
Definition: AllWize.cpp:1097
#define PREAMBLE_FORMAT_B
Definition: RC1701HP.h:251
#define SLEEP_MODE_DISABLE
Definition: RC1701HP.h:219
uint8_t type
Definition: AllWize.h:58
void getDefaultKey(uint8_t *key)
Gets the default encryption key.
Definition: AllWize.cpp:908
#define RX_BUFFER_SIZE
Definition: AllWize.h:45
void _readModel()
Searches for the module model.
Definition: AllWize.cpp:1264
uint8_t getMode()
Gets the MBus mode stored in non-volatile memory.
Definition: AllWize.cpp:677
uint8_t getControlInformation()
Gets the control information byte.
Definition: AllWize.cpp:567
uint8_t _module
Definition: AllWize.h:261
void setDataInterface(uint8_t value)
Sets the data interface for receiving packets.
Definition: AllWize.cpp:792
int _timedRead()
Reads a byte from the stream with a timeout.
Definition: AllWize.cpp:1760
void setKey(uint8_t reg, const uint8_t *key)
Sets the default encryption key.
Definition: AllWize.cpp:888
void setDefaultKey(const uint8_t *key)
Sets the default encryption key.
Definition: AllWize.cpp:900
uint8_t getInstallMode()
Gets the install modevalue stored in non-volatile memory.
Definition: AllWize.cpp:845
void setAccessNumber(uint8_t value)
Sets new/specific access number.
Definition: AllWize.cpp:916
SoftwareSerial * _sw_serial
Definition: AllWize.h:247
bool reset()
Resets the radio module.
Definition: AllWize.cpp:162
uint8_t _reset_gpio
Definition: AllWize.h:250
bool _setSlot(uint8_t slot, uint8_t data)
Sets non-volatile memory contents starting from given address.
Definition: AllWize.cpp:1447
bool setMID(uint16_t mid)
Sets the Manufacturer ID.
Definition: AllWize.cpp:1009
float getRSSI()
Returns the RSSI of the last valid packet received TODO: values do not seem right and are not the sam...
Definition: AllWize.cpp:958
bool _append_rssi
Definition: AllWize.h:259
#define DATARATE_2400bps
Definition: RC1701HP.h:186
void _bin2hex(uint8_t *bin, char *hex, uint8_t len)
Converts a binary buffer to an hex c-string.
Definition: AllWize.cpp:1857
uint8_t _memory[0x100]
Definition: AllWize.h:266
#define DATARATE_6400bps_OSP
Definition: RC1701HP.h:190
uint8_t _getAddress(uint8_t slot)
Return the physical memory address for the given slot.
Definition: AllWize.cpp:1419
Stream * _stream
Definition: AllWize.h:240
bool _setMemory(uint8_t address, uint8_t data)
Sets non-volatile memory contents starting from given address.
Definition: AllWize.cpp:1354
uint8_t _send(uint8_t *buffer, uint8_t len)
Sends a binary buffer to the module UART. Returns the number of bytes actually sent.
Definition: AllWize.cpp:1715
#define MBUS_MODE_N1
Definition: RC1701HP.h:210
static const uint8_t MEM_ADDRESS[MODULE_MAX-1][MEM_MAX_SLOTS]
Definition: RC1701HP.h:114
uint16_t getTimeout()
Gets the current buffer timeout (also used for auto sleep modes)
Definition: AllWize.cpp:750
#define C_ACK
Definition: OMS.h:13
void _hex2bin(char *hex, uint8_t *bin, uint8_t len)
Converts a hex c-string to a binary buffer.
Definition: AllWize.cpp:1844
uint16_t getDataRateSpeed(uint8_t dr)
Returns the speed for te given datarate.
Definition: AllWize.cpp:1153
void _flush()
Flushes the serial line to the module.
Definition: AllWize.cpp:1681
uint8_t getDataInterface()
Gets the data interface for receiving packets.
Definition: AllWize.cpp:803
void setDataRate(uint8_t dr)
Sets the data rate.
Definition: AllWize.cpp:621
void setBaudRate(uint8_t baudrate)
Sets the UART baud rate, requires reset to take effect.
Definition: AllWize.cpp:924
HardwareSerial * _hw_serial
Definition: AllWize.h:241
uint8_t getDataRate()
Gets the data rate stored in non-volatile memory.
Definition: AllWize.cpp:646
allwize_message_t read()
Returns latest received message.
Definition: AllWize.cpp:504
#define CMD_ENTER_CONFIG
Definition: RC1701HP.h:28
void setDevice(uint8_t type)
Sets the device type.
Definition: AllWize.cpp:1065
uint8_t getBaudRate()
Gets the UART baud rate.
Definition: AllWize.cpp:935
uint8_t _mbus_mode
Definition: AllWize.h:257
uint8_t _config_gpio
Definition: AllWize.h:251
uint16_t getVoltage()
Returns the internal voltage of the module.
Definition: AllWize.cpp:985
#define ALLWIZE_DEBUG_PRINTLN(...)
Definition: AllWize.h:84
bool factoryReset()
Resets the module to factory settings.
Definition: AllWize.cpp:212
uint8_t _ci
Definition: AllWize.h:256