Commit 607987fe authored by Kevin Seidel's avatar Kevin Seidel
Browse files

Allow custom notifyhandler

parent 688d4aa2
......@@ -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")
......@@ -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")
}
......
......@@ -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
}
......@@ -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}
......@@ -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
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment