我正在研究使此C ++ BLE库线程安全的要求: https://github.com/edrosten/libblepp
我需要一些有关如何连接到多个BLE设备或如何序列化我的任务(多次写入)的帮助。
使用gatttool可以同时打开(和连接)多个BLE设备。
如果使用此库,是否应该创建BLEGATTStateMachine
的多个实例?
在其非阻塞示例(https://github.com/edrosten/libblepp/blob/master/examples/bluetooth.cc)中:
//Writes are usually available, so only check for them when the
//state machine wants to write.
if(gatt.wait_on_write())
FD_SET(gatt.socket(), &write_set);
int result = select(gatt.socket() + 1, &read_set, &write_set, NULL, & tv);
if(FD_ISSET(gatt.socket(), &write_set))
gatt.write_and_process_next();
好的,所以如果我有东西要写,并且写得可用,它将调用write_and_process_next()
。
但是在https://github.com/edrosten/libblepp/blob/master/src/blestatemachine.cc#L476中:
似乎只处理if(state == Connecting)
。因此,如果我已经连接了,此呼叫似乎无济于事。
但是,还有send_write_request
,它调用dev.send_write_request
,最终以套接字写入(https://github.com/edrosten/libblepp/blob/master/src/bledevice.cc#L119)结尾:
int ret = write(sock, buf.data(), len);
现在我的问题是:
write
调用以使其具有线程安全性?