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

[01/16] incubator-mynewt-core git commit: remove DAC, ADC and PWM adcs. remove sysid and int/ext concept from I2C and SPI adcs. add drivers / device framework first cut. this is an interim checkin.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/sterly_refactor 2fcf6aa87 -> 93d29b847


remove DAC,ADC and PWM adcs.  remove sysid and int/ext concept from I2C and SPI adcs.  add drivers / device framework first cut.  this is an interim checkin.


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

Branch: refs/heads/sterly_refactor
Commit: 93d29b8476f16877518ba117ff1894931263716e
Parents: db980b2
Author: Sterling Hughes <st...@apache.org>
Authored: Sun Jul 31 21:58:14 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 .../adc/adc_nrf52/include/adc_nrf52/adc_nrf52.h |  27 +++
 drivers/adc/adc_nrf52/pkg.yml                   |  29 +++
 drivers/adc/adc_nrf52/src/adc_nrf52.c           |  65 +++++++
 drivers/adc/include/adc/adc.h                   |  43 +++++
 drivers/adc/pkg.yml                             |  27 +++
 drivers/adc/src/adc.c                           |  22 +++
 hw/hal/include/hal/hal_adc.h                    |  73 --------
 hw/hal/include/hal/hal_adc_int.h                |  71 --------
 hw/hal/include/hal/hal_dac.h                    |  94 ----------
 hw/hal/include/hal/hal_dac_int.h                |  74 --------
 hw/hal/include/hal/hal_i2c.h                    |  15 +-
 hw/hal/include/hal/hal_i2c_int.h                |  53 ------
 hw/hal/include/hal/hal_pwm.h                    |  84 ---------
 hw/hal/include/hal/hal_pwm_int.h                |  59 -------
 hw/hal/include/hal/hal_spi.h                    |   3 +-
 hw/hal/include/hal/hal_spi_int.h                |  78 --------
 hw/hal/src/hal_adc.c                            |  78 --------
 hw/hal/src/hal_dac.c                            |  96 ----------
 hw/hal/src/hal_i2c.c                            |  77 --------
 hw/hal/src/hal_pwm.c                            |  74 --------
 hw/hal/src/hal_spi.c                            |  44 -----
 libs/os/include/os/os_dev.h                     |  84 +++++++++
 libs/os/src/os.c                                |   4 +
 libs/os/src/os_dev.c                            | 177 +++++++++++++++++++
 24 files changed, 486 insertions(+), 965 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/drivers/adc/adc_nrf52/include/adc_nrf52/adc_nrf52.h
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_nrf52/include/adc_nrf52/adc_nrf52.h b/drivers/adc/adc_nrf52/include/adc_nrf52/adc_nrf52.h
new file mode 100644
index 0000000..898503b
--- /dev/null
+++ b/drivers/adc/adc_nrf52/include/adc_nrf52/adc_nrf52.h
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __ADC_NRF52_H__
+#define __ADC_NRF52_H__
+
+#include <adc/adc.h>
+
+int nrf52_adc_dev_init(struct os_dev *);
+
+#endif /* __ADC_H__ */

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

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/drivers/adc/adc_nrf52/src/adc_nrf52.c
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_nrf52/src/adc_nrf52.c b/drivers/adc/adc_nrf52/src/adc_nrf52.c
new file mode 100644
index 0000000..c8e5741
--- /dev/null
+++ b/drivers/adc/adc_nrf52/src/adc_nrf52.c
@@ -0,0 +1,65 @@
+/*
+ * 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 <adc/adc.h>
+
+/* Nordic headers */
+#include <nrf.h>
+#include <nrf_drv_saadc.h>
+#include <app_error.h>
+
+
+static int
+nrf52_adc_configure(struct adc_dev *dev, void *cfgdata)
+{
+
+    return (0);
+}
+
+static int
+nrf52_adc_sample(struct adc_dev *dev)
+{
+    nrf_drv_saadc_sample();
+
+    return (0);
+}
+
+/**
+ * Callback to initialize an adc_dev structure from the os device
+ * initialization callback.  This sets up a nrf52_adc_device(), so
+ * that subsequent lookups to this device allow us to manipulate it.
+ */
+int
+nrf52_adc_dev_init(struct os_dev *odev)
+{
+    struct adc_dev *dev;
+    struct adc_driver_funcs *af;
+
+    dev = (struct adc_dev *) odev;
+
+    af = &dev->ad_funcs;
+
+    af->af_config = nrf52_adc_configure;
+    af->af_sample = nrf52_adc_sample;
+
+    return (0);
+}
+
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/drivers/adc/include/adc/adc.h
----------------------------------------------------------------------
diff --git a/drivers/adc/include/adc/adc.h b/drivers/adc/include/adc/adc.h
new file mode 100644
index 0000000..01c60ea
--- /dev/null
+++ b/drivers/adc/include/adc/adc.h
@@ -0,0 +1,43 @@
+/*
+ * 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 __ADC_H__
+#define __ADC_H__
+
+#include <os/os_dev.h>
+
+struct adc_dev;
+
+typedef int (*adc_config_func_t)(struct adc_dev *dev, void *);
+typedef int (*adc_sample_func_t)(struct adc_dev *);
+
+struct adc_driver_funcs {
+    adc_config_func_t af_config;
+    adc_sample_func_t af_sample;
+};
+
+struct adc_dev {
+    struct os_dev ad_dev;
+    struct adc_driver_funcs ad_funcs;
+};
+
+#define adc_sample(__dev) ((__dev)->ad_funcs.af_sample((__dev)))
+#define adc_configure(__dev, __b) ((__dev)->ad_funcs.af_configure((__dev), (__b)))
+
+#endif /* __ADC_H__ */

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

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

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_adc.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_adc.h b/hw/hal/include/hal/hal_adc.h
deleted file mode 100644
index 84b16d5..0000000
--- a/hw/hal/include/hal/hal_adc.h
+++ /dev/null
@@ -1,73 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_HAL_ADC_
-#define H_HAL_ADC_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* for the pin descriptor enum */
-#include <bsp/bsp_sysid.h>
-
-/* This is the device for an ADC. The application using the ADC device
- * does not need to know the definition of this device and can operate
- * with a pointer to this device.  you can get/build device pointers in the
- * BSP
- */
-struct hal_adc;
-
-/* initialize the ADC on the corresponding BSP Pin. Returns a pointer
- * to the adc object to use for the methods below. Returns NULL on
- * error
- */
- struct hal_adc *hal_adc_init(enum system_device_id sysid);
-
-/* read the ADC corresponding to sysid in your system.  Returns
- * the  adc value read or negative on error, See
- * hal_adc_get_resolution to check the range of the return value
- */
-int hal_adc_read(struct hal_adc *padc);
-
-/* returns the number of bit of resolution in this ADC.
- * For example if the system has an 8-bit ADC reporting
- * values from 0= to 255 (2^8-1), this function would return
- * the value 8. returns negative or zero on error
- */
-int hal_adc_get_bits(struct hal_adc *padc);
-
-/* Returns the positive reference voltage for a maximum ADC reading.
- * This API assumes the negative reference voltage is zero volt.
- * Returns negative or zero on error.
- */
-int hal_adc_get_ref_mv(struct hal_adc *padc);
-
-/* Converts and ADC value to millivolts. This is a helper function
- * but does call the ADC to get the reference voltage and
- * resolution
- */
-int hal_adc_to_mv(struct hal_adc *padc, int val);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_HAL_ADC_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_adc_int.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_adc_int.h b/hw/hal/include/hal/hal_adc_int.h
deleted file mode 100644
index 109bb0a..0000000
--- a/hw/hal/include/hal/hal_adc_int.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef HAL_ADC_INT_H
-#define HAL_ADC_INT_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <inttypes.h>
-#include <bsp/bsp_sysid.h>
-
-
-struct hal_adc;
-
-/* These functions make up the driver API for ADC devices.  All
- * ADC devices with Mynewt support implement this interface
- */
-struct hal_adc_funcs {
-    int (*hadc_read)            (struct hal_adc *padc);
-    int (*hadc_get_bits)         (struct hal_adc *padc);
-    int (*hadc_get_ref_mv)       (struct hal_adc *padc);
-};
-
-/* This is the internal device representation for a hal_adc device.
- *
- * Its main goal is to wrap the const drivers in a non-const structure.
- * Thus these can be made on the stack and wrapped with other non-const
- * structures.
- *
- * For example, if you are creating a adc driver you can use
- *
- * struct my_adc_driver {
- *     struct hal_adc_s parent;
- *     int              my_stuff 1;
- *     char            *mybuff;
- * };
- *
- * See the native MCU and BSP for examples
- */
-struct hal_adc {
-    const struct hal_adc_funcs  *driver_api;
-};
-
-/* The  BSP must implement this factory to get devices for the
- * application.  */
-extern struct hal_adc *
-bsp_get_hal_adc(enum system_device_id sysid);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* HAL_ADC_INT_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_dac.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_dac.h b/hw/hal/include/hal/hal_dac.h
deleted file mode 100644
index 93eded4..0000000
--- a/hw/hal/include/hal/hal_dac.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef HAL_DAC_H_
-#define HAL_DAC_H_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* for the pin descriptor enum */
-#include <bsp/bsp_sysid.h>
-
-/* This is the device for a Digital to Analog Converter (DAC).
- * The application using the DAC device
- * does not need to know the definition of this device and can operate
- * with a pointer to this device.  you can get/build device pointers in the
- * BSP
- *
- * NOTE: You can also use PWM devices to simulate analog output.
- * These are defined in hal_pwm.h
- */
-struct hal_dac;
-
-/* initialize the DAC on the corresponding BSP device. Returns a pointer
- * to the DAC object to use for the methods below. Returns NULL on
- * error
- */
-struct hal_dac *
-hal_dac_init(enum system_device_id sysid);
-
-/*
- * write the DAC corresponding to sysid in your system
- * and enables the DAC.  Return 0 on success negative on failures. If you
- * write a value larger than the DAC size, it will get truncated to the
- * maximum DAC value but the write will succeed.
- */
-int
-hal_dac_write(struct hal_dac *pdac, int val);
-
-/*
- * Gets the current value that is output on the DAC .
- * Return the current value on success negative on failures.
- */
-int
-hal_dac_get_current(struct hal_dac *pdac);
-
-/*
- * Returns the number of bit of resolution in this DAC.
- * For example if the system has an 8-bit DAC reporting
- * values from 0= to 255 (2^8-1), this function would return
- * the value 8. returns negative or zero on error */
-int
-hal_dac_get_bits(struct hal_dac *pdac);
-
-/*
- * Returns the positive reference voltage for a maximum DAC reading.
- * This API assumes the negative reference voltage is zero volt.
- * Returns negative or zero on error.
- */
-int
-hal_dac_get_ref_mv(struct hal_dac *pdac);
-
-/* turns the DAC off.  Re-enable with hal_dac_write */
-int
-hal_dac_disable(struct hal_dac *pdac);
-
-
-/* Converts a value in millivolts to a DAC value for this DAC */
-int
-hal_dac_to_val(struct hal_dac *pdac, int mvolts);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_HAL_DAC_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_dac_int.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_dac_int.h b/hw/hal/include/hal/hal_dac_int.h
deleted file mode 100644
index 2bb01cd..0000000
--- a/hw/hal/include/hal/hal_dac_int.h
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_HAL_DAC_INT_
-#define H_HAL_DAC_INT_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <bsp/bsp_sysid.h>
-
-
-struct hal_dac;
-
-/* These functions make up the driver API for DAC devices.  All
- * DAC devices with Mynewt support implement this interface
- */
-struct hal_dac_funcs {
-    int (*hdac_write)            (struct hal_dac *pdac, int val);
-    int (*hdac_current)          (struct hal_dac *pdac);
-    int (*hdac_disable)          (struct hal_dac *pdac);
-    int (*hdac_get_bits)         (struct hal_dac *pdac);
-    int (*hdac_get_ref_mv)       (struct hal_dac *pdac);
-};
-
-/* This is the internal device representation for a hal_dac device.
- *
- * Its main goal is to wrap the const drivers in a non-const structure.
- * Thus these can be made on the stack and wrapped with other non-const
- * structures.
- *
- * For example, if you are creating a dac driver you can use
- *
- * struct my_dac_driver {
- *     struct hal_dac   parent;
- *     int              my_stuff 1;
- *     char            *mybuff;
- * };
- *
- * See the native MCU and BSP for examples
- */
-struct hal_dac {
-    const struct hal_dac_funcs  *driver_api;
-};
-
-/* The  BSP must implement this factory to get devices for the
- * application.
- */
-extern struct hal_dac *
-bsp_get_hal_dac(enum system_device_id sysid);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* HAL_DAC_INT_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/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
index a1361cd..876e287 100644
--- a/hw/hal/include/hal/hal_i2c.h
+++ b/hw/hal/include/hal/hal_i2c.h
@@ -64,11 +64,10 @@ struct hal_i2c_master_data {
     uint8_t *buffer;    /* buffer space to hold the transmit or receive */
 };
 
-/* Initialize a new i2c device with the given system id.
+/* Initialize a new i2c device with the I2C number.
  * Returns a pointer to the i2c device or NULL on error
  */
-struct hal_i2c*
-hal_i2c_init(enum system_device_id sysid);
+struct hal_i2c *hal_i2c_init(uint8_t i2c_num);
 
 /* Sends a start condition and writes <len> bytes of data on the i2c.
  * This API assumes that you have already called hal_i2c_master_begin
@@ -79,7 +78,7 @@ hal_i2c_init(enum system_device_id sysid);
  * Returns 0 on success, negative on failure
  */
 int
-hal_i2c_master_write(struct hal_i2c*, struct hal_i2c_master_data *pdata);
+hal_i2c_master_write(struct hal_i2c *, struct hal_i2c_master_data *pdata);
 
 /* Sends a start condition and reads <len> bytes of data on the i2c.
  * This API assumes that you have already called hal_i2c_master_begin
@@ -90,20 +89,20 @@ hal_i2c_master_write(struct hal_i2c*, struct hal_i2c_master_data *pdata);
  * Returns 0 on success, negative on failure
  */
 int
-hal_i2c_master_read(struct hal_i2c*, struct hal_i2c_master_data *pdata);
+hal_i2c_master_read(struct hal_i2c *, struct hal_i2c_master_data *pdata);
 
 /*
  * Starts an I2C transaction with the driver. This API does not send
  * anything over the bus itself
  */
 int
-hal_i2c_master_begin(struct hal_i2c*);
+hal_i2c_master_begin(struct hal_i2c *);
 
 /* issues a stop condition on the bus and ends the I2C transaction.
  * You must call i2c_master_end for every hal_i2c_master_begin
  * API call that succeeds  */
 int
-hal_i2c_master_end(struct hal_i2c*);
+hal_i2c_master_end(struct hal_i2c *);
 
 /* Probes the i2c bus for a device with this address.  THIS API
  * issues a start condition, probes the address using a read
@@ -111,7 +110,7 @@ hal_i2c_master_end(struct hal_i2c*);
  * hal_i2c_master_begin/end with this method
  */
 int
-hal_i2c_master_probe(struct hal_i2c*, uint8_t address);
+hal_i2c_master_probe(struct hal_i2c *, uint8_t address);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/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
deleted file mode 100644
index 13e6e5f..0000000
--- a/hw/hal/include/hal/hal_i2c_int.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef H_HAL_I2C_INT_
-#define H_HAL_I2C_INT_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <hal/hal_i2c.h>
-#include <inttypes.h>
-
-struct hal_i2c;
-
-struct hal_i2c_funcs {
-    int (*hi2cm_write_data) (struct hal_i2c *pi2c, struct hal_i2c_master_data *ppkt);
-    int (*hi2cm_read_data)  (struct hal_i2c *pi2c, struct hal_i2c_master_data *ppkt);
-    int (*hi2cm_probe)      (struct hal_i2c *pi2c, uint8_t address);
-    int (*hi2cm_start)      (struct hal_i2c *pi2c);
-    int (*hi2cm_stop)       (struct hal_i2c *pi2c);
-};
-
-struct hal_i2c {
-    const struct hal_i2c_funcs *driver_api;
-};
-
-struct hal_i2c *
-bsp_get_hal_i2c_driver(enum system_device_id sysid);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_HAL_I2C_INT_ */
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_pwm.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_pwm.h b/hw/hal/include/hal/hal_pwm.h
deleted file mode 100644
index 6deb1e7..0000000
--- a/hw/hal/include/hal/hal_pwm.h
+++ /dev/null
@@ -1,84 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_HAL_HAL_PWM_
-#define H_HAL_HAL_PWM_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <inttypes.h>
-#include <bsp/bsp_sysid.h>
-
-/* This is an abstract hardware API to Pulse Width Modulators.
- * A Pulse width module produces an output pulse stream with
- * a specified period, and duty cycle.
- */
-struct hal_pwm;
-
-/* Initialize a new PWM device with the given system id.
- * Returns negative on error, 0 on success.
- */
-struct hal_pwm*
-hal_pwm_init(enum system_device_id sysid);
-
-/* gets the underlying clock driving the PWM output. Return value
- * is in Hz. Returns negative on error
- */
-int
-hal_pwm_get_source_clock_freq(struct hal_pwm *ppwm);
-
-/* gets the resolution of the PWM in bits.  An N-bit PWM can have
- * on values between 0 and 2^bits - 1. Returns negative on error
- */
-int
-hal_pwm_get_resolution_bits(struct hal_pwm *ppwm);
-
-/* turns off the PWM channel */
-int
-hal_pwm_disable(struct hal_pwm *ppwm);
-
-/* enables the PWM with duty cycle specified. This duty cycle is
- * a fractional duty cycle where 0 == off, 65535=on, and
- * any value in between is on for fraction clocks and off
- * for 65535-fraction clocks.
- */
-int
-hal_pwm_enable_duty_cycle(struct hal_pwm *ppwm, uint16_t fraction);
-
-/*
- * This frequency must be between 1/2 the clock frequency and
- * the clock divided by the resolution. NOTE: This may affect
- * other PWM channels.
- */
-int
-hal_pwm_set_frequency(struct hal_pwm *ppwm, uint32_t freq_hz);
-
-/* NOTE: If you know the resolution and clock frequency, you can
- * compute the period of the PWM Its 2^resolution/clock_freq
- */
-
-
-#ifdef __cplusplus
-}
-#endif
-
-
-#endif /* H_HAL_HAL_PWM_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_pwm_int.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_pwm_int.h b/hw/hal/include/hal/hal_pwm_int.h
deleted file mode 100644
index b081da1..0000000
--- a/hw/hal/include/hal/hal_pwm_int.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_HAL_PWM_INT_
-#define H_HAL_PWM_INT_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <hal/hal_pwm.h>
-#include <inttypes.h>
-
-/* when you are implementing a driver for the hal_pwm. This is the interface
- * you must provide.
- */
-
-struct hal_pwm;
-
-struct hal_pwm_funcs {
-    /* the low level hal API */
-    int     (*hpwm_get_bits)        (struct hal_pwm *ppwm);
-    int     (*hpwm_get_clk)         (struct hal_pwm *ppwm);
-    int     (*hpwm_disable)         (struct hal_pwm *ppwm);
-    int     (*hpwm_ena_duty)  (struct hal_pwm *ppwm, uint16_t frac_duty);
-    int     (*hpwm_set_freq)  (struct hal_pwm *ppwm, uint32_t freq_hz);
-
-};
-
-struct hal_pwm {
-    const struct hal_pwm_funcs *driver_api;
-};
-
-struct hal_pwm *
-bsp_get_hal_pwm_driver(enum system_device_id sysid);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_HAL_PWM_INT_ */
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_spi.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_spi.h b/hw/hal/include/hal/hal_spi.h
index 587812f..4ed5aa6 100644
--- a/hw/hal/include/hal/hal_spi.h
+++ b/hw/hal/include/hal/hal_spi.h
@@ -62,8 +62,7 @@ struct hal_spi_settings {
  * to the SPI object to use for the methods below. Returns NULL on
  * error
  */
-struct hal_spi *
-hal_spi_init(enum system_device_id sysid);
+struct hal_spi *hal_spi_init(uint8_t spi_num);
 
 /* configure the spi., Reutrns 0 on success, negative on error */
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/include/hal/hal_spi_int.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_spi_int.h b/hw/hal/include/hal/hal_spi_int.h
deleted file mode 100644
index 473e07e..0000000
--- a/hw/hal/include/hal/hal_spi_int.h
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_HAL_SPI_INT_
-#define H_HAL_SPI_INT_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <bsp/bsp_sysid.h>
-
-struct hal_spi;
-
-/* configure the spi */
-int
-hal_spi_config(struct hal_spi *pspi, struct hal_spi_settings *psettings);
-
-/* do a blocking master spi transfer */
-int
-hal_spi_master_transfer(struct hal_spi *psdi, uint16_t tx);
-
-/* These functions make up the driver API for DAC devices.  All
- * DAC devices with Mynewt support implement this interface
- */
-struct hal_spi_funcs {
-    int (*hspi_config)           (struct hal_spi *pspi, struct hal_spi_settings *psettings);
-    int (*hspi_master_transfer)  (struct hal_spi *psdi, uint16_t tx);
-};
-
-/* This is the internal device representation for a hal_spi device.
- *
- * Its main goal is to wrap the const drivers in a non-const structure.
- * Thus these can be made on the stack and wrapped with other non-const
- * structures.
- *
- * For example, if you are creating a spi driver you can use
- *
- * struct my_spi_driver {
- *     struct hal_spi   parent;
- *     int              my_stuff 1;
- *     char            *mybuff;
- * };
- *
- * See the native MCU and BSP for examples
- */
-struct hal_spi {
-    const struct hal_spi_funcs  *driver_api;
-};
-
-/* The  BSP must implement this factory to get devices for the
- * application.
- */
-extern struct hal_spi *
-bsp_get_hal_spi(enum system_device_id sysid);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_HAL_SPI_INT_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/src/hal_adc.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/hal_adc.c b/hw/hal/src/hal_adc.c
deleted file mode 100644
index 6470ed9..0000000
--- a/hw/hal/src/hal_adc.c
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <inttypes.h>
-#include <hal/hal_adc.h>
-#include <hal/hal_adc_int.h>
-
-struct hal_adc *
-hal_adc_init(enum system_device_id pin) 
-{
-    return bsp_get_hal_adc(pin);
-}
-
-int 
-hal_adc_read(struct hal_adc *padc) 
-{
-    if (padc && padc->driver_api && padc->driver_api->hadc_read) {
-        return padc->driver_api->hadc_read(padc);
-    }
-    return -1;
-}
-
-int 
-hal_adc_get_bits(struct hal_adc *padc) 
-{
-    if (padc && padc->driver_api && padc->driver_api->hadc_get_bits) {
-        return padc->driver_api->hadc_get_bits(padc);
-    }
-    return -1;
-}
-
-int 
-hal_adc_get_ref_mv(struct hal_adc *padc) 
-{
-    if (padc && padc->driver_api && padc->driver_api->hadc_get_ref_mv) {
-        return padc->driver_api->hadc_get_ref_mv(padc);
-    }
-    return -1;
-}
-
-/* returns the ADC read value converted to mvolts or negative on error */
-int 
-hal_adc_to_mv(struct hal_adc *padc, int val) 
-{
-    int ret_val = -1;
-    
-    if (val >= 0)  {
-        int ref;
-        ref = hal_adc_get_ref_mv(padc);   
-        if (ref > 0) {
-            val *= ref;
-            ref = hal_adc_get_bits(padc);            
-            /* doubt there will be many 1 bit ADC, but this will
-             * adjust if its two bits or more */
-            if ( ref > 1) {
-                /* round */
-                val += (1 << (ref - 2));
-                ret_val = (val >> ref);                
-            } 
-        }
-    }
-    return ret_val;
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/src/hal_dac.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/hal_dac.c b/hw/hal/src/hal_dac.c
deleted file mode 100644
index c083a75..0000000
--- a/hw/hal/src/hal_dac.c
+++ /dev/null
@@ -1,96 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <hal/hal_dac.h>
-#include <hal/hal_dac_int.h>
-
-struct hal_dac *
-hal_dac_init(enum system_device_id pin) 
-{
-    return bsp_get_hal_dac(pin);
-}
-
-int 
-hal_dac_write(struct hal_dac *pdac, int val) 
-{
-    if (pdac && pdac->driver_api && pdac->driver_api->hdac_write) {
-        return pdac->driver_api->hdac_write(pdac, val);
-    }
-    return -1;
-}
-
-int 
-hal_dac_get_bits(struct hal_dac *pdac) 
-{
-    if (pdac && pdac->driver_api && pdac->driver_api->hdac_get_bits) {
-        return pdac->driver_api->hdac_get_bits(pdac);
-    }
-    return -1;
-}
-
-int 
-hal_dac_get_ref_mv(struct hal_dac *pdac) 
-{
-    if (pdac && pdac->driver_api && pdac->driver_api->hdac_get_ref_mv) {
-        return pdac->driver_api->hdac_get_ref_mv(pdac);
-    }
-    return -1;
-}
-
-/* gets the current DAC value */
-int 
-hal_dac_get_current(struct hal_dac *pdac) 
-{
-    if (pdac && pdac->driver_api && pdac->driver_api->hdac_get_ref_mv) {
-        return pdac->driver_api->hdac_current(pdac);
-    }
-    return -1;
-}
-
-/* Converts a millivolt value to the right DAC settings for this DAC.
- */
-int 
-hal_dac_to_val(struct hal_dac *pdac, int mvolts) 
-{
-    int rc = -1;
-    int bits = hal_dac_get_bits(pdac);
-    int ref = hal_dac_get_ref_mv(pdac);
-            
-    if ((bits > 0) && (ref >= 0)) 
-    {
-        /* assume that 2^N -1 is full scale. This line multiples by 2^N-1 
-         * and then rounds up */
-        rc = (mvolts << bits) - mvolts + (ref >> 1);
-        rc /= ref;
-        
-        /* don't let this exceed the DAC max */
-        if(rc >= (1 << bits)) {
-            rc = (1 << bits) - 1;
-        }
-    }
-    return rc;
-}
-
-int 
-hal_dac_disable(struct hal_dac *pdac) 
-{
-    if (pdac && pdac->driver_api && pdac->driver_api->hdac_disable) {
-        return pdac->driver_api->hdac_disable(pdac);
-    }
-    return -1;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/src/hal_i2c.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/hal_i2c.c b/hw/hal/src/hal_i2c.c
deleted file mode 100644
index c383fe5..0000000
--- a/hw/hal/src/hal_i2c.c
+++ /dev/null
@@ -1,77 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <bsp/bsp_sysid.h>
-#include <hal/hal_i2c.h>
-#include <hal/hal_i2c_int.h>
-
-struct hal_i2c *
-hal_i2c_init(enum system_device_id sysid)
-{
-    return bsp_get_hal_i2c_driver(sysid);
-}
-
-int
-hal_i2c_master_write(struct hal_i2c *pi2c, struct hal_i2c_master_data *ppkt)
-{
-    if (pi2c && pi2c->driver_api && pi2c->driver_api->hi2cm_write_data )
-    {
-        return pi2c->driver_api->hi2cm_write_data(pi2c, ppkt);
-    }
-    return -1;
-}
-
-int
-hal_i2c_master_read(struct hal_i2c *pi2c, struct hal_i2c_master_data *ppkt)
-{
-    if (pi2c && pi2c->driver_api && pi2c->driver_api->hi2cm_read_data )
-    {
-        return pi2c->driver_api->hi2cm_read_data(pi2c, ppkt);
-    }
-    return -1;
-}
-
-int
-hal_i2c_master_probe(struct hal_i2c *pi2c, uint8_t address)
-{
-    if (pi2c && pi2c->driver_api && pi2c->driver_api->hi2cm_probe )
-    {
-        return pi2c->driver_api->hi2cm_probe(pi2c, address);
-    }
-    return -1;
-}
-
-int
-hal_i2c_master_begin(struct hal_i2c *pi2c)
-{
-    if (pi2c && pi2c->driver_api && pi2c->driver_api->hi2cm_start )
-    {
-        return pi2c->driver_api->hi2cm_start(pi2c);
-    }
-    return -1;
-}
-
-int
-hal_i2c_master_end(struct hal_i2c *pi2c)
-{
-    if (pi2c && pi2c->driver_api && pi2c->driver_api->hi2cm_stop )
-    {
-        return pi2c->driver_api->hi2cm_stop(pi2c);
-    }
-    return -1;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/src/hal_pwm.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/hal_pwm.c b/hw/hal/src/hal_pwm.c
deleted file mode 100644
index b20b2e0..0000000
--- a/hw/hal/src/hal_pwm.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <bsp/bsp_sysid.h>
-#include <hal/hal_pwm.h>
-#include <hal/hal_pwm_int.h>
-
-struct hal_pwm *
-hal_pwm_init(enum system_device_id sysid) 
-{
-    return bsp_get_hal_pwm_driver(sysid);
-}
-
-
-int
-hal_pwm_disable(struct hal_pwm *ppwm) 
-{
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_disable ) 
-    {
-        return ppwm->driver_api->hpwm_disable(ppwm);
-    }
-    return -1;      
-}
-
-int
-hal_pwm_enable_duty_cycle(struct hal_pwm *ppwm, uint16_t fraction) {
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_ena_duty ) 
-    {
-        return ppwm->driver_api->hpwm_ena_duty(ppwm, fraction);
-    }
-    return -1;      
-}
-
-int
-hal_pwm_set_frequency(struct hal_pwm *ppwm, uint32_t freq_hz) {
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_set_freq) 
-    {
-        return ppwm->driver_api->hpwm_set_freq(ppwm, freq_hz);
-    }
-    return -1;       
-}
-
-int 
-hal_pwm_get_clock_freq(struct hal_pwm *ppwm) {
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_get_clk ) 
-    {
-        return ppwm->driver_api->hpwm_get_clk(ppwm);
-    }
-    return -1;     
-}
-
-int
-hal_pwm_get_resolution_bits(struct hal_pwm *ppwm) {
-    if (ppwm && ppwm->driver_api && ppwm->driver_api->hpwm_get_bits ) 
-    {
-        return ppwm->driver_api->hpwm_get_bits(ppwm);
-    }
-    return -1;        
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/hw/hal/src/hal_spi.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/hal_spi.c b/hw/hal/src/hal_spi.c
deleted file mode 100644
index 5b53150..0000000
--- a/hw/hal/src/hal_spi.c
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <hal/hal_spi.h>
-#include <hal/hal_spi_int.h>
-
-struct hal_spi *
-hal_spi_init(enum system_device_id pin) 
-{
-    return bsp_get_hal_spi(pin);
-}
-
-int 
-hal_spi_config(struct hal_spi *pspi, struct hal_spi_settings *psettings) 
-{
-    if (pspi && pspi->driver_api && pspi->driver_api->hspi_config) {
-        return pspi->driver_api->hspi_config(pspi, psettings);
-    }
-    return -1;
-}
-
-int 
-hal_spi_master_transfer(struct hal_spi *pspi, uint16_t val) 
-{
-    if (pspi && pspi->driver_api && pspi->driver_api->hspi_master_transfer) {
-        return pspi->driver_api->hspi_master_transfer(pspi, val);
-    }
-    return -1;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/libs/os/include/os/os_dev.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/os_dev.h b/libs/os/include/os/os_dev.h
new file mode 100644
index 0000000..7c3dc09
--- /dev/null
+++ b/libs/os/include/os/os_dev.h
@@ -0,0 +1,84 @@
+/**
+ * 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 _OS_DEV_H
+#define _OS_DEV_H
+
+#include <os/os.h>
+
+#include "os/queue.h"
+
+struct os_dev;
+
+/*
+ * Initialization order, defines when a device should be initialized
+ * by the Mynewt kernel.
+ *
+ */
+#define OS_DEV_INIT_KERNEL    (1)
+
+#define OS_DEV_INIT_F_CRITICAL (1 << 0)
+
+
+#define OS_DEV_INIT_PRIO_DEFAULT (0xff)
+
+/**
+ * Device status, so functions can ensure device is called in a
+ * consistent state.
+ */
+#define OS_DEV_STATUS_BASE    (1 << 0)
+#define OS_DEV_STATUS_INITING (1 << 1)
+#define OS_DEV_STATUS_READY   (1 << 2)
+
+typedef int (*os_dev_init_func_t)(struct os_dev *);
+typedef int (*os_dev_open_func_t)(struct os_dev *);
+typedef int (*os_dev_close_func_t)(struct os_dev *);
+
+struct os_dev_handlers {
+    os_dev_open_func_t od_open;
+    os_dev_close_func_t od_close;
+};
+
+/*
+ * Device structure.
+ *
+ */
+struct os_dev {
+    struct os_dev_handlers od_handlers;
+    os_dev_init_func_t od_init;
+    uint8_t od_stage;
+    uint8_t od_priority;
+    uint8_t od_init_flags;
+    uint8_t od_status;
+    char *od_name;
+    STAILQ_ENTRY(os_dev) od_next;
+};
+
+#define OS_DEV_SETHANDLERS(__dev, __open, __close)          \
+    (__dev)->od_handlers.od_open = (__open);                \
+    (__dev)->od_handlers.od_close = (__close);
+
+int os_dev_create(struct os_dev *dev, char *name, uint8_t stage,
+        uint8_t priority, os_dev_init_func_t od_init);
+int os_dev_initialize_all(uint8_t stage);
+struct os_dev *os_dev_lookup(char *name);
+int os_dev_open(struct os_dev *dev);
+int os_dev_close(struct os_dev *dev);
+
+#endif /* _OS_DEV_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/libs/os/src/os.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os.c b/libs/os/src/os.c
index 90f972e..25d9d08 100644
--- a/libs/os/src/os.c
+++ b/libs/os/src/os.c
@@ -19,6 +19,7 @@
 
 #include "os/os.h"
 #include "os/queue.h"
+#include "os/os_dev.h"
 #include "os_priv.h"
 
 #include "hal/hal_os_tick.h"
@@ -122,6 +123,9 @@ os_start(void)
 {
     os_error_t err;
 
+    err = os_dev_initialize_all(OS_DEV_INIT_KERNEL);
+    assert(err == OS_OK);
+
     err = os_arch_os_start();
     assert(err == OS_OK);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93d29b84/libs/os/src/os_dev.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_dev.c b/libs/os/src/os_dev.c
new file mode 100644
index 0000000..b99b4db
--- /dev/null
+++ b/libs/os/src/os_dev.c
@@ -0,0 +1,177 @@
+/**
+ * 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 "os/os.h"
+#include "os/queue.h"
+#include "os/os_dev.h"
+
+#include <string.h>
+
+static STAILQ_HEAD(, os_dev) g_os_dev_list;
+
+static int
+os_dev_init(struct os_dev *dev, char *name, uint8_t stage,
+        uint8_t priority, os_dev_init_func_t od_init)
+{
+    dev->od_name = name;
+    dev->od_stage = stage;
+    dev->od_priority = priority;
+    /* assume these are set after the fact. */
+    dev->od_init_flags = 0;
+    dev->od_init = od_init;
+    memset(&dev->od_handlers, 0, sizeof(dev->od_handlers));
+
+    return (0);
+}
+
+/**
+ * Add this device to the kernel.  This function adds the
+ * device to the OS
+ */
+static int
+os_dev_add(struct os_dev *dev)
+{
+    struct os_dev *cur_dev;
+
+    /* If no devices present, insert into head */
+    if (STAILQ_FIRST(&g_os_dev_list) == NULL) {
+        STAILQ_INSERT_HEAD(&g_os_dev_list, dev, od_next);
+        return (0);
+    }
+
+    /* Add devices to the list, sorted first by stage, then by
+     * priority.
+     */
+    cur_dev = NULL;
+    STAILQ_FOREACH(cur_dev, &g_os_dev_list, od_next) {
+        if (cur_dev->od_stage > dev->od_stage) {
+            continue;
+        }
+
+        if (dev->od_priority >= cur_dev->od_priority) {
+            break;
+        }
+    }
+
+    if (cur_dev) {
+        STAILQ_INSERT_AFTER(&g_os_dev_list, cur_dev, dev, od_next);
+    } else {
+        STAILQ_INSERT_TAIL(&g_os_dev_list, dev, od_next);
+    }
+
+    return (0);
+}
+
+int
+os_dev_create(struct os_dev *dev, char *name, uint8_t stage,
+        uint8_t priority, os_dev_init_func_t od_init)
+{
+    int rc;
+
+    rc = os_dev_init(dev, name, stage, priority, od_init);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = os_dev_add(dev);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+
+int
+os_dev_initialize_all(uint8_t stage)
+{
+    struct os_dev *dev;
+    int rc;
+
+    STAILQ_FOREACH(dev, &g_os_dev_list, od_next) {
+        if (dev->od_stage == stage) {
+            rc = dev->od_init(dev);
+            if (dev->od_init_flags & OS_DEV_INIT_F_CRITICAL &&
+                    rc != 0) {
+                goto err;
+            }
+        }
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+struct os_dev *
+os_dev_lookup(char *name)
+{
+    struct os_dev *dev;
+
+    dev = NULL;
+    STAILQ_FOREACH(dev, &g_os_dev_list, od_next) {
+        if (!strcmp(dev->od_name, name)) {
+            break;
+        }
+    }
+    return (dev);
+}
+
+
+int
+os_dev_open(struct os_dev *dev)
+{
+    int rc;
+
+    /* Device is not ready to be opened. */
+    if ((dev->od_status & OS_DEV_STATUS_READY) == 0) {
+        return (OS_EINVAL);
+    }
+
+    if (dev->od_handlers.od_open) {
+        rc = dev->od_handlers.od_open(dev);
+        if (rc != 0) {
+            goto err;
+        }
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+int
+os_dev_close(struct os_dev *dev)
+{
+    int rc;
+
+    if (dev->od_handlers.od_close) {
+        rc = dev->od_handlers.od_close(dev);
+        if (rc != 0) {
+            goto err;
+        }
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+


[12/16] incubator-mynewt-core git commit: os, cortex-M; keep using MSP as stack pointer until os_start(). This allows us to take interrupts during system init.

Posted by st...@apache.org.
os, cortex-M; keep using MSP as stack pointer until os_start().
This allows us to take interrupts during system init.


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

Branch: refs/heads/sterly_refactor
Commit: 74793416a91452a5361103a95b06b2c81ffa12fc
Parents: 5092dc3
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Jul 29 13:32:48 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 libs/os/include/os/arch/cortex_m0/os/os_arch.h | 12 +++----
 libs/os/include/os/arch/cortex_m4/os/os_arch.h | 12 +++----
 libs/os/src/arch/cortex_m0/m0/HAL_CM0.s        |  1 -
 libs/os/src/arch/cortex_m0/os_arch_arm.c       | 34 ++++++++++---------
 libs/os/src/arch/cortex_m4/m4/HAL_CM4.s        |  2 +-
 libs/os/src/arch/cortex_m4/os_arch_arm.c       | 36 ++++++++++++---------
 libs/os/src/os_priv.h                          |  3 +-
 7 files changed, 55 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74793416/libs/os/include/os/arch/cortex_m0/os/os_arch.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/arch/cortex_m0/os/os_arch.h b/libs/os/include/os/arch/cortex_m0/os/os_arch.h
index e986702..1a83ba9 100755
--- a/libs/os/include/os/arch/cortex_m0/os/os_arch.h
+++ b/libs/os/include/os/arch/cortex_m0/os/os_arch.h
@@ -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,
@@ -17,8 +17,8 @@
  * under the License.
  */
 
-#ifndef _OS_ARCH_ARM_H 
-#define _OS_ARCH_ARM_H 
+#ifndef _OS_ARCH_ARM_H
+#define _OS_ARCH_ARM_H
 
 #include <stdint.h>
 #include <mcu/cortex_m0.h>
@@ -48,7 +48,7 @@ typedef uint32_t os_stack_t;
     (OS_ALIGN((__nmemb), OS_STACK_ALIGNMENT))
 
 /* Enter a critical section, save processor state, and block interrupts */
-#define OS_ENTER_CRITICAL(__os_sr) (__os_sr = os_arch_save_sr()) 
+#define OS_ENTER_CRITICAL(__os_sr) (__os_sr = os_arch_save_sr())
 /* Exit a critical section, restore processor state and unblock interrupts */
 #define OS_EXIT_CRITICAL(__os_sr) (os_arch_restore_sr(__os_sr))
 #define OS_ASSERT_CRITICAL() (assert(os_arch_in_critical()))
@@ -63,7 +63,7 @@ void os_arch_init(void);
 uint32_t os_arch_start(void);
 os_error_t os_arch_os_init(void);
 os_error_t os_arch_os_start(void);
-void os_set_env(void);
+void os_set_env(os_stack_t *);
 void os_arch_init_task_stack(os_stack_t *sf);
 void os_default_irq_asm(void);
 
@@ -71,4 +71,4 @@ void os_default_irq_asm(void);
 void os_bsp_systick_init(uint32_t os_ticks_per_sec, int prio);
 void os_bsp_ctx_sw(void);
 
-#endif /* _OS_ARCH_X86_H */ 
+#endif /* _OS_ARCH_ARM_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74793416/libs/os/include/os/arch/cortex_m4/os/os_arch.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/arch/cortex_m4/os/os_arch.h b/libs/os/include/os/arch/cortex_m4/os/os_arch.h
index 8c67f09..9e43a7a 100755
--- a/libs/os/include/os/arch/cortex_m4/os/os_arch.h
+++ b/libs/os/include/os/arch/cortex_m4/os/os_arch.h
@@ -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,
@@ -17,8 +17,8 @@
  * under the License.
  */
 
-#ifndef _OS_ARCH_ARM_H 
-#define _OS_ARCH_ARM_H 
+#ifndef _OS_ARCH_ARM_H
+#define _OS_ARCH_ARM_H
 
 #include <stdint.h>
 #include "mcu/cortex_m4.h"
@@ -48,7 +48,7 @@ typedef uint32_t os_stack_t;
     (OS_ALIGN((__nmemb), OS_STACK_ALIGNMENT))
 
 /* Enter a critical section, save processor state, and block interrupts */
-#define OS_ENTER_CRITICAL(__os_sr) (__os_sr = os_arch_save_sr()) 
+#define OS_ENTER_CRITICAL(__os_sr) (__os_sr = os_arch_save_sr())
 /* Exit a critical section, restore processor state and unblock interrupts */
 #define OS_EXIT_CRITICAL(__os_sr) (os_arch_restore_sr(__os_sr))
 #define OS_ASSERT_CRITICAL() (assert(os_arch_in_critical()))
@@ -63,7 +63,7 @@ void os_arch_init(void);
 uint32_t os_arch_start(void);
 os_error_t os_arch_os_init(void);
 os_error_t os_arch_os_start(void);
-void os_set_env(void);
+void os_set_env(os_stack_t *);
 void os_arch_init_task_stack(os_stack_t *sf);
 void os_default_irq_asm(void);
 
@@ -72,4 +72,4 @@ void os_bsp_systick_init(uint32_t os_tick_per_sec, int prio);
 void os_bsp_idle(os_time_t ticks);
 void os_bsp_ctx_sw(void);
 
-#endif /* _OS_ARCH_X86_H */ 
+#endif /* _OS_ARCH_ARM_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74793416/libs/os/src/arch/cortex_m0/m0/HAL_CM0.s
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m0/m0/HAL_CM0.s b/libs/os/src/arch/cortex_m0/m0/HAL_CM0.s
index 03d7957..de7d46c 100644
--- a/libs/os/src/arch/cortex_m0/m0/HAL_CM0.s
+++ b/libs/os/src/arch/cortex_m0/m0/HAL_CM0.s
@@ -52,7 +52,6 @@ os_set_env:
         .fnstart
         .cantunwind
 
-        MOV     R0,SP           /* Copy MSP to PSP */
         MSR     PSP,R0
         LDR     R0,=os_flags
         LDRB    R0,[R0]

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74793416/libs/os/src/arch/cortex_m0/os_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m0/os_arch_arm.c b/libs/os/src/arch/cortex_m0/os_arch_arm.c
index 40b9254..ef1be4d 100755
--- a/libs/os/src/arch/cortex_m0/os_arch_arm.c
+++ b/libs/os/src/arch/cortex_m0/os_arch_arm.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,
@@ -23,6 +23,8 @@
 #include <hal/hal_os_tick.h>
 #include <bsp/cmsis_nvic.h>
 
+#include "os_priv.h"
+
 /*
  * From HAL_CM0.s
  */
@@ -223,13 +225,6 @@ os_arch_os_init(void)
         /* Set the SVC interrupt to priority 0 (highest configurable) */
         NVIC_SetPriority(SVCall_IRQn, SVC_PRIO);
 
-        /*
-         * Set the os environment. This will set stack pointers and, based
-         * on the contents of os_flags, will determine if the tasks run in
-         * priviliged or un-privileged mode.
-         */
-        os_set_env();
-
         /* Check if privileged or not */
         if ((__get_CONTROL() & 1) == 0) {
             os_arch_init();
@@ -273,17 +268,26 @@ static inline void svc_os_arch_start(void)
 }
 
 /**
- * Start the OS. First check to see if we are running with the correct stack 
- * pointer set (PSP) and privilege mode (PRIV). 
- * 
- * 
- * @return os_error_t 
+ * Start the OS. First check to see if we are running with the correct stack
+ * pointer set (PSP) and privilege mode (PRIV).
+ *
+ * @return os_error_t
  */
 os_error_t
 os_arch_os_start(void)
 {
     os_error_t err;
 
+    /*
+     * Set the os environment. This will set stack pointers and, based
+     * on the contents of os_flags, will determine if the tasks run in
+     * priviliged or un-privileged mode.
+     *
+     * We switch to using "empty" part of idle task's stack until
+     * the svc_os_arch_start() executes SVC, and we will never return.
+     */
+    os_set_env(g_idle_task.t_stackptr - 1);
+
     err = OS_ERR_IN_ISR;
     if (__get_IPSR() == 0) {
         /*
@@ -304,7 +308,7 @@ os_arch_os_start(void)
             err = OS_ERR_PRIV;
             break;
         case 0x02:
-            /* 
+            /*
              * We are running in Privileged Thread mode w/SP = PSP but we
              * are supposed to be un-privileged.
              */
@@ -313,7 +317,7 @@ os_arch_os_start(void)
             }
             break;
         case 0x03:
-            /* 
+            /*
              * We are running in Unprivileged Thread mode w/SP = PSP but we
              * are supposed to be privileged.
              */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74793416/libs/os/src/arch/cortex_m4/m4/HAL_CM4.s
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m4/m4/HAL_CM4.s b/libs/os/src/arch/cortex_m4/m4/HAL_CM4.s
index c9ae103..9c6ab52 100755
--- a/libs/os/src/arch/cortex_m4/m4/HAL_CM4.s
+++ b/libs/os/src/arch/cortex_m4/m4/HAL_CM4.s
@@ -52,7 +52,6 @@ os_set_env:
         .fnstart
         .cantunwind
 
-        MOV     R0,SP           /* Copy MSP to PSP */
         MSR     PSP,R0
         LDR     R0,=os_flags
         LDRB    R0,[R0]
@@ -63,6 +62,7 @@ os_set_env:
 
         .fnend
         .size   os_set_env, .-os_set_env
+
 /*--------------------------- os_set_env ------------------------------------*/
 
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74793416/libs/os/src/arch/cortex_m4/os_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m4/os_arch_arm.c b/libs/os/src/arch/cortex_m4/os_arch_arm.c
index 2bd084a..184170f 100755
--- a/libs/os/src/arch/cortex_m4/os_arch_arm.c
+++ b/libs/os/src/arch/cortex_m4/os_arch_arm.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,
@@ -23,6 +23,8 @@
 #include <hal/hal_os_tick.h>
 #include <bsp/cmsis_nvic.h>
 
+#include "os_priv.h"
+
 /* Initial program status register */
 #define INITIAL_xPSR    0x01000000
 
@@ -217,13 +219,6 @@ os_arch_os_init(void)
         /* Set the SVC interrupt to priority 0 (highest configurable) */
         NVIC_SetPriority(SVCall_IRQn, SVC_PRIO);
 
-        /*
-         * Set the os environment. This will set stack pointers and, based
-         * on the contents of os_flags, will determine if the tasks run in
-         * priviliged or un-privileged mode.
-         */
-        os_set_env();
-
         /* Check if privileged or not */
         if ((__get_CONTROL() & 1) == 0) {
             os_arch_init();
@@ -267,17 +262,28 @@ static inline void svc_os_arch_start(void)
 }
 
 /**
- * Start the OS. First check to see if we are running with the correct stack 
- * pointer set (PSP) and privilege mode (PRIV). 
- * 
- * 
- * @return os_error_t 
+ * Start the OS. First check to see if we are running with the correct stack
+ * pointer set (PSP) and privilege mode (PRIV).
+ *
+ *
+ * @return os_error_t
  */
 os_error_t
 os_arch_os_start(void)
 {
     os_error_t err;
 
+    /*
+     * Set the os environment. This will set stack pointers and, based
+     * on the contents of os_flags, will determine if the tasks run in
+     * privileged or un-privileged mode.
+     *
+     * We switch to using "empty" part of idle task's stack until
+     * the svc_os_arch_start() executes SVC, and we will never return.
+     */
+     */
+    os_set_env(g_idle_task.t_stackptr - 1);
+
     err = OS_ERR_IN_ISR;
     if (__get_IPSR() == 0) {
         /*
@@ -298,7 +304,7 @@ os_arch_os_start(void)
             err = OS_ERR_PRIV;
             break;
         case 0x02:
-            /* 
+            /*
              * We are running in Privileged Thread mode w/SP = PSP but we
              * are supposed to be un-privileged.
              */
@@ -307,7 +313,7 @@ os_arch_os_start(void)
             }
             break;
         case 0x03:
-            /* 
+            /*
              * We are running in Unprivileged Thread mode w/SP = PSP but we
              * are supposed to be privileged.
              */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74793416/libs/os/src/os_priv.h
----------------------------------------------------------------------
diff --git a/libs/os/src/os_priv.h b/libs/os/src/os_priv.h
index ddc656b..b5c8f65 100644
--- a/libs/os/src/os_priv.h
+++ b/libs/os/src/os_priv.h
@@ -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,
@@ -26,6 +26,7 @@ TAILQ_HEAD(os_task_list, os_task);
 TAILQ_HEAD(os_callout_list, os_callout);
 STAILQ_HEAD(os_task_stailq, os_task);
 
+extern struct os_task g_idle_task;
 extern struct os_task_list g_os_run_list;
 extern struct os_task_list g_os_sleep_list;
 extern struct os_task_stailq g_os_task_list;


[07/16] incubator-mynewt-core git commit: ble apps - Update for latest NimBLE host API.

Posted by st...@apache.org.
ble apps - Update for latest NimBLE host API.


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

Branch: refs/heads/sterly_refactor
Commit: ee4eaac5a936abb7db6927b1eb75dde9b75cb11d
Parents: 39bc059
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jul 27 17:41:50 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 apps/blehci/src/main.c     | 33 +++++++++++++--------------------
 apps/bletiny/src/bletiny.h |  6 +++---
 apps/bletiny/src/cmd.c     |  9 ++++++---
 apps/bletiny/src/main.c    |  6 +++---
 4 files changed, 25 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee4eaac5/apps/blehci/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blehci/src/main.c b/apps/blehci/src/main.c
index 3ec6bdb..09fb456 100755
--- a/apps/blehci/src/main.c
+++ b/apps/blehci/src/main.c
@@ -109,35 +109,28 @@ static struct {
 } hci;
 
 int
-ble_hs_rx_data(struct os_mbuf **om)
+ble_hs_rx_data(struct os_mbuf *om)
 {
     struct os_event *ev;
     os_sr_t sr;
-    int rc;
 
     ev = os_memblock_get(&g_hci_os_event_pool);
-    if (ev != NULL) {
-        ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_DATA;
-        ev->ev_arg = *om;
-        ev->ev_queued = 1;
-
-        *om = NULL;
-
-        OS_ENTER_CRITICAL(sr);
-        STAILQ_INSERT_TAIL(&hci.rx_pkts, ev, ev_next);
-        OS_EXIT_CRITICAL(sr);
+    if (!ev) {
+        os_mbuf_free_chain(om);
+        return -1;
+    }
 
-        hal_uart_start_tx(HCI_UART);
+    ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_DATA;
+    ev->ev_arg = om;
+    ev->ev_queued = 1;
 
-        rc = 0;
-    } else {
-        rc = -1;
-    }
+    OS_ENTER_CRITICAL(sr);
+    STAILQ_INSERT_TAIL(&hci.rx_pkts, ev, ev_next);
+    OS_EXIT_CRITICAL(sr);
 
-    /* Free the mbuf if we weren't able to enqueue it. */
-    os_mbuf_free_chain(*om);
+    hal_uart_start_tx(HCI_UART);
 
-    return rc;
+    return 0;
 }
 
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee4eaac5/apps/bletiny/src/bletiny.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny.h b/apps/bletiny/src/bletiny.h
index c744edb..e24f0f0 100644
--- a/apps/bletiny/src/bletiny.h
+++ b/apps/bletiny/src/bletiny.h
@@ -140,11 +140,11 @@ int bletiny_read_by_uuid(uint16_t conn_handle, uint16_t start_handle,
 int bletiny_read_mult(uint16_t conn_handle, uint16_t *attr_handles,
                        int num_attr_handles);
 int bletiny_write(uint16_t conn_handle, uint16_t attr_handle,
-                  struct os_mbuf **om);
+                  struct os_mbuf *om);
 int bletiny_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
-                         struct os_mbuf **om);
+                         struct os_mbuf *om);
 int bletiny_write_long(uint16_t conn_handle, uint16_t attr_handle,
-                       struct os_mbuf **om);
+                       struct os_mbuf *om);
 int bletiny_write_reliable(uint16_t conn_handle,
                            struct ble_gatt_attr *attrs, int num_attrs);
 int bletiny_adv_start(uint8_t own_addr_type, uint8_t peer_addr_type,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee4eaac5/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 7671253..1d133da 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1892,17 +1892,20 @@ cmd_write(int argc, char **argv)
             rc = -EINVAL;
             goto done;
         }
-        rc = bletiny_write_no_rsp(conn_handle, attrs[0].handle, &attrs[0].om);
+        rc = bletiny_write_no_rsp(conn_handle, attrs[0].handle, attrs[0].om);
+        attrs[0].om = NULL;
     } else if (is_long) {
         if (num_attrs != 1) {
             rc = -EINVAL;
             goto done;
         }
-        rc = bletiny_write_long(conn_handle, attrs[0].handle, &attrs[0].om);
+        rc = bletiny_write_long(conn_handle, attrs[0].handle, attrs[0].om);
+        attrs[0].om = NULL;
     } else if (num_attrs > 1) {
         rc = bletiny_write_reliable(conn_handle, attrs, num_attrs);
     } else if (num_attrs == 1) {
-        rc = bletiny_write(conn_handle, attrs[0].handle, &attrs[0].om);
+        rc = bletiny_write(conn_handle, attrs[0].handle, attrs[0].om);
+        attrs[0].om = NULL;
     } else {
         rc = -EINVAL;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee4eaac5/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 50084c1..f5c57ba 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1250,7 +1250,7 @@ bletiny_read_mult(uint16_t conn_handle, uint16_t *attr_handles,
 }
 
 int
-bletiny_write(uint16_t conn_handle, uint16_t attr_handle, struct os_mbuf **om)
+bletiny_write(uint16_t conn_handle, uint16_t attr_handle, struct os_mbuf *om)
 {
     int rc;
 
@@ -1266,7 +1266,7 @@ bletiny_write(uint16_t conn_handle, uint16_t attr_handle, struct os_mbuf **om)
 
 int
 bletiny_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
-                     struct os_mbuf **om)
+                     struct os_mbuf *om)
 {
     int rc;
 
@@ -1277,7 +1277,7 @@ bletiny_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
 
 int
 bletiny_write_long(uint16_t conn_handle, uint16_t attr_handle,
-                   struct os_mbuf **om)
+                   struct os_mbuf *om)
 {
     int rc;
 


[09/16] incubator-mynewt-core git commit: shell; call console_init() from shell task context instead of app main.

Posted by st...@apache.org.
shell; call console_init() from shell task context instead of app main.


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

Branch: refs/heads/sterly_refactor
Commit: cf9024968060866a3ca11df54a000678659a9d73
Parents: 3da7f49
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Jul 28 11:16:12 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 apps/bletest/src/main.c | 4 ----
 apps/bletiny/src/main.c | 4 ----
 apps/blinky/src/main.c  | 2 --
 apps/luatest/src/main.c | 1 -
 apps/slinky/src/main.c  | 6 ++----
 libs/shell/src/shell.c  | 1 +
 6 files changed, 3 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf902496/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index 6c59532..dbfa053 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -1105,10 +1105,6 @@ main(void)
     g_led_pin = LED_BLINK_PIN;
     hal_gpio_init_out(g_led_pin, 1);
 
-    /* Init the console */
-    rc = console_init(shell_console_rx_cb);
-    assert(rc == 0);
-
 #if 0
     rc = hal_flash_init();
     assert(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf902496/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index f5c57ba..733f8d1 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1661,10 +1661,6 @@ main(void)
     rc = os_msys_register(&default_mbuf_pool);
     assert(rc == 0);
 
-    /* Initialize the console (for shell and logging). */
-    rc = console_init(shell_console_rx_cb);
-    assert(rc == 0);
-
     /* Create the shell task. */
     rc = shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
                          SHELL_MAX_INPUT_LEN);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf902496/apps/blinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blinky/src/main.c b/apps/blinky/src/main.c
index fb9bb27..fa0bc89 100755
--- a/apps/blinky/src/main.c
+++ b/apps/blinky/src/main.c
@@ -222,8 +222,6 @@ main(int argc, char **argv)
     shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
                     SHELL_MAX_INPUT_LEN);
 
-    (void) console_init(shell_console_rx_cb);
-
     stats_module_init();
 
 #if 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf902496/apps/luatest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/luatest/src/main.c b/apps/luatest/src/main.c
index 76dd13f..9cd3d5a 100755
--- a/apps/luatest/src/main.c
+++ b/apps/luatest/src/main.c
@@ -88,7 +88,6 @@ main(int argc, char **argv)
     /* Init tasks */
     shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
                          SHELL_MAX_INPUT_LEN);
-    console_init(shell_console_rx_cb);
 
     nffs_init();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf902496/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c
index f57657f..d9dd29a 100755
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@ -58,7 +58,7 @@ volatile int tasks_initialized;
 int init_tasks(void);
 
 /* Task 1 */
-#define TASK1_PRIO (1)
+#define TASK1_PRIO (8)
 #define TASK1_STACK_SIZE    OS_STACK_ALIGN(128)
 #define MAX_CBMEM_BUF 600
 struct os_task task1;
@@ -66,7 +66,7 @@ os_stack_t stack1[TASK1_STACK_SIZE];
 static volatile int g_task1_loops;
 
 /* Task 2 */
-#define TASK2_PRIO (2)
+#define TASK2_PRIO (9)
 #define TASK2_STACK_SIZE    OS_STACK_ALIGN(128)
 struct os_task task2;
 os_stack_t stack2[TASK2_STACK_SIZE];
@@ -395,8 +395,6 @@ main(int argc, char **argv)
     shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
                     SHELL_MAX_INPUT_LEN);
 
-    (void) console_init(shell_console_rx_cb);
-
     nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
     imgmgr_module_init();
     bootutil_cfg_register();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf902496/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 95ddcb0..4b95a2b 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -454,6 +454,7 @@ shell_task_func(void *arg)
     struct os_event *ev;
 
     console_rdy_ev.ev_type = OS_EVENT_T_CONSOLE_RDY;
+    console_init(shell_console_rx_cb);
 
     while (1) {
         ev = os_eventq_get(&shell_evq);


[10/16] incubator-mynewt-core git commit: console; don't crash if there's console output when console has not been initialized.

Posted by st...@apache.org.
console; don't crash if there's console output when console has
not been initialized.


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

Branch: refs/heads/sterly_refactor
Commit: c092a8a9545fd80e84fd72f7d895d7e6446458c7
Parents: cf90249
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Jul 28 11:16:37 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 libs/console/full/src/cons_tty.c | 3 +++
 1 file changed, 3 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c092a8a9/libs/console/full/src/cons_tty.c
----------------------------------------------------------------------
diff --git a/libs/console/full/src/cons_tty.c b/libs/console/full/src/cons_tty.c
index 7dfd0b7..fb2015c 100644
--- a/libs/console/full/src/cons_tty.c
+++ b/libs/console/full/src/cons_tty.c
@@ -165,6 +165,9 @@ console_file_write(void *arg, const char *str, size_t cnt)
     struct console_tty *ct = &console_tty;
     int i;
 
+    if (!ct->ct_write_char) {
+        return cnt;
+    }
     for (i = 0; i < cnt; i++) {
         if (str[i] == '\n') {
             ct->ct_write_char('\r');


[03/16] incubator-mynewt-core git commit: Fix memory overwrite in random number generation code. This overwrite could cause privacy to fail

Posted by st...@apache.org.
Fix memory overwrite in random number generation code. This overwrite could cause privacy to 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/5092dc34
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/5092dc34
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/5092dc34

Branch: refs/heads/sterly_refactor
Commit: 5092dc34b9c6729fc9f7284aace84fa38f66a8b2
Parents: 37f8bb5
Author: William San Filippo <wi...@runtime.io>
Authored: Fri Jul 29 08:33:30 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 net/nimble/controller/src/ble_ll_rand.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5092dc34/net/nimble/controller/src/ble_ll_rand.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_rand.c b/net/nimble/controller/src/ble_ll_rand.c
index c5201f9..8d26f8d 100644
--- a/net/nimble/controller/src/ble_ll_rand.c
+++ b/net/nimble/controller/src/ble_ll_rand.c
@@ -37,7 +37,8 @@ struct ble_ll_rnum_data
 struct ble_ll_rnum_data g_ble_ll_rnum_data;
 uint8_t g_ble_ll_rnum_buf[NIMBLE_OPT_LL_RNG_BUFSIZE];
 
-#define IS_RNUM_BUF_END(x)  (x == &g_ble_ll_rnum_buf[NIMBLE_OPT_LL_RNG_BUFSIZE])
+#define IS_RNUM_BUF_END(x)  \
+    (x == &g_ble_ll_rnum_buf[NIMBLE_OPT_LL_RNG_BUFSIZE - 1])
 
 void
 ble_ll_rand_sample(uint8_t rnum)


[16/16] incubator-mynewt-core git commit: os; fix botched commit.

Posted by st...@apache.org.
os; fix botched commit.


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

Branch: refs/heads/sterly_refactor
Commit: 5a7c0a12d081e914cafcd8f93cdc0fdfed43c01b
Parents: 4eaca97
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Jul 29 13:46:17 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 libs/os/src/arch/cortex_m4/os_arch_arm.c | 1 -
 1 file changed, 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5a7c0a12/libs/os/src/arch/cortex_m4/os_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m4/os_arch_arm.c b/libs/os/src/arch/cortex_m4/os_arch_arm.c
index 184170f..48f6976 100755
--- a/libs/os/src/arch/cortex_m4/os_arch_arm.c
+++ b/libs/os/src/arch/cortex_m4/os_arch_arm.c
@@ -281,7 +281,6 @@ os_arch_os_start(void)
      * We switch to using "empty" part of idle task's stack until
      * the svc_os_arch_start() executes SVC, and we will never return.
      */
-     */
     os_set_env(g_idle_task.t_stackptr - 1);
 
     err = OS_ERR_IN_ISR;


[14/16] incubator-mynewt-core git commit: shell; initialize console at shell_task_init().

Posted by st...@apache.org.
shell; initialize console at shell_task_init().


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

Branch: refs/heads/sterly_refactor
Commit: 4eaca9712004959e820bfc3f9e4f68dc0f344edc
Parents: 7479341
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Jul 29 13:34:09 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 libs/shell/src/shell.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4eaca971/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 4b95a2b..d38573c 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -454,7 +454,6 @@ shell_task_func(void *arg)
     struct os_event *ev;
 
     console_rdy_ev.ev_type = OS_EVENT_T_CONSOLE_RDY;
-    console_init(shell_console_rx_cb);
 
     while (1) {
         ev = os_eventq_get(&shell_evq);
@@ -573,6 +572,8 @@ shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size,
     os_eventq_init(&shell_evq);
     os_mqueue_init(&g_shell_nlip_mq, NULL);
 
+    console_init(shell_console_rx_cb);
+
     rc = os_task_init(&shell_task, "shell", shell_task_func,
             NULL, prio, OS_WAIT_FOREVER, stack, stack_size);
     if (rc != 0) {


[06/16] incubator-mynewt-core git commit: BLE Host - Remove double indirection from tx API.

Posted by st...@apache.org.
BLE Host - Remove double indirection from tx API.

Prior to this change, data buffers for tx operations were passed as type
(struct os_mbuf **).  The second pointer was used so that the host could
set the caller's pointer to NULL, indicating that the mbuf had been
consumed and preventing a double-free.

This was useful, but confusing.  Using a single pointer is simpler and
consistent with the other networking libraries in core.  It is up to the
caller to read the function spec and understand that ownership of the
mbuf is transferred.


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

Branch: refs/heads/sterly_refactor
Commit: 40695b35d2711895bd950076f45c8e619d250c6f
Parents: 2fcf6aa
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jul 27 17:30:34 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_att.h      |   2 +-
 net/nimble/host/include/host/ble_gatt.h     |   8 +-
 net/nimble/host/include/host/ble_hs.h       |   2 +-
 net/nimble/host/include/host/host_hci.h     |   2 +-
 net/nimble/host/src/ble_att_clt.c           | 171 +++++++++++------------
 net/nimble/host/src/ble_att_priv.h          |  14 +-
 net/nimble/host/src/ble_att_svr.c           |  90 ++++++------
 net/nimble/host/src/ble_gatt_priv.h         |   8 +-
 net/nimble/host/src/ble_gattc.c             |  78 +++++------
 net/nimble/host/src/ble_hs.c                |  13 +-
 net/nimble/host/src/ble_l2cap.c             |  24 +---
 net/nimble/host/src/ble_l2cap_priv.h        |   5 +-
 net/nimble/host/src/ble_l2cap_sig_cmd.c     |   6 +-
 net/nimble/host/src/ble_sm_cmd.c            | 151 ++++++++------------
 net/nimble/host/src/ble_sm_priv.h           |   5 +-
 net/nimble/host/src/host_hci.c              |  30 ++--
 net/nimble/host/src/test/ble_att_clt_test.c |  11 +-
 net/nimble/host/src/test/ble_hs_test_util.c |  13 +-
 18 files changed, 276 insertions(+), 357 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/include/host/ble_att.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_att.h b/net/nimble/host/include/host/ble_att.h
index e5eb820..6afca97 100644
--- a/net/nimble/host/include/host/ble_att.h
+++ b/net/nimble/host/include/host/ble_att.h
@@ -95,7 +95,7 @@ struct os_mbuf;
 #define BLE_ATT_MTU_PREFERRED_DFLT      240
 
 int ble_att_svr_read_local(uint16_t attr_handle, struct os_mbuf **out_om);
-int ble_att_svr_write_local(uint16_t attr_handle, struct os_mbuf **om);
+int ble_att_svr_write_local(uint16_t attr_handle, struct os_mbuf *om);
 
 uint16_t ble_att_mtu(uint16_t conn_handle);
 int ble_att_set_preferred_mtu(uint16_t mtu);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index 8aab982..fbc32a2 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -164,24 +164,24 @@ int ble_gattc_read_mult(uint16_t conn_handle, const uint16_t *handles,
                         uint8_t num_handles, ble_gatt_attr_fn *cb,
                         void *cb_arg);
 int ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
-                           struct os_mbuf **om);
+                           struct os_mbuf *om);
 int ble_gattc_write_no_rsp_flat(uint16_t conn_handle, uint16_t attr_handle,
                                 const void *data, uint16_t data_len);
 int ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle,
-                    struct os_mbuf **om,
+                    struct os_mbuf *om,
                     ble_gatt_attr_fn *cb, void *cb_arg);
 int ble_gattc_write_flat(uint16_t conn_handle, uint16_t attr_handle,
                          const void *data, uint16_t data_len,
                          ble_gatt_attr_fn *cb, void *cb_arg);
 int ble_gattc_write_long(uint16_t conn_handle, uint16_t attr_handle,
-                         struct os_mbuf **om,
+                         struct os_mbuf *om,
                          ble_gatt_attr_fn *cb, void *cb_arg);
 int ble_gattc_write_reliable(uint16_t conn_handle,
                              struct ble_gatt_attr *attrs,
                              int num_attrs, ble_gatt_reliable_attr_fn *cb,
                              void *cb_arg);
 int ble_gattc_notify_custom(uint16_t conn_handle, uint16_t att_handle,
-                            struct os_mbuf **om);
+                            struct os_mbuf *om);
 int ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle);
 int ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/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 56fbc2b..5cdf863 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -230,7 +230,7 @@ struct ble_hs_cfg {
 
 extern const struct ble_hs_cfg ble_hs_cfg_dflt;
 
-int ble_hs_rx_data(struct os_mbuf **om);
+int ble_hs_rx_data(struct os_mbuf *om);
 int ble_hs_start(void);
 void ble_hs_event_enqueue(struct os_event *ev);
 int ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/include/host/host_hci.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/host_hci.h b/net/nimble/host/include/host/host_hci.h
index bcec2ef..f92c1a9 100644
--- a/net/nimble/host/include/host/host_hci.h
+++ b/net/nimble/host/include/host/host_hci.h
@@ -104,7 +104,7 @@ int host_hci_set_buf_size(uint16_t pktlen, uint8_t max_pkts);
 uint16_t host_hci_handle_pb_bc_join(uint16_t handle, uint8_t pb, uint8_t bc);
 
 int host_hci_data_rx(struct os_mbuf *om);
-int host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf **txom);
+int host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf *txom);
 
 int host_hci_cmd_build_set_data_len(uint16_t connection_handle,
                                     uint16_t tx_octets, uint16_t tx_time,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/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 5687a99..87e2f44 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -59,31 +59,29 @@ err:
 }
 
 static int
-ble_att_clt_tx_req(uint16_t conn_handle, struct os_mbuf **txom)
+ble_att_clt_tx_req(uint16_t conn_handle, struct os_mbuf *txom)
 {
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
-    struct os_mbuf *om;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
-    BLE_HS_DBG_ASSERT_EVAL(om->om_len >= 1);
-    ble_att_inc_tx_stat(om->om_data[0]);
+    BLE_HS_DBG_ASSERT_EVAL(txom->om_len >= 1);
+    ble_att_inc_tx_stat(txom->om_data[0]);
 
     ble_hs_lock();
 
     rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
     if (rc == 0) {
-        ble_att_truncate_to_mtu(chan, om);
-        rc = ble_l2cap_tx(conn, chan, &om);
+        ble_att_truncate_to_mtu(chan, txom);
+        rc = ble_l2cap_tx(conn, chan, txom);
     }
 
     ble_hs_unlock();
 
-    os_mbuf_free_chain(om);
+    if (rc != 0) {
+        os_mbuf_free_chain(txom);
+    }
+
     return rc;
 }
 
@@ -134,7 +132,7 @@ ble_att_clt_tx_mtu(uint16_t conn_handle, const struct ble_att_mtu_cmd *req)
     }
     ble_att_mtu_req_write(txom->om_data, txom->om_len, req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
     if (rc != 0) {
         return rc;
     }
@@ -211,7 +209,7 @@ ble_att_clt_tx_find_info(uint16_t conn_handle,
     }
     ble_att_find_info_req_write(txom->om_data, txom->om_len, req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
     if (rc != 0) {
         return rc;
     }
@@ -338,27 +336,30 @@ ble_att_clt_tx_find_type_value(uint16_t conn_handle,
         req->bavq_start_handle > req->bavq_end_handle) {
 
         rc = BLE_HS_EINVAL;
-        goto done;
+        goto err;
     }
 
     rc = ble_att_clt_init_req(BLE_ATT_FIND_TYPE_VALUE_REQ_BASE_SZ, &txom);
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
     ble_att_find_type_value_req_write(txom->om_data, txom->om_len, req);
     rc = os_mbuf_append(txom, attribute_value, value_len);
     if (rc != 0) {
         rc = BLE_HS_ENOMEM;
-        goto done;
+        goto err;
     }
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
+    txom = NULL;
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
-done:
+    return 0;
+
+err:
     os_mbuf_free_chain(txom);
     return rc;
 }
@@ -441,27 +442,30 @@ ble_att_clt_tx_read_type(uint16_t conn_handle,
         req->batq_start_handle > req->batq_end_handle) {
 
         rc = BLE_HS_EINVAL;
-        goto done;
+        goto err;
     }
 
     rc = ble_att_clt_init_req(BLE_ATT_READ_TYPE_REQ_BASE_SZ, &txom);
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
     ble_att_read_type_req_write(txom->om_data, txom->om_len, req);
     rc = ble_uuid_append(txom, uuid128);
     if (rc != 0) {
         rc = BLE_HS_ENOMEM;
-        goto done;
+        goto err;
     }
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
+    txom = NULL;
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
-done:
+    return 0;
+
+err:
     os_mbuf_free_chain(txom);
     return 0;
 }
@@ -551,7 +555,7 @@ ble_att_clt_tx_read(uint16_t conn_handle, const struct ble_att_read_req *req)
     }
     ble_att_read_req_write(txom->om_data, txom->om_len, req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
     if (rc != 0) {
         return rc;
     }
@@ -606,7 +610,7 @@ ble_att_clt_tx_read_blob(uint16_t conn_handle,
     }
     ble_att_read_blob_req_write(txom->om_data, txom->om_len, req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
     if (rc != 0) {
         return rc;
     }
@@ -695,7 +699,7 @@ ble_att_clt_tx_read_mult(uint16_t conn_handle, const uint16_t *att_handles,
         return rc;
     }
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
     if (rc != 0) {
         return rc;
     }
@@ -747,26 +751,29 @@ ble_att_clt_tx_read_group_type(uint16_t conn_handle,
         req->bagq_start_handle > req->bagq_end_handle) {
 
         rc = BLE_HS_EINVAL;
-        goto done;
+        goto err;
     }
 
     rc = ble_att_clt_init_req(BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ, &txom);
     if (rc != 0) {
-        goto done;
+        goto err;
     }
     ble_att_read_group_type_req_write(txom->om_data, txom->om_len, req);
 
     rc = ble_uuid_append(txom, uuid128);
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
+    txom = NULL;
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
-done:
+    return 0;
+
+err:
     os_mbuf_free_chain(txom);
     return rc;
 }
@@ -843,27 +850,22 @@ done:
 static int
 ble_att_clt_tx_write_req_or_cmd(uint16_t conn_handle,
                                 const struct ble_att_write_req *req,
-                                struct os_mbuf **txom, int is_req)
+                                struct os_mbuf *txom, int is_req)
 {
-    struct os_mbuf *om;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
-    om = os_mbuf_prepend_pullup(om, BLE_ATT_WRITE_REQ_BASE_SZ);
-    if (om == NULL) {
+    txom = os_mbuf_prepend_pullup(txom, BLE_ATT_WRITE_REQ_BASE_SZ);
+    if (txom == NULL) {
         return BLE_HS_ENOMEM;
     }
 
     if (is_req) {
-        ble_att_write_req_write(om->om_data, BLE_ATT_WRITE_REQ_BASE_SZ, req);
+        ble_att_write_req_write(txom->om_data, BLE_ATT_WRITE_REQ_BASE_SZ, req);
     } else {
-        ble_att_write_cmd_write(om->om_data, BLE_ATT_WRITE_REQ_BASE_SZ, req);
+        ble_att_write_cmd_write(txom->om_data, BLE_ATT_WRITE_REQ_BASE_SZ, req);
     }
 
-    rc = ble_att_clt_tx_req(conn_handle, &om);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
     if (rc != 0) {
         return rc;
     }
@@ -874,7 +876,7 @@ ble_att_clt_tx_write_req_or_cmd(uint16_t conn_handle,
 int
 ble_att_clt_tx_write_req(uint16_t conn_handle,
                          const struct ble_att_write_req *req,
-                         struct os_mbuf **txom)
+                         struct os_mbuf *txom)
 {
 #if !NIMBLE_OPT(ATT_CLT_WRITE)
     return BLE_HS_ENOTSUP;
@@ -891,7 +893,7 @@ ble_att_clt_tx_write_req(uint16_t conn_handle,
 int
 ble_att_clt_tx_write_cmd(uint16_t conn_handle,
                          const struct ble_att_write_req *req,
-                         struct os_mbuf **txom)
+                         struct os_mbuf *txom)
 {
 #if !NIMBLE_OPT(ATT_CLT_WRITE_NO_RSP)
     return BLE_HS_ENOTSUP;
@@ -926,55 +928,53 @@ ble_att_clt_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
 int
 ble_att_clt_tx_prep_write(uint16_t conn_handle,
                           const struct ble_att_prep_write_cmd *req,
-                          struct os_mbuf **txom)
+                          struct os_mbuf *txom)
 {
 #if !NIMBLE_OPT(ATT_CLT_PREP_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
-    struct os_mbuf *om;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
     BLE_ATT_LOG_CMD(1, "prep write req", conn_handle,
                     ble_att_prep_write_cmd_log, req);
 
     if (req->bapc_handle == 0) {
         rc = BLE_HS_EINVAL;
-        goto done;
+        goto err;
     }
 
-    if (req->bapc_offset + OS_MBUF_PKTLEN(om) > BLE_ATT_ATTR_MAX_LEN) {
+    if (req->bapc_offset + OS_MBUF_PKTLEN(txom) > BLE_ATT_ATTR_MAX_LEN) {
         rc = BLE_HS_EINVAL;
-        goto done;
+        goto err;
     }
 
-    if (OS_MBUF_PKTLEN(om) >
+    if (OS_MBUF_PKTLEN(txom) >
         ble_att_mtu(conn_handle) - BLE_ATT_PREP_WRITE_CMD_BASE_SZ) {
 
         rc = BLE_HS_EINVAL;
-        goto done;
+        goto err;
     }
 
-    om = os_mbuf_prepend_pullup(om, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-    if (om == NULL) {
+    txom = os_mbuf_prepend_pullup(txom, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
+    if (txom == NULL) {
         rc = BLE_HS_ENOMEM;
-        goto done;
+        goto err;
     }
 
-    ble_att_prep_write_req_write(om->om_data, BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+    ble_att_prep_write_req_write(txom->om_data, BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
                                  req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &om);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
+    txom = NULL;
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
-done:
-    os_mbuf_free_chain(om);
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -1037,7 +1037,7 @@ ble_att_clt_tx_exec_write(uint16_t conn_handle,
     }
     ble_att_exec_write_req_write(txom->om_data, txom->om_len, req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &txom);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
     if (rc != 0) {
         return rc;
     }
@@ -1072,19 +1072,14 @@ ble_att_clt_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
 int
 ble_att_clt_tx_notify(uint16_t conn_handle,
                       const struct ble_att_notify_req *req,
-                      struct os_mbuf **txom)
+                      struct os_mbuf *txom)
 {
 #if !NIMBLE_OPT(ATT_CLT_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
-    struct os_mbuf *om;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
     BLE_ATT_LOG_CMD(1, "notify req", conn_handle, ble_att_notify_req_log, req);
 
     if (req->banq_handle == 0) {
@@ -1092,14 +1087,15 @@ ble_att_clt_tx_notify(uint16_t conn_handle,
         goto err;
     }
 
-    om = os_mbuf_prepend_pullup(om, BLE_ATT_NOTIFY_REQ_BASE_SZ);
-    if (om == NULL) {
+    txom = os_mbuf_prepend_pullup(txom, BLE_ATT_NOTIFY_REQ_BASE_SZ);
+    if (txom == NULL) {
         rc = BLE_HS_ENOMEM;
         goto err;
     }
-    ble_att_notify_req_write(om->om_data, BLE_ATT_NOTIFY_REQ_BASE_SZ, req);
+    ble_att_notify_req_write(txom->om_data, BLE_ATT_NOTIFY_REQ_BASE_SZ, req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &om);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
+    txom = NULL;
     if (rc != 0) {
         goto err;
     }
@@ -1107,7 +1103,7 @@ ble_att_clt_tx_notify(uint16_t conn_handle,
     return 0;
 
 err:
-    os_mbuf_free_chain(om);
+    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -1118,19 +1114,14 @@ err:
 int
 ble_att_clt_tx_indicate(uint16_t conn_handle,
                         const struct ble_att_indicate_req *req,
-                        struct os_mbuf **txom)
+                        struct os_mbuf *txom)
 {
 #if !NIMBLE_OPT(ATT_CLT_INDICATE)
     return BLE_HS_ENOTSUP;
 #endif
 
-    struct os_mbuf *om;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
     BLE_ATT_LOG_CMD(1, "indicate req", conn_handle, ble_att_indicate_req_log,
                     req);
 
@@ -1139,15 +1130,17 @@ ble_att_clt_tx_indicate(uint16_t conn_handle,
         goto err;
     }
 
-    om = os_mbuf_prepend_pullup(om, BLE_ATT_INDICATE_REQ_BASE_SZ);
-    if (om == NULL) {
+    txom = os_mbuf_prepend_pullup(txom, BLE_ATT_INDICATE_REQ_BASE_SZ);
+    if (txom == NULL) {
         rc = BLE_HS_ENOMEM;
         goto err;
     }
 
-    ble_att_indicate_req_write(om->om_data, BLE_ATT_INDICATE_REQ_BASE_SZ, req);
+    ble_att_indicate_req_write(txom->om_data, BLE_ATT_INDICATE_REQ_BASE_SZ,
+                               req);
 
-    rc = ble_att_clt_tx_req(conn_handle, &om);
+    rc = ble_att_clt_tx_req(conn_handle, txom);
+    txom = NULL;
     if (rc != 0) {
         goto err;
     }
@@ -1155,7 +1148,7 @@ ble_att_clt_tx_indicate(uint16_t conn_handle,
     return 0;
 
 err:
-    os_mbuf_free_chain(om);
+    os_mbuf_free_chain(txom);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/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 f20817c..edf9dce 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -179,7 +179,7 @@ ble_att_svr_find_by_uuid(struct ble_att_svr_entry *start_at,
                          const uint8_t *uuid,
                          uint16_t end_handle);
 uint16_t ble_att_svr_prev_handle(void);
-int ble_att_svr_rx_mtu(uint16_t conn_handle, struct os_mbuf **om);
+int ble_att_svr_rx_mtu(uint16_t conn_handle, struct os_mbuf **rxom);
 struct ble_att_svr_entry *ble_att_svr_find_by_handle(uint16_t handle_id);
 int ble_att_svr_rx_find_info(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_svr_rx_find_type_value(uint16_t conn_handle,
@@ -266,7 +266,7 @@ int ble_att_clt_rx_read_group_type(uint16_t conn_handle,
                                    struct os_mbuf **rxom);
 int ble_att_clt_tx_find_info(uint16_t conn_handle,
                              const struct ble_att_find_info_req *req);
-int ble_att_clt_rx_find_info(uint16_t conn_handle, struct os_mbuf **om);
+int ble_att_clt_rx_find_info(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_find_type_value(
     uint16_t conn_handle, const struct ble_att_find_type_value_req *req,
     const void *attribute_value, int value_len);
@@ -274,13 +274,13 @@ int ble_att_clt_rx_find_type_value(uint16_t conn_handle,
                                    struct os_mbuf **rxom);
 int ble_att_clt_tx_write_req(uint16_t conn_handle,
                              const struct ble_att_write_req *req,
-                             struct os_mbuf **txom);
+                             struct os_mbuf *txom);
 int ble_att_clt_tx_write_cmd(uint16_t conn_handle,
                              const struct ble_att_write_req *req,
-                             struct os_mbuf **txom);
+                             struct os_mbuf *txom);
 int ble_att_clt_tx_prep_write(uint16_t conn_handle,
                               const struct ble_att_prep_write_cmd *req,
-                              struct os_mbuf **om);
+                              struct os_mbuf *txom);
 int ble_att_clt_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_exec_write(uint16_t conn_handle,
                               const struct ble_att_exec_write_req *req);
@@ -288,10 +288,10 @@ int ble_att_clt_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_rx_write(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_notify(uint16_t conn_handle,
                           const struct ble_att_notify_req *req,
-                          struct os_mbuf **txom);
+                          struct os_mbuf *txom);
 int ble_att_clt_tx_indicate(uint16_t conn_handle,
                             const struct ble_att_indicate_req *req,
-                            struct os_mbuf **txom);
+                            struct os_mbuf *txom);
 int ble_att_clt_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/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 a9f5409..7ccae06 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -502,7 +502,7 @@ ble_att_svr_write_handle(uint16_t conn_handle, uint16_t attr_handle,
 
 static int
 ble_att_svr_tx_error_rsp(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **om, uint8_t req_op,
+                         struct os_mbuf *txom, uint8_t req_op,
                          uint16_t handle, uint8_t error_code)
 {
     struct ble_att_error_rsp rsp;
@@ -510,11 +510,12 @@ ble_att_svr_tx_error_rsp(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
     int rc;
 
     BLE_HS_DBG_ASSERT(error_code != 0);
-    BLE_HS_DBG_ASSERT(OS_MBUF_PKTLEN(*om) == 0);
+    BLE_HS_DBG_ASSERT(OS_MBUF_PKTLEN(txom) == 0);
 
-    dst = os_mbuf_extend(*om, BLE_ATT_ERROR_RSP_SZ);
+    dst = os_mbuf_extend(txom, BLE_ATT_ERROR_RSP_SZ);
     if (dst == NULL) {
-        return BLE_HS_ENOMEM;
+        rc = BLE_HS_ENOMEM;
+        goto err;
     }
 
     rsp.baep_req_op = req_op;
@@ -525,12 +526,17 @@ ble_att_svr_tx_error_rsp(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
     BLE_ATT_LOG_CMD(1, "error rsp", conn->bhc_handle,
                     ble_att_error_rsp_log, &rsp);
 
-    rc = ble_l2cap_tx(conn, chan, om);
+    rc = ble_l2cap_tx(conn, chan, txom);
+    txom = NULL;
     if (rc != 0) {
-        return rc;
+        goto err;
     }
 
     return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
 }
 
 /**
@@ -557,15 +563,13 @@ ble_att_svr_tx_error_rsp(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
  *                                  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 rc, 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;
 
-    BLE_HS_DBG_ASSERT(om != NULL);
-
     if (rc == BLE_HS_ENOTCONN) {
         /* No connection; tx is not possible. */
         do_tx = 0;
@@ -584,11 +588,10 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf **om,
             rc = BLE_HS_ENOTCONN;
         } else {
             if (rc == 0) {
-                BLE_HS_DBG_ASSERT(*om != NULL);
-
-                ble_att_inc_tx_stat((*om)->om_data[0]);
-                ble_att_truncate_to_mtu(chan, *om);
+                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;
                 }
@@ -598,15 +601,15 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf **om,
                 STATS_INC(ble_att_stats, error_rsp_tx);
 
                 /* Reuse om for error response. */
-                if (*om == NULL) {
-                    *om = ble_hs_mbuf_l2cap_pkt();
+                if (om == NULL) {
+                    om = ble_hs_mbuf_l2cap_pkt();
                 } else {
-                    /* Remove response from mbuf. */
-                    os_mbuf_adj(*om, OS_MBUF_PKTLEN(*om));
+                    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;
                 }
             }
         }
@@ -614,6 +617,9 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf **om,
         ble_hs_unlock();
     }
 
+    /* Free mbuf if it was not consumed (send failed). */
+    os_mbuf_free_chain(om);
+
     return rc;
 }
 
@@ -696,10 +702,8 @@ ble_att_svr_rx_mtu(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_MTU_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_MTU_REQ,
                             att_err, 0);
-    os_mbuf_free_chain(txom);
-
     if (rc == 0) {
         ble_hs_lock();
         ble_att_conn_chan_find(conn_handle, &conn, &chan);
@@ -916,9 +920,8 @@ ble_att_svr_rx_find_info(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_FIND_INFO_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_FIND_INFO_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -1236,10 +1239,9 @@ ble_att_svr_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom,
                             BLE_ATT_OP_FIND_TYPE_VALUE_REQ, att_err,
                             err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -1444,9 +1446,8 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_READ_TYPE_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_READ_TYPE_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -1501,9 +1502,8 @@ ble_att_svr_rx_read(uint16_t conn_handle, struct os_mbuf **rxom)
     }
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_READ_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_READ_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -1570,9 +1570,8 @@ ble_att_svr_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_READ_BLOB_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_READ_BLOB_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -1689,9 +1688,8 @@ ble_att_svr_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_READ_MULT_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_READ_MULT_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -2027,10 +2025,9 @@ ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom,
                             BLE_ATT_OP_READ_GROUP_TYPE_REQ, att_err,
                             err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -2113,9 +2110,8 @@ ble_att_svr_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_WRITE_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_WRITE_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -2159,9 +2155,7 @@ ble_att_svr_rx_write_no_rsp(uint16_t conn_handle, struct os_mbuf **rxom)
  * performed by calling the registered GATT access callback.
  *
  * @param attr_handle           The 16-bit handle of the attribute to write.
- * @param om                    The value to write to the attribute.  Double
- *                                  indirection is used to effect a transfer of
- *                                  ownership from the caller.
+ * @param om                    The value to write to the attribute.
  *
  * @return                      0 on success;
  *                              NimBLE host ATT return code if the attribute
@@ -2170,16 +2164,15 @@ ble_att_svr_rx_write_no_rsp(uint16_t conn_handle, struct os_mbuf **rxom)
  *                                  error.
  */
 int
-ble_att_svr_write_local(uint16_t attr_handle, struct os_mbuf **om)
+ble_att_svr_write_local(uint16_t attr_handle, struct os_mbuf *om)
 {
     int rc;
 
     rc = ble_att_svr_write_handle(BLE_HS_CONN_HANDLE_NONE, attr_handle, 0,
-                                  om, NULL);
+                                  &om, NULL);
 
-    /* Free the mbuf in case it wasn't consumed (i.e., something failed). */
-    os_mbuf_free_chain(*om);
-    *om = NULL;
+    /* Free the mbuf if it wasn't relinquished to the application. */
+    os_mbuf_free_chain(om);
 
     return rc;
 }
@@ -2505,9 +2498,8 @@ done:
         ble_hs_unlock();
     }
 
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_PREP_WRITE_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_PREP_WRITE_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
 
     return rc;
 }
@@ -2614,9 +2606,8 @@ done:
         }
     }
 
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_EXEC_WRITE_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_EXEC_WRITE_REQ,
                             att_err, err_handle);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 
@@ -2735,9 +2726,8 @@ ble_att_svr_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    rc = ble_att_svr_tx_rsp(conn_handle, rc, &txom, BLE_ATT_OP_INDICATE_REQ,
+    rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_INDICATE_REQ,
                             0, 0);
-    os_mbuf_free_chain(txom);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/ble_gatt_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatt_priv.h b/net/nimble/host/src/ble_gatt_priv.h
index c33d5c6..7c0f020 100644
--- a/net/nimble/host/src/ble_gatt_priv.h
+++ b/net/nimble/host/src/ble_gatt_priv.h
@@ -103,11 +103,11 @@ void ble_gattc_rx_read_type_adata(uint16_t conn_handle,
                                   struct ble_att_read_type_adata *adata);
 void ble_gattc_rx_read_type_complete(uint16_t conn_handle, int status);
 void ble_gattc_rx_read_rsp(uint16_t conn_handle, int status,
-                           struct os_mbuf **om);
+                           struct os_mbuf **rxom);
 void ble_gattc_rx_read_blob_rsp(uint16_t conn_handle, int status,
-                                struct os_mbuf **om);
+                                struct os_mbuf **rxom);
 void ble_gattc_rx_read_mult_rsp(uint16_t conn_handle, int status,
-                                struct os_mbuf **om);
+                                struct os_mbuf **rxom);
 void ble_gattc_rx_read_group_type_adata(
     uint16_t conn_handle, struct ble_att_read_group_type_adata *adata);
 void ble_gattc_rx_read_group_type_complete(uint16_t conn_handle, int rc);
@@ -117,7 +117,7 @@ void ble_gattc_rx_find_type_value_complete(uint16_t conn_handle, int status);
 void ble_gattc_rx_write_rsp(uint16_t conn_handle);
 void ble_gattc_rx_prep_write_rsp(uint16_t conn_handle, int status,
                                  struct ble_att_prep_write_cmd *rsp,
-                                 struct os_mbuf **om);
+                                 struct os_mbuf **rxom);
 void ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status);
 void ble_gattc_rx_indicate_rsp(uint16_t conn_handle);
 void ble_gattc_rx_find_info_idata(uint16_t conn_handle,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/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 323c7f2..49c432f 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -3112,14 +3112,12 @@ done:
  * @param attr_handle           The handle of the characteristic value to write
  *                                  to.
  * @param txom                  The value to write to the characteristic.
- *                                  Double indirection is used to effect a
- *                                  transfer of ownership from the caller.
  *
  * @return                      0 on success; nonzero on failure.
  */
 int
 ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
-                       struct os_mbuf **txom)
+                       struct os_mbuf *txom)
 {
 #if !NIMBLE_OPT(GATT_WRITE_NO_RSP)
     return BLE_HS_ENOTSUP;
@@ -3130,7 +3128,7 @@ ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
 
     STATS_INC(ble_gattc_stats, write_no_rsp);
 
-    ble_gattc_log_write(attr_handle, OS_MBUF_PKTLEN(*txom), 0);
+    ble_gattc_log_write(attr_handle, OS_MBUF_PKTLEN(txom), 0);
 
     req.bawq_handle = attr_handle;
     rc = ble_att_clt_tx_write_cmd(conn_handle, &req, txom);
@@ -3138,10 +3136,6 @@ ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
         STATS_INC(ble_gattc_stats, write);
     }
 
-    /* Free the mbuf in case the send failed. */
-    os_mbuf_free_chain(*txom);
-    *txom = NULL;
-
     return rc;
 }
 
@@ -3170,7 +3164,7 @@ ble_gattc_write_no_rsp_flat(uint16_t conn_handle, uint16_t attr_handle,
         return BLE_HS_ENOMEM;
     }
 
-    rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, &om);
+    rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, om);
     if (rc != 0) {
         return rc;
     }
@@ -3237,8 +3231,6 @@ ble_gattc_write_err(struct ble_gattc_proc *proc, int status,
  * @param attr_handle           The handle of the characteristic value to write
  *                                  to.
  * @param txom                  The value to write to the characteristic.
- *                                  Double indirection is used to effect a
- *                                  transfer of ownership from the caller.
  * @param cb                    The function to call to report procedure status
  *                                  updates; null for no callback.
  * @param cb_arg                The optional argument to pass to the callback
@@ -3248,7 +3240,7 @@ ble_gattc_write_err(struct ble_gattc_proc *proc, int status,
  */
 int
 ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle,
-                struct os_mbuf **txom, ble_gatt_attr_fn *cb, void *cb_arg)
+                struct os_mbuf *txom, ble_gatt_attr_fn *cb, void *cb_arg)
 {
 #if !NIMBLE_OPT(GATT_WRITE)
     return BLE_HS_ENOTSUP;
@@ -3272,10 +3264,11 @@ ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle,
     proc->write.cb = cb;
     proc->write.cb_arg = cb_arg;
 
-    ble_gattc_log_write(attr_handle, OS_MBUF_PKTLEN(*txom), 1);
+    ble_gattc_log_write(attr_handle, OS_MBUF_PKTLEN(txom), 1);
 
     req.bawq_handle = attr_handle;
     rc = ble_att_clt_tx_write_req(conn_handle, &req, txom);
+    txom = NULL;
     if (rc != 0) {
         goto done;
     }
@@ -3286,8 +3279,7 @@ done:
     }
 
     /* Free the mbuf in case the send failed. */
-    os_mbuf_free_chain(*txom);
-    *txom = NULL;
+    os_mbuf_free_chain(txom);
 
     ble_gattc_process_status(proc, rc);
     return rc;
@@ -3322,7 +3314,7 @@ ble_gattc_write_flat(uint16_t conn_handle, uint16_t attr_handle,
         return BLE_HS_ENOMEM;
     }
 
-    rc = ble_gattc_write(conn_handle, attr_handle, &om, cb, cb_arg);
+    rc = ble_gattc_write(conn_handle, attr_handle, om, cb, cb_arg);
     if (rc != 0) {
         return rc;
     }
@@ -3421,7 +3413,8 @@ ble_gattc_write_long_go(struct ble_gattc_proc *proc, int cb_on_err)
     prep_req.bapc_handle = proc->write_long.attr.handle;
     prep_req.bapc_offset = proc->write_long.attr.offset;
 
-    rc = ble_att_clt_tx_prep_write(proc->conn_handle, &prep_req, &om);
+    rc = ble_att_clt_tx_prep_write(proc->conn_handle, &prep_req, om);
+    om = NULL;
     if (rc != 0) {
         goto done;
     }
@@ -3548,8 +3541,6 @@ ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc, int status)
  * @param attr_handle           The handle of the characteristic value to write
  *                                  to.
  * @param txom                  The value to write to the characteristic.
- *                                  Double indirection is used to effect a
- *                                  transfer of ownership from the caller.
  * @param cb                    The function to call to report procedure status
  *                                  updates; null for no callback.
  * @param cb_arg                The optional argument to pass to the callback
@@ -3559,7 +3550,7 @@ ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc, int status)
  */
 int
 ble_gattc_write_long(uint16_t conn_handle, uint16_t attr_handle,
-                     struct os_mbuf **txom, ble_gatt_attr_fn *cb, void *cb_arg)
+                     struct os_mbuf *txom, ble_gatt_attr_fn *cb, void *cb_arg)
 {
 #if !NIMBLE_OPT(GATT_WRITE_LONG)
     return BLE_HS_ENOTSUP;
@@ -3580,12 +3571,12 @@ ble_gattc_write_long(uint16_t conn_handle, uint16_t attr_handle,
     proc->conn_handle = conn_handle;
     proc->write_long.attr.handle = attr_handle;
     proc->write_long.attr.offset = 0;
-    proc->write_long.attr.om = *txom;
+    proc->write_long.attr.om = txom;
     proc->write_long.cb = cb;
     proc->write_long.cb_arg = cb_arg;
 
     /* The mbuf is consumed by the procedure. */
-    *txom = NULL;
+    txom = NULL;
 
     ble_gattc_log_write_long(proc);
 
@@ -3600,8 +3591,7 @@ done:
     }
 
     /* Free the mbuf in case of failure. */
-    os_mbuf_free_chain(*txom);
-    *txom = NULL;
+    os_mbuf_free_chain(txom);
 
     ble_gattc_process_status(proc, rc);
     return rc;
@@ -3699,7 +3689,8 @@ ble_gattc_write_reliable_go(struct ble_gattc_proc *proc, int cb_on_err)
     prep_req.bapc_handle = attr->handle;
     prep_req.bapc_offset = attr->offset;
 
-    rc = ble_att_clt_tx_prep_write(proc->conn_handle, &prep_req, &om);
+    rc = ble_att_clt_tx_prep_write(proc->conn_handle, &prep_req, om);
+    om = NULL;
     if (rc != 0) {
         goto done;
     }
@@ -3914,63 +3905,57 @@ done:
  * @param chr_val_handle        The attribute handle to indicate in the
  *                                  outgoing notification.
  * @param txom                  The value to write to the characteristic.
- *                                  Double indirection is used to effect a
- *                                  transfer of ownership from the caller.
  *
  * @return                      0 on success; nonzero on failure.
  */
 int
 ble_gattc_notify_custom(uint16_t conn_handle, uint16_t chr_val_handle,
-                        struct os_mbuf **txom)
+                        struct os_mbuf *txom)
 {
 #if !NIMBLE_OPT(GATT_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
     struct ble_att_notify_req req;
-    struct os_mbuf *om;
     int rc;
 
     STATS_INC(ble_gattc_stats, notify);
 
     ble_gattc_log_notify(chr_val_handle);
 
-    if (txom != NULL) {
-        /* Consume mbuf from caller. */
-        om = *txom;
-        *txom = NULL;
-    } else {
+    if (txom == NULL) {
         /* No custom attribute data; read the value from the specified
          * attribute.
          */
-        om = ble_hs_mbuf_att_pkt();
-        if (om == NULL) {
+        txom = ble_hs_mbuf_att_pkt();
+        if (txom == NULL) {
             rc = BLE_HS_ENOMEM;
-            goto done;
+            goto err;
         }
         rc = ble_att_svr_read_handle(BLE_HS_CONN_HANDLE_NONE,
-                                     chr_val_handle, 0, om, NULL);
+                                     chr_val_handle, 0, txom, NULL);
         if (rc != 0) {
             /* Fatal error; application disallowed attribute read. */
             rc = BLE_HS_EAPP;
-            goto done;
+            goto err;
         }
     }
 
     req.banq_handle = chr_val_handle;
-    rc = ble_att_clt_tx_notify(conn_handle, &req, &om);
+    rc = ble_att_clt_tx_notify(conn_handle, &req, txom);
+    txom = NULL;
     if (rc != 0) {
-        goto done;
+        goto err;
     }
 
-    rc = 0;
+    return 0;
 
-done:
+err:
     if (rc != 0) {
         STATS_INC(ble_gattc_stats, notify_fail);
     }
 
-    os_mbuf_free_chain(om);
+    os_mbuf_free_chain(txom);
 
     return rc;
 }
@@ -4100,6 +4085,8 @@ ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle)
 
     STATS_INC(ble_gattc_stats, indicate);
 
+    om = NULL;
+
     proc = ble_gattc_proc_alloc();
     if (proc == NULL) {
         rc = BLE_HS_ENOMEM;
@@ -4128,7 +4115,8 @@ ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle)
     }
 
     req.baiq_handle = chr_val_handle;
-    rc = ble_att_clt_tx_indicate(conn_handle, &req, &om);
+    rc = ble_att_clt_tx_indicate(conn_handle, &req, om);
+    om = NULL;
     if (rc != 0) {
         goto done;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/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 3d37c47..aeba768 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -343,19 +343,18 @@ ble_hs_start(void)
  * @return                      0 on success; nonzero on failure.
  */
 int
-ble_hs_rx_data(struct os_mbuf **om)
+ble_hs_rx_data(struct os_mbuf *om)
 {
     int rc;
 
-    rc = os_mqueue_put(&ble_hs_rx_q, &ble_hs_evq, *om);
-    if (rc == 0) {
-        *om = NULL;
-    } else {
-        rc = BLE_HS_EOS;
+    rc = os_mqueue_put(&ble_hs_rx_q, &ble_hs_evq, om);
+    if (rc != 0) {
+        os_mbuf_free_chain(om);
+        return BLE_HS_EOS;
     }
+
     os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
 
-    os_mbuf_free_chain(*om);
     return 0;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 7f284f5..a25c364 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -272,37 +272,27 @@ err:
  * mbuf is consumed, regardless of the outcome of the function call.
  * 
  * @param chan                  The L2CAP channel to transmit over.
- * @param om                    The data to transmit.
+ * @param txom                  The data to transmit.
  *
  * @return                      0 on success; nonzero on error.
  */
 int
 ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-             struct os_mbuf **txom)
+             struct os_mbuf *txom)
 {
-    struct os_mbuf *om;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
-    om = ble_l2cap_prepend_hdr(om, chan->blc_cid, OS_MBUF_PKTLEN(om));
-    if (om == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
+    txom = ble_l2cap_prepend_hdr(txom, chan->blc_cid, OS_MBUF_PKTLEN(txom));
+    if (txom == NULL) {
+        return BLE_HS_ENOMEM;
     }
 
-    rc = host_hci_data_tx(conn, &om);
+    rc = host_hci_data_tx(conn, txom);
     if (rc != 0) {
-        goto err;
+        return rc;
     }
 
     return 0;
-
-err:
-    os_mbuf_free_chain(om);
-    return rc;
 }
 
 static void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/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 3eaf245..ce19218 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -57,8 +57,7 @@ extern struct os_mempool ble_l2cap_chan_pool;
 
 typedef uint8_t ble_l2cap_chan_flags;
 
-typedef int ble_l2cap_rx_fn(uint16_t conn_handle,
-                            struct os_mbuf **om);
+typedef int ble_l2cap_rx_fn(uint16_t conn_handle, struct os_mbuf **rxom);
 
 struct ble_l2cap_chan {
     SLIST_ENTRY(ble_l2cap_chan) blc_next;
@@ -103,7 +102,7 @@ int ble_l2cap_rx(struct ble_hs_conn *conn,
                  ble_l2cap_rx_fn **out_rx_cb,
                  struct os_mbuf **out_rx_buf);
 int ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                 struct os_mbuf **txom);
+                 struct os_mbuf *txom);
 
 int ble_l2cap_init(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/ble_l2cap_sig_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_cmd.c b/net/nimble/host/src/ble_l2cap_sig_cmd.c
index 6b08a2b..d822032 100644
--- a/net/nimble/host/src/ble_l2cap_sig_cmd.c
+++ b/net/nimble/host/src/ble_l2cap_sig_cmd.c
@@ -129,7 +129,7 @@ ble_l2cap_sig_reject_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                                data, data_len);
 
     STATS_INC(ble_l2cap_stats, sig_rx);
-    rc = ble_l2cap_tx(conn, chan, &txom);
+    rc = ble_l2cap_tx(conn, chan, txom);
     if (rc != 0) {
         return rc;
     }
@@ -203,7 +203,7 @@ ble_l2cap_sig_update_req_tx(struct ble_hs_conn *conn,
     ble_l2cap_sig_update_req_write(payload_buf, BLE_L2CAP_SIG_UPDATE_REQ_SZ,
                                    req);
 
-    rc = ble_l2cap_tx(conn, chan, &txom);
+    rc = ble_l2cap_tx(conn, chan, txom);
     if (rc != 0) {
         return rc;
     }
@@ -255,7 +255,7 @@ ble_l2cap_sig_update_rsp_tx(struct ble_hs_conn *conn,
     ble_l2cap_sig_update_rsp_write(payload_buf, BLE_L2CAP_SIG_UPDATE_RSP_SZ,
                                    &rsp);
 
-    rc = ble_l2cap_tx(conn, chan, &txom);
+    rc = ble_l2cap_tx(conn, chan, txom);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/ble_sm_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_cmd.c b/net/nimble/host/src/ble_sm_cmd.c
index 86b0c7b..9854bde 100644
--- a/net/nimble/host/src/ble_sm_cmd.c
+++ b/net/nimble/host/src/ble_sm_cmd.c
@@ -28,28 +28,29 @@
 #if NIMBLE_OPT(SM)
 
 static int
-ble_sm_tx(uint16_t conn_handle, struct os_mbuf **txom)
+ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)
 {
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
-    struct os_mbuf *om;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
     BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task());
 
     STATS_INC(ble_l2cap_stats, sm_tx);
 
     rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
                                          &conn, &chan);
-    if (rc == 0) {
-        rc = ble_l2cap_tx(conn, chan, &om);
+    if (rc != 0) {
+        os_mbuf_free_chain(txom);
+        return rc;
     }
 
-    return rc;
+    rc = ble_l2cap_tx(conn, chan, txom);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
 }
 
 static int
@@ -153,8 +154,7 @@ ble_sm_pair_cmd_tx(uint16_t conn_handle, int is_req,
 
     rc = ble_sm_init_req(BLE_SM_PAIR_CMD_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_pair_cmd_write(txom->om_data, txom->om_len, is_req, cmd);
@@ -162,14 +162,12 @@ ble_sm_pair_cmd_tx(uint16_t conn_handle, int is_req,
                    ble_sm_pair_cmd_log, cmd);
     BLE_HS_DBG_ASSERT(ble_sm_pair_cmd_is_valid(cmd));
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -213,21 +211,18 @@ ble_sm_pair_confirm_tx(uint16_t conn_handle, struct ble_sm_pair_confirm *cmd)
 
     rc = ble_sm_init_req(BLE_SM_PAIR_CONFIRM_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_pair_confirm_write(txom->om_data, txom->om_len, cmd);
     BLE_SM_LOG_CMD(1, "confirm", conn_handle, ble_sm_pair_confirm_log, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -268,21 +263,18 @@ ble_sm_pair_random_tx(uint16_t conn_handle, struct ble_sm_pair_random *cmd)
 
     rc = ble_sm_init_req(BLE_SM_PAIR_RANDOM_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_pair_random_write(txom->om_data, txom->om_len, cmd);
     BLE_SM_LOG_CMD(1, "random", conn_handle, ble_sm_pair_random_log, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -328,8 +320,7 @@ ble_sm_pair_fail_tx(uint16_t conn_handle, uint8_t reason)
 
     rc = ble_sm_init_req(BLE_SM_PAIR_FAIL_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     cmd.reason = reason;
@@ -337,14 +328,12 @@ ble_sm_pair_fail_tx(uint16_t conn_handle, uint8_t reason)
     ble_sm_pair_fail_write(txom->om_data, txom->om_len, &cmd);
     BLE_SM_LOG_CMD(1, "fail", conn_handle, ble_sm_pair_fail_log, &cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -380,22 +369,19 @@ ble_sm_enc_info_tx(uint16_t conn_handle, struct ble_sm_enc_info *cmd)
 
     rc = ble_sm_init_req(BLE_SM_ENC_INFO_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_enc_info_write(txom->om_data, txom->om_len, cmd);
 
     BLE_SM_LOG_CMD(1, "enc info", conn_handle, ble_sm_enc_info_log, cmd);
     
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -438,8 +424,7 @@ ble_sm_master_id_tx(uint16_t conn_handle, struct ble_sm_master_id *cmd)
 
     rc = ble_sm_init_req(BLE_SM_MASTER_ID_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     txom->om_data[0] = BLE_SM_OP_MASTER_ID;
@@ -448,14 +433,12 @@ ble_sm_master_id_tx(uint16_t conn_handle, struct ble_sm_master_id *cmd)
 
     BLE_SM_LOG_CMD(1, "master id", conn_handle, ble_sm_master_id_log, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -497,20 +480,17 @@ ble_sm_id_info_tx(uint16_t conn_handle, struct ble_sm_id_info *cmd)
 
     rc = ble_sm_init_req(BLE_SM_ID_INFO_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_id_info_write(txom->om_data, txom->om_len, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -555,20 +535,17 @@ ble_sm_id_addr_info_tx(uint16_t conn_handle, struct ble_sm_id_addr_info *cmd)
 
     rc = ble_sm_init_req(BLE_SM_ID_ADDR_INFO_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_id_addr_info_write(txom->om_data, txom->om_len, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -607,20 +584,17 @@ ble_sm_sign_info_tx(uint16_t conn_handle, struct ble_sm_sign_info *cmd)
 
     rc = ble_sm_init_req(BLE_SM_SIGN_INFO_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_sign_info_write(txom->om_data, txom->om_len, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -662,22 +636,19 @@ ble_sm_sec_req_tx(uint16_t conn_handle, struct ble_sm_sec_req *cmd)
 
     rc = ble_sm_init_req(BLE_SM_SEC_REQ_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     ble_sm_sec_req_write(txom->om_data, txom->om_len, cmd);
 
     BLE_SM_LOG_CMD(1, "sec req", conn_handle, ble_sm_sec_req_log, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -728,8 +699,7 @@ ble_sm_public_key_tx(uint16_t conn_handle, struct ble_sm_public_key *cmd)
 
     rc = ble_sm_init_req(BLE_SM_PUBLIC_KEY_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     rc = ble_sm_public_key_write(txom->om_data, txom->om_len, cmd);
@@ -737,14 +707,12 @@ ble_sm_public_key_tx(uint16_t conn_handle, struct ble_sm_public_key *cmd)
 
     BLE_SM_LOG_CMD(1, "public key", conn_handle, ble_sm_public_key_log, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void
@@ -791,8 +759,7 @@ ble_sm_dhkey_check_tx(uint16_t conn_handle, struct ble_sm_dhkey_check *cmd)
 
     rc = ble_sm_init_req(BLE_SM_DHKEY_CHECK_SZ, &txom);
     if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto done;
+        return BLE_HS_ENOMEM;
     }
 
     rc = ble_sm_dhkey_check_write(txom->om_data, txom->om_len, cmd);
@@ -800,14 +767,12 @@ ble_sm_dhkey_check_tx(uint16_t conn_handle, struct ble_sm_dhkey_check *cmd)
 
     BLE_SM_LOG_CMD(1, "dhkey check", conn_handle, ble_sm_dhkey_check_log, cmd);
 
-    rc = ble_sm_tx(conn_handle, &txom);
+    rc = ble_sm_tx(conn_handle, txom);
     if (rc != 0) {
-        goto done;
+        return rc;
     }
 
-done:
-    os_mbuf_free_chain(txom);
-    return rc;
+    return 0;
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_priv.h b/net/nimble/host/src/ble_sm_priv.h
index fbc7139..80798e1 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -417,11 +417,12 @@ void ble_sm_sc_public_key_exec(struct ble_sm_proc *proc,
                                struct ble_sm_result *res,
                                void *arg);
 void ble_sm_sc_public_key_rx(uint16_t conn_handle, uint8_t op,
-                             struct os_mbuf **om, struct ble_sm_result *res);
+                             struct os_mbuf **rxom, struct ble_sm_result *res);
 void ble_sm_sc_dhkey_check_exec(struct ble_sm_proc *proc,
                                 struct ble_sm_result *res, void *arg);
 void ble_sm_sc_dhkey_check_rx(uint16_t conn_handle, uint8_t op,
-                              struct os_mbuf **om, struct ble_sm_result *res);
+                              struct os_mbuf **rxom,
+                              struct ble_sm_result *res);
 void ble_sm_sc_init(void);
 #else
 #define ble_sm_sc_io_action(proc) (BLE_SM_IOACT_NONE)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/host_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci.c b/net/nimble/host/src/host_hci.c
index 44d83b3..791aec2 100644
--- a/net/nimble/host/src/host_hci.c
+++ b/net/nimble/host/src/host_hci.c
@@ -801,14 +801,15 @@ host_hci_data_hdr_prepend(struct os_mbuf *om, uint16_t handle, uint8_t pb_flag)
  *                              Other BLE host core return code on error.
  */
 int
-host_hci_split_frag(struct os_mbuf *om, struct os_mbuf **out_frag)
+host_hci_split_frag(struct os_mbuf **om, struct os_mbuf **out_frag)
 {
     struct os_mbuf *frag;
     int rc;
 
-    if (OS_MBUF_PKTLEN(om) <= host_hci_buffer_sz) {
+    if (OS_MBUF_PKTLEN(*om) <= host_hci_buffer_sz) {
         /* Final fragment. */
-        *out_frag = om;
+        *out_frag = *om;
+        *om = NULL;
         return BLE_HS_EDONE;
     }
 
@@ -819,12 +820,12 @@ host_hci_split_frag(struct os_mbuf *om, struct os_mbuf **out_frag)
     }
 
     /* Move data from the front of the packet into the fragment mbuf. */
-    rc = os_mbuf_appendfrom(frag, om, 0, host_hci_buffer_sz);
+    rc = os_mbuf_appendfrom(frag, *om, 0, host_hci_buffer_sz);
     if (rc != 0) {
         rc = BLE_HS_ENOMEM;
         goto err;
     }
-    os_mbuf_adj(om, host_hci_buffer_sz);
+    os_mbuf_adj(*om, host_hci_buffer_sz);
 
     /* More fragments to follow. */
     *out_frag = frag;
@@ -843,18 +844,13 @@ err:
  * fragments.
  */
 int
-host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf **txom)
+host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf *txom)
 {
     struct os_mbuf *frag;
-    struct os_mbuf *om;
     uint8_t pb;
     int done;
     int rc;
 
-    /* Consume mbuf from caller. */
-    om = *txom;
-    *txom = NULL;
-
     /* The first fragment uses the first-non-flush packet boundary value.
      * After sending the first fragment, pb gets set appropriately for all
      * subsequent fragments in this packet.
@@ -864,12 +860,11 @@ host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf **txom)
     /* Send fragments until the entire packet has been sent. */
     done = 0;
     while (!done) {
-        rc = host_hci_split_frag(om, &frag);
+        rc = host_hci_split_frag(&txom, &frag);
         switch (rc) {
         case BLE_HS_EDONE:
             /* This is the final fragment. */
             done = 1;
-            om = NULL;
             break;
 
         case BLE_HS_EAGAIN:
@@ -891,12 +886,17 @@ host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf **txom)
         ble_hs_log_mbuf(frag);
         BLE_HS_LOG(DEBUG, "\n");
 
-        /* Next fragment */
+        /* XXX: Try to pullup the entire fragment.  The controller currently
+         * requires the entire fragment to fit in a single buffer.  When this
+         * restriction is removed from the controller, this operation can be
+         * removed.
+         */
         frag = os_mbuf_pullup(frag, OS_MBUF_PKTLEN(frag));
         if (frag == NULL) {
             rc = BLE_HS_ENOMEM;
             goto err;
         }
+
         rc = ble_hs_tx_data(frag);
         if (rc != 0) {
             goto err;
@@ -910,6 +910,6 @@ host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf **txom)
 err:
     BLE_HS_DBG_ASSERT(rc != 0);
 
-    os_mbuf_free_chain(om);
+    os_mbuf_free_chain(txom);
     return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/test/ble_att_clt_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_clt_test.c b/net/nimble/host/src/test/ble_att_clt_test.c
index 869fb19..9461337 100644
--- a/net/nimble/host/src/test/ble_att_clt_test.c
+++ b/net/nimble/host/src/test/ble_att_clt_test.c
@@ -68,12 +68,11 @@ ble_att_clt_test_tx_write_req_or_cmd(uint16_t conn_handle,
 
     om = ble_hs_test_util_om_from_flat(value, value_len);
     if (is_req) {
-        rc = ble_att_clt_tx_write_req(conn_handle, req, &om);
+        rc = ble_att_clt_tx_write_req(conn_handle, req, om);
     } else {
-        rc = ble_att_clt_tx_write_cmd(conn_handle, req, &om);
+        rc = ble_att_clt_tx_write_cmd(conn_handle, req, om);
     }
     TEST_ASSERT(rc == 0);
-    TEST_ASSERT(om == NULL);
 }
 
 TEST_CASE(ble_att_clt_test_tx_find_info)
@@ -216,9 +215,8 @@ ble_att_clt_test_misc_prep_good(uint16_t handle, uint16_t offset,
     req.bapc_handle = handle;
     req.bapc_offset = offset;
     om = ble_hs_test_util_om_from_flat(attr_data, attr_data_len);
-    rc = ble_att_clt_tx_prep_write(conn_handle, &req, &om);
+    rc = ble_att_clt_tx_prep_write(conn_handle, &req, om);
     TEST_ASSERT(rc == 0);
-    TEST_ASSERT(om == NULL);
 
     ble_hs_test_util_tx_all();
     om = ble_hs_test_util_prev_tx_dequeue_pullup();
@@ -273,9 +271,8 @@ ble_att_clt_test_misc_prep_bad(uint16_t handle, uint16_t offset,
 
     req.bapc_handle = handle;
     req.bapc_offset = offset;
-    rc = ble_att_clt_tx_prep_write(conn_handle, &req, &om);
+    rc = ble_att_clt_tx_prep_write(conn_handle, &req, om);
     TEST_ASSERT(rc == status);
-    TEST_ASSERT(om == NULL);
 }
 
 TEST_CASE(ble_att_clt_test_tx_write)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/40695b35/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 3877174..2fbe0ef 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -1064,8 +1064,7 @@ ble_hs_test_util_write_local_flat(uint16_t attr_handle,
     int rc;
 
     om = ble_hs_test_util_om_from_flat(buf, buf_len);
-    rc = ble_att_svr_write_local(attr_handle, &om);
-    TEST_ASSERT(om == NULL);
+    rc = ble_att_svr_write_local(attr_handle, om);
     return rc;
 }
 
@@ -1078,8 +1077,7 @@ ble_hs_test_util_gatt_write_flat(uint16_t conn_handle, uint16_t attr_handle,
     int rc;
 
     om = ble_hs_test_util_om_from_flat(data, data_len);
-    rc = ble_gattc_write(conn_handle, attr_handle, &om, cb, cb_arg);
-    TEST_ASSERT(om == NULL);
+    rc = ble_gattc_write(conn_handle, attr_handle, om, cb, cb_arg);
 
     return rc;
 }
@@ -1093,8 +1091,7 @@ ble_hs_test_util_gatt_write_no_rsp_flat(uint16_t conn_handle,
     int rc;
 
     om = ble_hs_test_util_om_from_flat(data, data_len);
-    rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, &om);
-    TEST_ASSERT(om == NULL);
+    rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, om);
 
     return rc;
 }
@@ -1109,8 +1106,8 @@ ble_hs_test_util_gatt_write_long_flat(uint16_t conn_handle,
     int rc;
 
     om = ble_hs_test_util_om_from_flat(data, data_len);
-    rc = ble_gattc_write_long(conn_handle, attr_handle, &om, cb, cb_arg);
-    TEST_ASSERT(om == NULL);
+    rc = ble_gattc_write_long(conn_handle, attr_handle, om, cb, cb_arg);
+
     return rc;
 }
 


[02/16] incubator-mynewt-core git commit: os: allow os_time_advance() when OS not started.

Posted by st...@apache.org.
os: allow os_time_advance() when OS not started.

This is useful in unit tests when we need to make time pass (e.g., to
test timeouts).


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

Branch: refs/heads/sterly_refactor
Commit: b73155586fd35f6592e5b554ad3d123000e3fc4e
Parents: 5a7c0a1
Author: Christopher Collins <cc...@apache.org>
Authored: Sat Jul 30 17:27:53 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 libs/os/src/os_time.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b7315558/libs/os/src/os_time.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_time.c b/libs/os/src/os_time.c
index 2ce379e..4ef439f 100644
--- a/libs/os/src/os_time.c
+++ b/libs/os/src/os_time.c
@@ -97,10 +97,14 @@ os_time_advance(int ticks)
     assert(ticks >= 0);
 
     if (ticks > 0) {
-        os_time_tick(ticks);
-        os_callout_tick();
-        os_sched_os_timer_exp();
-        os_sched(NULL);
+        if (!os_started()) {
+            g_os_time += ticks;
+        } else {
+            os_time_tick(ticks);
+            os_callout_tick();
+            os_sched_os_timer_exp();
+            os_sched(NULL);
+        }
     }
 }
 


[15/16] incubator-mynewt-core git commit: BLE Host - Wait for conn-cmplt-ev for GAP conn tmo

Posted by st...@apache.org.
BLE Host - Wait for conn-cmplt-ev for GAP conn tmo

Prior to this change, the host had the following behavior when a GAP
connect procedure timed out:

1. Send a cancel create connection HCI command to the controller.
2. Wait for the command status event from the controller.
3. Stop the connection-timeout timer.
4. Indicate to the application that the connect procedure has timed out.
5. Receive the connection complete event from the controller indicating
that the cancel procedure succeeded.

This was problematic for a number of reasons:

* The necessary context was discarded before the impending connection
complete event was received from the controller.  When the event arrived
(step 5), the host would interpret it as if the application had manually
cancelled a connect procedure in progress.

* If the application attempted another master procedure before the
controller cancelled the connect procedure, the controller would report
a failure with status 0x0c (Command Disallowed).  This happened because
the controller cannot perform two connect procedures at the same time.

* If the application attempted another master procedure after the
controller cancelled the connect procedure, but before the host received
the connection complete event, the timer for the second procedure would
never get set.  Consequently, the second procedure would never time out.

The new behavior is:

1. Send a cancel create connection HCI command to the controller.
2. Wait for the command status event from the controller.
3. Stop the connection-timeout timer.
4. Receive the connection complete event from the controller indicating
that the cancel procedure succeeded.
5. Indicate to the application that the connect procedure has timed out.


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

Branch: refs/heads/sterly_refactor
Commit: db980b24e5e66a5d9a038fd4ac43c9c6f65911bc
Parents: b731555
Author: Christopher Collins <cc...@apache.org>
Authored: Sat Jul 30 17:29:38 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap.c               |  95 +++---
 net/nimble/host/src/test/ble_gap_test.c     | 386 +++++++++++++++++------
 net/nimble/host/src/test/ble_hs_test_util.c |  98 ++++++
 net/nimble/host/src/test/ble_hs_test_util.h |   3 +
 4 files changed, 441 insertions(+), 141 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/db980b24/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 c8d0383..a001ae3 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -94,7 +94,7 @@ static const struct ble_gap_conn_params ble_gap_conn_params_dflt = {
  * The state of the in-progress master connection.  If no master connection is
  * currently in progress, then the op field is set to BLE_GAP_OP_NULL.
  */
-static bssnz_t struct {
+struct ble_gap_master_state {
     uint8_t op;
 
     uint8_t exp_set:1;
@@ -108,13 +108,15 @@ static bssnz_t struct {
         struct {
             uint8_t using_wl:1;
             uint8_t our_addr_type:2;
+            uint8_t cancel:1;
         } conn;
 
         struct {
             uint8_t limited:1;
         } disc;
     };
-} ble_gap_master;
+};
+static bssnz_t struct ble_gap_master_state ble_gap_master;
 
 /**
  * The state of the in-progress slave connection.  If no slave connection is
@@ -462,6 +464,7 @@ ble_gap_master_reset_state(void)
 {
     ble_gap_master.op = BLE_GAP_OP_NULL;
     ble_gap_master.exp_set = 0;
+    ble_gap_master.conn.cancel = 0;
 }
 
 static void
@@ -472,13 +475,12 @@ ble_gap_slave_reset_state(void)
 }
 
 static void
-ble_gap_master_extract_cb(ble_gap_event_fn **out_cb, void **out_cb_arg,
-                          int reset_state)
+ble_gap_master_extract_state(struct ble_gap_master_state *out_state,
+                             int reset_state)
 {
     ble_hs_lock();
 
-    *out_cb = ble_gap_master.cb;
-    *out_cb_arg = ble_gap_master.cb_arg;
+    *out_state = ble_gap_master;
 
     if (reset_state) {
         ble_gap_master_reset_state();
@@ -518,18 +520,17 @@ ble_gap_adv_finished(void)
 static int
 ble_gap_master_connect_failure(int status)
 {
+    struct ble_gap_master_state state;
     struct ble_gap_event event;
-    ble_gap_event_fn *cb;
-    void *cb_arg;
     int rc;
 
-    ble_gap_master_extract_cb(&cb, &cb_arg, 1);
-    if (cb != NULL) {
+    ble_gap_master_extract_state(&state, 1);
+    if (state.cb != NULL) {
         memset(&event, 0, sizeof event);
         event.type = BLE_GAP_EVENT_CONNECT;
         event.connect.status = status;
 
-        rc = cb(&event, cb_arg);
+        rc = state.cb(&event, state.cb_arg);
     } else {
         rc = 0;
     }
@@ -537,58 +538,61 @@ ble_gap_master_connect_failure(int status)
     return rc;
 }
 
-static int
-ble_gap_master_connect_cancel(void)
+static void
+ble_gap_master_connect_cancelled(void)
 {
+    struct ble_gap_master_state state;
     struct ble_gap_event event;
-    ble_gap_event_fn *cb;
-    void *cb_arg;
-    int rc;
 
-    ble_gap_master_extract_cb(&cb, &cb_arg, 1);
-    if (cb != NULL) {
+    ble_gap_master_extract_state(&state, 1);
+    if (state.cb != NULL) {
+        /* The GAP event type depends on whether 1) the application manually
+         * cancelled the connect procedure or 2) the connect procedure timed
+         * out.
+         */
         memset(&event, 0, sizeof event);
-        event.type = BLE_GAP_EVENT_CONN_CANCEL;
-        rc = cb(&event, cb_arg);
-    } else {
-        rc = 0;
-    }
+        if (state.conn.cancel) {
+            event.type = BLE_GAP_EVENT_CONN_CANCEL;
+        } else {
+            event.type = BLE_GAP_EVENT_CONNECT;
+            event.connect.status = BLE_HS_ETIMEOUT;
+            event.connect.conn_handle = BLE_HS_CONN_HANDLE_NONE;
 
-    return rc;
+        }
+        state.cb(&event, state.cb_arg);
+    }
 }
 
 static void
 ble_gap_disc_report(struct ble_gap_disc_desc *desc)
 {
+    struct ble_gap_master_state state;
     struct ble_gap_event event;
-    ble_gap_event_fn *cb;
-    void *cb_arg;
 
-    ble_gap_master_extract_cb(&cb, &cb_arg, 0);
+    ble_gap_master_extract_state(&state, 0);
 
-    if (cb != NULL) {
+    if (state.cb != NULL) {
         memset(&event, 0, sizeof event);
         event.type = BLE_GAP_EVENT_DISC;
         event.disc = *desc;
 
-        cb(&event, cb_arg);
+        state.cb(&event, state.cb_arg);
     }
 }
 
 static void
 ble_gap_disc_complete(void)
 {
+    struct ble_gap_master_state state;
     struct ble_gap_event event;
-    ble_gap_event_fn *cb;
-    void *cb_arg;
 
-    ble_gap_master_extract_cb(&cb, &cb_arg, 1);
+    ble_gap_master_extract_state(&state, 1);
 
-    if (cb != NULL) {
+    if (state.cb != NULL) {
         memset(&event, 0, sizeof event);
         event.type = BLE_GAP_EVENT_DISC_COMPLETE;
 
-        ble_gap_call_event_cb(&event, cb, cb_arg);
+        ble_gap_call_event_cb(&event, state.cb, state.cb_arg);
     }
 }
 
@@ -951,7 +955,9 @@ ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt)
     STATS_INC(ble_gap_stats, rx_conn_complete);
 
     /* Apply the event to the existing connection if it exists. */
-    if (ble_hs_atomic_conn_flags(evt->connection_handle, NULL) == 0) {
+    if (evt->status != BLE_ERR_UNK_CONN_ID &&
+        ble_hs_atomic_conn_flags(evt->connection_handle, NULL) == 0) {
+
         /* XXX: Does this ever happen? */
 
         if (evt->status != 0) {
@@ -976,7 +982,7 @@ ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt)
             if (ble_gap_master_in_progress()) {
                 if (evt->status == BLE_ERR_UNK_CONN_ID) {
                     /* Connect procedure successfully cancelled. */
-                    ble_gap_master_connect_cancel();
+                    ble_gap_master_connect_cancelled();
                 } else {
                     ble_gap_master_failed(BLE_HS_HCI_ERR(evt->status));
                 }
@@ -1089,9 +1095,17 @@ ble_gap_master_heartbeat(void)
         if (rc != 0) {
             /* Failed to stop connecting; try again in 100 ms. */
             return BLE_GAP_CANCEL_RETRY_RATE;
+        } else {
+            /* Stop the timer now that the cancel command has been acked. */
+            ble_gap_master.exp_set = 0;
+
+            /* Timeout gets reported when we receive a connection complete
+             * event indicating the connect procedure has been cancelled.
+             */
+            /* XXX: Set a timer to reset the controller if a connection
+             * complete event isn't received within a reasonable interval.
+             */
         }
-
-        ble_gap_master_failed(BLE_HS_ETIMEOUT);
         break;
 
     case BLE_GAP_OP_M_DISC:
@@ -1928,7 +1942,7 @@ ble_gap_disc_cancel(void)
 
     ble_hs_lock();
 
-    if (ble_gap_master.op != BLE_GAP_OP_M_DISC) {
+    if (!ble_gap_disc_active()) {
         rc = BLE_HS_EALREADY;
         goto done;
     }
@@ -2396,7 +2410,7 @@ ble_gap_conn_cancel(void)
 
     ble_hs_lock();
 
-    if (!ble_gap_master_in_progress()) {
+    if (!ble_gap_conn_active()) {
         rc = BLE_HS_EALREADY;
         goto done;
     }
@@ -2408,6 +2422,7 @@ ble_gap_conn_cancel(void)
         goto done;
     }
 
+    ble_gap_master.conn.cancel = 1;
     rc = 0;
 
 done:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/db980b24/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 86d0e6f..faf62c3 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -76,9 +76,12 @@ static int
 ble_gap_test_util_disc_cb(struct ble_gap_event *event, void *arg)
 {
     ble_gap_test_disc_event_type = event->type;
-    ble_gap_test_disc_desc = event->disc;
     ble_gap_test_disc_arg = arg;
 
+    if (event->type == BLE_GAP_EVENT_DISC) {
+        ble_gap_test_disc_desc = event->disc;
+    }
+
     return 0;
 }
 
@@ -207,66 +210,7 @@ ble_gap_test_util_verify_tx_scan_enable(uint8_t enable,
 }
 
 static void
-ble_gap_test_util_verify_tx_create_conn(const struct hci_create_conn *exp)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_CREATE_CONN,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_CREATE_CONN_LEN);
-
-    TEST_ASSERT(le16toh(param + 0) == exp->scan_itvl);
-    TEST_ASSERT(le16toh(param + 2) == exp->scan_window);
-    TEST_ASSERT(param[4] == exp->filter_policy);
-    TEST_ASSERT(param[5] == exp->peer_addr_type);
-    TEST_ASSERT(memcmp(param + 6, exp->peer_addr, 6) == 0);
-    TEST_ASSERT(param[12] == exp->own_addr_type);
-    TEST_ASSERT(le16toh(param + 13) == exp->conn_itvl_min);
-    TEST_ASSERT(le16toh(param + 15) == exp->conn_itvl_max);
-    TEST_ASSERT(le16toh(param + 17) == exp->conn_latency);
-    TEST_ASSERT(le16toh(param + 19) == exp->supervision_timeout);
-    TEST_ASSERT(le16toh(param + 21) == exp->min_ce_len);
-    TEST_ASSERT(le16toh(param + 23) == exp->max_ce_len);
-}
-
-static void
-ble_gap_test_util_hcc_from_conn_params(
-    struct hci_create_conn *hcc, uint8_t own_addr_type, uint8_t peer_addr_type,
-    const uint8_t *peer_addr, const struct ble_gap_conn_params *conn_params)
-{
-    hcc->scan_itvl = conn_params->scan_itvl;
-    hcc->scan_window = conn_params->scan_window;
-    hcc->filter_policy = peer_addr_type == BLE_GAP_ADDR_TYPE_WL ?
-        BLE_HCI_CONN_FILT_USE_WL :
-        BLE_HCI_CONN_FILT_NO_WL;
-    hcc->peer_addr_type = peer_addr_type;
-    memcpy(hcc->peer_addr, peer_addr, 6);
-    hcc->own_addr_type = own_addr_type;
-    hcc->conn_itvl_min = conn_params->itvl_min;
-    hcc->conn_itvl_max = conn_params->itvl_max;
-    hcc->conn_latency = conn_params->latency;
-    hcc->supervision_timeout = conn_params->supervision_timeout;
-    hcc->min_ce_len = conn_params->min_ce_len;
-    hcc->max_ce_len = conn_params->max_ce_len;
-}
-
-static void
-ble_gap_test_util_conn_params_dflt(struct ble_gap_conn_params *conn_params)
-{
-    conn_params->scan_itvl = 0x0010;
-    conn_params->scan_window = 0x0010;
-    conn_params->itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN;
-    conn_params->itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX;
-    conn_params->latency = BLE_GAP_INITIAL_CONN_LATENCY;
-    conn_params->supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT;
-    conn_params->min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
-    conn_params->max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
-}
-
-static void
-ble_gap_test_util_verify_tx_create_conn_cancel(void)
+ble_hs_test_util_verify_tx_create_conn_cancel(void)
 {
     uint8_t param_len;
 
@@ -838,7 +782,6 @@ TEST_CASE(ble_gap_test_case_conn_dir_good)
 {
     struct hci_le_conn_complete evt;
     struct ble_gap_conn_params params;
-    struct hci_create_conn hcc;
     int rc;
 
     uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
@@ -865,11 +808,6 @@ TEST_CASE(ble_gap_test_case_conn_dir_good)
     TEST_ASSERT(ble_gap_master_in_progress());
     TEST_ASSERT(ble_gap_conn_active());
 
-    /* Verify tx of create connection command. */
-    ble_gap_test_util_hcc_from_conn_params(&hcc, BLE_ADDR_TYPE_PUBLIC,
-                                           BLE_ADDR_TYPE_PUBLIC, peer_addr,
-                                           &params);
-    ble_gap_test_util_verify_tx_create_conn(&hcc);
     TEST_ASSERT(ble_gap_master_in_progress());
     TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
 
@@ -925,9 +863,6 @@ TEST_CASE(ble_gap_test_case_conn_dir_bad_args)
 TEST_CASE(ble_gap_test_case_conn_dir_dflt_params)
 {
     static const uint8_t peer_addr[6] = { 2, 3, 8, 6, 6, 1 };
-
-    struct ble_gap_conn_params conn_params;
-    struct hci_create_conn hcc;
     int rc;
 
     ble_gap_test_util_init();
@@ -936,12 +871,6 @@ TEST_CASE(ble_gap_test_case_conn_dir_dflt_params)
                                   BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, NULL,
                                   ble_gap_test_util_connect_cb, NULL, 0);
     TEST_ASSERT(rc == 0);
-
-    ble_gap_test_util_conn_params_dflt(&conn_params);
-    ble_gap_test_util_hcc_from_conn_params(&hcc, BLE_ADDR_TYPE_PUBLIC,
-                                           BLE_ADDR_TYPE_PUBLIC, peer_addr,
-                                           &conn_params);
-    ble_gap_test_util_verify_tx_create_conn(&hcc);
 }
 
 TEST_SUITE(ble_gap_test_suite_conn_dir)
@@ -958,34 +887,17 @@ TEST_SUITE(ble_gap_test_suite_conn_dir)
  *****************************************************************************/
 
 static void
-ble_gap_test_util_conn_cancel(uint8_t *peer_addr, uint8_t hci_status)
+ble_gap_test_util_conn_cancel(uint8_t hci_status)
 {
     struct hci_le_conn_complete evt;
-    struct ble_gap_conn_params conn_params;
-    struct hci_create_conn hcc;
     int rc;
 
-    ble_gap_test_util_init();
-
-    /* Begin creating a connection. */
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, NULL,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(ble_gap_master_in_progress());
-
-    ble_gap_test_util_conn_params_dflt(&conn_params);
-    ble_gap_test_util_hcc_from_conn_params(&hcc, BLE_ADDR_TYPE_PUBLIC,
-                                           BLE_ADDR_TYPE_PUBLIC, peer_addr,
-                                           &conn_params);
-    ble_gap_test_util_verify_tx_create_conn(&hcc);
-
     /* Initiate cancel procedure. */
     rc = ble_hs_test_util_conn_cancel(hci_status);
     TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
 
     /* Verify tx of cancel create connection command. */
-    ble_gap_test_util_verify_tx_create_conn_cancel();
+    ble_hs_test_util_verify_tx_create_conn_cancel();
     if (rc != 0) {
         return;
     }
@@ -995,12 +907,29 @@ ble_gap_test_util_conn_cancel(uint8_t *peer_addr, uint8_t hci_status)
     memset(&evt, 0, sizeof evt);
     evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
     evt.status = BLE_ERR_UNK_CONN_ID;
-    evt.connection_handle = 2;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
-    memcpy(evt.peer_addr, peer_addr, 6);
     rc = ble_gap_rx_conn_complete(&evt);
     TEST_ASSERT(rc == 0);
     TEST_ASSERT(!ble_gap_master_in_progress());
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_CANCEL);
+}
+
+static void
+ble_gap_test_util_conn_and_cancel(uint8_t *peer_addr, uint8_t hci_status)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Begin creating a connection. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(ble_gap_master_in_progress());
+
+    /* Initiate cancel procedure. */
+    ble_gap_test_util_conn_cancel(hci_status);
     TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
 }
 
@@ -1020,7 +949,7 @@ TEST_CASE(ble_gap_test_case_conn_cancel_good)
 {
     uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
 
-    ble_gap_test_util_conn_cancel(peer_addr, 0);
+    ble_gap_test_util_conn_and_cancel(peer_addr, 0);
 
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_CANCEL);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == BLE_HS_CONN_HANDLE_NONE);
@@ -1033,7 +962,7 @@ TEST_CASE(ble_gap_test_case_conn_cancel_ctlr_fail)
 
     uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
 
-    ble_gap_test_util_conn_cancel(peer_addr, BLE_ERR_REPEATED_ATTEMPTS);
+    ble_gap_test_util_conn_and_cancel(peer_addr, BLE_ERR_REPEATED_ATTEMPTS);
 
     /* Make sure the host didn't invoke the application callback.  The cancel
      * failure was indicated via the return code from the gap call.
@@ -1583,7 +1512,8 @@ TEST_CASE(ble_gap_test_case_adv_ctlr_fail)
                                   peer_addr, c, d, BLE_ERR_DIR_ADV_TMO, -1, 0);
 
             TEST_ASSERT(!ble_gap_adv_active());
-            TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_ADV_COMPLETE);
+            TEST_ASSERT(ble_gap_test_conn_event_type ==
+                        BLE_GAP_EVENT_ADV_COMPLETE);
             TEST_ASSERT(ble_gap_test_conn_desc.conn_handle ==
                         BLE_HS_CONN_HANDLE_NONE);
             TEST_ASSERT(ble_gap_test_conn_arg == NULL);
@@ -2280,6 +2210,259 @@ TEST_SUITE(ble_gap_test_suite_update_conn)
 }
 
 /*****************************************************************************
+ * $timeout                                                                  *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_conn_forever(void)
+{
+    int32_t ticks_from_now;
+
+    /* Initiate a connect procedure with no timeout. */
+    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                             BLE_ADDR_TYPE_PUBLIC,
+                             ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), BLE_HS_FOREVER,
+                             NULL, ble_gap_test_util_connect_cb,
+                             NULL, 0);
+
+    /* Ensure no pending GAP event. */
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Advance 100 seconds; ensure no timeout reported. */
+    os_time_advance(100 * OS_TICKS_PER_SEC);
+    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+    TEST_ASSERT(ble_gap_conn_active());
+}
+
+static void
+ble_gap_test_util_conn_timeout(int32_t duration_ms)
+{
+    struct hci_le_conn_complete evt;
+    uint32_t duration_ticks;
+    int32_t ticks_from_now;
+    int rc;
+
+    TEST_ASSERT_FATAL(duration_ms != BLE_HS_FOREVER);
+
+    /* Initiate a connect procedure with the specified timeout. */
+    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                             BLE_ADDR_TYPE_PUBLIC,
+                             ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), duration_ms,
+                             NULL, ble_gap_test_util_connect_cb,
+                             NULL, 0);
+
+    /* Ensure next GAP event is at the expected time. */
+    rc = os_time_ms_to_ticks(duration_ms, &duration_ticks);
+    TEST_ASSERT_FATAL(rc == 0);
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == duration_ticks);
+
+    /* Advance duration ms; ensure timeout event does not get reported before
+     * connection complete event rxed.
+     */
+    os_time_advance(duration_ms);
+
+    ble_hs_test_util_set_ack(
+        host_hci_opcode_join(BLE_HCI_OGF_LE,
+                             BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
+        0);
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Ensure cancel create connection command was sent. */
+    ble_hs_test_util_verify_tx_create_conn_cancel();
+
+    /* Ensure timer has been stopped. */
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Receive the connection complete event indicating a successful cancel. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_UNK_CONN_ID;
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure the GAP event was triggered. */
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
+    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_ETIMEOUT);
+
+    /* Clear GAP event for remainder of test. */
+    ble_gap_test_util_reset_cb_info();
+}
+
+static void
+ble_gap_test_util_disc_forever(void)
+{
+    struct ble_gap_disc_params params;
+    int32_t ticks_from_now;
+
+    memset(&params, 0, sizeof params);
+
+    /* Initiate a discovery procedure with no timeout. */
+    ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC,
+                          BLE_HS_FOREVER, &params, ble_gap_test_util_disc_cb,
+                          NULL, -1, 0);
+
+    /* Ensure no pending GAP event. */
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Advance 100 seconds; ensure no timeout reported. */
+    os_time_advance(100 * OS_TICKS_PER_SEC);
+    TEST_ASSERT(ble_gap_test_disc_event_type == -1);
+    TEST_ASSERT(ble_gap_disc_active());
+}
+
+static void
+ble_gap_test_util_disc_timeout(int32_t duration_ms)
+{
+    struct ble_gap_disc_params params;
+    uint32_t duration_ticks;
+    int32_t ticks_from_now;
+    int rc;
+
+    TEST_ASSERT_FATAL(duration_ms != BLE_HS_FOREVER);
+
+    memset(&params, 0, sizeof params);
+
+    /* Initiate a discovery procedure with the specified timeout. */
+    ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC,
+                          duration_ms, &params, ble_gap_test_util_disc_cb,
+                          NULL, -1, 0);
+
+    /* Ensure next GAP event is at the expected time. */
+    rc = os_time_ms_to_ticks(duration_ms, &duration_ticks);
+    TEST_ASSERT_FATAL(rc == 0);
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == duration_ticks);
+
+    /* Advance duration ms; ensure timeout event was reported. */
+    os_time_advance(duration_ms);
+
+    ble_hs_test_util_set_ack(
+        host_hci_opcode_join(BLE_HCI_OGF_LE,
+                             BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+        0);
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    TEST_ASSERT(ble_gap_test_disc_event_type == BLE_GAP_EVENT_DISC_COMPLETE);
+
+    /* Clear GAP event for remainder of test. */
+    ble_gap_test_util_reset_cb_info();
+}
+
+TEST_CASE(ble_gap_test_case_conn_timeout_conn_forever)
+{
+    ble_gap_test_util_init();
+
+    /* 3 ms. */
+    ble_gap_test_util_conn_timeout(3);
+
+    /* No timeout. */
+    ble_gap_test_util_conn_forever();
+
+}
+
+TEST_CASE(ble_gap_test_case_conn_timeout_conn_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* 30 ms. */
+    ble_gap_test_util_conn_timeout(30);
+
+    /* 5 ms. */
+    ble_gap_test_util_conn_timeout(5);
+
+}
+
+TEST_CASE(ble_gap_test_case_conn_forever_conn_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* No timeout. */
+    ble_gap_test_util_conn_forever();
+
+    /* Cancel connect procedure manually. */
+    ble_gap_test_util_conn_cancel(0);
+
+    /* Clear GAP event for remainder of test. */
+    ble_gap_test_util_reset_cb_info();
+
+    /* 3 ms. */
+    ble_gap_test_util_conn_timeout(3);
+}
+
+TEST_CASE(ble_gap_test_case_disc_timeout_disc_forever)
+{
+    ble_gap_test_util_init();
+
+    /* 3 ms. */
+    ble_gap_test_util_disc_timeout(3);
+
+    /* No timeout. */
+    ble_gap_test_util_disc_forever();
+
+}
+
+TEST_CASE(ble_gap_test_case_disc_timeout_disc_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* 30 ms. */
+    ble_gap_test_util_disc_timeout(30);
+
+    /* 5 ms. */
+    ble_gap_test_util_disc_timeout(5);
+
+}
+
+TEST_CASE(ble_gap_test_case_disc_forever_disc_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* No timeout. */
+    ble_gap_test_util_disc_forever();
+
+    /* Cancel discovery procedure manually. */
+    ble_hs_test_util_disc_cancel(0);
+
+    /* 3 ms. */
+    ble_gap_test_util_disc_timeout(3);
+}
+
+TEST_CASE(ble_gap_test_case_conn_timeout_disc_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* 15 seconds. */
+    ble_gap_test_util_conn_timeout(15000);
+
+    /* 1285 ms. */
+    ble_gap_test_util_disc_timeout(1285);
+}
+
+TEST_SUITE(ble_gap_test_suite_timeout)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_conn_timeout_conn_forever();
+    ble_gap_test_case_conn_timeout_conn_timeout();
+    ble_gap_test_case_conn_forever_conn_timeout();
+
+    ble_gap_test_case_disc_timeout_disc_forever();
+    ble_gap_test_case_disc_timeout_disc_timeout();
+    ble_gap_test_case_disc_forever_disc_timeout();
+
+    ble_gap_test_case_conn_timeout_disc_timeout();
+}
+
+/*****************************************************************************
  * $all                                                                      *
  *****************************************************************************/
 
@@ -2295,6 +2478,7 @@ ble_gap_test_all(void)
     ble_gap_test_suite_adv();
     ble_gap_test_suite_stop_adv();
     ble_gap_test_suite_update_conn();
+    ble_gap_test_suite_timeout();
 
     return tu_any_failed;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/db980b24/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 2fbe0ef..1a6dd5e 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -408,6 +408,70 @@ ble_hs_test_util_create_conn(uint16_t handle, uint8_t *peer_id_addr,
                                      null_addr, cb, cb_arg);
 }
 
+static void
+ble_hs_test_util_conn_params_dflt(struct ble_gap_conn_params *conn_params)
+{
+    conn_params->scan_itvl = 0x0010;
+    conn_params->scan_window = 0x0010;
+    conn_params->itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN;
+    conn_params->itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX;
+    conn_params->latency = BLE_GAP_INITIAL_CONN_LATENCY;
+    conn_params->supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT;
+    conn_params->min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
+    conn_params->max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
+}
+
+static void
+ble_hs_test_util_hcc_from_conn_params(
+    struct hci_create_conn *hcc, uint8_t own_addr_type, uint8_t peer_addr_type,
+    const uint8_t *peer_addr, const struct ble_gap_conn_params *conn_params)
+{
+    hcc->scan_itvl = conn_params->scan_itvl;
+    hcc->scan_window = conn_params->scan_window;
+
+    if (peer_addr_type == BLE_GAP_ADDR_TYPE_WL) {
+        hcc->filter_policy = BLE_HCI_CONN_FILT_USE_WL;
+        hcc->peer_addr_type = 0;
+        memset(hcc->peer_addr, 0, 6);
+    } else {
+        hcc->filter_policy = BLE_HCI_CONN_FILT_NO_WL;
+        hcc->peer_addr_type = peer_addr_type;
+        memcpy(hcc->peer_addr, peer_addr, 6);
+    }
+    hcc->own_addr_type = own_addr_type;
+    hcc->conn_itvl_min = conn_params->itvl_min;
+    hcc->conn_itvl_max = conn_params->itvl_max;
+    hcc->conn_latency = conn_params->latency;
+    hcc->supervision_timeout = conn_params->supervision_timeout;
+    hcc->min_ce_len = conn_params->min_ce_len;
+    hcc->max_ce_len = conn_params->max_ce_len;
+}
+
+void
+ble_hs_test_util_verify_tx_create_conn(const struct hci_create_conn *exp)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_CREATE_CONN,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_CREATE_CONN_LEN);
+
+    TEST_ASSERT(le16toh(param + 0) == exp->scan_itvl);
+    TEST_ASSERT(le16toh(param + 2) == exp->scan_window);
+    TEST_ASSERT(param[4] == exp->filter_policy);
+    TEST_ASSERT(param[5] == exp->peer_addr_type);
+    TEST_ASSERT(memcmp(param + 6, exp->peer_addr, 6) == 0);
+    TEST_ASSERT(param[12] == exp->own_addr_type);
+    TEST_ASSERT(le16toh(param + 13) == exp->conn_itvl_min);
+    TEST_ASSERT(le16toh(param + 15) == exp->conn_itvl_max);
+    TEST_ASSERT(le16toh(param + 17) == exp->conn_latency);
+    TEST_ASSERT(le16toh(param + 19) == exp->supervision_timeout);
+    TEST_ASSERT(le16toh(param + 21) == exp->min_ce_len);
+    TEST_ASSERT(le16toh(param + 23) == exp->max_ce_len);
+}
+
 int
 ble_hs_test_util_connect(uint8_t own_addr_type, uint8_t peer_addr_type,
                          const uint8_t *peer_addr, int32_t duration_ms,
@@ -415,8 +479,16 @@ ble_hs_test_util_connect(uint8_t own_addr_type, uint8_t peer_addr_type,
                          ble_gap_event_fn *cb, void *cb_arg,
                          uint8_t ack_status)
 {
+    struct ble_gap_conn_params dflt_params;
+    struct hci_create_conn hcc;
     int rc;
 
+    /* This function ensures the most recently sent HCI command is the expected
+     * create connection command.  If the current test case has unverified HCI
+     * commands, assume we are not interested in them and clear the queue.
+     */
+    ble_hs_test_util_prev_hci_tx_clear();
+
     ble_hs_test_util_set_ack(
         host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN),
         ack_status);
@@ -426,6 +498,15 @@ ble_hs_test_util_connect(uint8_t own_addr_type, uint8_t peer_addr_type,
 
     TEST_ASSERT(rc == BLE_HS_HCI_ERR(ack_status));
 
+    if (params == NULL) {
+        ble_hs_test_util_conn_params_dflt(&dflt_params);
+        params = &dflt_params;
+    }
+
+    ble_hs_test_util_hcc_from_conn_params(&hcc, own_addr_type,
+                                          peer_addr_type, peer_addr, params);
+    ble_hs_test_util_verify_tx_create_conn(&hcc);
+
     return rc;
 }
 
@@ -443,6 +524,23 @@ ble_hs_test_util_conn_cancel(uint8_t ack_status)
     return rc;
 }
 
+void
+ble_hs_test_util_conn_cancel_full(void)
+{
+    struct hci_le_conn_complete evt;
+    int rc;
+
+    ble_hs_test_util_conn_cancel(0);
+
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_UNK_CONN_ID;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
+
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
 int
 ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/db980b24/net/nimble/host/src/test/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.h b/net/nimble/host/src/test/ble_hs_test_util.h
index b663ce2..9c4b982 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -27,6 +27,7 @@
 struct ble_hs_conn;
 struct ble_l2cap_chan;
 struct hci_disconn_complete;
+struct hci_create_conn;
 
 extern struct os_eventq ble_hs_test_util_evq;
 extern const struct ble_gap_adv_params ble_hs_test_util_adv_params;
@@ -79,6 +80,7 @@ int ble_hs_test_util_connect(uint8_t own_addr_type,
                                    void *cb_arg,
                                    uint8_t ack_status);
 int ble_hs_test_util_conn_cancel(uint8_t ack_status);
+void ble_hs_test_util_conn_cancel_full(void);
 int ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status);
 void ble_hs_test_util_conn_disconnect(uint16_t conn_handle);
 int ble_hs_test_util_exp_hci_status(int cmd_idx, int fail_idx,
@@ -88,6 +90,7 @@ int ble_hs_test_util_disc(uint8_t own_addr_type, int32_t duration_ms,
                           ble_gap_event_fn *cb, void *cb_arg, int fail_idx,
                           uint8_t fail_status);
 int ble_hs_test_util_disc_cancel(uint8_t ack_status);
+void ble_hs_test_util_verify_tx_create_conn(const struct hci_create_conn *exp);
 int ble_hs_test_util_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
                                     uint8_t hci_status);
 int ble_hs_test_util_adv_start(uint8_t own_addr_type,


[13/16] incubator-mynewt-core git commit: MYNEWT-342: Fix invalid access address generation

Posted by st...@apache.org.
MYNEWT-342: Fix invalid access address generation


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

Branch: refs/heads/sterly_refactor
Commit: 37f8bb58ddf0b9aef5c180543b5f1e1b1f3ad127
Parents: cf6c3da
Author: William San Filippo <wi...@runtime.io>
Authored: Tue Jul 26 10:44:32 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

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


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/37f8bb58/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 ca94310..f711dfb 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -462,8 +462,8 @@ ble_ll_conn_calc_access_addr(void)
         transitions = 0;
         consecutive = 0;
         mask = 0x00000001;
-        prev_bit = aa & mask;
         while (mask < 0x80000000) {
+            prev_bit = aa & mask;
             mask <<= 1;
             if (mask & aa) {
                 if (prev_bit == 0) {


[11/16] incubator-mynewt-core git commit: Sim compiler - don't generate .bin file.

Posted by st...@apache.org.
Sim compiler - don't generate .bin file.

This file is unnecessary for sim targets, and the user's machine may not
have [g]objcopy installed.


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

Branch: refs/heads/sterly_refactor
Commit: cf6c3da00774da7ad02359bae44d8df47ac61a0d
Parents: c092a8a
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Jul 28 12:49:22 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

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


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf6c3da0/compiler/sim/compiler.yml
----------------------------------------------------------------------
diff --git a/compiler/sim/compiler.yml b/compiler/sim/compiler.yml
index eaeccec..0eba296 100644
--- a/compiler/sim/compiler.yml
+++ b/compiler/sim/compiler.yml
@@ -31,6 +31,7 @@ compiler.ld.resolve_circular_deps: true
 compiler.flags.default: [compiler.flags.base, -O1]
 compiler.flags.debug: [compiler.flags.base, -O0]
 compiler.ld.mapfile: false
+compiler.ld.binfile: false
 
 # Linux.
 compiler.flags.base.LINUX: >


[05/16] incubator-mynewt-core git commit: newtmgr_ble - Update for latest NimBLE host API.

Posted by st...@apache.org.
newtmgr_ble - Update for latest NimBLE host API.


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

Branch: refs/heads/sterly_refactor
Commit: 39bc0593ddc973bca4b89e66b53999001acb0e6d
Parents: 83d62d2
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jul 27 17:40:49 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr/transport/ble/src/newtmgr_ble.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39bc0593/libs/newtmgr/transport/ble/src/newtmgr_ble.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/transport/ble/src/newtmgr_ble.c b/libs/newtmgr/transport/ble/src/newtmgr_ble.c
index dbd9f12..716a974 100644
--- a/libs/newtmgr/transport/ble/src/newtmgr_ble.c
+++ b/libs/newtmgr/transport/ble/src/newtmgr_ble.c
@@ -171,10 +171,11 @@ nmgr_ble_proc_mq_evt(struct os_event *ev)
                 if (!m_resp) {
                     break;
                 }
+                assert(OS_MBUF_USRHDR_LEN(m_resp) >= sizeof (conn_handle));
                 memcpy(&conn_handle, OS_MBUF_USRHDR(m_resp),
                        sizeof (conn_handle));
                 ble_gattc_notify_custom(conn_handle, g_ble_nmgr_attr_handle,
-                                        &m_resp);
+                                        m_resp);
             }
             break;
 


[08/16] incubator-mynewt-core git commit: bleuart - Use latest NimBLE host API.

Posted by st...@apache.org.
bleuart - Use latest NimBLE host API.


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

Branch: refs/heads/sterly_refactor
Commit: 3da7f49df145a52d4470c9cf7c28d7adc0173bd0
Parents: ee4eaac
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jul 27 19:10:45 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

----------------------------------------------------------------------
 libs/bleuart/src/bleuart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3da7f49d/libs/bleuart/src/bleuart.c
----------------------------------------------------------------------
diff --git a/libs/bleuart/src/bleuart.c b/libs/bleuart/src/bleuart.c
index ae0673d..81e433c 100644
--- a/libs/bleuart/src/bleuart.c
+++ b/libs/bleuart/src/bleuart.c
@@ -168,7 +168,7 @@ bleuart_uart_read(void)
             return;
         }
         ble_gattc_notify_custom(g_console_conn_handle,
-                                g_bleuart_attr_read_handle, &om);
+                                g_bleuart_attr_read_handle, om);
         off = 0;
         break;
     }


[04/16] incubator-mynewt-core git commit: BLE ctlr - Use updated host rx data API.

Posted by st...@apache.org.
BLE ctlr - Use updated host rx data API.


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

Branch: refs/heads/sterly_refactor
Commit: 83d62d26542621eb6061b371e7ad61272c0a0966
Parents: 40695b3
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jul 27 18:52:39 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jul 31 21:58:57 2016 -0700

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


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/83d62d26/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 811b51f..ca94310 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -104,7 +104,7 @@ extern void bletest_completed_pkt(uint16_t handle);
  */
 
 /* XXX: this does not belong here! Move to transport? */
-extern int ble_hs_rx_data(struct os_mbuf **om);
+extern int ble_hs_rx_data(struct os_mbuf *om);
 
 /*
  * The amount of time that we will wait to hear the start of a receive
@@ -2569,7 +2569,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
                     acl_hdr = (acl_hdr << 12) | connsm->conn_handle;
                     htole16(rxbuf, acl_hdr);
                     htole16(rxbuf + 2, acl_len);
-                    ble_hs_rx_data(&rxpdu);
+                    ble_hs_rx_data(rxpdu);
                 }
 
                 /* NOTE: we dont free the mbuf since we handed it off! */