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
main.cpp@5:aad8eafb8702, 2019-01-25 (annotated)
- Committer:
- tkanas
- Date:
- Fri Jan 25 02:05:17 2019 +0000
- Revision:
- 5:aad8eafb8702
- Parent:
- 4:1a7fd4f12932
- Child:
- 6:0ab34e2cd5dc
dziala1!!!!1!!!11111!!!1!
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tkanas | 0:73e49785c686 | 1 | #include "mbed.h" |
| tkanas | 0:73e49785c686 | 2 | #include "cantcoap.h" |
| tkanas | 2:d2fd4c719ea0 | 3 | #include "msg.h" |
| tkanas | 0:73e49785c686 | 4 | |
| tkanas | 0:73e49785c686 | 5 | Serial pc(SERIAL_TX, SERIAL_RX); |
| tkanas | 0:73e49785c686 | 6 | Serial wifi(PA_11, PA_12); |
| tkanas | 0:73e49785c686 | 7 | DigitalOut send(PC_8); |
| tkanas | 0:73e49785c686 | 8 | |
| tkanas | 0:73e49785c686 | 9 | #define PCDBG(...) if(1){ pc.printf(__VA_ARGS__); pc.printf("\r\n");} |
| tkanas | 2:d2fd4c719ea0 | 10 | #define BINDBG(buff, len) if(1){ print_bin(buff, len); } |
| tkanas | 2:d2fd4c719ea0 | 11 | #define HEXDBG(buff, len) if(1){ print_hex(buff, len);} |
| tkanas | 0:73e49785c686 | 12 | |
| tkanas | 2:d2fd4c719ea0 | 13 | //Number of retransmissions |
| tkanas | 2:d2fd4c719ea0 | 14 | #define RETRANSMISSION 10 |
| tkanas | 2:d2fd4c719ea0 | 15 | //Timeout in ms |
| tkanas | 2:d2fd4c719ea0 | 16 | #define TIMEOUT 2000 |
| tkanas | 2:d2fd4c719ea0 | 17 | |
| tkanas | 2:d2fd4c719ea0 | 18 | //Length of PUT (data) response |
| tkanas | 5:aad8eafb8702 | 19 | #define PUT_ANS_LEN 5 |
| tkanas | 2:d2fd4c719ea0 | 20 | //Length of GET (conf) response |
| tkanas | 2:d2fd4c719ea0 | 21 | #define GET_ANS_LEN 10 |
| tkanas | 2:d2fd4c719ea0 | 22 | |
| tkanas | 2:d2fd4c719ea0 | 23 | int read_finished = 0; |
| tkanas | 2:d2fd4c719ea0 | 24 | uint16_t messageID = 1; |
| tkanas | 2:d2fd4c719ea0 | 25 | |
| tkanas | 2:d2fd4c719ea0 | 26 | void print_bin(uint8_t* buff, int len) { |
| tkanas | 2:d2fd4c719ea0 | 27 | char str[len]; |
| tkanas | 2:d2fd4c719ea0 | 28 | for(int i = 0; i < len; ++i) { |
| tkanas | 2:d2fd4c719ea0 | 29 | if((buff[i] >= 'a' && buff[i] <= 'z') || (buff[i] >= 'A' && buff[i] <= 'Z') || (buff[i] >= '0'&& buff[i] <= '9')) { |
| tkanas | 2:d2fd4c719ea0 | 30 | str[i] = buff[i]; |
| tkanas | 2:d2fd4c719ea0 | 31 | } else { |
| tkanas | 2:d2fd4c719ea0 | 32 | str[i] = '.'; |
| tkanas | 2:d2fd4c719ea0 | 33 | } |
| tkanas | 2:d2fd4c719ea0 | 34 | } |
| tkanas | 2:d2fd4c719ea0 | 35 | PCDBG("%s", str); |
| tkanas | 2:d2fd4c719ea0 | 36 | } |
| tkanas | 2:d2fd4c719ea0 | 37 | |
| tkanas | 2:d2fd4c719ea0 | 38 | void print_hex(uint8_t* buff, int len) { |
| tkanas | 2:d2fd4c719ea0 | 39 | for(int i = 0; i < len; ++i) { |
| tkanas | 2:d2fd4c719ea0 | 40 | pc.printf("%X ", buff[i]); |
| tkanas | 2:d2fd4c719ea0 | 41 | } |
| tkanas | 2:d2fd4c719ea0 | 42 | pc.printf("\r\n"); |
| tkanas | 2:d2fd4c719ea0 | 43 | } |
| tkanas | 2:d2fd4c719ea0 | 44 | |
| tkanas | 2:d2fd4c719ea0 | 45 | void read_cb(int arg) { |
| tkanas | 2:d2fd4c719ea0 | 46 | (void)arg; |
| tkanas | 2:d2fd4c719ea0 | 47 | read_finished = 1; |
| tkanas | 2:d2fd4c719ea0 | 48 | } |
| tkanas | 2:d2fd4c719ea0 | 49 | |
| tkanas | 4:1a7fd4f12932 | 50 | CoapPDU* read(uint8_t* rx_buff, int len) { |
| tkanas | 3:9074dfb9fc11 | 51 | int ret; |
| tkanas | 2:d2fd4c719ea0 | 52 | PCDBG("Reading"); |
| tkanas | 3:9074dfb9fc11 | 53 | read_finished = 0; |
| tkanas | 2:d2fd4c719ea0 | 54 | Timer t; |
| tkanas | 2:d2fd4c719ea0 | 55 | t.start(); |
| tkanas | 2:d2fd4c719ea0 | 56 | while(!wifi.readable() && t.read_ms() < TIMEOUT) {} |
| tkanas | 3:9074dfb9fc11 | 57 | if(wifi.readable()) |
| tkanas | 5:aad8eafb8702 | 58 | //ret = wifi.read(rx_buff, len, event_callback_t(&read_cb)); |
| tkanas | 5:aad8eafb8702 | 59 | wifi.gets((char*)rx_buff, len + 1); |
| tkanas | 5:aad8eafb8702 | 60 | else { |
| tkanas | 5:aad8eafb8702 | 61 | t.stop(); |
| tkanas | 3:9074dfb9fc11 | 62 | return NULL; |
| tkanas | 5:aad8eafb8702 | 63 | } |
| tkanas | 3:9074dfb9fc11 | 64 | PCDBG("Started reading"); |
| tkanas | 5:aad8eafb8702 | 65 | //t.reset(); |
| tkanas | 5:aad8eafb8702 | 66 | //t.start(); |
| tkanas | 3:9074dfb9fc11 | 67 | //while(read_finished == 0 && t.read_ms() < TIMEOUT) {} |
| tkanas | 5:aad8eafb8702 | 68 | //while(read_finished == 0) {} |
| tkanas | 2:d2fd4c719ea0 | 69 | t.stop(); |
| tkanas | 2:d2fd4c719ea0 | 70 | PCDBG("Finished Reading, ret = %d, read_finished = %d, time = %d", ret, read_finished, t.read_ms()) |
| tkanas | 4:1a7fd4f12932 | 71 | HEXDBG(rx_buff, len); |
| tkanas | 5:aad8eafb8702 | 72 | //if(read_finished != 1) { |
| tkanas | 5:aad8eafb8702 | 73 | // wifi.abort_read(); |
| tkanas | 5:aad8eafb8702 | 74 | // read_finished = 0; |
| tkanas | 5:aad8eafb8702 | 75 | // return NULL; |
| tkanas | 5:aad8eafb8702 | 76 | // } |
| tkanas | 2:d2fd4c719ea0 | 77 | read_finished = 0; |
| tkanas | 4:1a7fd4f12932 | 78 | CoapPDU* recvPDU = new CoapPDU(rx_buff, len); |
| tkanas | 2:d2fd4c719ea0 | 79 | PCDBG("PDU created"); |
| tkanas | 0:73e49785c686 | 80 | if(recvPDU->validate()) { |
| tkanas | 2:d2fd4c719ea0 | 81 | PCDBG("Validated"); |
| tkanas | 2:d2fd4c719ea0 | 82 | recvPDU->printHuman(); |
| tkanas | 2:d2fd4c719ea0 | 83 | PCDBG("Code = %d", recvPDU->getCode()); |
| tkanas | 2:d2fd4c719ea0 | 84 | PCDBG("Payload:"); |
| tkanas | 5:aad8eafb8702 | 85 | HEXDBG(recvPDU->getPayloadPointer(), len - 6); |
| tkanas | 0:73e49785c686 | 86 | return recvPDU; |
| tkanas | 0:73e49785c686 | 87 | } |
| tkanas | 0:73e49785c686 | 88 | return NULL; |
| tkanas | 0:73e49785c686 | 89 | } |
| tkanas | 0:73e49785c686 | 90 | |
| tkanas | 1:1b58077320a3 | 91 | void send_cb(int arg) { |
| tkanas | 1:1b58077320a3 | 92 | (void)arg; |
| tkanas | 1:1b58077320a3 | 93 | send.write(1); |
| tkanas | 1:1b58077320a3 | 94 | wait(0.05); |
| tkanas | 1:1b58077320a3 | 95 | send.write(0); |
| tkanas | 1:1b58077320a3 | 96 | } |
| tkanas | 1:1b58077320a3 | 97 | |
| tkanas | 0:73e49785c686 | 98 | int sendd(uint8_t* buff, int len) { |
| tkanas | 1:1b58077320a3 | 99 | while(!wifi.writable()) { |
| tkanas | 2:d2fd4c719ea0 | 100 | //PCDBG("Wifi not wirtable"); |
| tkanas | 2:d2fd4c719ea0 | 101 | //wait(0.5); |
| tkanas | 2:d2fd4c719ea0 | 102 | } |
| tkanas | 2:d2fd4c719ea0 | 103 | PCDBG("Sending:"); |
| tkanas | 2:d2fd4c719ea0 | 104 | HEXDBG(buff, len); |
| tkanas | 2:d2fd4c719ea0 | 105 | //For some mysterious reason it doesn't print first character |
| tkanas | 4:1a7fd4f12932 | 106 | uint8_t buf2[len+1]; |
| tkanas | 4:1a7fd4f12932 | 107 | std::memcpy(buf2+1, buff, len); |
| tkanas | 4:1a7fd4f12932 | 108 | buf2[0] = 0; |
| tkanas | 4:1a7fd4f12932 | 109 | int ret = wifi.write(buf2, len + 1, event_callback_t(&send_cb)); |
| tkanas | 4:1a7fd4f12932 | 110 | PCDBG("Sending initiated"); |
| tkanas | 2:d2fd4c719ea0 | 111 | return ret; |
| tkanas | 2:d2fd4c719ea0 | 112 | } |
| tkanas | 2:d2fd4c719ea0 | 113 | |
| tkanas | 4:1a7fd4f12932 | 114 | CoapPDU* send_or_fail(uint8_t* buff, int len, uint8_t* rx_buff, int ans_len) { |
| tkanas | 2:d2fd4c719ea0 | 115 | int ret; |
| tkanas | 2:d2fd4c719ea0 | 116 | CoapPDU* ans; |
| tkanas | 2:d2fd4c719ea0 | 117 | for(int i = 0; i < RETRANSMISSION; ++i) { |
| tkanas | 2:d2fd4c719ea0 | 118 | ret = sendd(buff, len); |
| tkanas | 2:d2fd4c719ea0 | 119 | if(ret < 0) { |
| tkanas | 2:d2fd4c719ea0 | 120 | PCDBG("Send Failed"); |
| tkanas | 2:d2fd4c719ea0 | 121 | return NULL; |
| tkanas | 2:d2fd4c719ea0 | 122 | } |
| tkanas | 4:1a7fd4f12932 | 123 | ans = read(rx_buff, ans_len); |
| tkanas | 2:d2fd4c719ea0 | 124 | if(ans == NULL) { |
| tkanas | 2:d2fd4c719ea0 | 125 | PCDBG("Read Failed"); |
| tkanas | 2:d2fd4c719ea0 | 126 | } else if(ans->getType() != CoapPDU::COAP_ACKNOWLEDGEMENT || ans->getMessageID() != messageID) { |
| tkanas | 2:d2fd4c719ea0 | 127 | PCDBG("Wrong answer IS: type = %d, id = %d SHOULD BE: type = %d, id = %d", ans->getType(), ans->getMessageID(), CoapPDU::COAP_ACKNOWLEDGEMENT, messageID); |
| tkanas | 4:1a7fd4f12932 | 128 | delete ans; |
| tkanas | 2:d2fd4c719ea0 | 129 | } else { |
| tkanas | 2:d2fd4c719ea0 | 130 | PCDBG("ACK get"); |
| tkanas | 2:d2fd4c719ea0 | 131 | return ans; |
| tkanas | 2:d2fd4c719ea0 | 132 | } |
| tkanas | 1:1b58077320a3 | 133 | } |
| tkanas | 2:d2fd4c719ea0 | 134 | PCDBG("Retransmission limit reached"); |
| tkanas | 2:d2fd4c719ea0 | 135 | return NULL; |
| tkanas | 2:d2fd4c719ea0 | 136 | } |
| tkanas | 2:d2fd4c719ea0 | 137 | |
| tkanas | 2:d2fd4c719ea0 | 138 | |
| tkanas | 2:d2fd4c719ea0 | 139 | void preparePDU(CoapPDU& coapPDU) { |
| tkanas | 2:d2fd4c719ea0 | 140 | coapPDU.setVersion(1); |
| tkanas | 2:d2fd4c719ea0 | 141 | coapPDU.setType(CoapPDU::COAP_CONFIRMABLE); |
| tkanas | 2:d2fd4c719ea0 | 142 | coapPDU.setMessageID(++messageID); |
| tkanas | 2:d2fd4c719ea0 | 143 | coapPDU.setToken((uint8_t*)"A", 1); |
| tkanas | 2:d2fd4c719ea0 | 144 | } |
| tkanas | 2:d2fd4c719ea0 | 145 | |
| tkanas | 2:d2fd4c719ea0 | 146 | int send_data(int type, int data) { |
| tkanas | 2:d2fd4c719ea0 | 147 | uint8_t buff[5]; |
| tkanas | 4:1a7fd4f12932 | 148 | uint8_t rx_buff[PUT_ANS_LEN]; |
| tkanas | 2:d2fd4c719ea0 | 149 | Msg::construct_data_msg(type, data, buff, 5); |
| tkanas | 2:d2fd4c719ea0 | 150 | CoapPDU pdu = CoapPDU(); |
| tkanas | 2:d2fd4c719ea0 | 151 | preparePDU(pdu); |
| tkanas | 2:d2fd4c719ea0 | 152 | pdu.setCode(CoapPDU::COAP_PUT); |
| tkanas | 2:d2fd4c719ea0 | 153 | pdu.setURI((char*)"data",4); |
| tkanas | 2:d2fd4c719ea0 | 154 | pdu.setPayload(buff, 5); |
| tkanas | 4:1a7fd4f12932 | 155 | CoapPDU* ans = send_or_fail(pdu.getPDUPointer(), pdu.getPDULength(), rx_buff, PUT_ANS_LEN); |
| tkanas | 2:d2fd4c719ea0 | 156 | if(ans == NULL) return -1; |
| tkanas | 2:d2fd4c719ea0 | 157 | delete ans; |
| tkanas | 2:d2fd4c719ea0 | 158 | return 0; |
| tkanas | 2:d2fd4c719ea0 | 159 | } |
| tkanas | 2:d2fd4c719ea0 | 160 | |
| tkanas | 2:d2fd4c719ea0 | 161 | int get_config(uint8_t type) { |
| tkanas | 2:d2fd4c719ea0 | 162 | uint8_t buff[1]; |
| tkanas | 4:1a7fd4f12932 | 163 | uint8_t rx_buff[GET_ANS_LEN]; |
| tkanas | 5:aad8eafb8702 | 164 | PCDBG("Gonna get the conf for type %d", type); |
| tkanas | 2:d2fd4c719ea0 | 165 | buff[0] = type; |
| tkanas | 2:d2fd4c719ea0 | 166 | CoapPDU pdu = CoapPDU(); |
| tkanas | 2:d2fd4c719ea0 | 167 | preparePDU(pdu); |
| tkanas | 2:d2fd4c719ea0 | 168 | pdu.setCode(CoapPDU::COAP_POST); |
| tkanas | 3:9074dfb9fc11 | 169 | pdu.setURI((char*)"conf",4); |
| tkanas | 2:d2fd4c719ea0 | 170 | pdu.setPayload(buff, 1); |
| tkanas | 4:1a7fd4f12932 | 171 | CoapPDU* ans = send_or_fail(pdu.getPDUPointer(), pdu.getPDULength(), rx_buff, GET_ANS_LEN); |
| tkanas | 2:d2fd4c719ea0 | 172 | if(ans == NULL) { |
| tkanas | 2:d2fd4c719ea0 | 173 | PCDBG("Failed to get config"); |
| tkanas | 2:d2fd4c719ea0 | 174 | return -1; |
| tkanas | 2:d2fd4c719ea0 | 175 | } |
| tkanas | 2:d2fd4c719ea0 | 176 | uint8_t* config = ans->getPayloadPointer(); |
| tkanas | 2:d2fd4c719ea0 | 177 | int conf = 0; |
| tkanas | 2:d2fd4c719ea0 | 178 | int a = 1; |
| tkanas | 4:1a7fd4f12932 | 179 | for(int i = 3; i >= 0; --i) { |
| tkanas | 2:d2fd4c719ea0 | 180 | conf += config[i] * a; |
| tkanas | 2:d2fd4c719ea0 | 181 | a *= 256; |
| tkanas | 2:d2fd4c719ea0 | 182 | } |
| tkanas | 2:d2fd4c719ea0 | 183 | delete ans; |
| tkanas | 2:d2fd4c719ea0 | 184 | PCDBG("New config for type %d is %d", type, conf); |
| tkanas | 2:d2fd4c719ea0 | 185 | return conf; |
| tkanas | 0:73e49785c686 | 186 | } |
| tkanas | 0:73e49785c686 | 187 | |
| tkanas | 0:73e49785c686 | 188 | int main() { |
| tkanas | 0:73e49785c686 | 189 | pc.baud(115200); |
| tkanas | 0:73e49785c686 | 190 | wifi.baud(115200); |
| tkanas | 1:1b58077320a3 | 191 | PCDBG("Started") |
| tkanas | 0:73e49785c686 | 192 | CoapPDU* pdu = new CoapPDU(); |
| tkanas | 2:d2fd4c719ea0 | 193 | //int ret; |
| tkanas | 2:d2fd4c719ea0 | 194 | int data = 1; |
| tkanas | 0:73e49785c686 | 195 | while(1) { |
| tkanas | 5:aad8eafb8702 | 196 | send_data(1, ++data); |
| tkanas | 5:aad8eafb8702 | 197 | wait(5); |
| tkanas | 2:d2fd4c719ea0 | 198 | get_config(1); |
| tkanas | 2:d2fd4c719ea0 | 199 | wait(5); |
| tkanas | 0:73e49785c686 | 200 | } |
| tkanas | 0:73e49785c686 | 201 | } |