client version of coap
Dependencies: nRF24L01P cantcoap3
Diff: coapClient.cpp
- Revision:
- 4:792cf53a73c4
- Parent:
- 2:e8823d9fa162
- Child:
- 5:d633e12f113f
--- a/coapClient.cpp Thu Jan 24 16:38:47 2019 +0000 +++ b/coapClient.cpp Fri Jan 25 02:00:42 2019 +0000 @@ -20,6 +20,7 @@ bool CoapClient::isAck(CoapPDU& response, uint16_t message_id, char* uri) { if (response.validate()) { + response.printHuman(); if (isExpectedResponse(response, message_id, uri)) { return true; } @@ -47,40 +48,49 @@ * -4 if if radio is not functioning properly * */ int CoapClient::get(uint8_t* buffer, int len, char* uri) { + Timer t; CoapPDU coapPDU = CoapPDU(); uint16_t messageId = preparePDU(coapPDU); coapPDU.setCode(CoapPDU::COAP_GET); coapPDU.setURI(uri); uint8_t returnBuffer[radioWrapper.packetSize()]; - int timeout = this->listeningTimeout; + int TIMEOUT = this->listeningTimeout; + int timeout = TIMEOUT; for (int i = 0; i < this->retransmissionLimit; i++) { - int ret = radioWrapper.write(coapPDU.getPDUPointer(), coapPDU.getPDULength()); // send empty payload to get uri - - if (ret < 0) { - return RADIO_NOT_WORKING; + while (timeout > 0) { + int ret = radioWrapper.write(coapPDU.getPDUPointer(), coapPDU.getPDULength()); // send empty payload to get uri + if (ret < 0) { + return RADIO_NOT_WORKING; + } + t.start(); + int readLen = radioWrapper.read(returnBuffer, radioWrapper.packetSize(), timeout); // add timeout param + t.stop(); + timeout -= t.read_ms(); + t.reset(); + if (readLen > 0) { // if something is recieved process it + CoapPDU response(returnBuffer, len, readLen); + if (isAck(response, messageId, uri) && hasGetResponse(response)) { + DBG("RESPONSE: %d", readLen); + response.printHuman(); + if (response.getPayloadLength() <= len) { + std::memcpy(buffer, response.getPayloadPointer(), + static_cast<size_t>(response.getPayloadLength())); + return response.getPayloadLength(); + } else { + return SMALL_BUFFER; + } + } else { + DBG("NIEMOJE"); + DBG("PAYLOAD: %s", response.getPayloadPointer()); + continue; + } + } else if (readLen < 0) { + return RADIO_NOT_WORKING; + } } - - int readLen = radioWrapper.read(returnBuffer, radioWrapper.packetSize(), timeout); // add timeout param - if (readLen > 0) { // if something is recieved process it - CoapPDU response(returnBuffer, len, readLen); - if (isAck(response, messageId, uri) && hasGetResponse(response)) { - DBG("RESPONSE: %d", readLen); - response.printHuman(); - if (response.getPayloadLength() <= len) { - std::memcpy(buffer, response.getPayloadPointer(), - static_cast<size_t>(response.getPayloadLength())); - return response.getPayloadLength(); - } else { - return SMALL_BUFFER; - } - } else { - return SERVER_RESPONSE_AMBIGOUS; - } - } else if (readLen < 0) { - return RADIO_NOT_WORKING; - } - timeout *= 2; + TIMEOUT *= 2; + timeout = TIMEOUT; } return @@ -95,6 +105,7 @@ * -4 if radio is not working properly * */ int CoapClient::post(uint8_t* buffer, int len, char* uri) { + Timer t; CoapPDU coapPDU = CoapPDU(); uint16_t messageId = preparePDU(coapPDU); coapPDU.setCode(CoapPDU::COAP_POST); @@ -102,25 +113,34 @@ coapPDU.setURI(uri); coapPDU.setPayload(buffer, len); uint8_t returnBuffer[radioWrapper.packetSize()]; - int timeout = this->listeningTimeout; + int TIMEOUT = this->listeningTimeout; + int timeout = TIMEOUT; for (int i = 0; i < this->retransmissionLimit; i++) { - - int ret = radioWrapper.write(coapPDU.getPDUPointer(), coapPDU.getPDULength()); - if (ret < 0) { - return RADIO_NOT_WORKING; + while(timeout > 0) { + int ret = radioWrapper.write(coapPDU.getPDUPointer(), coapPDU.getPDULength()); + if (ret < 0) { + return RADIO_NOT_WORKING; + } + t.start(); + int readLen = radioWrapper.read(returnBuffer, radioWrapper.packetSize(), timeout); // add timeout param + t.stop(); + timeout -= t.read_ms(); + t.reset(); + if (readLen > 0) { // if something is recieved process it + CoapPDU response(returnBuffer, len, readLen); + if (isAck(response, messageId, uri) && hasPostResponse(response)) { + return 0; + } else { // if server hasn't responded properly + DBG("NIEMOJE"); + DBG("PAYLOAD: %s", response.getPayloadPointer()); + continue; + } + } else if (readLen < 0) { + return RADIO_NOT_WORKING; + } } - int readLen = radioWrapper.read(returnBuffer, radioWrapper.packetSize(), timeout); // add timeout param - if (readLen > 0) { // if something is recieved process it - CoapPDU response(returnBuffer, len, readLen); - if (isAck(response, messageId, uri) && hasPostResponse(response)) { - return 0; - } else { // if server hasn't responded properly - return SERVER_RESPONSE_AMBIGOUS; - } - } else if (readLen < 0) { - return RADIO_NOT_WORKING; - } - timeout *= 2; + TIMEOUT *= 2; + timeout = TIMEOUT; } return SERVER_TIMED_OUT; } @@ -138,6 +158,5 @@ listeningTimeout(timeout), retransmissionLimit(retransmissionLimit), radioWrapper(channel, rx_address, tx_address) { std::memcpy(this->token, token, 1); - this->message_counter = 16; // set to random -} - + this->message_counter = 666; // set to random +} \ No newline at end of file