chengkai15 opened a new pull request, #4129:
URL: https://github.com/apache/nuttx-apps/pull/4129

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   depends-on: apache/nuttx#22268
   
   ## Summary
   
   On current master, `zblue_peripheral` on sim:zblue hangs in `bt_enable()`: 
HCI_Reset is never sent.
   
   In the flat build the Zephyr work queues are kernel threads 
(`work_queue_create()`), while the HCI receive thread and the application 
threads are pthreads of the application. They are in different task groups:
   
   - The thread-specific key of the porting layer is created by the first 
thread that uses it, a work queue (kernel thread). Since apache/nuttx 
0bc013bb2b, `pthread_setspecific()` rejects a key that was not created in the 
group of the caller, so it returns `EINVAL` in the receive thread. That thread 
then does not release the cooperative lock while it blocks in `read()`, and the 
host TX processor waits on that lock forever.
   - File descriptors are not shared either, which is why the HCI driver had 
its own TX work queue that opens the device a second time.
   
   This PR creates the work queues with `work_queue_create_pthread()` 
(apache/nuttx#22268), so all host threads are pthreads of the application. The 
HCI driver writes in `send()` again, and the TX work queue and its thread are 
removed. `uart_bth4` writes each packet whole under its send lock, so writes 
from different threads do not interleave.
   
   ## Impact
   
   - zblue only. It needs apache/nuttx#22268, so this PR stays in draft until 
that one is merged.
   - One thread fewer (`zblue_hci_tx`).
   
   ## Testing
   
   sim:zblue on Linux with a CSR USB dongle (`CONFIG_SIM_HCISOCKET`) as 
controller. BlueZ `gatttool` on a second CSR dongle is the central.
   
   **master (nuttx c68c0bfbc1, nuttx-apps fa6459263):** `bt_enable()` hangs, 
and `ps` shows the TX processor work queue waiting on a mutex held by the 
receive thread:
   
   ```
   nsh> zblue_peripheral &
   zblue_peripheral [8:100]
     TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK        
    STACK COMMAND
       8     8     7 100 FIFO     Task      - Waiting  Semaphore 
0000000000000000 0067456 zblue_peripheral
       9     0     8 121 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0071520 sysworkq 0x7caf25724010 0x7caf25724098
      10     0     8 110 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0070768 BT LW WQ 0x7caf25724280 0x7caf25724308
      11     0     8 121 FIFO     Kthread   - Waiting  Mutex:14  
0000000000000000 0070496 zblue_wq 0x7caf257244f0 0x7caf25724578
      12     0     8 128 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0070672 BT RX WQ 0x7caf25724760 0x7caf257247e8
      13     0     8 129 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0070496 zblue_hci_tx 0x7caf25724a00 0x7caf25724a88
      14     8     7 130 FIFO     pthread   - Waiting  Semaphore 
0000000000000000 0071600 zblue_hci_rx 0x400cc998 0x4023c440
   nsh> W: bt_hci_core: Dropping queued command 0x0c03: HCI transport closed
   ```
   
   With a debug print in `z_zblue_set_self()`, the work queues (pid 0) set the 
key, and the receive thread of the application (pid 8) gets `EINVAL`:
   
   ```
   DBG set_self sysworkq pid=0 ret=0
   DBG set_self BT LW WQ pid=0 ret=0
   DBG set_self zblue_wq pid=0 ret=0
   DBG set_self zblue_wq pid=0 ret=0
   DBG set_self zblue_hci_tx pid=0 ret=0
   DBG set_self (null) pid=8 ret=22
   ```
   
   **This PR + apache/nuttx#22268 on the same master:**
   
   ```
   I: bt_hci_core: HCI transport: HCI
   I: bt_hci_core: Identity: P:00:1A:7D:DA:71:11
   I: bt_hci_core: HCI: version 4.0 (0x06) revision 0x22bb, manufacturer 0x000a
   I: bt_hci_core: LMP: version 4.0 (0x06) subver 0x22bb
   zblue: Bluetooth ready, identity P:00:1A:7D:DA:71:11
   zblue: advertising as "NuttX zblue"
   ```
   
   All host threads now belong to the application (PID 8):
   
   ```
       8     8     7 100 FIFO     Task      - Waiting  Semaphore 
0000000000000000 0067456 zblue_peripheral
       9     8     7 121 FIFO     pthread   - Waiting  Semaphore 
0000000000000000 0071600 sysworkq 0x40130dac 0x7dfaa49e7f60
      10     8     7 110 FIFO     pthread   - Waiting  Semaphore 
0000000000000000 0070848 BT LW WQ 0x40130dac 0x7dfaa49e7f30
      11     8     7 121 FIFO     pthread   - Waiting  Semaphore 
0000000000000000 0070576 zblue_wq 0x40130dac 0x7dfaa49e7f30
      12     8     7 128 FIFO     pthread   - Waiting  Semaphore 
0000000000000000 0070752 BT RX WQ 0x40130dac 0x7dfaa49e7ff0
      13     8     7 130 FIFO     pthread   - Waiting  Semaphore 
0000000000000000 0071600 zblue_hci_rx 0x400cc8c8 0x4023d5c0
   ```
   
   - 10 GATT reads of the Device Name (`gatttool --char-read -a 0x000b`), each 
on a new connection: 10/10 read `4e 75 74 74 58 20 7a 62 6c 75 65` ("NuttX 
zblue").
   - 200 reads on one connection: 200/200. With the host ACL flow control (on 
by default), btmon on the controller shows 201 ACL packets received, 201 sent 
and 201 HCI_Host_Number_Of_Completed_Packets, and no `write failed`.
   
   Not tested: protected and kernel builds (zblue supports only the simulator), 
real boards.
   


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