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


##########
wireless/bluetooth/zblue/examples/peripheral.c:
##########
@@ -0,0 +1,133 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/examples/peripheral.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.
+ *
+ ****************************************************************************/
+
+/* Connectable advertising with the GAP and GATT services, written against
+ * the Zephyr Bluetooth API exactly as a Zephyr application would be.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdio.h>
+
+#include <zephyr/kernel.h>
+#include <zephyr/bluetooth/bluetooth.h>
+#include <zephyr/bluetooth/conn.h>
+#include <zephyr/bluetooth/gatt.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct bt_data g_ad[] =
+{
+  BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
+  BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME,
+          sizeof(CONFIG_BT_DEVICE_NAME) - 1),
+};
+
+static K_SEM_DEFINE(g_disconnected, 0, 1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void connected(struct bt_conn *conn, uint8_t err)
+{
+  char addr[BT_ADDR_LE_STR_LEN];
+
+  bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+  if (err != 0)
+    {
+      printf("zblue: connection to %s failed (0x%02x)\n", addr, err);
+      return;
+    }
+
+  printf("zblue: connected %s\n", addr);
+}
+
+static void disconnected(struct bt_conn *conn, uint8_t reason)
+{
+  char addr[BT_ADDR_LE_STR_LEN];
+
+  bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+  printf("zblue: disconnected %s (0x%02x)\n", addr, reason);
+  k_sem_give(&g_disconnected);
+}
+
+BT_CONN_CB_DEFINE(g_conn_cb) =
+{
+  .connected    = connected,
+  .disconnected = disconnected,
+};
+
+static int advertise(void)
+{
+  int err;
+
+  err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, g_ad, ARRAY_SIZE(g_ad),
+                        NULL, 0);
+  if (err != 0)
+    {
+      printf("zblue: advertising failed: %d\n", err);
+      return err;
+    }
+
+  printf("zblue: advertising as \"%s\"\n", CONFIG_BT_DEVICE_NAME);
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, char *argv[])
+{
+  bt_addr_le_t addr;
+  size_t count = 1;
+  char str[BT_ADDR_LE_STR_LEN];
+  int err;
+
+  err = bt_enable(NULL);
+  if (err != 0)
+    {
+      printf("zblue: bt_enable failed: %d\n", err);
+      return 1;
+    }
+
+  bt_id_get(&addr, &count);
+  bt_addr_le_to_str(&addr, str, sizeof(str));
+  printf("zblue: Bluetooth ready, identity %s\n", str);
+
+  for (; ; )
+    {
+      if (advertise() != 0)
+        {
+          return 1;
+        }
+

Review Comment:
   It does not spin. `k_sem_take(&g_disconnected, K_FOREVER)` blocks in 
`pthread_cond_wait()` (`k_sem_take()` in `port/src/sync.c`) until the 
`disconnected` callback gives the semaphore, so the loop runs once per 
connection: advertise, wait for the disconnection, advertise again.
   
   Measured on `sim:zblue` with a CSR controller on hci2 and BlueZ as central; 
CPU time of the `nuttx` process on the host, over 20 to 30 s:
   
   | State | Host CPU |
   |---|---|
   | nsh only, zblue not started | 0.25% |
   | advertising | 0.30% |
   | connected, idle | 0.33% |
   | after the disconnection, advertising again | 0.30% |
   
   With `CONFIG_SCHED_CPULOAD_SYSCLK`, `ps` shows 0.0% for `zblue_peripheral` 
and the five host threads (`sysworkq`, `BT LW WQ`, `zblue_wq`, `BT RX WQ`, 
`zblue_hci_rx`), and 98.2 to 98.5% for Idle. `hpwork` is 1.4 to 1.7%, the same 
as without zblue (1.5%).



##########
wireless/bluetooth/zblue/Makefile:
##########
@@ -0,0 +1,208 @@
+############################################################################
+# apps/wireless/bluetooth/zblue/Makefile
+#
+# 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.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+ZBLUE_DIR = $(APPDIR)/wireless/bluetooth/zblue
+
+# Zephyr source, downloaded from the release tag
+
+ZEPHYR_REF := $(patsubst "%",%,$(strip $(CONFIG_ZBLUE_REF)))
+ZEPHYR_URL ?= https://github.com/zephyrproject-rtos/zephyr/archive
+ZEPHYR_TAR = zephyr-$(ZEPHYR_REF).tar.gz
+ZEPHYR_UNPACK = zephyr
+ZEPHYR = $(ZBLUE_DIR)/$(ZEPHYR_UNPACK)
+
+# Only the parts of the tree used by the Bluetooth host are unpacked
+
+ZEPHYR_PATHS = LICENSE Kconfig.constants include lib/net_buf lib/utils \
+               lib/uuid modules/mbedtls scripts/kconfig subsys/bluetooth \
+               subsys/crc subsys/logging subsys/settings
+
+$(ZEPHYR_TAR):
+       @echo "Downloading: $(ZEPHYR_URL)/$(ZEPHYR_REF).tar.gz"
+       $(Q) curl -L $(ZEPHYR_URL)/$(ZEPHYR_REF).tar.gz -o $(ZEPHYR_TAR)

Review Comment:
   Done in 95ef0f2. `CONFIG_ZBLUE_SHA256` defaults to the SHA-256 of the v4.5.0 
archive when `CONFIG_ZBLUE_REF` is `v4.5.0`, and is empty for other tags; with 
an empty value the check is skipped with a warning, as `tools/check-hash.sh` 
does.
   
   The hash is checked before unpacking rather than at download, so an archive 
that is already in the directory is checked too: the Makefile calls 
`tools/check-hash.sh`, and CMake compares `file(SHA256)` with the same value, 
for the same reason instead of `EXPECTED_HASH` in `file(DOWNLOAD)`. curl now 
uses `-f`, so an HTTP error does not leave an error page in place of the 
archive.
   
   Tested with Make and CMake: a modified archive from the download URL is 
removed and the build fails; with the default value the build passes.



##########
wireless/bluetooth/zblue/Kconfig:
##########
@@ -0,0 +1,81 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+menuconfig ZBLUE
+       bool "zblue: Zephyr Bluetooth host"
+       default n
+       depends on ARCH_SIM

Review Comment:
   Done in 95ef0f2: `depends on ARCH_SIM && HOST_LINUX`, and the commit message 
says why. With `HOST_MACOS` set, `make olddefconfig` drops `CONFIG_ZBLUE`.



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