Skip to content

Commit 1da085d

Browse files
authored
[NXP][platform][rw61x] Switch to using BLE mgr impl files from common nxp folder (#34349)
Signed-off-by: Martin Girardot <martin.girardot@nxp.com>
1 parent 4ed9b7e commit 1da085d

File tree

5 files changed

+1271
-11
lines changed

5 files changed

+1271
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
*
3+
* Copyright (c) 2023-2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "BLEAdvertisingArbiter.h"
19+
20+
#include <lib/support/CodeUtils.h>
21+
#include <lib/support/logging/CHIPLogging.h>
22+
#include <system/SystemError.h>
23+
24+
namespace chip {
25+
namespace DeviceLayer {
26+
namespace BLEAdvertisingArbiter {
27+
namespace {
28+
29+
// List of advertising requests ordered by priority
30+
sys_slist_t sRequests;
31+
32+
// Cast an intrusive list node to the containing request object
33+
const BLEAdvertisingArbiter::Request & ToRequest(const sys_snode_t * node)
34+
{
35+
return *static_cast<const BLEAdvertisingArbiter::Request *>(node);
36+
}
37+
38+
// Notify application about stopped advertising if the callback has been provided
39+
void NotifyAdvertisingStopped(const sys_snode_t * node)
40+
{
41+
VerifyOrReturn(node);
42+
43+
const Request & request = ToRequest(node);
44+
45+
if (request.onStopped != nullptr)
46+
{
47+
request.onStopped();
48+
}
49+
}
50+
51+
// Restart advertising using the top-priority request
52+
CHIP_ERROR RestartAdvertising()
53+
{
54+
// Note: bt_le_adv_stop() returns success when the advertising was not started
55+
ReturnErrorOnFailure(MapErrorZephyr(bt_le_adv_stop()));
56+
ReturnErrorCodeIf(sys_slist_is_empty(&sRequests), CHIP_NO_ERROR);
57+
58+
const Request & top = ToRequest(sys_slist_peek_head(&sRequests));
59+
const bt_le_adv_param params = BT_LE_ADV_PARAM_INIT(top.options, top.minInterval, top.maxInterval, nullptr);
60+
const int result = bt_le_adv_start(&params, top.advertisingData.data(), top.advertisingData.size(), top.scanResponseData.data(),
61+
top.scanResponseData.size());
62+
63+
if (top.onStarted != nullptr)
64+
{
65+
top.onStarted(result);
66+
}
67+
68+
return MapErrorZephyr(result);
69+
}
70+
71+
} // namespace
72+
73+
CHIP_ERROR InsertRequest(Request & request)
74+
{
75+
CancelRequest(request);
76+
77+
sys_snode_t * prev = nullptr;
78+
sys_snode_t * node = nullptr;
79+
80+
// Find position of the request in the list that preserves ordering by priority
81+
SYS_SLIST_FOR_EACH_NODE(&sRequests, node)
82+
{
83+
if (request.priority < ToRequest(node).priority)
84+
{
85+
break;
86+
}
87+
88+
prev = node;
89+
}
90+
91+
if (prev == nullptr)
92+
{
93+
NotifyAdvertisingStopped(sys_slist_peek_head(&sRequests));
94+
sys_slist_prepend(&sRequests, &request);
95+
}
96+
else
97+
{
98+
sys_slist_insert(&sRequests, prev, &request);
99+
}
100+
101+
// If the request is top-priority, restart the advertising
102+
if (sys_slist_peek_head(&sRequests) == &request)
103+
{
104+
return RestartAdvertising();
105+
}
106+
107+
return CHIP_NO_ERROR;
108+
}
109+
110+
void CancelRequest(Request & request)
111+
{
112+
const bool isTopPriority = (sys_slist_peek_head(&sRequests) == &request);
113+
VerifyOrReturn(sys_slist_find_and_remove(&sRequests, &request));
114+
115+
// If cancelled request was top-priority, restart the advertising.
116+
if (isTopPriority)
117+
{
118+
RestartAdvertising();
119+
}
120+
}
121+
122+
} // namespace BLEAdvertisingArbiter
123+
124+
/**
125+
* This implements a mapping function for CHIP System Layer errors that allows mapping integers in the number space of the
126+
* Zephyr OS user API stack errors into the POSIX range.
127+
*
128+
* @param[in] aError The native Zephyr API error to map.
129+
*
130+
* @return The mapped POSIX error.
131+
*/
132+
DLL_EXPORT CHIP_ERROR MapErrorZephyr(int aError)
133+
{
134+
return chip::System::Internal::MapErrorPOSIX(-aError);
135+
}
136+
137+
} // namespace DeviceLayer
138+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
*
3+
* Copyright (c) 2023-2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include <lib/support/Span.h>
21+
22+
#include <zephyr/bluetooth/bluetooth.h>
23+
#include <zephyr/sys/slist.h>
24+
25+
#include <cstdint>
26+
27+
/**
28+
* @file
29+
* Bluetooth LE advertising arbiter.
30+
*
31+
* The purpose for this module is to coordinate BLE advertising between
32+
* different application components.
33+
*
34+
* An application component that wants to advertise BLE services is expected to
35+
* define a request with a desired priority, and pass it to the BLE advertising
36+
* arbiter. If there are multiple components that request BLE advertising at the
37+
* same time, the arbiter selects the one with the highest priority (represented
38+
* by the lowest numeric value) and starts the BLE advertising using parameters
39+
* defined in the winning request.
40+
*
41+
* The BLE arbiter does not take ownership of a submitted request, so the
42+
* request object must be sustained until it is cancelled by the application.
43+
*/
44+
45+
namespace chip {
46+
namespace DeviceLayer {
47+
namespace BLEAdvertisingArbiter {
48+
49+
using OnAdvertisingStarted = void (*)(int result);
50+
using OnAdvertisingStopped = void (*)();
51+
52+
struct Request : public sys_snode_t
53+
{
54+
uint8_t priority; ///< Advertising request priority. Lower value means higher priority
55+
uint32_t options; ///< Advertising options: bitmask of BT_LE_ADV_OPT_XXX constants from Zephyr
56+
uint16_t minInterval; ///< Minimum advertising interval in 0.625 ms units
57+
uint16_t maxInterval; ///< Maximum advertising interval in 0.625 ms units
58+
Span<const bt_data> advertisingData; ///< Advertising data fields
59+
Span<const bt_data> scanResponseData; ///< Scan response data fields
60+
OnAdvertisingStarted onStarted; ///< (Optional) Callback invoked when the request becomes top-priority.
61+
OnAdvertisingStopped onStopped; ///< (Optional) Callback invoked when the request stops being top-priority.
62+
};
63+
64+
/**
65+
* @brief Request BLE advertising
66+
*
67+
* Add the request to the internal list of competing requests. If the request
68+
* has higher priority than other requests in the list, restart the BLE
69+
* advertising immediately using parameters defined in the new request.
70+
*
71+
* Inserting a request object that is already registered at the advertising
72+
* arbiter automatically cancels the previous request.
73+
*
74+
* @note This method does not take ownership of the request object so the object
75+
* must not get destroyed before it is cancelled.
76+
*
77+
* @param request Reference to advertising request that contains priority and
78+
* other advertising parameters.
79+
* @return error If the request is top-priority and failed to restart the
80+
* advertising.
81+
* @return success Otherwise.
82+
*/
83+
CHIP_ERROR InsertRequest(Request & request);
84+
85+
/**
86+
* @brief Cancel BLE advertising request
87+
*
88+
* Remove the request from the internal list of competing requests. If the
89+
* request is the winning (top-priority) one at the time of calling this
90+
* function, restart the BLE advertising using parameters defined in the 2nd
91+
* top-priority request in the list, or stop the BLE advertising completely if
92+
* this is the last request in the list.
93+
*
94+
* An attempt to cancel a request that has not been registered at the
95+
* advertising arbiter is a no-op. That is, it returns immediately.
96+
*
97+
* @param request Reference to advertising request that contains priority and
98+
* other advertising parameters.
99+
*/
100+
void CancelRequest(Request & request);
101+
102+
} // namespace BLEAdvertisingArbiter
103+
104+
extern CHIP_ERROR MapErrorZephyr(int code);
105+
106+
} // namespace DeviceLayer
107+
} // namespace chip

0 commit comments

Comments
 (0)