Skip to content

Implemented UUID filters for peripheral_linux #60

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion central_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gatt
import (
"sync"

"github.com/paypal/gatt/xpc"
"github.com/cfreeman/gatt/xpc"
)

type central struct {
Expand Down
2 changes: 1 addition & 1 deletion device_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sync"
"time"

"github.com/paypal/gatt/xpc"
"github.com/cfreeman/gatt/xpc"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions device_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/binary"
"net"

"github.com/paypal/gatt/linux"
"github.com/paypal/gatt/linux/cmd"
"github.com/cfreeman/gatt/linux"
"github.com/cfreeman/gatt/linux/cmd"
)

type device struct {
Expand Down
4 changes: 2 additions & 2 deletions examples/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"log"

"github.com/paypal/gatt"
"github.com/paypal/gatt/examples/option"
"github.com/cfreeman/gatt"
"github.com/cfreeman/gatt/examples/option"
)

func onStateChanged(d gatt.Device, s gatt.State) {
Expand Down
4 changes: 2 additions & 2 deletions examples/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
"time"

"github.com/paypal/gatt"
"github.com/paypal/gatt/examples/option"
"github.com/cfreeman/gatt"
"github.com/cfreeman/gatt/examples/option"
)

var done = make(chan struct{})
Expand Down
2 changes: 1 addition & 1 deletion examples/option/option_darwin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package option

import "github.com/paypal/gatt"
import "github.com/cfreeman/gatt"

var DefaultClientOptions = []gatt.Option{
gatt.MacDeviceRole(gatt.CentralManager),
Expand Down
4 changes: 2 additions & 2 deletions examples/option/option_linux.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package option

import (
"github.com/paypal/gatt"
"github.com/paypal/gatt/linux/cmd"
"github.com/cfreeman/gatt"
"github.com/cfreeman/gatt/linux/cmd"
)

var DefaultClientOptions = []gatt.Option{
Expand Down
6 changes: 3 additions & 3 deletions examples/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"fmt"
"log"

"github.com/paypal/gatt"
"github.com/paypal/gatt/examples/option"
"github.com/paypal/gatt/examples/service"
"github.com/cfreeman/gatt"
"github.com/cfreeman/gatt/examples/option"
"github.com/cfreeman/gatt/examples/service"
)

func main() {
Expand Down
6 changes: 3 additions & 3 deletions examples/server_lnx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"log"
"time"

"github.com/paypal/gatt"
"github.com/paypal/gatt/examples/service"
"github.com/paypal/gatt/linux/cmd"
"github.com/cfreeman/gatt"
"github.com/cfreeman/gatt/examples/service"
"github.com/cfreeman/gatt/linux/cmd"
)

// server_lnx implements a GATT server.
Expand Down
2 changes: 1 addition & 1 deletion examples/service/battery.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package service

import "github.com/paypal/gatt"
import "github.com/cfreeman/gatt"

func NewBatteryService() *gatt.Service {
lv := byte(100)
Expand Down
2 changes: 1 addition & 1 deletion examples/service/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/paypal/gatt"
"github.com/cfreeman/gatt"
)

func NewCountService() *gatt.Service {
Expand Down
2 changes: 1 addition & 1 deletion examples/service/gap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package service

import "github.com/paypal/gatt"
import "github.com/cfreeman/gatt"

var (
attrGAPUUID = gatt.UUID16(0x1800)
Expand Down
2 changes: 1 addition & 1 deletion examples/service/gatt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package service
import (
"log"

"github.com/paypal/gatt"
"github.com/cfreeman/gatt"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions linux/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"io"
"log"

"github.com/paypal/gatt/linux/evt"
"github.com/paypal/gatt/linux/util"
"github.com/cfreeman/gatt/linux/evt"
"github.com/cfreeman/gatt/linux/util"
)

type CmdParam interface {
Expand Down
4 changes: 2 additions & 2 deletions linux/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"syscall"
"unsafe"

"github.com/paypal/gatt/linux/gioctl"
"github.com/paypal/gatt/linux/socket"
"github.com/cfreeman/gatt/linux/gioctl"
"github.com/cfreeman/gatt/linux/socket"
)

type device struct {
Expand Down
2 changes: 1 addition & 1 deletion linux/devices.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package linux

import "github.com/paypal/gatt/linux/gioctl"
import "github.com/cfreeman/gatt/linux/gioctl"

const (
ioctlSize = uintptr(4)
Expand Down
2 changes: 1 addition & 1 deletion linux/evt/evt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/binary"
"errors"

"github.com/paypal/gatt/linux/util"
"github.com/cfreeman/gatt/linux/util"
)

type EventHandler interface {
Expand Down
4 changes: 2 additions & 2 deletions linux/hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"log"
"sync"

"github.com/paypal/gatt/linux/cmd"
"github.com/paypal/gatt/linux/evt"
"github.com/cfreeman/gatt/linux/cmd"
"github.com/cfreeman/gatt/linux/evt"
)

type HCI struct {
Expand Down
2 changes: 1 addition & 1 deletion linux/l2cap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"log"

"github.com/paypal/gatt/linux/cmd"
"github.com/cfreeman/gatt/linux/cmd"
)

type aclData struct {
Expand Down
2 changes: 1 addition & 1 deletion option_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"io"

"github.com/paypal/gatt/linux/cmd"
"github.com/cfreeman/gatt/linux/cmd"
)

// LnxDeviceID specifies which HCI device to use.
Expand Down
2 changes: 1 addition & 1 deletion option_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gatt
import (
"bytes"

"github.com/paypal/gatt/linux/cmd"
"github.com/cfreeman/gatt/linux/cmd"
)

func ExampleLnxDeviceID() {
Expand Down
2 changes: 1 addition & 1 deletion peripheral_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"log"

"github.com/paypal/gatt/xpc"
"github.com/cfreeman/gatt/xpc"
)

type peripheral struct {
Expand Down
31 changes: 18 additions & 13 deletions peripheral_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net"
"strings"

"github.com/paypal/gatt/linux"
"github.com/cfreeman/gatt/linux"
)

type peripheral struct {
Expand Down Expand Up @@ -50,8 +50,7 @@ func finish(op byte, h uint16, b []byte) bool {
return done
}

func (p *peripheral) DiscoverServices(s []UUID) ([]*Service, error) {
// TODO: implement the UUID filters
func (p *peripheral) DiscoverServices(ds []UUID) ([]*Service, error) {
// p.pd.Conn.Write([]byte{0x02, 0x87, 0x00}) // MTU
done := false
start := uint16(0x0001)
Expand All @@ -77,14 +76,18 @@ func (p *peripheral) DiscoverServices(s []UUID) ([]*Service, error) {
}

for len(b) != 0 {
h := binary.LittleEndian.Uint16(b[:2])
endh := binary.LittleEndian.Uint16(b[2:4])
s := &Service{
uuid: UUID{b[4:l]},
h: h,
endh: endh,
u := UUID{b[4:l]}

if UUIDContains(ds, u) {
s := &Service{
uuid: u,
h: binary.LittleEndian.Uint16(b[:2]),
endh: endh,
}
p.svcs = append(p.svcs, s)
}
p.svcs = append(p.svcs, s)

b = b[l:]
done = endh == 0xFFFF
start = endh + 1
Expand All @@ -99,7 +102,6 @@ func (p *peripheral) DiscoverIncludedServices(ss []UUID, s *Service) ([]*Service
}

func (p *peripheral) DiscoverCharacteristics(cs []UUID, s *Service) ([]*Characteristic, error) {
// TODO: implement the UUID filters
done := false
start := s.h
var prev *Characteristic
Expand Down Expand Up @@ -142,7 +144,9 @@ func (p *peripheral) DiscoverCharacteristics(cs []UUID, s *Service) ([]*Characte
h: h,
vh: vh,
}
s.chars = append(s.chars, c)
if UUIDContains(cs, u) {
s.chars = append(s.chars, c)
}
b = b[l:]
done = vh == s.endh
start = vh + 1
Expand All @@ -159,7 +163,6 @@ func (p *peripheral) DiscoverCharacteristics(cs []UUID, s *Service) ([]*Characte
}

func (p *peripheral) DiscoverDescriptors(ds []UUID, c *Characteristic) ([]*Descriptor, error) {
// TODO: implement the UUID filters
done := false
start := c.vh + 1
for !done {
Expand Down Expand Up @@ -193,7 +196,9 @@ func (p *peripheral) DiscoverDescriptors(ds []UUID, c *Characteristic) ([]*Descr
h := binary.LittleEndian.Uint16(b[:2])
u := UUID{b[2:l]}
d := &Descriptor{uuid: u, h: h, char: c}
c.descs = append(c.descs, d)
if UUIDContains(ds, u) {
c.descs = append(c.descs, d)
}
if u.Equal(attrClientCharacteristicConfigUUID) {
c.cccd = d
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ OS X central, see http://stackoverflow.com/questions/20553957.

## Known Issues

Currently OS X vesion does not support subscribing to indications.
Currently OS X vesion does not support subscribing to indications.
Please check [#32](https://github.com/paypal/gatt/issues/32) for the status of this issue.

## REFERENCES
Expand Down
15 changes: 15 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ func (u UUID) Equal(v UUID) bool {
return bytes.Equal(u.b, v.b)
}

// UUIDContains returns a boolean reporting whether u is in the slice s.
func UUIDContains(s []UUID, u UUID) bool {
if s == nil {
return true
}

for _, a := range s {
if a.Equal(u) {
return true
}
}

return false
}

// reverse returns a reversed copy of u.
func reverse(u []byte) []byte {
// Special-case 16 bit UUIDS for speed.
Expand Down