MotionLib  1.0.0
SamBuCa motion library
MockGpio.cpp
1 /*
2 ** Copyright (C) 2025 CERN
3 **
4 ** This software is provided 'as-is', without any express or implied
5 ** warranty. In no event will the authors be held liable for any damages
6 ** arising from the use of this software.
7 **
8 ** Permission is granted to anyone to use this software for any purpose,
9 ** including commercial applications, and to alter it and redistribute it
10 ** freely, subject to the following restrictions:
11 **
12 ** 1. The origin of this software must not be misrepresented; you must not
13 ** claim that you wrote the original software. If you use this software
14 ** in a product, an acknowledgment in the product documentation would be
15 ** appreciated but is not required.
16 ** 2. Altered source versions must be plainly marked as such, and must not be
17 ** misrepresented as being the original software.
18 ** 3. This notice may not be removed or altered from any source distribution.
19 **
20 ** Created on: 2025-04-16T13:30:35
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "MockGpio.hpp"
25 
26 #include <ccut/async.hpp>
27 
28 #include "MockPlatform.hpp"
29 
30 using namespace logger;
31 using namespace smc;
32 using namespace smc::internal;
33 using namespace smc::units;
34 
35 MockGpio::MockGpio(const std::string &uid,
36  const std::shared_ptr<MockPlatform> &mock) :
37  Gpio{uid},
38  m_mock(mock)
39 {}
40 
41 std::future<void> MockGpio::setIoState(const io_port_t &state) const
42 {
43  std::shared_ptr<MockPlatform> mock(m_mock.lock());
44  if (!mock)
45  return ccut::make_future_error(
46  Exception(ErrorCode::Canceled, "platform stopped (deleted)"));
47 
48  info(MockPlatform::s_loggerCat)
49  << "Setting GPIO " << uid()
50  << " on MockPlatform id: " << mock->getPlatformId() << " to state "
51  << state;
52  std::promise<void> setStatePromise;
53  auto setStateFuture = setStatePromise.get_future();
54  try
55  {
56  mock->setGpioState(uid(), state);
57  setStatePromise.set_value();
58  }
59  catch (...)
60  {
61  setStatePromise.set_exception(std::current_exception());
62  }
63  return setStateFuture;
64 }
65 
66 std::future<io_port_t> MockGpio::getIoState() const
67 {
68  std::shared_ptr<MockPlatform> mock(m_mock.lock());
69  if (!mock)
70  return ccut::make_future_error<io_port_t>(
71  Exception(ErrorCode::Canceled, "platform stopped (deleted)"));
72 
73  info(MockPlatform::s_loggerCat)
74  << "Querying GPIO " << uid()
75  << " on MockPlatform id: " << mock->getPlatformId() << " for state";
76  std::promise<io_port_t> statePromise;
77  auto stateFuture = statePromise.get_future();
78  try
79  {
80  statePromise.set_value(mock->getGpioState(uid()));
81  }
82  catch (...)
83  {
84  statePromise.set_exception(std::current_exception());
85  }
86  return stateFuture;
87 }
DeviceId uid() const
Get the address of device.
Definition: DeviceBase.cpp:76
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
std::future< units::io_port_t > getIoState() const override
Get current state of this GPIO.
Definition: MockGpio.cpp:66
main motion-lib namespace
Definition: Client.cpp:30