Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
LogonModule.hxx
Go to the documentation of this file.
1
34#ifndef _DCC_LOGONMODULE_HXX_
35#define _DCC_LOGONMODULE_HXX_
36
37#include <map>
38#include <vector>
39
40#include "dcc/Defs.hxx"
41#include "dcc/Logon.hxx"
42
43namespace dcc
44{
45
47template<class Base>
49{
50public:
52 struct LocoInfo : public Base::Storage
53 {
55 uint8_t flags_ {0};
56
60 uint16_t assignedAddress_ {Defs::ADR_INVALID};
61
63 uint64_t decoderId_;
64 };
65
66 std::vector<LocoInfo> locos_;
67 std::map<uint64_t, uint16_t> ids_;
68
71 unsigned num_locos()
72 {
73 return locos_.size();
74 }
75
78 bool is_valid_loco_id(unsigned loco_id)
79 {
80 return loco_id < num_locos();
81 }
82
87 uint8_t &loco_flags(unsigned loco_id)
88 {
89 return locos_[loco_id].flags_;
90 }
91
95 uint64_t loco_did(unsigned loco_id)
96 {
97 return locos_[loco_id].decoderId_;
98 }
99
104 unsigned create_or_lookup_loco(uint64_t decoder_id)
105 {
106 auto it = ids_.find(decoder_id);
107 if (it == ids_.end())
108 {
109 // create new.
110 uint16_t lid = locos_.size();
111 locos_.emplace_back();
112 locos_[lid].decoderId_ = decoder_id;
113 ids_[decoder_id] = lid;
114 return lid;
115 }
116 else
117 {
118 return it->second;
119 }
120 }
121
128 void run_address_policy(unsigned loco_id, uint16_t desired_address)
129 {
131
132 // Note: we ignore the desired address and start assigning addresses
133 // from 10000 and up.
134 locos_[loco_id].assignedAddress_ = nextAddress_++;
135 }
136
139 uint16_t assigned_address(unsigned loco_id)
140 {
141 return locos_[loco_id].assignedAddress_;
142 }
143
146 void assign_complete(unsigned loco_id)
147 {
149 }
150
151 uint16_t nextAddress_ {(Defs::ADR_MOBILE_LONG << 8) + 10000};
152
153}; // class ParameterizedLogonModule
154
156public:
157 struct Storage {};
158};
159
161
162} // namespace dcc
163
164#endif // _DCC_LOGONMODULE_HXX_
This class needs to be a base class for the template argument of the Logon Handler.
Definition Logon.hxx:49
@ FLAG_COMPLETE
1 if we completed the address assignment.
Definition Logon.hxx:106
Default implementation of the storage and policy module for trains.
void assign_complete(unsigned loco_id)
Invoked when the address assignment completes for a decoder.
uint16_t assigned_address(unsigned loco_id)
unsigned create_or_lookup_loco(uint64_t decoder_id)
Creates a new locomotive by decoder ID, or looks up an existing locomotive by decoder ID.
bool is_valid_loco_id(unsigned loco_id)
void run_address_policy(unsigned loco_id, uint16_t desired_address)
Runs the locomotive address policy.
uint8_t & loco_flags(unsigned loco_id)
Finds the storage cell for a locomotive and returns the flag byte for it.
uint64_t loco_did(unsigned loco_id)
Retrieves the decoder unique ID.
We store this structure about each locomotive.
uint8_t flags_
State machine flags about this loco.
uint16_t assignedAddress_
The assigned DCC address.
uint64_t decoderId_
44-bit decoder unique ID.