I have thread which reads messages from a raw HCI socket in a loop like this:
void* loop_hci (void* args) {
params_hci_t* params = (params_hci_t*) args;
int result_hci = 0;
uint8_t* buf_hci = calloc(1, HCI_EVENT_MAX_LENGTH);
while (!poll_end()) {
result_hci = read(params->hci_sock, buf_hci, HCI_EVENT_MAX_LENGTH);
if (result_hci > 0) {
// ... do stuff with the received data
}
}
ancs_pdebug("HCI loop shutting down...");
return NULL;
}
The poll_end()
function works fine and as intended. It returns 0 until a SIGINT signal is received, after that it returns 1.
In the main thread I create the socket like this:
hci_sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
And also the thread:
ph->hci_sock = hci_sock;
pthread_create(&t_hci, NULL, &loop_hci, ph);
Then after a while call shutdown like this (in the main thread):
shutdown(hci_sock, SHUT_RD);
I'm assuming read() should return after I call shutdown(), I use the same method in a different thread for a L2CAP socket and it works fine. But it doesn't. My pthread_join(t_hci, NULL)
call in the main thread never returns.
The socket works fine. I can read messages from it. I also tried to call close (which I do after the threads have ended) instead, but the results are the same.
What could be the problem, or are my assumptions wrong?