-
Notifications
You must be signed in to change notification settings - Fork 134
/
gap_nrf528xx-advertisement.go
86 lines (74 loc) · 2.44 KB
/
gap_nrf528xx-advertisement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//go:build (softdevice && s113v7) || (softdevice && s132v6) || (softdevice && s140v6) || (softdevice && s140v7)
package bluetooth
import (
"runtime/volatile"
"time"
"unsafe"
)
/*
#include "ble_gap.h"
*/
import "C"
// Address contains a Bluetooth MAC address.
type Address struct {
MACAddress
}
// Advertisement encapsulates a single advertisement instance.
type Advertisement struct {
handle C.uint8_t
isAdvertising volatile.Register8
payload rawAdvertisementPayload
}
// The nrf528xx devices only seem to support one advertisement instance. The way
// multiple advertisements are implemented is by changing the packet data
// frequently.
var defaultAdvertisement = Advertisement{
handle: C.BLE_GAP_ADV_SET_HANDLE_NOT_SET,
}
// DefaultAdvertisement returns the default advertisement instance but does not
// configure it.
func (a *Adapter) DefaultAdvertisement() *Advertisement {
return &defaultAdvertisement
}
// Configure this advertisement.
func (a *Advertisement) Configure(options AdvertisementOptions) error {
// Fill empty options with reasonable defaults.
if options.Interval == 0 {
// Pick an advertisement interval recommended by Apple (section 35.5
// Advertising Interval):
// https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf
options.Interval = NewDuration(152500 * time.Microsecond) // 152.5ms
}
// Construct payload.
// Note that the payload needs to be part of the Advertisement object as the
// memory is still used after sd_ble_gap_adv_set_configure returns.
a.payload.reset()
if !a.payload.addFromOptions(options) {
return errAdvertisementPacketTooBig
}
data := C.ble_gap_adv_data_t{}
data.adv_data = C.ble_data_t{
p_data: (*C.uint8_t)(unsafe.Pointer(&a.payload.data[0])),
len: C.uint16_t(a.payload.len),
}
params := C.ble_gap_adv_params_t{
properties: C.ble_gap_adv_properties_t{
_type: C.BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED,
},
interval: C.uint32_t(options.Interval),
}
errCode := C.sd_ble_gap_adv_set_configure(&a.handle, &data, ¶ms)
return makeError(errCode)
}
// Start advertisement. May only be called after it has been configured.
func (a *Advertisement) Start() error {
a.isAdvertising.Set(1)
errCode := C.sd_ble_gap_adv_start(a.handle, C.BLE_CONN_CFG_TAG_DEFAULT)
return makeError(errCode)
}
// Stop advertisement.
func (a *Advertisement) Stop() error {
a.isAdvertising.Set(0)
errCode := C.sd_ble_gap_adv_stop(a.handle)
return makeError(errCode)
}