You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/12/02 19:43:21 UTC

[7/7] incubator-mynewt-core git commit: oic; use coap_udp_hdr for decoding coap header fields.

oic; use coap_udp_hdr for decoding coap header fields.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/325ce5f4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/325ce5f4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/325ce5f4

Branch: refs/heads/develop
Commit: 325ce5f48512abfa7c2ce442e9ccefa3f8b72bc5
Parents: c4a20f7
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Nov 30 16:45:04 2016 -0800
Committer: System Administrator <ma...@runtime.io>
Committed: Fri Dec 2 11:42:45 2016 -0800

----------------------------------------------------------------------
 net/oic/src/api/oc_server_api.c           |  1 +
 net/oic/src/messaging/coap/coap.c         | 33 ++++-----
 net/oic/src/messaging/coap/constants.h    | 19 ++++--
 net/oic/src/messaging/coap/engine.c       |  1 +
 net/oic/src/messaging/coap/observe.c      |  8 +--
 net/oic/src/messaging/coap/observe.h      |  4 +-
 net/oic/src/messaging/coap/transactions.c | 95 ++++++++++++--------------
 net/oic/src/messaging/coap/transactions.h | 17 +++--
 8 files changed, 88 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/api/oc_server_api.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_server_api.c b/net/oic/src/api/oc_server_api.c
index 344d29f..a45cc6f 100644
--- a/net/oic/src/api/oc_server_api.c
+++ b/net/oic/src/api/oc_server_api.c
@@ -258,6 +258,7 @@ oc_send_separate_response(oc_separate_response_t *handle,
                     coap_set_payload(response, handle->buffer,
                       response_buffer.response_length);
                 }
+                t->type = response->type;
                 t->message->length = coap_serialize_message(response,
                   t->message->data);
                 coap_send_transaction(t);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/messaging/coap/coap.c
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/coap.c b/net/oic/src/messaging/coap/coap.c
index 8117afb..10629f3 100644
--- a/net/oic/src/messaging/coap/coap.c
+++ b/net/oic/src/messaging/coap/coap.c
@@ -287,6 +287,7 @@ coap_init_message(coap_packet_t *coap_pkt, coap_message_type_t type,
 size_t
 coap_serialize_message(coap_packet_t *pkt, uint8_t *buffer)
 {
+    struct coap_udp_hdr *cuh;
     uint8_t *option;
     unsigned int current_number = 0;
 
@@ -297,17 +298,12 @@ coap_serialize_message(coap_packet_t *pkt, uint8_t *buffer)
     LOG("-Serializing MID %u to %p, ", pkt->mid, pkt->buffer);
 
     /* set header fields */
-    pkt->buffer[0] = 0x00;
-    pkt->buffer[0] |= COAP_HEADER_VERSION_MASK &
-                         (pkt->version) << COAP_HEADER_VERSION_POSITION;
-    pkt->buffer[0] |=
-      COAP_HEADER_TYPE_MASK & (pkt->type) << COAP_HEADER_TYPE_POSITION;
-    pkt->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK &
-                         (pkt->token_len)
-                           << COAP_HEADER_TOKEN_LEN_POSITION;
-    pkt->buffer[1] = pkt->code;
-    pkt->buffer[2] = (uint8_t)((pkt->mid) >> 8);
-    pkt->buffer[3] = (uint8_t)(pkt->mid);
+    cuh = (struct coap_udp_hdr *)pkt->buffer;
+    cuh->version = pkt->version;
+    cuh->type = pkt->type;
+    cuh->token_len = pkt->token_len;
+    cuh->code = pkt->code;
+    cuh->id = htons(pkt->mid);
 
     /* empty packet, dont need to do more stuff */
     if (!pkt->code) {
@@ -411,6 +407,7 @@ coap_send_message(oc_message_t *message)
 coap_status_t
 coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len)
 {
+    struct coap_udp_hdr *udp;
     uint8_t *current_option;
     unsigned int option_number = 0;
     unsigned int option_delta = 0;
@@ -422,14 +419,12 @@ coap_parse_message(coap_packet_t *pkt, uint8_t *data, uint16_t data_len)
     pkt->buffer = data;
 
     /* parse header fields */
-    pkt->version = (COAP_HEADER_VERSION_MASK & pkt->buffer[0]) >>
-                    COAP_HEADER_VERSION_POSITION;
-    pkt->type = (COAP_HEADER_TYPE_MASK & pkt->buffer[0]) >>
-                 COAP_HEADER_TYPE_POSITION;
-    pkt->token_len = (COAP_HEADER_TOKEN_LEN_MASK & pkt->buffer[0]) >>
-                      COAP_HEADER_TOKEN_LEN_POSITION;
-    pkt->code = pkt->buffer[1];
-    pkt->mid = pkt->buffer[2] << 8 | pkt->buffer[3];
+    udp = (struct coap_udp_hdr *)data;
+    pkt->version = udp->version;
+    pkt->type = udp->type;
+    pkt->token_len = udp->token_len;
+    pkt->code = udp->code;
+    pkt->mid = ntohs(udp->id);
 
     if (pkt->version != 1) {
         coap_error_message = "CoAP version must be 1";

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/messaging/coap/constants.h
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/constants.h b/net/oic/src/messaging/coap/constants.h
index 6cb37bc..e0c0f66 100644
--- a/net/oic/src/messaging/coap/constants.h
+++ b/net/oic/src/messaging/coap/constants.h
@@ -50,12 +50,19 @@ extern "C" {
 #define COAP_TOKEN_LEN 8 /* The maximum number of bytes for the Token */
 #define COAP_ETAG_LEN 8  /* The maximum number of bytes for the ETag */
 
-#define COAP_HEADER_VERSION_MASK 0xC0
-#define COAP_HEADER_VERSION_POSITION 6
-#define COAP_HEADER_TYPE_MASK 0x30
-#define COAP_HEADER_TYPE_POSITION 4
-#define COAP_HEADER_TOKEN_LEN_MASK 0x0F
-#define COAP_HEADER_TOKEN_LEN_POSITION 0
+struct coap_udp_hdr {
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+    uint8_t  version:2;         /* protocol version */
+    uint8_t  type:2;            /* type flag */
+    uint8_t  token_len:4;       /* length of token */
+#else
+    uint8_t  token_len:4;       /* length of token */
+    uint8_t  type:2;            /* type flag */
+    uint8_t  version:2;         /* protocol version */
+#endif
+    uint8_t  code;          /* request (1-10) or response (value 40-255) */
+    uint16_t id;          /* transaction id */
+};
 
 #define COAP_HEADER_OPTION_DELTA_MASK 0xF0
 #define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/messaging/coap/engine.c
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/engine.c b/net/oic/src/messaging/coap/engine.c
index 1145447..511b71c 100644
--- a/net/oic/src/messaging/coap/engine.c
+++ b/net/oic/src/messaging/coap/engine.c
@@ -223,6 +223,7 @@ coap_receive(oc_message_t *msg)
                  response, transaction->message->data)) == 0) {
             erbium_status_code = PACKET_SERIALIZATION_ERROR;
           }
+          transaction->type = response->type;
         }
       } else {
         erbium_status_code = SERVICE_UNAVAILABLE_5_03;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/messaging/coap/observe.c
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/observe.c b/net/oic/src/messaging/coap/observe.c
index 711cfff..0202bb6 100644
--- a/net/oic/src/messaging/coap/observe.c
+++ b/net/oic/src/messaging/coap/observe.c
@@ -298,6 +298,7 @@ coap_notify_observers(oc_resource_t *resource,
                 transaction->message->length =
                   coap_serialize_message(notification,
                     transaction->message->data);
+                transaction->type = notification->type;
 
                 coap_send_transaction(transaction);
             }
@@ -307,12 +308,11 @@ coap_notify_observers(oc_resource_t *resource,
 }
 /*---------------------------------------------------------------------------*/
 int
-coap_observe_handler(void *request, void *response, oc_resource_t *resource,
-                     oc_endpoint_t *endpoint)
+coap_observe_handler(coap_packet_t *coap_req, coap_packet_t *coap_res,
+                     oc_resource_t *resource, oc_endpoint_t *endpoint)
 {
-    coap_packet_t *const coap_req = (coap_packet_t *)request;
-    coap_packet_t *const coap_res = (coap_packet_t *)response;
     int dup = -1;
+
     if (coap_req->code == COAP_GET &&
       coap_res->code < 128) { /* GET request and response without error code */
         if (IS_OPTION(coap_req, COAP_OPTION_OBSERVE)) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/messaging/coap/observe.h
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/observe.h b/net/oic/src/messaging/coap/observe.h
index ae63bf1..df58cc2 100644
--- a/net/oic/src/messaging/coap/observe.h
+++ b/net/oic/src/messaging/coap/observe.h
@@ -74,8 +74,8 @@ int coap_notify_observers(oc_resource_t *resource,
                           oc_endpoint_t *endpoint);
 // int coap_notify_observers_sub(oc_resource_t *resource, const char *subpath);
 
-int coap_observe_handler(void *request, void *response, oc_resource_t *resource,
-                         oc_endpoint_t *endpoint);
+int coap_observe_handler(coap_packet_t *request, coap_packet_t *response,
+                         oc_resource_t *resource, oc_endpoint_t *endpoint);
 
 void coap_observe_init(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/messaging/coap/transactions.c
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/transactions.c b/net/oic/src/messaging/coap/transactions.c
index 42ca498..708fc8a 100644
--- a/net/oic/src/messaging/coap/transactions.c
+++ b/net/oic/src/messaging/coap/transactions.c
@@ -76,7 +76,6 @@ coap_new_transaction(uint16_t mid, oc_endpoint_t *endpoint)
             LOG("Created new transaction %d %d\n", mid, (int) message->length);
             t->mid = mid;
             t->retrans_counter = 0;
-
             t->message = message;
 
             /* save client address */
@@ -99,66 +98,62 @@ coap_new_transaction(uint16_t mid, oc_endpoint_t *endpoint)
 void
 coap_send_transaction(coap_transaction_t *t)
 {
-  LOG("Sending transaction %u\n", t->mid);
-  bool confirmable = false;
-
-  confirmable =
-    (COAP_TYPE_CON == ((COAP_HEADER_TYPE_MASK & t->message->data[0]) >>
-                       COAP_HEADER_TYPE_POSITION))
-      ? true
-      : false;
-
-  if (confirmable) {
-    if (t->retrans_counter < COAP_MAX_RETRANSMIT) {
-      /* not timed out yet */
-      LOG("Keeping transaction %u\n", t->mid);
-
-      if (t->retrans_counter == 0) {
-        t->retrans_tmo =
-          COAP_RESPONSE_TIMEOUT_TICKS +
-          (oc_random_rand() %
-           (oc_clock_time_t)COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
-        LOG("Initial interval " OC_CLK_FMT "\n", t->retrans_tmo);
-      } else {
-        t->retrans_tmo <<= 1; /* double */
-        LOG("Doubled " OC_CLK_FMT "\n", t->retrans_tmo);
-      }
-
-      os_callout_reset(&t->retrans_timer, t->retrans_tmo);
-
-      coap_send_message(t->message);
-
-      oc_message_add_ref(t->message);
-
-      t = NULL;
-    } else {
-      /* timed out */
-      LOG("Timeout\n");
+    LOG("Sending transaction %u\n", t->mid);
+    bool confirmable = false;
+
+    confirmable = (COAP_TYPE_CON == t->type) ? true : false;
+
+    if (confirmable) {
+        if (t->retrans_counter < COAP_MAX_RETRANSMIT) {
+            /* not timed out yet */
+            LOG("Keeping transaction %u\n", t->mid);
+
+            if (t->retrans_counter == 0) {
+                t->retrans_tmo =
+                  COAP_RESPONSE_TIMEOUT_TICKS +
+                  (oc_random_rand() %
+                    (oc_clock_time_t)COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
+                LOG("Initial interval " OC_CLK_FMT "\n", t->retrans_tmo);
+            } else {
+                t->retrans_tmo <<= 1; /* double */
+                LOG("Doubled " OC_CLK_FMT "\n", t->retrans_tmo);
+            }
+
+            os_callout_reset(&t->retrans_timer, t->retrans_tmo);
+
+            coap_send_message(t->message);
+
+            oc_message_add_ref(t->message);
+
+            t = NULL;
+        } else {
+            /* timed out */
+            LOG("Timeout\n");
 
 #ifdef OC_SERVER
-      LOG("timeout.. so removing observers\n");
-      /* handle observers */
-      coap_remove_observer_by_client(&t->message->endpoint);
+            LOG("timeout.. so removing observers\n");
+            /* handle observers */
+            coap_remove_observer_by_client(&t->message->endpoint);
 #endif /* OC_SERVER */
 
 #ifdef OC_SECURITY
-      if (t->message->endpoint.flags & SECURED) {
-        oc_sec_dtls_close_init(&t->message->endpoint);
-      }
+            if (t->message->endpoint.flags & SECURED) {
+                oc_sec_dtls_close_init(&t->message->endpoint);
+            }
 #endif /* OC_SECURITY */
 
 #ifdef OC_CLIENT
-      oc_ri_remove_client_cb_by_mid(t->mid);
+            oc_ri_remove_client_cb_by_mid(t->mid);
 #endif /* OC_CLIENT */
 
-      coap_clear_transaction(t);
-    }
-  } else {
-    coap_send_message(t->message);
-    oc_message_add_ref(t->message);
+            coap_clear_transaction(t);
+        }
+    } else {
+        coap_send_message(t->message);
+        oc_message_add_ref(t->message);
 
-    coap_clear_transaction(t);
-  }
+        coap_clear_transaction(t);
+    }
 }
 /*---------------------------------------------------------------------------*/
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/325ce5f4/net/oic/src/messaging/coap/transactions.h
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/transactions.h b/net/oic/src/messaging/coap/transactions.h
index 794b189..8768619 100644
--- a/net/oic/src/messaging/coap/transactions.h
+++ b/net/oic/src/messaging/coap/transactions.h
@@ -54,16 +54,15 @@ extern "C" {
     1
 
 /* container for transactions with message buffer and retransmission info */
-typedef struct coap_transaction
-{
-  SLIST_ENTRY(coap_transaction) next;
-
-  uint16_t mid;
-  uint8_t retrans_counter;
-  uint32_t retrans_tmo;
-  struct os_callout retrans_timer;
-  oc_message_t *message;
+typedef struct coap_transaction {
+    SLIST_ENTRY(coap_transaction) next;
 
+    uint16_t mid;
+    uint8_t retrans_counter;
+    coap_message_type_t type;
+    uint32_t retrans_tmo;
+    struct os_callout retrans_timer;
+    oc_message_t *message;
 } coap_transaction_t;
 
 void coap_register_as_transaction_handler(void);