xiaoxiang781216 commented on code in PR #3977: URL: https://github.com/apache/nuttx-apps/pull/3977#discussion_r4582485257
########## 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: remove z_zblue_log, let's forward to syslog in Z_ZBLUE_LOG directly ########## 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: forward to syslog directly ########## 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: ditto ########## 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: map to macro directly ########## 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: remove too, and call lib_hexdump directly ########## 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: let's inline the function ########## 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: ditto ########## 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: merge to line 20 ########## 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: why not reuse work/usrwork -- 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]
