Skip to content
Snippets Groups Projects
Select Git revision
  • 749d3ff1333e06dc481a5f84667b5d081a2b8c2d
  • main default
  • tammojan
  • slow_mode
  • debian-service
  • jeroen
  • dpkg
  • bless_test
  • improve_sattracker
  • update_dockerfile
  • yellowoffset
  • decplus
  • focusboxmouseout
  • trace2port_startup
  • 2016-04-02-trace2port
  • scan
  • azerror
  • beaglebone
  • build
  • corso2014
  • sercos
  • console_2020-06-28
  • controller_2020-06-28
  • 2018-01-17
  • 2016-04-02-with-trace2port
  • 2016-04-02
  • DT_update
  • 2013-06-21
28 results

command_send.c

Blame
  • command_send.c 3.70 KiB
    /*
    	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;
    	bool has_value = true;
    	
    	if (argc < 5) {
    		printf("Usage: %s <host> <port> <name> <type> <value>\n", argv[0]);
    		printf("Example: %s localhost %d spgname SETPOINT 42\n",
    		    argv[0], port);
    		return 1;
    	}
    	
    	host = argv[1];
    	port = atoi(argv[2]);
    	arg_name = argv[3];
    	type = argv[4];
    
    	if (!strcmp(type, "SETPOINT")) {
    		ptype = COMMAND_PTYPE_SETPOINT;
    	} else if (!strcmp(type, "SPEED")) {
    		ptype = COMMAND_PTYPE_SPEED;
    	} else if (!strcmp(type, "SETPOINT_TRACK")) {
    		ptype = COMMAND_PTYPE_SETPOINT_TRACK;
    		has_value = false;
    	} else {
    		printf("Unsupported type\n");
    		return 1;
    	}
    	
    	if (has_value) {
    		value = argv[5];
    	}
    
    	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;
    	
    	if (has_value) {
    		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;
    }