From 373daf4ad27bbe5b03a41f483e932dc6578a5992 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20De=20Keersmaeker?=
 <francois.dekeersmaeker@uclouvain.be>
Date: Sat, 19 Oct 2024 19:45:58 +0200
Subject: [PATCH] CoAP: check if request

---
 include/coap.h | 11 +++++++++++
 src/coap.c     | 14 ++++++++++++++
 test/coap.c    |  1 +
 3 files changed, 26 insertions(+)

diff --git a/include/coap.h b/include/coap.h
index d56a697..2941a89 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 649f51d..2532349 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 59ff864..60010d0 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);
-- 
GitLab