ebusctl: interactive mode added.
This commit is contained in:
+130
-29
@@ -25,6 +25,7 @@
|
||||
#include "tcpsocket.h"
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -46,6 +47,75 @@ void define_args()
|
||||
|
||||
}
|
||||
|
||||
enum CommandType {
|
||||
ct_open,
|
||||
ct_exit,
|
||||
ct_help,
|
||||
ct_invalid
|
||||
};
|
||||
|
||||
CommandType getCase(const string& item)
|
||||
{
|
||||
if (strcasecmp(item.c_str(), "OPEN") == 0) return ct_open;
|
||||
if (strcasecmp(item.c_str(), "EXIT") == 0) return ct_exit;
|
||||
if (strcasecmp(item.c_str(), "HELP") == 0) return ct_help;
|
||||
|
||||
return ct_invalid;
|
||||
}
|
||||
|
||||
bool connect(const char* host, int port, bool once=true)
|
||||
{
|
||||
|
||||
TCPClient* client = new TCPClient();
|
||||
TCPSocket* socket = client->connect(host, port);
|
||||
|
||||
if (socket != NULL) {
|
||||
|
||||
do {
|
||||
string message;
|
||||
|
||||
if (once == false) {
|
||||
cout << "online: ";
|
||||
getline(cin, message);
|
||||
}
|
||||
else {
|
||||
message = A.getCommand();
|
||||
for (int i = 0; i < A.numArgs(); i++) {
|
||||
message += " ";
|
||||
message += A.getArg(i);
|
||||
}
|
||||
}
|
||||
|
||||
socket->send(message.c_str(), message.size());
|
||||
|
||||
if (strncasecmp(message.c_str(), "QUIT", 4) != 0 && strncasecmp(message.c_str(), "STOP", 4) != 0) {
|
||||
|
||||
char data[1024];
|
||||
size_t datalen;
|
||||
|
||||
datalen = socket->recv(data, sizeof(data)-1);
|
||||
data[datalen] = '\0';
|
||||
|
||||
cout << data;
|
||||
}
|
||||
break;
|
||||
|
||||
} while (once == false);
|
||||
|
||||
delete socket;
|
||||
|
||||
}
|
||||
else
|
||||
cout << "error connecting to " << host << ":" << port << endl;
|
||||
|
||||
delete client;
|
||||
|
||||
if (once == false)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// define arguments and application variables
|
||||
@@ -56,37 +126,68 @@ int main(int argc, char* argv[])
|
||||
|
||||
if (A.missingCommand() == true) {
|
||||
cout << "interactive mode started." << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
bool running = true;
|
||||
|
||||
do {
|
||||
string input, token;
|
||||
vector<string> cmd;
|
||||
|
||||
cout << "$: ";
|
||||
getline(cin, input);
|
||||
|
||||
// prepare input
|
||||
istringstream stream(input);
|
||||
while (getline(stream, token, ' ') != 0)
|
||||
cmd.push_back(token);
|
||||
|
||||
if (cmd.size() == 0)
|
||||
cout << "command missing" << endl;
|
||||
|
||||
switch (getCase(cmd[0])) {
|
||||
case ct_invalid:
|
||||
cout << "command not found" << endl;
|
||||
break;
|
||||
|
||||
case ct_open:
|
||||
{
|
||||
bool ret = true;
|
||||
cout << "connect to..." << endl;
|
||||
if (cmd.size() == 1)
|
||||
ret = connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"), false);
|
||||
else if (cmd.size() == 2)
|
||||
ret = connect(cmd[1].c_str(), A.getOptVal<int>("port"), false);
|
||||
else if (cmd.size() == 3)
|
||||
ret = connect(cmd[1].c_str(), atoi(cmd[2].c_str()), false);
|
||||
else
|
||||
cout << "open [host [port]]" << endl;
|
||||
|
||||
running = ret;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ct_exit:
|
||||
running = false;
|
||||
break;
|
||||
|
||||
case ct_help:
|
||||
cout << "commands:" << endl
|
||||
<< " open - open connection to ebusd 'open [host [port]]'" << endl
|
||||
<< " exit - exit ebusctl" << endl
|
||||
<< " help - print this page" << endl;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
} while (running == true);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
TCPClient* client = new TCPClient();
|
||||
TCPSocket* socket = client->connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"));
|
||||
|
||||
if (socket != NULL) {
|
||||
// build message
|
||||
string message(A.getCommand());
|
||||
for (int i = 0; i < A.numArgs(); i++) {
|
||||
message += " ";
|
||||
message += A.getArg(i);
|
||||
}
|
||||
|
||||
socket->send(message.c_str(), message.size());
|
||||
|
||||
char data[1024];
|
||||
size_t datalen;
|
||||
|
||||
datalen = socket->recv(data, sizeof(data)-1);
|
||||
data[datalen] = '\0';
|
||||
|
||||
cout << data;
|
||||
|
||||
delete socket;
|
||||
}
|
||||
else
|
||||
cout << "error connecting to " << A.getOptVal<const char*>("server")
|
||||
<< ":" << A.getOptVal<int>("port") << endl;
|
||||
|
||||
delete client;
|
||||
connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"));
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -36,14 +36,14 @@ void define_args()
|
||||
{
|
||||
A.setVersion("ebusfeed is part of """PACKAGE_STRING"");
|
||||
|
||||
A.addText(" 'ebusfeed' sends a raw dump file to a local serial device (pts)\n\n"
|
||||
A.addText(" 'ebusfeed' sends a dump file to a local serial device (pts)\n\n"
|
||||
" Usage: 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n"
|
||||
" 2. create symbol links to appropriate devices\n"
|
||||
" for example: 'ln -s /dev/pts/2 /dev/ttyUSB60'\n"
|
||||
" 'ln -s /dev/pts/3 /dev/ttyUSB20'\n"
|
||||
" 3. start ebusd: 'ebusd -f -d /dev/ttyUSB20'\n"
|
||||
" 4. start ebusfeed: 'ebusfeed /path/to/raw_file'\n\n"
|
||||
"Command: '/path/to/raw_file'\n\n"
|
||||
" 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n\n"
|
||||
"Command: '/path/to/ebus_dump.bin'\n\n"
|
||||
"Options:\n");
|
||||
|
||||
A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory,
|
||||
@@ -62,6 +62,11 @@ int main(int argc, char* argv[])
|
||||
// parse arguments
|
||||
A.parseArgs(argc, argv);
|
||||
|
||||
if (A.missingCommand() == true) {
|
||||
cout << "ebus dump file is required." << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
string dev(A.getOptVal<const char*>("device"));
|
||||
Port port(dev, true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user