1 // Copyright 2023 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 <memory> 19 #include <utility> 20 21 #include "pw_function/function.h" 22 #include "pw_i2c/i2c.pwpb.h" 23 #include "pw_i2c/i2c.rpc.pwpb.h" 24 #include "pw_i2c/initiator.h" 25 #include "pw_rpc/pwpb/server_reader_writer.h" 26 27 namespace pw::i2c { 28 29 /// RPC service for performing I2C transactions. 30 class I2cService final : public pw_rpc::pwpb::I2c::Service<I2cService> { 31 public: 32 /// A callback that returns a `pw::i2c::Initiator` instance for the given 33 /// bus index position or ``nullptr`` if the position is not valid for this 34 /// I2C device. 35 using InitiatorSelector = pw::Function<Initiator*(size_t pos)>; 36 37 /// Creates an I2cService instance. I2cService(InitiatorSelector && initiator_selector)38 explicit I2cService(InitiatorSelector&& initiator_selector) 39 : initiator_selector_(std::move(initiator_selector)) {} 40 41 /// Writes a message to the specified I2C device register. 42 void I2cWrite( 43 const pwpb::I2cWriteRequest::Message& request, 44 pw::rpc::PwpbUnaryResponder<pwpb::I2cWriteResponse::Message>& responder); 45 46 /// Reads a message from the specified I2C device register. 47 void I2cRead( 48 const pwpb::I2cReadRequest::Message& request, 49 pw::rpc::PwpbUnaryResponder<pwpb::I2cReadResponse::Message>& responder); 50 51 private: 52 InitiatorSelector initiator_selector_; 53 }; 54 55 } // namespace pw::i2c 56