Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ServerCommandLoco.cxx
Go to the documentation of this file.
1
29
30#include <cstdio>
31
33#include "withrottle/Server.hxx"
34
35namespace withrottle
36{
37
38/*
39 * ServerCommandLoco::ServerCommandLoco()
40 */
47
48/*
49 * ServerCommandLoco::ServerCommandLoco()
50 */
56
57/*
58 * ServerCommandLoco::entry()
59 */
61{
62 switch (message()->data()->commandSubType)
63 {
64 default:
65 return release_and_exit();
66 case ADDR_LONG:
67 return call_immediately(STATE(address_long));
68 }
69}
70
71/*
72 * ServerCommandLoco::address_long()
73 */
75{
76 unsigned long value = strtoul(message()->data()->payload.c_str(), NULL, 0);
77
78 if ((value == 0 && errno == EINVAL) || value > 9999)
79 {
80 return release_and_exit();
81 }
82
83 printf("loco: %lu\n", value);
84
87 dcc::TrainAddressType::DCC_LONG_ADDRESS, value);
88
89 return invoke_subflow_and_wait(&throttle->olcbThrottle, STATE(assign_train),
90 openlcb::TractionThrottleCommands::ASSIGN_TRAIN, node_id, 0);
91}
92
93/*
94 * ServerCommandLoco::assign_train()
95 */
97{
98 auto *m = full_allocation_result(&throttle->olcbThrottle);
99 switch (m->data()->resultCode)
100 {
101 //case openlcb::Defs::ERROR_CODE_OK:
102 // break;
103 //case openlcb::Defs::ERROR_TIMEOUT:
104 //case openlcb::Defs::ERROR_REJECTED:
105 default:
106 break;
107 }
108 m->unref();
109
110 return invoke_subflow_and_wait(&throttle->olcbThrottle, STATE(load_state),
111 openlcb::TractionThrottleCommands::LOAD_STATE);
112}
113
114/*
115 * ServerCommandLoco::load_state()
116 */
118{
119 auto *m = full_allocation_result(&throttle->olcbThrottle);
120 m->unref();
121
122 string status = Defs::get_loco_status_string(&throttle->olcbThrottle,
123 message()->data()->train.c_str());
124
125 ::write(throttle->fd, status.c_str(), status.length());
126 printf("%s", status.c_str());
127
128 return release_and_exit();
129}
130
131} /* namespace withrottle */
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
void register_handler(HandlerType *handler, ID id, ID mask)
Adds a new handler to this dispatcher.
void unregister_handler(HandlerType *handler, ID id, ID mask)
Removes a specific instance of a handler from this dispatcher.
Return type for a state flow callback.
MessageType * message()
WiThrottle server command handler base object.
ThrottleFlow * throttle
pointer to parent throttle
StateFlowBase::Action entry() override
Entry point to the state machine.
ServerCommandLoco(ThrottleFlow *throttle)
Constructor.
StateFlowBase::Action address_long()
Handle a DCC long address sub-command.
StateFlowBase::Action assign_train()
Handle succes or failure of assigning the train, including getting the latest train state.
StateFlowBase::Action load_state()
Update the LCD display with the trains current state.
State flow for handling a throttle instance.
Definition Server.hxx:104
CommandDispatchFlow dispatcher
flow responsible for routing incoming messages to handlers.
Definition Server.hxx:199
int fd
socket descriptor of throttle connection
Definition Server.hxx:178
uint64_t NodeID
48-bit NMRAnet Node ID type
static NodeID train_node_id_from_legacy(dcc::TrainAddressType type, uint32_t addr)
Converts a legacy address to an NMRAnet node ID.
static string get_loco_status_string(openlcb::TractionThrottle *throttle, const char *loco)
Get the locomotive status command string.
@ SECONDARY
secondary throttle
@ TYPE_MASK
exact mask for dispatcher
@ MULTI
multi throttle
@ PRIMARY
primary throttle
@ ADDR_LONG
set long address