| 1 |
#include "protoApp.h" |
| 2 |
#include "protoSerial.h" |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
#include <ctype.h> // for "isspace()" |
| 8 |
|
| 9 |
/** |
| 10 |
* @class SerialApp |
| 11 |
* |
| 12 |
* @brief Example using ProtoSerial |
| 13 |
* |
| 14 |
*/ |
| 15 |
class SerialApp : public ProtoApp |
| 16 |
{ |
| 17 |
public: |
| 18 |
SerialApp(); |
| 19 |
~SerialApp(); |
| 20 |
|
| 21 |
bool OnStartup(int argc, const char*const* argv); |
| 22 |
bool ProcessCommands(int argc, const char*const* argv); |
| 23 |
void OnShutdown(); |
| 24 |
|
| 25 |
|
| 26 |
void PrintSignalStatus(); |
| 27 |
|
| 28 |
private: |
| 29 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 30 |
static const char* const CMD_LIST[]; |
| 31 |
static CmdType GetCmdType(const char* string); |
| 32 |
bool OnCommand(const char* cmd, const char* val); |
| 33 |
void Usage(); |
| 34 |
|
| 35 |
void OnSerialInput(ProtoChannel& theChannel, |
| 36 |
ProtoChannel::Notification notifyType); |
| 37 |
|
| 38 |
bool OnTxTimeout(ProtoTimer& theTimer); |
| 39 |
|
| 40 |
char device_name[PATH_MAX+1]; |
| 41 |
unsigned int baud_rate; |
| 42 |
|
| 43 |
ProtoSerial* serial; |
| 44 |
ProtoTimer tx_timer; |
| 45 |
|
| 46 |
}; // end class SerialApp |
| 47 |
|
| 48 |
void SerialApp::Usage() |
| 49 |
{ |
| 50 |
fprintf(stderr, "Usage: serialExample device <name>\n"); |
| 51 |
} |
| 52 |
|
| 53 |
const char* const SerialApp::CMD_LIST[] = |
| 54 |
{ |
| 55 |
"-help", // print help info an exit |
| 56 |
"+device", // serial device name |
| 57 |
"+baud", // set baud rate in bits/sec |
| 58 |
"-send", // send a short test message once per second |
| 59 |
"+debug", // <debugLevel> |
| 60 |
NULL |
| 61 |
}; |
| 62 |
|
| 63 |
/** |
| 64 |
* This macro creates our ProtoApp derived application instance |
| 65 |
*/ |
| 66 |
PROTO_INSTANTIATE_APP(SerialApp) |
| 67 |
|
| 68 |
SerialApp::SerialApp() |
| 69 |
: baud_rate(0), serial(NULL) |
| 70 |
{ |
| 71 |
device_name[0] = '\0'; |
| 72 |
tx_timer.SetListener(this, &SerialApp::OnTxTimeout); |
| 73 |
} |
| 74 |
|
| 75 |
SerialApp::~SerialApp() |
| 76 |
{ |
| 77 |
if (NULL != serial) |
| 78 |
{ |
| 79 |
serial->Close(); |
| 80 |
delete serial; |
| 81 |
serial = NULL; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
SerialApp::CmdType SerialApp::GetCmdType(const char* cmd) |
| 86 |
{ |
| 87 |
if (!cmd) return CMD_INVALID; |
| 88 |
unsigned int len = strlen(cmd); |
| 89 |
bool matched = false; |
| 90 |
CmdType type = CMD_INVALID; |
| 91 |
const char* const* nextCmd = CMD_LIST; |
| 92 |
while (*nextCmd) |
| 93 |
{ |
| 94 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 95 |
{ |
| 96 |
if (matched) |
| 97 |
{ |
| 98 |
// ambiguous command (command should match only once) |
| 99 |
return CMD_INVALID; |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
matched = true; |
| 104 |
if ('+' == *nextCmd[0]) |
| 105 |
type = CMD_ARG; |
| 106 |
else |
| 107 |
type = CMD_NOARG; |
| 108 |
} |
| 109 |
} |
| 110 |
nextCmd++; |
| 111 |
} |
| 112 |
return type; |
| 113 |
} // end SerialApp::GetCmdType() |
| 114 |
|
| 115 |
|
| 116 |
bool SerialApp::OnStartup(int argc, const char*const* argv) |
| 117 |
{ |
| 118 |
if (!ProcessCommands(argc, argv)) |
| 119 |
{ |
| 120 |
PLOG(PL_ERROR, "SerialApp::OnStartup() error processing command line options\n"); |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
if ('\0' == device_name[0]) |
| 125 |
{ |
| 126 |
PLOG(PL_ERROR, "SerialApp::OnStartup() error: no serial device specified\n"); |
| 127 |
Usage(); |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
if (NULL == (serial = ProtoSerial::Create())) |
| 133 |
{ |
| 134 |
PLOG(PL_ERROR, "SerialApp::OnStartup() ProtoSerial::Create() error: %s\n", GetErrorString()); |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
serial->SetNotifier(static_cast<ProtoChannel::Notifier*>(&dispatcher)); |
| 139 |
serial->SetListener(this, &SerialApp::OnSerialInput); |
| 140 |
|
| 141 |
TRACE("opening port ...\n"); |
| 142 |
|
| 143 |
// Open the serial device, accepting its current configuration (whatever it is) |
| 144 |
|
| 145 |
ProtoSerial::AccessMode accessMode = ProtoSerial::RDWR;//tx_timer.IsActive() ? ProtoSerial::WRONLY : ProtoSerial::RDONLY; |
| 146 |
|
| 147 |
if (!serial->Open(device_name, accessMode)) |
| 148 |
{ |
| 149 |
PLOG(PL_ERROR, "SerialApp::OnCommand(device) error: unable to open specified device\n"); |
| 150 |
delete serial; |
| 151 |
serial = NULL; |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
TRACE("port opened ...\n"); |
| 156 |
|
| 157 |
if (0 != baud_rate) |
| 158 |
serial->SetBaudRate(baud_rate); |
| 159 |
else |
| 160 |
baud_rate = serial->GetBaudRate(); |
| 161 |
|
| 162 |
TRACE("serialExample config: baud>%d bytes>%d parity>%s local>%s timeout>%lf sec\n", |
| 163 |
serial->GetBaudRate(), serial->GetByteSize(), |
| 164 |
serial->GetParity() ? "yes" : "no", |
| 165 |
serial->GetLocalControl() ? "yes" : "no", |
| 166 |
serial->GetReadTimeout()); |
| 167 |
|
| 168 |
serial->Clear(ProtoSerial::DTR); |
| 169 |
serial->Clear(ProtoSerial::RTS); |
| 170 |
|
| 171 |
return true; |
| 172 |
} // end SerialApp::OnStartup() |
| 173 |
|
| 174 |
void SerialApp::OnShutdown() |
| 175 |
{ |
| 176 |
if (NULL != serial) |
| 177 |
{ |
| 178 |
serial->Close(); |
| 179 |
delete serial; |
| 180 |
serial = NULL; |
| 181 |
} |
| 182 |
} // end SerialApp::OnShutdown() |
| 183 |
|
| 184 |
bool SerialApp::ProcessCommands(int argc, const char*const* argv) |
| 185 |
{ |
| 186 |
// Dispatch command-line commands to our OnCommand() method |
| 187 |
int i = 1; |
| 188 |
while ( i < argc) |
| 189 |
{ |
| 190 |
// Is it a serialExample command? |
| 191 |
switch (GetCmdType(argv[i])) |
| 192 |
{ |
| 193 |
case CMD_INVALID: |
| 194 |
{ |
| 195 |
PLOG(PL_ERROR, "SerialApp::ProcessCommands() Invalid command:%s\n", |
| 196 |
argv[i]); |
| 197 |
Usage(); |
| 198 |
return false; |
| 199 |
} |
| 200 |
case CMD_NOARG: |
| 201 |
if (!OnCommand(argv[i], NULL)) |
| 202 |
{ |
| 203 |
PLOG(PL_ERROR, "SerialApp::ProcessCommands() ProcessCommand(%s) error\n", |
| 204 |
argv[i]); |
| 205 |
Usage(); |
| 206 |
return false; |
| 207 |
} |
| 208 |
i++; |
| 209 |
break; |
| 210 |
case CMD_ARG: |
| 211 |
if (!OnCommand(argv[i], argv[i+1])) |
| 212 |
{ |
| 213 |
PLOG(PL_ERROR, "SerialApp::ProcessCommands() ProcessCommand(%s, %s) error\n", |
| 214 |
argv[i], argv[i+1]); |
| 215 |
Usage(); |
| 216 |
return false; |
| 217 |
} |
| 218 |
i += 2; |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
return true; |
| 223 |
} // end SerialApp::ProcessCommands() |
| 224 |
|
| 225 |
bool SerialApp::OnCommand(const char* cmd, const char* val) |
| 226 |
{ |
| 227 |
CmdType type = GetCmdType(cmd); |
| 228 |
ASSERT(CMD_INVALID != type); |
| 229 |
size_t len = strlen(cmd); |
| 230 |
if ((CMD_ARG == type) && !val) |
| 231 |
{ |
| 232 |
PLOG(PL_ERROR, "SerialApp::ProcessCommand(%s) missing argument\n", cmd); |
| 233 |
Usage(); |
| 234 |
return false; |
| 235 |
} |
| 236 |
else if (!strncmp("help", cmd, len)) |
| 237 |
{ |
| 238 |
Usage(); |
| 239 |
exit(0); |
| 240 |
} |
| 241 |
else if (!strncmp("device", cmd, len)) |
| 242 |
{ |
| 243 |
strncpy(device_name, val, PATH_MAX); |
| 244 |
device_name[PATH_MAX] = '\0'; |
| 245 |
} |
| 246 |
else if (!strncmp("baud", cmd, len)) |
| 247 |
{ |
| 248 |
baud_rate = atoi(val); |
| 249 |
} |
| 250 |
else if (!strncmp("send", cmd, len)) |
| 251 |
{ |
| 252 |
tx_timer.SetInterval(1.0); |
| 253 |
tx_timer.SetRepeat(-1); |
| 254 |
ActivateTimer(tx_timer); |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
PLOG(PL_ERROR, "serialExample error: invalid command\n"); |
| 259 |
Usage(); |
| 260 |
return false; |
| 261 |
} |
| 262 |
return true; |
| 263 |
} // end SerialApp::OnCommand() |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
void SerialApp::OnSerialInput(ProtoChannel& theChannel, |
| 268 |
ProtoChannel::Notification notifyType) |
| 269 |
{ |
| 270 |
if (ProtoChannel::NOTIFY_INPUT != notifyType) return; |
| 271 |
|
| 272 |
char buffer[256]; |
| 273 |
unsigned int numBytes = 255; |
| 274 |
if (serial->Read(buffer, numBytes)) |
| 275 |
{ |
| 276 |
buffer[numBytes] = '\0'; |
| 277 |
if (0 != numBytes) |
| 278 |
TRACE("%s", buffer); |
| 279 |
else |
| 280 |
TRACE("ZERO BYTES READ!\n"); |
| 281 |
|
| 282 |
PrintSignalStatus(); |
| 283 |
|
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
PLOG(PL_ERROR, "SerialApp::OnSerialInput() error: unable to read from device\n"); |
| 288 |
} |
| 289 |
|
| 290 |
} // end SerialApp::OnSerialInput() |
| 291 |
|
| 292 |
bool SerialApp::OnTxTimeout(ProtoTimer& /*theTimer*/) |
| 293 |
{ |
| 294 |
|
| 295 |
if (serial->IsSet(ProtoSerial::RTS)) |
| 296 |
serial->Clear(ProtoSerial::RTS); |
| 297 |
else |
| 298 |
serial->Set(ProtoSerial::RTS); |
| 299 |
|
| 300 |
|
| 301 |
const char* buffer = "Hello, serial world.\n"; |
| 302 |
unsigned int numBytes = strlen(buffer); |
| 303 |
if (serial->Write(buffer, numBytes)) |
| 304 |
{ |
| 305 |
if (numBytes != strlen(buffer)) |
| 306 |
TRACE("PARTIAL SEND!\n"); |
| 307 |
else |
| 308 |
TRACE("wrote %d bytes ...\n", numBytes); |
| 309 |
|
| 310 |
PrintSignalStatus(); |
| 311 |
|
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
PLOG(PL_ERROR, "SerialApp::OnSerialInput() error: unable to write to device\n"); |
| 316 |
} |
| 317 |
|
| 318 |
return true; |
| 319 |
} // end SerialApp::OnTxTimeout() |
| 320 |
|
| 321 |
void SerialApp::PrintSignalStatus() |
| 322 |
{ |
| 323 |
ASSERT(NULL != serial); |
| 324 |
if (serial->IsOpen()) |
| 325 |
{ |
| 326 |
int status = serial->GetStatus(); |
| 327 |
PLOG(PL_ALWAYS, "status: DTR:%d RTS:%d CTS:%d DCD:%d DSR:%d RNG:%d\n", |
| 328 |
ProtoSerial::IsSet(status, ProtoSerial::DTR), |
| 329 |
ProtoSerial::IsSet(status, ProtoSerial::RTS), |
| 330 |
ProtoSerial::IsSet(status, ProtoSerial::CTS), |
| 331 |
ProtoSerial::IsSet(status, ProtoSerial::DCD), |
| 332 |
ProtoSerial::IsSet(status, ProtoSerial::DSR), |
| 333 |
ProtoSerial::IsSet(status, ProtoSerial::RNG)); |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
PLOG(PL_ALWAYS, "status: serial port not open!\n"); |
| 338 |
} |
| 339 |
} // end SerialApp::PrintSignalStatus() |
| 340 |
|