Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 257381d1c -> 5c27bba13


oic; client response handler now gets similar argument to server
request handler. Instead of void * to payload, it gets coap_packet_t.


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/5c27bba1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/5c27bba1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/5c27bba1

Branch: refs/heads/develop
Commit: 5c27bba139146288d869fcf1d1177dc34b56b639
Parents: 257381d
Author: Marko Kiiskila <[email protected]>
Authored: Wed Jan 4 11:07:17 2017 -0800
Committer: Marko Kiiskila <[email protected]>
Committed: Wed Jan 4 11:30:19 2017 -0800

----------------------------------------------------------------------
 apps/ocf_sample/src/main.c            |  33 +++---
 net/oic/include/oic/oc_client_state.h |  22 ++--
 net/oic/src/api/oc_discovery.c        |   6 +-
 net/oic/src/api/oc_ri.c               | 173 ++++++++++++++---------------
 4 files changed, 118 insertions(+), 116 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5c27bba1/apps/ocf_sample/src/main.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/main.c b/apps/ocf_sample/src/main.c
index a68c5ff..2fdfb12 100644
--- a/apps/ocf_sample/src/main.c
+++ b/apps/ocf_sample/src/main.c
@@ -144,28 +144,33 @@ put_light(oc_client_response_t *data)
 }
 
 static void
-observe_light(oc_client_response_t *data)
+observe_light(oc_client_response_t *rsp)
 {
-    printf("OBSERVE_light:\n");
-    oc_rep_t *rep = data->payload;
-    while (rep != NULL) {
-        printf("key %s, value ", oc_string(rep->name));
-        switch (rep->type) {
-            case BOOL:
-                printf("%d\n", rep->value_boolean);
-                light_state = rep->value_boolean;
-                break;
-            default:
-                break;
+    bool state;
+    int len;
+    const uint8_t *data;
+    struct cbor_attr_t attrs[] = {
+        [0] = {
+            .attribute = "state",
+            .type = CborAttrBooleanType,
+            .addr.boolean = &state,
+            .dflt.boolean = false
+        },
+        [1] = {
         }
-        rep = rep->next;
+    };
+
+    len = coap_get_payload(rsp->packet, &data);
+    if (cbor_read_flat_attrs(data, len, attrs)) {
+        printf("OBSERVE_light: %d\n", state);
+        light_state = state;
     }
 
     if (oc_init_put(light_1, &light_server, NULL, &put_light, LOW_QOS)) {
         oc_rep_start_root_object();
         oc_rep_set_boolean(root, state, !light_state);
         oc_rep_end_root_object();
-        if (oc_do_put()) {
+        if (oc_do_put() == true) {
             printf("Sent PUT request\n");
         } else {
             printf("Could not send PUT\n");

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5c27bba1/net/oic/include/oic/oc_client_state.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_client_state.h 
b/net/oic/include/oic/oc_client_state.h
index 5897117..f281b8b 100644
--- a/net/oic/include/oic/oc_client_state.h
+++ b/net/oic/include/oic/oc_client_state.h
@@ -28,21 +28,19 @@ extern "C" {
 #ifdef OC_CLIENT
 typedef enum { HIGH_QOS = 0, LOW_QOS } oc_qos_t;
 
-typedef struct
-{
-  oc_rep_t *payload;
-  oc_status_t code;
-  int observe_option;
+typedef struct {
+    void *packet;
+    oc_status_t code;
+    uint32_t observe_option;
 } oc_client_response_t;
 
-typedef struct
-{
-  oc_endpoint_t endpoint;
+typedef struct {
+    oc_endpoint_t endpoint;
 } oc_server_handle_t;
 
 typedef enum {
-  OC_STOP_DISCOVERY = 0,
-  OC_CONTINUE_DISCOVERY
+    OC_STOP_DISCOVERY = 0,
+    OC_CONTINUE_DISCOVERY
 } oc_discovery_flags_t;
 
 typedef oc_discovery_flags_t(oc_discovery_cb_t)(const char *, const char *,
@@ -71,7 +69,7 @@ typedef struct oc_client_cb {
     oc_method_t method;
 } oc_client_cb_t;
 
-bool oc_ri_invoke_client_cb(void *response, oc_endpoint_t *endpoint);
+bool oc_ri_invoke_client_cb(coap_packet_t *response, oc_endpoint_t *endpoint);
 
 oc_client_cb_t *oc_ri_alloc_client_cb(const char *uri,
                                       oc_server_handle_t *server,
@@ -83,7 +81,7 @@ oc_client_cb_t *oc_ri_get_client_cb(const char *uri, 
oc_server_handle_t *server,
 
 void oc_ri_remove_client_cb_by_mid(uint16_t mid);
 
-oc_discovery_flags_t oc_ri_process_discovery_payload(uint8_t *payload, int len,
+oc_discovery_flags_t oc_ri_process_discovery_payload(coap_packet_t *rsp,
                                                      oc_discovery_cb_t 
*handler,
                                                      oc_endpoint_t *endpoint);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5c27bba1/net/oic/src/api/oc_discovery.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_discovery.c b/net/oic/src/api/oc_discovery.c
index 6427056..18992f3 100644
--- a/net/oic/src/api/oc_discovery.c
+++ b/net/oic/src/api/oc_discovery.c
@@ -176,7 +176,7 @@ oc_create_discovery_resource(void)
 
 #ifdef OC_CLIENT
 oc_discovery_flags_t
-oc_ri_process_discovery_payload(uint8_t *payload, int len,
+oc_ri_process_discovery_payload(coap_packet_t *rsp,
                                 oc_discovery_cb_t *handler,
                                 oc_endpoint_t *endpoint)
 {
@@ -194,10 +194,14 @@ oc_ri_process_discovery_payload(uint8_t *payload, int len,
   oc_string_array_t types = {};
   oc_interface_mask_t interfaces = 0;
   oc_server_handle_t handle;
+  const uint8_t *payload;
+  int len;
 
   memcpy(&handle.endpoint, endpoint, sizeof(oc_endpoint_t));
 
   oc_rep_t *array = 0, *rep;
+
+  len = coap_get_payload(rsp, &payload);
   int s = oc_parse_rep(payload, len, &rep);
   if (s == 0)
     array = rep;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5c27bba1/net/oic/src/api/oc_ri.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_ri.c b/net/oic/src/api/oc_ri.c
index 2f0faea..d3b358a 100644
--- a/net/oic/src/api/oc_ri.c
+++ b/net/oic/src/api/oc_ri.c
@@ -704,104 +704,99 @@ oc_ri_send_rst(oc_endpoint_t *endpoint, uint8_t *token, 
uint8_t token_len,
 }
 
 bool
-oc_ri_invoke_client_cb(void *response, oc_endpoint_t *endpoint)
+oc_ri_invoke_client_cb(coap_packet_t *rsp, oc_endpoint_t *endpoint)
 {
-  uint8_t *payload;
-  int payload_len;
-  coap_packet_t *const pkt = (coap_packet_t *)response;
-  oc_client_cb_t *cb, *tmp;
-  int i;
-  /*
-    if con then send ack and process as above
-    -empty ack sent from below by engine
-    if ack with piggyback then process as above
-    -processed below
-    if ack and empty then it is a separate response, and keep cb
-    -handled by separate flag
-    if ack is for block then store data and pass to client
-  */
-
-  unsigned int content_format = APPLICATION_CBOR;
-  coap_get_header_content_format(pkt, &content_format);
-
-  cb = SLIST_FIRST(&oc_client_cbs);
-  while (cb != NULL) {
-    tmp = SLIST_NEXT(cb, next);
-    if (cb->token_len == pkt->token_len &&
-        memcmp(cb->token, pkt->token, pkt->token_len) == 0) {
-
-      /* If content format is not CBOR, then reject response
-         and clear callback
-   If incoming response type is RST, then clear callback
-      */
-      if (content_format != APPLICATION_CBOR || pkt->type == COAP_TYPE_RST) {
-        free_client_cb(cb);
-        break;
-      }
+    oc_client_cb_t *cb, *tmp;
+    oc_client_response_t client_response;
+    unsigned int content_format = APPLICATION_CBOR;
+    oc_response_handler_t handler;
+    int i;
 
-      /* Check code, translate to oc_status_code, store
-   Check observe option:
-         if no observe option, set to -1, else store observe seq
-      */
-      oc_client_response_t client_response;
-      client_response.observe_option = -1;
-      client_response.payload = 0;
-
-      for (i = 0; i < __NUM_OC_STATUS_CODES__; i++) {
-        if (oc_coap_status_codes[i] == pkt->code) {
-          client_response.code = i;
-          break;
+    /*
+      if con then send ack and process as above
+      -empty ack sent from below by engine
+      if ack with piggyback then process as above
+      -processed below
+      if ack and empty then it is a separate response, and keep cb
+      -handled by separate flag
+      if ack is for block then store data and pass to client
+    */
+    coap_get_header_content_format(rsp, &content_format);
+
+    cb = SLIST_FIRST(&oc_client_cbs);
+    while (cb != NULL) {
+        tmp = SLIST_NEXT(cb, next);
+        if (cb->token_len != rsp->token_len ||
+            memcmp(cb->token, rsp->token, rsp->token_len)) {
+            cb = tmp;
+            continue;
         }
-      }
-      coap_get_header_observe(pkt, (uint32_t*)&client_response.observe_option);
-
-      bool separate = false;
-      /*
-  if payload exists, process payload and save in client response
-  send client response to callback and return
-      */
-      payload_len = coap_get_payload(response, (const uint8_t **)&payload);
-      if (payload_len) {
-        if (cb->discovery) {
-          if (oc_ri_process_discovery_payload(payload, payload_len, 
cb->handler,
-                                              endpoint) == OC_STOP_DISCOVERY) {
+
+        /* If content format is not CBOR, then reject response
+           and clear callback
+           If incoming response type is RST, then clear callback
+        */
+        if (content_format != APPLICATION_CBOR || rsp->type == COAP_TYPE_RST) {
             free_client_cb(cb);
-          }
-        } else {
-          uint16_t err =
-            oc_parse_rep(payload, payload_len, &client_response.payload);
-          if (err == 0) {
-            oc_response_handler_t handler = (oc_response_handler_t)cb->handler;
-            handler(&client_response);
-          }
-          oc_free_rep(client_response.payload);
-        }
-      } else { // no payload
-        if (pkt->type == COAP_TYPE_ACK && pkt->code == 0) {
-          separate = true;
-        } else if (!cb->discovery) {
-          oc_response_handler_t handler = (oc_response_handler_t)cb->handler;
-          handler(&client_response);
+            break;
         }
-      }
 
-      /* check observe sequence number:
-   if -1 then remove cb, else keep cb
-         if it is an ACK for a separate response, keep cb
-         if it is a discovery response, keep cb so that it will last
-   for the entirety of OC_CLIENT_CB_TIMEOUT_SECS
-      */
-      if (client_response.observe_option == -1 && !separate && !cb->discovery) 
{
-        free_client_cb(cb);
-      } else
-        cb->observe_seq = client_response.observe_option;
+        /* Check code, translate to oc_status_code, store
+           Check observe option:
+           if no observe option, set to -1, else store observe seq
+        */
+        client_response.packet = NULL;
+        client_response.observe_option = -1;
+
+        for (i = 0; i < __NUM_OC_STATUS_CODES__; i++) {
+            if (oc_coap_status_codes[i] == rsp->code) {
+                client_response.code = i;
+                break;
+            }
+        }
+        coap_get_header_observe(rsp, &client_response.observe_option);
+
+        bool separate = false;
+        /*
+          if payload exists, process payload and save in client response
+          send client response to callback and return
+        */
+        if (rsp->payload_len) {
+            if (cb->discovery) {
+                if (oc_ri_process_discovery_payload(rsp, cb->handler,
+                                              endpoint) == OC_STOP_DISCOVERY) {
+                    free_client_cb(cb);
+                }
+            } else {
+                client_response.packet = rsp;
+                handler = (oc_response_handler_t)cb->handler;
+                handler(&client_response);
+            }
+        } else { // no payload
+            if (rsp->type == COAP_TYPE_ACK && rsp->code == 0) {
+                separate = true;
+            } else if (!cb->discovery) {
+                handler = (oc_response_handler_t)cb->handler;
+                handler(&client_response);
+            }
+        }
 
-      break;
+        /* check observe sequence number:
+           if -1 then remove cb, else keep cb
+           if it is an ACK for a separate response, keep cb
+           if it is a discovery response, keep cb so that it will last
+           for the entirety of OC_CLIENT_CB_TIMEOUT_SECS
+        */
+        if (client_response.observe_option == -1 && !separate &&
+            !cb->discovery) {
+            free_client_cb(cb);
+        } else {
+            cb->observe_seq = client_response.observe_option;
+        }
+        break;
     }
-    cb = tmp;
-  }
 
-  return true;
+    return true;
 }
 
 oc_client_cb_t *

Reply via email to