chengkai15 commented on code in PR #3977:
URL: https://github.com/apache/nuttx-apps/pull/3977#discussion_r4582889945


##########
wireless/bluetooth/zblue/port/src/misc.c:
##########
@@ -0,0 +1,257 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/src/misc.c
+ *
+ * SPDX-License-Identifier: Apache-2.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <sys/random.h>
+#include <syslog.h>
+
+#include <zephyr/kernel.h>
+#include <zephyr/device.h>
+#include <zephyr/init.h>
+#include <zephyr/random/random.h>
+#include <zephyr/logging/log.h>
+
+#include "port.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* <zephyr/logging/log.h> replaced the syslog LOG_ERR priority */
+
+#define SYSLOG_ERR     3
+#define SYSLOG_WARNING 4
+#define SYSLOG_INFO    6
+#define SYSLOG_DEBUG   7
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const int g_syslog_prio[] =
+{
+  SYSLOG_ERR, SYSLOG_ERR, SYSLOG_WARNING, SYSLOG_INFO, SYSLOG_DEBUG
+};
+
+static const char g_level_tag[] = "?EWID";
+
+static pthread_once_t g_init_once = PTHREAD_ONCE_INIT;
+
+K_THREAD_STACK_DEFINE(g_sys_work_q_stack,
+                      CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE);
+
+/* Provided by the linker for the "zblue_init" section, see <zephyr/init.h> */
+
+extern const struct z_zblue_init __start_zblue_init[];
+extern const struct z_zblue_init __stop_zblue_init[];
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int init_cmp(const void *a, const void *b)
+{
+  const struct z_zblue_init *x = *(const struct z_zblue_init **)a;
+  const struct z_zblue_init *y = *(const struct z_zblue_init **)b;
+
+  if (x->level != y->level)
+    {
+      return x->level - y->level;
+    }
+
+  if (x->prio != y->prio)
+    {
+      return x->prio - y->prio;
+    }
+
+  /* qsort() is not stable: keep the link order of equal entries */
+
+  return x < y ? -1 : x > y;
+}
+
+static void kernel_init(void)
+{
+  const struct k_work_queue_config cfg =
+  {
+    .name = "sysworkq",
+  };
+
+  const struct z_zblue_init **list;
+  size_t n = __stop_zblue_init - __start_zblue_init;
+  size_t i;
+  int ret;
+
+  k_work_queue_start(&k_sys_work_q, g_sys_work_q_stack,
+                     K_THREAD_STACK_SIZEOF(g_sys_work_q_stack),
+                     CONFIG_SYSTEM_WORKQUEUE_PRIORITY, &cfg);
+
+  /* Run the SYS_INIT() entries in Zephyr order: by level, then priority */
+
+  list = malloc(n * sizeof(*list));
+  __ASSERT_NO_MSG(n == 0 || list != NULL);
+  for (i = 0; i < n; i++)
+    {
+      list[i] = &__start_zblue_init[i];
+    }
+
+  qsort(list, n, sizeof(*list), init_cmp);
+  for (i = 0; i < n; i++)
+    {
+      ret = list[i]->init_fn();
+      if (ret < 0)
+        {
+          syslog(SYSLOG_ERR, "zblue: init %p failed: %d\n",
+                 list[i]->init_fn, ret);
+        }
+    }
+
+  free(list);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/* Runs what the Zephyr kernel runs at boot: the system work queue and the
+ * SYS_INIT() functions.  bt_enable() checks that the HCI device is ready
+ * before anything else, which is the first point zblue gets control.
+ */
+
+void z_zblue_kernel_init(void)
+{
+  pthread_once(&g_init_once, kernel_init);
+}
+
+bool z_impl_device_is_ready(const struct device *dev)
+{
+  z_zblue_kernel_init();
+  return dev != NULL;
+}
+
+/* Assertions: __ASSERT() failures end the program, as in Zephyr */
+
+void zassert_vprint(const char *fmt, va_list ap)
+{
+  vsyslog(SYSLOG_ERR, fmt, ap);
+}
+
+void zassert_fail(const char *cond, const char *file, unsigned int line,
+                  const char *fmt, ...)
+{
+  char msg[128];
+  va_list ap;
+
+  msg[0] = '\0';
+  if (fmt != NULL)
+    {
+      va_start(ap, fmt);
+      vsnprintf(msg, sizeof(msg), fmt, ap);
+      va_end(ap);
+    }
+
+  syslog(SYSLOG_ERR, "zblue: assertion failed: %s at %s:%u %s\n",
+         cond != NULL ? cond : "?", file != NULL ? file : "?", line, msg);
+  abort();
+}
+
+/* Logging */
+
+void z_zblue_log(int level, const char *module, const char *fmt, ...)
+{
+  char buf[192];
+  va_list ap;
+
+  va_start(ap, fmt);
+  vsnprintf(buf, sizeof(buf), fmt, ap);
+  va_end(ap);
+
+  level = CLAMP(level, 0, 4);
+  syslog(g_syslog_prio[level], "%c: %s: %s\n", g_level_tag[level], module,
+         buf);
+}
+
+void z_zblue_hexdump(int level, const char *module, const void *data,
+                     size_t length, const char *str)
+{
+  const uint8_t *p = data;
+  char line[16 * 3 + 1];
+  size_t i;
+  size_t n;
+
+  z_zblue_log(level, module, "%s", str);
+  for (i = 0; i < length; i += 16)
+    {
+      for (n = 0; n < 16 && i + n < length; n++)
+        {
+          snprintf(&line[n * 3], 4, "%02x ", p[i + n]);
+        }
+
+      z_zblue_log(level, module, "  %s", line);
+    }
+}
+
+void printk(const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start(ap, fmt);
+  vprintk(fmt, ap);
+  va_end(ap);
+}
+
+void vprintk(const char *fmt, va_list ap)
+{
+  vsyslog(SYSLOG_INFO, fmt, ap);
+}
+
+int snprintk(char *str, size_t size, const char *fmt, ...)

Review Comment:
   Done, `snprintk()` maps to `snprintf()`.
   



##########
wireless/bluetooth/zblue/port/src/misc.c:
##########
@@ -0,0 +1,257 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/src/misc.c
+ *
+ * SPDX-License-Identifier: Apache-2.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <sys/random.h>
+#include <syslog.h>
+
+#include <zephyr/kernel.h>
+#include <zephyr/device.h>
+#include <zephyr/init.h>
+#include <zephyr/random/random.h>
+#include <zephyr/logging/log.h>
+
+#include "port.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* <zephyr/logging/log.h> replaced the syslog LOG_ERR priority */
+
+#define SYSLOG_ERR     3
+#define SYSLOG_WARNING 4
+#define SYSLOG_INFO    6
+#define SYSLOG_DEBUG   7
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const int g_syslog_prio[] =
+{
+  SYSLOG_ERR, SYSLOG_ERR, SYSLOG_WARNING, SYSLOG_INFO, SYSLOG_DEBUG
+};
+
+static const char g_level_tag[] = "?EWID";
+
+static pthread_once_t g_init_once = PTHREAD_ONCE_INIT;
+
+K_THREAD_STACK_DEFINE(g_sys_work_q_stack,
+                      CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE);
+
+/* Provided by the linker for the "zblue_init" section, see <zephyr/init.h> */
+
+extern const struct z_zblue_init __start_zblue_init[];
+extern const struct z_zblue_init __stop_zblue_init[];
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int init_cmp(const void *a, const void *b)
+{
+  const struct z_zblue_init *x = *(const struct z_zblue_init **)a;
+  const struct z_zblue_init *y = *(const struct z_zblue_init **)b;
+
+  if (x->level != y->level)
+    {
+      return x->level - y->level;
+    }
+
+  if (x->prio != y->prio)
+    {
+      return x->prio - y->prio;
+    }
+
+  /* qsort() is not stable: keep the link order of equal entries */
+
+  return x < y ? -1 : x > y;
+}
+
+static void kernel_init(void)
+{
+  const struct k_work_queue_config cfg =
+  {
+    .name = "sysworkq",
+  };
+
+  const struct z_zblue_init **list;
+  size_t n = __stop_zblue_init - __start_zblue_init;
+  size_t i;
+  int ret;
+
+  k_work_queue_start(&k_sys_work_q, g_sys_work_q_stack,
+                     K_THREAD_STACK_SIZEOF(g_sys_work_q_stack),
+                     CONFIG_SYSTEM_WORKQUEUE_PRIORITY, &cfg);
+
+  /* Run the SYS_INIT() entries in Zephyr order: by level, then priority */
+
+  list = malloc(n * sizeof(*list));
+  __ASSERT_NO_MSG(n == 0 || list != NULL);
+  for (i = 0; i < n; i++)
+    {
+      list[i] = &__start_zblue_init[i];
+    }
+
+  qsort(list, n, sizeof(*list), init_cmp);
+  for (i = 0; i < n; i++)
+    {
+      ret = list[i]->init_fn();
+      if (ret < 0)
+        {
+          syslog(SYSLOG_ERR, "zblue: init %p failed: %d\n",
+                 list[i]->init_fn, ret);
+        }
+    }
+
+  free(list);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/* Runs what the Zephyr kernel runs at boot: the system work queue and the
+ * SYS_INIT() functions.  bt_enable() checks that the HCI device is ready
+ * before anything else, which is the first point zblue gets control.
+ */
+
+void z_zblue_kernel_init(void)
+{
+  pthread_once(&g_init_once, kernel_init);
+}
+
+bool z_impl_device_is_ready(const struct device *dev)
+{
+  z_zblue_kernel_init();
+  return dev != NULL;
+}
+
+/* Assertions: __ASSERT() failures end the program, as in Zephyr */
+
+void zassert_vprint(const char *fmt, va_list ap)
+{
+  vsyslog(SYSLOG_ERR, fmt, ap);
+}
+
+void zassert_fail(const char *cond, const char *file, unsigned int line,
+                  const char *fmt, ...)
+{
+  char msg[128];
+  va_list ap;
+
+  msg[0] = '\0';
+  if (fmt != NULL)
+    {
+      va_start(ap, fmt);
+      vsnprintf(msg, sizeof(msg), fmt, ap);
+      va_end(ap);
+    }
+
+  syslog(SYSLOG_ERR, "zblue: assertion failed: %s at %s:%u %s\n",
+         cond != NULL ? cond : "?", file != NULL ? file : "?", line, msg);
+  abort();
+}
+
+/* Logging */
+
+void z_zblue_log(int level, const char *module, const char *fmt, ...)
+{
+  char buf[192];
+  va_list ap;
+
+  va_start(ap, fmt);
+  vsnprintf(buf, sizeof(buf), fmt, ap);
+  va_end(ap);
+
+  level = CLAMP(level, 0, 4);
+  syslog(g_syslog_prio[level], "%c: %s: %s\n", g_level_tag[level], module,
+         buf);
+}
+
+void z_zblue_hexdump(int level, const char *module, const void *data,
+                     size_t length, const char *str)
+{
+  const uint8_t *p = data;
+  char line[16 * 3 + 1];
+  size_t i;
+  size_t n;
+
+  z_zblue_log(level, module, "%s", str);
+  for (i = 0; i < length; i += 16)
+    {
+      for (n = 0; n < 16 && i + n < length; n++)
+        {
+          snprintf(&line[n * 3], 4, "%02x ", p[i + n]);
+        }
+
+      z_zblue_log(level, module, "  %s", line);
+    }
+}
+
+void printk(const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start(ap, fmt);
+  vprintk(fmt, ap);
+  va_end(ap);
+}
+
+void vprintk(const char *fmt, va_list ap)
+{
+  vsyslog(SYSLOG_INFO, fmt, ap);
+}
+
+int snprintk(char *str, size_t size, const char *fmt, ...)
+{
+  va_list ap;
+  int ret;
+
+  va_start(ap, fmt);
+  ret = vsnprintf(str, size, fmt, ap);
+  va_end(ap);
+  return ret;
+}
+
+int vsnprintk(char *str, size_t size, const char *fmt, va_list ap)

Review Comment:
   Done, `vsnprintk()` maps to `vsnprintf()`.
   



##########
crypto/mbedtls/Kconfig:
##########
@@ -18,6 +18,7 @@ config MBEDTLS_VERSION
 config MBEDTLS_MINIMAL
        bool "Minimal configuration"
        default y if NIMBLE && !NIMBLE_TINYCRYPT
+       default y if ZBLUE

Review Comment:
   Done: `default y if (NIMBLE && !NIMBLE_TINYCRYPT) || ZBLUE`.
   



##########
wireless/bluetooth/zblue/port/src/work.c:
##########
@@ -0,0 +1,474 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/src/work.c
+ *
+ * SPDX-License-Identifier: Apache-2.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.
+ *
+ ****************************************************************************/
+
+/* Work queues with the semantics of kernel/work.c in Zephyr (return
+ * values, RUNNING/QUEUED/DELAYED/CANCELING flags, resubmission to the
+ * queue a work item runs on).  Each queue is served by one thread, which
+ * also fires its delayed items.  One lock protects all work state; the
+ * waits for cancel_sync() and flush() use one shared condition.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <zephyr/kernel.h>
+
+#include "port.h"
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static pthread_mutex_t g_work_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t g_work_done = PTHREAD_COND_INITIALIZER;
+
+struct k_work_q k_sys_work_q;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int submit_locked(struct k_work *work, struct k_work_q *queue)
+{
+  int ret;
+
+  if (work->flags & K_WORK_CANCELING)
+    {
+      return -EBUSY;
+    }
+
+  if (work->flags & K_WORK_QUEUED)
+    {
+      return 0;
+    }
+
+  ret = 1;
+  if (queue == NULL)
+    {
+      queue = work->queue;
+    }
+
+  if (work->flags & K_WORK_RUNNING)
+    {
+      queue = work->queue;
+      ret = 2;
+    }
+
+  if (queue == NULL)
+    {
+      return -EINVAL;
+    }
+
+  sys_slist_append(&queue->pending, &work->node);
+  work->flags |= K_WORK_QUEUED;
+  work->queue = queue;
+  pthread_cond_signal(&queue->cond);
+  return ret;
+}
+
+static bool unschedule_locked(struct k_work_delayable *dwork)
+{
+  if ((dwork->work.flags & K_WORK_DELAYED) == 0)
+    {
+      return false;
+    }
+
+  dwork->work.flags &= ~K_WORK_DELAYED;
+  sys_dlist_remove(&dwork->node);
+  return true;
+}
+
+static int schedule_locked(struct k_work_q *queue,
+                           struct k_work_delayable *dwork,
+                           k_timeout_t delay)
+{
+  if (K_TIMEOUT_EQ(delay, K_NO_WAIT))
+    {
+      return submit_locked(&dwork->work, queue);
+    }
+
+  dwork->work.flags |= K_WORK_DELAYED;
+  dwork->queue = queue;
+  dwork->expiry = z_zblue_deadline(delay);
+  sys_dlist_append(&queue->delayed, &dwork->node);
+  pthread_cond_signal(&queue->cond);
+  return 1;
+}
+
+static int cancel_async_locked(struct k_work *work)
+{
+  if ((work->flags & K_WORK_CANCELING) == 0 &&
+      (work->flags & K_WORK_QUEUED))
+    {
+      sys_slist_find_and_remove(&work->queue->pending, &work->node);
+      work->flags &= ~K_WORK_QUEUED;
+      pthread_cond_broadcast(&g_work_done);
+    }
+
+  if (work->flags & K_WORK_MASK)
+    {
+      work->flags |= K_WORK_CANCELING;
+    }
+
+  return work->flags & K_WORK_MASK;
+}
+
+static void wait_locked(struct k_work *work, uint32_t mask)
+{
+  int depth;
+
+  if ((work->flags & mask) == 0)
+    {
+      return;
+    }
+
+  /* g_work_lock is dropped while the coop lock is released, so the coop
+   * lock is never taken with g_work_lock held.
+   */
+
+  pthread_mutex_unlock(&g_work_lock);
+  depth = z_zblue_block_begin();
+  pthread_mutex_lock(&g_work_lock);
+  while (work->flags & mask)
+    {
+      pthread_cond_wait(&g_work_done, &g_work_lock);
+    }
+
+  pthread_mutex_unlock(&g_work_lock);
+  z_zblue_block_end(depth);
+  pthread_mutex_lock(&g_work_lock);
+}
+
+/* Move the delayed items of a queue that are due to its pending list and
+ * return the time of the next one.
+ */
+
+static int64_t fire_delayed_locked(struct k_work_q *queue)
+{
+  struct k_work_delayable *dwork;
+  struct k_work_delayable *next;
+  int64_t now = k_uptime_ticks();
+  int64_t first = INT64_MAX;
+
+  SYS_DLIST_FOR_EACH_CONTAINER_SAFE(&queue->delayed, dwork, next, node)
+    {
+      if (dwork->expiry <= now)
+        {
+          unschedule_locked(dwork);
+          submit_locked(&dwork->work, dwork->queue);
+        }
+      else if (dwork->expiry < first)
+        {
+          first = dwork->expiry;
+        }
+    }
+
+  return first;
+}
+
+static void work_queue_main(void *p1, void *p2, void *p3)
+{
+  struct k_work_q *queue = p1;
+  struct k_work *work;
+  sys_snode_t *node;
+  int64_t next;
+  int depth;
+
+  UNUSED(p2);
+  UNUSED(p3);
+
+  pthread_mutex_lock(&g_work_lock);
+  for (; ; )
+    {
+      next = fire_delayed_locked(queue);
+      node = sys_slist_get(&queue->pending);
+      if (node == NULL)
+        {
+          pthread_mutex_unlock(&g_work_lock);
+          depth = z_zblue_block_begin();
+          pthread_mutex_lock(&g_work_lock);
+
+          /* Items may have been submitted or scheduled while
+           * g_work_lock was dropped: look again before waiting.
+           */
+
+          next = fire_delayed_locked(queue);
+          if (sys_slist_is_empty(&queue->pending))
+            {
+              z_zblue_cond_wait(&queue->cond, &g_work_lock, next);
+            }
+
+          pthread_mutex_unlock(&g_work_lock);
+          z_zblue_block_end(depth);
+          pthread_mutex_lock(&g_work_lock);
+          continue;
+        }
+
+      work = CONTAINER_OF(node, struct k_work, node);
+      work->flags &= ~K_WORK_QUEUED;
+      work->flags |= K_WORK_RUNNING;
+      pthread_mutex_unlock(&g_work_lock);
+
+      work->handler(work);
+
+      pthread_mutex_lock(&g_work_lock);
+      work->flags &= ~(K_WORK_RUNNING | K_WORK_CANCELING);
+      pthread_cond_broadcast(&g_work_done);
+
+      if ((queue->flags & 1) == 0)
+        {
+          /* Not no_yield: let other threads of the same priority run */
+
+          pthread_mutex_unlock(&g_work_lock);
+          k_yield();
+          pthread_mutex_lock(&g_work_lock);
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void k_work_init(struct k_work *work, k_work_handler_t handler)

Review Comment:
   I looked at it. The NuttX work queue covers part of k_work, but the Zephyr 
host relies on a few things it doesn't provide:
   
   1. Running state. NuttX clears `work->worker` before calling the worker, so 
there is no "running" state. Zephyr needs it: `k_work_busy_get()` reports 
RUNNING/CANCELING/FLUSHING, and submitting a work item while it runs re-queues 
it on the same queue and returns 2.
   2. Flush. `k_work_flush()` and `k_work_flush_delayable()` wait for a work 
item to finish without cancelling it. `work_cancel_sync()` cancels it first.
   3. Several queues. The host uses the system work queue and its own RX queue 
at a different priority. `work_queue_create()` is only available in the flat 
build (or in the kernel); outside it, usrwork offers a single queue, so both 
queues would be merged and their ordering and priority would change.
   
   It could be built on `work_queue_create()` + `work_queue_wq()` for the flat 
build, keeping a flags word in `struct k_work` for 1 and 2. That removes the 
thread loop and the delayed-work timer, but the state tracking stays, and zblue 
would be limited to the flat build. Would you like that in this PR, or as a 
follow-up after it is merged?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to