|
| 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