Mbed OS Reference
Loading...
Searching...
No Matches
CordioHCIDriver.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2017-2017 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
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#ifndef IMPL_HCI_DRIVER_H_
19#define IMPL_HCI_DRIVER_H_
20
21#include <cstddef>
22#include <cstdint>
23#include "platform/Callback.h"
24#include "ble/common/BLETypes.h"
25#include "ble/driver/CordioHCITransportDriver.h"
26#include "ble/common/blecommon.h"
27
28// FIXME: make this invisible!
29#include "wsf_buf.h"
30
31namespace ble {
32
33/**
34 * Contain description of the memory pool used by the Cordio stack.
35 */
37 /**
38 * Create a new memory pool description
39 * @param buffer the Buffer used by the memory pool.
40 * @param pool_desc How the memory is split
41 */
42 template<size_t BufferSize, size_t PoolCount>
44 uint8_t (&buffer)[BufferSize],
45 const wsfBufPoolDesc_t (&pool_desc)[PoolCount]
46 ) : buffer_memory(buffer), buffer_size(BufferSize),
47 pool_description(pool_desc), pool_count(PoolCount)
48 {
49 }
50
51 uint8_t* buffer_memory; /// Pointer to the buffer memory
52 size_t buffer_size; /// Size of the buffer
53 const wsfBufPoolDesc_t* pool_description; /// Pointer to the first element describing the pool
54 size_t pool_count; /// Number of pools
55};
56
57/**
58 * Base class of the HCI driver use by the BLE port of the Cordio stack.
59 * This class provide to the stack:
60 * - The buffer necessary to run BLE API
61 * - The reset sequence of the BLE module
62 * - Access to the write function of the underlying HCITransport driver.
63 */
65
66 // hook for internal tests and passthrough driver
67 friend class CordioHCIHook;
68
69public:
70 /**
71 * Construct a new instance of an HCI driver.
72 * @param transport_driver The driver used to communicate with the chip.
73 */
75
76 /**
77 * Driver destructor
78 */
79 virtual ~CordioHCIDriver() = default;
80
81 /**
82 * Return the set of memory pool which will be used by the Cordio stack
83 */
85
86 /**
87 * Initialize the HCI driver.
88 * This function start by initializing the transport driver then it delegates
89 * what's remain of the initialization to the function do_initialize.
90 */
91 void initialize();
92
93 /**
94 * Termination of the driver.
95 * It call in sequence:
96 * - do_terminate
97 * - terminate the transport driver.
98 */
99 void terminate();
100
101 /**
102 * Start the reset sequence of the BLE module.
103 */
104 virtual void start_reset_sequence();
105
106 /**
107 * Handle HCI messages received during the reset sequence.
108 *
109 * @param msg The HCI message received.
110 * @note The driver should signal to the stack that the initialization
111 * sequence is done by calling the function: signal_reset_sequence_done.
112 */
113 virtual void handle_reset_sequence(uint8_t *msg);
114
115 /**
116 * Get the random static address of the controller
117 *
118 * @param[out] address MAC address is written here.
119 *
120 * @return false if the address has not been set and true otherwise.
121 */
123
124 /**
125 * Signal to the stack that the reset sequence has been done.
126 */
128
129 /**
130 * Write data in the transport channel.
131 *
132 * @param type The type of packet to transmit. It might be an HCI command
133 * packet, ACL packet or EVT packet. Depending on the type of transport
134 * it can prefix the packet itself.
135 * @param len Number of bytes to transmit.
136 * @param pData pointer to the data to transmit.
137 *
138 * @return The number of bytes which have been transmited.
139 */
140 uint16_t write(uint8_t type, uint16_t len, uint8_t *pData);
141
142 /**
143 * React to host stack inactivity.
144 *
145 * The host stack invoke this function when it is inactive. It allows a
146 * driver to put its controller to sleep if all the conditions are met.
147 *
148 * Any call to write signals to the driver that the host stack is active.
149 */
151
152 /* BLE Tester commands */
153
154 /**
155 * This will be called by host part of the stack to indicate the end of the test.
156 *
157 * @param success True if the TEST END command was a success.
158 * @param packets Number of packets received during the test.
159 */
160 void handle_test_end(bool success, uint16_t packets);
161
162 /** Callback to inform the caller of the result of the test, the parameters are success and the
163 * number of packets received.
164 */
165 typedef mbed::Callback<void(bool, uint16_t)> test_end_handler_t;
166
167 /**
168 * Start BLE receiver test. Call rf_test_end when you want to stop.
169 * @param test_end_handler Handler that will be called with the number of packets received.
170 * @param channel Channel to use.
171 * @return BLE_ERROR_NONE on success.
172 */
174
175 /**
176 * Start BLE transmitter test. Call rf_test_end when you want to stop.
177 * @param test_end_handler Handler that will be called with status and the number of packets set to 0.
178 * @param channel Channel to use.
179 * @param length Size of payload.
180 * @param type Type of pattern to transmit @see BLE spec Volume 6 Part F, Section 4.1.5.
181 * @return BLE_ERROR_NONE on success.
182 */
184 uint8_t length, uint8_t type);
185
186 /**
187 * Complete the test. This will trigger the end test event which will call handle_test_end
188 * @return BLE_ERROR_NONE on success.
189 */
191
192 /**
193 * Set desired transmit power. Value equal or bigger will be used from available levels.
194 * Consult chip documentation for available values. Actual TX power is not guaranteed
195 * and is down to the implementation.
196 *
197 * @param level_db Signal level in dBm.
198 * @return BLE_ERROR_NONE on success.
199 */
200 virtual ble_error_t set_tx_power(int8_t level_db);
201
202protected:
203 /**
204 * Return a default set of memory pool that the Cordio stack can use.
205 * This function can be used to implement get_buffer_pool_description().
206 */
208
209 /**
210 * Allows the driver to set a random static address. Unlike the HCI command
211 * this function reports the random static address to the whole BLE system.
212 * @param random_static_address The random static address to set.
213 */
214 void set_random_static_address(const ble::address_t& random_static_address);
215
216private:
217 /**
218 * Initialize the chip.
219 * The transport is up at that time.
220 */
221 virtual void do_initialize() = 0;
222
223 /**
224 * Terminate the driver
225 */
226 virtual void do_terminate() = 0;
227
228protected:
229 test_end_handler_t _test_end_handler;
230private:
231 CordioHCITransportDriver& _transport_driver;
232};
233
234} // namespace ble
235
236#endif /* IMPL_HCI_DRIVER_H_ */
Base class of the HCI driver use by the BLE port of the Cordio stack.
ble_error_t rf_test_start_le_transmitter_test(test_end_handler_t test_end_handler, uint8_t channel, uint8_t length, uint8_t type)
Start BLE transmitter test.
uint16_t write(uint8_t type, uint16_t len, uint8_t *pData)
Write data in the transport channel.
void handle_test_end(bool success, uint16_t packets)
This will be called by host part of the stack to indicate the end of the test.
void initialize()
Initialize the HCI driver.
void set_random_static_address(const ble::address_t &random_static_address)
Allows the driver to set a random static address.
virtual ble_error_t set_tx_power(int8_t level_db)
Set desired transmit power.
void signal_reset_sequence_done()
Signal to the stack that the reset sequence has been done.
virtual bool get_random_static_address(ble::address_t &address)
Get the random static address of the controller.
virtual void handle_reset_sequence(uint8_t *msg)
Handle HCI messages received during the reset sequence.
virtual ~CordioHCIDriver()=default
Driver destructor.
void terminate()
Termination of the driver.
virtual buf_pool_desc_t get_buffer_pool_description()=0
Return the set of memory pool which will be used by the Cordio stack.
ble_error_t rf_test_end()
Complete the test.
CordioHCIDriver(CordioHCITransportDriver &transport_driver)
Construct a new instance of an HCI driver.
virtual void on_host_stack_inactivity()
React to host stack inactivity.
virtual void start_reset_sequence()
Start the reset sequence of the BLE module.
ble_error_t rf_test_start_le_receiver_test(test_end_handler_t test_end_handler, uint8_t channel)
Start BLE receiver test.
mbed::Callback< void(bool, uint16_t)> test_end_handler_t
Callback to inform the caller of the result of the test, the parameters are success and the number of...
buf_pool_desc_t get_default_buffer_pool_description()
Return a default set of memory pool that the Cordio stack can use.
Base class of the HCI transport driver.
Callback class based on template specialization.
Definition: Callback.h:53
ble_error_t
Error codes for the BLE API.
Entry namespace for all BLE API definitions.
MAC address data type.
Contain description of the memory pool used by the Cordio stack.
size_t buffer_size
Pointer to the buffer memory.
buf_pool_desc_t(uint8_t(&buffer)[BufferSize], const wsfBufPoolDesc_t(&pool_desc)[PoolCount])
Create a new memory pool description.
const wsfBufPoolDesc_t * pool_description
Size of the buffer.
size_t pool_count
Pointer to the first element describing the pool.