Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed cantcoap WIFI_BOARD
Diff: main.cpp
- Revision:
- 4:1a7fd4f12932
- Parent:
- 3:9074dfb9fc11
- Child:
- 5:aad8eafb8702
--- a/main.cpp Thu Jan 24 23:57:58 2019 +0000
+++ b/main.cpp Fri Jan 25 00:58:36 2019 +0000
@@ -47,8 +47,7 @@
read_finished = 1;
}
-CoapPDU* read(int len) {
- uint8_t rx_buff[len];
+CoapPDU* read(uint8_t* rx_buff, int len) {
int ret;
PCDBG("Reading");
read_finished = 0;
@@ -64,21 +63,21 @@
while(read_finished == 0) {}
t.stop();
PCDBG("Finished Reading, ret = %d, read_finished = %d, time = %d", ret, read_finished, t.read_ms())
- HEXDBG((uint8_t*)rx_buff, len);
+ HEXDBG(rx_buff, len);
if(read_finished != 1) {
wifi.abort_read();
read_finished = 0;
return NULL;
}
read_finished = 0;
- CoapPDU* recvPDU = new CoapPDU((uint8_t*)rx_buff, len);
+ CoapPDU* recvPDU = new CoapPDU(rx_buff, len);
PCDBG("PDU created");
if(recvPDU->validate()) {
PCDBG("Validated");
recvPDU->printHuman();
PCDBG("Code = %d", recvPDU->getCode());
PCDBG("Payload:");
- HEXDBG(recvPDU->getPayloadPointer(), len);
+ HEXDBG(recvPDU->getPayloadPointer(), len - 9);
return recvPDU;
}
return NULL;
@@ -99,11 +98,15 @@
PCDBG("Sending:");
HEXDBG(buff, len);
//For some mysterious reason it doesn't print first character
- int ret = wifi.write(buff - 1, len + 1, event_callback_t(&send_cb));
+ uint8_t buf2[len+1];
+ std::memcpy(buf2+1, buff, len);
+ buf2[0] = 0;
+ int ret = wifi.write(buf2, len + 1, event_callback_t(&send_cb));
+ PCDBG("Sending initiated");
return ret;
}
-CoapPDU* send_or_fail(uint8_t* buff, int len, int ans_len) {
+CoapPDU* send_or_fail(uint8_t* buff, int len, uint8_t* rx_buff, int ans_len) {
int ret;
CoapPDU* ans;
for(int i = 0; i < RETRANSMISSION; ++i) {
@@ -112,11 +115,12 @@
PCDBG("Send Failed");
return NULL;
}
- ans = read(ans_len);
+ ans = read(rx_buff, ans_len);
if(ans == NULL) {
PCDBG("Read Failed");
} else if(ans->getType() != CoapPDU::COAP_ACKNOWLEDGEMENT || ans->getMessageID() != messageID) {
PCDBG("Wrong answer IS: type = %d, id = %d SHOULD BE: type = %d, id = %d", ans->getType(), ans->getMessageID(), CoapPDU::COAP_ACKNOWLEDGEMENT, messageID);
+ delete ans;
} else {
PCDBG("ACK get");
return ans;
@@ -136,13 +140,14 @@
int send_data(int type, int data) {
uint8_t buff[5];
+ uint8_t rx_buff[PUT_ANS_LEN];
Msg::construct_data_msg(type, data, buff, 5);
CoapPDU pdu = CoapPDU();
preparePDU(pdu);
pdu.setCode(CoapPDU::COAP_PUT);
pdu.setURI((char*)"data",4);
pdu.setPayload(buff, 5);
- CoapPDU* ans = send_or_fail(pdu.getPDUPointer(), pdu.getPDULength(), PUT_ANS_LEN);
+ CoapPDU* ans = send_or_fail(pdu.getPDUPointer(), pdu.getPDULength(), rx_buff, PUT_ANS_LEN);
if(ans == NULL) return -1;
delete ans;
return 0;
@@ -150,13 +155,14 @@
int get_config(uint8_t type) {
uint8_t buff[1];
+ uint8_t rx_buff[GET_ANS_LEN];
buff[0] = type;
CoapPDU pdu = CoapPDU();
preparePDU(pdu);
pdu.setCode(CoapPDU::COAP_POST);
pdu.setURI((char*)"conf",4);
pdu.setPayload(buff, 1);
- CoapPDU* ans = send_or_fail(pdu.getPDUPointer(), pdu.getPDULength(), GET_ANS_LEN);
+ CoapPDU* ans = send_or_fail(pdu.getPDUPointer(), pdu.getPDULength(), rx_buff, GET_ANS_LEN);
if(ans == NULL) {
PCDBG("Failed to get config");
return -1;
@@ -164,7 +170,7 @@
uint8_t* config = ans->getPayloadPointer();
int conf = 0;
int a = 1;
- for(int i = 0; i < 4; ++i) {
+ for(int i = 3; i >= 0; --i) {
conf += config[i] * a;
a *= 256;
}