Skip to content

Commit f36a7a6

Browse files
committed
fix: actually parse of ServiceData and ManufacturerData when scanning, and use existing GAP implementation to do it
Signed-off-by: deadprogram <[email protected]>
1 parent 9482530 commit f36a7a6

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

gap_hci.go

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,59 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
7373
continue
7474
}
7575

76-
for i := 0; i < int(a.hci.advData.eirLength); {
77-
l, t := int(a.hci.advData.eirData[i]), a.hci.advData.eirData[i+1]
78-
if l < 1 {
79-
break
76+
rp := rawAdvertisementPayload{len: a.hci.advData.eirLength}
77+
copy(rp.data[:], a.hci.advData.eirData[:a.hci.advData.eirLength])
78+
if rp.LocalName() != "" {
79+
println("LocalName:", rp.LocalName())
80+
adf.LocalName = rp.LocalName()
81+
}
82+
83+
// Complete List of 16-bit Service Class UUIDs
84+
if b := rp.findField(0x03); len(b) > 0 {
85+
for i := 0; i < len(b)/2; i++ {
86+
uuid := uint16(b[i*2]) | (uint16(b[i*2+1]) << 8)
87+
adf.ServiceUUIDs = append(adf.ServiceUUIDs, New16BitUUID(uuid))
88+
}
89+
}
90+
// Incomplete List of 16-bit Service Class UUIDs
91+
if b := rp.findField(0x02); len(b) > 0 {
92+
for i := 0; i < len(b)/2; i++ {
93+
uuid := uint16(b[i*2]) | (uint16(b[i*2+1]) << 8)
94+
adf.ServiceUUIDs = append(adf.ServiceUUIDs, New16BitUUID(uuid))
95+
}
96+
}
97+
98+
// Complete List of 128-bit Service Class UUIDs
99+
if b := rp.findField(0x07); len(b) > 0 {
100+
for i := 0; i < len(b)/16; i++ {
101+
var uuid [16]byte
102+
copy(uuid[:], b[i*16:i*16+16])
103+
adf.ServiceUUIDs = append(adf.ServiceUUIDs, NewUUID(uuid))
80104
}
105+
}
81106

82-
switch t {
83-
case ADIncompleteAdvertisedService16, ADCompleteAdvertisedService16:
84-
adf.ServiceUUIDs = append(adf.ServiceUUIDs, New16BitUUID(binary.LittleEndian.Uint16(a.hci.advData.eirData[i+2:i+4])))
85-
case ADIncompleteAdvertisedService128, ADCompleteAdvertisedService128:
107+
// Incomplete List of 128-bit Service Class UUIDs
108+
if b := rp.findField(0x06); len(b) > 0 {
109+
for i := 0; i < len(b)/16; i++ {
86110
var uuid [16]byte
87-
copy(uuid[:], a.hci.advData.eirData[i+2:i+18])
111+
copy(uuid[:], b[i*16:i*16+16])
88112
adf.ServiceUUIDs = append(adf.ServiceUUIDs, NewUUID(uuid))
89-
case ADShortLocalName, ADCompleteLocalName:
90-
if debug {
91-
println("local name", string(a.hci.advData.eirData[i+2:i+1+l]))
92-
}
93-
94-
adf.LocalName = string(a.hci.advData.eirData[i+2 : i+1+l])
95-
case ADServiceData:
96-
// TODO: handle service data
97-
case ADManufacturerData:
98-
// TODO: handle manufacturer data
99113
}
114+
}
100115

101-
i += l + 1
116+
// service data
117+
sd := rp.ServiceData()
118+
if len(sd) > 0 {
119+
adf.ServiceData = append(adf.ServiceData, sd...)
102120
}
103121

104-
random := a.hci.advData.peerBdaddrType == 0x01
122+
// manufacturer data
123+
md := rp.ManufacturerData()
124+
if len(md) > 0 {
125+
adf.ManufacturerData = append(adf.ManufacturerData, md...)
126+
}
127+
128+
random := a.hci.advData.peerBdaddrType == GAPAddressTypeRandomStatic
105129

106130
callback(a, ScanResult{
107131
Address: Address{
@@ -163,11 +187,11 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
163187

164188
peerRandom := uint8(0)
165189
if address.isRandom {
166-
peerRandom = 1
190+
peerRandom = GAPAddressTypeRandomStatic
167191
}
168192
localRandom := uint8(0)
169193
if a.hci.address.isRandom {
170-
localRandom = 1
194+
localRandom = GAPAddressTypeRandomStatic
171195
}
172196
if err := a.hci.leCreateConn(0x0060, // interval
173197
0x0030, // window
@@ -334,11 +358,8 @@ func (a *Adapter) DefaultAdvertisement() *Advertisement {
334358
func (a *Advertisement) Configure(options AdvertisementOptions) error {
335359
a.advertisementType = options.AdvertisementType
336360

337-
switch {
338-
case options.LocalName != "":
361+
if options.LocalName != "" {
339362
a.localName = []byte(options.LocalName)
340-
default:
341-
a.localName = []byte("TinyGo")
342363
}
343364

344365
a.serviceUUIDs = append([]UUID{}, options.ServiceUUIDs...)
@@ -369,7 +390,7 @@ func (a *Advertisement) Start() error {
369390

370391
localRandom := uint8(0)
371392
if a.adapter.hci.address.isRandom {
372-
localRandom = 1
393+
localRandom = GAPAddressTypeRandomStatic
373394
}
374395

375396
if err := a.adapter.hci.leSetAdvertisingParameters(a.interval, a.interval,

0 commit comments

Comments
 (0)