From 8f2a825b5873d5e3c56bf4e4ff1d7e5bec71485b Mon Sep 17 00:00:00 2001
From: Hauke Petersen <hauke.petersen@fu-berlin.de>
Date: Fri, 19 Oct 2018 13:59:28 +0200
Subject: [PATCH] pkg/nimble: ipmrove host initialization

---
 pkg/nimble/contrib/controller_init.c     | 46 ---------------
 pkg/nimble/contrib/include/nimble_riot.h | 27 ++++++++-
 pkg/nimble/contrib/nimble_riot.c         | 71 ++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 49 deletions(-)
 delete mode 100644 pkg/nimble/contrib/controller_init.c
 create mode 100644 pkg/nimble/contrib/nimble_riot.c

diff --git a/pkg/nimble/contrib/controller_init.c b/pkg/nimble/contrib/controller_init.c
deleted file mode 100644
index a35a45c313..0000000000
--- a/pkg/nimble/contrib/controller_init.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (C) 2018 Freie Universität Berlin
- *
- * This file is subject to the terms and conditions of the GNU Lesser
- * General Public License v2.1. See the file LICENSE in the top level
- * directory for more details.
- */
-
-/**
- * @ingroup     pkg_nimble
- * @{
- *
- * @file
- * @brief       Initialization of the nimble controller
- *
- * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
- *
- * @}
- */
-
-#include "thread.h"
-#include "nimble_riot.h"
-
-#include "nimble/nimble_port.h"
-
-#ifdef CPU_FAM_NRF52
-#include "nrf_clock.h"
-#endif
-
-static char stack[THREAD_STACKSIZE_DEFAULT];
-
-void nimble_riot_controller_init(void)
-{
-#ifdef CPU_FAM_NRF52
-    clock_start_lf();
-#endif
-
-    /*
-     * Create task where NimBLE LL will run. This one is required as LL has its
-     * own event queue and should have highest priority.
-     */
-    thread_create(stack, sizeof(stack), NIMBLE_CONTROLLER_PRIO,
-                  THREAD_CREATE_STACKTEST,
-                  (thread_task_func_t)nimble_port_ll_task_func, NULL,
-                  "nimble_ctrl");
-}
diff --git a/pkg/nimble/contrib/include/nimble_riot.h b/pkg/nimble/contrib/include/nimble_riot.h
index 8336e90c08..a1f1992e8c 100644
--- a/pkg/nimble/contrib/include/nimble_riot.h
+++ b/pkg/nimble/contrib/include/nimble_riot.h
@@ -24,7 +24,7 @@ extern "C" {
 #endif
 
 /**
- * @brief   Define the priority used for NimBLE's controller thread
+ * @brief   Priority used for NimBLE's controller thread
  *
  * This should be as high as possible.
  */
@@ -33,9 +33,30 @@ extern "C" {
 #endif
 
 /**
- * @brief   Starts a thread running NimBLE's controller
+ * @brief   Stacksize used for NimBLE's controller thread
  */
-void nimble_riot_controller_init(void);
+#ifndef NIMBLE_CONTROLLER_STACKSIZE
+#define NIMBLE_CONTROLLER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
+#endif
+
+/**
+ * @brief   Priority used for NimBLE's host thread
+ */
+#ifndef NIMBLE_HOST_PRIO
+#define NIMBLE_HOST_PRIO            (THREAD_PRIORITY_MAIN - 2)
+#endif
+
+/**
+ * @brief   Stacksize used for NimBLE's host thread
+ */
+#ifndef NIMBLE_HOST_STACKSIZE
+#define NIMBLE_HOST_STACKSIZE       (THREAD_STACKSIZE_DEFAULT)
+#endif
+
+/**
+ * @brief   Setup and run NimBLE's controller and host threads
+ */
+void nimble_riot_init(void);
 
 #ifdef __cplusplus
 }
diff --git a/pkg/nimble/contrib/nimble_riot.c b/pkg/nimble/contrib/nimble_riot.c
new file mode 100644
index 0000000000..2fb7efdcb5
--- /dev/null
+++ b/pkg/nimble/contrib/nimble_riot.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2018 Freie Universität Berlin
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @ingroup     pkg_nimble
+ * @{
+ *
+ * @file
+ * @brief       Glue code for running NimBLE for RIOT
+ *
+ * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
+ *
+ * @}
+ */
+
+#include "thread.h"
+#include "nimble_riot.h"
+
+#include "nimble/nimble_port.h"
+
+#include "services/gap/ble_svc_gap.h"
+#include "services/gatt/ble_svc_gatt.h"
+
+#ifdef CPU_FAM_NRF52
+#include "nrf_clock.h"
+#endif
+
+static char _stack_controller[NIMBLE_CONTROLLER_STACKSIZE];
+static char _stack_host[NIMBLE_HOST_STACKSIZE];
+
+static void *_host_thread(void *arg)
+{
+    (void)arg;
+
+    nimble_port_init();
+    nimble_port_run();
+
+    /* never reached */
+    return NULL;
+}
+
+void nimble_riot_init(void)
+{
+    /* XXX: NimBLE needs the nRF5x's LF clock to run */
+#ifdef CPU_FAM_NRF52
+    clock_start_lf();
+#endif
+
+    /* Run the controller
+     *
+     * Create task where NimBLE LL will run. This one is required as LL has its
+     * own event queue and should have highest priority.
+     */
+    thread_create(_stack_controller, sizeof(_stack_controller),
+                  NIMBLE_CONTROLLER_PRIO,
+                  THREAD_CREATE_STACKTEST,
+                  (thread_task_func_t)nimble_port_ll_task_func, NULL,
+                  "nimble_ctrl");
+
+    /* and finally initialize and run the host */
+    thread_create(_stack_host, sizeof(_stack_host),
+                  NIMBLE_HOST_PRIO,
+                  THREAD_CREATE_STACKTEST,
+                  _host_thread, NULL,
+                  "nimble_host");
+}
-- 
GitLab