blob: 7a2a21fa42677f363029f68537f2fb531796fd2e [file] [log] [blame]
Alexei Frolov5d6d3922020-05-08 13:57:02 -07001// Copyright 2020 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 <cstdint>
Wyatt Heplerd1591422020-09-15 10:04:41 -070017#include <limits>
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070018#include <span>
Alexei Frolov5d6d3922020-05-08 13:57:02 -070019
Wyatt Hepler3e2d7192020-06-11 08:28:21 -070020#include "pw_containers/intrusive_list.h"
Wyatt Heplera77e5e72021-04-09 17:38:39 -070021#include "pw_preprocessor/compiler.h"
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070022#include "pw_rpc/internal/hash.h"
Wyatt Heplercbd09c22020-09-15 11:17:24 -070023#include "pw_rpc/internal/method.h"
Alexei Frolova4d71502020-10-14 12:43:14 -070024#include "pw_rpc/internal/method_union.h"
Alexei Frolov5d6d3922020-05-08 13:57:02 -070025
Alexei Frolov9a4d6bf2020-08-04 10:33:26 -070026namespace pw::rpc {
Alexei Frolov9a4d6bf2020-08-04 10:33:26 -070027
Wyatt Hepler80f26ff2020-06-01 09:30:17 -070028// Base class for all RPC services. This cannot be instantiated directly; use a
29// generated subclass instead.
Wyatt Heplerd1591422020-09-15 10:04:41 -070030//
Alexei Frolova4d71502020-10-14 12:43:14 -070031// Services store a span of concrete method implementation classes. To support
32// different Method implementations, Service stores a base MethodUnion* and the
33// size of the concrete MethodUnion object.
Wyatt Hepler3e2d7192020-06-11 08:28:21 -070034class Service : public IntrusiveList<Service>::Item {
Alexei Frolov5d6d3922020-05-08 13:57:02 -070035 public:
Alexei Frolov5d6d3922020-05-08 13:57:02 -070036 uint32_t id() const { return id_; }
37
Wyatt Hepler80f26ff2020-06-01 09:30:17 -070038 protected:
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070039 template <typename T, size_t kMethodCount>
40 constexpr Service(uint32_t id, const std::array<T, kMethodCount>& methods)
Wyatt Heplerd1591422020-09-15 10:04:41 -070041 : id_(id),
42 methods_(methods.data()),
43 method_size_(sizeof(T)),
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070044 method_count_(static_cast<uint16_t>(kMethodCount)) {
Wyatt Heplera77e5e72021-04-09 17:38:39 -070045 PW_MODIFY_DIAGNOSTICS_PUSH();
46 // GCC 10 emits spurious -Wtype-limits warnings for the static_assert.
47 PW_MODIFY_DIAGNOSTIC_GCC(ignored, "-Wtype-limits");
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070048 static_assert(kMethodCount <= std::numeric_limits<uint16_t>::max());
Wyatt Heplera77e5e72021-04-09 17:38:39 -070049 PW_MODIFY_DIAGNOSTICS_POP();
Wyatt Heplerd1591422020-09-15 10:04:41 -070050 }
51
52 // For use by tests with only one method.
Wyatt Hepler80f26ff2020-06-01 09:30:17 -070053 template <typename T>
Wyatt Heplerd1591422020-09-15 10:04:41 -070054 constexpr Service(uint32_t id, const T& method)
55 : id_(id), methods_(&method), method_size_(sizeof(T)), method_count_(1) {}
Alexei Frolov5d6d3922020-05-08 13:57:02 -070056
57 private:
Alexei Frolov9a4d6bf2020-08-04 10:33:26 -070058 friend class Server;
Wyatt Heplerd1591422020-09-15 10:04:41 -070059 friend class ServiceTestHelper;
Alexei Frolov9a4d6bf2020-08-04 10:33:26 -070060
61 // Finds the method with the provided method_id. Returns nullptr if no match.
Wyatt Heplercbd09c22020-09-15 11:17:24 -070062 const internal::Method* FindMethod(uint32_t method_id) const;
Alexei Frolov5d6d3922020-05-08 13:57:02 -070063
Wyatt Heplerd1591422020-09-15 10:04:41 -070064 const uint32_t id_;
Alexei Frolova4d71502020-10-14 12:43:14 -070065 const internal::MethodUnion* const methods_;
Wyatt Heplerd1591422020-09-15 10:04:41 -070066 const uint16_t method_size_;
67 const uint16_t method_count_;
Alexei Frolov5d6d3922020-05-08 13:57:02 -070068};
69
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070070// Calculates the method ID of the method with the given name. Services track
71// methods by this ID.
72constexpr uint32_t CalculateMethodId(std::string_view method_name) {
73 return internal::Hash(method_name);
74}
75
Alexei Frolov9a4d6bf2020-08-04 10:33:26 -070076} // namespace pw::rpc