You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by je...@apache.org on 2020/12/28 09:38:50 UTC

[incubator-nuttx] 02/06: bt_uart_shim: Support the multiple instances

This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit c0cd125bf37a4a30c0d67a1d4c4654fc0f54f54e
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Dec 27 17:16:21 2020 +0800

    bt_uart_shim: Support the multiple instances
    
    by removing the global variables(g_n)
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 drivers/wireless/bluetooth/bt_uart_shim.c | 45 ++++++++++++++++++-------------
 1 file changed, 27 insertions(+), 18 deletions(-)

diff --git a/drivers/wireless/bluetooth/bt_uart_shim.c b/drivers/wireless/bluetooth/bt_uart_shim.c
index 4c92a85..2d6146f 100644
--- a/drivers/wireless/bluetooth/bt_uart_shim.c
+++ b/drivers/wireless/bluetooth/bt_uart_shim.c
@@ -126,10 +126,6 @@ static struct btuart_lowerhalf_s g_lowerstatic =
   .rxdrain = hciuart_rxdrain
 };
 
-/* This is held global because its inconvenient to pass to the task */
-
-static FAR struct hciuart_config_s *g_n;
-
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -353,7 +349,12 @@ static ssize_t hciuart_rxdrain(FAR const struct btuart_lowerhalf_s *lower)
 
 static int hcicollecttask(int argc, FAR char **argv)
 {
-  FAR struct hciuart_state_s *s = &g_n->state;
+  FAR struct hciuart_config_s *n;
+  FAR struct hciuart_state_s *s;
+
+  n = (FAR struct hciuart_config_s *)
+    ((uintptr_t)strtoul(argv[1], NULL, 0));
+  s = &n->state;
 
   file_poll(&s->f, (struct pollfd *)&s->p, true);
 
@@ -390,19 +391,19 @@ static int hcicollecttask(int argc, FAR char **argv)
               /* We aren't expected to be listening, so drop these data */
 
               wlwarn("Dropping data\n");
-              hciuart_rxdrain(&g_n->lower);
+              hciuart_rxdrain(&n->lower);
             }
           else
             {
               if (s->callback != NULL)
                 {
                   wlinfo("Activating callback\n");
-                  s->callback(&g_n->lower, s->arg);
+                  s->callback(&n->lower, s->arg);
                 }
               else
                 {
                   wlwarn("Dropping data (no CB)\n");
-                  hciuart_rxdrain(&g_n->lower);
+                  hciuart_rxdrain(&n->lower);
                 }
             }
         }
@@ -432,32 +433,34 @@ static int hcicollecttask(int argc, FAR char **argv)
 
 FAR struct btuart_lowerhalf_s *bt_uart_shim_getdevice(FAR const char *path)
 {
+  FAR struct hciuart_config_s *n;
   FAR struct hciuart_state_s *s;
+  FAR char *argv[2];
+  char arg1[16];
   int ret;
 
   /* Get the memory for this shim instance */
 
-  g_n = (FAR struct hciuart_config_s *)
+  n = (FAR struct hciuart_config_s *)
     kmm_zalloc(sizeof(struct hciuart_config_s));
 
-  if (!g_n)
+  if (!n)
     {
-      return 0;
+      return NULL;
     }
 
-  s = &g_n->state;
+  s = &n->state;
 
   ret = file_open(&s->f, path, O_RDWR | O_BINARY);
   if (ret < 0)
     {
-      kmm_free(g_n);
-      g_n = 0;
-      return 0;
+      kmm_free(n);
+      return NULL;
     }
 
   /* Hook the routines in */
 
-  memcpy(&g_n->lower, &g_lowerstatic, sizeof(struct btuart_lowerhalf_s));
+  memcpy(&n->lower, &g_lowerstatic, sizeof(struct btuart_lowerhalf_s));
 
   /* Put materials into poll structure */
 
@@ -469,9 +472,15 @@ FAR struct btuart_lowerhalf_s *bt_uart_shim_getdevice(FAR const char *path)
 
   s->enabled = true;
 
+  /* Create the monitor thread */
+
+  snprintf(arg1, 16, "%p", n);
+  argv[0] = arg1;
+  argv[1] = NULL;
+
   s->serialmontask = kthread_create("BT HCI Rx",
                                     CONFIG_BLUETOOTH_TXCONN_PRIORITY,
-                                    1024, hcicollecttask, NULL);
+                                    1024, hcicollecttask, argv);
 
-  return (FAR struct btuart_lowerhalf_s *)g_n;
+  return (FAR struct btuart_lowerhalf_s *)n;
 }