[4/4] incubator-mynewt-core git commit: This closes pull request #45.

2016-04-19 Thread marko
This closes pull request #45.

Merge branch 'develop' of https://github.com/neelnatu/incubator-mynewt-larva 
into develop


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

Branch: refs/heads/develop
Commit: d200094bca8c7c1c51c4585ba5769a1ac076ace3
Parents: 9abd8ce dc15948
Author: Marko Kiiskila 
Authored: Tue Apr 19 21:13:23 2016 -0700
Committer: Marko Kiiskila 
Committed: Tue Apr 19 21:13:23 2016 -0700

--
 libs/os/src/arch/sim/os_arch_sim.c | 72 +
 1 file changed, 56 insertions(+), 16 deletions(-)
--




[3/4] incubator-mynewt-core git commit: Fix gratuitous newline deletion.

2016-04-19 Thread marko
Fix gratuitous newline deletion.


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

Branch: refs/heads/develop
Commit: dc1594897cbc22e4620e103e5b5d4ef03cbea5ed
Parents: 3eb0cfb
Author: Neel Natu 
Authored: Tue Apr 19 18:29:48 2016 -0700
Committer: Neel Natu 
Committed: Tue Apr 19 18:29:48 2016 -0700

--
 libs/os/src/arch/sim/os_arch_sim.c | 1 +
 1 file changed, 1 insertion(+)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dc159489/libs/os/src/arch/sim/os_arch_sim.c
--
diff --git a/libs/os/src/arch/sim/os_arch_sim.c 
b/libs/os/src/arch/sim/os_arch_sim.c
index 50b6663..14316c5 100644
--- a/libs/os/src/arch/sim/os_arch_sim.c
+++ b/libs/os/src/arch/sim/os_arch_sim.c
@@ -363,6 +363,7 @@ timer_handler(int sig)
 time_diff.tv_sec = 0;
 time_diff.tv_usec %= OS_USEC_PER_TICK;
 timersub(_now, _diff, _last);
+
 os_time_advance(ticks);
 }
 }



[1/4] incubator-mynewt-core git commit: Don't do anything in signal handlers when process is blocked in 'sigsuspend()'.

2016-04-19 Thread marko
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 9abd8ceeb -> d200094bc


Don't do anything in signal handlers when process is blocked in 'sigsuspend()'.

When the OS is idling it may disable the periodic timer and enter the tickless
mode. The OS exits the tickless mode on arrival of an interrupt/signal (note
that OS time is incorrect when this happens). Thus it is important to call
'timer_handler()' to correct the OS time before executing anything else.


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

Branch: refs/heads/develop
Commit: dd96f36edae81577343aeb3d439429e9d6eb710b
Parents: 27605cb
Author: Neel Natu 
Authored: Tue Apr 19 18:23:11 2016 -0700
Committer: Neel Natu 
Committed: Tue Apr 19 18:23:11 2016 -0700

--
 libs/os/src/arch/sim/os_arch_sim.c | 101 +++-
 1 file changed, 74 insertions(+), 27 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dd96f36e/libs/os/src/arch/sim/os_arch_sim.c
--
diff --git a/libs/os/src/arch/sim/os_arch_sim.c 
b/libs/os/src/arch/sim/os_arch_sim.c
index 7a3b27d..7bcca4d 100644
--- a/libs/os/src/arch/sim/os_arch_sim.c
+++ b/libs/os/src/arch/sim/os_arch_sim.c
@@ -57,6 +57,9 @@ static pid_t mypid;
 static sigset_t allsigs, nosigs;
 static void timer_handler(int sig);
 
+static bool suspended;  /* process is blocked in sigsuspend() */
+static sigset_t suspsigs;   /* signals delivered in sigsuspend() */
+
 /*
  * Called from 'os_arch_frame_init()' when setjmp returns indirectly via
  * longjmp. The return value of setjmp is passed to this function as 'rc'.
@@ -114,6 +117,17 @@ ctxsw_handler(int sig)
 int rc;
 
 OS_ASSERT_CRITICAL();
+
+/*
+ * Just record that this handler was called when the process was blocked.
+ * The handler will be called after sigsuspend() returns in the correct
+ * order.
+ */
+if (suspended) {
+sigaddset(, sig);
+return;
+}
+
 t = os_sched_get_current_task();
 next_t = os_sched_next_task();
 if (t == next_t) {
@@ -195,11 +209,22 @@ os_arch_in_critical(void)
 return (sigismember(, SIGALRM));
 }
 
+static struct {
+int num;
+void (*handler)(int sig);
+} signals[] = {
+{ SIGALRM, timer_handler },
+{ SIGURG, ctxsw_handler },
+};
+
+#define NUMSIGS (sizeof(signals)/sizeof(signals[0]))
+
 void
 os_tick_idle(os_time_t ticks)
 {
-int rc;
+int i, rc, sig;
 struct itimerval it;
+void (*handler)(int sig);
 
 OS_ASSERT_CRITICAL();
 
@@ -216,15 +241,28 @@ os_tick_idle(os_time_t ticks)
 assert(rc == 0);
 }
 
+suspended = true;
+sigemptyset();
 sigsuspend();/* Wait for a signal to wake us up */
+suspended = false;
 
-if (ticks > 0) {
-/*
- * Update OS time before anything else when coming out of
- * the tickless regime.
- */
+/*
+ * Call handlers for signals delivered to the process during sigsuspend().
+ * The SIGALRM handler is called before any other handlers to ensure that
+ * OS time is always correct.
+ */
+if (sigismember(, SIGALRM)) {
 timer_handler(SIGALRM);
+}
+for (i = 0; i < NUMSIGS; i++) {
+sig = signals[i].num;
+handler = signals[i].handler;
+if (sig != SIGALRM && sigismember(, sig)) {
+handler(sig);
+}
+}
 
+if (ticks > 0) {
 /*
  * Enable the periodic timer interrupt.
  */
@@ -237,16 +275,6 @@ os_tick_idle(os_time_t ticks)
 }
 }
 
-static struct {
-int num;
-void (*handler)(int sig);
-} signals[] = {
-{ SIGALRM, timer_handler },
-{ SIGURG, ctxsw_handler },
-};
-
-#define NUMSIGS (sizeof(signals)/sizeof(signals[0]))
-
 static void
 signals_init(void)
 {
@@ -299,24 +327,43 @@ timer_handler(int sig)
 static struct timeval time_last;
 static int time_inited; 
 
+OS_ASSERT_CRITICAL();
+
+/*
+ * Just record that this handler was called when the process was blocked.
+ * The handler will be called after sigsuspend() returns in the proper
+ * order.
+ */
+if (suspended) {
+sigaddset(, sig);
+return;
+}
+
 if (!time_inited) {
 gettimeofday(_last, NULL);
 time_inited = 1;
 }
 
 gettimeofday(_now, NULL);
-timersub(_now, _last, _diff);
-
-ticks = time_diff.tv_sec * OS_TICKS_PER_SEC;
-ticks += time_diff.tv_usec / OS_USEC_PER_TICK;
 
-/*
- * Update 

[2/4] incubator-mynewt-core git commit: Merge branch 'develop' of https://github.com/neelnatu/incubator-mynewt-larva into develop

2016-04-19 Thread marko
Merge branch 'develop' of https://github.com/neelnatu/incubator-mynewt-larva 
into develop


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

Branch: refs/heads/develop
Commit: 3eb0cfbacdea19e7e3dd8ef11b13405bdaf46e12
Parents: dd96f36 f6bc99f
Author: Neel Natu 
Authored: Tue Apr 19 18:27:36 2016 -0700
Committer: Neel Natu 
Committed: Tue Apr 19 18:27:36 2016 -0700

--
 fs/nffs/src/nffs_cache.c  |  85 ++---
 fs/nffs/src/nffs_gc.c |  34 +++
 fs/nffs/src/nffs_priv.h   |   6 +-
 fs/nffs/src/nffs_restore.c| 141 +
 fs/nffs/src/nffs_write.c  |  32 ++-
 fs/nffs/src/test/arch/sim/nffs_test.c |   1 -
 libs/os/src/arch/sim/os_arch_sim.c|  45 +
 7 files changed, 242 insertions(+), 102 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3eb0cfba/libs/os/src/arch/sim/os_arch_sim.c
--
diff --cc libs/os/src/arch/sim/os_arch_sim.c
index 7bcca4d,1d43ab2..50b6663
--- a/libs/os/src/arch/sim/os_arch_sim.c
+++ b/libs/os/src/arch/sim/os_arch_sim.c
@@@ -325,20 -297,8 +325,20 @@@ timer_handler(int sig
  int ticks;
  
  static struct timeval time_last;
- static int time_inited; 
+ static int time_inited;
  
 +OS_ASSERT_CRITICAL();
 +
 +/*
 + * Just record that this handler was called when the process was blocked.
 + * The handler will be called after sigsuspend() returns in the proper
 + * order.
 + */
 +if (suspended) {
 +sigaddset(, sig);
 +return;
 +}
 +
  if (!time_inited) {
  gettimeofday(_last, NULL);
  time_inited = 1;
@@@ -357,15 -323,9 +363,8 @@@
  time_diff.tv_sec = 0;
  time_diff.tv_usec %= OS_USEC_PER_TICK;
  timersub(_now, _diff, _last);
- } else {
- /*
-  * XXX time went backwards so just start afresh.
-  */
- time_last = time_now;
- ticks = 0;
 -
+ os_time_advance(ticks);
  }
- 
- os_time_advance(ticks);
  }
  
  static void



incubator-mynewt-core git commit: I2C interface supporting blocking read and write

2016-04-19 Thread paulfdietrich
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 9131d1e4c -> 598b91010


I2C interface supporting blocking read and write

changes to support start and stop conditions


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

Branch: refs/heads/develop
Commit: 598b91010431f175394c03f34b7cd2429b66248e
Parents: 9131d1e
Author: Paul Dietrich 
Authored: Thu Apr 14 16:55:12 2016 -0700
Committer: Paul Dietrich 
Committed: Tue Apr 19 20:48:12 2016 -0700

--
 hw/hal/include/hal/hal_i2c.h | 89 +++
 hw/hal/include/hal/hal_i2c_int.h | 54 +
 hw/hal/src/hal_i2c.c | 78 ++
 3 files changed, 221 insertions(+)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/598b9101/hw/hal/include/hal/hal_i2c.h
--
diff --git a/hw/hal/include/hal/hal_i2c.h b/hw/hal/include/hal/hal_i2c.h
new file mode 100644
index 000..82546d4
--- /dev/null
+++ b/hw/hal/include/hal/hal_i2c.h
@@ -0,0 +1,89 @@
+/**
+ * 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 HAL_I2C_H
+#define HAL_I2C_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include 
+#include 
+
+/* This is the API for an i2c bus.  I tried to make this a simple API */
+
+struct hal_i2c;
+
+/* when sending a packet, use this structure to pass the arguments */
+struct hal_i2c_master_data {
+uint8_t  address;   /* destination address */
+/* NOTE: Write addresses are even and read addresses are odd
+ * but in this API, you must enter a single address that is the
+ * write address divide by 2.  For example, for address 
+ * 80, you would enter a 40 */
+uint8_t *buffer;/* buffer space to hold the transmit or receive */
+uint16_t len;   /* length of buffer to transmit or receive */
+};
+
+/* Initialize a new i2c device with the given system id.
+ * Returns negative on error, 0 on success. */
+struct hal_i2c*
+hal_i2c_init(enum system_device_id sysid);
+
+/* Sends  bytes of data on the i2c.  This API assumes that you 
+ * issued a successful start condition.  It will fail if you
+ * have not. This API does NOT issue a stop condition.  You must stop the 
+ * bus after successful or unsuccessful write attempts.  Returns 0 on 
+ * success, negative on failure */
+int
+hal_i2c_master_write(struct hal_i2c*, struct hal_i2c_master_data *pdata);
+
+/* Reads  bytes of data on the i2c.  This API assumes that you 
+ * issued a successful start condition.  It will fail if you
+ * have not. This API does NOT issue a stop condition.  You must stop the 
+ * bus after successful or unsuccessful write attempts.  Returns 0 on 
+ * success, negative on failure */
+int
+hal_i2c_master_read(struct hal_i2c*, struct hal_i2c_master_data *pdata);
+
+/* issues a start condition and address on the SPI bus. Returns 0 
+ * on success, negative on error.
+ */
+int 
+hal_i2c_master_start(struct hal_i2c*);
+
+/* issues a stop condition on the bus.  You must issue a stop condition for
+ * every successful start condition on the bus */
+int 
+hal_i2c_master_stop(struct hal_i2c*);
+
+
+/* Probes the i2c bus for a device with this address.  THIS API 
+ * issues a start condition, probes the address and issues a stop
+ * condition.  
+ */
+int 
+hal_i2c_master_probe(struct hal_i2c*, uint8_t address);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_I2C_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/598b9101/hw/hal/include/hal/hal_i2c_int.h
--
diff --git a/hw/hal/include/hal/hal_i2c_int.h b/hw/hal/include/hal/hal_i2c_int.h
new file mode 100644
index 

incubator-mynewt-core git commit: ble host - fix security bug

2016-04-19 Thread ccollins
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 7235e90ad -> 9131d1e4c


ble host - fix security bug

The master (initiator) was overwriting its ltk while verifying the
slave's response.


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

Branch: refs/heads/develop
Commit: 9131d1e4cc55cd49a417182c9c205ca9c2320bb3
Parents: 7235e90
Author: Christopher Collins 
Authored: Tue Apr 19 20:01:01 2016 -0700
Committer: Christopher Collins 
Committed: Tue Apr 19 20:01:01 2016 -0700

--
 net/nimble/host/src/ble_hs.c   |  2 -
 net/nimble/host/src/ble_l2cap_sm.c | 67 ++---
 2 files changed, 29 insertions(+), 40 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9131d1e4/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 2de2bf9..8a65c00 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -157,8 +157,6 @@ ble_hs_heartbeat_timer_reset(void)
 static void
 ble_hs_heartbeat(void *unused)
 {
-ble_hs_misc_assert_not_locked();
-
 ble_gattc_heartbeat();
 ble_gap_heartbeat();
 ble_l2cap_sig_heartbeat();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9131d1e4/net/nimble/host/src/ble_l2cap_sm.c
--
diff --git a/net/nimble/host/src/ble_l2cap_sm.c 
b/net/nimble/host/src/ble_l2cap_sm.c
index a2e5785..e385ac6 100644
--- a/net/nimble/host/src/ble_l2cap_sm.c
+++ b/net/nimble/host/src/ble_l2cap_sm.c
@@ -51,23 +51,15 @@ struct ble_l2cap_sm_proc {
 uint8_t pair_alg;
 uint8_t state;
 
-
 /* XXX: Minimum security requirements. */
 
-union {
-struct {
-struct ble_l2cap_sm_pair_cmd pair_req;
-struct ble_l2cap_sm_pair_cmd pair_rsp;
-uint8_t tk[16];
-uint8_t confirm_their[16];
-uint8_t rand_our[16];
-uint8_t rand_their[16];
-} phase_1_2;
-
-struct {
-uint8_t key[16];
-} hci;
-};
+struct ble_l2cap_sm_pair_cmd pair_req;
+struct ble_l2cap_sm_pair_cmd pair_rsp;
+uint8_t tk[16];
+uint8_t confirm_their[16];
+uint8_t rand_our[16];
+uint8_t rand_their[16];
+uint8_t ltk[16];
 };
 
 STAILQ_HEAD(ble_l2cap_sm_proc_list, ble_l2cap_sm_proc);
@@ -236,13 +228,12 @@ ble_l2cap_sm_gen_key(struct ble_l2cap_sm_proc *proc)
 uint8_t key[16];
 int rc;
 
-rc = ble_l2cap_sm_alg_s1(proc->phase_1_2.tk, proc->phase_1_2.rand_our,
- proc->phase_1_2.rand_their, key);
+rc = ble_l2cap_sm_alg_s1(proc->tk, proc->rand_our, proc->rand_their, key);
 if (rc != 0) {
 return rc;
 }
 
-memcpy(proc->hci.key, key, sizeof key);
+memcpy(proc->ltk, key, sizeof key);
 
 return 0;
 }
@@ -522,7 +513,7 @@ ble_l2cap_sm_lt_key_req_handle(struct ble_l2cap_sm_proc 
*proc,
 {
 int rc;
 
-rc = ble_l2cap_sm_lt_key_req_reply_tx(proc->conn_handle, proc->hci.key);
+rc = ble_l2cap_sm_lt_key_req_reply_tx(proc->conn_handle, proc->ltk);
 if (rc != 0) {
 *out_sm_status = BLE_L2CAP_SM_ERR_UNSPECIFIED;
 return rc;
@@ -543,7 +534,7 @@ ble_l2cap_sm_random_go(struct ble_l2cap_sm_proc *proc)
 struct ble_l2cap_sm_pair_random cmd;
 int rc;
 
-memcpy(cmd.value, proc->phase_1_2.rand_our, 16);
+memcpy(cmd.value, proc->rand_our, 16);
 rc = ble_l2cap_sm_pair_random_tx(proc->conn_handle, );
 if (rc != 0) {
 return rc;
@@ -584,14 +575,14 @@ ble_l2cap_sm_random_handle(struct ble_l2cap_sm_proc *proc,
 return rc;
 }
 
-if (memcmp(proc->phase_1_2.confirm_their, confirm_val, 16) != 0) {
+if (memcmp(proc->confirm_their, confirm_val, 16) != 0) {
 /* Random number mismatch. */
 rc = BLE_HS_SM_US_ERR(BLE_L2CAP_SM_ERR_CONFIRM_MISMATCH);
 *out_sm_status = BLE_L2CAP_SM_ERR_CONFIRM_MISMATCH;
 return rc;
 }
 
-memcpy(proc->phase_1_2.rand_their, cmd->value, 16);
+memcpy(proc->rand_their, cmd->value, 16);
 
 /* Generate the key. */
 rc = ble_l2cap_sm_gen_key(proc);
@@ -602,7 +593,7 @@ ble_l2cap_sm_random_handle(struct ble_l2cap_sm_proc *proc,
 
 if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) {
 /* Send the start-encrypt HCI command to the controller. */
-rc = ble_l2cap_sm_start_encrypt_tx(proc->conn_handle, proc->hci.key);
+rc = 

[2/7] incubator-mynewt-core git commit: ble host - major changes.

2016-04-19 Thread ccollins
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e32f9f9f/net/nimble/host/src/ble_l2cap_sm.c
--
diff --git a/net/nimble/host/src/ble_l2cap_sm.c 
b/net/nimble/host/src/ble_l2cap_sm.c
index d53124f..a2e5785 100644
--- a/net/nimble/host/src/ble_l2cap_sm.c
+++ b/net/nimble/host/src/ble_l2cap_sm.c
@@ -26,18 +26,14 @@
 
 #if NIMBLE_OPT_SM
 
-#define BLE_L2CAP_SM_PROC_OP_NONE   ((uint8_t)-1)
-
-#define BLE_L2CAP_SM_PROC_OP_PAIR   0
-#define BLE_L2CAP_SM_PROC_OP_CONFIRM1
-#define BLE_L2CAP_SM_PROC_OP_RANDOM 2
-#define BLE_L2CAP_SM_PROC_OP_FAIL   3
-#define BLE_L2CAP_SM_PROC_OP_LTK4
-#define BLE_L2CAP_SM_PROC_OP_LTK_TXED   5
-#define BLE_L2CAP_SM_PROC_OP_ENC_CHANGE 6
-#define BLE_L2CAP_SM_PROC_OP_START_ENCRYPT  7
-#define BLE_L2CAP_SM_PROC_OP_START_ENCRYPT_TXED 8
-#define BLE_L2CAP_SM_PROC_OP_CNT9
+#define BLE_L2CAP_SM_PROC_STATE_NONE((uint8_t)-1)
+
+#define BLE_L2CAP_SM_PROC_STATE_PAIR0
+#define BLE_L2CAP_SM_PROC_STATE_CONFIRM 1
+#define BLE_L2CAP_SM_PROC_STATE_RANDOM  2
+#define BLE_L2CAP_SM_PROC_STATE_LTK 3
+#define BLE_L2CAP_SM_PROC_STATE_ENC_CHANGE  4
+#define BLE_L2CAP_SM_PROC_STATE_CNT 5
 
 #define BLE_L2CAP_SM_PROC_F_INITIATOR   0x01
 
@@ -47,10 +43,14 @@
 typedef uint16_t ble_l2cap_sm_proc_flags;
 
 struct ble_l2cap_sm_proc {
-struct ble_fsm_proc fsm_proc;
+STAILQ_ENTRY(ble_l2cap_sm_proc) next;
+
 uint32_t exp_os_ticks;
 ble_l2cap_sm_proc_flags flags;
+uint16_t conn_handle;
 uint8_t pair_alg;
+uint8_t state;
+
 
 /* XXX: Minimum security requirements. */
 
@@ -66,25 +66,13 @@ struct ble_l2cap_sm_proc {
 
 struct {
 uint8_t key[16];
-uint8_t handle;
 } hci;
-
-struct {
-uint8_t reason;
-} fail;
 };
 };
 
-/** Used for extracting proc entries from the fsm list. */
-struct ble_l2cap_sm_extract_arg {
-uint16_t conn_handle;
-uint8_t op;
-int8_t initiator; /* 0=no, 1=yes, -1=don't-care. */
-};
-
-typedef int ble_l2cap_sm_kick_fn(struct ble_l2cap_sm_proc *proc);
+STAILQ_HEAD(ble_l2cap_sm_proc_list, ble_l2cap_sm_proc);
 
-typedef int ble_l2cap_sm_rx_fn(uint16_t conn_handle, uint8_t op,
+typedef int ble_l2cap_sm_rx_fn(uint16_t conn_handle, uint8_t state,
struct os_mbuf **om);
 
 static ble_l2cap_sm_rx_fn ble_l2cap_sm_rx_noop;
@@ -94,13 +82,6 @@ static ble_l2cap_sm_rx_fn ble_l2cap_sm_rx_pair_confirm;
 static ble_l2cap_sm_rx_fn ble_l2cap_sm_rx_pair_random;
 static ble_l2cap_sm_rx_fn ble_l2cap_sm_rx_pair_fail;
 
-static ble_l2cap_sm_kick_fn ble_l2cap_sm_pair_kick;
-static ble_l2cap_sm_kick_fn ble_l2cap_sm_confirm_kick;
-static ble_l2cap_sm_kick_fn ble_l2cap_sm_random_kick;
-static ble_l2cap_sm_kick_fn ble_l2cap_sm_fail_kick;
-static ble_l2cap_sm_kick_fn ble_l2cap_sm_lt_key_req_kick;
-static ble_l2cap_sm_kick_fn ble_l2cap_sm_start_encrypt_kick;
-
 static ble_l2cap_sm_rx_fn * const ble_l2cap_sm_dispatch[] = {
[BLE_L2CAP_SM_OP_PAIR_REQ] = ble_l2cap_sm_rx_pair_req,
[BLE_L2CAP_SM_OP_PAIR_RSP] = ble_l2cap_sm_rx_pair_rsp,
@@ -118,31 +99,24 @@ static ble_l2cap_sm_rx_fn * const ble_l2cap_sm_dispatch[] 
= {
[BLE_L2CAP_SM_OP_PAIR_KEYPRESS_NOTIFY] = ble_l2cap_sm_rx_noop,
 };
 
-static ble_l2cap_sm_kick_fn * const
-ble_l2cap_sm_kick[BLE_L2CAP_SM_PROC_OP_CNT] = {
-[BLE_L2CAP_SM_PROC_OP_PAIR] = ble_l2cap_sm_pair_kick,
-[BLE_L2CAP_SM_PROC_OP_CONFIRM]  = ble_l2cap_sm_confirm_kick,
-[BLE_L2CAP_SM_PROC_OP_RANDOM]   = ble_l2cap_sm_random_kick,
-[BLE_L2CAP_SM_PROC_OP_FAIL] = ble_l2cap_sm_fail_kick,
-[BLE_L2CAP_SM_PROC_OP_LTK]  = ble_l2cap_sm_lt_key_req_kick,
-[BLE_L2CAP_SM_PROC_OP_START_ENCRYPT]= ble_l2cap_sm_start_encrypt_kick,
-};
-
-static void ble_l2cap_sm_rx_lt_key_req_reply_ack(struct ble_hci_ack *ack,
- void *arg);
-static void ble_l2cap_sm_rx_start_encrypt_ack(struct ble_hci_ack *ack,
-  void *arg);
-
 static void *ble_l2cap_sm_proc_mem;
 static struct os_mempool ble_l2cap_sm_proc_pool;
 
-static struct ble_fsm ble_l2cap_sm_fsm;
+/* Maintains the list of active security manager procedures. */
+static struct ble_l2cap_sm_proc_list ble_l2cap_sm_procs;
+
+static int ble_l2cap_sm_confirm_prepare_args(struct ble_l2cap_sm_proc *proc,
+ uint8_t *k, uint8_t *preq,
+ uint8_t *pres, uint8_t *iat,
+ uint8_t *rat, uint8_t *ia,
+ uint8_t *ra);
 
 /*
  * $debug

[4/7] incubator-mynewt-core git commit: ble host - major changes.

2016-04-19 Thread ccollins
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e32f9f9f/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 ffd57c8..64502f3 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -31,7 +31,7 @@
  * $definitions / declarations   *
  */
 
-#define BLE_GATT_UNRESPONSIVE_TIMEOUT   3   /* Milliseconds. */
+#define BLE_GATT_UNRESPONSIVE_TIMEOUT   (30 * OS_TICKS_PER_SEC)
 
 #define BLE_GATT_OP_NONEUINT8_MAX
 #define BLE_GATT_OP_MTU 0
@@ -39,23 +39,25 @@
 #define BLE_GATT_OP_DISC_SVC_UUID   2
 #define BLE_GATT_OP_FIND_INC_SVCS   3
 #define BLE_GATT_OP_DISC_ALL_CHRS   4
-#define BLE_GATT_OP_DISC_CHR_UUID  5
+#define BLE_GATT_OP_DISC_CHR_UUID   5
 #define BLE_GATT_OP_DISC_ALL_DSCS   6
 #define BLE_GATT_OP_READ7
 #define BLE_GATT_OP_READ_UUID   8
 #define BLE_GATT_OP_READ_LONG   9
 #define BLE_GATT_OP_READ_MULT   10
-#define BLE_GATT_OP_WRITE_NO_RSP11
-#define BLE_GATT_OP_WRITE   12
-#define BLE_GATT_OP_WRITE_LONG  13
-#define BLE_GATT_OP_WRITE_RELIABLE  14
-#define BLE_GATT_OP_NOTIFY  15
-#define BLE_GATT_OP_INDICATE16
-#define BLE_GATT_OP_MAX 17
+#define BLE_GATT_OP_WRITE   11
+#define BLE_GATT_OP_WRITE_LONG  12
+#define BLE_GATT_OP_WRITE_RELIABLE  13
+#define BLE_GATT_OP_INDICATE14
+#define BLE_GATT_OP_MAX 15
 
 /** Represents an in-progress GATT procedure. */
 struct ble_gattc_proc {
-struct ble_fsm_proc fsm_proc;
+STAILQ_ENTRY(ble_gattc_proc) next;
+
+uint32_t exp_os_ticks;
+uint16_t conn_handle;
+uint8_t op;
 
 union {
 struct {
@@ -117,9 +119,6 @@ struct ble_gattc_proc {
 } read;
 
 struct {
-uint16_t prev_handle;
-uint16_t end_handle;
-uint8_t uuid128[16];
 ble_gatt_attr_fn *cb;
 void *cb_arg;
 } read_uuid;
@@ -132,14 +131,12 @@ struct ble_gattc_proc {
 } read_long;
 
 struct {
-uint16_t *handles;
-uint8_t num_handles;
-ble_gatt_mult_attr_fn *cb;
+ble_gatt_attr_fn *cb;
 void *cb_arg;
 } read_mult;
 
 struct {
-struct ble_gatt_attr attr;
+uint16_t att_handle;
 ble_gatt_attr_fn *cb;
 void *cb_arg;
 } write;
@@ -160,284 +157,139 @@ struct ble_gattc_proc {
 } write_reliable;
 
 struct {
-struct ble_gatt_attr attr;
-ble_gatt_attr_fn *cb;
-void *cb_arg;
-} notify;
-
-struct {
-struct ble_gatt_attr attr;
 ble_gatt_attr_fn *cb;
+uint16_t chr_val_handle;
 void *cb_arg;
 } indicate;
 };
 };
 
-/**
- * Kick functions - these trigger the pending ATT transmit for an active GATT
- * procedure.
- */
-typedef int ble_gattc_kick_fn(struct ble_gattc_proc *proc);
-typedef void ble_gattc_err_fn(struct ble_gattc_proc *proc, int status,
-  uint16_t att_handle);
-
-static int ble_gattc_mtu_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_disc_all_svcs_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_disc_svc_uuid_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_find_inc_svcs_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_disc_all_chrs_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_disc_chr_uuid_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_disc_all_dscs_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_read_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_read_uuid_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_read_long_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_read_mult_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_write_no_rsp_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_write_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_write_long_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_write_reliable_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_notify_kick(struct ble_gattc_proc *proc);
-static int ble_gattc_indicate_kick(struct ble_gattc_proc *proc);
+STAILQ_HEAD(ble_gattc_proc_list, ble_gattc_proc);
 
 /**
  * Error functions - these handle an incoming ATT error response and apply it
  * to the appropriate active GATT procedure.
  */
-static void 

[6/7] incubator-mynewt-core git commit: ble host - major changes.

2016-04-19 Thread ccollins
ble host - major changes.

These changes are detailed in this thread on the mynewt dev list:
http://mail-archives.apache.org/mod_mbox/incubator-mynewt-dev/201604.mbox/%3C20160417225734.GB8991%40iori.nightmare-heaven.no-ip.biz%3E

Here is a summary of the changes:

1. All HCI command/acknowledgement exchanges are blocking.

* FSM machinery goes away; controller response is indicated in the
  return code of the HCI send function.
* Nearly all HCI failures are indicated to the application
  immediately, so there is no need for lots of mutexes and temporary
  copies of data structures.
* API is simplified; operation results are indicated via a simple
  function return code.

2. The Nimble host is "taskless"

the Nimble host is now a "flat" library that runs in an application
task.  When the application initializes the host, it indicates which OS
event queue should be used for host-related events.  Host operations are
captured in OS_EVENT_TIMER events that the application task would need
to handle generically, as it most likely already does.


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

Branch: refs/heads/develop
Commit: e32f9f9f20666ecbb42598db0737b9c8f1ca6c74
Parents: f6bc99f
Author: Christopher Collins 
Authored: Tue Apr 12 11:31:14 2016 -0700
Committer: Christopher Collins 
Committed: Tue Apr 19 17:52:53 2016 -0700

--
 apps/bletiny/src/main.c|8 +-
 net/nimble/host/include/host/ble_gatt.h|   14 +-
 net/nimble/host/include/host/ble_hs.h  |7 +-
 net/nimble/host/include/host/ble_l2cap.h   |1 +
 net/nimble/host/include/host/host_hci.h|   20 +-
 net/nimble/host/src/ble_att.c  |7 +-
 net/nimble/host/src/ble_att_clt.c  |   24 +-
 net/nimble/host/src/ble_att_svr.c  |   28 +-
 net/nimble/host/src/ble_fsm.c  |  530 
 net/nimble/host/src/ble_fsm_priv.h |   88 -
 net/nimble/host/src/ble_gap.c  | 1146 +++-
 net/nimble/host/src/ble_gatt_priv.h|1 -
 net/nimble/host/src/ble_gattc.c| 2690 +++
 net/nimble/host/src/ble_gatts.c|8 +-
 net/nimble/host/src/ble_hci_block.c|  229 --
 net/nimble/host/src/ble_hci_sched.c|  474 
 net/nimble/host/src/ble_hci_sched.h|   47 -
 net/nimble/host/src/ble_hci_util.c |   78 +
 net/nimble/host/src/ble_hci_util.h |   26 +
 net/nimble/host/src/ble_hs.c   |  179 +-
 net/nimble/host/src/ble_hs_atomic.c|   45 +
 net/nimble/host/src/ble_hs_atomic.h|   25 +
 net/nimble/host/src/ble_hs_cfg.c   |1 -
 net/nimble/host/src/ble_hs_conn.c  |  120 +-
 net/nimble/host/src/ble_hs_conn.h  |6 +-
 net/nimble/host/src/ble_hs_misc.c  |   10 +-
 net/nimble/host/src/ble_hs_priv.h  |   44 +-
 net/nimble/host/src/ble_hs_startup.c   |  213 +-
 net/nimble/host/src/ble_ibeacon.c  |9 +-
 net/nimble/host/src/ble_l2cap_priv.h   |3 +-
 net/nimble/host/src/ble_l2cap_sig.c|  439 +--
 net/nimble/host/src/ble_l2cap_sig_cmd.c|4 +-
 net/nimble/host/src/ble_l2cap_sm.c | 1142 +++-
 net/nimble/host/src/ble_l2cap_sm.h |9 +-
 net/nimble/host/src/ble_l2cap_sm_cmd.c |   13 +-
 net/nimble/host/src/host_hci.c |  356 ++-
 net/nimble/host/src/host_hci_cmd.c |  240 +-
 net/nimble/host/src/test/ble_gap_test.c|  155 +-
 net/nimble/host/src/test/ble_gatt_conn_test.c  |   50 +-
 net/nimble/host/src/test/ble_gatt_read_test.c  |   55 +-
 net/nimble/host/src/test/ble_gatt_write_test.c |9 +-
 net/nimble/host/src/test/ble_host_hci_test.c   |  133 -
 net/nimble/host/src/test/ble_hs_conn_test.c|   69 -
 net/nimble/host/src/test/ble_hs_test.c |   27 +-
 net/nimble/host/src/test/ble_hs_test_util.c|  149 +-
 net/nimble/host/src/test/ble_hs_test_util.h|   13 +-
 net/nimble/host/src/test/ble_l2cap_sm_test.c   |   30 +-
 net/nimble/host/src/test/ble_l2cap_test.c  |   12 +-
 net/nimble/host/src/test/ble_os_test.c |   99 +-
 net/nimble/include/nimble/hci_common.h |2 +-
 50 files changed, 2956 insertions(+), 6131 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e32f9f9f/apps/bletiny/src/main.c
--
diff --git 

[5/7] incubator-mynewt-core git commit: ble host - major changes.

2016-04-19 Thread ccollins
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e32f9f9f/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 f4cac43..7e87661 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -27,32 +27,12 @@
 #include "ble_hs_priv.h"
 
 #define BLE_GAP_OP_NULL 0
-#define BLE_GAP_STATE_NULL  255
 
 #define BLE_GAP_OP_M_DISC   1
 #define BLE_GAP_OP_M_CONN   2
 
 #define BLE_GAP_OP_S_ADV1
 
-/** Discovery master states. */
-#define BLE_GAP_STATE_M_DISC_PENDING0
-#define BLE_GAP_STATE_M_DISC_ACTIVE 1
-#define BLE_GAP_STATE_M_DISC_DISABLE2
-
-/** Connect master states. */
-#define BLE_GAP_STATE_M_CONN_PENDING0
-#define BLE_GAP_STATE_M_CONN_ACTIVE 1
-
-/** Advertise slave states. */
-#define BLE_GAP_STATE_S_ADV_PENDING 0
-#define BLE_GAP_STATE_S_ADV_ACTIVE  1
-
-/** Connection update states. */
-#define BLE_GAP_STATE_U_UPDATE  0
-#define BLE_GAP_STATE_U_REPLY   1
-#define BLE_GAP_STATE_U_REPLY_ACKED 2
-#define BLE_GAP_STATE_U_NEG_REPLY   3
-
 /**
  * The maximum amount of user data that can be put into the advertising data.
  * The stack may automatically insert some fields on its own, limiting the
@@ -92,24 +72,20 @@ static const struct hci_adv_params ble_gap_adv_params_dflt 
= {
  */
 static bssnz_t struct {
 uint8_t op;
-uint8_t state;
 
 unsigned exp_set:1;
 uint32_t exp_os_ticks;
 
 union {
 struct {
-uint8_t addr_type;
-uint8_t addr[6];
-struct ble_gap_crt_params params;
 ble_gap_conn_fn *cb;
 void *cb_arg;
+
+unsigned using_wl:1;
 } conn;
 
 struct {
 uint8_t disc_mode;
-uint8_t filter_policy;
-uint8_t scan_type;
 ble_gap_disc_fn *cb;
 void *cb_arg;
 } disc;
@@ -124,16 +100,10 @@ static bssnz_t struct {
 uint8_t op;
 
 uint8_t conn_mode;
-uint8_t state;
 uint8_t disc_mode;
 ble_gap_conn_fn *cb;
 void *cb_arg;
 
-uint8_t dir_addr_type;
-uint8_t dir_addr[BLE_DEV_ADDR_LEN];
-
-struct hci_adv_params adv_params;
-struct hci_adv_params rsp_params;
 uint8_t adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
 uint8_t rsp_data[BLE_HCI_MAX_ADV_DATA_LEN];
 uint8_t adv_data_len;
@@ -143,20 +113,7 @@ static bssnz_t struct {
 unsigned adv_pwr_lvl:1;
 } ble_gap_slave;
 
-struct ble_gap_update_entry {
-SLIST_ENTRY(ble_gap_update_entry) next;
-struct ble_gap_upd_params params;
-uint16_t conn_handle;
-uint8_t reject_reason;
-uint8_t state;
-uint8_t hci_handle;
-};
-static SLIST_HEAD(, ble_gap_update_entry) ble_gap_update_entries;
-
-static void *ble_gap_update_mem;
-static struct os_mempool ble_gap_update_pool;
-
-static int ble_gap_disc_tx_disable(void *arg);
+static int ble_gap_disc_tx_disable(void);
 
 struct ble_gap_snapshot {
 struct ble_gap_conn_desc desc;
@@ -164,8 +121,6 @@ struct ble_gap_snapshot {
 void *cb_arg;
 };
 
-static struct os_mutex ble_gap_mutex;
-
 STATS_SECT_DECL(ble_gap_stats) ble_gap_stats;
 STATS_NAME_START(ble_gap_stats)
 STATS_NAME(ble_gap_stats, wl_set)
@@ -198,83 +153,45 @@ STATS_NAME_START(ble_gap_stats)
 STATS_NAME_END(ble_gap_stats)
 
 /*
- * $mutex*
+ * $log  *
  */
 
 static void
-ble_gap_lock(void)
+ble_gap_log_conn(uint8_t addr_type, uint8_t *addr,
+ struct ble_gap_crt_params *params)
 {
-struct os_task *owner;
-int rc;
-
-owner = ble_gap_mutex.mu_owner;
-if (owner != NULL) {
-BLE_HS_DBG_ASSERT_EVAL(owner != os_sched_get_current_task());
+BLE_HS_LOG(INFO, "addr_type=%d addr=", addr_type);
+if (addr == NULL) {
+BLE_HS_LOG(INFO, "N/A");
+} else {
+BLE_HS_LOG_ADDR(INFO, addr);
 }
 
-rc = os_mutex_pend(_gap_mutex, 0x);
-BLE_HS_DBG_ASSERT_EVAL(rc == 0 || rc == OS_NOT_STARTED);
-}
-
-static void
-ble_gap_unlock(void)
-{
-int rc;
-
-rc = os_mutex_release(_gap_mutex);
-BLE_HS_DBG_ASSERT_EVAL(rc == 0 || rc == OS_NOT_STARTED);
-}
-
-int
-ble_gap_locked_by_cur_task(void)
-{
-struct os_task *owner;
-
-owner = ble_gap_mutex.mu_owner;
-return owner != NULL && owner == os_sched_get_current_task();
-}
-

[3/7] incubator-mynewt-core git commit: ble host - major changes.

2016-04-19 Thread ccollins
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e32f9f9f/net/nimble/host/src/ble_gatts.c
--
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 007958b..73bee78 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -639,7 +639,7 @@ ble_gatts_clt_cfg_access(uint16_t conn_handle, uint16_t 
attr_handle,
 struct ble_hs_conn *conn;
 int rc;
 
-ble_hs_conn_lock();
+ble_hs_lock();
 
 conn = ble_hs_conn_find(conn_handle);
 if (conn == NULL) {
@@ -649,7 +649,7 @@ ble_gatts_clt_cfg_access(uint16_t conn_handle, uint16_t 
attr_handle,
  ctxt, arg);
 }
 
-ble_hs_conn_unlock();
+ble_hs_unlock();
 
 return rc;
 }
@@ -1129,7 +1129,7 @@ ble_gatts_chr_updated(uint16_t chr_def_handle)
 return;
 }
 
-ble_hs_conn_lock();
+ble_hs_lock();
 
 for (conn = ble_hs_conn_first();
  conn != NULL;
@@ -1148,7 +1148,7 @@ ble_gatts_chr_updated(uint16_t chr_def_handle)
 }
 }
 
-ble_hs_conn_unlock();
+ble_hs_unlock();
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e32f9f9f/net/nimble/host/src/ble_hci_block.c
--
diff --git a/net/nimble/host/src/ble_hci_block.c 
b/net/nimble/host/src/ble_hci_block.c
deleted file mode 100644
index e8112c1..000
--- a/net/nimble/host/src/ble_hci_block.c
+++ /dev/null
@@ -1,229 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-/**
- * Provides a blocking HCI send interface.  These functions must not be called
- * from the ble_hs task.
- */
-
-#include 
-#include 
-#include "os/os.h"
-#include "ble_hs_priv.h"
-
-#define BLE_HCI_BLOCK_TIMEOUT   (OS_TICKS_PER_SEC)
-
-/** Protects resources from 2+ application tasks. */
-static struct os_mutex ble_hci_block_mutex;
-
-/** Used to block on expected HCI acknowledgements. */
-static struct os_sem ble_hci_block_sem;
-
-/** Global state corresponding to the current blocking operation. */
-static void *ble_hci_block_cmd;
-static void *ble_hci_block_evt_buf;
-static uint8_t ble_hci_block_evt_buf_len;
-static struct ble_hci_block_result *ble_hci_block_result;
-static uint8_t ble_hci_block_handle;
-static int ble_hci_block_status;
-
-/**
- * Used when the client passes a null result pointer (doesn't care about the
- * event data).
- */
-static struct ble_hci_block_result ble_hci_block_result_anon;
-
-#if PHONY_HCI_ACKS
-static uint8_t ble_hci_block_phony_ack_buf[256];
-static ble_hci_block_phony_ack_fn *ble_hci_block_phony_ack_cb;
-#endif
-
-/**
- * Copies the parameters from an acknowledgement into the application event
- * buffer.
- */
-static void
-ble_hci_block_copy_evt_data(void *src_data, uint8_t src_data_len)
-{
-if (ble_hci_block_evt_buf_len > src_data_len) {
-ble_hci_block_result->evt_buf_len = ble_hci_block_evt_buf_len;
-} else {
-ble_hci_block_result->evt_buf_len = src_data_len;
-}
-ble_hci_block_result->evt_total_len = src_data_len;
-
-if (ble_hci_block_result->evt_buf_len > 0) {
-memcpy(ble_hci_block_evt_buf, src_data,
-   ble_hci_block_result->evt_buf_len);
-}
-}
-
-/**
- * Callback that gets executed upon receiving an HCI acknowledgement.
- */
-static void
-ble_hci_block_ack_cb(struct ble_hci_ack *ack, void *arg)
-{
-uint8_t *ack_params;
-uint8_t ack_params_len;
-
-BLE_HS_DBG_ASSERT(ack->bha_hci_handle == ble_hci_block_handle);
-
-ack_params = ack->bha_params;
-ack_params_len = ack->bha_params_len;
-if (ack->bha_params_len > 0) {
-/* +1/-1 to ignore the status byte. */
-ack_params++;
-ack_params_len--;
-}
-ble_hci_block_copy_evt_data(ack_params, ack_params_len);
-ble_hci_block_status = ack->bha_status;
-
-/* Wake the application task up now that the acknowledgement has been
- * received.
- */
-os_sem_release(_hci_block_sem);
-}
-
-/**
- * Callback that gets executed when an HCI tx reservation is services.
- * Transmits the HCI command specifed by the client task.
- */

[7/7] incubator-mynewt-core git commit: Update example apps to account for blehost changes

2016-04-19 Thread ccollins
Update example apps to account for blehost changes


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

Branch: refs/heads/develop
Commit: 7235e90ad0fdbe44faa2afce0130c3f1cdc782d7
Parents: e32f9f9
Author: Christopher Collins 
Authored: Fri Apr 15 21:04:28 2016 -0700
Committer: Christopher Collins 
Committed: Tue Apr 19 18:11:03 2016 -0700

--
 apps/bleprph/src/main.c |   8 +-
 apps/bletest/src/main.c |  92 +
 apps/bletiny/src/main.c | 114 ++-
 net/nimble/host/include/host/host_hci.h |   1 +
 net/nimble/host/src/ble_hci_util.c  |  29 +++
 net/nimble/host/src/ble_hci_util.h  |   1 +
 net/nimble/host/src/host_hci_cmd.c  |  32 +++-
 net/nimble/include/nimble/hci_common.h  |   3 +-
 8 files changed, 110 insertions(+), 170 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7235e90a/apps/bleprph/src/main.c
--
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index e99c624..a9d8657 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -191,9 +191,6 @@ bleprph_task_handler(void *unused)
  */
 gatt_svr_init();
 
-/* Initialize eventq */
-os_eventq_init(_evq);
-
 /* Begin advertising. */
 bleprph_advertise();
 
@@ -283,7 +280,10 @@ main(void)
 cfg.max_l2cap_chans = 3;
 cfg.max_l2cap_sig_procs = 2;
 
-rc = ble_hs_init(BLEPRPH_BLE_HS_PRIO, );
+/* Initialize eventq */
+os_eventq_init(_evq);
+
+rc = ble_hs_init(_evq, );
 assert(rc == 0);
 
 /* Initialize the console (for log output). */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7235e90a/apps/bletest/src/main.c
--
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index 254abcb..d66a91a 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -223,7 +223,6 @@ bletest_inc_adv_pkt_num(void)
 
 rc = host_hci_cmd_le_set_adv_data(g_host_adv_data, g_host_adv_len);
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 }
 }
 #endif
@@ -244,7 +243,6 @@ bletest_send_conn_update(uint16_t handle)
 
 rc = host_hci_cmd_le_conn_update();
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 }
 
 #ifdef BLE_LL_CFG_FEAT_LE_ENCRYPTION
@@ -258,7 +256,6 @@ void
 bletest_send_ltk_req_neg_reply(uint16_t handle)
 {
 host_hci_cmd_le_lt_key_req_neg_reply(handle);
-host_hci_outstanding_opcode = 0;
 }
 
 void
@@ -269,7 +266,6 @@ bletest_send_ltk_req_reply(uint16_t handle)
 hkr.conn_handle = handle;
 swap_buf(hkr.long_term_key, (uint8_t *)g_bletest_LTK, 16);
 host_hci_cmd_le_lt_key_req_reply();
-host_hci_outstanding_opcode = 0;
 }
 #endif
 
@@ -368,7 +364,6 @@ bletest_init_advertising(void)
 /* Set the advertising parameters */
 rc = host_hci_cmd_le_set_adv_params();
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 
 /* If we are using a random address, we need to set it */
 if (adv.own_addr_type == BLE_HCI_ADV_OWN_ADDR_RANDOM) {
@@ -376,19 +371,16 @@ bletest_init_advertising(void)
 rand_addr[5] |= 0xc0;
 rc = host_hci_cmd_le_set_rand_addr(rand_addr);
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 }
 
 /* Set advertising data */
 if (adv_len != 0) {
 rc = host_hci_cmd_le_set_adv_data(_host_adv_data[0], adv_len);
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 
 /* Set scan response data */
 rc = host_hci_cmd_le_set_scan_rsp_data(_host_adv_data[0], adv_len);
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 }
 }
 #endif
@@ -408,7 +400,6 @@ bletest_init_scanner(void)
  BLE_HCI_ADV_OWN_ADDR_PUBLIC,
  BLETEST_CFG_SCAN_FILT_POLICY);
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 
 filter_policy = BLETEST_CFG_SCAN_FILT_POLICY;
 if (filter_policy & 1) {
@@ -421,7 +412,6 @@ bletest_init_scanner(void)
 dev_addr[5] = 0x08;
 rc = host_hci_cmd_le_add_to_whitelist(dev_addr, BLE_ADDR_TYPE_PUBLIC);
 assert(rc == 0);
-host_hci_outstanding_opcode = 0;
 }
 }
 
@@ -435,12 +425,10 @@ bletest_execute_scanner(void)
 if (g_bletest_state) {
 rc = host_hci_cmd_le_set_scan_enable(0, BLETEST_CFG_FILT_DUP_ADV);
  

[1/7] incubator-mynewt-core git commit: ble host - major changes.

2016-04-19 Thread ccollins
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop f6bc99f0a -> 7235e90ad


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e32f9f9f/net/nimble/host/src/test/ble_gap_test.c
--
diff --git a/net/nimble/host/src/test/ble_gap_test.c 
b/net/nimble/host/src/test/ble_gap_test.c
index 85b630f..7da3d3f 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -110,27 +110,6 @@ ble_gap_test_util_connect_cb(int event, int status,
 return 0;
 }
 
-static int
-ble_gap_test_util_rx_hci_ack(int *cmd_idx, int cmd_fail_idx,
- uint8_t ogf, uint16_t ocf, uint8_t fail_status)
-{
-uint16_t opcode;
-int cur_idx;
-
-opcode = (ogf << 10) | ocf;
-
-cur_idx = *cmd_idx;
-(*cmd_idx)++;
-
-if (cur_idx == cmd_fail_idx) {
-ble_hs_test_util_rx_ack(opcode, fail_status);
-return 1;
-} else {
-ble_hs_test_util_rx_ack(opcode, 0);
-return 0;
-}
-}
-
 static void
 ble_gap_test_util_verify_tx_clear_wl(void)
 {
@@ -364,10 +343,14 @@ ble_gap_test_util_rx_update_complete(
 ble_gap_rx_update_complete();
 }
 
-static void
-ble_gap_test_util_rx_param_req(struct ble_gap_upd_params *params)
+static int
+ble_gap_test_util_rx_param_req(struct ble_gap_upd_params *params, int pos,
+   int *cmd_idx, int cmd_fail_idx,
+   uint8_t fail_status)
 {
 struct hci_le_conn_param_req evt;
+uint16_t opcode;
+uint8_t hci_status;
 
 evt.subevent_code = BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ;
 evt.connection_handle = 2;
@@ -375,8 +358,25 @@ ble_gap_test_util_rx_param_req(struct ble_gap_upd_params 
*params)
 evt.itvl_max = params->itvl_max;
 evt.latency = params->latency;
 evt.timeout = params->supervision_timeout;
+ 
+if (pos) {
+opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
+  BLE_HCI_OCF_LE_REM_CONN_PARAM_RR);
+} else {
+opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
+  BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR);
+}
+if (*cmd_idx == cmd_fail_idx) {
+hci_status = fail_status;
+} else {
+hci_status = 0;
+}
+(*cmd_idx)++;
 
+ble_hs_test_util_set_ack(opcode, hci_status);
 ble_gap_rx_param_req();
+
+return hci_status;
 }
 
 /*
@@ -521,7 +521,6 @@ ble_gap_test_util_disc(uint8_t disc_mode, uint8_t 
*peer_addr,
 
 if (cmd_fail_idx > 1) {
 /* Verify tx of scan enable command. */
-ble_hci_sched_wakeup();
 ble_gap_test_util_verify_tx_scan_enable(1);
 }
 
@@ -576,7 +575,15 @@ TEST_CASE(ble_gap_test_case_conn_disc_good)
 ble_gap_test_util_disc(d, peer_addr, , -1, 0);
 
 TEST_ASSERT(ble_gap_master_in_progress());
-TEST_ASSERT(ble_gap_test_disc_event == -1);
+TEST_ASSERT(ble_gap_test_disc_event == BLE_GAP_EVENT_DISC_SUCCESS);
+TEST_ASSERT(ble_gap_test_disc_status == 0);
+TEST_ASSERT(ble_gap_test_disc_desc.event_type ==
+BLE_HCI_ADV_TYPE_ADV_IND);
+TEST_ASSERT(ble_gap_test_disc_desc.addr_type == BLE_ADDR_TYPE_PUBLIC);
+TEST_ASSERT(ble_gap_test_disc_desc.length_data == 3);
+TEST_ASSERT(ble_gap_test_disc_desc.rssi == 0);
+TEST_ASSERT(memcmp(ble_gap_test_disc_desc.addr, adv.addr, 6) == 0);
+TEST_ASSERT(ble_gap_test_disc_arg == NULL);
 }
 }
 
@@ -706,51 +713,10 @@ TEST_CASE(ble_gap_test_case_conn_dir_bad_args)
 TEST_ASSERT(rc == BLE_HS_EALREADY);
 }
 
-TEST_CASE(ble_gap_test_case_conn_dir_bad_addr)
-{
-struct hci_le_conn_complete evt;
-int rc;
-
-uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-ble_gap_test_util_init();
-
-TEST_ASSERT(!ble_gap_master_in_progress());
-
-rc = ble_hs_test_util_conn_initiate(BLE_ADDR_TYPE_PUBLIC, peer_addr, NULL,
-ble_gap_test_util_connect_cb, NULL, 0);
-TEST_ASSERT(rc == 0);
-
-TEST_ASSERT(ble_gap_master_in_progress());
-
-/* Verify tx of create connection command. */
-ble_gap_test_util_verify_tx_create_conn(BLE_HCI_CONN_FILT_NO_WL);
-TEST_ASSERT(ble_gap_master_in_progress());
-TEST_ASSERT(ble_hs_conn_find(2) == NULL);
-
-/* Receive connection complete event. */
-memset(, 0, sizeof evt);
-evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-evt.status = BLE_ERR_SUCCESS;
-evt.connection_handle = 2;
-evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
-memcpy(evt.peer_addr, ((uint8_t[]){1,1,1,1,1,1}), 6);
-rc = ble_gap_rx_conn_complete();
-TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
-
-TEST_ASSERT(!ble_gap_master_in_progress());
-
-TEST_ASSERT(ble_gap_test_conn_event == BLE_GAP_EVENT_CONN);
-TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 

incubator-mynewt-core git commit: os sim; deal with system time going backwards

2016-04-19 Thread marko
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 1a6f234c6 -> f6bc99f0a


os sim; deal with system time going backwards


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

Branch: refs/heads/develop
Commit: f6bc99f0a1d9d1daa1c8e35d171075f2f2129548
Parents: 1a6f234
Author: Marko Kiiskila 
Authored: Tue Apr 19 16:56:45 2016 -0700
Committer: Marko Kiiskila 
Committed: Tue Apr 19 16:56:45 2016 -0700

--
 libs/os/src/arch/sim/os_arch_sim.c | 55 +++--
 1 file changed, 31 insertions(+), 24 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f6bc99f0/libs/os/src/arch/sim/os_arch_sim.c
--
diff --git a/libs/os/src/arch/sim/os_arch_sim.c 
b/libs/os/src/arch/sim/os_arch_sim.c
index 7a3b27d..1d43ab2 100644
--- a/libs/os/src/arch/sim/os_arch_sim.c
+++ b/libs/os/src/arch/sim/os_arch_sim.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,
@@ -30,7 +30,7 @@
 #include 
 #include 
 #include 
-#include  
+#include 
 #include 
 
 struct stack_frame {
@@ -49,7 +49,7 @@ CTASSERT(offsetof(struct stack_frame, sf_jb) == 4);
 extern void os_arch_frame_init(struct stack_frame *sf);
 
 #define sim_setjmp(__jb) sigsetjmp(__jb, 0)
-#define sim_longjmp(__jb, __ret) siglongjmp(__jb, __ret) 
+#define sim_longjmp(__jb, __ret) siglongjmp(__jb, __ret)
 
 #define OS_USEC_PER_TICK(100 / OS_TICKS_PER_SEC)
 
@@ -79,12 +79,12 @@ os_arch_task_start(struct stack_frame *sf, int rc)
 task = sf->sf_task;
 task->t_func(task->t_arg);
 
-/* This should never return */ 
-assert(0); 
+/* This should never return */
+assert(0);
 }
 
 os_stack_t *
-os_arch_task_stack_init(struct os_task *t, os_stack_t *stack_top, int size) 
+os_arch_task_stack_init(struct os_task *t, os_stack_t *stack_top, int size)
 {
 struct stack_frame *sf;
 
@@ -146,7 +146,7 @@ ctxsw_handler(int sig)
  *
  * Returns 1 if signals were already blocked and 0 otherwise.
  */
-os_sr_t 
+os_sr_t
 os_arch_save_sr(void)
 {
 int error;
@@ -293,11 +293,11 @@ signals_cleanup(void)
 static void
 timer_handler(int sig)
 {
-struct timeval time_now, time_diff; 
+struct timeval time_now, time_diff;
 int ticks;
 
 static struct timeval time_last;
-static int time_inited; 
+static int time_inited;
 
 if (!time_inited) {
 gettimeofday(_last, NULL);
@@ -305,26 +305,33 @@ timer_handler(int sig)
 }
 
 gettimeofday(_now, NULL);
-timersub(_now, _last, _diff);
+if (timercmp(_now, _last, <)) {
+/*
+ * System time going backwards.
+ */
+time_last = time_now;
+} else {
+timersub(_now, _last, _diff);
 
-ticks = time_diff.tv_sec * OS_TICKS_PER_SEC;
-ticks += time_diff.tv_usec / OS_USEC_PER_TICK;
+ticks = time_diff.tv_sec * OS_TICKS_PER_SEC;
+ticks += time_diff.tv_usec / OS_USEC_PER_TICK;
 
-/*
- * Update 'time_last' but account for the remainder usecs that did not
- * contribute towards whole 'ticks'.
- */
-time_diff.tv_sec = 0;
-time_diff.tv_usec %= OS_USEC_PER_TICK;
-timersub(_now, _diff, _last);
+/*
+ * Update 'time_last' but account for the remainder usecs that did not
+ * contribute towards whole 'ticks'.
+ */
+time_diff.tv_sec = 0;
+time_diff.tv_usec %= OS_USEC_PER_TICK;
+timersub(_now, _diff, _last);
 
-os_time_advance(ticks);
+os_time_advance(ticks);
+}
 }
 
 static void
 start_timer(void)
 {
-struct itimerval it; 
+struct itimerval it;
 int rc;
 
 memset(, 0, sizeof(it));
@@ -349,7 +356,7 @@ stop_timer(void)
 assert(rc == 0);
 }
 
-os_error_t 
+os_error_t
 os_arch_os_init(void)
 {
 mypid = getpid();
@@ -378,7 +385,7 @@ os_arch_os_init(void)
 os_error_t
 os_arch_os_start(void)
 {
-struct stack_frame *sf; 
+struct stack_frame *sf;
 struct os_task *t;
 os_sr_t sr;
 
@@ -395,7 +402,7 @@ os_arch_os_start(void)
 t = os_sched_next_task();
 os_sched_set_current_task(t);
 
-g_os_started = 1; 
+g_os_started = 1;
 
 sf = (struct stack_frame *) t->t_stackptr;
 sim_longjmp(sf->sf_jb, 1);



[3/3] incubator-mynewt-core git commit: nffs - Only build logging code if LOG_LEVEL is low enough.

2016-04-19 Thread ccollins
nffs - Only build logging code if LOG_LEVEL is low enough.


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

Branch: refs/heads/develop
Commit: 1a6f234c6a3e99e4c84d6cb80b13e6bf9d3aff43
Parents: 2f430d3
Author: Christopher Collins 
Authored: Tue Apr 19 14:19:19 2016 -0700
Committer: Christopher Collins 
Committed: Tue Apr 19 14:32:23 2016 -0700

--
 fs/nffs/src/nffs_priv.h   | 4 ++--
 fs/nffs/src/nffs_restore.c| 9 +
 fs/nffs/src/test/arch/sim/nffs_test.c | 1 -
 3 files changed, 7 insertions(+), 7 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1a6f234c/fs/nffs/src/nffs_priv.h
--
diff --git a/fs/nffs/src/nffs_priv.h b/fs/nffs/src/nffs_priv.h
index a83256d..3670251 100644
--- a/fs/nffs/src/nffs_priv.h
+++ b/fs/nffs/src/nffs_priv.h
@@ -431,8 +431,8 @@ int nffs_restore_full(const struct nffs_area_desc 
*area_descs);
 int nffs_write_to_file(struct nffs_file *file, const void *data, int len);
 
 
-#define NFFS_HASH_FOREACH(entry, i)  \
-for ((i) = 0; (i) < NFFS_HASH_SIZE; (i)++) \
+#define NFFS_HASH_FOREACH(entry, i) \
+for ((i) = 0; (i) < NFFS_HASH_SIZE; (i)++)  \
 SLIST_FOREACH((entry), _hash[i], nhe_next)
 
 #define NFFS_FLASH_LOC_NONE  nffs_flash_loc(NFFS_AREA_ID_NONE, 0)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1a6f234c/fs/nffs/src/nffs_restore.c
--
diff --git a/fs/nffs/src/nffs_restore.c b/fs/nffs/src/nffs_restore.c
index 8f9c9f8..28ea39a 100644
--- a/fs/nffs/src/nffs_restore.c
+++ b/fs/nffs/src/nffs_restore.c
@@ -975,10 +975,13 @@ nffs_restore_corrupt_scratch(void)
 return 0;
 }
 
-#ifdef notnow
 static void
 nffs_log_contents(void)
 {
+#if LOG_LEVEL > LOG_LEVEL_DEBUG
+return;
+#endif
+
 struct nffs_inode_entry *inode_entry;
 struct nffs_hash_entry *entry;
 struct nffs_block block;
@@ -1030,7 +1033,6 @@ nffs_log_contents(void)
 }
 }
 }
-#endif
 
 /**
  * Searches for a valid nffs file system among the specified areas.  This
@@ -1168,10 +1170,9 @@ nffs_restore_full(const struct nffs_area_desc 
*area_descs)
 goto err;
 }
 
-#ifdef notnow
 NFFS_LOG(DEBUG, "CONTENTS\n");
 nffs_log_contents();
-#endif
+
 return 0;
 
 err:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1a6f234c/fs/nffs/src/test/arch/sim/nffs_test.c
--
diff --git a/fs/nffs/src/test/arch/sim/nffs_test.c 
b/fs/nffs/src/test/arch/sim/nffs_test.c
index 42a7623..8b3b03a 100644
--- a/fs/nffs/src/test/arch/sim/nffs_test.c
+++ b/fs/nffs/src/test/arch/sim/nffs_test.c
@@ -2505,7 +2505,6 @@ int
 main(void)
 {
 tu_config.tc_print_results = 1;
-tu_config.tc_system_assert = 1;
 tu_init();
 
 nffs_test_all();



[2/3] incubator-mynewt-core git commit: nffs - delete cached blocks after gc.

2016-04-19 Thread ccollins
nffs - delete cached blocks after gc.

Garbage collection deletes and recreates data blocks.  Any cached data
blocks are potentially invalid after a garbage collection cycle.
Garbage collection does not delete any inodes, but it may move them
around in flash.

This fix implements the following changes:
* After GC is performed, delete all cached data blocks.
* After GC is performed, refresh all cached inodes (recache them).
* For higher level operations that use pointers to cached data
  blocks, detect when garbage collection occurs, and reset the
  pointers as necessary.


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

Branch: refs/heads/develop
Commit: 2f430d3c9cf6014e2fd1928af07358c5d96abbea
Parents: 3abe740
Author: Christopher Collins 
Authored: Tue Apr 19 14:01:55 2016 -0700
Committer: Christopher Collins 
Committed: Tue Apr 19 14:32:23 2016 -0700

--
 fs/nffs/src/nffs_cache.c | 85 +--
 fs/nffs/src/nffs_gc.c| 34 +
 fs/nffs/src/nffs_priv.h  |  2 +
 fs/nffs/src/nffs_write.c | 32 +---
 4 files changed, 137 insertions(+), 16 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2f430d3c/fs/nffs/src/nffs_cache.c
--
diff --git a/fs/nffs/src/nffs_cache.c b/fs/nffs/src/nffs_cache.c
index 580d52e..51ebc30 100644
--- a/fs/nffs/src/nffs_cache.c
+++ b/fs/nffs/src/nffs_cache.c
@@ -26,7 +26,7 @@ TAILQ_HEAD(nffs_cache_inode_list, nffs_cache_inode);
 static struct nffs_cache_inode_list nffs_cache_inode_list =
 TAILQ_HEAD_INITIALIZER(nffs_cache_inode_list);
 
-static void nffs_cache_collect_blocks(void);
+static void nffs_cache_reclaim_blocks(void);
 
 static struct nffs_cache_block *
 nffs_cache_block_alloc(void)
@@ -56,7 +56,7 @@ nffs_cache_block_acquire(void)
 
 cache_block = nffs_cache_block_alloc();
 if (cache_block == NULL) {
-nffs_cache_collect_blocks();
+nffs_cache_reclaim_blocks();
 cache_block = nffs_cache_block_alloc();
 }
 
@@ -91,8 +91,7 @@ nffs_cache_inode_alloc(void)
 entry = os_memblock_get(_cache_inode_pool);
 if (entry != NULL) {
 memset(entry, 0, sizeof *entry);
-entry->nci_block_list = (struct nffs_cache_block_list)
-TAILQ_HEAD_INITIALIZER(entry->nci_block_list);
+TAILQ_INIT(>nci_block_list);
 }
 
 return entry;
@@ -216,7 +215,7 @@ nffs_cache_inode_range(const struct nffs_cache_inode 
*cache_inode,
 }
 
 static void
-nffs_cache_collect_blocks(void)
+nffs_cache_reclaim_blocks(void)
 {
 struct nffs_cache_inode *cache_inode;
 
@@ -279,6 +278,73 @@ done:
 }
 
 /**
+ * Recaches all cached inodes.  All cached blocks are deleted from the cache
+ * during this operation.  This function should be called after garbage
+ * collection occurs to ensure the cache is consistent.
+ *
+ * @return  0 on success; nonzero on failure.
+ */
+int
+nffs_cache_inode_refresh(void)
+{
+struct nffs_cache_inode *cache_inode;
+struct nffs_inode_entry *inode_entry;
+int rc;
+
+TAILQ_FOREACH(cache_inode, _cache_inode_list, nci_link) {
+/* Clear entire block list. */
+nffs_cache_inode_free_blocks(cache_inode);
+
+inode_entry = cache_inode->nci_inode.ni_inode_entry;
+rc = nffs_inode_from_entry(_inode->nci_inode, inode_entry);
+if (rc != 0) {
+return rc;
+}
+
+/* File size remains valid. */
+}
+
+return 0;
+}
+
+static void
+nffs_cache_log_block(struct nffs_cache_inode *cache_inode,
+ struct nffs_cache_block *cache_block)
+{
+NFFS_LOG(DEBUG, "id=%u inode=%u flash_off=0x%08x "
+"file_off=%u len=%d (entry=%p)\n",
+ cache_block->ncb_block.nb_hash_entry->nhe_id,
+ cache_inode->nci_inode.ni_inode_entry->nie_hash_entry.nhe_id,
+ cache_block->ncb_block.nb_hash_entry->nhe_flash_loc,
+ cache_block->ncb_file_offset,
+ cache_block->ncb_block.nb_data_len,
+ cache_block->ncb_block.nb_hash_entry);
+}
+
+static void
+nffs_cache_log_insert_block(struct nffs_cache_inode *cache_inode,
+struct nffs_cache_block *cache_block,
+int tail)
+{
+NFFS_LOG(DEBUG, "caching block (%s): ", tail ? "tail" : "head");
+nffs_cache_log_block(cache_inode, cache_block);
+}
+
+void
+nffs_cache_insert_block(struct nffs_cache_inode *cache_inode,
+   

[1/3] incubator-mynewt-core git commit: nffs - blocks were missing from end of files.

2016-04-19 Thread ccollins
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 27605cb1c -> 1a6f234c6


nffs - blocks were missing from end of files.

There was a bug in how nffs determines which block comes last in a file
inode.  This process is still horribly inefficient (MYNEWT-161), but it
should at least work 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/3abe7404
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/3abe7404
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/3abe7404

Branch: refs/heads/develop
Commit: 3abe7404b76f6025483486a64646f02acdca24b8
Parents: 27605cb
Author: Christopher Collins 
Authored: Mon Apr 18 20:09:26 2016 -0700
Committer: Christopher Collins 
Committed: Tue Apr 19 14:32:22 2016 -0700

--
 fs/nffs/src/nffs_restore.c | 132 +++-
 1 file changed, 76 insertions(+), 56 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3abe7404/fs/nffs/src/nffs_restore.c
--
diff --git a/fs/nffs/src/nffs_restore.c b/fs/nffs/src/nffs_restore.c
index 947a359..8f9c9f8 100644
--- a/fs/nffs/src/nffs_restore.c
+++ b/fs/nffs/src/nffs_restore.c
@@ -218,6 +218,76 @@ nffs_restore_inode_from_dummy_entry(struct nffs_inode 
*out_inode,
 out_inode->ni_inode_entry = inode_entry;
 }
 
+static int
+nffs_restore_find_file_end_block(struct nffs_hash_entry *block_entry)
+{
+struct nffs_inode_entry *inode_entry;
+struct nffs_block block;
+int rc;
+
+rc = nffs_block_from_hash_entry(, block_entry);
+assert(rc == 0);
+
+inode_entry = block.nb_inode_entry;
+
+/* Make sure the parent inode (file) points to the latest data block
+ * restored so far.
+ *
+ * XXX: This is an O(n) operation (n = # of blocks in the file), and is
+ * horribly inefficient for large files.  MYNEWT-161 has been opened to
+ * address this.
+ */
+if (inode_entry->nie_last_block_entry == NULL) {
+/* This is the first data block restored for this file. */
+inode_entry->nie_last_block_entry = block_entry;
+NFFS_LOG(DEBUG, "setting last block: %u\n", block_entry->nhe_id);
+} else {
+/* Determine if this this data block comes after our current idea of
+ * the file's last data block.
+ */
+
+rc = nffs_block_find_predecessor(
+block_entry, inode_entry->nie_last_block_entry->nhe_id);
+switch (rc) {
+case 0:
+/* The currently-last block is a predecessor of the new block; the
+ * new block comes later.
+ */
+NFFS_LOG(DEBUG, "replacing last block: %u --> %u\n",
+ inode_entry->nie_last_block_entry->nhe_id,
+ block_entry->nhe_id);
+inode_entry->nie_last_block_entry = block_entry;
+break;
+
+case FS_ENOENT:
+break;
+
+default:
+return rc;
+}
+}
+
+return 0;
+}
+
+
+static int
+nffs_restore_find_file_ends(void)
+{
+struct nffs_hash_entry *block_entry;
+int rc;
+int i;
+
+NFFS_HASH_FOREACH(block_entry, i) {
+if (!nffs_hash_id_is_inode(block_entry->nhe_id)) {
+rc = nffs_restore_find_file_end_block(block_entry);
+assert(rc == 0);
+}
+}
+
+return 0;
+}
+
 /**
  * Performs a sweep of the RAM representation at the end of a successful
  * restore.  The sweep phase performs the following actions of each inode in
@@ -483,13 +553,13 @@ nffs_restore_inode(const struct nffs_disk_inode 
*disk_inode, uint8_t area_idx,
 }
 
 if (nffs_hash_id_is_file(inode_entry->nie_hash_entry.nhe_id)) {
-NFFS_LOG(DEBUG, "restoring file; id=%d\n",
+NFFS_LOG(DEBUG, "restoring file; id=0x%08x\n",
  inode_entry->nie_hash_entry.nhe_id);
 if (inode_entry->nie_hash_entry.nhe_id >= nffs_hash_next_file_id) {
 nffs_hash_next_file_id = inode_entry->nie_hash_entry.nhe_id + 1;
 }
 } else {
-NFFS_LOG(DEBUG, "restoring dir; id=%d\n",
+NFFS_LOG(DEBUG, "restoring dir; id=0x%08x\n",
  inode_entry->nie_hash_entry.nhe_id);
 if (inode_entry->nie_hash_entry.nhe_id >= nffs_hash_next_dir_id) {
 nffs_hash_next_dir_id = inode_entry->nie_hash_entry.nhe_id + 1;
@@ -619,7 +689,7 @@ nffs_restore_block(const struct nffs_disk_block 
*disk_block, uint8_t area_idx,
 nffs_restore_largest_block_data_len = disk_block->ndb_data_len;
 }
 
-NFFS_LOG(DEBUG, "restoring block; id=%u seq=%u inode_id=%u prev_id=%u "
+NFFS_LOG(DEBUG, "restoring block; id=0x%08x seq=%u 

incubator-mynewt-core git commit: Add support for 1 megabaud for nrf UART.

2016-04-19 Thread ccollins
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop a590265d3 -> 27605cb1c


Add support for 1 megabaud for nrf UART.


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

Branch: refs/heads/develop
Commit: 27605cb1c0ed6e82bf75ccb0019c4a1e6de1e22d
Parents: a590265
Author: Christopher Collins 
Authored: Mon Apr 18 23:59:37 2016 -0700
Committer: Christopher Collins 
Committed: Mon Apr 18 23:59:37 2016 -0700

--
 hw/mcu/nordic/nrf51xxx/src/hal_uart.c | 2 ++
 hw/mcu/nordic/nrf52xxx/src/hal_uart.c | 2 ++
 2 files changed, 4 insertions(+)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27605cb1/hw/mcu/nordic/nrf51xxx/src/hal_uart.c
--
diff --git a/hw/mcu/nordic/nrf51xxx/src/hal_uart.c 
b/hw/mcu/nordic/nrf51xxx/src/hal_uart.c
index 06d2490..798c2df 100644
--- a/hw/mcu/nordic/nrf51xxx/src/hal_uart.c
+++ b/hw/mcu/nordic/nrf51xxx/src/hal_uart.c
@@ -209,6 +209,8 @@ hal_uart_baudrate(int baudrate)
 return UART_BAUDRATE_BAUDRATE_Baud460800;
 case 921600:
 return UART_BAUDRATE_BAUDRATE_Baud921600;
+case 100:
+return UART_BAUDRATE_BAUDRATE_Baud1M;
 default:
 return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27605cb1/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
--
diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_uart.c 
b/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
index 5e7adac..4986100 100644
--- a/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
@@ -210,6 +210,8 @@ hal_uart_baudrate(int baudrate)
 return UARTE_BAUDRATE_BAUDRATE_Baud460800;
 case 921600:
 return UARTE_BAUDRATE_BAUDRATE_Baud921600;
+case 100:
+return UARTE_BAUDRATE_BAUDRATE_Baud1M;
 default:
 return 0;
 }



[1/2] incubator-mynewt-core git commit: MYNEWT-99: Port encryption to nrf51

2016-04-19 Thread wes3
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop b0a8c8edb -> a590265d3


MYNEWT-99: Port encryption to nrf51

Add encryption support for the nrf51. The nrf51 cannot encrypt
or decrypt frames with payload greater than 27 bytes. This
means if you turn on encryption you will have to change the
maximum LL packet size in the nimble options header file to
27 as the default is 251.


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

Branch: refs/heads/develop
Commit: a590265d37f3bf671969f15cccf8b92828a1da92
Parents: 73ee5e8
Author: William San Filippo 
Authored: Mon Apr 18 23:25:04 2016 -0700
Committer: William San Filippo 
Committed: Mon Apr 18 23:30:06 2016 -0700

--
 net/nimble/controller/src/ble_ll.c  |   3 +-
 net/nimble/controller/src/ble_ll_ctrl.c |  14 +-
 net/nimble/drivers/nrf51/src/ble_phy.c  | 247 ++-
 net/nimble/drivers/nrf52/src/ble_phy.c  |  40 +++--
 net/nimble/include/nimble/nimble_opt.h  |  52 +++---
 5 files changed, 303 insertions(+), 53 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a590265d/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 0ffcfe9..003d594 100644
--- a/net/nimble/controller/src/ble_ll.c
+++ b/net/nimble/controller/src/ble_ll.c
@@ -735,8 +735,7 @@ ble_ll_rx_end(struct os_mbuf *rxpdu, struct ble_mbuf_hdr 
*ble_hdr)
 chan = ble_hdr->rxinfo.channel;
 crcok = BLE_MBUF_HDR_CRC_OK(ble_hdr);
 
-ble_ll_log(BLE_LL_LOG_ID_RX_END,
-   rxbuf[0],
+ble_ll_log(BLE_LL_LOG_ID_RX_END, rxbuf[0],
((uint16_t)ble_hdr->rxinfo.flags << 8) | rxbuf[1],
(BLE_MBUF_HDR_PTR(rxpdu))->end_cputime);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a590265d/net/nimble/controller/src/ble_ll_ctrl.c
--
diff --git a/net/nimble/controller/src/ble_ll_ctrl.c 
b/net/nimble/controller/src/ble_ll_ctrl.c
index 04ddd5b..63b4c98 100644
--- a/net/nimble/controller/src/ble_ll_ctrl.c
+++ b/net/nimble/controller/src/ble_ll_ctrl.c
@@ -31,14 +31,18 @@
 /* To use spec sample data for testing */
 #undef BLE_LL_ENCRYPT_USE_TEST_DATA
 
-/* For console debug to show session key calculation */
+/*
+ * For console debug to show session key calculation. NOTE: if you define
+ * this the stack requirements for the LL task go up considerably. The
+ * default stack will not be enough and must be increased.
+ */
 #undef BLE_LL_ENCRYPT_DEBUG
 #ifdef BLE_LL_ENCRYPT_DEBUG
 #include "console/console.h"
 #endif
 
 /*
- * XXX: TODO
+ * XXX:
  *  1) Do I need to keep track of which procedures have already been done?
  * Do I need to worry about repeating procedures?
  *  2) Should we create pool of control pdu's?. Dont need more
@@ -52,14 +56,8 @@
  *  5) We are supposed to remember when we do the data length update proc if
  *  the device sent us an unknown rsp. We should not send it another len req.
  *  Implement this how? Through remote supported features?
- *  6) Remember: some procedures dont have timeout rules.
- *  7) Says that we should reset procedure timer whenever a LL control pdu
- *  is queued for transmission. I dont get it... do some procedures send
- *  multiple packets? I guess so.
  *  8) How to count control pdus sent. DO we count enqueued + sent, or only
  *  sent (actually attempted to tx). Do we count failures? How?
- *  9) NOTE: we are not supposed to send a REJECT_IND_EXT unless we know the
- *  slave supports that feature
  */
 
 /*

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a590265d/net/nimble/drivers/nrf51/src/ble_phy.c
--
diff --git a/net/nimble/drivers/nrf51/src/ble_phy.c 
b/net/nimble/drivers/nrf51/src/ble_phy.c
index 4de8e83..bc4a258 100644
--- a/net/nimble/drivers/nrf51/src/ble_phy.c
+++ b/net/nimble/drivers/nrf51/src/ble_phy.c
@@ -18,16 +18,36 @@
  */
 
 #include 
+#include 
 #include 
 #include "os/os.h"
 #include "bsp/cmsis_nvic.h"
 #include "nimble/ble.h"
+#include "nimble/nimble_opt.h"
 #include "controller/ble_phy.h"
 #include "controller/ble_ll.h"
 #include "mcu/nrf51_bitfields.h"
 
+/*
+ * XXX: need to make the copy from mbuf into the PHY data structures 32-bit
+ * copies or we are screwed.
+ */
+
 /* XXX: 4) Make sure RF is higher priority interrupt than schedule */
 
+/*
+ * XXX: Maximum 

[2/2] incubator-mynewt-core git commit: Modify write suggested default data length command in controller

2016-04-19 Thread wes3
Modify write suggested default data length command in controller

The prior implementation of this command allowed the host to set
the initial max tx octets/time for a device to a value that it
did not support. The controller will now ignore the host request
if it is not in the supported range.


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

Branch: refs/heads/develop
Commit: 73ee5e8b0b0913280637af866d41c54a633aef25
Parents: b0a8c8e
Author: William San Filippo 
Authored: Mon Apr 18 23:12:10 2016 -0700
Committer: William San Filippo 
Committed: Mon Apr 18 23:30:06 2016 -0700

--
 net/nimble/controller/src/ble_ll_hci.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/73ee5e8b/net/nimble/controller/src/ble_ll_hci.c
--
diff --git a/net/nimble/controller/src/ble_ll_hci.c 
b/net/nimble/controller/src/ble_ll_hci.c
index 14a6b52..8149bf4 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -281,8 +281,14 @@ ble_ll_hci_le_read_bufsize(uint8_t *rspbuf, uint8_t 
*rsplen)
 
 #ifdef BLE_LL_CFG_FEAT_DATA_LEN_EXT
 /**
- * HCI write suggested default data length command. Returns the controllers
- * initial max tx octet/time.
+ * HCI write suggested default data length command.
+ *
+ * This command is used by the host to change the initial max tx octets/time
+ * for all connections. Note that if the controller does not support the
+ * requested times no error is returned; the controller simply ignores the
+ * request (but remembers what the host requested for the read suggested
+ * default data length command). The spec allows for the controller to
+ * disregard the host.
  *
  * @param rspbuf Pointer to response buffer
  * @param rsplen Length of response buffer
@@ -304,8 +310,12 @@ ble_ll_hci_le_wr_sugg_data_len(uint8_t *cmdbuf)
 if (ble_ll_chk_txrx_octets(tx_oct) && ble_ll_chk_txrx_time(tx_time)) {
 g_ble_ll_conn_params.sugg_tx_octets = (uint8_t)tx_oct;
 g_ble_ll_conn_params.sugg_tx_time = tx_time;
-g_ble_ll_conn_params.conn_init_max_tx_octets = tx_oct;
-g_ble_ll_conn_params.conn_init_max_tx_time = tx_time;
+
+if ((tx_time < g_ble_ll_conn_params.supp_max_tx_time) &&
+(tx_oct < g_ble_ll_conn_params.supp_max_tx_octets)) {
+g_ble_ll_conn_params.conn_init_max_tx_octets = tx_oct;
+g_ble_ll_conn_params.conn_init_max_tx_time = tx_time;
+}
 rc = BLE_ERR_SUCCESS;
 } else {
 rc = BLE_ERR_INV_HCI_CMD_PARMS;