Skip to content

Commit

Permalink
fixup! fix: apply code review suggestions from #15
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsastrix committed Jul 2, 2024
1 parent 872a53c commit 4241b97
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 52 deletions.
23 changes: 14 additions & 9 deletions libcoap/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use libcoap_sys::{
pub use request::CoapRequest;
pub use response::CoapResponse;

use crate::types::{decode_var_len_u16, decode_var_len_u32, decode_var_len_u8, encode_var_len_u16, encode_var_len_u32, encode_var_len_u8};
use crate::{
error::{MessageConversionError, OptionValueError},
protocol::{
Expand All @@ -39,7 +38,11 @@ use crate::{
session::CoapSessionCommon,
types::CoapMessageId,
};
use crate::protocol::{Echo, OsCore, RequestTag};
use crate::protocol::{Echo, Oscore, RequestTag};
use crate::types::{
decode_var_len_u16, decode_var_len_u32, decode_var_len_u8, encode_var_len_u16, encode_var_len_u32,
encode_var_len_u8,
};

pub mod request;
pub mod response;
Expand Down Expand Up @@ -72,7 +75,7 @@ pub enum CoapOption {
MaxAge(MaxAge),
Observe(Observe),
Other(CoapOptionNum, Box<[u8]>),
OsCore(OsCore),
Oscore(Oscore),
Echo(Echo),
RTag(RequestTag),
QBlock1(Block),
Expand Down Expand Up @@ -126,13 +129,15 @@ impl CoapOption {
CoapOptionType::Block1 => Ok(CoapOption::Block1(decode_var_len_u32(value.as_slice()))),
CoapOptionType::Block2 => Ok(CoapOption::Block2(decode_var_len_u32(value.as_slice()))),
CoapOptionType::HopLimit => Ok(CoapOption::HopLimit(decode_var_len_u16(value.as_slice()))),
CoapOptionType::NoResponse => Ok(CoapOption::NoResponse(decode_var_len_u8(value.as_slice()) as NoResponse)),
CoapOptionType::NoResponse => {
Ok(CoapOption::NoResponse(decode_var_len_u8(value.as_slice()) as NoResponse))
},
CoapOptionType::Observe => Ok(CoapOption::Observe(decode_var_len_u32(value.as_slice()))),
CoapOptionType::OsCore => Ok(CoapOption::OsCore(value.into_boxed_slice())),
CoapOptionType::Echo => Ok(CoapOption::Echo(value.into_boxed_slice())),
CoapOptionType::Oscore => Ok(CoapOption::Oscore(value.into_boxed_slice())),
CoapOptionType::Echo => Ok(CoapOption::Echo(value.into_boxed_slice())),
CoapOptionType::RTag => Ok(CoapOption::RTag(value.into_boxed_slice())),
CoapOptionType::QBlock1 => Ok(CoapOption::QBlock1(decode_var_len_u32(value.as_slice()))),
CoapOptionType::QBlock2 => Ok(CoapOption::QBlock2(decode_var_len_u32(value.as_slice())))
CoapOptionType::QBlock2 => Ok(CoapOption::QBlock2(decode_var_len_u32(value.as_slice()))),
}
},
_ => Ok(CoapOption::Other(number, value.into_boxed_slice())),
Expand Down Expand Up @@ -163,7 +168,7 @@ impl CoapOption {
CoapOption::ETag(_) => CoapOptionType::ETag as u16,
CoapOption::MaxAge(_) => CoapOptionType::MaxAge as u16,
CoapOption::Observe(_) => CoapOptionType::Observe as u16,
CoapOption::OsCore(_) => CoapOptionType::OsCore as u16,
CoapOption::Oscore(_) => CoapOptionType::Oscore as u16,
CoapOption::Echo(_) => CoapOptionType::Echo as u16,
CoapOption::RTag(_) => CoapOptionType::RTag as u16,
CoapOption::QBlock1(_) => CoapOptionType::QBlock1 as u16,
Expand Down Expand Up @@ -201,7 +206,7 @@ impl CoapOption {
CoapOption::MaxAge(value) => encode_var_len_u32(value),
CoapOption::Observe(value) => encode_var_len_u32(value),
CoapOption::Other(_num, data) => data,
CoapOption::OsCore(value) => value,
CoapOption::Oscore(value) => value,
CoapOption::Echo(value) => value,
CoapOption::RTag(value) => value,
CoapOption::QBlock1(value) => encode_var_len_u32(value),
Expand Down
9 changes: 4 additions & 5 deletions libcoap/src/message/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ impl CoapRequest {
));
},
// libcoap handles blockwise transfer for us (for now).
CoapOption::QBlock1(_) => {}
CoapOption::QBlock2(_) => {}
CoapOption::QBlock1(_) => {},
CoapOption::QBlock2(_) => {},
CoapOption::HopLimit(value) => {
if hop_limit.is_some() {
return Err(MessageConversionError::NonRepeatableOptionRepeated(
Expand Down Expand Up @@ -508,14 +508,13 @@ impl CoapRequest {
observe = Some(*value);
},
// Handling of echo options is automatically done by libcoap (see man coap_send)
CoapOption::Echo(_) => {
},
CoapOption::Echo(_) => {},
// Handling of request tag options is automatically done by libcoap (see man
// coap_send)
CoapOption::RTag(_) => {},
// OSCORE is currently not supported, and even if it should probably be handled by
// libcoap, so I'm unsure whether we have to expose this.
CoapOption::OsCore(v) => {},
CoapOption::Oscore(v) => {},
// TODO maybe we can save some copies here if we use into_iter for the options instead.
CoapOption::Other(n, v) => {
additional_opts.push(CoapOption::Other(*n, v.clone()));
Expand Down
14 changes: 8 additions & 6 deletions libcoap/src/message/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
* See the README as well as the LICENSE file for more information.
*/

use std::fmt::Display;
use std::fmt::Formatter;

use crate::error::{MessageConversionError, MessageTypeError, OptionValueError};
use crate::message::{CoapMessage, CoapMessageCommon, CoapOption};
use crate::protocol::{CoapMessageCode, CoapMessageType, CoapOptionType, CoapResponseCode, ContentFormat, Echo, ETag, MaxAge, Observe};
use crate::protocol::{
CoapMessageCode, CoapMessageType, CoapOptionType, CoapResponseCode, ContentFormat, Echo, ETag, MaxAge, Observe,
};
use crate::types::CoapUri;
use std::fmt::Display;
use std::fmt::Formatter;

/// Internal representation of a CoAP URI that can be used as a response location.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -350,11 +353,9 @@ impl CoapResponse {
CoapOptionType::NoResponse,
));
},
CoapOption::Other(n, v) => additional_opts.push(CoapOption::Other(*n, v.clone())),

// Handling of echo options is automatically done by libcoap (see man coap_send)
CoapOption::Echo(v) => {

if echo.is_some() {
return Err(MessageConversionError::NonRepeatableOptionRepeated(
CoapOptionType::Echo,
Expand All @@ -367,7 +368,8 @@ impl CoapResponse {
CoapOption::RTag(_) => {},
// OSCORE is currently not supported, and even if it should probably be handled by
// libcoap, so I'm unsure whether we have to expose this.
CoapOption::OsCore(_) => {},
CoapOption::Oscore(_) => {},
CoapOption::Other(n, v) => additional_opts.push(CoapOption::Other(*n, v.clone())),
}
}
let location = if location_path.is_some() || location_query.is_some() {
Expand Down
65 changes: 43 additions & 22 deletions libcoap/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,28 @@ use std::{
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

use libcoap_sys::{coap_option_num_t, coap_pdu_code_t, coap_pdu_type_t, coap_pdu_type_t::{COAP_MESSAGE_ACK, COAP_MESSAGE_CON, COAP_MESSAGE_NON, COAP_MESSAGE_RST}, coap_request_t, coap_response_phrase, COAP_MEDIATYPE_APPLICATION_CBOR, COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT, COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0, COAP_MEDIATYPE_APPLICATION_COSE_KEY, COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET, COAP_MEDIATYPE_APPLICATION_COSE_MAC, COAP_MEDIATYPE_APPLICATION_COSE_MAC0, COAP_MEDIATYPE_APPLICATION_COSE_SIGN, COAP_MEDIATYPE_APPLICATION_COSE_SIGN1, COAP_MEDIATYPE_APPLICATION_CWT, COAP_MEDIATYPE_APPLICATION_DOTS_CBOR, COAP_MEDIATYPE_APPLICATION_EXI, COAP_MEDIATYPE_APPLICATION_JSON, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, COAP_MEDIATYPE_APPLICATION_OCTET_STREAM, COAP_MEDIATYPE_APPLICATION_RDF_XML, COAP_MEDIATYPE_APPLICATION_SENML_CBOR, COAP_MEDIATYPE_APPLICATION_SENML_EXI, COAP_MEDIATYPE_APPLICATION_SENML_JSON, COAP_MEDIATYPE_APPLICATION_SENML_XML, COAP_MEDIATYPE_APPLICATION_SENSML_CBOR, COAP_MEDIATYPE_APPLICATION_SENSML_EXI, COAP_MEDIATYPE_APPLICATION_SENSML_JSON, COAP_MEDIATYPE_APPLICATION_SENSML_XML, COAP_MEDIATYPE_APPLICATION_XML, COAP_MEDIATYPE_TEXT_PLAIN, COAP_OPTION_ACCEPT, COAP_OPTION_BLOCK1, COAP_OPTION_BLOCK2, COAP_OPTION_CONTENT_FORMAT, COAP_OPTION_ETAG, COAP_OPTION_HOP_LIMIT, COAP_OPTION_IF_MATCH, COAP_OPTION_IF_NONE_MATCH, COAP_OPTION_LOCATION_PATH, COAP_OPTION_LOCATION_QUERY, COAP_OPTION_MAXAGE, COAP_OPTION_NORESPONSE, COAP_OPTION_OBSERVE, COAP_OPTION_PROXY_SCHEME, COAP_OPTION_PROXY_URI, COAP_OPTION_SIZE1, COAP_OPTION_SIZE2, COAP_OPTION_URI_HOST, COAP_OPTION_URI_PATH, COAP_OPTION_URI_PORT, COAP_OPTION_URI_QUERY, COAP_OPTION_OSCORE, COAP_OPTION_Q_BLOCK1, COAP_OPTION_Q_BLOCK2, COAP_OPTION_RTAG, COAP_OPTION_ECHO, COAP_MEDIATYPE_APPLICATION_ACE_CBOR, COAP_MEDIATYPE_APPLICATION_COAP_GROUP_JSON, COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ, COAP_MEDIATYPE_APPLICATION_OSCORE};
use libcoap_sys::{
COAP_MEDIATYPE_APPLICATION_ACE_CBOR, COAP_MEDIATYPE_APPLICATION_CBOR, COAP_MEDIATYPE_APPLICATION_COAP_GROUP_JSON,
COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT,
COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0, COAP_MEDIATYPE_APPLICATION_COSE_KEY, COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET, COAP_MEDIATYPE_APPLICATION_COSE_MAC,
COAP_MEDIATYPE_APPLICATION_COSE_MAC0, COAP_MEDIATYPE_APPLICATION_COSE_SIGN,
COAP_MEDIATYPE_APPLICATION_COSE_SIGN1, COAP_MEDIATYPE_APPLICATION_CWT,
COAP_MEDIATYPE_APPLICATION_DOTS_CBOR, COAP_MEDIATYPE_APPLICATION_EXI, COAP_MEDIATYPE_APPLICATION_JSON,
COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ, COAP_MEDIATYPE_APPLICATION_OCTET_STREAM,
COAP_MEDIATYPE_APPLICATION_OSCORE, COAP_MEDIATYPE_APPLICATION_RDF_XML, COAP_MEDIATYPE_APPLICATION_SENML_CBOR,
COAP_MEDIATYPE_APPLICATION_SENML_EXI, COAP_MEDIATYPE_APPLICATION_SENML_JSON,
COAP_MEDIATYPE_APPLICATION_SENML_XML, COAP_MEDIATYPE_APPLICATION_SENSML_CBOR, COAP_MEDIATYPE_APPLICATION_SENSML_EXI,
COAP_MEDIATYPE_APPLICATION_SENSML_JSON, COAP_MEDIATYPE_APPLICATION_SENSML_XML, COAP_MEDIATYPE_APPLICATION_XML,
COAP_MEDIATYPE_TEXT_PLAIN, COAP_OPTION_ACCEPT,
COAP_OPTION_BLOCK1, COAP_OPTION_BLOCK2,
COAP_OPTION_CONTENT_FORMAT, COAP_OPTION_ECHO, COAP_OPTION_ETAG,
COAP_OPTION_HOP_LIMIT, COAP_OPTION_IF_MATCH, COAP_OPTION_IF_NONE_MATCH, COAP_OPTION_LOCATION_PATH, COAP_OPTION_LOCATION_QUERY,
COAP_OPTION_MAXAGE, COAP_OPTION_NORESPONSE, coap_option_num_t, COAP_OPTION_OBSERVE,
COAP_OPTION_OSCORE, COAP_OPTION_PROXY_SCHEME, COAP_OPTION_PROXY_URI, COAP_OPTION_Q_BLOCK1,
COAP_OPTION_Q_BLOCK2, COAP_OPTION_RTAG, COAP_OPTION_SIZE1, COAP_OPTION_SIZE2, COAP_OPTION_URI_HOST,
COAP_OPTION_URI_PATH, COAP_OPTION_URI_PORT, COAP_OPTION_URI_QUERY, coap_pdu_code_t, coap_pdu_type_t,
coap_pdu_type_t::{COAP_MESSAGE_ACK, COAP_MESSAGE_CON, COAP_MESSAGE_NON, COAP_MESSAGE_RST}, coap_request_t, coap_response_phrase,
};

use crate::error::{MessageCodeError, UnknownOptionError};

Expand All @@ -38,7 +59,7 @@ pub type HopLimit = u16;
pub type NoResponse = u8;
pub type Observe = u32;
// TODO actually parse this option (for OSCORE support)
pub type OsCore = Box<[u8]>;
pub type Oscore = Box<[u8]>;
pub type Echo = Box<[u8]>;
pub type RequestTag = Box<[u8]>;

Expand Down Expand Up @@ -73,10 +94,14 @@ pub enum CoapOptionType {
ETag = COAP_OPTION_ETAG as u16,
/// If-None-Match option ([RFC 7252, Section 5.10.8.2](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.8.2)).
IfNoneMatch = COAP_OPTION_IF_NONE_MATCH as u16,
/// Observe option ([RFC 7641, Section 2](https://datatracker.ietf.org/doc/html/rfc7641#section-2)).
Observe = COAP_OPTION_OBSERVE as u16,
/// Uri-Port option ([RFC 7252, Section 5.10.1](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.1)).
UriPort = COAP_OPTION_URI_PORT as u16,
/// Location-Path option ([RFC 7252, Section 5.10.7](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.7)).
LocationPath = COAP_OPTION_LOCATION_PATH as u16,
/// OSCORE option ([RFC 8613, Section 2](https://datatracker.ietf.org/doc/html/rfc8613#section-2).
Oscore = COAP_OPTION_OSCORE as u16,
/// Uri-Path option ([RFC 7252, Section 5.10.1](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.1)).
UriPath = COAP_OPTION_URI_PATH as u16,
/// Content-Format option ([RFC 7252, Section 5.10.3](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.3)).
Expand All @@ -85,38 +110,34 @@ pub enum CoapOptionType {
MaxAge = COAP_OPTION_MAXAGE as u16,
/// Uri-Query option ([RFC 7252, Section 5.10.1](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.1)).
UriQuery = COAP_OPTION_URI_QUERY as u16,
/// Hop-Limit option ([RFC 8768, Section 3](https://datatracker.ietf.org/doc/html/rfc8768#section-3)).
HopLimit = COAP_OPTION_HOP_LIMIT as u16,
/// Accept option ([RFC 7252, Section 5.10.4](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.4)).
Accept = COAP_OPTION_ACCEPT as u16,
/// Q-Block1 option ([RFC 9177, Section 4](https://datatracker.ietf.org/doc/html/rfc9177#section-4)).
QBlock1 = COAP_OPTION_Q_BLOCK1 as u16,
/// Location-Query option ([RFC 7252, Section 5.10.7](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.7)).
LocationQuery = COAP_OPTION_LOCATION_QUERY as u16,
/// Block2 option ([RFC 7959, Section 2.1](https://datatracker.ietf.org/doc/html/rfc7959#section-2.1)).
Block2 = COAP_OPTION_BLOCK2 as u16,
/// Block1 option ([RFC 7959, Section 2.1](https://datatracker.ietf.org/doc/html/rfc7959#section-2.1)).
Block1 = COAP_OPTION_BLOCK1 as u16,
/// Size2 option ([RFC 7959, Section 4](https://datatracker.ietf.org/doc/html/rfc7959#section-4)).
Size2 = COAP_OPTION_SIZE2 as u16,
/// Q-Block2 option ([RFC 9177, Section 4](https://datatracker.ietf.org/doc/html/rfc9177#section-4)).
QBlock2 = COAP_OPTION_Q_BLOCK2 as u16,
/// Proxy-Uri option ([RFC 7252, Section 5.10.2](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.2)).
ProxyUri = COAP_OPTION_PROXY_URI as u16,
/// Proxy-Scheme option ([RFC 7252, Section 5.10.2](https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.2)).
ProxyScheme = COAP_OPTION_PROXY_SCHEME as u16,
/// Observe option ([RFC 7641, Section 2](https://datatracker.ietf.org/doc/html/rfc7641#section-2)).
Observe = COAP_OPTION_OBSERVE as u16,
/// Size1 option ([RFC 7959, Section 4](https://datatracker.ietf.org/doc/html/rfc7959#section-4)).
Size1 = COAP_OPTION_SIZE1 as u16,
/// Size2 option ([RFC 7959, Section 4](https://datatracker.ietf.org/doc/html/rfc7959#section-4)).
Size2 = COAP_OPTION_SIZE2 as u16,
/// Block1 option ([RFC 7959, Section 2.1](https://datatracker.ietf.org/doc/html/rfc7959#section-2.1)).
Block1 = COAP_OPTION_BLOCK1 as u16,
/// Block2 option ([RFC 7959, Section 2.1](https://datatracker.ietf.org/doc/html/rfc7959#section-2.1)).
Block2 = COAP_OPTION_BLOCK2 as u16,
/// No-Response option ([RFC 7967, Section 2](https://datatracker.ietf.org/doc/html/rfc7967#section-2)).
NoResponse = COAP_OPTION_NORESPONSE as u16,
/// OSCORE option ([RFC 8613, Section 2](https://datatracker.ietf.org/doc/html/rfc8613#section-2).
OsCore = COAP_OPTION_OSCORE as u16,
/// Hop-Limit option ([RFC 8768, Section 3](https://datatracker.ietf.org/doc/html/rfc8768#section-3)).
HopLimit = COAP_OPTION_HOP_LIMIT as u16,
/// Echo option ([RFC 9175, Section 2.2](https://datatracker.ietf.org/doc/html/rfc9175#section-2.2)).
Echo = COAP_OPTION_ECHO as u16,
/// No-Response option ([RFC 7967, Section 2](https://datatracker.ietf.org/doc/html/rfc7967#section-2)).
NoResponse = COAP_OPTION_NORESPONSE as u16,
/// Request-Tag option ([RFC 9175, Section 3.2](https://datatracker.ietf.org/doc/html/rfc9175#section-3.2)).
RTag = COAP_OPTION_RTAG as u16,
/// Q-Block1 option ([RFC 9177, Section 4](https://datatracker.ietf.org/doc/html/rfc9177#section-4)).
QBlock1 = COAP_OPTION_Q_BLOCK1 as u16,
/// Q-Block2 option ([RFC 9177, Section 4](https://datatracker.ietf.org/doc/html/rfc9177#section-4)).
QBlock2 = COAP_OPTION_Q_BLOCK2 as u16,
}

impl CoapOptionType {
Expand Down Expand Up @@ -149,7 +170,7 @@ impl CoapOptionType {
CoapOptionType::HopLimit => 1,
CoapOptionType::NoResponse => 1,
CoapOptionType::Observe => 3,
CoapOptionType::OsCore => 255,
CoapOptionType::Oscore => 255,
CoapOptionType::Echo => 40,
CoapOptionType::RTag => 8,
CoapOptionType::QBlock1 => 3,
Expand Down Expand Up @@ -181,7 +202,7 @@ impl CoapOptionType {
CoapOptionType::HopLimit => 1,
CoapOptionType::NoResponse => 0,
CoapOptionType::Observe => 0,
CoapOptionType::OsCore => 0,
CoapOptionType::Oscore => 0,
CoapOptionType::Echo => 1,
CoapOptionType::RTag => 0,
CoapOptionType::QBlock1 => 0,
Expand Down
Loading

0 comments on commit 4241b97

Please sign in to comment.