CoAPConnect / coap-pubsub-rust

MIT License
5 stars 0 forks source link

Informing subscribers results in malformed CoAP packets #74

Open Kavan-M opened 2 months ago

Kavan-M commented 2 months ago

The inform_subscribers function doesn't send out packets recognisable as coap-packets, you can test this with Wireshark for example. The packets sent are UDP, with the payload of the CoAP packet set as the data of the UDP packet.

Kavan-M commented 2 months ago

When attempting to use the aiocoap client to subscribe to your broker, we get a "not a server" response from the client when the broker sends update packages to the subscriber. When comparing packets for a response to an observe, the code field should have 45 in hex, corresponding to 2.05. Our broker generates a packet with a 01 code, i.e. a GET request.

Kavan-M commented 2 months ago

Troubleshooted this issue a ltitle further, the correct header can be set if we dont use CoapResponse but rather just form a packet and set the header manually there, for example

`
fn generate_random_token() -> Vec { let mut rng = rand::threadrng(); (0..4).map(|| rng.gen()).collect() }

let mut packet = coap_lite::Packet::new();
packet.header.set_version(1);
packet.header.set_type(coap_lite::MessageType::NonConfirmable);
packet.header.set_code("2.05");
let token = generate_random_token();
packet.set_token(token.clone());
packet.header.set_token_length(token.len() as u8);
packet.payload = resource.as_bytes().to_vec();
packet.set_content_format(coap_lite::ContentFormat::try_from(110).unwrap());
packet.add_option(coap_lite::CoapOption::Observe, 10002_i32.to_be_bytes().to_vec());
`

Some issues that are possible is that the CoapOption::Observe is incorrect here, and the contentformat should be chosen from the contentformat enum rather. Wireshark still doesnt accept this as a valid coap packet so there are some issues left which is also why i rather didnt commit.