HardFOC Core
API reference
Loading...
Searching...
No Matches
Se050Handler.h
Go to the documentation of this file.
1
24#ifndef COMPONENT_HANDLER_SE050_HANDLER_H_
25#define COMPONENT_HANDLER_SE050_HANDLER_H_
26
27#include <atomic>
28#include <cstddef>
29#include <cstdint>
30
31#include "RtosMutex.h"
32#include "base/BaseGpio.h"
33#include "base/BaseI2c.h"
34
35#include "core/hf-core-drivers/external/hf-se050-driver/inc/se050_device.hpp"
36#include "core/hf-core-drivers/external/hf-se050-driver/inc/se050_i2c_transport_interface.hpp"
37
38//==============================================================================
39// CRTP I2C ADAPTER (BaseI2c → se050 transport)
40//==============================================================================
41
52class HalI2cSe050Comm : public se050::I2cTransceiveInterface<HalI2cSe050Comm> {
53public:
59 HalI2cSe050Comm(BaseI2c& i2c, BaseGpio* reset_gpio) noexcept : i2c_(i2c), reset_gpio_(reset_gpio) {}
60
61 [[nodiscard]] bool EnsureInitialized() noexcept { return i2c_.EnsureInitialized(); }
62
69 [[nodiscard]] se050::Error I2cWrite(const std::uint8_t* tx, std::size_t tx_len,
70 std::uint32_t timeout_ms) noexcept;
71
78 [[nodiscard]] se050::Error I2cRead(std::uint8_t* rx, std::size_t rx_len,
79 std::uint32_t timeout_ms) noexcept;
80
90 [[nodiscard]] se050::Error Transceive(const std::uint8_t* tx, std::size_t tx_len, std::uint8_t* rx,
91 std::size_t rx_cap, std::size_t* rx_len_out,
92 std::uint32_t timeout_ms) noexcept;
93
94 [[nodiscard]] se050::Error HardwareReset() noexcept;
95
96 void delay_ms_impl(std::uint32_t ms) noexcept;
97
102 [[nodiscard]] bool HoldBus(std::uint32_t timeout_ms) noexcept;
103 void ReleaseBus() noexcept;
104 [[nodiscard]] bool BusHeld() const noexcept { return bus_held_; }
105
106private:
107 BaseI2c& i2c_;
108 BaseGpio* reset_gpio_;
109 bool bus_held_{false};
110};
111
112//==============================================================================
113// Handler configuration
114//==============================================================================
115
119 std::uint32_t apdu_timeout_ms{300};
121 std::uint32_t warm_reset_timeout_ms{200};
123 std::uint32_t t1_inter_frame_delay_ms{2};
126};
127
128//==============================================================================
129// Handler
130//==============================================================================
131
145public:
146 using DeviceType = se050::Device<HalI2cSe050Comm>;
147
154 explicit Se050Handler(BaseI2c& i2c, const Se050HandlerConfig& config = Se050HandlerConfig{},
155 BaseGpio* reset_gpio = nullptr, RtosMutex* bus_mutex = nullptr) noexcept;
156
157 Se050Handler(const Se050Handler&) = delete;
161
162 [[nodiscard]] const Se050HandlerConfig& Config() const noexcept { return config_; }
163
168 bool EnsureInitialized() noexcept;
169
170 [[nodiscard]] bool IsPresent() const noexcept {
171 return initialized_.load(std::memory_order_acquire);
172 }
173
175 [[nodiscard]] DeviceType& GetDevice() noexcept { return device_; }
176 [[nodiscard]] const DeviceType& GetDevice() const noexcept { return device_; }
177
178private:
179 bool EnsureInitializedLocked() noexcept;
180
184
185 std::atomic<bool> initialized_{false};
186
187 RtosMutex private_mutex_{};
188 RtosMutex* bus_mutex_;
189};
190
191#endif // COMPONENT_HANDLER_SE050_HANDLER_H_
CRTP adapter mapping BaseI2c primitives to se050::I2cTransceiveInterface.
Definition Se050Handler.h:52
void ReleaseBus() noexcept
Definition Se050Handler.cpp:232
se050::Error Transceive(const std::uint8_t *tx, std::size_t tx_len, std::uint8_t *rx, std::size_t rx_cap, std::size_t *rx_len_out, std::uint32_t timeout_ms) noexcept
Combined write-then-read (T=1oI2C frame).
Definition Se050Handler.cpp:148
se050::Error I2cWrite(const std::uint8_t *tx, std::size_t tx_len, std::uint32_t timeout_ms) noexcept
Write bytes on I²C.
Definition Se050Handler.cpp:67
bool HoldBus(std::uint32_t timeout_ms) noexcept
Hold the I²C bus across a T=1 APDU (Write/Read pairs). Nested I2cWrite/Read skip lock.
Definition Se050Handler.cpp:210
HalI2cSe050Comm(BaseI2c &i2c, BaseGpio *reset_gpio) noexcept
Construct the CRTP I²C adapter.
Definition Se050Handler.h:59
se050::Error HardwareReset() noexcept
Definition Se050Handler.cpp:193
se050::Error I2cRead(std::uint8_t *rx, std::size_t rx_len, std::uint32_t timeout_ms) noexcept
Read bytes on I²C.
Definition Se050Handler.cpp:110
bool BusHeld() const noexcept
Definition Se050Handler.h:104
bool EnsureInitialized() noexcept
Definition Se050Handler.h:61
BaseI2c & i2c_
Definition Se050Handler.h:107
void delay_ms_impl(std::uint32_t ms) noexcept
Definition Se050Handler.cpp:208
BaseGpio * reset_gpio_
Definition Se050Handler.h:108
bool bus_held_
Definition Se050Handler.h:109
Thread-safe façade around se050::Device<HalI2cSe050Comm>.
Definition Se050Handler.h:144
bool EnsureInitialized() noexcept
Initialize I²C, pulse optional reset GPIO, run T=1 warm reset, SELECT applet, and verify with GetVers...
Definition Se050Handler.cpp:255
bool IsPresent() const noexcept
Definition Se050Handler.h:170
RtosMutex * bus_mutex_
Definition Se050Handler.h:188
Se050Handler(Se050Handler &&)=delete
se050::Device< HalI2cSe050Comm > DeviceType
Definition Se050Handler.h:146
DeviceType & GetDevice() noexcept
Direct access to the full se050::Device API (lock externally if needed).
Definition Se050Handler.h:175
DeviceType device_
Definition Se050Handler.h:183
Se050Handler & operator=(const Se050Handler &)=delete
const Se050HandlerConfig & Config() const noexcept
Definition Se050Handler.h:162
bool EnsureInitializedLocked() noexcept
Definition Se050Handler.cpp:263
Se050HandlerConfig config_
Definition Se050Handler.h:181
RtosMutex private_mutex_
Definition Se050Handler.h:187
const DeviceType & GetDevice() const noexcept
Definition Se050Handler.h:176
Se050Handler(const Se050Handler &)=delete
std::atomic< bool > initialized_
Definition Se050Handler.h:185
Se050Handler & operator=(Se050Handler &&)=delete
HalI2cSe050Comm comm_
Definition Se050Handler.h:182
Runtime tuning for one SE050 instance on a BaseI2c device.
Definition Se050Handler.h:117
std::uint32_t apdu_timeout_ms
Timeout for management / crypto APDUs (GetVersion, GetRandom, …).
Definition Se050Handler.h:119
bool skip_chip_warm_reset
When true, skip ChipWarmReset (caller already did T=1 warm-reset + ATR).
Definition Se050Handler.h:125
std::uint32_t t1_inter_frame_delay_ms
Inter-frame delay before reading a T=1 response (ms).
Definition Se050Handler.h:123
std::uint32_t warm_reset_timeout_ms
T=1 warm-reset S-block exchange timeout.
Definition Se050Handler.h:121