mDot AT-Firmware for testing

Dependencies:   MTS-Serial libmDot-mbed5

Fork of Dot-AT-Firmware by MultiTech

CommandTerminal/CmdSendString.cpp

Committer:
Mike Fiore
Date:
2016-04-04
Revision:
9:ff62b20f7000
Parent:
4:666017851052
Child:
14:f9a77400b622

File content as of revision 9:ff62b20f7000:

#include "CmdSendString.h"
#include "CommandTerminal.h"

CmdSendString::CmdSendString(mDot* dot, mts::MTSSerial& serial)
:
  Command(dot, "Send Once", "AT+SEND", "Sends supplied packet data one time and return response, (max:242 bytes)"),
  _serial(serial) {
    _help = std::string(text()) + ": " + std::string(desc());
    _usage = "(string:242)";
}

uint32_t CmdSendString::action(std::vector<std::string> args) {
    // Argument had been split on each comma, rebuild payload
    int32_t code;
    std::string text;
    for (size_t i = 1; i < args.size(); i++) {
        text.append(args[i]);
        if (i != args.size() - 1)
            text.append(",");
    }

    std::vector<uint8_t> data(text.begin(), text.end());
    if ((code = _dot->send(data, _dot->getTxWait())) != mDot::MDOT_OK) {
        setErrorMessage(_dot->getLastError());
        return 1;
    } else if (_dot->getTxWait()) {
        data.clear();

        if (_dot->recv(data) == mDot::MDOT_OK) {
            if (!data.empty()) {
                if (_dot->getVerbose())
                    _serial.writef("Packet data:\r\n");
                _serial.writef("%s\r\n", CommandTerminal::formatPacketData(data, _dot->getRxOutput()).c_str());
            }
        }
    }

    return 0;
}

bool CmdSendString::verify(std::vector<std::string> args) {

    if (args.size() == 1) {
        // allow sending empty packet to retrieve downstream messages
        return true;
    }

    if (args.size() >= 2) {
        size_t size = 0;
        for (size_t i = 1; i < args.size() - 1; i++)
            size += args[i].size() + 1;
        if (size > 242) {
            setErrorMessage("Invalid packet, expects (string:242)");
            return false;
        }

        return true;
    }

    setErrorMessage("Invalid arguments");
    return false;
}