Merge branch 'master' of github.com:yuhu-/ebusd

This commit is contained in:
john30
2014-12-29 21:56:53 +01:00
3 changed files with 44 additions and 111 deletions
+3 -4
View File
@@ -153,11 +153,10 @@ void BaseLoop::start()
} }
void BaseLoop::logRaw(const unsigned char byte, bool received) { void BaseLoop::logRaw(const unsigned char byte, bool received) {
if (received == true) { if (received == true)
L.log(bus, event, "<%02x", byte); L.log(bus, event, "<%02x", byte);
} else { else
L.log(bus, event, ">%02x", byte); L.log(bus, event, ">%02x", byte);
}
} }
string BaseLoop::decodeMessage(const string& data) string BaseLoop::decodeMessage(const string& data)
@@ -553,7 +552,7 @@ string BaseLoop::decodeMessage(const string& data)
result << "commands:" << endl result << "commands:" << endl
<< " read - read ebus values 'read [-v] [-f] [-m seconds] [class] name' or 'read [-v] [-f] [-m seconds] class name field'" << endl << " read - read ebus values 'read [-v] [-f] [-m seconds] [class] name' or 'read [-v] [-f] [-m seconds] class name field'" << endl
<< " write - write ebus values 'write class name value[;value]*' or 'write -h ZZPBSBNNDx'" << endl << " write - write ebus values 'write class name value[;value]*' or 'write -h ZZPBSBNNDx'" << endl
<< " find - find ebus values 'find [name]' or 'find class name'" << endl << " find - find ebus values usage: 'find [-v] [-r] [-w] [-p] [name]' or 'find [-v] [-r] [-w] [-p] class name'" << endl << endl
<< " scan - scan ebus known addresses 'scan'" << endl << " scan - scan ebus known addresses 'scan'" << endl
<< " - scan ebus all addresses 'scan full'" << endl << " - scan ebus all addresses 'scan full'" << endl
<< " - show scan results 'scan result'" << endl << endl << " - show scan results 'scan result'" << endl << endl
+26 -22
View File
@@ -31,34 +31,29 @@ using namespace std;
/** @brief the available data types. */ /** @brief the available data types. */
enum DataType { enum DataType {
dt_none, /*!< default for __text_only__ */ dt_none, //!< default for __text_only__
dt_bool, /*!< boolean */ dt_bool, //!< boolean
dt_hex, /*!< hex integer */ dt_hex, //!< hex integer
dt_int, /*!< dec integer */ dt_int, //!< dec integer
dt_long, /*!< long */ dt_long, //!< long
dt_float, /*!< float */ dt_float, //!< float
dt_string /*!< string */ dt_string //!< string
}; };
/** @brief option types. */ /** @brief option types. */
enum OptionType { enum OptionType {
ot_none, /*!< no option type is needed */ ot_none, //!< no option type is needed
ot_optional, /*!< a value is optional */ ot_optional, //!< a value is optional
ot_mandatory /*!< a value is mandatory */ ot_mandatory //!< a value is mandatory
}; };
/** @brief structure for defining application options */ /** @brief structure for defining application options */
typedef struct { typedef struct {
/** long option name */ const char* name; //!< long option name
const char* name; const char* shortname; //!< short option name
/** short option name */ const char* description; //!< description for this option
const char* shortname; DataType datatype; //!< data type for this option
/** description for this option */ OptionType optiontype; //!< indicates whether an option takes an argument
const char* description;
/** data type for this option */
DataType datatype;
/** indicates whether an option takes an argument */
OptionType optiontype;
} opt_t; } opt_t;
/** /**
@@ -67,12 +62,16 @@ typedef struct {
union OptVal { union OptVal {
/** boolean */ /** boolean */
bool b; bool b;
/** integer */ /** integer */
int i; int i;
/** long */ /** long */
long l; long l;
/** float */ /** float */
float f; float f;
/** string */ /** string */
const char* c; const char* c;
@@ -80,26 +79,31 @@ union OptVal {
* @brief clear memory * @brief clear memory
*/ */
OptVal() { memset(this, 0, sizeof(OptVal)); } OptVal() { memset(this, 0, sizeof(OptVal)); }
/** /**
* @brief create boolean type * @brief create boolean type
* @param _b the boolean * @param _b the boolean
*/ */
OptVal(bool _b) : b(_b) {} OptVal(bool _b) : b(_b) {}
/** /**
* @brief create integer type * @brief create integer type
* @param _i the integer * @param _i the integer
*/ */
OptVal(int _i) : i(_i) {} OptVal(int _i) : i(_i) {}
/** /**
* @brief create long type * @brief create long type
* @param _l the long * @param _l the long
*/ */
OptVal(long _l) : l(_l) {} OptVal(long _l) : l(_l) {}
/** /**
* @brief create float type * @brief create float type
* @param _f the float * @param _f the float
*/ */
OptVal(float _f) : f(_f) {} OptVal(float _f) : f(_f) {}
/** /**
* @brief create string type * @brief create string type
* @param _c the string * @param _c the string
@@ -191,8 +195,8 @@ public:
string getCommand() const { return m_command; } string getCommand() const { return m_command; }
/** /**
* @brief returns the string of given command. * @brief Get whether a command string is missing.
* @return the command string. * @return true if a command string is missing.
*/ */
bool missingCommand() const { return (m_command.size() == 0 ? true : false); } bool missingCommand() const { return (m_command.size() == 0 ? true : false); }
+15 -85
View File
@@ -47,23 +47,7 @@ void define_args()
} }
enum CommandType { bool connect(const char* host, int port, bool once)
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(); TCPClient* client = new TCPClient();
@@ -89,13 +73,19 @@ bool connect(const char* host, int port, bool once=true)
socket->send(message.c_str(), message.size()); socket->send(message.c_str(), message.size());
if (strncasecmp(message.c_str(), "QUIT", 4) != 0 && strncasecmp(message.c_str(), "STOP", 4) != 0) { if (strncasecmp(message.c_str(), "QUIT", 4) != 0 && strncasecmp(message.c_str(), "STOP", 4) != 0) {
char data[1024]; char data[1024];
size_t datalen; size_t datalen;
datalen = socket->recv(data, sizeof(data)-1); do {
data[datalen] = '\0'; memset(data, 0, sizeof(data));
datalen = socket->recv(data, sizeof(data)-1);
if (data[datalen-1] != '\n')
cout << data;
} while (data[datalen-1] != '\n');
data[datalen] = '\0';
cout << data; cout << data;
} }
else else
@@ -124,72 +114,12 @@ int main(int argc, char* argv[])
// parse arguments // parse arguments
if (A.parseArgs(argc, argv) == false) if (A.parseArgs(argc, argv) == false)
return EXIT_SUCCESS; exit(EXIT_FAILURE);
if (A.missingCommand() == true) { if (A.missingCommand() == true)
cout << "interactive mode started." << endl; connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"), false);
else
bool running = true; connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"), 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);
}
connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"));
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }