Skip to content

Commit 5623054

Browse files
committed
nrf51: add SetRandomAddress() function when Advertisment
Signed-off-by: deadprogram <[email protected]>
1 parent 51f0492 commit 5623054

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

gap_nrf51.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Address struct {
2727
type Advertisement struct {
2828
interval Duration
2929
isAdvertising volatile.Register8
30+
typ uint8
3031
}
3132

3233
var defaultAdvertisement Advertisement
@@ -53,6 +54,17 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
5354
return errAdvertisementPacketTooBig
5455
}
5556

57+
switch options.AdvertisementType {
58+
case AdvertisingTypeInd:
59+
a.typ = C.BLE_GAP_ADV_TYPE_ADV_IND
60+
case AdvertisingTypeDirectInd:
61+
a.typ = C.BLE_GAP_ADV_TYPE_ADV_DIRECT_IND
62+
case AdvertisingTypeScanInd:
63+
a.typ = C.BLE_GAP_ADV_TYPE_ADV_SCAN_IND
64+
case AdvertisingTypeNonConnInd:
65+
a.typ = C.BLE_GAP_ADV_TYPE_ADV_NONCONN_IND
66+
}
67+
5668
errCode := C.sd_ble_gap_adv_data_set((*C.uint8_t)(unsafe.Pointer(&payload.data[0])), C.uint8_t(payload.len), nil, 0)
5769
a.interval = options.Interval
5870
return makeError(errCode)
@@ -76,10 +88,23 @@ func (a *Advertisement) Stop() error {
7688
// is lost.
7789
func (a *Advertisement) start() C.uint32_t {
7890
params := C.ble_gap_adv_params_t{
79-
_type: C.BLE_GAP_ADV_TYPE_ADV_IND,
91+
_type: a.typ,
8092
fp: C.BLE_GAP_ADV_FP_ANY,
8193
interval: C.uint16_t(a.interval),
8294
timeout: 0, // no timeout
8395
}
8496
return C.sd_ble_gap_adv_start_noescape(params)
8597
}
98+
99+
// SetRandomAddress sets the random address to be used for advertising.
100+
func (a *Adapter) SetRandomAddress(mac MAC) error {
101+
var addr C.ble_gap_addr_t
102+
addr.addr = makeSDAddress(mac)
103+
addr.addr_type = C.BLE_GAP_ADDR_TYPE_RANDOM_STATIC
104+
105+
errCode := C.sd_ble_gap_address_set(C.BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr)
106+
if errCode != 0 {
107+
return Error(errCode)
108+
}
109+
return nil
110+
}

0 commit comments

Comments
 (0)