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

[01/37] incubator-mynewt-core git commit: MYNEWT-683 SensorAPI: Add BNO055 driver [Forced Update!]

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/nrf_cputime 34d901901 -> 244227b77 (forced update)


MYNEWT-683 SensorAPI: Add BNO055 driver

- fix bno055_get_int_enable()
  was reading wrong enable values
- fix indentation


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

Branch: refs/heads/nrf_cputime
Commit: cebfd2e3ee78466f712e50a40e3f3860981af0d9
Parents: e7934fa
Author: Vipul Rahane <vi...@apache.org>
Authored: Thu Mar 23 17:22:50 2017 -0700
Committer: Vipul Rahane <vi...@apache.org>
Committed: Fri Mar 24 14:36:20 2017 -0700

----------------------------------------------------------------------
 .../sensors/bno055/include/bno055/bno055.h      |  26 ++--
 hw/drivers/sensors/bno055/src/bno055.c          | 120 ++++++++++---------
 2 files changed, 74 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cebfd2e3/hw/drivers/sensors/bno055/include/bno055/bno055.h
----------------------------------------------------------------------
diff --git a/hw/drivers/sensors/bno055/include/bno055/bno055.h b/hw/drivers/sensors/bno055/include/bno055/bno055.h
index f04c2ae..c8bd5c6 100644
--- a/hw/drivers/sensors/bno055/include/bno055/bno055.h
+++ b/hw/drivers/sensors/bno055/include/bno055/bno055.h
@@ -104,14 +104,14 @@ extern "C" {
 #define BNO055_GYR_CFG_OPR_MODE_ADV_PWR_SAVE              (0x4 << 5)
 
 /* Magnetometer config */
-#define BNO055_MAG_CFG_ODR_2HZ                                    0x0
-#define BNO055_MAG_CFG_ODR_6HZ                                    0x1
-#define BNO055_MAG_CFG_ODR_8HZ                                    0x2
-#define BNO055_MAG_CFG_ODR_10HZ                                   0x3
-#define BNO055_MAG_CFG_ODR_15HZ                                   0x4
-#define BNO055_MAG_CFG_ODR_20HZ                                   0x5
-#define BNO055_MAG_CFG_ODR_25HZ                                   0x6
-#define BNO055_MAG_CFG_ODR_30HZ                                   0x7
+#define BNO055_MAG_CFG_ODR_2HZ                                   0x0
+#define BNO055_MAG_CFG_ODR_6HZ                                   0x1
+#define BNO055_MAG_CFG_ODR_8HZ                                   0x2
+#define BNO055_MAG_CFG_ODR_10HZ                                  0x3
+#define BNO055_MAG_CFG_ODR_15HZ                                  0x4
+#define BNO055_MAG_CFG_ODR_20HZ                                  0x5
+#define BNO055_MAG_CFG_ODR_25HZ                                  0x6
+#define BNO055_MAG_CFG_ODR_30HZ                                  0x7
 
 #define BNO055_MAG_CFG_OPR_MODE_LOWPWR                    (0x0 << 3)
 #define BNO055_MAG_CFG_OPR_MODE_REG                       (0x1 << 3)
@@ -168,11 +168,11 @@ struct bno055 {
 };
 
 struct bno055_rev_info {
-     uint8_t bri_accel_rev;
-     uint8_t bri_mag_rev;
-     uint8_t bri_gyro_rev;
-     uint8_t bri_bl_rev;
-     uint16_t bri_sw_rev;
+    uint8_t bri_accel_rev;
+    uint8_t bri_mag_rev;
+    uint8_t bri_gyro_rev;
+    uint8_t bri_bl_rev;
+    uint16_t bri_sw_rev;
 };
 
 struct bno055_calib_info {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cebfd2e3/hw/drivers/sensors/bno055/src/bno055.c
----------------------------------------------------------------------
diff --git a/hw/drivers/sensors/bno055/src/bno055.c b/hw/drivers/sensors/bno055/src/bno055.c
index 5f2993c..0e34987 100644
--- a/hw/drivers/sensors/bno055/src/bno055.c
+++ b/hw/drivers/sensors/bno055/src/bno055.c
@@ -1625,35 +1625,35 @@ bno055_get_int_duration(uint32_t intr, uint8_t *duration)
 
     mask = val = shift = 0;
     switch(intr) {
-      case BNO055_INT_GYR_HR_X_AXIS:
-          reg = BNO055_GYRO_DURN_X_ADDR;
-          break;
-      case BNO055_INT_GYR_HR_Y_AXIS:
-          reg = BNO055_GYRO_DURN_Y_ADDR;
-          break;
-      case BNO055_INT_GYR_HR_Z_AXIS:
-          reg = BNO055_GYRO_DURN_Z_ADDR;
-          break;
-      case BNO055_INT_ACC_HG:
-          reg = BNO055_ACCEL_HIGH_G_DURN_ADDR;
-          break;
-      case BNO055_INT_ACC_NM:
-          reg = BNO055_ACCEL_NO_MOTION_SET_ADDR;
-          mask = 0x3F;
-          shift = 1;
-          break;
-      case BNO055_INT_ACC_AM:
-          reg = BNO055_ACCEL_INTR_SETTINGS_ADDR;
-          mask = 0x3;
-          break;
-      case BNO055_INT_GYR_AM:
-          reg = BNO055_GYRO_INTR_SETTINGS_ADDR;
-          mask = 0xc;
-          shift = 2;
-          break;
-      default:
-          rc = SYS_EINVAL;
-          goto err;
+        case BNO055_INT_GYR_HR_X_AXIS:
+            reg = BNO055_GYRO_DURN_X_ADDR;
+            break;
+        case BNO055_INT_GYR_HR_Y_AXIS:
+            reg = BNO055_GYRO_DURN_Y_ADDR;
+            break;
+        case BNO055_INT_GYR_HR_Z_AXIS:
+            reg = BNO055_GYRO_DURN_Z_ADDR;
+            break;
+        case BNO055_INT_ACC_HG:
+            reg = BNO055_ACCEL_HIGH_G_DURN_ADDR;
+            break;
+        case BNO055_INT_ACC_NM:
+            reg = BNO055_ACCEL_NO_MOTION_SET_ADDR;
+            mask = 0x3F;
+            shift = 1;
+            break;
+        case BNO055_INT_ACC_AM:
+            reg = BNO055_ACCEL_INTR_SETTINGS_ADDR;
+            mask = 0x3;
+            break;
+        case BNO055_INT_GYR_AM:
+            reg = BNO055_GYRO_INTR_SETTINGS_ADDR;
+            mask = 0xc;
+            shift = 2;
+            break;
+        default:
+            rc = SYS_EINVAL;
+            goto err;
     }
 
     rc = bno055_read8(reg, &val);
@@ -1689,35 +1689,35 @@ bno055_set_int_duration(uint32_t intr, uint8_t duration)
 
     val = mask = shift = 0;
     switch(intr) {
-      case BNO055_INT_GYR_HR_X_AXIS:
-          reg = BNO055_GYRO_DURN_X_ADDR;
-          break;
-      case BNO055_INT_GYR_HR_Y_AXIS:
-          reg = BNO055_GYRO_DURN_Y_ADDR;
-          break;
-      case BNO055_INT_GYR_HR_Z_AXIS:
-          reg = BNO055_GYRO_DURN_Z_ADDR;
-          break;
-      case BNO055_INT_ACC_HG:
-          reg = BNO055_ACCEL_HIGH_G_DURN_ADDR;
-          break;
-      case BNO055_INT_ACC_NM:
-          reg = BNO055_ACCEL_NO_MOTION_SET_ADDR;
-          mask = 0x3F;
-          shift = 1;
-          break;
-      case BNO055_INT_ACC_AM:
-          reg = BNO055_ACCEL_INTR_SETTINGS_ADDR;
-          mask = 0x3;
-          break;
-      case BNO055_INT_GYR_AM:
-          reg = BNO055_GYRO_INTR_SETTINGS_ADDR;
-          mask = 0x3;
-          shift = 2;
-          break;
-      default:
-          rc = SYS_EINVAL;
-          goto err;
+        case BNO055_INT_GYR_HR_X_AXIS:
+            reg = BNO055_GYRO_DURN_X_ADDR;
+            break;
+        case BNO055_INT_GYR_HR_Y_AXIS:
+            reg = BNO055_GYRO_DURN_Y_ADDR;
+            break;
+        case BNO055_INT_GYR_HR_Z_AXIS:
+            reg = BNO055_GYRO_DURN_Z_ADDR;
+            break;
+        case BNO055_INT_ACC_HG:
+            reg = BNO055_ACCEL_HIGH_G_DURN_ADDR;
+            break;
+        case BNO055_INT_ACC_NM:
+            reg = BNO055_ACCEL_NO_MOTION_SET_ADDR;
+            mask = 0x3F;
+            shift = 1;
+            break;
+        case BNO055_INT_ACC_AM:
+            reg = BNO055_ACCEL_INTR_SETTINGS_ADDR;
+            mask = 0x3;
+            break;
+        case BNO055_INT_GYR_AM:
+            reg = BNO055_GYRO_INTR_SETTINGS_ADDR;
+            mask = 0x3;
+            shift = 2;
+            break;
+        default:
+            rc = SYS_EINVAL;
+            goto err;
     }
 
     if (mask && duration > mask) {
@@ -1877,7 +1877,7 @@ bno055_get_int_enable(uint8_t *intr)
         goto err;
     }
 
-    mask |= (val & BNO055_INT_EN_ACC_AM ? BNO055_INT_ACC_AM : 0);
+    mask = (val & BNO055_INT_EN_ACC_AM ? BNO055_INT_ACC_AM : 0);
     mask |= (val & BNO055_INT_EN_ACC_HG ? BNO055_INT_ACC_HG : 0);
     mask |= (val & BNO055_INT_EN_GYR_HR ? BNO055_INT_GYR_HR : 0);
     mask |= (val & BNO055_INT_EN_GYR_AM ? BNO055_INT_GYR_AM : 0);
@@ -1892,6 +1892,8 @@ bno055_get_int_enable(uint8_t *intr)
         mask |= (val & BNO055_ACCEL_SMNM ? BNO055_INT_ACC_SM : BNO055_INT_ACC_NM);
     }
 
+    *intr = mask;
+
     return 0;
 err:
     return rc;


[37/37] incubator-mynewt-core git commit: The number of concurrent connections should be default to 1.

Posted by we...@apache.org.
The number of concurrent connections should be default 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/244227b7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/244227b7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/244227b7

Branch: refs/heads/nrf_cputime
Commit: 244227b77eddce0a2a09034b436e5fafcf2815fb
Parents: 3c78595
Author: William San Filippo <wi...@runtime.io>
Authored: Thu Mar 30 13:59:58 2017 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Mar 30 14:10:22 2017 -0700

----------------------------------------------------------------------
 apps/bletest/src/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/244227b7/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index efb3fd4..e2519eb 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -172,7 +172,7 @@ bletest_multi_adv_instances[BLETEST_CFG_ADV_TEST_INSTANCES] = {
 #define BLETEST_CFG_MAX_CE_LEN          (BLETEST_CFG_CONN_ITVL)
 #define BLETEST_CFG_CONN_PEER_ADDR_TYPE (BLE_HCI_CONN_PEER_ADDR_PUBLIC)
 #define BLETEST_CFG_CONN_OWN_ADDR_TYPE  (BLE_HCI_ADV_OWN_ADDR_PUBLIC)
-#define BLETEST_CFG_CONCURRENT_CONNS    (8)
+#define BLETEST_CFG_CONCURRENT_CONNS    (1)
 
 /* Test packet config */
 #define BLETEST_CFG_RAND_PKT_SIZE       (1)


[25/37] incubator-mynewt-core git commit: bletiny - fix build error in sim

Posted by we...@apache.org.
bletiny - fix build error in sim


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

Branch: refs/heads/nrf_cputime
Commit: 07746cc052ed1b22b0efa23dcc24e6a17234cdd2
Parents: badc6e9
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 18:04:22 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 18:04:22 2017 -0700

----------------------------------------------------------------------
 apps/bletiny/src/cmd.c  | 3 +--
 apps/bletiny/src/main.c | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/07746cc0/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 45debc5..15563a0 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1796,8 +1796,7 @@ cmd_show_coc(int argc, char **argv)
         console_printf("conn_handle: 0x%04x\n", conn->handle);
         j = 0;
         SLIST_FOREACH(coc, &conn->coc_list, next) {
-            console_printf("    idx: %i, chan pointer = 0x%08lx\n", j++,
-                           (uint32_t)coc->chan);
+            console_printf("    idx: %i, chan pointer = %p\n", j++, coc->chan);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/07746cc0/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 8ee724e..96ac90e 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1847,7 +1847,7 @@ bletiny_l2cap_send(uint16_t conn_handle, uint16_t idx, uint16_t bytes)
  * @return int NOTE: this function should never return!
  */
 int
-main(void)
+main(int argc, char **argv)
 {
     int rc;
 


[24/37] incubator-mynewt-core git commit: baselibc - Add %p format specifier to tinyprintf.

Posted by we...@apache.org.
baselibc - Add %p format specifier to tinyprintf.


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

Branch: refs/heads/nrf_cputime
Commit: badc6e99ad77db4d818cf5ca904a18a028afe8df
Parents: 0eed22f
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 18:03:55 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 18:03:55 2017 -0700

----------------------------------------------------------------------
 libc/baselibc/src/tinyprintf.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/badc6e99/libc/baselibc/src/tinyprintf.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/tinyprintf.c b/libc/baselibc/src/tinyprintf.c
index e48be4a..7719d80 100644
--- a/libc/baselibc/src/tinyprintf.c
+++ b/libc/baselibc/src/tinyprintf.c
@@ -52,7 +52,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 /* This is a smaller implementation of printf-family of functions,
  * based on tinyprintf code by Kustaa Nyholm.
- * The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
+ * The formats supported by this implementation are:
+ *     'd' 'u' 'c' 's' 'x' 'X' 'p'.
  * Zero padding and field width are also supported.
  * If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
  * long specifier is also supported.
@@ -61,6 +62,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <stdio.h>
+#include <inttypes.h>
 
 struct param {
     unsigned char width; /**< field width */
@@ -231,6 +233,7 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
     char bf[23];
     char ch;
     char lng;
+    void *v;
 
     p.bf = bf;
 
@@ -300,6 +303,16 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
                 ui2a(intarg(lng, 0, &va), &p);
                 written += putchw(putp, &p);
                 break;
+            case 'p':
+                v = va_arg(va, void *);
+                ui2a((uintptr_t)v, &p);
+                p.base = 16;
+                p.width = 2 * sizeof(void*);
+                p.lz = 1;
+                written += putf(putp, '0');
+                written += putf(putp, 'x');
+                written += putchw(putp, &p);
+                break;
             case 'c':
                 written += putf(putp, (char)(va_arg(va, int)));
                 break;


[04/37] incubator-mynewt-core git commit: net/oic; add a testcase for basic observe.

Posted by we...@apache.org.
net/oic; add a testcase for basic observe.


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

Branch: refs/heads/nrf_cputime
Commit: 133a628fd0ebff170beb53703af388476dcf1d29
Parents: 4393af4
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Sun Mar 26 16:43:03 2017 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Sun Mar 26 16:43:03 2017 -0700

----------------------------------------------------------------------
 net/oic/test/src/test_discovery.c      |  17 +--
 net/oic/test/src/test_getset.c         |  24 ++--
 net/oic/test/src/test_observe.c        | 171 ++++++++++++++++++++++++++++
 net/oic/test/src/test_oic.h            |   1 +
 net/oic/test/src/testcases/oic_tests.c |  11 +-
 5 files changed, 201 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_discovery.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_discovery.c b/net/oic/test/src/test_discovery.c
index 4722a1e..c9e1cfd 100644
--- a/net/oic/test/src/test_discovery.c
+++ b/net/oic/test/src/test_discovery.c
@@ -23,6 +23,7 @@
 
 static int test_discovery_state;
 static volatile int test_discovery_done;
+static struct oc_resource *test_res_light;
 
 static void test_discovery_next_step(struct os_event *);
 static struct os_event test_discovery_next_ev = {
@@ -121,15 +122,16 @@ test_discovery_next_step(struct os_event *ev)
         oic_test_reset_tmo("2nd discovery");
         break;
     case 3: {
-        oc_resource_t *res = oc_new_resource("/light/test", 1, 0);
+        test_res_light = oc_new_resource("/light/test", 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_bind_resource_type(test_res_light, "oic.r.light");
+        oc_resource_bind_resource_interface(test_res_light, OC_IF_RW);
+        oc_resource_set_default_interface(test_res_light, OC_IF_RW);
 
-        oc_resource_set_discoverable(res);
-        oc_resource_set_request_handler(res, OC_GET, test_discovery_get);
-        oc_add_resource(res);
+        oc_resource_set_discoverable(test_res_light);
+        oc_resource_set_request_handler(test_res_light, OC_GET,
+                                        test_discovery_get);
+        oc_add_resource(test_res_light);
         oc_do_ip_discovery(NULL, test_discovery_cb);
         oic_test_reset_tmo("3rd discovery");
         break;
@@ -148,6 +150,7 @@ test_discovery(void)
     while (!test_discovery_done) {
         os_eventq_run(&oic_tapp_evq);
     }
+    oc_delete_resource(test_res_light);
 }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_getset.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_getset.c b/net/oic/test/src/test_getset.c
index 73e6c60..ff59657 100644
--- a/net/oic/test/src/test_getset.c
+++ b/net/oic/test/src/test_getset.c
@@ -23,6 +23,7 @@
 
 static int test_getset_state;
 static volatile int test_getset_done;
+static struct oc_resource *test_res_getset;
 
 static void test_getset_next_step(struct os_event *);
 static struct os_event test_getset_next_ev = {
@@ -80,20 +81,19 @@ test_getset_next_step(struct os_event *ev)
 
     test_getset_state++;
     switch (test_getset_state) {
-    case 1: {
-        oc_resource_t *res;
-
-        res = oc_new_resource("/getset", 1, 0);
-        TEST_ASSERT_FATAL(res);
+    case 1:
+        test_res_getset = oc_new_resource("/getset", 1, 0);
+        TEST_ASSERT_FATAL(test_res_getset);
 
-        oc_resource_bind_resource_interface(res, OC_IF_RW);
-        oc_resource_set_default_interface(res, OC_IF_RW);
+        oc_resource_bind_resource_interface(test_res_getset, OC_IF_RW);
+        oc_resource_set_default_interface(test_res_getset, OC_IF_RW);
 
-        oc_resource_set_request_handler(res, OC_GET, test_getset_get);
-        oc_resource_set_request_handler(res, OC_PUT, test_getset_put);
-        b_rc = oc_add_resource(res);
+        oc_resource_set_request_handler(test_res_getset, OC_GET,
+                                        test_getset_get);
+        oc_resource_set_request_handler(test_res_getset, OC_PUT,
+                                        test_getset_put);
+        b_rc = oc_add_resource(test_res_getset);
         TEST_ASSERT(b_rc == true);
-    }
         /* fall-through */
     case 2:
     case 3:
@@ -121,7 +121,7 @@ test_getset(void)
     while (!test_getset_done) {
         os_eventq_run(&oic_tapp_evq);
     }
-    oc_main_shutdown();
+    oc_delete_resource(test_res_getset);
 }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_observe.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_observe.c b/net/oic/test/src/test_observe.c
new file mode 100644
index 0000000..155e84a
--- /dev/null
+++ b/net/oic/test/src/test_observe.c
@@ -0,0 +1,171 @@
+/*
+ * 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 "test_oic.h"
+
+#include <os/os.h>
+#include <oic/oc_api.h>
+
+#include <cborattr/cborattr.h>
+
+#include <oic/../../src/messaging/coap/observe.h>
+
+static int test_observe_state;
+static volatile int test_observe_done;
+static struct oc_resource *test_res_observe;
+
+static void test_observe_next_step(struct os_event *);
+static struct os_event test_observe_next_ev = {
+    .ev_cb = test_observe_next_step
+};
+
+static void
+test_observe_get(struct oc_request *request, oc_interface_mask_t interface)
+{
+    oc_rep_start_root_object();
+    switch (interface) {
+    case OC_IF_BASELINE:
+        oc_process_baseline_interface(request->resource);
+    case OC_IF_R:
+        switch (test_observe_state) {
+        case 1:
+        case 2:
+            /* initial get request */
+        case 3:
+        case 4:
+        case 5:
+        case 6:
+            oc_rep_set_int(root, value, test_observe_state);
+            break;
+        default:
+            break;
+        }
+    default:
+        break;
+    }
+    oc_rep_end_root_object();
+    oc_send_response(request, OC_STATUS_OK);
+}
+
+static void
+test_observe_rsp(struct oc_client_response *rsp)
+{
+    long long rsp_value;
+    struct cbor_attr_t attrs[] = {
+        [0] = {
+            .attribute = "state",
+            .type = CborAttrIntegerType,
+            .addr.integer = &rsp_value,
+            .dflt.integer = 0
+        },
+        [1] = {
+        }
+    };
+    struct os_mbuf *m;
+    uint16_t data_off;
+    int len;
+
+    switch (test_observe_state) {
+    case 1:
+        TEST_ASSERT(rsp->code == OC_STATUS_NOT_FOUND);
+        break;
+    case 2:
+    case 3:
+    case 4:
+        TEST_ASSERT(rsp->code == OC_STATUS_OK);
+        len = coap_get_payload(rsp->packet, &m, &data_off);
+        if (cbor_read_mbuf_attrs(m, data_off, len, attrs)) {
+            TEST_ASSERT(rsp_value == test_observe_state);
+        }
+        break;
+    default:
+        break;
+    }
+    os_eventq_put(&oic_tapp_evq, &test_observe_next_ev);
+}
+
+static void
+test_observe_next_step(struct os_event *ev)
+{
+    bool b_rc;
+    int rc;
+    struct oc_server_handle server;
+
+    test_observe_state++;
+    switch (test_observe_state) {
+    case 1:
+        test_res_observe = oc_new_resource("/observe", 1, 0);
+        TEST_ASSERT_FATAL(test_res_observe);
+
+        oc_resource_bind_resource_interface(test_res_observe, OC_IF_R);
+        oc_resource_set_default_interface(test_res_observe, OC_IF_R);
+        oc_resource_set_observable(test_res_observe);
+
+        oc_resource_set_request_handler(test_res_observe, OC_GET,
+                                        test_observe_get);
+        b_rc = oc_add_resource(test_res_observe);
+        TEST_ASSERT(b_rc == true);
+
+        /*
+         * Observe nonexistent URI
+         */
+        oic_test_get_endpoint(&server);
+        b_rc = oc_do_observe("/observe_wrong", &server, NULL, test_observe_rsp,
+          LOW_QOS);
+        TEST_ASSERT_FATAL(b_rc == true);
+
+        oic_test_reset_tmo("observe1");
+        break;
+    case 2:
+        oic_test_get_endpoint(&server);
+        b_rc = oc_do_observe("/observe", &server, NULL, test_observe_rsp,
+                             LOW_QOS);
+        TEST_ASSERT_FATAL(b_rc == true);
+
+        oic_test_reset_tmo("observe2");
+        break;
+    case 3:
+    case 4:
+        /*
+         * Valid notifications
+         */
+        rc = oc_notify_observers(test_res_observe);
+        TEST_ASSERT(rc == 1); /* one observer */
+        oic_test_reset_tmo("observe3-4");
+        break;
+    case 5:
+        test_observe_done = 1;
+        break;
+    default:
+        TEST_ASSERT_FATAL(0);
+        break;
+    }
+}
+
+void
+test_observe(void)
+{
+    os_eventq_put(&oic_tapp_evq, &test_observe_next_ev);
+
+    while (!test_observe_done) {
+        os_eventq_run(&oic_tapp_evq);
+    }
+    oc_delete_resource(test_res_observe);
+}
+
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_oic.h
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_oic.h b/net/oic/test/src/test_oic.h
index 5aef7b7..d247226 100644
--- a/net/oic/test/src/test_oic.h
+++ b/net/oic/test/src/test_oic.h
@@ -42,6 +42,7 @@ void oic_test_get_endpoint(struct oc_server_handle *);
 
 void test_discovery(void);
 void test_getset(void);
+void test_observe(void);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/testcases/oic_tests.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/testcases/oic_tests.c b/net/oic/test/src/testcases/oic_tests.c
index cacd731..aefdb3e 100644
--- a/net/oic/test/src/testcases/oic_tests.c
+++ b/net/oic/test/src/testcases/oic_tests.c
@@ -24,7 +24,7 @@
 #include <mn_socket/mn_socket.h>
 
 #define OIC_TAPP_PRIO       9
-#define OIC_TAPP_STACK_SIZE 1024
+#define OIC_TAPP_STACK_SIZE OS_STACK_ALIGN(1024)
 
 /*
  * How long to wait before declaring discovery process failure.
@@ -33,7 +33,7 @@
 
 static struct os_task oic_tapp;
 static const char *oic_test_phase;
-static os_stack_t oic_tapp_stack[OS_STACK_ALIGN(OIC_TAPP_STACK_SIZE)];
+static os_stack_t oic_tapp_stack[OIC_TAPP_STACK_SIZE];
 struct os_eventq oic_tapp_evq;
 static struct os_callout oic_test_timer;
 static struct oc_server_handle oic_tgt;
@@ -41,7 +41,8 @@ static struct oc_server_handle oic_tgt;
 static void
 oic_test_timer_cb(struct os_event *ev)
 {
-    TEST_ASSERT_FATAL(0, "test_phase: %s\n", oic_test_phase ? oic_test_phase : "unknwn");
+    TEST_ASSERT_FATAL(0, "test_phase: %s\n",
+                      oic_test_phase ? oic_test_phase : "unknwn");
 }
 
 void
@@ -85,6 +86,8 @@ oic_test_handler(void *arg)
     oc_main_init(&test_handler);
     test_discovery();
     test_getset();
+    test_observe();
+    oc_main_shutdown();
     tu_restart();
 }
 
@@ -102,7 +105,7 @@ oic_test_init(void)
 
     rc = os_task_init(&oic_tapp, "oic_test", oic_test_handler, NULL,
                       OIC_TAPP_PRIO, OS_WAIT_FOREVER,
-                      oic_tapp_stack, OS_STACK_ALIGN(OIC_TAPP_STACK_SIZE));
+                      oic_tapp_stack, OIC_TAPP_STACK_SIZE);
     TEST_ASSERT_FATAL(rc == 0);
     oc_evq_set(&oic_tapp_evq);
 


[09/37] incubator-mynewt-core git commit: BLE Host - Rename parameter for clarity

Posted by we...@apache.org.
BLE Host - Rename parameter for clarity


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

Branch: refs/heads/nrf_cputime
Commit: 03650bcaf55c9d0fec4178f69635b6f7553ae6ac
Parents: 7f690f7
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Mar 17 15:24:50 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:48:26 2017 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h |  2 +-
 net/nimble/host/src/ble_gap.c          | 15 ++++++++-------
 2 files changed, 9 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/03650bca/net/nimble/host/include/host/ble_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gap.h b/net/nimble/host/include/host/ble_gap.h
index 1c0c93e..189244a 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -556,7 +556,7 @@ int ble_gap_connect(uint8_t own_addr_type, const ble_addr_t *peer_addr,
 int ble_gap_conn_cancel(void);
 int ble_gap_conn_active(void);
 int ble_gap_terminate(uint16_t conn_handle, uint8_t hci_reason);
-int ble_gap_wl_set(const ble_addr_t *addr, uint8_t white_list_count);
+int ble_gap_wl_set(const ble_addr_t *addrs, uint8_t white_list_count);
 int ble_gap_update_params(uint16_t conn_handle,
                           const struct ble_gap_upd_params *params);
 int ble_gap_dbg_update_active(uint16_t conn_handle);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/03650bca/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 d14c55c..ed71374 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -1434,13 +1434,13 @@ ble_gap_wl_tx_clear(void)
 /**
  * Overwrites the controller's white list with the specified contents.
  *
- * @param addr                  The entries to write to the white list.
+ * @param addrs                 The entries to write to the white list.
  * @param white_list_count      The number of entries in the white list.
  *
  * @return                      0 on success; nonzero on failure.
  */
 int
-ble_gap_wl_set(const ble_addr_t *addr, uint8_t white_list_count)
+ble_gap_wl_set(const ble_addr_t *addrs, uint8_t white_list_count)
 {
 #if !MYNEWT_VAL(BLE_WHITELIST)
     return BLE_HS_ENOTSUP;
@@ -1459,8 +1459,8 @@ ble_gap_wl_set(const ble_addr_t *addr, uint8_t white_list_count)
     }
 
     for (i = 0; i < white_list_count; i++) {
-        if (addr[i].type != BLE_ADDR_PUBLIC &&
-            addr[i].type != BLE_ADDR_RANDOM) {
+        if (addrs[i].type != BLE_ADDR_PUBLIC &&
+            addrs[i].type != BLE_ADDR_RANDOM) {
 
             rc = BLE_HS_EINVAL;
             goto done;
@@ -1473,7 +1473,7 @@ ble_gap_wl_set(const ble_addr_t *addr, uint8_t white_list_count)
     }
 
     BLE_HS_LOG(INFO, "GAP procedure initiated: set whitelist; ");
-    ble_gap_log_wl(addr, white_list_count);
+    ble_gap_log_wl(addrs, white_list_count);
     BLE_HS_LOG(INFO, "\n");
 
     rc = ble_gap_wl_tx_clear();
@@ -1482,7 +1482,7 @@ ble_gap_wl_set(const ble_addr_t *addr, uint8_t white_list_count)
     }
 
     for (i = 0; i < white_list_count; i++) {
-        rc = ble_gap_wl_tx_add(addr + i);
+        rc = ble_gap_wl_tx_add(addrs + i);
         if (rc != 0) {
             goto done;
         }
@@ -2353,7 +2353,8 @@ ble_gap_conn_create_tx(uint8_t own_addr_type, const ble_addr_t *peer_addr,
  *                                  already in progress;
  *                              BLE_HS_EBUSY if initiating a connection is not
  *                                  possible because scanning is in progress;
- *                              BLE_HS_EDONE if the specified peer is already connected;
+ *                              BLE_HS_EDONE if the specified peer is already
+ *                                  connected;
  *                              Other nonzero on error.
  */
 int


[22/37] incubator-mynewt-core git commit: BLE uart transport: Add text to sysinit panic.

Posted by we...@apache.org.
BLE uart transport: Add text to sysinit panic.


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

Branch: refs/heads/nrf_cputime
Commit: 70191424871b916134ff9829d59bb12075790de9
Parents: eddcf8f
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 7 17:19:15 2017 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 net/nimble/transport/uart/src/ble_hci_uart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/70191424/net/nimble/transport/uart/src/ble_hci_uart.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/src/ble_hci_uart.c b/net/nimble/transport/uart/src/ble_hci_uart.c
index b1ffd8a..6584112 100755
--- a/net/nimble/transport/uart/src/ble_hci_uart.c
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@ -1021,7 +1021,7 @@ ble_hci_uart_init(void)
     SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_hci_uart_config();
-    SYSINIT_PANIC_ASSERT(rc == 0);
+    SYSINIT_PANIC_ASSERT_MSG(rc == 0, "Failure configuring UART HCI");
 
     memset(&ble_hci_uart_state, 0, sizeof ble_hci_uart_state);
     STAILQ_INIT(&ble_hci_uart_state.tx_pkts);


[11/37] incubator-mynewt-core git commit: BLE Host - Additional debug logging.

Posted by we...@apache.org.
BLE Host - Additional debug logging.


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

Branch: refs/heads/nrf_cputime
Commit: 519e5164bbee1e3c75e679f9d5dc1201af3b8f93
Parents: 5ae196a
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 16:34:15 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_clt.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/519e5164/net/nimble/host/src/ble_att_clt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_clt.c b/net/nimble/host/src/ble_att_clt.c
index 4a93171..f310a6f 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -918,7 +918,20 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle,
     return BLE_HS_ENOTSUP;
 #endif
 
+    uint8_t b;
     int rc;
+    int i;
+
+    BLE_HS_LOG(DEBUG, "ble_att_clt_tx_write_cmd(): ");
+    for (i = 0; i < OS_MBUF_PKTLEN(txom); i++) {
+        if (i != 0) {
+            BLE_HS_LOG(DEBUG, ":");
+        }
+        rc = os_mbuf_copydata(txom, i, 1, &b);
+        assert(rc == 0);
+        BLE_HS_LOG(DEBUG, "0x%02x", b);
+    }
+    
 
     rc = ble_att_clt_tx_write_req_or_cmd(conn_handle, req, txom, 0);
     if (rc != 0) {


[10/37] incubator-mynewt-core git commit: native UART: Allow flow control.

Posted by we...@apache.org.
native UART: Allow flow control.


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

Branch: refs/heads/nrf_cputime
Commit: 54086acef681f69cea9aaa24aad1fb946614bded
Parents: 4fa3357
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Mar 27 17:03:46 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 hw/mcu/native/src/native_uart_cfg.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54086ace/hw/mcu/native/src/native_uart_cfg.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/native_uart_cfg.c b/hw/mcu/native/src/native_uart_cfg.c
index de6bd0b..75c1c22 100644
--- a/hw/mcu/native/src/native_uart_cfg.c
+++ b/hw/mcu/native/src/native_uart_cfg.c
@@ -167,8 +167,20 @@ uart_dev_set_attr(int fd, int32_t baudrate, uint8_t databits,
 
     tty.c_cflag |= (speed | CLOCAL | CREAD);
 
-    /* Disable flow control. */
-    tty.c_cflag &= ~CRTSCTS;
+    /* Set flow control. */
+    switch (flow_ctl) {
+    case HAL_UART_FLOW_CTL_NONE:
+        tty.c_cflag &= ~CRTSCTS;
+        break;
+
+    case HAL_UART_FLOW_CTL_RTS_CTS:
+        tty.c_cflag |= CRTSCTS;
+        break;
+
+    default:
+        fprintf(stderr, "invalid flow control setting: %d\n", flow_ctl);
+        return -1;
+    }
 
     errno = 0;
     rc = cfsetospeed(&tty, speed);


[33/37] incubator-mynewt-core git commit: nimble/controller: Slight refactor of use of pkt receive time

Posted by we...@apache.org.
nimble/controller: Slight refactor of use of pkt receive time

Modified the code so that the packet receive time is used in less
places. No functionality change.


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

Branch: refs/heads/nrf_cputime
Commit: 8158805cd5d86b3fb17365065425711f0cb6b7ea
Parents: 9b6c284
Author: William San Filippo <wi...@runtime.io>
Authored: Thu Jan 19 14:57:21 2017 -0800
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Mar 30 14:09:04 2017 -0700

----------------------------------------------------------------------
 .../controller/include/controller/ble_ll_conn.h |  2 +-
 net/nimble/controller/src/ble_ll_adv.c          |  7 +------
 net/nimble/controller/src/ble_ll_conn.c         | 21 ++++++++------------
 net/nimble/controller/src/ble_ll_conn_priv.h    |  2 +-
 4 files changed, 11 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8158805c/net/nimble/controller/include/controller/ble_ll_conn.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_conn.h b/net/nimble/controller/include/controller/ble_ll_conn.h
index d0badff..b2e6410 100644
--- a/net/nimble/controller/include/controller/ble_ll_conn.h
+++ b/net/nimble/controller/include/controller/ble_ll_conn.h
@@ -202,7 +202,7 @@ struct ble_ll_conn_sm
     uint16_t max_ce_len;
     uint16_t tx_win_off;
     uint32_t anchor_point;
-    uint32_t last_anchor_point;
+    uint32_t last_anchor_point;     /* Slave only */
     uint32_t slave_cur_tx_win_usecs;
     uint32_t slave_cur_window_widening;
     uint32_t last_rxd_pdu_cputime;  /* Used exclusively for supervision timer */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8158805c/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 1130638..203f13a 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -1209,14 +1209,12 @@ 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;
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     uint8_t resolved;
 #endif
     uint8_t addr_type;
     uint8_t *inita;
     uint8_t *ident_addr;
-    uint32_t endtime;
 
     /* Check filter policy. */
     valid = 0;
@@ -1275,10 +1273,7 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr,
 #endif
 
         /* Try to start slave connection. If successful, stop advertising */
-        pyld_len = rxbuf[1] & BLE_ADV_PDU_HDR_LEN_MASK;
-        endtime = hdr->beg_cputime +
-            os_cputime_usecs_to_ticks(BLE_TX_DUR_USECS_M(pyld_len));
-        valid = ble_ll_conn_slave_start(rxbuf, endtime, addr_type, hdr);
+        valid = ble_ll_conn_slave_start(rxbuf, addr_type, hdr);
         if (valid) {
             ble_ll_adv_sm_stop(advsm);
         }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8158805c/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 171d938..1d2714a 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -1770,11 +1770,11 @@ 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,
-                    struct ble_mbuf_hdr *rxhdr)
+ble_ll_conn_created(struct ble_ll_conn_sm *connsm, struct ble_mbuf_hdr *rxhdr)
 {
     int rc;
     uint8_t *evbuf;
+    uint32_t endtime;
     uint32_t usecs;
 
     /* Set state to created */
@@ -1798,8 +1798,10 @@ ble_ll_conn_created(struct ble_ll_conn_sm *connsm, uint32_t endtime,
      * the transmit window offset from the end of the connection request.
      */
     rc = 1;
-    connsm->last_anchor_point = endtime;
     if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
+        endtime = rxhdr->beg_cputime +
+            os_cputime_usecs_to_ticks(BLE_TX_DUR_USECS_M(BLE_CONNECT_REQ_LEN));
+        connsm->last_anchor_point = endtime;
         connsm->slave_cur_tx_win_usecs =
             connsm->tx_win_size * BLE_LL_CONN_TX_WIN_USECS;
         usecs = 1250 + (connsm->tx_win_off * BLE_LL_CONN_TX_WIN_USECS);
@@ -2169,9 +2171,7 @@ ble_ll_init_rx_pkt_in(uint8_t *rxbuf, struct ble_mbuf_hdr *ble_hdr)
     int8_t rpa_index;
 #endif
     uint8_t addr_type;
-    uint8_t payload_len;
     uint8_t *addr;
-    uint32_t endtime;
     struct ble_ll_conn_sm *connsm;
 
     /* Get the connection state machine we are trying to create */
@@ -2216,10 +2216,7 @@ ble_ll_init_rx_pkt_in(uint8_t *rxbuf, struct ble_mbuf_hdr *ble_hdr)
         /* Connection has been created. Stop scanning */
         g_ble_ll_conn_create_sm = NULL;
         ble_ll_scan_sm_stop(0);
-        payload_len = rxbuf[1] & BLE_ADV_PDU_HDR_LEN_MASK;
-        endtime = ble_hdr->beg_cputime +
-            os_cputime_usecs_to_ticks(BLE_TX_DUR_USECS_M(payload_len));
-        ble_ll_conn_created(connsm, endtime, NULL);
+        ble_ll_conn_created(connsm, NULL);
     } else {
         ble_ll_scan_chk_resume();
     }
@@ -3053,8 +3050,7 @@ 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,
-                        struct ble_mbuf_hdr *rxhdr)
+ble_ll_conn_slave_start(uint8_t *rxbuf, uint8_t pat, struct ble_mbuf_hdr *rxhdr)
 {
     int rc;
     uint32_t temp;
@@ -3147,8 +3143,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, rxhdr);
+    rc = ble_ll_conn_created(connsm, 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/8158805c/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 f3ede51..0913892 100644
--- a/net/nimble/controller/src/ble_ll_conn_priv.h
+++ b/net/nimble/controller/src/ble_ll_conn_priv.h
@@ -103,7 +103,7 @@ 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, uint8_t pat,
                             struct ble_mbuf_hdr *rxhdr);
 
 /* Link Layer interface */


[13/37] incubator-mynewt-core git commit: baselibc - Optionally include file/line in assert.

Posted by we...@apache.org.
baselibc - Optionally include file/line in assert.

To include filename and line number in assert messages, enable this
syscfg setting: BASELIBC_ASSERT_FILE_LINE

This behavior is disabled by default for non-native BSPs.  The native
bsp enables this setting.


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

Branch: refs/heads/nrf_cputime
Commit: 537b2200e40ff38b977eb24aeda20b0c30db1960
Parents: 03fa865
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 16:24:56 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 hw/bsp/native/syscfg.yml       |  1 +
 libc/baselibc/include/assert.h | 11 ++++++++++-
 libc/baselibc/syscfg.yml       |  6 ++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/537b2200/hw/bsp/native/syscfg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/native/syscfg.yml b/hw/bsp/native/syscfg.yml
index 89171a5..9ca0608 100644
--- a/hw/bsp/native/syscfg.yml
+++ b/hw/bsp/native/syscfg.yml
@@ -18,6 +18,7 @@
 #
 
 syscfg.vals:
+    BASELIBC_ASSERT_FILE_LINE: 1
     NFFS_FLASH_AREA: FLASH_AREA_NFFS
     CONFIG_FCB_FLASH_AREA: FLASH_AREA_NFFS
     REBOOT_LOG_FLASH_AREA: FLASH_AREA_REBOOT_LOG

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/537b2200/libc/baselibc/include/assert.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/assert.h b/libc/baselibc/include/assert.h
index 68f2f86..8a131b5 100644
--- a/libc/baselibc/include/assert.h
+++ b/libc/baselibc/include/assert.h
@@ -5,6 +5,8 @@
 #ifndef _ASSERT_H
 #define _ASSERT_H
 
+#include "syscfg/syscfg.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -25,7 +27,14 @@ extern "C" {
 extern void __assert_func(const char *, int, const char *, const char *)
     __attribute((noreturn));
 
-#define assert(x) ((x) ? (void)0 : __assert_func(NULL, 0, NULL, NULL))
+#if MYNEWT_VAL(BASELIBC_ASSERT_FILE_LINE)
+#define assert(x) ((x) ? (void)0 : \
+    __assert_func(__FILE__, __LINE__, NULL, NULL))
+#else
+#define assert(x) ((x) ? (void)0 : \
+    __assert_func(NULL, 0, NULL, NULL))
+#endif
+
 
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/537b2200/libc/baselibc/syscfg.yml
----------------------------------------------------------------------
diff --git a/libc/baselibc/syscfg.yml b/libc/baselibc/syscfg.yml
index 7ec5b18..62188e1 100644
--- a/libc/baselibc/syscfg.yml
+++ b/libc/baselibc/syscfg.yml
@@ -4,3 +4,9 @@ syscfg.defs:
     BASELIBC_PRESENT:
         description: "Indicates that baselibc is the libc implementation."
         value: 1
+
+    BASELIBC_ASSERT_FILE_LINE:
+        description: >
+            Include filename and line number in assert messages.  Aids in
+            debugging, but increases text size.
+        value: 0


[08/37] incubator-mynewt-core git commit: native; allow user to specify real ttys to connect to from command line when invoking simulator. --uart0 for uart0, --uart1 for uart1.

Posted by we...@apache.org.
native; allow user to specify real ttys to connect to from command line when invoking
simulator. --uart0 <filename> for uart0, --uart1 <filename> for uart1.


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

Branch: refs/heads/nrf_cputime
Commit: 7f690f7f59bb561d89898af8ce53b37fa89320ad
Parents: efbaf05
Author: System Administrator <ma...@runtime.io>
Authored: Tue Mar 28 16:38:52 2017 -0700
Committer: System Administrator <ma...@runtime.io>
Committed: Tue Mar 28 16:38:52 2017 -0700

----------------------------------------------------------------------
 hw/mcu/native/include/mcu/mcu_sim.h |  1 +
 hw/mcu/native/src/hal_system.c      | 48 ++++++++++++++++++++++++++++----
 hw/mcu/native/src/hal_uart.c        |  1 +
 3 files changed, 45 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f690f7f/hw/mcu/native/include/mcu/mcu_sim.h
----------------------------------------------------------------------
diff --git a/hw/mcu/native/include/mcu/mcu_sim.h b/hw/mcu/native/include/mcu/mcu_sim.h
index cf7dd1a..5c9f804 100644
--- a/hw/mcu/native/include/mcu/mcu_sim.h
+++ b/hw/mcu/native/include/mcu/mcu_sim.h
@@ -27,6 +27,7 @@ extern "C" {
 
 extern char *native_flash_file;
 extern char *native_uart_log_file;
+extern const char *native_uart_dev_strs[];
 
 void mcu_sim_parse_args(int argc, char **argv);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f690f7f/hw/mcu/native/src/hal_system.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_system.c b/hw/mcu/native/src/hal_system.c
index a27932c..ad78cba 100644
--- a/hw/mcu/native/src/hal_system.c
+++ b/hw/mcu/native/src/hal_system.c
@@ -21,6 +21,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <assert.h>
+#include <getopt.h>
 
 #include "syscfg/syscfg.h"
 #include "hal/hal_system.h"
@@ -49,13 +50,18 @@ hal_system_reset(void)
 static void
 usage(char *progname, int rc)
 {
-    const char msg[] =
-      "Usage: %s [-f flash_file] [-u uart_log_file]\n"
+    const char msg1[] = "Usage: ";
+    const char msg2[] =
+      "\n [-f flash_file][-u uart_log_file][--uart0 <file>][--uart1 <file>]\n"
       "     -f flash_file tells where binary flash file is located. It gets\n"
       "        created if it doesn't already exist.\n"
-      "     -u uart_log_file puts all UART data exchanges into a logfile.\n";
+      "     -u uart_log_file puts all UART data exchanges into a logfile.\n"
+      "     -uart0 uart0_file connects UART0 to character device uart0_file.\n"
+      "     -uart1 uart1_file connects UART1 to character device uart1_file.\n";
 
-    write(2, msg, strlen(msg));
+    write(2, msg1, strlen(msg1));
+    write(2, progname, strlen(progname));
+    write(2, msg2, strlen(msg2));
     exit(rc);
 }
 
@@ -64,6 +70,15 @@ mcu_sim_parse_args(int argc, char **argv)
 {
     int ch;
     char *progname;
+    struct option options[] = {
+        { "flash",      required_argument,      0, 'f' },
+        { "uart_log",   required_argument,      0, 'u' },
+        { "help",       no_argument,            0, 'h' },
+        { "uart0",      required_argument,      0, 0 },
+        { "uart1",      required_argument,      0, 0 },
+        { NULL }
+    };
+    int opt_idx;
     extern int main(int argc, char **arg);
 
 #if MYNEWT_VAL(OS_SCHEDULING)
@@ -72,7 +87,8 @@ mcu_sim_parse_args(int argc, char **argv)
     }
 #endif
     progname = argv[0];
-    while ((ch = getopt(argc, argv, "hf:u:")) != -1) {
+    while ((ch = getopt_long(argc, argv, "hf:u:", options, &opt_idx)) !=
+            -1) {
         switch (ch) {
         case 'f':
             native_flash_file = optarg;
@@ -83,6 +99,28 @@ mcu_sim_parse_args(int argc, char **argv)
         case 'h':
             usage(progname, 0);
             break;
+        case 0:
+            switch (opt_idx) {
+            case 0:
+                native_flash_file = optarg;
+                break;
+            case 1:
+                native_uart_log_file = optarg;
+                break;
+            case 2:
+                usage(progname, 0);
+                break;
+            case 3:
+                native_uart_dev_strs[0] = optarg;
+                break;
+            case 4:
+                native_uart_dev_strs[1] = optarg;
+                break;
+            default:
+                usage(progname, -1);
+                break;
+            }
+            break;
         default:
             usage(progname, -1);
             break;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f690f7f/hw/mcu/native/src/hal_uart.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_uart.c b/hw/mcu/native/src/hal_uart.c
index d9328aa..4ef6186 100644
--- a/hw/mcu/native/src/hal_uart.c
+++ b/hw/mcu/native/src/hal_uart.c
@@ -37,6 +37,7 @@
 #include <termios.h>
 #include <errno.h>
 
+#include "mcu/mcu_sim.h"
 #include "native_uart_cfg_priv.h"
 
 #define UART_CNT                2


[06/37] incubator-mynewt-core git commit: net/oic; use hal_bsp_dev_id() to generate platform uuid.

Posted by we...@apache.org.
net/oic; use hal_bsp_dev_id() to generate platform uuid.


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

Branch: refs/heads/nrf_cputime
Commit: e62a2458c2d8835bd8f5c532573ece4534532f57
Parents: 9d8c6f2
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Mar 27 15:22:59 2017 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Mar 27 15:22:59 2017 -0700

----------------------------------------------------------------------
 net/oic/src/api/oc_uuid.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e62a2458/net/oic/src/api/oc_uuid.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_uuid.c b/net/oic/src/api/oc_uuid.c
index 2ceaf7a..e0244fb 100644
--- a/net/oic/src/api/oc_uuid.c
+++ b/net/oic/src/api/oc_uuid.c
@@ -15,12 +15,13 @@
 */
 
 #include "oc_uuid.h"
-#include "port/oc_random.h"
 #include <ctype.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 
+#include <hal/hal_bsp.h>
+
 void
 oc_str_to_uuid(const char *str, oc_uuid_t *uuid)
 {
@@ -92,17 +93,11 @@ oc_uuid_to_str(const oc_uuid_t *uuid, char *buffer, int buflen)
 void
 oc_gen_uuid(oc_uuid_t *uuid)
 {
-  int i;
-  uint16_t r;
-
-  for (i = 0; i < 8; i++) {
-    r = oc_random_rand();
-    memcpy((uint8_t *)&uuid->id[i * 2], (uint8_t *)&r, sizeof(r));
-  }
+  hal_bsp_hw_id(&uuid->id[0], sizeof(uuid->id));
 
   /*  From RFC 4122
       Set the two most significant bits of the
-      clock_seq_hi_and_reserved (8th octect) to
+      clock_seq_hi_and_reserved (8th octet) to
       zero and one, respectively.
   */
   uuid->id[8] &= 0x3f;


[15/37] incubator-mynewt-core git commit: BLE Host - If initial sync fails, retry later.

Posted by we...@apache.org.
BLE Host - If initial sync fails, retry later.


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

Branch: refs/heads/nrf_cputime
Commit: e679687fcd6a98a622054ac5786682fe0aa9cc9a
Parents: 9b98206
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 16:40:38 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e679687f/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 9fb5702..231eea1 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -475,10 +475,7 @@ ble_hs_start(void)
         return rc;
     }
 
-    rc = ble_hs_sync();
-    if (rc != 0) {
-        return rc;
-    }
+    ble_hs_sync();
 
     return 0;
 }


[18/37] incubator-mynewt-core git commit: BLE Host - Make L2CAP CID definitions public.

Posted by we...@apache.org.
BLE Host - Make L2CAP CID definitions public.


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

Branch: refs/heads/nrf_cputime
Commit: 9b9820611e7554557612dc13b6b42b0019c7ec88
Parents: c44b4ad
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 16:39:36 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_hs.h    | 1 +
 net/nimble/host/include/host/ble_l2cap.h | 4 ++++
 net/nimble/host/src/ble_l2cap_priv.h     | 4 ----
 3 files changed, 5 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b982061/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index 8dd2296..ada830d 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -30,6 +30,7 @@
 #include "host/ble_hs_log.h"
 #include "host/ble_hs_test.h"
 #include "host/ble_hs_mbuf.h"
+#include "host/ble_l2cap.h"
 #include "host/ble_sm.h"
 #include "host/ble_store.h"
 #include "host/ble_uuid.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b982061/net/nimble/host/include/host/ble_l2cap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_l2cap.h b/net/nimble/host/include/host/ble_l2cap.h
index b3b03b9..0b54189 100644
--- a/net/nimble/host/include/host/ble_l2cap.h
+++ b/net/nimble/host/include/host/ble_l2cap.h
@@ -28,6 +28,10 @@ extern "C" {
 struct ble_l2cap_sig_update_req;
 struct ble_hs_conn;
 
+#define BLE_L2CAP_CID_ATT           4
+#define BLE_L2CAP_CID_SIG           5
+#define BLE_L2CAP_CID_SM            6
+
 #define BLE_L2CAP_SIG_OP_REJECT                 0x01
 #define BLE_L2CAP_SIG_OP_CONNECT_REQ            0x02
 #define BLE_L2CAP_SIG_OP_CONNECT_RSP            0x03

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b982061/net/nimble/host/src/ble_l2cap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index 1d035e8..5bc8d83 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -49,10 +49,6 @@ extern STATS_SECT_DECL(ble_l2cap_stats) ble_l2cap_stats;
 
 extern struct os_mempool ble_l2cap_chan_pool;
 
-#define BLE_L2CAP_CID_ATT           4
-#define BLE_L2CAP_CID_SIG           5
-#define BLE_L2CAP_CID_SM            6
-
 /* This is nimble specific; packets sent to the black hole CID do not elicit
  * an "invalid CID" response.
  */


[12/37] incubator-mynewt-core git commit: BLE Host - Don't allow conn to already-conned peer

Posted by we...@apache.org.
BLE Host - Don't allow conn to already-conned peer


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

Branch: refs/heads/nrf_cputime
Commit: 5ae196a95248b3531f7e3c940f0444df78d3e554
Parents: 537b220
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 16:32:25 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap.c | 6 ++++++
 1 file changed, 6 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5ae196a9/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 ed71374..9d7d0f0 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -2384,6 +2384,12 @@ ble_gap_connect(uint8_t own_addr_type, const ble_addr_t *peer_addr,
         goto done;
     }
 
+    /* Verify peer not already connected. */
+    if (ble_hs_conn_find_by_addr(peer_addr) != NULL) {
+        rc = BLE_HS_EDONE;
+        goto done;
+    }
+
     if (!ble_hs_conn_can_alloc()) {
         rc = BLE_HS_ENOMEM;
         goto done;


[14/37] incubator-mynewt-core git commit: sim compiler: add -lm flag.

Posted by we...@apache.org.
sim compiler: add -lm flag.


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

Branch: refs/heads/nrf_cputime
Commit: 03fa86591eac0c82a8e8489b79d301fb2383efee
Parents: 03650bc
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Mar 8 19:37:54 2017 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 compiler/sim/compiler.yml | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/03fa8659/compiler/sim/compiler.yml
----------------------------------------------------------------------
diff --git a/compiler/sim/compiler.yml b/compiler/sim/compiler.yml
index 5e5e7e5..a2e619f 100644
--- a/compiler/sim/compiler.yml
+++ b/compiler/sim/compiler.yml
@@ -34,6 +34,7 @@ compiler.flags.debug: [compiler.flags.base, -O0]
 compiler.as.flags: [-x, assembler-with-cpp]
 compiler.ld.mapfile: false
 compiler.ld.binfile: false
+compiler.ld.flags: -lm
 
 # Linux.
 compiler.flags.base.LINUX: >


[36/37] incubator-mynewt-core git commit: MYNEWT-701: Low power timer support

Posted by we...@apache.org.
MYNEWT-701: Low power timer support

This commit adds low power timer support to the controller for
the nordic chips. Please see the Jira ticket for more information.


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

Branch: refs/heads/nrf_cputime
Commit: 3c7859538a63df76d170fb5cc30cac902f13a7b0
Parents: 507f493
Author: William San Filippo <wi...@runtime.io>
Authored: Thu Mar 30 13:30:57 2017 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Mar 30 14:10:22 2017 -0700

----------------------------------------------------------------------
 hw/drivers/nimble/nrf51/include/ble/xcvr.h      |  11 +-
 hw/drivers/nimble/nrf51/src/ble_phy.c           | 440 +++++++++++++++++--
 hw/drivers/nimble/nrf52/src/ble_phy.c           |   5 +
 kernel/os/src/os_cputime.c                      |  13 +-
 .../controller/include/controller/ble_ll.h      |   2 +-
 net/nimble/controller/src/ble_ll_conn.c         |   6 +-
 6 files changed, 434 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3c785953/hw/drivers/nimble/nrf51/include/ble/xcvr.h
----------------------------------------------------------------------
diff --git a/hw/drivers/nimble/nrf51/include/ble/xcvr.h b/hw/drivers/nimble/nrf51/include/ble/xcvr.h
index 0736506..9f15271 100644
--- a/hw/drivers/nimble/nrf51/include/ble/xcvr.h
+++ b/hw/drivers/nimble/nrf51/include/ble/xcvr.h
@@ -25,9 +25,18 @@ extern "C" {
 #endif
 
 /* Transceiver specific defintions */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+/*
+ * NOTE: we have to account for the RTC output compare issue, which is why
+ * this number is much larger when using the 32.768 crystal.
+ */
+#define XCVR_PROC_DELAY_USECS         (230)
+#else
+#define XCVR_PROC_DELAY_USECS         (100)
+#endif
+
 #define XCVR_RX_START_DELAY_USECS     (140)
 #define XCVR_TX_START_DELAY_USECS     (140)
-#define XCVR_PROC_DELAY_USECS         (100)
 #define XCVR_TX_SCHED_DELAY_USECS     \
     (XCVR_TX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
 #define XCVR_RX_SCHED_DELAY_USECS     \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3c785953/hw/drivers/nimble/nrf51/src/ble_phy.c
----------------------------------------------------------------------
diff --git a/hw/drivers/nimble/nrf51/src/ble_phy.c b/hw/drivers/nimble/nrf51/src/ble_phy.c
index d6ca388..c9e2b74 100644
--- a/hw/drivers/nimble/nrf51/src/ble_phy.c
+++ b/hw/drivers/nimble/nrf51/src/ble_phy.c
@@ -22,6 +22,7 @@
 #include <assert.h>
 #include "syscfg/syscfg.h"
 #include "os/os.h"
+#include "ble/xcvr.h"
 #include "bsp/cmsis_nvic.h"
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
@@ -84,6 +85,9 @@ struct ble_phy_obj
     struct ble_mbuf_hdr rxhdr;
     void *txend_arg;
     ble_phy_tx_end_func txend_cb;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    uint32_t phy_start_cputime;
+#endif
 };
 struct ble_phy_obj g_ble_phy_data;
 
@@ -278,6 +282,133 @@ nrf_wait_disabled(void)
     }
 }
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+/**
+ *
+ *
+ */
+int
+ble_phy_set_start_time(uint32_t cputime, uint8_t rem_usecs)
+{
+    uint32_t next_cc;
+    uint32_t cur_cc;
+    uint32_t cntr;
+    uint32_t delta;
+
+    /*
+     * XXX: The TXEN time is 140 usecs but there may be additional delays
+     * Need to look at this.
+     */
+
+    /*
+     * With the 32.768 kHz crystal, we may need to adjust the RTC compare
+     * value by 1 tick due to the time it takes for TXEN. The code uses a 5 RTC
+     * tick offset, which is 152.5 usecs. The TXEN time is 140 usecs. This
+     * means that with a remainder of 0, TIMER0 should be set to 12 or 13 (as
+     * TIMER0 counts at 1MHz). A remainder of 19 or more we will need to add
+     * 1 tick. We dont need to add 1 tick per se, but it does give us slightly
+     * more time and thus less of a chance to miss a tick. Another note: we
+     * cant set TIMER0 CC to 0 as the compare wont occur; it must be 1 or more.
+     * This is why we subtract 18 (as opposed to 19) as rem_uses will be >= 1.
+     */
+    if (rem_usecs <= 18) {
+        cputime -= 5;
+        rem_usecs += 12;
+    } else {
+        cputime -= 4;
+        rem_usecs -= 18;
+    }
+
+    /*
+     * Can we set the RTC compare to start TIMER0? We can do it if:
+     *      a) Current compare value is not N+1 or N+2 ticks from current
+     *      counter.
+     *      b) The value we want to set is not at least N+2 from current
+     *      counter.
+     *
+     * NOTE: since the counter can tick 1 while we do these calculations we
+     * need to account for it.
+     */
+    next_cc = cputime & 0xffffff;
+    cur_cc = NRF_RTC0->CC[0];
+    cntr = NRF_RTC0->COUNTER;
+
+    delta = (cur_cc - cntr) & 0xffffff;
+    if ((delta <= 3) && (delta != 0)) {
+        return -1;
+    }
+    delta = (next_cc - cntr) & 0xffffff;
+    if ((delta & 0x800000) || (delta < 3)) {
+        return -1;
+    }
+
+    /* Clear and set TIMER0 to fire off at proper time */
+    NRF_TIMER0->TASKS_CLEAR = 1;
+    NRF_TIMER0->CC[0] = rem_usecs;
+    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
+
+    /* Set RTC compare to start TIMER0 */
+    NRF_RTC0->EVENTS_COMPARE[0] = 0;
+    NRF_RTC0->CC[0] = next_cc;
+    NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
+
+    /* Enable PPI */
+    NRF_PPI->CHENSET = PPI_CHEN_CH31_Msk;
+
+    /* Store the cputime at which we set the RTC */
+    g_ble_phy_data.phy_start_cputime = cputime;
+
+    return 0;
+}
+
+/**
+ * Function is used to set PPI so that we can time out waiting for a reception
+ * to occur. This happens for two reasons: we have sent a packet and we are
+ * waiting for a respons (txrx should be set to ENABLE_TXRX) or we are
+ * starting a connection event and we are a slave and we are waiting for the
+ * master to send us a packet (txrx should be set to ENABLE_RX).
+ *
+ * NOTE: when waiting for a txrx turn-around, wfr_usecs is not used as there
+ * is no additional time to wait; we know when we should receive the address of
+ * the received frame.
+ *
+ * @param txrx Flag denoting if this wfr is a txrx turn-around or not.
+ * @param wfr_usecs Amount of usecs to wait.
+ */
+void
+ble_phy_wfr_enable(int txrx, uint32_t wfr_usecs)
+{
+    uint32_t end_time;
+
+    if (txrx == BLE_PHY_WFR_ENABLE_TXRX) {
+        /*
+         * Timeout occurs an IFS time plus time it takes to receive address
+         * from the transmit end. We add additional time to make sure the
+         * address event comes before the compare. Note that transmit end
+         * is captured in CC[2]
+         *
+         * XXX: this assumes 1Mbps as 40 usecs is header rx time for 1Mbps
+         */
+        end_time = NRF_TIMER0->CC[2] + BLE_LL_IFS + 40 + 16;
+    } else {
+        /* CC[0] is set to when RXEN occurs. NOTE: the extra 16 usecs is
+           jitter */
+        end_time = NRF_TIMER0->CC[0] + XCVR_RX_START_DELAY_USECS + wfr_usecs +
+            40 + 16;
+    }
+
+    /* wfr_secs is the time from rxen until timeout */
+    NRF_TIMER0->CC[3] = end_time;
+    NRF_TIMER0->EVENTS_COMPARE[3] = 0;
+
+    /* Enable wait for response PPI */
+    NRF_PPI->CHENSET = (PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk);
+
+    /* Enable the disabled interrupt so we time out on events compare */
+    NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
+}
+#endif
+
 /**
  * Setup transceiver for receive.
  */
@@ -368,14 +499,18 @@ ble_phy_tx_end_isr(void)
     uint8_t transition;
     uint8_t txlen;
     uint32_t wfr_time;
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768)
     uint32_t txstart;
+#endif
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768)
     /*
      * Read captured tx start time. This is not the actual transmit start
      * time but it is the time at which the address event occurred
      * (after transmission of access address)
      */
     txstart = NRF_TIMER0->CC[1];
+#endif
 
     /* If this transmission was encrypted we need to remember it */
     was_encrypted = g_ble_phy_data.phy_encrypted;
@@ -384,14 +519,20 @@ 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,
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+    ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, g_ble_phy_data.phy_tx_pyld_len,
+               was_encrypted, NRF_TIMER0->CC[2]);
+#else
+    ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, g_ble_phy_data.phy_tx_pyld_len,
                was_encrypted, txstart);
+#endif
 
     /* Clear events and clear interrupt on disabled event */
     NRF_RADIO->EVENTS_DISABLED = 0;
     NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
     NRF_RADIO->EVENTS_END = 0;
     wfr_time = NRF_RADIO->SHORTS;
+    (void)wfr_time;
 
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     /*
@@ -424,13 +565,28 @@ ble_phy_tx_end_isr(void)
         if (txlen && was_encrypted) {
             txlen += BLE_LL_DATA_MIC_LEN;
         }
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+        ble_phy_wfr_enable(BLE_PHY_WFR_ENABLE_TXRX, 0);
+#else
         wfr_time = BLE_LL_WFR_USECS - BLE_TX_LEN_USECS_M(NRF_RX_START_OFFSET);
         wfr_time += BLE_TX_DUR_USECS_M(txlen);
         wfr_time = os_cputime_usecs_to_ticks(wfr_time);
         ble_ll_wfr_enable(txstart + wfr_time);
+#endif
     } else {
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+        /*
+         * XXX: not sure we need to stop the timer here all the time. Or that
+         * it should be stopped here.
+         */
+        NRF_TIMER0->TASKS_STOP = 1;
+        NRF_TIMER0->TASKS_SHUTDOWN = 1;
+        NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk |
+                           PPI_CHEN_CH20_Msk | PPI_CHEN_CH31_Msk;
+#else
         /* Disable automatic TXEN */
         NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
+#endif
         assert(transition == BLE_PHY_TRANSITION_NONE);
     }
 }
@@ -518,11 +674,48 @@ ble_phy_rx_start_isr(void)
 {
     int rc;
     uint32_t state;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    uint32_t usecs;
+    uint32_t ticks;
+#endif
     struct ble_mbuf_hdr *ble_hdr;
 
     /* Clear events and clear interrupt */
     NRF_RADIO->EVENTS_ADDRESS = 0;
+
+    /* Clear wfr timer channels and DISABLED interrupt */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk | RADIO_INTENCLR_ADDRESS_Msk;
+    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk;
+#else
     NRF_RADIO->INTENCLR = RADIO_INTENCLR_ADDRESS_Msk;
+#endif
+
+    /* Initialize flags, channel and state in ble header at rx start */
+    ble_hdr = &g_ble_phy_data.rxhdr;
+    ble_hdr->rxinfo.flags = ble_ll_state_get();
+    ble_hdr->rxinfo.channel = g_ble_phy_data.phy_chan;
+    ble_hdr->rxinfo.handle = 0;
+
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+    /*
+     * Calculate receive start time. We assume that the header time is
+     * 40 usecs (only 1 mbps supported right now).
+     *
+     * XXX: possibly use other routine with remainder!
+     */
+    usecs = NRF_TIMER0->CC[1] - 40;
+    ticks = os_cputime_usecs_to_ticks(usecs);
+    ble_hdr->rem_usecs = usecs - os_cputime_ticks_to_usecs(ticks);
+    if (ble_hdr->rem_usecs == 31) {
+        ble_hdr->rem_usecs = 0;
+        ++ticks;
+    }
+    ble_hdr->beg_cputime = g_ble_phy_data.phy_start_cputime + ticks;
+#else
+    ble_hdr->beg_cputime = NRF_TIMER0->CC[1] -
+        os_cputime_usecs_to_ticks(BLE_TX_LEN_USECS_M(NRF_RX_START_OFFSET));
+#endif
 
     /* Wait to get 1st byte of frame */
     while (1) {
@@ -542,14 +735,6 @@ ble_phy_rx_start_isr(void)
         }
     }
 
-    /* Initialize flags, channel and state in ble header at rx start */
-    ble_hdr = &g_ble_phy_data.rxhdr;
-    ble_hdr->rxinfo.flags = ble_ll_state_get();
-    ble_hdr->rxinfo.channel = g_ble_phy_data.phy_chan;
-    ble_hdr->rxinfo.handle = 0;
-    ble_hdr->beg_cputime = NRF_TIMER0->CC[1] -
-        os_cputime_usecs_to_ticks(BLE_TX_LEN_USECS_M(NRF_RX_START_OFFSET));
-
     /* Call Link Layer receive start function */
     rc = ble_ll_rx_start(g_ble_phy_data.rxdptr, g_ble_phy_data.phy_chan,
                          &g_ble_phy_data.rxhdr);
@@ -584,16 +769,34 @@ ble_phy_isr(void)
     /* Read irq register to determine which interrupts are enabled */
     irq_en = NRF_RADIO->INTENCLR;
 
-    /* Check for disabled event. This only happens for transmits now */
-    if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
-        ble_phy_tx_end_isr();
-    }
+    /*
+     * NOTE: order of checking is important! Possible, if things get delayed,
+     * we have both an ADDRESS and DISABLED interrupt in rx state. If we get
+     * an address, we disable the DISABLED interrupt.
+     */
 
     /* We get this if we have started to receive a frame */
     if ((irq_en & RADIO_INTENCLR_ADDRESS_Msk) && NRF_RADIO->EVENTS_ADDRESS) {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        irq_en &= ~RADIO_INTENCLR_DISABLED_Msk;
+#endif
         ble_phy_rx_start_isr();
     }
 
+    /* Check for disabled event. This only happens for transmits now */
+    if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        if (g_ble_phy_data.phy_state == BLE_PHY_STATE_RX) {
+            NRF_RADIO->EVENTS_DISABLED = 0;
+            ble_ll_wfr_timer_exp(NULL);
+        } else {
+            ble_phy_tx_end_isr();
+        }
+#else
+        ble_phy_tx_end_isr();
+#endif
+    }
+
     /* Receive packet end (we dont enable this for transmit) */
     if ((irq_en & RADIO_INTENCLR_END_Msk) && NRF_RADIO->EVENTS_END) {
         ble_phy_rx_end_isr();
@@ -617,6 +820,8 @@ int
 ble_phy_init(void)
 {
     int rc;
+
+#if !defined(BLE_XCVR_RFCLK)
     uint32_t os_tmo;
 
     /* Make sure HFXO is started */
@@ -631,6 +836,7 @@ ble_phy_init(void)
             return BLE_PHY_ERR_INIT;
         }
     }
+#endif
 
     /* Set phy channel to an invalid channel so first set channel works */
     g_ble_phy_data.phy_chan = BLE_PHY_NUM_CHANS;
@@ -665,8 +871,13 @@ ble_phy_init(void)
     /* Configure IFS */
     NRF_RADIO->TIFS = BLE_LL_IFS;
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /* Captures tx/rx start in timer0 cc 1 and tx/rx end in timer0 cc 2 */
+    NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk | PPI_CHEN_CH27_Msk;
+#else
     /* Captures tx/rx start in timer0 capture 1 */
     NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk;
+#endif
 
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     NRF_CCM->INTENCLR = 0xffffffff;
@@ -685,6 +896,27 @@ ble_phy_init(void)
     NRF_AAR->NIRK = 0;
 #endif
 
+    /* TIMER0 setup for PHY when using RTC */
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+    NRF_TIMER0->TASKS_STOP = 1;
+    NRF_TIMER0->TASKS_SHUTDOWN = 1;
+    NRF_TIMER0->BITMODE = 3;    /* 32-bit timer */
+    NRF_TIMER0->MODE = 0;       /* Timer mode */
+    NRF_TIMER0->PRESCALER = 4;  /* gives us 1 MHz */
+
+    /*
+     * PPI setup.
+     * Channel 4: Captures TIMER0 in CC[3] when EVENTS_ADDRESS occurs. Used
+     *            to cancel the wait for response timer.
+     * Channel 5: TIMER0 CC[3] to TASKS_DISABLE on radio. This is the wait
+     *            for response timer.
+     */
+    NRF_PPI->CH[4].EEP = (uint32_t)&(NRF_RADIO->EVENTS_ADDRESS);
+    NRF_PPI->CH[4].TEP = (uint32_t)&(NRF_TIMER0->TASKS_CAPTURE[3]);
+    NRF_PPI->CH[5].EEP = (uint32_t)&(NRF_TIMER0->EVENTS_COMPARE[3]);
+    NRF_PPI->CH[5].TEP = (uint32_t)&(NRF_RADIO->TASKS_DISABLE);
+#endif
+
     /* Set isr in vector table and enable interrupt */
     NVIC_SetPriority(RADIO_IRQn, 0);
     NVIC_SetVector(RADIO_IRQn, (uint32_t)ble_phy_isr);
@@ -803,6 +1035,7 @@ ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
     g_ble_phy_data.txend_arg = arg;
 }
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
 /**
  * Called to set the start time of a transmission.
  *
@@ -812,28 +1045,38 @@ ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
  * NOTE: care must be taken when calling this function. The channel should
  * already be set.
  *
- * @param cputime
- *
+ * @param cputime   This is the tick at which the 1st bit of the preamble
+ *                  should be transmitted
+ * @param rem_usecs This is used only when the underlying timing uses a 32.768
+ *                  kHz crystal. It is the # of usecs from the cputime tick
+ *                  at which the first bit of the preamble should be
+ *                  transmitted.
  * @return int
  */
 int
-ble_phy_tx_set_start_time(uint32_t cputime)
+ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
 {
     int rc;
 
-    NRF_TIMER0->CC[0] = cputime;
-    NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
+    /* XXX: This should not be necessary, but paranoia is good! */
+    /* Clear timer0 compare to RXEN since we are transmitting */
     NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-    if ((int32_t)(os_cputime_get32() - cputime) >= 0) {
+
+    /*
+     * XXX: The TXEN time is 140 usecs but there may be additional delays
+     * Need to look at this.
+     */
+    if (ble_phy_set_start_time(cputime, rem_usecs) != 0) {
         STATS_INC(ble_phy_stats, tx_late);
         ble_phy_disable();
-        rc =  BLE_PHY_ERR_TX_LATE;
+        rc = BLE_PHY_ERR_TX_LATE;
     } else {
+        /* Enable PPI to automatically start TXEN */
+        NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
         rc = 0;
     }
     return rc;
 }
-
 /**
  * Called to set the start time of a reception
  *
@@ -848,23 +1091,91 @@ ble_phy_tx_set_start_time(uint32_t cputime)
  * @return int
  */
 int
-ble_phy_rx_set_start_time(uint32_t cputime)
+ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
 {
     int rc;
 
+    /* XXX: This should not be necessary, but paranoia is good! */
+    /* Clear timer0 compare to TXEN since we are transmitting */
+    NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
+
+    /*
+     * XXX: The RXEN time is 138 usecs but there may be additional delays
+     * Need to look at this.
+     */
+    if (ble_phy_set_start_time(cputime, rem_usecs) != 0) {
+        STATS_INC(ble_phy_stats, rx_late);
+        NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
+        NRF_RADIO->TASKS_RXEN = 1;
+        rc = BLE_PHY_ERR_RX_LATE;
+    } else {
+        /* Enable PPI to automatically start RXEN */
+        NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk;
+
+        /* Start rx */
+        rc = ble_phy_rx();
+    }
+    return rc;
+}
+#else
+/**
+ * Called to set the start time of a transmission.
+ *
+ * This function is called to set the start time when we are not going from
+ * rx to tx automatically.
+ *
+ * NOTE: care must be taken when calling this function. The channel should
+ * already be set.
+ *
+ * @param cputime   This is the tick at which the 1st bit of the preamble
+ *                  should be transmitted
+ * @return int
+ */
+int
+ble_phy_tx_set_start_time(uint32_t cputime)
+{
+    int rc;
+
+    cputime -= os_cputime_usecs_to_ticks(XCVR_TX_START_DELAY_USECS);
+    NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
     NRF_TIMER0->CC[0] = cputime;
+    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
+    NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
+    if ((int32_t)(os_cputime_get32() - cputime) >= 0) {
+        STATS_INC(ble_phy_stats, tx_late);
+        ble_phy_disable();
+        rc = BLE_PHY_ERR_TX_LATE;
+    } else {
+        rc = 0;
+    }
+
+    return rc;
+}
+
+#if 0
+int
+ble_phy_rx_set_start_time(void)
+{
+    /*
+     * XXX: For now, we dont use this function if we are not using the
+     * RTC. Keeping the code around just in case we want to use it later.
+     */
     NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
+    NRF_TIMER0->CC[0] = cputime;
+    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
     NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk;
     if ((int32_t)(os_cputime_get32() - cputime) >= 0) {
         STATS_INC(ble_phy_stats, rx_late);
         NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
         NRF_RADIO->TASKS_RXEN = 1;
-        rc =  BLE_PHY_ERR_TX_LATE;
+        rc =  BLE_PHY_ERR_RX_LATE;
     } else {
         rc = 0;
     }
     return rc;
 }
+#endif
+#endif
 
 int
 ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
@@ -888,6 +1199,16 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
     ble_hdr = BLE_MBUF_HDR_PTR(txpdu);
     payload_len = ble_hdr->txinfo.pyld_len;
 
+    /*
+     * XXX: Although we may not have to do this here, I clear all the PPI
+     * that should not be used when transmitting. Some of them are only enabled
+     * if encryption and/or privacy is on, but I dont care. Better to be
+     * paranoid, and if you are going to clear one, might as well clear them
+     * all.
+     */
+    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH23_Msk |
+                       PPI_CHEN_CH25_Msk;
+
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (g_ble_phy_data.phy_encrypted) {
         /* RAM representation has S0, LENGTH and S1 fields. (3 bytes) */
@@ -904,15 +1225,12 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
         NRF_CCM->EVENTS_ERROR = 0;
         NRF_CCM->MODE = CCM_MODE_MODE_Encryption;
         NRF_CCM->CNFPTR = (uint32_t)&g_nrf_ccm_data;
-        NRF_PPI->CHENCLR = PPI_CHEN_CH25_Msk | PPI_CHEN_CH23_Msk;
         NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk;
     } else {
-
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         /* Reconfigure PCNF0 */
         NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
                            (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-        NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk;
         NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
 #endif
         /* RAM representation has S0 and LENGTH fields (2 bytes) */
@@ -927,7 +1245,6 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
     /* Reconfigure PCNF0 */
     NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
                        (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-    NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk;
 #endif
 
     /* RAM representation has S0 and LENGTH fields (2 bytes) */
@@ -1106,29 +1423,62 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
     return 0;
 }
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
 /**
- * Disable the PHY. This will do the following:
- *  -> Turn off all phy interrupts.
- *  -> Disable internal shortcuts.
- *  -> Disable the radio.
- *  -> Make sure we wont automatically go to rx/tx on output compare
- *  -> Sets phy state to idle.
- *  -> Clears any pending irqs in the NVIC. Might not be necessary but we do
- *  it as a precaution.
+ * Stop the timer used to count microseconds when using RTC for cputime
  */
 void
-ble_phy_disable(void)
+ble_phy_stop_usec_timer(void)
 {
-    ble_ll_log(BLE_LL_LOG_ID_PHY_DISABLE, g_ble_phy_data.phy_state, 0, 0);
+    NRF_TIMER0->TASKS_STOP = 1;
+    NRF_TIMER0->TASKS_SHUTDOWN = 1;
+    NRF_RTC0->EVTENCLR = RTC_EVTENSET_COMPARE0_Msk;
+}
+#endif
 
+/**
+ * ble phy disable irq and ppi
+ *
+ * This routine is to be called when reception was stopped due to either a
+ * wait for response timeout or a packet being received and the phy is to be
+ * restarted in receive mode. Generally, the disable routine is called to stop
+ * the phy.
+ */
+void
+ble_phy_disable_irq_and_ppi(void)
+{
     NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
     NRF_RADIO->SHORTS = 0;
     NRF_RADIO->TASKS_DISABLE = 1;
-    NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk | PPI_CHEN_CH21_Msk | PPI_CHEN_CH20_Msk;
+    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH20_Msk |
+          PPI_CHEN_CH21_Msk | PPI_CHEN_CH23_Msk | PPI_CHEN_CH24_Msk |
+          PPI_CHEN_CH25_Msk | PPI_CHEN_CH31_Msk;
     NVIC_ClearPendingIRQ(RADIO_IRQn);
     g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
 }
 
+void
+ble_phy_restart_rx(void)
+{
+    ble_phy_disable_irq_and_ppi();
+    ble_phy_rx();
+}
+
+/**
+ * ble phy disable
+ *
+ * Disables the PHY. This should be called when an event is over. It stops
+ * the usec timer (if used), disables interrupts, disables the RADIO, disables
+ * PPI and sets state to idle.
+ */
+void
+ble_phy_disable(void)
+{
+    ble_ll_log(BLE_LL_LOG_ID_PHY_DISABLE, g_ble_phy_data.phy_state, 0, 0);
+    ble_phy_stop_usec_timer();
+    ble_phy_disable_irq_and_ppi();
+}
+
 /* Gets the current access address */
 uint32_t ble_phy_access_addr_get(void)
 {
@@ -1201,3 +1551,17 @@ ble_phy_resolv_list_disable(void)
     g_ble_phy_data.phy_privacy = 0;
 }
 #endif
+
+#ifdef BLE_XCVR_RFCLK
+void
+ble_phy_rfclk_enable(void)
+{
+    NRF_CLOCK->TASKS_HFCLKSTART = 1;
+}
+
+void
+ble_phy_rfclk_disable(void)
+{
+    NRF_CLOCK->TASKS_HFCLKSTOP = 1;
+}
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3c785953/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 8d8b0e1..e6b470e 100644
--- a/hw/drivers/nimble/nrf52/src/ble_phy.c
+++ b/hw/drivers/nimble/nrf52/src/ble_phy.c
@@ -694,6 +694,10 @@ ble_phy_rx_start_isr(void)
     usecs = NRF_TIMER0->CC[1] - 40;
     ticks = os_cputime_usecs_to_ticks(usecs);
     ble_hdr->rem_usecs = usecs - os_cputime_ticks_to_usecs(ticks);
+    if (ble_hdr->rem_usecs == 31) {
+        ble_hdr->rem_usecs = 0;
+        ++ticks;
+    }
     ble_hdr->beg_cputime = g_ble_phy_data.phy_start_cputime + ticks;
 #else
     ble_hdr->beg_cputime = NRF_TIMER0->CC[1] -
@@ -1152,6 +1156,7 @@ ble_phy_rx_set_start_time(void)
     } else {
         rc = 0;
     }
+    return rc;
 }
 #endif
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3c785953/kernel/os/src/os_cputime.c
----------------------------------------------------------------------
diff --git a/kernel/os/src/os_cputime.c b/kernel/os/src/os_cputime.c
index 01b15fe..56b626c 100644
--- a/kernel/os/src/os_cputime.c
+++ b/kernel/os/src/os_cputime.c
@@ -122,8 +122,17 @@ os_cputime_usecs_to_ticks(uint32_t usecs)
 {
     uint64_t ticks;
 
-    ticks = ((uint64_t)usecs << 9) / 15625;
-    return (uint32_t)ticks;
+    /*
+     * Faster calculation but could be off 1 full tick since we do not
+     * add residual back. Adding back the residual is commented out below, but
+     * shown.
+     */
+    ticks = (uint64_t)usecs * (uint32_t)((((uint64_t)1 << 32) * 32768) / 1000000);
+
+    /* Residual */
+    //ticks += ((uint64_t)us * (1526122139+1)) >> 32;
+
+    return (uint32_t)(ticks >> 32);
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3c785953/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
index acc201d..2094290 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -425,7 +425,7 @@ int ble_ll_rand_start(void);
  * XXX: temporary LL debug log. Will get removed once we transition to real
  * log
  */
-#define BLE_LL_LOG
+#undef BLE_LL_LOG
 #include "console/console.h"
 
 #define BLE_LL_LOG_ID_PHY_SETCHAN       (1)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3c785953/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 d7f926e..9a6dc1a 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -242,9 +242,13 @@ ble_ll_conn_calc_itvl_ticks(struct ble_ll_conn_sm *connsm)
      */
     usecs = connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS;
     ticks = os_cputime_usecs_to_ticks(usecs);
-    connsm->conn_itvl_ticks = ticks;
     connsm->conn_itvl_usecs = (uint8_t)(usecs -
                                         os_cputime_ticks_to_usecs(ticks));
+    if (connsm->conn_itvl_usecs == 31) {
+        connsm->conn_itvl_usecs = 0;
+        ++ticks;
+    }
+    connsm->conn_itvl_ticks = ticks;
 }
 #endif
 


[16/37] incubator-mynewt-core git commit: BLE UART xport - sysinit panic on init fail.

Posted by we...@apache.org.
BLE UART xport - sysinit panic on init fail.


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

Branch: refs/heads/nrf_cputime
Commit: c44b4adeae797ec89f32fe821f062a7f27ee33be
Parents: e51ce9d
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 16:38:19 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 .../uart/include/transport/uart/ble_hci_uart.h  |  2 +-
 net/nimble/transport/uart/src/ble_hci_uart.c    | 60 +++-----------------
 2 files changed, 9 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44b4ade/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h b/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
index d54fd66..e5e1084 100644
--- a/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
+++ b/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
@@ -24,7 +24,7 @@
 extern "C" {
 #endif
 
-int ble_hci_uart_init(void);
+void ble_hci_uart_init(void);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44b4ade/net/nimble/transport/uart/src/ble_hci_uart.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/src/ble_hci_uart.c b/net/nimble/transport/uart/src/ble_hci_uart.c
index 402c5a1..b1ffd8a 100755
--- a/net/nimble/transport/uart/src/ble_hci_uart.c
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@ -685,25 +685,6 @@ ble_hci_uart_free_pkt(uint8_t type, uint8_t *cmdevt, struct os_mbuf *acl)
     }
 }
 
-static void
-ble_hci_uart_free_mem(void)
-{
-    free(ble_hci_uart_evt_hi_buf);
-    ble_hci_uart_evt_hi_buf = NULL;
-
-    free(ble_hci_uart_evt_lo_buf);
-    ble_hci_uart_evt_lo_buf = NULL;
-
-    free(ble_hci_uart_pkt_buf);
-    ble_hci_uart_pkt_buf = NULL;
-
-    free(ble_hci_uart_acl_buf);
-    ble_hci_uart_acl_buf = NULL;
-
-    free(ble_hci_uart_cmd_buf);
-    ble_hci_uart_cmd_buf = NULL;
-}
-
 static int
 ble_hci_uart_config(void)
 {
@@ -967,7 +948,7 @@ ble_hci_trans_reset(void)
  * @return                      0 on success;
  *                              A BLE_ERR_[...] error code on failure.
  */
-int
+void
 ble_hci_uart_init(void)
 {
     int acl_data_length;
@@ -977,8 +958,6 @@ ble_hci_uart_init(void)
     /* Ensure this function only gets called by sysinit. */
     SYSINIT_ASSERT_ACTIVE();
 
-    ble_hci_uart_free_mem();
-
     /*
      * The MBUF payload size must accommodate the HCI data header size plus the
      * maximum ACL data packet length. The ACL block size is the size of the
@@ -994,14 +973,11 @@ ble_hci_uart_init(void)
                             acl_block_size,
                             "ble_hci_uart_acl_pool",
                             &ble_hci_uart_acl_buf);
-    if (rc != 0) {
-        rc = ble_err_from_os(rc);
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = os_mbuf_pool_init(&ble_hci_uart_acl_mbuf_pool, &ble_hci_uart_acl_pool,
                            acl_block_size, MYNEWT_VAL(BLE_ACL_BUF_COUNT));
-    assert(rc == 0);
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     /*
      * Create memory pool of HCI command buffers. NOTE: we currently dont
@@ -1014,30 +990,21 @@ ble_hci_uart_init(void)
                             BLE_HCI_TRANS_CMD_SZ,
                             "ble_hci_uart_cmd_pool",
                             &ble_hci_uart_cmd_buf);
-    if (rc != 0) {
-        rc = ble_err_from_os(rc);
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = mem_malloc_mempool(&ble_hci_uart_evt_hi_pool,
                             MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
                             MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
                             "ble_hci_uart_evt_hi_pool",
                             &ble_hci_uart_evt_hi_buf);
-    if (rc != 0) {
-        rc = ble_err_from_os(rc);
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = mem_malloc_mempool(&ble_hci_uart_evt_lo_pool,
                             MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
                             MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
                             "ble_hci_uart_evt_lo_pool",
                             &ble_hci_uart_evt_lo_buf);
-    if (rc != 0) {
-        rc = ble_err_from_os(rc);
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     /*
      * Create memory pool of packet list nodes. NOTE: the number of these
@@ -1051,22 +1018,11 @@ ble_hci_uart_init(void)
                             sizeof (struct ble_hci_uart_pkt),
                             "ble_hci_uart_pkt_pool",
                             &ble_hci_uart_pkt_buf);
-    if (rc != 0) {
-        rc = ble_err_from_os(rc);
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_hci_uart_config();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     memset(&ble_hci_uart_state, 0, sizeof ble_hci_uart_state);
     STAILQ_INIT(&ble_hci_uart_state.tx_pkts);
-
-    return 0;
-
-err:
-    ble_hci_uart_free_mem();
-    return rc;
 }


[02/37] incubator-mynewt-core git commit: net/oic; when deleting resource, remove it from oc_app_resources list.

Posted by we...@apache.org.
net/oic; when deleting resource, remove it from oc_app_resources
list.


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

Branch: refs/heads/nrf_cputime
Commit: 0df95ce1f7734aed927b68f7df7dcf1edd7ba603
Parents: cebfd2e
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Sun Mar 26 16:22:52 2017 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Sun Mar 26 16:22:52 2017 -0700

----------------------------------------------------------------------
 net/oic/src/api/oc_ri.c | 8 ++++++++
 1 file changed, 8 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0df95ce1/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 d6180a6..d5779e5 100644
--- a/net/oic/src/api/oc_ri.c
+++ b/net/oic/src/api/oc_ri.c
@@ -251,6 +251,14 @@ oc_ri_alloc_resource(void)
 void
 oc_ri_delete_resource(oc_resource_t *resource)
 {
+    oc_resource_t *tmp;
+
+    SLIST_FOREACH(tmp, &oc_app_resources, next) {
+        if (tmp == resource) {
+            SLIST_REMOVE(&oc_app_resources, tmp, oc_resource, next);
+            break;
+        }
+    }
     os_memblock_put(&oc_resource_pool, resource);
 }
 


[23/37] incubator-mynewt-core git commit: ocd_sample - Fix broken build.

Posted by we...@apache.org.
ocd_sample - Fix broken build.


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

Branch: refs/heads/nrf_cputime
Commit: 0eed22ffd01c5f62b85728b4032e3b132c940bf0
Parents: 54086ac
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 17:31:48 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 17:31:48 2017 -0700

----------------------------------------------------------------------
 apps/ocf_sample/src/ocf_ble.c | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0eed22ff/apps/ocf_sample/src/ocf_ble.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/ocf_ble.c b/apps/ocf_sample/src/ocf_ble.c
index 3fe3f9a..d93bdd9 100644
--- a/apps/ocf_sample/src/ocf_ble.c
+++ b/apps/ocf_sample/src/ocf_ble.c
@@ -74,17 +74,17 @@ static void
 ocf_ble_print_conn_desc(struct ble_gap_conn_desc *desc)
 {
     OCF_BLE_LOG(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=",
-                desc->conn_handle, desc->our_ota_addr_type);
-    print_addr(desc->our_ota_addr);
+                desc->conn_handle, desc->our_ota_addr.type);
+    print_addr(desc->our_ota_addr.val);
     OCF_BLE_LOG(INFO, " our_id_addr_type=%d our_id_addr=",
-                desc->our_id_addr_type);
-    print_addr(desc->our_id_addr);
+                desc->our_id_addr.type);
+    print_addr(desc->our_id_addr.val);
     OCF_BLE_LOG(INFO, " peer_ota_addr_type=%d peer_ota_addr=",
-                desc->peer_ota_addr_type);
-    print_addr(desc->peer_ota_addr);
+                desc->peer_ota_addr.type);
+    print_addr(desc->peer_ota_addr.val);
     OCF_BLE_LOG(INFO, " peer_id_addr_type=%d peer_id_addr=",
-                desc->peer_id_addr_type);
-    print_addr(desc->peer_id_addr);
+                desc->peer_id_addr.type);
+    print_addr(desc->peer_id_addr.val);
     OCF_BLE_LOG(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
                 "encrypted=%d authenticated=%d bonded=%d\n",
                 desc->conn_itvl, desc->conn_latency,
@@ -145,7 +145,7 @@ ocf_ble_advertise(void)
     memset(&adv_params, 0, sizeof adv_params);
     adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
     adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
-    rc = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, 0, NULL, BLE_HS_FOREVER,
+    rc = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER,
                            &adv_params, ocf_ble_gap_event, NULL);
     if (rc != 0) {
         OCF_BLE_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc);
@@ -257,22 +257,12 @@ static const uint8_t ocf_ble_addr[6] = {1,2,3,4,5,6};
 void
 ocf_ble_init(void)
 {
-    int rc;
-
     /* Initialize the ocf_ble log. */
     log_register("ocf_ble", &ocf_ble_log, &log_console_handler, NULL,
                  LOG_SYSLEVEL);
 
     memcpy(g_dev_addr, ocf_ble_addr, sizeof(g_dev_addr));
 
-    /* COAP Gatt server initialization */
-    rc = ble_coap_gatt_srv_init();
-    assert(rc == 0);
-
-    /* Set the default device name. */
-    rc = ble_svc_gap_device_name_set("Mynewt_OCF");
-    assert(rc == 0);
-
     /* Initialize the BLE host. */
     log_register("ble_hs", &ble_hs_log, &log_console_handler, NULL,
                  LOG_SYSLEVEL);


[32/37] incubator-mynewt-core git commit: MYNEWT-700 BLE Host - Race condition: discon + att

Posted by we...@apache.org.
MYNEWT-700 BLE Host - Race condition: discon + att

Some parts of the ATT code assume a peer is still connected after an
initial check. This assumption leads to a race condition when a task
other than the host task is doing the transmitting (e.g., tx of
unsolicited notification). It is possible that the peer gets
disconnected after the tx function is called, but before it completes.
When this occurs, an assertion fails (ble_att_conn_chan_find()).

The fix is to remove this assumption. Always check that the connection /
channel lookup succeeds before accessing the returned pointers.


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

Branch: refs/heads/nrf_cputime
Commit: 9b6c2847d4fef37e2cbe47b5dfc4b85555c8e3a7
Parents: 612f8ef
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Mar 30 12:09:48 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Mar 30 12:10:49 2017 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att.c               | 11 ++--
 net/nimble/host/src/ble_att_clt.c           | 26 +++++---
 net/nimble/host/src/ble_att_priv.h          |  4 +-
 net/nimble/host/src/ble_att_svr.c           | 82 ++++++++++++++----------
 net/nimble/host/src/ble_gattc.c             | 10 ++-
 net/nimble/host/test/src/ble_att_svr_test.c |  6 +-
 net/nimble/host/test/src/ble_hs_test_util.c |  6 +-
 7 files changed, 91 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b6c2847/net/nimble/host/src/ble_att.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att.c b/net/nimble/host/src/ble_att.c
index 0eaed34..1756fc8 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -143,12 +143,12 @@ ble_att_rx_dispatch_entry_find(uint8_t op)
     return NULL;
 }
 
-void
+int
 ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
                        struct ble_l2cap_chan **out_chan)
 {
-    ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_ATT,
-                                    out_conn, out_chan);
+    return ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
+                                      out_conn, out_chan);
 }
 
 void
@@ -414,11 +414,12 @@ ble_att_mtu(uint16_t conn_handle)
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
     uint16_t mtu;
+    int rc;
 
     ble_hs_lock();
 
-    ble_att_conn_chan_find(conn_handle, &conn, &chan);
-    if (chan != NULL) {
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
         mtu = ble_att_chan_mtu(chan);
     } else {
         mtu = 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b6c2847/net/nimble/host/src/ble_att_clt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_clt.c b/net/nimble/host/src/ble_att_clt.c
index f310a6f..8c92893 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -70,8 +70,8 @@ ble_att_clt_tx_req(uint16_t conn_handle, struct os_mbuf *txom)
 
     ble_hs_lock();
 
-    ble_att_conn_chan_find(conn_handle, &conn, &chan);
-    if (chan == NULL) {
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc != 0) {
         rc = BLE_HS_ENOTCONN;
     } else {
         ble_att_truncate_to_mtu(chan, txom);
@@ -129,8 +129,8 @@ ble_att_clt_tx_mtu(uint16_t conn_handle, const struct ble_att_mtu_cmd *req)
 
     ble_hs_lock();
 
-    ble_att_conn_chan_find(conn_handle, &conn, &chan);
-    if (chan == NULL) {
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc != 0) {
         rc = BLE_HS_ENOTCONN;
     } else if (chan->flags & BLE_L2CAP_CHAN_F_TXED_MTU) {
         rc = BLE_HS_EALREADY;
@@ -158,8 +158,10 @@ ble_att_clt_tx_mtu(uint16_t conn_handle, const struct ble_att_mtu_cmd *req)
 
     ble_hs_lock();
 
-    ble_att_conn_chan_find(conn_handle, &conn, &chan);
-    chan->flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        chan->flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
+    }
 
     ble_hs_unlock();
 
@@ -183,13 +185,17 @@ ble_att_clt_rx_mtu(uint16_t conn_handle, struct os_mbuf **rxom)
 
         ble_hs_lock();
 
-        ble_att_conn_chan_find(conn_handle, NULL, &chan);
-        ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
-        mtu = ble_att_chan_mtu(chan);
+        rc = ble_att_conn_chan_find(conn_handle, NULL, &chan);
+        if (rc == 0) {
+            ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
+            mtu = ble_att_chan_mtu(chan);
+        }
 
         ble_hs_unlock();
 
-        ble_gap_mtu_event(conn_handle, BLE_L2CAP_CID_ATT, mtu);
+        if (rc == 0) {
+            ble_gap_mtu_event(conn_handle, BLE_L2CAP_CID_ATT, mtu);
+        }
     }
 
     ble_gattc_rx_mtu(conn_handle, rc, mtu);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b6c2847/net/nimble/host/src/ble_att_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_priv.h b/net/nimble/host/src/ble_att_priv.h
index af05684..918ca9b 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -160,8 +160,8 @@ SLIST_HEAD(ble_att_clt_entry_list, ble_att_clt_entry);
 /*** @gen */
 
 struct ble_l2cap_chan *ble_att_create_chan(uint16_t conn_handle);
-void ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
-                            struct ble_l2cap_chan **out_chan);
+int ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
+                           struct ble_l2cap_chan **out_chan);
 void ble_att_inc_tx_stat(uint8_t att_op);
 void ble_att_truncate_to_mtu(const struct ble_l2cap_chan *att_chan,
                              struct os_mbuf *txom);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b6c2847/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c b/net/nimble/host/src/ble_att_svr.c
index 1c2d4a2..1775292 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -619,7 +619,7 @@ err:
  * sent instead.
  *
  * @param conn_handle           The handle of the connection to send over.
- * @param rc                    The status indicating whether to transmit an
+ * @param hs_status             The status indicating whether to transmit an
  *                                  affirmative response or an error.
  * @param txom                  Contains the affirmative response payload.
  * @param att_op                If an error is transmitted, this is the value
@@ -631,14 +631,15 @@ err:
  *                                  field.
  */
 static int
-ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf *om,
+ble_att_svr_tx_rsp(uint16_t conn_handle, int hs_status, struct os_mbuf *om,
                    uint8_t att_op, uint8_t err_status, uint16_t err_handle)
 {
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
     int do_tx;
+    int rc;
 
-    if (rc != 0 && err_status == 0) {
+    if (hs_status != 0 && err_status == 0) {
         /* Processing failed, but err_status of 0 means don't send error. */
         do_tx = 0;
     } else {
@@ -648,32 +649,37 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf *om,
     if (do_tx) {
         ble_hs_lock();
 
-        ble_att_conn_chan_find(conn_handle, &conn, &chan);
-        if (rc == 0) {
-            BLE_HS_DBG_ASSERT(om != NULL);
+        rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+        if (rc != 0) {
+            /* No longer connected. */
+            hs_status = rc;
+        } else {
+            if (hs_status == 0) {
+                BLE_HS_DBG_ASSERT(om != NULL);
 
-            ble_att_inc_tx_stat(om->om_data[0]);
-            ble_att_truncate_to_mtu(chan, om);
-            rc = ble_l2cap_tx(conn, chan, om);
-            om = NULL;
-            if (rc != 0) {
-                err_status = BLE_ATT_ERR_UNLIKELY;
+                ble_att_inc_tx_stat(om->om_data[0]);
+                ble_att_truncate_to_mtu(chan, om);
+                hs_status = ble_l2cap_tx(conn, chan, om);
+                om = NULL;
+                if (hs_status != 0) {
+                    err_status = BLE_ATT_ERR_UNLIKELY;
+                }
             }
-        }
 
-        if (rc != 0) {
-            STATS_INC(ble_att_stats, error_rsp_tx);
+            if (hs_status != 0) {
+                STATS_INC(ble_att_stats, error_rsp_tx);
 
-            /* Reuse om for error response. */
-            if (om == NULL) {
-                om = ble_hs_mbuf_l2cap_pkt();
-            } else {
-                os_mbuf_adj(om, OS_MBUF_PKTLEN(om));
-            }
-            if (om != NULL) {
-                ble_att_svr_tx_error_rsp(conn, chan, om, att_op,
-                                         err_handle, err_status);
-                om = NULL;
+                /* Reuse om for error response. */
+                if (om == NULL) {
+                    om = ble_hs_mbuf_l2cap_pkt();
+                } else {
+                    os_mbuf_adj(om, OS_MBUF_PKTLEN(om));
+                }
+                if (om != NULL) {
+                    ble_att_svr_tx_error_rsp(conn, chan, om, att_op,
+                                             err_handle, err_status);
+                    om = NULL;
+                }
             }
         }
 
@@ -683,7 +689,7 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf *om,
     /* Free mbuf if it was not consumed (i.e., if the send failed). */
     os_mbuf_free_chain(om);
 
-    return rc;
+    return hs_status;
 }
 
 static int
@@ -701,10 +707,16 @@ ble_att_svr_build_mtu_rsp(uint16_t conn_handle, struct os_mbuf **rxom,
     txom = NULL;
 
     ble_hs_lock();
-    ble_att_conn_chan_find(conn_handle, NULL, &chan);
-    mtu = chan->my_mtu;
+    rc = ble_att_conn_chan_find(conn_handle, NULL, &chan);
+    if (rc == 0) {
+        mtu = chan->my_mtu;
+    }
     ble_hs_unlock();
 
+    if (rc != 0) {
+        goto done;
+    }
+
     /* Just reuse the request buffer for the response. */
     txom = *rxom;
     *rxom = NULL;
@@ -763,14 +775,18 @@ done:
     if (rc == 0) {
         ble_hs_lock();
 
-        ble_att_conn_chan_find(conn_handle, &conn, &chan);
-        ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
-        chan->flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
-        mtu = ble_att_chan_mtu(chan);
+        rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+        if (rc == 0) {
+            ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
+            chan->flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
+            mtu = ble_att_chan_mtu(chan);
+        }
 
         ble_hs_unlock();
 
-        ble_gap_mtu_event(conn_handle, BLE_L2CAP_CID_ATT, mtu);
+        if (rc == 0) {
+            ble_gap_mtu_event(conn_handle, BLE_L2CAP_CID_ATT, mtu);
+        }
     }
     return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b6c2847/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 fabb52d..40f4c5c 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -1270,10 +1270,16 @@ ble_gattc_mtu_tx(struct ble_gattc_proc *proc)
     int rc;
 
     ble_hs_lock();
-    ble_att_conn_chan_find(proc->conn_handle, &conn, &chan);
-    req.bamc_mtu = chan->my_mtu;
+    rc = ble_att_conn_chan_find(proc->conn_handle, &conn, &chan);
+    if (rc == 0) {
+        req.bamc_mtu = chan->my_mtu;
+    }
     ble_hs_unlock();
 
+    if (rc != 0) {
+        return rc;
+    }
+
     rc = ble_att_clt_tx_mtu(proc->conn_handle, &req);
     if (rc != 0) {
         return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b6c2847/net/nimble/host/test/src/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_att_svr_test.c b/net/nimble/host/test/src/ble_att_svr_test.c
index b22e754..d25118e 100644
--- a/net/nimble/host/test/src/ble_att_svr_test.c
+++ b/net/nimble/host/test/src/ble_att_svr_test.c
@@ -449,10 +449,14 @@ ble_att_svr_test_misc_verify_tx_mtu_rsp(uint16_t conn_handle)
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
     uint16_t my_mtu;
+    int rc;
 
     ble_hs_lock();
-    ble_att_conn_chan_find(conn_handle, &conn, &chan);
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    assert(rc == 0);
     my_mtu = chan->my_mtu;
+
     ble_hs_unlock();
 
     ble_hs_test_util_verify_tx_mtu_cmd(0, my_mtu);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b6c2847/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.c b/net/nimble/host/test/src/ble_hs_test_util.c
index d084c39..26783ef 100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -994,16 +994,20 @@ ble_hs_test_util_set_att_mtu(uint16_t conn_handle, uint16_t mtu)
 {
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
+    int rc;
 
     if (mtu <= BLE_ATT_MTU_DFLT) {
         return;
     }
 
     ble_hs_lock();
-    ble_att_conn_chan_find(conn_handle, &conn, &chan);
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    assert(rc == 0);
     chan->my_mtu = mtu;
     chan->peer_mtu = mtu;
     chan->flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
+
     ble_hs_unlock();
 }
 


[19/37] incubator-mynewt-core git commit: sysinit - Allow panic message.

Posted by we...@apache.org.
sysinit - Allow panic message.


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

Branch: refs/heads/nrf_cputime
Commit: 103f39e8004d3908b7c39d19b2a8b82ba9b1c714
Parents: e679687
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 7 15:09:58 2017 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 hw/bsp/native/syscfg.yml              |  5 +++++
 sys/sysinit/include/sysinit/sysinit.h | 34 ++++++++++++++++++++++++------
 sys/sysinit/src/sysinit.c             | 17 ++++++++++++++-
 sys/sysinit/syscfg.yml                |  8 +++++++
 4 files changed, 56 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/103f39e8/hw/bsp/native/syscfg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/native/syscfg.yml b/hw/bsp/native/syscfg.yml
index 9ca0608..2d91f9c 100644
--- a/hw/bsp/native/syscfg.yml
+++ b/hw/bsp/native/syscfg.yml
@@ -18,7 +18,12 @@
 #
 
 syscfg.vals:
+    # Sim isn't flash constrained, so include filename, line number, and
+    # message in asserts and sysinit panic messages.
     BASELIBC_ASSERT_FILE_LINE: 1
+    SYSINIT_PANIC_FILE_LINE: 1
+    SYSINIT_PANIC_MESSAGE: 1
+
     NFFS_FLASH_AREA: FLASH_AREA_NFFS
     CONFIG_FCB_FLASH_AREA: FLASH_AREA_NFFS
     REBOOT_LOG_FLASH_AREA: FLASH_AREA_REBOOT_LOG

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/103f39e8/sys/sysinit/include/sysinit/sysinit.h
----------------------------------------------------------------------
diff --git a/sys/sysinit/include/sysinit/sysinit.h b/sys/sysinit/include/sysinit/sysinit.h
index 32df52b..7694c42 100644
--- a/sys/sysinit/include/sysinit/sysinit.h
+++ b/sys/sysinit/include/sysinit/sysinit.h
@@ -38,21 +38,41 @@ void sysinit_start(void);
 void sysinit_end(void);
 
 typedef void sysinit_panic_fn(const char *file, int line, const char *func,
-                              const char *expr);
+                              const char *expr, const char *msg);
 
 extern sysinit_panic_fn *sysinit_panic_cb;
 
 void sysinit_panic_set(sysinit_panic_fn *panic_fn);
 
-#define SYSINIT_PANIC() sysinit_panic_cb(NULL, 0, NULL, NULL)
+#if MYNEWT_VAL(SYSINIT_PANIC_MESSAGE)
 
-#define SYSINIT_PANIC_ASSERT(rc) do \
-{                                   \
-    if (!(rc)) {                    \
-        SYSINIT_PANIC();            \
-    }                               \
+#if MYNEWT_VAL(SYSINIT_PANIC_FILE_LINE)
+#define SYSINIT_PANIC_MSG(msg) sysinit_panic_cb(__FILE__, __LINE__, 0, 0, msg)
+#else
+#define SYSINIT_PANIC_MSG(msg) sysinit_panic_cb(0, 0, 0, 0, msg)
+#endif
+
+#else
+
+#if MYNEWT_VAL(SYSINIT_PANIC_FILE_LINE)
+#define SYSINIT_PANIC_MSG(msg) sysinit_panic_cb(__FILE__, __LINE__, 0, 0, 0)
+#else
+#define SYSINIT_PANIC_MSG(msg) sysinit_panic_cb(0, 0, 0, 0, 0)
+#endif
+
+#endif
+
+#define SYSINIT_PANIC() SYSINIT_PANIC_MSG(NULL)
+
+#define SYSINIT_PANIC_ASSERT_MSG(rc, msg) do \
+{                                            \
+    if (!(rc)) {                             \
+        SYSINIT_PANIC_MSG(msg);              \
+    }                                        \
 } while (0)
 
+#define SYSINIT_PANIC_ASSERT(rc) SYSINIT_PANIC_ASSERT_MSG(rc, NULL)
+
 /**
  * Asserts that system initialization is in progress.  This macro is used to
  * ensure packages don't get initialized a second time after system

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/103f39e8/sys/sysinit/src/sysinit.c
----------------------------------------------------------------------
diff --git a/sys/sysinit/src/sysinit.c b/sys/sysinit/src/sysinit.c
index 767032f..393d1c5 100644
--- a/sys/sysinit/src/sysinit.c
+++ b/sys/sysinit/src/sysinit.c
@@ -17,12 +17,27 @@
  * under the License.
  */
 
+#include <stdio.h>
 #include <stddef.h>
 #include <limits.h>
 #include "os/os_fault.h"
+#include "syscfg/syscfg.h"
 #include "sysinit/sysinit.h"
 
-sysinit_panic_fn *sysinit_panic_cb = __assert_func;
+static void
+sysinit_dflt_panic_cb(const char *file, int line, const char *func,
+                      const char *expr, const char *msg)
+{
+#if MYNEWT_VAL(SYSINIT_PANIC_MESSAGE)
+    if (msg != NULL) {
+        fprintf(stderr, "sysinit failure: %s\n", msg);
+    }
+#endif
+
+    __assert_func(file, line, func, expr);
+}
+
+sysinit_panic_fn *sysinit_panic_cb = sysinit_dflt_panic_cb;
 
 uint8_t sysinit_active;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/103f39e8/sys/sysinit/syscfg.yml
----------------------------------------------------------------------
diff --git a/sys/sysinit/syscfg.yml b/sys/sysinit/syscfg.yml
index 74dbc29..d6bac23 100644
--- a/sys/sysinit/syscfg.yml
+++ b/sys/sysinit/syscfg.yml
@@ -22,3 +22,11 @@ syscfg.defs:
     SYSINIT_CONSTRAIN_INIT:
         description: Only allow packages to be initialized by sysinit.
         value: 1
+
+    SYSINIT_PANIC_FILE_LINE:
+        description: Include filename and line number in sysinit panic.
+        value: 0
+
+    SYSINIT_PANIC_MESSAGE:
+        description: Include descriptive message in sysinit panic.
+        value: 0


[29/37] incubator-mynewt-core git commit: No jira ticket: fixed compilation error if the only timer allowed by syscfg was the RTC (TIMER_3).

Posted by we...@apache.org.
No jira ticket: fixed compilation error if the only timer
allowed by syscfg was the RTC (TIMER_3).


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

Branch: refs/heads/nrf_cputime
Commit: 28df4dd62b6b6f22f0c2893f3aefa65f1d2179ca
Parents: bf90105
Author: William San Filippo <wi...@runtime.io>
Authored: Wed Mar 29 20:20:07 2017 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Wed Mar 29 20:21:07 2017 -0700

----------------------------------------------------------------------
 hw/mcu/nordic/nrf51xxx/src/hal_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28df4dd6/hw/mcu/nordic/nrf51xxx/src/hal_timer.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf51xxx/src/hal_timer.c b/hw/mcu/nordic/nrf51xxx/src/hal_timer.c
index 1776406..f2f5957 100644
--- a/hw/mcu/nordic/nrf51xxx/src/hal_timer.c
+++ b/hw/mcu/nordic/nrf51xxx/src/hal_timer.c
@@ -365,7 +365,7 @@ hal_timer_chk_queue(struct nrf51_hal_timer *bsptimer)
  * This is the global timer interrupt routine.
  *
  */
-#if NRF51_TIMER_DEFINED
+#if (MYNEWT_VAL(TIMER_0) || MYNEWT_VAL(TIMER_1) || MYNEWT_VAL(TIMER_2))
 static void
 hal_timer_irq_handler(struct nrf51_hal_timer *bsptimer)
 {


[27/37] incubator-mynewt-core git commit: MYNEWT-696 NMP datetime rsp uses inconsistent fmt

Posted by we...@apache.org.
MYNEWT-696 NMP datetime rsp uses inconsistent fmt

If the time was set with a time zone, the datetime response includes the
time zone at the end of the string. Otherwise, no time zone is present.

This inconsistency makes parsing more difficult. The fix is to report
UTC as the time zone if none was explicitly specified.


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

Branch: refs/heads/nrf_cputime
Commit: cb97248bcd5c05bac0b8177095d0a638bea4221c
Parents: fdc3d2b
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 18:55:32 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 19:06:34 2017 -0700

----------------------------------------------------------------------
 time/datetime/src/datetime.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cb97248b/time/datetime/src/datetime.c
----------------------------------------------------------------------
diff --git a/time/datetime/src/datetime.c b/time/datetime/src/datetime.c
index f160d4b..229866a 100644
--- a/time/datetime/src/datetime.c
+++ b/time/datetime/src/datetime.c
@@ -405,7 +405,7 @@ datetime_format(const struct os_timeval *tv, const struct os_timezone *tz,
         minswest = 0;
     }
 
-    if (minswest < 0) {
+    if (minswest <= 0) {
         sign = '+';
         minswest = -minswest;
     } else {
@@ -414,14 +414,14 @@ datetime_format(const struct os_timeval *tv, const struct os_timezone *tz,
 
     off_hour = minswest / 60;
     off_min = minswest % 60;
-    if (off_hour || off_min) {
-        rc = snprintf(cp, rlen, "%c%02d:%02d", sign, off_hour, off_min);
-        cp += rc;
-        rlen -= rc;
-        if (rc < 0 || rlen <= 0) {
-            goto err;
-        }
+
+    rc = snprintf(cp, rlen, "%c%02d:%02d", sign, off_hour, off_min);
+    cp += rc;
+    rlen -= rc;
+    if (rc < 0 || rlen <= 0) {
+        goto err;
     }
+
     return (0);
 
 err:


[07/37] incubator-mynewt-core git commit: MYNEWT-688; disabling unused-result warning when compiling hw/mcu/native, and assigning return value from write() in os_fault.c:__assert_func().

Posted by we...@apache.org.
MYNEWT-688; disabling unused-result warning when compiling hw/mcu/native,
and assigning return value from write() in os_fault.c:__assert_func().


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

Branch: refs/heads/nrf_cputime
Commit: efbaf05f414e4e4076334ab676c47a6f5a5235b9
Parents: e62a245
Author: System Administrator <ma...@runtime.io>
Authored: Tue Mar 28 15:29:09 2017 -0700
Committer: System Administrator <ma...@runtime.io>
Committed: Tue Mar 28 15:29:09 2017 -0700

----------------------------------------------------------------------
 hw/mcu/native/pkg.yml             | 3 +++
 kernel/os/src/arch/sim/os_fault.c | 4 +++-
 2 files changed, 6 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/efbaf05f/hw/mcu/native/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/mcu/native/pkg.yml b/hw/mcu/native/pkg.yml
index 7094340..79df5a7 100644
--- a/hw/mcu/native/pkg.yml
+++ b/hw/mcu/native/pkg.yml
@@ -31,3 +31,6 @@ pkg.deps:
 
 pkg.req_apis:
     - console
+
+pkg.cflags:
+    - -Wno-unused-result

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/efbaf05f/kernel/os/src/arch/sim/os_fault.c
----------------------------------------------------------------------
diff --git a/kernel/os/src/arch/sim/os_fault.c b/kernel/os/src/arch/sim/os_fault.c
index 964202e..92d7aa0 100644
--- a/kernel/os/src/arch/sim/os_fault.c
+++ b/kernel/os/src/arch/sim/os_fault.c
@@ -27,6 +27,7 @@ void
 __assert_func(const char *file, int line, const char *func, const char *e)
 {
     char msg[256];
+    int len;
 
     if (file) {
         snprintf(msg, sizeof(msg), "assert @ %s:%d\n", file, line);
@@ -34,6 +35,7 @@ __assert_func(const char *file, int line, const char *func, const char *e)
         snprintf(msg, sizeof(msg), "assert @ %p\n",
                  __builtin_return_address(0));
     }
-    write(1, msg, strlen(msg));
+    len = write(1, msg, strlen(msg));
+    (void)len;
     _exit(1);
 }


[20/37] incubator-mynewt-core git commit: native uart: print errors to stderr.

Posted by we...@apache.org.
native uart: print errors to stderr.


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

Branch: refs/heads/nrf_cputime
Commit: eddcf8fa05e6c70ac4670e47c6ac90dcace1ccb6
Parents: 103f39e
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 7 17:19:04 2017 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 hw/mcu/native/src/native_uart_cfg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/eddcf8fa/hw/mcu/native/src/native_uart_cfg.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/native_uart_cfg.c b/hw/mcu/native/src/native_uart_cfg.c
index 4f9dcef..de6bd0b 100644
--- a/hw/mcu/native/src/native_uart_cfg.c
+++ b/hw/mcu/native/src/native_uart_cfg.c
@@ -225,7 +225,7 @@ uart_dev_set_attr(int fd, int32_t baudrate, uint8_t databits,
 
     rc = tcsetattr(fd, TCSANOW, &tty);
     if (rc != 0) {
-        dprintf(1, "tcsetattr failed; %d (%s)\n", errno, strerror(errno));
+        fprintf(stderr, "tcsetattr failed; %d (%s)\n", errno, strerror(errno));
         return -1;
     }
 


[30/37] incubator-mynewt-core git commit: MYNEWT-698: Unhandled exception in controller in ble_ll_conn_end

Posted by we...@apache.org.
MYNEWT-698: Unhandled exception in controller in ble_ll_conn_end

The controller was generating an unhandled exception inside the
function ble_ll_conn_end. This was occurring when the controller
was scheduling a new connection (master role) but no mbufs were
available to hand the received pdu to the link layer task. The
code attempts to schedule a master role connection and send a
connection request prior to allocating a receive buffer for the
received advertising pdu. It does this to save time since we need
all the time available from when the advertising pdu is received
until the time we need to send the connect request.

I am not terribly pleased with the fix since it is inefficient.
However, I wanted to get a fix in now. A better way might not be
to re-enable scanning or initiating unless a buffer is available
for receive but that change would be a bit more work.


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

Branch: refs/heads/nrf_cputime
Commit: 3538765730aa8aaa1b49fd4a2b3a263272bcff4f
Parents: 28df4dd
Author: William San Filippo <wi...@runtime.io>
Authored: Wed Mar 29 20:47:49 2017 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Wed Mar 29 20:47:49 2017 -0700

----------------------------------------------------------------------
 net/nimble/controller/src/ble_ll_conn.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/35387657/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 d8d3f8f..0ca72d0 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -2258,6 +2258,9 @@ ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
     struct os_mbuf *rxpdu;
     struct ble_ll_conn_sm *connsm;
 
+    /* Get connection state machine to use if connection to be established */
+    connsm = g_ble_ll_conn_create_sm;
+
     /*
      * We have to restart receive if we cant hand up pdu. We return 0 so that
      * the phy does not get disabled.
@@ -2268,9 +2271,6 @@ ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
         goto init_rx_isr_exit;
     }
 
-    /* Get connection state machine to use if connection to be established */
-    connsm = g_ble_ll_conn_create_sm;
-
     /* Only interested in ADV IND or ADV DIRECT IND */
     pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK;
     inita_is_rpa = 0;


[34/37] incubator-mynewt-core git commit: Low power timer. Only nrf52 right now.

Posted by we...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/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 dd42c3f..ffe8ef3 100644
--- a/net/nimble/controller/src/ble_ll_sched.c
+++ b/net/nimble/controller/src/ble_ll_sched.c
@@ -21,24 +21,35 @@
 #include <string.h>
 #include "os/os.h"
 #include "os/os_cputime.h"
+#include "bsp.h"
 #include "ble/xcvr.h"
 #include "controller/ble_phy.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_sched.h"
 #include "controller/ble_ll_adv.h"
 #include "controller/ble_ll_scan.h"
+#include "controller/ble_ll_xcvr.h"
 #include "ble_ll_conn_priv.h"
 
 /* XXX: this is temporary. Not sure what I want to do here */
 struct hal_timer g_ble_ll_sched_timer;
 
+#ifdef BLE_XCVR_RFCLK
+/* Settling time of crystal, in ticks */
+uint8_t g_ble_ll_sched_xtal_ticks;
+#endif
+
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+uint8_t g_ble_ll_sched_offset_ticks;
+#endif
+
 #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;
+int32_t g_ble_ll_sched_max_early;
 #endif
 
 /* XXX: TODO:
@@ -155,6 +166,17 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
     /* Get schedule element from connection */
     sch = &connsm->conn_sch;
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /* Set schedule start and end times */
+    sch->start_time = connsm->anchor_point - g_ble_ll_sched_offset_ticks;
+    if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
+        usecs = connsm->slave_cur_window_widening;
+        sch->start_time -= (os_cputime_usecs_to_ticks(usecs) + 1);
+        sch->remainder = 0;
+    } else {
+        sch->remainder = connsm->anchor_point_usecs;
+    }
+#else
     /* Set schedule start and end times */
     if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
         usecs = XCVR_RX_SCHED_DELAY_USECS;
@@ -163,6 +185,7 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
         usecs = XCVR_TX_SCHED_DELAY_USECS;
     }
     sch->start_time = connsm->anchor_point - os_cputime_usecs_to_ticks(usecs);
+#endif
     sch->end_time = connsm->ce_end_time;
 
     /* Better be past current time or we just leave */
@@ -236,8 +259,17 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
         entry = start_overlap;
     }
 
+#ifdef BLE_XCVR_RFCLK
+    entry = TAILQ_FIRST(&g_ble_ll_sched_q);
+    if (entry == sch) {
+        ble_ll_xcvr_rfclk_timer_start(sch->start_time);
+    } else {
+        sch = entry;
+    }
+#else
     /* Get first on list */
     sch = TAILQ_FIRST(&g_ble_ll_sched_q);
+#endif
 
     OS_EXIT_CRITICAL(sr);
 
@@ -247,67 +279,108 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
     return rc;
 }
 
+/**
+ * Called to schedule a connection when the current role is master.
+ *
+ * Context: Interrupt
+ *
+ * @param connsm
+ * @param ble_hdr
+ * @param pyld_len
+ *
+ * @return int
+ */
 int
-ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, uint32_t adv_rxend,
-                        uint8_t req_slots)
+ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm,
+                        struct ble_mbuf_hdr *ble_hdr, uint8_t pyld_len)
 {
     int rc;
     os_sr_t sr;
-    uint32_t tps;
+    uint8_t req_slots;
     uint32_t initial_start;
     uint32_t earliest_start;
     uint32_t earliest_end;
     uint32_t dur;
     uint32_t itvl_t;
-    uint32_t ce_end_time;
+    uint32_t adv_rxend;
     struct ble_ll_sched_item *entry;
     struct ble_ll_sched_item *sch;
 
-    /* Better have a connsm */
-    assert(connsm != NULL);
-
     /* Get schedule element from connection */
     rc = -1;
     sch = &connsm->conn_sch;
-
+    req_slots = MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS);
+
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /* XXX:
+     * The calculations for the 32kHz crystal bear alot of explanation. The
+     * earliest possible time that the master can start the connection with a
+     * slave is 1.25 msecs from the end of the connection request. The
+     * connection request is sent an IFS time from the end of the advertising
+     * packet that was received plus the time it takes to send the connection
+     * request. At 1 Mbps, this is 1752 usecs, or 57.41 ticks. Using 57 ticks
+     * makes us off ~13 usecs. Since we dont want to actually calculate the
+     * receive end time tick (this would take too long), we assume the end of
+     * the advertising PDU is 'now' (we call os_cputime_get32). We dont know
+     * how much time it will take to service the ISR but if we are more than the
+     * rx to tx time of the chip we will not be successful transmitting the
+     * connect request. All this means is that we presume that the slave will
+     * receive the connect request later than we expect but no earlier than
+     * 13 usecs before (this is important).
+     *
+     * The code then attempts to schedule the connection at the
+     * earliest time although this may not be possible. When the actual
+     * schedule start time is determined, the master has to determine if this
+     * time is more than a transmit window offset interval (1.25 msecs). The
+     * master has to tell the slave how many transmit window offsets there are
+     * from the earliest possible time to when the actual transmit start will
+     * occur. Later in this function you will see the calculation. The actual
+     * transmission start has to occur within the transmit window. The transmit
+     * window interval is in units of 1.25 msecs and has to be at least 1. To
+     * make things a bit easier (but less power efficient for the slave), we
+     * use a transmit window of 2. We do this because we dont quite know the
+     * exact start of the transmission and if we are too early or too late we
+     * could miss the transmit window. A final note: the actual transmission
+     * start (the anchor point) is sched offset ticks from the schedule start
+     * time. We dont add this to the calculation when calculating the window
+     * offset. The reason we dont do this is we want to insure we transmit
+     * after the window offset we tell the slave. For example, say we think
+     * we are transmitting 1253 usecs from the earliest start. This would cause
+     * us to send a transmit window offset of 1. Since we are actually
+     * transmitting earlier than the slave thinks we could end up transmitting
+     * before the window offset. Transmitting later is fine since we have the
+     * transmit window to do so. Transmitting before is bad, since the slave
+     * wont be listening. We could do better calculation if we wanted to use
+     * a transmit window of 1 as opposed to 2, but for now we dont care.
+     */
+    dur = req_slots * BLE_LL_SCHED_32KHZ_TICKS_PER_SLOT;
+    adv_rxend = os_cputime_get32();
+    earliest_start = adv_rxend + 57;    /* XXX: only works for 1 Mbps */
+    earliest_end = earliest_start + dur;
+    itvl_t = connsm->conn_itvl_ticks;
+#else
+    adv_rxend = ble_hdr->beg_cputime +
+        os_cputime_usecs_to_ticks(BLE_TX_DUR_USECS_M(pyld_len));
     /*
      * The earliest start time is 1.25 msecs from the end of the connect
      * request transmission. Note that adv_rxend is the end of the received
      * advertisement, so we need to add an IFS plus the time it takes to send
-     * the connection request
+     * the connection request. The 1.25 msecs starts from the end of the conn
+     * request.
      */
     dur = os_cputime_usecs_to_ticks(req_slots * BLE_LL_SCHED_USECS_PER_SLOT);
     earliest_start = adv_rxend +
         os_cputime_usecs_to_ticks(BLE_LL_IFS + BLE_LL_CONN_REQ_DURATION +
                                   BLE_LL_CONN_INITIAL_OFFSET);
     earliest_end = earliest_start + dur;
-
     itvl_t = os_cputime_usecs_to_ticks(connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS);
+#endif
 
     /* We have to find a place for this schedule */
     OS_ENTER_CRITICAL(sr);
 
     /* The schedule item must occur after current running item (if any) */
     sch->start_time = earliest_start;
-
-    /*
-     * 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) {
-        tps = os_cputime_usecs_to_ticks(BLE_LL_SCHED_USECS_PER_SLOT);
-        ce_end_time = ble_ll_conn_get_ce_end_time();
-        while ((int32_t)(ce_end_time - os_cputime_get32()) < 0) {
-            ce_end_time += tps;
-        }
-
-        /* Start at next slot boundary past earliest */
-        while ((int32_t)(ce_end_time - earliest_start) < 0) {
-            ce_end_time += tps;
-        }
-        earliest_start = ce_end_time;
-        earliest_end = earliest_start + dur;
-    }
     initial_start = earliest_start;
 
     if (!ble_ll_sched_insert_if_empty(sch)) {
@@ -338,6 +411,7 @@ ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, uint32_t adv_rxend,
             }
         }
 
+        /* Must be able to schedule within one connection interval */
         if (!entry) {
             if ((earliest_start - initial_start) <= itvl_t) {
                 rc = 0;
@@ -346,18 +420,38 @@ ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, uint32_t adv_rxend,
         }
 
         if (!rc) {
-            /* calculate number of connection intervals before start */
+            /* calculate number of window offsets. Each offset is 1.25 ms */
             sch->enqueued = 1;
-            connsm->tx_win_off = (earliest_start - initial_start) /
-                os_cputime_usecs_to_ticks(BLE_LL_CONN_ITVL_USECS);
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+            /*
+             * NOTE: we dont add sched offset ticks as we want to under-estimate
+             * the transmit window slightly since the window size is currently
+             * 2 when using a 32768 crystal.
+             */
+            dur = os_cputime_ticks_to_usecs(earliest_start - initial_start);
+            connsm->tx_win_off = dur / BLE_LL_CONN_TX_OFF_USECS;
+#else
+            dur = os_cputime_ticks_to_usecs(earliest_start - initial_start);
+            dur += XCVR_TX_SCHED_DELAY_USECS;
+            connsm->tx_win_off = dur / BLE_LL_CONN_TX_OFF_USECS;
+#endif
         }
     }
 
     if (!rc) {
         sch->start_time = earliest_start;
         sch->end_time = earliest_end;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        /*
+         * Since we have the transmit window to transmit in, we dont need
+         * to set the anchor point usecs; just transmit to the nearest tick.
+         */
+        connsm->anchor_point = earliest_start + g_ble_ll_sched_offset_ticks;
+        connsm->anchor_point_usecs = 0;
+#else
         connsm->anchor_point = earliest_start +
             os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
+#endif
         connsm->ce_end_time = earliest_end;
     }
 
@@ -371,6 +465,15 @@ ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, uint32_t adv_rxend,
     return rc;
 }
 
+/**
+ * Schedules a slave connection for the first time.
+ *
+ * Context: Link Layer
+ *
+ * @param connsm
+ *
+ * @return int
+ */
 int
 ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
 {
@@ -380,15 +483,32 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
     struct ble_ll_sched_item *next_sch;
     struct ble_ll_sched_item *sch;
 
+#ifdef BLE_XCVR_RFCLK
+    int first;
+    first = 0;
+#endif
+
     /* Get schedule element from connection */
     rc = -1;
     sch = &connsm->conn_sch;
 
     /* Set schedule start and end times */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /*
+     * XXX: for now, we dont care about anchor point usecs for the slave. It
+     * does not matter if we turn on the receiver up to one tick before w
+     * need to. We also subtract one extra tick since the conversion from
+     * usecs to ticks could be off by up to 1 tick.
+     */
+    sch->start_time = connsm->anchor_point - g_ble_ll_sched_offset_ticks -
+        os_cputime_usecs_to_ticks(connsm->slave_cur_window_widening) - 1;
+#else
     sch->start_time = connsm->anchor_point -
         os_cputime_usecs_to_ticks(XCVR_RX_SCHED_DELAY_USECS +
                                   connsm->slave_cur_window_widening);
+#endif
     sch->end_time = connsm->ce_end_time;
+    sch->remainder = 0;
 
     /* We have to find a place for this schedule */
     OS_ENTER_CRITICAL(sr);
@@ -403,6 +523,9 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
     if (!entry) {
         /* Nothing in schedule. Schedule as soon as possible */
         rc = 0;
+#ifdef BLE_XCVR_RFCLK
+        first = 1;
+#endif
     } else {
         os_cputime_timer_stop(&g_ble_ll_sched_timer);
         while (1) {
@@ -435,9 +558,24 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
         if (!rc) {
             sch->enqueued = 1;
         }
+#ifdef BLE_XCVR_RFCLK
+        next_sch = TAILQ_FIRST(&g_ble_ll_sched_q);
+        if (next_sch == sch) {
+            first = 1;
+        } else {
+            sch = next_sch;
+        }
+#else
         sch = TAILQ_FIRST(&g_ble_ll_sched_q);
+#endif
     }
 
+#ifdef BLE_XCVR_RFCLK
+    if (first) {
+        ble_ll_xcvr_rfclk_timer_start(sch->start_time);
+    }
+#endif
+
     OS_EXIT_CRITICAL(sr);
 
     os_cputime_timer_start(&g_ble_ll_sched_timer, sch->start_time);
@@ -450,9 +588,6 @@ 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;
@@ -463,39 +598,12 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
     orig = sch;
 
     OS_ENTER_CRITICAL(sr);
-
-    /*
-     * If we are currently in a connection, we add one slot time to the
-     * earliest start so we can end the connection reasonably.
-     */
-    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) {
-            ce_end_time += ticks;
-        }
-        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) {
         rc = 0;
         adv_start = sch->start_time;
     } else {
+        /* XXX: no need to stop timer if not first on list. Modify code? */
         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 */
@@ -529,13 +637,13 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
 
     ble_ll_adv_scheduled((struct ble_ll_adv_sm *)orig->cb_arg, adv_start);
 
-    OS_EXIT_CRITICAL(sr);
+#ifdef BLE_XCVR_RFCLK
+    if (orig == sch) {
+        ble_ll_xcvr_rfclk_timer_start(sch->start_time);
+    }
+#endif
 
-    /* XXX: some things to test. I am not sure that if we are passed the
-       output compare that we actually get the interrupt. */
-    /* XXX: I am not sure that if we receive a packet while scanning
-     * that we actually go back to scanning. I need to make sure
-       we re-enable the receive. Put an event in the log! */
+    OS_EXIT_CRITICAL(sr);
 
     os_cputime_timer_start(&g_ble_ll_sched_timer, sch->start_time);
 
@@ -653,6 +761,12 @@ ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch, uint32_t *start,
         }
         sch->end_time = sch->start_time + duration;
         *start = sch->start_time;
+
+#ifdef BLE_XCVR_RFCLK
+        if (sch == TAILQ_FIRST(&g_ble_ll_sched_q)) {
+            ble_ll_xcvr_rfclk_timer_start(sch->start_time);
+        }
+#endif
     }
 
     OS_EXIT_CRITICAL(sr);
@@ -784,26 +898,33 @@ ble_ll_sched_execute_item(struct ble_ll_sched_item *sch)
 void
 ble_ll_sched_run(void *arg)
 {
-    int32_t dt;
     struct ble_ll_sched_item *sch;
 
     /* Look through schedule queue */
-    while ((sch = TAILQ_FIRST(&g_ble_ll_sched_q)) != NULL) {
+    sch = TAILQ_FIRST(&g_ble_ll_sched_q);
+    if (sch) {
+#if (BLE_LL_SCHED_DEBUG == 1)
+        int32_t dt;
+
         /* Make sure we have passed the start time of the first event */
         dt = (int32_t)(os_cputime_get32() - sch->start_time);
-        if (dt >= 0) {
-#if (BLE_LL_SCHED_DEBUG == 1)
-            if (dt > g_ble_ll_sched_max_late) {
-                g_ble_ll_sched_max_late = dt;
-            }
+        if (dt > g_ble_ll_sched_max_late) {
+            g_ble_ll_sched_max_late = dt;
+        }
+        if (dt < g_ble_ll_sched_max_early) {
+            g_ble_ll_sched_max_early = dt;
+        }
 #endif
-            /* Remove schedule item and execute the callback */
-            TAILQ_REMOVE(&g_ble_ll_sched_q, sch, link);
-            sch->enqueued = 0;
-            ble_ll_sched_execute_item(sch);
-        } else {
+
+        /* Remove schedule item and execute the callback */
+        TAILQ_REMOVE(&g_ble_ll_sched_q, sch, link);
+        sch->enqueued = 0;
+        ble_ll_sched_execute_item(sch);
+
+        /* Restart if there is an item on the schedule */
+        sch = TAILQ_FIRST(&g_ble_ll_sched_q);
+        if (sch) {
             os_cputime_timer_start(&g_ble_ll_sched_timer, sch->start_time);
-            break;
         }
     }
 }
@@ -837,6 +958,55 @@ ble_ll_sched_next_time(uint32_t *next_event_time)
     return rc;
 }
 
+#ifdef BLE_XCVR_RFCLK
+/**
+ * Checks to see if we need to restart the cputime timer which starts the
+ * rf clock settling.
+ *
+ * NOTE: Should only be called from the Link Layer task!
+ *
+ * Context: Link-Layer task.
+ *
+ */
+void
+ble_ll_sched_rfclk_chk_restart(void)
+{
+    int stop;
+    os_sr_t sr;
+    uint8_t ll_state;
+    int32_t time_till_next;
+    uint32_t next_time;
+
+    stop = 0;
+    OS_ENTER_CRITICAL(sr);
+    ll_state = ble_ll_state_get();
+    if (ble_ll_sched_next_time(&next_time)) {
+        /*
+         * If the time until the next event is too close, no need to start
+         * the timer. Leave clock on.
+         */
+        time_till_next = (int32_t)(next_time - os_cputime_get32());
+        if (time_till_next > g_ble_ll_data.ll_xtal_ticks) {
+            /* Stop the clock */
+            stop = 1;
+            ble_ll_xcvr_rfclk_timer_start(next_time);
+        }
+    } else {
+        stop = 1;
+    }
+
+    /* Only disable the rfclk if doing nothing */
+    if (stop && (ll_state == BLE_LL_STATE_STANDBY)) {
+        ble_ll_log(BLE_LL_LOG_ID_RFCLK_SCHED_DIS, g_ble_ll_data.ll_rfclk_state,
+                   0, 0);
+        ble_ll_xcvr_rfclk_disable();
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+
+#endif
+
+
 /**
  * Stop the scheduler
  *
@@ -857,6 +1027,24 @@ ble_ll_sched_stop(void)
 int
 ble_ll_sched_init(void)
 {
+    /*
+     * Initialize max early to large negative number. This is used
+     * to determine the worst-case "early" time the schedule was called. Dont
+     * expect this to be less than -3 or -4.
+     */
+#if (BLE_LL_SCHED_DEBUG == 1)
+    g_ble_ll_sched_max_early = -50000;
+#endif
+
+    /*
+     * This is the offset from the start of the scheduled item until the actual
+     * tx/rx should occur, in ticks. We also "round up" to the nearest tick.
+     */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    g_ble_ll_sched_offset_ticks =
+        os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS + 30);
+#endif
+
     /* Initialize cputimer for the scheduler */
     os_cputime_timer_init(&g_ble_ll_sched_timer, ble_ll_sched_run, NULL);
     return 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/src/ble_ll_xcvr.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_xcvr.c b/net/nimble/controller/src/ble_ll_xcvr.c
new file mode 100644
index 0000000..50ab32e
--- /dev/null
+++ b/net/nimble/controller/src/ble_ll_xcvr.c
@@ -0,0 +1,153 @@
+/*
+ * 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 <stdint.h>
+#include <assert.h>
+#include "syscfg/syscfg.h"
+#include "os/os_cputime.h"
+#include "controller/ble_phy.h"
+#include "controller/ble_ll.h"
+#include "controller/ble_ll_xcvr.h"
+
+#ifdef BLE_XCVR_RFCLK
+int
+ble_ll_xcvr_rfclk_state(void)
+{
+    uint32_t expiry;
+
+    if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_ON) {
+        expiry = g_ble_ll_data.ll_rfclk_start_time;
+        if ((int32_t)(os_cputime_get32() - expiry) >
+                g_ble_ll_data.ll_xtal_ticks) {
+            g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_SETTLED;
+        }
+    }
+    return g_ble_ll_data.ll_rfclk_state;
+}
+
+void
+ble_ll_xcvr_rfclk_enable(void)
+{
+    g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_ON;
+    ble_phy_rfclk_enable();
+}
+
+void
+ble_ll_xcvr_rfclk_disable(void)
+{
+    ble_phy_rfclk_disable();
+    g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_OFF;
+}
+
+void
+ble_ll_xcvr_rfclk_stop(void)
+{
+    ble_ll_log(BLE_LL_LOG_ID_RFCLK_STOP, g_ble_ll_data.ll_rfclk_state, 0,0);
+    os_cputime_timer_stop(&g_ble_ll_data.ll_rfclk_timer);
+    ble_ll_xcvr_rfclk_disable();
+}
+
+uint32_t
+ble_ll_xcvr_rfclk_time_till_settled(void)
+{
+    int32_t dt;
+    uint32_t rc;
+
+    rc = 0;
+    if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_ON) {
+        dt = (int32_t)(os_cputime_get32() - g_ble_ll_data.ll_rfclk_start_time);
+        assert(dt >= 0);
+        if (dt < g_ble_ll_data.ll_xtal_ticks) {
+            rc = g_ble_ll_data.ll_xtal_ticks - (uint32_t)dt;
+        }
+    }
+
+    return rc;
+}
+
+/**
+ * Called when the timer to turn on the RF CLOCK expires. This function checks
+ * the state of the clock. If the clock is off, the clock is turned on.
+ * Otherwise, we just exit.
+ *
+ * Context: Interrupt
+ *
+ * @param arg
+ */
+void
+ble_ll_xcvr_rfclk_timer_exp(void *arg)
+{
+    if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_OFF) {
+        ble_ll_xcvr_rfclk_start_now(os_cputime_get32());
+    }
+}
+
+/**
+ * This API is used to turn on the rfclock without setting the cputime timer to
+ * start the clock at some later point.
+ *
+ * NOTE: presumes that the state of the rf clock was checked prior to calling.
+ *
+ * @param now
+ */
+void
+ble_ll_xcvr_rfclk_start_now(uint32_t now)
+{
+    ble_ll_xcvr_rfclk_enable();
+    g_ble_ll_data.ll_rfclk_start_time = now;
+    ble_ll_log(BLE_LL_LOG_ID_RFCLK_ENABLE, 0, 0, now);
+}
+
+/**
+ * Starts the timer that will turn the rf clock on. The 'cputime' is
+ * the time at which the clock needs to be settled.
+ *
+ * @param cputime   Time at which rfclock should be on and settled.
+ */
+void
+ble_ll_xcvr_rfclk_timer_start(uint32_t cputime)
+{
+    /*
+     * If we are currently in an advertising event or a connection event,
+     * no need to start the cputime timer
+     */
+    if ((g_ble_ll_data.ll_state == BLE_LL_STATE_ADV) ||
+        (g_ble_ll_data.ll_state == BLE_LL_STATE_CONNECTION)) {
+        return;
+    }
+
+    /* Account for the settling time */
+    cputime -= g_ble_ll_data.ll_xtal_ticks;
+
+    /*
+     * If the timer is on the list, we need to see if its expiry is before
+     * 'cputime'. If the expiry is before, no need to do anything. If it
+     * is after, we need to stop the timer and start at new time.
+     */
+    if (g_ble_ll_data.ll_rfclk_timer.link.tqe_prev != NULL) {
+        if ((int32_t)(cputime - g_ble_ll_data.ll_rfclk_timer.expiry) >= 0) {
+            return;
+        }
+        os_cputime_timer_stop(&g_ble_ll_data.ll_rfclk_timer);
+    }
+    os_cputime_timer_start(&g_ble_ll_data.ll_rfclk_timer, cputime);
+    ble_ll_log(BLE_LL_LOG_ID_RFCLK_START, g_ble_ll_data.ll_rfclk_state, 0,
+               g_ble_ll_data.ll_rfclk_timer.expiry);
+}
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/syscfg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/controller/syscfg.yml b/net/nimble/controller/syscfg.yml
index 357decc..569010f 100644
--- a/net/nimble/controller/syscfg.yml
+++ b/net/nimble/controller/syscfg.yml
@@ -137,6 +137,14 @@ syscfg.defs:
             material often.
         value: '32'
 
+    # Crystal setting time
+    BLE_XTAL_SETTLE_TIME:
+        description: >
+            The settling time of the high-frequency oscillator. This is
+            used to turn on/off the clock used for the radio (assuming
+            the HW supports this). This value is in microseconds.
+        value: '0'
+
     # Configuration for LL supported features.
     #
     # There are a total 8 features that the LL can support. These can be found

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/host/src/ble_hs_hci_evt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_evt.c b/net/nimble/host/src/ble_hs_hci_evt.c
index cd98f7f..a9655e3 100644
--- a/net/nimble/host/src/ble_hs_hci_evt.c
+++ b/net/nimble/host/src/ble_hs_hci_evt.c
@@ -6,7 +6,7 @@
  * 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,
@@ -624,8 +624,8 @@ ble_hs_hci_evt_acl_process(struct os_mbuf *om)
 #if (BLETEST_THROUGHPUT_TEST == 0)
     BLE_HS_LOG(DEBUG, "ble_hs_hci_evt_acl_process(): conn_handle=%u pb=%x "
                       "len=%u data=",
-               BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc), 
-               BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc), 
+               BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc),
+               BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc),
                hci_hdr.hdh_len);
     ble_hs_log_mbuf(om);
     BLE_HS_LOG(DEBUG, "\n");

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/include/nimble/ble.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble.h b/net/nimble/include/nimble/ble.h
index b514131..6f29fd4 100644
--- a/net/nimble/include/nimble/ble.h
+++ b/net/nimble/include/nimble/ble.h
@@ -96,6 +96,9 @@ struct ble_mbuf_hdr
         struct ble_mbuf_hdr_txinfo txinfo;
     };
     uint32_t beg_cputime;
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+    uint32_t rem_usecs;
+#endif
 };
 
 #define BLE_MBUF_HDR_CRC_OK(hdr)        \


[17/37] incubator-mynewt-core git commit: BLE Host - Add missing #include.

Posted by we...@apache.org.
BLE Host - Add missing #include.


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

Branch: refs/heads/nrf_cputime
Commit: e51ce9de50b2950273b1db79e26e43e01b0082b0
Parents: 519e516
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 16:37:21 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_uuid.h | 3 +++
 net/nimble/host/src/ble_uuid.c          | 1 +
 2 files changed, 4 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e51ce9de/net/nimble/host/include/host/ble_uuid.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_uuid.h b/net/nimble/host/include/host/ble_uuid.h
index a2be095..4cfaf53 100644
--- a/net/nimble/host/include/host/ble_uuid.h
+++ b/net/nimble/host/include/host/ble_uuid.h
@@ -99,6 +99,7 @@ typedef union {
 #define BLE_UUID128(u) \
     ((ble_uuid128_t *) (u))
 
+/** Includes trailing \0. */
 #define BLE_UUID_STR_LEN (37)
 
 int ble_uuid_init_from_buf(ble_uuid_any_t *uuid, const void *buf, size_t len);
@@ -106,6 +107,8 @@ int ble_uuid_cmp(const ble_uuid_t *uuid1, const ble_uuid_t *uuid2);
 char *ble_uuid_to_str(const ble_uuid_t *uuid, char *dst);
 uint16_t ble_uuid_u16(const ble_uuid_t *uuid);
 
+int ble_uuid_to_s(char *dst, int dst_sz, const void *uuid128);
+
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e51ce9de/net/nimble/host/src/ble_uuid.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_uuid.c b/net/nimble/host/src/ble_uuid.c
index f9ccc6d..ff94870 100644
--- a/net/nimble/host/src/ble_uuid.c
+++ b/net/nimble/host/src/ble_uuid.c
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <stdio.h>
 #include "os/os_mbuf.h"
 #include "nimble/ble.h"
 #include "ble_hs_priv.h"


[35/37] incubator-mynewt-core git commit: Low power timer. Only nrf52 right now.

Posted by we...@apache.org.
Low power timer. Only nrf52 right now.


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

Branch: refs/heads/nrf_cputime
Commit: 507f493ab86266528c7f921784978e63a07ddd33
Parents: 8158805
Author: William San Filippo <wi...@runtime.io>
Authored: Thu Feb 16 16:12:39 2017 -0800
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Mar 30 14:09:52 2017 -0700

----------------------------------------------------------------------
 apps/bletest/src/main.c                         |   2 +-
 hw/drivers/nimble/nrf52/include/ble/xcvr.h      |  12 +-
 hw/drivers/nimble/nrf52/src/ble_phy.c           | 438 +++++++++++++++++--
 kernel/os/include/os/os_cputime.h               |   8 +
 kernel/os/src/os_cputime.c                      |  50 +++
 .../controller/include/controller/ble_ll.h      |  51 ++-
 .../controller/include/controller/ble_ll_conn.h |   5 +
 .../include/controller/ble_ll_sched.h           |  32 +-
 .../controller/include/controller/ble_ll_xcvr.h |  48 ++
 .../controller/include/controller/ble_phy.h     |  31 +-
 net/nimble/controller/src/ble_ll.c              |  37 ++
 net/nimble/controller/src/ble_ll_adv.c          |  86 +++-
 net/nimble/controller/src/ble_ll_conn.c         | 265 +++++++++--
 net/nimble/controller/src/ble_ll_conn_priv.h    |   3 +-
 net/nimble/controller/src/ble_ll_scan.c         | 145 +++++-
 net/nimble/controller/src/ble_ll_sched.c        | 354 +++++++++++----
 net/nimble/controller/src/ble_ll_xcvr.c         | 153 +++++++
 net/nimble/controller/syscfg.yml                |   8 +
 net/nimble/host/src/ble_hs_hci_evt.c            |   6 +-
 net/nimble/include/nimble/ble.h                 |   3 +
 20 files changed, 1531 insertions(+), 206 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index e2519eb..efb3fd4 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -172,7 +172,7 @@ bletest_multi_adv_instances[BLETEST_CFG_ADV_TEST_INSTANCES] = {
 #define BLETEST_CFG_MAX_CE_LEN          (BLETEST_CFG_CONN_ITVL)
 #define BLETEST_CFG_CONN_PEER_ADDR_TYPE (BLE_HCI_CONN_PEER_ADDR_PUBLIC)
 #define BLETEST_CFG_CONN_OWN_ADDR_TYPE  (BLE_HCI_ADV_OWN_ADDR_PUBLIC)
-#define BLETEST_CFG_CONCURRENT_CONNS    (1)
+#define BLETEST_CFG_CONCURRENT_CONNS    (8)
 
 /* Test packet config */
 #define BLETEST_CFG_RAND_PKT_SIZE       (1)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/hw/drivers/nimble/nrf52/include/ble/xcvr.h
----------------------------------------------------------------------
diff --git a/hw/drivers/nimble/nrf52/include/ble/xcvr.h b/hw/drivers/nimble/nrf52/include/ble/xcvr.h
index 6a35124..2b6d7ea 100644
--- a/hw/drivers/nimble/nrf52/include/ble/xcvr.h
+++ b/hw/drivers/nimble/nrf52/include/ble/xcvr.h
@@ -24,10 +24,18 @@
 extern "C" {
 #endif
 
-/* Transceiver specific definitions */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+/*
+ * NOTE: we have to account for the RTC output compare issue, which is why
+ * this number is much larger when using the 32.768 crystal for cputime. We
+ * want it to be 5 ticks.
+ */
+#define XCVR_PROC_DELAY_USECS         (153)
+#else
+#define XCVR_PROC_DELAY_USECS         (50)
+#endif
 #define XCVR_RX_START_DELAY_USECS     (140)
 #define XCVR_TX_START_DELAY_USECS     (140)
-#define XCVR_PROC_DELAY_USECS         (50)
 #define XCVR_TX_SCHED_DELAY_USECS     \
     (XCVR_TX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
 #define XCVR_RX_SCHED_DELAY_USECS     \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/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 8771f49..8d8b0e1 100644
--- a/hw/drivers/nimble/nrf52/src/ble_phy.c
+++ b/hw/drivers/nimble/nrf52/src/ble_phy.c
@@ -78,6 +78,9 @@ struct ble_phy_obj
     struct ble_mbuf_hdr rxhdr;
     void *txend_arg;
     ble_phy_tx_end_func txend_cb;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    uint32_t phy_start_cputime;
+#endif
 };
 struct ble_phy_obj g_ble_phy_data;
 
@@ -280,6 +283,133 @@ nrf_wait_disabled(void)
     }
 }
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+/**
+ *
+ *
+ */
+int
+ble_phy_set_start_time(uint32_t cputime, uint8_t rem_usecs)
+{
+    uint32_t next_cc;
+    uint32_t cur_cc;
+    uint32_t cntr;
+    uint32_t delta;
+
+    /*
+     * XXX: The TXEN time is 140 usecs but there may be additional delays
+     * Need to look at this.
+     */
+
+    /*
+     * With the 32.768 kHz crystal, we may need to adjust the RTC compare
+     * value by 1 tick due to the time it takes for TXEN. The code uses a 5 RTC
+     * tick offset, which is 152.5 usecs. The TXEN time is 140 usecs. This
+     * means that with a remainder of 0, TIMER0 should be set to 12 or 13 (as
+     * TIMER0 counts at 1MHz). A remainder of 19 or more we will need to add
+     * 1 tick. We dont need to add 1 tick per se, but it does give us slightly
+     * more time and thus less of a chance to miss a tick. Another note: we
+     * cant set TIMER0 CC to 0 as the compare wont occur; it must be 1 or more.
+     * This is why we subtract 18 (as opposed to 19) as rem_uses will be >= 1.
+     */
+    if (rem_usecs <= 18) {
+        cputime -= 5;
+        rem_usecs += 12;
+    } else {
+        cputime -= 4;
+        rem_usecs -= 18;
+    }
+
+    /*
+     * Can we set the RTC compare to start TIMER0? We can do it if:
+     *      a) Current compare value is not N+1 or N+2 ticks from current
+     *      counter.
+     *      b) The value we want to set is not at least N+2 from current
+     *      counter.
+     *
+     * NOTE: since the counter can tick 1 while we do these calculations we
+     * need to account for it.
+     */
+    next_cc = cputime & 0xffffff;
+    cur_cc = NRF_RTC0->CC[0];
+    cntr = NRF_RTC0->COUNTER;
+
+    delta = (cur_cc - cntr) & 0xffffff;
+    if ((delta <= 3) && (delta != 0)) {
+        return -1;
+    }
+    delta = (next_cc - cntr) & 0xffffff;
+    if ((delta & 0x800000) || (delta < 3)) {
+        return -1;
+    }
+
+    /* Clear and set TIMER0 to fire off at proper time */
+    NRF_TIMER0->TASKS_CLEAR = 1;
+    NRF_TIMER0->CC[0] = rem_usecs;
+    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
+
+    /* Set RTC compare to start TIMER0 */
+    NRF_RTC0->EVENTS_COMPARE[0] = 0;
+    NRF_RTC0->CC[0] = next_cc;
+    NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
+
+    /* Enable PPI */
+    NRF_PPI->CHENSET = PPI_CHEN_CH31_Msk;
+
+    /* Store the cputime at which we set the RTC */
+    g_ble_phy_data.phy_start_cputime = cputime;
+
+    return 0;
+}
+
+/**
+ * Function is used to set PPI so that we can time out waiting for a reception
+ * to occur. This happens for two reasons: we have sent a packet and we are
+ * waiting for a respons (txrx should be set to ENABLE_TXRX) or we are
+ * starting a connection event and we are a slave and we are waiting for the
+ * master to send us a packet (txrx should be set to ENABLE_RX).
+ *
+ * NOTE: when waiting for a txrx turn-around, wfr_usecs is not used as there
+ * is no additional time to wait; we know when we should receive the address of
+ * the received frame.
+ *
+ * @param txrx Flag denoting if this wfr is a txrx turn-around or not.
+ * @param wfr_usecs Amount of usecs to wait.
+ */
+void
+ble_phy_wfr_enable(int txrx, uint32_t wfr_usecs)
+{
+    uint32_t end_time;
+
+    if (txrx == BLE_PHY_WFR_ENABLE_TXRX) {
+        /*
+         * Timeout occurs an IFS time plus time it takes to receive address
+         * from the transmit end. We add additional time to make sure the
+         * address event comes before the compare. Note that transmit end
+         * is captured in CC[2]
+         *
+         * XXX: this assumes 1Mbps as 40 usecs is header rx time for 1Mbps
+         */
+        end_time = NRF_TIMER0->CC[2] + BLE_LL_IFS + 40 + 16;
+    } else {
+        /* CC[0] is set to when RXEN occurs. NOTE: the extra 16 usecs is
+           jitter */
+        end_time = NRF_TIMER0->CC[0] + XCVR_RX_START_DELAY_USECS + wfr_usecs +
+            40 + 16;
+    }
+
+    /* wfr_secs is the time from rxen until timeout */
+    NRF_TIMER0->CC[3] = end_time;
+    NRF_TIMER0->EVENTS_COMPARE[3] = 0;
+
+    /* Enable wait for response PPI */
+    NRF_PPI->CHENSET = (PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk);
+
+    /* Enable the disabled interrupt so we time out on events compare */
+    NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
+}
+#endif
+
 /**
  * Setup transceiver for receive.
  */
@@ -360,14 +490,18 @@ ble_phy_tx_end_isr(void)
     uint8_t transition;
     uint8_t txlen;
     uint32_t wfr_time;
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768)
     uint32_t txstart;
+#endif
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768)
     /*
      * Read captured tx start time. This is not the actual transmit start
      * time but it is the time at which the address event occurred
      * (after transmission of access address)
      */
     txstart = NRF_TIMER0->CC[1];
+#endif
 
     /* If this transmission was encrypted we need to remember it */
     was_encrypted = g_ble_phy_data.phy_encrypted;
@@ -376,14 +510,20 @@ ble_phy_tx_end_isr(void)
     assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
 
     /* Log the event */
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+    ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, g_ble_phy_data.phy_tx_pyld_len,
+               was_encrypted, NRF_TIMER0->CC[2]);
+#else
     ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, g_ble_phy_data.phy_tx_pyld_len,
                was_encrypted, txstart);
+#endif
 
     /* Clear events and clear interrupt on disabled event */
     NRF_RADIO->EVENTS_DISABLED = 0;
     NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
     NRF_RADIO->EVENTS_END = 0;
     wfr_time = NRF_RADIO->SHORTS;
+    (void)wfr_time;
 
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     /*
@@ -416,13 +556,28 @@ ble_phy_tx_end_isr(void)
         if (txlen && was_encrypted) {
             txlen += BLE_LL_DATA_MIC_LEN;
         }
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+        ble_phy_wfr_enable(BLE_PHY_WFR_ENABLE_TXRX, 0);
+#else
         wfr_time = BLE_LL_WFR_USECS - BLE_TX_LEN_USECS_M(NRF_RX_START_OFFSET);
         wfr_time += BLE_TX_DUR_USECS_M(txlen);
         wfr_time = os_cputime_usecs_to_ticks(wfr_time);
         ble_ll_wfr_enable(txstart + wfr_time);
+#endif
     } else {
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+        /*
+         * XXX: not sure we need to stop the timer here all the time. Or that
+         * it should be stopped here.
+         */
+        NRF_TIMER0->TASKS_STOP = 1;
+        NRF_TIMER0->TASKS_SHUTDOWN = 1;
+        NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk |
+                           PPI_CHEN_CH20_Msk | PPI_CHEN_CH31_Msk;
+#else
         /* Disable automatic TXEN */
         NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
+#endif
         assert(transition == BLE_PHY_TRANSITION_NONE);
     }
 }
@@ -506,12 +661,48 @@ ble_phy_rx_start_isr(void)
 {
     int rc;
     uint32_t state;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    uint32_t usecs;
+    uint32_t ticks;
+#endif
     struct ble_mbuf_hdr *ble_hdr;
 
     /* Clear events and clear interrupt */
     NRF_RADIO->EVENTS_ADDRESS = 0;
+
+    /* Clear wfr timer channels and DISABLED interrupt */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk | RADIO_INTENCLR_ADDRESS_Msk;
+    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk;
+#else
     NRF_RADIO->INTENCLR = RADIO_INTENCLR_ADDRESS_Msk;
+#endif
 
+    /* Initialize the ble mbuf header */
+    ble_hdr = &g_ble_phy_data.rxhdr;
+    ble_hdr->rxinfo.flags = ble_ll_state_get();
+    ble_hdr->rxinfo.channel = g_ble_phy_data.phy_chan;
+    ble_hdr->rxinfo.handle = 0;
+
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+    /*
+     * Calculate receive start time. We assume that the header time is
+     * 40 usecs (only 1 mbps supported right now).
+     *
+     * XXX: possibly use other routine with remainder!
+     */
+    usecs = NRF_TIMER0->CC[1] - 40;
+    ticks = os_cputime_usecs_to_ticks(usecs);
+    ble_hdr->rem_usecs = usecs - os_cputime_ticks_to_usecs(ticks);
+    ble_hdr->beg_cputime = g_ble_phy_data.phy_start_cputime + ticks;
+#else
+    ble_hdr->beg_cputime = NRF_TIMER0->CC[1] -
+        os_cputime_usecs_to_ticks(BLE_TX_LEN_USECS_M(NRF_RX_START_OFFSET));
+#endif
+
+    /* XXX: I wonder if we always have the 1st byte. If we need to wait for
+     * rx chain delay, it could be 18 usecs from address interrupt. The
+       nrf52 may be able to get here early. */
     /* Wait to get 1st byte of frame */
     while (1) {
         state = NRF_RADIO->STATE;
@@ -530,14 +721,6 @@ ble_phy_rx_start_isr(void)
         }
     }
 
-    /* Initialize flags, channel and state in ble header at rx start */
-    ble_hdr = &g_ble_phy_data.rxhdr;
-    ble_hdr->rxinfo.flags = ble_ll_state_get();
-    ble_hdr->rxinfo.channel = g_ble_phy_data.phy_chan;
-    ble_hdr->rxinfo.handle = 0;
-    ble_hdr->beg_cputime = NRF_TIMER0->CC[1] -
-        os_cputime_usecs_to_ticks(BLE_TX_LEN_USECS_M(NRF_RX_START_OFFSET));
-
     /* Call Link Layer receive start function */
     rc = ble_ll_rx_start((uint8_t *)&g_ble_phy_rx_buf[0] + 3,
                          g_ble_phy_data.phy_chan,
@@ -573,16 +756,34 @@ ble_phy_isr(void)
     /* Read irq register to determine which interrupts are enabled */
     irq_en = NRF_RADIO->INTENCLR;
 
-    /* Check for disabled event. This only happens for transmits now */
-    if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
-        ble_phy_tx_end_isr();
-    }
+    /*
+     * NOTE: order of checking is important! Possible, if things get delayed,
+     * we have both an ADDRESS and DISABLED interrupt in rx state. If we get
+     * an address, we disable the DISABLED interrupt.
+     */
 
     /* We get this if we have started to receive a frame */
     if ((irq_en & RADIO_INTENCLR_ADDRESS_Msk) && NRF_RADIO->EVENTS_ADDRESS) {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        irq_en &= ~RADIO_INTENCLR_DISABLED_Msk;
+#endif
         ble_phy_rx_start_isr();
     }
 
+    /* Check for disabled event. This only happens for transmits now */
+    if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        if (g_ble_phy_data.phy_state == BLE_PHY_STATE_RX) {
+            NRF_RADIO->EVENTS_DISABLED = 0;
+            ble_ll_wfr_timer_exp(NULL);
+        } else {
+            ble_phy_tx_end_isr();
+        }
+#else
+        ble_phy_tx_end_isr();
+#endif
+    }
+
     /* Receive packet end (we dont enable this for transmit) */
     if ((irq_en & RADIO_INTENCLR_END_Msk) && NRF_RADIO->EVENTS_END) {
         ble_phy_rx_end_isr();
@@ -606,6 +807,8 @@ int
 ble_phy_init(void)
 {
     int rc;
+
+#if !defined(BLE_XCVR_RFCLK)
     uint32_t os_tmo;
 
     /* Make sure HFXO is started */
@@ -620,6 +823,7 @@ ble_phy_init(void)
             return BLE_PHY_ERR_INIT;
         }
     }
+#endif
 
     /* Set phy channel to an invalid channel so first set channel works */
     g_ble_phy_data.phy_chan = BLE_PHY_NUM_CHANS;
@@ -657,8 +861,13 @@ ble_phy_init(void)
     /* Configure IFS */
     NRF_RADIO->TIFS = BLE_LL_IFS;
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /* Captures tx/rx start in timer0 cc 1 and tx/rx end in timer0 cc 2 */
+    NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk | PPI_CHEN_CH27_Msk;
+#else
     /* Captures tx/rx start in timer0 capture 1 */
     NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk;
+#endif
 
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     NRF_CCM->INTENCLR = 0xffffffff;
@@ -677,6 +886,27 @@ ble_phy_init(void)
     NRF_AAR->NIRK = 0;
 #endif
 
+    /* TIMER0 setup for PHY when using RTC */
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+    NRF_TIMER0->TASKS_STOP = 1;
+    NRF_TIMER0->TASKS_SHUTDOWN = 1;
+    NRF_TIMER0->BITMODE = 3;    /* 32-bit timer */
+    NRF_TIMER0->MODE = 0;       /* Timer mode */
+    NRF_TIMER0->PRESCALER = 4;  /* gives us 1 MHz */
+
+    /*
+     * PPI setup.
+     * Channel 4: Captures TIMER0 in CC[3] when EVENTS_ADDRESS occurs. Used
+     *            to cancel the wait for response timer.
+     * Channel 5: TIMER0 CC[3] to TASKS_DISABLE on radio. This is the wait
+     *            for response timer.
+     */
+    NRF_PPI->CH[4].EEP = (uint32_t)&(NRF_RADIO->EVENTS_ADDRESS);
+    NRF_PPI->CH[4].TEP = (uint32_t)&(NRF_TIMER0->TASKS_CAPTURE[3]);
+    NRF_PPI->CH[5].EEP = (uint32_t)&(NRF_TIMER0->EVENTS_COMPARE[3]);
+    NRF_PPI->CH[5].TEP = (uint32_t)&(NRF_RADIO->TASKS_DISABLE);
+#endif
+
     /* Set isr in vector table and enable interrupt */
     NVIC_SetPriority(RADIO_IRQn, 0);
     NVIC_SetVector(RADIO_IRQn, (uint32_t)ble_phy_isr);
@@ -785,6 +1015,7 @@ ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
     g_ble_phy_data.txend_arg = arg;
 }
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
 /**
  * Called to set the start time of a transmission.
  *
@@ -794,28 +1025,38 @@ ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
  * NOTE: care must be taken when calling this function. The channel should
  * already be set.
  *
- * @param cputime
- *
+ * @param cputime   This is the tick at which the 1st bit of the preamble
+ *                  should be transmitted
+ * @param rem_usecs This is used only when the underlying timing uses a 32.768
+ *                  kHz crystal. It is the # of usecs from the cputime tick
+ *                  at which the first bit of the preamble should be
+ *                  transmitted.
  * @return int
  */
 int
-ble_phy_tx_set_start_time(uint32_t cputime)
+ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
 {
     int rc;
 
-    NRF_TIMER0->CC[0] = cputime;
-    NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
+    /* XXX: This should not be necessary, but paranoia is good! */
+    /* Clear timer0 compare to RXEN since we are transmitting */
     NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-    if ((int32_t)(os_cputime_get32() - cputime) >= 0) {
+
+    /*
+     * XXX: The TXEN time is 140 usecs but there may be additional delays
+     * Need to look at this.
+     */
+    if (ble_phy_set_start_time(cputime, rem_usecs) != 0) {
         STATS_INC(ble_phy_stats, tx_late);
         ble_phy_disable();
-        rc =  BLE_PHY_ERR_TX_LATE;
+        rc = BLE_PHY_ERR_TX_LATE;
     } else {
+        /* Enable PPI to automatically start TXEN */
+        NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
         rc = 0;
     }
     return rc;
 }
-
 /**
  * Called to set the start time of a reception
  *
@@ -830,23 +1071,90 @@ ble_phy_tx_set_start_time(uint32_t cputime)
  * @return int
  */
 int
-ble_phy_rx_set_start_time(uint32_t cputime)
+ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
 {
     int rc;
 
+    /* XXX: This should not be necessary, but paranoia is good! */
+    /* Clear timer0 compare to TXEN since we are transmitting */
+    NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
+
+    /*
+     * XXX: The RXEN time is 138 usecs but there may be additional delays
+     * Need to look at this.
+     */
+    if (ble_phy_set_start_time(cputime, rem_usecs) != 0) {
+        STATS_INC(ble_phy_stats, rx_late);
+        NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
+        NRF_RADIO->TASKS_RXEN = 1;
+        rc = BLE_PHY_ERR_RX_LATE;
+    } else {
+        /* Enable PPI to automatically start RXEN */
+        NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk;
+
+        /* Start rx */
+        rc = ble_phy_rx();
+    }
+    return rc;
+}
+#else
+/**
+ * Called to set the start time of a transmission.
+ *
+ * This function is called to set the start time when we are not going from
+ * rx to tx automatically.
+ *
+ * NOTE: care must be taken when calling this function. The channel should
+ * already be set.
+ *
+ * @param cputime   This is the tick at which the 1st bit of the preamble
+ *                  should be transmitted
+ * @return int
+ */
+int
+ble_phy_tx_set_start_time(uint32_t cputime)
+{
+    int rc;
+
+    cputime -= os_cputime_usecs_to_ticks(XCVR_TX_START_DELAY_USECS);
+    NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
     NRF_TIMER0->CC[0] = cputime;
+    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
+    NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
+    if ((int32_t)(os_cputime_get32() - cputime) >= 0) {
+        STATS_INC(ble_phy_stats, tx_late);
+        ble_phy_disable();
+        rc = BLE_PHY_ERR_TX_LATE;
+    } else {
+        rc = 0;
+    }
+
+    return rc;
+}
+
+#if 0
+int
+ble_phy_rx_set_start_time(void)
+{
+    /*
+     * XXX: For now, we dont use this function if we are not using the
+     * RTC. Keeping the code around just in case we want to use it later.
+     */
     NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
+    NRF_TIMER0->CC[0] = cputime;
+    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
     NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk;
     if ((int32_t)(os_cputime_get32() - cputime) >= 0) {
         STATS_INC(ble_phy_stats, rx_late);
         NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
         NRF_RADIO->TASKS_RXEN = 1;
-        rc =  BLE_PHY_ERR_TX_LATE;
+        rc =  BLE_PHY_ERR_RX_LATE;
     } else {
         rc = 0;
     }
-    return rc;
 }
+#endif
+#endif
 
 int
 ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
@@ -871,6 +1179,16 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
     ble_hdr = BLE_MBUF_HDR_PTR(txpdu);
     payload_len = ble_hdr->txinfo.pyld_len;
 
+    /*
+     * XXX: Although we may not have to do this here, I clear all the PPI
+     * that should not be used when transmitting. Some of them are only enabled
+     * if encryption and/or privacy is on, but I dont care. Better to be
+     * paranoid, and if you are going to clear one, might as well clear them
+     * all.
+     */
+    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH23_Msk |
+                       PPI_CHEN_CH25_Msk;
+
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (g_ble_phy_data.phy_encrypted) {
         dptr = (uint8_t *)&g_ble_phy_enc_buf[0];
@@ -883,11 +1201,9 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
         NRF_CCM->EVENTS_ERROR = 0;
         NRF_CCM->MODE = CCM_MODE_LENGTH_Msk;
         NRF_CCM->CNFPTR = (uint32_t)&g_nrf_ccm_data;
-        NRF_PPI->CHENCLR = PPI_CHEN_CH25_Msk | PPI_CHEN_CH23_Msk;
         NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk;
     } else {
 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-        NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk;
         NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
 #endif
         dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
@@ -895,9 +1211,6 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
         pktptr = dptr;
     }
 #else
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk;
-#endif
     dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
     ++dptr;
     pktptr = dptr;
@@ -1077,29 +1390,62 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
     return 0;
 }
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
 /**
- * Disable the PHY. This will do the following:
- *  -> Turn off all phy interrupts.
- *  -> Disable internal shortcuts.
- *  -> Disable the radio.
- *  -> Make sure we wont automatically go to rx/tx on output compare
- *  -> Sets phy state to idle.
- *  -> Clears any pending irqs in the NVIC. Might not be necessary but we do
- *  it as a precaution.
+ * Stop the timer used to count microseconds when using RTC for cputime
  */
 void
-ble_phy_disable(void)
+ble_phy_stop_usec_timer(void)
 {
-    ble_ll_log(BLE_LL_LOG_ID_PHY_DISABLE, g_ble_phy_data.phy_state, 0, 0);
+    NRF_TIMER0->TASKS_STOP = 1;
+    NRF_TIMER0->TASKS_SHUTDOWN = 1;
+    NRF_RTC0->EVTENCLR = RTC_EVTENSET_COMPARE0_Msk;
+}
+#endif
 
+/**
+ * ble phy disable irq and ppi
+ *
+ * This routine is to be called when reception was stopped due to either a
+ * wait for response timeout or a packet being received and the phy is to be
+ * restarted in receive mode. Generally, the disable routine is called to stop
+ * the phy.
+ */
+void
+ble_phy_disable_irq_and_ppi(void)
+{
     NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
     NRF_RADIO->SHORTS = 0;
     NRF_RADIO->TASKS_DISABLE = 1;
-    NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk | PPI_CHEN_CH21_Msk | PPI_CHEN_CH20_Msk;
+    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH20_Msk |
+          PPI_CHEN_CH21_Msk | PPI_CHEN_CH23_Msk | PPI_CHEN_CH24_Msk |
+          PPI_CHEN_CH25_Msk | PPI_CHEN_CH31_Msk;
     NVIC_ClearPendingIRQ(RADIO_IRQn);
     g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
 }
 
+void
+ble_phy_restart_rx(void)
+{
+    ble_phy_disable_irq_and_ppi();
+    ble_phy_rx();
+}
+
+/**
+ * ble phy disable
+ *
+ * Disables the PHY. This should be called when an event is over. It stops
+ * the usec timer (if used), disables interrupts, disables the RADIO, disables
+ * PPI and sets state to idle.
+ */
+void
+ble_phy_disable(void)
+{
+    ble_ll_log(BLE_LL_LOG_ID_PHY_DISABLE, g_ble_phy_data.phy_state, 0, 0);
+    ble_phy_stop_usec_timer();
+    ble_phy_disable_irq_and_ppi();
+}
+
 /* Gets the current access address */
 uint32_t ble_phy_access_addr_get(void)
 {
@@ -1168,3 +1514,17 @@ ble_phy_resolv_list_disable(void)
     g_ble_phy_data.phy_privacy = 0;
 }
 #endif
+
+#ifdef BLE_XCVR_RFCLK
+void
+ble_phy_rfclk_enable(void)
+{
+    NRF_CLOCK->TASKS_HFCLKSTART = 1;
+}
+
+void
+ble_phy_rfclk_disable(void)
+{
+    NRF_CLOCK->TASKS_HFCLKSTOP = 1;
+}
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/kernel/os/include/os/os_cputime.h
----------------------------------------------------------------------
diff --git a/kernel/os/include/os/os_cputime.h b/kernel/os/include/os/os_cputime.h
index f44c941..022efdc 100644
--- a/kernel/os/include/os/os_cputime.h
+++ b/kernel/os/include/os/os_cputime.h
@@ -45,6 +45,10 @@ extern "C" {
 #define OS_CPUTIME_FREQ_1MHZ
 #endif
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+#define OS_CPUTIME_FREQ_32768
+#endif
+
 /* CPUTIME data. */
 struct os_cputime_data
 {
@@ -80,6 +84,7 @@ int os_cputime_init(uint32_t clock_freq);
  */
 uint32_t os_cputime_get32(void);
 
+#if !defined(OS_CPUTIME_FREQ_32768)
 /**
  * os cputime nsecs to ticks
  *
@@ -101,6 +106,7 @@ uint32_t os_cputime_nsecs_to_ticks(uint32_t nsecs);
  * @return uint32_t The number of nanoseconds corresponding to 'ticks'
  */
 uint32_t os_cputime_ticks_to_nsecs(uint32_t ticks);
+#endif
 
 #if defined(OS_CPUTIME_FREQ_1MHZ)
 #define os_cputime_usecs_to_ticks(x)    (x)
@@ -138,6 +144,7 @@ uint32_t os_cputime_ticks_to_usecs(uint32_t ticks);
  */
 void os_cputime_delay_ticks(uint32_t ticks);
 
+#if !defined(OS_CPUTIME_FREQ_32768)
 /**
  * os cputime delay nsecs
  *
@@ -146,6 +153,7 @@ void os_cputime_delay_ticks(uint32_t ticks);
  * @param nsecs The number of nanoseconds to wait.
  */
 void os_cputime_delay_nsecs(uint32_t nsecs);
+#endif
 
 /**
  * os cputime delay usecs

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/kernel/os/src/os_cputime.c
----------------------------------------------------------------------
diff --git a/kernel/os/src/os_cputime.c b/kernel/os/src/os_cputime.c
index 067f1a1..01b15fe 100644
--- a/kernel/os/src/os_cputime.c
+++ b/kernel/os/src/os_cputime.c
@@ -30,7 +30,9 @@
  *   @{
  */
 
+#if !defined(OS_CPUTIME_FREQ_32768) && !defined(OS_CPUTIME_FREQ_1MHZ)
 struct os_cputime_data g_os_cputime;
+#endif
 
 /**
  * os cputime init
@@ -49,11 +51,14 @@ os_cputime_init(uint32_t clock_freq)
     int rc;
 
     /* Set the ticks per microsecond. */
+#if !defined(OS_CPUTIME_FREQ_32768) && !defined(OS_CPUTIME_FREQ_1MHZ)
     g_os_cputime.ticks_per_usec = clock_freq / 1000000U;
+#endif
     rc = hal_timer_config(MYNEWT_VAL(OS_CPUTIME_TIMER_NUM), clock_freq);
     return rc;
 }
 
+#if !defined(OS_CPUTIME_FREQ_32768)
 /**
  * os cputime nsecs to ticks
  *
@@ -99,8 +104,50 @@ os_cputime_ticks_to_nsecs(uint32_t ticks)
 
     return nsecs;
 }
+#endif
 
 #if !defined(OS_CPUTIME_FREQ_1MHZ)
+#if defined(OS_CPUTIME_FREQ_32768)
+/**
+ * os cputime usecs to ticks
+ *
+ * Converts the given number of microseconds into cputime ticks.
+ *
+ * @param usecs The number of microseconds to convert to ticks
+ *
+ * @return uint32_t The number of ticks corresponding to 'usecs'
+ */
+uint32_t
+os_cputime_usecs_to_ticks(uint32_t usecs)
+{
+    uint64_t ticks;
+
+    ticks = ((uint64_t)usecs << 9) / 15625;
+    return (uint32_t)ticks;
+}
+
+/**
+ * cputime ticks to usecs
+ *
+ * Convert the given number of ticks into microseconds.
+ *
+ * @param ticks The number of ticks to convert to microseconds.
+ *
+ * @return uint32_t The number of microseconds corresponding to 'ticks'
+ *
+ * NOTE: This calculation will overflow if the value for ticks is greater
+ * than 140737488. I am not going to check that here because that many ticks
+ * is about 4222 seconds, way more than what this routine should be used for.
+ */
+uint32_t
+os_cputime_ticks_to_usecs(uint32_t ticks)
+{
+    uint32_t usecs;
+
+    usecs = ((ticks >> 9) * 15625) + (((ticks & 0x1ff) * 15625) >> 9);
+    return usecs;
+}
+#else
 /**
  * os cputime usecs to ticks
  *
@@ -138,6 +185,7 @@ os_cputime_ticks_to_usecs(uint32_t ticks)
     return us;
 }
 #endif
+#endif
 
 /**
  * os cputime delay ticks
@@ -157,6 +205,7 @@ os_cputime_delay_ticks(uint32_t ticks)
     }
 }
 
+#if !defined(OS_CPUTIME_FREQ_32768)
 /**
  * os cputime delay nsecs
  *
@@ -172,6 +221,7 @@ os_cputime_delay_nsecs(uint32_t nsecs)
     ticks = os_cputime_nsecs_to_ticks(nsecs);
     os_cputime_delay_ticks(ticks);
 }
+#endif
 
 /**
  * os cputime delay usecs

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
index e349d71..acc201d 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -22,13 +22,41 @@
 
 #include "stats/stats.h"
 #include "os/os_eventq.h"
+#include "os/os_callout.h"
 #include "os/os_cputime.h"
 #include "nimble/nimble_opt.h"
+#include "controller/ble_phy.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/*
+ * XXX:
+ * I guess this should not depend on the 32768 crystal to be honest. This
+ * should be done for TIMER0 as well since the rf clock chews up more current.
+ * Deal with this later.
+ *
+ * Another note: BLE_XTAL_SETTLE_TIME should be bsp related (I guess). There
+ * should be a note in there that the converted usecs to ticks value of this
+ * should not be 0. Thus: if you are using a 32.768 os cputime freq, the min
+ * value of settle time should be 31 usecs. I would suspect all settling times
+ * would exceed 31 usecs.
+ */
+
+/* Determines if we need to turn on/off rf clock */
+#undef BLE_XCVR_RFCLK
+
+/* Transceiver specific definitions */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+
+/* We will turn on/off rf clock */
+#if MYNEWT_VAL(BLE_XTAL_SETTLE_TIME) != 0
+#define BLE_XCVR_RFCLK
+#endif
+
+#endif
+
 /* Controller revision. */
 #define BLE_LL_SUB_VERS_NR      (0x0000)
 
@@ -61,11 +89,22 @@ struct ble_ll_obj
 
     /* Number of ACL data packets supported */
     uint8_t ll_num_acl_pkts;
+
+#ifdef BLE_XCVR_RFCLK
+    uint8_t ll_rfclk_state;
+    uint16_t ll_xtal_ticks;
+#else
     uint8_t _pad;
+    uint16_t _pad16;
+#endif
 
     /* ACL data packet size */
     uint16_t ll_acl_pkt_size;
-    uint16_t _pad16;
+
+#ifdef BLE_XCVR_RFCLK
+    uint32_t ll_rfclk_start_time;
+    struct hal_timer ll_rfclk_timer;
+#endif
 
     /* Task event queue */
     struct os_eventq ll_evq;
@@ -362,6 +401,9 @@ void ble_ll_wfr_enable(uint32_t cputime);
 /* Disable wait for response timer */
 void ble_ll_wfr_disable(void);
 
+/* Wait for response timer expiration callback */
+void ble_ll_wfr_timer_exp(void *arg);
+
 /* Read set of features supported by the Link Layer */
 uint8_t ble_ll_read_supp_features(void);
 
@@ -383,7 +425,7 @@ int ble_ll_rand_start(void);
  * XXX: temporary LL debug log. Will get removed once we transition to real
  * log
  */
-#undef BLE_LL_LOG
+#define BLE_LL_LOG
 #include "console/console.h"
 
 #define BLE_LL_LOG_ID_PHY_SETCHAN       (1)
@@ -405,6 +447,11 @@ int ble_ll_rand_start(void);
 #define BLE_LL_LOG_ID_ADV_TXBEG         (50)
 #define BLE_LL_LOG_ID_ADV_TXDONE        (60)
 #define BLE_LL_LOG_ID_SCHED             (80)
+#define BLE_LL_LOG_ID_RFCLK_START       (90)
+#define BLE_LL_LOG_ID_RFCLK_ENABLE      (91)
+#define BLE_LL_LOG_ID_RFCLK_STOP        (95)
+#define BLE_LL_LOG_ID_RFCLK_SCHED_DIS   (96)
+#define BLE_LL_LOG_ID_RFCLK_SCAN_DIS    (97)
 
 #ifdef BLE_LL_LOG
 void ble_ll_log(uint8_t id, uint8_t arg8, uint16_t arg16, uint32_t arg32);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/include/controller/ble_ll_conn.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_conn.h b/net/nimble/controller/include/controller/ble_ll_conn.h
index b2e6410..041504d 100644
--- a/net/nimble/controller/include/controller/ble_ll_conn.h
+++ b/net/nimble/controller/include/controller/ble_ll_conn.h
@@ -202,6 +202,11 @@ struct ble_ll_conn_sm
     uint16_t max_ce_len;
     uint16_t tx_win_off;
     uint32_t anchor_point;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    uint8_t anchor_point_usecs;     /* XXX: can this be uint8_t ?*/
+    uint8_t conn_itvl_usecs;
+    uint32_t conn_itvl_ticks;
+#endif
     uint32_t last_anchor_point;     /* Slave only */
     uint32_t slave_cur_tx_win_usecs;
     uint32_t slave_cur_window_widening;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/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 3a05cfe..d2321b7 100644
--- a/net/nimble/controller/include/controller/ble_ll_sched.h
+++ b/net/nimble/controller/include/controller/ble_ll_sched.h
@@ -25,7 +25,8 @@ extern "C" {
 #endif
 
 /* Time per BLE scheduler slot */
-#define BLE_LL_SCHED_USECS_PER_SLOT (1250)
+#define BLE_LL_SCHED_USECS_PER_SLOT         (1250)
+#define BLE_LL_SCHED_32KHZ_TICKS_PER_SLOT   (41)    /* 1 tick = 30.517 usecs */
 
 /*
  * Worst case time needed for scheduled advertising item. This is the longest
@@ -46,6 +47,17 @@ extern "C" {
 #define BLE_LL_SCHED_DIRECT_ADV_MAX_USECS   (502)
 #define BLE_LL_SCHED_MAX_ADV_PDU_USECS      (376)
 
+/* BLE Jitter (+/- 16 useecs) */
+#define BLE_LL_JITTER_USECS                 (16)
+
+/*
+ * This is the offset from the start of the scheduled item until the actual
+ * tx/rx should occur, in ticks.
+ */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+extern uint8_t g_ble_ll_sched_offset_ticks;
+#endif
+
 /*
  * 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
@@ -69,10 +81,19 @@ extern "C" {
 struct ble_ll_sched_item;
 typedef int (*sched_cb_func)(struct ble_ll_sched_item *sch);
 
+/*
+ * Schedule item
+ *  sched_type: This is the type of the schedule item.
+ *  enqueued: Flag denoting if item is on the scheduler list. 0: no, 1:yes
+ *  remainder: # of usecs from offset till tx/rx should occur
+ *  txrx_offset: Number of ticks from start time until tx/rx should occur.
+ *
+ */
 struct ble_ll_sched_item
 {
     uint8_t         sched_type;
     uint8_t         enqueued;
+    uint8_t         remainder;
     uint32_t        start_time;
     uint32_t        end_time;
     void            *cb_arg;
@@ -94,8 +115,8 @@ void ble_ll_sched_free_item(struct ble_ll_sched_item *sch);
 
 /* Schedule a new master connection */
 struct ble_ll_conn_sm;
-int ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, uint32_t adv_rxend,
-                            uint8_t req_slots);
+int ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm,
+                            struct ble_mbuf_hdr *ble_hdr, uint8_t pyld_len);
 
 /* Schedule a new slave connection */
 int ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm);
@@ -128,6 +149,11 @@ int ble_ll_sched_next_time(uint32_t *next_event_time);
 /* Stop the scheduler */
 void ble_ll_sched_stop(void);
 
+#ifdef BLE_XCVR_RFCLK
+/* Check if RF clock needs to be restarted */
+void ble_ll_sched_rfclk_chk_restart(void);
+#endif
+
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/include/controller/ble_ll_xcvr.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_xcvr.h b/net/nimble/controller/include/controller/ble_ll_xcvr.h
new file mode 100644
index 0000000..c59b009
--- /dev/null
+++ b/net/nimble/controller/include/controller/ble_ll_xcvr.h
@@ -0,0 +1,48 @@
+/*
+ * 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_LL_XCVR_
+#define H_BLE_LL_XCVR_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef BLE_XCVR_RFCLK
+
+/* RF clock states */
+#define BLE_RFCLK_STATE_OFF     (0)
+#define BLE_RFCLK_STATE_ON      (1)
+#define BLE_RFCLK_STATE_SETTLED (2)
+
+int ble_ll_xcvr_rfclk_state(void);
+void ble_ll_xcvr_rfclk_start_now(uint32_t now);
+void ble_ll_xcvr_rfclk_stop(void);
+void ble_ll_xcvr_rfclk_enable(void);
+void ble_ll_xcvr_rfclk_disable(void);
+uint32_t ble_ll_xcvr_rfclk_time_till_settled(void);
+void ble_ll_xcvr_rfclk_timer_exp(void *arg);
+void ble_ll_xcvr_rfclk_timer_start(uint32_t cputime);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_LL_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/include/controller/ble_phy.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_phy.h b/net/nimble/controller/include/controller/ble_phy.h
index 674cbc5..e62b216 100644
--- a/net/nimble/controller/include/controller/ble_phy.h
+++ b/net/nimble/controller/include/controller/ble_phy.h
@@ -70,6 +70,7 @@ struct os_mbuf;
 #define BLE_PHY_ERR_INV_PARAM       (3)
 #define BLE_PHY_ERR_NO_BUFS         (4)
 #define BLE_PHY_ERR_TX_LATE         (5)
+#define BLE_PHY_ERR_RX_LATE         (6)
 
 /* Maximun PDU length. Includes LL header of 2 bytes and 255 bytes payload. */
 #define BLE_PHY_MAX_PDU_LEN         (257)
@@ -86,11 +87,16 @@ int ble_phy_reset(void);
 /* Set the PHY channel */
 int ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit);
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
 /* Set transmit start time */
-int ble_phy_tx_set_start_time(uint32_t cputime);
+int ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
 
 /* Set receive start time */
-int ble_phy_rx_set_start_time(uint32_t cputime);
+int ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
+#else
+/* Set transmit start time */
+int ble_phy_tx_set_start_time(uint32_t cputime);
+#endif
 
 /* Set the transmit end callback and argument */
 void ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg);
@@ -116,6 +122,27 @@ int ble_phy_txpwr_get(void);
 /* Disable the PHY */
 void ble_phy_disable(void);
 
+#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768)
+void ble_phy_stop_usec_timer(void);
+void ble_phy_wfr_enable(int txrx, uint32_t wfr_usecs);
+#define BLE_PHY_WFR_ENABLE_RX       (0)
+#define BLE_PHY_WFR_ENABLE_TXRX     (1)
+#else
+#define ble_phy_stop_usec_timer()
+#endif
+
+/* Starts rf clock */
+void ble_phy_rfclk_enable(void);
+
+/* Stops rf clock */
+void ble_phy_rfclk_disable(void);
+
+/*
+ * Used to restart reception on same channel after wfr timer expiration or
+ * frame received.
+ */
+void ble_phy_restart_rx(void);
+
 /* Gets the current state of the PHY */
 int ble_phy_state_get(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/src/ble_ll.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c
index 6071d3c..3204ff2 100644
--- a/net/nimble/controller/src/ble_ll.c
+++ b/net/nimble/controller/src/ble_ll.c
@@ -39,6 +39,7 @@
 #include "controller/ble_ll_hci.h"
 #include "controller/ble_ll_whitelist.h"
 #include "controller/ble_ll_resolv.h"
+#include "controller/ble_ll_xcvr.h"
 #include "ble_ll_conn_priv.h"
 
 /* XXX:
@@ -51,6 +52,11 @@
  * 4) Should look into always disabled the wfr interrupt if we receive the
  * start of a frame. Need to look at the various states to see if this is the
  * right thing to do.
+ * 5) I am not sure that if we are passed the output compare that we actually
+ * get the interrupt. Test this.
+ * 6) I am not sure that if we receive a packet while scanning that we actually
+ * go back to scanning. I need to make sure we re-enable the receive.
+ * Put an event in the log!
  */
 
 /* Supported states */
@@ -536,7 +542,9 @@ ble_ll_wfr_timer_exp(void *arg)
 void
 ble_ll_wfr_enable(uint32_t cputime)
 {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768
     os_cputime_timer_start(&g_ble_ll_data.ll_wfr_timer, cputime);
+#endif
 }
 
 /**
@@ -545,7 +553,9 @@ ble_ll_wfr_enable(uint32_t cputime)
 void
 ble_ll_wfr_disable(void)
 {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768
     os_cputime_timer_stop(&g_ble_ll_data.ll_wfr_timer);
+#endif
 }
 
 /**
@@ -786,7 +796,12 @@ ble_ll_rx_start(uint8_t *rxbuf, uint8_t chan, struct ble_mbuf_hdr *rxhdr)
     int rc;
     uint8_t pdu_type;
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    ble_ll_log(BLE_LL_LOG_ID_RX_START, chan, rxhdr->rem_usecs,
+               rxhdr->beg_cputime);
+#else
     ble_ll_log(BLE_LL_LOG_ID_RX_START, chan, 0, rxhdr->beg_cputime);
+#endif
 
     /* Check channel type */
     if (chan < BLE_PHY_NUM_DATA_CHANS) {
@@ -1167,6 +1182,11 @@ ble_ll_reset(void)
     /* Set state to standby */
     ble_ll_state_set(BLE_LL_STATE_STANDBY);
 
+#ifdef BLE_XCVR_RFCLK
+    /* Stops rf clock and rfclock timer */
+    ble_ll_xcvr_rfclk_stop();
+#endif
+
     /* Reset our random address */
     memset(g_random_addr, 0, BLE_DEV_ADDR_LEN);
 
@@ -1211,11 +1231,26 @@ ble_ll_init(void)
 {
     int rc;
     uint8_t features;
+#ifdef BLE_XCVR_RFCLK
+    uint32_t xtal_ticks;
+#endif
     struct ble_ll_obj *lldata;
 
     /* Ensure this function only gets called by sysinit. */
     SYSINIT_ASSERT_ACTIVE();
 
+#ifdef BLE_XCVR_RFCLK
+    /* Settling time of crystal, in ticks */
+    xtal_ticks = MYNEWT_VAL(BLE_XTAL_SETTLE_TIME);
+    assert(xtal_ticks != 0);
+    g_ble_ll_data.ll_xtal_ticks = os_cputime_usecs_to_ticks(xtal_ticks);
+
+    /* Initialize rf clock timer */
+    os_cputime_timer_init(&g_ble_ll_data.ll_rfclk_timer,
+                          ble_ll_xcvr_rfclk_timer_exp, NULL);
+
+#endif
+
     /* Get pointer to global data object */
     lldata = &g_ble_ll_data;
 
@@ -1241,9 +1276,11 @@ ble_ll_init(void)
                     ble_ll_hw_err_timer_cb,
                     NULL);
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768
     /* Initialize wait for response timer */
     os_cputime_timer_init(&g_ble_ll_data.ll_wfr_timer, ble_ll_wfr_timer_exp,
                           NULL);
+#endif
 
     /* Initialize LL HCI */
     ble_ll_hci_init();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/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 203f13a..8240d7a 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -430,8 +430,14 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
     assert(rc == 0);
 
     /* Set transmit start time. */
-    txstart = sch->start_time + os_cputime_usecs_to_ticks(XCVR_PROC_DELAY_USECS);
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    txstart = sch->start_time + g_ble_ll_sched_offset_ticks;
+    rc = ble_phy_tx_set_start_time(txstart, sch->remainder);
+#else
+    txstart = sch->start_time +
+        os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
     rc = ble_phy_tx_set_start_time(txstart);
+#endif
     if (rc) {
         STATS_INC(ble_ll_stats, adv_late_starts);
         goto adv_tx_done;
@@ -498,7 +504,7 @@ adv_tx_done:
 }
 
 static void
-ble_ll_adv_set_sched(struct ble_ll_adv_sm *advsm, int sched_new)
+ble_ll_adv_set_sched(struct ble_ll_adv_sm *advsm)
 {
     uint32_t max_usecs;
     struct ble_ll_sched_item *sch;
@@ -529,20 +535,15 @@ ble_ll_adv_set_sched(struct ble_ll_adv_sm *advsm, int sched_new)
      */
     max_usecs += XCVR_PROC_DELAY_USECS;
 
-    if (sched_new) {
-        /*
-         * We have to add the scheduling delay and tx start delay to the max
-         * time of the event since the pdu does not start at the scheduled start.
-         */
-        max_usecs += XCVR_TX_SCHED_DELAY_USECS;
-        sch->start_time = os_cputime_get32();
-        sch->end_time = sch->start_time + os_cputime_usecs_to_ticks(max_usecs);
-    } else {
-        sch->start_time = advsm->adv_pdu_start_time -
-            os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
-        sch->end_time = advsm->adv_pdu_start_time +
-            os_cputime_usecs_to_ticks(max_usecs);
-    }
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    sch->start_time = advsm->adv_pdu_start_time - g_ble_ll_sched_offset_ticks;
+    sch->remainder = 0;
+#else
+    sch->start_time = advsm->adv_pdu_start_time -
+        os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
+#endif
+    sch->end_time = advsm->adv_pdu_start_time +
+        os_cputime_usecs_to_ticks(max_usecs);
 }
 
 /**
@@ -745,6 +746,10 @@ ble_ll_adv_sm_stop(struct ble_ll_adv_sm *advsm)
             ble_ll_wfr_disable();
             ble_ll_state_set(BLE_LL_STATE_STANDBY);
             g_ble_ll_cur_adv_sm = NULL;
+            ble_ll_scan_chk_resume();
+#ifdef BLE_XCVR_RFCLK
+            ble_ll_sched_rfclk_chk_restart();
+#endif
         }
 #else
         if (ble_ll_state_get() == BLE_LL_STATE_ADV) {
@@ -752,6 +757,10 @@ ble_ll_adv_sm_stop(struct ble_ll_adv_sm *advsm)
             ble_ll_wfr_disable();
             ble_ll_state_set(BLE_LL_STATE_STANDBY);
             g_ble_ll_cur_adv_sm = NULL;
+            ble_ll_scan_chk_resume();
+#ifdef BLE_XCVR_RFCLK
+            ble_ll_sched_rfclk_chk_restart();
+#endif
         }
 #endif
         OS_EXIT_CRITICAL(sr);
@@ -872,10 +881,23 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
     advsm->adv_chan = adv_chan;
 
     /*
+     * XXX: while this may not be the most efficient, schedule the first
+     * advertising event some time in the future (5 msecs). This will give
+     * time to start up any clocks or anything and also avoid a bunch of code
+     * to check if we are currently doing anything. Just makes this simple.
+     *
+     * Might also want to align this on a slot in the future.
+     *
+     * NOTE: adv_event_start_time gets set by the sched_adv_new
+     */
+    advsm->adv_pdu_start_time = os_cputime_get32() +
+        os_cputime_usecs_to_ticks(BLE_LL_SCHED_MAX_TXRX_SLOT);
+
+    /*
      * Schedule advertising. We set the initial schedule start and end
      * times to the earliest possible start/end.
      */
-    ble_ll_adv_set_sched(advsm, 1);
+    ble_ll_adv_set_sched(advsm);
     ble_ll_sched_adv_new(&advsm->adv_sch);
 
     return BLE_ERR_SUCCESS;
@@ -884,9 +906,14 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
 void
 ble_ll_adv_scheduled(struct ble_ll_adv_sm *advsm, uint32_t sch_start)
 {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /* The event start time is when we start transmission of the adv PDU */
+    advsm->adv_event_start_time = sch_start + g_ble_ll_sched_offset_ticks;
+#else
     /* 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);
+#endif
 
     advsm->adv_pdu_start_time = advsm->adv_event_start_time;
 
@@ -1489,6 +1516,11 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
         /* Check if we need to resume scanning */
         ble_ll_scan_chk_resume();
 
+        /* Turn off the clock if not doing anything else */
+#ifdef BLE_XCVR_RFCLK
+        ble_ll_sched_rfclk_chk_restart();
+#endif
+
         /* This event is over. Set adv channel to first one */
         advsm->adv_chan = ble_ll_adv_first_chan(advsm);
 
@@ -1511,8 +1543,12 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
          * The scheduled time better be in the future! If it is not, we will
          * just keep advancing until we the time is in the future
          */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        start_time = advsm->adv_pdu_start_time - g_ble_ll_sched_offset_ticks;
+#else
         start_time = advsm->adv_pdu_start_time -
             os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
+#endif
 
         delta_t = (int32_t)(start_time - os_cputime_get32());
         if (delta_t < 0) {
@@ -1543,9 +1579,13 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
          * We will transmit right away. Set next pdu start time to now
          * plus a xcvr start delay just so we dont count late adv starts
          */
-        advsm->adv_pdu_start_time = os_cputime_get32() +
+        advsm->adv_pdu_start_time = os_cputime_get32();
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        advsm->adv_pdu_start_time += g_ble_ll_sched_offset_ticks;
+#else
+        advsm->adv_pdu_start_time +=
             os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
-
+#endif
         resched_pdu = 1;
     }
 
@@ -1571,7 +1611,7 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
 #endif
 
     /* Schedule advertising transmit */
-    ble_ll_adv_set_sched(advsm, 0);
+    ble_ll_adv_set_sched(advsm);
 
     /*
      * In the unlikely event we cant reschedule this, just post a done
@@ -1580,9 +1620,15 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
     if (resched_pdu) {
         rc = ble_ll_sched_adv_resched_pdu(&advsm->adv_sch);
     } else {
+        /* Reschedule advertising event */
         rc = ble_ll_sched_adv_reschedule(&advsm->adv_sch, &start_time,
                                          max_delay_ticks);
         if (!rc) {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+            start_time += g_ble_ll_sched_offset_ticks;
+#else
+            start_time += os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
+#endif
             advsm->adv_event_start_time = start_time;
             advsm->adv_pdu_start_time = start_time;
         }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/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 1d2714a..d7f926e 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -95,15 +95,26 @@ extern void bletest_completed_pkt(uint16_t handle);
  * response. Well, it should. If this packet will overrun the next scheduled
  * event, what should we do? Transmit anyway? Not transmit? For now, we just
  * transmit.
+ *
+ * 32kHz crystal
+ * 1) When scheduling, I need to make sure I have time between
+ * this one and the next. Should I deal with this in the sched. Or
+ * is this basically accounted for given a slot? I really just need to
+ * make sure everything is over N ticks before the next sched start!
+ * Just add to end time?
+ *
+ * 2) I think one way to handle the problem of losing up to a microsecond
+ * every time we call ble_ll_conn_next_event in a loop is to do everything by
+ * keeping track of last anchor point. Would need last anchor usecs too. I guess
+ * we could also keep last anchor usecs as a uint32 or something and when we
+ * do the next event keep track of the residual using a different ticks to
+ * usecs calculation. Not sure.
  */
 
 /*
  * XXX: How should we deal with a late connection event? We need to determine
  * what we want to do under the following cases:
  *  1) The current connection event has not ended but a schedule item starts
- *  2) The connection event start cb is called but we are later than we
- *  expected. What to do? If we cant transmit at correct point in slot we
- *  are hosed. Well, anchor point can get really messed up!
  */
 
 /*
@@ -218,6 +229,25 @@ STATS_NAME_END(ble_ll_conn_stats)
 
 static void ble_ll_conn_event_end(struct os_event *ev);
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+static void
+ble_ll_conn_calc_itvl_ticks(struct ble_ll_conn_sm *connsm)
+{
+    uint32_t ticks;
+    uint32_t usecs;
+
+    /*
+     * Precalculate the number of ticks and remaining microseconds for
+     * the connection interval
+     */
+    usecs = connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS;
+    ticks = os_cputime_usecs_to_ticks(usecs);
+    connsm->conn_itvl_ticks = ticks;
+    connsm->conn_itvl_usecs = (uint8_t)(usecs -
+                                        os_cputime_ticks_to_usecs(ticks));
+}
+#endif
+
 /**
  * Get the event buffer allocated to send the connection complete event
  * when we are initiating.
@@ -402,7 +432,6 @@ ble_ll_conn_calc_window_widening(struct ble_ll_conn_sm *connsm)
         window_widening = (total_sca_ppm * delta_msec) / 1000;
     }
 
-    /* XXX: spec gives 16 usecs error btw. Probably should add that in */
     return window_widening;
 }
 
@@ -689,7 +718,10 @@ ble_ll_conn_continue_rx_encrypt(void *arg)
  * the current connection event. The current connection event must end before
  * the next scheduled item. However, the current connection itself is not
  * in the scheduler list! Thus, we need to calculate the time at which the
- * next connection will start and not overrun it.
+ * next connection will start (the schedule start time; not the anchor point)
+ * and not overrun it.
+ *
+ * Context: Interrupt
  *
  * @param connsm
  *
@@ -698,13 +730,25 @@ ble_ll_conn_continue_rx_encrypt(void *arg)
 static uint32_t
 ble_ll_conn_get_next_sched_time(struct ble_ll_conn_sm *connsm)
 {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768
     uint32_t itvl;
+#endif
     uint32_t ce_end;
     uint32_t next_sched_time;
 
     /* Calculate time at which next connection event will start */
-    itvl = connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /* NOTE: We dont care if this time is tick short. */
+    ce_end = connsm->anchor_point + connsm->conn_itvl_ticks -
+        g_ble_ll_sched_offset_ticks;
+    if ((connsm->anchor_point_usecs + connsm->conn_itvl_usecs) >= 31) {
+        ++ce_end;
+    }
+#else
+    itvl = (connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS) -
+        XCVR_TX_SCHED_DELAY_USECS;
     ce_end = connsm->anchor_point + os_cputime_usecs_to_ticks(itvl);
+#endif
 
     if (ble_ll_sched_next_time(&next_sched_time)) {
         if (CPUTIME_LT(next_sched_time, ce_end)) {
@@ -1121,8 +1165,10 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
 {
     int rc;
     uint32_t usecs;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768
     uint32_t wfr_time;
-    uint32_t txstart;
+#endif
+    uint32_t start;
     struct ble_ll_conn_sm *connsm;
 
     /* XXX: note that we can extend end time here if we want. Look at this */
@@ -1151,9 +1197,15 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
 #endif
 
     if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        /* Set start time of transmission */
+        start = sch->start_time + g_ble_ll_sched_offset_ticks;
+        rc = ble_phy_tx_set_start_time(start, sch->remainder);
+#else
         /* Set start time of transmission */
-        txstart = sch->start_time + os_cputime_usecs_to_ticks(XCVR_PROC_DELAY_USECS);
-        rc = ble_phy_tx_set_start_time(txstart);
+        start = sch->start_time + os_cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
+        rc = ble_phy_tx_set_start_time(start);
+#endif
         if (!rc) {
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
             if (CONN_F_ENCRYPTED(connsm)) {
@@ -1187,11 +1239,13 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
                 ble_phy_encrypt_disable();
             }
 #endif
-        /*
-         * XXX: make sure I dont care that I get here early to start receiving.
-         * I could use events compare and all that shit to start rx.
-         */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        /* XXX: what is this really for the slave? */
+        start = sch->start_time + g_ble_ll_sched_offset_ticks;
+        rc = ble_phy_rx_set_start_time(start, sch->remainder);
+#else
         rc = ble_phy_rx();
+#endif
         if (rc) {
             /* End the connection event as we have no more buffers */
             STATS_INC(ble_ll_conn_stats, slave_ce_failures);
@@ -1207,15 +1261,35 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
              * Set the wait for response time. The anchor point is when we
              * expect the master to start transmitting. Worst-case, we expect
              * to hear a reply within the anchor point plus:
-             *  -> the current tx window size
-             *  -> The current window widening amount
+             *  -> current tx window size
+             *  -> current window widening amount (includes +/- 16 usec jitter)
              *  -> Amount of time it takes to detect packet start.
+             *  -> Some extra time (16 usec) to insure timing is OK
+             */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+            /*
+             * For the 32 kHz crystal, the amount of usecs we have to wait
+             * is not from the anchor point; we have to account for the time
+             * from when the receiver is enabled until the anchor point. The
+             * time we start before the anchor point is this:
+             *   -> current window widening.
+             *   -> up to one 32 kHz tick since we discard remainder.
+             *   -> Up to one tick since the usecs to ticks calc can be off
+             *   by up to one tick.
+             * NOTES:
+             * 1) the 61 we add is for the two ticks mentioned above.
+             * 2) The address rx time and jitter is accounted for in the
+             * phy function
              */
+            usecs = connsm->slave_cur_tx_win_usecs + 61 +
+                (2 * connsm->slave_cur_window_widening);
+            ble_phy_wfr_enable(BLE_PHY_WFR_ENABLE_RX, usecs);
+#else
             usecs = connsm->slave_cur_tx_win_usecs + BLE_LL_WFR_USECS +
                 connsm->slave_cur_window_widening;
             wfr_time = connsm->anchor_point + os_cputime_usecs_to_ticks(usecs);
             ble_ll_wfr_enable(wfr_time);
-
+#endif
             /* Set next wakeup time to connection event end time */
             rc = BLE_LL_SCHED_STATE_RUNNING;
         }
@@ -1246,15 +1320,20 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
  * @return int 0: not allowed to send 1: allowed to send
  */
 static int
-ble_ll_conn_can_send_next_pdu(struct ble_ll_conn_sm *connsm, uint32_t begtime)
+ble_ll_conn_can_send_next_pdu(struct ble_ll_conn_sm *connsm, uint32_t begtime,
+                              uint32_t add_usecs)
 {
     int rc;
     uint8_t rem_bytes;
     uint32_t ticks;
+    uint32_t usecs;
     uint32_t next_sched_time;
     struct os_mbuf *txpdu;
     struct os_mbuf_pkthdr *pkthdr;
     struct ble_mbuf_hdr *txhdr;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    uint32_t allowed_usecs;
+#endif
 
     rc = 1;
     if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
@@ -1277,16 +1356,25 @@ ble_ll_conn_can_send_next_pdu(struct ble_ll_conn_sm *connsm, uint32_t begtime)
             if (rem_bytes > connsm->eff_max_tx_octets) {
                 rem_bytes = connsm->eff_max_tx_octets;
             }
-            ticks = BLE_TX_DUR_USECS_M(rem_bytes);
+            usecs = BLE_TX_DUR_USECS_M(rem_bytes);
         } else {
             /* We will send empty pdu (just a LL header) */
-            ticks = BLE_TX_DUR_USECS_M(0);
+            usecs = BLE_TX_DUR_USECS_M(0);
         }
-        ticks += (BLE_LL_IFS * 2) + connsm->eff_max_rx_time;
-        ticks = os_cputime_usecs_to_ticks(ticks);
+        usecs += (BLE_LL_IFS * 2) + connsm->eff_max_rx_time;
+
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        ticks = (uint32_t)(next_sched_time - begtime);
+        allowed_usecs = os_cputime_ticks_to_usecs(ticks);
+        if ((usecs + add_usecs) >= allowed_usecs) {
+            rc = 0;
+        }
+#else
+        ticks = os_cputime_usecs_to_ticks(usecs);
         if ((begtime + ticks) >= next_sched_time) {
             rc = 0;
         }
+#endif
     }
 
     return rc;
@@ -1347,7 +1435,15 @@ ble_ll_conn_master_init(struct ble_ll_conn_sm *connsm,
     connsm->conn_role = BLE_LL_CONN_ROLE_MASTER;
 
     /* Set default ce parameters */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /*
+     * XXX: for now, we need twice the transmit window as our calculations
+     * for the transmit window offset could be off.
+     */
+    connsm->tx_win_size = BLE_LL_CONN_TX_WIN_MIN + 1;
+#else
     connsm->tx_win_size = BLE_LL_CONN_TX_WIN_MIN;
+#endif
     connsm->tx_win_off = 0;
     connsm->master_sca = MYNEWT_VAL(BLE_LL_MASTER_SCA);
 
@@ -1482,6 +1578,10 @@ ble_ll_conn_sm_new(struct ble_ll_conn_sm *connsm)
                     connsm);
 #endif
 
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    ble_ll_conn_calc_itvl_ticks(connsm);
+#endif
+
     /* Add to list of active connections */
     SLIST_INSERT_HEAD(&g_ble_ll_conn_active_list, connsm, act_sle);
 }
@@ -1629,6 +1729,11 @@ ble_ll_conn_next_event(struct ble_ll_conn_sm *connsm)
     uint32_t cur_ww;
     uint32_t max_ww;
     struct ble_ll_conn_upd_req *upd;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    uint32_t ticks;
+    uint32_t usecs;
+#endif
+
 
     /* XXX: deal with connection request procedure here as well */
     ble_ll_conn_chk_csm_flags(connsm);
@@ -1647,7 +1752,24 @@ ble_ll_conn_next_event(struct ble_ll_conn_sm *connsm)
     connsm->event_cntr += latency;
 
     /* Set next connection event start time */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    /* We can use pre-calculated values for one interval if latency is 1. */
+    if (latency == 1) {
+        connsm->anchor_point += connsm->conn_itvl_ticks;
+        connsm->anchor_point_usecs += connsm->conn_itvl_usecs;
+    } else {
+        uint32_t ticks;
+        ticks = os_cputime_usecs_to_ticks(itvl);
+        connsm->anchor_point += ticks;
+        connsm->anchor_point_usecs += (itvl - os_cputime_ticks_to_usecs(ticks));
+    }
+    if (connsm->anchor_point_usecs >= 31) {
+        ++connsm->anchor_point;
+        connsm->anchor_point_usecs -= 31;
+    }
+#else
     connsm->anchor_point += os_cputime_usecs_to_ticks(itvl);
+#endif
 
     /*
      * If a connection update has been scheduled and the event counter
@@ -1669,15 +1791,30 @@ ble_ll_conn_next_event(struct ble_ll_conn_sm *connsm)
             connsm->csmflags.cfbit.host_expects_upd_event = 1;
         }
 
-        connsm->conn_itvl = upd->interval;
         connsm->supervision_tmo = upd->timeout;
         connsm->slave_latency = upd->latency;
         connsm->tx_win_size = upd->winsize;
         connsm->slave_cur_tx_win_usecs =
             connsm->tx_win_size * BLE_LL_CONN_TX_WIN_USECS;
         connsm->tx_win_off = upd->winoffset;
+        connsm->conn_itvl = upd->interval;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        ble_ll_conn_calc_itvl_ticks(connsm);
+        if (upd->winoffset != 0) {
+            usecs = upd->winoffset * BLE_LL_CONN_ITVL_USECS;
+            ticks = os_cputime_usecs_to_ticks(usecs);
+            connsm->anchor_point += ticks;
+            usecs = usecs - os_cputime_ticks_to_usecs(ticks);
+            connsm->anchor_point_usecs += usecs;
+            if (connsm->anchor_point_usecs >= 31) {
+                ++connsm->anchor_point;
+                connsm->anchor_point_usecs -= 31;
+            }
+        }
+#else
         connsm->anchor_point +=
             os_cputime_usecs_to_ticks(upd->winoffset * BLE_LL_CONN_ITVL_USECS);
+#endif
 
         /* Reset the starting point of the connection supervision timeout */
         connsm->last_rxd_pdu_cputime = connsm->anchor_point;
@@ -1736,20 +1873,35 @@ ble_ll_conn_next_event(struct ble_ll_conn_sm *connsm)
      * Calculate ce end time. For a slave, we need to add window widening and
      * the transmit window if we still have one.
      */
-    itvl = MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_USECS_PER_SLOT;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    itvl = MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_32KHZ_TICKS_PER_SLOT;
     if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
         cur_ww = ble_ll_conn_calc_window_widening(connsm);
         max_ww = (connsm->conn_itvl * (BLE_LL_CONN_ITVL_USECS/2)) - BLE_LL_IFS;
         if (cur_ww >= max_ww) {
             return -1;
         }
+        cur_ww += BLE_LL_JITTER_USECS;
         connsm->slave_cur_window_widening = cur_ww;
+        itvl += os_cputime_usecs_to_ticks(cur_ww + connsm->slave_cur_tx_win_usecs);
+    }
+    itvl -= g_ble_ll_sched_offset_ticks;
+    connsm->ce_end_time = connsm->anchor_point + itvl;
+#else
+    itvl = MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_USECS_PER_SLOT;
+    if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
+        cur_ww = ble_ll_conn_calc_window_widening(connsm);
+        max_ww = (connsm->conn_itvl * (BLE_LL_CONN_ITVL_USECS/2)) - BLE_LL_IFS;
+        if (cur_ww >= max_ww) {
+            return -1;
+        }
+        connsm->slave_cur_window_widening = cur_ww + BLE_LL_JITTER_USECS;
         itvl += cur_ww + connsm->slave_cur_tx_win_usecs;
     } else {
-        /* We adjust end time for connection to end of time slot */
         itvl -= XCVR_TX_SCHED_DELAY_USECS;
     }
     connsm->ce_end_time = connsm->anchor_point + os_cputime_usecs_to_ticks(itvl);
+#endif
 
     return 0;
 }
@@ -1799,9 +1951,38 @@ ble_ll_conn_created(struct ble_ll_conn_sm *connsm, struct ble_mbuf_hdr *rxhdr)
      */
     rc = 1;
     if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        /*
+         * With a 32.768 kHz crystal we dont care about the remaining usecs
+         * when setting last anchor point. The only thing last anchor is used
+         * for is to calculate window widening. The effect of this is
+         * negligible.
+         */
+        connsm->last_anchor_point = rxhdr->beg_cputime;
+
+        usecs = rxhdr->rem_usecs + 1250 +
+            (connsm->tx_win_off * BLE_LL_CONN_TX_WIN_USECS) +
+            BLE_TX_DUR_USECS_M(BLE_CONNECT_REQ_LEN);
+
+        /* Anchor point is cputime. */
+        endtime = os_cputime_usecs_to_ticks(usecs);
+        connsm->anchor_point = rxhdr->beg_cputime + endtime;
+        connsm->anchor_point_usecs = usecs - os_cputime_ticks_to_usecs(endtime);
+        if (connsm->anchor_point_usecs == 31) {
+            ++connsm->anchor_point;
+            connsm->anchor_point_usecs = 0;
+        }
+
+        connsm->slave_cur_tx_win_usecs =
+            connsm->tx_win_size * BLE_LL_CONN_TX_WIN_USECS;
+
+        connsm->ce_end_time = connsm->anchor_point +
+            (MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_32KHZ_TICKS_PER_SLOT)
+            + os_cputime_usecs_to_ticks(connsm->slave_cur_tx_win_usecs) + 1;
+#else
+        connsm->last_anchor_point = rxhdr->beg_cputime;
         endtime = rxhdr->beg_cputime +
             os_cputime_usecs_to_ticks(BLE_TX_DUR_USECS_M(BLE_CONNECT_REQ_LEN));
-        connsm->last_anchor_point = endtime;
         connsm->slave_cur_tx_win_usecs =
             connsm->tx_win_size * BLE_LL_CONN_TX_WIN_USECS;
         usecs = 1250 + (connsm->tx_win_off * BLE_LL_CONN_TX_WIN_USECS);
@@ -1810,7 +1991,8 @@ ble_ll_conn_created(struct ble_ll_conn_sm *connsm, struct ble_mbuf_hdr *rxhdr)
             (MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_USECS_PER_SLOT);
         connsm->ce_end_time = connsm->anchor_point +
             os_cputime_usecs_to_ticks(usecs);
-        connsm->slave_cur_window_widening = 0;
+#endif
+        connsm->slave_cur_window_widening = BLE_LL_JITTER_USECS;
 
         /* Start the scheduler for the first connection event */
         while (ble_ll_sched_slave_new(connsm)) {
@@ -1870,6 +2052,10 @@ ble_ll_conn_event_end(struct os_event *ev)
     /* Check if we need to resume scanning */
     ble_ll_scan_chk_resume();
 
+#ifdef BLE_XCVR_RFCLK
+    ble_ll_sched_rfclk_chk_restart();
+#endif
+
     /* If we have transmitted the terminate IND successfully, we are done */
     if ((connsm->csmflags.cfbit.terminate_ind_txd) ||
         (connsm->csmflags.cfbit.terminate_ind_rxd)) {
@@ -1958,6 +2144,7 @@ ble_ll_conn_event_end(struct os_event *ev)
     } else {
         tmo = connsm->supervision_tmo * BLE_HCI_CONN_SPVN_TMO_UNITS * 1000UL;
     }
+    /* XXX: Convert to ticks to usecs calculation instead??? */
     tmo = os_cputime_usecs_to_ticks(tmo);
     if ((int32_t)(connsm->anchor_point - connsm->last_rxd_pdu_cputime) >= tmo) {
         ble_ll_conn_end(connsm, BLE_ERR_CONN_SPVN_TMO);
@@ -2251,7 +2438,6 @@ ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
     uint8_t *init_addr = NULL;
     uint8_t pyld_len;
     uint8_t inita_is_rpa;
-    uint32_t endtime;
     struct os_mbuf *rxpdu;
     struct ble_ll_conn_sm *connsm;
 
@@ -2366,10 +2552,7 @@ ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
         }
 
         /* Attempt to schedule new connection. Possible that this might fail */
-        endtime = ble_hdr->beg_cputime +
-            os_cputime_usecs_to_ticks(BLE_TX_DUR_USECS_M(pyld_len));
-        if (!ble_ll_sched_master_new(connsm, endtime,
-                                     MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS))) {
+        if (!ble_ll_sched_master_new(connsm, ble_hdr, pyld_len)) {
             /* Setup to transmit the connect request */
             rc = ble_ll_conn_request_send(addr_type, adv_addr,
                                           connsm->tx_win_off, index);
@@ -2392,8 +2575,6 @@ init_rx_isr_exit:
      */
     rxpdu = ble_ll_rxpdu_alloc(pyld_len + BLE_LL_PDU_HDR_LEN);
     if (rxpdu == NULL) {
-        ble_phy_disable();
-
         /*
          * XXX: possible allocate the PDU when we start initiating?
          * I cannot say I like this solution, but if we cannot allocate a PDU
@@ -2405,8 +2586,7 @@ init_rx_isr_exit:
             CONN_F_CONN_REQ_TXD(connsm) = 0;
             ble_ll_sched_rmv_elem(&connsm->conn_sch);
         }
-
-        ble_phy_rx();
+        ble_phy_restart_rx();
         rc = 0;
     } else {
         ble_phy_rxpdu_copy(rxbuf, rxpdu);
@@ -2498,6 +2678,9 @@ ble_ll_conn_rx_isr_start(struct ble_mbuf_hdr *rxhdr, uint32_t aa)
             connsm->csmflags.cfbit.slave_set_last_anchor = 0;
             connsm->last_anchor_point = rxhdr->beg_cputime;
             connsm->anchor_point = connsm->last_anchor_point;
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+            connsm->anchor_point_usecs = rxhdr->rem_usecs;
+#endif
         }
     }
     return 1;
@@ -2659,6 +2842,7 @@ ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
     uint8_t opcode = 0;
     uint8_t rx_pyld_len;
     uint32_t endtime;
+    uint32_t add_usecs;
     struct os_mbuf *txpdu;
     struct ble_ll_conn_sm *connsm;
     struct os_mbuf *rxpdu;
@@ -2689,8 +2873,14 @@ ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
     }
 
     /* Calculate the end time of the received PDU */
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+    endtime = rxhdr->beg_cputime;
+    add_usecs = rxhdr->rem_usecs + BLE_TX_DUR_USECS_M(rx_pyld_len);
+#else
     endtime = rxhdr->beg_cputime +
         os_cputime_usecs_to_ticks(BLE_TX_DUR_USECS_M(rx_pyld_len));
+    add_usecs = 0;
+#endif
 
     /*
      * Check the packet CRC. A connection event can continue even if the
@@ -2861,7 +3051,7 @@ chk_rx_terminate_ind:
     if (rx_pyld_len && CONN_F_ENCRYPTED(connsm)) {
         rx_pyld_len += BLE_LL_DATA_MIC_LEN;
     }
-    if (reply && ble_ll_conn_can_send_next_pdu(connsm, endtime)) {
+    if (reply && ble_ll_conn_can_send_next_pdu(connsm, endtime, add_usecs)) {
         rc = ble_ll_conn_tx_data_pdu(connsm);
     }
 
@@ -3045,7 +3235,6 @@ ble_ll_conn_set_global_chanmap(uint8_t num_used_chans, uint8_t *chanmap)
  * Context: Link Layer
  *
  * @param rxbuf Pointer to received Connect Request PDU
- * @param conn_req_end receive end time of connect request
  *
  * @return 0: connection not started; 1 connecton started
  */
@@ -3269,3 +3458,5 @@ ble_ll_conn_module_init(void)
     /* Call reset to finish reset of initialization */
     ble_ll_conn_module_reset();
 }
+
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/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 0913892..02cac60 100644
--- a/net/nimble/controller/src/ble_ll_conn_priv.h
+++ b/net/nimble/controller/src/ble_ll_conn_priv.h
@@ -42,12 +42,13 @@ extern "C" {
 #define BLE_LL_CONN_INITIAL_OFFSET          (1250)
 #define BLE_LL_CONN_ITVL_USECS              (1250)
 #define BLE_LL_CONN_TX_WIN_USECS            (1250)
+#define BLE_LL_CONN_TX_OFF_USECS            (1250)
 #define BLE_LL_CONN_CE_USECS                (625)
 #define BLE_LL_CONN_TX_WIN_MIN              (1)         /* in tx win units */
 #define BLE_LL_CONN_SLAVE_LATENCY_MAX       (499)
 
 /* Connection request duration (in usecs) */
-#define BLE_LL_CONN_REQ_DURATION            (352)
+#define BLE_LL_CONN_REQ_DURATION            (352)       /* 1 Mbps only */
 
 /* Connection handle range */
 #define BLE_LL_CONN_MAX_CONN_HANDLE         (0x0EFF)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/507f493a/net/nimble/controller/src/ble_ll_scan.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_scan.c b/net/nimble/controller/src/ble_ll_scan.c
index f732753..f5eca7f 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -37,6 +37,7 @@
 #include "controller/ble_ll_hci.h"
 #include "controller/ble_ll_whitelist.h"
 #include "controller/ble_ll_resolv.h"
+#include "controller/ble_ll_xcvr.h"
 #include "hal/hal_gpio.h"
 
 /*
@@ -588,8 +589,14 @@ ble_ll_scan_start(struct ble_ll_scan_sm *scansm, uint8_t chan)
     }
 #endif
 
-    /* Start receiving */
-    rc = ble_phy_rx();
+#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
+        /* XXX: probably need to make sure hfxo is running too */
+        /* XXX: can make this better; want to just start asap. */
+        rc = ble_phy_rx_set_start_time(os_cputime_get32() +
+                                       g_ble_ll_sched_offset_ticks, 0);
+#else
+        rc = ble_phy_rx();
+#endif
     if (!rc) {
         /* Enable/disable whitelisting */
         if (scansm->scan_filt_policy & 1) {
@@ -612,6 +619,37 @@ ble_ll_scan_start(struct ble_ll_scan_sm *scansm, uint8_t chan)
     }
 }
 
+#ifdef BLE_XCVR_RFCLK
+static void
+ble_ll_scan_rfclk_chk_stop(void)
+{
+    int stop;
+    int32_t time_till_next;
+    os_sr_t sr;
+    uint32_t next_time;
+
+    stop = 0;
+    OS_ENTER_CRITICAL(sr);
+    if (ble_ll_sched_next_time(&next_time)) {
+        /*
+         * If the time until the next event is too close, dont bother to turn
+         * off the clock
+         */
+        time_till_next = (int32_t)(next_time - os_cputime_get32());
+        if (time_till_next > g_ble_ll_data.ll_xtal_ticks) {
+            stop = 1;
+        }
+    } else {
+        stop = 1;
+    }
+    if (stop) {
+        ble_ll_log(BLE_LL_LOG_ID_RFCLK_SCAN_DIS, g_ble_ll_data.ll_rfclk_state,0,0);
+        ble_ll_xcvr_rfclk_disable();
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+#endif
+
 /**
  * Called to determine if we are inside or outside the scan window. If we
  * are inside the scan window it means that the device should be receiving
@@ -629,6 +667,7 @@ ble_ll_scan_window_chk(struct ble_ll_scan_sm *scansm, uint32_t cputime)
     int rc;
     uint8_t chan;
     uint32_t itvl;
+    uint32_t dt;
     uint32_t win_start;
 
     itvl = os_cputime_usecs_to_ticks(scansm->scan_itvl * BLE_HCI_SCAN_ITVL);
@@ -645,7 +684,13 @@ ble_ll_scan_window_chk(struct ble_ll_scan_sm *scansm, uint32_t cputime)
     rc = 0;
     if (scansm->scan_window != scansm->scan_itvl) {
         itvl = os_cputime_usecs_to_ticks(scansm->scan_window * BLE_HCI_SCAN_ITVL);
-        if ((cputime - win_start) >= itvl) {
+        dt = cputime - win_start;
+        if (dt >= itvl) {
+#ifdef BLE_XCVR_RFCLK
+            if (dt < (scansm->scan_itvl - g_ble_ll_data.ll_xtal_ticks)) {
+                ble_ll_scan_rfclk_chk_stop();
+            }
+#endif
             rc = 1;
         }
     }
@@ -688,6 +733,11 @@ ble_ll_scan_sm_stop(int chk_disable)
 
             /* Set LL state to standby */
             ble_ll_state_set(BLE_LL_STATE_STANDBY);
+
+            /* May need to stop the rfclk */
+#ifdef BLE_XCVR_RFCLK
+            ble_ll_scan_rfclk_chk_stop();
+#endif
         }
         OS_EXIT_CRITICAL(sr);
     }
@@ -757,6 +807,9 @@ ble_ll_scan_event_proc(struct os_event *ev)
     uint32_t win_start;
     uint32_t scan_itvl;
     uint32_t next_event_time;
+#ifdef BLE_XCVR_RFCLK
+    uint32_t xtal_ticks;
+#endif
     struct ble_ll_scan_sm *scansm;
 
     /*
@@ -786,12 +839,19 @@ ble_ll_scan_event_proc(struct os_event *ev)
     dt = now - win_start;
     scansm->scan_chan = chan;
     scansm->scan_win_start_time = win_start;
+    if (scansm->scan_window != scansm->scan_itvl) {
+        win = os_cputime_usecs_to_ticks(scansm->scan_window * BLE_HCI_SCAN_ITVL);
+    } else {
+        win = 0;
+    }
 
     /* Determine on/off state based on scan window */
     rxstate = 1;
     next_event_time = win_start + scan_itvl;
-    if (scansm->scan_window != scansm->scan_itvl) {
-        win = os_cputime_usecs_to_ticks(scansm->scan_window * BLE_HCI_SCAN_ITVL);
+
+    OS_ENTER_CRITICAL(sr);
+
+    if (win != 0) {
         if (dt >= win) {
             rxstate = 0;
         } else {
@@ -799,7 +859,6 @@ ble_ll_scan_event_proc(struct os_event *ev)
         }
     }
 
-    OS_ENTER_CRITICAL(sr);
     /*
      * If we are not in the standby state it means that the scheduled
      * scanning event was overlapped in the schedule. In this case all we do
@@ -815,6 +874,9 @@ ble_ll_scan_event_proc(struct os_event *ev)
     case BLE_LL_STATE_SCANNING:
         /* Must disable PHY since we will move to a new channel */
         ble_phy_disable();
+        if (!rxstate) {
+            ble_ll_state_set(BLE_LL_STATE_STANDBY);
+        }
         break;
     case BLE_LL_STATE_STANDBY:
         break;
@@ -822,11 +884,70 @@ ble_ll_scan_event_proc(struct os_event *ev)
         assert(0);
         break;
     }
+
+#ifdef BLE_XCVR_RFCLK
+    if (rxstate == 0) {
+        /*
+         * We need to wake up before we need to start scanning in order
+         * to make sure the rfclock is on. If we are close to being on,
+         * enable the rfclock. If not, set wakeup time.
+         */
+        if (dt >= (scan_itvl - g_ble_ll_data.ll_xtal_ticks)) {
+            /* Start the clock if necessary */
+            if (start_scan) {
+                if (ble_ll_xcvr_rfclk_state() == BLE_RFCLK_STATE_OFF) {
+                    ble_ll_xcvr_rfclk_start_now(now);
+                    next_event_time = now + g_ble_ll_data.ll_xtal_ticks;
+                }
+            }
+        } else {
+            next_event_time -= g_ble_ll_data.ll_xtal_ticks;
+            if (start_scan) {
+                ble_ll_scan_rfclk_chk_stop();
+            }
+        }
+    }
+#endif
+
     if (start_scan && rxstate) {
+#ifdef BLE_XCVR_RFCLK
+        /* NOTE: reuse rxstate */
+        rxstate = ble_ll_xcvr_rfclk_state();
+        if (rxstate != BLE_RFCLK_STATE_SETTLED) {
+            if (rxstate == BLE_RFCLK_STATE_OFF) {
+                xtal_ticks = g_ble_ll_data.ll_xtal_ticks;
+            } else {
+                xtal_ticks = ble_ll_xcvr_rfclk_time_till_settled();
+            }
+
+            /*
+             * Only bother if we have enough time to receive anything
+             * here. The next event time will turn off the clock.
+             */
+            if (win != 0) {
+                if ((win - dt) <= xtal_ticks)  {
+                    goto rfclk_not_settled;
+                }
+            }
+
+            /*
+             * If clock off, start clock. Set next event time to now plus
+             * the clock setting time.
+             */
+            if (rxstate == BLE_RFCLK_STATE_OFF) {
+                ble_ll_xcvr_rfclk_start_now(now);
+            }
+            next_event_time = now + xtal_ticks;
+            goto rfclk_not_settled;
+        }
+#endif
         ble_ll_scan_start(scansm, scansm->scan_chan);
     }
-    OS_EXIT_CRITICAL(sr);
 
+#ifdef BLE_XCVR_RFCLK
+rfclk_not_settled:
+#endif
+    OS_EXIT_CRITICAL(sr);
     os_cputime_timer_start(&scansm->scan_timer, next_event_time);
 }
 
@@ -929,8 +1050,7 @@ ble_ll_scan_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok)
         if (scansm->scan_rsp_pending) {
             ble_ll_scan_req_backoff(scansm, 0);
         }
-        ble_phy_disable();
-        ble_phy_rx();
+        ble_phy_restart_rx();
         return 0;
     }
 
@@ -1081,8 +1201,6 @@ ble_ll_scan_wfr_timer_exp(void)
 {
     struct ble_ll_scan_sm *scansm;
 
-    ble_phy_disable();
-
     /*
      * If we timed out waiting for a response, the scan response pending
      * flag should be set. Deal with scan backoff. Put device back into rx.
@@ -1091,7 +1209,8 @@ ble_ll_scan_wfr_timer_exp(void)
     if (scansm->scan_rsp_pending) {
         ble_ll_scan_req_backoff(scansm, 0);
     }
-    ble_phy_rx();
+
+    ble_phy_restart_rx();
 }
 
 /**
@@ -1480,7 +1599,7 @@ ble_ll_scan_init(void)
     scansm->scan_itvl = BLE_HCI_SCAN_ITVL_DEF;
     scansm->scan_window = BLE_HCI_SCAN_WINDOW_DEF;
 
-    /* Initialize connection supervision timer */
+    /* Initialize scanning timer */
     os_cputime_timer_init(&scansm->scan_timer, ble_ll_scan_timer_cb, scansm);
 
     /* Get a scan request mbuf (packet header) and attach to state machine */


[31/37] incubator-mynewt-core git commit: MYNEWT-698: Unhandled exception in controller in ble_ll_conn_end

Posted by we...@apache.org.
MYNEWT-698: Unhandled exception in controller in ble_ll_conn_end

Not sure what happened but only some of the fix was committed
in the prior commit. This is the actual fix for this issue.


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

Branch: refs/heads/nrf_cputime
Commit: 612f8efe462f27641cc4fd87993f7a3a079ec67b
Parents: 3538765
Author: William San Filippo <wi...@runtime.io>
Authored: Wed Mar 29 21:07:33 2017 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Wed Mar 29 21:07:33 2017 -0700

----------------------------------------------------------------------
 net/nimble/controller/src/ble_ll_conn.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/612f8efe/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 0ca72d0..171d938 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -2396,6 +2396,19 @@ init_rx_isr_exit:
     rxpdu = ble_ll_rxpdu_alloc(pyld_len + BLE_LL_PDU_HDR_LEN);
     if (rxpdu == NULL) {
         ble_phy_disable();
+
+        /*
+         * XXX: possible allocate the PDU when we start initiating?
+         * I cannot say I like this solution, but if we cannot allocate a PDU
+         * to hand up to the LL, we need to remove the connection we just
+         * scheduled since the connection state machine will not get processed
+         * by link layer properly. For now, just remove it from the scheduler
+         */
+        if (CONN_F_CONN_REQ_TXD(connsm) == 1) {
+            CONN_F_CONN_REQ_TXD(connsm) = 0;
+            ble_ll_sched_rmv_elem(&connsm->conn_sch);
+        }
+
         ble_phy_rx();
         rc = 0;
     } else {


[28/37] incubator-mynewt-core git commit: MYNEWT-697: Controller can leak connection state machines

Posted by we...@apache.org.
MYNEWT-697: Controller can leak connection state machines

Fixed bug where it was possible that a controller could allocate
a connection state machine and never make it active or free it.
If this happens, it is possible that the controller would not
be able to create a new connection. This particular bug would
only occur if the device was a central and would occur during
the le create connection command.


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

Branch: refs/heads/nrf_cputime
Commit: bf901055b8e6b24072f2ae1dd3b2ccf0f0a4fac3
Parents: cb97248
Author: William San Filippo <wi...@runtime.io>
Authored: Wed Mar 29 20:15:55 2017 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Wed Mar 29 20:21:07 2017 -0700

----------------------------------------------------------------------
 net/nimble/controller/src/ble_ll_conn_hci.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bf901055/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 3ed9147..9b25002 100644
--- a/net/nimble/controller/src/ble_ll_conn_hci.c
+++ b/net/nimble/controller/src/ble_ll_conn_hci.c
@@ -481,17 +481,17 @@ ble_ll_conn_create(uint8_t *cmdbuf)
         return BLE_ERR_INV_HCI_CMD_PARMS;
     }
 
+    /* Make sure we can allocate an event to send the connection complete */
+    if (ble_ll_init_alloc_conn_comp_ev()) {
+        return BLE_ERR_MEM_CAPACITY;
+    }
+
     /* Make sure we can accept a connection! */
     connsm = ble_ll_conn_sm_get();
     if (connsm == NULL) {
         return BLE_ERR_CONN_LIMIT;
     }
 
-    /* Make sure we can allocate an event to send the connection complete */
-    if (ble_ll_init_alloc_conn_comp_ev()) {
-        return BLE_ERR_MEM_CAPACITY;
-    }
-
     /* Initialize state machine in master role and start state machine */
     ble_ll_conn_master_init(connsm, hcc);
     ble_ll_conn_sm_new(connsm);


[21/37] incubator-mynewt-core git commit: sim - Terminate on assert failure.

Posted by we...@apache.org.
sim - Terminate on assert failure.

Prior to this change, the process would just hang.


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

Branch: refs/heads/nrf_cputime
Commit: 4fa3357158846d5a45a414954c0e6a45b35048d8
Parents: 7019142
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Mar 8 14:58:43 2017 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 16:58:41 2017 -0700

----------------------------------------------------------------------
 kernel/os/src/arch/sim/os_fault.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4fa33571/kernel/os/src/arch/sim/os_fault.c
----------------------------------------------------------------------
diff --git a/kernel/os/src/arch/sim/os_fault.c b/kernel/os/src/arch/sim/os_fault.c
index 92d7aa0..3bcd76a 100644
--- a/kernel/os/src/arch/sim/os_fault.c
+++ b/kernel/os/src/arch/sim/os_fault.c
@@ -37,5 +37,5 @@ __assert_func(const char *file, int line, const char *func, const char *e)
     }
     len = write(1, msg, strlen(msg));
     (void)len;
-    _exit(1);
+    _Exit(1);
 }


[05/37] incubator-mynewt-core git commit: net/oic; don't send confirmable requests over TCP-like transports.

Posted by we...@apache.org.
net/oic; don't send confirmable requests over TCP-like transports.


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

Branch: refs/heads/nrf_cputime
Commit: 9d8c6f27c3fbc2d58c2eff24cf0a6ad3039598b7
Parents: 133a628
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Mar 27 15:21:37 2017 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Mar 27 15:21:37 2017 -0700

----------------------------------------------------------------------
 net/oic/src/messaging/coap/observe.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9d8c6f27/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 4369360..a262465 100644
--- a/net/oic/src/messaging/coap/observe.c
+++ b/net/oic/src/messaging/coap/observe.c
@@ -278,7 +278,8 @@ coap_notify_observers(oc_resource_t *resource,
                 coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0);
 
                 notification->mid = transaction->mid;
-                if (obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0) {
+                if (!oc_endpoint_use_tcp(&obs->endpoint) &&
+                    obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0) {
                     OC_LOG_DEBUG("coap_observe_notify: forcing CON "
                                  "notification to check for client liveness\n");
                     notification->type = COAP_TYPE_CON;


[03/37] incubator-mynewt-core git commit: net/oic; oc_endpoint size is different depending on transport type. Take this into account when comparing them.

Posted by we...@apache.org.
net/oic; oc_endpoint size is different depending on transport type.
Take this into account when comparing them.


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

Branch: refs/heads/nrf_cputime
Commit: 4393af41e3938601972ffe8be7e859c67cf57785
Parents: 0df95ce
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Sun Mar 26 16:41:44 2017 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Sun Mar 26 16:41:44 2017 -0700

----------------------------------------------------------------------
 net/oic/src/api/oc_ri.c              |  2 +-
 net/oic/src/messaging/coap/observe.c | 14 ++++++++------
 net/oic/src/port/oc_connectivity.h   | 14 ++++++++++++++
 net/oic/src/security/oc_dtls.c       |  2 +-
 4 files changed, 24 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4393af41/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 d5779e5..c03458b 100644
--- a/net/oic/src/api/oc_ri.c
+++ b/net/oic/src/api/oc_ri.c
@@ -809,7 +809,7 @@ oc_ri_get_client_cb(const char *uri, oc_server_handle_t *server,
         if (oc_string_len(cb->uri) == strlen(uri) &&
           strncmp(oc_string(cb->uri), uri, strlen(uri)) == 0 &&
           memcmp(&cb->server.endpoint, &server->endpoint,
-            sizeof(oc_endpoint_t)) == 0 &&
+                 oc_endpoint_size(&cb->server.endpoint)) == 0 &&
           cb->method == method) {
             return cb;
         }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4393af41/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 3d45e03..4369360 100644
--- a/net/oic/src/messaging/coap/observe.c
+++ b/net/oic/src/messaging/coap/observe.c
@@ -111,7 +111,7 @@ coap_remove_observer_by_client(oc_endpoint_t *endpoint)
     obs = SLIST_FIRST(&oc_observers);
     while (obs) {
         next = SLIST_NEXT(obs, next);
-        if (memcmp(&obs->endpoint, endpoint, sizeof(oc_endpoint_t)) == 0) {
+        if (memcmp(&obs->endpoint, endpoint, oc_endpoint_size(endpoint)) == 0) {
             obs->resource->num_observers--;
             coap_remove_observer(obs);
             removed++;
@@ -131,7 +131,7 @@ coap_remove_observer_by_token(oc_endpoint_t *endpoint, uint8_t *token,
     obs = SLIST_FIRST(&oc_observers);
     while (obs) {
         next = SLIST_NEXT(obs, next);
-        if (memcmp(&obs->endpoint, endpoint, sizeof(oc_endpoint_t)) == 0 &&
+        if (memcmp(&obs->endpoint, endpoint, oc_endpoint_size(endpoint)) == 0 &&
           obs->token_len == token_len &&
           memcmp(obs->token, token, token_len) == 0) {
             obs->resource->num_observers--;
@@ -153,7 +153,8 @@ coap_remove_observer_by_uri(oc_endpoint_t *endpoint, const char *uri)
     obs = SLIST_FIRST(&oc_observers);
     while (obs) {
         next = SLIST_NEXT(obs, next);
-        if (((memcmp(&obs->endpoint, endpoint, sizeof(oc_endpoint_t)) == 0)) &&
+        if (((memcmp(&obs->endpoint, endpoint,
+                     oc_endpoint_size(endpoint)) == 0)) &&
           (obs->url == uri || memcmp(obs->url, uri, strlen(obs->url)) == 0)) {
             obs->resource->num_observers--;
             coap_remove_observer(obs);
@@ -173,7 +174,7 @@ coap_remove_observer_by_mid(oc_endpoint_t *endpoint, uint16_t mid)
     obs = SLIST_FIRST(&oc_observers);
     while (obs) {
         next = SLIST_NEXT(obs, next);
-        if (memcmp(&obs->endpoint, endpoint, sizeof(*endpoint)) == 0 &&
+        if (memcmp(&obs->endpoint, endpoint, oc_endpoint_size(endpoint)) == 0 &&
           obs->last_mid == mid) {
             obs->resource->num_observers--;
             coap_remove_observer(obs);
@@ -233,8 +234,9 @@ coap_notify_observers(oc_resource_t *resource,
     /* iterate over observers */
     for (obs = SLIST_FIRST(&oc_observers); obs; obs = SLIST_NEXT(obs, next)) {
         /* skip if neither resource nor endpoint match */
-        if (resource != obs->resource &&
-            0 != memcmp(&obs->endpoint, endpoint, sizeof(oc_endpoint_t))) {
+        if ((resource && resource != obs->resource) ||
+            (endpoint && memcmp(&obs->endpoint, endpoint,
+                                oc_endpoint_size(endpoint)) != 0)) {
             continue;
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4393af41/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 a7cce39..787fbf2 100644
--- a/net/oic/src/port/oc_connectivity.h
+++ b/net/oic/src/port/oc_connectivity.h
@@ -83,6 +83,20 @@ typedef struct oc_endpoint {
     };
 } oc_endpoint_t;
 
+static inline int
+oc_endpoint_size(struct oc_endpoint *oe)
+{
+    if (oe->oe.flags & (IP | IP4)) {
+        return sizeof (struct oc_endpoint_ip);
+    } else if (oe->oe.flags & GATT) {
+        return sizeof (struct oc_endpoint_ble);
+    } else if (oe->oe.flags & SERIAL) {
+        return sizeof (struct oc_endpoint_plain);
+    } else {
+        return sizeof (struct oc_endpoint);
+    }
+}
+
 #define OC_MBUF_ENDPOINT(m)                                            \
     ((struct oc_endpoint *)((uint8_t *)m + sizeof(struct os_mbuf) +    \
                             sizeof(struct os_mbuf_pkthdr)))

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4393af41/net/oic/src/security/oc_dtls.c
----------------------------------------------------------------------
diff --git a/net/oic/src/security/oc_dtls.c b/net/oic/src/security/oc_dtls.c
index fdd0249..a904d79 100644
--- a/net/oic/src/security/oc_dtls.c
+++ b/net/oic/src/security/oc_dtls.c
@@ -37,7 +37,7 @@ oc_sec_dtls_get_peer(oc_endpoint_t *endpoint)
 {
   oc_sec_dtls_peer_t *peer = oc_list_head(dtls_peers);
   while (peer != NULL) {
-    if (memcmp(&peer->session.addr, endpoint, sizeof(oc_endpoint_t)) == 0)
+    if (memcmp(&peer->session.addr, endpoint, oc_endpoint_size(endpoint)) == 0)
       break;
     peer = oc_list_item_next(peer);
   }


[26/37] incubator-mynewt-core git commit: BLE Host - Remove duplicate conn check.

Posted by we...@apache.org.
BLE Host - Remove duplicate conn check.


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

Branch: refs/heads/nrf_cputime
Commit: fdc3d2b3ef64e95401fb2431adf2072b551d4c1e
Parents: 07746cc
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 28 18:11:06 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 28 18:11:35 2017 -0700

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


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fdc3d2b3/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 9d7d0f0..ed71374 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -2384,12 +2384,6 @@ ble_gap_connect(uint8_t own_addr_type, const ble_addr_t *peer_addr,
         goto done;
     }
 
-    /* Verify peer not already connected. */
-    if (ble_hs_conn_find_by_addr(peer_addr) != NULL) {
-        rc = BLE_HS_EDONE;
-        goto done;
-    }
-
     if (!ble_hs_conn_can_alloc()) {
         rc = BLE_HS_ENOMEM;
         goto done;