rc433/send.c

Mon, 16 Feb 2015 20:41:09 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 16 Feb 2015 20:41:09 +0100
changeset 313
8448fcf3d799
parent 58
e8e7b46b705b
permissions
-rw-r--r--

Code cleanup

/*****************************************************************************
 * Copyright (C) 2014
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the mbsePi-apps
 *
 * This 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 2, or (at your option) any
 * later version.
 *
 * mbsePi-apps 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 EC-65K; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "rc433.h"

#ifdef HAVE_WIRINGPI_H

void help(void)
{
    fprintf(stdout, "mbsePi-apps send v%s\n\n", VERSION);
    fprintf(stdout, "Usage: send [-t type ] [-f family ] [-g group ] [-d device] [-s state ] [-h]\n");
    fprintf(stdout, "       send -t A -g 00001 -d 00001 -s 1\n");
    fprintf(stdout, "       send -t B -g 2 -d 2 -s 0\n");
    fprintf(stdout, "       send -t C -f c -g 3 -d 3 -s 1\n");
    fprintf(stdout, "\n");
    fprintf(stdout, "       send -t E -g A -d 2 -s 0\n");
}



int isAcode(char *s)
{
    int	i;

    if (strlen(s) != 5)
	return FALSE;
    for (i = 0; i < 5; i++) {
	if ((s[i] < '0') || (s[i] > '1'))
	    return FALSE;
    }

    return TRUE;
}



/*
 * A - char group, char device (2 x 5 binary)
 * B - int system, int unit
 * C - char family, int group, int device
 * D - char group, int device
 */

int main(int argc, char *argv[]) {
    char	buf[127], *endptr;
    int		c;
    char	cType = '0', *sGroup = NULL, *sDevice = NULL, cFamily = '0', cGroup = '0';
    int		iState = -1, iGroup = -1, iDevice = -1;

    while (1) {
	int	option_index = 0;
	static struct option long_options[] = {
	    {"device", 1, 0, 'd'},
	    {"family", 1, 0, 'f'},
	    {"group", 1, 0, 'g'},
	    {"help", 0, 0, 'h'},
	    {"state", 1, 0, 's'},
	    {"type", 1, 0, 't'},
	    {0, 0, 0, 0}
	};

	c = getopt_long(argc, argv, "d:f:g:hs:t:", long_options, &option_index);
	if (c == -1)
	    break;

	switch (c) {
	    case 'd':	snprintf(buf, 127, "%s", optarg);
			if (cType == 'A') {
			    sDevice = xstrcpy(buf);
			} else {
			    iDevice = strtol(buf, &endptr, 10);
			}
			break;
	    case 'f':	snprintf(buf, 127, "%s", optarg);
			if (cType == 'C') {
			    cFamily = tolower(buf[0]);
			}
			break;
	    case 'g':	snprintf(buf, 127, "%s", optarg);
			if (cType == 'A') {
			    sGroup = xstrcpy(buf);
			} else if (cType == 'E') {
			    cGroup = toupper(buf[0]);
			} else {
			    iGroup = strtol(buf, &endptr, 10);
			}
			break;
	    case 'h':   help();
			return 1;
	    case 's':	snprintf(buf, 127, "%s", optarg);
			iState = strtol(buf, &endptr, 10);
			break;
	    case 't':	snprintf(buf, 127, "%s", optarg);
			cType = toupper(buf[0]);
			break;
	}
    }

    /*
     * Check all option and combinations
     */
    if ((cType < 'A') || (cType > 'E')) {
	fprintf(stderr, "Type must be A, B, C or E\n");
	return 1;
    }
    if ((iState < 0) || (iState > 1)) {
	fprintf(stderr, "State must be 0 or 1\n");
	return 1;
    }
    if (cType == 'A') {
	if (sGroup == NULL) {
	    fprintf(stderr, "Group not set\n");
	    return 1;
	}
	if (sDevice == NULL) {
	    fprintf(stderr, "Device not set\n");
	    return 1;
	}
	if (isAcode(sGroup) == FALSE) {
	    fprintf(stderr, "Group must be 5 binary digits like 10000\n");
	    return 1;
	}
	if (isAcode(sDevice) == FALSE) {
	    fprintf(stderr, "Device must be 5 binary digits like 10000\n");
	    return 1;
	}
    } else {
	if (iDevice == -1) {
	    fprintf(stderr, "Device not set\n");
	    return 1;
	}
    }
    if ((cType == 'B') || (cType == 'C')) {
	if (iGroup == -1) {
	    fprintf(stderr, "Group not set\n");
	    return 1;
	}
	if ((iGroup < 1) || (iGroup > 4)) {
	    fprintf(stderr, "Group must be 1..4\n");
	    return 1;
	}
	if ((iDevice < 1) || (iDevice > 4)) {
	    fprintf(stderr, "Device must be 1..4\n");
	    return 1;
	}
    }
    if (cType == 'E') {
	if ((iDevice < 1) || (iDevice > 3)) {
	    fprintf(stderr, "Device must be 1..3\n");
    	    return 1;
	}
	if ((cGroup < 'A') || (cGroup > 'D')) {
	    fprintf(stderr, "Group must be A..D\n");
	    return 1;
	}
    }
    if (cType == 'C') {
	if ((cFamily < 'a') || (cFamily > 'f')) {
	    fprintf(stderr, "Family must be a..f\n");
	    return 1;
	}
    }

    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
    int PIN = 0;
    
    if (wiringPiSetup () )
	return 1;

    enableTransmit(PIN);
    switch (cType) {
	case 'A':
		    printf("Type A sending group[%s] device[%s] command[%d]\n", sGroup, sDevice, iState);
		    toggleTypeA(sGroup, sDevice, iState);
		    break;
	case 'B':
		    printf("Type B sending group[%d] device[%i] command[%i]\n", iGroup, iDevice, iState);
		    toggleTypeB(iGroup, iDevice, iState);
		    break;
	case 'C':
		    printf("Type C sending family[%c] group[%d] device[%i] command[%i]\n", cFamily, iGroup, iDevice, iState);
		    toggleTypeC(cFamily, iGroup, iDevice, iState);
		    break;
	case 'E':
		    printf("Type E sending group[%c] device[%i] command[%i]\n", cGroup, iDevice, iState);
		    toggleTypeE(cGroup, iDevice, iState);
		    break;
    }
    disableTransmit();

    return 0;
}

#else

int main(int argc, char *argv[]) {
    fprintf(stderr, "This program does nothing without the wiringPi library\n");
    return 0;
}

#endif

mercurial