Skip to content

Commit 49a5eff

Browse files
George KongGeorge Kong
authored andcommitted
added security levels to CharacteristicConfig with linux implementation
1 parent a88a5c4 commit 49a5eff

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

gatts.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ type Service struct {
1010

1111
type WriteEvent = func(client Connection, offset int, value []byte)
1212

13+
// SecurityLevel specifies the security required for a characteristic operation.
14+
// Settings other than the default (SecurityNone) may result in the peer initiating a pairing operation.
15+
type SecurityLevel uint8
16+
17+
const (
18+
SecurityNone SecurityLevel = iota // encryption not required
19+
SecurityEncrypted // encryption required
20+
SecurityEncryptedAuthenticated // encryption and authentication (MITM protection) required
21+
)
22+
1323
// CharacteristicConfig contains some parameters for the configuration of a
1424
// single characteristic.
1525
//
@@ -18,9 +28,13 @@ type WriteEvent = func(client Connection, offset int, value []byte)
1828
type CharacteristicConfig struct {
1929
Handle *Characteristic
2030
UUID
21-
Value []byte
22-
Flags CharacteristicPermissions
23-
WriteEvent WriteEvent
31+
Value []byte
32+
Flags CharacteristicPermissions
33+
WriteEvent WriteEvent
34+
ReadSecurity SecurityLevel
35+
WriteSecurity SecurityLevel
36+
NotifySecurity SecurityLevel
37+
IndicateSecurity SecurityLevel
2438
}
2539

2640
// CharacteristicPermissions lists a number of basic permissions/capabilities

gatts_linux.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ func (a *Adapter) AddService(s *Service) error {
108108
flags = append(flags, bluezCharFlags[i])
109109
}
110110
}
111+
if char.ReadSecurity == SecurityEncrypted {
112+
flags = append(flags, "encrypt-read")
113+
} else if char.ReadSecurity == SecurityEncryptedAuthenticated {
114+
flags = append(flags, "encrypt-authenticated-read")
115+
}
116+
if char.WriteSecurity == SecurityEncrypted {
117+
flags = append(flags, "encrypt-write")
118+
} else if char.WriteSecurity == SecurityEncryptedAuthenticated {
119+
flags = append(flags, "encrypt-authenticated-write")
120+
}
121+
if char.NotifySecurity == SecurityEncrypted {
122+
flags = append(flags, "encrypt-notify")
123+
} else if char.NotifySecurity == SecurityEncryptedAuthenticated {
124+
flags = append(flags, "encrypt-authenticated-notify")
125+
}
126+
if char.IndicateSecurity == SecurityEncrypted {
127+
flags = append(flags, "encrypt-indicate")
128+
} else if char.IndicateSecurity == SecurityEncryptedAuthenticated {
129+
flags = append(flags, "encrypt-authenticated-indicate")
130+
}
111131

112132
// Export the properties of this characteristic.
113133
charPath := path + dbus.ObjectPath("/char"+strconv.Itoa(i))

0 commit comments

Comments
 (0)