AllWize Library
AllWize.cpp
Go to the documentation of this file.
1 /*
2 
3 AllWize Library
4 
5 Copyright (C) 2018-2020 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  _niceDelay(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  _resetSerial();
165  _niceDelay(100);
167  if (_setConfig(true)) {
168  _send('@');
169  _send('R');
170  _send('R');
171  _niceDelay(100);
172  if (GPIO_NONE != _config_gpio) {
173  digitalWrite(_config_gpio, LOW);
174  }
175  _config = false;
176  _resetSerial();
177  return true;
178  }
179  } else {
180  digitalWrite(_reset_gpio, LOW);
181  _niceDelay(1);
182  digitalWrite(_reset_gpio, HIGH);
183  _niceDelay(100);
184  if (GPIO_NONE != _config_gpio) {
185  digitalWrite(_config_gpio, LOW);
186  }
187  _config = false;
188  _resetSerial();
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  _niceDelay(10);
205  */
206 }
207 
213  _resetSerial();
214  _niceDelay(100);
216  if (_setConfig(true)) {
217  _send('@');
218  _send('R');
219  _send('C');
220  _niceDelay(100);
221  if (GPIO_NONE != _config_gpio) {
222  digitalWrite(_config_gpio, LOW);
223  }
224  _config = false;
225  _resetSerial();
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  _niceDelay(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  _niceDelay(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 if 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) + 5 (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 += 5;
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 & 0xFF); // Wize Control
397  _send(_wize_network_id & 0xFF); // Network ID HIGH
398  _send((_counter >> 0) & 0xFF); // Frame counter LOW
399  _send((_counter >> 8) & 0xFF); // Frame counter HIGH
400  _send(_wize_application); // Wize app indicator
401  }
402 
403  // application payload
404  if (len != _send(buffer, len)) return false;
405 
406  // timestamp, TODO: add option to provide a timestamp
407  if (MODULE_WIZE == _module) {
408  _send(0);
409  _send(0);
410  }
411 
412  _access_number++;
413  _counter++;
414  return true;
415 
416 }
417 
423 bool AllWize::send(const char *buffer) {
424  return send((uint8_t *)buffer, strlen(buffer));
425 }
426 
431 bool AllWize::ack() {
432  if (_config) return false;
434  return send("ACK");
435 }
436 
442 bool AllWize::enableRX(bool enable) {
443  if (_config) return false;
444  if (enable) {
446  } else {
448  }
449  return true;
450 }
451 
458 
459  bool response = false;
460 
461  static uint32_t when = millis();
462 
463  while (_stream->available() && _pointer < RX_BUFFER_SIZE) {
464 
465  uint8_t ch = _stream->read();
466 
467  #if defined(ALLWIZE_DEBUG_PORT)
468  {
469  char buffer[10];
470  snprintf(buffer, sizeof(buffer), "r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
471  ALLWIZE_DEBUG_PRINTLN(buffer);
472  }
473  #endif
474 
475  _buffer[_pointer++] = ch;
476  when = millis();
477 
478  #if defined(ARDUINO_ARCH_ESP8266)
479  yield();
480  #endif
481 
482  }
483 
484  // Check if message finished and decode it
485  if ((_pointer > 0) && (millis() - when > 100)) {
486 
487  response = _decode();
488  _pointer = 0;
489 
490  // If we don't soft-reset the line the RX channel gets stalled
491  softReset();
492 
493  }
494 
495  return response;
496 
497 }
498 
504  return _message;
505 }
506 
512 bool AllWize::setWizeControl(uint8_t wize_control) {
513  if (wize_control > 14) return false;
514  _wize_control = wize_control;
515  return true;
516 }
517 
522 void AllWize::setWizeOperatorId(uint16_t wize_network_id) {
523  setWizeNetworkId(wize_network_id);
524 }
525 
530 void AllWize::setWizeNetworkId(uint16_t wize_network_id) {
531  _wize_network_id = wize_network_id;
532 }
533 
538 void AllWize::setWizeApplication(uint8_t wize_application) {
539  _wize_application = wize_application;
540 }
541 
546 void AllWize::setCounter(uint16_t counter) {
547  _counter = counter;
548 }
549 
554 uint16_t AllWize::getCounter() {
555  return _counter;
556 }
557 
558 // -----------------------------------------------------------------------------
559 // Configuration
560 // -----------------------------------------------------------------------------
561 
567  _ci = ci;
568 }
569 
575  return _ci;
576 }
577 
583 void AllWize::setChannel(uint8_t channel, bool persist) {
584  if (channel > 41) return;
585  if (persist) {
586  _setSlot(MEM_CHANNEL, channel);
587  if (MODULE_WIZE == _module) {
588  _setSlot(MEM_CHANNEL_RX, channel);
589  }
590  }
591  _sendCommand(CMD_CHANNEL, channel);
592 }
593 
599  return _getSlot(MEM_CHANNEL);
600 }
601 
607 void AllWize::setPower(uint8_t power, bool persist) {
608  if (0 < power && power < 6) {
609  if (persist) {
610  _setSlot(MEM_RF_POWER, power);
611  }
612  _sendCommand(CMD_RF_POWER, power);
613  }
614 }
615 
620 uint8_t AllWize::getPower() {
621  return _getSlot(MEM_RF_POWER);
622 }
623 
628 void AllWize::setDataRate(uint8_t dr) {
629 
630  if (_module == MODULE_MBUS4) return;
631  if (dr < 1) return;
632  if (_module == MODULE_OSP) {
633  if (DATARATE_6400bps == dr) {
635  }
636  if (dr > 5) return;
637  }
638  if (_module == MODULE_WIZE) {
639  if (dr > 3) return;
640  }
641 
642  _setSlot(MEM_DATA_RATE, dr);
643  if (MODULE_WIZE == _module) {
645  }
646 
647 }
648 
654  return _getSlot(MEM_DATA_RATE);
655 }
656 
662 void AllWize::setMode(uint8_t mode, bool persist) {
663 
664  // Wize FW accepts only modes 0x10 and 0x11
665  if (MODULE_WIZE == _module) {
666  if ((MBUS_MODE_N1 != mode) && (MBUS_MODE_N2 != mode)) return;
667  }
668 
669  // Only OSP FW accepts mode 0x12
670  if ((MBUS_MODE_OSP == mode) && (MODULE_OSP != _module)) return;
671 
672  if (persist) {
673  _setSlot(MEM_MBUS_MODE, mode);
674  }
676  _mbus_mode = mode;
677 
678 }
679 
684 uint8_t AllWize::getMode() {
685  return _mbus_mode;
686 }
687 
692 void AllWize::setSleepMode(uint8_t mode) {
693  _setSlot(MEM_SLEEP_MODE, mode);
694 }
695 
701  return _getSlot(MEM_SLEEP_MODE);
702 }
703 
708 void AllWize::setAppendRSSI(bool value) {
709  if (value == 1) {
711  } else {
713  }
714  _append_rssi = value;
715 }
716 
722  return _append_rssi;
723 }
724 
729 void AllWize::setPreamble(uint8_t preamble) {
730  if (PREAMBLE_FORMAT_A == preamble || PREAMBLE_FORMAT_B == preamble) {
731  _setSlot(MEM_PREAMBLE_LENGTH, preamble);
732  }
733 }
734 
741 }
742 
747 void AllWize::setTimeout(uint16_t ms) {
748  if (ms > 4080) return;
749  uint8_t timeout = (ms / 16) - 1;
750  _setSlot(MEM_TIMEOUT, timeout);
751 }
752 
758  uint8_t timeout = _getSlot(MEM_TIMEOUT);
759  return 16 * (uint16_t) (timeout + 1);
760 }
761 
766 void AllWize::setNetworkRole(uint8_t role) {
767  _setSlot(MEM_NETWORK_ROLE, role);
768 }
769 
775  return _getSlot(MEM_NETWORK_ROLE);
776 }
777 
782 void AllWize::setLEDControl(uint8_t value) {
783  if (value > 3) return;
784  _setSlot(MEM_LED_CONTROL, value);
785 }
786 
792  return _getSlot(MEM_LED_CONTROL);
793 }
794 
799 void AllWize::setDataInterface(uint8_t value) {
800  if (value <= 0x0C) {
802  _data_interface = value;
803  }
804 }
805 
811  return _data_interface;
812 }
813 
819 void AllWize::setControlField(uint8_t value, bool persist) {
820  if (persist) {
821  _setSlot(MEM_CONTROL_FIELD, value);
822  }
824 }
825 
831  return _getSlot(MEM_CONTROL_FIELD);
832 }
833 
839 void AllWize::setInstallMode(uint8_t mode, bool persist) {
840  if (mode <= 2) {
841  if (persist) {
842  _setSlot(MEM_INSTALL_MODE, mode);
843  }
845  }
846 }
847 
853  return _getSlot(MEM_INSTALL_MODE);
854 }
855 
860 void AllWize::setEncryptFlag(uint8_t flag) {
861  if (0 == flag || 1 == flag || 3 == flag) {
862  _setSlot(MEM_ENCRYPT_FLAG, flag);
863  }
864 }
865 
871  return _getSlot(MEM_ENCRYPT_FLAG);
872 }
873 
878 void AllWize::setDecryptFlag(uint8_t flag) {
879  _setSlot(MEM_DECRYPT_FLAG, flag);
880 }
881 
887  return _getSlot(MEM_DECRYPT_FLAG);
888 }
889 
895 void AllWize::setKey(uint8_t reg, const uint8_t *key) {
896  if (reg > 128) return;
897  uint8_t data[17];
898  data[0] = reg;
899  memcpy(&data[1], key, 16);
900  _sendCommand(CMD_KEY_REGISTER, data, 17);
901 }
902 
907 void AllWize::setDefaultKey(const uint8_t *key) {
908  _setSlot(MEM_DEFAULT_KEY, (uint8_t *)key, 16);
909 }
910 
915 void AllWize::getDefaultKey(uint8_t *key) {
916  _getSlot(MEM_DEFAULT_KEY, key, 16);
917 }
918 
923 void AllWize::setAccessNumber(uint8_t value) {
925 }
926 
931 void AllWize::setBaudRate(uint8_t value) {
932  if ((0 < value) & (value < 12)) {
933  if (ready()) _setSlot(MEM_UART_BAUD_RATE, value);
934  _baudrate = BAUDRATES[value-1];
935  }
936 }
937 
944 }
945 
951 uint32_t AllWize::getBaudRateSpeed(uint8_t value) {
952  if ((0 < value) & (value < 12)) {
953  return BAUDRATES[value-1];
954  }
955  return 0;
956 }
957 
958 // -----------------------------------------------------------------------------
959 
966  uint8_t response = _sendCommand(CMD_RSSI);
967  if (response > 0) return -0.5 * (float) _buffer[0];
968  return 0;
969 }
970 
976  uint8_t response = _sendCommand(CMD_TEMPERATURE);
977  uint8_t ret_val = 0;
978 
979  if (response > 0) {
980  ret_val = _buffer[0] - 128;
981  } else {
982  ret_val = 0;
983  }
984 
985  return ret_val;
986 }
987 
993  uint8_t response = _sendCommand(CMD_VOLTAGE);
994  uint16_t ret_val;
995  if (response > 0) {
996  ret_val = 30 * _buffer[0];
997  } else {
998  ret_val = 0;
999  }
1000 
1001  return ret_val;
1002 }
1003 
1010 }
1011 
1016 bool AllWize::setMID(uint16_t mid) {
1017  uint8_t buffer[2];
1018  buffer[0] = (mid >> 8) & 0xFF;
1019  buffer[1] = (mid >> 0) & 0xFF;
1020  return _setSlot(MEM_MANUFACTURER_ID, buffer, 2);
1021 }
1022 
1029 }
1030 
1035 bool AllWize::setUID(uint32_t uid) {
1036  uint8_t buffer[4];
1037  buffer[0] = (uid >> 24) & 0xFF;
1038  buffer[1] = (uid >> 16) & 0xFF;
1039  buffer[2] = (uid >> 8) & 0xFF;
1040  buffer[3] = (uid >> 0) & 0xFF;
1041  return _setSlot(MEM_UNIQUE_ID, buffer, 4);
1042 }
1043 
1049  return _getSlot(MEM_VERSION);
1050 }
1051 
1056 void AllWize::setVersion(uint8_t version) {
1057  _setSlot(MEM_VERSION, version);
1058 }
1059 
1065  return _getSlot(MEM_DEVICE);
1066 }
1067 
1072 void AllWize::setDevice(uint8_t type) {
1073  _setSlot(MEM_DEVICE, type);
1074 }
1075 
1081  return _model;
1082 }
1083 
1089  return _hw;
1090 }
1091 
1097  return _fw;
1098 }
1099 
1106 }
1107 
1113  return _module;
1114 }
1115 
1121  switch (_module) {
1122  case MODULE_MBUS4: return String("MBUS4");
1123  case MODULE_OSP: return String("OSP");
1124  case MODULE_WIZE: return String("WIZE");
1125  }
1126  return String("Unknown");
1127 }
1128 
1129 
1135 double AllWize::getFrequency(uint8_t channel) {
1136  if (channel < 7) {
1137  return 169.40625 + 0.0125 * (channel - 1);
1138  } else if (channel == 7) {
1139  return 169.41250;
1140  } else if (channel == 8) {
1141  return 169.43750;
1142  } else if (channel == 9) {
1143  return 169.46250;
1144  } else if (channel == 10) {
1145  return 169.43750;
1146  } else if (channel < 38) {
1147  return 169.48125 + 0.0125 * (channel - 11);
1148  } else if (channel < 42) {
1149  return 169.62500 + 0.0500 * (channel - 38);
1150  } else {
1151  return 0;
1152  }
1153 }
1154 
1160 uint16_t AllWize::getDataRateSpeed(uint8_t dr) {
1161  if (dr == DATARATE_6400bps_OSP) dr = DATARATE_6400bps;
1162  if ((0 < dr) && (dr < 5)) {
1163  return DATARATES[dr-1];
1164  }
1165  return 0;
1166 }
1167 
1168 // -----------------------------------------------------------------------------
1169 // Protected
1170 // -----------------------------------------------------------------------------
1171 
1178 bool AllWize::_setConfig(bool value) {
1179  if (value != _config) {
1180  _flush();
1181  if (value) {
1182  if (GPIO_NONE != _config_gpio) {
1183  digitalWrite(_config_gpio, HIGH);
1184  _config = true;
1185  } else {
1187  }
1188  } else {
1189  if (GPIO_NONE != _config_gpio) {
1190  digitalWrite(_config_gpio, LOW);
1191  }
1193  _niceDelay(5);
1194  _config = false;
1195  }
1196  }
1197  return _config;
1198 }
1199 
1207 int8_t AllWize::_sendCommand(uint8_t command, uint8_t *data, uint8_t len) {
1208  int8_t response = -1;
1209  if (!_setConfig(true)) return response;
1210  if (_sendAndReceive(command) != -1) {
1211  response = _sendAndReceive(data, len);
1212  }
1213  _setConfig(false);
1214  return response;
1215 }
1216 
1224 int8_t AllWize::_sendCommand(uint8_t command, uint8_t data) {
1225  int8_t response = -1;
1226  if (!_setConfig(true)) return response;
1227  if (_sendAndReceive(command) != -1) {
1228  response = _sendAndReceive(data);
1229  }
1230  _setConfig(false);
1231  return response;
1232 }
1233 
1240 int8_t AllWize::_sendCommand(uint8_t command) {
1241  int8_t response = -1;
1242  if (!_setConfig(true)) return response;
1243  response = _sendAndReceive(command);
1244  _setConfig(false);
1245  return response;
1246 }
1247 
1248 // ------------------------------------------------------------------------------------------------
1249 
1256 bool AllWize::_cacheMemory(uint8_t * buffer) {
1257 
1258  // Read memory
1259  _setConfig(true);
1261  bool ret = (256 == _readBytes((char *) buffer, 256));
1262  _setConfig(false);
1263  return ret;
1264 
1265 }
1266 
1272 
1273  #if not USE_MEMORY_CACHE
1274  uint8_t _memory[0x100] = {0xFF};
1275  bool _ready = _cacheMemory(_memory);
1276  if (!_ready) return;
1277  #endif
1278 
1279  // Look for the part_number
1280  bool found = false;
1281  uint8_t index = 0;
1282  uint8_t len = strlen(MODULE_SIGNATURE);
1283  for (index=0; index<0xFF-32; index++) {
1284  if (memcmp(&_memory[index], (uint8_t *) MODULE_SIGNATURE, len) == 0) {
1285  found = true;
1286  break;
1287  }
1288  }
1289 
1290  // Parse signature
1291  if (found) {
1292 
1293  String part_number = String((char *) &_memory[index]);
1294  part_number.substring(0, 31);
1295  part_number.trim();
1296 
1297  uint8_t end = part_number.indexOf(",");
1298  _model = part_number.substring(0, end);
1299  uint8_t start = end + 1;
1300  end = part_number.indexOf(",", start);
1301  _hw = part_number.substring(start, end);
1302  _fw = part_number.substring(end + 1, 32);
1303  _fw.trim();
1304 
1305  }
1306 
1307 }
1308 
1317 uint8_t AllWize::_getMemory(uint8_t address, uint8_t *buffer, uint8_t len) {
1318  #if USE_MEMORY_CACHE
1319  if (!_ready) return 0;
1320  memcpy(buffer, &_memory[address], len);
1321  return len;
1322  #else
1323  uint8_t count = 0;
1324  if (_setConfig(true)) {
1325  for (uint8_t i=0; i<len; i++) {
1326  if (_sendAndReceive(CMD_READ_MEMORY) == -1) break;
1327  if (_sendAndReceive(address + i) != 1) break;
1328  count++;
1329  buffer[i] = _buffer[0];
1330  }
1331  _setConfig(false);
1332  }
1333  return count;
1334  #endif
1335 }
1336 
1343 uint8_t AllWize::_getMemory(uint8_t address) {
1344  #if USE_MEMORY_CACHE
1345  if (!_ready) return 0;
1346  return _memory[address];
1347  #else
1348  uint8_t response = _sendCommand(CMD_READ_MEMORY, address);
1349  if (response > 0) return _buffer[0];
1350  return 0;
1351  #endif
1352 }
1353 
1361 bool AllWize::_setMemory(uint8_t address, uint8_t data) {
1362 
1363  // Check cached data
1364  #if USE_MEMORY_CACHE
1365  if (_memory[address] == data) return true;
1366  #endif
1367 
1368  // Build query buffer
1369  uint8_t buffer[3] = {address, data, (uint8_t)CMD_EXIT_MEMORY};
1370 
1371  // Execute command
1372  bool ret = (_sendCommand(CMD_WRITE_MEMORY, buffer, 3) != -1);
1373 
1374  // Update cached memory
1375  #if USE_MEMORY_CACHE
1376  if (ret) _memory[address] = data;
1377  #endif
1378 
1379  return ret;
1380 
1381 }
1382 
1391 bool AllWize::_setMemory(uint8_t address, uint8_t *data, uint8_t len) {
1392 
1393  // Check cached data
1394  #if USE_MEMORY_CACHE
1395  if (memcmp(&_memory[address], data, len) == 0) return true;
1396  #endif
1397 
1398  // Build query buffer
1399  uint8_t buffer[len * 2 + 1];
1400  for (uint8_t i = 0; i < len; i++) {
1401  buffer[i * 2] = address + i;
1402  buffer[i * 2 + 1] = data[i];
1403  }
1404  buffer[len * 2] = CMD_EXIT_MEMORY;
1405 
1406  // Execute command
1407  bool ret = (_sendCommand(CMD_WRITE_MEMORY, buffer, len * 2 + 1) != -1);
1408 
1409  // Update cached memory
1410  #if USE_MEMORY_CACHE
1411  if (ret) memcpy(&_memory[address], data, len);
1412  #endif
1413 
1414  return ret;
1415 
1416 }
1417 
1418 // ------------------------------------------------------------------------------------------------
1419 
1426 uint8_t AllWize::_getAddress(uint8_t slot) {
1427  if ((slot >= MEM_MAX_SLOTS) || (MODULE_UNKNOWN == _module)) {
1428  return 0xFF;
1429  }
1430  return MEM_ADDRESS[_module-1][slot];
1431 }
1432 
1441 bool AllWize::_setSlot(uint8_t slot, uint8_t *data, uint8_t len) {
1442  uint8_t address = _getAddress(slot);
1443  if (0xFF == address) return false;
1444  return _setMemory(address, data, len);
1445 }
1446 
1454 bool AllWize::_setSlot(uint8_t slot, uint8_t data) {
1455  uint8_t address = _getAddress(slot);
1456  if (0xFF == address) return false;
1457  return _setMemory(address, data);
1458 }
1459 
1468 uint8_t AllWize::_getSlot(uint8_t slot, uint8_t *buffer, uint8_t len) {
1469  uint8_t address = _getAddress(slot);
1470  if (0xFF == address) return 0;
1471  return _getMemory(address, buffer, len);
1472 }
1473 
1480 uint8_t AllWize::_getSlot(uint8_t slot) {
1481  uint8_t address = _getAddress(slot);
1482  if (0xFF == address) return 0;
1483  return _getMemory(address);
1484 }
1485 
1493 String AllWize::_getSlotAsHexString(uint8_t slot, uint8_t len) {
1494  uint8_t bin[len];
1495  char hex[2 * len + 1];
1496  hex[0] = 0;
1497  if (len == _getSlot(slot, bin, len)) {
1498  _bin2hex(bin, hex, len);
1499  }
1500  return String(hex);
1501 }
1502 
1510 String AllWize::_getSlotAsString(uint8_t slot, uint8_t len) {
1511  uint8_t bin[len];
1512  char hex[len + 1];
1513  hex[0] = 0;
1514  if (len == _getSlot(slot, bin, len)) {
1515  memcpy(hex, bin, len);
1516  hex[len - 1] = 0;
1517  }
1518  return String(hex);
1519 }
1520 
1521 // ------------------------------------------------------------------------------------------------
1522 
1550 
1551  #if defined(ALLWIZE_DEBUG_PORT)
1552  {
1553  char ch[4];
1554  ALLWIZE_DEBUG_PRINT("recv:");
1555  for (uint8_t i = 0; i < _pointer; i++) {
1556  snprintf(ch, sizeof(ch), " %02X", _buffer[i]);
1557  ALLWIZE_DEBUG_PRINT(ch);
1558  }
1560  }
1561  #endif
1562 
1563  // Get current values
1564  uint8_t mbus_mode = getMode();
1565  uint8_t data_interface = getDataInterface();
1566  bool has_start = (data_interface & 0x04) == 0x04;
1567  bool has_header = (mbus_mode != MBUS_MODE_OSP) & ((data_interface & 0x01) == 0x00);
1568  bool has_rssi = getAppendRSSI();
1569  bool has_crc = (data_interface & 0x08) == 0x08;
1570  uint8_t bytes_not_in_len = has_start ? 3 : 1;
1571  uint8_t bytes_not_in_app = (has_header ? 9 : 0) + 1 + (has_rssi ? 1 : 0) + (has_crc ? 2 : 0);
1572  uint8_t bytes_not_in_msg = 0;
1573 
1574  // This variable will contain the pointer to the current reading position
1575  uint8_t in = 0;
1576 
1577  // Start byte
1578  if (has_start) {
1579  if (START_BYTE != _buffer[in++]) return false;
1580  };
1581 
1582  // Get and check buffer length
1583  uint8_t len = _buffer[in++];
1584  if (_pointer != len + bytes_not_in_len) return false;
1585 
1586  if (has_header) {
1587 
1588  // C-field
1589  _message.c = _buffer[in++];
1590 
1591  // Manufacturer
1592  uint16_t man = (_buffer[in + 1] << 8) + _buffer[in];
1593  _message.man[0] = ((man >> 10) & 0x001F) + 64;
1594  _message.man[1] = ((man >> 5) & 0x001F) + 64;
1595  _message.man[2] = ((man >> 0) & 0x001F) + 64;
1596  _message.man[3] = 0;
1597  in += 2;
1598 
1599  // Address
1600  _message.address[0] = _buffer[in + 3];
1601  _message.address[1] = _buffer[in + 2];
1602  _message.address[2] = _buffer[in + 1];
1603  _message.address[3] = _buffer[in + 0];
1604  in += 4;
1605 
1606  // Version
1607  _message.version = _buffer[in++];
1608 
1609  // Type
1610  _message.type = _buffer[in++];
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 
1623  // Wize transport layer
1624  if (MODULE_WIZE == _module) {
1625 
1626  if (CI_WIZE == _message.ci) {
1627 
1628  bytes_not_in_app += 5;
1629 
1630  // Wize control
1631  _message.wize_control = _buffer[in++];
1632 
1633  // Wize operator ID
1635 
1636  // Wize counter
1637  _message.wize_counter = (_buffer[in + 1] << 8) + _buffer[in];
1638  in += 2;
1639 
1640  // Wize application
1642 
1643  } else {
1644 
1645  // Undocumented hack
1646  bytes_not_in_msg = 8;
1647 
1648  }
1649 
1650  }
1651 
1652  // Application data
1653  _message.len = len - bytes_not_in_app - bytes_not_in_msg;
1654  memcpy(_message.data, &_buffer[in], _message.len);
1655  _message.data[_message.len] = 0;
1656  in += (_message.len + bytes_not_in_msg);
1657 
1658  // RSSI
1659  if (has_rssi) {
1660  _message.rssi = _buffer[in++];
1661  } else {
1662  _message.rssi = 0xFF;
1663  }
1664 
1665  // CRC
1666  if (has_crc) {
1667  in += 2;
1668  }
1669 
1670  // Stop byte
1671  if (has_start) {
1672  if (STOP_BYTE != _buffer[in]) return false;
1673  }
1674 
1675  return true;
1676 
1677 }
1678 
1679 // -----------------------------------------------------------------------------
1680 
1686 
1687  // Flush TX line
1688  _stream->flush();
1689 
1690  // Flush RX line
1691  while (_stream->available()) _stream->read();
1692 
1693 }
1694 
1701 uint8_t AllWize::_send(uint8_t ch) {
1702  #if defined(ALLWIZE_DEBUG_PORT)
1703  {
1704  char buffer[10];
1705  snprintf(buffer, sizeof(buffer), "w %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1706  ALLWIZE_DEBUG_PRINTLN(buffer);
1707  }
1708  #endif
1709  return _stream->write(ch);
1710 }
1711 
1719 uint8_t AllWize::_send(uint8_t *buffer, uint8_t len) {
1720  uint8_t n = 0;
1721  for (uint8_t i = 0; i < len; i++) {
1722  if (_send(buffer[i])) n++;
1723  }
1724  return n;
1725 }
1726 
1734 }
1735 
1743 int8_t AllWize::_sendAndReceive(uint8_t *buffer, uint8_t len) {
1744  if (_send(buffer, len) != len) return -1;
1745  return _receive();
1746 }
1747 
1754 int8_t AllWize::_sendAndReceive(uint8_t ch) {
1755  if (_send(ch) != 1) return -1;
1756  return _receive();
1757 }
1758 
1765 
1766  uint32_t _start = millis();
1767  int ch = -1;
1768  while (millis() - _start < _timeout) {
1769  #if defined(ARDUINO_ARCH_ESP8266)
1770  yield();
1771  #endif
1772  ch = _stream->read();
1773  if (ch >= 0) break;
1774  };
1775 
1776  #if defined(ALLWIZE_DEBUG_PORT)
1777  /*
1778  {
1779  if (ch < 0) {
1780  ALLWIZE_DEBUG_PRINTLN("r TIMEOUT");
1781  } else {
1782  char buffer[10];
1783  snprintf(buffer, sizeof(buffer), "r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1784  ALLWIZE_DEBUG_PRINTLN(buffer);
1785  }
1786  }
1787  */
1788  #endif
1789 
1790  return ch;
1791 }
1792 
1800 int AllWize::_readBytes(char * data, uint16_t len) {
1801 
1802  if (len < 1) return 0;
1803 
1804  uint16_t index = 0;
1805  while (index < len) {
1806  int ch = _timedRead();
1807  if (ch < 0) return -1;
1808  *data++ = (char)ch;
1809  index++;
1810  }
1811 
1812  return index;
1813 
1814 }
1815 
1824 int AllWize::_readBytesUntil(char terminator, char *data, uint16_t len) {
1825 
1826  if (len < 1) return 0;
1827 
1828  uint16_t index = 0;
1829  while (index < len) {
1830  int ch = _timedRead();
1831  if (ch < 0) return -1;
1832  if (ch == terminator) break;
1833  *data++ = (char) ch;
1834  index++;
1835  }
1836 
1837  return index;
1838 
1839 }
1840 
1848 void AllWize::_hex2bin(char *hex, uint8_t *bin, uint8_t len) {
1849  for (uint8_t i = 0; i < len; i += 2) {
1850  bin[i / 2] = ((hex[i] - '0') * 16 + (hex[i + 1] - '0')) & 0xFF;
1851  }
1852 }
1853 
1861 void AllWize::_bin2hex(uint8_t *bin, char *hex, uint8_t len) {
1862  for (uint8_t i = 0; i < len; i++) {
1863  sprintf(&hex[i * 2], "%02X", bin[i]);
1864  }
1865 }
1866 
1872 void AllWize::_niceDelay(uint32_t ms) {
1873  uint32_t start = millis();
1874  while (millis() - start < ms) delay(1);
1875 }
uint8_t _data_interface
Definition: AllWize.h:261
String getModuleTypeName()
Returns the module type.
Definition: AllWize.cpp:1120
#define STOP_BYTE
Definition: RC1701HP.h:25
uint8_t _access_number
Definition: AllWize.h:263
#define CMD_VOLTAGE
Definition: RC1701HP.h:57
uint8_t getChannel()
Gets the channel stored in non-volatile memory.
Definition: AllWize.cpp:598
#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:839
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:442
#define PREAMBLE_FORMAT_A
Definition: RC1701HP.h:250
uint8_t version
Definition: AllWize.h:59
String _model
Definition: AllWize.h:272
bool setUID(uint32_t uid)
Saved the UID into the module memory.
Definition: AllWize.cpp:1035
bool ack()
Sends an ACK.
Definition: AllWize.cpp:431
void setEncryptFlag(uint8_t flag)
Sets the encrypt flag setting.
Definition: AllWize.cpp:860
uint8_t getSleepMode()
Gets the sleep mode stored in non-volatile memory.
Definition: AllWize.cpp:700
#define MODULE_SIGNATURE
Definition: RC1701HP.h:7
uint8_t getLEDControl()
Gets the current LED control.
Definition: AllWize.cpp:791
uint8_t getPower()
Gets the RF power stored in non-volatile memory.
Definition: AllWize.cpp:620
#define DATA_INTERFACE_START_STOP
Definition: RC1701HP.h:245
uint8_t len
Definition: AllWize.h:61
#define CMD_INSTALL_MODE
Definition: RC1701HP.h:45
String _fw
Definition: AllWize.h:274
int _readBytes(char *buffer, uint16_t len)
Reads the stream buffer up to a number of bytes.
Definition: AllWize.cpp:1800
#define CI_WIZE
Definition: OMS.h:28
void setWizeApplication(uint8_t wize_application)
Sets the wize applicaton field in the transpoprt layer.
Definition: AllWize.cpp:538
uint8_t getEncryptFlag()
Gets the encrypt flag setting.
Definition: AllWize.cpp:870
void setVersion(uint8_t version)
Sets the device version.
Definition: AllWize.cpp:1056
void slave()
Sets the module in slave mode.
Definition: AllWize.cpp:249
allwize_message_t _message
Definition: AllWize.h:283
void setPower(uint8_t power, bool persist=false)
Sets the RF power.
Definition: AllWize.cpp:607
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:766
uint8_t _wize_application
Definition: AllWize.h:279
#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:951
bool getAppendRSSI()
Gets the current RSSI mode value.
Definition: AllWize.cpp:721
bool _decode()
Decodes the current RX buffer contents.
Definition: AllWize.cpp:1549
#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:1048
uint16_t _wize_network_id
Definition: AllWize.h:278
void setControlField(uint8_t value, bool persist=false)
Sets the control field value.
Definition: AllWize.cpp:819
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:1207
void setMode(uint8_t mode, bool persist=false)
Sets the module in one of the available MBus modes.
Definition: AllWize.cpp:662
void _resetSerial()
Resets the serial object.
Definition: AllWize.cpp:120
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:1088
void setPreamble(uint8_t preamble)
Sets the preamble length frame format.
Definition: AllWize.cpp:729
#define NETWORK_ROLE_REPEATER
Definition: RC1701HP.h:228
uint8_t getControlField()
Gets the control field value stored in non-volatile memory.
Definition: AllWize.cpp:830
uint16_t _counter
Definition: AllWize.h:280
uint8_t getPreamble()
Gets the preamble length frame format.
Definition: AllWize.cpp:739
int8_t _receive()
Listens to incoming data from the module until timeout or END_OF_RESPONSE.
Definition: AllWize.cpp:1732
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:546
uint8_t wize_network_id
Definition: AllWize.h:65
void softReset()
Cleans the RX/TX line.
Definition: AllWize.cpp:197
bool _ready
Definition: AllWize.h:268
uint8_t _getSlot(uint8_t slot)
Returns the contents of single-byte memory slot.
Definition: AllWize.cpp:1480
#define CMD_ACCESS_NUMBER
Definition: RC1701HP.h:49
uint8_t _buffer[RX_BUFFER_SIZE]
Definition: AllWize.h:284
String getUID()
Returns the Unique ID string.
Definition: AllWize.cpp:1027
#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:975
#define DEFAULT_MBUS_MODE
Definition: AllWize.h:48
void setWizeOperatorId(uint16_t wize_network_id)
Use AllWize::setWizeNetworkId instead.
Definition: AllWize.cpp:522
#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:692
#define POWER_20dBm
Definition: RC1701HP.h:197
uint8_t getDecryptFlag()
Gets the decrypt flag setting.
Definition: AllWize.cpp:886
void setControlInformation(uint8_t ci)
Sets the control information byte.
Definition: AllWize.cpp:566
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:457
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:1493
#define CMD_RSSI
Definition: RC1701HP.h:54
uint8_t getDevice()
Returns the device type from non-volatile memory.
Definition: AllWize.cpp:1064
#define CMD_TEST_MODE_0
Definition: RC1701HP.h:62
void setLEDControl(uint8_t value)
Sets the LED control.
Definition: AllWize.cpp:782
#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:1743
uint8_t _pointer
Definition: AllWize.h:285
#define CMD_IDLE_DISABLE_RF
Definition: RC1701HP.h:33
void setDecryptFlag(uint8_t flag)
Sets the decrypt flag setting.
Definition: AllWize.cpp:878
uint32_t _baudrate
Definition: AllWize.h:257
String getPartNumber()
Returns the module part number.
Definition: AllWize.cpp:1080
uint8_t _wize_control
Definition: AllWize.h:277
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:1112
#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:747
uint8_t getNetworkRole()
Gets the current network role.
Definition: AllWize.cpp:774
bool setWizeControl(uint8_t wize_control)
Sets the wize control field in the transpoprt layer.
Definition: AllWize.cpp:512
uint8_t wize_control
Definition: AllWize.h:64
bool _config
Definition: AllWize.h:255
int8_t _tx
Definition: AllWize.h:241
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:1510
int8_t _rx
Definition: AllWize.h:240
static const uint32_t BAUDRATES[11]
Definition: RC1701HP.h:266
uint32_t _timeout
Definition: AllWize.h:256
#define END_OF_RESPONSE
Definition: RC1701HP.h:23
uint16_t getCounter()
Gets the current wize counter.
Definition: AllWize.cpp:554
#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:1824
uint8_t _getMemory(uint8_t address)
Returns the contents of memory address.
Definition: AllWize.cpp:1343
void sleep()
Sets the radio module in sleep mode.
Definition: AllWize.cpp:268
String getFirmwareVersion()
Returns the module firmware revision.
Definition: AllWize.cpp:1096
#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:1256
#define CMD_IDLE_ENABLE_RF
Definition: RC1701HP.h:32
#define NETWORK_ROLE_MASTER
Definition: RC1701HP.h:227
void _niceDelay(uint32_t ms)
Does a non-blocking delay.
Definition: AllWize.cpp:1872
#define CMD_WRITE_MEMORY
Definition: RC1701HP.h:48
bool _setConfig(bool value)
Sets or unsets config mode.
Definition: AllWize.cpp:1178
#define CMD_EXIT_MEMORY
Definition: RC1701HP.h:35
double getFrequency(uint8_t channel)
Returns the frequency for the given channel.
Definition: AllWize.cpp:1135
String getMID()
Returns the Manufacturer ID.
Definition: AllWize.cpp:1008
void setChannel(uint8_t channel, bool persist=false)
Sets the communications channel (for MBUS_MODE_R2 only)
Definition: AllWize.cpp:583
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:708
uint8_t wize_application
Definition: AllWize.h:67
String _hw
Definition: AllWize.h:273
#define CMD_RF_POWER
Definition: RC1701HP.h:51
String getSerialNumber()
Returns the module serial number.
Definition: AllWize.cpp:1104
#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:915
#define RX_BUFFER_SIZE
Definition: AllWize.h:45
void _readModel()
Searches for the module model.
Definition: AllWize.cpp:1271
uint8_t getMode()
Gets the MBus mode stored in non-volatile memory.
Definition: AllWize.cpp:684
uint8_t getControlInformation()
Gets the control information byte.
Definition: AllWize.cpp:574
uint8_t _module
Definition: AllWize.h:264
void setDataInterface(uint8_t value)
Sets the data interface for receiving packets.
Definition: AllWize.cpp:799
int _timedRead()
Reads a byte from the stream with a timeout.
Definition: AllWize.cpp:1764
void setKey(uint8_t reg, const uint8_t *key)
Sets the default encryption key.
Definition: AllWize.cpp:895
void setDefaultKey(const uint8_t *key)
Sets the default encryption key.
Definition: AllWize.cpp:907
uint8_t getInstallMode()
Gets the install modevalue stored in non-volatile memory.
Definition: AllWize.cpp:852
void setAccessNumber(uint8_t value)
Sets new/specific access number.
Definition: AllWize.cpp:923
SoftwareSerial * _sw_serial
Definition: AllWize.h:250
bool reset()
Resets the radio module.
Definition: AllWize.cpp:162
uint8_t _reset_gpio
Definition: AllWize.h:253
bool _setSlot(uint8_t slot, uint8_t data)
Sets non-volatile memory contents starting from given address.
Definition: AllWize.cpp:1454
bool setMID(uint16_t mid)
Sets the Manufacturer ID.
Definition: AllWize.cpp:1016
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:965
bool _append_rssi
Definition: AllWize.h:262
#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:1861
uint8_t _memory[0x100]
Definition: AllWize.h:269
#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:1426
Stream * _stream
Definition: AllWize.h:243
bool _setMemory(uint8_t address, uint8_t data)
Sets non-volatile memory contents starting from given address.
Definition: AllWize.cpp:1361
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:1719
#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:757
#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:1848
uint16_t getDataRateSpeed(uint8_t dr)
Returns the speed for te given datarate.
Definition: AllWize.cpp:1160
void _flush()
Flushes the serial line to the module.
Definition: AllWize.cpp:1685
void setWizeNetworkId(uint16_t wize_network_id)
Sets the wize network ID field in the transpoprt layer.
Definition: AllWize.cpp:530
uint8_t getDataInterface()
Gets the data interface for receiving packets.
Definition: AllWize.cpp:810
void setDataRate(uint8_t dr)
Sets the data rate.
Definition: AllWize.cpp:628
void setBaudRate(uint8_t baudrate)
Sets the UART baud rate, requires reset to take effect.
Definition: AllWize.cpp:931
HardwareSerial * _hw_serial
Definition: AllWize.h:244
uint8_t getDataRate()
Gets the data rate stored in non-volatile memory.
Definition: AllWize.cpp:653
allwize_message_t read()
Returns latest received message.
Definition: AllWize.cpp:503
#define CMD_ENTER_CONFIG
Definition: RC1701HP.h:28
void setDevice(uint8_t type)
Sets the device type.
Definition: AllWize.cpp:1072
uint8_t getBaudRate()
Gets the UART baud rate.
Definition: AllWize.cpp:942
uint8_t _mbus_mode
Definition: AllWize.h:260
uint8_t _config_gpio
Definition: AllWize.h:254
uint16_t getVoltage()
Returns the internal voltage of the module.
Definition: AllWize.cpp:992
#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:259