Pigweed
 
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
36 public:
37 enum Direction {
38 kMockRead,
39 kMockWrite,
40 };
41 constexpr MockMessage(Status expected_return_value,
43 Direction direction,
44 ConstByteSpan data_buffer)
45 : return_value_(expected_return_value),
46 address_(address),
47 direction_(direction),
48 data_buffer_(data_buffer) {}
49
51 constexpr MockMessage(Status expected_return_value, Address device_address)
52 : MockMessage(expected_return_value,
53 device_address,
54 MockMessage::kMockRead,
55 ignored_buffer_) {}
56
58 Status return_value() const { return return_value_; }
59
61 Address address() const { return address_; }
62
63 Direction direction() const { return direction_; }
64
66 ConstByteSpan data_buffer() const { return data_buffer_; }
67
68 private:
69 const Status return_value_;
70 const Address address_;
71 const Direction direction_;
72 const ConstByteSpan data_buffer_;
73 static constexpr std::array<std::byte, 1> ignored_buffer_ = {};
74};
75
76constexpr MockMessage MockReadMessage(Status expected_return_value,
77 Address address,
78 ConstByteSpan data_buffer) {
79 return MockMessage(
80 expected_return_value, address, MockMessage::kMockRead, data_buffer);
81}
82
83constexpr MockMessage MockWriteMessage(Status expected_return_value,
84 Address address,
85 ConstByteSpan data_buffer) {
86 return MockMessage(
87 expected_return_value, address, MockMessage::kMockWrite, data_buffer);
88}
89
90constexpr MockMessage MockProbeMessage(Status expected_return_value,
91 Address address) {
92 return MockMessage(expected_return_value, address);
93}
94
95// Makes a new i2c messages list.
96template <size_t kSize>
97constexpr std::array<MockMessage, kSize> MakeExpectedMessageArray(
98 const MockMessage (&messages)[kSize]) {
99 return containers::to_array(messages);
100}
101
107 public:
109 Status expected_return_value,
110 span<const MockMessage> test_messages,
111 std::optional<chrono::SystemClock::duration> timeout = std::nullopt)
112 : return_value_(expected_return_value),
113 test_messages_(test_messages.begin(), test_messages.end()),
114 timeout_(timeout) {}
116 std::optional<chrono::SystemClock::duration> timeout() const {
117 return timeout_;
118 }
119
120 const std::vector<MockMessage>& test_messages() const {
121 return test_messages_;
122 }
123
124 Status return_value() const { return return_value_; }
125
126 private:
127 const Status return_value_;
128 const std::vector<MockMessage> test_messages_;
129 const std::optional<chrono::SystemClock::duration> timeout_;
130};
131
187 public:
188 explicit MockMessageInitiator(span<MockMessageTransaction> transaction_list)
189 : Initiator(Initiator::Feature::kStandard),
190 expected_transactions_(transaction_list),
191 expected_transaction_index_(0) {}
192
205 Status Finalize() const {
206 if (expected_transaction_index_ != expected_transactions_.size()) {
207 return Status::OutOfRange();
208 }
209 return Status();
210 }
211
215
216 private:
217 // Implements a mocked backend for the i2c initiator.
218 //
219 // Expects (via Gtest):
220 // messages.size() == next_expected_transaction.messages.size()
221 // For each element in messages, a corresponding and matching element
222 // should exist in the next expected transaction's message list.
223 //
224 // Asserts:
225 // When the number of calls to this method exceed the number of expected
226 // transactions.
227 //
228 // If the number of elements in the next expected transaction's message list
229 // does not equal the number of elements in messages.
230 //
231 // The byte size of any individual expected transaction message does not
232 // equal the byte size of the corresponding element in messages.
233 //
234 // Returns:
235 // Specified transaction return type
236 Status DoTransferFor(span<const Message> messages,
237 chrono::SystemClock::duration timeout) override;
238
239 span<MockMessageTransaction> expected_transactions_;
240 size_t expected_transaction_index_;
241};
242
243// Makes a new list of i2c transactions. Each transaction is made up of
244// individual read and write messages transmitted together on the i2c bus.
245template <size_t kSize>
246constexpr std::array<MockMessageTransaction, kSize>
247MakeExpectedTransactionArray(
248 const MockMessageTransaction (&transactions)[kSize]) {
249 return containers::to_array(transactions);
250}
251
252} // namespace pw::i2c
Definition: status.h:85
Definition: address.h:39
The common, base driver interface for initiating thread-safe transactions with devices on an I2C bus....
Definition: initiator.h:49
constexpr Initiator()
Definition: initiator.h:68
Definition: initiator_message_mock.h:35
Address address() const
Gets the I2C address that the I2C transaction is targeting.
Definition: initiator_message_mock.h:61
constexpr MockMessage(Status expected_return_value, Address device_address)
Alternative constructor for creating probe transactions.
Definition: initiator_message_mock.h:51
Status return_value() const
Gets the expected return value for the transaction.
Definition: initiator_message_mock.h:58
ConstByteSpan data_buffer() const
Gets the buffer that is virtually read.
Definition: initiator_message_mock.h:66
Definition: initiator_message_mock.h:186
Status Finalize() const
Definition: initiator_message_mock.h:205
Definition: initiator_message_mock.h:106
std::optional< chrono::SystemClock::duration > timeout() const
Gets the minimum duration to wait for a blocking I2C transaction.
Definition: initiator_message_mock.h:116
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:86