blob: 8ad045b6decfd0eb49de0e2388bd9284b8376618 [file] [log] [blame]
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <span>
#include "pw_rpc/internal/channel.h"
#include "pw_rpc/internal/method.h"
#include "pw_rpc/internal/packet.h"
#include "pw_rpc/internal/server.h"
namespace pw::rpc {
template <size_t buffer_size>
class TestOutput : public ChannelOutput {
public:
constexpr TestOutput(const char* name = "TestOutput")
: ChannelOutput(name), sent_packet_{} {}
std::span<std::byte> AcquireBuffer() override { return buffer_; }
void SendAndReleaseBuffer(size_t size) override {
sent_packet_ = std::span(buffer_.data(), size);
}
std::span<const std::byte> buffer() const { return buffer_; }
const std::span<const std::byte>& sent_packet() const { return sent_packet_; }
private:
std::array<std::byte, buffer_size> buffer_;
std::span<const std::byte> sent_packet_;
};
// Version of the internal::Server with extra methods exposed for testing.
class TestServer : public internal::Server {
public:
using internal::Server::writers;
};
template <typename Service,
size_t output_buffer_size = 128,
uint32_t channel_id = 99,
uint32_t service_id = 16>
class ServerContextForTest {
public:
static constexpr uint32_t kChannelId = channel_id;
static constexpr uint32_t kServiceId = service_id;
ServerContextForTest(const internal::Method& method)
: channel_(Channel::Create<kChannelId>(&output_)),
server_(std::span(&channel_, 1)),
service_(kServiceId),
context_(static_cast<internal::Server&>(server_),
static_cast<internal::Channel&>(channel_),
service_,
method) {
server_.RegisterService(service_);
}
ServerContextForTest() : ServerContextForTest(service_.method) {}
// Creates a packet for this context's channel, service, and method.
internal::Packet packet(std::span<const std::byte> payload) const {
return internal::Packet(internal::PacketType::RPC,
kChannelId,
kServiceId,
context_.method().id(),
payload,
Status::OK);
}
internal::ServerCall& get() { return context_; }
const auto& output() const { return output_; }
TestServer& server() { return static_cast<TestServer&>(server_); }
private:
TestOutput<output_buffer_size> output_;
Channel channel_;
Server server_;
Service service_;
internal::ServerCall context_;
};
} // namespace pw::rpc