gusimplewhiteboard
FSMControlStatus.hpp
Go to the documentation of this file.
1/*
2 * FSMControlStatus.h
3 * gusimplewhiteboard / clfsm
4 *
5 * Created by Rene Hexel on 25/03/13.
6 * Copyright (c) 2013, 2014 Rene Hexel. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Rene Hexel.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58#ifndef guWhiteboard_FSMControlStatus_DEFINED
59#define guWhiteboard_FSMControlStatus_DEFINED
60#define guWhiteboard_FSMNames_DEFINED
61#define guWhiteboard_FSMNames_DEFINED
62
63
64#include <cstdlib>
65#include <cstring>
66#include <bitset>
67#include <gu_util.h>
69#include "wb_fsm_state_status.h"
70#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
71#include <sstream>
72#include <ctype.h>
73#endif
74
75namespace guWhiteboard
76{
83 {
84 public:
87
89 FSMControlStatus(const FSMControlStatus &other) { memcpy(this, &other, sizeof(wb_fsm_control_status)); }
90
92 const FSMControlStatus &operator=(const FSMControlStatus &other) { memcpy(this, &other, sizeof(wb_fsm_control_status)); return *this; }
93
95 FSMControlType command() const { return static_cast<FSMControlType>(CONTROLSTATUS_GET_CMD(this)); }
96
99
101 bool get(int fsm) const { return CONTROLSTATUS_GET_FSM(this, fsm); }
102
104 void set(int fsm) { CONTROLSTATUS_SET_FSM(this, fsm); }
105
107 void clr(int fsm) { CONTROLSTATUS_CLR_FSM(this, fsm); }
108
110 void set(int fsm, bool value) { value ? set(fsm) : clr(fsm); }
111
113 void reset() { memset(this, 0, sizeof(*this)); }
114
116 bool operator==(const FSMControlStatus &other) const { return memcmp(this, &other, sizeof(*this)) == 0; }
117
118#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
120 FSMControlStatus(const std::string &names): wb_fsm_control_status() { from_string(names); }
121
123 std::string description() const
124 {
125 std::ostringstream ss;
126 ss << command();
127 for (size_t i = 0; i < CONTROLSTATUS_NUM_FSMS; i++)
128 if (CONTROLSTATUS_GET_FSM(this, i))
129 ss << "," << i;
130
131 return ss.str();
132 }
133
135 void from_string(const std::string &str)
136 {
137 std::istringstream iss(str);
138 std::string token;
139 if (getline(iss, token, ','))
140 {
141 set_command(static_cast<enum FSMControlType>(atoi(token.c_str())));
142 while (getline(iss, token, ','))
143 {
144 size_t i = static_cast<size_t>(atol(token.c_str()));
146 CONTROLSTATUS_SET_FSM(this, i);
147 }
148 }
149 }
150#endif //WHITEBOARD_POSTER_STRING_CONVERSION
151 };
152
157 {
159 PROPERTY(uint16_t, startoffs)
161 char _names[sizeof(gsw_simple_message)-sizeof(uint16_t)];
162 public:
164 FSMNames(uint16_t startoffs = 0, const char *names = NULLPTR): _startoffs(startoffs)
165 {
166 int i = names ? int(strlen(names) + 1) : 0;
167 if (names) strcpy(_names, names);
168 else _names[i++] = '\0';
169 _names[i] = '\0';
170 }
171
173 FSMNames(const FSMNames &other): _startoffs(other._startoffs)
174 {
175 memcpy(_names, other._names, sizeof(_names));
176 }
177
179 const FSMNames &operator=(const FSMNames &other) { memcpy(this, &other, sizeof(other)); return *this; }
180
182 char *names() { return _names; }
183
185 const char *end() { return &_names[sizeof(_names)]; }
186
188 char *next_name(const char *name = NULLPTR) { if (!name) return names(); while (name < end() && *name++) {} return const_cast<char *>(name); }
189
191 char *next_slot(const char *name = NULLPTR) { while (*(name = next_name(name)) && name < end()) {} return const_cast<char *>(name); }
192
194 int available_space(const char *pos) { return int(end() - pos); }
195
197 char *add_name(char *name)
198 {
199 char *pos = next_slot();
200 int n = available_space(pos);
201 if (n <= 0) return NULLPTR;
202 gu_strlcpy(pos, name, size_t(n));
203 return pos;
204 }
205
206#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
208 FSMNames(std::string names) { from_string(names); }
209
211 std::string description()
212 {
213 std::stringstream ss;
214 ss << startoffs();
215 for (const char *s = names(); s < end() && *s; s = next_name(s))
216 ss << "," << s;
217
218 return ss.str();
219 }
220
222 void from_string(const std::string &str)
223 {
224 char *dst = names();
225 std::istringstream iss(str);
226 std::string token;
227 if (getline(iss, token, ','))
228 {
229 set_startoffs(static_cast<uint16_t>(atoi(token.c_str())));
230 while (getline(iss, token, ','))
231 {
232 int len = int(token.length());
233 if (len >= available_space(dst))
234 break;
235 strcpy(dst, token.c_str());
236 dst += len;
237 *dst++ = '\0';
238 }
239 }
240 else set_startoffs(0);
241
242 if (available_space(dst)) *dst++ = '\0';
243 if (available_space(dst)) *dst = '\0';
244 }
245#endif //WHITEBOARD_POSTER_STRING_CONVERSION
246 };
247
252 {
253
254 public:
257
259 FSMState(const FSMState &other) { memcpy(this, &other, sizeof(wb_fsm_state_status)); }
260
262 const FSMState &operator=(const FSMState &other) { memcpy(this, &other, sizeof(other)); return *this; }
263
265 uint8_t getStateForMachineID(uint8_t machineID) {
266
267 if (machineID >= STATE_BYTE_SIZE) {
268 return INVALIDMACHINE;
269 }
270
271 return STATESTATUS_GET_STATE(this, machineID);
272 }
273
279 void setStateForMachineID (uint8_t machineID, uint8_t state)
280 {
281 if (machineID < STATE_BYTE_SIZE)
282 STATESTATUS_SET_STATE(this, machineID, state);
283 }
284
286 void reset() { memset(this, INVALIDMACHINE, sizeof(wb_fsm_state_status)); }
287
288#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
290 FSMState(std::string states) { from_string(states); }
291
293 std::string description()
294 {
295 std::stringstream ss;
296 size_t sizeAtLastValidState = 0;
297 for (uint8_t machineID =0; machineID < STATE_BYTE_SIZE; machineID++) {
298 uint8_t state = STATESTATUS_GET_STATE(this, machineID);
299 ss << static_cast<unsigned>(state) << ((machineID < (STATE_BYTE_SIZE-1))? "," : "");
300 if (state != INVALIDMACHINE) {
301 sizeAtLastValidState = ss.str().size() - static_cast<size_t>(((machineID < (STATE_BYTE_SIZE-1))? 1 : 0));
302 }
303 }
304 std::string message = ss.str();
305 message.erase(sizeAtLastValidState, message.size()-sizeAtLastValidState);
306
307 return message;
308 }
309
311 void from_string(const std::string &states)
312 {
313 std::istringstream iss(states);
314 std::string token;
315 uint8_t machineID = 0;
316 while (!iss.eof() && machineID < STATE_BYTE_SIZE) {
317 getline(iss, token, ',');
318 STATESTATUS_SET_STATE(this, machineID++, static_cast<uint8_t>(atoi(token.c_str())));
319 }
320 }
321#endif //WHITEBOARD_POSTER_STRING_CONVERSION
322 };
323}
324
325
326#endif // FSMControlStatus_DEFINED
Class for controlling and getting the status of FSMs.
FSMControlStatus(const FSMControlStatus &other)
copy constructor
FSMControlStatus(const std::string &names)
string constructor
void set(int fsm, bool value)
machine control/status setter / clearer
void clr(int fsm)
machine control/status clearer
bool operator==(const FSMControlStatus &other) const
comparison operator
std::string description() const
convert to a string
FSMControlType command() const
command getter
const FSMControlStatus & operator=(const FSMControlStatus &other)
assignment operator
FSMControlStatus(FSMControlType t=FSMStatus)
designated constructor
void set(int fsm)
machine control/status setter
void reset()
clear all machines and reset back to status
bool get(int fsm) const
machine status setter
void set_command(FSMControlType command)
command setter
void from_string(const std::string &str)
convert from a string
Class for transmitting machine names over the whiteboard.
int available_space(const char *pos)
return the available space after a given pointer
char * names()
names getter
char * next_name(const char *name=NULLPTR)
get the next name
char * add_name(char *name)
try to add a new name
std::string description()
convert to a string
char * next_slot(const char *name=NULLPTR)
get the next empty slot
const char * end()
end of string
FSMNames(const FSMNames &other)
copy constructor
FSMNames(uint16_t startoffs=0, const char *names=NULLPTR)
designated constructor
const FSMNames & operator=(const FSMNames &other)
assignment operator
FSMNames(std::string names)
string constructor
void from_string(const std::string &str)
convert from a string
Class for transmitting machine names over the whiteboard.
void setStateForMachineID(uint8_t machineID, uint8_t state)
Record the given state for the given Machine ID.
const FSMState & operator=(const FSMState &other)
assignment operator
uint8_t getStateForMachineID(uint8_t machineID)
names getter
void from_string(const std::string &states)
convert from a string
std::string description()
convert to a string
FSMState(const FSMState &other)
copy constructor
FSMState()
designated constructor
void reset()
Reset all machines to INVALIDMACHINE.
FSMState(std::string states)
string constructor
/file APM_Interface.h
FSMControlType
command for ControlStatus
@ FSMStatus
check status only
Finite State Machine Control and Status information.
Idempotent Message publicising the state -about to be executed- (0-254) of up-to the first 128 machin...
union type that is used to store data in shared memory
#define CONTROLSTATUS_GET_CMD(s)
#define CONTROLSTATUS_CLR_FSM(s, fsm)
#define CONTROLSTATUS_GET_FSM(s, fsm)
#define CONTROLSTATUS_SET_CMD(s, cmd)
#define CONTROLSTATUS_BIT_SIZE
#define CONTROLSTATUS_SET_FSM(s, fsm)
#define CONTROLSTATUS_NUM_FSMS
#define STATESTATUS_GET_STATE(s, machineID)
#define INVALIDMACHINE
#define STATESTATUS_SET_STATE(s, machineID, state)
#define STATE_BYTE_SIZE