uint8_t Sodaq_RN2483::macTransmit(const char* type, uint8_t port, const uint8_t* payload, uint8_t size) { debugPrintLn("[macTransmit]"); this->loraStream->print(STR_CMD_MAC_TX); this->loraStream->print(type); this->loraStream->print(port); this->loraStream->print(" "); for (int i = 0; i < size; ++i) { this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(payload[i])))); this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(payload[i])))); } this->loraStream->print(CRLF); if (!expectOK()) { return lookupMacTransmitError(this->inputBuffer); // inputBuffer still has the last line read } this->packetReceived = false; // prepare for receiving a new packet debugPrint("Waiting for server response"); unsigned long timeout = millis() + RECEIVE_TIMEOUT; // hard timeout while (millis() < timeout) { sodaq_wdt_reset(); debugPrint("."); if (readLn() > 0) { debugPrintLn(".");debugPrint("(");debugPrint(this->inputBuffer);debugPrintLn(")"); if (strstr(this->inputBuffer, " ") != NULL) // to avoid double delimiter search { // there is a splittable line -only case known is mac_rx debugPrintLn("Splittable response found"); return onMacRX(); } else if (strstr(this->inputBuffer, STR_RESULT_MAC_TX_OK)) { // done debugPrintLn("Received mac_tx_ok"); return NoError; } else { // lookup the error message debugPrintLn("Some other string received (error)"); return lookupMacTransmitError(this->inputBuffer); } } } debugPrintLn("Timed-out waiting for a response!"); return Timeout; }
/** * @brief Sends a a payload and blocks until there is a response back, * or the receive windows have closed or the hard timeout has passed. * @param Transmit type * @param Port to use for transmit * @param Payload buffer * @param Size of payload buffer * @return Returns if sucessfull or if a MAC transmit error. */ uint8_t RN2483::macTransmit(const char* type, uint8_t port, const uint8_t* payload, uint8_t size) { _RN2483.printf(STR_CMD_MAC_TX); _RN2483.printf(type); _RN2483.printf("%u",port); _RN2483.printf(" "); for (int i = 0; i < size; ++i) { _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(payload[i])))); _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(payload[i])))); } _RN2483.printf(CRLF); if (!expectOK()) { return lookupMacTransmitError(this->inputBuffer); // inputBuffer still has the last line read } this->packetReceived = false; // prepare for receiving a new packet Timer t; t.start(); int timeout = t.read_ms() + RECEIVE_TIMEOUT; // hard timeouts while (t.read_ms() < timeout) { if (readLn() > 0) { if (strstr(this->inputBuffer, " ") != NULL) { // to avoid double delimiter search // there is a splittable line -only case known is mac_rx t.stop(); return onMacRX(); } else if (strstr(this->inputBuffer, STR_RESULT_MAC_TX_OK)) { // done t.stop(); return NoError; } else { // lookup the error message t.stop(); return lookupMacTransmitError(this->inputBuffer); } } } t.stop(); return Timedout; }
// Provides a quick test of several methods as a pseudo-unit test. void Sodaq_RN2483::runTestSequence(Stream& stream) { debugPrint("free ram: "); debugPrintLn(freeRam()); init(stream); this->loraStream = &stream; this->diagStream = &stream; // expectString debugPrintLn("write \"testString\" and then CRLF"); if (expectString("testString", 5000)) { debugPrintLn("[expectString] positive case works!"); } debugPrintLn(""); debugPrintLn("write something other than \"testString\" and then CRLF"); if (!expectString("testString", 5000)) { debugPrintLn("[expectString] negative case works!"); } debugPrint("free ram: "); debugPrintLn(freeRam()); // setMacParam(array) debugPrintLn(""); debugPrintLn(""); uint8_t testValue[] = {0x01, 0x02, 0xDE, 0xAD, 0xBE, 0xEF}; setMacParam("testParam ", testValue, ARRAY_SIZE(testValue)); // macTransmit debugPrintLn(""); debugPrintLn(""); uint8_t testValue2[] = {0x01, 0x02, 0xDE, 0xAD, 0xBE, 0xEF}; macTransmit(STR_CONFIRMED, 1, testValue2, ARRAY_SIZE(testValue2)); debugPrint("free ram: "); debugPrintLn(freeRam()); // receive debugPrintLn(""); debugPrintLn("==== receive"); char mockResult[] = "303132333435363738"; memcpy(this->receivedPayloadBuffer, mockResult, strlen(mockResult) + 1); uint8_t payload[64]; debugPrintLn("* without having received packet"); uint8_t length = receive(payload, sizeof(payload)); debugPrintLn(reinterpret_cast<char*>(payload)); debugPrint("Length: "); debugPrintLn(length); debugPrintLn("* having received packet"); this->packetReceived = true; length = receive(payload, sizeof(payload)); debugPrintLn(reinterpret_cast<char*>(payload)); debugPrint("Length: "); debugPrintLn(length); // onMacRX debugPrintLn(""); debugPrintLn("==== onMacRX"); char mockRx[] = "mac_rx 1 303132333435363738"; memcpy(this->inputBuffer, mockRx, strlen(mockRx) + 1); this->packetReceived = false;// reset debugPrint("Input buffer now is: "); debugPrintLn(this->inputBuffer); debugPrint("onMacRX result code: "); debugPrintLn(onMacRX()); uint8_t payload2[64]; if (receive(payload2, sizeof(payload2)) != 9) { debugPrintLn("len is wrong!"); } debugPrintLn(reinterpret_cast<char*>(payload2)); if (receive(payload2, sizeof(payload2), 2) != 7) { debugPrintLn("len is wrong!"); } debugPrintLn(reinterpret_cast<char*>(payload2)); if (receive(payload2, sizeof(payload2), 3) != 6) { debugPrintLn("len is wrong!"); } debugPrintLn(reinterpret_cast<char*>(payload2)); debugPrint("free ram: "); debugPrintLn(freeRam()); // lookup error debugPrintLn(""); debugPrintLn(""); debugPrint("empty string: "); debugPrintLn((lookupMacTransmitError("") == NoResponse) ? "passed" : "wrong"); debugPrint("\"random\": "); debugPrintLn((lookupMacTransmitError("random") == NoResponse) ? "passed" : "wrong"); debugPrint("\"invalid_param\": "); debugPrintLn((lookupMacTransmitError("invalid_param") == InternalError) ? "passed" : "wrong"); debugPrint("\"not_joined\": "); debugPrintLn((lookupMacTransmitError("not_joined") == NotConnected) ? "passed" : "wrong"); debugPrint("\"busy\": "); debugPrintLn((lookupMacTransmitError("busy") == Busy) ? "passed" : "wrong"); debugPrint("\"invalid_param\": "); debugPrintLn((lookupMacTransmitError("invalid_param") == InternalError) ? "passed" : "wrong"); debugPrint("free ram: "); debugPrintLn(freeRam()); }