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


##########
wireless/bluetooth/zblue/port/include/zephyr/logging/log.h:
##########
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/include/zephyr/logging/log.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* Zephyr logging macros on top of syslog().  The level of each module is
+ * fixed at build time, from LOG_LEVEL or the second argument of
+ * LOG_MODULE_REGISTER()/LOG_MODULE_DECLARE(), as in Zephyr.
+ */
+
+#ifndef __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_LOGGING_LOG_H
+#define __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_LOGGING_LOG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <zephyr/sys/util.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#define LOG_LEVEL_NONE 0
+#define LOG_LEVEL_ERR  1
+#define LOG_LEVEL_WRN  2
+#define LOG_LEVEL_INF  3
+#define LOG_LEVEL_DBG  4
+
+#ifndef CONFIG_LOG
+#  define Z_ZBLUE_LOG_MAX 0
+#else
+#  define Z_ZBLUE_LOG_MAX CONFIG_LOG_MAX_LEVEL
+#endif
+
+#ifdef LOG_LEVEL
+#  define Z_ZBLUE_LOG_DEFAULT LOG_LEVEL
+#else
+#  define Z_ZBLUE_LOG_DEFAULT CONFIG_LOG_DEFAULT_LEVEL
+#endif
+
+#define Z_ZBLUE_LOG_RESOLVE(...) \
+  GET_ARG_N(2, __VA_ARGS__, Z_ZBLUE_LOG_DEFAULT)
+
+#define LOG_MODULE_REGISTER(...) \
+  static const char __unused z_log_name[] = STRINGIFY(GET_ARG_N(1, 
__VA_ARGS__)); \
+  enum { z_log_level = Z_ZBLUE_LOG_RESOLVE(__VA_ARGS__) }
+
+#define LOG_MODULE_DECLARE(...) LOG_MODULE_REGISTER(__VA_ARGS__)
+
+void z_zblue_log(int level, const char *module, const char *fmt, ...)

Review Comment:
   Done. `Z_ZBLUE_LOG` now calls `syslog()` directly; the output format (`I: 
module: message`) is unchanged.
   



##########
wireless/bluetooth/zblue/port/include/zephyr/logging/log.h:
##########
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/include/zephyr/logging/log.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* Zephyr logging macros on top of syslog().  The level of each module is
+ * fixed at build time, from LOG_LEVEL or the second argument of
+ * LOG_MODULE_REGISTER()/LOG_MODULE_DECLARE(), as in Zephyr.
+ */
+
+#ifndef __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_LOGGING_LOG_H
+#define __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_LOGGING_LOG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <zephyr/sys/util.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#define LOG_LEVEL_NONE 0
+#define LOG_LEVEL_ERR  1
+#define LOG_LEVEL_WRN  2
+#define LOG_LEVEL_INF  3
+#define LOG_LEVEL_DBG  4
+
+#ifndef CONFIG_LOG
+#  define Z_ZBLUE_LOG_MAX 0
+#else
+#  define Z_ZBLUE_LOG_MAX CONFIG_LOG_MAX_LEVEL
+#endif
+
+#ifdef LOG_LEVEL
+#  define Z_ZBLUE_LOG_DEFAULT LOG_LEVEL
+#else
+#  define Z_ZBLUE_LOG_DEFAULT CONFIG_LOG_DEFAULT_LEVEL
+#endif
+
+#define Z_ZBLUE_LOG_RESOLVE(...) \
+  GET_ARG_N(2, __VA_ARGS__, Z_ZBLUE_LOG_DEFAULT)
+
+#define LOG_MODULE_REGISTER(...) \
+  static const char __unused z_log_name[] = STRINGIFY(GET_ARG_N(1, 
__VA_ARGS__)); \
+  enum { z_log_level = Z_ZBLUE_LOG_RESOLVE(__VA_ARGS__) }
+
+#define LOG_MODULE_DECLARE(...) LOG_MODULE_REGISTER(__VA_ARGS__)
+
+void z_zblue_log(int level, const char *module, const char *fmt, ...)
+  __printf_like(3, 4);
+void z_zblue_hexdump(int level, const char *module, const void *data,

Review Comment:
   Done. There is no `lib_hexdump` in NuttX, so I used `lib_dumpbuffer()`. It 
always logs at LOG_INFO and without the module name; the level check still 
happens before the call.
   



##########
wireless/bluetooth/zblue/port/include/zephyr/logging/log.h:
##########
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/include/zephyr/logging/log.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* Zephyr logging macros on top of syslog().  The level of each module is
+ * fixed at build time, from LOG_LEVEL or the second argument of
+ * LOG_MODULE_REGISTER()/LOG_MODULE_DECLARE(), as in Zephyr.
+ */
+
+#ifndef __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_LOGGING_LOG_H
+#define __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_LOGGING_LOG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <zephyr/sys/util.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#define LOG_LEVEL_NONE 0
+#define LOG_LEVEL_ERR  1
+#define LOG_LEVEL_WRN  2
+#define LOG_LEVEL_INF  3
+#define LOG_LEVEL_DBG  4
+
+#ifndef CONFIG_LOG
+#  define Z_ZBLUE_LOG_MAX 0
+#else
+#  define Z_ZBLUE_LOG_MAX CONFIG_LOG_MAX_LEVEL
+#endif
+
+#ifdef LOG_LEVEL
+#  define Z_ZBLUE_LOG_DEFAULT LOG_LEVEL
+#else
+#  define Z_ZBLUE_LOG_DEFAULT CONFIG_LOG_DEFAULT_LEVEL
+#endif
+
+#define Z_ZBLUE_LOG_RESOLVE(...) \
+  GET_ARG_N(2, __VA_ARGS__, Z_ZBLUE_LOG_DEFAULT)
+
+#define LOG_MODULE_REGISTER(...) \
+  static const char __unused z_log_name[] = STRINGIFY(GET_ARG_N(1, 
__VA_ARGS__)); \
+  enum { z_log_level = Z_ZBLUE_LOG_RESOLVE(__VA_ARGS__) }
+
+#define LOG_MODULE_DECLARE(...) LOG_MODULE_REGISTER(__VA_ARGS__)
+
+void z_zblue_log(int level, const char *module, const char *fmt, ...)
+  __printf_like(3, 4);
+void z_zblue_hexdump(int level, const char *module, const void *data,
+                     size_t length, const char *str);
+
+#define Z_ZBLUE_LOG(_level, fmt, ...) \
+  do \
+    { \
+      if (_level <= z_log_level && _level <= Z_ZBLUE_LOG_MAX) \
+        { \
+          z_zblue_log(_level, z_log_name, fmt, ##__VA_ARGS__); \
+        } \
+    } \
+  while (0)
+
+#define Z_ZBLUE_HEXDUMP(_level, _data, _len, _str) \
+  do \
+    { \
+      if (_level <= z_log_level && _level <= Z_ZBLUE_LOG_MAX) \
+        { \
+          z_zblue_hexdump(_level, z_log_name, _data, _len, _str); \
+        } \
+    } \
+  while (0)
+
+/* <syslog.h> uses LOG_ERR for a priority */
+
+#undef LOG_ERR
+#define LOG_ERR(...) Z_ZBLUE_LOG(LOG_LEVEL_ERR, __VA_ARGS__)
+#define LOG_WRN(...) Z_ZBLUE_LOG(LOG_LEVEL_WRN, __VA_ARGS__)
+#define LOG_INF(...) Z_ZBLUE_LOG(LOG_LEVEL_INF, __VA_ARGS__)
+#define LOG_DBG(...) Z_ZBLUE_LOG(LOG_LEVEL_DBG, __VA_ARGS__)
+
+#define LOG_HEXDUMP_ERR(d, l, s) Z_ZBLUE_HEXDUMP(LOG_LEVEL_ERR, d, l, s)
+#define LOG_HEXDUMP_WRN(d, l, s) Z_ZBLUE_HEXDUMP(LOG_LEVEL_WRN, d, l, s)
+#define LOG_HEXDUMP_INF(d, l, s) Z_ZBLUE_HEXDUMP(LOG_LEVEL_INF, d, l, s)
+#define LOG_HEXDUMP_DBG(d, l, s) Z_ZBLUE_HEXDUMP(LOG_LEVEL_DBG, d, l, s)
+
+#define LOG_PRINTK(...) printk(__VA_ARGS__)
+#define LOG_RAW(...)    printk(__VA_ARGS__)

Review Comment:
   Done.
   



##########
wireless/bluetooth/zblue/port/include/zephyr/spinlock.h:
##########
@@ -0,0 +1,67 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/include/zephyr/spinlock.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_SPINLOCK_H
+#define __APPS_WIRELESS_BLUETOOTH_ZBLUE_PORT_INCLUDE_ZEPHYR_SPINLOCK_H
+
+/* Spinlocks protect short sections against other threads.  In userspace
+ * there are no interrupts to mask, so all of them map to one recursive
+ * lock; nothing blocks while holding it.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+struct k_spinlock
+{
+  char unused;
+};
+
+typedef struct
+{
+  int key;
+} k_spinlock_key_t;
+
+k_spinlock_key_t k_spin_lock(struct k_spinlock *l);

Review Comment:
   Done. `k_spin_lock()`, `k_spin_trylock()`, `k_spin_unlock()`, `irq_lock()` 
and `irq_unlock()` are now static inline. The lock is initialized statically 
with `PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP`, so `pthread_once()` is no longer 
needed.
   
   This also fixed a bug: `sim:zblue` did not enable 
`CONFIG_PTHREAD_MUTEX_TYPES`, so 
`pthread_mutexattr_settype(PTHREAD_MUTEX_RECURSIVE)` returned ENOSYS and the 
lock was not recursive. ZBLUE now selects `PTHREAD_MUTEX_TYPES`.
   



##########
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, ...)

Review Comment:
   Done. Added `port/include/zephyr/sys/printk.h`, which replaces the Zephyr 
header and maps `printk()`/`vprintk()` to `syslog()`/`vsyslog()`.
   



-- 
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