Merge branch 'develop'
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index ce66a8e..a9cde09 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -891,7 +891,7 @@
 
 #define CMD_ADV_DATA_MAX_UUIDS16                8
 #define CMD_ADV_DATA_MAX_UUIDS32                8
-#define CMD_ADV_DATA_MAX_UUIDS128               8
+#define CMD_ADV_DATA_MAX_UUIDS128               2
 #define CMD_ADV_DATA_MAX_PUBLIC_TGT_ADDRS       8
 #define CMD_ADV_DATA_SVC_DATA_UUID16_MAX_LEN    32
 #define CMD_ADV_DATA_SVC_DATA_UUID32_MAX_LEN    32
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 7e92e0e..caa5d4a 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -53,7 +53,7 @@
 
 #define SHELL_TASK_PRIO         (3)
 #define SHELL_MAX_INPUT_LEN     (64)
-#define SHELL_TASK_STACK_SIZE   (OS_STACK_ALIGN(216))
+#define SHELL_TASK_STACK_SIZE   (OS_STACK_ALIGN(210))
 static bssnz_t os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
 
 static struct os_mutex bletiny_mutex;
@@ -65,7 +65,6 @@
 uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
 
 /* A buffer for host advertising data */
-uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
 uint8_t g_host_adv_len;
 
 static uint8_t bletiny_addr[6] = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00};
@@ -82,7 +81,7 @@
 struct os_mempool default_mbuf_mpool;
 
 /* BLETINY variables */
-#define BLETINY_STACK_SIZE             (OS_STACK_ALIGN(200))
+#define BLETINY_STACK_SIZE             (OS_STACK_ALIGN(210))
 #define BLETINY_TASK_PRIO              (HOST_TASK_PRIO + 1)
 
 #if NIMBLE_OPT_ROLE_CENTRAL
diff --git a/hw/bsp/nrf51dk-16kbram/boot-nrf51dk-16kbram.ld b/hw/bsp/nrf51dk-16kbram/boot-nrf51dk-16kbram.ld
index 8643065..ce95980 100755
--- a/hw/bsp/nrf51dk-16kbram/boot-nrf51dk-16kbram.ld
+++ b/hw/bsp/nrf51dk-16kbram/boot-nrf51dk-16kbram.ld
@@ -19,7 +19,7 @@
 
 MEMORY
 {
-  FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x7000
+  FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x8000
   RAM (rwx) :  ORIGIN = 0x20000000, LENGTH = 0x4000
 }
 
diff --git a/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram.ld b/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram.ld
index 8f588da..043af42 100755
--- a/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram.ld
+++ b/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram.ld
@@ -19,7 +19,7 @@
 
 MEMORY
 {
-  FLASH (rx) : ORIGIN = 0x00007000, LENGTH = 0x1c000
+  FLASH (rx) : ORIGIN = 0x00008000, LENGTH = 0x1b800
   RAM (rwx) :  ORIGIN = 0x20000000, LENGTH = 0x4000
 }
 
diff --git a/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram_download.sh b/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram_download.sh
index 5ce4bfa..46b2481 100755
--- a/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram_download.sh
+++ b/hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram_download.sh
@@ -48,7 +48,7 @@
     FLASH_OFFSET=0x0
     FILE_NAME=$BASENAME.elf.bin
 else
-    FLASH_OFFSET=0x7000
+    FLASH_OFFSET=0x8000
     FILE_NAME=$BASENAME.img
 fi
 
diff --git a/hw/bsp/nrf51dk-16kbram/src/os_bsp.c b/hw/bsp/nrf51dk-16kbram/src/os_bsp.c
index e09bdee..906bc81 100644
--- a/hw/bsp/nrf51dk-16kbram/src/os_bsp.c
+++ b/hw/bsp/nrf51dk-16kbram/src/os_bsp.c
@@ -28,17 +28,17 @@
     [FLASH_AREA_BOOTLOADER] = {
         .fa_flash_id = 0,       /* internal flash */
         .fa_off = 0x00000000,   /* beginning */
-        .fa_size = (28 * 1024)
+        .fa_size = (32 * 1024)
     },
     [FLASH_AREA_IMAGE_0] = {
         .fa_flash_id = 0,
-        .fa_off = 0x00007000,
-        .fa_size = (112 * 1024)
+        .fa_off = 0x00008000,
+        .fa_size = (110 * 1024)
     },
     [FLASH_AREA_IMAGE_1] = {
         .fa_flash_id = 0,
-        .fa_off = 0x00023000,
-        .fa_size = (112 * 1024)
+        .fa_off = 0x00023800,
+        .fa_size = (110 * 1024)
     },
     [FLASH_AREA_IMAGE_SCRATCH] = {
         .fa_flash_id = 0,
diff --git a/hw/mcu/native/src/hal_gpio.c b/hw/mcu/native/src/hal_gpio.c
index 7ab2d0f..677b738 100644
--- a/hw/mcu/native/src/hal_gpio.c
+++ b/hw/mcu/native/src/hal_gpio.c
@@ -56,7 +56,7 @@
         return -1;
     }
     hal_gpio[pin].dir = OUTPUT;
-    hal_gpio[pin].val = (val != 0);
+    hal_gpio_write(pin,val);
     return 0;
 }
 
@@ -81,6 +81,8 @@
         return;
     }
     hal_gpio[pin].val = (val != 0);
+    printf("hal_gpio set pin %2d to %1d\r", pin, hal_gpio[pin].val); 
+    fflush(stdout);
 }
 
 int
diff --git a/libs/json/include/json/json.h b/libs/json/include/json/json.h
index 9f8e29c..668b7b1 100644
--- a/libs/json/include/json/json.h
+++ b/libs/json/include/json/json.h
@@ -189,8 +189,14 @@
 };
 
 struct json_buffer;
+
+/* when you implement a json buffer, you must implement these functions */
+
+/* returns the next character in the buffer or '\0'*/
 typedef char (*json_buffer_read_next_byte_t)(struct json_buffer *);
+/* returns the previous character in the buffer or '\0' */
 typedef char (*json_buffer_read_prev_byte_t)(struct json_buffer *);
+/* returns the number of characters read or zero */
 typedef int (*json_buffer_readn_t)(struct json_buffer *, char *buf, int n);
 
 struct json_buffer {
diff --git a/libs/json/pkg.yml b/libs/json/pkg.yml
index 4944622..3dc0be6 100644
--- a/libs/json/pkg.yml
+++ b/libs/json/pkg.yml
@@ -22,5 +22,7 @@
 pkg.author: "Apache Mynewt <dev@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
+pkg.deps.TEST:
+   - libs/testutil
 
 pkg.cflags.float_user: -DFLOAT_SUPPORT
diff --git a/libs/json/src/test/test_json.c b/libs/json/src/test/test_json.c
new file mode 100644
index 0000000..80e9abe
--- /dev/null
+++ b/libs/json/src/test/test_json.c
@@ -0,0 +1,41 @@
+/**
+ * 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 <testutil/testutil.h>
+#include <test_json.h>
+
+
+TEST_SUITE(test_json_suite) {
+    test_json_simple_encode();
+    test_json_simple_decode();
+}
+
+#ifdef MYNEWT_SELFTEST
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    test_json_suite();
+
+    return tu_any_failed;
+}
+
+#endif
diff --git a/libs/json/src/test/test_json.h b/libs/json/src/test/test_json.h
new file mode 100644
index 0000000..ea5efcf
--- /dev/null
+++ b/libs/json/src/test/test_json.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 TEST_JSON_H
+#define TEST_JSON_H
+
+TEST_CASE_DECL(test_json_simple_encode);
+TEST_CASE_DECL(test_json_simple_decode);
+
+#endif /* TEST_JSON_H */
+
diff --git a/libs/json/src/test/test_json_simple.c b/libs/json/src/test/test_json_simple.c
new file mode 100644
index 0000000..ab2d005
--- /dev/null
+++ b/libs/json/src/test/test_json_simple.c
@@ -0,0 +1,205 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "test_json.h"
+#include "json/json.h"
+
+static char *output = "{\"KeyBool\": true,\"KeyInt\": -1234,\"KeyUint\": 1353214,\"KeyString\": \"foobar\",\"KeyStringN\": \"foobarlong\"}";
+static char bigbuf[512];
+static int buf_index;
+
+static int test_write(void *buf, char* data, int len) {
+    int i;
+    for(i = 0; i < len; i++) {
+        bigbuf[buf_index++] = data[i];        
+    }
+    return len;
+}
+
+TEST_CASE(test_json_simple_encode){
+    struct json_encoder encoder;
+    struct json_value value;
+    int rc;
+    
+    /* reset the state of the internal test */
+    buf_index = 0;    
+    memset(&encoder, 0, sizeof(encoder));
+    
+    encoder.je_write = test_write;
+    encoder.je_arg= NULL;
+    
+    rc = json_encode_object_start(&encoder);    
+    TEST_ASSERT(rc == 0);
+    
+    JSON_VALUE_BOOL(&value, 1);
+    rc = json_encode_object_entry(&encoder, "KeyBool", &value);
+    TEST_ASSERT(rc == 0);
+    
+    JSON_VALUE_INT(&value, -1234);
+    rc = json_encode_object_entry(&encoder, "KeyInt", &value);
+    TEST_ASSERT(rc == 0);    
+
+    JSON_VALUE_UINT(&value, 1353214);
+    rc = json_encode_object_entry(&encoder, "KeyUint", &value);
+    TEST_ASSERT(rc == 0);    
+    
+    JSON_VALUE_STRING(&value, "foobar");
+    rc = json_encode_object_entry(&encoder, "KeyString", &value);
+    TEST_ASSERT(rc == 0);    
+    
+    /* we'll decode later differently */
+    JSON_VALUE_STRINGN(&value, "foobarlongstring", 10);
+    rc = json_encode_object_entry(&encoder, "KeyStringN", &value);
+    TEST_ASSERT(rc == 0);  
+    
+    rc = json_encode_object_finish(&encoder);    
+    TEST_ASSERT(rc == 0);
+    
+    /* does it match what we expect it to */
+    rc = strcmp(bigbuf, output);
+    TEST_ASSERT(rc == 0);
+}
+
+
+/* a test structure to hold the json flat buffer and pass bytes
+ * to the decoder */
+struct test_jbuf {
+    /* json_buffer must be first element in the structure */
+    struct json_buffer json_buf;
+    char * start_buf;
+    char * end_buf;
+    int current_position;
+};
+
+
+static char 
+test_jbuf_read_next(struct json_buffer *jb) {
+    char c;
+    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;    
+    
+    if((ptjb->start_buf + ptjb->current_position) <= ptjb->end_buf) {
+        c = *(ptjb->start_buf + ptjb->current_position);
+        ptjb->current_position++;
+        return c;
+    }
+    return '\0';    
+}
+
+/* this goes backward in the buffer one character */
+static char 
+test_jbuf_read_prev(struct json_buffer *jb) {
+    char c;
+    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;
+    if(ptjb->current_position) {
+       ptjb->current_position--; 
+       c = *(ptjb->start_buf + ptjb->current_position);
+       return c;
+    }
+    
+    /* can't rewind */
+    return '\0';
+    
+}
+
+static int 
+test_jbuf_readn(struct json_buffer *jb, char *buf, int size) {
+    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;    
+    
+    if((ptjb->end_buf - (ptjb->start_buf + ptjb->current_position) + 1) >= size) {
+        memcpy(buf, ptjb->start_buf + ptjb->current_position, size);
+        ptjb->current_position += size;
+        return 0;
+    }
+    return -1;
+}
+
+static void 
+test_buf_init(struct test_jbuf *ptjb, char *string) {
+    /* initialize the decode */
+    ptjb->json_buf.jb_read_next = test_jbuf_read_next;
+    ptjb->json_buf.jb_read_prev = test_jbuf_read_prev;
+    ptjb->json_buf.jb_readn = test_jbuf_readn;
+    ptjb->start_buf = string;
+    ptjb->end_buf = string + strlen(string);  
+    /* end buf points to the NULL */
+    ptjb->current_position = 0;
+}
+
+/* now test the decode on a string */
+TEST_CASE(test_json_simple_decode){
+    struct test_jbuf tjb; 
+    unsigned int uint_val;
+    int int_val;
+    bool bool_val;
+    char string1[16];
+    char string2[16];
+    int rc;
+
+    const struct json_attr_t test_attr[5] = {
+        [0] = {
+            .attribute = "KeyBool",
+            .type = t_boolean,
+            .addr.boolean = &bool_val,
+            .nodefault = true
+        },
+        [1] = {
+            .attribute = "KeyInt",
+            .type = t_integer,
+            .addr.integer = &int_val,
+            .nodefault = true
+            },
+        [2] = {
+            .attribute = "KeyUint",
+            .type = t_uinteger,
+            .addr.uinteger = &uint_val,
+            .nodefault = true
+            },
+        [3] = {
+            .attribute = "KeyString",
+            .type = t_string,
+            .addr.string = string1,
+            .nodefault = true,
+            .len = sizeof(string1)
+            },
+        [4] = {
+            .attribute = "KeyStringN",
+            .type = t_string,
+            .addr.string = string2,
+            .nodefault = true,
+            .len = sizeof(string2)                    
+            }
+    };    
+    
+    test_buf_init(&tjb, output);
+
+    rc = json_read_object(&tjb.json_buf, test_attr);
+    TEST_ASSERT(rc==0);    
+    TEST_ASSERT(bool_val == 1);
+    TEST_ASSERT(int_val ==  -1234);
+    TEST_ASSERT(uint_val == 1353214);
+    
+    rc = memcmp(string1, "foobar", strlen("foobar"));
+    TEST_ASSERT(rc==0);    
+   
+    rc = memcmp(string2, "foobarlongstring", 10);
+    TEST_ASSERT(rc==0);            
+}
\ No newline at end of file
diff --git a/libs/os/src/arch/sim/os_arch_sim.c b/libs/os/src/arch/sim/os_arch_sim.c
index 94182a8..d8c1b83 100644
--- a/libs/os/src/arch/sim/os_arch_sim.c
+++ b/libs/os/src/arch/sim/os_arch_sim.c
@@ -181,8 +181,6 @@
 void
 os_arch_idle(void)
 {
-    assert(!os_arch_in_critical());
-
     sigsuspend(&nosigs);        /* Wait for a signal to wake us up */
 }
 
diff --git a/libs/testutil/include/testutil/testutil.h b/libs/testutil/include/testutil/testutil.h
index 2868cdd..2c8000a 100644
--- a/libs/testutil/include/testutil/testutil.h
+++ b/libs/testutil/include/testutil/testutil.h
@@ -98,6 +98,10 @@
     static void                                                               \
     TEST_SUITE_##suite_name(void)
 
+/* for creating multiple files with test cases all belonging to the same
+ * suite */
+#define TEST_CASE_DECL(case_name)  int case_name(void);
+
 #define TEST_CASE(case_name)                                                  \
     static void TEST_CASE_##case_name(void);                                  \
                                                                               \
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
index d520427..f210dac 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -342,7 +342,7 @@
  * XXX: temporary LL debug log. Will get removed once we transition to real
  * log
  */ 
-#define BLE_LL_LOG
+#undef BLE_LL_LOG
 
 #define BLE_LL_LOG_ID_PHY_SETCHAN       (1)
 #define BLE_LL_LOG_ID_RX_START          (2)
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 678cde3..0681998 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -33,7 +33,7 @@
 #ifdef ARCH_sim
 #define BLE_HS_STACK_SIZE   (1024)
 #else
-#define BLE_HS_STACK_SIZE   (240)
+#define BLE_HS_STACK_SIZE   (250)
 #endif
 
 static struct log_handler ble_hs_log_console_handler;