Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hci: implement set random MAC address #325

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion adapter_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,25 @@ func (a *hciAdapter) enable() error {
}

func (a *hciAdapter) Address() (MACAddress, error) {
var empty MAC
if a.hci.address.MAC != empty {
return a.hci.address, nil
}

if err := a.hci.readBdAddr(); err != nil {
return MACAddress{}, err
}

return MACAddress{MAC: makeAddress(a.hci.address)}, nil
return a.hci.address, nil
}

func (a *Adapter) SetRandomAddress(mac MAC) error {
if err := a.hci.sendCommandWithParams(ogfLECtrl<<ogfCommandPos|ocfLESetRandomAddress, mac[:]); err != nil {
return err
}
copy(a.hci.address.MAC[:], mac[:])
a.hci.address.SetRandom(true)
return nil
}

func newBLEStack(port hciTransport) (*hci, *att) {
Expand Down
20 changes: 15 additions & 5 deletions gap_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,27 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
println("Connect")
}

random := uint8(0)
peerRandom := uint8(0)
if address.isRandom {
random = 1
peerRandom = 1
}
localRandom := uint8(0)
if a.hci.address.isRandom {
localRandom = 1
}
if err := a.hci.leCreateConn(0x0060, // interval
0x0030, // window
0x00, // initiatorFilter
random, // peerBdaddrType
peerRandom, // peerBdaddrType
makeNINAAddress(address.MAC), // peerBdaddr
0x00, // ownBdaddrType
localRandom, // ownBdaddrType
0x0006, // minInterval
0x000c, // maxInterval
0x0000, // latency
0x00c8, // supervisionTimeout
0x0004, // minCeLength
0x0006); err != nil { // maxCeLength

return Device{}, err
}

Expand Down Expand Up @@ -353,8 +358,13 @@ func (a *Advertisement) Start() error {
// uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
typ := uint8(a.advertisementType)

localRandom := uint8(0)
if a.adapter.hci.address.isRandom {
localRandom = 1
}

if err := a.adapter.hci.leSetAdvertisingParameters(a.interval, a.interval,
typ, 0x00, 0x00, [6]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0x07, 0); err != nil {
typ, localRandom, 0x00, [6]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0x07, 0); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type hci struct {
pos int
end int
writebuf []byte
address [6]byte
address MACAddress
cmdCompleteOpcode uint16
cmdCompleteStatus uint8
cmdResponse []byte
Expand Down Expand Up @@ -318,7 +318,7 @@ func (h *hci) readBdAddr() error {
return err
}

copy(h.address[:], h.cmdResponse[:7])
copy(h.address.MAC[:], h.cmdResponse[:7])

return nil
}
Expand Down
Loading