Skip to content

Commit

Permalink
gap: make generic services function for HCI separate function
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jan 5, 2025
1 parent 945091e commit 5795b60
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions gap_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,14 @@ var defaultAdvertisement Advertisement
type Advertisement struct {
adapter *Adapter

advertisementType AdvertisingType
localName []byte
serviceUUIDs []UUID
interval uint16
manufacturerData []ManufacturerDataElement
serviceData []ServiceDataElement
stop chan struct{}
advertisementType AdvertisingType
localName []byte
serviceUUIDs []UUID
interval uint16
manufacturerData []ManufacturerDataElement
serviceData []ServiceDataElement
stop chan struct{}
genericServiceInit bool
}

// DefaultAdvertisement returns the default advertisement instance but does not
Expand Down Expand Up @@ -335,31 +336,7 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
a.manufacturerData = append([]ManufacturerDataElement{}, options.ManufacturerData...)
a.serviceData = append([]ServiceDataElement{}, options.ServiceData...)

a.adapter.AddService(
&Service{
UUID: ServiceUUIDGenericAccess,
Characteristics: []CharacteristicConfig{
{
UUID: CharacteristicUUIDDeviceName,
Flags: CharacteristicReadPermission,
Value: a.localName,
},
{
UUID: CharacteristicUUIDAppearance,
Flags: CharacteristicReadPermission,
},
},
})
a.adapter.AddService(
&Service{
UUID: ServiceUUIDGenericAttribute,
Characteristics: []CharacteristicConfig{
{
UUID: CharacteristicUUIDServiceChanged,
Flags: CharacteristicIndicatePermission,
},
},
})
a.configureGenericServices(string(a.localName), 0x0540) // Generic Sensor. TODO: make this configurable

return nil
}
Expand Down Expand Up @@ -515,3 +492,40 @@ func (a *Advertisement) setServiceData(sd []ServiceDataElement) error {

return nil
}

// configureGenericServices adds the Generic Access and Generic Attribute services that are
// required by the Bluetooth specification.
// Note that once these services are added, they cannot be removed or changed.
func (a *Advertisement) configureGenericServices(name string, appearance uint16) {
if a.genericServiceInit {
return
}

a.adapter.AddService(
&Service{
UUID: ServiceUUIDGenericAccess,
Characteristics: []CharacteristicConfig{
{
UUID: CharacteristicUUIDDeviceName,
Flags: CharacteristicReadPermission,
Value: a.localName,
},
{
UUID: CharacteristicUUIDAppearance,
Flags: CharacteristicReadPermission,
Value: []byte{byte(appearance & 0xff), byte(appearance >> 8)},
},
},
})
a.adapter.AddService(
&Service{
UUID: ServiceUUIDGenericAttribute,
Characteristics: []CharacteristicConfig{
{
UUID: CharacteristicUUIDServiceChanged,
Flags: CharacteristicIndicatePermission,
},
},
})
a.genericServiceInit = true
}

0 comments on commit 5795b60

Please sign in to comment.