Skip to content

Fix blocking after disconnect #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 63 additions & 14 deletions peripheral_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func (p *peripheral) DiscoverServices(s []UUID) ([]*Service, error) {
binary.LittleEndian.PutUint16(b[3:5], 0xFFFF)
binary.LittleEndian.PutUint16(b[5:7], 0x2800)

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return nil, err
}
if finish(op, start, b) {
break
}
Expand Down Expand Up @@ -111,7 +115,11 @@ func (p *peripheral) DiscoverCharacteristics(cs []UUID, s *Service) ([]*Characte
binary.LittleEndian.PutUint16(b[3:5], s.endh)
binary.LittleEndian.PutUint16(b[5:7], 0x2803)

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return nil, err
}
if finish(op, start, b) {
break
}
Expand Down Expand Up @@ -172,7 +180,11 @@ func (p *peripheral) DiscoverDescriptors(ds []UUID, c *Characteristic) ([]*Descr
binary.LittleEndian.PutUint16(b[1:3], start)
binary.LittleEndian.PutUint16(b[3:5], c.endh)

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return nil, err
}
if finish(attOpFindInfoReq, start, b) {
break
}
Expand Down Expand Up @@ -211,7 +223,11 @@ func (p *peripheral) ReadCharacteristic(c *Characteristic) ([]byte, error) {
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], c.vh)

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return nil, err
}
b = b[1:]
return b, nil
}
Expand All @@ -238,8 +254,10 @@ func (p *peripheral) ReadLongCharacteristic(c *Characteristic) ([]byte, error) {
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], c.vh)
binary.LittleEndian.PutUint16(b[3:5], off)

b = p.sendReq(op, b)
b, err = p.sendReq(op, b)
if err != nil {
return nil, err
}
b = b[1:]
if len(b) == 0 {
break
Expand Down Expand Up @@ -267,7 +285,11 @@ func (p *peripheral) WriteCharacteristic(c *Characteristic, value []byte, noRsp
p.sendCmd(op, b)
return nil
}
b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return err
}
// TODO: error handling
b = b[1:]
return nil
Expand All @@ -279,7 +301,11 @@ func (p *peripheral) ReadDescriptor(d *Descriptor) ([]byte, error) {
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], d.h)

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return nil, err
}
b = b[1:]
// TODO: error handling
return b, nil
Expand All @@ -292,7 +318,11 @@ func (p *peripheral) WriteDescriptor(d *Descriptor, value []byte) error {
binary.LittleEndian.PutUint16(b[1:3], d.h)
copy(b[3:], value)

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return err
}
b = b[1:]
// TODO: error handling
return nil
Expand All @@ -314,7 +344,11 @@ func (p *peripheral) setNotifyValue(c *Characteristic, flag uint16,
binary.LittleEndian.PutUint16(b[1:3], c.cccd.h)
binary.LittleEndian.PutUint16(b[3:5], ccc)

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return err
}
b = b[1:]
// TODO: error handling
if f == nil {
Expand Down Expand Up @@ -358,21 +392,31 @@ func (p *peripheral) sendCmd(op byte, b []byte) {
p.reqc <- message{op: op, b: b}
}

func (p *peripheral) sendReq(op byte, b []byte) []byte {
func (p *peripheral) sendReq(op byte, b []byte) (data []byte, err error) {
defer func() {
if panicErr := recover(); panicErr != nil {
err = fmt.Errorf("[sendReq]recover: %+v", panicErr)
}
}()
m := message{op: op, b: b, rspc: make(chan []byte)}
p.reqc <- m
return <-m.rspc
data, ok := <-m.rspc
if !ok {
return nil, fmt.Errorf("[sendReq] connection is closed")
}
return data, nil
}

func (p *peripheral) loop() {
// Serialize the request.
rspc := make(chan []byte)

var req message
// Dequeue request loop
go func() {
for {
select {
case req := <-p.reqc:
case req = <-p.reqc:
p.l2c.Write(req.b)
if req.rspc == nil {
break
Expand Down Expand Up @@ -401,6 +445,7 @@ func (p *peripheral) loop() {
n, err := p.l2c.Read(buf)
if n == 0 || err != nil {
close(p.quitc)
close(req.rspc)
return
}

Expand Down Expand Up @@ -435,7 +480,11 @@ func (p *peripheral) SetMTU(mtu uint16) error {
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], uint16(mtu))

b = p.sendReq(op, b)
var err error
b, err = p.sendReq(op, b)
if err != nil {
return err
}
serverMTU := binary.LittleEndian.Uint16(b[1:3])
if serverMTU < mtu {
mtu = serverMTU
Expand Down