diff --git a/include/coap.h b/include/coap.h index d56a6978215faf60f35afc187dcfd7f8ab5cc5cf..2941a89e04a40c1f5a2294b7c73b8b8e7b715fff 100644 --- a/include/coap.h +++ b/include/coap.h @@ -63,6 +63,17 @@ typedef struct coap_message */ coap_message_t coap_parse_message(uint8_t *data, uint16_t length); +/** + * @brief Check if a CoAP message is a request. + * + * A CoAP message is a request if its type is Confirmable or Non-Confirmable. + * + * @param coap_message CoAP message to check + * @return true if the given CoAP message is a request + * @return false if the given CoAP message is a response + */ +bool coap_is_request(coap_message_t coap_message); + ///// DESTROY ///// diff --git a/src/coap.c b/src/coap.c index 649f51d8538cff5222aabbb81b4b8395cd46b4d8..2532349688ee67cb0b221e73d0bb3a53f1214dc2 100644 --- a/src/coap.c +++ b/src/coap.c @@ -146,6 +146,20 @@ coap_message_t coap_parse_message(uint8_t *data, uint16_t length) return message; } +/** + * @brief Check if a CoAP message is a request. + * + * A CoAP message is a request if its type is Confirmable or Non-Confirmable. + * + * @param coap_message CoAP message to check + * @return true if the given CoAP message is a request + * @return false if the given CoAP message is a response + */ +bool coap_is_request(coap_message_t coap_message) +{ + return coap_message.type == COAP_CON || coap_message.type == COAP_NON; +} + ///// DESTROY ///// diff --git a/test/coap.c b/test/coap.c index 59ff864efa580545d67890b4575c3e67f61b3777..60010d04066179c0f7919d907c1176e0609c07ea 100644 --- a/test/coap.c +++ b/test/coap.c @@ -45,6 +45,7 @@ void test_coap_non_get() { expected.uri = "/oic/res?rt=x.com.samsung.provisioninginfo"; // Compare messages + CU_ASSERT_TRUE(coap_is_request(actual)); CU_ASSERT_EQUAL(actual.type, expected.type); CU_ASSERT_EQUAL(actual.method, expected.method); CU_ASSERT_STRING_EQUAL(actual.uri, expected.uri);