--- a/rc433/send.c Sun May 04 17:30:18 2014 +0200 +++ b/rc433/send.c Sun May 04 22:29:18 2014 +0200 @@ -24,35 +24,204 @@ #include "send.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, " send -t D -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 == 'D') { + 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 > 'D')) { + fprintf(stderr, "Type must be A, B, C or D\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 == 'D') { + 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; - int systemCode = atoi(argv[1]); - int unitCode = atoi(argv[2]); - int command = atoi(argv[3]); if (wiringPiSetup () ) return 1; - printf("sending systemCode[%d] unitCode[%i] command[%i]\n", systemCode, unitCode, command); enableTransmit(PIN); - - switch(command) { - case 1: - switchOn(systemCode, unitCode); - break; - case 0: - switchOff(systemCode, unitCode); - break; - default: - printf("command[%i] is unsupported\n", command); - return -1; + switch (cType) { + case 'A': + printf("Type A sending group[%s] device[%s] command[%d]\n", sGroup, sDevice, iState); + if (iState) + switchOn_A(sGroup, sDevice); + else + switchOff_A(sGroup, sDevice); + break; + case 'B': + printf("Type B sending group[%d] device[%i] command[%i]\n", iGroup, iDevice, iState); + if (iState) + switchOn_B(iGroup, iDevice); + else + switchOff_B(iGroup, iDevice); + break; + case 'C': + printf("Type C sending family[%c] group[%d] device[%i] command[%i]\n", cFamily, iGroup, iDevice, iState); + if (iState) + switchOn_C(cFamily, iGroup, iDevice); + else + switchOff_C(cFamily, iGroup, iDevice); + break; + case 'D': + printf("Type D sending group[%c] device[%i] command[%i]\n", cGroup, iDevice, iState); + if (iState) + switchOn_D(cGroup, iDevice); + else + switchOff_D(cGroup, iDevice); + break; } - + disableTransmit(); + return 0; }