Пример #1
0
int main(int argc, char **argv) {

	gc_attach(main_gc);

	/* Catch all exit signals for gc */
	gc_catch();

	log_shell_enable();
	log_file_disable();
	log_level_set(LOG_NOTICE);

	struct options_t *options = NULL;
	
	char *args = NULL;	
	
	progname = malloc(13);
	strcpy(progname, "pilight-uuid");	
	
	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);

	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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");		
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, VERSION);
				return (EXIT_SUCCESS);
			break;	
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	options_delete(options);

	printf("%s\n", ssdp_genuuid());

	main_gc();
	return (EXIT_FAILURE);
}
Пример #2
0
int main(int argc, char **argv) {
	// memtrack();

	gc_attach(main_gc);

	/* Catch all exit signals for gc */
	gc_catch();

	log_shell_enable();
	log_file_disable();
	log_level_set(LOG_NOTICE);

	struct options_t *options = NULL;
	struct ifaddrs *ifaddr, *ifa;
	int family = 0;
	char *p = NULL;
	char *args = NULL;

	progname = MALLOC(13);
	if(!progname) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-uuid");

	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);

	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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");
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, PILIGHT_VERSION);
				return (EXIT_SUCCESS);
			break;
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	options_delete(options);

#ifdef __FreeBSD__
	if(rep_getifaddrs(&ifaddr) == -1) {
		logprintf(LOG_ERR, "could not get network adapter information");
		goto clear;
	}
#else
	if(getifaddrs(&ifaddr) == -1) {
		perror("getifaddrs");
		goto clear;
	}
#endif

	for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
		if(ifa->ifa_addr == NULL) {
			continue;
		}

		family = ifa->ifa_addr->sa_family;

		if((strstr(ifa->ifa_name, "lo") == NULL && strstr(ifa->ifa_name, "vbox") == NULL
		    && strstr(ifa->ifa_name, "dummy") == NULL) && (family == AF_INET || family == AF_INET6)) {
			if((p = genuuid(ifa->ifa_name)) == NULL) {
				logprintf(LOG_ERR, "could not generate the device uuid");
				freeifaddrs(ifaddr);
				goto clear;
			} else {
				printf("%s\n", p);
				FREE(p);
				break;
			}
		}
	}

	freeifaddrs(ifaddr);

clear:
	main_gc();
	return (EXIT_FAILURE);
}
Пример #3
0
int main(int argc, char **argv) {
	// memtrack();

	atomicinit();
	gc_attach(main_gc);

	/* Catch all exit signals for gc */
	gc_catch();

	log_shell_enable();
	log_file_disable();

	log_level_set(LOG_NOTICE);

	if((progname = MALLOC(16)) == NULL) {
		fprintf(stderr, "out of memory\n");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-receive");
	struct options_t *options = NULL;
	struct ssdp_list_t *ssdp_list = NULL;

	char *server = NULL;
	unsigned short port = 0;
	unsigned short stats = 0;

	char *args = NULL;

	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'S', "server", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "^(([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])$");
	options_add(&options, 'P', "port", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "[0-9]{1,4}");
	options_add(&options, 's', "stats", OPTION_NO_VALUE, 0, JSON_NULL, NULL, "[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 = options_parse(&options, argc, argv, 1, &args);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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=x.x.x.x\t\tconnect to server address\n");
				printf("\t -P --port=xxxx\t\t\tconnect to server port\n");
				printf("\t -s --stats\t\t\tshow CPU and RAM statistics\n");
				exit(EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s v%s\n", progname, PILIGHT_VERSION);
				exit(EXIT_SUCCESS);
			break;
			case 'S':
				if((server = MALLOC(strlen(args)+1)) == NULL) {
					fprintf(stderr, "out of memory\n");
					exit(EXIT_FAILURE);
				}
				strcpy(server, args);
			break;
			case 'P':
				port = (unsigned short)atoi(args);
			break;
			case 's':
				stats = 1;
			break;
			default:
				printf("Usage: %s -l location -d device\n", progname);
				exit(EXIT_SUCCESS);
			break;
		}
	}
	options_delete(options);

	if(server != NULL && port > 0) {
		if((sockfd = socket_connect(server, port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			return EXIT_FAILURE;
		}
	} else if(ssdp_seek(&ssdp_list) == -1) {
		logprintf(LOG_NOTICE, "no pilight ssdp connections found");
		goto close;
	} else {
		if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			goto close;
		}
	}
	if(ssdp_list != NULL) {
		ssdp_free(ssdp_list);
	}
	if(server != NULL) {
		FREE(server);
	}

	struct JsonNode *jclient = json_mkobject();
	struct JsonNode *joptions = json_mkobject();
	json_append_member(jclient, "action", json_mkstring("identify"));
	json_append_member(joptions, "receiver", json_mknumber(1, 0));
	json_append_member(joptions, "stats", json_mknumber(stats, 0));
	json_append_member(jclient, "options", joptions);
	char *out = json_stringify(jclient, NULL);
	socket_write(sockfd, out);
	json_free(out);
	json_delete(jclient);

	if(socket_read(sockfd, &recvBuff, 0) != 0 ||
     strcmp(recvBuff, "{\"status\":\"success\"}") != 0) {
		goto close;
	}

	while(main_loop) {
		if(socket_read(sockfd, &recvBuff, 0) != 0) {
			goto close;
		}
		char **array = NULL;
		unsigned int n = explode(recvBuff, "\n", &array), i = 0;
		for(i=0;i<n;i++) {
			struct JsonNode *jcontent = json_decode(array[i]);
			struct JsonNode *jtype = json_find_member(jcontent, "type");
			if(jtype != NULL) {
				json_remove_from_parent(jtype);
				json_delete(jtype);
			}
			char *content = json_stringify(jcontent, "\t");
			printf("%s\n", content);
			json_delete(jcontent);
			json_free(content);
		}
		array_free(&array, n);
	}

close:
	if(sockfd > 0) {
		socket_close(sockfd);
	}
	if(recvBuff != NULL) {
		FREE(recvBuff);
		recvBuff = NULL;
	}
	options_gc();
	log_shell_disable();
	log_gc();
	FREE(progname);
	return EXIT_SUCCESS;
}
Пример #4
0
int main(int argc, char **argv) {

	log_shell_enable();
	log_file_disable();

	log_level_set(LOG_NOTICE);

	progname = malloc(16);
	if(!progname) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-receive");
	struct options_t *options = NULL;
	struct ssdp_list_t *ssdp_list = NULL;

	JsonNode *json = NULL;

	char *server = NULL;
	unsigned short port = 0;

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

	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'S', "server", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "^(([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])$");
	options_add(&options, 'P', "port", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "[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 = options_parse(&options, argc, argv, 1, &args);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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=x.x.x.x\t\tconnect to server address\n");
				printf("\t -P --port=xxxx\t\t\tconnect to server port\n");
				exit(EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, VERSION);
				exit(EXIT_SUCCESS);
			break;
			case 'S':
				server = realloc(server, strlen(args)+1);
				memset(server, '\0', strlen(args)+1);
				if(!server) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				strcpy(server, args);
			break;
			case 'P':
				port = (unsigned short)atoi(args);
			break;
			default:
				printf("Usage: %s -l location -d device\n", progname);
				exit(EXIT_SUCCESS);
			break;
		}
	}
	options_delete(options);

	if(server && port > 0) {
		if((sockfd = socket_connect(server, port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			return EXIT_FAILURE;
		}
	} else if(ssdp_seek(&ssdp_list) == -1) {
		logprintf(LOG_ERR, "no pilight ssdp connections found");
		goto close;
	} else {
		if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			goto close;
		}
	}
	if(ssdp_list) {
		ssdp_free(ssdp_list);
	}
	if(server) {
		sfree((void *)&server);
	}

	while(1) {
		if(steps > WELCOME) {
			if((recvBuff = socket_read(sockfd)) == NULL) {
				goto close;
			}
		}
		switch(steps) {
			case WELCOME:
				socket_write(sockfd, "{\"message\":\"client receiver\"}");
				steps=IDENTIFY;
			break;
			case IDENTIFY:
				//extract the message
				json = json_decode(recvBuff);
				json_find_string(json, "message", &message);
				if(strcmp(message, "accept client") == 0) {
					steps=RECEIVE;
				} else if(strcmp(message, "reject client") == 0) {
					steps=REJECT;
				}
				//cleanup
				json_delete(json);
				sfree((void *)&recvBuff);
				json = NULL;
				message = NULL;
				recvBuff = NULL;
			break;
			case RECEIVE: {
				char *line = strtok(recvBuff, "\n");
				//for each line
				while(line) {
					json = json_decode(line);
					char *output = json_stringify(json, "\t");
					printf("%s\n", output);
					sfree((void *)&output);
					json_delete(json);
					line = strtok(NULL,"\n");
				}
				sfree((void *)&recvBuff);
				sfree((void *)&line);
				recvBuff = NULL;
			} break;
			case REJECT:
			default:
				goto close;
			break;
		}
	}
close:
	if(sockfd > 0) {
		socket_close(sockfd);
	}
	if(recvBuff) {
		sfree((void *)&recvBuff);
	}
	options_gc();
	log_shell_disable();
	log_gc();
	sfree((void *)&progname);
	return EXIT_SUCCESS;
}
Пример #5
0
int main(int argc, char **argv) {

	gc_attach(main_gc);

	/* Catch all exit signals for gc */
	gc_catch();	
	
	log_shell_enable();
	log_level_set(LOG_NOTICE);

	struct options_t *options = NULL;
	struct hardware_t *hardware = NULL;	
	
	char *args = NULL;
	char *stmp = NULL;

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

	steps_t state = CAPTURE;
	steps_t pState = WAIT;

	int recording = 1;
	int bit = 0;
	int raw[255] = {0};
	int pRaw[255] = {0};
	int code[255] = {0};
	int onCode[255] = {0};
	int offCode[255] = {0};
	int allCode[255] = {0};
	int unit1Code[255] = {0};
	int unit2Code[255] = {0};
	int unit3Code[255] = {0};
	int binary[255] = {0};
	int pBinary[255] = {0};
	int onBinary[255] = {0};
	int offBinary[255] = {0};
	int allBinary[255] = {0};
	int unit1Binary[255] = {0};
	int unit2Binary[255] = {0};
	int unit3Binary[255] = {0};
	int loop = 1;
	unsigned short match = 0;
	
	int temp[75] = {-1};
	int footer = 0;
	int pulse = 0;
	int onoff[75] = {-1};
	int all[75] = {-1};
	int unit[75] = {-1};
	int rawLength = 0;
	int binaryLength = 0;

	memset(onoff, -1, 75);
	memset(unit, -1, 75);
	memset(all, -1, 75);
	memset(temp, -1, 75);
	
	settingsfile = malloc(strlen(SETTINGS_FILE)+1);
	strcpy(settingsfile, SETTINGS_FILE);	
	
	progname = malloc(16);
	strcpy(progname, "pilight-learn");

	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);
	options_add(&options, 'S', "settings", has_value, 0, NULL);

	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		if(c == -1 || c == -2)
			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");		
				printf("\t -S --settings\t\tsettings file\n");
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, VERSION);
				return (EXIT_SUCCESS);
			break;	
			case 'S': 
				if(access(args, F_OK) != -1) {
					settingsfile = realloc(settingsfile, strlen(args)+1);
					strcpy(settingsfile, args);
					settings_set_file(args);
				} else {
					fprintf(stderr, "%s: the settings file %s does not exists\n", progname, args);
					return EXIT_FAILURE;
				}
			break;
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	options_delete(options);

	if(access(settingsfile, F_OK) != -1) {
		if(settings_read() != 0) {
			return EXIT_FAILURE;
		}
	}
	
	if(settings_find_string("hw-mode", &stmp) == 0) {
		hw_mode = stmp;
	}	
	
	hardware_init();	
	
	struct hardwares_t *htmp = hardwares;
	match = 0;
	while(htmp) {
		if(strcmp(htmp->listener->id, hw_mode) == 0) {
			hardware = htmp->listener;
			match = 1;
			break;
		}
		htmp = htmp->next;
	}
	if(match == 1 && hardware->init) {
		hardware->init();
	
		printf("Please make sure the daemon is not running when using this debugger.\n\n");	
	}

	if(match == 0 || !hardware->receive || strcmp(hw_mode, "none") == 0) {
		printf("The hw-mode \"%s\" isn't compatible with %s\n", hw_mode, progname);
		main_gc();
		return EXIT_SUCCESS;
	}

	while(loop && match == 1 && hardware->receive) {
		switch(state) {
			case CAPTURE:
				printf("1. Please send and hold one of the OFF buttons.");
				break;
			case ON:
				/* Store the previous OFF button code */
				for(i=0;i<binaryLength;i++) {
					offBinary[i] = binary[i];
				}
				printf("2. Please send and hold the ON button for the same device\n");
				printf("   as for which you send the previous OFF button.");
				break;
			case OFF:
				/* Store the previous ON button code */
				for(i=0;i<binaryLength;i++) {
					onBinary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					onCode[i] = code[i];
				}
				
				z=0;
				/* Compare the ON and OFF codes and save bit that are different */
				for(i=0;i<binaryLength;i++) {
					if(offBinary[i] != onBinary[i]) {
						onoff[z++]=i;
					}
				}
				for(i=0;i<rawLength;i++) {
					offCode[i] = code[i];
				}
				printf("3. Please send and hold (one of the) ALL buttons.\n");
				printf("   If you're remote doesn't support turning ON or OFF\n");
				printf("   all devices at once, press the same OFF button as in\n");
				printf("   the beginning.");
			break;
			case ALL:
				z=0;
				memset(temp,-1,75);
				/* Store the ALL code */
				for(i=0;i<binaryLength;i++) {
					allBinary[i] = binary[i];
					if(allBinary[i] != onBinary[i]) {
						temp[z++] = i;
					}
				}
				for(i=0;i<rawLength;i++) {
					allCode[i] = code[i];
				}
				/* Compare the ALL code to the ON and OFF code and store the differences */
				y=0;
				for(i=0;i<binaryLength;i++) {
					if(allBinary[i] != offBinary[i]) {
						all[y++] = i;
					}
				}
				if((unsigned int)z < y) {
					for(i=0;i<z;i++) {
						all[z]=temp[z];
					}
				}
				printf("4. Please send and hold the ON button with the lowest ID.");
			break;
			case UNIT1:
				/* Store the lowest unit code */
				for(i=0;i<binaryLength;i++) {
					unit1Binary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					unit1Code[i] = code[i];
				}
				printf("5. Please send and hold the ON button with the second to lowest ID.");
			break;
			case UNIT2:
				/* Store the second to lowest unit code */
				for(i=0;i<binaryLength;i++) {
					unit2Binary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					unit2Code[i] = code[i];
				}
				printf("6. Please send and hold the ON button with the highest ID.");
			break;
			case PROCESSUNIT:
				z=0;
				/* Store the highest unit code and compare the three codes. Store all
				   bit that are different */
				for(i=0;i<binaryLength;i++) {
					unit3Binary[i] = binary[i];
					if((unit2Binary[i] != unit1Binary[i]) || (unit1Binary[i] != unit3Binary[i]) || (unit2Binary[i] != unit3Binary[i])) {
						unit[z++]=i;
					}
				}
				for(i=0;i<rawLength;i++) {
					unit3Code[i] = code[i];
				}
				state=STOP;
			break;
			case WAIT:
			case STOP:
			default:;
		}
		fflush(stdout);
		if(state!=WAIT)
			pState=state;	
		if(state==STOP)
			loop = 0;
		else
			state=WAIT;	

		duration = hardware->receive();

		/* 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 > 4440 && footer == 0) || ((footer-(footer*0.1)<duration) && (footer+(footer*0.1)>duration))) {
			recording = 1;
			pulse_length = duration/PULSE_DIV;

			/* 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;
					z=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 raw code length */
				if(footer>0) {
					if(rawLength == 0)
						rawLength=bit;
				}
				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(footer > 0 && pulse > 0 && rawLength > 0) {
						/* 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;
							}
						}
						if(binaryLength == 0)
							binaryLength = (int)((float)i/4);

						/* Check if the subsequent binary code matches
						   to check if the same button was still held */
						if(binaryLength == (i/4)) {
							for(i=0;i<binaryLength;i++) {
								if(pBinary[i] != binary[i]) {
									z=1;
								}
							}

							/* If we are capturing a different button
							   continue to the next step */
							if(z==1 || state == CAPTURE) {
								switch(pState) {
									case CAPTURE:
										state=ON;
									break;
									case ON:
										state=OFF;
									break;
									case OFF:
										state=ALL;
									break;
									case ALL:
										state=UNIT1;
									break;
									case UNIT1:
										state=UNIT2;
									break;
									case UNIT2:
										state=PROCESSUNIT;
									break;
									case PROCESSUNIT:
										state=STOP;
									break;
									case WAIT:
									case STOP:
									default:;
								}
							printf(" Done.\n\n");
							pState=WAIT;
							}
						}
					}
				}
			}
			bit=0;
		}

		/* Reset the button repeat counter */
		if(z==1) {
			z=0;
			for(i=0;i<binaryLength;i++) {
				pBinary[i]=binary[i];
			}
		}
	}

	rmDup(all, onoff);
	rmDup(unit, onoff);
	rmDup(all, unit);

	/* Print everything */
	printf("--[RESULTS]--\n");
	printf("\n");
	printf("pulse:\t\t%d\n",normalize(pulse));
	printf("rawlen:\t\t%d\n",rawLength);
	printf("binlen:\t\t%d\n",binaryLength);
	printf("plslen:\t\t%d\n",pulse_length);
	printf("\n");
	printf("on-off bit(s):\t");
	z=0;
	while(onoff[z] > -1) {
		printf("%d ",onoff[z++]);
	}
	printf("\n");
	printf("all bit(s):\t");
	z=0;
	while(all[z] > -1) {
		printf("%d ",all[z++]);
	}
	printf("\n");
	printf("unit bit(s):\t");
	z=0;
	while(unit[z] > -1) {
		printf("%d ",unit[z++]);
	}
	printf("\n\n");
	printf("Raw code:\n");
	for(i=0;i<rawLength;i++) {
		printf("%d ",normalize(raw[i])*pulse_length);
	}
	printf("\n");
	printf("Raw simplified:\n");
	printf("On:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",onCode[i]);
	}
	printf("\n");
	printf("Off:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",offCode[i]);
	}
	printf("\n");
	printf("All:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",allCode[i]);
	}
	printf("\n");
	printf("Unit 1:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit1Code[i]);
	}
	printf("\n");
	printf("Unit 2:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit2Code[i]);
	}
	printf("\n");
	printf("Unit 3:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit3Code[i]);
	}
	printf("\n");
	printf("Binary code:\n");
	printf("On:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",onBinary[i]);
	}
	printf("\n");
	printf("Off:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",offBinary[i]);
	}
	printf("\n");
	printf("All:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",allBinary[i]);
	}
	printf("\n");
	printf("Unit 1:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit1Binary[i]);
	}
	printf("\n");
	printf("Unit 2:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit2Binary[i]);
	}
	printf("\n");
	printf("Unit 3:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit3Binary[i]);
	}
	printf("\n");

	return (EXIT_SUCCESS);
}
Пример #6
0
int main(int argc, char **argv) {
	log_shell_enable();
	log_file_disable();

	log_level_set(LOG_NOTICE);

	progname = malloc(16);
	strcpy(progname, "pilight-receive");
	struct options_t *options = NULL;

	JsonNode *json = NULL;

	char *server = malloc(17);
	strcpy(server, "127.0.0.1");
	unsigned short port = PORT;

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

	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);
	options_add(&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])$");
	options_add(&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 = options_parse(&options, argc, argv, 1, &args);
		if(c == -1 || c == -2)
			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);
				exit(EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, VERSION);
				exit(EXIT_SUCCESS);
			break;
			case 'S':
				server = realloc(server, strlen(args)+1);
				strcpy(server, args);
			break;
			case 'P':
				port = (unsigned short)atoi(args);
			break;
			default:
				printf("Usage: %s -l location -d device\n", progname);
				exit(EXIT_SUCCESS);
			break;
		}
	}

	options_delete(options);
    if((sockfd = socket_connect(server, port)) == -1) {
		logprintf(LOG_ERR, "could not connect to pilight-daemon");
		return EXIT_FAILURE;
	}
	sfree((void *)&server);
	while(1) {
		if(steps > WELCOME) {
			/* Clear the receive buffer again and read the welcome message */
			recvBuff = socket_read(sockfd);
			if(recvBuff == NULL) {
				goto close;
			}
		}
		switch(steps) {
			case WELCOME:
				socket_write(sockfd, "{\"message\":\"client receiver\"}");
				steps=IDENTIFY;
			break;
			case IDENTIFY:
				//extract the message
				json = json_decode(recvBuff);
				json_find_string(json, "message", &message);
				assert(message != NULL);
				if(strcmp(message, "accept client") == 0) {
					steps=RECEIVE;
				} else if(strcmp(message, "reject client") == 0) {
					steps=REJECT;
				} else {
					assert(false);
				}
				//cleanup
				json_delete(json);
				json = NULL;
				message = NULL;
			break;
			case RECEIVE: {
					char *line = strtok(recvBuff, "\n");
					//for each line
					while(line) {
						json = json_decode(recvBuff);
						assert(json != NULL);
						char *output = json_stringify(json, "\t");
						printf("%s\n", output);
						sfree((void *)&output);
						json_delete(json);
						line = strtok(NULL,"\n");
					}
			} break;
			case REJECT:
			default:
				goto close;
			break;
		}
	}
close:
	socket_close(sockfd);

	protocol_gc();
	options_gc();
	sfree((void *)&progname);
	sfree((void *)&server);
	sfree((void *)&message);

return EXIT_SUCCESS;
}
Пример #7
0
int main(int argc, char **argv) {

	log_shell_enable();
	log_shell_disable();
	log_level_set(LOG_NOTICE);

	progname = strdup("pilight-learn");
	struct options_t *options = NULL;
	
	lirc_t data;
	char *socket = strdup("/dev/lirc0");
	char *args;
	int have_device = 0;
	int use_lirc = USE_LIRC;
	int gpio_in = GPIO_IN_PIN;
	
	int duration = 0;
	int i = 0;
	int y = 0;
	int z = 0;

	steps_t state = CAPTURE;
	steps_t pState = WAIT;

	int recording = 1;
	int bit = 0;
	int raw[255];
	int pRaw[255];
	int code[255];
	int onCode[255];
	int offCode[255];
	int allCode[255];
	int unit1Code[255];
	int unit2Code[255];
	int unit3Code[255];
	int binary[255];
	int pBinary[255];
	int onBinary[255];
	int offBinary[255];
	int allBinary[255];
	int unit1Binary[255];
	int unit2Binary[255];
	int unit3Binary[255];
	int loop = 1;
	
	int temp[75];
	int footer = 0;
	int header = 0;
	int pulse = 0;
	int onoff[75];
	int all[75];
	int unit[75];
	int rawLength = 0;
	int binaryLength = 0;

	memset(onoff,-1,75);
	memset(all,-1,75);
	memset(unit,-1,75);

	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);
	options_add(&options, 'S', "socket", has_value, 0, "^/dev/([A-Za-z]+)([0-9]+)");
	options_add(&options, 'L', "lirc", no_value, 0, NULL);
	options_add(&options, 'G', "gpio", has_value, 0, "^[0-7]$");
	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		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");		
				printf("\t -S --socket=socket\tread from given socket\n");
				printf("\t -L --lirc\t\tuse the lirc_rpi kernel module\n");
				printf("\t -G --gpio=#\t\tGPIO pin we're directly reading from\n");
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, "1.0");
				return (EXIT_SUCCESS);
			break;
			case 'L':
				use_lirc = 1;
			break;
			case 'G':
				gpio_in = atoi(optarg);
				use_lirc = 0;
			break;
			case 'S':
				free(socket);
				socket = optarg;
				have_device = 1;
			break;
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	
	if(use_lirc == 1) {
		hw_choose_driver(NULL);
		
		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;
		}
	} else {
		if(wiringPiSetup() == -1)
			return EXIT_FAILURE;

		if(wiringPiISR(gpio_in, INT_EDGE_BOTH) < 0) {
			logprintf(LOG_ERR, "unable to register interrupt for pin %d", gpio_in) ;
			return 1 ;
		}

		(void)piHiPri(55);
	}
	
	printf("Please make sure the daemon is not running when using this debugger.\n\n");	

	while(loop) {
		switch(state) {
			case CAPTURE:
				printf("1. Please send and hold one of the OFF buttons.");
				break;
			case ON:
				/* Store the previous OFF button code */
				for(i=0;i<binaryLength;i++) {
					offBinary[i] = binary[i];
				}
				printf("2. Please send and hold the ON button for the same device\n");
				printf("   as for which you send the previous OFF button.");
				break;
			case OFF:
				/* Store the previous ON button code */
				for(i=0;i<binaryLength;i++) {
					onBinary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					onCode[i] = code[i];
				}
				z=0;

				/* Compare the ON and OFF codes and save bit that are different */
				for(i=0;i<binaryLength;i++) {
					if(offBinary[i] != onBinary[i]) {
						onoff[z++]=i;
					}
				}
				for(i=0;i<rawLength;i++) {
					offCode[i] = code[i];
				}
				printf("3. Please send and hold (one of the) ALL buttons.\n");
				printf("   If you're remote doesn't support turning ON or OFF\n");
				printf("   all devices at once, press the same OFF button as in\n");
				printf("   the beginning.");
			break;
			case ALL:
				z=0;
				memset(temp,-1,75);
				/* Store the ALL code */
				for(i=0;i<binaryLength;i++) {
					allBinary[i] = binary[i];
					if(allBinary[i] != onBinary[i]) {
						temp[z++] = i;
					}
				}
				for(i=0;i<rawLength;i++) {
					allCode[i] = code[i];
				}
				/* Compare the ALL code to the ON and OFF code and store the differences */
				y=0;
				for(i=0;i<binaryLength;i++) {
					if(allBinary[i] != offBinary[i]) {
						all[y++] = i;
					}
				}
				if((unsigned int)z < y) {
					for(i=0;i<z;i++) {
						all[z]=temp[z];
					}
				}
				printf("4. Please send and hold the ON button with the lowest ID.");
			break;
			case UNIT1:
				/* Store the lowest unit code */
				for(i=0;i<binaryLength;i++) {
					unit1Binary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					unit1Code[i] = code[i];
				}
				printf("5. Please send and hold the ON button with the second to lowest ID.");
			break;
			case UNIT2:
				/* Store the second to lowest unit code */
				for(i=0;i<binaryLength;i++) {
					unit2Binary[i] = binary[i];
				}
				for(i=0;i<rawLength;i++) {
					unit2Code[i] = code[i];
				}
				printf("6. Please send and hold the ON button with the highest ID.");
			break;
			case PROCESSUNIT:
				z=0;
				/* Store the highest unit code and compare the three codes. Store all
				   bit that are different */
				for(i=0;i<binaryLength;i++) {
					unit3Binary[i] = binary[i];
					if((unit2Binary[i] != unit1Binary[i]) || (unit1Binary[i] != unit3Binary[i]) || (unit2Binary[i] != unit3Binary[i])) {
						unit[z++]=i;
					}
				}
				for(i=0;i<rawLength;i++) {
					unit3Code[i] = code[i];
				}
				state=STOP;
			break;
			case WAIT:
			case STOP:
			default:;
		}
		fflush(stdout);
		if(state!=WAIT)
			pState=state;	
		if(state==STOP)
			loop = 0;
		else
			state=WAIT;		
		if(use_lirc == 1) {
			data = hw.readdata(0);
			duration = (data & PULSE_MASK);
		} else {
			duration = irq_read(gpio_in);
		}

		/* 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;
					z=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;
				}
				if(rawLength == 0 || rawLength == bit) {
				   /*|| ((((raw[0]-(raw[0]*0.3)) < header[0]) || ((raw[0]+(raw[0]*0.3)) > header[0]))
 				       && (((raw[1]-(raw[1]*0.3)) < header[1]) || ((raw[1]+(raw[1]*0.3)) > header[1]))
					   && (((raw[bit-1]-(raw[bit-1]*0.1)) < footer) || ((raw[bit-1]+(raw[bit-1]*0.1)) > footer)))) {*/

					/* 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) {
						/* 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;
							}
						}
						if(binaryLength == 0)
							binaryLength = (int)((float)i/4);

						/* Check if the subsequent binary code matches
						   to check if the same button was still held */
						if(binaryLength == (i/4)) {
							for(i=0;i<binaryLength;i++) {
								if(pBinary[i] != binary[i]) {
									z=1;
								}
							}

							/* If we are capturing a different button
							   continue to the next step */
							if(z==1 || state == CAPTURE) {
								switch(pState) {
									case CAPTURE:
										state=ON;
									break;
									case ON:
										state=OFF;
									break;
									case OFF:
										state=ALL;
									break;
									case ALL:
										state=UNIT1;
									break;
									case UNIT1:
										state=UNIT2;
									break;
									case UNIT2:
										state=PROCESSUNIT;
									break;
									case PROCESSUNIT:
										state=STOP;
									break;
									case WAIT:
									case STOP:
									default:;
								}
							printf(" Done.\n\n");
							pState=WAIT;
							}
						}
					}
				}
			}
			bit=0;
		}

		/* Reset the button repeat counter */
		if(z==1) {
			z=0;
			for(i=0;i<binaryLength;i++) {
				pBinary[i]=binary[i];
			}
		}
	}

	rmDup(all, onoff);
	rmDup(unit, onoff);
	rmDup(all, unit);

	/* Print everything */
	printf("--[RESULTS]--\n");
	printf("\n");
	if(normalize(header) == normalize(pulse)) {
		printf("header:\t\t0\n");
	} else {
		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("on-off bit(s):\t");
	z=0;
	while(onoff[z] > -1) {
		printf("%d ",onoff[z++]);
	}
	printf("\n");
	printf("all bit(s):\t");
	z=0;
	while(all[z] > -1) {
		printf("%d ",all[z++]);
	}
	printf("\n");
	printf("unit bit(s):\t");
	z=0;
	while(unit[z] > -1) {
		printf("%d ",unit[z++]);
	}
	printf("\n\n");
	printf("Raw code:\n");
	for(i=0;i<rawLength;i++) {
		printf("%d ",normalize(raw[i])*PULSE_LENGTH);
	}
	printf("\n");
	printf("Raw simplified:\n");
	printf("On:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",onCode[i]);
	}
	printf("\n");
	printf("Off:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",offCode[i]);
	}
	printf("\n");
	printf("All:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",allCode[i]);
	}
	printf("\n");
	printf("Unit 1:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit1Code[i]);
	}
	printf("\n");
	printf("Unit 2:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit2Code[i]);
	}
	printf("\n");
	printf("Unit 3:\t");
	for(i=0;i<rawLength;i++) {
		printf("%d",unit3Code[i]);
	}
	printf("\n");
	printf("Binary code:\n");
	printf("On:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",onBinary[i]);
	}
	printf("\n");
	printf("Off:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",offBinary[i]);
	}
	printf("\n");
	printf("All:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",allBinary[i]);
	}
	printf("\n");
	printf("Unit 1:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit1Binary[i]);
	}
	printf("\n");
	printf("Unit 2:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit2Binary[i]);
	}
	printf("\n");
	printf("Unit 3:\t");
	for(i=0;i<binaryLength;i++) {
		printf("%d",unit3Binary[i]);
	}
	printf("\n");
	
	free(socket);
	free(progname);
	return (EXIT_SUCCESS);
}
Пример #8
0
int main(int argc, char **argv) {

	gc_attach(main_gc);

	/* Catch all exit signals for gc */
	gc_catch();

	log_shell_enable();
	log_file_disable();
	log_level_set(LOG_NOTICE);

	struct options_t *options = NULL;
	
	char *args = NULL;
	char *hwfile = NULL;
	pid_t pid = 0;

	if(!(settingsfile = malloc(strlen(SETTINGS_FILE)+1))) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}
	strcpy(settingsfile, SETTINGS_FILE);	
	
	if(!(progname = malloc(12))) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-raw");	

	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'S', "settings", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);

	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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");		
				printf("\t -S --settings\t\tsettings file\n");
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, VERSION);
				return (EXIT_SUCCESS);
			break;	
			case 'S': 
				if(access(args, F_OK) != -1) {
					settingsfile = realloc(settingsfile, strlen(args)+1);
					if(!settingsfile) {
						logprintf(LOG_ERR, "out of memory");
						exit(EXIT_FAILURE);
					}
					strcpy(settingsfile, args);
					settings_set_file(args);
				} else {
					fprintf(stderr, "%s: the settings file %s does not exists\n", progname, args);
					return EXIT_FAILURE;
				}
			break;
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	options_delete(options);

	char pilight_daemon[] = "pilight-daemon";
	char pilight_learn[] = "pilight-learn";
	char pilight_debug[] = "pilight-debug";
	if((pid = findproc(pilight_daemon, NULL)) > 0) {
		logprintf(LOG_ERR, "pilight-daemon instance found (%d)", (int)pid);
		return (EXIT_FAILURE);
	}

	if((pid = findproc(pilight_learn, NULL)) > 0) {
		logprintf(LOG_ERR, "pilight-learn instance found (%d)", (int)pid);
		return (EXIT_FAILURE);
	}

	if((pid = findproc(pilight_debug, NULL)) > 0) {
		logprintf(LOG_ERR, "pilight-debug instance found (%d)", (int)pid);
		return (EXIT_FAILURE);
	}	
	
	if(access(settingsfile, F_OK) != -1) {
		if(settings_read() != 0) {
			return EXIT_FAILURE;
		}
	}	
	
	hardware_init();
	
	if(settings_find_string("hardware-file", &hwfile) == 0) {
		hardware_set_file(hwfile);
		if(hardware_read() == EXIT_FAILURE) {
			goto clear;
		}
	}

	/* Start threads library that keeps track of all threads used */
	threads_create(&pth, NULL, &threads_start, (void *)NULL);

	struct conf_hardware_t *tmp_confhw = conf_hardware;
	while(tmp_confhw) {
		if(tmp_confhw->hardware->init() == EXIT_FAILURE) {
			logprintf(LOG_ERR, "could not initialize %s hardware mode", tmp_confhw->hardware->id);
			goto clear;
		}
		threads_register(tmp_confhw->hardware->id, &receive_code, (void *)tmp_confhw->hardware, 0);
		tmp_confhw = tmp_confhw->next;
	}

	while(main_loop) {
		sleep(1);
	}

clear:
	main_gc();
	return (EXIT_FAILURE);
}
Пример #9
0
int main(int argc, char **argv) {
	// memtrack();
	atomicinit();
	gc_attach(main_gc);

	/* Catch all exit signals for gc */
	gc_catch();

	log_shell_enable();
	log_file_disable();
	log_level_set(LOG_NOTICE);

	struct options_t *options = NULL;
	char *p = NULL;
	char *args = NULL;

	if((progname = MALLOC(13)) == NULL) {
		fprintf(stderr, "out of memory\n");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-uuid");

	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);

	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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");
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s v%s\n", progname, PILIGHT_VERSION);
				return (EXIT_SUCCESS);
			break;
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	options_delete(options);

	int nrdevs = 0, x = 0;
	char **devs = NULL;
	if((nrdevs = inetdevs(&devs)) > 0) {
		for(x=0;x<nrdevs;x++) {
			if((p = genuuid(devs[x])) == NULL) {
				logprintf(LOG_ERR, "could not generate the device uuid");
			} else {
				strcpy(pilight_uuid, p);
				FREE(p);
				break;
			}
		}
	}
	array_free(&devs, nrdevs);

	printf("%s\n", pilight_uuid);

	main_gc();
	return (EXIT_FAILURE);
}
Пример #10
0
int main(int argc, char **argv) {

	log_shell_enable();
	log_file_disable();
	log_level_set(LOG_DEBUG);

	struct options_t *options = NULL;

	char *args = NULL;
	char fwfile[4096] = {'\0'};

	progname = malloc(15);
	if(!progname) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-flash");

	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'f', "file", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);

	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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");
				printf("\t -f --file=firmware\tfirmware file\n");
				goto close;
			break;
			case 'V':
				printf("%s %s\n", progname, VERSION);
				goto close;
			break;
			case 'f':
				if(access(args, F_OK) != -1) {
					strcpy(fwfile, args);
				} else {
					fprintf(stderr, "%s: the firmware file %s does not exists\n", progname, args);
					goto close;
				}
			break;
			default:
				printf("Usage: %s -f pilight_firmware_tX5_v3.hex\n", progname);
				goto close;
			break;
		}
	}
	options_delete(options);

#ifdef FIRMWARE
	if(strlen(fwfile) == 0) {
		printf("Usage: %s -f pilight_firmware_tX5_vX.hex\n", progname);
		goto close;
	}

	firmware.version = 0;
	logprintf(LOG_INFO, "**** START UPD. FW ****");
	firmware_getmp();
	if(firmware_update(fwfile) != 0) {
		logprintf(LOG_INFO, "**** FAILED UPD. FW ****");
	} else {
		logprintf(LOG_INFO, "**** DONE UPD. FW ****");
	}
#else
	logprintf(LOG_ERR, "pilight was compiled without firmware flashing support");
#endif

close:
	log_shell_disable();
	options_gc();
	log_gc();
	sfree((void *)&progname);
	return (EXIT_SUCCESS);
}
Пример #11
0
int main(int argc, char **argv) {
	/* Run main garbage collector when quiting the daemon */
	gc_attach(main_gc);

	/* Catch all exit signals for gc */
	gc_catch();

	loglevel = LOG_INFO;

	log_file_disable();
	log_shell_enable();

	char *logfile = malloc(strlen(LOG_FILE)+1);
	strcpy(logfile, LOG_FILE);
	log_file_set(logfile);
	sfree((void *)&logfile);

	prevMessage = malloc(4);
	memset(prevMessage, '\0', 4);

	progname = malloc(14);
	strcpy(progname, "splash-daemon");

	struct socket_callback_t socket_callback;
	struct options_t *options = NULL;
	char *args = NULL;
	char buffer[BUFFER_SIZE];
	int f;

	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);
	options_add(&options, 'D', "nodaemon", no_value, 0, NULL);

	while (1) {
		int c;
		c = options_parse(&options, argc, argv, 1, &args);
		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");
				printf("\t -S --settings\t\tsettings file\n");
				printf("\t -D --nodaemon\t\tdo not daemonize and\n");
				printf("\t\t\t\tshow debug information\n");
				return (EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, "1.0");
				return (EXIT_SUCCESS);
			break;
			case 'D':
				nodaemon=1;
			break;
			default:
				printf("Usage: %s [options]\n", progname);
				return (EXIT_FAILURE);
			break;
		}
	}
	options_delete(options);

	pid_file = malloc(sizeof(PID_FILE)+1);
	strcpy(pid_file, PID_FILE);

	template_file = malloc(14);
	strcpy(template_file, "template.json");

	if((f = open(pid_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) != -1) {
		if(read(f, buffer, BUFFER_SIZE) != -1) {
			//If the file is empty, create a new process
			if(!atoi(buffer)) {
				running = 0;
			} else {
				//Check if the process is running
				kill(atoi(buffer), 0);
				//If not, create a new process
				if(errno == ESRCH) {
					running = 0;
				}
			}
		}
	} else {
		logprintf(LOG_ERR, "could not open / create pid_file %s", pid_file);
		return EXIT_FAILURE;
	}
	close(f);

	if(nodaemon == 1 || running == 1) {
		log_level_set(LOG_DEBUG);
	}

	if(running == 1) {
		nodaemon=1;
		logprintf(LOG_NOTICE, "already active (pid %d)", atoi(buffer));
		return EXIT_FAILURE;
	}

	if(nodaemon == 0) {
		deamonize();
		socket_start(PORT);
		fb_init();
	} else {
		socket_start(PORT);
	}

	if(template_read(template_file) == EXIT_FAILURE) {
		logprintf(LOG_NOTICE, "failed to read template file %s", template_file);
		main_gc();
		return EXIT_FAILURE;
	}

	if(nodaemon == 1) {
		//template_print();
	}

    //initialise all socket_clients and handshakes to 0 so not checked
	memset(socket_clients, 0, sizeof(socket_clients));

    socket_callback.client_disconnected_callback = NULL;
    socket_callback.client_connected_callback = NULL;
    socket_callback.client_data_callback = &socket_parse_data;

	main_draw();

	/* Make sure the server part is non-blocking by creating a new thread */
	pthread_create(&pth, NULL, &update_progress, (void *)NULL);

	socket_wait((void *)&socket_callback);

	while(main_loop) {
		sleep(1);
	}

	return EXIT_FAILURE;
}
Пример #12
0
int main(int argc, char **argv) {
	// memtrack();

	struct options_t *options = NULL;
	struct ssdp_list_t *ssdp_list = NULL;
	struct devices_t *dev = NULL;
	struct JsonNode *json = NULL;
	struct JsonNode *tmp = NULL;
	char *recvBuff = NULL, *message = NULL, *output = NULL;
	char *device = NULL, *state = NULL, *values = NULL;
	char *server = NULL;
	int has_values = 0, sockfd = 0, hasconfarg = 0;
	unsigned short port = 0, showhelp = 0, showversion = 0;

	log_file_disable();
	log_shell_enable();
	log_level_set(LOG_NOTICE);

	if(!(progname = MALLOC(16))) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-control");

	/* Define all CLI arguments of this program */
	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'd', "device", OPTION_HAS_VALUE, 0,  JSON_NULL, NULL, NULL);
	options_add(&options, 's', "state", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'v', "values", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'S', "server", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "^(([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])$");
	options_add(&options, 'P', "port", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "[0-9]{1,4}");
	options_add(&options, 'C', "config", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);

	/* 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 = options_parse(&options, argc, argv, 1, &optarg);
		if(c == -1)
			break;
		if(c == -2) {
			showhelp = 1;
			break;
		}
		switch(c) {
			case 'H':
				showhelp = 1;
			break;
			case 'V':
				showversion = 1;
			break;
			case 'd':
				if((device = REALLOC(device, strlen(optarg)+1)) == NULL) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				strcpy(device, optarg);
			break;
			case 's':
				if((state = REALLOC(state, strlen(optarg)+1)) == NULL) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				strcpy(state, optarg);
			break;
			case 'v':
				if((values = REALLOC(values, strlen(optarg)+1)) == NULL) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				strcpy(values, optarg);
			break;
			case 'C':
				if(config_set_file(optarg) == EXIT_FAILURE) {
					return EXIT_FAILURE;
				}
				hasconfarg = 1;
			break;
			case 'S':
				if(!(server = REALLOC(server, strlen(optarg)+1))) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				strcpy(server, optarg);
			break;
			case 'P':
				port = (unsigned short)atoi(optarg);
			break;
			default:
				printf("Usage: %s -l location -d device -s state\n", progname);
				goto close;
			break;
		}
	}
	options_delete(options);

	if(showversion == 1) {
		printf("%s %s\n", progname, PILIGHT_VERSION);
		goto close;
	}
	if(showhelp == 1) {
		printf("\t -H --help\t\t\tdisplay this message\n");
		printf("\t -V --version\t\t\tdisplay version\n");
		printf("\t -S --server=x.x.x.x\t\tconnect to server address\n");
		printf("\t -C --config\t\t\tconfig file\n");
		printf("\t -P --port=xxxx\t\t\tconnect to server port\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");
		printf("\t -v --values=values\t\tspecific comma separated values, e.g.:\n");
		printf("\t\t\t\t\t-v dimlevel=10\n");
		goto close;
	}
	if(device == NULL || state == NULL ||
	   strlen(device) == 0 || strlen(state) == 0) {
		printf("Usage: %s -d device -s state\n", progname);
		goto close;
	}

	if(server && port > 0) {
		if((sockfd = socket_connect(server, port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			goto close;
		}
	} else if(ssdp_seek(&ssdp_list) == -1) {
		logprintf(LOG_ERR, "no pilight ssdp connections found");
		goto close;
	} else {
		if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			goto close;
		}
	}
	if(ssdp_list) {
		ssdp_free(ssdp_list);
	}

	protocol_init();
	config_init();
	if(hasconfarg == 1) {
		if(config_read() != EXIT_SUCCESS) {
			goto close;
		}
	}

	socket_write(sockfd, "{\"action\":\"identify\"}");
	if(socket_read(sockfd, &recvBuff, 0) != 0
	   || strcmp(recvBuff, "{\"status\":\"success\"}") != 0) {
		goto close;
	}

	json = json_mkobject();
	json_append_member(json, "action", json_mkstring("request config"));
	output = json_stringify(json, NULL);
	socket_write(sockfd, output);
	json_free(output);
	json_delete(json);

	if(socket_read(sockfd, &recvBuff, 0) == 0) {
		if(json_validate(recvBuff) == true) {
			json = json_decode(recvBuff);
			if(json_find_string(json, "message", &message) == 0) {
				if(strcmp(message, "config") == 0) {
					struct JsonNode *jconfig = NULL;
					if((jconfig = json_find_member(json, "config")) != NULL) {
						int match = 1;
						while(match) {
							struct JsonNode *jchilds = json_first_child(jconfig);
							match = 0;
							while(jchilds) {
								if(strcmp(jchilds->key, "devices") != 0) {
									json_remove_from_parent(jchilds);
									tmp = jchilds;
									match = 1;
								}
								jchilds = jchilds->next;
								if(tmp != NULL) {
									json_delete(tmp);
								}
								tmp = NULL;
							}
						}
						config_parse(jconfig);
						if(devices_get(device, &dev) == 0) {
							JsonNode *joutput = json_mkobject();
							JsonNode *jcode = json_mkobject();
							JsonNode *jvalues = json_mkobject();
							json_append_member(jcode, "device", json_mkstring(device));

							if(values != NULL) {
								char **array = NULL;
								unsigned int n = explode(values, ",=", &array), q = 0;
								for(q=0;q<n;q+=2) {
									char *name = MALLOC(strlen(array[q])+1);
									if(name == NULL) {
										logprintf(LOG_ERR, "out of memory\n");
										exit(EXIT_FAILURE);
									}
									strcpy(name, array[q]);
									if(q+1 == n) {
										for(q=0;q<n;q++) {
											FREE(array[q]);
										}
										FREE(array);
										logprintf(LOG_ERR, "\"%s\" is missing a value for device \"%s\"", name, device);
										FREE(name);
										break;
									} else {
										char *val = MALLOC(strlen(array[q+1])+1);
										if(val == NULL) {
											logprintf(LOG_ERR, "out of memory\n");
											exit(EXIT_FAILURE);
										}
										strcpy(val, array[q+1]);
										if(devices_valid_value(device, name, val) == 0) {
											if(isNumeric(val) == EXIT_SUCCESS) {
												char *ptr = strstr(array[q+1], ".");
												int decimals = 0;
												if(ptr != NULL) {
													decimals = (int)(strlen(array[q+1])-((size_t)(ptr-array[q+1])+1));
												}
												json_append_member(jvalues, name, json_mknumber(atof(val), decimals));
											} else {
												json_append_member(jvalues, name, json_mkstring(val));
											}
											has_values = 1;
										} else {
											logprintf(LOG_ERR, "\"%s\" is an invalid value for device \"%s\"", name, device);
											for(q=0;q<n;q++) {
												FREE(array[q]);
											}
											FREE(array);
											FREE(name);
											json_delete(json);
											goto close;
										}
									}
									FREE(name);
								}
								unsigned int z = 0;
								for(z=q;z<n;z++) {
									FREE(array[z]);
								}
								if(n > 0) {
									FREE(array);
								}
							}

							if(devices_valid_state(device, state) == 0) {
								json_append_member(jcode, "state", json_mkstring(state));
							} else {
								logprintf(LOG_ERR, "\"%s\" is an invalid state for device \"%s\"", state, device);
								json_delete(json);
								goto close;
							}

							if(has_values == 1) {
								json_append_member(jcode, "values", jvalues);
							} else {
								json_delete(jvalues);
							}
							json_append_member(joutput, "action", json_mkstring("control"));
							json_append_member(joutput, "code", jcode);
							output = json_stringify(joutput, NULL);
							socket_write(sockfd, output);
							json_free(output);
							json_delete(joutput);
							if(socket_read(sockfd, &recvBuff, 0) != 0
							   || strcmp(recvBuff, "{\"status\":\"success\"}") != 0) {
								logprintf(LOG_ERR, "failed to control %s", device);
							}
						} else {
							logprintf(LOG_ERR, "the device \"%s\" does not exist", device);
							json_delete(json);
							goto close;
						}
					}
				}
			}
			json_delete(json);
		}
	}
close:
	if(recvBuff) {
		FREE(recvBuff);
	}
	if(sockfd > 0) {
		socket_close(sockfd);
	}
	if(server != NULL) {
		FREE(server);
	}
	if(device != NULL) {
		FREE(device);
	}
	if(state != NULL) {
		FREE(state);
	}
	if(values != NULL) {
		FREE(values);
	}
	log_shell_disable();
	socket_gc();
	config_gc();
	protocol_gc();
	options_gc();
	event_operator_gc();
	event_action_gc();
	dso_gc();
	log_gc();
	threads_gc();
	gc_clear();
	FREE(progname);
	xfree();

	return EXIT_SUCCESS;
}
Пример #13
0
int main(int argc, char **argv) {

	log_file_disable();
	log_shell_enable();
	log_level_set(LOG_NOTICE);

	progname = malloc(13);
	strcpy(progname, "pilight-send");

	struct options_t *options = NULL;

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

	/* Hold the name of the protocol */
	char protobuffer[25] = "\0";
	/* 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 = malloc(17);
	strcpy(server, "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 */
	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);
	options_add(&options, 'p', "protocol", has_value, 0, NULL);
	options_add(&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])$");
	options_add(&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 = options_parse(&options, argc, argv, 0, &args);

		if (c == -1)
			break;
		switch(c) {
			case 'p':
				if(strlen(args) == 0) {
					logprintf(LOG_ERR, "options '-p' and '--protocol' require an argument");
					exit(EXIT_FAILURE);
				} else {
					strcpy(protobuffer, args);
				}
			break;
			case 'V':
				version = 1;
			break;
			case 'H':
				help = 1;
			break;
			case 'S':
				server = realloc(server, strlen(args)+1);
				strcpy(server, args);
			break;
			case 'P':
				port = (unsigned short)atoi(args);
			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 {
			struct protocols_t *pnode = protocols;
			/* Retrieve the used protocol */
			while(pnode) {
				/* Check if the protocol exists */
				protocol = pnode->listener;
				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 && help == 0) {
						options_merge(&options, &protocol->options);
					} else if(help == 1) {
						protohelp=1;
					}
					break;
				}
				pnode = pnode->next;
			}
			/* 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");
		goto close;
	} else if(help == 1 || protohelp == 1 || match == 0) {
		if(protohelp == 1 && match == 1 && protocol->printHelp)
			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) {
			printf("\n\t[%s]\n", protobuffer);
			protocol->printHelp();
		} else {
			printf("\nThe supported protocols are:\n");
			struct protocols_t *pnode = protocols;
			/* Retrieve the used protocol */
			while(pnode) {
				protocol = pnode->listener;
				if(protocol->createCode) {
					while(protocol->devices) {
						printf("\t %s\t\t",protocol->devices->id);
						if(strlen(protocol->devices->id)<7)
							printf("\t");
						if(strlen(protocol->devices->id)<14)
							printf("\t");
						printf("%s\n", protocol->devices->desc);
						protocol->devices = protocol->devices->next;
					}
				}
				pnode = pnode->next;
			}
		}
		goto close;
	}

	/* 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 = options_parse(&options, argc, argv, 1, &args);

		if(c == -1)
			break;
	}

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

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

		while(1) {
			if(steps > WELCOME) {
				/* Clear the receive buffer again and read the welcome message */
				if((recvBuff = socket_read(sockfd))) {
					json = json_decode(recvBuff);
					json_find_string(json, "message", &message);
				} else {
					goto close;
				}
				usleep(100);
			}
			switch(steps) {
				case WELCOME:
					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);
					char *output = json_stringify(json, NULL);
					socket_write(sockfd, output);
					free(output);
					goto close;
				break;
				case REJECT:
				default:
					goto close;
				break;
			}
		}
	}
close:
	if(json) {
		json_delete(json);
	}
	if(sockfd) {
		socket_close(sockfd);
	}
	log_shell_disable();
	free(server);
	protocol_gc();
	options_delete(options);
	options_gc();
	free(progname);

return EXIT_SUCCESS;
}
Пример #14
0
int main(int argc, char **argv) {

	log_file_disable();
	log_shell_enable();
	log_level_set(LOG_NOTICE);

	progname = malloc(16);
	strcpy(progname, "pilight-control");

	struct options_t *options = NULL;
	struct ssdp_list_t *ssdp_list = NULL;

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

	char device[50];
	char location[50];
	char state[10] = {'\0'};
	char values[255] = {'\0'};
	struct conf_locations_t *slocation = NULL;
	struct conf_devices_t *sdevice = NULL;
	int has_values = 0;

	char *server = NULL;
	unsigned short port = 0;

	JsonNode *json = NULL;
	JsonNode *jconfig = NULL;
	JsonNode *jcode = NULL;
	JsonNode *jvalues = NULL;

	/* Define all CLI arguments of this program */
	options_add(&options, 'H', "help", no_value, 0, NULL);
	options_add(&options, 'V', "version", no_value, 0, NULL);
	options_add(&options, 'l', "location", has_value, 0, NULL);
	options_add(&options, 'd', "device", has_value, 0,  NULL);
	options_add(&options, 's', "state", has_value, 0,  NULL);
	options_add(&options, 'v', "values", has_value, 0,  NULL);
	options_add(&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])$");
	options_add(&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 = options_parse(&options, argc, argv, 1, &optarg);
		if(c == -1)
			break;
		if(c == -2)
			c = 'H';
		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=x.x.x.x\t\tconnect to server address\n");
				printf("\t -P --port=xxxx\t\t\tconnect to server port\n");
				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");
				printf("\t -v --values=values\t\tspecific comma separated values, e.g.:\n");
				printf("\t\t\t\t\t-v dimlevel=10\n");
				exit(EXIT_SUCCESS);
			break;
			case 'V':
				printf("%s %s\n", progname, VERSION);
				exit(EXIT_SUCCESS);
			break;
			case 'l':
				strcpy(location, optarg);
			break;
			case 'd':
				strcpy(device, optarg);
			break;
			case 's':
				strcpy(state, optarg);
			break;
			case 'v':
				strcpy(values, optarg);
			break;
			case 'S':
				server = realloc(server, strlen(optarg)+1);
				strcpy(server, optarg);
			break;
			case 'P':
				port = (unsigned short)atoi(optarg);
			break;
			default:
				printf("Usage: %s -l location -d device -s state\n", progname);
				exit(EXIT_SUCCESS);
			break;
		}
	}
	options_delete(options);

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

	if(server && port > 0) {
		if((sockfd = socket_connect(server, port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			exit(EXIT_FAILURE);
		}
	} else if(ssdp_seek(&ssdp_list) == -1) {
		logprintf(LOG_ERR, "no pilight ssdp connections found");
		goto close;
	} else {
		if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1) {
			logprintf(LOG_ERR, "could not connect to pilight-daemon");
			goto close;
		}
		sfree((void *)&ssdp_list);
	}

	protocol_init();

	while(1) {
		if(steps > WELCOME) {
			/* Clear the receive buffer again and read the welcome message */
			if(steps == CONFIG) {
				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(100);
		}
		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;
				json_delete(json);
			break;
			case CONFIG:
				if((jconfig = json_find_member(json, "config")) != NULL) {
					config_parse(jconfig);
					if(config_get_location(location, &slocation) == 0) {
						if(config_get_device(location, device, &sdevice) == 0) {
							JsonNode *joutput = json_mkobject();
							jcode = json_mkobject();
							jvalues = json_mkobject();

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

							pch = strtok(values, ",=");
							while(pch != NULL) {
								char *name = strdup(pch);
								pch = strtok(NULL, ",=");
								if(pch == NULL) {
									break;
								} else {
									char *val = strdup(pch);
									if(pch != NULL) {
										if(config_valid_value(location, device, name, val) == 0) {
											if(isNumeric(val) == EXIT_SUCCESS) {
												json_append_member(jvalues, name, json_mknumber(atoi(val)));
											} else {
												json_append_member(jvalues, name, json_mkstring(val));
											}
											has_values = 1;
										} else {
											logprintf(LOG_ERR, "\"%s\" is an invalid value for device \"%s\"", name, device);
											goto close;
										}
									} else {
										logprintf(LOG_ERR, "\"%s\" is an invalid value for device \"%s\"", name, device);
										goto close;
									}
									pch = strtok(NULL, ",=");
									if(pch == NULL) {
										break;
									}
								}
							}

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

							if(has_values == 1) {
								json_append_member(jcode, "values", jvalues);
							} else {
								json_delete(jvalues);
							}

							json_append_member(joutput, "message", json_mkstring("send"));
							json_append_member(joutput, "code", jcode);
							char *output = json_stringify(joutput, NULL);
							socket_write(sockfd, output);
							sfree((void *)&output);
							json_delete(joutput);
						} 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;
					}
				}
				json_delete(json);
				goto close;
			break;
			case REJECT:
			default:
				json_delete(json);
				goto close;
			break;
		}
	}
close:
	if(sockfd > 0) {
		socket_close(sockfd);
	}
	if(server) {
		sfree((void *)&server);
	}
	log_shell_disable();
	config_gc();
	protocol_gc();
	socket_gc();
	options_gc();
	log_gc();
	sfree((void *)&progname);

return EXIT_SUCCESS;
}
Пример #15
0
int main(int argc, char **argv) {
	// memtrack();

	wiringXLog = _lognone;

	log_file_disable();
	log_shell_enable();
	log_level_set(LOG_NOTICE);

	if(!(progname = MALLOC(13))) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}
	strcpy(progname, "pilight-send");

	struct options_t *options = NULL;
	struct ssdp_list_t *ssdp_list = NULL;

	int sockfd = 0;
	char *args = NULL, *recvBuff = NULL;

	/* Hold the name of the protocol */
	char *protobuffer = NULL;
	/* 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 *uuid = NULL;
	char *server = NULL;
	unsigned short port = 0;

	/* Hold the final protocol struct */
	protocol_t *protocol = NULL;
	JsonNode *code = NULL;

	/* Define all CLI arguments of this program */
	options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'p', "protocol", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
	options_add(&options, 'S', "server", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "^(([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])$");
	options_add(&options, 'P', "port", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "[0-9]{1,4}");
	options_add(&options, 'U', "uuid", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "[a-zA-Z0-9]{4}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{6}");

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

	/* Initialize protocols */
	protocol_init();

	/* Check if a protocol was given */
	if(protobuffer && strlen(protobuffer) > 0 && strcmp(protobuffer, "-V") != 0) {
		if(strlen(protobuffer) > 0 && version) {
			printf("-p and -V cannot be combined\n");
		} else {
			struct protocols_t *pnode = protocols;
			/* Retrieve the used protocol */
			while(pnode) {
				/* Check if the protocol exists */
				protocol = pnode->listener;
				if(protocol_device_exists(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 && help == 0) {
						options_merge(&options, &protocol->options);
					} else if(help == 1) {
						protohelp=1;
					}
					break;
				}
				pnode = pnode->next;
			}
			/* If no protocols matches the requested protocol */
			if(!match) {
				logprintf(LOG_ERR, "this protocol is not supported or doesn't support sending");
			}
		}
	}

	/* 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 = options_parse(&options, argc, argv, 2, &args);

		if(c == -1)
			break;
		if(c == -2) {
			if(match == 1) {
				protohelp = 1;
			} else {
				help = 1;
			}
		break;
		}
	}

	/* Display help or version information */
	if(version == 1) {
		printf("%s %s\n", progname, PILIGHT_VERSION);
		goto close;
	} else if(help == 1 || protohelp == 1 || match == 0) {
		if(protohelp == 1 && match == 1 && protocol->printHelp)
			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 -p --protocol=protocol\t\tthe protocol that you want to control\n");
			printf("\t -S --server=x.x.x.x\t\tconnect to server address\n");
			printf("\t -P --port=xxxx\t\t\tconnect to server port\n");
			printf("\t -C --config\t\t\tconfig file\n");
			printf("\t -U --uuid=xxx-xx-xx-xx-xxxxxx\tUUID\n");
		}
		if(protohelp == 1 && match == 1 && protocol->printHelp) {
			printf("\n\t[%s]\n", protobuffer);
			protocol->printHelp();
		} else {
			printf("\nThe supported protocols are:\n");
			struct protocols_t *pnode = protocols;
			/* Retrieve the used protocol */
			while(pnode) {
				protocol = pnode->listener;
				if(protocol->createCode) {
					struct protocol_devices_t *tmpdev = protocol->devices;
					while(tmpdev) {
						struct pname_t *node = MALLOC(sizeof(struct pname_t));
						if(!node) {
							logprintf(LOG_ERR, "out of memory");
							exit(EXIT_FAILURE);
						}
						if(!(node->name = MALLOC(strlen(tmpdev->id)+1))) {
							logprintf(LOG_ERR, "out of memory");
							exit(EXIT_FAILURE);
						}
						strcpy(node->name, tmpdev->id);
						if(!(node->desc = MALLOC(strlen(tmpdev->desc)+1))) {
							logprintf(LOG_ERR, "out of memory");
							exit(EXIT_FAILURE);
						}
						strcpy(node->desc, tmpdev->desc);
						node->next = pname;
						pname = node;
						tmpdev = tmpdev->next;
					}
				}
				pnode = pnode->next;
			}
			sort_list();
			struct pname_t *ptmp = NULL;
			while(pname) {
				ptmp = pname;
				printf("\t %s\t\t",ptmp->name);
				if(strlen(ptmp->name) < 7)
					printf("\t");
				if(strlen(ptmp->name) < 15)
					printf("\t");
				printf("%s\n", ptmp->desc);
				FREE(ptmp->name);
				FREE(ptmp->desc);
				pname = pname->next;
				FREE(ptmp);
			}
			FREE(pname);
		}
		goto close;
	}

	code = json_mkobject();
	int itmp = 0;
	/* Check if we got sufficient arguments from this protocol */
	struct options_t *tmp = options;
	while(tmp) {
		if(strlen(tmp->name) > 0) {
			/* Only send the CLI arguments that belong to this protocol, the protocol name
			and those that are called by the user */
			if((options_get_id(&protocol->options, tmp->name, &itmp) == 0)
			    && tmp->vartype == JSON_STRING && tmp->string_ != NULL
				&& (strlen(tmp->string_) > 0)) {
				if(isNumeric(tmp->string_) == 0) {
					char *ptr = strstr(tmp->string_, ".");
					int decimals = 0;
					if(ptr != NULL) {
						decimals = (int)(strlen(tmp->string_)-((size_t)(ptr-tmp->string_)+1));
					}
					json_append_member(code, tmp->name, json_mknumber(atof(tmp->string_), decimals));
				} else {
					json_append_member(code, tmp->name, json_mkstring(tmp->string_));
				}
			}
			if(strcmp(tmp->name, "protocol") == 0 && strlen(tmp->string_) > 0) {
				JsonNode *jprotocol = json_mkarray();
				json_append_element(jprotocol, json_mkstring(tmp->string_));
				json_append_member(code, "protocol", jprotocol);
			}
		}
		tmp = tmp->next;
	}

	if(protocol->createCode(code) == 0) {
		if(protocol->message) {
			json_delete(protocol->message);
		}
		if(server && port > 0) {
			if((sockfd = socket_connect(server, port)) == -1) {
				logprintf(LOG_ERR, "could not connect to pilight-daemon");
				goto close;
			}
		} else if(ssdp_seek(&ssdp_list) == -1) {
			logprintf(LOG_ERR, "no pilight ssdp connections found");
			goto close;
		} else {
			if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1) {
				logprintf(LOG_ERR, "could not connect to pilight-daemon");
				goto close;
			}
		}
		if(ssdp_list) {
			ssdp_free(ssdp_list);
		}

		socket_write(sockfd, "{\"action\":\"identify\"}");
		if(socket_read(sockfd, &recvBuff, 0) != 0
		   || strcmp(recvBuff, "{\"status\":\"success\"}") != 0) {
			goto close;
		}

		JsonNode *json = json_mkobject();
		json_append_member(json, "action", json_mkstring("send"));
		if(uuid != NULL) {
			json_append_member(code, "uuid", json_mkstring(uuid));
		}
		json_append_member(json, "code", code);
		char *output = json_stringify(json, NULL);
		socket_write(sockfd, output);
		json_free(output);
		json_delete(json);

		if(socket_read(sockfd, &recvBuff, 0) != 0
		   || strcmp(recvBuff, "{\"status\":\"success\"}") != 0) {
			logprintf(LOG_ERR, "failed to send codes");
			goto close;
		}
	}
close:
	if(sockfd > 0) {
		socket_close(sockfd);
	}
	if(recvBuff != NULL) {
		FREE(recvBuff);
	}
	if(server != NULL) {
		FREE(server);
	}
	if(protobuffer != NULL) {
		FREE(protobuffer);
	}
	if(uuid != NULL) {
		FREE(uuid);
	}
	log_shell_disable();
	protocol_gc();
	options_delete(options);
	options_gc();
	config_gc();
	threads_gc();
	dso_gc();
	log_gc();
	FREE(progname);
	xfree();

	return EXIT_SUCCESS;
}