ble: eliminate race conditions from response handling

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-06-14 20:17:16 +02:00
parent cf14909b14
commit 39104fb982
1 changed files with 2 additions and 1 deletions

View File

@ -23,6 +23,7 @@ func NewSerialPort(address string, rid string, tid string) *SerialPort {
// Open opens a connection to a BLE serial device
func (p *SerialPort) Open() (err error) {
p.client = NewClientAdaptor(p.address)
err = p.client.Connect()
// subscribe to response notifications
@ -40,13 +41,13 @@ func (p *SerialPort) Read(b []byte) (n int, err error) {
return
}
p.responseMutex.Lock()
n = len(b)
if len(p.responseData) < n {
n = len(p.responseData)
}
copy(b, p.responseData[:n])
p.responseMutex.Lock()
if len(p.responseData) > n {
p.responseData = p.responseData[n:]
} else {