forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEAdvertisingArbiter.cpp
151 lines (122 loc) · 3.87 KB
/
BLEAdvertisingArbiter.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "BLEAdvertisingArbiter.h"
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <system/SystemError.h>
namespace chip {
namespace DeviceLayer {
namespace BLEAdvertisingArbiter {
namespace {
// List of advertising requests ordered by priority
sys_slist_t sRequests;
bool sIsInitialized = false;
uint8_t sBtId = 0;
// Cast an intrusive list node to the containing request object
const BLEAdvertisingArbiter::Request & ToRequest(const sys_snode_t * node)
{
return *static_cast<const BLEAdvertisingArbiter::Request *>(node);
}
// Notify application about stopped advertising if the callback has been provided
void NotifyAdvertisingStopped(const sys_snode_t * node)
{
VerifyOrReturn(node);
const Request & request = ToRequest(node);
if (request.onStopped != nullptr)
{
request.onStopped();
}
}
// Restart advertising using the top-priority request
CHIP_ERROR RestartAdvertising()
{
// Note: bt_le_adv_stop() returns success when the advertising was not started
ReturnErrorOnFailure(System::MapErrorZephyr(bt_le_adv_stop()));
ReturnErrorCodeIf(sys_slist_is_empty(&sRequests), CHIP_NO_ERROR);
const Request & top = ToRequest(sys_slist_peek_head(&sRequests));
bt_le_adv_param params = BT_LE_ADV_PARAM_INIT(top.options, top.minInterval, top.maxInterval, nullptr);
params.id = sBtId;
const int result = bt_le_adv_start(¶ms, top.advertisingData.data(), top.advertisingData.size(), top.scanResponseData.data(),
top.scanResponseData.size());
if (top.onStarted != nullptr)
{
top.onStarted(result);
}
return System::MapErrorZephyr(result);
}
} // namespace
CHIP_ERROR Init(uint8_t btId)
{
if (sIsInitialized)
{
return CHIP_ERROR_INCORRECT_STATE;
}
sBtId = btId;
sIsInitialized = true;
return CHIP_NO_ERROR;
}
CHIP_ERROR InsertRequest(Request & request)
{
if (!sIsInitialized)
{
return CHIP_ERROR_INCORRECT_STATE;
}
CancelRequest(request);
sys_snode_t * prev = nullptr;
sys_snode_t * node = nullptr;
// Find position of the request in the list that preserves ordering by priority
SYS_SLIST_FOR_EACH_NODE(&sRequests, node)
{
if (request.priority < ToRequest(node).priority)
{
break;
}
prev = node;
}
if (prev == nullptr)
{
NotifyAdvertisingStopped(sys_slist_peek_head(&sRequests));
sys_slist_prepend(&sRequests, &request);
}
else
{
sys_slist_insert(&sRequests, prev, &request);
}
// If the request is top-priority, restart the advertising
if (sys_slist_peek_head(&sRequests) == &request)
{
return RestartAdvertising();
}
return CHIP_NO_ERROR;
}
void CancelRequest(Request & request)
{
if (!sIsInitialized)
{
return;
}
const bool isTopPriority = (sys_slist_peek_head(&sRequests) == &request);
VerifyOrReturn(sys_slist_find_and_remove(&sRequests, &request));
// If cancelled request was top-priority, restart the advertising.
if (isTopPriority)
{
RestartAdvertising();
}
}
} // namespace BLEAdvertisingArbiter
} // namespace DeviceLayer
} // namespace chip