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

[01/50] incubator-mynewt-core git commit: oic; construct writes directly to mbuf.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/sensors_branch 269791e47 -> 9482961d3


oic; construct writes directly to mbuf.


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

Branch: refs/heads/sensors_branch
Commit: 6331a5e5a22825ded18054b42f140d74658442e6
Parents: 30f6b67
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 19:03:25 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/oic/include/oic/oc_rep.h         |  3 +-
 net/oic/include/oic/oc_ri.h          |  3 +
 net/oic/src/api/oc_client_api.c      | 76 ++++++++++++-------------
 net/oic/src/api/oc_core_res.c        | 94 +++++++++++++++++--------------
 net/oic/src/api/oc_rep.c             | 14 ++---
 net/oic/src/api/oc_ri.c              | 42 +++++++++-----
 net/oic/src/api/oc_server_api.c      |  3 +-
 net/oic/src/messaging/coap/coap.c    | 23 ++++++--
 net/oic/src/messaging/coap/coap.h    |  3 +-
 net/oic/src/messaging/coap/engine.c  | 42 +++++---------
 net/oic/src/messaging/coap/observe.c | 24 +++++---
 net/oic/src/messaging/coap/oc_coap.h |  3 +-
 12 files changed, 182 insertions(+), 148 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/net/oic/include/oic/oc_rep.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_rep.h b/net/oic/include/oic/oc_rep.h
index 72ae7fc..17983a4 100644
--- a/net/oic/include/oic/oc_rep.h
+++ b/net/oic/include/oic/oc_rep.h
@@ -31,7 +31,8 @@ extern "C" {
 extern CborEncoder g_encoder, root_map, links_array;
 extern CborError g_err;
 
-void oc_rep_new(uint8_t *payload, int size);
+struct os_mbuf;
+void oc_rep_new(struct os_mbuf *m);
 void oc_rep_reset(void);
 int oc_rep_finalize(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/net/oic/include/oic/oc_ri.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_ri.h b/net/oic/include/oic/oc_ri.h
index 9f07d5f..9449685 100644
--- a/net/oic/include/oic/oc_ri.h
+++ b/net/oic/include/oic/oc_ri.h
@@ -149,6 +149,9 @@ int oc_ri_get_query_value(const char *query, int query_len, const char *key,
 
 oc_interface_mask_t oc_ri_get_interface_mask(char *iface, int if_len);
 
+bool oc_ri_invoke_coap_entity_handler(void *request, void *response,
+                                      int32_t *offset, oc_endpoint_t *endpoint);
+
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/net/oic/src/api/oc_client_api.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_client_api.c b/net/oic/src/api/oc_client_api.c
index c15e412..bc29fe1 100644
--- a/net/oic/src/api/oc_client_api.c
+++ b/net/oic/src/api/oc_client_api.c
@@ -22,43 +22,42 @@
 #ifdef OC_CLIENT
 #define OC_CLIENT_CB_TIMEOUT_SECS COAP_RESPONSE_TIMEOUT
 
-static struct os_mbuf *message;
-static oc_message_t *rsp;
-static coap_transaction_t *transaction;
-coap_packet_t request[1];
+static struct os_mbuf *oc_c_message;
+static struct os_mbuf *oc_c_rsp;
+static coap_transaction_t *oc_c_transaction;
+static coap_packet_t oc_c_request[1];
 
 static bool
 dispatch_coap_request(void)
 {
     int response_length = oc_rep_finalize();
 
-    if (!transaction) {
-        if (message) {
-            if (response_length) {
-                coap_set_payload(request, rsp->data, response_length);
-                coap_set_header_content_format(request, APPLICATION_CBOR);
-            }
-            if (!coap_serialize_message(request, message)) {
-                coap_send_message(message, 0);
+    if (response_length) {
+        oc_c_request->payload_m = oc_c_rsp;
+        oc_c_request->payload_len = response_length;
+        coap_set_header_content_format(oc_c_request, APPLICATION_CBOR);
+    } else {
+        os_mbuf_free_chain(oc_c_rsp);
+    }
+    oc_c_rsp = NULL;
+
+    if (!oc_c_transaction) {
+        if (oc_c_message) {
+            if (!coap_serialize_message(oc_c_request, oc_c_message)) {
+                coap_send_message(oc_c_message, 0);
             } else {
-                os_mbuf_free_chain(message);
+                os_mbuf_free_chain(oc_c_message);
             }
-            oc_message_unref(rsp);
-            message = NULL;
+            oc_c_message = NULL;
             return true;
         }
     } else {
-        if (response_length) {
-            coap_set_payload(request, transaction->m, response_length);
-            coap_set_header_content_format(request, APPLICATION_CBOR);
-        }
-        if (!coap_serialize_message(request, transaction->m)) {
-            coap_send_transaction(transaction);
+        if (!coap_serialize_message(oc_c_request, oc_c_transaction->m)) {
+            coap_send_transaction(oc_c_transaction);
         } else {
-            coap_clear_transaction(transaction);
+            coap_clear_transaction(oc_c_transaction);
         }
-        oc_message_unref(rsp);
-        transaction = NULL;
+        oc_c_transaction = NULL;
         return true;
     }
     return false;
@@ -69,33 +68,33 @@ prepare_coap_request(oc_client_cb_t *cb, oc_string_t *query)
 {
     coap_message_type_t type = COAP_TYPE_NON;
 
-    rsp = oc_allocate_message();
-    if (!rsp) {
+    oc_c_rsp = os_msys_get_pkthdr(0, 0);
+    if (!oc_c_rsp) {
         return false;
     }
     if (cb->qos == HIGH_QOS) {
         type = COAP_TYPE_CON;
-        transaction = coap_new_transaction(cb->mid, &cb->server.endpoint);
-        if (!transaction) {
+        oc_c_transaction = coap_new_transaction(cb->mid, &cb->server.endpoint);
+        if (!oc_c_transaction) {
             goto free_rsp;
         }
     } else {
-        message = oc_allocate_mbuf(&cb->server.endpoint);
-        if (!message) {
+        oc_c_message = oc_allocate_mbuf(&cb->server.endpoint);
+        if (!oc_c_message) {
             goto free_rsp;
         }
     }
-    oc_rep_new(rsp->data, COAP_MAX_BLOCK_SIZE);
+    oc_rep_new(oc_c_rsp);
 
-    coap_init_message(request, type, cb->method, cb->mid);
-    coap_set_header_accept(request, APPLICATION_CBOR);
-    coap_set_token(request, cb->token, cb->token_len);
-    coap_set_header_uri_path(request, oc_string(cb->uri));
+    coap_init_message(oc_c_request, type, cb->method, cb->mid);
+    coap_set_header_accept(oc_c_request, APPLICATION_CBOR);
+    coap_set_token(oc_c_request, cb->token, cb->token_len);
+    coap_set_header_uri_path(oc_c_request, oc_string(cb->uri));
     if (cb->observe_seq != -1) {
-        coap_set_header_observe(request, cb->observe_seq);
+        coap_set_header_observe(oc_c_request, cb->observe_seq);
     }
     if (query && oc_string_len(*query)) {
-        coap_set_header_uri_query(request, oc_string(*query));
+        coap_set_header_uri_query(oc_c_request, oc_string(*query));
     }
     if (cb->observe_seq == -1 && cb->qos == LOW_QOS) {
         os_callout_reset(&cb->callout,
@@ -104,7 +103,8 @@ prepare_coap_request(oc_client_cb_t *cb, oc_string_t *query)
 
     return true;
 free_rsp:
-    oc_message_unref(rsp);
+    os_mbuf_free_chain(oc_c_rsp);
+    oc_c_rsp = NULL;
     return false;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/net/oic/src/api/oc_core_res.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_core_res.c b/net/oic/src/api/oc_core_res.c
index 0c4a919..ccb3896 100644
--- a/net/oic/src/api/oc_core_res.c
+++ b/net/oic/src/api/oc_core_res.c
@@ -14,10 +14,12 @@
  // limitations under the License.
  */
 
-#include "oc_core_res.h"
+#include <os/os_mbuf.h>
+
+#include "oic/oc_core_res.h"
 #include "messaging/coap/oc_coap.h"
-#include "oc_rep.h"
-#include "oc_ri.h"
+#include "oic/oc_rep.h"
+#include "oic/oc_ri.h"
 
 #ifdef OC_SECURITY
 #include "security/oc_pstat.h"
@@ -62,26 +64,26 @@ oc_core_encode_interfaces_mask(CborEncoder *parent,
 static void
 oc_core_device_handler(oc_request_t *req, oc_interface_mask_t interface)
 {
-    uint8_t *buffer = req->response->response_buffer->buffer;
-    uint16_t buffer_size = req->response->response_buffer->buffer_size;
+    struct oc_response_buffer *rsp_buf;
+    struct os_mbuf *buffer;
     int size;
+    char *str;
 
+    rsp_buf = req->response->response_buffer;
+    buffer = rsp_buf->buffer;
     size = oc_string_len(oc_device_info[req->resource->device].payload);
-
-    if (buffer_size < size) {
-        req->response->response_buffer->response_length = 0;
-        req->response->response_buffer->code =
-          oc_status_code(OC_STATUS_INTERNAL_SERVER_ERROR);
-        return;
-    }
+    str = oc_string(oc_device_info[req->resource->device].payload);
 
     switch (interface) {
     case OC_IF_R:
     case OC_IF_BASELINE:
-        memcpy(buffer, oc_string(oc_device_info[req->resource->device].payload),
-          size);
-        req->response->response_buffer->response_length = size;
-        req->response->response_buffer->code = oc_status_code(OC_STATUS_OK);
+        if (os_mbuf_append(buffer, str, size)) {
+            rsp_buf->response_length = 0;
+            rsp_buf->code = oc_status_code(OC_STATUS_INTERNAL_SERVER_ERROR);
+        } else {
+            rsp_buf->response_length = size;
+            rsp_buf->code = oc_status_code(OC_STATUS_OK);
+        }
         break;
     default:
         break;
@@ -95,7 +97,7 @@ oc_core_get_num_devices(void)
 }
 
 static int
-finalize_payload(oc_string_t *temp_buffer, oc_string_t *payload)
+finalize_payload(struct os_mbuf *m, oc_string_t *payload)
 {
     int size;
 
@@ -103,11 +105,11 @@ finalize_payload(oc_string_t *temp_buffer, oc_string_t *payload)
     size = oc_rep_finalize();
     if (size != -1) {
         oc_alloc_string(payload, size + 1);
-        memcpy(payload->os_str, temp_buffer->os_str, size);
-        oc_free_string(temp_buffer);
+        os_mbuf_copydata(m, 0, size, payload->os_str);
+        os_mbuf_free_chain(m);
         return 1;
     }
-    oc_free_string(temp_buffer);
+    os_mbuf_free_chain(m);
     return -1;
 }
 
@@ -116,12 +118,12 @@ oc_core_add_new_device(const char *uri, const char *rt, const char *name,
                        const char *spec_version, const char *data_model_version,
                        oc_core_add_device_cb_t add_device_cb, void *data)
 {
-    oc_string_t temp_buffer;
+    struct os_mbuf *tmp;
     int ocf_d;
     char uuid[37];
 
     if (device_count == MAX_NUM_DEVICES) {
-        return false;
+        return NULL;
     }
 
     /* Once provisioned, UUID is retrieved from the credential store.
@@ -146,8 +148,11 @@ oc_core_add_new_device(const char *uri, const char *rt, const char *name,
                               oc_core_device_handler, 0, 0, 0, device_count);
 
     /* Encoding device resource payload */
-    oc_alloc_string(&temp_buffer, MAX_DEVICE_PAYLOAD_SIZE);
-    oc_rep_new(temp_buffer.os_str, MAX_DEVICE_PAYLOAD_SIZE);
+    tmp = os_msys_get_pkthdr(0, 0);
+    if (!tmp) {
+        return NULL;
+    }
+    oc_rep_new(tmp);
 
     oc_rep_start_root_object();
 
@@ -165,7 +170,7 @@ oc_core_add_new_device(const char *uri, const char *rt, const char *name,
     if (add_device_cb) {
         add_device_cb(data);
     }
-    if (!finalize_payload(&temp_buffer, &oc_device_info[device_count].payload)){
+    if (!finalize_payload(tmp, &oc_device_info[device_count].payload)) {
         return NULL;
     }
 
@@ -173,27 +178,28 @@ oc_core_add_new_device(const char *uri, const char *rt, const char *name,
 }
 
 void
-oc_core_platform_handler(oc_request_t *request, oc_interface_mask_t interface)
+oc_core_platform_handler(oc_request_t *req, oc_interface_mask_t interface)
 {
-    uint8_t *buffer = request->response->response_buffer->buffer;
-    uint16_t buffer_size = request->response->response_buffer->buffer_size;
+    struct oc_response_buffer *rsp_buf;
+    struct os_mbuf *buffer;
     int size;
+    char *str;
 
+    rsp_buf = req->response->response_buffer;
+    buffer = rsp_buf->buffer;
     size = oc_string_len(oc_platform_payload);
-
-    if (buffer_size < size) {
-        request->response->response_buffer->response_length = 0;
-        request->response->response_buffer->code =
-          oc_status_code(OC_STATUS_INTERNAL_SERVER_ERROR);
-        return;
-    }
+    str = oc_string(oc_platform_payload);
 
     switch (interface) {
     case OC_IF_R:
     case OC_IF_BASELINE:
-        memcpy(buffer, oc_string(oc_platform_payload), size);
-        request->response->response_buffer->response_length = size;
-        request->response->response_buffer->code = oc_status_code(OC_STATUS_OK);
+        if (os_mbuf_append(buffer, str, size)) {
+            rsp_buf->response_length = 0;
+            rsp_buf->code = oc_status_code(OC_STATUS_INTERNAL_SERVER_ERROR);
+        } else {
+            rsp_buf->response_length = size;
+            rsp_buf->code = oc_status_code(OC_STATUS_OK);
+        }
         break;
     default:
         break;
@@ -204,7 +210,7 @@ oc_string_t *
 oc_core_init_platform(const char *mfg_name, oc_core_init_platform_cb_t init_cb,
                       void *data)
 {
-    oc_string_t temp_buffer;
+    struct os_mbuf *tmp;
     oc_uuid_t uuid; /*fix uniqueness of platform id?? */
     char uuid_str[37];
 
@@ -218,11 +224,13 @@ oc_core_init_platform(const char *mfg_name, oc_core_init_platform_cb_t init_cb,
                               oc_core_platform_handler, 0, 0, 0, 0);
 
     /* Encoding platform resource payload */
-    oc_alloc_string(&temp_buffer, MAX_PLATFORM_PAYLOAD_SIZE);
-    oc_rep_new(temp_buffer.os_str, MAX_PLATFORM_PAYLOAD_SIZE);
+    tmp = os_msys_get_pkthdr(0, 0);
+    if (!tmp) {
+        return NULL;
+    }
+    oc_rep_new(tmp);
     oc_rep_start_root_object();
     oc_rep_set_string_array(root, rt, core_resources[OCF_P].types);
-
     oc_core_encode_interfaces_mask(oc_rep_object(root),
                                    core_resources[OCF_P].interfaces);
     oc_rep_set_uint(root, p, core_resources[OCF_P].properties & ~OC_PERIODIC);
@@ -237,7 +245,7 @@ oc_core_init_platform(const char *mfg_name, oc_core_init_platform_cb_t init_cb,
     if (init_cb) {
         init_cb(data);
     }
-    if (!finalize_payload(&temp_buffer, &oc_platform_payload)) {
+    if (!finalize_payload(tmp, &oc_platform_payload)) {
         return NULL;
     }
     return &oc_platform_payload;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/net/oic/src/api/oc_rep.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_rep.c b/net/oic/src/api/oc_rep.c
index 20a133a..2e70582 100644
--- a/net/oic/src/api/oc_rep.c
+++ b/net/oic/src/api/oc_rep.c
@@ -22,7 +22,7 @@
 #include "config.h"
 #include "port/oc_assert.h"
 #include "api/oc_priv.h"
-#include <tinycbor/cbor_buf_writer.h>
+#include <tinycbor/cbor_mbuf_writer.h>
 #include <tinycbor/cbor_buf_reader.h>
 
 #ifdef OC_CLIENT
@@ -32,24 +32,24 @@ static uint8_t oc_rep_objects_area[OS_MEMPOOL_BYTES(EST_NUM_REP_OBJECTS,
 #endif
 
 static const CborEncoder g_empty;
-static uint8_t *g_buf;
+static struct os_mbuf *g_outm;
 CborEncoder g_encoder, root_map, links_array;
 CborError g_err;
-struct CborBufWriter g_buf_writer;
+struct CborMbufWriter g_buf_writer;
 
 void
-oc_rep_new(uint8_t *out_payload, int size)
+oc_rep_new(struct os_mbuf *m)
 {
     g_err = CborNoError;
-    g_buf = out_payload;
-    cbor_buf_writer_init(&g_buf_writer, out_payload, size);
+    g_outm = m;
+    cbor_mbuf_writer_init(&g_buf_writer, m);
     cbor_encoder_init(&g_encoder, &g_buf_writer.enc, 0);
 }
 
 int
 oc_rep_finalize(void)
 {
-    int size = cbor_buf_writer_buffer_size(&g_buf_writer, g_buf);
+    int size = OS_MBUF_PKTLEN(g_outm);
     oc_rep_reset();
     if (g_err != CborNoError) {
         return -1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/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 36d3cbb..3ff5040 100644
--- a/net/oic/src/api/oc_ri.c
+++ b/net/oic/src/api/oc_ri.c
@@ -357,9 +357,8 @@ does_interface_support_method(oc_resource_t *resource,
 }
 
 bool
-oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
-                                 uint16_t buffer_size, int32_t *offset,
-                                 oc_endpoint_t *endpoint)
+oc_ri_invoke_coap_entity_handler(void *request, void *response,
+                                 int32_t *offset, oc_endpoint_t *endpoint)
 {
   /* Flags that capture status along various stages of processing
    *  the request.
@@ -382,9 +381,9 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
   oc_request_t request_obj;
   oc_response_buffer_t response_buffer;
   oc_response_t response_obj;
+  struct os_mbuf *m = NULL;
 
-  response_buffer.buffer = buffer;
-  response_buffer.buffer_size = buffer_size;
+  response_buffer.buffer = NULL;
   response_buffer.block_offset = offset;
   response_buffer.code = 0;
   response_buffer.response_length = 0;
@@ -473,6 +472,12 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
       bad_request = true;
   }
 
+  m = os_msys_get_pkthdr(0, 0);
+  if (!m) {
+      bad_request = true;
+  }
+  response_buffer.buffer = m;
+
   if (cur_resource && !bad_request) {
     /* Process a request against a valid resource, request payload, and
      * interface.
@@ -482,7 +487,7 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
      * points to memory allocated in the messaging layer for the "CoAP
      * Transaction" to service this request.
      */
-    oc_rep_new(buffer, buffer_size);
+    oc_rep_new(m);
 
 #ifdef OC_SECURITY
     /* If cur_resource is a coaps:// resource, then query ACL to check if
@@ -514,9 +519,14 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
   }
 
   if (bad_request) {
-    OC_LOG_ERROR("ocri: Bad request\n");
-    /* Return a 4.00 response */
-    response_buffer.code = oc_status_code(OC_STATUS_BAD_REQUEST);
+    if (!m) {
+        OC_LOG_ERROR("ocri: No bufs\n");
+        response_buffer.code = oc_status_code(OC_STATUS_SERVICE_UNAVAILABLE);
+    } else {
+        OC_LOG_ERROR("ocri: Bad request\n");
+        /* Return a 4.00 response */
+        response_buffer.code = oc_status_code(OC_STATUS_BAD_REQUEST);
+    }
     success = false;
   } else if (!cur_resource) {
     OC_LOG_ERROR("ocri: Could not find resource\n");
@@ -627,19 +637,23 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
      * of that resource with the change.
      */
     if ((method == OC_PUT || method == OC_POST) &&
-        response_buffer.code < oc_status_code(OC_STATUS_BAD_REQUEST))
-      coap_notify_observers(cur_resource, NULL, NULL);
+        response_buffer.code < oc_status_code(OC_STATUS_BAD_REQUEST)) {
+        coap_notify_observers(cur_resource, NULL, NULL);
+    }
 #endif
     if (response_buffer.response_length) {
-      coap_set_payload(response, response_buffer.buffer,
-                       response_buffer.response_length);
-      coap_set_header_content_format(response, APPLICATION_CBOR);
+        coap_set_payload(response, response_buffer.buffer,
+                         OS_MBUF_PKTLEN(response_buffer.buffer));
+        coap_set_header_content_format(response, APPLICATION_CBOR);
     }
     /* response_buffer.code at this point contains a valid CoAP status
      *  code.
      */
     coap_set_status_code(response, response_buffer.code);
   }
+  if (response_buffer.buffer) {
+      os_mbuf_free_chain(response_buffer.buffer);
+  }
   return success;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/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 071bf40..f697f5c 100644
--- a/net/oic/src/api/oc_server_api.c
+++ b/net/oic/src/api/oc_server_api.c
@@ -216,6 +216,7 @@ oc_interate_query(oc_request_t *request, char **key, int *key_len, char **value,
   return 1;
 }
 
+#if 0 /* XXXX fix this */
 void
 oc_indicate_separate_response(oc_request_t *request,
                               oc_separate_response_t *response)
@@ -277,7 +278,7 @@ oc_send_separate_response(oc_separate_response_t *handle,
         handle->active = 0;
     }
 }
-
+#endif
 int
 oc_notify_observers(oc_resource_t *resource)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/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 d4cb082..909e95f 100644
--- a/net/oic/src/messaging/coap/coap.c
+++ b/net/oic/src/messaging/coap/coap.c
@@ -446,16 +446,23 @@ coap_serialize_message(coap_packet_t *pkt, struct os_mbuf *m)
         }
     }
 
-    if (os_mbuf_append(m, pkt->payload, pkt->payload_len)) {
-        goto err_mem;
+    if (pkt->payload_m) {
+        assert(pkt->payload_len <= OS_MBUF_PKTLEN(pkt->payload_m));
+        if (pkt->payload_len < OS_MBUF_PKTLEN(pkt->payload_m)) {
+            os_mbuf_adj(pkt->payload_m,
+                        OS_MBUF_PKTLEN(pkt->payload_m) - pkt->payload_len);
+        }
+        os_mbuf_concat(m, pkt->payload_m);
     }
-
     OC_LOG_DEBUG("coap_tx: serialized %u B (header len %u, payload len %u)\n",
         OS_MBUF_PKTLEN(m), OS_MBUF_PKTLEN(m) - pkt->payload_len,
         pkt->payload_len);
 
     return 0;
 err_mem:
+    if (pkt->payload_m) {
+        os_mbuf_free_chain(pkt->payload_m);
+    }
     STATS_INC(coap_stats, oerr);
     return -1;
 }
@@ -1251,11 +1258,15 @@ coap_get_payload(coap_packet_t *pkt, const uint8_t **payload)
         return 0;
     }
 }
+
 int
-coap_set_payload(coap_packet_t *pkt, const void *payload, size_t length)
+coap_set_payload(coap_packet_t *pkt, struct os_mbuf *m, size_t length)
 {
-    pkt->payload = (uint8_t *)payload;
-    pkt->payload_len = MIN(MAX_PAYLOAD_SIZE, length);
+    pkt->payload_m = os_mbuf_dup(m);
+    if (!pkt->payload_m) {
+        return -1;
+    }
+    pkt->payload_len = MIN(OS_MBUF_PKTLEN(m), length);
 
     return pkt->payload_len;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/net/oic/src/messaging/coap/coap.h
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/coap.h b/net/oic/src/messaging/coap/coap.h
index 3dc0f49..c422a2f 100644
--- a/net/oic/src/messaging/coap/coap.h
+++ b/net/oic/src/messaging/coap/coap.h
@@ -158,6 +158,7 @@ typedef struct coap_packet {
 
     uint16_t payload_len;
     uint8_t *payload;
+    struct os_mbuf *payload_m;
 } coap_packet_t;
 
 /*
@@ -320,7 +321,7 @@ int coap_get_header_size1(coap_packet_t *, uint32_t *size);
 int coap_set_header_size1(coap_packet_t *, uint32_t size);
 
 int coap_get_payload(coap_packet_t *, const uint8_t **payload);
-int coap_set_payload(coap_packet_t *, const void *payload, size_t length);
+int coap_set_payload(coap_packet_t *, struct os_mbuf *m, size_t length);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/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 f06097b..13abeb1 100644
--- a/net/oic/src/messaging/coap/engine.c
+++ b/net/oic/src/messaging/coap/engine.c
@@ -44,12 +44,6 @@
 #include "oc_client_state.h"
 #endif
 
-extern bool oc_ri_invoke_coap_entity_handler(void *request, void *response,
-                                             uint8_t *buffer,
-                                             uint16_t buffer_size,
-                                             int32_t *offset,
-                                             oc_endpoint_t *endpoint);
-
 /*---------------------------------------------------------------------------*/
 /*- Internal API ------------------------------------------------------------*/
 /*---------------------------------------------------------------------------*/
@@ -61,7 +55,7 @@ coap_receive(oc_message_t *msg)
     static coap_packet_t message[1];
     static coap_packet_t response[1];
     static coap_transaction_t *transaction = NULL;
-    static oc_message_t *rsp;
+    struct os_mbuf *rsp;
 
     erbium_status_code = NO_ERROR;
 
@@ -150,13 +144,8 @@ coap_receive(oc_message_t *msg)
             new_offset = block_offset;
         }
 
-        rsp = oc_allocate_message();
-        if (!rsp) {
-            erbium_status_code = SERVICE_UNAVAILABLE_5_03;
-            coap_error_message = "NoFreeTraBuffer";
-        } else if (oc_ri_invoke_coap_entity_handler(message, response,
-                              rsp->data, block_size, &new_offset,
-                              &msg->endpoint)) {
+        if (oc_ri_invoke_coap_entity_handler(message, response,
+                                             &new_offset, &msg->endpoint)) {
             if (erbium_status_code == NO_ERROR) {
                 /*
                  * TODO coap_handle_blockwise(request, response,
@@ -184,17 +173,21 @@ coap_receive(oc_message_t *msg)
                                      response->payload_len, block_size);
                         if (block_offset >= response->payload_len) {
                             response->code = BAD_OPTION_4_02;
-                            coap_set_payload(response, "BlockOutOfScope", 15);
+                            rsp = os_msys_get_pkthdr(0, 0);
+                            if (rsp) {
+                                os_mbuf_copyinto(rsp, 0, "BlockOutOfScope", 15);
+                                response->payload_m = rsp;
+                                response->payload_len = 15;
+                            }
                             /* a const char str[] and sizeof(str)
                                produces larger code size */
                         } else {
                             coap_set_header_block2(response, block_num,
                                          response->payload_len - block_offset >
                                            block_size, block_size);
-                            coap_set_payload(response,
-                                             response->payload + block_offset,
-                                             MIN(response->payload_len -
-                                                 block_offset, block_size));
+                            response->payload_len = MIN(response->payload_len -
+                                                        block_offset,
+                                                        block_size);
                         } /* if(valid offset) */
 
                         /* resource provides chunk-wise data */
@@ -207,8 +200,7 @@ coap_receive(oc_message_t *msg)
                                                block_size);
 
                         if (response->payload_len > block_size) {
-                            coap_set_payload(response, response->payload,
-                                             block_size);
+                            response->payload_len = block_size;
                         }
                     } /* if(resource aware of blockwise) */
 
@@ -219,9 +211,8 @@ coap_receive(oc_message_t *msg)
 
                     coap_set_header_block2(response, 0, new_offset != -1,
                                            COAP_MAX_BLOCK_SIZE);
-                    coap_set_payload(response, response->payload,
-                                     MIN(response->payload_len,
-                                         COAP_MAX_BLOCK_SIZE));
+                    response->payload_len = MIN(response->payload_len,
+                                                COAP_MAX_BLOCK_SIZE);
                 } /* blockwise transfer handling */
             }   /* no errors/hooks */
             /* successful service callback */
@@ -233,9 +224,6 @@ coap_receive(oc_message_t *msg)
             }
             transaction->type = response->type;
         }
-        if (rsp) {
-            oc_message_unref(rsp);
-        }
     } else { // Fix this
         /* handle responses */
         if (message->type == COAP_TYPE_CON) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/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 1b7404d..7bc8537 100644
--- a/net/oic/src/messaging/coap/observe.c
+++ b/net/oic/src/messaging/coap/observe.c
@@ -193,6 +193,11 @@ coap_notify_observers(oc_resource_t *resource,
                       oc_endpoint_t *endpoint)
 {
     int num_observers = 0;
+    oc_request_t request = {};
+    oc_response_t response = {};
+    oc_response_buffer_t response_buffer;
+    struct os_mbuf *m = NULL;
+
     if (resource) {
         if (!resource->num_observers) {
             OC_LOG_DEBUG("coap_notify_observers: no observers left\n");
@@ -200,21 +205,21 @@ coap_notify_observers(oc_resource_t *resource,
         }
         num_observers = resource->num_observers;
     }
-    uint8_t buffer[COAP_MAX_BLOCK_SIZE];
-    oc_request_t request = {};
-    oc_response_t response = {};
     response.separate_response = 0;
-    oc_response_buffer_t response_buffer;
     if (!response_buf && resource && (resource->properties & OC_PERIODIC)) {
         OC_LOG_DEBUG("coap_notify_observers: Issue GET request to resource\n");
         /* performing GET on the resource */
-        response_buffer.buffer = buffer;
-        response_buffer.buffer_size = COAP_MAX_BLOCK_SIZE;
+        m = os_msys_get_pkthdr(0, 0);
+        if (!m) {
+            /* XXX count */
+            return num_observers;
+        }
+        response_buffer.buffer = m;
         response_buffer.block_offset = NULL;
         response.response_buffer = &response_buffer;
         request.resource = resource;
         request.response = &response;
-        oc_rep_new(buffer, COAP_MAX_BLOCK_SIZE);
+        oc_rep_new(m);
         resource->get_handler(&request, resource->default_interface);
         response_buf = &response_buffer;
         if (response_buf->code == OC_IGNORE) {
@@ -270,7 +275,7 @@ coap_notify_observers(oc_resource_t *resource,
                     notification->type = COAP_TYPE_CON;
                 }
                 coap_set_payload(notification, response_buf->buffer,
-                  response_buf->response_length);
+                                 OS_MBUF_PKTLEN(response_buf->buffer));
                 coap_set_status_code(notification, response_buf->code);
                 if (notification->code < BAD_REQUEST_4_00 &&
                   obs->resource->num_observers) {
@@ -290,6 +295,9 @@ coap_notify_observers(oc_resource_t *resource,
             }
         }
     }
+    if (m) {
+        os_mbuf_free_chain(m);
+    }
     return num_observers;
 }
 /*---------------------------------------------------------------------------*/

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6331a5e5/net/oic/src/messaging/coap/oc_coap.h
----------------------------------------------------------------------
diff --git a/net/oic/src/messaging/coap/oc_coap.h b/net/oic/src/messaging/coap/oc_coap.h
index dcc0352..2654d98 100644
--- a/net/oic/src/messaging/coap/oc_coap.h
+++ b/net/oic/src/messaging/coap/oc_coap.h
@@ -30,8 +30,7 @@ typedef struct oc_separate_response {
 } oc_separate_response_t;
 
 typedef struct oc_response_buffer {
-    uint8_t *buffer;
-    uint16_t buffer_size;
+    struct os_mbuf *buffer;
     int32_t *block_offset;
     uint16_t response_length;
     int code;


[48/50] incubator-mynewt-core git commit: oic; free incoming mbuf right after copying the data to oc_message, as opposed to after calling coap_receive().

Posted by st...@apache.org.
oic; free incoming mbuf right after copying the data to oc_message,
as opposed to after calling coap_receive().


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

Branch: refs/heads/sensors_branch
Commit: ed6020f68c9495f5a83b36869ab8c3dc91453295
Parents: 50ab14b
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 14 10:02:42 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/api/oc_buffer.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ed6020f6/net/oic/src/api/oc_buffer.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_buffer.c b/net/oic/src/api/oc_buffer.c
index 3e3cf8f..a2a00a8 100644
--- a/net/oic/src/api/oc_buffer.c
+++ b/net/oic/src/api/oc_buffer.c
@@ -81,6 +81,7 @@ void
 oc_message_unref(oc_message_t *message)
 {
     if (message) {
+        assert(message->ref_count > 0);
         message->ref_count--;
         if (message->ref_count == 0) {
             os_memblock_put(&oc_buffers, message);
@@ -171,6 +172,8 @@ oc_buffer_rx(struct os_event *ev)
         }
         memcpy(&msg->endpoint, OC_MBUF_ENDPOINT(m), sizeof(msg->endpoint));
         msg->length = OS_MBUF_PKTHDR(m)->omp_len;
+        os_mbuf_free_chain(m);
+        m = NULL;
 
 #ifdef OC_SECURITY
         b = m->om_data[0];
@@ -179,17 +182,17 @@ oc_buffer_rx(struct os_event *ev)
             oc_process_post(&oc_dtls_handler, oc_events[UDP_TO_DTLS_EVENT], m);
         } else {
             coap_receive(msg);
-            oc_message_unref(msg);
         }
 #else
         coap_receive(msg);
-        oc_message_unref(msg);
 #endif
 free_msg:
-        os_mbuf_free_chain(m);
         if (msg) {
             oc_message_unref(msg);
         }
+        if (m) {
+            os_mbuf_free_chain(m);
+        }
     }
 }
 


[07/50] incubator-mynewt-core git commit: nimble/gap: Fix for missing ble_hs_unlock

Posted by st...@apache.org.
nimble/gap: Fix for missing ble_hs_unlock

Use goto done; so lock can be released properly


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

Branch: refs/heads/sensors_branch
Commit: e527e305a0732d46ed8da72c19574fadab4170be
Parents: 982dfa7
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Mon Dec 12 15:15:17 2016 +0100
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e527e305/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index fcfdc57..e97888b 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -1897,7 +1897,7 @@ ble_gap_adv_start(uint8_t own_addr_type, uint8_t peer_addr_type,
 
     rc = ble_hs_id_use_addr(own_addr_type);
     if (rc != 0) {
-        return rc;
+        goto done;
     }
 
     BLE_HS_LOG(INFO, "GAP procedure initiated: advertise; ");


[14/50] incubator-mynewt-core git commit: bletiny: Add check for directed advertising type

Posted by st...@apache.org.
bletiny: Add check for directed advertising type

Connectable directed advertising event does not contain advertising
data, therefore there is no need to print it.
In the same time we add log saying about event type as this is
useful for PTS testing


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

Branch: refs/heads/sensors_branch
Commit: 4fe1cb327edf900e470d8d95d7c745df4df6f7f1
Parents: c7a5c5d
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Mon Dec 12 12:04:24 2016 +0100
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 apps/bletiny/src/main.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4fe1cb32/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index d3a9d38..c78d160 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -936,12 +936,23 @@ bletiny_gap_event(struct ble_gap_event *event, void *arg)
         return 0;
 
     case BLE_GAP_EVENT_DISC:
-        console_printf("received advertisement; event_type=%d addr_type=%d "
-                       "addr=", event->disc.event_type,
-                       event->disc.addr_type);
+        console_printf("received advertisement; event_type=%d rssi=%d "
+                       "addr_type=%d addr=", event->disc.event_type,
+                       event->disc.rssi, event->disc.addr_type);
         print_addr(event->disc.addr);
-        console_printf(" length_data=%d rssi=%d data=",
-                       event->disc.length_data, event->disc.rssi);
+
+        /*
+         * There is no adv data to print in case of connectable
+         * directed advertising
+         */
+        if (event->disc.event_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD ||
+                event->disc.event_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD) {
+                console_printf("\nConnectable directed advertising event\n");
+                return 0;
+        }
+
+        console_printf(" length_data=%d data=",
+                               event->disc.length_data);
         print_bytes(event->disc.data, event->disc.length_data);
         console_printf(" fields:\n");
         bletiny_print_adv_fields(event->disc.fields);


[06/50] incubator-mynewt-core git commit: MYNEWT-508: Add multiple advertising instances

Posted by st...@apache.org.
MYNEWT-508: Add multiple advertising instances

This commit adds the ability for the nimble controller to have
multiple advertising instances. There were many changes to the code
to support this feature. More on how to enable it is in the jira
ticket.


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

Branch: refs/heads/sensors_branch
Commit: 8c876d8c350e71026827f35868356a1af6826b46
Parents: d64f500
Author: William San Filippo <wi...@runtime.io>
Authored: Tue Dec 13 11:41:57 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 apps/bletest/src/bletest_hci.c                  | 173 ++++++-
 apps/bletest/src/bletest_priv.h                 |  20 +-
 apps/bletest/src/main.c                         | 266 +++++++++-
 hw/drivers/nimble/nrf52/src/ble_phy.c           |   2 +-
 .../controller/include/controller/ble_ll_adv.h  |  46 +-
 .../include/controller/ble_ll_sched.h           |  25 +-
 net/nimble/controller/src/ble_ll_adv.c          | 479 ++++++++++++++-----
 net/nimble/controller/src/ble_ll_conn.c         |  24 +-
 net/nimble/controller/src/ble_ll_conn_hci.c     |   7 +-
 net/nimble/controller/src/ble_ll_conn_priv.h    |   7 +-
 net/nimble/controller/src/ble_ll_hci.c          | 103 +++-
 net/nimble/controller/src/ble_ll_sched.c        | 185 +++++--
 net/nimble/controller/syscfg.yml                |  11 +-
 net/nimble/include/nimble/ble.h                 |   4 +
 net/nimble/include/nimble/hci_common.h          |   1 +
 net/nimble/include/nimble/hci_vendor.h          | 112 +++++
 net/nimble/syscfg.yml                           |  10 +
 17 files changed, 1268 insertions(+), 207 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/apps/bletest/src/bletest_hci.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/bletest_hci.c b/apps/bletest/src/bletest_hci.c
index fd3985f..528e888 100755
--- a/apps/bletest/src/bletest_hci.c
+++ b/apps/bletest/src/bletest_hci.c
@@ -21,11 +21,13 @@
 #include <string.h>
 #include "os/os.h"
 #include "bsp/bsp.h"
+#include "syscfg/syscfg.h"
 
 /* BLE */
 #include "nimble/ble.h"
 #include "nimble/ble_hci_trans.h"
 #include "nimble/hci_common.h"
+#include "nimble/hci_vendor.h"
 #include "host/ble_hs.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_hci.h"
@@ -361,6 +363,35 @@ bletest_hci_le_rd_max_datalen(void)
     return rc;
 }
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int
+bletest_hci_le_set_multi_adv_data(uint8_t *data, uint8_t len, uint8_t instance)
+{
+    uint8_t *dst;
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_MULTI_ADV_DATA_LEN];
+
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return -1;
+    }
+
+    dst = buf;
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_VENDOR, BLE_HCI_OCF_MULTI_ADV,
+                             BLE_HCI_MULTI_ADV_DATA_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    if (((data == NULL) && (len != 0)) || (len > BLE_HCI_MAX_ADV_DATA_LEN)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    memset(dst, 0, BLE_HCI_MULTI_ADV_DATA_LEN);
+    dst[0] = BLE_HCI_MULTI_ADV_DATA;
+    dst[1] = len;
+    memcpy(dst + 2, data, len);
+    dst[33] = instance;
+
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
+}
+#else
 int
 bletest_hci_le_set_adv_data(uint8_t *data, uint8_t len)
 {
@@ -371,6 +402,7 @@ bletest_hci_le_set_adv_data(uint8_t *data, uint8_t len)
     assert(rc == 0);
     return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
+#endif
 
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 int
@@ -398,6 +430,68 @@ bletest_hci_le_read_rem_used_feat(uint16_t handle)
     return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int
+bletest_hci_le_set_multi_adv_params(struct hci_multi_adv_params *adv,
+                                    uint8_t instance)
+{
+    uint8_t *dst;
+    uint16_t itvl;
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_MULTI_ADV_PARAMS_LEN];
+
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return -1;
+    }
+
+    dst = buf;
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_VENDOR, BLE_HCI_OCF_MULTI_ADV,
+                             BLE_HCI_MULTI_ADV_PARAMS_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    /* Make sure parameters are valid */
+    if ((adv->adv_itvl_min > adv->adv_itvl_max) ||
+        (adv->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) ||
+        (adv->peer_addr_type > BLE_HCI_ADV_PEER_ADDR_MAX) ||
+        (adv->adv_filter_policy > BLE_HCI_ADV_FILT_MAX) ||
+        (adv->adv_type > BLE_HCI_ADV_TYPE_MAX) ||
+        (adv->adv_channel_map == 0) ||
+        ((adv->adv_channel_map & 0xF8) != 0)) {
+        /* These parameters are not valid */
+        return -1;
+    }
+
+    /* Make sure interval is valid for advertising type. */
+    if ((adv->adv_type == BLE_HCI_ADV_TYPE_ADV_NONCONN_IND) ||
+        (adv->adv_type == BLE_HCI_ADV_TYPE_ADV_SCAN_IND)) {
+        itvl = BLE_HCI_ADV_ITVL_NONCONN_MIN;
+    } else {
+        itvl = BLE_HCI_ADV_ITVL_MIN;
+    }
+
+    /* Do not check if high duty-cycle directed */
+    if (adv->adv_type != BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
+        if ((adv->adv_itvl_min < itvl) ||
+            (adv->adv_itvl_min > BLE_HCI_ADV_ITVL_MAX)) {
+            return -1;
+        }
+    }
+
+    dst[0] = BLE_HCI_MULTI_ADV_PARAMS;
+    htole16(dst + 1, adv->adv_itvl_min);
+    htole16(dst + 3, adv->adv_itvl_max);
+    dst[5] = adv->adv_type;
+    dst[6] = adv->own_addr_type;
+    memcpy(dst + 7, adv->own_addr, BLE_DEV_ADDR_LEN);
+    dst[13] = adv->peer_addr_type;
+    memcpy(dst + 14, adv->peer_addr, BLE_DEV_ADDR_LEN);
+    dst[20] = adv->adv_channel_map;
+    dst[21] = adv->adv_filter_policy;
+    dst[22] = instance;
+    dst[23] = adv->adv_tx_pwr;
+
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
+}
+#else
 int
 bletest_hci_le_set_adv_params(struct hci_adv_params *adv)
 {
@@ -410,12 +504,13 @@ bletest_hci_le_set_adv_params(struct hci_adv_params *adv)
     }
     return rc;
 }
+#endif
 
 int
 bletest_hci_le_set_rand_addr(uint8_t *addr)
 {
     uint8_t *dst;
-    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_DATALEN_LEN];
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RAND_ADDR_LEN];
 
     dst = buf;
     ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_RAND_ADDR,
@@ -426,6 +521,29 @@ bletest_hci_le_set_rand_addr(uint8_t *addr)
     return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int
+bletest_hci_le_set_multi_rand_addr(uint8_t *addr, uint8_t instance)
+{
+    uint8_t *dst;
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_MULTI_ADV_SET_RAND_ADDR_LEN];
+
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return -1;
+    }
+
+    dst = buf;
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_VENDOR, BLE_HCI_OCF_MULTI_ADV,
+                       BLE_HCI_MULTI_ADV_SET_RAND_ADDR_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    dst[0] = BLE_HCI_MULTI_ADV_SET_RAND_ADDR;
+    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
+    dst[7] = instance;
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
+}
+#endif
+
 int
 bletest_hci_rd_rem_version(uint16_t handle)
 {
@@ -483,6 +601,28 @@ bletest_hci_le_rd_chanmap(uint16_t handle)
     return rc;
 }
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int
+bletest_hci_le_set_multi_adv_enable(uint8_t enable, uint8_t instance)
+{
+    uint8_t *dst;
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_MULTI_ADV_ENABLE_LEN];
+
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return -1;
+    }
+
+    dst = buf;
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_VENDOR, BLE_HCI_OCF_MULTI_ADV,
+                             BLE_HCI_MULTI_ADV_ENABLE_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    dst[0] = BLE_HCI_MULTI_ADV_ENABLE;
+    dst[1] = enable;
+    dst[2] = instance;
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
+}
+#else
 int
 bletest_hci_le_set_adv_enable(uint8_t enable)
 {
@@ -497,6 +637,7 @@ bletest_hci_le_set_adv_enable(uint8_t enable)
     dst[0] = enable;
     return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
+#endif
 
 int
 bletest_hci_le_set_event_mask(uint64_t event_mask)
@@ -516,6 +657,35 @@ bletest_hci_set_event_mask(uint64_t event_mask)
     return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int
+bletest_hci_le_set_multi_scan_rsp_data(uint8_t *data, uint8_t len,
+                                       uint8_t instance)
+{
+    uint8_t *dst;
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_MULTI_ADV_SCAN_RSP_DATA_LEN];
+
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return -1;
+    }
+
+    dst = buf;
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_VENDOR, BLE_HCI_OCF_MULTI_ADV,
+                             BLE_HCI_MULTI_ADV_SCAN_RSP_DATA_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    if (((data == NULL) && (len != 0)) || (len>BLE_HCI_MAX_SCAN_RSP_DATA_LEN)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    memset(dst, 0, BLE_HCI_MULTI_ADV_SCAN_RSP_DATA_LEN);
+    dst[0] = BLE_HCI_MULTI_ADV_SCAN_RSP_DATA;
+    dst[1] = len;
+    memcpy(dst + 2, data, len);
+    dst[33] = instance;
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
+}
+#else
 int
 bletest_hci_le_set_scan_rsp_data(uint8_t *data, uint8_t len)
 {
@@ -526,6 +696,7 @@ bletest_hci_le_set_scan_rsp_data(uint8_t *data, uint8_t len)
     assert(rc == 0);
     return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
+#endif
 
 int
 bletest_hci_cmd_le_set_scan_params(uint8_t scan_type, uint16_t scan_itvl,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/apps/bletest/src/bletest_priv.h
----------------------------------------------------------------------
diff --git a/apps/bletest/src/bletest_priv.h b/apps/bletest/src/bletest_priv.h
index cbb207e..7ecc8df 100644
--- a/apps/bletest/src/bletest_priv.h
+++ b/apps/bletest/src/bletest_priv.h
@@ -45,17 +45,13 @@ int bletest_hci_rd_local_feat(void);
 int bletest_hci_rd_local_supp_cmd(void);
 int bletest_hci_le_read_supp_states(void);
 int bletest_hci_le_rd_max_datalen(void);
-int bletest_hci_le_set_adv_data(uint8_t *data, uint8_t len);
-int bletest_hci_le_set_adv_params(struct hci_adv_params *adv);
 int bletest_hci_le_read_rem_used_feat(uint16_t handle);
 int bletest_hci_le_set_rand_addr(uint8_t *addr);
 int bletest_hci_rd_rem_version(uint16_t handle);
 int bletest_hci_le_set_host_chan_class(uint8_t *chanmap);
 int bletest_hci_le_rd_chanmap(uint16_t handle);
-int bletest_hci_le_set_adv_enable(uint8_t enable);
 int bletest_hci_le_set_event_mask(uint64_t event_mask);
 int bletest_hci_set_event_mask(uint64_t event_mask);
-int bletest_hci_le_set_scan_rsp_data(uint8_t *data, uint8_t len);
 int bletest_hci_le_add_to_whitelist(uint8_t *addr, uint8_t addr_type);
 int bletest_hci_le_set_scan_enable(uint8_t enable, uint8_t filter_dups);
 int bletest_hci_le_create_connection(struct hci_create_conn *hcc);
@@ -66,6 +62,22 @@ int bletest_hci_le_add_resolv_list(uint8_t *local_irk, uint8_t *peer_irk,
                                    uint8_t *peer_ident_addr, uint8_t addr_type);
 int bletest_hci_le_enable_resolv_list(uint8_t enable);
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int bletest_hci_le_set_multi_rand_addr(uint8_t *addr, uint8_t instance);
+int bletest_hci_le_set_multi_adv_data(uint8_t *data, uint8_t len,
+                                      uint8_t instance);
+int bletest_hci_le_set_multi_adv_params(struct hci_multi_adv_params *adv,
+                                        uint8_t instance);
+int bletest_hci_le_set_multi_adv_enable(uint8_t enable, uint8_t instance);
+int bletest_hci_le_set_multi_scan_rsp_data(uint8_t *data, uint8_t len,
+                                           uint8_t instance);
+#else
+int bletest_hci_le_set_adv_data(uint8_t *data, uint8_t len);
+int bletest_hci_le_set_adv_params(struct hci_adv_params *adv);
+int bletest_hci_le_set_adv_enable(uint8_t enable);
+int bletest_hci_le_set_scan_rsp_data(uint8_t *data, uint8_t len);
+#endif
+
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index dddfcce..6c6cd88 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -20,6 +20,7 @@
 #include <assert.h>
 #include <string.h>
 #include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "bsp/bsp.h"
 #include "hal/hal_bsp.h"
@@ -34,6 +35,7 @@
 #include "nimble/ble.h"
 #include "nimble/ble_hci_trans.h"
 #include "nimble/hci_common.h"
+#include "nimble/hci_vendor.h"
 #include "host/ble_hs.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_hci.h"
@@ -83,6 +85,72 @@ uint8_t g_host_adv_len;
 #define BLETEST_CFG_ADV_FILT_POLICY     (BLE_HCI_ADV_FILT_NONE)
 #define BLETEST_CFG_ADV_ADDR_RES_EN     (0)
 
+/* Multi-adv config */
+/*
+ * Number of advertising instances to start up, not including the default
+ * instance. The default instance is used to connect. If this number is greater
+ * than the number of available advertising instances, we only use the number
+ * of available advertising instances (defined by the configuration setting:
+ * BLE_MULTI_ADV_INSTANCES.
+ */
+#define BLETEST_CFG_ADV_TEST_INSTANCES     (8)
+
+struct bletest_multi_adv_interval
+{
+    uint8_t adv_type;
+    /*
+     * Note: if own addr type greater than 1, we use own addr field; otherwise
+     * we use the set multi random address call to set the random address
+     */
+    uint8_t adv_own_addr_type;
+    uint16_t adv_itvl;
+};
+
+/*
+ * NOTE: currently, these are all NONCONN_IND. Thus, must be 100 msecs or
+ * greater
+ */
+const struct bletest_multi_adv_interval
+bletest_multi_adv_instances[BLETEST_CFG_ADV_TEST_INSTANCES] = {
+    {BLE_HCI_ADV_TYPE_ADV_NONCONN_IND,
+     BLE_HCI_ADV_OWN_ADDR_PUBLIC,
+     (100000 / BLE_HCI_ADV_ITVL)},
+
+    {BLE_HCI_ADV_TYPE_ADV_SCAN_IND,
+     BLE_HCI_ADV_OWN_ADDR_RANDOM,
+     (110000 / BLE_HCI_ADV_ITVL)},
+
+    {BLE_HCI_ADV_TYPE_ADV_NONCONN_IND,
+     BLE_HCI_ADV_OWN_ADDR_RANDOM,
+     (120000 / BLE_HCI_ADV_ITVL)},
+
+    {BLE_HCI_ADV_TYPE_ADV_NONCONN_IND,
+     BLE_HCI_ADV_OWN_ADDR_PUBLIC,
+     (130000 / BLE_HCI_ADV_ITVL)},
+
+    {BLE_HCI_ADV_TYPE_ADV_SCAN_IND,
+     BLE_HCI_ADV_OWN_ADDR_MAX + 1,
+     (140000 / BLE_HCI_ADV_ITVL)},
+
+    {BLE_HCI_ADV_TYPE_ADV_NONCONN_IND,
+     BLE_HCI_ADV_OWN_ADDR_MAX + 1,
+     (150000 / BLE_HCI_ADV_ITVL)},
+
+    {BLE_HCI_ADV_TYPE_ADV_NONCONN_IND,
+     BLE_HCI_ADV_OWN_ADDR_PUBLIC,
+     (160000 / BLE_HCI_ADV_ITVL)},
+
+    {BLE_HCI_ADV_TYPE_ADV_SCAN_IND,
+     BLE_HCI_ADV_OWN_ADDR_PUBLIC,
+     (170000 / BLE_HCI_ADV_ITVL)}
+};
+
+/*
+ * Determines if own address contains random address or set through the
+ * multi-adv set random address command
+ */
+#define BLETEST_CFG_MULTI_ADV_RANDOM_OWN    (0)
+
 /* Scan config */
 #define BLETEST_CFG_SCAN_ITVL           (700000 / BLE_HCI_SCAN_ITVL)
 #define BLETEST_CFG_SCAN_WINDOW         (700000 / BLE_HCI_SCAN_ITVL)
@@ -95,7 +163,7 @@ uint8_t g_host_adv_len;
 #define BLETEST_CFG_CONN_ITVL           (128)  /* in 1.25 msec increments */
 #define BLETEST_CFG_SLAVE_LATENCY       (0)
 #define BLETEST_CFG_INIT_FILTER_POLICY  (BLE_HCI_CONN_FILT_NO_WL)
-#define BLETEST_CFG_CONN_SPVN_TMO       (1000)  /* 20 seconds */
+#define BLETEST_CFG_CONN_SPVN_TMO       (1000)  /* 10 msec increments */
 #define BLETEST_CFG_MIN_CE_LEN          (6)
 #define BLETEST_CFG_MAX_CE_LEN          (BLETEST_CFG_CONN_ITVL)
 #define BLETEST_CFG_CONN_PEER_ADDR_TYPE (BLE_HCI_CONN_PEER_ADDR_PUBLIC)
@@ -294,6 +362,175 @@ bletest_set_adv_data(uint8_t *dptr, uint8_t *addr)
 }
 
 #if (BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER)
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+void
+bletest_init_adv_instances(void)
+{
+    uint8_t i;
+    int rc;
+    uint8_t *addr;
+    uint8_t adv_len;
+    uint8_t inst_allowed;
+    uint8_t rand_addr[BLE_DEV_ADDR_LEN];
+    struct hci_multi_adv_params adv;
+
+    inst_allowed = MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES);
+    if (inst_allowed > BLETEST_CFG_ADV_TEST_INSTANCES) {
+        inst_allowed = BLETEST_CFG_ADV_TEST_INSTANCES;
+    }
+
+    /* Start up all the instances */
+    for (i = 1; i <= inst_allowed; ++i) {
+        memset(&adv, 0, sizeof(struct hci_multi_adv_params));
+
+        adv.own_addr_type = bletest_multi_adv_instances[i-1].adv_own_addr_type;
+        if (adv.own_addr_type == BLE_HCI_ADV_OWN_ADDR_PUBLIC) {
+            addr = g_dev_addr;
+        } else {
+            memcpy(rand_addr, g_dev_addr, BLE_DEV_ADDR_LEN);
+            rand_addr[5] |= 0xc0;
+            rand_addr[0] = i;
+            /*
+             * NOTE: we overload own address type with a special case
+             * to denote if we use own address or call to set multi random
+             * address.
+             */
+            if (adv.own_addr_type == BLE_HCI_ADV_OWN_ADDR_RANDOM) {
+                rc = bletest_hci_le_set_multi_rand_addr(rand_addr, i);
+                assert(rc == 0);
+                addr = rand_addr;
+            } else {
+                adv.own_addr_type = BLE_HCI_ADV_OWN_ADDR_RANDOM;
+                addr = rand_addr;
+                memcpy(adv.own_addr, addr, BLE_DEV_ADDR_LEN);
+            }
+        }
+
+        adv.adv_type = bletest_multi_adv_instances[i - 1].adv_type;
+        adv.adv_channel_map = 0x07;
+        adv.adv_filter_policy = BLE_HCI_ADV_FILT_NONE;
+        adv.peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC;
+        adv_len = bletest_set_adv_data(&g_host_adv_data[0], addr);
+
+        adv.adv_itvl_min = bletest_multi_adv_instances[i - 1].adv_itvl;
+        adv.adv_itvl_max = bletest_multi_adv_instances[i - 1].adv_itvl;
+        adv.adv_tx_pwr = -1 * i;
+
+        /* Set the advertising parameters */
+        rc = bletest_hci_le_set_multi_adv_params(&adv, i);
+        assert(rc == 0);
+
+        /* Set advertising data */
+        if (adv_len != 0) {
+            rc = bletest_hci_le_set_multi_adv_data(&g_host_adv_data[0], adv_len,
+                                                   i);
+            assert(rc == 0);
+
+            /* Set scan response data */
+            rc = bletest_hci_le_set_multi_scan_rsp_data(&g_host_adv_data[0],
+                                                        adv_len, i);
+            assert(rc == 0);
+        }
+
+        /* Set the advertising parameters */
+        rc = bletest_hci_le_set_multi_adv_enable(1, i);
+        assert(rc == 0);
+    }
+}
+
+void
+bletest_init_advertising(uint8_t instance, int8_t txpwr)
+{
+    int rc;
+    int set_peer_addr;
+    uint8_t adv_len;
+    uint8_t *addr;
+    uint8_t rand_addr[BLE_DEV_ADDR_LEN];
+    struct hci_multi_adv_params adv;
+
+    /* Make sure it is a valid instance */
+    assert(instance < BLE_LL_ADV_INSTANCES);
+
+    /* Just zero out advertising */
+    set_peer_addr = 0;
+    memset(&adv, 0, sizeof(struct hci_multi_adv_params));
+
+    /* If we are using a random address, we need to set it */
+    adv.own_addr_type = BLETEST_CFG_ADV_OWN_ADDR_TYPE;
+    if (adv.own_addr_type & 1) {
+        memcpy(rand_addr, g_dev_addr, BLE_DEV_ADDR_LEN);
+        rand_addr[5] |= 0xc0;
+        if (BLETEST_CFG_MULTI_ADV_RANDOM_OWN == 1) {
+            addr = rand_addr;
+            memcpy(adv.own_addr, addr, BLE_DEV_ADDR_LEN);
+        } else {
+            rc = bletest_hci_le_set_multi_rand_addr(rand_addr, instance);
+            assert(rc == 0);
+            addr = rand_addr;
+        }
+    } else {
+        addr = g_dev_addr;
+    }
+
+    /* Set advertising parameters */
+    adv.adv_type = BLETEST_CFG_ADV_TYPE;
+    adv.adv_channel_map = 0x07;
+    adv.adv_filter_policy = BLETEST_CFG_ADV_FILT_POLICY;
+    if ((adv.adv_filter_policy & 1) || (BLETEST_CFG_ADV_ADDR_RES_EN == 1)) {
+        set_peer_addr = 1;
+    }
+    adv.peer_addr_type = BLETEST_CFG_ADV_PEER_ADDR_TYPE;
+    if ((adv.adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) ||
+        (adv.adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD)) {
+        set_peer_addr = 1;
+        adv_len = 0;
+    } else {
+        adv_len = bletest_set_adv_data(&g_host_adv_data[0], addr);
+    }
+
+    /* Not allowed for multi-adv command */
+    if (adv.own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
+        assert(0);
+    }
+
+    if (set_peer_addr) {
+        memcpy(adv.peer_addr, g_bletest_cur_peer_addr, BLE_DEV_ADDR_LEN);
+        if (adv.peer_addr_type == BLE_HCI_ADV_PEER_ADDR_RANDOM) {
+            adv.peer_addr[5] |= 0xc0;
+        }
+    }
+
+    console_printf("Trying to connect to %x.%x.%x.%x.%x.%x\n",
+                   adv.peer_addr[0], adv.peer_addr[1], adv.peer_addr[2],
+                   adv.peer_addr[3], adv.peer_addr[4], adv.peer_addr[5]);
+
+    if (adv.adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
+        adv.adv_itvl_min = 0;
+        adv.adv_itvl_max = 0;
+    } else {
+        adv.adv_itvl_min = BLETEST_CFG_ADV_ITVL;
+        adv.adv_itvl_max = BLETEST_CFG_ADV_ITVL; /* Advertising interval */
+    }
+
+    adv.adv_tx_pwr = txpwr;
+
+    /* Set the advertising parameters */
+    rc = bletest_hci_le_set_multi_adv_params(&adv, instance);
+    assert(rc == 0);
+
+    /* Set advertising data */
+    if (adv_len != 0) {
+        rc = bletest_hci_le_set_multi_adv_data(&g_host_adv_data[0], adv_len,
+                                               instance);
+        assert(rc == 0);
+
+        /* Set scan response data */
+        rc = bletest_hci_le_set_multi_scan_rsp_data(&g_host_adv_data[0],adv_len,
+                                                    instance);
+        assert(rc == 0);
+    }
+}
+#else
 void
 bletest_init_advertising(void)
 {
@@ -387,7 +624,8 @@ bletest_init_advertising(void)
         assert(rc == 0);
     }
 }
-#endif
+#endif  /* MULTI_ADV SUPPORT */
+#endif  /* BLETEST_ROLE_ADVERTISER */
 
 #if (BLETEST_CFG_ROLE == BLETEST_ROLE_SCANNER)
 void
@@ -733,13 +971,22 @@ bletest_execute_advertiser(void)
                 /* restart initiating */
                 g_bletest_cur_peer_addr[5] += 1;
                 g_dev_addr[5] += 1;
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+                bletest_init_advertising(0,0);
+                bletest_hci_le_set_multi_adv_enable(1, 0);
+#else
                 bletest_init_advertising();
                 bletest_hci_le_set_adv_enable(1);
+#endif
             }
         } else {
             /* If we failed to start advertising we should keep trying */
             if (ble_ll_adv_enabled() == 0) {
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+                bletest_hci_le_set_multi_adv_enable(1, 0);
+#else
                 bletest_hci_le_set_adv_enable(1);
+#endif
             }
         }
     }
@@ -817,7 +1064,7 @@ bletest_execute_advertiser(void)
     }
 #endif /* XXX: throughput test */
 }
-#endif
+#endif /* XXX: BLETEST_ROLE_ADVERTISER */
 
 /**
  * Main bletest function. Called by the task timer every 50 msecs.
@@ -888,8 +1135,16 @@ bletest_task_handler(void *arg)
 #if (BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER)
     /* Initialize the advertiser */
     console_printf("Starting BLE test task as advertiser\n");
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    /* Start up all advertising instances except default one */
+    bletest_init_adv_instances();
+
+    /* Start advertising on instance 0 at 0 dbm */
+    bletest_init_advertising(0, 0);
+#else
     bletest_init_advertising();
 #endif
+#endif
 
 #if (BLETEST_CFG_ROLE == BLETEST_ROLE_SCANNER)
     /* Initialize the scanner */
@@ -985,9 +1240,14 @@ bletest_task_handler(void *arg)
 
     /* Begin advertising if we are an advertiser */
 #if (BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER)
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    rc = bletest_hci_le_set_multi_adv_enable(1, 0);
+    assert(rc == 0);
+#else
     rc = bletest_hci_le_set_adv_enable(1);
     assert(rc == 0);
 #endif
+#endif
 
     bletest_timer_cb(NULL);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/hw/drivers/nimble/nrf52/src/ble_phy.c
----------------------------------------------------------------------
diff --git a/hw/drivers/nimble/nrf52/src/ble_phy.c b/hw/drivers/nimble/nrf52/src/ble_phy.c
index 5d41322..92941f2 100644
--- a/hw/drivers/nimble/nrf52/src/ble_phy.c
+++ b/hw/drivers/nimble/nrf52/src/ble_phy.c
@@ -376,7 +376,7 @@ ble_phy_tx_end_isr(void)
     assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
 
     /* Log the event */
-    ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, (g_ble_phy_tx_buf[0] >> 8) & 0xFF,
+    ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, g_ble_phy_data.phy_tx_pyld_len,
                was_encrypted, txstart);
 
     /* Clear events and clear interrupt on disabled event */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/include/controller/ble_ll_adv.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_adv.h b/net/nimble/controller/include/controller/ble_ll_adv.h
index 74eabdf..298f4bf 100644
--- a/net/nimble/controller/include/controller/ble_ll_adv.h
+++ b/net/nimble/controller/include/controller/ble_ll_adv.h
@@ -20,10 +20,15 @@
 #ifndef H_BLE_LL_ADV_
 #define H_BLE_LL_ADV_
 
+#include "syscfg/syscfg.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/* The number of advertising instances */
+#define BLE_LL_ADV_INSTANCES    (MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES + 1)
+
 /*
  * ADV event timing
  *      T_advEvent = advInterval + advDelay
@@ -101,34 +106,43 @@ extern "C" {
 #define BLE_ADV_SCAN_IND_MAX_LEN        (37)
 
 /*---- HCI ----*/
+struct ble_ll_adv_sm;
+struct ble_ll_conn_sm;
+
 /* Start an advertiser */
 int ble_ll_adv_start_req(uint8_t adv_chanmask, uint8_t adv_type,
                          uint8_t *init_addr, uint16_t adv_itvl, void *handle);
 
 /* Start or stop advertising */
-int ble_ll_adv_set_enable(uint8_t *cmd);
+int ble_ll_adv_set_enable(uint8_t *cmd, uint8_t instance);
 
 /* Set advertising data */
-int ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t len);
+int ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t instance);
 
 /* Set scan response data */
-int ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t len);
+int ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t instance);
 
 /* Set advertising parameters */
-int ble_ll_adv_set_adv_params(uint8_t *cmd);
+int ble_ll_adv_set_adv_params(uint8_t *cmd, uint8_t instance, int is_multi);
 
 /* Read advertising channel power */
 int ble_ll_adv_read_txpwr(uint8_t *rspbuf, uint8_t *rsplen);
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int ble_ll_adv_multi_adv_cmd(uint8_t *cmd, uint8_t cmdlen, uint8_t *rspbuf,
+                             uint8_t *rsplen);
+#endif
+
 /*---- API used by BLE LL ----*/
-/* Returns the event allocated to send the connection complete event */
-uint8_t *ble_ll_adv_get_conn_comp_ev(void);
+/* Send the connection complete event */
+void ble_ll_adv_send_conn_comp_ev(struct ble_ll_conn_sm *connsm,
+                                  struct ble_mbuf_hdr *rxhdr);
 
 /* Returns local resolvable private address */
-uint8_t *ble_ll_adv_get_local_rpa(void);
+uint8_t *ble_ll_adv_get_local_rpa(struct ble_ll_adv_sm *advsm);
 
 /* Returns peer resolvable private address */
-uint8_t *ble_ll_adv_get_peer_rpa(void);
+uint8_t *ble_ll_adv_get_peer_rpa(struct ble_ll_adv_sm *advsm);
 
 /* Called to initialize advertising functionality. */
 void ble_ll_adv_init(void);
@@ -136,9 +150,6 @@ void ble_ll_adv_init(void);
 /* Called when LL wait for response timer expires in advertising state */
 void ble_ll_adv_wfr_timer_exp(void);
 
-/* Called to initialize advertising functionality. */
-void ble_ll_adv_init(void);
-
 /* Called to reset the advertiser. */
 void ble_ll_adv_reset(void);
 
@@ -155,14 +166,17 @@ void ble_ll_adv_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf,
 /* Boolean function denoting whether or not the whitelist can be changed */
 int ble_ll_adv_can_chg_whitelist(void);
 
-/* Called when a connection request has been received at the link layer */
-int ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr);
-
 /* Called when an advertising event has been scheduled */
-void ble_ll_adv_scheduled(uint32_t sch_start);
+void ble_ll_adv_scheduled(struct ble_ll_adv_sm *, uint32_t sch_start);
+
+/*
+ * Called when an advertising event has been removed from the scheduler
+ * without being run.
+ */
+void ble_ll_adv_event_rmvd_from_sched(struct ble_ll_adv_sm *advsm);
 
 /* Called to halt currently running advertising event */
-void ble_ll_adv_halt(void);
+void ble_ll_adv_halt(struct ble_ll_adv_sm *advsm);
 
 /* Called to determine if advertising is enabled */
 uint8_t ble_ll_adv_enabled(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/include/controller/ble_ll_sched.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_sched.h b/net/nimble/controller/include/controller/ble_ll_sched.h
index cda4f40..3a05cfe 100644
--- a/net/nimble/controller/include/controller/ble_ll_sched.h
+++ b/net/nimble/controller/include/controller/ble_ll_sched.h
@@ -28,6 +28,25 @@ extern "C" {
 #define BLE_LL_SCHED_USECS_PER_SLOT (1250)
 
 /*
+ * Worst case time needed for scheduled advertising item. This is the longest
+ * possible time to receive a scan request and send a scan response (with the
+ * appropriate IFS time between them). This number is calculated using the
+ * following formula: IFS + SCAN_REQ + IFS + SCAN_RSP = 150 + 176 + 150 + 376.
+ * Note: worst case time to tx adv, rx scan req and send scan rsp is 1228 usecs.
+ * This assumes maximum sized advertising PDU and scan response PDU.
+ *
+ * For connectable advertising events no scan request is allowed. In this case
+ * we just need to receive a connect request PDU: IFS + CONNECT_REQ = 150 + 352.
+ * Note: worst-case is 376 + 150 + 352 = 878 usecs
+ *
+ * NOTE: The advertising PDU transmit time is NOT included here since we know
+ * how long that will take (worst-case is 376 usecs).
+ */
+#define BLE_LL_SCHED_ADV_MAX_USECS          (852)
+#define BLE_LL_SCHED_DIRECT_ADV_MAX_USECS   (502)
+#define BLE_LL_SCHED_MAX_ADV_PDU_USECS      (376)
+
+/*
  * This is the number of slots needed to transmit and receive a maximum
  * size PDU, including an IFS time before each. The actual time is
  * 2120 usecs for tx/rx and 150 for IFS = 4540 usecs.
@@ -85,7 +104,11 @@ int ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm);
 int ble_ll_sched_adv_new(struct ble_ll_sched_item *sch);
 
 /* Reschedule an advertising event */
-int ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch);
+int ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch, uint32_t *start,
+                                uint32_t max_delay_ticks);
+
+/* Reschedule and advertising pdu */
+int ble_ll_sched_adv_resched_pdu(struct ble_ll_sched_item *sch);
 
 /* Reschedule a connection that had previously been scheduled or that is over */
 int ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm * connsm);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/src/ble_ll_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_adv.c b/net/nimble/controller/src/ble_ll_adv.c
index 61a2291..0b7b470 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -27,10 +27,12 @@
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
+#include "nimble/hci_vendor.h"
 #include "nimble/ble_hci_trans.h"
 #include "controller/ble_phy.h"
 #include "controller/ble_hw.h"
 #include "controller/ble_ll.h"
+#include "controller/ble_ll_hci.h"
 #include "controller/ble_ll_adv.h"
 #include "controller/ble_ll_sched.h"
 #include "controller/ble_ll_scan.h"
@@ -69,7 +71,8 @@
  */
 struct ble_ll_adv_sm
 {
-    uint8_t enabled;
+    uint8_t adv_enabled;
+    uint8_t adv_instance;
     uint8_t adv_type;
     uint8_t adv_len;
     uint8_t adv_chanmask;
@@ -83,6 +86,7 @@ struct ble_ll_adv_sm
     uint8_t adv_directed;           /* note: can be 1 bit */
     uint8_t adv_txadd;              /* note: can be 1 bit */
     uint8_t adv_rxadd;              /* note: can be 1 bit */
+    int8_t adv_txpwr;
     uint16_t adv_itvl_min;
     uint16_t adv_itvl_max;
     uint32_t adv_itvl_usecs;
@@ -99,31 +103,17 @@ struct ble_ll_adv_sm
     uint8_t *conn_comp_ev;
     struct os_event adv_txdone_ev;
     struct ble_ll_sched_item adv_sch;
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    uint8_t adv_random_addr[BLE_DEV_ADDR_LEN];
+#endif
 };
 
 /* The advertising state machine global object */
-struct ble_ll_adv_sm g_ble_ll_adv_sm;
+struct ble_ll_adv_sm g_ble_ll_adv_sm[BLE_LL_ADV_INSTANCES];
+struct ble_ll_adv_sm *g_ble_ll_cur_adv_sm;
 
 static void ble_ll_adv_done(struct ble_ll_adv_sm *advsm);
 
-/*
- * Worst case time needed for scheduled advertising item. This is the longest
- * possible time to receive a scan request and send a scan response (with the
- * appropriate IFS time between them). This number is calculated using the
- * following formula: IFS + SCAN_REQ + IFS + SCAN_RSP = 150 + 176 + 150 + 376.
- * Note: worst case time to tx adv, rx scan req and send scan rsp is 1228 usecs.
- * This assumes maximum sized advertising PDU and scan response PDU.
- *
- * For connectable advertising events no scan request is allowed. In this case
- * we just need to receive a connect request PDU: IFS + CONNECT_REQ = 150 + 352.
- * Note: worst-case is 376 + 150 + 352 = 878 usecs
- *
- * NOTE: The advertising PDU transmit time is NOT included here since we know
- * how long that will take (worst-case is 376 usecs).
- */
-#define BLE_LL_ADV_SCHED_MAX_USECS          (852)
-#define BLE_LL_ADV_DIRECT_SCHED_MAX_USECS   (502)
-
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 /**
  * Called to change advertisers ADVA and INITA (for directed advertisements)
@@ -209,6 +199,30 @@ ble_ll_adv_first_chan(struct ble_ll_adv_sm *advsm)
 }
 
 /**
+ * Calculate the final channel that we should advertise upon when we start
+ * an advertising event.
+ *
+ * @param advsm
+ *
+ * @return uint8_t The number of the final channel usable for advertising.
+ */
+static uint8_t
+ble_ll_adv_final_chan(struct ble_ll_adv_sm *advsm)
+{
+    uint8_t adv_chan;
+
+    if (advsm->adv_chanmask & 0x04) {
+        adv_chan = BLE_PHY_ADV_CHAN_START + 2;
+    } else if (advsm->adv_chanmask & 0x02) {
+        adv_chan = BLE_PHY_ADV_CHAN_START + 1;
+    } else {
+        adv_chan = BLE_PHY_ADV_CHAN_START;
+    }
+
+    return adv_chan;
+}
+
+/**
  * Create the advertising PDU
  *
  * @param advsm Pointer to advertisement state machine
@@ -353,12 +367,34 @@ ble_ll_adv_tx_done(void *arg)
 {
     struct ble_ll_adv_sm *advsm;
 
+    /* XXX: for now, reset power to max after advertising */
+    ble_phy_txpwr_set(MYNEWT_VAL(BLE_LL_TX_PWR_DBM));
+
     advsm = (struct ble_ll_adv_sm *)arg;
     os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
 
-    ble_ll_log(BLE_LL_LOG_ID_ADV_TXDONE, ble_ll_state_get(), 0, 0);
+    ble_ll_log(BLE_LL_LOG_ID_ADV_TXDONE, ble_ll_state_get(),
+               advsm->adv_instance, 0);
 
     ble_ll_state_set(BLE_LL_STATE_STANDBY);
+
+    /* We no longer have a current state machine */
+    g_ble_ll_cur_adv_sm = NULL;
+}
+
+/*
+ * Called when an advertising event has been removed from the scheduler
+ * without being run.
+ */
+void
+ble_ll_adv_event_rmvd_from_sched(struct ble_ll_adv_sm *advsm)
+{
+    /*
+     * Need to set advertising channel to final chan so new event gets
+     * scheduled.
+     */
+    advsm->adv_chan = ble_ll_adv_final_chan(advsm);
+    os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
 }
 
 /**
@@ -383,6 +419,12 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
     /* Get the state machine for the event */
     advsm = (struct ble_ll_adv_sm *)sch->cb_arg;
 
+    /* Set the current advertiser */
+    g_ble_ll_cur_adv_sm = advsm;
+
+    /* Set the power */
+    ble_phy_txpwr_set(advsm->adv_txpwr);
+
     /* Set channel */
     rc = ble_phy_setchan(advsm->adv_chan, 0, 0);
     assert(rc == 0);
@@ -471,11 +513,11 @@ ble_ll_adv_set_sched(struct ble_ll_adv_sm *advsm, int sched_new)
     switch (advsm->adv_type) {
     case BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD:
     case BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD:
-        max_usecs += BLE_LL_ADV_DIRECT_SCHED_MAX_USECS;
+        max_usecs += BLE_LL_SCHED_DIRECT_ADV_MAX_USECS;
         break;
     case BLE_HCI_ADV_TYPE_ADV_IND:
     case BLE_HCI_ADV_TYPE_ADV_SCAN_IND:
-        max_usecs += BLE_LL_ADV_SCHED_MAX_USECS;
+        max_usecs += BLE_LL_SCHED_ADV_MAX_USECS;
         break;
     default:
         break;
@@ -508,11 +550,12 @@ ble_ll_adv_set_sched(struct ble_ll_adv_sm *advsm, int sched_new)
  * and is only called when a scheduled item executes but advertising is still
  * running.
  *
+ * Context: Interrupt
  */
 void
-ble_ll_adv_halt(void)
+ble_ll_adv_halt(struct ble_ll_adv_sm *advsm)
 {
-    ble_ll_adv_tx_done(&g_ble_ll_adv_sm);
+    ble_ll_adv_tx_done(advsm);
 }
 
 /**
@@ -526,24 +569,36 @@ ble_ll_adv_halt(void)
  * @return int
  */
 int
-ble_ll_adv_set_adv_params(uint8_t *cmd)
+ble_ll_adv_set_adv_params(uint8_t *cmd, uint8_t instance, int is_multi)
 {
     uint8_t adv_type;
     uint8_t adv_filter_policy;
     uint8_t adv_chanmask;
     uint8_t own_addr_type;
     uint8_t peer_addr_type;
+    uint8_t offset;
     uint16_t adv_itvl_min;
     uint16_t adv_itvl_max;
     uint16_t min_itvl;
     struct ble_ll_adv_sm *advsm;
 
     /* If already enabled, we return an error */
-    advsm = &g_ble_ll_adv_sm;
-    if (advsm->enabled) {
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    advsm = &g_ble_ll_adv_sm[instance];
+    if (advsm->adv_enabled) {
         return BLE_ERR_CMD_DISALLOWED;
     }
 
+    /* Add offset if this is a multi-advertising command */
+    if (is_multi) {
+        offset = 6;
+    } else {
+        offset = 0;
+    }
+
     /* Make sure intervals are OK (along with advertising type */
     adv_itvl_min = le16toh(cmd);
     adv_itvl_max = le16toh(cmd + 2);
@@ -553,7 +608,7 @@ ble_ll_adv_set_adv_params(uint8_t *cmd)
      * Get the filter policy now since we will ignore it if we are doing
      * directed advertising
      */
-    adv_filter_policy = cmd[14];
+    adv_filter_policy = cmd[14 + offset];
 
     /* Assume min interval based on low duty cycle/indirect advertising */
     min_itvl = BLE_LL_ADV_ITVL_MIN;
@@ -580,6 +635,7 @@ ble_ll_adv_set_adv_params(uint8_t *cmd)
         min_itvl = BLE_LL_ADV_ITVL_NONCONN_MIN;
         break;
     default:
+        /* This will cause an invalid parameter error */
         min_itvl = 0xFFFF;
         break;
     }
@@ -593,17 +649,44 @@ ble_ll_adv_set_adv_params(uint8_t *cmd)
 
     /* Check own and peer address type */
     own_addr_type =  cmd[5];
-    peer_addr_type = cmd[6];
+    peer_addr_type = cmd[6 + offset];
 
     if ((own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) ||
         (peer_addr_type > BLE_HCI_ADV_PEER_ADDR_MAX)) {
         return BLE_ERR_INV_HCI_CMD_PARMS;
     }
 
+    /* Deal with multi-advertising command specific*/
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    if (is_multi) {
+        /* Get and check advertising power */
+        advsm->adv_txpwr = cmd[22];
+        if (advsm->adv_txpwr > 20) {
+            return BLE_ERR_INV_HCI_CMD_PARMS;
+        }
+
+        /* Get own address if it is there. */
+        if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
+            return BLE_ERR_INV_HCI_CMD_PARMS;
+        } else {
+            if (own_addr_type == BLE_HCI_ADV_OWN_ADDR_RANDOM) {
+                /* Use this random address if set in own address */
+                if (ble_ll_is_valid_random_addr(cmd + 6)) {
+                    memcpy(advsm->adv_random_addr, cmd + 6, BLE_DEV_ADDR_LEN);
+                }
+            }
+        }
+    } else {
+        advsm->adv_txpwr = MYNEWT_VAL(BLE_LL_TX_PWR_DBM);
+    }
+#else
+    advsm->adv_txpwr = MYNEWT_VAL(BLE_LL_TX_PWR_DBM);
+#endif
+
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
         /* Copy peer address */
-        memcpy(advsm->peer_addr, cmd + 7, BLE_DEV_ADDR_LEN);
+        memcpy(advsm->peer_addr, cmd + 7 + offset, BLE_DEV_ADDR_LEN);
 
         /* Reset RPA timer so we generate a new RPA */
         advsm->adv_rpa_timer = os_time_get();
@@ -616,7 +699,7 @@ ble_ll_adv_set_adv_params(uint8_t *cmd)
 #endif
 
     /* There are only three adv channels, so check for any outside the range */
-    adv_chanmask = cmd[13];
+    adv_chanmask = cmd[13 + offset];
     if (((adv_chanmask & 0xF8) != 0) || (adv_chanmask == 0)) {
         return BLE_ERR_INV_HCI_CMD_PARMS;
     }
@@ -650,7 +733,7 @@ ble_ll_adv_sm_stop(struct ble_ll_adv_sm *advsm)
 {
     os_sr_t sr;
 
-    if (advsm->enabled) {
+    if (advsm->adv_enabled) {
         /* Remove any scheduled advertising items */
         ble_ll_sched_rmv_elem(&advsm->adv_sch);
 
@@ -672,7 +755,7 @@ ble_ll_adv_sm_stop(struct ble_ll_adv_sm *advsm)
         }
 
         /* Disable advertising */
-        advsm->enabled = 0;
+        advsm->adv_enabled = 0;
     }
 }
 
@@ -702,9 +785,15 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
      * is why I chose command disallowed.
      */
     if (advsm->own_addr_type == BLE_HCI_ADV_OWN_ADDR_RANDOM) {
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+        if (!ble_ll_is_valid_random_addr(advsm->adv_random_addr)) {
+            return BLE_ERR_CMD_DISALLOWED;
+        }
+#else
         if (!ble_ll_is_valid_random_addr(g_random_addr)) {
             return BLE_ERR_CMD_DISALLOWED;
         }
+#endif
     }
 
     /*
@@ -733,7 +822,11 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
         addr = g_dev_addr;
         advsm->adv_txadd = 0;
     } else {
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+        addr = advsm->adv_random_addr;
+#else
         addr = g_random_addr;
+#endif
         advsm->adv_txadd = 1;
     }
     memcpy(advsm->adva, addr, BLE_DEV_ADDR_LEN);
@@ -753,7 +846,7 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
 #endif
 
     /* Set flag telling us that advertising is enabled */
-    advsm->enabled = 1;
+    advsm->adv_enabled = 1;
 
     /* Determine the advertising interval we will use */
     if (advsm->adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
@@ -779,12 +872,8 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
 }
 
 void
-ble_ll_adv_scheduled(uint32_t sch_start)
+ble_ll_adv_scheduled(struct ble_ll_adv_sm *advsm, uint32_t sch_start)
 {
-    struct ble_ll_adv_sm *advsm;
-
-    advsm = &g_ble_ll_adv_sm;
-
     /* The event start time is when we start transmission of the adv PDU */
     advsm->adv_event_start_time = sch_start +
         os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
@@ -826,19 +915,23 @@ ble_ll_adv_read_txpwr(uint8_t *rspbuf, uint8_t *rsplen)
  * @return int
  */
 int
-ble_ll_adv_set_enable(uint8_t *cmd)
+ble_ll_adv_set_enable(uint8_t *cmd, uint8_t instance)
 {
     int rc;
     uint8_t enable;
     struct ble_ll_adv_sm *advsm;
 
-    advsm = &g_ble_ll_adv_sm;
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    advsm = &g_ble_ll_adv_sm[instance];
 
-    rc = 0;
+    rc = BLE_ERR_SUCCESS;
     enable = cmd[0];
     if (enable == 1) {
         /* If already enabled, do nothing */
-        if (!advsm->enabled) {
+        if (!advsm->adv_enabled) {
             /* Start the advertising state machine */
             rc = ble_ll_adv_sm_start(advsm);
         }
@@ -860,7 +953,7 @@ ble_ll_adv_set_enable(uint8_t *cmd)
  * @return int
  */
 int
-ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t len)
+ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t instance)
 {
     uint8_t datalen;
     struct ble_ll_adv_sm *advsm;
@@ -871,12 +964,16 @@ ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t len)
         return BLE_ERR_INV_HCI_CMD_PARMS;
     }
 
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
     /* Copy the new data into the advertising structure. */
-    advsm = &g_ble_ll_adv_sm;
+    advsm = &g_ble_ll_adv_sm[instance];
     advsm->scan_rsp_len = datalen;
     memcpy(advsm->scan_rsp_data, cmd + 1, datalen);
 
-    return 0;
+    return BLE_ERR_SUCCESS;
 }
 
 /**
@@ -889,7 +986,7 @@ ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t len)
  * @return int 0: success; BLE_ERR_INV_HCI_CMD_PARMS otherwise.
  */
 int
-ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t len)
+ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t instance)
 {
     uint8_t datalen;
     struct ble_ll_adv_sm *advsm;
@@ -900,14 +997,93 @@ ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t len)
         return BLE_ERR_INV_HCI_CMD_PARMS;
     }
 
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
     /* Copy the new data into the advertising structure. */
-    advsm = &g_ble_ll_adv_sm;
+    advsm = &g_ble_ll_adv_sm[instance];
     advsm->adv_len = datalen;
     memcpy(advsm->adv_data, cmd + 1, datalen);
 
-    return 0;
+    return BLE_ERR_SUCCESS;
 }
 
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+int
+ble_ll_adv_set_random_addr(uint8_t *addr, uint8_t instance)
+{
+    struct ble_ll_adv_sm *advsm;
+
+    if (instance >= BLE_LL_ADV_INSTANCES) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+    advsm = &g_ble_ll_adv_sm[instance];
+    memcpy(advsm->adv_random_addr, addr, BLE_DEV_ADDR_LEN);
+    return BLE_ERR_SUCCESS;
+}
+
+/**
+ * Process the multi-advertising command
+ *
+ * NOTE: the command length was already checked to make sure it is non-zero.
+ *
+ * @param cmdbuf    Pointer to command buffer
+ * @param cmdlen    The length of the command data
+ * @param rspbuf    Pointer to response buffer
+ * @param rsplen    Pointer to response length
+ *
+ * @return int
+ */
+int
+ble_ll_adv_multi_adv_cmd(uint8_t *cmdbuf, uint8_t cmdlen, uint8_t *rspbuf,
+                         uint8_t *rsplen)
+{
+    int rc;
+    uint8_t subcmd;
+
+    /* NOTE: the command length includes the sub command byte */
+    rc = BLE_ERR_INV_HCI_CMD_PARMS;
+    subcmd = cmdbuf[0];
+    ++cmdbuf;
+    switch (subcmd) {
+    case BLE_HCI_MULTI_ADV_PARAMS:
+        if (cmdlen == BLE_HCI_MULTI_ADV_PARAMS_LEN) {
+            rc = ble_ll_adv_set_adv_params(cmdbuf, cmdbuf[21], 1);
+        }
+        break;
+    case BLE_HCI_MULTI_ADV_DATA:
+        if (cmdlen == BLE_HCI_MULTI_ADV_DATA_LEN) {
+            rc = ble_ll_adv_set_adv_data(cmdbuf, cmdbuf[32]);
+        }
+        break;
+    case BLE_HCI_MULTI_ADV_SCAN_RSP_DATA:
+        if (cmdlen == BLE_HCI_MULTI_ADV_SCAN_RSP_DATA_LEN) {
+            rc = ble_ll_adv_set_scan_rsp_data(cmdbuf, cmdbuf[32]);
+        }
+        break;
+    case BLE_HCI_MULTI_ADV_SET_RAND_ADDR:
+        if (cmdlen == BLE_HCI_MULTI_ADV_SET_RAND_ADDR_LEN) {
+            rc = ble_ll_adv_set_random_addr(cmdbuf, cmdbuf[6]);
+        }
+        break;
+    case BLE_HCI_MULTI_ADV_ENABLE:
+        if (cmdlen == BLE_HCI_MULTI_ADV_ENABLE_LEN) {
+            rc = ble_ll_adv_set_enable(cmdbuf, cmdbuf[1]);
+        }
+        break;
+    default:
+        rc = BLE_ERR_UNKNOWN_HCI_CMD;
+        break;
+    }
+
+    rspbuf[0] = subcmd;
+    *rsplen = 1;
+
+    return rc;
+}
+#endif
+
 /**
  * Called when the LL receives a scan request or connection request
  *
@@ -935,7 +1111,7 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
     struct os_mbuf *scan_rsp;
 
     /* See if adva in the request (scan or connect) matches what we sent */
-    advsm = &g_ble_ll_adv_sm;
+    advsm = g_ble_ll_cur_adv_sm;
     rxbuf = rxpdu->om_data;
     adva = rxbuf + BLE_LL_PDU_HDR_LEN + BLE_DEV_ADDR_LEN;
     if (memcmp(advsm->adva, adva, BLE_DEV_ADDR_LEN)) {
@@ -995,7 +1171,7 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
     if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) {
         scan_rsp = ble_ll_adv_scan_rsp_pdu_make(advsm);
         if (scan_rsp) {
-            ble_phy_set_txend_cb(ble_ll_adv_tx_done, &g_ble_ll_adv_sm);
+            ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
             rc = ble_phy_tx(scan_rsp, BLE_PHY_TRANSITION_NONE);
             if (!rc) {
                 ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_SCAN_RSP_TXD;
@@ -1018,8 +1194,9 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
  *
  * @return 0: no connection started. 1: connection started
  */
-int
-ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
+static int
+ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr,
+                        struct ble_ll_adv_sm *advsm)
 {
     int valid;
     uint8_t pyld_len;
@@ -1030,14 +1207,12 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
     uint8_t *inita;
     uint8_t *ident_addr;
     uint32_t endtime;
-    struct ble_ll_adv_sm *advsm;
 
     /* Check filter policy. */
     valid = 0;
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     resolved = BLE_MBUF_HDR_RESOLVED(hdr);
 #endif
-    advsm = &g_ble_ll_adv_sm;
     inita = rxbuf + BLE_LL_PDU_HDR_LEN;
     if (hdr->rxinfo.flags & BLE_MBUF_HDR_F_DEVMATCH) {
 
@@ -1092,7 +1267,7 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
         /* Try to start slave connection. If successful, stop advertising */
         pyld_len = rxbuf[1] & BLE_ADV_PDU_HDR_LEN_MASK;
         endtime = hdr->beg_cputime + BLE_TX_DUR_USECS_M(pyld_len);
-        valid = ble_ll_conn_slave_start(rxbuf, endtime, addr_type);
+        valid = ble_ll_conn_slave_start(rxbuf, endtime, addr_type, hdr);
         if (valid) {
             ble_ll_adv_sm_stop(advsm);
         }
@@ -1126,11 +1301,18 @@ int
 ble_ll_adv_rx_isr_end(uint8_t pdu_type, struct os_mbuf *rxpdu, int crcok)
 {
     int rc;
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    struct ble_mbuf_hdr *rxhdr;
+#endif
 
     rc = -1;
     if (rxpdu == NULL) {
-        ble_ll_adv_tx_done(&g_ble_ll_adv_sm);
+        ble_ll_adv_tx_done(g_ble_ll_cur_adv_sm);
     } else {
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+        rxhdr = BLE_MBUF_HDR_PTR(rxpdu);
+        rxhdr->rxinfo.advsm = g_ble_ll_cur_adv_sm;
+#endif
         if (crcok) {
             if ((pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) ||
                 (pdu_type == BLE_ADV_PDU_TYPE_CONNECT_REQ)) {
@@ -1138,6 +1320,11 @@ ble_ll_adv_rx_isr_end(uint8_t pdu_type, struct os_mbuf *rxpdu, int crcok)
                 rc = ble_ll_adv_rx_req(pdu_type, rxpdu);
             }
         }
+
+        if (rc) {
+            /* We no longer have a current state machine */
+            g_ble_ll_cur_adv_sm = NULL;
+        }
     }
 
     if (rc) {
@@ -1164,13 +1351,20 @@ void
 ble_ll_adv_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
 {
     int adv_event_over;
+    struct ble_ll_adv_sm *advsm;
+
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    advsm = (struct ble_ll_adv_sm *)hdr->rxinfo.advsm;
+#else
+    advsm = &g_ble_ll_adv_sm[0];
+#endif
 
     /*
      * It is possible that advertising was stopped and a packet plcaed on the
      * LL receive packet queue. In this case, just ignore the received packet
      * as the advertising state machine is no longer "valid"
      */
-    if (!g_ble_ll_adv_sm.enabled) {
+    if (!advsm->adv_enabled) {
         return;
     }
 
@@ -1184,7 +1378,7 @@ ble_ll_adv_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
     adv_event_over = 1;
     if (BLE_MBUF_HDR_CRC_OK(hdr)) {
         if (ptype == BLE_ADV_PDU_TYPE_CONNECT_REQ) {
-            if (ble_ll_adv_conn_req_rxd(rxbuf, hdr)) {
+            if (ble_ll_adv_conn_req_rxd(rxbuf, hdr, advsm)) {
                 adv_event_over = 0;
             }
         } else {
@@ -1196,7 +1390,7 @@ ble_ll_adv_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
     }
 
     if (adv_event_over) {
-        ble_ll_adv_done(&g_ble_ll_adv_sm);
+        ble_ll_adv_done(advsm);
     }
 }
 
@@ -1223,7 +1417,7 @@ ble_ll_adv_rx_isr_start(uint8_t pdu_type)
     rc = -1;
 
     /* If we get a scan request we must tell the phy to go from rx to tx */
-    advsm = &g_ble_ll_adv_sm;
+    advsm = g_ble_ll_cur_adv_sm;
     if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) {
         /* Only accept scan requests if we are indirect adv or scan adv */
         if ((advsm->adv_type == BLE_HCI_ADV_TYPE_ADV_SCAN_IND) ||
@@ -1262,13 +1456,17 @@ ble_ll_adv_rx_isr_start(uint8_t pdu_type)
 static void
 ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
 {
+    int rc;
+    int resched_pdu;
     uint8_t mask;
     uint8_t final_adv_chan;
     int32_t delta_t;
     uint32_t itvl;
+    uint32_t tick_itvl;
     uint32_t start_time;
+    uint32_t max_delay_ticks;
 
-    assert(advsm->enabled);
+    assert(advsm->adv_enabled);
 
     /* Remove the element from the schedule if it is still there. */
     ble_ll_sched_rmv_elem(&advsm->adv_sch);
@@ -1279,13 +1477,7 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
      * packet was sent on the last channel, it means we are done with this
      * event.
      */
-    if (advsm->adv_chanmask & 0x04) {
-        final_adv_chan = BLE_PHY_ADV_CHAN_START + 2;
-    } else if (advsm->adv_chanmask & 0x02) {
-        final_adv_chan = BLE_PHY_ADV_CHAN_START + 1;
-    } else {
-        final_adv_chan = BLE_PHY_ADV_CHAN_START;
-    }
+    final_adv_chan = ble_ll_adv_final_chan(advsm);
 
     if (advsm->adv_chan == final_adv_chan) {
         /* Check if we need to resume scanning */
@@ -1294,12 +1486,19 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
         /* This event is over. Set adv channel to first one */
         advsm->adv_chan = ble_ll_adv_first_chan(advsm);
 
-        /* Calculate start time of next advertising event */
+        /*
+         * Calculate start time of next advertising event. NOTE: we do not
+         * add the random advDelay as the scheduling code will do that.
+         */
         itvl = advsm->adv_itvl_usecs;
         if (advsm->adv_type != BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
-            itvl += rand() % (BLE_LL_ADV_DELAY_MS_MAX * 1000);
+            max_delay_ticks =
+                os_cputime_usecs_to_ticks(BLE_LL_ADV_DELAY_MS_MAX * 1000);
+        } else {
+            max_delay_ticks = 0;
         }
-        advsm->adv_event_start_time += os_cputime_usecs_to_ticks(itvl);
+        tick_itvl = os_cputime_usecs_to_ticks(itvl);
+        advsm->adv_event_start_time += tick_itvl;
         advsm->adv_pdu_start_time = advsm->adv_event_start_time;
 
         /*
@@ -1311,18 +1510,17 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
 
         delta_t = (int32_t)(start_time - os_cputime_get32());
         if (delta_t < 0) {
-            /* Calculate start time of next advertising event */
+            /*
+             * NOTE: we just the same interval that we calculated earlier.
+             * No real need to keep recalculating a new interval.
+             */
             while (delta_t < 0) {
-                itvl = advsm->adv_itvl_usecs;
-                if (advsm->adv_type != BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
-                    itvl += rand() % (BLE_LL_ADV_DELAY_MS_MAX * 1000);
-                }
-                itvl = os_cputime_usecs_to_ticks(itvl);
-                advsm->adv_event_start_time += itvl;
+                advsm->adv_event_start_time += tick_itvl;
                 advsm->adv_pdu_start_time = advsm->adv_event_start_time;
-                delta_t += (int32_t)itvl;
+                delta_t += (int32_t)tick_itvl;
             }
         }
+        resched_pdu = 0;
     } else {
         /*
          * Move to next advertising channel. If not in the mask, just
@@ -1341,6 +1539,8 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
          */
         advsm->adv_pdu_start_time = os_cputime_get32() +
             os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
+
+        resched_pdu = 1;
     }
 
     /*
@@ -1350,9 +1550,9 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
     if (advsm->adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
         if (advsm->adv_pdu_start_time >= advsm->adv_dir_hd_end_time) {
             /* Disable advertising */
-            advsm->enabled = 0;
+            advsm->adv_enabled = 0;
             ble_ll_conn_comp_event_send(NULL, BLE_ERR_DIR_ADV_TMO,
-                                        advsm->conn_comp_ev);
+                                        advsm->conn_comp_ev, advsm);
             advsm->conn_comp_ev = NULL;
             ble_ll_scan_chk_resume();
             return;
@@ -1371,7 +1571,19 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
      * In the unlikely event we cant reschedule this, just post a done
      * event and we will reschedule the next advertising event
      */
-    if (ble_ll_sched_adv_reschedule(&advsm->adv_sch)) {
+    if (resched_pdu) {
+        rc = ble_ll_sched_adv_resched_pdu(&advsm->adv_sch);
+    } else {
+        rc = ble_ll_sched_adv_reschedule(&advsm->adv_sch, &start_time,
+                                         max_delay_ticks);
+        if (!rc) {
+            advsm->adv_event_start_time = start_time;
+            advsm->adv_pdu_start_time = start_time;
+        }
+    }
+
+    if (rc) {
+        advsm->adv_chan = final_adv_chan;
         os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
     }
 }
@@ -1395,31 +1607,64 @@ ble_ll_adv_can_chg_whitelist(void)
     int rc;
     struct ble_ll_adv_sm *advsm;
 
-    advsm = &g_ble_ll_adv_sm;
-    if (advsm->enabled && (advsm->adv_filter_policy != BLE_HCI_ADV_FILT_NONE)) {
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    int i;
+
+    rc = 1;
+    for (i = 0; i < BLE_LL_ADV_INSTANCES; ++i) {
+        advsm = &g_ble_ll_adv_sm[i];
+        if (advsm->adv_enabled &&
+            (advsm->adv_filter_policy != BLE_HCI_ADV_FILT_NONE)) {
+            rc = 0;
+            break;
+        }
+    }
+#else
+    advsm = &g_ble_ll_adv_sm[0];
+    if (advsm->adv_enabled &&
+        (advsm->adv_filter_policy != BLE_HCI_ADV_FILT_NONE)) {
         rc = 0;
     } else {
         rc = 1;
     }
+#endif
 
     return rc;
 }
 
 /**
- * Returns the event allocated to send the connection complete event
+ * Sends the connection complete event when advertising a connection starts.
  *
  * @return uint8_t* Pointer to event buffer
  */
-uint8_t *
-ble_ll_adv_get_conn_comp_ev(void)
+void
+ble_ll_adv_send_conn_comp_ev(struct ble_ll_conn_sm *connsm,
+                             struct ble_mbuf_hdr *rxhdr)
 {
     uint8_t *evbuf;
+    struct ble_ll_adv_sm *advsm;
 
-    evbuf = g_ble_ll_adv_sm.conn_comp_ev;
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    advsm = (struct ble_ll_adv_sm *)rxhdr->rxinfo.advsm;
+    evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
+    if (evbuf) {
+        evbuf[0] = BLE_HCI_EVCODE_LE_META;
+        evbuf[1] = 5;   /* Length of event, including sub-event code */
+        evbuf[2] = BLE_HCI_LE_SUBEV_ADV_STATE_CHG;
+        evbuf[3] = advsm->adv_instance;
+        evbuf[4] = 0x00;    /* status code */
+        htole16(evbuf + 5, connsm->conn_handle);
+        ble_ll_hci_event_send(evbuf);
+    }
+#else
+    advsm = &g_ble_ll_adv_sm[0];
+#endif
+
+    evbuf = advsm->conn_comp_ev;
     assert(evbuf != NULL);
-    g_ble_ll_adv_sm.conn_comp_ev = NULL;
+    advsm->conn_comp_ev = NULL;
 
-    return evbuf;
+    ble_ll_conn_comp_event_send(connsm, BLE_ERR_SUCCESS, evbuf, advsm);
 }
 
 /**
@@ -1429,12 +1674,9 @@ ble_ll_adv_get_conn_comp_ev(void)
  * @return uint8_t*
  */
 uint8_t *
-ble_ll_adv_get_local_rpa(void)
+ble_ll_adv_get_local_rpa(struct ble_ll_adv_sm *advsm)
 {
     uint8_t *rpa;
-    struct ble_ll_adv_sm *advsm;
-
-    advsm = &g_ble_ll_adv_sm;
 
     rpa = NULL;
     if (advsm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
@@ -1450,12 +1692,8 @@ ble_ll_adv_get_local_rpa(void)
  * @return uint8_t*
  */
 uint8_t *
-ble_ll_adv_get_peer_rpa(void)
+ble_ll_adv_get_peer_rpa(struct ble_ll_adv_sm *advsm)
 {
-    struct ble_ll_adv_sm *advsm;
-
-    advsm = &g_ble_ll_adv_sm;
-
     /* XXX: should this go into IRK list or connection? */
     return advsm->adv_rpa;
 }
@@ -1469,7 +1707,7 @@ void
 ble_ll_adv_wfr_timer_exp(void)
 {
     ble_phy_disable();
-    ble_ll_adv_tx_done(&g_ble_ll_adv_sm);
+    ble_ll_adv_tx_done(g_ble_ll_cur_adv_sm);
 }
 
 /**
@@ -1481,21 +1719,27 @@ ble_ll_adv_wfr_timer_exp(void)
 void
 ble_ll_adv_reset(void)
 {
+    int i;
     struct ble_ll_adv_sm *advsm;
-    advsm = &g_ble_ll_adv_sm;
 
-    /* Stop advertising state machine */
-    ble_ll_adv_sm_stop(advsm);
+    for (i = 0; i < BLE_LL_ADV_INSTANCES; ++i) {
+        /* Stop advertising state machine */
+        advsm = &g_ble_ll_adv_sm[i];
+        ble_ll_adv_sm_stop(advsm);
+    }
 
     /* re-initialize the advertiser state machine */
     ble_ll_adv_init();
 }
 
-/* Called to determine if advertising is enabled */
+/* Called to determine if advertising is enabled.
+ *
+ * NOTE: this currently only applies to the default advertising index.
+ */
 uint8_t
 ble_ll_adv_enabled(void)
 {
-    return g_ble_ll_adv_sm.enabled;
+    return g_ble_ll_adv_sm[0].adv_enabled;
 }
 
 /**
@@ -1505,18 +1749,23 @@ ble_ll_adv_enabled(void)
 void
 ble_ll_adv_init(void)
 {
+    int i;
     struct ble_ll_adv_sm *advsm;
 
     /* Set default advertising parameters */
-    advsm = &g_ble_ll_adv_sm;
-    memset(advsm, 0, sizeof(struct ble_ll_adv_sm));
+    for (i = 0; i < BLE_LL_ADV_INSTANCES; ++i) {
+        advsm = &g_ble_ll_adv_sm[i];
 
-    advsm->adv_itvl_min = BLE_HCI_ADV_ITVL_DEF;
-    advsm->adv_itvl_max = BLE_HCI_ADV_ITVL_DEF;
-    advsm->adv_chanmask = BLE_HCI_ADV_CHANMASK_DEF;
+        memset(advsm, 0, sizeof(struct ble_ll_adv_sm));
 
-    /* Initialize advertising tx done event */
-    advsm->adv_txdone_ev.ev_cb = ble_ll_adv_event_done;
-    advsm->adv_txdone_ev.ev_arg = advsm;
+        advsm->adv_instance = i;
+        advsm->adv_itvl_min = BLE_HCI_ADV_ITVL_DEF;
+        advsm->adv_itvl_max = BLE_HCI_ADV_ITVL_DEF;
+        advsm->adv_chanmask = BLE_HCI_ADV_CHANMASK_DEF;
+
+        /* Initialize advertising tx done event */
+        advsm->adv_txdone_ev.ev_cb = ble_ll_adv_event_done;
+        advsm->adv_txdone_ev.ev_arg = advsm;
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/src/ble_ll_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn.c b/net/nimble/controller/src/ble_ll_conn.c
index e67408d..c72b0b4 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -1630,7 +1630,7 @@ ble_ll_conn_end(struct ble_ll_conn_sm *connsm, uint8_t ble_err)
     if (ble_err) {
         if (ble_err == BLE_ERR_UNK_CONN_ID) {
             evbuf = ble_ll_init_get_conn_comp_ev();
-            ble_ll_conn_comp_event_send(connsm, ble_err, evbuf);
+            ble_ll_conn_comp_event_send(connsm, ble_err, evbuf, NULL);
         } else {
             ble_ll_disconn_comp_event_send(connsm, ble_err);
         }
@@ -1804,7 +1804,8 @@ ble_ll_conn_next_event(struct ble_ll_conn_sm *connsm)
  * @ return 0: connection NOT created. 1: connection created
  */
 static int
-ble_ll_conn_created(struct ble_ll_conn_sm *connsm, uint32_t endtime)
+ble_ll_conn_created(struct ble_ll_conn_sm *connsm, uint32_t endtime,
+                    struct ble_mbuf_hdr *rxhdr)
 {
     int rc;
     uint8_t *evbuf;
@@ -1867,11 +1868,11 @@ ble_ll_conn_created(struct ble_ll_conn_sm *connsm, uint32_t endtime)
             }
         }
         if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-            evbuf = ble_ll_adv_get_conn_comp_ev();
+            ble_ll_adv_send_conn_comp_ev(connsm, rxhdr);
         } else {
             evbuf = ble_ll_init_get_conn_comp_ev();
+            ble_ll_conn_comp_event_send(connsm, BLE_ERR_SUCCESS, evbuf, NULL);
         }
-        ble_ll_conn_comp_event_send(connsm, BLE_ERR_SUCCESS, evbuf);
     }
 
     return rc;
@@ -1898,10 +1899,6 @@ ble_ll_conn_event_end(struct os_event *ev)
     /* Check if we need to resume scanning */
     ble_ll_scan_chk_resume();
 
-    /* Log event end */
-    ble_ll_log(BLE_LL_LOG_ID_CONN_EV_END, 0, connsm->event_cntr,
-               connsm->ce_end_time);
-
     /* If we have transmitted the terminate IND successfully, we are done */
     if ((connsm->csmflags.cfbit.terminate_ind_txd) ||
         (connsm->csmflags.cfbit.terminate_ind_rxd)) {
@@ -1970,6 +1967,10 @@ ble_ll_conn_event_end(struct os_event *ev)
         }
     }
 
+    /* Log event end */
+    ble_ll_log(BLE_LL_LOG_ID_CONN_EV_END, connsm->conn_handle,
+               connsm->event_cntr, connsm->conn_sch.start_time);
+
     /* If we have completed packets, send an event */
     ble_ll_conn_num_comp_pkts_event_send(connsm);
 }
@@ -2222,7 +2223,7 @@ ble_ll_init_rx_pkt_in(uint8_t *rxbuf, struct ble_mbuf_hdr *ble_hdr)
         ble_ll_scan_sm_stop(0);
         payload_len = rxbuf[1] & BLE_ADV_PDU_HDR_LEN_MASK;
         endtime = ble_hdr->beg_cputime + BLE_TX_DUR_USECS_M(payload_len);
-        ble_ll_conn_created(connsm, endtime);
+        ble_ll_conn_created(connsm, endtime, NULL);
     } else {
         ble_ll_scan_chk_resume();
     }
@@ -3054,7 +3055,8 @@ ble_ll_conn_set_global_chanmap(uint8_t num_used_chans, uint8_t *chanmap)
  * @return 0: connection not started; 1 connecton started
  */
 int
-ble_ll_conn_slave_start(uint8_t *rxbuf, uint32_t conn_req_end, uint8_t pat)
+ble_ll_conn_slave_start(uint8_t *rxbuf, uint32_t conn_req_end, uint8_t pat,
+                        struct ble_mbuf_hdr *rxhdr)
 {
     int rc;
     uint32_t temp;
@@ -3148,7 +3150,7 @@ ble_ll_conn_slave_start(uint8_t *rxbuf, uint32_t conn_req_end, uint8_t pat)
     /* Set initial schedule callback */
     connsm->conn_sch.sched_cb = ble_ll_conn_event_start_cb;
 
-    rc = ble_ll_conn_created(connsm, conn_req_end);
+    rc = ble_ll_conn_created(connsm, conn_req_end, rxhdr);
     if (!rc) {
         SLIST_REMOVE(&g_ble_ll_conn_active_list, connsm, ble_ll_conn_sm, act_sle);
         STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/src/ble_ll_conn_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn_hci.c b/net/nimble/controller/src/ble_ll_conn_hci.c
index 0ce49f8..99b8665 100644
--- a/net/nimble/controller/src/ble_ll_conn_hci.c
+++ b/net/nimble/controller/src/ble_ll_conn_hci.c
@@ -156,7 +156,7 @@ ble_ll_conn_req_pdu_make(struct ble_ll_conn_sm *connsm)
  */
 void
 ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
-                            uint8_t *evbuf)
+                            uint8_t *evbuf, struct ble_ll_adv_sm *advsm)
 {
     uint8_t peer_addr_type;
     uint8_t enabled;
@@ -181,6 +181,7 @@ ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
 
         if (connsm) {
             htole16(evbuf + 4, connsm->conn_handle);
+
             evbuf[6] = connsm->conn_role - 1;
             peer_addr_type = connsm->peer_addr_type;
 
@@ -194,7 +195,7 @@ ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
                         rpa = NULL;
                     }
                 } else {
-                    rpa = ble_ll_adv_get_local_rpa();
+                    rpa = ble_ll_adv_get_local_rpa(advsm);
                 }
                 if (rpa) {
                     memcpy(evdata, rpa, BLE_DEV_ADDR_LEN);
@@ -204,7 +205,7 @@ ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
                     if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
                         rpa = ble_ll_scan_get_peer_rpa();
                     } else {
-                        rpa = ble_ll_adv_get_peer_rpa();
+                        rpa = ble_ll_adv_get_peer_rpa(advsm);
                     }
                     memcpy(evdata + 6, rpa, BLE_DEV_ADDR_LEN);
                 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/src/ble_ll_conn_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn_priv.h b/net/nimble/controller/src/ble_ll_conn_priv.h
index f1abbdc..eea5439 100644
--- a/net/nimble/controller/src/ble_ll_conn_priv.h
+++ b/net/nimble/controller/src/ble_ll_conn_priv.h
@@ -92,6 +92,8 @@ extern struct os_mempool g_ble_ll_hci_ev_pool;
 struct ble_ll_len_req;
 struct hci_create_conn;
 struct ble_mbuf_hdr;
+struct ble_ll_adv_sm;
+
 void ble_ll_conn_sm_new(struct ble_ll_conn_sm *connsm);
 void ble_ll_conn_end(struct ble_ll_conn_sm *connsm, uint8_t ble_err);
 void ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
@@ -104,7 +106,8 @@ void ble_ll_conn_datalen_update(struct ble_ll_conn_sm *connsm,
                                 struct ble_ll_len_req *req);
 
 /* Advertising interface */
-int ble_ll_conn_slave_start(uint8_t *rxbuf, uint32_t conn_req_end, uint8_t pat);
+int ble_ll_conn_slave_start(uint8_t *rxbuf, uint32_t conn_req_end, uint8_t pat,
+                            struct ble_mbuf_hdr *rxhdr);
 
 /* Link Layer interface */
 void ble_ll_conn_module_init(void);
@@ -136,7 +139,7 @@ int ble_ll_conn_hci_param_reply(uint8_t *cmdbuf, int negative_reply);
 int ble_ll_conn_create_cancel(void);
 void ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm);
 void ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
-                                 uint8_t *evbuf);
+                                 uint8_t *evbuf, struct ble_ll_adv_sm *advsm);
 void ble_ll_conn_timeout(struct ble_ll_conn_sm *connsm, uint8_t ble_err);
 int ble_ll_conn_hci_chk_conn_params(uint16_t itvl_min, uint16_t itvl_max,
                                     uint16_t latency, uint16_t spvn_tmo);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/src/ble_ll_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci.c b/net/nimble/controller/src/ble_ll_hci.c
index 7b549ad..7f3bb09 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -24,6 +24,7 @@
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
+#include "nimble/hci_vendor.h"
 #include "nimble/ble_hci_trans.h"
 #include "controller/ble_hw.h"
 #include "controller/ble_ll_adv.h"
@@ -505,6 +506,87 @@ ble_ll_hci_le_cmd_send_cmd_status(uint16_t ocf)
 }
 
 /**
+ * Returns the vendor specific capabilities
+ *
+ * @param rspbuf Pointer to response buffer
+ * @param rsplen Length of response buffer
+ *
+ * @return int BLE error code
+ */
+static int
+ble_ll_hci_vendor_caps(uint8_t *rspbuf, uint8_t *rsplen)
+{
+    /* Clear all bytes */
+    memset(rspbuf, 0, 14);
+
+    /* Fill out the ones we support */
+    rspbuf[0] = BLE_LL_ADV_INSTANCES;
+    rspbuf[9] = 0x60;
+    *rsplen = 14;
+    return BLE_ERR_SUCCESS;
+}
+
+/**
+ * Process a vendor command sent from the host to the controller. The HCI
+ * command has a 3 byte command header followed by data. The header is:
+ *  -> opcode (2 bytes)
+ *  -> Length of parameters (1 byte; does include command header bytes).
+ *
+ * @param cmdbuf Pointer to command buffer. Points to start of command header.
+ * @param ocf    Opcode command field.
+ * @param *rsplen Pointer to length of response
+ *
+ * @return int  This function returns a BLE error code. If a command status
+ *              event should be returned as opposed to command complete,
+ *              256 gets added to the return value.
+ */
+static int
+ble_ll_hci_vendor_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
+{
+    int rc;
+    uint8_t cmdlen;
+    uint8_t *rspbuf;
+
+    /* Assume error; if all pass rc gets set to 0 */
+    rc = BLE_ERR_INV_HCI_CMD_PARMS;
+
+    /* Get length from command */
+    cmdlen = cmdbuf[sizeof(uint16_t)];
+
+    /*
+     * The command response pointer points into the same buffer as the
+     * command data itself. That is fine, as each command reads all the data
+     * before crafting a response.
+     */
+    rspbuf = cmdbuf + BLE_HCI_EVENT_CMD_COMPLETE_MIN_LEN;
+
+    /* Move past HCI command header */
+    cmdbuf += BLE_HCI_CMD_HDR_LEN;
+
+    switch (ocf) {
+    case BLE_HCI_OCF_VENDOR_CAPS:
+        if (cmdlen == 0) {
+            ble_ll_hci_vendor_caps(rspbuf, rsplen);
+            rc = BLE_ERR_SUCCESS;
+        }
+        break;
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    case BLE_HCI_OCF_MULTI_ADV:
+        if (cmdlen > 0) {
+            rc = ble_ll_adv_multi_adv_cmd(cmdbuf, cmdlen, rspbuf, rsplen);
+        }
+        break;
+#endif
+    default:
+        rc = BLE_ERR_UNKNOWN_HCI_CMD;
+        break;
+    }
+
+    /* XXX: for now, all vendor commands return a command complete */
+    return rc;
+}
+
+/**
  * Process a LE command sent from the host to the controller. The HCI command
  * has a 3 byte command header followed by data. The header is:
  *  -> opcode (2 bytes)
@@ -534,7 +616,7 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
 
     /* Check the length to make sure it is valid */
     cmdlen = g_ble_hci_le_cmd_len[ocf];
-    if ((cmdlen != 0xFF) && (len != cmdlen)) {
+    if (len != cmdlen) {
         goto ll_hci_le_cmd_exit;
     }
 
@@ -562,27 +644,19 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
         rc = ble_ll_set_random_addr(cmdbuf);
         break;
     case BLE_HCI_OCF_LE_SET_ADV_PARAMS:
-        /* Length should be one byte */
-        rc = ble_ll_adv_set_adv_params(cmdbuf);
+        rc = ble_ll_adv_set_adv_params(cmdbuf, 0, 0);
         break;
     case BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR:
         rc = ble_ll_adv_read_txpwr(rspbuf, rsplen);
         break;
     case BLE_HCI_OCF_LE_SET_ADV_DATA:
-        if (len > 0) {
-            --len;
-            rc = ble_ll_adv_set_adv_data(cmdbuf, len);
-        }
+        rc = ble_ll_adv_set_adv_data(cmdbuf, 0);
         break;
     case BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA:
-        if (len > 0) {
-            --len;
-            rc = ble_ll_adv_set_scan_rsp_data(cmdbuf, len);
-        }
+        rc = ble_ll_adv_set_scan_rsp_data(cmdbuf, 0);
         break;
     case BLE_HCI_OCF_LE_SET_ADV_ENABLE:
-        /* Length should be one byte */
-        rc = ble_ll_adv_set_enable(cmdbuf);
+        rc = ble_ll_adv_set_enable(cmdbuf, 0);
         break;
     case BLE_HCI_OCF_LE_SET_SCAN_ENABLE:
         rc = ble_ll_scan_set_enable(cmdbuf);
@@ -951,6 +1025,9 @@ ble_ll_hci_cmd_proc(struct os_event *ev)
     case BLE_HCI_OGF_LE:
         rc = ble_ll_hci_le_cmd_proc(cmdbuf, ocf, &rsplen);
         break;
+    case BLE_HCI_OGF_VENDOR:
+        rc = ble_ll_hci_vendor_cmd_proc(cmdbuf, ocf, &rsplen);
+        break;
     default:
         /* XXX: Need to support other OGF. For now, return unsupported */
         rc = BLE_ERR_UNKNOWN_HCI_CMD;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/src/ble_ll_sched.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_sched.c b/net/nimble/controller/src/ble_ll_sched.c
index b3cc52d..99b387c 100644
--- a/net/nimble/controller/src/ble_ll_sched.c
+++ b/net/nimble/controller/src/ble_ll_sched.c
@@ -32,6 +32,11 @@
 /* XXX: this is temporary. Not sure what I want to do here */
 struct hal_timer g_ble_ll_sched_timer;
 
+#define BLE_LL_SCHED_ADV_WORST_CASE_USECS       \
+    (BLE_LL_SCHED_MAX_ADV_PDU_USECS + BLE_LL_IFS + BLE_LL_SCHED_ADV_MAX_USECS \
+     + XCVR_TX_SCHED_DELAY_USECS)
+
+
 #if (BLE_LL_SCHED_DEBUG == 1)
 int32_t g_ble_ll_sched_max_late;
 #endif
@@ -182,8 +187,7 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
     TAILQ_FOREACH(entry, &g_ble_ll_sched_q, link) {
         if (ble_ll_sched_is_overlap(sch, entry)) {
             /* Only insert if this element is older than all that we overlap */
-            if ((entry->sched_type == BLE_LL_SCHED_TYPE_ADV) ||
-                !ble_ll_conn_is_lru((struct ble_ll_conn_sm *)sch->cb_arg,
+            if (!ble_ll_conn_is_lru((struct ble_ll_conn_sm *)sch->cb_arg,
                                     (struct ble_ll_conn_sm *)entry->cb_arg)) {
                 start_overlap = NULL;
                 rc = -1;
@@ -196,7 +200,7 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
                 end_overlap = entry;
             }
         } else {
-            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+            if ((int32_t)(sch->end_time - entry->start_time) <= 0) {
                 rc = 0;
                 TAILQ_INSERT_BEFORE(entry, sch, link);
                 break;
@@ -218,6 +222,9 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
         if (entry->sched_type == BLE_LL_SCHED_TYPE_CONN) {
             tmp = (struct ble_ll_conn_sm *)entry->cb_arg;
             ble_ll_event_send(&tmp->conn_ev_end);
+        } else {
+            assert(entry->sched_type == BLE_LL_SCHED_TYPE_ADV);
+            ble_ll_adv_event_rmvd_from_sched((struct ble_ll_adv_sm *)entry->cb_arg);
         }
 
         TAILQ_REMOVE(&g_ble_ll_sched_q, entry, link);
@@ -315,7 +322,7 @@ ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, uint32_t adv_rxend,
             sch->end_time = earliest_end;
 
             /* We can insert if before entry in list */
-            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+            if ((int32_t)(sch->end_time - entry->start_time) <= 0) {
                 if ((earliest_start - initial_start) <= itvl_t) {
                     rc = 0;
                     TAILQ_INSERT_BEFORE(entry, sch, link);
@@ -401,7 +408,7 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
         while (1) {
             next_sch = entry->link.tqe_next;
             /* Insert if event ends before next starts */
-            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+            if ((int32_t)(sch->end_time - entry->start_time) <= 0) {
                 rc = 0;
                 TAILQ_INSERT_BEFORE(entry, sch, link);
                 break;
@@ -443,14 +450,17 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
 {
     int rc;
     os_sr_t sr;
+    uint8_t ll_state;
     int32_t ticks;
     uint32_t ce_end_time;
     uint32_t adv_start;
     uint32_t duration;
     struct ble_ll_sched_item *entry;
+    struct ble_ll_sched_item *orig;
 
     /* Get length of schedule item */
     duration = sch->end_time - sch->start_time;
+    orig = sch;
 
     OS_ENTER_CRITICAL(sr);
 
@@ -458,7 +468,8 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
      * If we are currently in a connection, we add one slot time to the
      * earliest start so we can end the connection reasonably.
      */
-    if (ble_ll_state_get() == BLE_LL_STATE_CONNECTION) {
+    ll_state = ble_ll_state_get();
+    if (ll_state == BLE_LL_STATE_CONNECTION) {
         ticks = (int32_t)os_cputime_usecs_to_ticks(BLE_LL_SCHED_MAX_TXRX_SLOT);
         ce_end_time = ble_ll_conn_get_ce_end_time();
         if ((int32_t)(ce_end_time - sch->start_time) < ticks) {
@@ -467,6 +478,18 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
         sch->start_time = ce_end_time;
         sch->end_time = ce_end_time + duration;
     }
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    else if ((ll_state == BLE_LL_STATE_ADV) && (BLE_LL_ADV_INSTANCES > 1)) {
+        /*
+         * Since we currently dont know how long this item might be scheduled
+         * for we add what we think the worst-case time for the advertising
+         * scheduled item to be over. We add in a IFS for good measure.
+         */
+        sch->start_time += BLE_LL_SCHED_MAX_ADV_PDU_USECS + BLE_LL_IFS +
+            BLE_LL_SCHED_ADV_MAX_USECS + XCVR_TX_SCHED_DELAY_USECS;
+        sch->end_time = sch->start_time + duration;
+    }
+#endif
 
     entry = ble_ll_sched_insert_if_empty(sch);
     if (!entry) {
@@ -476,7 +499,7 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
         os_cputime_timer_stop(&g_ble_ll_sched_timer);
         TAILQ_FOREACH(entry, &g_ble_ll_sched_q, link) {
             /* We can insert if before entry in list */
-            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+            if ((int32_t)(sch->end_time - entry->start_time) <= 0) {
                 rc = 0;
                 TAILQ_INSERT_BEFORE(entry, sch, link);
                 break;
@@ -504,7 +527,7 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
         sch = TAILQ_FIRST(&g_ble_ll_sched_q);
     }
 
-    ble_ll_adv_scheduled(adv_start);
+    ble_ll_adv_scheduled((struct ble_ll_adv_sm *)orig->cb_arg, adv_start);
 
     OS_EXIT_CRITICAL(sr);
 
@@ -520,65 +543,158 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
 }
 
 int
-ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch)
+ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch, uint32_t *start,
+                            uint32_t max_delay_ticks)
 {
     int rc;
     os_sr_t sr;
+    uint32_t orig_start;
+    uint32_t duration;
+    uint32_t rand_ticks;
     struct ble_ll_sched_item *entry;
     struct ble_ll_sched_item *next_sch;
+    struct ble_ll_sched_item *before;
+    struct ble_ll_sched_item *start_overlap;
+    struct ble_ll_sched_item *end_overlap;
+
+    /* Get length of schedule item */
+    duration = sch->end_time - sch->start_time;
+
+    /* Add maximum randomization delay to end */
+    rand_ticks = max_delay_ticks;
+    sch->end_time += max_delay_ticks;
 
+    start_overlap = NULL;
+    before = NULL;
     rc = 0;
     OS_ENTER_CRITICAL(sr);
 
-    /* The schedule item must occur after current running item (if any) */
-    if (ble_ll_sched_overlaps_current(sch)) {
-        OS_EXIT_CRITICAL(sr);
-        return -1;
-    }
-
     entry = ble_ll_sched_insert_if_empty(sch);
     if (entry) {
         os_cputime_timer_stop(&g_ble_ll_sched_timer);
         while (1) {
-            /* Insert before if adv event is before this event */
             next_sch = entry->link.tqe_next;
-            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
-                rc = 0;
-                TAILQ_INSERT_BEFORE(entry, sch, link);
-                break;
-            }
-
             if (ble_ll_sched_is_overlap(sch, entry)) {
-                if (ble_ll_sched_conn_overlap(entry)) {
-                    assert(0);
+                if (start_overlap == NULL) {
+                    start_overlap = entry;
+                    end_overlap = entry;
+                } else {
+                    end_overlap = entry;
+                }
+            } else {
+                if ((int32_t)(sch->end_time - entry->start_time) <= 0) {
+                    before = entry;
+                    break;
                 }
             }
 
-            /* Move to next entry */
             entry = next_sch;
-
-            /* Insert at tail if none left to check */
-            if (!entry) {
-                rc = 0;
-                TAILQ_INSERT_TAIL(&g_ble_ll_sched_q, sch, link);
+            if (entry == NULL) {
                 break;
             }
         }
 
-        if (!rc) {
-            sch->enqueued = 1;
+        /*
+         * If there is no overlap, we either insert before the 'before' entry
+         * or we insert at the end if there is no before entry.
+         */
+        if (start_overlap == NULL) {
+            if (before) {
+                TAILQ_INSERT_BEFORE(before, sch, link);
+            } else {
+                TAILQ_INSERT_TAIL(&g_ble_ll_sched_q, sch, link);
+            }
+        } else {
+            /*
+             * This item will overlap with others. See if we can fit it in
+             * with original duration.
+             */
+            before = NULL;
+            orig_start = sch->start_time;
+            entry = start_overlap;
+            sch->end_time = sch->start_time + duration;
+            while (1) {
+                next_sch = entry->link.tqe_next;
+                if ((int32_t)(sch->end_time - entry->start_time) <= 0) {
+                    rand_ticks = entry->start_time - sch->end_time;
+                    before = entry;
+                    TAILQ_INSERT_BEFORE(before, sch, link);
+                    break;
+                } else {
+                    sch->start_time = entry->end_time;
+                    sch->end_time = sch->start_time + duration;
+                }
+
+                if (entry == end_overlap) {
+                    rand_ticks = (orig_start + max_delay_ticks) - sch->start_time;
+                    if (rand_ticks > max_delay_ticks) {
+                        /* No place for advertisement. */
+                        rc = -1;
+                    } else {
+                        if (next_sch == NULL) {
+                            TAILQ_INSERT_TAIL(&g_ble_ll_sched_q, sch, link);
+                        } else {
+                            TAILQ_INSERT_BEFORE(next_sch, sch, link);
+                        }
+                    }
+                    break;
+                }
+                entry = next_sch;
+                assert(entry != NULL);
+            }
         }
+    }
 
-        sch = TAILQ_FIRST(&g_ble_ll_sched_q);
+    if (!rc) {
+        sch->enqueued = 1;
+        if (rand_ticks) {
+            sch->start_time += rand() % rand_ticks;
+        }
+        sch->end_time = sch->start_time + duration;
+        *start = sch->start_time;
     }
 
     OS_EXIT_CRITICAL(sr);
 
+    sch = TAILQ_FIRST(&g_ble_ll_sched_q);
     os_cputime_timer_start(&g_ble_ll_sched_timer, sch->start_time);
 
     return rc;
 }
 
+int
+ble_ll_sched_adv_resched_pdu(struct ble_ll_sched_item *sch)
+{
+    uint8_t lls;
+    os_sr_t sr;
+    struct ble_ll_sched_item *entry;
+
+    OS_ENTER_CRITICAL(sr);
+
+    lls = ble_ll_state_get();
+    if ((lls == BLE_LL_STATE_ADV) || (lls == BLE_LL_STATE_CONNECTION)) {
+        goto adv_resched_pdu_fail;
+    }
+
+    entry = ble_ll_sched_insert_if_empty(sch);
+    if (entry) {
+        /* If we overlap with the first item, simply re-schedule */
+        if (ble_ll_sched_is_overlap(sch, entry)) {
+            goto adv_resched_pdu_fail;
+        }
+        os_cputime_timer_stop(&g_ble_ll_sched_timer);
+        TAILQ_INSERT_BEFORE(entry, sch, link);
+    }
+
+    OS_EXIT_CRITICAL(sr);
+    os_cputime_timer_start(&g_ble_ll_sched_timer, sch->start_time);
+    return 0;
+
+adv_resched_pdu_fail:
+    OS_EXIT_CRITICAL(sr);
+    return -1;
+}
+
 /**
  * Remove a schedule element
  *
@@ -645,11 +761,10 @@ ble_ll_sched_execute_item(struct ble_ll_sched_item *sch)
             ble_ll_state_set(BLE_LL_STATE_STANDBY);
         } else if (lls == BLE_LL_STATE_ADV) {
             STATS_INC(ble_ll_stats, sched_state_adv_errs);
-            ble_ll_adv_halt();
+            ble_ll_adv_halt((struct ble_ll_adv_sm *)sch->cb_arg);
         } else {
             STATS_INC(ble_ll_stats, sched_state_conn_errs);
             ble_ll_conn_event_halt();
-            return -1;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/controller/syscfg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/controller/syscfg.yml b/net/nimble/controller/syscfg.yml
index 1b0e7d7..b5119cd 100644
--- a/net/nimble/controller/syscfg.yml
+++ b/net/nimble/controller/syscfg.yml
@@ -109,8 +109,15 @@ syscfg.defs:
 
     # The number of slots that will be allocated to each connection
     BLE_LL_CONN_INIT_SLOTS:
-        description: 'TBD'
-        value: '2'
+        description: >
+            This is the number of "slots" allocated to a connection when scheduling
+            connections. Each slot is 1.25 msecs long. Note that a connection event may
+            last longer than the number of slots allocated here and may also end earlier
+            (depending on when the next scheduled event occurs and how much data needs
+            to be transferred in the connection). However, you will be guaranteed that
+            a connection event will be given this much time, if needed. Consecutively
+            scheduled items will be at least this far apart
+        value: '4'
 
     # The number of random bytes to store
     BLE_LL_RNG_BUFSIZE:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/include/nimble/ble.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble.h b/net/nimble/include/nimble/ble.h
index bff3e9d..29f335c 100644
--- a/net/nimble/include/nimble/ble.h
+++ b/net/nimble/include/nimble/ble.h
@@ -27,6 +27,7 @@ extern "C" {
 
 /* XXX: some or all of these should not be here */
 #include "os/os.h"
+#include "syscfg/syscfg.h"
 
 /* BLE encryption block definitions */
 #define BLE_ENC_BLOCK_SIZE       (16)
@@ -63,6 +64,9 @@ struct ble_mbuf_hdr_rxinfo
     uint8_t channel;
     uint8_t handle;
     int8_t  rssi;
+#if MYNEWT_VAL(BLE_MULTI_ADV_SUPPORT)
+    void *advsm;   /* advertising state machine */
+#endif
 };
 
 /* Flag definitions for rxinfo  */



[29/50] incubator-mynewt-core git commit: oic; remove oc_signal api.

Posted by st...@apache.org.
oic; remove oc_signal api.


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

Branch: refs/heads/sensors_branch
Commit: bcc40505f586096a841a5e59d2221b36776630f3
Parents: 98c08c0
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Dec 12 14:18:02 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/oic/include/oic/oc_api.h | 30 ++----------------------------
 1 file changed, 2 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bcc40505/net/oic/include/oic/oc_api.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_api.h b/net/oic/include/oic/oc_api.h
index f0c6346..ca512e4 100644
--- a/net/oic/include/oic/oc_api.h
+++ b/net/oic/include/oic/oc_api.h
@@ -19,7 +19,8 @@
 
 #include "../src/port/mynewt/config.h"
 #include "../src/messaging/coap/oc_coap.h"
-#include "oc_ri.h"
+#include "oic/oc_ri.h"
+#include "oic/oc_api.h"
 #include "../src/port/oc_signal_main_loop.h"
 #include "../src/port/oc_storage.h"
 
@@ -136,33 +137,6 @@ bool oc_do_observe(const char *uri, oc_server_handle_t *server,
 
 bool oc_stop_observe(const char *uri, oc_server_handle_t *server);
 
-/** Common operations */
-
-/** API for setting handlers for interrupts */
-
-#define oc_signal_interrupt_handler(name)                                      \
-  do {                                                                         \
-    oc_process_poll(&(name##_interrupt_x));                                    \
-    oc_signal_main_loop();                                                     \
-  } while (0)
-
-#define oc_activate_interrupt_handler(name)                                    \
-  (oc_process_start(&(name##_interrupt_x), 0))
-
-#define oc_define_interrupt_handler(name)                                      \
-  void name##_interrupt_x_handler(void);                                       \
-  OC_PROCESS(name##_interrupt_x, "");                                          \
-  OC_PROCESS_THREAD(name##_interrupt_x, ev, data)                              \
-  {                                                                            \
-    OC_PROCESS_POLLHANDLER(name##_interrupt_x_handler());                      \
-    OC_PROCESS_BEGIN();                                                        \
-    while (oc_process_is_running(&(name##_interrupt_x))) {                     \
-      OC_PROCESS_YIELD();                                                      \
-    }                                                                          \
-    OC_PROCESS_END();                                                          \
-  }                                                                            \
-  void name##_interrupt_x_handler(void)
-
 #ifdef __cplusplus
 }
 #endif


[21/50] incubator-mynewt-core git commit: oic; don't automatically decode incoming payload to a tree of allocated objects. Make handlers do the decoding, allowing them to use stack-only construct.

Posted by st...@apache.org.
oic; don't automatically decode incoming payload to a tree of
allocated objects. Make handlers do the decoding, allowing them
to use stack-only construct.


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

Branch: refs/heads/sensors_branch
Commit: 3657169a4e3190f317c768f8423fd6a7c6eb2ff1
Parents: a372c10
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 12:18:08 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/oic/include/oic/oc_rep.h         |  2 +
 net/oic/include/oic/oc_ri.h          |  4 +-
 net/oic/src/api/oc_discovery.c       | 66 +++++++++++++++----------------
 net/oic/src/api/oc_rep.c             |  4 ++
 net/oic/src/api/oc_ri.c              | 26 +-----------
 net/oic/src/messaging/coap/observe.c |  1 -
 6 files changed, 40 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3657169a/net/oic/include/oic/oc_rep.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_rep.h b/net/oic/include/oic/oc_rep.h
index 4ccaa20..72ae7fc 100644
--- a/net/oic/include/oic/oc_rep.h
+++ b/net/oic/include/oic/oc_rep.h
@@ -190,6 +190,7 @@ int oc_rep_finalize(void);
     g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array);  \
   } while (0)
 
+#ifdef OC_CLIENT
 typedef enum {
   NIL = 0,
   INT = 0x01,
@@ -229,6 +230,7 @@ uint16_t oc_parse_rep(const uint8_t *payload, uint16_t payload_size,
                       oc_rep_t **value_list);
 
 void oc_free_rep(oc_rep_t *rep);
+#endif
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3657169a/net/oic/include/oic/oc_ri.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_ri.h b/net/oic/include/oic/oc_ri.h
index c501f46..9f07d5f 100644
--- a/net/oic/include/oic/oc_ri.h
+++ b/net/oic/include/oic/oc_ri.h
@@ -97,13 +97,11 @@ typedef enum {
 
 typedef struct oc_resource oc_resource_t;
 
-typedef struct
-{
+typedef struct {
   oc_endpoint_t *origin;
   oc_resource_t *resource;
   const char *query;
   int query_len;
-  oc_rep_t *request_payload;
   oc_response_t *response;
   void *packet;
 } oc_request_t;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3657169a/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 29311b7..be5be0c 100644
--- a/net/oic/src/api/oc_discovery.c
+++ b/net/oic/src/api/oc_discovery.c
@@ -127,45 +127,43 @@ process_device_object(CborEncoder *device, const char *uuid, const char *rt,
 }
 
 static void
-oc_core_discovery_handler(oc_request_t *request, oc_interface_mask_t interface)
+oc_core_discovery_handler(oc_request_t *req, oc_interface_mask_t interface)
 {
-  char *rt = NULL;
-  int rt_len = 0, matches = 0;
-  if (request->query_len) {
-    rt_len =
-      oc_ri_get_query_value(request->query, request->query_len, "rt", &rt);
-  }
+    char *rt = NULL;
+    int rt_len = 0, matches = 0;
+    char uuid[37];
 
-  char uuid[37];
-  oc_uuid_to_str(oc_core_get_device_id(0), uuid, 37);
+    rt_len = oc_ri_get_query_value(req->query, req->query_len, "rt", &rt);
 
-  switch (interface) {
-  case OC_IF_LL: {
-    oc_rep_start_links_array();
-    matches = process_device_object(oc_rep_array(links), uuid, rt, rt_len);
-    oc_rep_end_links_array();
-  } break;
-  case OC_IF_BASELINE: {
-    oc_rep_start_root_object();
-    oc_process_baseline_interface(request->resource);
-    oc_rep_set_array(root, links);
-    matches = process_device_object(oc_rep_array(links), uuid, rt, rt_len);
-    oc_rep_close_array(root, links);
-    oc_rep_end_root_object();
-  } break;
-  default:
-    break;
-  }
+    oc_uuid_to_str(oc_core_get_device_id(0), uuid, sizeof(uuid));
 
-  int response_length = oc_rep_finalize();
+    switch (interface) {
+    case OC_IF_LL: {
+        oc_rep_start_links_array();
+        matches = process_device_object(oc_rep_array(links), uuid, rt, rt_len);
+        oc_rep_end_links_array();
+    } break;
+    case OC_IF_BASELINE: {
+        oc_rep_start_root_object();
+        oc_process_baseline_interface(req->resource);
+        oc_rep_set_array(root, links);
+        matches = process_device_object(oc_rep_array(links), uuid, rt, rt_len);
+        oc_rep_close_array(root, links);
+        oc_rep_end_root_object();
+    } break;
+    default:
+        break;
+    }
 
-  if (matches && response_length > 0) {
-    request->response->response_buffer->response_length = response_length;
-    request->response->response_buffer->code = oc_status_code(OC_STATUS_OK);
-  } else {
-    /* There were rt/if selections and there were no matches, so ignore */
-    request->response->response_buffer->code = OC_IGNORE;
-  }
+    int response_length = oc_rep_finalize();
+
+    if (matches && response_length > 0) {
+        req->response->response_buffer->response_length = response_length;
+        req->response->response_buffer->code = oc_status_code(OC_STATUS_OK);
+    } else {
+        /* There were rt/if selections and there were no matches, so ignore */
+        req->response->response_buffer->code = OC_IGNORE;
+    }
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3657169a/net/oic/src/api/oc_rep.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_rep.c b/net/oic/src/api/oc_rep.c
index 4ef7161..20a133a 100644
--- a/net/oic/src/api/oc_rep.c
+++ b/net/oic/src/api/oc_rep.c
@@ -25,9 +25,11 @@
 #include <tinycbor/cbor_buf_writer.h>
 #include <tinycbor/cbor_buf_reader.h>
 
+#ifdef OC_CLIENT
 static struct os_mempool oc_rep_objects;
 static uint8_t oc_rep_objects_area[OS_MEMPOOL_BYTES(EST_NUM_REP_OBJECTS,
       sizeof(oc_rep_t))];
+#endif
 
 static const CborEncoder g_empty;
 static uint8_t *g_buf;
@@ -61,6 +63,7 @@ oc_rep_reset(void)
     g_encoder = g_empty;
 }
 
+#ifdef OC_CLIENT
 static oc_rep_t *
 _alloc_rep(void)
 {
@@ -321,3 +324,4 @@ oc_rep_init(void)
     os_mempool_init(&oc_rep_objects, EST_NUM_REP_OBJECTS,
       sizeof(oc_rep_t), oc_rep_objects_area, "oc_rep_o");
 }
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3657169a/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 d240193..36d3cbb 100644
--- a/net/oic/src/api/oc_ri.c
+++ b/net/oic/src/api/oc_ri.c
@@ -229,8 +229,8 @@ oc_ri_init(void)
   SLIST_INIT(&oc_client_cbs);
   os_mempool_init(&oc_client_cb_pool, MAX_NUM_CONCURRENT_REQUESTS,
     sizeof(oc_client_cb_t), oc_client_cb_area, "oc_cl_cbs");
-#endif
   oc_rep_init();
+#endif
   oc_buffer_init();
 
   start_processes();
@@ -393,7 +393,6 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
   response_obj.response_buffer = &response_buffer;
 
   request_obj.response = &response_obj;
-  request_obj.request_payload = 0;
   request_obj.query_len = 0;
   request_obj.resource = 0;
   request_obj.origin = endpoint;
@@ -422,22 +421,6 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
     }
   }
 
-  /* Obtain handle to buffer containing the serialized payload */
-  const uint8_t *payload;
-  int payload_len = coap_get_payload(request, &payload);
-  if (payload_len) {
-    /* Attempt to parse request payload using tinyCBOR via oc_rep helper
-     * functions. The result of this parse is a tree of oc_rep_t structures
-     * which will reflect the schema of the payload.
-     * Any failures while parsing the payload is viewed as an erroneous
-     * request and results in a 4.00 response being sent.
-     */
-    if (oc_parse_rep(payload, payload_len, &request_obj.request_payload) != 0) {
-      OC_LOG_ERROR("ocri: error parsing request payload\n");
-      bad_request = true;
-    }
-  }
-
   oc_resource_t *resource, *cur_resource = NULL;
 
   /* If there were no errors thus far, attempt to locate the specific
@@ -530,13 +513,6 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
     }
   }
 
-  if (payload_len) {
-    /* To the extent that the request payload was parsed, free the
-     * payload structure (and return its memory to the pool).
-     */
-    oc_free_rep(request_obj.request_payload);
-  }
-
   if (bad_request) {
     OC_LOG_ERROR("ocri: Bad request\n");
     /* Return a 4.00 response */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3657169a/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 73d70e8..1b7404d 100644
--- a/net/oic/src/messaging/coap/observe.c
+++ b/net/oic/src/messaging/coap/observe.c
@@ -214,7 +214,6 @@ coap_notify_observers(oc_resource_t *resource,
         response.response_buffer = &response_buffer;
         request.resource = resource;
         request.response = &response;
-        request.request_payload = NULL;
         oc_rep_new(buffer, COAP_MAX_BLOCK_SIZE);
         resource->get_handler(&request, resource->default_interface);
         response_buf = &response_buffer;


[38/50] incubator-mynewt-core git commit: oic; reduce the number of oc_message_t's to 1.

Posted by st...@apache.org.
oic; reduce the number of oc_message_t's to 1.


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

Branch: refs/heads/sensors_branch
Commit: f06273307eb3c7ad69e03e0dc780b8a56c9a3423
Parents: 6331a5e
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 21:39:00 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/api/oc_buffer.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f0627330/net/oic/src/api/oc_buffer.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_buffer.c b/net/oic/src/api/oc_buffer.c
index 6fb6beb..3e3cf8f 100644
--- a/net/oic/src/api/oc_buffer.c
+++ b/net/oic/src/api/oc_buffer.c
@@ -33,8 +33,7 @@
 #include "port/mynewt/adaptor.h"
 
 static struct os_mempool oc_buffers;
-static uint8_t oc_buffer_area[OS_MEMPOOL_BYTES(MAX_NUM_CONCURRENT_REQUESTS * 2,
-      sizeof(oc_message_t))];
+static uint8_t oc_buffer_area[OS_MEMPOOL_BYTES(1, sizeof(oc_message_t))];
 
 static struct os_mqueue oc_inq;
 static struct os_mqueue oc_outq;
@@ -197,8 +196,8 @@ free_msg:
 void
 oc_buffer_init(void)
 {
-    os_mempool_init(&oc_buffers, MAX_NUM_CONCURRENT_REQUESTS * 2,
-      sizeof(oc_message_t), oc_buffer_area, "oc_bufs");
+    os_mempool_init(&oc_buffers, 1, sizeof(oc_message_t), oc_buffer_area,
+                    "oc_bufs");
     os_mqueue_init(&oc_inq, oc_buffer_rx, NULL);
     os_mqueue_init(&oc_outq, oc_buffer_tx, NULL);
 }


[43/50] incubator-mynewt-core git commit: oic; server was making a copy of payload mbuf, instead of directly chaining it to response header.

Posted by st...@apache.org.
oic; server was making a copy of payload mbuf, instead of
directly chaining it to response header.


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

Branch: refs/heads/sensors_branch
Commit: 0dd80b7b1aaa28d1468a25e2bd86def1eca75e04
Parents: ed6020f
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 14 10:03:53 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/include/oic/oc_ri.h |  4 +++-
 net/oic/src/api/oc_ri.c     | 17 ++++++++---------
 2 files changed, 11 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0dd80b7b/net/oic/include/oic/oc_ri.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_ri.h b/net/oic/include/oic/oc_ri.h
index 9449685..e58af0b 100644
--- a/net/oic/include/oic/oc_ri.h
+++ b/net/oic/include/oic/oc_ri.h
@@ -149,7 +149,9 @@ int oc_ri_get_query_value(const char *query, int query_len, const char *key,
 
 oc_interface_mask_t oc_ri_get_interface_mask(char *iface, int if_len);
 
-bool oc_ri_invoke_coap_entity_handler(void *request, void *response,
+typedef struct coap_packet coap_packet_t;
+bool oc_ri_invoke_coap_entity_handler(coap_packet_t *request,
+                                      coap_packet_t *response,
                                       int32_t *offset, oc_endpoint_t *endpoint);
 
 #ifdef __cplusplus

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0dd80b7b/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 3ff5040..2f0faea 100644
--- a/net/oic/src/api/oc_ri.c
+++ b/net/oic/src/api/oc_ri.c
@@ -357,8 +357,9 @@ does_interface_support_method(oc_resource_t *resource,
 }
 
 bool
-oc_ri_invoke_coap_entity_handler(void *request, void *response,
-                                 int32_t *offset, oc_endpoint_t *endpoint)
+oc_ri_invoke_coap_entity_handler(coap_packet_t *request,
+                                 coap_packet_t *response, int32_t *offset,
+                                 oc_endpoint_t *endpoint)
 {
   /* Flags that capture status along various stages of processing
    *  the request.
@@ -369,13 +370,10 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response,
   bool authorized = true;
 #endif
 
-  /* Parsed CoAP PDU structure. */
-  coap_packet_t *const packet = (coap_packet_t *)request;
-
   /* This function is a server-side entry point solely for requests.
    *  Hence, "code" contains the CoAP method code.
    */
-  oc_method_t method = packet->code;
+  oc_method_t method = request->code;
 
   /* Initialize request/response objects to be sent up to the app layer. */
   oc_request_t request_obj;
@@ -395,7 +393,7 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response,
   request_obj.query_len = 0;
   request_obj.resource = 0;
   request_obj.origin = endpoint;
-  request_obj.packet = packet;
+  request_obj.packet = request;
 
   /* Initialize OCF interface selector. */
   oc_interface_mask_t interface = 0;
@@ -642,8 +640,9 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response,
     }
 #endif
     if (response_buffer.response_length) {
-        coap_set_payload(response, response_buffer.buffer,
-                         OS_MBUF_PKTLEN(response_buffer.buffer));
+        response->payload_m = response_buffer.buffer;
+        response->payload_len = OS_MBUF_PKTLEN(response_buffer.buffer);
+        response_buffer.buffer = NULL; /* freed in coap_serialize_message() */
         coap_set_header_content_format(response, APPLICATION_CBOR);
     }
     /* response_buffer.code at this point contains a valid CoAP status


[39/50] incubator-mynewt-core git commit: oic; reduce the default number of registrable resources.

Posted by st...@apache.org.
oic; reduce the default number of registrable resources.


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

Branch: refs/heads/sensors_branch
Commit: e696b98db70ecafe6d0c1817e595f5d072ce3534
Parents: 4947fa4
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Dec 15 12:27:25 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/syscfg.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e696b98d/net/oic/syscfg.yml
----------------------------------------------------------------------
diff --git a/net/oic/syscfg.yml b/net/oic/syscfg.yml
index 3d71e7d..c5dbfa2 100644
--- a/net/oic/syscfg.yml
+++ b/net/oic/syscfg.yml
@@ -52,7 +52,7 @@ syscfg.defs:
 
     OC_APP_RESOURCES:
         description: 'Maximum number of server resources'
-        value: 8
+        value: 3
 
     OC_NUM_DEVICES:
         description: 'Number of devices on the OCF platform'


[44/50] incubator-mynewt-core git commit: bleprph_oic; add an example resource, which controls BSP's LED pin.

Posted by st...@apache.org.
bleprph_oic; add an example resource, which controls BSP's LED pin.


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

Branch: refs/heads/sensors_branch
Commit: f65669d88d77fcce93a15bde97af4459d62659dc
Parents: 8680eeb
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 14 16:18:30 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 apps/bleprph_oic/src/main.c | 98 ++++++++++++++++++++++++++++++++++------
 1 file changed, 84 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f65669d8/apps/bleprph_oic/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph_oic/src/main.c b/apps/bleprph_oic/src/main.c
index 203b1d4..22b4b81 100755
--- a/apps/bleprph_oic/src/main.c
+++ b/apps/bleprph_oic/src/main.c
@@ -21,26 +21,28 @@
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
-#include "sysinit/sysinit.h"
-#include "bsp/bsp.h"
-#include "os/os.h"
-#include "bsp/bsp.h"
-#include "hal/hal_gpio.h"
-#include "console/console.h"
-#include "imgmgr/imgmgr.h"
-#include "mgmt/mgmt.h"
-#include "oic/oc_api.h"
+#include <sysinit/sysinit.h>
+#include <bsp/bsp.h>
+#include <os/os.h>
+#include <bsp/bsp.h>
+#include <hal/hal_gpio.h>
+#include <console/console.h>
+#include <imgmgr/imgmgr.h>
+#include <mgmt/mgmt.h>
+
+#include <oic/oc_api.h>
+#include <oic/oc_gatt.h>
+#include <oic/oc_log.h>
+#include <cborattr/cborattr.h>
 
 /* BLE */
-#include "nimble/ble.h"
-#include "host/ble_hs.h"
-#include "services/gap/ble_svc_gap.h"
+#include <nimble/ble.h>
+#include <host/ble_hs.h>
+#include <services/gap/ble_svc_gap.h>
 
 /* Application-specified header. */
 #include "bleprph.h"
 
-#include <oic/oc_gatt.h>
-#include <oic/oc_log.h>
 
 /** Log data. */
 struct log bleprph_log;
@@ -255,11 +257,76 @@ bleprph_on_sync(void)
 }
 
 static void
+app_get_light(oc_request_t *request, oc_interface_mask_t interface)
+{
+    bool state;
+
+    if (hal_gpio_read(LED_BLINK_PIN)) {
+        state = true;
+    } else {
+        state = false;
+    }
+    oc_rep_start_root_object();
+        switch (interface) {
+    case OC_IF_BASELINE:
+        oc_process_baseline_interface(request->resource);
+    case OC_IF_RW:
+        oc_rep_set_boolean(root, state, state);
+        break;
+    default:
+        break;
+    }
+    oc_rep_end_root_object();
+    oc_send_response(request, OC_STATUS_OK);
+}
+
+static void
+app_set_light(oc_request_t *request, oc_interface_mask_t interface)
+{
+    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] = {
+        }
+    };
+
+    len = coap_get_payload(request->packet, &data);
+    if (cbor_read_flat_attrs(data, len, attrs)) {
+        oc_send_response(request, OC_STATUS_BAD_REQUEST);
+    } else {
+        hal_gpio_write(LED_BLINK_PIN, state == true);
+        oc_send_response(request, OC_STATUS_CHANGED);
+    }
+
+}
+
+static void
 omgr_app_init(void)
 {
+    oc_resource_t *res;
+
     oc_init_platform("MyNewt", NULL, NULL);
     oc_add_device("/oic/d", "oic.d.light", "MynewtLed", "1.0", "1.0", NULL,
                   NULL);
+
+    res = oc_new_resource("/light/1", 1, 0);
+    oc_resource_bind_resource_type(res, "oic.r.light");
+    oc_resource_bind_resource_interface(res, OC_IF_RW);
+    oc_resource_set_default_interface(res, OC_IF_RW);
+
+    oc_resource_set_discoverable(res);
+    oc_resource_set_periodic_observable(res, 1);
+    oc_resource_set_request_handler(res, OC_GET, app_get_light);
+    oc_resource_set_request_handler(res, OC_PUT, app_set_light);
+    oc_add_resource(res);
+
 }
 
 static const oc_handler_t omgr_oc_handler = {
@@ -333,6 +400,9 @@ main(void)
     rc = ble_svc_gap_device_name_set("pi");
     assert(rc == 0);
 
+    /* Our light resource */
+    hal_gpio_init_out(LED_BLINK_PIN, 1);
+
     /* Start the OS */
     os_start();
 


[37/50] incubator-mynewt-core git commit: oic; fix endpoint logging at RX, used wrong pointer as data.

Posted by st...@apache.org.
oic; fix endpoint logging at RX, used wrong pointer as data.


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

Branch: refs/heads/sensors_branch
Commit: 896011d558e8b4dd44693db1b5e9cf5afa884f6f
Parents: 645a5b7
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Dec 16 15:39:39 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/api/oc_buffer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/896011d5/net/oic/src/api/oc_buffer.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_buffer.c b/net/oic/src/api/oc_buffer.c
index 7c3b477..7bca928 100644
--- a/net/oic/src/api/oc_buffer.c
+++ b/net/oic/src/api/oc_buffer.c
@@ -160,7 +160,7 @@ oc_buffer_rx(struct os_event *ev)
             goto free_msg;
         }
         OC_LOG_DEBUG("oc_buffer_rx: ");
-        OC_LOG_ENDPOINT(LOG_LEVEL_DEBUG, &msg->endpoint);
+        OC_LOG_ENDPOINT(LOG_LEVEL_DEBUG, OC_MBUF_ENDPOINT(m));
 
         if (OS_MBUF_PKTHDR(m)->omp_len > MAX_PAYLOAD_SIZE) {
             STATS_INC(coap_stats, itoobig);


[03/50] incubator-mynewt-core git commit: bletiny: Enhance logs on advertising events

Posted by st...@apache.org.
bletiny: Enhance logs on advertising events

This patch add additional logs decoding advertising flags.
This is useful for PTS testing.


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

Branch: refs/heads/sensors_branch
Commit: c7a5c5db613cbca6ba3eca240d9cd29c91378ab6
Parents: e527e30
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Mon Dec 12 12:02:00 2016 +0100
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 apps/bletiny/src/main.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c7a5c5db/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 4ec6d5d..d3a9d38 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -141,7 +141,24 @@ bletiny_print_adv_fields(const struct ble_hs_adv_fields *fields)
     int i;
 
     if (fields->flags_is_present) {
-        console_printf("    flags=0x%02x\n", fields->flags);
+        console_printf("    flags=0x%02x:\n", fields->flags);
+
+        if (!(fields->flags & BLE_HS_ADV_F_DISC_LTD) &&
+                !(fields->flags & BLE_HS_ADV_F_DISC_GEN)) {
+                console_printf("        Non-discoverable mode\n");
+        }
+
+        if (fields->flags & BLE_HS_ADV_F_DISC_LTD) {
+                console_printf("        Limited discoverable mode\n");
+        }
+
+        if (fields->flags & BLE_HS_ADV_F_DISC_GEN) {
+                console_printf("        General discoverable mode\n");
+        }
+
+        if (fields->flags & BLE_HS_ADV_F_BREDR_UNSUP) {
+                console_printf("        BR/EDR not supported\n");
+        }
     }
 
     if (fields->uuids16 != NULL) {


[18/50] incubator-mynewt-core git commit: Grammar/links cleanup

Posted by st...@apache.org.
Grammar/links cleanup

Changed some links based on things that have been reorganized.

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

Branch: refs/heads/sensors_branch
Commit: 3aa49e686af4d2743ad48befff160e0b50526a22
Parents: 269791e
Author: David G. Simmons <da...@users.noreply.github.com>
Authored: Fri Dec 2 13:08:51 2016 -0500
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 README.md | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3aa49e68/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 55996e5..12c8227 100644
--- a/README.md
+++ b/README.md
@@ -24,8 +24,8 @@
 ## Overview
 
 Apache Mynewt is an open-source operating system for tiny embedded devices.
-It's goal is to make it easy to develop applications for microcontroller
-environments, where power and cost are driving factors.
+Its goal is to make it easy to develop applications for microcontroller
+environments where power and cost are driving factors.
 
 It currently supports the following hardware platforms:
 
@@ -86,12 +86,13 @@ Mynewt is being actively developed.  Some of the features we're currently workin
 If you are browsing around the source tree, and want to see some of the
 major functional chunks, here are a few pointers:
 
-- libs: Contains the core of the RTOS ([kernel/os](https://github.com/apache/incubator-mynewt-core/tree/master/libs/os))
-and a number of helper libraries for building applications.  Including a
-console ([sys/console](https://github.com/apache/incubator-mynewt-core/tree/master/libs/console))),
-shell ([sys/shell](https://github.com/apache/incubator-mynewt-core/tree/master/libs/shell))), and
-[mgmt/newtmgr](https://github.com/apache/incubator-mynewt-core/tree/master/libs/newtmgr)), which
-supports software upgrade and remote fetching of logs and statistics.
+- kernel: Contains the core of the RTOS ([kernel/os](https://github.com/apache/incubator-mynewt-core/tree/master/kernel/os))
+
+- sys: Contains a number of helper libraries for building applications.  Including a
+console ([sys/console](https://github.com/apache/incubator-mynewt-core/tree/master/sys/console))),
+shell ([sys/shell](https://github.com/apache/incubator-mynewt-core/tree/master/sys/shell)))
+
+- mgmt: Contains the management libraries for newtmgr [mgmt/newtmgr](https://github.com/apache/incubator-mynewt-core/tree/master/sys/newtmgr)), which supports software upgrade and remote fetching of logs and statistics.
 
 - net: Contains the networking packages.  Highlights of the net directory are the NimBLE and IP packages.
 [Nimble](https://github.com/apache/incubator-mynewt-core/tree/master/net/nimble)


[25/50] incubator-mynewt-core git commit: oic; fix build warning when neither IP and BLE transport options were compiled in.

Posted by st...@apache.org.
oic; fix build warning when neither IP and BLE transport options
were compiled in.


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

Branch: refs/heads/sensors_branch
Commit: a372c10fa7d7315b54341f084734926cee8478ee
Parents: 256f8b6
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 11:48:51 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/oic/src/port/mynewt/log.c | 2 ++
 1 file changed, 2 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a372c10f/net/oic/src/port/mynewt/log.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/log.c b/net/oic/src/port/mynewt/log.c
index 9a877bf..85b80c6 100644
--- a/net/oic/src/port/mynewt/log.c
+++ b/net/oic/src/port/mynewt/log.c
@@ -32,6 +32,8 @@ oc_log_endpoint(uint16_t lvl, struct oc_endpoint *oe)
     char *str;
     char tmp[46 + 6];
 
+    (void)tmp;
+
     switch (oe->flags) {
 #if (MYNEWT_VAL(OC_TRANSPORT_IP) == 1)
     case IP: {


[30/50] incubator-mynewt-core git commit: Add extra doc + errors messages

Posted by st...@apache.org.
Add extra doc + errors messages


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

Branch: refs/heads/sensors_branch
Commit: fe4416f962f97ee1c3ff8bf40cf2207a08c2e096
Parents: 646945e
Author: Fabio Utzig <ut...@utzig.org>
Authored: Fri Dec 9 09:10:24 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/include/mmc/mmc.h | 39 +++++++++++++++++++----------------
 hw/drivers/mmc/src/mmc.c         | 22 +++++++++++---------
 2 files changed, 33 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fe4416f9/hw/drivers/mmc/include/mmc/mmc.h
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/include/mmc/mmc.h b/hw/drivers/mmc/include/mmc/mmc.h
index 9dbf35e..d65363d 100644
--- a/hw/drivers/mmc/include/mmc/mmc.h
+++ b/hw/drivers/mmc/include/mmc/mmc.h
@@ -29,16 +29,19 @@ extern "C" {
 /**
  * MMC driver errors.
  */
-#define MMC_OK              (0)
-#define MMC_CARD_ERROR      (-1)  /* Is there a card installed? */
-#define MMC_READ_ERROR      (-2)
-#define MMC_WRITE_ERROR     (-3)
-#define MMC_TIMEOUT         (-4)
-#define MMC_PARAM_ERROR     (-5)
-#define MMC_CRC_ERROR       (-6)
-#define MMC_DEVICE_ERROR    (-7)
-#define MMC_RESPONSE_ERROR  (-8)
-#define MMC_VOLTAGE_ERROR   (-9)
+#define MMC_OK                (0)
+#define MMC_CARD_ERROR        (-1)  /* Is there a card installed? */
+#define MMC_READ_ERROR        (-2)
+#define MMC_WRITE_ERROR       (-3)
+#define MMC_TIMEOUT           (-4)
+#define MMC_PARAM_ERROR       (-5)
+#define MMC_CRC_ERROR         (-6)
+#define MMC_DEVICE_ERROR      (-7)
+#define MMC_RESPONSE_ERROR    (-8)
+#define MMC_VOLTAGE_ERROR     (-9)
+#define MMC_INVALID_COMMAND   (-10)
+#define MMC_ERASE_ERROR       (-11)
+#define MMC_ADDR_ERROR        (-12)
 
 /**
  * Initialize the MMC driver
@@ -55,10 +58,10 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin);
 /**
  * Read data from MMC
  *
- * @param mmc_id
- * @param addr
- * @param buf
- * @param len
+ * @param mmc_id Id of the MMC device (currently must be 0)
+ * @param addr Disk address (in bytes) to be read from
+ * @param buf Buffer where data should be copied to
+ * @param len Amount of data to read/copy
  *
  * @return 0 on success, non-zero on failure
  */
@@ -68,10 +71,10 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len);
 /**
  * Write data to the MMC
  *
- * @param mmc_id
- * @param addr
- * @param buf
- * @param len
+ * @param mmc_id Id of the MMC device (currently must be 0)
+ * @param addr Disk address (in bytes) to be written to
+ * @param buf Buffer where data should be copied from
+ * @param len Amount of data to copy/write
  *
  * @return 0 on success, non-zero on failure
  */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fe4416f9/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index 653e08f..6c5dcbe 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -86,27 +86,29 @@ static struct mmc_cfg {
 static int
 error_by_response(uint8_t status)
 {
+    int rc = MMC_CARD_ERROR;
+
     if (status == 0) {
-        return MMC_OK;
+        rc = MMC_OK;
     } else if (status == 0xff) {
-        return MMC_CARD_ERROR;
+        rc = MMC_CARD_ERROR;
     } else if (status & R_IDLE) {
-        return MMC_TIMEOUT;
+        rc = MMC_TIMEOUT;
     } else if (status & R_ERASE_RESET) {
-        /* TODO */
+        rc = MMC_ERASE_ERROR;
     } else if (status & R_ILLEGAL_COMMAND) {
-        /* TODO */
+        rc = MMC_INVALID_COMMAND;
     } else if (status & R_CRC_ERROR) {
-        return MMC_CRC_ERROR;
+        rc = MMC_CRC_ERROR;
     } else if (status & R_ERASE_ERROR) {
-        /* TODO */
+        rc = MMC_ERASE_ERROR;
     } else if (status & R_ADDR_ERROR) {
-        /* TODO */
+        rc = MMC_ADDR_ERROR;
     } else if (status & R_PARAM_ERROR) {
-        return MMC_PARAM_ERROR;
+        rc = MMC_PARAM_ERROR;
     }
 
-    return MMC_CARD_ERROR;
+    return (rc);
 }
 
 static struct mmc_cfg *


[26/50] incubator-mynewt-core git commit: Better error checking + cleanups/refactorings

Posted by st...@apache.org.
Better error checking + cleanups/refactorings


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

Branch: refs/heads/sensors_branch
Commit: 373ae607dccf0a0a40f041507384510c6505253f
Parents: 41c17c4
Author: Fabio Utzig <ut...@utzig.org>
Authored: Tue Dec 6 23:14:57 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/include/mmc/mmc.h |   2 +
 hw/drivers/mmc/src/mmc.c         | 199 ++++++++++++++++------------------
 2 files changed, 94 insertions(+), 107 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/373ae607/hw/drivers/mmc/include/mmc/mmc.h
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/include/mmc/mmc.h b/hw/drivers/mmc/include/mmc/mmc.h
index 2f434ef..9dbf35e 100644
--- a/hw/drivers/mmc/include/mmc/mmc.h
+++ b/hw/drivers/mmc/include/mmc/mmc.h
@@ -37,6 +37,8 @@ extern "C" {
 #define MMC_PARAM_ERROR     (-5)
 #define MMC_CRC_ERROR       (-6)
 #define MMC_DEVICE_ERROR    (-7)
+#define MMC_RESPONSE_ERROR  (-8)
+#define MMC_VOLTAGE_ERROR   (-9)
 
 /**
  * Initialize the MMC driver

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/373ae607/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index 199b201..8b30929 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -26,7 +26,7 @@
 
 #define MIN(n, m) (((n) < (m)) ? (n) : (m))
 
-/* Currently used MMC commands */
+/* MMC commands used by the driver */
 #define CMD0                (0)            /* GO_IDLE_STATE */
 #define CMD1                (1)            /* SEND_OP_COND (MMC) */
 #define CMD8                (8)            /* SEND_IF_COND */
@@ -40,14 +40,16 @@
 #define CMD58               (58)           /* READ_OCR */
 #define ACMD41              (0x80 + 41)    /* SEND_OP_COND (SDC) */
 
+#define HCS                 ((uint32_t) 1 << 30)
+
 /* Response types */
 #define R1                  (0)
 #define R1b                 (1)
 #define R2                  (2)
-#define R3                  (3)
-#define R7                  (4)
+#define R3                  (3)            /* CMD58 */
+#define R7                  (4)            /* CMD8 */
 
-/* Response status */
+/* R1 response status */
 #define R_IDLE              (0x01)
 #define R_ERASE_RESET       (0x02)
 #define R_ILLEGAL_COMMAND   (0x04)
@@ -80,58 +82,26 @@ static struct mmc_cfg {
 static int
 mmc_error_by_status(uint8_t status)
 {
-    if (status == 0xff) {
-        return MMC_CARD_ERROR;
+    if (status == 0) {
+        return MMC_OK;
     } else if (status & R_IDLE) {
         return MMC_TIMEOUT;
     } else if (status & R_ERASE_RESET) {
+        /* TODO */
     } else if (status & R_ILLEGAL_COMMAND) {
+        /* TODO */
     } else if (status & R_CRC_ERROR) {
+        return MMC_CRC_ERROR;
     } else if (status & R_ERASE_ERROR) {
+        /* TODO */
     } else if (status & R_ADDR_ERROR) {
+        /* TODO */
     } else if (status & R_PARAM_ERROR) {
+        return MMC_PARAM_ERROR;
     }
 
-    return MMC_OK;
-}
-
-static int8_t
-response_type_by_cmd(uint8_t cmd)
-{
-    switch (cmd) {
-        case CMD8   : return R7;
-        case CMD58  : return R3;
-    }
-    return R1;
-}
-
-/*
-static uint32_t
-ocr_from_r3(uint8_t *response)
-{
-    uint32_t ocr;
-
-    ocr  = (uint32_t) response[3];
-    ocr |= (uint32_t) response[2] <<  8;
-    ocr |= (uint32_t) response[1] << 16;
-    ocr |= (uint32_t) response[0] << 24;
-
-#if 0
-    printf("Card supports: ");
-    if (ocr & (1 << 15)) {
-        printf("2.7-2.8 ");
-    }
-#endif
-
-    return ocr;
-}
-
-static uint8_t
-voltage_from_r7(uint8_t *response)
-{
-    return response[2] & 0xF;
+    return MMC_CARD_ERROR;
 }
-*/
 
 static struct mmc_cfg *
 mmc_cfg_dev(uint8_t id)
@@ -147,9 +117,7 @@ static uint8_t
 send_mmc_cmd(struct mmc_cfg *mmc, uint8_t cmd, uint32_t payload)
 {
     int n;
-    uint8_t response[4];
     uint8_t status;
-    uint8_t type;
     uint8_t crc;
 
     if (cmd & 0x80) {
@@ -195,31 +163,6 @@ send_mmc_cmd(struct mmc_cfg *mmc, uint8_t cmd, uint32_t payload)
         return status;
     }
 
-    type = response_type_by_cmd(cmd);
-
-    /* FIXME:
-     *       R1 and R1b don't have extra payload
-     *       R2 has extra status byte
-     *       R3 has 4 extra bytes for OCR
-     *       R7 has 4 extra bytes with pattern, voltage, etc
-     */
-    if (!(type == R1 || status & R_ILLEGAL_COMMAND || status & R_CRC_ERROR)) {
-        /* Read remaining data for this command */
-        for (n = 0; n < sizeof(response); n++) {
-            response[n] = (uint8_t) hal_spi_tx_val(mmc->spi_num, 0xff);
-        }
-
-        switch (type) {
-            case R3:
-                /* NOTE: ocr is defined in section 5.1 */
-                //printf("ocr=0x%08lx\n", ocr_from_r3(response));
-                break;
-            case R7:
-                //printf("voltage=0x%x\n", voltage_from_r7(response));
-                break;
-        }
-    }
-
     return status;
 }
 
@@ -243,28 +186,32 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     int rc;
     int i;
     uint8_t status;
+    uint8_t cmd_resp[4];
     uint32_t ocr;
-    os_time_t wait_to;
+    os_time_t timeout;
+    struct mmc_cfg *mmc;
 
-    g_mmc_cfg.spi_num = spi_num;
-    g_mmc_cfg.ss_pin = ss_pin;
-    g_mmc_cfg.spi_cfg = spi_cfg;
-    g_mmc_cfg.settings = &mmc_settings;
+    /* TODO: create new struct for every new spi mmc, add to SLIST */
+    mmc = &g_mmc_cfg;
+    mmc->spi_num = spi_num;
+    mmc->ss_pin = ss_pin;
+    mmc->spi_cfg = spi_cfg;
+    mmc->settings = &mmc_settings;
 
-    hal_gpio_init_out(g_mmc_cfg.ss_pin, 1);
+    hal_gpio_init_out(mmc->ss_pin, 1);
 
-    rc = hal_spi_init(g_mmc_cfg.spi_num, g_mmc_cfg.spi_cfg, HAL_SPI_TYPE_MASTER);
+    rc = hal_spi_init(mmc->spi_num, mmc->spi_cfg, HAL_SPI_TYPE_MASTER);
     if (rc) {
         return (rc);
     }
 
-    rc = hal_spi_config(g_mmc_cfg.spi_num, g_mmc_cfg.settings);
+    rc = hal_spi_config(mmc->spi_num, mmc->settings);
     if (rc) {
         return (rc);
     }
 
-    hal_spi_set_txrx_cb(g_mmc_cfg.spi_num, NULL, NULL);
-    hal_spi_enable(g_mmc_cfg.spi_num);
+    hal_spi_set_txrx_cb(mmc->spi_num, NULL, NULL);
+    hal_spi_enable(mmc->spi_num);
 
     /**
      * NOTE: The state machine below follows:
@@ -275,21 +222,21 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     /* give 10ms for VDD rampup */
     os_time_delay(OS_TICKS_PER_SEC / 100);
 
-    hal_gpio_write(g_mmc_cfg.ss_pin, 0);
-    hal_spi_tx_val(g_mmc_cfg.spi_num, 0xff);
+    hal_gpio_write(mmc->ss_pin, 0);
+    hal_spi_tx_val(mmc->spi_num, 0xff);
 
     /* send the required >= 74 clock cycles */
     for (i = 0; i < 74; i++) {
-        hal_spi_tx_val(g_mmc_cfg.spi_num, 0xff);
+        hal_spi_tx_val(mmc->spi_num, 0xff);
     }
 
     /* put card in idle state */
-    status = send_mmc_cmd(&g_mmc_cfg, CMD0, 0);
+    status = send_mmc_cmd(mmc, CMD0, 0);
 
+    /* No card inserted or bad card? */
     if (status != R_IDLE) {
-        /* No card inserted or bad card! */
-        hal_gpio_write(g_mmc_cfg.ss_pin, 1);
-        return mmc_error_by_status(status);
+        rc = mmc_error_by_status(status);
+        goto out;
     }
 
     /**
@@ -298,11 +245,15 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
      * NOTE: cards that are not compliant with "Physical Spec Version 2.00"
      *       will answer this with R_ILLEGAL_COMMAND.
      */
-    status = send_mmc_cmd(&g_mmc_cfg, CMD8, 0x1AA);
+    status = send_mmc_cmd(mmc, CMD8, 0x1AA);
+    for (i = 0; i < 4; i++) {
+        cmd_resp[i] = (uint8_t) hal_spi_tx_val(mmc->spi_num, 0xff);
+    }
+
     if (status & R_ILLEGAL_COMMAND) {
         /* Ver1.x SD Memory Card or Not SD Memory Card */
 
-        ocr = send_mmc_cmd(&g_mmc_cfg, CMD58, 0);
+        ocr = send_mmc_cmd(mmc, CMD58, 0);
 
         /* TODO: check if voltage range is ok! */
 
@@ -313,35 +264,69 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
         /* TODO: set blocklen */
 
     } else {
-        /* Ver2.00 or later SD Memory Card */
 
-        /* TODO:
-         * 1) check CMD8 response pattern and voltage range.
-         * 2) DONE: send ACMD41 while in R_IDLE up to 1s.
-         * 3) send CMD58, check CCS in response.
+        /**
+         * Ver2.00 or later SD Memory Card
          */
 
-#define TIME_TO_WAIT (3 * OS_TICKS_PER_SEC)
+        if (cmd_resp[3] != 0xAA) {
+            rc = MMC_RESPONSE_ERROR;
+            goto out;
+        }
+
+        /**
+         * 4.3.13 Send Interface Condition Command (CMD8)
+         *   Check VHS for 2.7-3.6V support
+         */
+        if (cmd_resp[2] != 0x01) {
+            rc = MMC_VOLTAGE_ERROR;
+            goto out;
+        }
 
-        wait_to = os_time_get() + TIME_TO_WAIT;
-        status = send_mmc_cmd(&g_mmc_cfg, ACMD41, 0x40000000); /* FIXME */
+        /**
+         * Wait for card to leave IDLE state or time out
+         */
 
-        while (status & R_IDLE) {
-            if (os_time_get() > wait_to) {
+        timeout = os_time_get() + OS_TICKS_PER_SEC;
+        for (;;) {
+            status = send_mmc_cmd(mmc, ACMD41, HCS);
+            if ((status & R_IDLE) == 0 || os_time_get() > timeout) {
                 break;
             }
             os_time_delay(OS_TICKS_PER_SEC / 10);
-            status = send_mmc_cmd(&g_mmc_cfg, ACMD41, 0);
         }
-        //printf("ACMD41 status=%x\n", status);
 
-        /* TODO: check CCS = OCR[30] */
-        status = send_mmc_cmd(&g_mmc_cfg, CMD58, 0);
-        //printf("CMD58 status=%x\n", status);
+        if (status) {
+            rc = mmc_error_by_status(status);
+            goto out;
+        }
+
+        /**
+         * Check if this is an high density card
+         */
+
+        status = send_mmc_cmd(mmc, CMD58, 0);
+        for (i = 0; i < 4; i++) {
+            cmd_resp[i] = (uint8_t) hal_spi_tx_val(mmc->spi_num, 0xff);
+        }
+        if (status == 0 && (cmd_resp[0] & (1 << 6))) {  /* FIXME: CCS */
+            /**
+             * TODO: if CCS is set this is an SDHC or SDXC card!
+             *       SDSC uses byte addressing, SDHC/SDXC block addressing
+             */
+        }
+
+#if 0  /* TODO: Can check all possible voltages supported by card */
+    printf("Card supports: ");
+    if (ocr & (1 << 15)) {
+        printf("2.7-2.8 ");
     }
+#endif
 
-    hal_gpio_write(g_mmc_cfg.ss_pin, 1);
+    }
 
+out:
+    hal_gpio_write(mmc->ss_pin, 1);
     return rc;
 }
 


[17/50] incubator-mynewt-core git commit: gattc: Fix unnecessary status overwrite in read by type err

Posted by st...@apache.org.
gattc: Fix unnecessary status overwrite in read by type err

This status change is not needed. With this patch it is possible to
pass TC_GAR_CL_BI_07_C.


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

Branch: refs/heads/sensors_branch
Commit: c656e5a92e54e88c1d196ffbb8a5b73c9ab42129
Parents: 3aa49e6
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Mon Dec 5 15:25:24 2016 +0100
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_gattc.c | 4 ----
 1 file changed, 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c656e5a9/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index 8bbe0b1..d962208 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -3108,10 +3108,6 @@ ble_gattc_read_uuid_err(struct ble_gattc_proc *proc, int status,
 {
     ble_gattc_dbg_assert_proc_not_inserted(proc);
 
-    if (status == BLE_HS_ATT_ERR(BLE_ATT_ERR_ATTR_NOT_FOUND)) {
-        /* Read is complete. */
-        status = 0;
-    }
     ble_gattc_read_uuid_cb(proc, status, att_handle, NULL);
 }
 


[12/50] incubator-mynewt-core git commit: BLE ctlr - Fix maybe-uninitialized warning.

Posted by st...@apache.org.
BLE ctlr - Fix maybe-uninitialized warning.

This warning was only getting reported with the "debug" profile (-O1).


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

Branch: refs/heads/sensors_branch
Commit: 7f8109d3330187668b722d5c53fcfa5258176c08
Parents: c27678e
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Dec 13 15:19:25 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/nimble/controller/src/ble_ll_sched.c | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f8109d3/net/nimble/controller/src/ble_ll_sched.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_sched.c b/net/nimble/controller/src/ble_ll_sched.c
index 99b387c..dd42c3f 100644
--- a/net/nimble/controller/src/ble_ll_sched.c
+++ b/net/nimble/controller/src/ble_ll_sched.c
@@ -565,6 +565,7 @@ ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch, uint32_t *start,
     sch->end_time += max_delay_ticks;
 
     start_overlap = NULL;
+    end_overlap = NULL;
     before = NULL;
     rc = 0;
     OS_ENTER_CRITICAL(sr);


[31/50] incubator-mynewt-core git commit: Remove empty comment block

Posted by st...@apache.org.
Remove empty comment block


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

Branch: refs/heads/sensors_branch
Commit: 98c08c00de6029542156913223e71bfda3b409d6
Parents: fe4416f
Author: Fabio Utzig <ut...@utzig.org>
Authored: Fri Dec 9 09:14:12 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/src/mmc.c | 4 ----
 1 file changed, 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98c08c00/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index 6c5dcbe..ca89434 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -168,10 +168,6 @@ send_mmc_cmd(struct mmc_cfg *mmc, uint8_t cmd, uint32_t payload)
 }
 
 /**
- * NOTE:
- */
-
-/**
  * Initialize the MMC driver
  *
  * @param spi_num Number of the SPI channel to be used by MMC


[33/50] incubator-mynewt-core git commit: ocf_sample; use cbor_read_flat_attrs() to read in payload from put light request.

Posted by st...@apache.org.
ocf_sample; use cbor_read_flat_attrs() to read in payload from
put light request.


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

Branch: refs/heads/sensors_branch
Commit: 0a07efd4882bd50dd1cd892d1324286cc7c9451c
Parents: 9cafdcd
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 10:08:45 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 apps/ocf_sample/pkg.yml    |  1 +
 apps/ocf_sample/src/main.c | 54 +++++++++++++++++++++++------------------
 2 files changed, 31 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0a07efd4/apps/ocf_sample/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/pkg.yml b/apps/ocf_sample/pkg.yml
index c18f2eb..7cf3510 100644
--- a/apps/ocf_sample/pkg.yml
+++ b/apps/ocf_sample/pkg.yml
@@ -28,6 +28,7 @@ pkg.deps:
     - kernel/os
     - sys/log
     - net/oic
+    - encoding/cborattr
     - sys/console/full
 
 pkg.deps.OC_TRANSPORT_SERIAL:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0a07efd4/apps/ocf_sample/src/main.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/main.c b/apps/ocf_sample/src/main.c
index bd6d7e1..a68c5ff 100644
--- a/apps/ocf_sample/src/main.c
+++ b/apps/ocf_sample/src/main.c
@@ -23,6 +23,7 @@
 #include <bsp/bsp.h>
 #include <log/log.h>
 #include <oic/oc_api.h>
+#include <cborattr/cborattr.h>
 #if (MYNEWT_VAL(OC_TRANSPORT_SERIAL) == 1)
 #include <console/console.h>
 #include <console/prompt.h>
@@ -54,13 +55,13 @@ get_light(oc_request_t *request, oc_interface_mask_t interface)
     printf("GET_light:\n");
     oc_rep_start_root_object();
     switch (interface) {
-        case OC_IF_BASELINE:
-            oc_process_baseline_interface(request->resource);
-        case OC_IF_RW:
-            oc_rep_set_boolean(root, state, light_state);
-            break;
-        default:
-            break;
+    case OC_IF_BASELINE:
+        oc_process_baseline_interface(request->resource);
+    case OC_IF_RW:
+        oc_rep_set_boolean(root, state, light_state);
+        break;
+    default:
+        break;
     }
     oc_rep_end_root_object();
     oc_send_response(request, OC_STATUS_OK);
@@ -70,25 +71,30 @@ get_light(oc_request_t *request, oc_interface_mask_t interface)
 static void
 put_light(oc_request_t *request, oc_interface_mask_t interface)
 {
-    printf("PUT_light:\n");
-    bool state = false;
-    oc_rep_t *rep = request->request_payload;
-    while (rep != NULL) {
-        printf("key: %s ", oc_string(rep->name));
-        switch (rep->type) {
-            case BOOL:
-                state = rep->value_boolean;
-                printf("value: %d\n", state);
-                break;
-            default:
-                oc_send_response(request, OC_STATUS_BAD_REQUEST);
-                return;
-                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;
+    };
+
+    printf("PUT_light:\n");
+
+    len = coap_get_payload(request->packet, &data);
+    if (cbor_read_flat_attrs(data, len, attrs)) {
+        oc_send_response(request, OC_STATUS_BAD_REQUEST);
+    } else {
+        printf("value: %d\n", state);
+        light_state = state;
+        oc_send_response(request, OC_STATUS_CHANGED);
     }
-    oc_send_response(request, OC_STATUS_CHANGED);
-    light_state = state;
 }
 
 static void


[04/50] incubator-mynewt-core git commit: MYNEWT-508: The ocf at the host needs to be a uint16_t.

Posted by st...@apache.org.
MYNEWT-508: The ocf at the host needs to be a uint16_t.

This is due to the vendor specific ocf added for multi-advertising.


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

Branch: refs/heads/sensors_branch
Commit: 982dfa7b979221e80d76486d64bf687450095f94
Parents: 8c876d8
Author: William San Filippo <wi...@runtime.io>
Authored: Tue Dec 13 11:45:56 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs_hci_cmd.c  | 6 +++---
 net/nimble/host/src/ble_hs_hci_priv.h | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/982dfa7b/net/nimble/host/src/ble_hs_hci_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_cmd.c b/net/nimble/host/src/ble_hs_hci_cmd.c
index 0f2d981..f8e7dde 100644
--- a/net/nimble/host/src/ble_hs_hci_cmd.c
+++ b/net/nimble/host/src/ble_hs_hci_cmd.c
@@ -47,7 +47,7 @@ ble_hs_hci_cmd_transport(uint8_t *cmdbuf)
 }
 
 void
-ble_hs_hci_cmd_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len, void *buf)
+ble_hs_hci_cmd_write_hdr(uint8_t ogf, uint16_t ocf, uint8_t len, void *buf)
 {
     uint16_t opcode;
     uint8_t *u8ptr;
@@ -60,7 +60,7 @@ ble_hs_hci_cmd_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len, void *buf)
 }
 
 int
-ble_hs_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len, const void *cmddata)
+ble_hs_hci_cmd_send(uint8_t ogf, uint16_t ocf, uint8_t len, const void *cmddata)
 {
     uint8_t *buf;
     int rc;
@@ -74,7 +74,7 @@ ble_hs_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len, const void *cmddata)
         memcpy(buf + BLE_HCI_CMD_HDR_LEN, cmddata, len);
     }
 
-    BLE_HS_LOG(DEBUG, "ble_hs_hci_cmd_send: ogf=0x%02x ocf=0x%02x len=%d\n",
+    BLE_HS_LOG(DEBUG, "ble_hs_hci_cmd_send: ogf=0x%02x ocf=0x%04x len=%d\n",
                ogf, ocf, len);
     ble_hs_log_flat_buf(buf, len + BLE_HCI_CMD_HDR_LEN);
     BLE_HS_LOG(DEBUG, "\n");

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/982dfa7b/net/nimble/host/src/ble_hs_hci_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_priv.h b/net/nimble/host/src/ble_hs_hci_priv.h
index a379730..bdc5457 100644
--- a/net/nimble/host/src/ble_hs_hci_priv.h
+++ b/net/nimble/host/src/ble_hs_hci_priv.h
@@ -58,9 +58,9 @@ int ble_hs_hci_util_data_hdr_strip(struct os_mbuf *om,
 
 int ble_hs_hci_evt_process(uint8_t *data);
 uint16_t ble_hs_hci_util_opcode_join(uint8_t ogf, uint16_t ocf);
-void ble_hs_hci_cmd_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len,
+void ble_hs_hci_cmd_write_hdr(uint8_t ogf, uint16_t ocf, uint8_t len,
                               void *buf);
-int ble_hs_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len,
+int ble_hs_hci_cmd_send(uint8_t ogf, uint16_t ocf, uint8_t len,
                         const void *cmddata);
 int ble_hs_hci_cmd_send_buf(void *cmddata);
 void ble_hs_hci_cmd_build_read_bd_addr(uint8_t *dst, int dst_len);


[49/50] incubator-mynewt-core git commit: bletiny: add services and characteristics for PTS tests

Posted by st...@apache.org.
bletiny: add services and characteristics for PTS tests

Defined GATT db for testing purposes. Placed the definitions above
already defined services to ensure that the new ones will be used in
tests.


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

Branch: refs/heads/sensors_branch
Commit: 50ab14bca7ccb14e66c6497e2f860652118c3df5
Parents: f062733
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Mon Nov 28 10:09:43 2016 +0100
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 apps/bletiny/src/gatt_svr.c | 325 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 324 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50ab14bc/apps/bletiny/src/gatt_svr.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/gatt_svr.c b/apps/bletiny/src/gatt_svr.c
index c21ca3c..af7b00d 100644
--- a/apps/bletiny/src/gatt_svr.c
+++ b/apps/bletiny/src/gatt_svr.c
@@ -25,6 +25,38 @@
 #include "host/ble_hs.h"
 #include "bletiny.h"
 
+/* 00xxxx-8c26-476f-89a7-a108033a69c7 */
+#define PTS_UUID(uuid16) ((uint8_t[16]) {                                \
+    0xc7, 0x69, 0x3a, 0x03, 0x08, 0xa1, 0xa7, 0x89,                      \
+    0x6f, 0x47, 0x26, 0x8c, (uuid16) & 0xff, (((uuid16) & 0xff00) >> 8), \
+    0x00, 0x00                                                           \
+})
+
+#define  PTS_SVC                         0x0001
+#define  PTS_CHR_READ                    0x0002
+#define  PTS_CHR_WRITE                   0x0003
+#define  PTS_CHR_RELIABLE_WRITE          0x0004
+#define  PTS_CHR_WRITE_NO_RSP            0x0005
+#define  PTS_CHR_READ_WRITE              0x0006
+#define  PTS_CHR_READ_WRITE_ENC          0x0007
+#define  PTS_CHR_READ_WRITE_AUTHEN       0x0008
+#define  PTS_DSC_READ                    0x0009
+#define  PTS_DSC_WRITE                   0x000a
+#define  PTS_DSC_READ_WRITE_ENC          0x000b
+#define  PTS_DSC_READ_WRITE_AUTHEN       0x000c
+
+#define  PTS_LONG_SVC                    0x0011
+#define  PTS_LONG_CHR_READ               0x0012
+#define  PTS_LONG_CHR_WRITE              0x0013
+#define  PTS_LONG_CHR_RELIABLE_WRITE     0x0014
+#define  PTS_LONG_CHR_READ_WRITE         0x0015
+#define  PTS_LONG_CHR_READ_WRITE_ENC     0x0016
+#define  PTS_LONG_CHR_READ_WRITE_AUTHEN  0x0017
+#define  PTS_LONG_DSC_READ               0x0018
+#define  PTS_LONG_DSC_WRITE              0x0019
+#define  PTS_LONG_DSC_READ_WRITE_ENC     0x001a
+#define  PTS_LONG_DSC_READ_WRITE_AUTHEN  0x001b
+
 /**
  * The vendor specific security test service consists of two characteristics:
  *     o random-number-generator: generates a random 32-bit number each time
@@ -60,6 +92,9 @@ const uint8_t gatt_svr_chr_sec_test_static_auth_uuid[16] = {
 
 static uint8_t gatt_svr_sec_test_static_val;
 
+static uint8_t gatt_svr_pts_static_val;
+static uint8_t gatt_svr_pts_static_long_val[30];
+
 static int
 gatt_svr_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle,
                           struct ble_gatt_access_ctxt *ctxt,
@@ -70,9 +105,137 @@ gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
                              struct ble_gatt_access_ctxt *ctxt,
                              void *arg);
 
+static int
+gatt_svr_access_test(uint16_t conn_handle, uint16_t attr_handle,
+                     struct ble_gatt_access_ctxt *ctxt,
+                     void *arg);
+
+static int
+gatt_svr_long_access_test(uint16_t conn_handle, uint16_t attr_handle,
+                                  struct ble_gatt_access_ctxt *ctxt,
+                                  void *arg);
+
 static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
     {
-        /*** Alert Notification Service. */
+        /*** Service: PTS test. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = PTS_UUID(PTS_SVC),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+                .uuid128 = PTS_UUID(PTS_CHR_READ),
+                .access_cb = gatt_svr_access_test,
+                .flags = BLE_GATT_CHR_F_READ,
+            }, {
+                .uuid128 = PTS_UUID(PTS_CHR_WRITE),
+                .access_cb = gatt_svr_access_test,
+                .flags = BLE_GATT_CHR_F_WRITE,
+            }, {
+                .uuid128 = PTS_UUID(PTS_CHR_RELIABLE_WRITE),
+                .access_cb = gatt_svr_access_test,
+                .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_RELIABLE_WRITE,
+            }, {
+                .uuid128 = PTS_UUID(PTS_CHR_WRITE_NO_RSP),
+                .access_cb = gatt_svr_access_test,
+                .flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
+            }, {
+                .uuid128 = PTS_UUID(PTS_CHR_READ_WRITE),
+                .access_cb = gatt_svr_access_test,
+                .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
+            }, {
+                .uuid128 = PTS_UUID(PTS_CHR_READ_WRITE_ENC),
+                .access_cb = gatt_svr_access_test,
+                .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC |
+                BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
+            }, {
+                .uuid128 = PTS_UUID(PTS_CHR_READ_WRITE_AUTHEN),
+                .access_cb = gatt_svr_access_test,
+                .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_AUTHEN |
+                BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_AUTHEN,
+
+                .descriptors = (struct ble_gatt_dsc_def[]){ {
+                        .uuid128 = PTS_UUID(PTS_DSC_READ),
+                        .access_cb = gatt_svr_access_test,
+                        .att_flags = BLE_ATT_F_READ,
+                    }, {
+                        .uuid128 = PTS_UUID(PTS_DSC_WRITE),
+                        .access_cb = gatt_svr_access_test,
+                        .att_flags = BLE_ATT_F_WRITE,
+                    }, {
+                        .uuid128 = PTS_UUID(PTS_DSC_READ_WRITE_ENC),
+                        .access_cb = gatt_svr_access_test,
+                        .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_ENC |
+                        BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC,
+                    }, {
+                        .uuid128 = PTS_UUID(PTS_DSC_READ_WRITE_AUTHEN),
+                        .access_cb = gatt_svr_access_test,
+                        .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_AUTHEN |
+                        BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_AUTHEN,
+                    }, {
+                        0, /* No more descriptors in this characteristic. */
+                    } }
+            }, {
+                0, /* No more characteristics in this service. */
+            } },
+    },
+
+    {
+        /*** Service: PTS long test. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = PTS_UUID(PTS_LONG_SVC),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+                .uuid128 = PTS_UUID(PTS_LONG_CHR_READ),
+                .access_cb = gatt_svr_long_access_test,
+                .flags = BLE_GATT_CHR_F_READ,
+            }, {
+                .uuid128 = PTS_UUID(PTS_LONG_CHR_WRITE),
+                .access_cb = gatt_svr_long_access_test,
+                .flags = BLE_GATT_CHR_F_WRITE,
+            }, {
+                .uuid128 = PTS_UUID(PTS_LONG_CHR_RELIABLE_WRITE),
+                .access_cb = gatt_svr_long_access_test,
+                .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_RELIABLE_WRITE,
+            }, {
+                .uuid128 = PTS_UUID(PTS_LONG_CHR_READ_WRITE),
+                .access_cb = gatt_svr_long_access_test,
+                .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
+            }, {
+                .uuid128 = PTS_UUID(PTS_LONG_CHR_READ_WRITE_ENC),
+                .access_cb = gatt_svr_long_access_test,
+                .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC |
+                BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
+            }, {
+                .uuid128 = PTS_UUID(PTS_LONG_CHR_READ_WRITE_AUTHEN),
+                .access_cb = gatt_svr_long_access_test,
+                .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_AUTHEN |
+                BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_AUTHEN,
+
+                .descriptors = (struct ble_gatt_dsc_def[]){ {
+                        .uuid128 = PTS_UUID(PTS_LONG_DSC_READ),
+                        .access_cb = gatt_svr_long_access_test,
+                        .att_flags = BLE_ATT_F_READ,
+                    }, {
+                        .uuid128 = PTS_UUID(PTS_LONG_DSC_WRITE),
+                        .access_cb = gatt_svr_long_access_test,
+                        .att_flags = BLE_ATT_F_WRITE,
+                    }, {
+                        .uuid128 = PTS_UUID(PTS_LONG_DSC_READ_WRITE_ENC),
+                        .access_cb = gatt_svr_long_access_test,
+                        .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_ENC |
+                        BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC,
+                    }, {
+                        .uuid128 = PTS_UUID(PTS_LONG_DSC_READ_WRITE_AUTHEN),
+                        .access_cb = gatt_svr_long_access_test,
+                        .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_AUTHEN |
+                        BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_AUTHEN,
+                    }, {
+                        0, /* No more descriptors in this characteristic. */
+                    } }
+            }, {
+                0, /* No more characteristics in this service. */
+            } },
+    },
+
+    {
+        /*** Alert notification service. */
         .type = BLE_GATT_SVC_TYPE_PRIMARY,
         .uuid128 = BLE_UUID16(GATT_SVR_SVC_ALERT_UUID),
         .characteristics = (struct ble_gatt_chr_def[]) { {
@@ -274,6 +437,166 @@ gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
     return BLE_ATT_ERR_UNLIKELY;
 }
 
+/* This method is used for PTS testing only, to extract 16 bit value
+ * from 128 bit vendor specific UUID.
+ */
+static uint16_t
+extract_uuid16_from_pts_uuid128(const void *uuid128)
+{
+    const uint8_t *u8ptr;
+    uint16_t uuid16;
+
+    u8ptr = uuid128;
+    uuid16 = u8ptr[12];
+    uuid16 |= (uint16_t)u8ptr[13] << 8;
+    return uuid16;
+}
+
+static int
+gatt_svr_access_test(uint16_t conn_handle, uint16_t attr_handle,
+                     struct ble_gatt_access_ctxt *ctxt,
+                     void *arg)
+{
+    uint16_t uuid16;
+    int rc;
+
+    uuid16 = extract_uuid16_from_pts_uuid128(ctxt->chr->uuid128);
+    assert(uuid16 != 0);
+
+    switch (uuid16) {
+    case PTS_CHR_READ:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+        rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val,
+                            sizeof gatt_svr_pts_static_val);
+        return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+
+    case PTS_CHR_WRITE:
+    case PTS_CHR_RELIABLE_WRITE:
+    case PTS_CHR_WRITE_NO_RSP:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR);
+        rc = gatt_svr_chr_write(ctxt->om,0,
+                                sizeof gatt_svr_pts_static_val,
+                                &gatt_svr_pts_static_val, NULL);
+        return rc;
+
+    case PTS_CHR_READ_WRITE:
+    case PTS_CHR_READ_WRITE_ENC:
+    case PTS_CHR_READ_WRITE_AUTHEN:
+        if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
+            rc = gatt_svr_chr_write(ctxt->om,0,
+                                    sizeof gatt_svr_pts_static_val,
+                                    &gatt_svr_pts_static_val, NULL);
+            return rc;
+        } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
+            rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val,
+                                sizeof gatt_svr_pts_static_val);
+            return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+        }
+
+    case PTS_DSC_READ:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC);
+        rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val,
+                            sizeof gatt_svr_pts_static_val);
+        return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+
+    case PTS_DSC_WRITE:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC);
+        rc = gatt_svr_chr_write(ctxt->om,0,
+                                sizeof gatt_svr_pts_static_val,
+                                &gatt_svr_pts_static_val, NULL);
+        return rc;
+
+    case PTS_DSC_READ_WRITE_ENC:
+    case PTS_DSC_READ_WRITE_AUTHEN:
+        if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC) {
+            rc = gatt_svr_chr_write(ctxt->om,0,
+                                    sizeof gatt_svr_pts_static_val,
+                                    &gatt_svr_pts_static_val, NULL);
+            return rc;
+        } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) {
+            rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val,
+                                sizeof gatt_svr_pts_static_val);
+            return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+        }
+
+    default:
+        assert(0);
+        return BLE_ATT_ERR_UNLIKELY;
+    }
+}
+
+static int
+gatt_svr_long_access_test(uint16_t conn_handle, uint16_t attr_handle,
+                          struct ble_gatt_access_ctxt *ctxt,
+                          void *arg)
+{
+    uint16_t uuid16;
+    int rc;
+
+    uuid16 = extract_uuid16_from_pts_uuid128(ctxt->chr->uuid128);
+    assert(uuid16 != 0);
+
+    switch (uuid16) {
+    case PTS_LONG_CHR_READ:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+        rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val,
+                            sizeof gatt_svr_pts_static_long_val);
+        return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+
+    case PTS_LONG_CHR_WRITE:
+    case PTS_LONG_CHR_RELIABLE_WRITE:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR);
+        rc = gatt_svr_chr_write(ctxt->om,0,
+                                sizeof gatt_svr_pts_static_long_val,
+                                &gatt_svr_pts_static_long_val, NULL);
+        return rc;
+
+    case PTS_LONG_CHR_READ_WRITE:
+    case PTS_LONG_CHR_READ_WRITE_ENC:
+    case PTS_LONG_CHR_READ_WRITE_AUTHEN:
+        if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
+            rc = gatt_svr_chr_write(ctxt->om,0,
+                                    sizeof gatt_svr_pts_static_long_val,
+                                    &gatt_svr_pts_static_long_val, NULL);
+            return rc;
+        } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
+            rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val,
+                                sizeof gatt_svr_pts_static_long_val);
+            return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+        }
+
+    case PTS_LONG_DSC_READ:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC);
+        rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val,
+                            sizeof gatt_svr_pts_static_long_val);
+        return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+
+    case PTS_LONG_DSC_WRITE:
+        assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC);
+        rc = gatt_svr_chr_write(ctxt->om,0,
+                                sizeof gatt_svr_pts_static_long_val,
+                                &gatt_svr_pts_static_long_val, NULL);
+        return rc;
+
+    case PTS_LONG_DSC_READ_WRITE_ENC:
+    case PTS_LONG_DSC_READ_WRITE_AUTHEN:
+        if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC) {
+            rc = gatt_svr_chr_write(ctxt->om,0,
+                                    sizeof gatt_svr_pts_static_long_val,
+                                    &gatt_svr_pts_static_long_val, NULL);
+            return rc;
+        } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) {
+            rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val,
+                                sizeof gatt_svr_pts_static_long_val);
+            return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+        }
+
+    default:
+        assert(0);
+        return BLE_ATT_ERR_UNLIKELY;
+    }
+}
+
 static char *
 gatt_svr_uuid_to_s(const void *uuid128, char *dst)
 {


[46/50] incubator-mynewt-core git commit: oic; ble, change reassembly take place in write callback from host. Only allocate oc_endpoint mbuf for the first fragment.

Posted by st...@apache.org.
oic; ble, change reassembly take place in write callback from host.
Only allocate oc_endpoint mbuf for the first fragment.


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

Branch: refs/heads/sensors_branch
Commit: 7bdbd5e93fe03fd9e99e7459e7d8b3ee18ad0c03
Parents: f65669d
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 14 16:43:22 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/port/mynewt/ble_adaptor.c | 196 ++++++++++-------------------
 1 file changed, 65 insertions(+), 131 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7bdbd5e9/net/oic/src/port/mynewt/ble_adaptor.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/ble_adaptor.c b/net/oic/src/port/mynewt/ble_adaptor.c
index 14128f2..23742b9 100644
--- a/net/oic/src/port/mynewt/ble_adaptor.c
+++ b/net/oic/src/port/mynewt/ble_adaptor.c
@@ -75,8 +75,6 @@ STATS_NAME_START(oc_ble_stats)
     STATS_NAME(oc_ble_stats, oerr)
 STATS_NAME_END(oc_ble_stats)
 
-/* queue to hold mbufs until we get called from oic */
-static struct os_mqueue oc_ble_coap_mq;
 static STAILQ_HEAD(, os_mbuf_pkthdr) oc_ble_reass_q;
 
 #if (MYNEWT_VAL(OC_SERVER) == 1)
@@ -115,6 +113,70 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = { {
     },
 };
 
+int
+oc_ble_reass(struct os_mbuf *om1, uint16_t conn_handle)
+{
+    struct os_mbuf_pkthdr *pkt1;
+    struct oc_endpoint *oe;
+    struct os_mbuf *om2;
+    struct os_mbuf_pkthdr *pkt2;
+    uint8_t hdr[6]; /* sizeof(coap_tcp_hdr32) */
+
+    pkt1 = OS_MBUF_PKTHDR(om1);
+    assert(pkt1);
+
+    STATS_INC(oc_ble_stats, iseg);
+    STATS_INCN(oc_ble_stats, ibytes, pkt1->omp_len);
+
+    OC_LOG_DEBUG("oc_gatt rx seg %u-%x-%u\n", conn_handle,
+                 (unsigned)pkt1, pkt1->omp_len);
+
+    STAILQ_FOREACH(pkt2, &oc_ble_reass_q, omp_next) {
+        om2 = OS_MBUF_PKTHDR_TO_MBUF(pkt2);
+        oe = OC_MBUF_ENDPOINT(om2);
+        if (conn_handle == oe->bt_addr.conn_handle) {
+            /*
+             * Data from same connection. Append.
+             */
+            os_mbuf_concat(om2, om1);
+            os_mbuf_copydata(om2, 0, sizeof(hdr), hdr);
+
+            if (coap_tcp_msg_size(hdr, sizeof(hdr)) <= pkt2->omp_len) {
+                STAILQ_REMOVE(&oc_ble_reass_q, pkt2, os_mbuf_pkthdr, omp_next);
+                oc_recv_message(om2);
+            }
+            pkt1 = NULL;
+            break;
+        }
+    }
+    if (pkt1) {
+        /*
+         * New frame
+         */
+        om2 = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint));
+        if (!om2) {
+            OC_LOG_ERROR("oc_gatt_rx: Could not allocate mbuf\n");
+            STATS_INC(oc_ble_stats, ierr);
+            return -1;
+        }
+        OS_MBUF_PKTHDR(om2)->omp_len = pkt1->omp_len;
+        SLIST_NEXT(om2, om_next) = om1;
+
+        oe = OC_MBUF_ENDPOINT(om2);
+        oe->flags = GATT;
+        oe->bt_addr.conn_handle = conn_handle;
+        pkt2 = OS_MBUF_PKTHDR(om2);
+
+        if (os_mbuf_copydata(om2, 0, sizeof(hdr), hdr) ||
+          coap_tcp_msg_size(hdr, sizeof(hdr)) > pkt2->omp_len) {
+            STAILQ_INSERT_TAIL(&oc_ble_reass_q, pkt2, omp_next);
+        } else {
+            oc_recv_message(om2);
+        }
+    }
+    return 0;
+}
+
 static int
 oc_gatt_chr_access(uint16_t conn_handle, uint16_t attr_handle,
                    struct ble_gatt_access_ctxt *ctxt, void *arg)
@@ -127,13 +189,7 @@ oc_gatt_chr_access(uint16_t conn_handle, uint16_t attr_handle,
     case BLE_GATT_ACCESS_OP_WRITE_CHR:
         m = ctxt->om;
 
-        /* stick the conn handle at the end of the frame -- we will
-         * pull it out later */
-        rc = os_mbuf_append(m, &conn_handle, sizeof(conn_handle));
-        if (rc) {
-            return BLE_ATT_ERR_INSUFFICIENT_RES;
-        }
-        rc = os_mqueue_put(&oc_ble_coap_mq, oc_evq_get(), m);
+        rc = oc_ble_reass(m, conn_handle);
         if (rc) {
             return BLE_ATT_ERR_INSUFFICIENT_RES;
         }
@@ -148,60 +204,6 @@ oc_gatt_chr_access(uint16_t conn_handle, uint16_t attr_handle,
     }
     return 0;
 }
-
-static struct os_mbuf *
-oc_attempt_rx_gatt(void)
-{
-    int rc;
-    struct os_mbuf *m;
-    struct os_mbuf *n;
-    struct os_mbuf_pkthdr *pkt;
-    struct oc_endpoint *oe;
-    uint16_t conn_handle;
-
-    /* get an mbuf from the queue */
-    n = os_mqueue_get(&oc_ble_coap_mq);
-    if (NULL == n) {
-        return NULL;
-    }
-
-    pkt = OS_MBUF_PKTHDR(n);
-
-    STATS_INC(oc_ble_stats, iseg);
-    STATS_INCN(oc_ble_stats, ibytes, pkt->omp_len);
-
-    /* get the conn handle from the end of the message */
-    rc = os_mbuf_copydata(n, pkt->omp_len - sizeof(conn_handle),
-                          sizeof(conn_handle), &conn_handle);
-    if (rc != 0) {
-        OC_LOG_ERROR("oc_gatt_rx: Failed to get conn_handle from mbuf\n");
-        goto rx_attempt_err;
-    }
-
-    /* trim conn_handle from the end */
-    os_mbuf_adj(n, - sizeof(conn_handle));
-
-    m = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint));
-    if (!m) {
-        OC_LOG_ERROR("oc_gatt_rx: Could not allocate mbuf\n");
-        goto rx_attempt_err;
-    }
-    OS_MBUF_PKTHDR(m)->omp_len = pkt->omp_len;
-    SLIST_NEXT(m, om_next) = n;
-
-    oe = OC_MBUF_ENDPOINT(m);
-
-    oe->flags = GATT;
-    oe->bt_addr.conn_handle = conn_handle;
-
-    return m;
-
-    /* add the addr info to the message */
-rx_attempt_err:
-    STATS_INC(oc_ble_stats, ierr);
-    os_mbuf_free_chain(n);
-    return NULL;
-}
 #endif
 
 int
@@ -227,60 +229,6 @@ oc_ble_coap_gatt_srv_init(void)
 }
 
 void
-oc_ble_reass(struct os_mbuf *om1)
-{
-    struct os_mbuf_pkthdr *pkt1;
-    struct oc_endpoint *oe1;
-    struct os_mbuf *om2;
-    struct os_mbuf_pkthdr *pkt2;
-    struct oc_endpoint *oe2;
-    int sr;
-    uint8_t hdr[6]; /* sizeof(coap_tcp_hdr32) */
-
-    pkt1 = OS_MBUF_PKTHDR(om1);
-    assert(pkt1);
-    oe1 = OC_MBUF_ENDPOINT(om1);
-
-    OC_LOG_DEBUG("oc_gatt rx seg %d-%x-%u\n", oe1->bt_addr.conn_handle,
-      (unsigned)pkt1, pkt1->omp_len);
-
-    OS_ENTER_CRITICAL(sr);
-    STAILQ_FOREACH(pkt2, &oc_ble_reass_q, omp_next) {
-        om2 = OS_MBUF_PKTHDR_TO_MBUF(pkt2);
-        oe2 = OC_MBUF_ENDPOINT(om2);
-        if (oe1->bt_addr.conn_handle == oe2->bt_addr.conn_handle) {
-            /*
-             * Data from same connection. Append.
-             */
-            os_mbuf_concat(om2, om1);
-            if (os_mbuf_copydata(om2, 0, sizeof(hdr), hdr)) {
-                pkt1 = NULL;
-                break;
-            }
-            if (coap_tcp_msg_size(hdr, sizeof(hdr)) <= pkt2->omp_len) {
-                STAILQ_REMOVE(&oc_ble_reass_q, pkt2, os_mbuf_pkthdr,
-                  omp_next);
-                oc_recv_message(om2);
-            }
-            pkt1 = NULL;
-            break;
-        }
-    }
-    if (pkt1) {
-        /*
-         *
-         */
-        if (os_mbuf_copydata(om1, 0, sizeof(hdr), hdr) ||
-          coap_tcp_msg_size(hdr, sizeof(hdr)) > pkt1->omp_len) {
-            STAILQ_INSERT_TAIL(&oc_ble_reass_q, pkt1, omp_next);
-        } else {
-            oc_recv_message(om1);
-        }
-    }
-    OS_EXIT_CRITICAL(sr);
-}
-
-void
 oc_ble_coap_conn_new(uint16_t conn_handle)
 {
     OC_LOG_DEBUG("oc_gatt newconn %x\n", conn_handle);
@@ -292,10 +240,8 @@ oc_ble_coap_conn_del(uint16_t conn_handle)
     struct os_mbuf_pkthdr *pkt;
     struct os_mbuf *m;
     struct oc_endpoint *oe;
-    int sr;
 
     OC_LOG_DEBUG("oc_gatt endconn %x\n", conn_handle);
-    OS_ENTER_CRITICAL(sr);
     STAILQ_FOREACH(pkt, &oc_ble_reass_q, omp_next) {
         m = OS_MBUF_PKTHDR_TO_MBUF(pkt);
         oe = OC_MBUF_ENDPOINT(m);
@@ -305,23 +251,11 @@ oc_ble_coap_conn_del(uint16_t conn_handle)
             break;
         }
     }
-    OS_EXIT_CRITICAL(sr);
-}
-
-static void
-oc_event_gatt(struct os_event *ev)
-{
-    struct os_mbuf *m;
-
-    while ((m = oc_attempt_rx_gatt()) != NULL) {
-        oc_ble_reass(m);
-    }
 }
 
 int
 oc_connectivity_init_gatt(void)
 {
-    os_mqueue_init(&oc_ble_coap_mq, oc_event_gatt, NULL);
     STAILQ_INIT(&oc_ble_reass_q);
     return 0;
 }


[02/50] incubator-mynewt-core git commit: util/mem - Generic packet fragmentation function.

Posted by st...@apache.org.
util/mem - Generic packet fragmentation function.

/**
 * Specifies a function used as a callback.  Functions of this type allocate an
 * mbuf chain meant to hold a packet fragment.  The resulting mbuf must contain
 * a pkthdr.
 *
 * @param frag_size             The number of data bytes that the mbuf will
 *                                  eventually contain.
 * @param arg                   A generic parameter.
 *
 * @return                      An allocated mbuf chain on success;
 *                              NULL on failure.
 */
typedef struct os_mbuf *mem_frag_alloc_fn(uint16_t frag_size, void *arg);

/*
 * Splits an appropriately-sized fragment from the front of an mbuf chain, as
 * neeeded.  If the length of the mbuf chain greater than specified maximum
 * fragment size, a new mbuf is allocated, and data is moved from the source
 * mbuf to the new mbuf.  If the mbuf chain is small enough to fit in a single
 * fragment, the source mbuf itself is returned unmodified, and the suplied
 * pointer is set to NULL.
 *
 * This function is expected to be called in a loop until the entire mbuf chain
 * has been consumed.  For example:
 *
 *     struct os_mbuf *frag;
 *     struct os_mbuf *rsp;
 *     // [...]
 *     while (rsp != NULL) {
 *         frag = mem_split_frag(&rsp, get_mtu(), frag_alloc, NULL);
 *         if (frag == NULL) {
 *             os_mbuf_free_chain(rsp);
 *             return SYS_ENOMEM;
 *         }
 *         send_packet(frag)
 *     }
 *
 * @param om                    The packet to fragment.  Upon fragmentation,
 *                                  this mbuf is adjusted such that the
 *                                  fragment data is removed.  If the packet
 *                                  constitutes a single fragment, this gets
 *                                  set to NULL on success.
 * @param max_frag_sz           The maximum payload size of a fragment.
 *                                  Typically this is the MTU of the
 *                                  connection.
 * @param alloc_cb              Points to a function that allocates an mbuf to
 *                                  hold a fragment.  This function gets called
 *                                  before the source mbuf chain is modified,
 *                                  so it can safely inspect it.
 * @param cb_arg                Generic parameter that gets passed to the
 *                                  callback function.
 *
 * @return                      The next fragment to send on success;
 *                              NULL on failure.
 */


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

Branch: refs/heads/sensors_branch
Commit: 93cb0c08c8226fc729e2e824ebf8947014e310c3
Parents: 7f8109d
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Dec 13 18:06:31 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 util/mem/include/mem/mem.h | 17 +++++++++
 util/mem/src/mem.c         | 79 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93cb0c08/util/mem/include/mem/mem.h
----------------------------------------------------------------------
diff --git a/util/mem/include/mem/mem.h b/util/mem/include/mem/mem.h
index 2ea7276..18461b6 100644
--- a/util/mem/include/mem/mem.h
+++ b/util/mem/include/mem/mem.h
@@ -42,6 +42,23 @@ int mem_init_mbuf_pool(void *mem, struct os_mempool *mempool,
                        struct os_mbuf_pool *mbuf_pool, int num_blocks,
                        int block_size, char *name);
 
+/**
+ * Specifies a function used as a callback.  Functions of this type allocate an
+ * mbuf chain meant to hold a packet fragment.  The resulting mbuf must contain
+ * a pkthdr.
+ *
+ * @param frag_size             The number of data bytes that the mbuf will
+ *                                  eventually contain.
+ * @param arg                   A generic parameter.
+ *
+ * @return                      An allocated mbuf chain on success;
+ *                              NULL on failure.
+ */
+typedef struct os_mbuf *mem_frag_alloc_fn(uint16_t frag_size, void *arg);
+
+struct os_mbuf *mem_split_frag(struct os_mbuf **om, uint16_t max_frag_sz,
+                               mem_frag_alloc_fn *alloc_cb, void *cb_arg);
+
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93cb0c08/util/mem/src/mem.c
----------------------------------------------------------------------
diff --git a/util/mem/src/mem.c b/util/mem/src/mem.c
index 637edbc..340e60e 100644
--- a/util/mem/src/mem.c
+++ b/util/mem/src/mem.c
@@ -18,6 +18,7 @@
  */
 
 #include "os/os.h"
+#include "mem/mem.h"
 
 /**
  * Mallocs a block of memory and initializes a mempool to use it.
@@ -164,3 +165,81 @@ mem_init_mbuf_pool(void *mem, struct os_mempool *mempool,
 
     return 0;
 }
+
+/*
+ * Splits an appropriately-sized fragment from the front of an mbuf chain, as
+ * neeeded.  If the length of the mbuf chain greater than specified maximum
+ * fragment size, a new mbuf is allocated, and data is moved from the source
+ * mbuf to the new mbuf.  If the mbuf chain is small enough to fit in a single
+ * fragment, the source mbuf itself is returned unmodified, and the suplied
+ * pointer is set to NULL.
+ *
+ * This function is expected to be called in a loop until the entire mbuf chain
+ * has been consumed.  For example:
+ *
+ *     struct os_mbuf *frag;
+ *     struct os_mbuf *rsp;
+ *     // [...]
+ *     while (rsp != NULL) {
+ *         frag = mem_split_frag(&rsp, get_mtu(), frag_alloc, NULL);
+ *         if (frag == NULL) {
+ *             os_mbuf_free_chain(rsp);
+ *             return SYS_ENOMEM;
+ *         }
+ *         send_packet(frag)
+ *     }
+ *
+ * @param om                    The packet to fragment.  Upon fragmentation,
+ *                                  this mbuf is adjusted such that the
+ *                                  fragment data is removed.  If the packet
+ *                                  constitutes a single fragment, this gets
+ *                                  set to NULL on success.
+ * @param max_frag_sz           The maximum payload size of a fragment.
+ *                                  Typically this is the MTU of the
+ *                                  connection.
+ * @param alloc_cb              Points to a function that allocates an mbuf to
+ *                                  hold a fragment.  This function gets called
+ *                                  before the source mbuf chain is modified,
+ *                                  so it can safely inspect it.
+ * @param cb_arg                Generic parameter that gets passed to the
+ *                                  callback function.
+ *
+ * @return                      The next fragment to send on success;
+ *                              NULL on failure.
+ */
+struct os_mbuf *
+mem_split_frag(struct os_mbuf **om, uint16_t max_frag_sz,
+               mem_frag_alloc_fn *alloc_cb, void *cb_arg)
+{
+    struct os_mbuf *frag;
+    int rc;
+
+    if (OS_MBUF_PKTLEN(*om) <= max_frag_sz) {
+        /* Final fragment. */
+        frag = *om;
+        *om = NULL;
+        return frag;
+    }
+
+    /* Packet needs to be split.  Allocate a new buffer for the fragment. */
+    frag = alloc_cb(max_frag_sz, cb_arg);
+    if (frag == NULL) {
+        goto err;
+    }
+
+    /* Move data from the front of the packet into the fragment mbuf. */
+    rc = os_mbuf_appendfrom(frag, *om, 0, max_frag_sz);
+    if (rc != 0) {
+        goto err;
+    }
+    os_mbuf_adj(*om, max_frag_sz);
+
+    /* Free unused portion of of source mbuf chain, if possible. */
+    *om = os_mbuf_trim_front(*om);
+
+    return frag;
+
+err:
+    os_mbuf_free_chain(frag);
+    return NULL;
+}


[27/50] incubator-mynewt-core git commit: Fix multi-block write, misc refactors

Posted by st...@apache.org.
Fix multi-block write, misc refactors


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

Branch: refs/heads/sensors_branch
Commit: 791236aa6cc779c2ac75b15d23c3674e21eac701
Parents: 8d45cf8
Author: Fabio Utzig <ut...@utzig.org>
Authored: Fri Dec 9 08:49:43 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/src/mmc.c | 120 ++++++++++++++++++++++++------------------
 1 file changed, 68 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/791236aa/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index f0fe71d..653e08f 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -58,6 +58,10 @@
 #define R_ADDR_ERROR        (0x20)
 #define R_PARAM_ERROR       (0x40)
 
+#define START_BLOCK         (0xFE)
+#define START_BLOCK_TOKEN   (0xFC)
+#define STOP_TRAN_TOKEN     (0xFD)
+
 #define BLOCK_LEN           (512)
 
 static uint8_t g_block_buf[BLOCK_LEN];
@@ -80,7 +84,7 @@ static struct mmc_cfg {
 } g_mmc_cfg;
 
 static int
-mmc_error_by_status(uint8_t status)
+error_by_response(uint8_t status)
 {
     if (status == 0) {
         return MMC_OK;
@@ -153,8 +157,6 @@ send_mmc_cmd(struct mmc_cfg *mmc, uint8_t cmd, uint32_t payload)
     }
     hal_spi_tx_val(mmc->spi_num, crc);
 
-    //printf("==> sending cmd %d\n", cmd & ~0x80);
-
     for (n = 255; n > 0; n--) {
         status = hal_spi_tx_val(mmc->spi_num, 0xff);
         if ((status & 0x80) == 0) break;
@@ -231,11 +233,19 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
 
     /* No card inserted or bad card? */
     if (status != R_IDLE) {
-        rc = mmc_error_by_status(status);
+        rc = error_by_response(status);
         goto out;
     }
 
     /**
+     * FIXME: while doing a hot-swap of the card or powering off the board
+     * it is required to send a CMD1 here otherwise CMD8's response will
+     * be always invalid.
+     *
+     * Why is this happening? CMD1 should never be required for SDxC cards...
+     */
+
+    /**
      * 4.3.13: Ask for 2.7-3.3V range, send 0xAA pattern
      *
      * NOTE: cards that are not compliant with "Physical Spec Version 2.00"
@@ -282,7 +292,7 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
         }
 
         if (status) {
-            rc = mmc_error_by_status(status);
+            rc = error_by_response(status);
             goto out;
         }
 
@@ -298,16 +308,11 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
             /**
              * TODO: if CCS is set this is an SDHC or SDXC card!
              *       SDSC uses byte addressing, SDHC/SDXC block addressing
+             *
+             *       can also check the whole voltage range supported here
              */
         }
 
-#if 0  /* TODO: Can check all possible voltages supported by card */
-    printf("Card supports: ");
-    if (ocr & (1 << 15)) {
-        printf("2.7-2.8 ");
-    }
-#endif
-
     } else if (status != 0xff && status & R_ILLEGAL_COMMAND) {
 
         /**
@@ -325,7 +330,7 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
         /* TODO: set blocklen */
 
     } else {
-        rc = mmc_error_by_status(status);
+        rc = error_by_response(status);
     }
 
 out:
@@ -334,6 +339,27 @@ out:
 }
 
 /**
+ * Commands that return response in R1b format and write
+ * commands enter busy state and keep return 0 while the
+ * operations are in progress.
+ */
+static uint8_t
+wait_busy(struct mmc_cfg *mmc)
+{
+    os_time_t timeout;
+    uint8_t res;
+
+    timeout = os_time_get() + OS_TICKS_PER_SEC / 2;
+    do {
+        res = hal_spi_tx_val(mmc->spi_num, 0xff);
+        if (res) break;
+        os_time_delay(OS_TICKS_PER_SEC / 1000);
+    } while (os_time_get() < timeout);
+
+    return res;
+}
+
+/**
  * @return 0 on success, non-zero on failure
  */
 int
@@ -364,15 +390,12 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
     block_addr = addr / BLOCK_LEN;
     offset = addr - (block_addr * BLOCK_LEN);
 
-    //printf("addr=0x%lx, block_addr=0x%lx, offset=%d\n", addr, block_addr, offset);
-    //printf("len=%d, block_len=%d, block_count=%d\n", len, block_len, block_count);
-
     hal_gpio_write(mmc->ss_pin, 0);
 
     cmd = (block_count == 1) ? CMD17 : CMD18;
     res = send_mmc_cmd(mmc, cmd, block_addr);
     if (res) {
-        rc = mmc_error_by_status(res);
+        rc = error_by_response(res);
         goto out;
     }
 
@@ -390,7 +413,7 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
     /**
      * 7.3.3.2 Start Block Tokens and Stop Tran Token
      */
-    if (res != 0xFE) {
+    if (res != START_BLOCK) {
         rc = MMC_TIMEOUT;
         goto out;
     }
@@ -416,8 +439,7 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
 
     if (cmd == CMD18) {
         send_mmc_cmd(mmc, CMD12, 0);
-        /* FIXME: sending extra cycle to wait for stop, need to loop here? */
-        hal_spi_tx_val(mmc->spi_num, 0xff);
+        wait_busy(mmc);
     }
 
 out:
@@ -441,7 +463,7 @@ mmc_write(uint8_t mmc_id, uint32_t addr, const void *buf, size_t len)
     size_t offset;
     size_t index;
     size_t amount;
-    int status;
+    int rc;
     struct mmc_cfg *mmc;
 
     mmc = mmc_cfg_dev(mmc_id);
@@ -466,9 +488,9 @@ mmc_write(uint8_t mmc_id, uint32_t addr, const void *buf, size_t len)
      */
     if (offset) {
         res = send_mmc_cmd(mmc, CMD17, block_addr);
-        if (res != MMC_OK) {
-            hal_gpio_write(mmc->ss_pin, 1);
-            return (MMC_CARD_ERROR);
+        if (res != 0) {
+            rc = error_by_response(res);
+            goto out;
         }
 
         timeout = os_time_get() + OS_TICKS_PER_SEC / 5;
@@ -478,9 +500,9 @@ mmc_write(uint8_t mmc_id, uint32_t addr, const void *buf, size_t len)
             os_time_delay(OS_TICKS_PER_SEC / 20);
         } while (os_time_get() < timeout);
 
-        if (res != 0xfe) {
-            hal_gpio_write(mmc->ss_pin, 1);
-            return (MMC_CARD_ERROR);
+        if (res != START_BLOCK) {
+            rc = MMC_CARD_ERROR;
+            goto out;
         }
 
         for (n = 0; n < BLOCK_LEN; n++) {
@@ -495,25 +517,20 @@ mmc_write(uint8_t mmc_id, uint32_t addr, const void *buf, size_t len)
 
     cmd = (block_count == 1) ? CMD24 : CMD25;
     res = send_mmc_cmd(mmc, cmd, block_addr);
-    if (res != MMC_OK) {
-        hal_gpio_write(mmc->ss_pin, 1);
-        return (MMC_CARD_ERROR);
+    if (res) {
+        rc = error_by_response(res);
+        goto out;
     }
 
-    /* FIXME: one byte gap, is this really required? */
-    hal_spi_tx_val(mmc->spi_num, 0xff);
-
     index = 0;
-    do {
-        /* FIXME: must wait for ready here? */
-
+    while (block_count--) {
         /**
          * 7.3.3.2 Start Block Tokens and Stop Tran Token
          */
         if (cmd == CMD24) {
-            hal_spi_tx_val(mmc->spi_num, 0xFE);
+            hal_spi_tx_val(mmc->spi_num, START_BLOCK);
         } else {
-            hal_spi_tx_val(mmc->spi_num, 0xFC);
+            hal_spi_tx_val(mmc->spi_num, START_BLOCK_TOKEN);
         }
 
         amount = MIN(BLOCK_LEN - offset, len);
@@ -535,37 +552,36 @@ mmc_write(uint8_t mmc_id, uint32_t addr, const void *buf, size_t len)
             break;
         }
 
+        if (cmd == CMD25) {
+            wait_busy(mmc);
+        }
+
         offset = 0;
         len -= amount;
         index += amount;
+    }
 
-    } while (len);
-
-    /* TODO: send stop tran token */
     if (cmd == CMD25) {
+        hal_spi_tx_val(mmc->spi_num, STOP_TRAN_TOKEN);
+        wait_busy(mmc);
     }
 
     res &= 0x1f;
     switch (res) {
         case 0x05:
-            status = MMC_OK;
+            rc = MMC_OK;
             break;
         case 0x0b:
-            status = MMC_CRC_ERROR;
+            rc = MMC_CRC_ERROR;
             break;
         case 0x0d:  /* passthrough */
         default:
-            status = MMC_WRITE_ERROR;
+            rc = MMC_WRITE_ERROR;
     }
 
-    timeout = os_time_get() + 5 * OS_TICKS_PER_SEC;
-    do {
-        res = hal_spi_tx_val(mmc->spi_num, 0xff);
-        if (res) break;
-        os_time_delay(OS_TICKS_PER_SEC / 100);
-    } while (os_time_get() < timeout);
+    wait_busy(mmc);
 
+out:
     hal_gpio_write(mmc->ss_pin, 1);
-
-    return status;
+    return (rc);
 }


[47/50] incubator-mynewt-core git commit: bleprph_oic; increase the number of mbufs allocated.

Posted by st...@apache.org.
bleprph_oic; increase the number of mbufs allocated.


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

Branch: refs/heads/sensors_branch
Commit: 4947fa48f7ff6a7efdbaebee5fd6e479379d91b7
Parents: 429ab09
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Dec 15 12:26:47 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 apps/bleprph_oic/syscfg.yml | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4947fa48/apps/bleprph_oic/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph_oic/syscfg.yml b/apps/bleprph_oic/syscfg.yml
index f9ec211..677c5fe 100644
--- a/apps/bleprph_oic/syscfg.yml
+++ b/apps/bleprph_oic/syscfg.yml
@@ -20,15 +20,27 @@ syscfg.vals:
     # Use INFO log level to reduce code size.  DEBUG is too large for nRF51.
     LOG_LEVEL: 0
 
+    BLE_GATT_WRITE_MAX_ATTRS: 6
+    BLE_HCI_EVT_HI_BUF_COUNT: 4
+    BLE_HCI_EVT_LO_BUF_COUNT: 3
+
     # Disable central and observer roles.
     BLE_ROLE_BROADCASTER: 1
     BLE_ROLE_CENTRAL: 0
     BLE_ROLE_OBSERVER: 0
     BLE_ROLE_PERIPHERAL: 1
 
+    # Disable security manager
+    BLE_SM: 0
+    BLE_LL_CFG_FEAT_LE_ENCRYPTION: 0
+    BLE_LL_CFG_FEAT_LL_PRIVACY: 0
+
     # Disable unused eddystone feature.
     BLE_EDDYSTONE: 0
 
+    # Need to get this down. XXXXX
+    MSYS_1_BLOCK_COUNT: 18
+
     # Log reboot messages to a flash circular buffer.
     REBOOT_LOG_FCB: 1
     LOG_FCB: 1
@@ -41,9 +53,9 @@ syscfg.vals:
     # Enable Config.
     CONFIG_NEWTMGR: 1
 
+    # OC server, with bluetooth transport.
     OC_SERVER: 1
     OC_TRANSPORT_GATT: 1
-    OC_TRANSPORT_SERIAL: 1
 
     SHELL_TASK: 1
     STATS_CLI: 1


[41/50] incubator-mynewt-core git commit: oic; reverse the order in which fragments are created from original packet. This allows os_mbuf_adj() of original packet to free unused mbufs, when possible.

Posted by st...@apache.org.
oic; reverse the order in which fragments are created from original
packet. This allows os_mbuf_adj() of original packet to free
unused mbufs, when possible.


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

Branch: refs/heads/sensors_branch
Commit: c8c06dd14d0adff1f6dc9f61186030d52edc0323
Parents: 7dca12c
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Dec 16 11:08:00 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/port/mynewt/ble_adaptor.c | 16 +++++++---------
 net/oic/src/port/oc_connectivity.h    |  3 +--
 2 files changed, 8 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c8c06dd1/net/oic/src/port/mynewt/ble_adaptor.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/ble_adaptor.c b/net/oic/src/port/mynewt/ble_adaptor.c
index 23742b9..39ceac9 100644
--- a/net/oic/src/port/mynewt/ble_adaptor.c
+++ b/net/oic/src/port/mynewt/ble_adaptor.c
@@ -278,25 +278,22 @@ oc_ble_frag(struct os_mbuf *m, uint16_t mtu)
         STAILQ_NEXT(pkt, omp_next) = NULL;
         return 0;
     }
-    off = mtu;
+    off = pkt->omp_len % mtu;
 
-    while (off < OS_MBUF_PKTLEN(m)) {
+    while (off > mtu) {
         n = os_msys_get_pkthdr(mtu, 0);
         if (!n) {
             goto err;
         }
-
+        STAILQ_NEXT(OS_MBUF_PKTHDR(n), omp_next) = STAILQ_NEXT(pkt, omp_next);
         STAILQ_NEXT(pkt, omp_next) = OS_MBUF_PKTHDR(n);
-        pkt = STAILQ_NEXT(pkt, omp_next);
 
-        blk = OS_MBUF_PKTLEN(m) - off;
-        if (blk > mtu) {
-            blk = mtu;
-        }
+        blk = pkt->omp_len - off;
         if (os_mbuf_appendfrom(n, m, off, blk)) {
             goto err;
         }
-        off += blk;
+        off -= blk;
+        os_mbuf_adj(m, -blk);
     }
     os_mbuf_adj(m, mtu - OS_MBUF_PKTLEN(m));
     return 0;
@@ -334,6 +331,7 @@ oc_send_buffer_gatt(struct os_mbuf *m)
     STATS_INCN(oc_ble_stats, obytes, OS_MBUF_PKTLEN(m));
 
     mtu = ble_att_mtu(oe->bt_addr.conn_handle);
+    assert(mtu > 4);
     mtu -= 3; /* # of bytes for ATT notification base */
 
     if (oc_ble_frag(m, mtu)) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c8c06dd1/net/oic/src/port/oc_connectivity.h
----------------------------------------------------------------------
diff --git a/net/oic/src/port/oc_connectivity.h b/net/oic/src/port/oc_connectivity.h
index ea44e3e..7f50a6b 100644
--- a/net/oic/src/port/oc_connectivity.h
+++ b/net/oic/src/port/oc_connectivity.h
@@ -41,9 +41,8 @@ typedef struct oc_endpoint {
         IPSP = 1 << 2,
         MULTICAST = 1 << 3,
         SECURED = 1 << 4,
-        SERIAL = 1 <<5,
+        SERIAL = 1 << 5,
     } flags;
-
     union {
         oc_ipv6_addr_t ipv6_addr;
         oc_le_addr_t bt_addr;


[10/50] incubator-mynewt-core git commit: Add write support - WIP

Posted by st...@apache.org.
Add write support - WIP


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

Branch: refs/heads/sensors_branch
Commit: 92c4a5d74785fa21fcfd8c4587ef062d3c1b536a
Parents: 964ea25
Author: Fabio Utzig <ut...@utzig.org>
Authored: Mon Dec 5 15:15:14 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/include/mmc/mmc.h |  20 +++
 hw/drivers/mmc/src/mmc.c         | 234 +++++++++++++++++++++++++++-------
 2 files changed, 205 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/92c4a5d7/hw/drivers/mmc/include/mmc/mmc.h
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/include/mmc/mmc.h b/hw/drivers/mmc/include/mmc/mmc.h
index 9c805ec..add9b9b 100644
--- a/hw/drivers/mmc/include/mmc/mmc.h
+++ b/hw/drivers/mmc/include/mmc/mmc.h
@@ -34,6 +34,8 @@ extern "C" {
 #define MMC_READ_ERROR      (-2)
 #define MMC_WRITE_ERROR     (-3)
 #define MMC_TIMEOUT         (-4)
+#define MMC_PARAM           (-5)
+#define MMC_CRC_ERROR       (-6)
 
 /**
  * Initialize the MMC driver
@@ -47,9 +49,27 @@ extern "C" {
 int
 mmc_init(int spi_num, void *spi_cfg, int ss_pin);
 
+/**
+ * Read data from MMC
+ *
+ * @param addr
+ * @param buf
+ * @param len
+ *
+ * @return 0 on success, non-zero on failure
+ */
 int
 mmc_read(uint32_t addr, void *buf, size_t len);
 
+/**
+ * Write data to the MMC
+ *
+ * @param addr
+ * @param buf
+ * @param len
+ *
+ * @return 0 on success, non-zero on failure
+ */
 int
 mmc_write(uint32_t addr, void *buf, size_t len);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/92c4a5d7/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index be7f54c..92acc45 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -26,7 +26,7 @@
 
 #define MIN(n, m) (((n) < (m)) ? (n) : (m))
 
-/* Current used MMC commands */
+/* Currently used MMC commands */
 #define CMD0                (0)            /* GO_IDLE_STATE */
 #define CMD1                (1)            /* SEND_OP_COND (MMC) */
 #define CMD8                (8)            /* SEND_IF_COND */
@@ -68,6 +68,7 @@ static struct hal_spi_settings mmc_settings = {
     .word_size  = HAL_SPI_WORD_SIZE_8BIT,
 };
 
+/* FIXME: this limits usage to single MMC spi device */
 static int g_spi_num = -1;
 static int g_ss_pin = -1;
 
@@ -78,6 +79,18 @@ static int g_ss_pin = -1;
 static int
 mmc_error_by_status(uint8_t status)
 {
+    if (status == 0xff) {
+        return MMC_CARD_ERROR;
+    } else if (status & R_IDLE) {
+        return MMC_TIMEOUT;
+    } else if (status & R_ERASE_RESET) {
+    } else if (status & R_ILLEGAL_COMMAND) {
+    } else if (status & R_CRC_ERROR) {
+    } else if (status & R_ERASE_ERROR) {
+    } else if (status & R_ADDR_ERROR) {
+    } else if (status & R_PARAM_ERROR) {
+    }
+
     return MMC_OK;
 }
 
@@ -145,52 +158,57 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
      *   NOTE: CRC can be turned on with CMD59 (CRC_ON_OFF).
      */
     if (cmd == CMD0) {
-        status = hal_spi_tx_val(g_spi_num, 0x95);
+        hal_spi_tx_val(g_spi_num, 0x95);
     } else if (cmd == CMD8) {
-        status = hal_spi_tx_val(g_spi_num, 0x87);
+        hal_spi_tx_val(g_spi_num, 0x87);
     } else {
-        /* Any value is OK */
-        status = hal_spi_tx_val(g_spi_num, 0x01);
+        /* Any CRC value is OK here */
+        hal_spi_tx_val(g_spi_num, 0x01);
     }
 
-    printf("=======================\n");
-    printf("sending cmd %d\n", cmd & ~0x80);
+    //printf("==> sending cmd %d\n", cmd & ~0x80);
 
     n = 10;
     do {
         status = hal_spi_tx_val(g_spi_num, 0xff);
     } while ((status & 0x80) && --n);
 
-    if (n) {
-        type = response_type_by_cmd(cmd);
-        printf("status=0x%x\n", status);
-        if (!(type == R1 || status & R_ILLEGAL_COMMAND || status & R_CRC_ERROR)) {
-            /* Read remaining data for this command */
-            for (n = 0; n < sizeof(response); n++) {
-                response[n] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
-            }
+    if (!n) {
+        return status;
+    }
 
-            printf("response=");
-            for (n = 0; n < sizeof(response); n++) {
-                printf("[%02x]", response[n]);
-            }
-            printf(" \n");
-
-            switch (type) {
-                case R3:
-                    /* NOTE: ocr is defined in section 5.1 */
-                    printf("ocr=0x%08lx\n", ocr_from_r3(response));
-                    break;
-                case R7:
-                    printf("voltage=0x%x\n", voltage_from_r7(response));
-                    break;
-            }
+    type = response_type_by_cmd(cmd);
+
+    /* FIXME:
+     *       R1 and R1b don't have extra payload
+     *       R2 has extra status byte
+     *       R3 has 4 extra bytes for OCR
+     *       R7 has 4 extra bytes with pattern, voltage, etc
+     */
+    if (!(type == R1 || status & R_ILLEGAL_COMMAND || status & R_CRC_ERROR)) {
+        /* Read remaining data for this command */
+        for (n = 0; n < sizeof(response); n++) {
+            response[n] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
         }
-    } else {
-        printf("status=%x, n=%d\n", status, n);
-    }
 
-    printf("\n");
+#if 0
+        printf("response=");
+        for (n = 0; n < sizeof(response); n++) {
+            printf("[%02x]", response[n]);
+        }
+        printf(" \n");
+#endif
+
+        switch (type) {
+            case R3:
+                /* NOTE: ocr is defined in section 5.1 */
+                printf("ocr=0x%08lx\n", ocr_from_r3(response));
+                break;
+            case R7:
+                printf("voltage=0x%x\n", voltage_from_r7(response));
+                break;
+        }
+    }
 
     return status;
 }
@@ -294,7 +312,7 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
 #define TIME_TO_WAIT (3 * OS_TICKS_PER_SEC)
 
         wait_to = os_time_get() + TIME_TO_WAIT;
-        status = send_mmc_cmd(ACMD41, 0x40000000);
+        status = send_mmc_cmd(ACMD41, 0x40000000); /* FIXME */
 
         while (status & R_IDLE) {
             if (os_time_get() > wait_to) {
@@ -337,27 +355,30 @@ mmc_read(uint32_t addr, void *buf, size_t len)
     block_addr = addr / BLOCK_LEN;
     offset = addr - (block_addr * BLOCK_LEN);
 
-    printf("block_addr=%d, offset=%d\n", block_addr, offset);
-    printf("block_len=%d, block_count=%d\n", block_len, block_count);
+    //printf("block_addr=%d, offset=%d\n", block_addr, offset);
+    //printf("len=%d, block_len=%d, block_count=%d\n", len, block_len, block_count);
 
     cmd = (block_count == 1) ? CMD17 : CMD18;
     res = send_mmc_cmd(cmd, block_addr);
-    /* TODO: should be 0 */
-    printf("res=0x%x\n", res);
-
-    /* 7.3.3 Control tokens */
+    if (res != MMC_OK) {
+        hal_gpio_write(g_ss_pin, 1);
+        return MMC_CARD_ERROR;
+    }
 
-    /* wait up to 1s (should be 200ms???) for control token */
-    timeout = os_time_get() + OS_TICKS_PER_SEC;
+    /**
+     * 7.3.3 Control tokens
+     *   Wait up to 200ms for control token.
+     */
+    timeout = os_time_get() + OS_TICKS_PER_SEC / 5;
     do {
         res = hal_spi_tx_val(g_spi_num, 0xff);
-        if (res != 0xFF) {
-            break;
-        }
+        if (res != 0xFF) break;
         os_time_delay(OS_TICKS_PER_SEC / 20);
     } while (os_time_get() < timeout);
 
-    printf("res=0x%x\n", res);
+    /**
+     * 7.3.3.2 Start Block Tokens and Stop Tran Token
+     */
     if (res == 0xFE) {
         index = 0;
         while (block_count--) {
@@ -369,7 +390,7 @@ mmc_read(uint32_t addr, void *buf, size_t len)
                 g_block_buf[n] = hal_spi_tx_val(g_spi_num, 0xff);
             }
 
-            /* consume CRC-16 */
+            /* FIXME: consume CRC-16, but should check */
             hal_spi_tx_val(g_spi_num, 0xff);
             hal_spi_tx_val(g_spi_num, 0xff);
 
@@ -403,5 +424,120 @@ mmc_read(uint32_t addr, void *buf, size_t len)
 int
 mmc_write(uint32_t addr, void *buf, size_t len)
 {
-    return 0;
+    uint8_t cmd;
+    uint8_t res;
+    uint32_t n;
+    size_t block_len;
+    size_t block_count;
+    os_time_t timeout;
+    size_t block_addr;
+    size_t offset;
+    size_t index;
+    size_t amount;
+
+    block_len = (len + BLOCK_LEN - 1) & ~(BLOCK_LEN - 1);
+    block_count = block_len / BLOCK_LEN;
+    block_addr = addr / BLOCK_LEN;
+    offset = addr - (block_addr * BLOCK_LEN);
+
+    if (offset) {
+        res = send_mmc_cmd(CMD17, block_addr);
+        if (res != MMC_OK) {
+            hal_gpio_write(g_ss_pin, 1);
+            return MMC_CARD_ERROR;
+        }
+
+        timeout = os_time_get() + OS_TICKS_PER_SEC / 5;
+        do {
+            res = hal_spi_tx_val(g_spi_num, 0xff);
+            if (res != 0xFF) break;
+            os_time_delay(OS_TICKS_PER_SEC / 20);
+        } while (os_time_get() < timeout);
+
+        if (res != 0xFE) {
+            hal_gpio_write(g_ss_pin, 1);
+            return MMC_CARD_ERROR;
+        }
+
+        for (n = 0; n < BLOCK_LEN; n++) {
+            g_block_buf[n] = hal_spi_tx_val(g_spi_num, 0xff);
+        }
+
+        hal_spi_tx_val(g_spi_num, 0xff);
+        hal_spi_tx_val(g_spi_num, 0xff);
+    }
+
+    /* now start write */
+
+    cmd = (block_count == 1) ? CMD24 : CMD25;
+    res = send_mmc_cmd(cmd, block_addr);
+    if (res != MMC_OK) {
+        hal_gpio_write(g_ss_pin, 1);
+        return MMC_CARD_ERROR;
+    }
+
+    /**
+     * 7.3.3 Control tokens
+     *   Wait up to 500ms for control token.
+     */
+    timeout = os_time_get() + OS_TICKS_PER_SEC / 2;
+    do {
+        res = hal_spi_tx_val(g_spi_num, 0xff);
+        if (res != 0xFF) break;
+        os_time_delay(OS_TICKS_PER_SEC / 20);
+    } while (os_time_get() < timeout);
+
+    if (res == 0xFF) {
+        hal_gpio_write(g_ss_pin, 1);
+        return MMC_CARD_ERROR;
+    }
+
+    index = 0;
+    do {
+        /* FIXME: must wait for ready here? */
+
+        /**
+         * 7.3.3.2 Start Block Tokens and Stop Tran Token
+         */
+        if (cmd == CMD24) {
+            hal_spi_tx_val(g_spi_num, 0xFE);
+        } else {
+            hal_spi_tx_val(g_spi_num, 0xFC);
+        }
+
+        amount = MIN(BLOCK_LEN - offset, len);
+        memcpy(&g_block_buf[offset], ((uint8_t *)buf + index), amount);
+
+        for (n = 0; n < BLOCK_LEN; n++) {
+            hal_spi_tx_val(g_spi_num, g_block_buf[n]);
+        }
+
+        /* CRC */
+        hal_spi_tx_val(g_spi_num, 0xff);
+        hal_spi_tx_val(g_spi_num, 0xff);
+
+        /**
+         * 7.3.3.1 Data Response Token
+         */
+        res = hal_spi_tx_val(g_spi_num, 0xff);
+        if ((res & 0x1f) != 0x05) {
+            break;
+        }
+
+        offset = 0;
+        len -= amount;
+        index += amount;
+
+    } while (len);
+
+    hal_gpio_write(g_ss_pin, 1);
+
+    res &= 0x1f;
+    if (res == 0x0b) {
+        return MMC_CRC_ERROR;
+    } else if (res == 0x0d) {
+        return MMC_WRITE_ERROR;
+    }
+
+    return MMC_OK;
 }


[16/50] incubator-mynewt-core git commit: Remove test app

Posted by st...@apache.org.
Remove test app


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

Branch: refs/heads/sensors_branch
Commit: 646945eea279e8278f227905bb035862a52db141
Parents: 791236a
Author: Fabio Utzig <ut...@utzig.org>
Authored: Fri Dec 9 08:54:25 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 apps/mmctest/pkg.yml    |  31 ----
 apps/mmctest/src/main.c | 369 -------------------------------------------
 apps/mmctest/syscfg.yml |  22 ---
 3 files changed, 422 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/646945ee/apps/mmctest/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/mmctest/pkg.yml b/apps/mmctest/pkg.yml
deleted file mode 100644
index c42160c..0000000
--- a/apps/mmctest/pkg.yml
+++ /dev/null
@@ -1,31 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#  http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-
-pkg.name: apps/mmctest
-pkg.type: app
-pkg.description: TBD
-pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-
-pkg.deps:
-    - sys/console/full
-    - kernel/os
-    - hw/drivers/mmc
-    - hw/mcu/stm/stm32f4xx

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/646945ee/apps/mmctest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/mmctest/src/main.c b/apps/mmctest/src/main.c
deleted file mode 100755
index 34847ea..0000000
--- a/apps/mmctest/src/main.c
+++ /dev/null
@@ -1,369 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include "sysinit/sysinit.h"
-#include "os/os.h"
-#include "bsp/bsp.h"
-#include "hal/hal_gpio.h"
-#include "hal/hal_spi.h"
-#include <mmc/mmc.h>
-#include <mcu/stm32f4xx_mynewt_hal.h>
-#include <os/os_dev.h>
-#include <assert.h>
-#include <string.h>
-#include <console/console.h>
-#ifdef ARCH_sim
-#include <mcu/mcu_sim.h>
-#endif
-
-/* The spi txrx callback */
-struct sblinky_spi_cb_arg
-{
-    int transfers;
-    int txlen;
-    uint32_t tx_rx_bytes;
-};
-struct sblinky_spi_cb_arg spi_cb_obj;
-void *spi_cb_arg;
-
-/* Task 1 */
-#define TASK1_PRIO (1)
-#define TASK1_STACK_SIZE    OS_STACK_ALIGN(1024)
-struct os_task task1;
-
-/* Task 2 */
-#define TASK2_PRIO (2)
-#define TASK2_STACK_SIZE    OS_STACK_ALIGN(512)
-static struct os_task task2;
-
-#define TASK3_PRIO (3)
-#define TASK3_STACK_SIZE    OS_STACK_ALIGN(512)
-static struct os_task task3;
-
-static struct os_eventq mmctest_evq;
-
-/* Global test semaphore */
-struct os_sem g_test_sem;
-
-/* For LED toggling */
-int g_led_pin;
-
-static const int SPI_SS_PIN   = MCU_GPIO_PORTA(4);
-static const int SPI_SCK_PIN  = MCU_GPIO_PORTA(5);
-static const int SPI_MISO_PIN = MCU_GPIO_PORTA(6);
-static const int SPI_MOSI_PIN = MCU_GPIO_PORTA(7);
-
-uint8_t g_spi_tx_buf[32];
-uint8_t g_spi_last_tx_buf[32];
-uint8_t g_spi_rx_buf[32];
-uint32_t g_spi_xfr_num;
-uint8_t g_spi_null_rx;
-uint8_t g_last_tx_len;
-
-static
-void mmctest_validate_last(int len)
-{
-    int i;
-    int curlen;
-    int remlen;
-    int curindex;
-    uint8_t expval;
-
-    if (g_spi_null_rx == 0) {
-        expval = 0xaa;
-        if (g_last_tx_len < len) {
-            curlen = g_last_tx_len;
-            remlen = len - g_last_tx_len;
-        } else {
-            curlen = len;
-            remlen = 0;
-        }
-        for (i = 0; i < curlen; ++i) {
-            if (g_spi_rx_buf[i] != g_spi_last_tx_buf[i]) {
-                assert(0);
-            }
-        }
-        curindex = curlen;
-        for (i = 0; i < remlen; ++i) {
-            if (g_spi_rx_buf[curindex + i] != expval) {
-                assert(0);
-            }
-        }
-    }
-}
-
-void
-sblinky_spi_irqm_handler(void *arg, int len)
-{
-    int i;
-    struct sblinky_spi_cb_arg *cb;
-
-    //hal_gpio_write(SPI_SS_PIN, 1);
-
-    assert(arg == spi_cb_arg);
-    if (spi_cb_arg) {
-        cb = (struct sblinky_spi_cb_arg *)arg;
-        assert(len == cb->txlen);
-        ++cb->transfers;
-    }
-
-    /* Make sure we get back the data we expect! */
-    if (g_spi_xfr_num == 1) {
-        /* The first time we expect entire buffer to be filled with 0x88 */
-        for (i = 0; i < len; ++i) {
-            if (g_spi_rx_buf[i] != 0x88) {
-                assert(0);
-            }
-        }
-
-        /* copy current tx buf to last */
-        memcpy(g_spi_last_tx_buf, g_spi_tx_buf, len);
-    } else {
-        /* Check that we received what we last sent */
-        mmctest_validate_last(len);
-    }
-    ++g_spi_xfr_num;
-}
-
-void
-task1_handler(void *arg)
-{
-    //int i;
-    int rc;
-    //uint16_t rxval;
-    //uint8_t last_val;
-    //uint8_t spi_nb_cntr;
-    //uint8_t spi_b_cntr;
-
-    struct stm32f4_hal_spi_cfg spi_cfg = {
-        .ss_pin   = SPI_SS_PIN,
-        .sck_pin  = SPI_SCK_PIN,
-        .miso_pin = SPI_MISO_PIN,
-        .mosi_pin = SPI_MOSI_PIN,
-        .irq_prio = 2
-    };
-
-    rc = mmc_init(0, &spi_cfg, spi_cfg.ss_pin);
-    printf("mmc_init %d\n", rc);
-
-#if 0
-    /*
-     * Send some bytes in a non-blocking manner to SPI using tx val. The
-     * slave should send back 0x77.
-     */
-    g_spi_tx_buf[0] = 0xde;
-    g_spi_tx_buf[1] = 0xad;
-    g_spi_tx_buf[2] = 0xbe;
-    g_spi_tx_buf[3] = 0xef;
-    hal_gpio_write(SPI_SS_PIN, 0);
-    for (i = 0; i < 4; ++i) {
-        rxval = hal_spi_tx_val(0, g_spi_tx_buf[i]);
-        assert(rxval == 0x77);
-        g_spi_rx_buf[i] = (uint8_t)rxval;
-    }
-    hal_gpio_write(SPI_SS_PIN, 1);
-    ++g_spi_xfr_num;
-
-    /* Set up the callback to use when non-blocking API used */
-    hal_spi_disable(0);
-    spi_cb_arg = &spi_cb_obj;
-    spi_cb_obj.txlen = 32;
-    hal_spi_set_txrx_cb(0, sblinky_spi_irqm_handler, spi_cb_arg);
-    hal_spi_enable(0);
-    spi_nb_cntr = 0;
-    spi_b_cntr = 0;
-
-    while (1) {
-        /* Get random length to send */
-        g_last_tx_len = spi_cb_obj.txlen;
-        spi_cb_obj.txlen = (rand() & 0x1F) + 1;
-        memcpy(g_spi_last_tx_buf, g_spi_tx_buf, g_last_tx_len);
-        last_val = g_spi_last_tx_buf[g_last_tx_len - 1];
-        for (i= 0; i < spi_cb_obj.txlen; ++i) {
-            g_spi_tx_buf[i] = (uint8_t)(last_val + i);
-        }
-
-        if (g_spi_xfr_num & 1) {
-            /* Send non-blocking */
-            ++spi_nb_cntr;
-            assert(hal_gpio_read(SPI_SS_PIN) == 1);
-            hal_gpio_write(SPI_SS_PIN, 0);
-#if 0
-            if (spi_nb_cntr == 7) {
-                g_spi_null_rx = 1;
-                rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, NULL, 32);
-            } else {
-                g_spi_null_rx = 0;
-                rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, g_spi_rx_buf, 32);
-            }
-            assert(!rc);
-#else
-            g_spi_null_rx = 0;
-            rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, g_spi_rx_buf,
-                                      spi_cb_obj.txlen);
-            assert(!rc);
-            console_printf("a transmitted: ");
-            for (i = 0; i < spi_cb_obj.txlen; i++) {
-                console_printf("%2x ", g_spi_tx_buf[i]);
-            }
-            console_printf("\n");
-            console_printf("received: ");
-            for (i = 0; i < spi_cb_obj.txlen; i++) {
-                console_printf("%2x ", g_spi_rx_buf[i]);
-            }
-            console_printf("\n");
-#endif
-        } else {
-            /* Send blocking */
-            ++spi_b_cntr;
-            assert(hal_gpio_read(SPI_SS_PIN) == 1);
-            hal_gpio_write(SPI_SS_PIN, 0);
-#if 0
-            if (spi_b_cntr == 7) {
-                g_spi_null_rx = 1;
-                rc = hal_spi_txrx(0, g_spi_tx_buf, NULL, 32);
-                spi_b_cntr = 0;
-            } else {
-                g_spi_null_rx = 0;
-                rc = hal_spi_txrx(0, g_spi_tx_buf, g_spi_rx_buf, 32);
-            }
-            assert(!rc);
-            hal_gpio_write(SPI_SS_PIN, 1);
-            mmctest_validate_last(spi_cb_obj.txlen);
-#else
-            rc = hal_spi_txrx(0, g_spi_tx_buf, g_spi_rx_buf, spi_cb_obj.txlen);
-            assert(!rc);
-            hal_gpio_write(SPI_SS_PIN, 1);
-            console_printf("b transmitted: ");
-            for (i = 0; i < spi_cb_obj.txlen; i++) {
-                console_printf("%2x ", g_spi_tx_buf[i]);
-            }
-            console_printf("\n");
-            console_printf("received: ");
-            for (i = 0; i < spi_cb_obj.txlen; i++) {
-                console_printf("%2x ", g_spi_rx_buf[i]);
-            }
-            console_printf("\n");
-            mmctest_validate_last(spi_cb_obj.txlen);
-            ++g_spi_xfr_num;
-#endif
-        }
-    }
-#endif
-
-    while (1) {
-        os_time_delay(OS_TICKS_PER_SEC);
-    }
-}
-
-/**
- * This task serves as a container for the shell and newtmgr packages.  These
- * packages enqueue timer events when they need this task to do work.
- */
-static void
-task2_handler(void *arg)
-{
-    while (1) {
-        os_eventq_run(&mmctest_evq);
-    }
-}
-
-static void
-task3_handler(void *arg)
-{
-    while (1) {
-        os_time_delay(OS_TICKS_PER_SEC);
-        hal_gpio_toggle(g_led_pin);
-    }
-}
-
-/**
- * init_tasks
- *
- * Called by main.c after sysinit(). This function performs initializations
- * that are required before tasks are running.
- *
- * @return int 0 success; error otherwise.
- */
-static void
-init_tasks(void)
-{
-    os_stack_t *pstack;
-
-    /* Initialize global test semaphore */
-    os_sem_init(&g_test_sem, 0);
-
-    pstack = malloc(sizeof(os_stack_t)*TASK1_STACK_SIZE);
-    assert(pstack);
-
-    os_task_init(&task1, "task1", task1_handler, NULL,
-            TASK1_PRIO, OS_WAIT_FOREVER, pstack, TASK1_STACK_SIZE);
-
-    pstack = malloc(sizeof(os_stack_t)*TASK2_STACK_SIZE);
-    assert(pstack);
-
-    os_task_init(&task2, "task2", task2_handler, NULL,
-            TASK2_PRIO, OS_WAIT_FOREVER, pstack, TASK2_STACK_SIZE);
-
-    pstack = malloc(sizeof(os_stack_t)*TASK3_STACK_SIZE);
-    assert(pstack);
-
-    os_task_init(&task3, "task3", task3_handler, NULL,
-            TASK3_PRIO, OS_WAIT_FOREVER, pstack, TASK3_STACK_SIZE);
-
-    /* Initialize eventq and designate it as the default.  Packages that need
-     * to schedule work items will piggyback on this eventq.  Example packages
-     * which do this are sys/shell and mgmt/newtmgr.
-     */
-    os_eventq_init(&mmctest_evq);
-    os_eventq_dflt_set(&mmctest_evq);
-}
-
-/**
- * main
- *
- * The main function for the project. This function initializes the os, calls
- * init_tasks to initialize tasks (and possibly other objects), then starts the
- * OS. We should not return from os start.
- *
- * @return int NOTE: this function should never return!
- */
-int
-main(int argc, char **argv)
-{
-    int rc;
-
-#ifdef ARCH_sim
-    mcu_sim_parse_args(argc, argv);
-#endif
-
-    sysinit();
-
-    /* Set the led pin for the devboard */
-    g_led_pin = LED_BLINK_PIN;
-    hal_gpio_init_out(g_led_pin, 0);
-
-    init_tasks();
-    os_start();
-
-    /* os start should never return. If it does, this should be an error */
-    assert(0);
-
-    return rc;
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/646945ee/apps/mmctest/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/mmctest/syscfg.yml b/apps/mmctest/syscfg.yml
deleted file mode 100644
index 40c4c52..0000000
--- a/apps/mmctest/syscfg.yml
+++ /dev/null
@@ -1,22 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#  http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-
-# Package: apps/mmctest
-
-syscfg.vals:
-    SHELL_TASK: 0


[40/50] incubator-mynewt-core git commit: oic; make oc_endpoint structure size dependent on type of transport. For IPv6 it needs a full v6 address/port, while ble only needs 2 bytes for connection handle.

Posted by st...@apache.org.
oic; make oc_endpoint structure size dependent on type of transport.
For IPv6 it needs a full v6 address/port, while ble only needs 2 bytes
for connection handle.


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

Branch: refs/heads/sensors_branch
Commit: 9482961d3c143dc76a4c83230eda1e74476e5ce3
Parents: 896011d
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Dec 16 15:41:10 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/api/oc_buffer.c              |  2 +-
 net/oic/src/api/oc_client_api.c          |  2 +-
 net/oic/src/api/oc_discovery.c           | 10 ++---
 net/oic/src/port/mynewt/adaptor.c        |  4 +-
 net/oic/src/port/mynewt/ble_adaptor.c    | 46 ++++++++++++---------
 net/oic/src/port/mynewt/ip_adaptor.c     | 19 +++++----
 net/oic/src/port/mynewt/log.c            |  8 ++--
 net/oic/src/port/mynewt/serial_adaptor.c |  4 +-
 net/oic/src/port/oc_connectivity.h       | 59 +++++++++++++++++++--------
 9 files changed, 93 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/api/oc_buffer.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_buffer.c b/net/oic/src/api/oc_buffer.c
index 7bca928..1df8b8d 100644
--- a/net/oic/src/api/oc_buffer.c
+++ b/net/oic/src/api/oc_buffer.c
@@ -118,7 +118,7 @@ oc_buffer_tx(struct os_event *ev)
         OC_LOG_DEBUG("oc_buffer_tx: ");
         OC_LOG_ENDPOINT(LOG_LEVEL_DEBUG, OC_MBUF_ENDPOINT(m));
 #ifdef OC_CLIENT
-        if (OC_MBUF_ENDPOINT(m)->flags & MULTICAST) {
+        if (OC_MBUF_ENDPOINT(m)->oe.flags & MULTICAST) {
             oc_send_multicast_message(m);
         } else {
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/api/oc_client_api.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_client_api.c b/net/oic/src/api/oc_client_api.c
index bc29fe1..692e0f6 100644
--- a/net/oic/src/api/oc_client_api.c
+++ b/net/oic/src/api/oc_client_api.c
@@ -273,7 +273,7 @@ oc_do_ip_discovery(const char *rt, oc_discovery_cb_t handler)
 
     oc_make_ip_endpoint(mcast, IP | MULTICAST, 5683,
                        0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xfd);
-    mcast.ipv6_addr.scope = 0;
+    mcast.oe_ip.v6.scope = 0;
 
     memcpy(&handle.endpoint, &mcast, sizeof(oc_endpoint_t));
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/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 be5be0c..6427056 100644
--- a/net/oic/src/api/oc_discovery.c
+++ b/net/oic/src/api/oc_discovery.c
@@ -190,7 +190,7 @@ oc_ri_process_discovery_payload(uint8_t *payload, int len,
       .os_str = NULL
   };
   bool secure = false;
-  uint16_t dtls_port = 0, default_port = endpoint->ipv6_addr.port;
+  uint16_t dtls_port = 0, default_port = endpoint->oe_ip.v6.port;
   oc_string_array_t types = {};
   oc_interface_mask_t interfaces = 0;
   oc_server_handle_t handle;
@@ -269,11 +269,11 @@ oc_ri_process_discovery_payload(uint8_t *payload, int len,
               resource_info = resource_info->next;
             }
             if (secure) {
-              handle.endpoint.ipv6_addr.port = dtls_port;
-              handle.endpoint.flags |= SECURED;
+              handle.endpoint.oe_ip.v6.port = dtls_port;
+              handle.endpoint.oe_ip.flags |= SECURED;
             } else {
-              handle.endpoint.ipv6_addr.port = default_port;
-              handle.endpoint.flags &= ~SECURED;
+              handle.endpoint.oe_ip.v6.port = default_port;
+              handle.endpoint.oe_ip.flags &= ~SECURED;
             }
 
             if (handler(oc_string(di), oc_string(uri), types, interfaces,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/port/mynewt/adaptor.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/adaptor.c b/net/oic/src/port/mynewt/adaptor.c
index 22edda3..6f4134e 100644
--- a/net/oic/src/port/mynewt/adaptor.c
+++ b/net/oic/src/port/mynewt/adaptor.c
@@ -75,7 +75,7 @@ oc_send_buffer(struct os_mbuf *m)
 
     oe = OC_MBUF_ENDPOINT(m);
 
-    switch (oe->flags) {
+    switch (oe->oe.flags) {
 #if (MYNEWT_VAL(OC_TRANSPORT_IP) == 1)
     case IP:
         oc_send_buffer_ip(m);
@@ -92,7 +92,7 @@ oc_send_buffer(struct os_mbuf *m)
         break;
 #endif
     default:
-        OC_LOG_ERROR("Unknown transport option %u\n", oe->flags);
+        OC_LOG_ERROR("Unknown transport option %u\n", oe->oe.flags);
         os_mbuf_free_chain(m);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/port/mynewt/ble_adaptor.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/ble_adaptor.c b/net/oic/src/port/mynewt/ble_adaptor.c
index 39ceac9..8ede614 100644
--- a/net/oic/src/port/mynewt/ble_adaptor.c
+++ b/net/oic/src/port/mynewt/ble_adaptor.c
@@ -117,7 +117,7 @@ int
 oc_ble_reass(struct os_mbuf *om1, uint16_t conn_handle)
 {
     struct os_mbuf_pkthdr *pkt1;
-    struct oc_endpoint *oe;
+    struct oc_endpoint_ble *oe_ble;
     struct os_mbuf *om2;
     struct os_mbuf_pkthdr *pkt2;
     uint8_t hdr[6]; /* sizeof(coap_tcp_hdr32) */
@@ -133,8 +133,8 @@ oc_ble_reass(struct os_mbuf *om1, uint16_t conn_handle)
 
     STAILQ_FOREACH(pkt2, &oc_ble_reass_q, omp_next) {
         om2 = OS_MBUF_PKTHDR_TO_MBUF(pkt2);
-        oe = OC_MBUF_ENDPOINT(om2);
-        if (conn_handle == oe->bt_addr.conn_handle) {
+        oe_ble = (struct oc_endpoint_ble *)OC_MBUF_ENDPOINT(om2);
+        if (conn_handle == oe_ble->conn_handle) {
             /*
              * Data from same connection. Append.
              */
@@ -151,20 +151,25 @@ oc_ble_reass(struct os_mbuf *om1, uint16_t conn_handle)
     }
     if (pkt1) {
         /*
-         * New frame
+         * New frame, need to add oc_endpoint_ble in the front.
+         * Check if there is enough space available. If not, allocate a
+         * new pkthdr.
          */
-        om2 = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint));
-        if (!om2) {
-            OC_LOG_ERROR("oc_gatt_rx: Could not allocate mbuf\n");
-            STATS_INC(oc_ble_stats, ierr);
-            return -1;
+        if (OS_MBUF_USRHDR_LEN(om1) < sizeof(struct oc_endpoint_ble)) {
+            om2 = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint_ble));
+            if (!om2) {
+                OC_LOG_ERROR("oc_gatt_rx: Could not allocate mbuf\n");
+                STATS_INC(oc_ble_stats, ierr);
+                return -1;
+            }
+            OS_MBUF_PKTHDR(om2)->omp_len = pkt1->omp_len;
+            SLIST_NEXT(om2, om_next) = om1;
+        } else {
+            om2 = om1;
         }
-        OS_MBUF_PKTHDR(om2)->omp_len = pkt1->omp_len;
-        SLIST_NEXT(om2, om_next) = om1;
-
-        oe = OC_MBUF_ENDPOINT(om2);
-        oe->flags = GATT;
-        oe->bt_addr.conn_handle = conn_handle;
+        oe_ble = (struct oc_endpoint_ble *)OC_MBUF_ENDPOINT(om2);
+        oe_ble->flags = GATT;
+        oe_ble->conn_handle = conn_handle;
         pkt2 = OS_MBUF_PKTHDR(om2);
 
         if (os_mbuf_copydata(om2, 0, sizeof(hdr), hdr) ||
@@ -239,13 +244,13 @@ oc_ble_coap_conn_del(uint16_t conn_handle)
 {
     struct os_mbuf_pkthdr *pkt;
     struct os_mbuf *m;
-    struct oc_endpoint *oe;
+    struct oc_endpoint_ble *oe_ble;
 
     OC_LOG_DEBUG("oc_gatt endconn %x\n", conn_handle);
     STAILQ_FOREACH(pkt, &oc_ble_reass_q, omp_next) {
         m = OS_MBUF_PKTHDR_TO_MBUF(pkt);
-        oe = OC_MBUF_ENDPOINT(m);
-        if (oe->bt_addr.conn_handle == conn_handle) {
+        oe_ble = (struct oc_endpoint_ble *)OC_MBUF_ENDPOINT(m);
+        if (oe_ble->conn_handle == conn_handle) {
             STAILQ_REMOVE(&oc_ble_reass_q, pkt, os_mbuf_pkthdr, omp_next);
             os_mbuf_free_chain(m);
             break;
@@ -318,8 +323,9 @@ oc_send_buffer_gatt(struct os_mbuf *m)
     uint16_t mtu;
     uint16_t conn_handle;
 
+    assert(OS_MBUF_USRHDR_LEN(m) >= sizeof(struct oc_endpoint_ble));
     oe = OC_MBUF_ENDPOINT(m);
-    conn_handle = oe->bt_addr.conn_handle;
+    conn_handle = oe->oe_ble.conn_handle;
 
 #if (MYNEWT_VAL(OC_CLIENT) == 1)
     OC_LOG_ERROR("oc_gatt send not supported on client");
@@ -330,7 +336,7 @@ oc_send_buffer_gatt(struct os_mbuf *m)
     STATS_INC(oc_ble_stats, oseg);
     STATS_INCN(oc_ble_stats, obytes, OS_MBUF_PKTLEN(m));
 
-    mtu = ble_att_mtu(oe->bt_addr.conn_handle);
+    mtu = ble_att_mtu(conn_handle);
     assert(mtu > 4);
     mtu -= 3; /* # of bytes for ATT notification base */
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/port/mynewt/ip_adaptor.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/ip_adaptor.c b/net/oic/src/port/mynewt/ip_adaptor.c
index 634417a..9d8a300 100644
--- a/net/oic/src/port/mynewt/ip_adaptor.c
+++ b/net/oic/src/port/mynewt/ip_adaptor.c
@@ -69,13 +69,14 @@ oc_send_buffer_ip_int(struct os_mbuf *m, int is_mcast)
     struct os_mbuf *n;
     int rc;
 
+    assert(OS_MBUF_USRHDR_LEN(m) >= sizeof(struct oc_endpoint_ip));
     oe = OC_MBUF_ENDPOINT(m);
 
     to.msin6_len = sizeof(to);
     to.msin6_family = MN_AF_INET6;
-    to.msin6_port = htons(oe->ipv6_addr.port);
-    to.msin6_scope_id = oe->ipv6_addr.scope;
-    memcpy(&to.msin6_addr, oe->ipv6_addr.address, sizeof(to.msin6_addr));
+    to.msin6_port = htons(oe->oe_ip.v6.port);
+    to.msin6_scope_id = oe->oe_ip.v6.scope;
+    memcpy(&to.msin6_addr, oe->oe_ip.v6.address, sizeof(to.msin6_addr));
 
     if (is_mcast) {
         memset(&itf, 0, sizeof(itf));
@@ -156,7 +157,7 @@ oc_attempt_rx_ip_sock(struct mn_socket *rxsock)
     }
     assert(OS_MBUF_IS_PKTHDR(n));
 
-    m = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint));
+    m = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint_ip));
     if (!m) {
         OC_LOG_ERROR("Could not allocate RX buffer\n");
         goto rx_attempt_err;
@@ -166,11 +167,11 @@ oc_attempt_rx_ip_sock(struct mn_socket *rxsock)
 
     oe = OC_MBUF_ENDPOINT(m);
 
-    oe->flags = IP;
-    memcpy(&oe->ipv6_addr.address, &from.msin6_addr,
-             sizeof(oe->ipv6_addr.address));
-    oe->ipv6_addr.scope = from.msin6_scope_id;
-    oe->ipv6_addr.port = ntohs(from.msin6_port);
+    oe->oe_ip.flags = IP;
+    memcpy(&oe->oe_ip.v6.address, &from.msin6_addr,
+           sizeof(oe->oe_ip.v6.address));
+    oe->oe_ip.v6.scope = from.msin6_scope_id;
+    oe->oe_ip.v6.port = ntohs(from.msin6_port);
 
     return m;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/port/mynewt/log.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/log.c b/net/oic/src/port/mynewt/log.c
index b2f1b88..85f7e5d 100644
--- a/net/oic/src/port/mynewt/log.c
+++ b/net/oic/src/port/mynewt/log.c
@@ -34,21 +34,21 @@ oc_log_endpoint(uint16_t lvl, struct oc_endpoint *oe)
 
     (void)tmp;
 
-    switch (oe->flags) {
+    switch (oe->oe.flags) {
 #if (MYNEWT_VAL(OC_TRANSPORT_IP) == 1)
     case IP: {
         int len;
 
-        mn_inet_ntop(MN_PF_INET6, oe->ipv6_addr.address, tmp, sizeof(tmp));
+        mn_inet_ntop(MN_PF_INET6, oe->oe_ip.v6.address, tmp, sizeof(tmp));
         len = strlen(tmp);
-        snprintf(tmp + len, sizeof(tmp) - len, "-%u\n", oe->ipv6_addr.port);
+        snprintf(tmp + len, sizeof(tmp) - len, "-%u\n", oe->oe_ip.v6.port);
         str = tmp;
         break;
     }
 #endif
 #if (MYNEWT_VAL(OC_TRANSPORT_GATT) == 1)
     case GATT:
-        snprintf(tmp, sizeof(tmp), "%u\n", oe->bt_addr.conn_handle);
+        snprintf(tmp, sizeof(tmp), "ble %u\n", oe->oe_ble.conn_handle);
         str = tmp;
         break;
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/port/mynewt/serial_adaptor.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/serial_adaptor.c b/net/oic/src/port/mynewt/serial_adaptor.c
index 130e26d..6e6450b 100644
--- a/net/oic/src/port/mynewt/serial_adaptor.c
+++ b/net/oic/src/port/mynewt/serial_adaptor.c
@@ -102,7 +102,7 @@ oc_attempt_rx_serial(void)
         return NULL;
     }
 
-    m = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint));
+    m = os_msys_get_pkthdr(0, sizeof(struct oc_endpoint_plain));
     if (!m) {
         OC_LOG_ERROR("Could not allocate OC message buffer\n");
         goto rx_attempt_err;
@@ -111,7 +111,7 @@ oc_attempt_rx_serial(void)
     SLIST_NEXT(m, om_next) = n;
 
     oe = OC_MBUF_ENDPOINT(m);
-    oe->flags = SERIAL;
+    oe->oe.flags = SERIAL;
 
     return m;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9482961d/net/oic/src/port/oc_connectivity.h
----------------------------------------------------------------------
diff --git a/net/oic/src/port/oc_connectivity.h b/net/oic/src/port/oc_connectivity.h
index 7f50a6b..b86ec19 100644
--- a/net/oic/src/port/oc_connectivity.h
+++ b/net/oic/src/port/oc_connectivity.h
@@ -30,22 +30,47 @@ typedef struct {
     uint8_t scope;
 } oc_ipv6_addr_t;
 
-typedef struct {
+enum oc_transport_flags {
+    IP = 1 << 0,
+    GATT = 1 << 1,
+    IPSP = 1 << 2,
+    MULTICAST = 1 << 3,
+    SECURED = 1 << 4,
+    SERIAL = 1 << 5,
+};
+
+/*
+ * OC endpoint data structure comes in different variations,
+ * depending on flags field.
+ */
+/*
+ * oc_endpoint for IPv6 source
+ */
+struct oc_endpoint_ip {
+    enum oc_transport_flags flags;
+    oc_ipv6_addr_t v6;
+};
+
+/*
+ * oc_endpoint for BLE source.
+ */
+struct oc_endpoint_ble {
+    enum oc_transport_flags flags;
     uint16_t conn_handle;
-} oc_le_addr_t;
+};
+
+/*
+ * oc_endpoint for multicast target and serial port.
+ */
+struct oc_endpoint_plain {
+    enum oc_transport_flags flags;
+};
 
 typedef struct oc_endpoint {
-    enum transport_flags {
-        IP = 1 << 0,
-        GATT = 1 << 1,
-        IPSP = 1 << 2,
-        MULTICAST = 1 << 3,
-        SECURED = 1 << 4,
-        SERIAL = 1 << 5,
-    } flags;
     union {
-        oc_ipv6_addr_t ipv6_addr;
-        oc_le_addr_t bt_addr;
+        struct oc_endpoint_ip oe_ip;
+        struct oc_endpoint_ble oe_ble;
+        struct oc_endpoint_plain oe;
     };
 } oc_endpoint_t;
 
@@ -54,10 +79,10 @@ typedef struct oc_endpoint {
                             sizeof(struct os_mbuf_pkthdr)))
 
 
-#define oc_make_ip_endpoint(__name__, __flags__, __port__, ...)                \
-  oc_endpoint_t __name__ = {.flags = __flags__,                                \
-                            .ipv6_addr = {.port = __port__,                    \
-                                          .address = { __VA_ARGS__ } } }
+#define oc_make_ip_endpoint(__name__, __flags__, __port__, ...)         \
+    oc_endpoint_t __name__ = {.oe_ip = {.flags = __flags__,             \
+                                        .v6 = {.port = __port__,        \
+                                               .address = { __VA_ARGS__ } } } }
 
 typedef struct oc_message {
     oc_endpoint_t endpoint;
@@ -76,7 +101,7 @@ void oc_connectivity_shutdown(void);
 static inline int
 oc_endpoint_use_tcp(struct oc_endpoint *oe)
 {
-    if (oe->flags & GATT) {
+    if (oe->oe.flags & GATT) {
         return 1;
     }
     return 0;


[22/50] incubator-mynewt-core git commit: BLE Host - Use generic fragmentation function.

Posted by st...@apache.org.
BLE Host - Use generic fragmentation function.


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

Branch: refs/heads/sensors_branch
Commit: 256f8b641fa354dd0a4bd4ee5026271c8ec6654f
Parents: fba50b6
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Dec 13 18:10:15 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/pkg.yml          |  1 +
 net/nimble/host/src/ble_hs_hci.c | 65 ++++-------------------------------
 2 files changed, 8 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/256f8b64/net/nimble/host/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/host/pkg.yml b/net/nimble/host/pkg.yml
index 377a3a7..4341d03 100644
--- a/net/nimble/host/pkg.yml
+++ b/net/nimble/host/pkg.yml
@@ -30,6 +30,7 @@ pkg.deps:
     - net/nimble
     - sys/log
     - sys/stats
+    - util/mem
 
 pkg.deps.BLE_SM:
     - crypto/mbedtls

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/256f8b64/net/nimble/host/src/ble_hs_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci.c b/net/nimble/host/src/ble_hs_hci.c
index ba86e2f..33b77a0 100644
--- a/net/nimble/host/src/ble_hs_hci.c
+++ b/net/nimble/host/src/ble_hs_hci.c
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include "os/os.h"
+#include "mem/mem.h"
 #include "nimble/ble_hci_trans.h"
 #include "ble_hs_priv.h"
 #include "ble_hs_dbg_priv.h"
@@ -363,62 +364,12 @@ ble_hs_hci_max_acl_payload_sz(void)
 }
 
 /**
- * Splits an appropriately-sized fragment from the front of an outgoing ACL
- * data packet, if necessary.  If the packet size is within the controller's
- * buffer size requirements, no splitting is performed.  The fragment data is
- * removed from the data packet mbuf.
- *
- * @param om                    The ACL data packet.  If this constitutes a
- *                                  single fragment, it gets set to NULL on
- *                                  success.
- * @param out_frag              On success, this points to the fragment to
- *                                  send.  If the entire packet can fit within
- *                                  a single fragment, this will point to the
- *                                  ACL data packet itself ('om').
- *
- * @return                      0 on success;
- *                              BLE host core return code on error.
+ * Allocates an mbuf to contain an outgoing ACL data fragment.
  */
-static int
-ble_hs_hci_split_frag(struct os_mbuf **om, struct os_mbuf **out_frag)
+static struct os_mbuf *
+ble_hs_hci_frag_alloc(uint16_t frag_size, void *arg)
 {
-    struct os_mbuf *frag;
-    uint16_t max_sz;
-    int rc;
-
-    /* Assume failure. */
-    *out_frag = NULL;
-
-    max_sz = ble_hs_hci_max_acl_payload_sz();
-
-    if (OS_MBUF_PKTLEN(*om) <= max_sz) {
-        /* Final fragment. */
-        *out_frag = *om;
-        *om = NULL;
-        return 0;
-    }
-
-    frag = ble_hs_mbuf_acm_pkt();
-    if (frag == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-
-    /* Move data from the front of the packet into the fragment mbuf. */
-    rc = os_mbuf_appendfrom(frag, *om, 0, max_sz);
-    if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-    os_mbuf_adj(*om, max_sz);
-
-    /* More fragments to follow. */
-    *out_frag = frag;
-    return 0;
-
-err:
-    os_mbuf_free_chain(frag);
-    return rc;
+    return ble_hs_mbuf_acm_pkt();
 }
 
 static struct os_mbuf *
@@ -473,10 +424,8 @@ ble_hs_hci_acl_tx(struct ble_hs_conn *connection, struct os_mbuf *txom)
 
     /* Send fragments until the entire packet has been sent. */
     while (txom != NULL) {
-        rc = ble_hs_hci_split_frag(&txom, &frag);
-        if (rc != 0) {
-            goto err;
-        }
+        frag = mem_split_frag(&txom, ble_hs_hci_max_acl_payload_sz(),
+                              ble_hs_hci_frag_alloc, NULL);
 
         frag = ble_hs_hci_acl_hdr_prepend(frag, connection->bhc_handle, pb);
         if (frag == NULL) {


[36/50] incubator-mynewt-core git commit: MYNEWT-139: mistaken add in previous commit

Posted by st...@apache.org.
MYNEWT-139: mistaken add in previous commit


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

Branch: refs/heads/sensors_branch
Commit: 7dca12ce6f8d47114eea37143760f30727cfb519
Parents: 95c57d6
Author: Peter Snyder <pe...@apache.org>
Authored: Thu Dec 15 13:51:52 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 test/runtest/src/ctags.list | 1 -
 1 file changed, 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7dca12ce/test/runtest/src/ctags.list
----------------------------------------------------------------------
diff --git a/test/runtest/src/ctags.list b/test/runtest/src/ctags.list
deleted file mode 120000
index ff9c2c0..0000000
--- a/test/runtest/src/ctags.list
+++ /dev/null
@@ -1 +0,0 @@
-/Users/peterfs/dev/newt/newt_ci/ctags.list
\ No newline at end of file


[09/50] incubator-mynewt-core git commit: Add mmc read support - WIP

Posted by st...@apache.org.
Add mmc read support - WIP


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

Branch: refs/heads/sensors_branch
Commit: 964ea25673e632109590e3e837f615f8ff4c2d16
Parents: a049361
Author: Fabio Utzig <ut...@utzig.org>
Authored: Thu Dec 1 23:54:56 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/include/mmc/mmc.h |  12 ++-
 hw/drivers/mmc/src/mmc.c         | 168 ++++++++++++++++++++++++++--------
 2 files changed, 142 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/964ea256/hw/drivers/mmc/include/mmc/mmc.h
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/include/mmc/mmc.h b/hw/drivers/mmc/include/mmc/mmc.h
index 1a03edb..9c805ec 100644
--- a/hw/drivers/mmc/include/mmc/mmc.h
+++ b/hw/drivers/mmc/include/mmc/mmc.h
@@ -30,8 +30,10 @@ extern "C" {
  * MMC driver errors.
  */
 #define MMC_OK              (0)
-#define MMC_CARD_ERROR      (1)  /* Is there a card installed? */
-#define MMC_TIMEOUT         (2)
+#define MMC_CARD_ERROR      (-1)  /* Is there a card installed? */
+#define MMC_READ_ERROR      (-2)
+#define MMC_WRITE_ERROR     (-3)
+#define MMC_TIMEOUT         (-4)
 
 /**
  * Initialize the MMC driver
@@ -45,6 +47,12 @@ extern "C" {
 int
 mmc_init(int spi_num, void *spi_cfg, int ss_pin);
 
+int
+mmc_read(uint32_t addr, void *buf, size_t len);
+
+int
+mmc_write(uint32_t addr, void *buf, size_t len);
+
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/964ea256/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index 6aefce4..be7f54c 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -24,10 +24,13 @@
 
 #include <stdio.h>
 
+#define MIN(n, m) (((n) < (m)) ? (n) : (m))
+
 /* Current used MMC commands */
 #define CMD0                (0)            /* GO_IDLE_STATE */
 #define CMD1                (1)            /* SEND_OP_COND (MMC) */
 #define CMD8                (8)            /* SEND_IF_COND */
+#define CMD12               (12)           /* STOP_TRANSMISSION */
 #define CMD16               (16)           /* SET_BLOCKLEN */
 #define CMD17               (17)           /* READ_SINGLE_BLOCK */
 #define CMD18               (18)           /* READ_MULTIPLE_BLOCK */
@@ -53,6 +56,10 @@
 #define R_ADDR_ERROR        (0x20)
 #define R_PARAM_ERROR       (0x40)
 
+#define BLOCK_LEN           (512)
+
+static uint8_t g_block_buf[BLOCK_LEN];
+
 static struct hal_spi_settings mmc_settings = {
     .data_order = HAL_SPI_MSB_FIRST,
     .data_mode  = HAL_SPI_MODE0,
@@ -89,31 +96,32 @@ ocr_from_r3(uint8_t *response)
 {
     uint32_t ocr;
 
-    ocr  = (uint32_t) response[4];
-    ocr |= (uint32_t) response[3] <<  8;
-    ocr |= (uint32_t) response[2] << 16;
-    ocr |= (uint32_t) response[1] << 24;
+    ocr  = (uint32_t) response[3];
+    ocr |= (uint32_t) response[2] <<  8;
+    ocr |= (uint32_t) response[1] << 16;
+    ocr |= (uint32_t) response[0] << 24;
 
-    return ocr;
-}
+#if 0
+    printf("Card supports: ");
+    if (ocr & (1 << 15)) {
+        printf("2.7-2.8 ");
+    }
+#endif
 
-static uint8_t
-status_from_r1(uint8_t *response)
-{
-    return response[0];
+    return ocr;
 }
 
 static uint8_t
 voltage_from_r7(uint8_t *response)
 {
-    return response[3] & 0xF;
+    return response[2] & 0xF;
 }
 
 static uint8_t
 send_mmc_cmd(uint8_t cmd, uint32_t payload)
 {
     int n;
-    uint8_t response[5];
+    uint8_t response[4];
     uint8_t status;
     uint8_t type;
 
@@ -122,8 +130,6 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
         send_mmc_cmd(CMD55, 0);
     }
 
-    hal_gpio_write(g_ss_pin, 0);
-
     /* 4.7.2: Command Format */
     hal_spi_tx_val(g_spi_num, 0x40 | (cmd & ~0x80));
 
@@ -139,35 +145,36 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
      *   NOTE: CRC can be turned on with CMD59 (CRC_ON_OFF).
      */
     if (cmd == CMD0) {
-        hal_spi_tx_val(g_spi_num, 0x95);
+        status = hal_spi_tx_val(g_spi_num, 0x95);
     } else if (cmd == CMD8) {
-        hal_spi_tx_val(g_spi_num, 0x87);
+        status = hal_spi_tx_val(g_spi_num, 0x87);
     } else {
         /* Any value is OK */
-        hal_spi_tx_val(g_spi_num, 0x01);
+        status = hal_spi_tx_val(g_spi_num, 0x01);
     }
 
     printf("=======================\n");
     printf("sending cmd %d\n", cmd & ~0x80);
 
-    status = 0xff;
     n = 10;
     do {
-        response[0] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
-    } while ((response[0] & 0x80) && --n);
+        status = hal_spi_tx_val(g_spi_num, 0xff);
+    } while ((status & 0x80) && --n);
 
     if (n) {
         type = response_type_by_cmd(cmd);
-        status = status_from_r1(response);
         printf("status=0x%x\n", status);
         if (!(type == R1 || status & R_ILLEGAL_COMMAND || status & R_CRC_ERROR)) {
             /* Read remaining data for this command */
-            for (n = 0; n < 4; n++) {
-                response[n+1] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
+            for (n = 0; n < sizeof(response); n++) {
+                response[n] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
             }
 
-            printf("response=[%0x][%0x][%0x][%0x]\n",
-                    response[1], response[2], response[3], response[4]);
+            printf("response=");
+            for (n = 0; n < sizeof(response); n++) {
+                printf("[%02x]", response[n]);
+            }
+            printf(" \n");
 
             switch (type) {
                 case R3:
@@ -179,12 +186,12 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
                     break;
             }
         }
+    } else {
+        printf("status=%x, n=%d\n", status, n);
     }
 
     printf("\n");
 
-    hal_gpio_write(g_ss_pin, 1);
-
     return status;
 }
 
@@ -208,6 +215,7 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     int i;
     uint8_t status;
     uint32_t ocr;
+    os_time_t wait_to;
 
     g_ss_pin = ss_pin;
     g_spi_num = spi_num;
@@ -238,20 +246,19 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     /* give 10ms for VDD rampup */
     os_time_delay(OS_TICKS_PER_SEC / 100);
 
-    /* send the required >= 74 clock cycles */
     hal_gpio_write(g_ss_pin, 0);
 
+    /* send the required >= 74 clock cycles */
     for (i = 0; i < 74; i++) {
         hal_spi_tx_val(0, 0xff);
     }
 
-    hal_gpio_write(g_ss_pin, 1);
-
     /* put card in idle state */
     status = send_mmc_cmd(CMD0, 0);
 
     if (status != R_IDLE) {
         /* No card inserted or bad card! */
+        hal_gpio_write(g_ss_pin, 1);
         return mmc_error_by_status(status);
     }
 
@@ -272,21 +279,37 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
         if (ocr & R_ILLEGAL_COMMAND) {
 
         }
+
+        /* TODO: set blocklen */
+
     } else {
         /* Ver2.00 or later SD Memory Card */
 
         /* TODO:
          * 1) check CMD8 response pattern and voltage range.
-         * 2) send ACMD41 while in R_IDLE up to 1s.
+         * 2) DONE: send ACMD41 while in R_IDLE up to 1s.
          * 3) send CMD58, check CCS in response.
          */
 
-        status = send_mmc_cmd(ACMD41, 0);
+#define TIME_TO_WAIT (3 * OS_TICKS_PER_SEC)
+
+        wait_to = os_time_get() + TIME_TO_WAIT;
+        status = send_mmc_cmd(ACMD41, 0x40000000);
+
+        while (status & R_IDLE) {
+            if (os_time_get() > wait_to) {
+                break;
+            }
+            os_time_delay(OS_TICKS_PER_SEC / 10);
+            status = send_mmc_cmd(ACMD41, 0);
+        }
 
-        /* CCS = OCR[30] */
+        /* TODO: check CCS = OCR[30] */
         status = send_mmc_cmd(CMD58, 0);
     }
 
+    hal_gpio_write(g_ss_pin, 1);
+
     return rc;
 }
 
@@ -294,18 +317,91 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_read(void *buf, size_t len)
+mmc_read(uint32_t addr, void *buf, size_t len)
 {
+    uint8_t cmd;
+    uint8_t res;
+    uint32_t n;
+    size_t block_len;
+    size_t block_count;
+    os_time_t timeout;
+    size_t block_addr;
+    size_t offset;
+    size_t index;
+    size_t amount;
+
     hal_gpio_write(g_ss_pin, 0);
+
+    block_len = (len + BLOCK_LEN - 1) & ~(BLOCK_LEN - 1);
+    block_count = block_len / BLOCK_LEN;
+    block_addr = addr / BLOCK_LEN;
+    offset = addr - (block_addr * BLOCK_LEN);
+
+    printf("block_addr=%d, offset=%d\n", block_addr, offset);
+    printf("block_len=%d, block_count=%d\n", block_len, block_count);
+
+    cmd = (block_count == 1) ? CMD17 : CMD18;
+    res = send_mmc_cmd(cmd, block_addr);
+    /* TODO: should be 0 */
+    printf("res=0x%x\n", res);
+
+    /* 7.3.3 Control tokens */
+
+    /* wait up to 1s (should be 200ms???) for control token */
+    timeout = os_time_get() + OS_TICKS_PER_SEC;
+    do {
+        res = hal_spi_tx_val(g_spi_num, 0xff);
+        if (res != 0xFF) {
+            break;
+        }
+        os_time_delay(OS_TICKS_PER_SEC / 20);
+    } while (os_time_get() < timeout);
+
+    printf("res=0x%x\n", res);
+    if (res == 0xFE) {
+        index = 0;
+        while (block_count--) {
+            /**
+             * FIXME: on last run doesn't need to transfer all BLOCK_LEN bytes!
+             */
+
+            for (n = 0; n < BLOCK_LEN; n++) {
+                g_block_buf[n] = hal_spi_tx_val(g_spi_num, 0xff);
+            }
+
+            /* consume CRC-16 */
+            hal_spi_tx_val(g_spi_num, 0xff);
+            hal_spi_tx_val(g_spi_num, 0xff);
+
+            amount = MIN(BLOCK_LEN - offset, len);
+
+            printf("copying %d bytes to index %d\n", amount, index);
+            memcpy(((uint8_t *)buf + index), &g_block_buf[offset], amount);
+
+            offset = 0;
+            len -= amount;
+            index += amount;
+        }
+
+        if (cmd == CMD18) {
+            send_mmc_cmd(CMD12, 0);
+        }
+
+        hal_gpio_write(g_ss_pin, 1);
+
+        return MMC_OK;
+    }
+
     hal_gpio_write(g_ss_pin, 1);
-    return 0;
+
+    return MMC_CARD_ERROR;
 }
 
 /**
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_write(void *buf, size_t len)
+mmc_write(uint32_t addr, void *buf, size_t len)
 {
     return 0;
 }


[45/50] incubator-mynewt-core git commit: oic; fix debug log string in oc_buffer_tx().

Posted by st...@apache.org.
oic; fix debug log string in oc_buffer_tx().


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

Branch: refs/heads/sensors_branch
Commit: e4c5e613c283c35e9ad8893aedc21048cb37d824
Parents: 0dd80b7
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 14 16:15:26 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/api/oc_buffer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e4c5e613/net/oic/src/api/oc_buffer.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_buffer.c b/net/oic/src/api/oc_buffer.c
index a2a00a8..7c3b477 100644
--- a/net/oic/src/api/oc_buffer.c
+++ b/net/oic/src/api/oc_buffer.c
@@ -115,7 +115,7 @@ oc_buffer_tx(struct os_event *ev)
     struct os_mbuf *m;
 
     while ((m = os_mqueue_get(&oc_outq)) != NULL) {
-        OC_LOG_DEBUG("oc_buffer_tx");
+        OC_LOG_DEBUG("oc_buffer_tx: ");
         OC_LOG_ENDPOINT(LOG_LEVEL_DEBUG, OC_MBUF_ENDPOINT(m));
 #ifdef OC_CLIENT
         if (OC_MBUF_ENDPOINT(m)->flags & MULTICAST) {


[23/50] incubator-mynewt-core git commit: newtmgr - Use generic fragmentation function.

Posted by st...@apache.org.
newtmgr - Use generic fragmentation function.


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

Branch: refs/heads/sensors_branch
Commit: fba50b6b8957156c2ad99204aa39b9e26e3f5c5f
Parents: 93cb0c0
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Dec 13 18:09:56 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 mgmt/newtmgr/pkg.yml       |  1 +
 mgmt/newtmgr/src/newtmgr.c | 73 +++++++++++------------------------------
 2 files changed, 20 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fba50b6b/mgmt/newtmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/mgmt/newtmgr/pkg.yml b/mgmt/newtmgr/pkg.yml
index a289dc3..12e5c33 100644
--- a/mgmt/newtmgr/pkg.yml
+++ b/mgmt/newtmgr/pkg.yml
@@ -28,6 +28,7 @@ pkg.deps:
     - kernel/os
     - mgmt/mgmt
     - mgmt/newtmgr/nmgr_os
+    - util/mem
 
 pkg.deps.NEWTMGR_BLE_HOST:
     - mgmt/newtmgr/transport/ble

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fba50b6b/mgmt/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/mgmt/newtmgr/src/newtmgr.c b/mgmt/newtmgr/src/newtmgr.c
index 9e636a4..cbb0958 100644
--- a/mgmt/newtmgr/src/newtmgr.c
+++ b/mgmt/newtmgr/src/newtmgr.c
@@ -25,6 +25,8 @@
 #include "os/os.h"
 #include "os/endian.h"
 
+#include "mem/mem.h"
+
 #include "mgmt/mgmt.h"
 
 #include "newtmgr/newtmgr.h"
@@ -112,64 +114,27 @@ nmgr_send_err_rsp(struct nmgr_transport *nt, struct os_mbuf *m,
 }
 
 /**
- * Splits an appropriately-sized fragment off from the front of an outgoing
- * newtmgr response packet, if necessary.  If the packet size is within the
- * transport MTU, no splitting is performed.  The fragment data is removed from
- * the data packet mbuf.
- *
- * @param om                    The newtmgr response packet.  If this
- *                                  constitutes a single fragment, it gets set
- *                                  to NULL on success.
- * @param out_frag              On success, this points to the fragment to
- *                                  send.  If the entire packet can fit within
- *                                  a single fragment, this will point to the
- *                                  newtmgr response packet itself ('om').
- *
- * @return                      0 on success;
- *                              BLE host core return code on error.
+ * Allocates an mbuf to contain an outgoing response fragment.
  */
-static int
-nmgr_rsp_split_frag(struct os_mbuf **om, uint16_t mtu,
-                    struct os_mbuf **out_frag)
+static struct os_mbuf *
+nmgr_rsp_frag_alloc(uint16_t frag_size, void *arg)
 {
+    struct os_mbuf *src_rsp;
     struct os_mbuf *frag;
-    int rc;
 
-    /* Assume failure. */
-    *out_frag = NULL;
+    /* We need to duplicate the user header from the source response, as that
+     * is where transport-specific information is stored.
+     */
+    src_rsp = arg;
 
-    if (OS_MBUF_PKTLEN(*om) <= mtu) {
-        /* Final fragment. */
-        *out_frag = *om;
-        *om = NULL;
-        return 0;
+    frag = os_msys_get_pkthdr(frag_size, OS_MBUF_USRHDR_LEN(src_rsp));
+    if (frag != NULL) {
+        /* Copy the user header from the response into the fragment mbuf. */
+        memcpy(OS_MBUF_USRHDR(frag), OS_MBUF_USRHDR(src_rsp),
+               OS_MBUF_USRHDR_LEN(src_rsp));
     }
 
-    frag = os_msys_get_pkthdr(mtu, OS_MBUF_USRHDR_LEN(*om));
-    if (frag == NULL) {
-        rc = MGMT_ERR_ENOMEM;
-        goto err;
-    }
-
-    /* Copy the user header from the response into the fragment mbuf. */
-    memcpy(OS_MBUF_USRHDR(frag), OS_MBUF_USRHDR(*om), OS_MBUF_USRHDR_LEN(*om));
-
-    /* Move data from the front of the packet into the fragment mbuf. */
-    rc = os_mbuf_appendfrom(frag, *om, 0, mtu);
-    if (rc != 0) {
-        rc = MGMT_ERR_ENOMEM;
-        goto err;
-    }
-    os_mbuf_adj(*om, mtu);
-    *om = os_mbuf_trim_front(*om);
-
-    /* More fragments to follow. */
-    *out_frag = frag;
-    return 0;
-
-err:
-    os_mbuf_free_chain(frag);
-    return rc;
+    return frag;
 }
 
 /**
@@ -184,9 +149,9 @@ nmgr_rsp_tx(struct nmgr_transport *nt, struct os_mbuf **rsp, uint16_t mtu)
     int rc;
 
     while (*rsp != NULL) {
-        rc = nmgr_rsp_split_frag(rsp, mtu, &frag);
-        if (rc != 0) {
-            return rc;
+        frag = mem_split_frag(rsp, mtu, nmgr_rsp_frag_alloc, *rsp);
+        if (frag == NULL) {
+            return MGMT_ERR_ENOMEM;
         }
 
         rc = nt->nt_output(nt, frag);


[28/50] incubator-mynewt-core git commit: Fix usage of FILE flags

Posted by st...@apache.org.
Fix usage of FILE flags


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

Branch: refs/heads/sensors_branch
Commit: d64f5005b283cde6e7483a19c76bb43150d3a742
Parents: ec77b34
Author: Fabio Utzig <ut...@utzig.org>
Authored: Fri Dec 9 09:35:16 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 fs/fatfs/src/mynewt_glue.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d64f5005/fs/fatfs/src/mynewt_glue.c
----------------------------------------------------------------------
diff --git a/fs/fatfs/src/mynewt_glue.c b/fs/fatfs/src/mynewt_glue.c
index c1dde99..0f45cfb 100644
--- a/fs/fatfs/src/mynewt_glue.c
+++ b/fs/fatfs/src/mynewt_glue.c
@@ -143,19 +143,18 @@ fatfs_open(const char *path, uint8_t access_flags, struct fs_file **out_fs_file)
     }
 
     mode = FA_OPEN_EXISTING;
-    switch (access_flags) {
-    case FS_ACCESS_READ:
+    if (access_flags & FS_ACCESS_READ) {
         mode |= FA_READ;
-        break;
-    case FS_ACCESS_WRITE:
+    }
+    if (access_flags & FS_ACCESS_WRITE) {
         mode |= FA_WRITE;
-        break;
-    case FS_ACCESS_APPEND:
+    }
+    if (access_flags & FS_ACCESS_APPEND) {
         mode |= FA_OPEN_APPEND;
-        break;
-    case FS_ACCESS_TRUNCATE:
+    }
+    if (access_flags & FS_ACCESS_TRUNCATE) {
+        mode &= ~FA_OPEN_EXISTING;
         mode |= FA_CREATE_ALWAYS;
-        break;
     }
 
     res = f_open(out_file, path, mode);
@@ -219,7 +218,7 @@ fatfs_read(struct fs_file *fs_file, uint32_t len, void *out_data,
     FRESULT res;
     FIL *file = (FIL *)fs_file;
 
-    res = f_read(file, out_data, len, out_len);
+    res = f_read(file, out_data, len, (UINT *)out_len);
     return fatfs_to_vfs_error(res);
 }
 


[15/50] incubator-mynewt-core git commit: Misc fixes + fix multi-block read

Posted by st...@apache.org.
Misc fixes + fix multi-block read


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

Branch: refs/heads/sensors_branch
Commit: 8d45cf876c1af7faf9c3126e18af597ecb9ee940
Parents: 373ae60
Author: Fabio Utzig <ut...@utzig.org>
Authored: Thu Dec 8 21:55:01 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/src/mmc.c | 114 +++++++++++++++++++++---------------------
 1 file changed, 58 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d45cf87/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index 8b30929..f0fe71d 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -71,7 +71,7 @@ static struct hal_spi_settings mmc_settings = {
     .word_size  = HAL_SPI_WORD_SIZE_8BIT,
 };
 
-/* FIXME: this limits usage to single MMC spi device */
+/* FIXME: currently limited to single MMC spi device */
 static struct mmc_cfg {
     int                      spi_num;
     int                      ss_pin;
@@ -84,6 +84,8 @@ mmc_error_by_status(uint8_t status)
 {
     if (status == 0) {
         return MMC_OK;
+    } else if (status == 0xff) {
+        return MMC_CARD_ERROR;
     } else if (status & R_IDLE) {
         return MMC_TIMEOUT;
     } else if (status & R_ERASE_RESET) {
@@ -156,11 +158,6 @@ send_mmc_cmd(struct mmc_cfg *mmc, uint8_t cmd, uint32_t payload)
     for (n = 255; n > 0; n--) {
         status = hal_spi_tx_val(mmc->spi_num, 0xff);
         if ((status & 0x80) == 0) break;
-        //os_time_delay(OS_TICKS_PER_SEC / 1000);
-    }
-
-    if (n == 0) {
-        return status;
     }
 
     return status;
@@ -223,7 +220,6 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     os_time_delay(OS_TICKS_PER_SEC / 100);
 
     hal_gpio_write(mmc->ss_pin, 0);
-    hal_spi_tx_val(mmc->spi_num, 0xff);
 
     /* send the required >= 74 clock cycles */
     for (i = 0; i < 74; i++) {
@@ -246,29 +242,18 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
      *       will answer this with R_ILLEGAL_COMMAND.
      */
     status = send_mmc_cmd(mmc, CMD8, 0x1AA);
-    for (i = 0; i < 4; i++) {
-        cmd_resp[i] = (uint8_t) hal_spi_tx_val(mmc->spi_num, 0xff);
-    }
-
-    if (status & R_ILLEGAL_COMMAND) {
-        /* Ver1.x SD Memory Card or Not SD Memory Card */
-
-        ocr = send_mmc_cmd(mmc, CMD58, 0);
-
-        /* TODO: check if voltage range is ok! */
-
-        if (ocr & R_ILLEGAL_COMMAND) {
-
-        }
-
-        /* TODO: set blocklen */
-
-    } else {
+    if (status != 0xff && !(status & R_ILLEGAL_COMMAND)) {
 
         /**
          * Ver2.00 or later SD Memory Card
          */
 
+        /* Read the contents of R7 */
+        for (i = 0; i < 4; i++) {
+            cmd_resp[i] = (uint8_t) hal_spi_tx_val(mmc->spi_num, 0xff);
+        }
+
+        /* Did the card return the same pattern? */
         if (cmd_resp[3] != 0xAA) {
             rc = MMC_RESPONSE_ERROR;
             goto out;
@@ -323,6 +308,24 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     }
 #endif
 
+    } else if (status != 0xff && status & R_ILLEGAL_COMMAND) {
+
+        /**
+         * Ver1.x SD Memory Card or Not SD Memory Card
+         */
+
+        ocr = send_mmc_cmd(mmc, CMD58, 0);
+
+        /* TODO: check if voltage range is ok! */
+
+        if (ocr & R_ILLEGAL_COMMAND) {
+
+        }
+
+        /* TODO: set blocklen */
+
+    } else {
+        rc = mmc_error_by_status(status);
     }
 
 out:
@@ -338,6 +341,7 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
 {
     uint8_t cmd;
     uint8_t res;
+    int rc;
     uint32_t n;
     size_t block_len;
     size_t block_count;
@@ -353,6 +357,8 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
         return (MMC_DEVICE_ERROR);
     }
 
+    rc = MMC_OK;
+
     block_len = (len + BLOCK_LEN - 1) & ~(BLOCK_LEN - 1);
     block_count = block_len / BLOCK_LEN;
     block_addr = addr / BLOCK_LEN;
@@ -365,9 +371,9 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
 
     cmd = (block_count == 1) ? CMD17 : CMD18;
     res = send_mmc_cmd(mmc, cmd, block_addr);
-    if (res != MMC_OK) {
-        hal_gpio_write(mmc->ss_pin, 1);
-        return (MMC_CARD_ERROR);
+    if (res) {
+        rc = mmc_error_by_status(res);
+        goto out;
     }
 
     /**
@@ -384,43 +390,39 @@ mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
     /**
      * 7.3.3.2 Start Block Tokens and Stop Tran Token
      */
-    if (res == 0xFE) {
-        index = 0;
-        while (block_count--) {
-            /**
-             * FIXME: on last run doesn't need to transfer all BLOCK_LEN bytes!
-             */
-
-            for (n = 0; n < BLOCK_LEN; n++) {
-                g_block_buf[n] = hal_spi_tx_val(mmc->spi_num, 0xff);
-            }
-
-            /* FIXME: consume CRC-16, but should check */
-            hal_spi_tx_val(mmc->spi_num, 0xff);
-            hal_spi_tx_val(mmc->spi_num, 0xff);
+    if (res != 0xFE) {
+        rc = MMC_TIMEOUT;
+        goto out;
+    }
 
-            amount = MIN(BLOCK_LEN - offset, len);
+    index = 0;
+    while (block_count--) {
+        for (n = 0; n < BLOCK_LEN; n++) {
+            g_block_buf[n] = hal_spi_tx_val(mmc->spi_num, 0xff);
+        }
 
-            //printf("copying %d bytes to index %d\n", amount, index);
-            memcpy(((uint8_t *)buf + index), &g_block_buf[offset], amount);
+        /* TODO: CRC-16 not used here but would be cool to have */
+        hal_spi_tx_val(mmc->spi_num, 0xff);
+        hal_spi_tx_val(mmc->spi_num, 0xff);
 
-            offset = 0;
-            len -= amount;
-            index += amount;
-        }
+        amount = MIN(BLOCK_LEN - offset, len);
 
-        if (cmd == CMD18) {
-            send_mmc_cmd(mmc, CMD12, 0);  /* FIXME */
-        }
+        memcpy(((uint8_t *)buf + index), &g_block_buf[offset], amount);
 
-        hal_gpio_write(mmc->ss_pin, 1);
+        offset = 0;
+        len -= amount;
+        index += amount;
+    }
 
-        return MMC_OK;
+    if (cmd == CMD18) {
+        send_mmc_cmd(mmc, CMD12, 0);
+        /* FIXME: sending extra cycle to wait for stop, need to loop here? */
+        hal_spi_tx_val(mmc->spi_num, 0xff);
     }
 
+out:
     hal_gpio_write(mmc->ss_pin, 1);
-
-    return MMC_CARD_ERROR;
+    return (rc);
 }
 
 /**


[05/50] incubator-mynewt-core git commit: MYNEWT-508: Add multiple advertising instances

Posted by st...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/include/nimble/hci_common.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/hci_common.h b/net/nimble/include/nimble/hci_common.h
index 9787862..ff34316 100644
--- a/net/nimble/include/nimble/hci_common.h
+++ b/net/nimble/include/nimble/hci_common.h
@@ -51,6 +51,7 @@ extern "C" {
 #define BLE_HCI_OGF_STATUS_PARAMS           (0x05)
 #define BLE_HCI_OGF_TESTING                 (0x06)
 #define BLE_HCI_OGF_LE                      (0x08)
+#define BLE_HCI_OGF_VENDOR                  (0x3F)
 
 /*
  * Number of LE commands. NOTE: this is really just used to size the array

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/include/nimble/hci_vendor.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/hci_vendor.h b/net/nimble/include/nimble/hci_vendor.h
new file mode 100644
index 0000000..2ef3190
--- /dev/null
+++ b/net/nimble/include/nimble/hci_vendor.h
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_HCI_VENDOR_
+#define H_BLE_HCI_VENDOR_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Here is a list of the vendor specific OCFs */
+#define BLE_HCI_OCF_VENDOR_CAPS         (0x153)
+#define BLE_HCI_OCF_MULTI_ADV           (0x154)
+
+/* Multi-advertiser sub-commands */
+#define BLE_HCI_MULTI_ADV_PARAMS        (0x01)
+#define BLE_HCI_MULTI_ADV_DATA          (0x02)
+#define BLE_HCI_MULTI_ADV_SCAN_RSP_DATA (0x03)
+#define BLE_HCI_MULTI_ADV_SET_RAND_ADDR (0x04)
+#define BLE_HCI_MULTI_ADV_ENABLE        (0x05)
+
+/* Command lengths. Includes sub-command opcode */
+#define BLE_HCI_MULTI_ADV_PARAMS_LEN        (24)
+#define BLE_HCI_MULTI_ADV_DATA_LEN          (34)
+#define BLE_HCI_MULTI_ADV_SCAN_RSP_DATA_LEN (34)
+#define BLE_HCI_MULTI_ADV_SET_RAND_ADDR_LEN (8)
+#define BLE_HCI_MULTI_ADV_ENABLE_LEN        (3)
+
+/* Vendor specific events (LE meta events) */
+#define BLE_HCI_LE_SUBEV_ADV_STATE_CHG      (0x55)
+
+/* Data structures associated with vendor specific commands */
+struct hci_vendor_capabilities
+{
+    uint8_t max_advt_instances;
+    uint8_t offloaded_resolution_of_priv_addr;
+    uint16_t total_scan_results_bytes;
+    uint8_t max_irk_list_sz;
+    uint8_t filtering_support;
+    uint8_t max_filters;
+    uint8_t activity_energy_info_support;
+    uint16_t version_supported;
+    uint16_t total_adv_tracked;
+    uint8_t extended_scan_support;
+    uint8_t debug_logging_supported;
+};
+
+/* NOTE: these are not in command order */
+struct hci_multi_adv_params
+{
+    uint8_t adv_type;
+    uint8_t adv_channel_map;
+    uint8_t own_addr_type;
+    uint8_t peer_addr_type;
+    uint8_t adv_filter_policy;
+    int8_t adv_tx_pwr;          /* -70 to +20 */
+    uint8_t adv_instance;
+    uint16_t adv_itvl_min;
+    uint16_t adv_itvl_max;
+    uint8_t peer_addr[BLE_DEV_ADDR_LEN];
+    uint8_t own_addr[BLE_DEV_ADDR_LEN];
+};
+
+/*
+ * NOTE: structures are not defined for the following sub commands.
+ * The format of these commands is:
+ *
+ * Multi-adv Set Advertising Data:
+ *  - Advertising data length (1)
+ *  - Advertising data (31)
+ *  - Advertising Instance (1)
+ *
+ *  Multi-adv Set Scan Response Data:
+ *  - Scan response data length (1)
+ *  - Scan response data (31)
+ *  - Advertising Instance (1)
+ *
+ *  Multi-adv Set Random Address:
+ *  - Random Address (6)
+ *  - Advertising Instance (1)
+ *
+ *  Multi-adv Set Advertising Enable:
+ *  - Random Address (6)
+ *  - Advertising Instance (1)
+ *
+ *  All of these commands generate a Command Complete with this format:
+ *  - Status (1)
+ *  - Multi-adv opcode (1)
+ */
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_BLE_HCI_VENDOR_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c876d8c/net/nimble/syscfg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/syscfg.yml b/net/nimble/syscfg.yml
index 80322c8..f44da01 100644
--- a/net/nimble/syscfg.yml
+++ b/net/nimble/syscfg.yml
@@ -39,3 +39,13 @@ syscfg.defs:
     BLE_WHITELIST:
         description: 'TBD'
         value: 1
+    BLE_MULTI_ADV_SUPPORT:
+        description: 'Support for multi-advertisers'
+        value: 0
+    BLE_MULTI_ADV_INSTANCES:
+        description: >
+            This is the number of multi-advertising instances. This is NOT the
+            total number of advertising instances. The total number of advertising
+            instances is this number plus 1 (assuming the device supports
+            advertising).
+        value: 0


[50/50] incubator-mynewt-core git commit: os; add owner task to eventq structure. Assert if os_eventq_get() is not done by owner.

Posted by st...@apache.org.
os; add owner task to eventq structure. Assert if os_eventq_get()
is not done by owner.


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

Branch: refs/heads/sensors_branch
Commit: 429ab09cb69749781290825764f01636ec0ff149
Parents: 7bdbd5e
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Dec 15 11:02:47 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 kernel/os/include/os/os_eventq.h |  3 ++-
 kernel/os/src/os_eventq.c        | 13 ++++++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/429ab09c/kernel/os/include/os/os_eventq.h
----------------------------------------------------------------------
diff --git a/kernel/os/include/os/os_eventq.h b/kernel/os/include/os/os_eventq.h
index 87b93ea..cfe0d34 100644
--- a/kernel/os/include/os/os_eventq.h
+++ b/kernel/os/include/os/os_eventq.h
@@ -41,7 +41,8 @@ struct os_event {
 #define OS_EVENT_QUEUED(__ev) ((__ev)->ev_queued)
 
 struct os_eventq {
-    struct os_task *evq_task;
+    struct os_task *evq_owner;  /* owner task */
+    struct os_task *evq_task;   /* sleeper; must be either NULL, or the owner */
     STAILQ_HEAD(, os_event) evq_list;
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/429ab09c/kernel/os/src/os_eventq.c
----------------------------------------------------------------------
diff --git a/kernel/os/src/os_eventq.c b/kernel/os/src/os_eventq.c
index 1e76c7c..d443dfd 100644
--- a/kernel/os/src/os_eventq.c
+++ b/kernel/os/src/os_eventq.c
@@ -111,10 +111,21 @@ os_eventq_get(struct os_eventq *evq)
     os_sr_t sr;
     struct os_task *t;
 
+    t = os_sched_get_current_task();
+    if (evq->evq_owner != t) {
+        if (evq->evq_owner == NULL) {
+            evq->evq_owner = t;
+        } else {
+            /*
+             * A task is trying to read from event queue which is handled
+             * by another.
+             */
+            assert(0);
+        }
+    }
     OS_ENTER_CRITICAL(sr);
 pull_one:
     ev = STAILQ_FIRST(&evq->evq_list);
-    t = os_sched_get_current_task();
     if (ev) {
         STAILQ_REMOVE(&evq->evq_list, ev, os_event, ev_next);
         ev->ev_queued = 0;


[32/50] incubator-mynewt-core git commit: oic; surround OIC client only function prototypes with an appropriate #ifdef.

Posted by st...@apache.org.
oic; surround OIC client only function prototypes with an
appropriate #ifdef.


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

Branch: refs/heads/sensors_branch
Commit: ec77b345b90e1083477195b628006c6ef5f73e8b
Parents: 0a07efd
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 10:14:09 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 net/oic/include/oic/oc_api.h          | 2 ++
 net/oic/include/oic/oc_client_state.h | 2 ++
 2 files changed, 4 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ec77b345/net/oic/include/oic/oc_api.h
----------------------------------------------------------------------
diff --git a/net/oic/include/oic/oc_api.h b/net/oic/include/oic/oc_api.h
index ca512e4..e7438e7 100644
--- a/net/oic/include/oic/oc_api.h
+++ b/net/oic/include/oic/oc_api.h
@@ -109,6 +109,7 @@ void oc_send_separate_response(oc_separate_response_t *handle,
 
 int oc_notify_observers(oc_resource_t *resource);
 
+#ifdef OC_CLIENT
 /** Client side */
 #include "oc_client_state.h"
 
@@ -136,6 +137,7 @@ bool oc_do_observe(const char *uri, oc_server_handle_t *server,
                    oc_qos_t qos);
 
 bool oc_stop_observe(const char *uri, oc_server_handle_t *server);
+#endif
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ec77b345/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 dd6308d..5897117 100644
--- a/net/oic/include/oic/oc_client_state.h
+++ b/net/oic/include/oic/oc_client_state.h
@@ -25,6 +25,7 @@
 extern "C" {
 #endif
 
+#ifdef OC_CLIENT
 typedef enum { HIGH_QOS = 0, LOW_QOS } oc_qos_t;
 
 typedef struct
@@ -88,6 +89,7 @@ oc_discovery_flags_t oc_ri_process_discovery_payload(uint8_t *payload, int len,
 
 bool oc_ri_send_rst(oc_endpoint_t *endpoint, uint8_t *token, uint8_t token_len,
                     uint16_t mid);
+#endif
 
 #ifdef __cplusplus
 }


[24/50] incubator-mynewt-core git commit: Write support working - WIP

Posted by st...@apache.org.
Write support working - WIP


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

Branch: refs/heads/sensors_branch
Commit: 4dcf6cb0e2db93e8dff8bc41e86becd800f1cc4f
Parents: 92c4a5d
Author: Fabio Utzig <ut...@utzig.org>
Authored: Tue Dec 6 16:37:03 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/include/mmc/mmc.h |  2 +-
 hw/drivers/mmc/src/mmc.c         | 94 ++++++++++++++++++++---------------
 2 files changed, 55 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4dcf6cb0/hw/drivers/mmc/include/mmc/mmc.h
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/include/mmc/mmc.h b/hw/drivers/mmc/include/mmc/mmc.h
index add9b9b..c9b03af 100644
--- a/hw/drivers/mmc/include/mmc/mmc.h
+++ b/hw/drivers/mmc/include/mmc/mmc.h
@@ -71,7 +71,7 @@ mmc_read(uint32_t addr, void *buf, size_t len);
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_write(uint32_t addr, void *buf, size_t len);
+mmc_write(uint32_t addr, const void *buf, size_t len);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4dcf6cb0/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index 92acc45..d6e4a0d 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -104,6 +104,7 @@ response_type_by_cmd(uint8_t cmd)
     return R1;
 }
 
+/*
 static uint32_t
 ocr_from_r3(uint8_t *response)
 {
@@ -129,6 +130,7 @@ voltage_from_r7(uint8_t *response)
 {
     return response[2] & 0xF;
 }
+*/
 
 static uint8_t
 send_mmc_cmd(uint8_t cmd, uint32_t payload)
@@ -168,12 +170,13 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
 
     //printf("==> sending cmd %d\n", cmd & ~0x80);
 
-    n = 10;
-    do {
+    for (n = 255; n > 0; n--) {
         status = hal_spi_tx_val(g_spi_num, 0xff);
-    } while ((status & 0x80) && --n);
+        if ((status & 0x80) == 0) break;
+        //os_time_delay(OS_TICKS_PER_SEC / 1000);
+    }
 
-    if (!n) {
+    if (n == 0) {
         return status;
     }
 
@@ -191,21 +194,13 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
             response[n] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
         }
 
-#if 0
-        printf("response=");
-        for (n = 0; n < sizeof(response); n++) {
-            printf("[%02x]", response[n]);
-        }
-        printf(" \n");
-#endif
-
         switch (type) {
             case R3:
                 /* NOTE: ocr is defined in section 5.1 */
-                printf("ocr=0x%08lx\n", ocr_from_r3(response));
+                //printf("ocr=0x%08lx\n", ocr_from_r3(response));
                 break;
             case R7:
-                printf("voltage=0x%x\n", voltage_from_r7(response));
+                //printf("voltage=0x%x\n", voltage_from_r7(response));
                 break;
         }
     }
@@ -265,6 +260,7 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     os_time_delay(OS_TICKS_PER_SEC / 100);
 
     hal_gpio_write(g_ss_pin, 0);
+    hal_spi_tx_val(0, 0xff);
 
     /* send the required >= 74 clock cycles */
     for (i = 0; i < 74; i++) {
@@ -321,9 +317,11 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
             os_time_delay(OS_TICKS_PER_SEC / 10);
             status = send_mmc_cmd(ACMD41, 0);
         }
+        //printf("ACMD41 status=%x\n", status);
 
         /* TODO: check CCS = OCR[30] */
         status = send_mmc_cmd(CMD58, 0);
+        //printf("CMD58 status=%x\n", status);
     }
 
     hal_gpio_write(g_ss_pin, 1);
@@ -343,7 +341,7 @@ mmc_read(uint32_t addr, void *buf, size_t len)
     size_t block_len;
     size_t block_count;
     os_time_t timeout;
-    size_t block_addr;
+    uint32_t block_addr;
     size_t offset;
     size_t index;
     size_t amount;
@@ -355,7 +353,7 @@ mmc_read(uint32_t addr, void *buf, size_t len)
     block_addr = addr / BLOCK_LEN;
     offset = addr - (block_addr * BLOCK_LEN);
 
-    //printf("block_addr=%d, offset=%d\n", block_addr, offset);
+    //printf("addr=0x%lx, block_addr=0x%lx, offset=%d\n", addr, block_addr, offset);
     //printf("len=%d, block_len=%d, block_count=%d\n", len, block_len, block_count);
 
     cmd = (block_count == 1) ? CMD17 : CMD18;
@@ -396,7 +394,7 @@ mmc_read(uint32_t addr, void *buf, size_t len)
 
             amount = MIN(BLOCK_LEN - offset, len);
 
-            printf("copying %d bytes to index %d\n", amount, index);
+            //printf("copying %d bytes to index %d\n", amount, index);
             memcpy(((uint8_t *)buf + index), &g_block_buf[offset], amount);
 
             offset = 0;
@@ -422,7 +420,7 @@ mmc_read(uint32_t addr, void *buf, size_t len)
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_write(uint32_t addr, void *buf, size_t len)
+mmc_write(uint32_t addr, const void *buf, size_t len)
 {
     uint8_t cmd;
     uint8_t res;
@@ -430,16 +428,27 @@ mmc_write(uint32_t addr, void *buf, size_t len)
     size_t block_len;
     size_t block_count;
     os_time_t timeout;
-    size_t block_addr;
+    uint32_t block_addr;
     size_t offset;
     size_t index;
     size_t amount;
+    int status;
+
+    hal_gpio_write(g_ss_pin, 0);
 
     block_len = (len + BLOCK_LEN - 1) & ~(BLOCK_LEN - 1);
     block_count = block_len / BLOCK_LEN;
     block_addr = addr / BLOCK_LEN;
     offset = addr - (block_addr * BLOCK_LEN);
 
+    /**
+     * This code ensures that if the requested address doesn't align with the
+     * beginning address of a sector, the initial bytes are first read to the
+     * buffer to be then written later.
+     *
+     * NOTE: this code will never run when using a FS that is sector addressed
+     * like FAT (offset is always 0).
+     */
     if (offset) {
         res = send_mmc_cmd(CMD17, block_addr);
         if (res != MMC_OK) {
@@ -476,21 +485,8 @@ mmc_write(uint32_t addr, void *buf, size_t len)
         return MMC_CARD_ERROR;
     }
 
-    /**
-     * 7.3.3 Control tokens
-     *   Wait up to 500ms for control token.
-     */
-    timeout = os_time_get() + OS_TICKS_PER_SEC / 2;
-    do {
-        res = hal_spi_tx_val(g_spi_num, 0xff);
-        if (res != 0xFF) break;
-        os_time_delay(OS_TICKS_PER_SEC / 20);
-    } while (os_time_get() < timeout);
-
-    if (res == 0xFF) {
-        hal_gpio_write(g_ss_pin, 1);
-        return MMC_CARD_ERROR;
-    }
+    /* FIXME: one byte gap, is this really required? */
+    hal_spi_tx_val(g_spi_num, 0xff);
 
     index = 0;
     do {
@@ -530,14 +526,32 @@ mmc_write(uint32_t addr, void *buf, size_t len)
 
     } while (len);
 
-    hal_gpio_write(g_ss_pin, 1);
+    /* TODO: send stop tran token */
+    if (cmd == CMD25) {
+    }
 
     res &= 0x1f;
-    if (res == 0x0b) {
-        return MMC_CRC_ERROR;
-    } else if (res == 0x0d) {
-        return MMC_WRITE_ERROR;
+    //printf("final response token: 0x%x\n", res);
+    switch (res) {
+        case 0x05:
+            status = MMC_OK;
+            break;
+        case 0x0b:
+            status = MMC_CRC_ERROR;
+            break;
+        case 0x0d:  /* passthrough */
+        default:
+            status = MMC_WRITE_ERROR;
     }
 
-    return MMC_OK;
+    timeout = os_time_get() + 5 * OS_TICKS_PER_SEC;
+    do {
+        res = hal_spi_tx_val(g_spi_num, 0xff);
+        if (res) break;
+        os_time_delay(OS_TICKS_PER_SEC / 100);
+    } while (os_time_get() < timeout);
+
+    hal_gpio_write(g_ss_pin, 1);
+
+    return status;
 }


[20/50] incubator-mynewt-core git commit: bootutil; update signed_images.md with the new location of bootutil.

Posted by st...@apache.org.
bootutil; update signed_images.md with the new location of bootutil.


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

Branch: refs/heads/sensors_branch
Commit: 30f6b67dbd9ab2316e6690a68086c25710260033
Parents: 3657169
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 17:15:24 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 boot/bootutil/signed_images.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/30f6b67d/boot/bootutil/signed_images.md
----------------------------------------------------------------------
diff --git a/boot/bootutil/signed_images.md b/boot/bootutil/signed_images.md
index 64869c1..e7cc2dd 100644
--- a/boot/bootutil/signed_images.md
+++ b/boot/bootutil/signed_images.md
@@ -65,7 +65,7 @@ This gets bootutil to turn on image signature validation.
 
     pkg.name: libs/mykeys
     pkg.deps:
-        - libs/bootutil
+        - "@apache-mynewt-core/boot/bootutil"
 
 ## Sample source file
 This exports the keys.


[11/50] incubator-mynewt-core git commit: Initial bare support for multiple MMC

Posted by st...@apache.org.
Initial bare support for multiple MMC


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

Branch: refs/heads/sensors_branch
Commit: 41c17c470b59f794369b200bb70e70fa7012a791
Parents: 4dcf6cb
Author: Fabio Utzig <ut...@utzig.org>
Authored: Tue Dec 6 21:48:53 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 hw/drivers/mmc/include/mmc/mmc.h |  13 ++-
 hw/drivers/mmc/src/mmc.c         | 187 +++++++++++++++++++---------------
 2 files changed, 115 insertions(+), 85 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/41c17c47/hw/drivers/mmc/include/mmc/mmc.h
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/include/mmc/mmc.h b/hw/drivers/mmc/include/mmc/mmc.h
index c9b03af..2f434ef 100644
--- a/hw/drivers/mmc/include/mmc/mmc.h
+++ b/hw/drivers/mmc/include/mmc/mmc.h
@@ -34,15 +34,16 @@ extern "C" {
 #define MMC_READ_ERROR      (-2)
 #define MMC_WRITE_ERROR     (-3)
 #define MMC_TIMEOUT         (-4)
-#define MMC_PARAM           (-5)
+#define MMC_PARAM_ERROR     (-5)
 #define MMC_CRC_ERROR       (-6)
+#define MMC_DEVICE_ERROR    (-7)
 
 /**
  * Initialize the MMC driver
  *
  * @param spi_num Number of the SPI channel to be used by MMC
  * @param spi_cfg Low-level device specific SPI configuration
- * @param ss_pin Number of SS pin if SW controlled, -1 otherwise
+ * @param ss_pin GPIO number of the SS pin
  *
  * @return 0 on success, non-zero on failure
  */
@@ -52,6 +53,7 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin);
 /**
  * Read data from MMC
  *
+ * @param mmc_id
  * @param addr
  * @param buf
  * @param len
@@ -59,11 +61,12 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin);
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_read(uint32_t addr, void *buf, size_t len);
+mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len);
 
 /**
  * Write data to the MMC
  *
+ * @param mmc_id
  * @param addr
  * @param buf
  * @param len
@@ -71,10 +74,10 @@ mmc_read(uint32_t addr, void *buf, size_t len);
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_write(uint32_t addr, const void *buf, size_t len);
+mmc_write(uint8_t mmc_id, uint32_t addr, const void *buf, size_t len);
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* __ADC_H__ */
+#endif /* __MMC_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/41c17c47/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
index d6e4a0d..199b201 100644
--- a/hw/drivers/mmc/src/mmc.c
+++ b/hw/drivers/mmc/src/mmc.c
@@ -64,17 +64,18 @@ static struct hal_spi_settings mmc_settings = {
     .data_order = HAL_SPI_MSB_FIRST,
     .data_mode  = HAL_SPI_MODE0,
     /* XXX: MMC initialization accepts clocks in the range 100-400KHz */
+    /* TODO: switch to high-speed aka 25MHz after initialization. */
     .baudrate   = 100,
     .word_size  = HAL_SPI_WORD_SIZE_8BIT,
 };
 
 /* FIXME: this limits usage to single MMC spi device */
-static int g_spi_num = -1;
-static int g_ss_pin = -1;
-
-/**
- * TODO: switch to high-speed SPI after initialization.
- */
+static struct mmc_cfg {
+    int                      spi_num;
+    int                      ss_pin;
+    void                     *spi_cfg;
+    struct hal_spi_settings  *settings;
+} g_mmc_cfg;
 
 static int
 mmc_error_by_status(uint8_t status)
@@ -132,26 +133,38 @@ voltage_from_r7(uint8_t *response)
 }
 */
 
+static struct mmc_cfg *
+mmc_cfg_dev(uint8_t id)
+{
+    if (id != 0) {
+        return NULL;
+    }
+
+    return &g_mmc_cfg;
+}
+
 static uint8_t
-send_mmc_cmd(uint8_t cmd, uint32_t payload)
+send_mmc_cmd(struct mmc_cfg *mmc, uint8_t cmd, uint32_t payload)
 {
     int n;
     uint8_t response[4];
     uint8_t status;
     uint8_t type;
+    uint8_t crc;
 
     if (cmd & 0x80) {
         /* TODO: refactor recursion? */
-        send_mmc_cmd(CMD55, 0);
+        /* TODO: error checking */
+        send_mmc_cmd(mmc, CMD55, 0);
     }
 
     /* 4.7.2: Command Format */
-    hal_spi_tx_val(g_spi_num, 0x40 | (cmd & ~0x80));
+    hal_spi_tx_val(mmc->spi_num, 0x40 | (cmd & ~0x80));
 
-    hal_spi_tx_val(g_spi_num, payload >> 24 & 0xff);
-    hal_spi_tx_val(g_spi_num, payload >> 16 & 0xff);
-    hal_spi_tx_val(g_spi_num, payload >>  8 & 0xff);
-    hal_spi_tx_val(g_spi_num, payload       & 0xff);
+    hal_spi_tx_val(mmc->spi_num, payload >> 24 & 0xff);
+    hal_spi_tx_val(mmc->spi_num, payload >> 16 & 0xff);
+    hal_spi_tx_val(mmc->spi_num, payload >>  8 & 0xff);
+    hal_spi_tx_val(mmc->spi_num, payload       & 0xff);
 
     /**
      * 7.2.2 Bus Transfer Protection
@@ -159,19 +172,21 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
      *   require a valid CRC.
      *   NOTE: CRC can be turned on with CMD59 (CRC_ON_OFF).
      */
-    if (cmd == CMD0) {
-        hal_spi_tx_val(g_spi_num, 0x95);
-    } else if (cmd == CMD8) {
-        hal_spi_tx_val(g_spi_num, 0x87);
-    } else {
-        /* Any CRC value is OK here */
-        hal_spi_tx_val(g_spi_num, 0x01);
+    crc = 0x01;
+    switch (cmd) {
+        case CMD0:
+            crc = 0x95;
+            break;
+        case CMD8:
+            crc = 0x87;
+            break;
     }
+    hal_spi_tx_val(mmc->spi_num, crc);
 
     //printf("==> sending cmd %d\n", cmd & ~0x80);
 
     for (n = 255; n > 0; n--) {
-        status = hal_spi_tx_val(g_spi_num, 0xff);
+        status = hal_spi_tx_val(mmc->spi_num, 0xff);
         if ((status & 0x80) == 0) break;
         //os_time_delay(OS_TICKS_PER_SEC / 1000);
     }
@@ -191,7 +206,7 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
     if (!(type == R1 || status & R_ILLEGAL_COMMAND || status & R_CRC_ERROR)) {
         /* Read remaining data for this command */
         for (n = 0; n < sizeof(response); n++) {
-            response[n] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
+            response[n] = (uint8_t) hal_spi_tx_val(mmc->spi_num, 0xff);
         }
 
         switch (type) {
@@ -218,6 +233,7 @@ send_mmc_cmd(uint8_t cmd, uint32_t payload)
  * @param spi_num Number of the SPI channel to be used by MMC
  * @param spi_cfg Low-level device specific SPI configuration
  * @param ss_pin Number of SS pin if SW controlled, -1 otherwise
+ * @param mmc_spi_cfg 
  *
  * @return 0 on success, non-zero on failure
  */
@@ -230,25 +246,25 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     uint32_t ocr;
     os_time_t wait_to;
 
-    g_ss_pin = ss_pin;
-    g_spi_num = spi_num;
+    g_mmc_cfg.spi_num = spi_num;
+    g_mmc_cfg.ss_pin = ss_pin;
+    g_mmc_cfg.spi_cfg = spi_cfg;
+    g_mmc_cfg.settings = &mmc_settings;
 
-    if (g_ss_pin != -1) {
-        hal_gpio_init_out(g_ss_pin, 1);
-    }
+    hal_gpio_init_out(g_mmc_cfg.ss_pin, 1);
 
-    rc = hal_spi_init(g_spi_num, spi_cfg, HAL_SPI_TYPE_MASTER);
+    rc = hal_spi_init(g_mmc_cfg.spi_num, g_mmc_cfg.spi_cfg, HAL_SPI_TYPE_MASTER);
     if (rc) {
         return (rc);
     }
 
-    rc = hal_spi_config(g_spi_num, &mmc_settings);
+    rc = hal_spi_config(g_mmc_cfg.spi_num, g_mmc_cfg.settings);
     if (rc) {
         return (rc);
     }
 
-    hal_spi_set_txrx_cb(0, NULL, NULL);
-    hal_spi_enable(0);
+    hal_spi_set_txrx_cb(g_mmc_cfg.spi_num, NULL, NULL);
+    hal_spi_enable(g_mmc_cfg.spi_num);
 
     /**
      * NOTE: The state machine below follows:
@@ -259,20 +275,20 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
     /* give 10ms for VDD rampup */
     os_time_delay(OS_TICKS_PER_SEC / 100);
 
-    hal_gpio_write(g_ss_pin, 0);
-    hal_spi_tx_val(0, 0xff);
+    hal_gpio_write(g_mmc_cfg.ss_pin, 0);
+    hal_spi_tx_val(g_mmc_cfg.spi_num, 0xff);
 
     /* send the required >= 74 clock cycles */
     for (i = 0; i < 74; i++) {
-        hal_spi_tx_val(0, 0xff);
+        hal_spi_tx_val(g_mmc_cfg.spi_num, 0xff);
     }
 
     /* put card in idle state */
-    status = send_mmc_cmd(CMD0, 0);
+    status = send_mmc_cmd(&g_mmc_cfg, CMD0, 0);
 
     if (status != R_IDLE) {
         /* No card inserted or bad card! */
-        hal_gpio_write(g_ss_pin, 1);
+        hal_gpio_write(g_mmc_cfg.ss_pin, 1);
         return mmc_error_by_status(status);
     }
 
@@ -282,11 +298,11 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
      * NOTE: cards that are not compliant with "Physical Spec Version 2.00"
      *       will answer this with R_ILLEGAL_COMMAND.
      */
-    status = send_mmc_cmd(CMD8, 0x1AA);
+    status = send_mmc_cmd(&g_mmc_cfg, CMD8, 0x1AA);
     if (status & R_ILLEGAL_COMMAND) {
         /* Ver1.x SD Memory Card or Not SD Memory Card */
 
-        ocr = send_mmc_cmd(CMD58, 0);
+        ocr = send_mmc_cmd(&g_mmc_cfg, CMD58, 0);
 
         /* TODO: check if voltage range is ok! */
 
@@ -308,23 +324,23 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
 #define TIME_TO_WAIT (3 * OS_TICKS_PER_SEC)
 
         wait_to = os_time_get() + TIME_TO_WAIT;
-        status = send_mmc_cmd(ACMD41, 0x40000000); /* FIXME */
+        status = send_mmc_cmd(&g_mmc_cfg, ACMD41, 0x40000000); /* FIXME */
 
         while (status & R_IDLE) {
             if (os_time_get() > wait_to) {
                 break;
             }
             os_time_delay(OS_TICKS_PER_SEC / 10);
-            status = send_mmc_cmd(ACMD41, 0);
+            status = send_mmc_cmd(&g_mmc_cfg, ACMD41, 0);
         }
         //printf("ACMD41 status=%x\n", status);
 
         /* TODO: check CCS = OCR[30] */
-        status = send_mmc_cmd(CMD58, 0);
+        status = send_mmc_cmd(&g_mmc_cfg, CMD58, 0);
         //printf("CMD58 status=%x\n", status);
     }
 
-    hal_gpio_write(g_ss_pin, 1);
+    hal_gpio_write(g_mmc_cfg.ss_pin, 1);
 
     return rc;
 }
@@ -333,7 +349,7 @@ mmc_init(int spi_num, void *spi_cfg, int ss_pin)
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_read(uint32_t addr, void *buf, size_t len)
+mmc_read(uint8_t mmc_id, uint32_t addr, void *buf, size_t len)
 {
     uint8_t cmd;
     uint8_t res;
@@ -345,8 +361,12 @@ mmc_read(uint32_t addr, void *buf, size_t len)
     size_t offset;
     size_t index;
     size_t amount;
+    struct mmc_cfg *mmc;
 
-    hal_gpio_write(g_ss_pin, 0);
+    mmc = mmc_cfg_dev(mmc_id);
+    if (mmc == NULL) {
+        return (MMC_DEVICE_ERROR);
+    }
 
     block_len = (len + BLOCK_LEN - 1) & ~(BLOCK_LEN - 1);
     block_count = block_len / BLOCK_LEN;
@@ -356,11 +376,13 @@ mmc_read(uint32_t addr, void *buf, size_t len)
     //printf("addr=0x%lx, block_addr=0x%lx, offset=%d\n", addr, block_addr, offset);
     //printf("len=%d, block_len=%d, block_count=%d\n", len, block_len, block_count);
 
+    hal_gpio_write(mmc->ss_pin, 0);
+
     cmd = (block_count == 1) ? CMD17 : CMD18;
-    res = send_mmc_cmd(cmd, block_addr);
+    res = send_mmc_cmd(mmc, cmd, block_addr);
     if (res != MMC_OK) {
-        hal_gpio_write(g_ss_pin, 1);
-        return MMC_CARD_ERROR;
+        hal_gpio_write(mmc->ss_pin, 1);
+        return (MMC_CARD_ERROR);
     }
 
     /**
@@ -369,7 +391,7 @@ mmc_read(uint32_t addr, void *buf, size_t len)
      */
     timeout = os_time_get() + OS_TICKS_PER_SEC / 5;
     do {
-        res = hal_spi_tx_val(g_spi_num, 0xff);
+        res = hal_spi_tx_val(mmc->spi_num, 0xff);
         if (res != 0xFF) break;
         os_time_delay(OS_TICKS_PER_SEC / 20);
     } while (os_time_get() < timeout);
@@ -385,12 +407,12 @@ mmc_read(uint32_t addr, void *buf, size_t len)
              */
 
             for (n = 0; n < BLOCK_LEN; n++) {
-                g_block_buf[n] = hal_spi_tx_val(g_spi_num, 0xff);
+                g_block_buf[n] = hal_spi_tx_val(mmc->spi_num, 0xff);
             }
 
             /* FIXME: consume CRC-16, but should check */
-            hal_spi_tx_val(g_spi_num, 0xff);
-            hal_spi_tx_val(g_spi_num, 0xff);
+            hal_spi_tx_val(mmc->spi_num, 0xff);
+            hal_spi_tx_val(mmc->spi_num, 0xff);
 
             amount = MIN(BLOCK_LEN - offset, len);
 
@@ -403,15 +425,15 @@ mmc_read(uint32_t addr, void *buf, size_t len)
         }
 
         if (cmd == CMD18) {
-            send_mmc_cmd(CMD12, 0);
+            send_mmc_cmd(mmc, CMD12, 0);  /* FIXME */
         }
 
-        hal_gpio_write(g_ss_pin, 1);
+        hal_gpio_write(mmc->ss_pin, 1);
 
         return MMC_OK;
     }
 
-    hal_gpio_write(g_ss_pin, 1);
+    hal_gpio_write(mmc->ss_pin, 1);
 
     return MMC_CARD_ERROR;
 }
@@ -420,7 +442,7 @@ mmc_read(uint32_t addr, void *buf, size_t len)
  * @return 0 on success, non-zero on failure
  */
 int
-mmc_write(uint32_t addr, const void *buf, size_t len)
+mmc_write(uint8_t mmc_id, uint32_t addr, const void *buf, size_t len)
 {
     uint8_t cmd;
     uint8_t res;
@@ -433,14 +455,20 @@ mmc_write(uint32_t addr, const void *buf, size_t len)
     size_t index;
     size_t amount;
     int status;
+    struct mmc_cfg *mmc;
 
-    hal_gpio_write(g_ss_pin, 0);
+    mmc = mmc_cfg_dev(mmc_id);
+    if (mmc == NULL) {
+        return (MMC_DEVICE_ERROR);
+    }
 
     block_len = (len + BLOCK_LEN - 1) & ~(BLOCK_LEN - 1);
     block_count = block_len / BLOCK_LEN;
     block_addr = addr / BLOCK_LEN;
     offset = addr - (block_addr * BLOCK_LEN);
 
+    hal_gpio_write(mmc->ss_pin, 0);
+
     /**
      * This code ensures that if the requested address doesn't align with the
      * beginning address of a sector, the initial bytes are first read to the
@@ -450,43 +478,43 @@ mmc_write(uint32_t addr, const void *buf, size_t len)
      * like FAT (offset is always 0).
      */
     if (offset) {
-        res = send_mmc_cmd(CMD17, block_addr);
+        res = send_mmc_cmd(mmc, CMD17, block_addr);
         if (res != MMC_OK) {
-            hal_gpio_write(g_ss_pin, 1);
-            return MMC_CARD_ERROR;
+            hal_gpio_write(mmc->ss_pin, 1);
+            return (MMC_CARD_ERROR);
         }
 
         timeout = os_time_get() + OS_TICKS_PER_SEC / 5;
         do {
-            res = hal_spi_tx_val(g_spi_num, 0xff);
-            if (res != 0xFF) break;
+            res = hal_spi_tx_val(mmc->spi_num, 0xff);
+            if (res != 0xff) break;
             os_time_delay(OS_TICKS_PER_SEC / 20);
         } while (os_time_get() < timeout);
 
-        if (res != 0xFE) {
-            hal_gpio_write(g_ss_pin, 1);
-            return MMC_CARD_ERROR;
+        if (res != 0xfe) {
+            hal_gpio_write(mmc->ss_pin, 1);
+            return (MMC_CARD_ERROR);
         }
 
         for (n = 0; n < BLOCK_LEN; n++) {
-            g_block_buf[n] = hal_spi_tx_val(g_spi_num, 0xff);
+            g_block_buf[n] = hal_spi_tx_val(mmc->spi_num, 0xff);
         }
 
-        hal_spi_tx_val(g_spi_num, 0xff);
-        hal_spi_tx_val(g_spi_num, 0xff);
+        hal_spi_tx_val(mmc->spi_num, 0xff);
+        hal_spi_tx_val(mmc->spi_num, 0xff);
     }
 
     /* now start write */
 
     cmd = (block_count == 1) ? CMD24 : CMD25;
-    res = send_mmc_cmd(cmd, block_addr);
+    res = send_mmc_cmd(mmc, cmd, block_addr);
     if (res != MMC_OK) {
-        hal_gpio_write(g_ss_pin, 1);
-        return MMC_CARD_ERROR;
+        hal_gpio_write(mmc->ss_pin, 1);
+        return (MMC_CARD_ERROR);
     }
 
     /* FIXME: one byte gap, is this really required? */
-    hal_spi_tx_val(g_spi_num, 0xff);
+    hal_spi_tx_val(mmc->spi_num, 0xff);
 
     index = 0;
     do {
@@ -496,26 +524,26 @@ mmc_write(uint32_t addr, const void *buf, size_t len)
          * 7.3.3.2 Start Block Tokens and Stop Tran Token
          */
         if (cmd == CMD24) {
-            hal_spi_tx_val(g_spi_num, 0xFE);
+            hal_spi_tx_val(mmc->spi_num, 0xFE);
         } else {
-            hal_spi_tx_val(g_spi_num, 0xFC);
+            hal_spi_tx_val(mmc->spi_num, 0xFC);
         }
 
         amount = MIN(BLOCK_LEN - offset, len);
         memcpy(&g_block_buf[offset], ((uint8_t *)buf + index), amount);
 
         for (n = 0; n < BLOCK_LEN; n++) {
-            hal_spi_tx_val(g_spi_num, g_block_buf[n]);
+            hal_spi_tx_val(mmc->spi_num, g_block_buf[n]);
         }
 
         /* CRC */
-        hal_spi_tx_val(g_spi_num, 0xff);
-        hal_spi_tx_val(g_spi_num, 0xff);
+        hal_spi_tx_val(mmc->spi_num, 0xff);
+        hal_spi_tx_val(mmc->spi_num, 0xff);
 
         /**
          * 7.3.3.1 Data Response Token
          */
-        res = hal_spi_tx_val(g_spi_num, 0xff);
+        res = hal_spi_tx_val(mmc->spi_num, 0xff);
         if ((res & 0x1f) != 0x05) {
             break;
         }
@@ -531,7 +559,6 @@ mmc_write(uint32_t addr, const void *buf, size_t len)
     }
 
     res &= 0x1f;
-    //printf("final response token: 0x%x\n", res);
     switch (res) {
         case 0x05:
             status = MMC_OK;
@@ -546,12 +573,12 @@ mmc_write(uint32_t addr, const void *buf, size_t len)
 
     timeout = os_time_get() + 5 * OS_TICKS_PER_SEC;
     do {
-        res = hal_spi_tx_val(g_spi_num, 0xff);
+        res = hal_spi_tx_val(mmc->spi_num, 0xff);
         if (res) break;
         os_time_delay(OS_TICKS_PER_SEC / 100);
     } while (os_time_get() < timeout);
 
-    hal_gpio_write(g_ss_pin, 1);
+    hal_gpio_write(mmc->ss_pin, 1);
 
     return status;
 }


[35/50] incubator-mynewt-core git commit: oic; we don't actually keep track of mac address in oc_endpoint. So remove it.

Posted by st...@apache.org.
oic; we don't actually keep track of mac address in oc_endpoint.
So remove it.


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

Branch: refs/heads/sensors_branch
Commit: 8680eeb63eac7a8b26181c0eed20db190878f354
Parents: e4c5e61
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 14 16:17:01 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 net/oic/src/port/mynewt/log.c      | 6 +-----
 net/oic/src/port/oc_connectivity.h | 2 --
 2 files changed, 1 insertion(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8680eeb6/net/oic/src/port/mynewt/log.c
----------------------------------------------------------------------
diff --git a/net/oic/src/port/mynewt/log.c b/net/oic/src/port/mynewt/log.c
index 85b80c6..b2f1b88 100644
--- a/net/oic/src/port/mynewt/log.c
+++ b/net/oic/src/port/mynewt/log.c
@@ -48,11 +48,7 @@ oc_log_endpoint(uint16_t lvl, struct oc_endpoint *oe)
 #endif
 #if (MYNEWT_VAL(OC_TRANSPORT_GATT) == 1)
     case GATT:
-        snprintf(tmp, sizeof(tmp), "%02x:%02x:%02x:%02x:%02x:%02x-%u\n",
-                 oe->bt_addr.address[0], oe->bt_addr.address[1],
-                 oe->bt_addr.address[2], oe->bt_addr.address[3],
-                 oe->bt_addr.address[4], oe->bt_addr.address[5],
-                 oe->bt_addr.conn_handle);
+        snprintf(tmp, sizeof(tmp), "%u\n", oe->bt_addr.conn_handle);
         str = tmp;
         break;
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8680eeb6/net/oic/src/port/oc_connectivity.h
----------------------------------------------------------------------
diff --git a/net/oic/src/port/oc_connectivity.h b/net/oic/src/port/oc_connectivity.h
index 807b0fb..ea44e3e 100644
--- a/net/oic/src/port/oc_connectivity.h
+++ b/net/oic/src/port/oc_connectivity.h
@@ -31,8 +31,6 @@ typedef struct {
 } oc_ipv6_addr_t;
 
 typedef struct {
-    uint8_t type;
-    uint8_t address[6];
     uint16_t conn_handle;
 } oc_le_addr_t;
 


[34/50] incubator-mynewt-core git commit: bleprph_oic; app does not need extra mbufs anymore.

Posted by st...@apache.org.
bleprph_oic; app does not need extra mbufs anymore.


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

Branch: refs/heads/sensors_branch
Commit: 645a5b760d7f9019370924d3f455a3985f94b8a0
Parents: c8c06dd
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Dec 16 15:38:59 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 apps/bleprph_oic/syscfg.yml | 3 ---
 1 file changed, 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/645a5b76/apps/bleprph_oic/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph_oic/syscfg.yml b/apps/bleprph_oic/syscfg.yml
index 677c5fe..220978c 100644
--- a/apps/bleprph_oic/syscfg.yml
+++ b/apps/bleprph_oic/syscfg.yml
@@ -38,9 +38,6 @@ syscfg.vals:
     # Disable unused eddystone feature.
     BLE_EDDYSTONE: 0
 
-    # Need to get this down. XXXXX
-    MSYS_1_BLOCK_COUNT: 18
-
     # Log reboot messages to a flash circular buffer.
     REBOOT_LOG_FCB: 1
     LOG_FCB: 1


[13/50] incubator-mynewt-core git commit: bootutil tests - Don't assume min-write-sz==1.

Posted by st...@apache.org.
bootutil tests - Don't assume min-write-sz==1.


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

Branch: refs/heads/sensors_branch
Commit: c27678e8430cea01f2181109b43dd47d97359d76
Parents: 4fe1cb3
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Dec 13 14:50:29 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 boot/bootutil/test/src/boot_test.h       |  1 +
 boot/bootutil/test/src/boot_test_utils.c | 23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c27678e8/boot/bootutil/test/src/boot_test.h
----------------------------------------------------------------------
diff --git a/boot/bootutil/test/src/boot_test.h b/boot/bootutil/test/src/boot_test.h
index b10bec6..072dfbb 100644
--- a/boot/bootutil/test/src/boot_test.h
+++ b/boot/bootutil/test/src/boot_test.h
@@ -59,6 +59,7 @@ extern struct boot_test_img_addrs boot_test_img_addrs[];
 #define BOOT_TEST_AREA_IDX_SCRATCH 6
 
 uint8_t boot_test_util_byte_at(int img_msb, uint32_t image_offset);
+uint8_t boot_test_util_flash_align(void);
 void boot_test_util_init_flash(void);
 void boot_test_util_copy_area(int from_area_idx, int to_area_idx);
 void boot_test_util_swap_areas(int area_idx1, int area_idx2);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c27678e8/boot/bootutil/test/src/boot_test_utils.c
----------------------------------------------------------------------
diff --git a/boot/bootutil/test/src/boot_test_utils.c b/boot/bootutil/test/src/boot_test_utils.c
index d882101..dc6fbb0 100644
--- a/boot/bootutil/test/src/boot_test_utils.c
+++ b/boot/bootutil/test/src/boot_test_utils.c
@@ -56,6 +56,17 @@ boot_test_util_byte_at(int img_msb, uint32_t image_offset)
     return u8p[image_offset % 4];
 }
 
+uint8_t
+boot_test_util_flash_align(void)
+{
+    const struct flash_area *fap;
+    int rc;
+
+    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    return flash_area_align(fap);
+}
 
 void
 boot_test_util_init_flash(void)
@@ -113,6 +124,7 @@ boot_test_util_area_write_size(int dst_idx, uint32_t off, uint32_t size)
     const struct flash_area *desc;
     int64_t diff;
     uint32_t trailer_start;
+    uint8_t elem_sz;
 
     if (dst_idx != BOOT_TEST_AREA_IDX_SCRATCH - 1) {
         return size;
@@ -120,7 +132,8 @@ boot_test_util_area_write_size(int dst_idx, uint32_t off, uint32_t size)
 
     /* Don't include trailer in copy to second slot. */
     desc = boot_test_area_descs + dst_idx;
-    trailer_start = desc->fa_size - boot_trailer_sz(1);
+    elem_sz = boot_test_util_flash_align();
+    trailer_start = desc->fa_size - boot_trailer_sz(elem_sz);
     diff = off + size - trailer_start;
     if (diff > 0) {
         if (diff > size) {
@@ -255,10 +268,10 @@ boot_test_util_write_hash(const struct image_header *hdr, int slot)
     tlv._pad = 0;
     tlv.it_len = sizeof(hash);
 
-    rc = hal_flash_write(flash_id, addr + off, &tlv, sizeof(tlv));
-    TEST_ASSERT(rc == 0);
-    off += sizeof(tlv);
-    rc = hal_flash_write(flash_id, addr + off, hash, sizeof(hash));
+    memcpy(tmpdata, &tlv, sizeof tlv);
+    memcpy(tmpdata + sizeof tlv, hash, sizeof hash);
+    rc = hal_flash_write(flash_id, addr + off, tmpdata,
+                         sizeof tlv + sizeof hash);
     TEST_ASSERT(rc == 0);
 }
 


[19/50] incubator-mynewt-core git commit: cborattr; add helper routine cbor_read_flat_attrs(). This takes as argument pointer to beginning of cbor encoded data, and decodes it into an array of cbor_attr_t's.

Posted by st...@apache.org.
cborattr; add helper routine cbor_read_flat_attrs().
This takes as argument pointer to beginning of cbor encoded data,
and decodes it into an array of cbor_attr_t's.


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

Branch: refs/heads/sensors_branch
Commit: 9cafdcdbcd0a878232d7a7cbf29d4befa275dd48
Parents: bcc4050
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Dec 13 10:07:02 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 encoding/cborattr/include/cborattr/cborattr.h |  2 ++
 encoding/cborattr/src/cborattr.c              | 28 ++++++++++++++++++++++
 2 files changed, 30 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9cafdcdb/encoding/cborattr/include/cborattr/cborattr.h
----------------------------------------------------------------------
diff --git a/encoding/cborattr/include/cborattr/cborattr.h b/encoding/cborattr/include/cborattr/cborattr.h
index d3ca2fa..d7f0e03 100644
--- a/encoding/cborattr/include/cborattr/cborattr.h
+++ b/encoding/cborattr/include/cborattr/cborattr.h
@@ -121,6 +121,8 @@ struct cbor_attr_t {
 int cbor_read_object(struct CborValue *, const struct cbor_attr_t *);
 int cbor_read_array(struct CborParser *, const struct cbor_array_t *);
 
+int cbor_read_flat_attrs(const uint8_t *data, int len,
+                         const struct cbor_attr_t *attrs);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9cafdcdb/encoding/cborattr/src/cborattr.c
----------------------------------------------------------------------
diff --git a/encoding/cborattr/src/cborattr.c b/encoding/cborattr/src/cborattr.c
index a7ca9c0..4101646 100644
--- a/encoding/cborattr/src/cborattr.c
+++ b/encoding/cborattr/src/cborattr.c
@@ -19,6 +19,7 @@
 
 #include <cborattr/cborattr.h>
 #include <tinycbor/cbor.h>
+#include <tinycbor/cbor_buf_reader.h>
 
 /* this maps a CborType to a matching CborAtter Type. The mapping is not
  * one-to-one because of signedness of integers
@@ -265,3 +266,30 @@ cbor_read_object(struct CborValue *value, const struct cbor_attr_t *attrs)
     st = cbor_internal_read_object(value, attrs, NULL, 0);
     return st;
 }
+
+/*
+ * Read in cbor key/values from flat buffer pointed by data, and fill them
+ * into attrs.
+ *
+ * @param data		Pointer to beginning of cbor encoded data
+ * @param len		Number of bytes in the buffer
+ * @param attrs		Array of cbor objects to look for.
+ *
+ * @return		0 on success; non-zero on failure.
+ */
+int
+cbor_read_flat_attrs(const uint8_t *data, int len,
+                     const struct cbor_attr_t *attrs)
+{
+    struct cbor_buf_reader reader;
+    struct CborParser parser;
+    struct CborValue value;
+    CborError err;
+
+    cbor_buf_reader_init(&reader, data, len);
+    err = cbor_parser_init(&reader.r, 0, &parser, &value);
+    if (err != CborNoError) {
+        return -1;
+    }
+    return cbor_read_object(&value, attrs);
+}


[08/50] incubator-mynewt-core git commit: MMC driver WIP

Posted by st...@apache.org.
MMC driver WIP


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

Branch: refs/heads/sensors_branch
Commit: a049361eae2a8e1eddadf9c9874f94ae3a3abc11
Parents: c656e5a
Author: Fabio Utzig <ut...@utzig.org>
Authored: Wed Nov 30 17:01:49 2016 -0200
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:16 2016 -0800

----------------------------------------------------------------------
 apps/mmctest/pkg.yml             |  31 +++
 apps/mmctest/src/main.c          | 369 ++++++++++++++++++++++++++++++++++
 apps/mmctest/syscfg.yml          |  22 ++
 hw/drivers/mmc/include/mmc/mmc.h |  52 +++++
 hw/drivers/mmc/pkg.yml           |  27 +++
 hw/drivers/mmc/src/mmc.c         | 311 ++++++++++++++++++++++++++++
 6 files changed, 812 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a049361e/apps/mmctest/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/mmctest/pkg.yml b/apps/mmctest/pkg.yml
new file mode 100644
index 0000000..c42160c
--- /dev/null
+++ b/apps/mmctest/pkg.yml
@@ -0,0 +1,31 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: apps/mmctest
+pkg.type: app
+pkg.description: TBD
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - sys/console/full
+    - kernel/os
+    - hw/drivers/mmc
+    - hw/mcu/stm/stm32f4xx

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a049361e/apps/mmctest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/mmctest/src/main.c b/apps/mmctest/src/main.c
new file mode 100755
index 0000000..34847ea
--- /dev/null
+++ b/apps/mmctest/src/main.c
@@ -0,0 +1,369 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+#include "bsp/bsp.h"
+#include "hal/hal_gpio.h"
+#include "hal/hal_spi.h"
+#include <mmc/mmc.h>
+#include <mcu/stm32f4xx_mynewt_hal.h>
+#include <os/os_dev.h>
+#include <assert.h>
+#include <string.h>
+#include <console/console.h>
+#ifdef ARCH_sim
+#include <mcu/mcu_sim.h>
+#endif
+
+/* The spi txrx callback */
+struct sblinky_spi_cb_arg
+{
+    int transfers;
+    int txlen;
+    uint32_t tx_rx_bytes;
+};
+struct sblinky_spi_cb_arg spi_cb_obj;
+void *spi_cb_arg;
+
+/* Task 1 */
+#define TASK1_PRIO (1)
+#define TASK1_STACK_SIZE    OS_STACK_ALIGN(1024)
+struct os_task task1;
+
+/* Task 2 */
+#define TASK2_PRIO (2)
+#define TASK2_STACK_SIZE    OS_STACK_ALIGN(512)
+static struct os_task task2;
+
+#define TASK3_PRIO (3)
+#define TASK3_STACK_SIZE    OS_STACK_ALIGN(512)
+static struct os_task task3;
+
+static struct os_eventq mmctest_evq;
+
+/* Global test semaphore */
+struct os_sem g_test_sem;
+
+/* For LED toggling */
+int g_led_pin;
+
+static const int SPI_SS_PIN   = MCU_GPIO_PORTA(4);
+static const int SPI_SCK_PIN  = MCU_GPIO_PORTA(5);
+static const int SPI_MISO_PIN = MCU_GPIO_PORTA(6);
+static const int SPI_MOSI_PIN = MCU_GPIO_PORTA(7);
+
+uint8_t g_spi_tx_buf[32];
+uint8_t g_spi_last_tx_buf[32];
+uint8_t g_spi_rx_buf[32];
+uint32_t g_spi_xfr_num;
+uint8_t g_spi_null_rx;
+uint8_t g_last_tx_len;
+
+static
+void mmctest_validate_last(int len)
+{
+    int i;
+    int curlen;
+    int remlen;
+    int curindex;
+    uint8_t expval;
+
+    if (g_spi_null_rx == 0) {
+        expval = 0xaa;
+        if (g_last_tx_len < len) {
+            curlen = g_last_tx_len;
+            remlen = len - g_last_tx_len;
+        } else {
+            curlen = len;
+            remlen = 0;
+        }
+        for (i = 0; i < curlen; ++i) {
+            if (g_spi_rx_buf[i] != g_spi_last_tx_buf[i]) {
+                assert(0);
+            }
+        }
+        curindex = curlen;
+        for (i = 0; i < remlen; ++i) {
+            if (g_spi_rx_buf[curindex + i] != expval) {
+                assert(0);
+            }
+        }
+    }
+}
+
+void
+sblinky_spi_irqm_handler(void *arg, int len)
+{
+    int i;
+    struct sblinky_spi_cb_arg *cb;
+
+    //hal_gpio_write(SPI_SS_PIN, 1);
+
+    assert(arg == spi_cb_arg);
+    if (spi_cb_arg) {
+        cb = (struct sblinky_spi_cb_arg *)arg;
+        assert(len == cb->txlen);
+        ++cb->transfers;
+    }
+
+    /* Make sure we get back the data we expect! */
+    if (g_spi_xfr_num == 1) {
+        /* The first time we expect entire buffer to be filled with 0x88 */
+        for (i = 0; i < len; ++i) {
+            if (g_spi_rx_buf[i] != 0x88) {
+                assert(0);
+            }
+        }
+
+        /* copy current tx buf to last */
+        memcpy(g_spi_last_tx_buf, g_spi_tx_buf, len);
+    } else {
+        /* Check that we received what we last sent */
+        mmctest_validate_last(len);
+    }
+    ++g_spi_xfr_num;
+}
+
+void
+task1_handler(void *arg)
+{
+    //int i;
+    int rc;
+    //uint16_t rxval;
+    //uint8_t last_val;
+    //uint8_t spi_nb_cntr;
+    //uint8_t spi_b_cntr;
+
+    struct stm32f4_hal_spi_cfg spi_cfg = {
+        .ss_pin   = SPI_SS_PIN,
+        .sck_pin  = SPI_SCK_PIN,
+        .miso_pin = SPI_MISO_PIN,
+        .mosi_pin = SPI_MOSI_PIN,
+        .irq_prio = 2
+    };
+
+    rc = mmc_init(0, &spi_cfg, spi_cfg.ss_pin);
+    printf("mmc_init %d\n", rc);
+
+#if 0
+    /*
+     * Send some bytes in a non-blocking manner to SPI using tx val. The
+     * slave should send back 0x77.
+     */
+    g_spi_tx_buf[0] = 0xde;
+    g_spi_tx_buf[1] = 0xad;
+    g_spi_tx_buf[2] = 0xbe;
+    g_spi_tx_buf[3] = 0xef;
+    hal_gpio_write(SPI_SS_PIN, 0);
+    for (i = 0; i < 4; ++i) {
+        rxval = hal_spi_tx_val(0, g_spi_tx_buf[i]);
+        assert(rxval == 0x77);
+        g_spi_rx_buf[i] = (uint8_t)rxval;
+    }
+    hal_gpio_write(SPI_SS_PIN, 1);
+    ++g_spi_xfr_num;
+
+    /* Set up the callback to use when non-blocking API used */
+    hal_spi_disable(0);
+    spi_cb_arg = &spi_cb_obj;
+    spi_cb_obj.txlen = 32;
+    hal_spi_set_txrx_cb(0, sblinky_spi_irqm_handler, spi_cb_arg);
+    hal_spi_enable(0);
+    spi_nb_cntr = 0;
+    spi_b_cntr = 0;
+
+    while (1) {
+        /* Get random length to send */
+        g_last_tx_len = spi_cb_obj.txlen;
+        spi_cb_obj.txlen = (rand() & 0x1F) + 1;
+        memcpy(g_spi_last_tx_buf, g_spi_tx_buf, g_last_tx_len);
+        last_val = g_spi_last_tx_buf[g_last_tx_len - 1];
+        for (i= 0; i < spi_cb_obj.txlen; ++i) {
+            g_spi_tx_buf[i] = (uint8_t)(last_val + i);
+        }
+
+        if (g_spi_xfr_num & 1) {
+            /* Send non-blocking */
+            ++spi_nb_cntr;
+            assert(hal_gpio_read(SPI_SS_PIN) == 1);
+            hal_gpio_write(SPI_SS_PIN, 0);
+#if 0
+            if (spi_nb_cntr == 7) {
+                g_spi_null_rx = 1;
+                rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, NULL, 32);
+            } else {
+                g_spi_null_rx = 0;
+                rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, g_spi_rx_buf, 32);
+            }
+            assert(!rc);
+#else
+            g_spi_null_rx = 0;
+            rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, g_spi_rx_buf,
+                                      spi_cb_obj.txlen);
+            assert(!rc);
+            console_printf("a transmitted: ");
+            for (i = 0; i < spi_cb_obj.txlen; i++) {
+                console_printf("%2x ", g_spi_tx_buf[i]);
+            }
+            console_printf("\n");
+            console_printf("received: ");
+            for (i = 0; i < spi_cb_obj.txlen; i++) {
+                console_printf("%2x ", g_spi_rx_buf[i]);
+            }
+            console_printf("\n");
+#endif
+        } else {
+            /* Send blocking */
+            ++spi_b_cntr;
+            assert(hal_gpio_read(SPI_SS_PIN) == 1);
+            hal_gpio_write(SPI_SS_PIN, 0);
+#if 0
+            if (spi_b_cntr == 7) {
+                g_spi_null_rx = 1;
+                rc = hal_spi_txrx(0, g_spi_tx_buf, NULL, 32);
+                spi_b_cntr = 0;
+            } else {
+                g_spi_null_rx = 0;
+                rc = hal_spi_txrx(0, g_spi_tx_buf, g_spi_rx_buf, 32);
+            }
+            assert(!rc);
+            hal_gpio_write(SPI_SS_PIN, 1);
+            mmctest_validate_last(spi_cb_obj.txlen);
+#else
+            rc = hal_spi_txrx(0, g_spi_tx_buf, g_spi_rx_buf, spi_cb_obj.txlen);
+            assert(!rc);
+            hal_gpio_write(SPI_SS_PIN, 1);
+            console_printf("b transmitted: ");
+            for (i = 0; i < spi_cb_obj.txlen; i++) {
+                console_printf("%2x ", g_spi_tx_buf[i]);
+            }
+            console_printf("\n");
+            console_printf("received: ");
+            for (i = 0; i < spi_cb_obj.txlen; i++) {
+                console_printf("%2x ", g_spi_rx_buf[i]);
+            }
+            console_printf("\n");
+            mmctest_validate_last(spi_cb_obj.txlen);
+            ++g_spi_xfr_num;
+#endif
+        }
+    }
+#endif
+
+    while (1) {
+        os_time_delay(OS_TICKS_PER_SEC);
+    }
+}
+
+/**
+ * This task serves as a container for the shell and newtmgr packages.  These
+ * packages enqueue timer events when they need this task to do work.
+ */
+static void
+task2_handler(void *arg)
+{
+    while (1) {
+        os_eventq_run(&mmctest_evq);
+    }
+}
+
+static void
+task3_handler(void *arg)
+{
+    while (1) {
+        os_time_delay(OS_TICKS_PER_SEC);
+        hal_gpio_toggle(g_led_pin);
+    }
+}
+
+/**
+ * init_tasks
+ *
+ * Called by main.c after sysinit(). This function performs initializations
+ * that are required before tasks are running.
+ *
+ * @return int 0 success; error otherwise.
+ */
+static void
+init_tasks(void)
+{
+    os_stack_t *pstack;
+
+    /* Initialize global test semaphore */
+    os_sem_init(&g_test_sem, 0);
+
+    pstack = malloc(sizeof(os_stack_t)*TASK1_STACK_SIZE);
+    assert(pstack);
+
+    os_task_init(&task1, "task1", task1_handler, NULL,
+            TASK1_PRIO, OS_WAIT_FOREVER, pstack, TASK1_STACK_SIZE);
+
+    pstack = malloc(sizeof(os_stack_t)*TASK2_STACK_SIZE);
+    assert(pstack);
+
+    os_task_init(&task2, "task2", task2_handler, NULL,
+            TASK2_PRIO, OS_WAIT_FOREVER, pstack, TASK2_STACK_SIZE);
+
+    pstack = malloc(sizeof(os_stack_t)*TASK3_STACK_SIZE);
+    assert(pstack);
+
+    os_task_init(&task3, "task3", task3_handler, NULL,
+            TASK3_PRIO, OS_WAIT_FOREVER, pstack, TASK3_STACK_SIZE);
+
+    /* Initialize eventq and designate it as the default.  Packages that need
+     * to schedule work items will piggyback on this eventq.  Example packages
+     * which do this are sys/shell and mgmt/newtmgr.
+     */
+    os_eventq_init(&mmctest_evq);
+    os_eventq_dflt_set(&mmctest_evq);
+}
+
+/**
+ * main
+ *
+ * The main function for the project. This function initializes the os, calls
+ * init_tasks to initialize tasks (and possibly other objects), then starts the
+ * OS. We should not return from os start.
+ *
+ * @return int NOTE: this function should never return!
+ */
+int
+main(int argc, char **argv)
+{
+    int rc;
+
+#ifdef ARCH_sim
+    mcu_sim_parse_args(argc, argv);
+#endif
+
+    sysinit();
+
+    /* Set the led pin for the devboard */
+    g_led_pin = LED_BLINK_PIN;
+    hal_gpio_init_out(g_led_pin, 0);
+
+    init_tasks();
+    os_start();
+
+    /* os start should never return. If it does, this should be an error */
+    assert(0);
+
+    return rc;
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a049361e/apps/mmctest/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/mmctest/syscfg.yml b/apps/mmctest/syscfg.yml
new file mode 100644
index 0000000..40c4c52
--- /dev/null
+++ b/apps/mmctest/syscfg.yml
@@ -0,0 +1,22 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# Package: apps/mmctest
+
+syscfg.vals:
+    SHELL_TASK: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a049361e/hw/drivers/mmc/include/mmc/mmc.h
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/include/mmc/mmc.h b/hw/drivers/mmc/include/mmc/mmc.h
new file mode 100644
index 0000000..1a03edb
--- /dev/null
+++ b/hw/drivers/mmc/include/mmc/mmc.h
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __MMC_H__
+#define __MMC_H__
+
+#include <os/os_dev.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * MMC driver errors.
+ */
+#define MMC_OK              (0)
+#define MMC_CARD_ERROR      (1)  /* Is there a card installed? */
+#define MMC_TIMEOUT         (2)
+
+/**
+ * Initialize the MMC driver
+ *
+ * @param spi_num Number of the SPI channel to be used by MMC
+ * @param spi_cfg Low-level device specific SPI configuration
+ * @param ss_pin Number of SS pin if SW controlled, -1 otherwise
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+mmc_init(int spi_num, void *spi_cfg, int ss_pin);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ADC_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a049361e/hw/drivers/mmc/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/pkg.yml b/hw/drivers/mmc/pkg.yml
new file mode 100644
index 0000000..ee98870
--- /dev/null
+++ b/hw/drivers/mmc/pkg.yml
@@ -0,0 +1,27 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/drivers/mmc
+pkg.description: MMC interfaces
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - hw/hal

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a049361e/hw/drivers/mmc/src/mmc.c
----------------------------------------------------------------------
diff --git a/hw/drivers/mmc/src/mmc.c b/hw/drivers/mmc/src/mmc.c
new file mode 100644
index 0000000..6aefce4
--- /dev/null
+++ b/hw/drivers/mmc/src/mmc.c
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <hal/hal_spi.h>
+#include <hal/hal_gpio.h>
+
+#include <mmc/mmc.h>
+
+#include <stdio.h>
+
+/* Current used MMC commands */
+#define CMD0                (0)            /* GO_IDLE_STATE */
+#define CMD1                (1)            /* SEND_OP_COND (MMC) */
+#define CMD8                (8)            /* SEND_IF_COND */
+#define CMD16               (16)           /* SET_BLOCKLEN */
+#define CMD17               (17)           /* READ_SINGLE_BLOCK */
+#define CMD18               (18)           /* READ_MULTIPLE_BLOCK */
+#define CMD24               (24)           /* WRITE_BLOCK */
+#define CMD25               (25)           /* WRITE_MULTIPLE_BLOCK */
+#define CMD55               (55)           /* APP_CMD */
+#define CMD58               (58)           /* READ_OCR */
+#define ACMD41              (0x80 + 41)    /* SEND_OP_COND (SDC) */
+
+/* Response types */
+#define R1                  (0)
+#define R1b                 (1)
+#define R2                  (2)
+#define R3                  (3)
+#define R7                  (4)
+
+/* Response status */
+#define R_IDLE              (0x01)
+#define R_ERASE_RESET       (0x02)
+#define R_ILLEGAL_COMMAND   (0x04)
+#define R_CRC_ERROR         (0x08)
+#define R_ERASE_ERROR       (0x10)
+#define R_ADDR_ERROR        (0x20)
+#define R_PARAM_ERROR       (0x40)
+
+static struct hal_spi_settings mmc_settings = {
+    .data_order = HAL_SPI_MSB_FIRST,
+    .data_mode  = HAL_SPI_MODE0,
+    /* XXX: MMC initialization accepts clocks in the range 100-400KHz */
+    .baudrate   = 100,
+    .word_size  = HAL_SPI_WORD_SIZE_8BIT,
+};
+
+static int g_spi_num = -1;
+static int g_ss_pin = -1;
+
+/**
+ * TODO: switch to high-speed SPI after initialization.
+ */
+
+static int
+mmc_error_by_status(uint8_t status)
+{
+    return MMC_OK;
+}
+
+static int8_t
+response_type_by_cmd(uint8_t cmd)
+{
+    switch (cmd) {
+        case CMD8   : return R7;
+        case CMD58  : return R3;
+    }
+    return R1;
+}
+
+static uint32_t
+ocr_from_r3(uint8_t *response)
+{
+    uint32_t ocr;
+
+    ocr  = (uint32_t) response[4];
+    ocr |= (uint32_t) response[3] <<  8;
+    ocr |= (uint32_t) response[2] << 16;
+    ocr |= (uint32_t) response[1] << 24;
+
+    return ocr;
+}
+
+static uint8_t
+status_from_r1(uint8_t *response)
+{
+    return response[0];
+}
+
+static uint8_t
+voltage_from_r7(uint8_t *response)
+{
+    return response[3] & 0xF;
+}
+
+static uint8_t
+send_mmc_cmd(uint8_t cmd, uint32_t payload)
+{
+    int n;
+    uint8_t response[5];
+    uint8_t status;
+    uint8_t type;
+
+    if (cmd & 0x80) {
+        /* TODO: refactor recursion? */
+        send_mmc_cmd(CMD55, 0);
+    }
+
+    hal_gpio_write(g_ss_pin, 0);
+
+    /* 4.7.2: Command Format */
+    hal_spi_tx_val(g_spi_num, 0x40 | (cmd & ~0x80));
+
+    hal_spi_tx_val(g_spi_num, payload >> 24 & 0xff);
+    hal_spi_tx_val(g_spi_num, payload >> 16 & 0xff);
+    hal_spi_tx_val(g_spi_num, payload >>  8 & 0xff);
+    hal_spi_tx_val(g_spi_num, payload       & 0xff);
+
+    /**
+     * 7.2.2 Bus Transfer Protection
+     *   NOTE: SD is in CRC off mode by default but CMD0 and CMD8 always
+     *   require a valid CRC.
+     *   NOTE: CRC can be turned on with CMD59 (CRC_ON_OFF).
+     */
+    if (cmd == CMD0) {
+        hal_spi_tx_val(g_spi_num, 0x95);
+    } else if (cmd == CMD8) {
+        hal_spi_tx_val(g_spi_num, 0x87);
+    } else {
+        /* Any value is OK */
+        hal_spi_tx_val(g_spi_num, 0x01);
+    }
+
+    printf("=======================\n");
+    printf("sending cmd %d\n", cmd & ~0x80);
+
+    status = 0xff;
+    n = 10;
+    do {
+        response[0] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
+    } while ((response[0] & 0x80) && --n);
+
+    if (n) {
+        type = response_type_by_cmd(cmd);
+        status = status_from_r1(response);
+        printf("status=0x%x\n", status);
+        if (!(type == R1 || status & R_ILLEGAL_COMMAND || status & R_CRC_ERROR)) {
+            /* Read remaining data for this command */
+            for (n = 0; n < 4; n++) {
+                response[n+1] = (uint8_t) hal_spi_tx_val(g_spi_num, 0xff);
+            }
+
+            printf("response=[%0x][%0x][%0x][%0x]\n",
+                    response[1], response[2], response[3], response[4]);
+
+            switch (type) {
+                case R3:
+                    /* NOTE: ocr is defined in section 5.1 */
+                    printf("ocr=0x%08lx\n", ocr_from_r3(response));
+                    break;
+                case R7:
+                    printf("voltage=0x%x\n", voltage_from_r7(response));
+                    break;
+            }
+        }
+    }
+
+    printf("\n");
+
+    hal_gpio_write(g_ss_pin, 1);
+
+    return status;
+}
+
+/**
+ * NOTE:
+ */
+
+/**
+ * Initialize the MMC driver
+ *
+ * @param spi_num Number of the SPI channel to be used by MMC
+ * @param spi_cfg Low-level device specific SPI configuration
+ * @param ss_pin Number of SS pin if SW controlled, -1 otherwise
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+mmc_init(int spi_num, void *spi_cfg, int ss_pin)
+{
+    int rc;
+    int i;
+    uint8_t status;
+    uint32_t ocr;
+
+    g_ss_pin = ss_pin;
+    g_spi_num = spi_num;
+
+    if (g_ss_pin != -1) {
+        hal_gpio_init_out(g_ss_pin, 1);
+    }
+
+    rc = hal_spi_init(g_spi_num, spi_cfg, HAL_SPI_TYPE_MASTER);
+    if (rc) {
+        return (rc);
+    }
+
+    rc = hal_spi_config(g_spi_num, &mmc_settings);
+    if (rc) {
+        return (rc);
+    }
+
+    hal_spi_set_txrx_cb(0, NULL, NULL);
+    hal_spi_enable(0);
+
+    /**
+     * NOTE: The state machine below follows:
+     *       Section 6.4.1: Power Up Sequence for SD Bus Interface.
+     *       Section 7.2.1: Mode Selection and Initialization.
+     */
+
+    /* give 10ms for VDD rampup */
+    os_time_delay(OS_TICKS_PER_SEC / 100);
+
+    /* send the required >= 74 clock cycles */
+    hal_gpio_write(g_ss_pin, 0);
+
+    for (i = 0; i < 74; i++) {
+        hal_spi_tx_val(0, 0xff);
+    }
+
+    hal_gpio_write(g_ss_pin, 1);
+
+    /* put card in idle state */
+    status = send_mmc_cmd(CMD0, 0);
+
+    if (status != R_IDLE) {
+        /* No card inserted or bad card! */
+        return mmc_error_by_status(status);
+    }
+
+    /**
+     * 4.3.13: Ask for 2.7-3.3V range, send 0xAA pattern
+     *
+     * NOTE: cards that are not compliant with "Physical Spec Version 2.00"
+     *       will answer this with R_ILLEGAL_COMMAND.
+     */
+    status = send_mmc_cmd(CMD8, 0x1AA);
+    if (status & R_ILLEGAL_COMMAND) {
+        /* Ver1.x SD Memory Card or Not SD Memory Card */
+
+        ocr = send_mmc_cmd(CMD58, 0);
+
+        /* TODO: check if voltage range is ok! */
+
+        if (ocr & R_ILLEGAL_COMMAND) {
+
+        }
+    } else {
+        /* Ver2.00 or later SD Memory Card */
+
+        /* TODO:
+         * 1) check CMD8 response pattern and voltage range.
+         * 2) send ACMD41 while in R_IDLE up to 1s.
+         * 3) send CMD58, check CCS in response.
+         */
+
+        status = send_mmc_cmd(ACMD41, 0);
+
+        /* CCS = OCR[30] */
+        status = send_mmc_cmd(CMD58, 0);
+    }
+
+    return rc;
+}
+
+/**
+ * @return 0 on success, non-zero on failure
+ */
+int
+mmc_read(void *buf, size_t len)
+{
+    hal_gpio_write(g_ss_pin, 0);
+    hal_gpio_write(g_ss_pin, 1);
+    return 0;
+}
+
+/**
+ * @return 0 on success, non-zero on failure
+ */
+int
+mmc_write(void *buf, size_t len)
+{
+    return 0;
+}


[42/50] incubator-mynewt-core git commit: MYNEWT-139: newtmgr cli to execute device tests

Posted by st...@apache.org.
MYNEWT-139: newtmgr cli to execute device tests

Handle "run test" "run list" commands from newtmgr.
"run list" lists all the registered test suites on device
"run test" takes a specific test or defaults to running all tests.
Optional "token" argument allows log messages to have the specified token
added. Use "run test all token" to run all tests by default.


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

Branch: refs/heads/sensors_branch
Commit: 95c57d6787bfe8be4ff35a646ca0f32065dc4b0b
Parents: e696b98
Author: Peter Snyder <pe...@apache.org>
Authored: Thu Dec 15 13:28:26 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Dec 18 13:56:17 2016 -0800

----------------------------------------------------------------------
 fs/fcb/test/src/fcb_test.c                      |   2 +-
 mgmt/mgmt/include/mgmt/mgmt.h                   |   2 +-
 test/runtest/include/runtest/runtest.h          |  19 +-
 test/runtest/src/ctags.list                     |   1 +
 test/runtest/src/runtest.c                      |  18 +-
 test/runtest/src/runtest_cli.c                  |   4 +-
 test/runtest/src/runtest_nmgr.c                 | 152 +++++++++++----
 test/testreport/include/testreport/testreport.h |  42 ----
 test/testreport/pkg.yml                         |  30 ---
 test/testreport/src/arch/cortex_m4/io.c         |  91 ---------
 test/testreport/src/arch/sim/io.c               | 140 --------------
 test/testreport/src/results.c                   | 190 -------------------
 test/testreport/src/testreport.c                | 131 -------------
 test/testreport/src/testreport_priv.h           |  56 ------
 test/testutil/include/testutil/testutil.h       |  13 +-
 test/testutil/src/suite.c                       |  12 +-
 16 files changed, 167 insertions(+), 736 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/fs/fcb/test/src/fcb_test.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/fcb_test.c b/fs/fcb/test/src/fcb_test.c
index 2cd6a0e..77d2d3e 100644
--- a/fs/fcb/test/src/fcb_test.c
+++ b/fs/fcb/test/src/fcb_test.c
@@ -58,7 +58,6 @@ struct flash_area test_fcb_area[] = {
         .fa_size = 0x4000
     }
 };
-#endif
 
 void
 fcb_test_wipe(void)
@@ -73,6 +72,7 @@ fcb_test_wipe(void)
         TEST_ASSERT(rc == 0);
     }
 }
+#endif
 
 int
 fcb_test_empty_walk_cb(struct fcb_entry *loc, void *arg)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/mgmt/mgmt/include/mgmt/mgmt.h
----------------------------------------------------------------------
diff --git a/mgmt/mgmt/include/mgmt/mgmt.h b/mgmt/mgmt/include/mgmt/mgmt.h
index 9856405..a6a1c99 100644
--- a/mgmt/mgmt/include/mgmt/mgmt.h
+++ b/mgmt/mgmt/include/mgmt/mgmt.h
@@ -47,7 +47,7 @@ extern "C" {
 #define MGMT_GROUP_ID_LOGS      (4)
 #define MGMT_GROUP_ID_CRASH     (5)
 #define MGMT_GROUP_ID_SPLIT     (6)
-#define MGMT_GROUP_ID_RUNTEST   (7)
+#define MGMT_GROUP_ID_RUN   (7)
 #define MGMT_GROUP_ID_PERUSER   (64)
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/runtest/include/runtest/runtest.h
----------------------------------------------------------------------
diff --git a/test/runtest/include/runtest/runtest.h b/test/runtest/include/runtest/runtest.h
index 59c7ef0..12f3e52 100644
--- a/test/runtest/include/runtest/runtest.h
+++ b/test/runtest/include/runtest/runtest.h
@@ -29,9 +29,24 @@ extern "C" {
 void runtest_init(void);
 
 /*
- * XXX global used to gate starting test - hack
+ * Callback for runtest events - newtmgr uses this to add
+ * run test requests to default queue for test application (e.g., mynewtsanity)
  */
-extern volatile int runtest_start;
+void run_evcb_set(os_event_fn *cb);
+
+/*
+ * Token is append to log messages - for use by ci gateway
+ */
+#define RUNTEST_REQ_SIZE  32
+extern char runtest_test_token[RUNTEST_REQ_SIZE];
+
+/*
+ * Argument struct passed in from "run" requests via newtmgr
+ */
+struct runtest_evq_arg {
+    char* run_testname;
+    char* run_token;
+};
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/runtest/src/ctags.list
----------------------------------------------------------------------
diff --git a/test/runtest/src/ctags.list b/test/runtest/src/ctags.list
new file mode 120000
index 0000000..ff9c2c0
--- /dev/null
+++ b/test/runtest/src/ctags.list
@@ -0,0 +1 @@
+/Users/peterfs/dev/newt/newt_ci/ctags.list
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/runtest/src/runtest.c
----------------------------------------------------------------------
diff --git a/test/runtest/src/runtest.c b/test/runtest/src/runtest.c
index 28657d0..3a68185 100644
--- a/test/runtest/src/runtest.c
+++ b/test/runtest/src/runtest.c
@@ -39,27 +39,25 @@ struct shell_cmd runtest_cmd_struct;
 struct mgmt_group runtest_nmgr_group;
 #endif
 
-int
-runtest()
-{
-    /* XXX */
-
-    return 0;
-}
+extern int run_nmgr_register_group();
 
+/*
+ * Package init routine to register newtmgr "run" commands
+ */
 void
 runtest_init(void)
 {
+    int rc;
+
     /* Ensure this function only gets called by sysinit. */
     SYSINIT_ASSERT_ACTIVE();
 
-    runtest_start = 1;
-
 #if MYNEWT_VAL(RUNTEST_CLI)
     shell_cmd_register(&runtest_cmd_struct);
 #endif
 
 #if MYNEWT_VAL(RUNTEST_NEWTMGR)
-    mgmt_group_register(&runtest_nmgr_group);
+    rc = run_nmgr_register_group();
+    SYSINIT_PANIC_ASSERT(rc == 0);
 #endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/runtest/src/runtest_cli.c
----------------------------------------------------------------------
diff --git a/test/runtest/src/runtest_cli.c b/test/runtest/src/runtest_cli.c
index 282663c..739dc2f 100644
--- a/test/runtest/src/runtest_cli.c
+++ b/test/runtest/src/runtest_cli.c
@@ -32,14 +32,14 @@
 
 static int runtest_cli_cmd(int argc, char **argv);
 struct shell_cmd runtest_cmd_struct = {
-    .sc_cmd = "runtest",
+    .sc_cmd = "run",
     .sc_cmd_func = runtest_cli_cmd
 };
 
 static int
 runtest_cli_cmd(int argc, char **argv)
 {
-    console_printf("Usage runtest \n");
+    console_printf("Usage run [list | test testname token] \n");
     return 0;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/runtest/src/runtest_nmgr.c
----------------------------------------------------------------------
diff --git a/test/runtest/src/runtest_nmgr.c b/test/runtest/src/runtest_nmgr.c
index 625af75..7281e99 100644
--- a/test/runtest/src/runtest_nmgr.c
+++ b/test/runtest/src/runtest_nmgr.c
@@ -25,63 +25,151 @@
 #include "mgmt/mgmt.h"
 #include "cborattr/cborattr.h"
 #include "console/console.h"
+#include "os/os_eventq.h"
+
+#include "testutil/testutil.h"
 
 #include "runtest/runtest.h"
 #include "runtest_priv.h"
 
-static int runtest_nmgr_write(struct mgmt_cbuf *);
+static int run_nmgr_test(struct mgmt_cbuf *);
+static int run_nmgr_list(struct mgmt_cbuf *);
 
-static const struct mgmt_handler runtest_nmgr_handler[] = {
-    [0] = { runtest_nmgr_write, runtest_nmgr_write }
-};
+struct mgmt_group run_nmgr_group;
+
+#define RUN_NMGR_OP_TEST    0
+#define RUN_NMGR_OP_LIST    1
 
-struct mgmt_group runtest_nmgr_group = {
-    .mg_handlers = (struct mgmt_handler *)runtest_nmgr_handler,
-    .mg_handlers_count = 1,
-    .mg_group_id = MGMT_GROUP_ID_RUNTEST
+const struct mgmt_handler run_nmgr_handlers[] = {
+    [RUN_NMGR_OP_TEST] = {NULL, run_nmgr_test},
+    [RUN_NMGR_OP_LIST] = {run_nmgr_list, NULL}
 };
 
+extern void sanity_start_test();
+struct os_event run_test_event;
+
+char run_testname[RUNTEST_REQ_SIZE];
+char run_token[RUNTEST_REQ_SIZE];
+
+static struct os_eventq *run_evq;
+struct runtest_evq_arg runtest_arg;
+os_event_fn *run_callback;
+
+void
+run_evcb_set(os_event_fn *cb)
+{
+    run_callback = cb;
+}
+
+static struct os_eventq *
+run_evq_get(void)
+{
+    os_eventq_ensure(&run_evq, NULL);
+    return run_evq;
+}
+
 /*
- * XXX global used to gate starting test - hack
+ * package "run test" request from newtmgr and enqueue on default queue
+ * of the application which is actually running the tests (e.g., mynewtsanity).
+ * Application callback was initialized by call to run_evb_set() above.
  */
-volatile int runtest_start;
-
 static int
-runtest_nmgr_write(struct mgmt_cbuf *cb)
+run_nmgr_test(struct mgmt_cbuf *cb)
 {
-    char tmp_str[64];
-    const struct cbor_attr_t attr[2] = {
+    int rc;
+
+    const struct cbor_attr_t attr[3] = {
         [0] = {
-            .attribute = "u",
+            .attribute = "testname",
             .type = CborAttrTextStringType,
-            .addr.string = tmp_str,
-            .len = sizeof(tmp_str)
+            .addr.string = run_testname,
+            .len = sizeof(run_testname)
         },
         [1] = {
+            .attribute = "token",
+            .type = CborAttrTextStringType,
+            .addr.string = run_token,
+            .len = sizeof(run_token)
+        },
+        [2] = {
             .attribute = NULL
         }
     };
-    int rc;
 
     rc = cbor_read_object(&cb->it, attr);
     if (rc) {
+        return rc;
+    }
+
+    /*
+     * testname is either a specific test or newtmgr passed "all".
+     * token is appened to log messages.
+     */
+    runtest_arg.run_testname = run_testname;
+    runtest_arg.run_token = run_token;
+
+    run_test_event.ev_arg = &runtest_arg;
+    run_test_event.ev_cb = run_callback;
+
+    os_eventq_put(run_evq_get(), &run_test_event);
+            
+    if (rc) {
         rc = MGMT_ERR_EINVAL;
-    } else {
-        /* XXX ugh - drop out of a loop allowing a test to run in an app */
-        runtest_start = 0;
-        /*
-         * we should be doing something more like this where we call
-         * a named test suite directly
-         */
-#ifdef NOTYET
-        rc = start_test(tmp_str);
-#endif
-        if (rc) {
-            rc = MGMT_ERR_EINVAL;
-        }
     }
+
     mgmt_cbuf_setoerr(cb, rc);
     return 0;
 }
 
-#endif /* MYNEWT_VAL(RUNTEST_NEWTMGR) */
+/*
+ * List all register tests
+ */
+static int
+run_nmgr_list(struct mgmt_cbuf *cb)
+{
+    CborError g_err = CborNoError;
+    CborEncoder *penc = &cb->encoder;
+    CborEncoder rsp, run_list;
+    struct ts_suite *ts;
+
+    g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength);
+    g_err |= cbor_encode_text_stringz(&rsp, "rc");
+    g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK);
+
+    g_err |= cbor_encode_text_stringz(&rsp, "run_list");
+    g_err |= cbor_encoder_create_array(&rsp, &run_list, CborIndefiniteLength);
+
+    SLIST_FOREACH(ts, &g_ts_suites, ts_next) {
+        g_err |= cbor_encode_text_stringz(&run_list, ts->ts_name);
+    }
+
+    g_err |= cbor_encoder_close_container(&rsp, &run_list);
+    g_err |= cbor_encoder_close_container(penc, &rsp);
+
+    if (g_err) {
+        return MGMT_ERR_ENOMEM;
+    }
+    return (0);
+}
+
+/*
+ * Register nmgr group handlers
+ */
+int
+run_nmgr_register_group(void)
+{
+    int rc;
+
+    MGMT_GROUP_SET_HANDLERS(&run_nmgr_group, run_nmgr_handlers);
+    run_nmgr_group.mg_group_id = MGMT_GROUP_ID_RUN;
+
+    rc = mgmt_group_register(&run_nmgr_group);
+    if (rc != 0) {
+        goto err;
+    }
+    return (0);
+
+err:
+    return (rc);
+}
+#endif /* MYNEWT_VAL(RUN_NEWTMGR) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testreport/include/testreport/testreport.h
----------------------------------------------------------------------
diff --git a/test/testreport/include/testreport/testreport.h b/test/testreport/include/testreport/testreport.h
deleted file mode 100644
index 3b14da8..0000000
--- a/test/testreport/include/testreport/testreport.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_TESTREPORT_
-#define H_TESTREPORT_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-struct nffs_area_desc;
-
-struct tr_config {
-    const char *tc_base_path;
-    const struct nffs_area_desc *tc_area_descs;
-};
-
-extern struct tr_config tr_config;
-
-int tr_init(void);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testreport/pkg.yml
----------------------------------------------------------------------
diff --git a/test/testreport/pkg.yml b/test/testreport/pkg.yml
deleted file mode 100644
index 42e8482..0000000
--- a/test/testreport/pkg.yml
+++ /dev/null
@@ -1,30 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-# 
-#  http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-
-pkg.name: test/testreport
-pkg.description: Library for recording unit test results to flash.
-pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-    - unit
-    - test
-
-pkg.deps:
-    - fs/nffs
-    - test/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testreport/src/arch/cortex_m4/io.c
----------------------------------------------------------------------
diff --git a/test/testreport/src/arch/cortex_m4/io.c b/test/testreport/src/arch/cortex_m4/io.c
deleted file mode 100644
index 0ae2aa0..0000000
--- a/test/testreport/src/arch/cortex_m4/io.c
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <stddef.h>
-#include "nffs/nffs.h"
-#include "nffs/nffsutil.h"
-#include "testreport_priv.h"
-
-int
-tr_io_write(const char *path, const void *contents, size_t len)
-{
-    int rc;
-
-    rc = nffsutil_write_file(path, contents, len);
-    if (rc != 0) {
-        return -1;
-    }
-
-    return 0;
-}
-
-int
-tr_io_mkdir(const char *path)
-{
-    int rc;
-
-    rc = nffs_mkdir(path);
-    if (rc != 0 && rc != NFFS_EEXIST) {
-        return -1;
-    }
-
-    return 0;
-}
-
-int
-tr_io_rmdir(const char *path)
-{
-    int rc;
-
-    rc = nffs_unlink(path);
-    if (rc != 0 && rc != NFFS_ENOENT) {
-        return -1;
-    }
-
-    return 0;
-}
-
-int
-tr_io_read(const char *path, void *out_data, size_t len, size_t *out_len)
-{
-    uint32_t u32;
-    int rc;
-
-    rc = nffsutil_read_file(path, 0, len, out_data, &u32);
-    if (rc != 0) {
-        return -1;
-    }
-
-    *out_len = u32;
-
-    return 0;
-}
-
-int
-tr_io_delete(const char *path)
-{
-    int rc;
-
-    rc = nffs_unlink(path);
-    if (rc != 0 && rc != NFFS_ENOENT) {
-        return -1;
-    }
-
-    return 0;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testreport/src/arch/sim/io.c
----------------------------------------------------------------------
diff --git a/test/testreport/src/arch/sim/io.c b/test/testreport/src/arch/sim/io.c
deleted file mode 100644
index f28a4dd..0000000
--- a/test/testreport/src/arch/sim/io.c
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include "testreport_priv.h"
-
-static char tr_io_buf[1024];
-
-int
-tr_io_write(const char *path, const void *contents, size_t len)
-{
-    FILE *fp;
-    int rc;
-
-    fp = NULL;
-
-    fp = fopen(path, "w+");
-    if (fp == NULL) {
-        rc = -1;
-        goto done;
-    }
-
-    if (contents != NULL && len > 0) {
-        rc = fwrite(contents, len, 1, fp);
-        if (rc != 1) {
-            rc = -1;
-            goto done;
-        }
-    }
-
-    rc = 0;
-
-done:
-    if (fp != NULL) {
-        fclose(fp);
-    }
-
-    return rc;
-}
-
-int
-tr_io_mkdir(const char *path)
-{
-    int rc;
-
-    rc = mkdir(path, 0755);
-    if (rc == -1 && errno != EEXIST) {
-        return -1;
-    }
-
-    return 0;
-}
-
-/* XXX security risk, not portable, blah blah blah */
-int
-tr_io_rmdir(const char *path)
-{
-    int rc; 
-
-    rc = snprintf(tr_io_buf, sizeof tr_io_buf,
-                  "rm -rf '%s'", path);
-    if (rc >= sizeof tr_io_buf) {
-        return -1;
-    }
-
-    rc = system(tr_io_buf);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-int
-tr_io_read(const char *path, void *out_data, size_t len, size_t *out_len)
-{
-    FILE *fp;
-    uint8_t *dst;
-    int rc;
-    int i;
-
-    fp = NULL;
-
-    fp = fopen(path, "rb");
-    if (fp == NULL) {
-        rc = -1;
-        goto done;
-    }
-
-    dst = out_data;
-    for (i = 0; i < len; i++) {
-        rc = getc(fp);
-        if (rc == EOF) {
-            rc = -1;
-            goto done;
-        }
-
-        dst[i] = rc;
-    }
-
-    *out_len = i;
-    rc = 0;
-
-done:
-    if (fp != NULL) {
-        fclose(fp);
-    }
-
-    return rc;
-}
-
-int
-tr_io_delete(const char *path)
-{
-    int rc;
-
-    rc = remove(path);
-
-    return rc;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testreport/src/results.c
----------------------------------------------------------------------
diff --git a/test/testreport/src/results.c b/test/testreport/src/results.c
deleted file mode 100644
index bc54bb3..0000000
--- a/test/testreport/src/results.c
+++ /dev/null
@@ -1,190 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <stdio.h>
-#include "testutil/testutil.h"
-#include "testreport/testreport.h"
-#include "testreport_priv.h"
-
-#define TU_REPORT_META_DIR          ".meta"
-#define TU_REPORT_STATUS_FILENAME   "status"
-
-static char tr_report_buf[1024];
-
-int
-tr_report_rmdir_results(void)
-{
-    if (tr_config.tc_base_path == NULL) {
-        return 0;
-    }
-
-    return tr_io_rmdir(tr_config.tc_base_path);
-}
-
-int
-tr_report_mkdir_results(void)
-{
-    int rc;
-
-    if (tr_config.tc_base_path == NULL) {
-        return 0;
-    }
-
-    rc = snprintf(tr_report_buf, sizeof tr_report_buf,
-                  "%s", tr_config.tc_base_path);
-    if (rc >= sizeof tr_report_buf) {
-        return -1;
-    }
-    return tr_io_mkdir(tr_report_buf);
-}
-
-int
-tr_report_mkdir_meta(void)
-{
-    int rc;
-
-    if (tr_config.tc_base_path == NULL) {
-        return 0;
-    }
-
-    rc = snprintf(tr_report_buf, sizeof tr_report_buf,
-                  "%s/" TU_REPORT_META_DIR, tr_config.tc_base_path);
-    if (rc >= sizeof tr_report_buf) {
-        return -1;
-    }
-    return tr_io_mkdir(tr_report_buf);
-}
-
-int
-tr_report_mkdir_suite(void)
-{
-    int rc;
-
-    if (tr_config.tc_base_path == NULL) {
-        return 0;
-    }
-
-    rc = snprintf(tr_report_buf, sizeof tr_report_buf,
-                  "%s/%s", tr_config.tc_base_path,
-                  ts_current_config->ts_suite_name);
-    if (rc >= sizeof tr_report_buf) {
-        return -1;
-    }
-
-    rc = tr_io_mkdir(tr_report_buf);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-int
-tr_report_mkdir_case(void)
-{
-    int rc;
-
-    if (tr_config.tc_base_path == NULL) {
-        return 0;
-    }
-
-    rc = snprintf(tr_report_buf, sizeof tr_report_buf,
-                  "%s/%s/%s", tr_config.tc_base_path,
-                  ts_current_config->ts_suite_name, tu_case_name);
-    if (rc >= sizeof tr_report_buf) {
-        return -1;
-    }
-
-    rc = tr_io_mkdir(tr_report_buf);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-int
-tr_report_write_file(const char *filename, const uint8_t *data,
-                     size_t data_len)
-{
-    int rc;
-
-    if (tr_config.tc_base_path == NULL) {
-        return 0;
-    }
-
-    rc = snprintf(tr_report_buf, sizeof tr_report_buf,
-                  "%s/%s/%s/%s", tr_config.tc_base_path,
-                  ts_current_config->ts_suite_name, tu_case_name, filename);
-    if (rc >= sizeof tr_report_buf) {
-        return -1;
-    }
-
-    rc = tr_io_write(tr_report_buf, data, data_len);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-int
-tr_report_read_status(void)
-{
-    size_t bytes_read;
-    int rc;
-
-    rc = snprintf(tr_report_buf, sizeof tr_report_buf,
-                  "%s/%s/%s", tr_config.tc_base_path,
-                  TU_REPORT_META_DIR, TU_REPORT_STATUS_FILENAME);
-    if (rc >= sizeof tr_report_buf) {
-        return -1;
-    }
-
-    rc = tr_io_read(tr_report_buf, &tu_first_idx, sizeof tu_first_idx,
-                    &bytes_read);
-    if (rc != 0 || bytes_read != sizeof tu_first_idx) {
-        return -1;
-    }
-
-    tr_io_delete(tr_report_buf);
-
-    return 0;
-}
-
-int
-tr_report_write_status(void)
-{
-    int rc;
-
-    rc = snprintf(tr_report_buf, sizeof tr_report_buf,
-                  "%s/%s/%s", tr_config.tc_base_path,
-                  TU_REPORT_META_DIR, TU_REPORT_STATUS_FILENAME);
-    if (rc >= sizeof tr_report_buf) {
-        return -1;
-    }
-
-    rc = tr_io_write(tr_report_buf, &tu_first_idx, sizeof tu_first_idx);
-    if (rc != 0) {
-        return -1;
-    }
-
-    return 0;
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testreport/src/testreport.c
----------------------------------------------------------------------
diff --git a/test/testreport/src/testreport.c b/test/testreport/src/testreport.c
deleted file mode 100644
index 85c1258..0000000
--- a/test/testreport/src/testreport.c
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <assert.h>
-#include <stdio.h>
-#include "hal/hal_flash.h"
-#include "fs/fs.h"
-#include "nffs/nffs.h"
-#include "testutil/testutil.h"
-#include "testreport/testreport.h"
-#include "testreport_priv.h"
-
-struct tr_config tr_config;
-
-static int tr_case_fail_idx;
-
-static void
-tr_case_init(void *unused)
-{
-    int rc;
-
-    rc = tr_results_mkdir_case();
-    assert(rc == 0);
-}
-
-static void
-tr_case_fail(char *msg, int msg_len, void *unused)
-{
-    char filename[14];
-    int rc;
-
-    rc = snprintf(filename, sizeof filename, "fail-%04d.txt",
-                  tr_case_fail_idx);
-    assert(rc < sizeof filename);
-
-    rc = tr_results_write_file(filename, (uint8_t *)msg, msg_len);
-    assert(rc == 0);
-
-    tr_case_fail_idx++;
-}
-
-static void
-tr_case_pass(char *msg, int msg_len, void *unused)
-{
-    int rc;
-
-    rc = tr_results_write_file("pass.txt", (uint8_t *)msg, msg_len);
-    assert(rc == 0);
-}
-
-static void
-tr_suite_init(void *unused)
-{
-    int rc;
-
-    rc = tr_results_mkdir_suite();
-    assert(rc == 0);
-}
-
-static void
-tr_restart(void *unused)
-{
-    tr_results_write_status();
-}
-
-int
-tr_init(void)
-{
-    int rc;
-
-    if (tr_config.tc_base_path != NULL) {
-        if (tr_config.tc_area_descs != NULL) {
-            rc = hal_flash_init();
-            if (rc != 0) {
-                return -1;
-            }
-
-            rc = nffs_init();
-            if (rc != 0) {
-                return -1;
-            }
-
-            rc = nffs_detect(tr_config.tc_area_descs);
-            if (rc == FS_ECORRUPT) {
-                rc = nffs_format(tr_config.tc_area_descs);
-            }
-            if (rc != 0) {
-                return -1;
-            }
-        }
-
-        rc = tr_results_read_status();
-        if (rc != 0) {
-            tr_results_rmdir_results();
-        }
-
-        rc = tr_results_mkdir_results();
-        if (rc != 0) {
-            return -1;
-        }
-
-        rc = tr_results_mkdir_meta();
-        if (rc != 0) {
-            return -1;
-        }
-    }
-
-    tu_config.tc_case_init_cb = tr_case_init;
-    tu_config.tc_case_fail_cb = tr_case_fail;
-    tu_config.tc_case_pass_cb = tr_case_pass;
-    tu_config.tc_suite_init_cb = tr_suite_init;
-    tu_config.tc_restart_cb = tr_restart;
-
-    return 0;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testreport/src/testreport_priv.h
----------------------------------------------------------------------
diff --git a/test/testreport/src/testreport_priv.h b/test/testreport/src/testreport_priv.h
deleted file mode 100644
index 64f95a1..0000000
--- a/test/testreport/src/testreport_priv.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_TESTREPORT_PRIV_
-#define H_TESTREPORT_PRIV_
-
-#include <stddef.h>
-#include <inttypes.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int tr_results_mkdir_results(void);
-int tr_results_rmdir_results(void);
-
-int tr_results_mkdir_meta(void);
-
-int tr_results_mkdir_suite(void);
-int tr_results_mkdir_case(void);
-
-int tr_results_write_file(const char *filename, const uint8_t *data,
-                          size_t data_len);
-
-int tr_results_read_status(void);
-int tr_results_write_status(void);
-
-int tr_io_read(const char *path, void *out_data, size_t len, size_t *out_len);
-int tr_io_write(const char *path, const void *contents, size_t len);
-
-int tr_io_delete(const char *path);
-
-int tr_io_mkdir(const char *path);
-int tr_io_rmdir(const char *path);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testutil/include/testutil/testutil.h
----------------------------------------------------------------------
diff --git a/test/testutil/include/testutil/testutil.h b/test/testutil/include/testutil/testutil.h
index 66701f2..4749186 100644
--- a/test/testutil/include/testutil/testutil.h
+++ b/test/testutil/include/testutil/testutil.h
@@ -74,6 +74,8 @@ void tu_suite_pre_test(void);
 void tu_suite_post_test(void);
 void tu_suite_complete(void);
 
+int tu_suite_register(tu_testsuite_fn_t* ts, const char *name);
+
 struct ts_suite {
     SLIST_ENTRY(ts_suite) ts_next;
     const char *ts_name;
@@ -81,8 +83,7 @@ struct ts_suite {
 };
 
 SLIST_HEAD(ts_testsuite_list, ts_suite);
-
-extern struct ts_testsuite_list *ts_suites;
+extern struct ts_testsuite_list g_ts_suites;
 
 struct ts_config {
     int ts_print_results;
@@ -193,10 +194,10 @@ extern int tu_case_failed;
 extern int tu_case_idx;
 extern jmp_buf tu_case_jb;
 
-#define TEST_SUITE_NAME(suite_name) TEST_SUITE##suite_name
+#define TEST_SUITE_DECL(suite_name) extern void suite_name()
 
-#define TEST_SUITE_DECL(suite_name)                         \
-  extern tu_testsuite_fn_t *TEST_SUITE##suite_name()
+#define TEST_SUITE_REGISTER(suite_name)                      \
+  tu_suite_register((tu_testsuite_fn_t*)suite_name, ((const char *)#suite_name));
 
 #define TEST_SUITE(suite_name)                               \
 void                                                         \
@@ -266,7 +267,9 @@ TEST_SUITE_##suite_name(void);                               \
 #define REST_OR_0_AUX_N(first, ...) __VA_ARGS__
 
 #define XSTR(s) STR(s)
+#ifndef STR
 #define STR(s) #s
+#endif
 
 #if MYNEWT_VAL(TESTUTIL_SYSTEM_ASSERT)
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/95c57d67/test/testutil/src/suite.c
----------------------------------------------------------------------
diff --git a/test/testutil/src/suite.c b/test/testutil/src/suite.c
index 6cca44a..4ccff7c 100644
--- a/test/testutil/src/suite.c
+++ b/test/testutil/src/suite.c
@@ -25,11 +25,17 @@
 const char *tu_suite_name = 0;
 int tu_suite_failed = 0;
 
+struct ts_testsuite_list g_ts_suites;
+
+/*
+ * tu_suite_register must be called for each test_suite that's to
+ * be run from a list rather than explicitly called.
+ * See mynewtsanity.
+ */
 int
-tu_suite_register(const char *name, tu_testsuite_fn_t* ts)
+tu_suite_register(tu_testsuite_fn_t* ts, const char *name)
 {
     struct ts_suite *tsp;
-    TEST_SUITE_DECL(name);
 
     tsp = (struct ts_suite *)os_malloc(sizeof(*tsp));
     if (!tsp) {
@@ -37,7 +43,7 @@ tu_suite_register(const char *name, tu_testsuite_fn_t* ts)
     }
     tsp->ts_name = name;
     tsp->ts_test = ts;
-    SLIST_INSERT_HEAD(ts_suites, tsp, ts_next);
+    SLIST_INSERT_HEAD(&g_ts_suites, tsp, ts_next);
     return 0;
 }