-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonality.cpp
243 lines (195 loc) · 6.32 KB
/
Personality.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <cstring>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <sstream>
#include "Personality.h"
SerialPort * Personality::m_serial;
std::vector<PersonalityProgram*> * Personality::m_progs;
void Personality::Init()
{
// Initialize list of programs.
m_progs = new std::vector<PersonalityProgram*>();
// Configure serial port for PuTTY defaults.
m_serial = new SerialPort(9600, 8, SerialPort::kParity_None, SerialPort::kStopBits_One);
// Set the console to stop reading on a newline character.
m_serial->EnableTermination();
// Set the buffer size to 1 char for read and write since we're interactive
m_serial->SetReadBufferSize(1);
m_serial->SetWriteBufferSize(1);
// This is another PuTTY default.
m_serial->SetFlowControl(SerialPort::kFlowControl_XonXoff);
// Print the prompt.
m_serial->Printf(">> ");
// Set the timeout for half a second.
// This means that after somebody presses [RETURN] on the
// console, we get their entire string half a second later.
m_serial->SetTimeout(.5);
// Initialize the programs.
// If you create another PersonalityProgram, initialize
// and push_back here.
m_progs->push_back(new PPConfig());
}
void Personality::Process()
{
// Get bytes available.
int bytes_received = m_serial->GetBytesReceived();
if (bytes_received > 0) {
// Something is in the buffer. Get it.
std::string line = ReadFromSerial();
std::string response = "";
std::vector<std::string> tokens;
if (line.length() > 0) {
// This is a full line. Process it.
// Break apart the line into non-whitespace
// tokens. stringstreams take a string
// and allow us to pretend it's a stream
// (like cin or cout)
std::istringstream tokenizer(line);
std::string temp = "";
while (tokenizer >> temp) {
tokens.push_back(temp);
}
// Check to make sure we've got something
// that could be a command.
if (tokens.size() < 1) {
response += "Error parsing output, need a command\n";
}
// Here it is.
std::string command = tokens.front();
// Grab everything else and make it into the
// args vector.
std::vector<std::string> args;
for (unsigned int i = 1; i < tokens.size(); ++i)
{
args.push_back(tokens[i]);
}
// Iterators are basically nice clean
// pointers that advance along an STL
// container. You could do the same
// thing using a regular for loop
// with a counter variable, but
// I felt fancy.
std::vector<PersonalityProgram*>::iterator itr = m_progs->begin();
// Search for the command entered by the user.
bool found = false;
for ( ; itr != m_progs->end(); ++itr) {
if ((*itr)->GetName() == command) {
// We found one!
found = true;
break;
}
}
if (!found) {
// Couldn't find the command entered
// by the user. See if they wanted something
// else.
if (command == "help") {
// User wanted help. Give it to them.
response += Help();
} else {
// No, user just made a mistake.
// Chastise them.
response += "Command '" + command + "' not found.\n";
}
} else {
// User entered a correct program name,
// and itr is pointing at the correct program.
// Run it and save the output.
std::string output = (*itr)->Run(args);
// Add the output to the stuff sent back
// to the client.
response += output;
}
// Write the response back to the terminal.
WriteToSerial(response);
// Show the prompt again.
m_serial->Printf(">> ");
}
}
}
// Provide help to the user.
std::string Personality::Help()
{
// Set up an iterator to visit every program.
std::vector<PersonalityProgram*>::iterator itr = m_progs->begin();
std::string retval = "";
// For every iterator
for ( ; itr != m_progs->end(); ++itr) {
// Make a string like this:
// name help
// command: does stuff
retval += (*itr)->GetName() + ": " + (*itr)->GetHelp() + "\n";
}
// return the string.
return retval;
}
std::string Personality::ReadFromSerial()
{
std::string retval = "";
// Set up the buffer. (leave room for nul byte)
char *bp, buf[Personality::kBufferSize+1];
int n, bytes_to_read;
// Set the "cursor" to the beginning of the buffer.
bp = buf;
// Set the max bytes to read to the buffer size.
bytes_to_read = Personality::kBufferSize;
// Read up to bytes_to_read bytes from the serial port
// and write them to the buffer at the "cursor"'s location
while ((n = m_serial->Read(bp, bytes_to_read)) > 0) {
// Update the cursor's position to point to the next
// empty spot.
bp += n;
// We have n fewer bytes before the end of the array.
bytes_to_read -= n;
}
// Set the last character to 0 so it looks like a normal
// C string.
bp = 0;
// Assign the buffer to a nice self-managing c++ string.
retval = buf;
// Clean up random whitespace from the line.
CleanString(retval);
// Reset the serial port so we can read another line.
m_serial->Reset();
// Return the c++ string.
return retval;
}
void Personality::CleanString(std::string & str)
{
// Clean up string.
// Find any offensive characters.
int found = str.find_first_of("\t\r\n");
while (found != -1) {
// As long as there are offensive characters...
// Get 'em
str.erase(found);
// Find more!
found = str.find_first_of("\t\r\n");
}
}
// Writes a standard c++ string to serial port.
// No need for newline.
void Personality::WriteToSerial(std::string data)
{
int n, bytes_to_write;
char * bp, buf[Personality::kBufferSize];
// Add the newline here to keep the console clean.
data += "\r\n";
// Set up the buffer like in ReadFromSerial()
bp = buf;
// Either write the entire string (likely) or the part that
// will fit.
bytes_to_write = min( Personality::kBufferSize, data.length()+1);
// Write the entire string to the C-style array.
::strncpy(buf, data.c_str(), bytes_to_write);
// While there's stuff to write... write them
while ((n = m_serial->Write(bp, bytes_to_write)) > 0 && bytes_to_write > 0)
{
// Advance cursor to unwritten part
bp += n;
// Count down to the end of the array.
bytes_to_write -= n;
}
// We're done.
}