use spaces instead of tab
This commit is contained in:
+183
-183
@@ -17,13 +17,13 @@
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <argp.h>
|
||||
#include <string.h>
|
||||
#ifdef HAVE_PPOLL
|
||||
# include <poll.h>
|
||||
# include <poll.h>
|
||||
#endif
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
@@ -39,20 +39,20 @@ using std::endl;
|
||||
|
||||
/** A structure holding all program options. */
|
||||
struct options {
|
||||
const char* server; //!< ebusd server host (name or ip) [localhost]
|
||||
uint16_t port; //!< ebusd server port [8888]
|
||||
const char* server; //!< ebusd server host (name or ip) [localhost]
|
||||
uint16_t port; //!< ebusd server port [8888]
|
||||
|
||||
char* const *args; //!< arguments to pass to ebusd
|
||||
unsigned int argCount; //!< number of arguments to pass to ebusd
|
||||
char* const *args; //!< arguments to pass to ebusd
|
||||
unsigned int argCount; //!< number of arguments to pass to ebusd
|
||||
};
|
||||
|
||||
/** the program options. */
|
||||
static struct options opt = {
|
||||
"localhost", // server
|
||||
8888, // port
|
||||
"localhost", // server
|
||||
8888, // port
|
||||
|
||||
NULL, // args
|
||||
0 // argCount
|
||||
NULL, // args
|
||||
0 // argCount
|
||||
};
|
||||
|
||||
/** the version string of the program. */
|
||||
@@ -63,21 +63,21 @@ const char *argp_program_bug_address = ""PACKAGE_BUGREPORT"";
|
||||
|
||||
/** the documentation of the program. */
|
||||
static const char argpdoc[] =
|
||||
"Client for acessing "PACKAGE" via TCP.\n"
|
||||
"\v"
|
||||
"If given, send COMMAND together with CMDOPT options to "PACKAGE".\n"
|
||||
"Use 'help' as COMMAND for help on available "PACKAGE" commands.";
|
||||
"Client for acessing "PACKAGE" via TCP.\n"
|
||||
"\v"
|
||||
"If given, send COMMAND together with CMDOPT options to "PACKAGE".\n"
|
||||
"Use 'help' as COMMAND for help on available "PACKAGE" commands.";
|
||||
|
||||
/** the description of the accepted arguments. */
|
||||
static char argpargsdoc[] = "\nCOMMAND [CMDOPT...]";
|
||||
|
||||
/** the definition of the known program arguments. */
|
||||
static const struct argp_option argpoptions[] = {
|
||||
{NULL, 0, NULL, 0, "Options:", 1 },
|
||||
{"server", 's', "HOST", 0, "Connect to HOST running "PACKAGE" (name or IP) [localhost]", 0 },
|
||||
{"port", 'p', "PORT", 0, "Connect to PORT on HOST [8888]", 0 },
|
||||
{NULL, 0, NULL, 0, "Options:", 1 },
|
||||
{"server", 's', "HOST", 0, "Connect to HOST running "PACKAGE" (name or IP) [localhost]", 0 },
|
||||
{"port", 'p', "PORT", 0, "Connect to PORT on HOST [8888]", 0 },
|
||||
|
||||
{NULL, 0, NULL, 0, NULL, 0 },
|
||||
{NULL, 0, NULL, 0, NULL, 0 },
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -87,216 +87,216 @@ static const struct argp_option argpoptions[] = {
|
||||
* @param state the parsing state.
|
||||
*/
|
||||
error_t parse_opt(int key, char *arg, struct argp_state *state) {
|
||||
struct options *opt = (struct options*)state->input;
|
||||
char* strEnd = NULL;
|
||||
unsigned int port;
|
||||
switch (key) {
|
||||
// Device settings:
|
||||
case 's': // --server=localhost
|
||||
if (arg == NULL || arg[0] == 0) {
|
||||
argp_error(state, "invalid server");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->server = arg;
|
||||
break;
|
||||
case 'p': // --port=8888
|
||||
port = strtoul(arg, &strEnd, 10);
|
||||
if (strEnd == NULL || strEnd == arg || *strEnd != 0 || port < 1 || port > 65535) {
|
||||
argp_error(state, "invalid port");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->port = (uint16_t)port;
|
||||
break;
|
||||
case ARGP_KEY_ARGS:
|
||||
opt->args = state->argv + state->next;
|
||||
opt->argCount = state->argc - state->next;
|
||||
break;
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
struct options *opt = (struct options*)state->input;
|
||||
char* strEnd = NULL;
|
||||
unsigned int port;
|
||||
switch (key) {
|
||||
// Device settings:
|
||||
case 's': // --server=localhost
|
||||
if (arg == NULL || arg[0] == 0) {
|
||||
argp_error(state, "invalid server");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->server = arg;
|
||||
break;
|
||||
case 'p': // --port=8888
|
||||
port = strtoul(arg, &strEnd, 10);
|
||||
if (strEnd == NULL || strEnd == arg || *strEnd != 0 || port < 1 || port > 65535) {
|
||||
argp_error(state, "invalid port");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->port = (uint16_t)port;
|
||||
break;
|
||||
case ARGP_KEY_ARGS:
|
||||
opt->args = state->argv + state->next;
|
||||
opt->argCount = state->argc - state->next;
|
||||
break;
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
string fetchData(TCPSocket* socket, bool& listening) {
|
||||
char data[1024];
|
||||
ssize_t datalen;
|
||||
ostringstream ostream;
|
||||
string message, sendmessage;
|
||||
char data[1024];
|
||||
ssize_t datalen;
|
||||
ostringstream ostream;
|
||||
string message, sendmessage;
|
||||
|
||||
int ret;
|
||||
struct timespec tdiff;
|
||||
int ret;
|
||||
struct timespec tdiff;
|
||||
|
||||
// set timeout
|
||||
tdiff.tv_sec = 0;
|
||||
tdiff.tv_nsec = 1E8;
|
||||
// set timeout
|
||||
tdiff.tv_sec = 0;
|
||||
tdiff.tv_nsec = 1E8;
|
||||
|
||||
#ifdef HAVE_PPOLL
|
||||
int nfds = 2;
|
||||
struct pollfd fds[nfds];
|
||||
int nfds = 2;
|
||||
struct pollfd fds[nfds];
|
||||
|
||||
memset(fds, 0, sizeof(fds));
|
||||
memset(fds, 0, sizeof(fds));
|
||||
|
||||
fds[0].fd = STDIN_FILENO;
|
||||
fds[0].events = POLLIN;
|
||||
fds[0].fd = STDIN_FILENO;
|
||||
fds[0].events = POLLIN;
|
||||
|
||||
fds[1].fd = socket->getFD();
|
||||
fds[1].events = POLLIN;
|
||||
fds[1].fd = socket->getFD();
|
||||
fds[1].events = POLLIN;
|
||||
#else
|
||||
#ifdef HAVE_PSELECT
|
||||
int maxfd;
|
||||
fd_set checkfds;
|
||||
int maxfd;
|
||||
fd_set checkfds;
|
||||
|
||||
FD_ZERO(&checkfds);
|
||||
FD_SET(STDIN_FILENO, &checkfds);
|
||||
FD_SET(socket->getFD(), &checkfds);
|
||||
maxfd = STDIN_FILENO;
|
||||
if (socket->getFD() > maxfd) {
|
||||
maxfd = socket->getFD();
|
||||
}
|
||||
FD_ZERO(&checkfds);
|
||||
FD_SET(STDIN_FILENO, &checkfds);
|
||||
FD_SET(socket->getFD(), &checkfds);
|
||||
maxfd = STDIN_FILENO;
|
||||
if (socket->getFD() > maxfd) {
|
||||
maxfd = socket->getFD();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
while(true) {
|
||||
while(true) {
|
||||
#ifdef HAVE_PPOLL
|
||||
// wait for new fd event
|
||||
ret = ppoll(fds, nfds, &tdiff, NULL);
|
||||
// wait for new fd event
|
||||
ret = ppoll(fds, nfds, &tdiff, NULL);
|
||||
#else
|
||||
#ifdef HAVE_PSELECT
|
||||
// set readfds to inital checkfds
|
||||
fd_set readfds = checkfds;
|
||||
// wait for new fd event
|
||||
ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tdiff, NULL);
|
||||
// set readfds to inital checkfds
|
||||
fd_set readfds = checkfds;
|
||||
// wait for new fd event
|
||||
ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tdiff, NULL);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bool newData = false;
|
||||
bool newInput = false;
|
||||
if (ret != 0) {
|
||||
bool newData = false;
|
||||
bool newInput = false;
|
||||
if (ret != 0) {
|
||||
#ifdef HAVE_PPOLL
|
||||
// new data from notify
|
||||
newInput = fds[0].revents & POLLIN;
|
||||
// new data from notify
|
||||
newInput = fds[0].revents & POLLIN;
|
||||
|
||||
// new data from socket
|
||||
newData = fds[1].revents & POLLIN;
|
||||
// new data from socket
|
||||
newData = fds[1].revents & POLLIN;
|
||||
#else
|
||||
#ifdef HAVE_PSELECT
|
||||
// new data from notify
|
||||
newInput = FD_ISSET(STDIN_FILENO, &readfds);
|
||||
// new data from notify
|
||||
newInput = FD_ISSET(STDIN_FILENO, &readfds);
|
||||
|
||||
// new data from socket
|
||||
newData = FD_ISSET(socket->getFD(), &readfds);
|
||||
// new data from socket
|
||||
newData = FD_ISSET(socket->getFD(), &readfds);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (newData) {
|
||||
if (socket->isValid()) {
|
||||
datalen = socket->recv(data, sizeof(data));
|
||||
if (newData) {
|
||||
if (socket->isValid()) {
|
||||
datalen = socket->recv(data, sizeof(data));
|
||||
|
||||
if (datalen < 0) {
|
||||
perror("recv");
|
||||
break;
|
||||
}
|
||||
if (datalen < 0) {
|
||||
perror("recv");
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < datalen; i++) {
|
||||
ostream << data[i];
|
||||
}
|
||||
string str = ostream.str();
|
||||
if (listening) {
|
||||
return str;
|
||||
}
|
||||
if (str.length() >= 2 && str[str.length()-2] == '\n' && str[str.length()-1] == '\n') {
|
||||
return str;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if (newInput) {
|
||||
getline(cin, message);
|
||||
sendmessage = message+'\n';
|
||||
socket->send(sendmessage.c_str(), sendmessage.size());
|
||||
for (int i = 0; i < datalen; i++) {
|
||||
ostream << data[i];
|
||||
}
|
||||
string str = ostream.str();
|
||||
if (listening) {
|
||||
return str;
|
||||
}
|
||||
if (str.length() >= 2 && str[str.length()-2] == '\n' && str[str.length()-1] == '\n') {
|
||||
return str;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if (newInput) {
|
||||
getline(cin, message);
|
||||
sendmessage = message+'\n';
|
||||
socket->send(sendmessage.c_str(), sendmessage.size());
|
||||
|
||||
if (strcasecmp(message.c_str(), "Q") == 0
|
||||
|| strcasecmp(message.c_str(), "QUIT") == 0
|
||||
|| strcasecmp(message.c_str(), "STOP") == 0) {
|
||||
exit(EXIT_SUCCESS);
|
||||
return "";
|
||||
}
|
||||
message.clear();
|
||||
}
|
||||
}
|
||||
if (strcasecmp(message.c_str(), "Q") == 0
|
||||
|| strcasecmp(message.c_str(), "QUIT") == 0
|
||||
|| strcasecmp(message.c_str(), "STOP") == 0) {
|
||||
exit(EXIT_SUCCESS);
|
||||
return "";
|
||||
}
|
||||
message.clear();
|
||||
}
|
||||
}
|
||||
|
||||
return ostream.str();
|
||||
return ostream.str();
|
||||
}
|
||||
|
||||
void connect(const char* host, uint16_t port, char* const *args, int argCount) {
|
||||
TCPClient* client = new TCPClient();
|
||||
TCPSocket* socket = client->connect(host, port);
|
||||
TCPClient* client = new TCPClient();
|
||||
TCPSocket* socket = client->connect(host, port);
|
||||
|
||||
bool once = args != NULL && argCount > 0;
|
||||
if (socket != NULL) {
|
||||
string message, sendmessage;
|
||||
do {
|
||||
bool listening = false;
|
||||
bool once = args != NULL && argCount > 0;
|
||||
if (socket != NULL) {
|
||||
string message, sendmessage;
|
||||
do {
|
||||
bool listening = false;
|
||||
|
||||
if (!once) {
|
||||
cout << host << ": ";
|
||||
getline(cin, message);
|
||||
} else {
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
if (i > 0) {
|
||||
message += " ";
|
||||
}
|
||||
bool quote = strchr(args[i], ' ') != NULL && strchr(args[i], '"') == NULL;
|
||||
if (quote) {
|
||||
message += "\"";
|
||||
}
|
||||
message += args[i];
|
||||
if (quote) {
|
||||
message += "\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!once) {
|
||||
cout << host << ": ";
|
||||
getline(cin, message);
|
||||
} else {
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
if (i > 0) {
|
||||
message += " ";
|
||||
}
|
||||
bool quote = strchr(args[i], ' ') != NULL && strchr(args[i], '"') == NULL;
|
||||
if (quote) {
|
||||
message += "\"";
|
||||
}
|
||||
message += args[i];
|
||||
if (quote) {
|
||||
message += "\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sendmessage = message+'\n';
|
||||
socket->send(sendmessage.c_str(), sendmessage.size());
|
||||
sendmessage = message+'\n';
|
||||
socket->send(sendmessage.c_str(), sendmessage.size());
|
||||
|
||||
if (strcasecmp(message.c_str(), "Q") == 0
|
||||
|| strcasecmp(message.c_str(), "QUIT") == 0
|
||||
|| strcasecmp(message.c_str(), "STOP") == 0)
|
||||
break;
|
||||
if (strcasecmp(message.c_str(), "Q") == 0
|
||||
|| strcasecmp(message.c_str(), "QUIT") == 0
|
||||
|| strcasecmp(message.c_str(), "STOP") == 0)
|
||||
break;
|
||||
|
||||
if (message.length() > 0) {
|
||||
if (strcasecmp(message.c_str(), "L") == 0
|
||||
|| strcasecmp(message.c_str(), "LISTEN") == 0) {
|
||||
listening = true;
|
||||
while (listening && !cin.eof()) {
|
||||
string result(fetchData(socket, listening));
|
||||
cout << result;
|
||||
if (strcasecmp(result.c_str(), "LISTEN STOPPED") == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cout << fetchData(socket, listening);
|
||||
}
|
||||
}
|
||||
} while (!once && !cin.eof());
|
||||
delete socket;
|
||||
} else {
|
||||
cout << "error connecting to " << host << ":" << port << endl;
|
||||
}
|
||||
delete client;
|
||||
if (message.length() > 0) {
|
||||
if (strcasecmp(message.c_str(), "L") == 0
|
||||
|| strcasecmp(message.c_str(), "LISTEN") == 0) {
|
||||
listening = true;
|
||||
while (listening && !cin.eof()) {
|
||||
string result(fetchData(socket, listening));
|
||||
cout << result;
|
||||
if (strcasecmp(result.c_str(), "LISTEN STOPPED") == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cout << fetchData(socket, listening);
|
||||
}
|
||||
}
|
||||
} while (!once && !cin.eof());
|
||||
delete socket;
|
||||
} else {
|
||||
cout << "error connecting to " << host << ":" << port << endl;
|
||||
}
|
||||
delete client;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
struct argp argp = { argpoptions, parse_opt, argpargsdoc, argpdoc, NULL, NULL, NULL };
|
||||
setenv("ARGP_HELP_FMT", "no-dup-args-note", 0);
|
||||
if (argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opt) != 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
connect(opt.server, opt.port, opt.args, opt.argCount);
|
||||
struct argp argp = { argpoptions, parse_opt, argpargsdoc, argpdoc, NULL, NULL, NULL };
|
||||
setenv("ARGP_HELP_FMT", "no-dup-args-note", 0);
|
||||
if (argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opt) != 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
connect(opt.server, opt.port, opt.args, opt.argCount);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
+91
-91
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <argp.h>
|
||||
@@ -42,18 +42,18 @@ using ebusd::Device;
|
||||
|
||||
/** A structure holding all program options. */
|
||||
struct options {
|
||||
const char* device; //!< device to write to [/dev/ttyUSB60]
|
||||
unsigned int time; //!< delay between bytes in us [10000]
|
||||
const char* device; //!< device to write to [/dev/ttyUSB60]
|
||||
unsigned int time; //!< delay between bytes in us [10000]
|
||||
|
||||
const char* dumpFile; //!< dump file to read
|
||||
const char* dumpFile; //!< dump file to read
|
||||
};
|
||||
|
||||
/** the program options. */
|
||||
static struct options opt = {
|
||||
"/dev/ttyUSB60", // device
|
||||
10000, // time
|
||||
"/dev/ttyUSB60", // device
|
||||
10000, // time
|
||||
|
||||
"/tmp/ebus_dump.bin", // dumpFile
|
||||
"/tmp/ebus_dump.bin", // dumpFile
|
||||
};
|
||||
|
||||
/** the version string of the program. */
|
||||
@@ -64,27 +64,27 @@ const char *argp_program_bug_address = ""PACKAGE_BUGREPORT"";
|
||||
|
||||
/** the documentation of the program. */
|
||||
static const char argpdoc[] =
|
||||
"Feed data from an "PACKAGE" DUMPFILE to a serial device.\n"
|
||||
"\v"
|
||||
"With no DUMPFILE, /tmp/ebus_dump.bin is used.\n"
|
||||
"\n"
|
||||
"Example for setting up two pseudo terminals with 'socat':\n"
|
||||
" 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n"
|
||||
" 2. create symbol links to appropriate devices, e.g.\n"
|
||||
" 'ln -s /dev/pts/2 /dev/ttyUSB60'\n"
|
||||
" 'ln -s /dev/pts/3 /dev/ttyUSB20'\n"
|
||||
" 3. start "PACKAGE": '"PACKAGE" -f -d /dev/ttyUSB20 --nodevicecheck'\n"
|
||||
" 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n";
|
||||
"Feed data from an "PACKAGE" DUMPFILE to a serial device.\n"
|
||||
"\v"
|
||||
"With no DUMPFILE, /tmp/ebus_dump.bin is used.\n"
|
||||
"\n"
|
||||
"Example for setting up two pseudo terminals with 'socat':\n"
|
||||
" 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n"
|
||||
" 2. create symbol links to appropriate devices, e.g.\n"
|
||||
" 'ln -s /dev/pts/2 /dev/ttyUSB60'\n"
|
||||
" 'ln -s /dev/pts/3 /dev/ttyUSB20'\n"
|
||||
" 3. start "PACKAGE": '"PACKAGE" -f -d /dev/ttyUSB20 --nodevicecheck'\n"
|
||||
" 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n";
|
||||
|
||||
/** the description of the accepted arguments. */
|
||||
static char argpargsdoc[] = "[DUMPFILE]";
|
||||
|
||||
/** the definition of the known program arguments. */
|
||||
static const struct argp_option argpoptions[] = {
|
||||
{"device", 'd', "DEV", 0, "Write to DEV (serial device) [/dev/ttyUSB60]", 0 },
|
||||
{"time", 't', "USEC", 0, "Delay each byte by USEC us [10000]", 0 },
|
||||
{"device", 'd', "DEV", 0, "Write to DEV (serial device) [/dev/ttyUSB60]", 0 },
|
||||
{"time", 't', "USEC", 0, "Delay each byte by USEC us [10000]", 0 },
|
||||
|
||||
{NULL, 0, NULL, 0, NULL, 0 },
|
||||
{NULL, 0, NULL, 0, NULL, 0 },
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -94,80 +94,80 @@ static const struct argp_option argpoptions[] = {
|
||||
* @param state the parsing state.
|
||||
*/
|
||||
error_t parse_opt(int key, char *arg, struct argp_state *state) {
|
||||
struct options *opt = (struct options*)state->input;
|
||||
char* strEnd = NULL;
|
||||
switch (key) {
|
||||
// Device settings:
|
||||
case 'd': // --device=/dev/ttyUSB60
|
||||
if (arg == NULL || arg[0] == 0) {
|
||||
argp_error(state, "invalid device");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->device = arg;
|
||||
break;
|
||||
case 't': // --time=10000
|
||||
opt->time = (unsigned int)strtoul(arg, &strEnd, 10);
|
||||
if (strEnd == NULL || strEnd == arg || *strEnd != 0 || opt->time < 1000 || opt->time > 100000000) {
|
||||
argp_error(state, "invalid time");
|
||||
return EINVAL;
|
||||
}
|
||||
break;
|
||||
case ARGP_KEY_ARG:
|
||||
if (state->arg_num == 0) {
|
||||
if (arg == NULL || arg[0] == 0 || strcmp("/", arg) == 0) {
|
||||
argp_error(state, "invalid dumpfile");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->dumpFile = arg;
|
||||
} else {
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
struct options *opt = (struct options*)state->input;
|
||||
char* strEnd = NULL;
|
||||
switch (key) {
|
||||
// Device settings:
|
||||
case 'd': // --device=/dev/ttyUSB60
|
||||
if (arg == NULL || arg[0] == 0) {
|
||||
argp_error(state, "invalid device");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->device = arg;
|
||||
break;
|
||||
case 't': // --time=10000
|
||||
opt->time = (unsigned int)strtoul(arg, &strEnd, 10);
|
||||
if (strEnd == NULL || strEnd == arg || *strEnd != 0 || opt->time < 1000 || opt->time > 100000000) {
|
||||
argp_error(state, "invalid time");
|
||||
return EINVAL;
|
||||
}
|
||||
break;
|
||||
case ARGP_KEY_ARG:
|
||||
if (state->arg_num == 0) {
|
||||
if (arg == NULL || arg[0] == 0 || strcmp("/", arg) == 0) {
|
||||
argp_error(state, "invalid dumpfile");
|
||||
return EINVAL;
|
||||
}
|
||||
opt->dumpFile = arg;
|
||||
} else {
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
struct argp argp = { argpoptions, parse_opt, argpargsdoc, argpdoc, NULL, NULL, NULL };
|
||||
setenv("ARGP_HELP_FMT", "no-dup-args-note", 0);
|
||||
if (argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opt) != 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
Device* device = Device::create(opt.device, false, false, NULL);
|
||||
if (device == NULL) {
|
||||
cout << "unable to create device " << opt.device << endl;
|
||||
return EINVAL;
|
||||
}
|
||||
result_t result = device->open();
|
||||
if (result != ebusd::RESULT_OK) {
|
||||
cout << "unable to open " << opt.device << ": " << getResultCode(result) << endl;
|
||||
}
|
||||
if (!device->isValid()) {
|
||||
cout << "device " << opt.device << " not available" << endl;
|
||||
} else {
|
||||
cout << "device opened" << endl;
|
||||
fstream file(opt.dumpFile, ios::in | ios::binary);
|
||||
if (file.is_open()) {
|
||||
while (true) {
|
||||
unsigned char byte = (unsigned char)file.get();
|
||||
if (file.eof()) {
|
||||
break;
|
||||
}
|
||||
cout << hex << setw(2) << setfill('0')
|
||||
<< static_cast<unsigned>(byte) << endl;
|
||||
device->send(byte);
|
||||
usleep(opt.time);
|
||||
}
|
||||
file.close();
|
||||
} else {
|
||||
cout << "error opening file " << opt.dumpFile << endl;
|
||||
}
|
||||
}
|
||||
struct argp argp = { argpoptions, parse_opt, argpargsdoc, argpdoc, NULL, NULL, NULL };
|
||||
setenv("ARGP_HELP_FMT", "no-dup-args-note", 0);
|
||||
if (argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opt) != 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
Device* device = Device::create(opt.device, false, false, NULL);
|
||||
if (device == NULL) {
|
||||
cout << "unable to create device " << opt.device << endl;
|
||||
return EINVAL;
|
||||
}
|
||||
result_t result = device->open();
|
||||
if (result != ebusd::RESULT_OK) {
|
||||
cout << "unable to open " << opt.device << ": " << getResultCode(result) << endl;
|
||||
}
|
||||
if (!device->isValid()) {
|
||||
cout << "device " << opt.device << " not available" << endl;
|
||||
} else {
|
||||
cout << "device opened" << endl;
|
||||
fstream file(opt.dumpFile, ios::in | ios::binary);
|
||||
if (file.is_open()) {
|
||||
while (true) {
|
||||
unsigned char byte = (unsigned char)file.get();
|
||||
if (file.eof()) {
|
||||
break;
|
||||
}
|
||||
cout << hex << setw(2) << setfill('0')
|
||||
<< static_cast<unsigned>(byte) << endl;
|
||||
device->send(byte);
|
||||
usleep(opt.time);
|
||||
}
|
||||
file.close();
|
||||
} else {
|
||||
cout << "error opening file " << opt.dumpFile << endl;
|
||||
}
|
||||
}
|
||||
|
||||
delete device;
|
||||
exit(EXIT_SUCCESS);
|
||||
delete device;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user