Allow setting permission flags on characteristics

This commit is contained in:
Ayke van Laethem 2019-11-09 12:27:48 +01:00
parent 6553fe682d
commit 62131e4c55
No known key found for this signature in database
GPG key ID: E97FF5335DFDFDED
4 changed files with 49 additions and 4 deletions

View file

@ -34,7 +34,7 @@ var clockConfig C.nrf_clock_lf_cfg_t = C.nrf_clock_lf_cfg_t{
} }
var ( var (
secModeOpen C.ble_gap_conn_sec_mode_t secModeOpen C.ble_gap_conn_sec_mode_t // No security is needed (aka open link).
defaultDeviceName = [6]byte{'T', 'i', 'n', 'y', 'G', 'o'} defaultDeviceName = [6]byte{'T', 'i', 'n', 'y', 'G', 'o'}
) )

View file

@ -28,6 +28,7 @@ func main() {
Handle: &heartRateMeasurement, Handle: &heartRateMeasurement,
UUID: bluetooth.New16BitUUID(0x2A37), // Heart Rate Measurement UUID: bluetooth.New16BitUUID(0x2A37), // Heart Rate Measurement
Value: []byte{0, 75}, // 75bpm Value: []byte{0, 75}, // 75bpm
Flags: bluetooth.CharacteristicReadPermission,
}, },
}, },
})) }))

View file

@ -11,6 +11,7 @@ type Service struct {
// value. // value.
type Characteristic struct { type Characteristic struct {
handle uint16 handle uint16
permissions CharacteristicPermissions
} }
// CharacteristicConfig contains some parameters for the configuration of a // CharacteristicConfig contains some parameters for the configuration of a
@ -22,6 +23,7 @@ type CharacteristicConfig struct {
Handle *Characteristic Handle *Characteristic
UUID UUID
Value []byte Value []byte
Flags CharacteristicPermissions
} }
// Handle returns the numeric handle for this characteristic. This is used // Handle returns the numeric handle for this characteristic. This is used
@ -29,3 +31,40 @@ type CharacteristicConfig struct {
func (c *Characteristic) Handle() uint16 { func (c *Characteristic) Handle() uint16 {
return c.handle return c.handle
} }
// CharacteristicPermissions lists a number of basic permissions/capabilities
// that clients have regarding this characteristic. For example, if you want to
// allow clients to read the value of this characteristic (a common scenario),
// set the Read permission.
type CharacteristicPermissions uint8
// Characteristic permission bitfields.
const (
CharacteristicBroadcastPermission CharacteristicPermissions = 1 << iota
CharacteristicReadPermission
CharacteristicWriteWithoutResponsePermission
CharacteristicWritePermission
CharacteristicNotifyPermission
CharacteristicIndicatePermission
)
// Broadcast returns whether broadcasting of the value is permitted.
func (p CharacteristicPermissions) Broadcast() bool {
return p&CharacteristicBroadcastPermission != 0
}
// Read returns whether reading of the value is permitted.
func (p CharacteristicPermissions) Read() bool {
return p&CharacteristicReadPermission != 0
}
// Write returns whether writing of the value with Write Request is permitted.
func (p CharacteristicPermissions) Write() bool {
return p&CharacteristicWritePermission != 0
}
// WriteWithoutResponse returns whether writing of the value with Write Command
// is permitted.
func (p CharacteristicPermissions) WriteWithoutResponse() bool {
return p&CharacteristicWriteWithoutResponsePermission != 0
}

View file

@ -22,8 +22,12 @@ func (a *Adapter) AddService(service *Service) error {
} }
for _, char := range service.Characteristics { for _, char := range service.Characteristics {
metadata := C.ble_gatts_char_md_t{} metadata := C.ble_gatts_char_md_t{}
metadata.char_props.set_bitfield_read(1) metadata.char_props.set_bitfield_broadcast(uint8(char.Flags>>0) & 1)
metadata.char_props.set_bitfield_write(1) metadata.char_props.set_bitfield_read(uint8(char.Flags>>1) & 1)
metadata.char_props.set_bitfield_write_wo_resp(uint8(char.Flags>>2) & 1)
metadata.char_props.set_bitfield_write(uint8(char.Flags>>3) & 1)
metadata.char_props.set_bitfield_notify(uint8(char.Flags>>4) & 1)
metadata.char_props.set_bitfield_indicate(uint8(char.Flags>>5) & 1)
handles := C.ble_gatts_char_handles_t{} handles := C.ble_gatts_char_handles_t{}
charUUID, errCode := char.UUID.shortUUID() charUUID, errCode := char.UUID.shortUUID()
if errCode != 0 { if errCode != 0 {
@ -49,6 +53,7 @@ func (a *Adapter) AddService(service *Service) error {
} }
if char.Handle != nil { if char.Handle != nil {
char.Handle.handle = handles.value_handle char.Handle.handle = handles.value_handle
char.Handle.permissions = char.Flags
} }
} }
return makeError(errCode) return makeError(errCode)