firm newest
Dependencies: MTS-Serial libmDot-dev-mbed5-deprecated
CommandTerminal/CmdSendString.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 "CmdSendString.h" |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 2 | #include "CommandTerminal.h" |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 3 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 4 | CmdSendString::CmdSendString() |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 5 | : Command("Send Once", "AT+SEND", "Sends supplied packet data one time and return response, (max:242 bytes)", "(string:242)") { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 6 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 7 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 8 | uint32_t CmdSendString::action(std::vector<std::string> args) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 9 | // Argument had been split on each comma, rebuild payload |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 10 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 11 | std::string text; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 12 | for (size_t i = 1; i < args.size(); i++) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 13 | text.append(args[i]); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 14 | if (i != args.size() - 1) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 15 | text.append(","); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 16 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 17 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 18 | std::vector<uint8_t> data(text.begin(), text.end()); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 19 | if (CommandTerminal::Dot()->send(data, CommandTerminal::Dot()->getTxWait()) != mDot::MDOT_OK) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 20 | CommandTerminal::setErrorMessage(CommandTerminal::Dot()->getLastError()); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 21 | return 1; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 22 | } else if (CommandTerminal::Dot()->getTxWait()) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 23 | data.clear(); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 24 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 25 | if (CommandTerminal::Dot()->recv(data) == mDot::MDOT_OK) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 26 | if (!data.empty()) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 27 | if (CommandTerminal::Dot()->getRxOutput() == mDot::HEXADECIMAL) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 28 | for (size_t i = 0; i < data.size(); i++) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 29 | CommandTerminal::Serial()->writef("%02x", data[i]); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 30 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 31 | CommandTerminal::Serial()->writef("\r\n"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 32 | } else { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 33 | CommandTerminal::Serial()->writef("%s\r\n", CommandTerminal::formatPacketData(data, CommandTerminal::Dot()->getRxOutput()).c_str()); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 34 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 35 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 36 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 37 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 38 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 39 | return 0; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 40 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 41 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 42 | bool CmdSendString::verify(std::vector<std::string> args) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 43 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 44 | if (args.size() == 1) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 45 | // allow sending empty packet to retrieve downstream messages |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 46 | return true; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 47 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 48 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 49 | if (args.size() >= 2) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 50 | size_t size = 0; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 51 | for (size_t i = 1; i < args.size() - 1; i++) |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 52 | size += args[i].size() + 1; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 53 | if (size > 242) { |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 54 | CommandTerminal::setErrorMessage("Invalid packet, expects (string:242)"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 55 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 56 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 57 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 58 | return true; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 59 | } |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 60 | |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 61 | CommandTerminal::setErrorMessage("Invalid arguments"); |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 62 | return false; |
nguyenhoang9x5555 | 0:3c869a8cb8f8 | 63 | } |