C/C++ API Reference
Loading...
Searching...
No Matches
responder.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
15#pragma once
16
17#include "pw_bytes/span.h"
18#include "pw_i2c/address.h"
19#include "pw_result/result.h"
20#include "pw_status/status.h"
21
22namespace pw::i2c {
23
28 public:
29 virtual ~ResponderEvents() = default;
30
36 [[nodiscard]] virtual bool OnStartRead() { return true; }
37
43 [[nodiscard]] virtual bool OnStartWrite() { return true; }
44
55 [[nodiscard]] virtual bool OnWrite(ConstByteSpan data) {
56 // Provide a name for argument to be used in doxygen
57 static_cast<void>(data);
58 return false;
59 }
60
72 [[nodiscard]] virtual Result<ConstByteSpan> OnRead() {
73 return Status::Unimplemented();
74 }
75
81 [[nodiscard]] virtual bool OnStop() { return true; }
82};
83
87class Responder {
88 public:
89 constexpr Responder(Address address, ResponderEvents& events)
90 : address_(address), events_(events) {}
91 virtual ~Responder() = default;
92
99 Status Enable() { return DoEnable(); }
100
106 Status Disable() { return DoDisable(); }
107
108 protected:
110 virtual Status DoEnable() = 0;
111
113 virtual Status DoDisable() = 0;
114
122 bool OnStartRead() { return events_.OnStartRead(); }
123
131 bool OnStartWrite() { return events_.OnStartWrite(); }
132
141 bool OnWrite(ConstByteSpan data) { return events_.OnWrite(data); }
142
151 Result<ConstByteSpan> OnRead() { return events_.OnRead(); }
152
159 bool OnStop() { return events_.OnStop(); }
160
162 const Address& address() const { return address_; }
163
164 private:
165 const Address address_;
166 ResponderEvents& events_;
167};
168
169} // namespace pw::i2c
Definition: result.h:143
Definition: status.h:120
static constexpr Status Unimplemented()
Definition: status.h:280
Definition: address.h:42
Definition: responder.h:27
virtual bool OnWrite(ConstByteSpan data)
Definition: responder.h:55
virtual bool OnStartRead()
Definition: responder.h:36
virtual bool OnStop()
Definition: responder.h:81
virtual Result< ConstByteSpan > OnRead()
Definition: responder.h:72
virtual bool OnStartWrite()
Definition: responder.h:43
Definition: responder.h:87
Result< ConstByteSpan > OnRead()
Definition: responder.h:151
const Address & address() const
Definition: responder.h:162
bool OnStop()
Definition: responder.h:159
virtual Status DoEnable()=0
Implementation of Enable()
bool OnWrite(ConstByteSpan data)
Definition: responder.h:141
Status Enable()
Definition: responder.h:99
bool OnStartRead()
Definition: responder.h:122
virtual Status DoDisable()=0
Implementation of Disable()
Status Disable()
Definition: responder.h:106
bool OnStartWrite()
Definition: responder.h:131
Cross-platform I2C library.
Definition: address.h:19