LoraWan/P2P通信に対応しているTLM922S用のライブラリを作ってみた。Arduino(AVR/ESP8266/ESP32/ARM/…)/Windows/Linuxに対応している。
LoRaモジュール TLM922S-P01A
TLM922S搭載LoRa評価ボード(Arduinoシールド) ADB922S
TLM922Sの文字列コマンドを関数に置き換えただけのライブラリであるが基本ロジックのみ試験しただけで通信試験も全機能試験も行っていないので不具合が残されている可能性があることに注意するべし。
ちなみにAVRでのRAM消費量を抑えるためにPROGMEMを利用したため若干複雑になってしまったがおかげで328/32u4などでも使えるようになった。
【Arduinoサンプル】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include "tlm922s.h" #define TLM922S TLM922S_ARDUINO(Serial1) // Arduino通信ポート指定 TLM922S lora; void lorawan_rx_cb(const char *payload, size_t len, uint8_t port) { ... } void p2p_rx_cb(const char *payload, size_t len, int8_t rssi, int8_t snr) { ... } void setup(void) { lora.begin(9600); lora.lorawan_callback(lorawan_rx_cb); lora.p2p_callback(p2p_rx_cb); lora.mod_set_echo(false); lora.p2p_set_freq(lora.AS923_JP_CH36); // 923.0MHz } void loop(void) { lora.p2p_tx("test"); lora.p2p_rx(3000); } |
【Linux/Windowsサンプル】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include "tlm922s.h" TLM922S lora; void lorawan_rx_cb(const char *payload, size_t len, uint8_t port) { ... } void p2p_rx_cb(const char *payload, size_t len, int8_t rssi, int8_t snr) { ... } int main(int argc, char **argv) { if (lora.begin("通信ポート指定(com? or ttyUSB?)", 9600) >= 0) { lora.lorawan_callback(lorawan_rx_cb); lora.p2p_callback(p2p_rx_cb); lora.mod_set_echo(false); lora.p2p_set_freq(lora.AS923_JP_CH36); // 923.0MHz while (1) { lora.p2p_tx("test"); lora.p2p_rx(3000); } lora.end(); } return 0; } |
【修正】
2023-10-10
LoRaWANのClass-C対応のためにlorawan_handle()を追加。LoRaWANの受信(A/C)のためにはlorawan_handle()を常に実行し続ける必要がある。
2023-10-06
tlm922s.hのマクロ定義の修正。
2023-10-05
バグ修正と改良。
2023-10-04
まだ見逃していた機能があった。製品CDに新旧バージョンのドキュメントが同梱されていて古いのを見たり新しいのを見たりしてたのがまずかったようだ。
旧: TLM922S Command InterfaceUser’s Guide-1.1.1.pdf
新: TLM922S Product Specification-2.0.0.pdf
名前が違うので異なるドキュメントかと思ってたら単にバージョンの違いだけみたい。紛らわしいので同じ名前にして欲しかったかも...
STATUS lorawan_get_uplink_dwell_table(uint8_t index, uint32_t &minfreq, uint32_t &maxfreq, uint8_t &uplinkdwell);
2023-10-03
見逃していた機能の追加。再度調べてみたら次々と...古いドキュメント見てたのかも。(-_-;)
STATUS lorawan_set_battery(uint8_t batterylevel);
STATUS lorawan_set_multicast_key(uint8_t index, const char *address, const char *nwkskey, const char *appskey);
STATUS lorawan_set_lbt_retry(uint8_t retrycount);
uint8_t lorawan_get_battery(void);
bool lorawan_get_multicast_key(uint8_t index, const char * &address, const char * &nwkskey, const char * &appskey);
STATUS lorawan_set_uplink_dwell_table(uint8_t index, uint32_t minfreq, uint32_t maxfreq, uint8_t uplinkdwell);
hex2bin()の汎用化とpublicアクセスへの変更。引数 len が -1 のときは入力データをヌル終端文字列とみなす。
static size_t bin2hex(char *buf, size_t size, const char *bin, int len = -1);
static size_t hex2bin(char *buf, size_t size, const char *hex, int len = -1);
2023-10-02
tlm922s.hのマクロ定義がバグッてたのとTLM922Sのシリアル通信速度の規定値(9600bps)に合わせてコードを修正。ついでにP2Pでエコーバック通信試験をしてみた。宛先アドレス指定がなくて所謂ブロードキャスト通信となるようだ。
【ライブラリ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
/* tlm922s.h - Kiwi TLM922S Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdbool.h> #include <stdarg.h> #include <string.h> #define TLM922S_MAX_PAYLOAD_SIZE 242 class TLM922SClass { public: typedef enum : uint32_t { /* AS923 ISM Band (20mW/250mW), ARIB STD-T108 */ AS923_JP_CH24 = 920600000UL, AS923_JP_CH25 = 920800000UL, AS923_JP_CH26 = 921000000UL, AS923_JP_CH27 = 921200000UL, AS923_JP_CH28 = 921400000UL, AS923_JP_CH29 = 921600000UL, AS923_JP_CH30 = 921800000UL, AS923_JP_CH31 = 922000000UL, AS923_JP_CH32 = 922200000UL, AS923_JP_CH33 = 922400000UL, AS923_JP_CH34 = 922600000UL, AS923_JP_CH35 = 922800000UL, AS923_JP_CH36 = 923000000UL, AS923_JP_CH37 = 923200000UL, // LoRaWAN default AS923_JP_CH38 = 923400000UL, // LoRaWAN default /* AS923 ISM Band (20mW) */ AS923_JP_CH39 = 923600000UL, AS923_JP_CH40 = 923800000UL, AS923_JP_CH41 = 924000000UL, AS923_JP_CH42 = 924200000UL, AS923_JP_CH43 = 924400000UL, AS923_JP_CH44 = 924600000UL, AS923_JP_CH45 = 924800000UL, AS923_JP_CH46 = 925000000UL, AS923_JP_CH47 = 925200000UL, AS923_JP_CH48 = 925400000UL, AS923_JP_CH49 = 925600000UL, AS923_JP_CH50 = 925800000UL, AS923_JP_CH51 = 926000000UL, AS923_JP_CH52 = 926200000UL, AS923_JP_CH53 = 926400000UL, AS923_JP_CH54 = 926600000UL, AS923_JP_CH55 = 926800000UL, AS923_JP_CH56 = 927000000UL, AS923_JP_CH57 = 927200000UL, AS923_JP_CH58 = 927400000UL, AS923_JP_CH59 = 927600000UL, AS923_JP_CH60 = 927800000UL, AS923_JP_CH61 = 928000000UL, } AS923_JP; typedef enum : uint8_t { STATUS_OK, STATUS_INVALID, STATUS_JOINED, STATUS_UNJOINED, STATUS_ACCEPTED, STATUS_UNSUCCESS, STATUS_KEYS_NOT_INIT, STATUS_NOT_JOINED, STATUS_NO_FREE_CH, STATUS_BUSY, STATUS_INVALID_DATA_LENGTH, STATUS_TX_OK, STATUS_RX, STATUS_ERR, STATUS_DEMODMARGIN, STATUS_NBGATEWAYS, STATUS_RADIO_RX, STATUS_RADIO_ERROR_TIMEOUT, STATUS_RADIO_ERR, STATUS_RADIO_TX_OK, STATUS_UNKNOWN, } STATUS; typedef enum : uint32_t { BAUDRATE9600 = 9600UL, BAUDRATE19200 = 19200UL, BAUDRATE57600 = 57600UL, BAUDRATE115200 = 115200UL, } BAUDRATE; typedef enum : uint8_t { PIN_MODE_OUTPUT, PIN_MODE_INPUT, } PIN_MODE; typedef enum : uint8_t { PIN_P05 = 5, PIN_P10 = 10, PIN_P11 = 11, PIN_P12 = 12, PIN_P13 = 13, PIN_P14 = 14, } PIN; typedef enum : uint8_t { LORAWAN_CH_PLAN_NONE, LORAWAN_CH_PLAN_EU868, LORAWAN_CH_PLAN_EU433, LORAWAN_CH_PLAN_CN780, LORAWAN_CH_PLAN_AS923, LORAWAN_CH_PLAN_KR920, LORAWAN_CH_PLAN_IND865, LORAWAN_CH_PLAN_US915, LORAWAN_CH_PLAN_US915_HYBRID, LORAWAN_CH_PLAN_AU915 } LORAWAN_CH_PLAN; typedef enum : uint8_t { LORAWAN_CLASS_A, LORAWAN_CLASS_B, LORAWAN_CLASS_C, } LORAWAN_CLASS; typedef enum : uint8_t { LORAWAN_MODE_OTAA, LORAWAN_MODE_ABP, } LORAWAN_MODE; typedef enum : uint8_t { CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH8, CH9, CH10, CH11, CH12, CH13, CH14, CH15, } CH; typedef enum : uint8_t { BAND0, BAND1, BAND2, BAND3, BAND4, BAND5, BAND6, BAND7, BAND8, BAND9, BAND10, BAND11, BAND12, BAND13, BAND14, BAND15, } BAND; typedef enum : uint8_t { DR0, // SF12/BW125KHz: 250BPS (UplinkDwell=0 only) DR1, // SF11/BW125KHz: 440BPS (UplinkDwell=0 only) DR2, // SF10/BW125KHz: 980BPS DR3, // SF9 /BW125KHz: 1760BPS DR4, // SF8 /BW125KHz: 3125BPS DR5, // SF7 /BW125KHz: 5470BPS DR6, // SF7 /BW250KHz: 11000BPS } DR; typedef enum : uint8_t { TXPWI0, TXPWI1, TXPWI2, TXPWI3, TXPWI4, TXPWI5, } TXPWI; typedef enum : uint8_t { TXPWR2 = 2, TXPWR3, TXPWR4, TXPWR5, TXPWR6, TXPWR7, TXPWR8, TXPWR9, TXPWR10, TXPWR11, TXPWR12, TXPWR13, TXPWR14, TXPWR15, TXPWR16, TXPWR17, TXPWR18, TXPWR19, TXPWR20, } TXPWR; typedef enum : uint8_t { MAX_EIRP_8dBm, MAX_EIRP_10dBm, MAX_EIRP_12dBm, MAX_EIRP_13dBm, MAX_EIRP_14dBm, MAX_EIRP_16dBm, MAX_EIRP_18dBm, MAX_EIRP_20dBm, MAX_EIRP_21dBm, MAX_EIRP_24dBm, MAX_EIRP_26dBm, MAX_EIRP_27dBm, MAX_EIRP_29dBm, MAX_EIRP_30dBm, MAX_EIRP_33dBm, MAX_EIRP_36dBm, } MAX_EIRP; typedef enum : uint8_t { SF7 = 7, // BW125KHz: 5470BPS SF8, // BW125KHz: 3125BPS SF9, // BW125KHz: 1760BPS SF10, // BW125KHz: 980BPS SF11, // BW125KHz: 440BPS SF12, // BW125KHz: 250BPS } SF; typedef enum : uint16_t { BW125 = 125, BW250 = 250, BW500 = 500, } BW; typedef enum : uint8_t { CR45, CR46, CR47, CR48, } CR; /* * Callback Method Types */ typedef void (*lorawan_rx_t)(const char *payload, size_t len, uint8_t port); typedef void (*p2p_rx_t)(const char *payload, size_t len, int8_t rssi, int8_t snr); /* * Constructor / Destructor */ TLM922SClass(unsigned long timeout = 1000); virtual ~TLM922SClass(void); /* * Common Methods */ const char *strchplan(LORAWAN_CH_PLAN ch_plan, char *buf = nullptr, size_t len = 0); const char *strstatus(STATUS status, char *buf = nullptr, size_t len = 0); const char *strcr(CR cr, char *buf = nullptr, size_t len = 0); static size_t bin2hex(char *buf, size_t size, const char *bin, int len = -1); static size_t hex2bin(char *buf, size_t size, const char *hex, int len = -1); /* * Module Commands */ const char *mod_factory_reset(void); const char *mod_get_ver(void); const char *mod_get_hw_deveui(void); const char *mod_get_hw_model(void); STATUS mod_set_pin_mode(PIN pin, PIN_MODE mode); uint8_t mod_get_dio(PIN pin); STATUS mod_set_dio(PIN pin, uint8_t value); STATUS mod_sleep(bool deep, bool wakeup, uint16_t seconds); STATUS mod_set_baudrate(BAUDRATE speed); STATUS mod_set_echo(bool enable); void mod_reset(void); STATUS mod_save(void); /* * LoRaWAN Commands */ void lorawan_callback(lorawan_rx_t cb); STATUS lorawan_join(LORAWAN_MODE mode); STATUS lorawan_get_join_status(void); STATUS lorawan_set_ch_plan(LORAWAN_CH_PLAN ch_plan, MAX_EIRP maxeirp = MAX_EIRP_14dBm, uint8_t uplinkdwell = 0, uint8_t downlinkdwell = 0, uint8_t rx1droffset = 0); LORAWAN_CH_PLAN lorawan_get_ch_plan(void); STATUS lorawan_tx(bool confirmed, uint8_t port, const char *payload, int len = -1); STATUS lorawan_tx(uint8_t *demodmargin, uint8_t *nbgateways, bool confirmed, uint8_t port, const char *payload, int len = -1); STATUS lorawan_set_deveui(const char *deveui); STATUS lorawan_set_appeui(const char *appeui); STATUS lorawan_set_appkey(const char *appkey); STATUS lorawan_set_devaddr(const char *devaddr); STATUS lorawan_set_nwkskey(const char *nwksessionkey); STATUS lorawan_set_appskey(const char *appsessionkey); STATUS lorawan_set_pwr(TXPWI powerindex); STATUS lorawan_set_dr(DR datarate); STATUS lorawan_set_adr(bool state); STATUS lorawan_set_txretry(uint8_t retrycount); STATUS lorawan_set_rxdelay1(uint16_t retrycount); STATUS lorawan_set_rx2(DR datarate, uint32_t frequency); STATUS lorawan_set_ch_freq(CH channelid, uint32_t frequency); STATUS lorawan_set_ch_dr_range(CH channelid, DR mindr, DR maxdr); STATUS lorawan_set_ch_status(CH channelid, bool status); STATUS lorawan_set_dc_ctl(bool status = false); STATUS lorawan_set_dc_band(BAND bandid, uint32_t minfreq, uint32_t maxfreq, uint16_t dutycycle); STATUS lorawan_set_join_ch(CH channelid, bool status); STATUS lorawan_set_upcnt(uint32_t uplinkcounter = 1); STATUS lorawan_set_downcnt(uint32_t downlinkcounter = 0); STATUS lorawan_set_class(LORAWAN_CLASS aclass = LORAWAN_CLASS_A); STATUS lorawan_set_pwr_table(TXPWI index, TXPWR power); STATUS lorawan_set_max_payload_table(DR drindex, uint8_t maxpayload); STATUS lorawan_set_dl_freq(CH channelid, uint32_t frequency); STATUS lorawan_update_payload_table(uint16_t dwelltime); STATUS lorawan_set_battery(uint8_t batterylevel); STATUS lorawan_set_multicast_key(uint8_t index, const char *address, const char *nwkskey, const char *appskey); STATUS lorawan_set_lbt_retry(uint8_t retrycount); STATUS lorawan_save(void); const char *lorawan_get_devaddr(void); const char *lorawan_get_deveui(void); const char *lorawan_get_appeui(void); const char *lorawan_get_nwkskey(void); const char *lorawan_get_appskey(void); const char *lorawan_get_appkey(void); DR lorawan_get_dr(void); TXPWI lorawan_get_pwr(void); bool lorawan_get_adr(void); uint8_t lorawan_get_txretry(void); void lorawan_get_rxdelay(uint16_t &rxdelay1, uint16_t &rxdelay2); void lorawan_get_rx2(DR &dr, uint32_t &freq); void lorawan_get_ch_para(CH channelid, uint32_t &freq, uint8_t &mindr, uint8_t &maxdr); bool lorawan_get_ch_status(CH channelid); bool lorawan_get_dc_ctl(void); void lorawan_get_dc_band(BAND bandid, uint32_t &minfreq, uint32_t &maxfreq, uint16_t &dutycycle); uint8_t lorawan_get_join_ch(CH *channelids, uint8_t count); uint32_t lorawan_get_upcnt(void); uint32_t lorawan_get_downcnt(void); LORAWAN_CLASS lorawan_get_class(void); TXPWR lorawan_get_pwr_table(TXPWI index); uint8_t lorawan_get_max_payload_table(DR drindex); uint32_t lorawan_get_dl_freq(CH channelid); uint8_t lorawan_get_battery(void); void lorawan_get_multicast_key(uint8_t index, const char * &address, const char * &nwkskey, const char * &appskey); /* AS923 Only --> */ STATUS lorawan_set_uplink_dwell_table(uint8_t index, uint32_t minfreq, uint32_t maxfreq, uint8_t uplinkdwell); void lorawan_get_uplink_dwell_table(uint8_t index, uint32_t &minfreq, uint32_t &maxfreq, uint8_t &uplinkdwell); STATUS lorawan_set_max_tx(MAX_EIRP maxeirp = MAX_EIRP_14dBm); STATUS lorawan_set_uplink_dwell(uint8_t uplinkdwell = 0); STATUS lorawan_set_downlink_dwell(uint8_t downlinkdwell = 0); STATUS lorawan_set_rx1dr_offset(uint8_t rx1droffset = 0); void lorawan_get_as923_para(MAX_EIRP &maxeirp, uint8_t &uplinkdwell, uint8_t &downlinkdwell, uint8_t &rx1droffset); /* <-- AS923 Only */ void lorawan_handle(void); /* * P2P Commands */ void p2p_callback(p2p_rx_t cb); STATUS p2p_rx(uint16_t rxwindowtime = 3000); STATUS p2p_tx(const char *payload, int len = -1); STATUS p2p_set_freq(uint32_t frequency = 922500000); STATUS p2p_set_pwr(TXPWR power = TXPWR14); STATUS p2p_set_sf(SF spreadingfacotr = SF7); STATUS p2p_set_bw(BW bandwidth = BW125); STATUS p2p_set_cr(CR codingrate = CR46); STATUS p2p_set_prlen(uint16_t preamblelength = 12); STATUS p2p_set_crc(bool state = true); STATUS p2p_set_iqi(bool invert = false); STATUS p2p_set_sync(uint8_t syncword = 0x12); STATUS p2p_save(void); uint32_t p2p_get_freq(void); TXPWR p2p_get_pwr(void); SF p2p_get_sf(void); BW p2p_get_bw(void); uint16_t p2p_get_prlen(void); bool p2p_get_crc(void); bool p2p_get_iqi(void); CR p2p_get_cr(void); uint8_t p2p_get_sync(void); protected: STATUS lorawan_set_linkchk(void); static int hex2bin(char c); static size_t hexlen(const char *hex); STATUS validate(const char *response, size_t *size = nullptr); const char *command(const char *format,...); bool request(const char *buf, size_t len, unsigned long timeout); bool response(unsigned long timeout); virtual void baudrate(uint32_t speed) = 0; virtual size_t read(char *buf, size_t len, unsigned long timeout) = 0; virtual size_t write(const char *buf, size_t len, unsigned long timeout) = 0; unsigned long _timeout; const char * _txdata; int _txsize; lorawan_rx_t _lorawan_rx; p2p_rx_t _p2p_rx; char _buffer[64 + (TLM922S_MAX_PAYLOAD_SIZE * 2)]; }; #ifdef ARDUINO #include <Arduino.h> #define TLM922S_ARDUINO(obj) TLM922S_ARDUINO<typeof(obj), obj> template<typename T, T& OBJ> class TLM922S_ARDUINO : public TLM922SClass { public: void begin(unsigned long speed = 9600) { OBJ.begin(speed); } void end(void) { OBJ.end(); } protected: void baudrate(uint32_t speed) override { end(); begin(speed); } size_t read(char *buf, size_t len, unsigned long timeout) override { OBJ.setTimeout(timeout); return OBJ.readBytes(buf, len); } size_t write(const char *buf, size_t len, unsigned long timeout) override { size_t cnt = 0; if (len > 0) { unsigned long t = millis(); do { if ((cnt = OBJ.write(buf, len))) break; yield(); } while ((unsigned long)millis() - t < timeout); } return cnt; } }; #else #include "serial.h" class TLM922S : public TLM922SClass { public: int begin(const char *device, unsigned long speed = 9600) { int rv = _serial.open(device, speed); if (rv >= 0) snprintf(_device, sizeof(_device), "%s", device); return rv; } void end(void) { _serial.close(); } protected: void baudrate(uint32_t speed) override { end(); begin(_device, speed); } size_t read(char *buf, size_t len, unsigned long timeout) override { ssize_t rv = _serial.read(buf, len, timeout); return rv > 0 ? rv : 0; } size_t write(const char *buf, size_t len, unsigned long timeout) override { ssize_t rv = _serial.write(buf, len, timeout); return rv > 0 ? rv : 0; } private: Serial _serial; char _device[16]; }; #endif #ifdef __AVR__ #include <avr/pgmspace.h> #define PGM_PTR(v) pgm_read_ptr(&(v)) #define PGM_BYTE(v) pgm_read_byte(&(v)) #define PGM_STR(s) PSTR(s) #else #ifndef PGM_PTR #ifndef pgm_read_ptr #define PGM_PTR #else #define PGM_PTR(v) pgm_read_ptr(&(v)) #endif #endif #ifndef PGM_BYTE #ifndef pgm_read_byte #define PGM_BYTE #else #define PGM_BYTE(v) pgm_read_byte(&(v)) #endif #endif #ifndef PGM_STR #ifndef PSTR #define PGM_STR #else #define PGM_STR(s) PSTR(s) #endif #endif #ifndef PROGMEM #define PROGMEM #endif #ifndef strcasecmp_P #define strcasecmp_P strcasecmp #endif #ifndef strncasecmp_P #define strncasecmp_P strncasecmp #endif #ifndef strncmp_P #define strncmp_P strncmp #endif #ifndef strlen_P #define strlen_P strlen #endif #ifndef snprintf_P #define snprintf_P snprintf #endif #ifndef vsnprintf_P #define vsnprintf_P vsnprintf #endif #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
/* tlm922s.cpp - Kiwi TLM922S Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tlm922s.h" PROGMEM static const char CH_PLANS_NONE[] = "none"; PROGMEM static const char CH_PLANS_EU868[] = "EU868"; PROGMEM static const char CH_PLANS_EU433[] = "EU433"; PROGMEM static const char CH_PLANS_CN780[] = "CN780"; PROGMEM static const char CH_PLANS_AS923[] = "AS923"; PROGMEM static const char CH_PLANS_KR920[] = "KR920"; PROGMEM static const char CH_PLANS_IND865[] = "IND865"; PROGMEM static const char CH_PLANS_US915[] = "US915"; PROGMEM static const char CH_PLANS_US915_HYBRID[] = "US915_HYBRID"; PROGMEM static const char CH_PLANS_AU915[] = "AU915"; PROGMEM static const char * const CH_PLANS[] = { CH_PLANS_NONE, CH_PLANS_EU868, CH_PLANS_EU433, CH_PLANS_CN780, CH_PLANS_AS923, CH_PLANS_KR920, CH_PLANS_IND865, CH_PLANS_US915, CH_PLANS_US915_HYBRID, CH_PLANS_AU915, }; PROGMEM static const char RESPONSES_OK[] = "Ok"; PROGMEM static const char RESPONSES_INVALID[] = "Invalid"; PROGMEM static const char RESPONSES_JOINED[] = "joined"; PROGMEM static const char RESPONSES_UNJOINED[] = "unjoined"; PROGMEM static const char RESPONSES_ACCEPTED[] = "accepted"; PROGMEM static const char RESPONSES_UNSUCCESS[] = "unsuccess"; PROGMEM static const char RESPONSES_KEYS_NOT_INIT[] = "keys_not_init"; PROGMEM static const char RESPONSES_NOT_JOINED[] = "not_joined"; PROGMEM static const char RESPONSES_NO_FREE_CH[] = "no_free_ch"; PROGMEM static const char RESPONSES_BUSY[] = "busy"; PROGMEM static const char RESPONSES_INVALID_DATA_LENGTH[] = "invalid_data_length"; PROGMEM static const char RESPONSES_TX_OK[] = "tx_ok"; PROGMEM static const char RESPONSES_RX[] = "rx "; PROGMEM static const char RESPONSES_ERR[] = "err"; PROGMEM static const char RESPONSES_DEMODMARGIN[] = "DemodMargin = "; PROGMEM static const char RESPONSES_NBGATEWAYS[] = "NbGateways = "; PROGMEM static const char RESPONSES_RADIO_RX[] = "radio_rx "; PROGMEM static const char RESPONSES_RADIO_ERR_TIMEOUT[] = "radio_err_timeout"; PROGMEM static const char RESPONSES_RADIO_ERR[] = "radio_err"; PROGMEM static const char RESPONSES_RADIO_TX_OK[] = "radio_tx_ok"; PROGMEM static const char RESPONSES_UNKNOWN_COMMAND[] = "Unknown command!"; PROGMEM static const char * const RESPONSES[] = { RESPONSES_OK, RESPONSES_INVALID, RESPONSES_JOINED, RESPONSES_UNJOINED, RESPONSES_ACCEPTED, RESPONSES_UNSUCCESS, RESPONSES_KEYS_NOT_INIT, RESPONSES_NOT_JOINED, RESPONSES_NO_FREE_CH, RESPONSES_BUSY, RESPONSES_INVALID_DATA_LENGTH, RESPONSES_TX_OK, RESPONSES_RX, RESPONSES_ERR, RESPONSES_DEMODMARGIN, RESPONSES_NBGATEWAYS, RESPONSES_RADIO_RX, RESPONSES_RADIO_ERR_TIMEOUT, RESPONSES_RADIO_ERR, RESPONSES_RADIO_TX_OK, RESPONSES_UNKNOWN_COMMAND }; PROGMEM static const char CODING_RATES_45[] = "4/5"; PROGMEM static const char CODING_RATES_46[] = "4/6"; PROGMEM static const char CODING_RATES_47[] = "4/7"; PROGMEM static const char CODING_RATES_48[] = "4/8"; PROGMEM static const char * const CODING_RATES[] = { CODING_RATES_45, CODING_RATES_46, CODING_RATES_47, CODING_RATES_48 }; PROGMEM static const char RESPONSE_PREFIX[] = { '>', '>', ' ' }; #define RESPONSE_DATA (_buffer + sizeof(RESPONSE_PREFIX)) #define MAX_STRING_SIZE 24 static const char _ON_[] = "on"; static const char _OFF_[] = "off"; TLM922SClass::TLM922SClass(unsigned long timeout) : _timeout(timeout), _txdata(nullptr), _txsize(0), _lorawan_rx(nullptr), _p2p_rx(nullptr) { } TLM922SClass::~TLM922SClass(void) { } void TLM922SClass::lorawan_callback(lorawan_rx_t cb) { _lorawan_rx = cb; } void TLM922SClass::p2p_callback(p2p_rx_t cb) { _p2p_rx = cb; } const char *TLM922SClass::strchplan(LORAWAN_CH_PLAN ch_plan, char *buf, size_t len) { if (buf == nullptr) buf = _buffer + sizeof(_buffer) - (len = MAX_STRING_SIZE); snprintf_P(buf, len, (const char *)PGM_PTR(CH_PLANS[ch_plan])); return buf; } const char *TLM922SClass::strstatus(STATUS status, char *buf, size_t len) { if (buf == nullptr) buf = _buffer + sizeof(_buffer) - (len = MAX_STRING_SIZE); snprintf_P(buf, len, (const char *)PGM_PTR(RESPONSES[status])); return buf; } const char *TLM922SClass::strcr(CR cr, char *buf, size_t len) { if (buf == nullptr) buf = _buffer + sizeof(_buffer) - (len = MAX_STRING_SIZE); snprintf_P(buf, len, (const char *)PGM_PTR(CODING_RATES[cr])); return buf; } /* * Module Commands */ const char *TLM922SClass::mod_factory_reset(void) { return command(PGM_STR("mod factory_reset")); } const char *TLM922SClass::mod_get_ver(void) { return command(PGM_STR("mod get_ver")); } const char *TLM922SClass::mod_get_hw_deveui(void) { return command(PGM_STR("mod get_hw_deveui")); } const char *TLM922SClass::mod_get_hw_model(void) { return command(PGM_STR("mod get_hw_model")); } TLM922SClass::STATUS TLM922SClass::mod_set_pin_mode(PIN pin, PIN_MODE mode) { return validate(command(PGM_STR("mod set_pin_mode P%02u %s"), pin, mode ? "di" : "do")); } uint8_t TLM922SClass::mod_get_dio(PIN pin) { return atoi(command(PGM_STR("mod get_dio P%02u"), pin)); } TLM922SClass::STATUS TLM922SClass::mod_set_dio(PIN pin, uint8_t value) { return validate(command(PGM_STR("mod set_dio P%02u %u"), pin, value != 0)); } TLM922SClass::STATUS TLM922SClass::mod_sleep(bool deep, bool wakeup, uint16_t seconds) { return validate(command(PGM_STR("mod sleep %u %u %u"), deep, wakeup, seconds)); } TLM922SClass::STATUS TLM922SClass::mod_set_baudrate(BAUDRATE speed) { /* response is "OK". (capital letter 'K') */ STATUS status = validate(command(PGM_STR("mod set_baudrate %lu"), speed)); if (status == STATUS_OK) baudrate(speed); return status; } TLM922SClass::STATUS TLM922SClass::mod_set_echo(bool enable) { return validate(command(PGM_STR("mod set_echo %s"), enable ? _ON_ : _OFF_)); } void TLM922SClass::mod_reset(void) { static const char RESET[] = "mod reset\r"; uint32_t timeout = 1000; /* reset command */ request(RESET, strlen(RESET), timeout); /* waiting for reboot */ char c; while (read(&c, sizeof(c), timeout) != 0) timeout = 100; } TLM922SClass::STATUS TLM922SClass::mod_save(void) { return validate(command(PGM_STR("mod save"))); } /* * LoRaWAN Commands */ TLM922SClass::STATUS TLM922SClass::lorawan_join(LORAWAN_MODE mode) { STATUS status = validate(command(PGM_STR("lorawan join %s"), mode ? "abp" : "otaa")); return status != STATUS_OK ? status : response(_timeout) ? validate(RESPONSE_DATA) : STATUS_ERR; } TLM922SClass::STATUS TLM922SClass::lorawan_get_join_status(void) { return validate(command(PGM_STR("lorawan get_join_status"))); } TLM922SClass::STATUS TLM922SClass::lorawan_set_ch_plan(LORAWAN_CH_PLAN ch_plan, MAX_EIRP maxeirp, uint8_t uplinkdwell, uint8_t downlinkdwell, uint8_t rx1droffset) { STATUS status; if ((status = validate(command(PGM_STR("lorawan set_ch_plan %s"), strchplan(ch_plan)))) == STATUS_OK) { if (ch_plan == LORAWAN_CH_PLAN_AS923) { if ((status = lorawan_set_max_tx(maxeirp)) != STATUS_OK) return status; if ((status = lorawan_set_uplink_dwell(uplinkdwell)) != STATUS_OK) return status; if ((status = lorawan_set_downlink_dwell(downlinkdwell)) != STATUS_OK) return status; if ((status = lorawan_set_rx1dr_offset(rx1droffset)) != STATUS_OK) return status; } } return status; } TLM922SClass::LORAWAN_CH_PLAN TLM922SClass::lorawan_get_ch_plan(void) { const char *rv = command(PGM_STR("lorawan get_ch_plan")); for (uint8_t i = 0; i <= sizeof(CH_PLANS) / sizeof(CH_PLANS[0]); ++i) { if (strcasecmp_P(rv, (const char *)PGM_PTR(CH_PLANS[i])) == 0) return (LORAWAN_CH_PLAN)i; } return LORAWAN_CH_PLAN_NONE; } TLM922SClass::STATUS TLM922SClass::lorawan_tx(bool confirmed, uint8_t port, const char *payload, int len) { return lorawan_tx(nullptr, nullptr, confirmed, port, payload, len); } TLM922SClass::STATUS TLM922SClass::lorawan_tx(uint8_t *demodmargin, uint8_t *nbgateways, bool confirmed, uint8_t port, const char *payload, int len) { if (demodmargin) *demodmargin = 0; if (nbgateways) *nbgateways = 0; if (demodmargin || nbgateways) lorawan_set_linkchk(); _txdata = payload; _txsize = len; STATUS status = validate(command(PGM_STR("lorawan tx %s %u "), confirmed ? "cnf" : "ucnf", port)); if (status == STATUS_OK) { while (response(_timeout)) { size_t size; switch ((status = validate(RESPONSE_DATA, &size))) { default: break; case STATUS_DEMODMARGIN: if (demodmargin) *demodmargin = strtoul(RESPONSE_DATA + size, nullptr, 10); break; case STATUS_NBGATEWAYS: if (nbgateways) *nbgateways = strtoul(RESPONSE_DATA + size, nullptr, 10); break; case STATUS_TX_OK: case STATUS_ERR: return status; } } status = STATUS_ERR; } return status; } TLM922SClass::STATUS TLM922SClass::lorawan_set_deveui(const char *deveui) { return validate(command(PGM_STR("lorawan set_deveui %s"), deveui)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_appeui(const char *appeui) { return validate(command(PGM_STR("lorawan set_appeui %s"), appeui)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_appkey(const char *appkey) { return validate(command(PGM_STR("lorawan set_appkey %s"), appkey)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_devaddr(const char *devaddr) { return validate(command(PGM_STR("lorawan set_devaddr %s"), devaddr)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_nwkskey(const char *nwksessionkey) { return validate(command(PGM_STR("lorawan set_nwkskey %s"), nwksessionkey)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_appskey(const char *appsessionkey) { return validate(command(PGM_STR("lorawan set_appskey %s"), appsessionkey)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_pwr(TXPWI powerindex) { return validate(command(PGM_STR("lorawan set_pwr %u"), powerindex)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_dr(DR datarate) { return validate(command(PGM_STR("lorawan set_dr %u"), datarate)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_adr(bool state) { return validate(command(PGM_STR("lorawan set_adr %s"), state ? _ON_ : _OFF_)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_txretry(uint8_t retrycount) { return validate(command(PGM_STR("lorawan set_txretry %u"), retrycount)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_rxdelay1(uint16_t retrycount) { return validate(command(PGM_STR("lorawan set_rxdelay1 %u"), retrycount)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_rx2(DR datarate, uint32_t frequency) { return validate(command(PGM_STR("lorawan set_rx2 %u %lu"), datarate, frequency)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_ch_freq(CH channelid, uint32_t frequency) { return validate(command(PGM_STR("lorawan set_ch_freq %u %lu"), channelid, frequency)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_ch_dr_range(CH channelid, DR mindr, DR maxdr) { return validate(command(PGM_STR("lorawan set_ch_dr_range %u %u %u"), channelid, mindr, maxdr)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_ch_status(CH channelid, bool status) { return validate(command(PGM_STR("lorawan set_ch_status %u %s"), channelid, status ? _ON_ : _OFF_)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_linkchk(void) { return validate(command(PGM_STR("lorawan set_linkchk"))); } TLM922SClass::STATUS TLM922SClass::lorawan_set_dc_ctl(bool status) { return validate(command(PGM_STR("lorawan set_dc_ctl %s"), status ? _ON_ : _OFF_)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_dc_band(BAND bandid, uint32_t minfreq, uint32_t maxfreq, uint16_t dutycycle) { return validate(command(PGM_STR("lorawan set_dc_band %u %lu %lu %u"), bandid, minfreq, maxfreq, dutycycle)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_join_ch(CH channelid, bool status) { return validate(command(PGM_STR("lorawan set_join_ch %u %s"), channelid, status ? _ON_ : _OFF_)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_upcnt(uint32_t uplinkcounter) { return validate(command(PGM_STR("lorawan set_upcnt %lu"), uplinkcounter)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_downcnt(uint32_t downlinkcounter) { return validate(command(PGM_STR("lorawan set_downcnt %lu"), downlinkcounter)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_class(LORAWAN_CLASS aclass) { return validate(command(PGM_STR("lorawan set_class %c"), aclass + 'A')); } TLM922SClass::STATUS TLM922SClass::lorawan_set_pwr_table(TXPWI index, TXPWR power) { return validate(command(PGM_STR("lorawan set_pwr_table %u %u"), index, power)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_max_payload_table(DR drindex, uint8_t maxpayload) { return validate(command(PGM_STR("lorawan set_max_payload_table %u %u"), drindex, maxpayload)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_dl_freq(CH channelid, uint32_t frequency) { return validate(command(PGM_STR("lorawan set_dl_freq %u %lu"), channelid, frequency)); } TLM922SClass::STATUS TLM922SClass::lorawan_update_payload_table(uint16_t dwelltime) { return validate(command(PGM_STR("lorawan update_payload_table %u"), dwelltime)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_battery(uint8_t batterylevel) { return validate(command(PGM_STR("lorawan set_battery %u"), batterylevel)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_multicast_key(uint8_t index, const char *address, const char *nwkskey, const char *appskey) { return validate(command(PGM_STR("lorawan set_multicast_key %u %s, %s, %s"), index, address, nwkskey, appskey)); } TLM922SClass::STATUS TLM922SClass::lorawan_set_lbt_retry(uint8_t retrycount) { return validate(command(PGM_STR("lorawan set_lbt_retry %u"), retrycount)); } TLM922SClass::STATUS TLM922SClass::lorawan_save(void) { return validate(command(PGM_STR("lorawan save"))); } const char *TLM922SClass::lorawan_get_devaddr(void) { return command(PGM_STR("lorawan get_devaddr")); } const char *TLM922SClass::lorawan_get_deveui(void) { return command(PGM_STR("lorawan get_deveui")); } const char *TLM922SClass::lorawan_get_appeui(void) { return command(PGM_STR("lorawan get_appeui")); } const char *TLM922SClass::lorawan_get_nwkskey(void) { return command(PGM_STR("lorawan get_nwkskey")); } const char *TLM922SClass::lorawan_get_appskey(void) { return command(PGM_STR("lorawan get_appskey")); } const char *TLM922SClass::lorawan_get_appkey(void) { return command(PGM_STR("lorawan get_appkey")); } TLM922SClass::DR TLM922SClass::lorawan_get_dr(void) { return (DR)atoi(command(PGM_STR("lorawan get_dr"))); } TLM922SClass::TXPWI TLM922SClass::lorawan_get_pwr(void) { return (TXPWI)atoi(command(PGM_STR("lorawan get_pwr"))); } bool TLM922SClass::lorawan_get_adr(void) { return strcasecmp(command(PGM_STR("lorawan get_adr")), _ON_) == 0; } uint8_t TLM922SClass::lorawan_get_txretry(void) { return atoi(command(PGM_STR("lorawan get_txretry"))); } void TLM922SClass::lorawan_get_rxdelay(uint16_t &rxdelay1, uint16_t &rxdelay2) { char *data = (char *)command(PGM_STR("lorawan get_rxdelay")); rxdelay1 = strtoul(data, &data, 10); rxdelay2 = strtoul(data + 1, &data, 10); } void TLM922SClass::lorawan_get_rx2(DR &dr, uint32_t &freq) { char *data = (char *)command(PGM_STR("lorawan get_rx2")); dr = (DR)strtoul(data, &data, 10); freq = strtoul(data + 1, &data, 10); } void TLM922SClass::lorawan_get_ch_para(CH channelid, uint32_t &freq, uint8_t &mindr, uint8_t &maxdr) { char *data = (char *)command(PGM_STR("lorawan get_ch_para %u"), channelid); freq = strtoul(data, &data, 10); mindr = strtoul(data + 1, &data, 10); maxdr = strtoul(data + 1, &data, 10); } bool TLM922SClass::lorawan_get_ch_status(CH channelid) { return strcasecmp(command(PGM_STR("lorawan get_ch_status %u"), channelid), _ON_) == 0; } bool TLM922SClass::lorawan_get_dc_ctl(void) { return strcasecmp(command(PGM_STR("lorawan get_dc_ctl")), _ON_) == 0; } void TLM922SClass::lorawan_get_dc_band(BAND bandid, uint32_t &minfreq, uint32_t &maxfreq, uint16_t &dutycycle) { char *data = (char *)command(PGM_STR("lorawan get_dc_band %u"), bandid); minfreq = strtoul(data, &data, 10); maxfreq = strtoul(data + 1, &data, 10); dutycycle = strtoul(data + 1, &data, 10); } uint8_t TLM922SClass::lorawan_get_join_ch(CH *channelids, uint8_t count) { uint8_t cnt; char *data = (char *)command(PGM_STR("lorawan get_join_ch")); for (cnt = 0; (cnt < count) && (*data >= '0') && (*data <= '9'); ++cnt) { *channelids++ = (CH)strtoul(data, (char **)&data, 10); if (*data == ' ') ++data; } return cnt; } uint32_t TLM922SClass::lorawan_get_upcnt(void) { return strtoul(command(PGM_STR("lorawan get_upcnt")), nullptr, 10); } uint32_t TLM922SClass::lorawan_get_downcnt(void) { return strtoul(command(PGM_STR("lorawan get_downcnt")), nullptr, 10); } TLM922SClass::LORAWAN_CLASS TLM922SClass::lorawan_get_class(void) { return (LORAWAN_CLASS)(command(PGM_STR("lorawan get_class"))[0] - 'A'); } TLM922SClass::TXPWR TLM922SClass::lorawan_get_pwr_table(TXPWI index) { return (TXPWR)atoi(command(PGM_STR("lorawan get_pwr_table %u"), index)); } uint8_t TLM922SClass::lorawan_get_max_payload_table(DR drindex) { return atoi(command(PGM_STR("lorawan get_max_payload_table %u"), drindex)); } uint32_t TLM922SClass::lorawan_get_dl_freq(CH channelid) { return strtoul(command(PGM_STR("lorawan get_dl_freq %u"), channelid), nullptr, 10); } uint8_t TLM922SClass::lorawan_get_battery(void) { return atoi(command(PGM_STR("lorawan get_battery"))); } void TLM922SClass::lorawan_get_multicast_key(uint8_t index, const char * &address, const char * &nwkskey, const char * &appskey) { char *data = (char *)command(PGM_STR("lorawan get_multicast_key %u"), index); data += hexlen(address = data); *data++ = 0; data += hexlen(nwkskey = data); *data++ = 0; data += hexlen(appskey = data); *data = 0; } /* AS923 Only */ TLM922SClass::STATUS TLM922SClass::lorawan_set_uplink_dwell_table(uint8_t index, uint32_t minfreq, uint32_t maxfreq, uint8_t uplinkdwell) { return validate(command(PGM_STR("lorawan set_uplink_dwell_table %u %lu %lu %u"), index, minfreq, maxfreq, uplinkdwell)); } /* AS923 Only */ void TLM922SClass::lorawan_get_uplink_dwell_table(uint8_t index, uint32_t &minfreq, uint32_t &maxfreq, uint8_t &uplinkdwell) { char *data = (char *)command(PGM_STR("lorawan get_uplink_dwell_table %u"), index); minfreq = strtoul(data, &data, 10); maxfreq = strtoul(data + 1, &data, 10); uplinkdwell = strtoul(data + 1, &data, 10); } /* AS923 Only */ TLM922SClass::STATUS TLM922SClass::lorawan_set_max_tx(MAX_EIRP maxeirp) { return validate(command(PGM_STR("lorawan set_max_tx %u"), maxeirp)); } /* AS923 Only */ TLM922SClass::STATUS TLM922SClass::lorawan_set_uplink_dwell(uint8_t uplinkdwell) { return validate(command(PGM_STR("lorawan set_uplink_dwell %u"), uplinkdwell)); } /* AS923 Only */ TLM922SClass::STATUS TLM922SClass::lorawan_set_downlink_dwell(uint8_t downlinkdwell) { return validate(command(PGM_STR("lorawan set_downlink_dwell %u"), downlinkdwell)); } /* AS923 Only */ TLM922SClass::STATUS TLM922SClass::lorawan_set_rx1dr_offset(uint8_t rx1droffset) { return validate(command(PGM_STR("lorawan set_rx1dr_offset %u"), rx1droffset)); } /* AS923 Only */ void TLM922SClass::lorawan_get_as923_para(MAX_EIRP &maxeirp, uint8_t &uplinkdwell, uint8_t &downlinkdwell, uint8_t &rx1droffset) { char *data = (char *)command(PGM_STR("lorawan get_as923_para")); maxeirp = (MAX_EIRP)strtoul(data, &data, 10); uplinkdwell = strtoul(data + 1, &data, 10); downlinkdwell = strtoul(data + 1, &data, 10); rx1droffset = strtoul(data + 1, &data, 10); } void TLM922SClass::lorawan_handle(void) { response(0); } /* * P2P Commands */ TLM922SClass::STATUS TLM922SClass::p2p_rx(uint16_t rxwindowtime) { STATUS status = validate(command(PGM_STR("p2p rx %u"), rxwindowtime)); if (status == STATUS_OK) { if (response(rxwindowtime)) { size_t size; if ((status = validate(RESPONSE_DATA, &size)) == STATUS_RADIO_RX) { int8_t rssi, snr; char *save, *data = RESPONSE_DATA + size; size = hexlen(save = data); rssi = strtol(data + size + 1, &data, 10); snr = strtol(data + 1, &data, 10); if (_p2p_rx) _p2p_rx(save, hex2bin(save, size, save, size), rssi, snr); } } else status = STATUS_ERR; } return status; } TLM922SClass::STATUS TLM922SClass::p2p_tx(const char *payload, int len) { _txdata = payload; _txsize = len; STATUS status = validate(command(PGM_STR("p2p tx "))); return status != STATUS_OK ? status : response(_timeout) ? validate(RESPONSE_DATA) : STATUS_ERR; } TLM922SClass::STATUS TLM922SClass::p2p_set_freq(uint32_t frequency) { /* frequency is 862000000 to 932000000 */ return validate(command(PGM_STR("p2p set_freq %lu"), frequency)); } TLM922SClass::STATUS TLM922SClass::p2p_set_pwr(TXPWR power) { return validate(command(PGM_STR("p2p set_pwr %u"), power)); } TLM922SClass::STATUS TLM922SClass::p2p_set_sf(SF spreadingfacotr) { return validate(command(PGM_STR("p2p set_sf %u"), spreadingfacotr)); } TLM922SClass::STATUS TLM922SClass::p2p_set_bw(BW bandwidth) { return validate(command(PGM_STR("p2p set_bw %u"), bandwidth)); } TLM922SClass::STATUS TLM922SClass::p2p_set_cr(CR codingrate) { return validate(command(PGM_STR("p2p set_cr %s"), strcr(codingrate))); } TLM922SClass::STATUS TLM922SClass::p2p_set_prlen(uint16_t preamblelength) { return validate(command(PGM_STR("p2p set_prlen %u"), preamblelength)); } TLM922SClass::STATUS TLM922SClass::p2p_set_crc(bool state) { return validate(command(PGM_STR("p2p set_crc %s"), state ? _ON_ : _OFF_)); } TLM922SClass::STATUS TLM922SClass::p2p_set_iqi(bool invert) { return validate(command(PGM_STR("p2p set_iqi %s"), invert ? _ON_ : _OFF_)); } TLM922SClass::STATUS TLM922SClass::p2p_set_sync(uint8_t syncword) { return validate(command(PGM_STR("p2p set_sync %02x"), syncword)); } TLM922SClass::STATUS TLM922SClass::p2p_save(void) { return validate(command(PGM_STR("p2p save"))); } uint32_t TLM922SClass::p2p_get_freq(void) { return strtoul(command(PGM_STR("p2p get_freq")), nullptr, 10); } TLM922SClass::TXPWR TLM922SClass::p2p_get_pwr(void) { return (TXPWR)atoi(command(PGM_STR("p2p get_pwr"))); } TLM922SClass::SF TLM922SClass::p2p_get_sf(void) { return (SF)atoi(command(PGM_STR("p2p get_sf"))); } TLM922SClass::BW TLM922SClass::p2p_get_bw(void) { return (BW)atoi(command(PGM_STR("p2p get_bw"))); } uint16_t TLM922SClass::p2p_get_prlen(void) { return atoi(command(PGM_STR("p2p get_prlen"))); } bool TLM922SClass::p2p_get_crc(void) { return strcasecmp(command(PGM_STR("p2p get_crc")), _ON_) == 0; } bool TLM922SClass::p2p_get_iqi(void) { return strcasecmp(command(PGM_STR("p2p get_iqi")), _ON_) == 0; } TLM922SClass::CR TLM922SClass::p2p_get_cr(void) { const char *p = command(PGM_STR("p2p get_cr")); for (uint8_t i = 1; i < sizeof(CODING_RATES) / sizeof(CODING_RATES[0]); ++i) { if (strcasecmp_P(p, (const char *)PGM_PTR(CODING_RATES[i])) == 0) return (CR)i; } return CR46; } uint8_t TLM922SClass::p2p_get_sync(void) { return strtol(command(PGM_STR("p2p get_sync")), nullptr, 16); } size_t TLM922SClass::bin2hex(char *buf, size_t size, const char *bin, int len) { PROGMEM static const char XDIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; size_t cnt = 0; if (size) { if (len < 0) len = strlen(bin); while (len-- && (cnt + 2 < size)) { char c = *bin++; *buf++ = PGM_BYTE(XDIGITS[(c >> 4) & 0x0F]); *buf++ = PGM_BYTE(XDIGITS[(c >> 0) & 0x0F]); cnt += 2; } *buf = 0; } return cnt; } size_t TLM922SClass::hex2bin(char *buf, size_t size, const char *hex, int len) { size_t cnt = 0; if (size) { if (len < 0) len = strlen(hex); while (len-- && (cnt + 1 < size)) { int h, l; if ((h = hex2bin(*hex++)) < 0) break; if ((l = hex2bin(*hex++)) < 0) break; *buf++ = (h << 4) | l; ++cnt; } *buf = 0; } return cnt; } int TLM922SClass::hex2bin(char c) { if ((c >= '0') && (c <= '9')) return c - '0'; if ((c >= 'A') && (c <= 'F')) return c - 'A' + 10; if ((c >= 'a') && (c <= 'f')) return c - 'a' + 10; return -1; } size_t TLM922SClass::hexlen(const char *hex) { size_t cnt = 0; while (hex2bin(*hex++) != -1) cnt++; return cnt; } TLM922SClass::STATUS TLM922SClass::validate(const char *response, size_t *size) { for (uint8_t i = 0; i < sizeof(RESPONSES) / sizeof(RESPONSES[0]); ++i) { const char *str = (const char *)PGM_PTR(RESPONSES[i]); size_t len = strlen_P(str); if (strncasecmp_P(response, str, len) == 0) { if (size) *size = len; return (STATUS)i; } } return STATUS_UNKNOWN; } const char *TLM922SClass::command(const char *format,...) { va_list ap; va_start(ap, format); size_t cnt = vsnprintf_P(_buffer, sizeof(_buffer) - (TLM922S_MAX_PAYLOAD_SIZE * 2), format, ap); va_end(ap); if (_txdata && _txsize) cnt += bin2hex(_buffer + cnt, (TLM922S_MAX_PAYLOAD_SIZE * 2), _txdata, _txsize); _txdata = nullptr; _txsize = 0; _buffer[cnt++] = '\r'; _buffer[cnt ] = 0; return request(_buffer, cnt, _timeout) && response(_timeout) ? RESPONSE_DATA : strstatus(STATUS_ERR); } bool TLM922SClass::request(const char *buf, size_t len, unsigned long timeout) { while (len) { size_t n = write(buf, len, timeout); if (n == 0) break; len -= n; buf += n; } return len == 0; } bool TLM922SClass::response(unsigned long timeout) { unsigned long save = timeout; for (size_t cnt = 0; cnt < sizeof(_buffer); ) { char c; if (read(&c, sizeof(c), timeout) != sizeof(c)) { // bug support: p2p_rx() --> "[CR]>> Ok[timeout][LF]" _buffer[cnt] = 0; return strncmp_P(_buffer, RESPONSE_PREFIX, sizeof(RESPONSE_PREFIX)) == 0; } timeout = 100; if (c >= ' ') _buffer[cnt++] = c; else { _buffer[cnt] = 0; if (strncmp_P(_buffer, RESPONSE_PREFIX, sizeof(RESPONSE_PREFIX)) == 0) { char *data = (char *)RESPONSES_RX; cnt = strlen_P(data); if (strncasecmp_P(RESPONSE_DATA, data, cnt) != 0) return true; if (_lorawan_rx) { uint8_t port = strtoul(RESPONSE_DATA + cnt, &data, 10); cnt = strlen(++data); _lorawan_rx(data, hex2bin(data, cnt, data, cnt), port); } timeout = save; } cnt = 0; } } return false; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
/* serial.h - Serial I/O Library Copyright (c) 2021 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _SERIAL_H #define _SERIAL_H #include <stdio.h> #include <stdint.h> #include <string.h> #include <fcntl.h> #ifdef __linux__ #include <unistd.h> #include <termios.h> #include <sys/time.h> #include <sys/types.h> #define DEVICE_PATH "/dev/%s" #define OPEN_MODE (O_RDWR | O_NOCTTY) #else #include <windows.h> #if SIZE_MAX == UINT64_MAX #define ssize_t long long #else #define ssize_t int #endif #define DEVICE_PATH "\\\\.\\%s" #define OPEN_MODE (O_RDWR | _O_BINARY) #endif typedef enum { SERIAL_FRAME_5N1, SERIAL_FRAME_6N1, SERIAL_FRAME_7N1, SERIAL_FRAME_8N1, SERIAL_FRAME_5N2, SERIAL_FRAME_6N2, SERIAL_FRAME_7N2, SERIAL_FRAME_8N2, SERIAL_FRAME_5O1, SERIAL_FRAME_6O1, SERIAL_FRAME_7O1, SERIAL_FRAME_8O1, SERIAL_FRAME_5O2, SERIAL_FRAME_6O2, SERIAL_FRAME_7O2, SERIAL_FRAME_8O2, SERIAL_FRAME_5E1, SERIAL_FRAME_6E1, SERIAL_FRAME_7E1, SERIAL_FRAME_8E1, SERIAL_FRAME_5E2, SERIAL_FRAME_6E2, SERIAL_FRAME_7E2, SERIAL_FRAME_8E2, } SERIAL_FRAME; typedef enum { SERIAL_FLOW_NONE, SERIAL_FLOW_RTSCTS, SERIAL_FLOW_XONOFF, } SERIAL_FLOW; #define SERIAL_FLOW_XONOFF_START 0x11 // DC1 #define SERIAL_FLOW_XONOFF_STOP 0x13 // DC3 #define SERIAL_FLOW_XONOFF_LIMIT 256 class Serial { private: int _fd; long _timeout; #ifdef __linux__ struct termios _tcattr; #else COMMTIMEOUTS _comtimeouts; #endif void config(unsigned long speed, SERIAL_FRAME frame, SERIAL_FLOW flow, long timeout) { #ifdef __linux__ struct termios tcattr; tcgetattr(_fd, &tcattr); _tcattr= tcattr; cfmakeraw(&tcattr); speed_t speed0 = cfgetospeed(&tcattr); int cflags = tcattr.c_cflag & (CSIZE | CSTOPB | PARENB | PARODD); tcattr.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD); switch (speed) { case 50: speed0 = B50; break; case 75: speed0 = B75; break; case 110: speed0 = B110; break; case 134: speed0 = B134; break; case 150: speed0 = B150; break; case 200: speed0 = B200; break; case 300: speed0 = B300; break; case 600: speed0 = B600; break; case 1200: speed0 = B1200; break; case 1800: speed0 = B1800; break; case 2400: speed0 = B2400; break; case 4800: speed0 = B4800; break; case 9600: speed0 = B9600; break; case 19200: speed0 = B19200; break; case 38400: speed0 = B38400; break; case 57600: speed0 = B57600; break; case 115200: speed0 = B115200; break; case 230400: speed0 = B230400; break; case 460800: speed0 = B460800; break; case 500000: speed0 = B500000; break; case 576000: speed0 = B576000; break; case 921600: speed0 = B921600; break; case 1000000: speed0 = B1000000; break; case 1152000: speed0 = B1152000; break; case 1500000: speed0 = B1500000; break; case 2000000: speed0 = B2000000; break; case 2500000: speed0 = B2500000; break; case 3000000: speed0 = B3000000; break; case 3500000: speed0 = B3500000; break; case 4000000: speed0 = B4000000; break; } switch (frame) { case SERIAL_FRAME_5N1: cflags = CS5; break; case SERIAL_FRAME_6N1: cflags = CS6; break; case SERIAL_FRAME_7N1: cflags = CS7; break; case SERIAL_FRAME_8N1: cflags = CS8; break; case SERIAL_FRAME_5N2: cflags = CS5 | CSTOPB; break; case SERIAL_FRAME_6N2: cflags = CS6 | CSTOPB; break; case SERIAL_FRAME_7N2: cflags = CS7 | CSTOPB; break; case SERIAL_FRAME_8N2: cflags = CS8 | CSTOPB; break; case SERIAL_FRAME_5O1: cflags = CS5 | PARENB | PARODD; break; case SERIAL_FRAME_6O1: cflags = CS6 | PARENB | PARODD; break; case SERIAL_FRAME_7O1: cflags = CS7 | PARENB | PARODD; break; case SERIAL_FRAME_8O1: cflags = CS8 | PARENB | PARODD; break; case SERIAL_FRAME_5O2: cflags = CS5 | CSTOPB | PARENB | PARODD; break; case SERIAL_FRAME_6O2: cflags = CS6 | CSTOPB | PARENB | PARODD; break; case SERIAL_FRAME_7O2: cflags = CS7 | CSTOPB | PARENB | PARODD; break; case SERIAL_FRAME_8O2: cflags = CS8 | CSTOPB | PARENB | PARODD; break; case SERIAL_FRAME_5E1: cflags = CS5 | PARENB; break; case SERIAL_FRAME_6E1: cflags = CS6 | PARENB; break; case SERIAL_FRAME_7E1: cflags = CS7 | PARENB; break; case SERIAL_FRAME_8E1: cflags = CS8 | PARENB; break; case SERIAL_FRAME_5E2: cflags = CS5 | CSTOPB | PARENB; break; case SERIAL_FRAME_6E2: cflags = CS6 | CSTOPB | PARENB; break; case SERIAL_FRAME_7E2: cflags = CS7 | CSTOPB | PARENB; break; case SERIAL_FRAME_8E2: cflags = CS8 | CSTOPB | PARENB; break; } cfsetspeed(&tcattr, speed0); tcattr.c_cflag = (tcattr.c_cflag & ~(CSIZE | CSTOPB | PARENB | PARODD)) | cflags; #ifdef CRTSCTS if (flow == SERIAL_FLOW_RTSCTS) tcattr.c_iflag |= CRTSCTS; else tcattr.c_iflag &= ~CRTSCTS; #endif if (flow == SERIAL_FLOW_XONOFF) tcattr.c_iflag |= IXON; else tcattr.c_iflag &= ~IXON; tcattr.c_cc[VSTART] = SERIAL_FLOW_XONOFF_START; tcattr.c_cc[VSTOP] = SERIAL_FLOW_XONOFF_STOP; tcattr.c_oflag &= ~OPOST; tcflush(_fd, TCIOFLUSH); tcsetattr(_fd, TCSANOW, &tcattr); #else DCB dcb; HANDLE hSerial = (HANDLE) _get_osfhandle(_fd); memset(&dcb, 0, sizeof(DCB)); dcb.DCBlength = sizeof(DCB); GetCommState(hSerial, &dcb); dcb.BaudRate = speed; dcb.ByteSize = (frame & 3) + 5; dcb.StopBits = (frame >> 1) & 2; dcb.Parity = (frame >> 3); dcb.fDtrControl = (flow == SERIAL_FLOW_RTSCTS ? DTR_CONTROL_HANDSHAKE : DTR_CONTROL_DISABLE); dcb.fRtsControl = (flow == SERIAL_FLOW_RTSCTS ? RTS_CONTROL_HANDSHAKE : DTR_CONTROL_DISABLE); dcb.fOutxCtsFlow = (flow == SERIAL_FLOW_RTSCTS); dcb.fOutxDsrFlow = (flow == SERIAL_FLOW_RTSCTS); dcb.fOutX = (flow == SERIAL_FLOW_XONOFF); dcb.fInX = (flow == SERIAL_FLOW_XONOFF); dcb.XonLim = SERIAL_FLOW_XONOFF_LIMIT; dcb.XoffLim = SERIAL_FLOW_XONOFF_LIMIT; dcb.XonChar = SERIAL_FLOW_XONOFF_START; dcb.XoffChar = SERIAL_FLOW_XONOFF_STOP; dcb.fTXContinueOnXoff = TRUE; dcb.fDsrSensitivity = FALSE; dcb.fErrorChar = FALSE; dcb.fNull = FALSE; dcb.fAbortOnError = FALSE; dcb.fBinary = TRUE; SetCommState(hSerial, &dcb); PurgeComm(hSerial, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); memset(&_comtimeouts, 0, sizeof(_comtimeouts)); _comtimeouts.ReadIntervalTimeout = MAXDWORD; _comtimeouts.ReadTotalTimeoutMultiplier = MAXDWORD; _comtimeouts.ReadTotalTimeoutConstant = timeout; _comtimeouts.WriteTotalTimeoutConstant = timeout; SetCommTimeouts(hSerial, &_comtimeouts); #endif _timeout = timeout; } public: Serial(void) : _fd(-1) , _timeout(1000) { } virtual ~Serial(void) { close(); } virtual int open(const char *device, unsigned long speed = 115200, SERIAL_FRAME frame = SERIAL_FRAME_8N1, SERIAL_FLOW flow = SERIAL_FLOW_NONE, unsigned long timeout = 1000) { char path[32]; snprintf(path, sizeof(path), DEVICE_PATH, device); if ((_fd = ::open(path, OPEN_MODE)) >= 0) { #ifdef __linux__ if (lockf(_fd, F_TLOCK, 0) < 0) { ::close(_fd); return _fd = -1; } #endif config(speed, frame, flow, timeout); } return _fd; } virtual int close(void) { int rv = 0; if (_fd >= 0) { #ifdef __linux__ tcsetattr(_fd, TCSANOW, &_tcattr); #endif rv = ::close(_fd); _fd = -1; } return rv; } virtual ssize_t read(void *buf, size_t len, long timeout = -1) { int rv; if (timeout < 0) timeout = _timeout; #ifdef __linux__ struct timeval tov = { timeout / 1000, (timeout % 1000) * 1000 }; fd_set rdfds; FD_ZERO(&rdfds); FD_SET(_fd, &rdfds); if ((rv = select(_fd + 1, &rdfds, NULL, NULL, &tov)) > 0) rv = ::read(_fd, buf, len); #else if (_comtimeouts.ReadTotalTimeoutConstant != (DWORD)timeout) { _comtimeouts.ReadTotalTimeoutConstant = timeout; SetCommTimeouts((HANDLE)_get_osfhandle(_fd), &_comtimeouts); } rv = ::read(_fd, buf, len); #endif return rv; } virtual ssize_t write(const void *buf, size_t len, long timeout = -1) { int rv; if (timeout < 0) timeout = _timeout; #ifdef __linux__ struct timeval tov = { timeout / 1000, (timeout % 1000) * 1000 }; fd_set wrfds; FD_ZERO(&wrfds); FD_SET(_fd, &wrfds); if ((rv = select(_fd + 1, NULL, &wrfds, NULL, &tov)) > 0) rv = ::write(_fd, buf, len); #else if (_comtimeouts.WriteTotalTimeoutConstant != (DWORD)timeout) { _comtimeouts.WriteTotalTimeoutConstant = timeout; SetCommTimeouts((HANDLE)_get_osfhandle(_fd), &_comtimeouts); } rv = ::write(_fd, buf, len); #endif return rv; } }; #endif |