Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 1 | // 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 Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 17 | #include <limits> |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 18 | #include <span> |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 19 | |
Wyatt Hepler | 3e2d719 | 2020-06-11 08:28:21 -0700 | [diff] [blame] | 20 | #include "pw_containers/intrusive_list.h" |
Wyatt Hepler | a77e5e7 | 2021-04-09 17:38:39 -0700 | [diff] [blame] | 21 | #include "pw_preprocessor/compiler.h" |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame^] | 22 | #include "pw_rpc/internal/hash.h" |
Wyatt Hepler | cbd09c2 | 2020-09-15 11:17:24 -0700 | [diff] [blame] | 23 | #include "pw_rpc/internal/method.h" |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 24 | #include "pw_rpc/internal/method_union.h" |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 25 | |
Alexei Frolov | 9a4d6bf | 2020-08-04 10:33:26 -0700 | [diff] [blame] | 26 | namespace pw::rpc { |
Alexei Frolov | 9a4d6bf | 2020-08-04 10:33:26 -0700 | [diff] [blame] | 27 | |
Wyatt Hepler | 80f26ff | 2020-06-01 09:30:17 -0700 | [diff] [blame] | 28 | // Base class for all RPC services. This cannot be instantiated directly; use a |
| 29 | // generated subclass instead. |
Wyatt Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 30 | // |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 31 | // 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 Hepler | 3e2d719 | 2020-06-11 08:28:21 -0700 | [diff] [blame] | 34 | class Service : public IntrusiveList<Service>::Item { |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 35 | public: |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 36 | uint32_t id() const { return id_; } |
| 37 | |
Wyatt Hepler | 80f26ff | 2020-06-01 09:30:17 -0700 | [diff] [blame] | 38 | protected: |
Ewout van Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 39 | template <typename T, size_t kMethodCount> |
| 40 | constexpr Service(uint32_t id, const std::array<T, kMethodCount>& methods) |
Wyatt Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 41 | : id_(id), |
| 42 | methods_(methods.data()), |
| 43 | method_size_(sizeof(T)), |
Ewout van Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 44 | method_count_(static_cast<uint16_t>(kMethodCount)) { |
Wyatt Hepler | a77e5e7 | 2021-04-09 17:38:39 -0700 | [diff] [blame] | 45 | 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 Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 48 | static_assert(kMethodCount <= std::numeric_limits<uint16_t>::max()); |
Wyatt Hepler | a77e5e7 | 2021-04-09 17:38:39 -0700 | [diff] [blame] | 49 | PW_MODIFY_DIAGNOSTICS_POP(); |
Wyatt Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // For use by tests with only one method. |
Wyatt Hepler | 80f26ff | 2020-06-01 09:30:17 -0700 | [diff] [blame] | 53 | template <typename T> |
Wyatt Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 54 | constexpr Service(uint32_t id, const T& method) |
| 55 | : id_(id), methods_(&method), method_size_(sizeof(T)), method_count_(1) {} |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 56 | |
| 57 | private: |
Alexei Frolov | 9a4d6bf | 2020-08-04 10:33:26 -0700 | [diff] [blame] | 58 | friend class Server; |
Wyatt Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 59 | friend class ServiceTestHelper; |
Alexei Frolov | 9a4d6bf | 2020-08-04 10:33:26 -0700 | [diff] [blame] | 60 | |
| 61 | // Finds the method with the provided method_id. Returns nullptr if no match. |
Wyatt Hepler | cbd09c2 | 2020-09-15 11:17:24 -0700 | [diff] [blame] | 62 | const internal::Method* FindMethod(uint32_t method_id) const; |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 63 | |
Wyatt Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 64 | const uint32_t id_; |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 65 | const internal::MethodUnion* const methods_; |
Wyatt Hepler | d159142 | 2020-09-15 10:04:41 -0700 | [diff] [blame] | 66 | const uint16_t method_size_; |
| 67 | const uint16_t method_count_; |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame^] | 70 | // Calculates the method ID of the method with the given name. Services track |
| 71 | // methods by this ID. |
| 72 | constexpr uint32_t CalculateMethodId(std::string_view method_name) { |
| 73 | return internal::Hash(method_name); |
| 74 | } |
| 75 | |
Alexei Frolov | 9a4d6bf | 2020-08-04 10:33:26 -0700 | [diff] [blame] | 76 | } // namespace pw::rpc |