• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 "pw_rpc/internal/method_info.h"
17 #include "pw_rpc/method_id.h"
18 #include "pw_rpc/service_id.h"
19 
20 namespace pw::rpc {
21 
22 // Collection of various helpers for RPC calls introspection. For now contains
23 // only MethodRequestType/MethodResponseTypes types to obtain information about
24 // RPC methods request/response types.
25 //
26 // Example. We have an RPC service:
27 //
28 //   package some.package;
29 //   service SpecialService {
30 //     rpc MyMethod(MyMethodRequest) returns (MyMethodResponse) {}
31 //   }
32 //
33 // We also have a templated Storage type alias:
34 //
35 //   template <auto kMethod>
36 //   using Storage =
37 //      std::pair<MethodRequestType<kMethod>, MethodResponseType<kMethod>>;
38 //
39 // Storage<some::package::pw_rpc::pwpb::SpecialService::MyMethod> will
40 // instantiate as:
41 //
42 //   std::pair<some::package::MyMethodRequest::Message,
43 //             some::package::MyMethodResponse::Message>;
44 
45 // Request type for given kMethod.
46 template <auto kMethod>
47 using MethodRequestType = typename internal::MethodInfo<kMethod>::Request;
48 
49 // Response type for given kMethod.
50 template <auto kMethod>
51 using MethodResponseType = typename internal::MethodInfo<kMethod>::Response;
52 
53 // Function which returns a serializer for given kMethod.
54 // For e.g. `pwpb` methods, this returns a `const PwpbMethodSerde&`.
55 template <auto kMethod>
MethodSerde()56 constexpr const auto& MethodSerde() {
57   return internal::MethodInfo<kMethod>::serde();
58 }
59 
60 // Returns the identifier for this particular method.
61 //
62 // Identifiers are not guaranteed to be unique across services, so this should
63 // be paired with a service ID when checking against packets which could target
64 // different services.
65 template <auto kMethod>
GetMethodId()66 constexpr MethodId GetMethodId() {
67   return internal::WrapMethodId(internal::MethodInfo<kMethod>::kMethodId);
68 }
69 
70 // Returns the identifier for the service this method belongs to.
71 template <auto kMethod>
GetServiceIdForMethod()72 constexpr ServiceId GetServiceIdForMethod() {
73   return internal::WrapServiceId(internal::MethodInfo<kMethod>::kServiceId);
74 }
75 
76 }  // namespace pw::rpc
77