• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 
15 #include "pw_rpc/integration_testing.h"
16 
17 #include <limits>
18 
19 #include "gtest/gtest.h"
20 #include "pw_log/log.h"
21 #include "pw_rpc/integration_test_socket_client.h"
22 #include "pw_unit_test/logging_event_handler.h"
23 
24 namespace pw::rpc::integration_test {
25 namespace {
26 
27 // Hard-coded to 1055 bytes, which is enough to fit 512-byte payloads when using
28 // HDLC framing.
29 SocketClientContext<1055> context;
30 unit_test::LoggingEventHandler log_test_events;
31 
32 }  // namespace
33 
client()34 Client& client() { return context.client(); }
35 
GetClientSocketFd()36 int GetClientSocketFd() { return context.GetSocketFd(); }
37 
SetEgressChannelManipulator(ChannelManipulator * new_channel_manipulator)38 void SetEgressChannelManipulator(ChannelManipulator* new_channel_manipulator) {
39   context.SetEgressChannelManipulator(new_channel_manipulator);
40 }
41 
SetIngressChannelManipulator(ChannelManipulator * new_channel_manipulator)42 void SetIngressChannelManipulator(ChannelManipulator* new_channel_manipulator) {
43   context.SetIngressChannelManipulator(new_channel_manipulator);
44 }
45 
InitializeClient(int port)46 Status InitializeClient(int port) {
47   unit_test::RegisterEventHandler(&log_test_events);
48   if (port <= 0 || port > std::numeric_limits<uint16_t>::max()) {
49     PW_LOG_CRITICAL("Port numbers must be between 1 and 65535; %d is invalid",
50                     port);
51     return Status::InvalidArgument();
52   }
53 
54   PW_LOG_INFO("Connecting to pw_rpc client at localhost:%d", port);
55   return context.Start(port);
56 }
57 
TerminateClient()58 void TerminateClient() { context.Terminate(); }
59 
InitializeClient(int argc,char * argv[],const char * usage_args)60 Status InitializeClient(int argc, char* argv[], const char* usage_args) {
61   if (argc < 2) {
62     PW_LOG_INFO("Usage: %s %s", argv[0], usage_args);
63     return Status::InvalidArgument();
64   }
65 
66   const int port = std::atoi(argv[1]);
67   return InitializeClient(port);
68 }
69 
70 }  // namespace pw::rpc::integration_test
71