Skip to content

Commit

Permalink
Allow custom notifyhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
moogle19 committed Oct 17, 2017
1 parent 688d4aa commit 607987f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
6 changes: 5 additions & 1 deletion linux/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func NewDevice() (*Device, error) {

// NewDeviceWithName returns the default HCI device.
func NewDeviceWithName(name string) (*Device, error) {
return NewDeviceWithNameAndHandler(name, nil)
}

func NewDeviceWithNameAndHandler(name string, handler ble.NotifyHandler) (*Device, error) {
dev, err := hci.NewHCI()
if err != nil {
return nil, errors.Wrap(err, "can't create hci")
Expand All @@ -26,7 +30,7 @@ func NewDeviceWithName(name string) (*Device, error) {
return nil, errors.Wrap(err, "can't init hci")
}

srv, err := gatt.NewServerWithName(name)
srv, err := gatt.NewServerWithNameAndHandler(name, handler)
if err != nil {
return nil, errors.Wrap(err, "can't create server")
}
Expand Down
38 changes: 26 additions & 12 deletions linux/gatt/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import (

// NewServerWithName creates a new Server with the specified name
func NewServerWithName(name string) (*Server, error) {
return NewServerWithNameAndHandler(name, nil)
}

// NewServerWithNameAndHandler allow to specify a custom NotifyHandler
func NewServerWithNameAndHandler(name string, notifyHandler ble.NotifyHandler) (*Server, error) {
return &Server{
name: name,
svcs: defaultServices(name),
svcs: defaultServicesWithHandler(name, notifyHandler),
db: att.NewDB(defaultServices(name), uint16(1)),
}, nil
}
Expand Down Expand Up @@ -64,6 +69,9 @@ func (s *Server) DB() *att.DB {
}

func defaultServices(name string) []*ble.Service {
return defaultServicesWithHandler(name, nil)
}
func defaultServicesWithHandler(name string, handler ble.NotifyHandler) []*ble.Service {
// https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.ble.appearance.xml
var gapCharAppearanceGenericComputer = []byte{0x00, 0x80}

Expand All @@ -75,16 +83,22 @@ func defaultServices(name string) []*ble.Service {
gapSvc.NewCharacteristic(ble.PeferredParamsUUID).SetValue([]byte{0x06, 0x00, 0x06, 0x00, 0x00, 0x00, 0xd0, 0x07})

gattSvc := ble.NewService(ble.GATTUUID)
gattSvc.NewCharacteristic(ble.ServiceChangedUUID).HandleIndicate(
ble.NotifyHandlerFunc(func(r ble.Request, n ble.Notifier) {
log.Printf("TODO: indicate client when the services are changed")
for {
select {
case <-n.Context().Done():
log.Printf("count: Notification unsubscribed")
return
}
}
}))
var indicationHandler ble.NotifyHandlerFunc
indicationHandler = defaultHanderFunc
if handler != nil {
indicationHandler = handler.ServeNotify
}
gattSvc.NewCharacteristic(ble.ServiceChangedUUID).HandleIndicate(indicationHandler)
return []*ble.Service{gapSvc, gattSvc}
}

func defaultHanderFunc(r ble.Request, n ble.Notifier) {
log.Printf("TODO: indicate client when the services are changed")
for {
select {
case <-n.Context().Done():
log.Printf("count: Notification unsubscribed")
return
}
}
}

0 comments on commit 607987f

Please sign in to comment.