C/C++ API Reference
Loading...
Searching...
No Matches
initiator_message_mock.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <array>
17#include <cstddef>
18#include <optional>
19#include <vector>
20
21#include "pw_bytes/span.h"
22#include "pw_containers/to_array.h"
23#include "pw_i2c/initiator.h"
24
25namespace pw::i2c {
26
28
38 public:
39 enum Direction {
40 kMockRead,
41 kMockWrite,
42 };
43 constexpr MockMessage(Status expected_return_value,
45 Direction direction,
47 : return_value_(expected_return_value),
48 address_(address),
49 direction_(direction),
50 data_buffer_(data_buffer) {}
51
53 constexpr MockMessage(Status expected_return_value, Address device_address)
54 : MockMessage(expected_return_value,
55 device_address,
56 MockMessage::kMockRead,
57 ignored_buffer_) {}
58
60 Status return_value() const { return return_value_; }
61
63 Address address() const { return address_; }
64
65 Direction direction() const { return direction_; }
66
68 ConstByteSpan data_buffer() const { return data_buffer_; }
69
70 private:
71 const Status return_value_;
72 const Address address_;
73 const Direction direction_;
74 const ConstByteSpan data_buffer_;
75 static constexpr std::array<std::byte, 1> ignored_buffer_ = {};
76};
77
78constexpr MockMessage MockReadMessage(Status expected_return_value,
79 Address address,
80 ConstByteSpan data_buffer) {
81 return MockMessage(
82 expected_return_value, address, MockMessage::kMockRead, data_buffer);
83}
84
85constexpr MockMessage MockWriteMessage(Status expected_return_value,
86 Address address,
87 ConstByteSpan data_buffer) {
88 return MockMessage(
89 expected_return_value, address, MockMessage::kMockWrite, data_buffer);
90}
91
92constexpr MockMessage MockProbeMessage(Status expected_return_value,
93 Address address) {
94 return MockMessage(expected_return_value, address);
95}
96
97// Makes a new i2c messages list.
98template <size_t kSize>
99constexpr std::array<MockMessage, kSize> MakeExpectedMessageArray(
100 const MockMessage (&messages)[kSize]) {
101 return containers::to_array(messages);
102}
103
109 public:
111 Status expected_return_value,
112 span<const MockMessage> test_messages,
113 std::optional<chrono::SystemClock::duration> timeout = std::nullopt)
114 : return_value_(expected_return_value),
115 test_messages_(test_messages.begin(), test_messages.end()),
116 timeout_(timeout) {}
118 std::optional<chrono::SystemClock::duration> timeout() const {
119 return timeout_;
120 }
121
122 const std::vector<MockMessage>& test_messages() const {
123 return test_messages_;
124 }
125
126 Status return_value() const { return return_value_; }
127
128 private:
129 const Status return_value_;
130 const std::vector<MockMessage> test_messages_;
131 const std::optional<chrono::SystemClock::duration> timeout_;
132};
133
189 public:
190 explicit MockMessageInitiator(span<MockMessageTransaction> transaction_list)
191 : Initiator(Initiator::Feature::kStandard),
192 expected_transactions_(transaction_list),
193 expected_transaction_index_(0) {}
194
207 Status Finalize() const {
208 if (expected_transaction_index_ != expected_transactions_.size()) {
209 return Status::OutOfRange();
210 }
211 return Status();
212 }
213
217
218 private:
219 // Implements a mocked backend for the i2c initiator.
220 //
221 // Expects (via Gtest):
222 // messages.size() == next_expected_transaction.messages.size()
223 // For each element in messages, a corresponding and matching element
224 // should exist in the next expected transaction's message list.
225 //
226 // Asserts:
227 // When the number of calls to this method exceed the number of expected
228 // transactions.
229 //
230 // If the number of elements in the next expected transaction's message list
231 // does not equal the number of elements in messages.
232 //
233 // The byte size of any individual expected transaction message does not
234 // equal the byte size of the corresponding element in messages.
235 //
236 // Returns:
237 // Specified transaction return type
238 Status DoTransferFor(span<const Message> messages,
239 chrono::SystemClock::duration timeout) override;
240
241 span<MockMessageTransaction> expected_transactions_;
242 size_t expected_transaction_index_;
243};
244
245// Makes a new list of i2c transactions. Each transaction is made up of
246// individual read and write messages transmitted together on the i2c bus.
247template <size_t kSize>
248constexpr std::array<MockMessageTransaction, kSize>
249MakeExpectedTransactionArray(
250 const MockMessageTransaction (&transactions)[kSize]) {
251 return containers::to_array(transactions);
252}
253
254} // namespace pw::i2c
Definition: status.h:86
Definition: address.h:42
The common, base driver interface for initiating thread-safe transactions with devices on an I2C bus....
Definition: initiator.h:52
Definition: initiator_message_mock.h:37
Definition: initiator_message_mock.h:188
Definition: initiator_message_mock.h:108
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:90
constexpr std::array< std::remove_cv_t< T >, kSize > to_array(T(&values)[kSize])
Definition: to_array.h:55
constexpr Initiator()
Definition: initiator.h:71
Address address() const
Gets the I2C address that the I2C transaction is targeting.
Definition: initiator_message_mock.h:63
constexpr MockMessage(Status expected_return_value, Address device_address)
Alternative constructor for creating probe transactions.
Definition: initiator_message_mock.h:53
Status return_value() const
Gets the expected return value for the transaction.
Definition: initiator_message_mock.h:60
std::optional< chrono::SystemClock::duration > timeout() const
Gets the minimum duration to wait for a blocking I2C transaction.
Definition: initiator_message_mock.h:118
Status Finalize() const
Definition: initiator_message_mock.h:207
ConstByteSpan data_buffer() const
Gets the buffer that is virtually read.
Definition: initiator_message_mock.h:68
Cross-platform I2C library.
Definition: address.h:19