Commit 019e62b4 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Add command_send tool for sending commands from the commandline.

Handy for testing, or scripts.
parent f4af71f1
COMMAND_TARGETS += $(LIBDIR)/libcommand.la COMMAND_TARGETS += $(LIBDIR)/libcommand.la
COMMAND_TARGETS += $(DIR)/command_list COMMAND_TARGETS += $(DIR)/command_list $(DIR)/command_send
ARCHSRCS := $(DIR)/command.c $(DIR)/command_tcp.c ARCHSRCS := $(DIR)/command.c $(DIR)/command_tcp.c
...@@ -21,6 +21,16 @@ $(DIR)/command_list: libcommand.la ...@@ -21,6 +21,16 @@ $(DIR)/command_list: libcommand.la
$(DIR)/command_list_LDFLAGS += -lcommand $(DIR)/command_list_LDFLAGS += -lcommand
$(DIR)/command_list: $(COMMAND_LIST_OBJS) $(DIR)/command_list: $(COMMAND_LIST_OBJS)
SRCS += $(ARCHSRCS) $(COMMAND_LIST_SRCS) COMMAND_SEND_SRCS := $(DIR)/command_send.c
COMMAND_SEND_OBJS := $(COMMAND_SEND_SRCS:.c=.o)
$(DIR)/command_send: libcommand.la
$(DIR)/command_send_LDFLAGS += -lcommand
$(DIR)/command_send: $(COMMAND_SEND_OBJS)
SRCS += $(ARCHSRCS) $(COMMAND_LIST_SRCS) $(COMMAND_SEND_SRCS)
TARGETS += $(COMMAND_TARGETS) TARGETS += $(COMMAND_TARGETS)
CLEAN += $(COMMAND_TARGETS) $(ARCHOBJS) $(COMMAND_LIST_OBJS) $(LIBDIR)/libcommand.a CLEAN += $(COMMAND_TARGETS) $(ARCHOBJS) \
$(COMMAND_LIST_OBJS) \
$(COMMAND_SEND_OBJS) \
$(LIBDIR)/libcommand.a
/*
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2015
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <command/command.h>
#include <dt_port_numbers.h>
static char *host = "localhost";
static int port = CONSOLE_COMMAND_PORT;
char *arg_name;
enum command_value_type vtype;
bool found = false;
static void handler_list_entry(struct command *command,
char *name, enum command_value_type type, char *unit,
int typec, enum command_ptype typev[])
{
int i;
if (strcmp(name, arg_name))
return;
vtype = type;
found = true;
printf("Command name='%s', type=%s, unit='%s'\n",
name, enum_command_value_type2str(type), unit);
for (i = 0; i < typec; i++) {
printf("\tAccepts: %s\n", enum_command_ptype2str(typev[i]));
}
}
int main(int argc, char **argv)
{
struct command *command;
char *id = "command_send";
char *type;
char *value;
enum command_ptype ptype = COMMAND_PTYPE_NULL;
if (argc < 5) {
printf("Usage: %s <host> <port> <name> <type> <value>\n", argv[0]);
printf("Example: %s localhost %d spgname SETPOINT 42", port);
return 1;
}
host = argv[1];
port = atoi(argv[2]);
arg_name = argv[3];
type = argv[4];
value = argv[5];
if (!strcmp(type, "SETPOINT")) {
ptype = COMMAND_PTYPE_SETPOINT;
} else if (!strcmp(type, "SPEED")) {
ptype = COMMAND_PTYPE_SPEED;
} else {
printf("Unsupported type\n");
return 1;
}
command = command_open(host, port);
command->handler_list_entry = handler_list_entry;
while (command_state_get(command) != COMMAND_STATE_READY &&
command_state_get(command) != COMMAND_STATE_DISCONNECTED) {
fd_set fdrx;
int high = 0;
FD_ZERO(&fdrx);
command_fd_set(command, &fdrx, &high);
select(high + 1, &fdrx, NULL, NULL, NULL);
command_handle(command, &fdrx);
}
if (command_state_get(command) == COMMAND_STATE_DISCONNECTED) {
printf("Not connected.\n");
return 1;
}
if (!found) {
printf("Could not find command server named '%s'\n", arg_name);
return 1;
}
command->type = vtype;
command_id_set(command, id);
command_name_set(command, arg_name);
struct command_entry entry;
entry.type = ptype;
switch (vtype) {
case COMMAND_VALUE_TYPE_FLOAT:
entry.value.f = atof(value);
break;
case COMMAND_VALUE_TYPE_BOOL:
entry.value.b = atoi(value);
break;
case COMMAND_VALUE_TYPE_UINT8:
entry.value.u8 = atoi(value);
break;
case COMMAND_VALUE_TYPE_UINT16:
entry.value.u16 = atoi(value);
break;
case COMMAND_VALUE_TYPE_UINT32:
entry.value.u32 = atoi(value);
break;
case COMMAND_VALUE_TYPE_SINT8:
entry.value.s8 = atoi(value);
break;
case COMMAND_VALUE_TYPE_SINT16:
entry.value.s16 = atoi(value);
break;
case COMMAND_VALUE_TYPE_SINT32:
entry.value.s32 = atoi(value);
break;
}
command_send(command, &entry);
printf("Command sent\n");
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment