Skip to content

Commit 3778f18

Browse files
committed
chore: complete moving findmy lib and remove some stuttering in the naming
Signed-off-by: deadprogram <[email protected]>
1 parent c22786c commit 3778f18

File tree

5 files changed

+36
-43
lines changed

5 files changed

+36
-43
lines changed

firmware/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/hybridgroup/go-haystack/firmware
22

33
go 1.23.0
44

5-
require tinygo.org/x/bluetooth v0.10.1-0.20250109131232-43edf72c9496
5+
require (
6+
github.com/hybridgroup/go-haystack v0.0.0-20250111071702-c22786c33269
7+
tinygo.org/x/bluetooth v0.10.1-0.20250109131232-43edf72c9496
8+
)
69

710
require (
811
github.com/go-ole/go-ole v1.2.6 // indirect

firmware/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
55
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
66
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
77
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
8+
github.com/hybridgroup/go-haystack v0.0.0-20250111071702-c22786c33269 h1:99eSocBWMtc4jGlXqubSDIJUafkyKInt38fIRxh2xKs=
9+
github.com/hybridgroup/go-haystack v0.0.0-20250111071702-c22786c33269/go.mod h1:DvH8VgHcL/L57TkU2JjwjZSIb+CBD5kD4H2ijx/9f7w=
810
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
911
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1012
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

firmware/main.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"errors"
1414
"time"
1515

16+
"github.com/hybridgroup/go-haystack/lib/findmy"
1617
"tinygo.org/x/bluetooth"
1718
)
1819

@@ -31,7 +32,7 @@ func main() {
3132
opts := bluetooth.AdvertisementOptions{
3233
AdvertisementType: bluetooth.AdvertisingTypeNonConnInd,
3334
Interval: bluetooth.NewDuration(1285000 * time.Microsecond), // 1285ms
34-
ManufacturerData: []bluetooth.ManufacturerDataElement{findMyData(key)},
35+
ManufacturerData: []bluetooth.ManufacturerDataElement{findmy.NewData(key)},
3536
}
3637

3738
must("enable BLE stack", adapter.Enable())
@@ -66,39 +67,6 @@ func getKeyData() ([]byte, error) {
6667
return val, nil
6768
}
6869

69-
const (
70-
// Apple, Inc.
71-
appleCompanyID = 0x004C
72-
73-
// Offline Finding type
74-
findMyPayloadType = 0x12
75-
76-
// Length of the payload
77-
findMyPayloadLength = 0x19
78-
79-
// Status byte
80-
findMyStatus = 0x10
81-
82-
// Hint byte
83-
findMyHint = 0x00
84-
)
85-
86-
// findMyData creates the ManufacturerDataElement for the advertising data used by FindMy devices.
87-
// See https://adamcatley.com/AirTag.html#advertising-data
88-
func findMyData(keyData []byte) bluetooth.ManufacturerDataElement {
89-
data := make([]byte, 0, 27)
90-
data = append(data, findMyPayloadType, findMyPayloadLength)
91-
data = append(data, findMyStatus)
92-
data = append(data, keyData[6:]...) // copy last 22 bytes of advertising key
93-
data = append(data, (keyData[0] >> 6)) // first two bits of advertising key
94-
data = append(data, findMyHint)
95-
96-
return bluetooth.ManufacturerDataElement{
97-
CompanyID: appleCompanyID,
98-
Data: data,
99-
}
100-
}
101-
10270
// must calls a function and fails if an error occurs.
10371
func must(action string, err error) {
10472
if err != nil {

lib/findmy/parse.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ const (
1111
AppleCompanyID = 0x004C
1212

1313
// Offline Finding type
14-
FindMyPayloadType = 0x12
14+
PayloadType = 0x12
1515

1616
// Length of the payload
17-
FindMyPayloadLength = 0x19
17+
PayloadLength = 0x19
1818

1919
// Hint byte
20-
FindMyHint = 0x00
20+
Hint = 0x00
21+
22+
// Default Status byte
23+
DefaultStatus = 0x10
2124
)
2225

2326
var (
@@ -34,15 +37,15 @@ func ParseData(address bluetooth.Address, data []byte) (byte, []byte, error) {
3437
return 0, nil, ErrorDataTooShort
3538
}
3639

37-
if data[0] != FindMyPayloadType {
40+
if data[0] != PayloadType {
3841
return 0, nil, ErrorInvalidPayloadType
3942
}
4043

41-
if data[1] != FindMyPayloadLength {
44+
if data[1] != PayloadLength {
4245
return 0, nil, ErrorInvalidPayloadLength
4346
}
4447

45-
if data[26] != FindMyHint {
48+
if data[26] != Hint {
4649
return 0, nil, ErrorInvalidHint
4750
}
4851

@@ -60,3 +63,19 @@ func ParseData(address bluetooth.Address, data []byte) (byte, []byte, error) {
6063

6164
return findMyStatus, key[:], nil
6265
}
66+
67+
// NewData creates the ManufacturerDataElement for the advertising data used by FindMy devices.
68+
// See https://adamcatley.com/AirTag.html#advertising-data
69+
func NewData(keyData []byte) bluetooth.ManufacturerDataElement {
70+
data := make([]byte, 0, 27)
71+
data = append(data, PayloadType, PayloadLength)
72+
data = append(data, DefaultStatus)
73+
data = append(data, keyData[6:]...) // copy last 22 bytes of advertising key
74+
data = append(data, (keyData[0] >> 6)) // first two bits of advertising key
75+
data = append(data, Hint)
76+
77+
return bluetooth.ManufacturerDataElement{
78+
CompanyID: AppleCompanyID,
79+
Data: data,
80+
}
81+
}

tinyscan/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"image/color"
77
"time"
88

9+
"github.com/hybridgroup/go-haystack/lib/findmy"
910
"tinygo.org/x/bluetooth"
1011
"tinygo.org/x/tinyterm"
1112
)
@@ -36,8 +37,8 @@ func main() {
3637
}
3738

3839
func scanHandler(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
39-
if device.ManufacturerData() != nil && device.ManufacturerData()[0].CompanyID == appleCompanyID {
40-
status, key, err := parseData(device.Address, device.ManufacturerData()[0].Data)
40+
if device.ManufacturerData() != nil && device.ManufacturerData()[0].CompanyID == findmy.AppleCompanyID {
41+
status, key, err := findmy.ParseData(device.Address, device.ManufacturerData()[0].Data)
4142
switch {
4243
case err != nil:
4344
terminalOutput("ERROR: failed to parse data:" + err.Error())

0 commit comments

Comments
 (0)