コード例 #1
0
ファイル: send.c プロジェクト: sieren/433.92-Raspberry-Pi
int main(int argc, char **argv) {

	disable_file_log();
	enable_shell_log();
	set_loglevel(LOG_NOTICE);

	progname = malloc((10*sizeof(char))+1);
	progname = strdup("433-send");

	options = malloc(255*sizeof(struct options_t));

	int sockfd = 0;
    char *recvBuff = NULL;
    char *message;
	steps_t steps = WELCOME;

	/* Hold the name of the protocol */
	char protobuffer[25] = "\0";
	int i;
	/* Does this protocol exists */
	int match = 0;

	/* Do we need to print the help */
	int help = 0;
	/* Do we need to print the version */
	int version = 0;
	/* Do we need to print the protocol help */
	int protohelp = 0;

	char server[16] = "127.0.0.1";
	unsigned short port = PORT;
	
	/* Hold the final protocol struct */
	protocol_t *protocol = NULL;

	JsonNode *json = json_mkobject();
	JsonNode *code = json_mkobject();

	/* Define all CLI arguments of this program */
	addOption(&options, 'H', "help", no_value, 0, NULL);
	addOption(&options, 'V', "version", no_value, 0, NULL);
	addOption(&options, 'p', "protocol", has_value, 0, NULL);
	addOption(&options, 'S', "server", has_value, 0, "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
	addOption(&options, 'P', "port", has_value, 0, "[0-9]{1,4}");

	/* Initialize peripheral modules */
	hw_init();

	/* Get the protocol to be used */
	while (1) {
		int c;
		c = getOptions(&options, argc, argv, 0);
		if (c == -1)
			break;
		switch(c) {
			case 'p':
				if(strlen(optarg) == 0) {
					logprintf(LOG_ERR, "options '-p' and '--protocol' require an argument");
					exit(EXIT_FAILURE);
				} else {
					strcpy(protobuffer, optarg);
				}
			break;
			case 'V':
				version = 1;
			break;
			case 'H':
				help = 1;
			break;
			case 'S':
				strcpy(server, optarg);
			break;
			case 'P':
				port = (unsigned short)atoi(optarg);
			break;
			default:;
		}
	}

	/* Check if a protocol was given */
	if(strlen(protobuffer) > 0 && strcmp(protobuffer,"-v") != 0) {
		if(strlen(protobuffer) > 0 && version) {
			printf("-p and -V cannot be combined\n");
		} else {
			for(i=0; i<protocols.nr; ++i) {
				protocol = protocols.listeners[i];
				/* Check if the protocol exists */
				if(protocol_has_device(&protocol, protobuffer) == 0 && match == 0 && protocol->createCode != NULL) {
					match=1;
					/* Check if the protocol requires specific CLI arguments
					   and merge them with the main CLI arguments */
					if(protocol->options != NULL && help == 0) {
						mergeOptions(&options, &protocol->options);
					} else if(help == 1) {
						protohelp=1;
					}
					break;
				}
			}
			/* If no protocols matches the requested protocol */
			if(!match) {
				logprintf(LOG_ERR, "this protocol is not supported");
			}
		}
	}

	/* Display help or version information */
	if(version == 1) {
		printf("%s %s\n", progname, "1.0");
		return (EXIT_SUCCESS);
	} else if(help == 1 || protohelp == 1 || match == 0) {
		if(protohelp == 1 && match == 1 && protocol->printHelp != NULL)
			printf("Usage: %s -p %s [options]\n", progname, protobuffer);
		else
			printf("Usage: %s -p protocol [options]\n", progname);
		if(help == 1) {
			printf("\t -H --help\t\t\tdisplay this message\n");
			printf("\t -V --version\t\t\tdisplay version\n");
			printf("\t -S --server=%s\t\tconnect to server address\n", server);
			printf("\t -P --port=%d\t\t\tconnect to server port\n", port);
			printf("\t -p --protocol=protocol\t\tthe protocol that you want to control\n");
		}
		if(protohelp == 1 && match == 1 && protocol->printHelp != NULL) {
			printf("\n\t[%s]\n", protobuffer);
			protocol->printHelp();
		} else {
			printf("\nThe supported protocols are:\n");
			for(i=0; i<protocols.nr; ++i) {
				protocol = protocols.listeners[i];
				if(protocol->createCode != NULL) {
					while(protocol->devices != NULL) {
						printf("\t %s\t\t\t",protocol->devices->id);
						if(strlen(protocol->devices->id)<6)
							printf("\t");
						printf("%s\n", protocol->devices->desc);
						protocol->devices = protocol->devices->next;
					}
				}
			}
		}
		return (EXIT_SUCCESS);
	}

	/* Store all CLI arguments for later usage
	   and also check if the CLI arguments where
	   used correctly by the user. This will also
	   fill all necessary values in the options struct */
	while(1) {
		int c;
		c = getOptions(&options, argc, argv, 1);

		if(c == -1)
			break;
	}

	int itmp;
	/* Check if we got sufficient arguments from this protocol */
	while(options != NULL && strlen(options->name) > 0) {
		/* Only send the CLI arguments that belong to this protocol, the protocol name
		   and those that are called by the user */
		if((getOptionIdByName(&protocol->options, options->name, &itmp) == 0 || strcmp(options->name, "protocol") == 0)
		   && strlen(options->value) > 0) {
			json_append_member(code, options->name, json_mkstring(options->value));
		}
		options = options->next;
	}

	if(protocol->createCode(code) == 0) {
		if((sockfd = connect_to_server(strdup(server), port)) == -1) {
			logprintf(LOG_ERR, "could not connect to 433-daemon");
			goto close;
		}

		while(1) {
			/* Clear the receive buffer again and read the welcome message */
			if((recvBuff = socket_read(sockfd)) != NULL) {
				json = json_decode(recvBuff);
				json_find_string(json, "message", &message);
			} else {
				goto close;
			}
			usleep(100);
			switch(steps) {
				case WELCOME:
					if(strcmp(message, "accept connection") == 0) {
						socket_write(sockfd, "{\"message\":\"client sender\"}");
						steps=IDENTIFY;
					}
				case IDENTIFY:
					if(strcmp(message, "accept client") == 0) {
						steps=SEND;
					}
					if(strcmp(message, "reject client") == 0) {
						steps=REJECT;
					}
				case SEND:
					json_delete(json);
					json = json_mkobject();
					json_append_member(json, "message", json_mkstring("send"));
					json_append_member(json, "code", code);
					socket_write(sockfd, json_stringify(json, NULL));
					goto close;
				break;
				case REJECT:
				default:
					goto close;
				break;
			}
		}
	}
close:
	json_delete(json);
	socket_close(sockfd);
return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: debug.c プロジェクト: sieren/433.92-Raspberry-Pi
int main(int argc, char **argv) {

    enable_shell_log();
    disable_file_log();
    set_loglevel(LOG_NOTICE);

    progname = malloc((10*sizeof(char))+1);
    progname = strdup("433-debug");

#ifdef USE_LIRC
    lirc_t data;
    char *socket = strdup("/dev/lirc0");
    int have_device = 0;
#else
    int newDuration;
#endif

    int duration = 0;
    int i = 0;
    int y = 0;

    int recording = 1;
    int bit = 0;
    int raw[255];
    int pRaw[255];
    int code[255];
    int binary[255];
    int footer = 0;
    int header = 0;
    int pulse = 0;
    int rawLength = 0;
    int binaryLength = 0;

    int loop = 1;

#ifdef USE_LIRC
    hw_choose_driver(NULL);
#endif

    addOption(&options, 'H', "help", no_value, 0, NULL);
    addOption(&options, 'V', "version", no_value, 0, NULL);
#ifdef USE_LIRC
    addOption(&options, 'S', "socket", has_value, 0, "^/dev/([A-Za-z]+)([0-9]+)");
#endif

    while (1) {
        int c;
        c = getOptions(&options, argc, argv, 1);
        if(c == -1)
            break;
        switch (c) {
        case 'h':
            printf("Usage: %s [options]\n", progname);
            printf("\t -H --help\t\tdisplay usage summary\n");
            printf("\t -V --version\t\tdisplay version\n");
#ifdef USE_LIRC
            printf("\t -S --socket=socket\tread from given socket\n");
#endif
            return (EXIT_SUCCESS);
            break;
        case 'v':
            printf("%s %s\n", progname, "1.0");
            return (EXIT_SUCCESS);
            break;
#ifdef USE_LIRC
        case 'S':
            socket = optarg;
            have_device = 1;
            break;
#endif
        default:
            printf("Usage: %s [options]\n", progname);
            return (EXIT_FAILURE);
            break;
        }
    }

#ifdef USE_LIRC
    if(strcmp(socket, "/var/lirc/lircd") == 0) {
        logprintf(LOG_ERR, "refusing to connect to lircd socket");
        return EXIT_FAILURE;
    }

    if(have_device)
        hw.device = socket;

    if(!hw.init_func()) {
        return EXIT_FAILURE;
    }
#endif

    /*
    End of the original (but stripped) code of mode2
    */

#ifndef USE_LIRC
    if(gpio_request(GPIO_IN_PIN) == 0) {
        /* Attach an interrupt to the requested pin */
        irq_attach(GPIO_IN_PIN, CHANGE);
    } else {
        return EXIT_FAILURE;
    }
#endif

    while(loop) {
#ifdef USE_LIRC
        data = hw.readdata(0);
        duration = (data & PULSE_MASK);
#else

        unsigned short a = 0;

        if((newDuration = irq_read()) == 1) {
            continue;
        }

        for(a=0; a<2; a++) {
            if(a==0) {
                duration = PULSE_LENGTH;
            } else {
                duration = newDuration;
                if(duration < 750) {
                    duration = PULSE_LENGTH;
                }
            }
#endif

        /* If we are recording, keep recording until the next footer has been matched */
        if(recording == 1) {
            if(bit < 255) {
                raw[bit++] = duration;
            } else {
                bit = 0;
                recording = 0;
            }
        }

        /* First try to catch code that seems to be a footer.
           If a real footer has been recognized, start using that as the new footer */
        if((duration > 5000
                && duration < 100000 && footer == 0) || ((footer-(footer*0.1)<duration) && (footer+(footer*0.1)>duration))) {
            recording = 1;

            /* Check if we are recording similar codes */
            for(i=0; i<(bit-1); i++) {
                if(!(((pRaw[i]-(pRaw[i]*0.3)) < raw[i]) && ((pRaw[i]+(pRaw[i]*0.3)) > raw[i]))) {
                    y=0;
                    recording=0;
                }
                pRaw[i]=raw[i];
            }
            y++;

            /* Continue if we have 2 matches */
            if(y>2) {
                /* If we are certain we are recording similar codes.
                   Save the header values and the raw code length */
                if(footer>0) {
                    if(header == 0) {
                        header=raw[1];
                    }
                    if(rawLength == 0)
                        rawLength=bit;
                }
                /* Try to catch the footer, and the low and high values */
                for(i=0; i<bit; i++) {
                    if((i+1)<bit && i > 2 && footer > 0) {
                        if((raw[i]/PULSE_LENGTH) >= 2) {
                            pulse=raw[i];
                        }
                    }
                    if(duration > 5000 && duration < 100000) {
                        footer=raw[i];
                    }
                }

                /* If we have gathered all data, stop with the loop */
                if(header > 0 && footer > 0 && pulse > 0 && rawLength > 0) {
                    loop = 0;
                }
            }
            bit=0;
        }

        fflush(stdout);
#ifndef USE_LIRC
    }
#endif
};

/* Convert the raw code into binary code */
for(i=0; i<rawLength; i++) {
    if((unsigned int)raw[i] > (pulse-PULSE_LENGTH)) {
        code[i]=1;
    } else {
        code[i]=0;
    }
}
for(i=2; i<rawLength; i+=4) {
    if(code[i+1] == 1) {
        binary[i/4]=1;
    } else {
        binary[i/4]=0;
    }
}

binaryLength = (int)((float)i/4);

/* Print everything */
printf("--[RESULTS]--\n");
printf("\n");
printf("header:\t\t%d\n",normalize(header));
printf("pulse:\t\t%d\n",normalize(pulse));
printf("footer:\t\t%d\n",normalize(footer));
printf("rawLength:\t%d\n",rawLength);
printf("binaryLength:\t%d\n",binaryLength);
printf("\n");
printf("Raw code:\n");
for(i=0; i<rawLength; i++) {
    printf("%d ",normalize(raw[i])*PULSE_LENGTH);
}
printf("\n");
printf("Binary code:\n");
for(i=0; i<binaryLength; i++) {
    printf("%d",binary[i]);
}
printf("\n");
return (EXIT_SUCCESS);
}
コード例 #3
0
ファイル: control.c プロジェクト: pilight/windows-cli
int main(int argc, char **argv) {

	disable_file_log();
	enable_shell_log();
	set_loglevel(LOG_NOTICE);

	progname = malloc((10*sizeof(char))+1);
	progname = strdup("433-control");

	options = malloc(255*sizeof(struct options_t));

	int sockfd = 0;
    char *recvBuff = NULL;
	char *message;
	steps_t steps = WELCOME;

	char device[50];
	char location[50];
	char state[10] = {'\0'};
	struct conf_locations_t *slocation = NULL;
	struct conf_devices_t *sdevice = NULL;
	
	char server[16] = "127.0.0.1";
	unsigned short port = PORT;
	
	JsonNode *json = json_mkobject();
	JsonNode *config = json_mkobject();
	JsonNode *code = json_mkobject();

	/* Define all CLI arguments of this program */
	addOption(&options, 'h', "help", no_value, 0, NULL);
	addOption(&options, 'v', "version", no_value, 0, NULL);
	addOption(&options, 'l', "location", has_value, 0, NULL);
	addOption(&options, 'd', "device", has_value, 0,  NULL);
	addOption(&options, 's', "state", has_value, 0,  NULL);
	addOption(&options, 'S', "server", has_value, 0, "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
	addOption(&options, 'P', "port", has_value, 0, "[0-9]{1,4}");

	/* Store all CLI arguments for later usage
	   and also check if the CLI arguments where
	   used correctly by the user. This will also
	   fill all necessary values in the options struct */
	while(1) {
		int c;
		c = getOptions(&options, argc, argv, 1);
		if(c == -1)
			break;
		switch(c) {
			case 'h':
				printf("\t -h --help\t\t\tdisplay this message\n");
				printf("\t -v --version\t\t\tdisplay version\n");
				printf("\t -S --server=%s\t\tconnect to server address\n", server);
				printf("\t -P --port=%d\t\t\tconnect to server port\n", port);
				printf("\t -l --location=location\t\tthe location in which the device resides\n");
				printf("\t -d --device=device\t\tthe device that you want to control\n");
				printf("\t -s --state=state\t\tthe new state of the device\n");
				exit(EXIT_SUCCESS);
			break;
			case 'v':
				printf("%s %s\n", progname, "1.0");
				exit(EXIT_SUCCESS);
			break;
			case 'l':
				strcpy(location, optarg);
			break;
			case 'd':
				strcpy(device, optarg);
			break;
			case 's':
				strcpy(state, optarg);
			break;
			case 'S':
				strcpy(server, optarg);
			break;
			case 'P':
				port = (unsigned short)atoi(optarg);
			break;			
			default:
				printf("Usage: %s -l location -d device\n", progname);
				exit(EXIT_SUCCESS);
			break;
		}
	}

	if(strlen(location) == 0 || strlen(device) == 0) {
		printf("Usage: %s -l location -d device\n", progname);
		exit(EXIT_SUCCESS);
	}

	if((sockfd = connect_to_server(strdup(server), port)) == -1) {
		logprintf(LOG_ERR, "could not connect to 433-daemon");
		exit(EXIT_FAILURE);
	}

	/* Initialize peripheral modules */
	hw_init();

	while(1) {
		if(steps > WELCOME) {
			/* Clear the receive buffer again and read the welcome message */
			if(steps == REQUEST) {
				if((recvBuff = socket_read_big(sockfd)) != NULL) {
					json = json_decode(recvBuff);
					json_find_string(json, "message", &message);
				} else {
					goto close;
				}
			} else {
				if((recvBuff = socket_read(sockfd)) != NULL) {
					json = json_decode(recvBuff);
					json_find_string(json, "message", &message);
				} else {
					goto close;
				}
			}
			usleep(250);
		}
		switch(steps) {
			case WELCOME:
				socket_write(sockfd, "{\"message\":\"client controller\"}");
				steps=IDENTIFY;
			break;
			case IDENTIFY:
				if(strcmp(message, "accept client") == 0) {
					steps=REQUEST;
				}
				if(strcmp(message, "reject client") == 0) {
					steps=REJECT;
				}
			case REQUEST:
				socket_write(sockfd, "{\"message\":\"request config\"}");
				steps=CONFIG;
			break;
			case CONFIG:
				if((config = json_find_member(json, "config")) != NULL) {
					config_parse(config);
					if(config_get_location(location, &slocation) == 0) {
						if(config_get_device(location, device, &sdevice) == 0) {
							json_delete(json);

							json = json_mkobject();
							code = json_mkobject();

							json_append_member(code, "location", json_mkstring(location));
							json_append_member(code, "device", json_mkstring(device));

							if(strlen(state) > 0) {
								if(config_valid_state(location, device, state) == 0) {
									json_append_member(code, "state", json_mkstring(state));
								} else {
									logprintf(LOG_ERR, "\"%s\" is an invalid state for device \"%s\"", state, device);
									goto close;
								}
							}

							json_append_member(json, "message", json_mkstring("send"));
							json_append_member(json, "code", code);

							socket_write(sockfd, json_stringify(json, NULL));

						} else {
							logprintf(LOG_ERR, "the device \"%s\" does not exist", device);
							goto close;
						}
					} else {
						logprintf(LOG_ERR, "the location \"%s\" does not exist", location);
						goto close;
					}
				}
				goto close;
			break;
			case REJECT:
			default:
				goto close;
			break;
		}
	}
close:
	json_delete(json);
	socket_close(sockfd);
return EXIT_SUCCESS;
}