firm newest
Dependencies: MTS-Serial libmDot-dev-mbed5-deprecated
CommandTerminal/CmdSendStringOnInterval.cpp@1:c1c6549b9333, 2018-10-22 (annotated)
- Committer:
- nguyenhoang9x5555
- Date:
- Mon Oct 22 01:56:09 2018 +0000
- Revision:
- 1:c1c6549b9333
- Parent:
- 0:3c869a8cb8f8
hey
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 1 | #include "CmdSendStringOnInterval.h" |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 2 | #include "CommandTerminal.h" |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 3 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 4 | CmdSendStringOnInterval::CmdSendStringOnInterval() |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 5 | : Command("Send On Interval", "AT+SENDI", "Sends supplied packet data on interval between sends, output any received packets (escape sequence: +++)", "(1-2147483647) ms,(string:242)") { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 6 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 7 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 8 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 9 | uint32_t CmdSendStringOnInterval::action(std::vector<std::string> args) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 10 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 11 | int32_t code; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 12 | int interval; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 13 | std::string text; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 14 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 15 | sscanf(args[1].c_str(), "%d", &interval); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 16 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 17 | for (size_t i = 2; i < args.size(); i++) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 18 | text.append(args[i]); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 19 | if (i != args.size() - 1) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 20 | text.append(","); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 21 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 22 | std::vector<uint8_t> data(text.begin(), text.end()); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 23 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 24 | while (true) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 25 | if (CommandTerminal::Dot()->getJoinMode() == mDot::AUTO_OTA) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 26 | Timer timer; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 27 | timer.start(); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 28 | while (!CommandTerminal::Dot()->getNetworkJoinStatus()) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 29 | CommandTerminal::Serial()->writef("Joining network... "); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 30 | if (CommandTerminal::Dot()->joinNetworkOnce() == mDot::MDOT_OK) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 31 | CommandTerminal::Serial()->writef("Network Joined\r\n"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 32 | } else { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 33 | CommandTerminal::Serial()->writef("%s\r\n", CommandTerminal::Dot()->getLastError().c_str()); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 34 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 35 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 36 | if (CommandTerminal::waitForEscape(CommandTerminal::Dot()->getNextTxMs())) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 37 | return 0; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 38 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 39 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 40 | } else if (!CommandTerminal::Dot()->getNetworkJoinStatus()) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 41 | CommandTerminal::setErrorMessage(mDot::getReturnCodeString(mDot::MDOT_NOT_JOINED)); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 42 | return 1; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 43 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 44 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 45 | if ((code = CommandTerminal::Dot()->send(data, CommandTerminal::Dot()->getTxWait())) != mDot::MDOT_OK) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 46 | std::string error = CommandTerminal::Dot()->getLastError(); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 47 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 48 | if (code == mDot::MDOT_INVALID_PARAM) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 49 | CommandTerminal::setErrorMessage(CommandTerminal::Dot()->getLastError());; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 50 | return 1; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 51 | } else { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 52 | CommandTerminal::Serial()->writef("%s\r\n", error.c_str()); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 53 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 54 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 55 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 56 | if (code == mDot::MDOT_OK) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 57 | if (CommandTerminal::waitForEscape(3000, CommandTerminal::Dot(), CommandTerminal::WAIT_SEND)) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 58 | return 0; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 59 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 60 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 61 | if (CommandTerminal::Dot()->getTxWait() || CommandTerminal::Dot()->getAck() > 0) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 62 | std::vector<uint8_t> rx_data; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 63 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 64 | if (CommandTerminal::Dot()->recv(rx_data) == mDot::MDOT_OK) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 65 | if (!rx_data.empty()) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 66 | if (CommandTerminal::Dot()->getRxOutput() == mDot::HEXADECIMAL) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 67 | for (size_t i = 0; i < data.size(); i++) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 68 | CommandTerminal::Serial()->writef("%02x", data[i]); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 69 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 70 | CommandTerminal::Serial()->writef("\r\n"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 71 | } else { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 72 | CommandTerminal::Serial()->writef("%s\r\n", CommandTerminal::formatPacketData(data, CommandTerminal::Dot()->getRxOutput()).c_str()); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 73 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 74 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 75 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 76 | } else if (!CommandTerminal::Dot()->getTxWait()) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 77 | CommandTerminal::Dot()->cancelRxWindow(); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 78 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 79 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 80 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 81 | if (CommandTerminal::waitForEscape(interval)) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 82 | return 0; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 83 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 84 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 85 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 86 | return 0; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 87 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 88 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 89 | bool CmdSendStringOnInterval::verify(std::vector<std::string> args) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 90 | if (args.size() >= 3) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 91 | int interval; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 92 | size_t size = 0; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 93 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 94 | if (sscanf(args[1].c_str(), "%d", &interval) != 1) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 95 | CommandTerminal::setErrorMessage("Invalid argument"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 96 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 97 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 98 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 99 | if (interval < 1 || interval > INT_MAX-1) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 100 | CommandTerminal::setErrorMessage("Invalid interval, expects (1-2147483646) ms"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 101 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 102 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 103 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 104 | for (size_t i = 2; i < args.size() - 1; i++) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 105 | size += args[i].size() + 1; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 106 | if (size > 242) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 107 | CommandTerminal::setErrorMessage("Invalid packet, expects (string:242)"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 108 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 109 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 110 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 111 | return true; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 112 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 113 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 114 | CommandTerminal::setErrorMessage("Invalid arguments"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 115 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 116 | } |