Esempio n. 1
0
static void *thread(void *param) {
	struct protocol_threads_t *node = (struct protocol_threads_t *)param;
	struct JsonNode *json = (struct JsonNode *)node->param;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;

#ifndef _WIN32
	struct dirent *file = NULL;
	struct stat st;

	DIR *d = NULL;
	FILE *fp = NULL;
	char crcVar[5];
	int w1valid = 0;
	double w1temp = 0.0;
	size_t bytes = 0;
#endif
	char **id = NULL, *stmp = NULL, *content = NULL;
	char *ds18s20_sensor = NULL;
	int nrid = 0, interval = 10, nrloops = 0, y = 0;
	double temp_offset = 0.0, itmp = 0.0;

	threads++;

	if((jid = json_find_member(json, "id"))) {
		jchild = json_first_child(jid);
		while(jchild) {
			if(json_find_string(jchild, "id", &stmp) == 0) {
				if((id = REALLOC(id, (sizeof(char *)*(size_t)(nrid+1)))) == NULL) {
					fprintf(stderr, "out of memory\n");
					exit(EXIT_FAILURE);
				}
				if((id[nrid] = MALLOC(strlen(stmp)+1)) == NULL) {
					fprintf(stderr, "out of memory\n");
					exit(EXIT_FAILURE);
				}
				strcpy(id[nrid], stmp);
				nrid++;
			}
			jchild = jchild->next;
		}
	}

	if(json_find_number(json, "poll-interval", &itmp) == 0)
		interval = (int)round(itmp);
	json_find_number(json, "temperature-offset", &temp_offset);

	while(loop) {
		if(protocol_thread_wait(node, interval, &nrloops) == ETIMEDOUT) {
#ifndef _WIN32
			pthread_mutex_lock(&lock);
			for(y=0;y<nrid;y++) {
				if((ds18s20_sensor = REALLOC(ds18s20_sensor, strlen(source_path)+strlen(id[y])+5)) == NULL) {
					fprintf(stderr, "out of memory\n");
					exit(EXIT_FAILURE);
				}
				sprintf(ds18s20_sensor, "%s10-%s/", source_path, id[y]);
				if((d = opendir(ds18s20_sensor))) {
					while((file = readdir(d)) != NULL) {
						if(file->d_type == DT_REG) {
							if(strcmp(file->d_name, "w1_slave") == 0) {
								size_t w1slavelen = strlen(ds18s20_sensor)+10;
								char ds18s20_w1slave[w1slavelen];
								memset(ds18s20_w1slave, '\0', w1slavelen);
								strncpy(ds18s20_w1slave, ds18s20_sensor, strlen(ds18s20_sensor));
								strcat(ds18s20_w1slave, "w1_slave");

								if(!(fp = fopen(ds18s20_w1slave, "rb"))) {
									logprintf(LOG_ERR, "cannot read w1 file: %s", ds18s20_w1slave);
									break;
								}

								fstat(fileno(fp), &st);
								bytes = (size_t)st.st_size;

								if(!(content = REALLOC(content, bytes+1))) {
									fprintf(stderr, "out of memory\n");
									fclose(fp);
									break;
								}
								memset(content, '\0', bytes+1);

								if(fread(content, sizeof(char), bytes, fp) == -1) {
									logprintf(LOG_ERR, "cannot read config file: %s", ds18s20_w1slave);
									fclose(fp);
									break;
								}
								fclose(fp);
								w1valid = 0;

								char **array = NULL;
								unsigned int n = explode(content, "\n", &array);
								if(n > 0) {
									sscanf(array[0], "%*x %*x %*x %*x %*x %*x %*x %*x %*x : crc=%*x %s", crcVar);
									if(strncmp(crcVar, "YES", 3) == 0 && n > 1) {
										w1valid = 1;
										sscanf(array[1], "%*x %*x %*x %*x %*x %*x %*x %*x %*x t=%lf", &w1temp);
										w1temp = (w1temp/1000)+temp_offset;
									}
								}
								array_free(&array, n);

								if(w1valid) {
									ds18s20->message = json_mkobject();

									JsonNode *code = json_mkobject();

									json_append_member(code, "id", json_mkstring(id[y]));
									json_append_member(code, "temperature", json_mknumber(w1temp, 1));

									json_append_member(ds18s20->message, "message", code);
									json_append_member(ds18s20->message, "origin", json_mkstring("receiver"));
									json_append_member(ds18s20->message, "protocol", json_mkstring(ds18s20->id));

									if(pilight.broadcast != NULL) {
										pilight.broadcast(ds18s20->id, ds18s20->message, PROTOCOL);
									}
									json_delete(ds18s20->message);
									ds18s20->message = NULL;
								}
							}
						}
					}
					closedir(d);
				} else {
					logprintf(LOG_ERR, "1-wire device %s does not exists", ds18s20_sensor);
				}
			}
#endif
			pthread_mutex_unlock(&lock);
		}
	}
	pthread_mutex_unlock(&lock);

	if(ds18s20_sensor) {
		FREE(ds18s20_sensor);
	}
	if(content) {
		FREE(content);
	}
	for(y=0;y<nrid;y++) {
		FREE(id[y]);
	}
	FREE(id);
	threads--;
	return (void *)NULL;
}
Esempio n. 2
0
static void *dht22Parse(void *param) {
	struct protocol_threads_t *node = (struct protocol_threads_t *)param;
	struct JsonNode *json = (struct JsonNode *)node->param;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;
	int *id = 0;
	int nrid = 0, y = 0, interval = 10, nrloops = 0;
	double temp_offset = 0.0, humi_offset = 0.0, itmp = 0.0;

	dht22_threads++;

	if((jid = json_find_member(json, "id"))) {
		jchild = json_first_child(jid);
		while(jchild) {
			if(json_find_number(jchild, "gpio", &itmp) == 0) {
				id = REALLOC(id, (sizeof(int)*(size_t)(nrid+1)));
				id[nrid] = (int)round(itmp);
				nrid++;
			}
			jchild = jchild->next;
		}
	}

	if(json_find_number(json, "poll-interval", &itmp) == 0)
		interval = (int)round(itmp);
	json_find_number(json, "temperature-offset", &temp_offset);
	json_find_number(json, "humidity-offset", &humi_offset);

	while(dht22_loop) {
		if(protocol_thread_wait(node, interval, &nrloops) == ETIMEDOUT) {
			pthread_mutex_lock(&dht22lock);
			for(y=0;y<nrid;y++) {
				int tries = 5;
				unsigned short got_correct_date = 0;
				while(tries && !got_correct_date && dht22_loop) {

					uint8_t laststate = HIGH;
					uint8_t counter = 0;
					uint8_t j = 0, i = 0;

					int dht22_dat[5] = {0,0,0,0,0};

					// pull pin down for 18 milliseconds
					pinMode(id[y], OUTPUT);
					digitalWrite(id[y], HIGH);
					usleep(500000);  // 500 ms
					// then pull it up for 40 microseconds
					digitalWrite(id[y], LOW);
					usleep(20000);
					// prepare to read the pin
					pinMode(id[y], INPUT);

					// detect change and read data
					for(i=0; (i<MAXTIMINGS && dht22_loop); i++) {
						counter = 0;
						delayMicroseconds(10);
						while(sizecvt(digitalRead(id[y])) == laststate && dht22_loop) {
							counter++;
							delayMicroseconds(1);
							if(counter == 255) {
								break;
							}
						}
						laststate = sizecvt(digitalRead(id[y]));

						if(counter == 255)
							break;

						// ignore first 3 transitions
						if((i >= 4) && (i%2 == 0)) {

							// shove each bit into the storage bytes
							dht22_dat[(int)((double)j/8)] <<= 1;
							if(counter > 16)
								dht22_dat[(int)((double)j/8)] |= 1;
							j++;
						}
					}

					// check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
					// print it out if data is good
					if((j >= 40) && (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF))) {
						got_correct_date = 1;

						double h = dht22_dat[0] * 256 + dht22_dat[1];
						double t = (dht22_dat[2] & 0x7F)* 256 + dht22_dat[3];
						t += temp_offset;
						h += humi_offset;

						if((dht22_dat[2] & 0x80) != 0)
							t *= -1;

						dht22->message = json_mkobject();
						JsonNode *code = json_mkobject();
						json_append_member(code, "gpio", json_mknumber(id[y], 0));
						json_append_member(code, "temperature", json_mknumber(t/10, 1));
						json_append_member(code, "humidity", json_mknumber(h/10, 1));

						json_append_member(dht22->message, "message", code);
						json_append_member(dht22->message, "origin", json_mkstring("receiver"));
						json_append_member(dht22->message, "protocol", json_mkstring(dht22->id));

						if(pilight.broadcast != NULL) {
							pilight.broadcast(dht22->id, dht22->message);
						}
						json_delete(dht22->message);
						dht22->message = NULL;
					} else {
						logprintf(LOG_DEBUG, "dht22 data checksum was wrong");
						tries--;
						protocol_thread_wait(node, 1, &nrloops);
					}
				}
			}
			pthread_mutex_unlock(&dht22lock);
		}
	}
	pthread_mutex_unlock(&dht22lock);

	FREE(id);
	dht22_threads--;
	return (void *)NULL;
}
Esempio n. 3
0
void *ds18b20Parse(void *param) {
	struct protocol_threads_t *node = (struct protocol_threads_t *)param;
	struct JsonNode *json = (struct JsonNode *)node->param;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;
	struct dirent *file = NULL;
	struct stat st;

	DIR *d = NULL;
	FILE *fp = NULL;
	char *ds18b20_sensor = NULL;
	char *stmp = NULL;
	char **id = NULL;
	char *content = NULL;
	int w1valid = 0, w1temp = 0, interval = 10, x = 0;
	int temp_offset = 0, nrid = 0, y = 0, nrloops = 0;
	double itmp = 0;
	size_t bytes = 0;

	ds18b20_threads++;

	if((jid = json_find_member(json, "id"))) {
		jchild = json_first_child(jid);
		while(jchild) {
			if(json_find_string(jchild, "id", &stmp) == 0) {
				id = realloc(id, (sizeof(char *)*(size_t)(nrid+1)));
				if(!id) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				id[nrid] = malloc(strlen(stmp)+1);
				if(!id[nrid]) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				strcpy(id[nrid], stmp);
				nrid++;
			}
			jchild = jchild->next;
		}
	}

	if(json_find_number(json, "poll-interval", &itmp) == 0)
		interval = (int)round(itmp);
	if(json_find_number(json, "device-temperature-offset", &itmp) == 0)
		temp_offset = (int)round(itmp);

	while(ds18b20_loop) {
		if(protocol_thread_wait(node, interval, &nrloops) == ETIMEDOUT) {
			for(y=0;y<nrid;y++) {
				ds18b20_sensor = realloc(ds18b20_sensor, strlen(ds18b20_path)+strlen(id[y])+5);
				if(!ds18b20_sensor) {
					logprintf(LOG_ERR, "out of memory");
					exit(EXIT_FAILURE);
				}
				sprintf(ds18b20_sensor, "%s28-%s/", ds18b20_path, id[y]);
				if((d = opendir(ds18b20_sensor))) {
					while((file = readdir(d)) != NULL) {
						if(file->d_type == DT_REG) {
							if(strcmp(file->d_name, "w1_slave") == 0) {
								size_t w1slavelen = strlen(ds18b20_sensor)+10;
								char ds18b20_w1slave[w1slavelen];
								memset(ds18b20_w1slave, '\0', w1slavelen);
								strncpy(ds18b20_w1slave, ds18b20_sensor, strlen(ds18b20_sensor));
								strcat(ds18b20_w1slave, "w1_slave");

								if(!(fp = fopen(ds18b20_w1slave, "rb"))) {
									logprintf(LOG_ERR, "cannot read w1 file: %s", ds18b20_w1slave);
									break;
								}

								fstat(fileno(fp), &st);
								bytes = (size_t)st.st_size;

								if(!(content = realloc(content, bytes+1))) {
									logprintf(LOG_ERR, "out of memory");
									fclose(fp);
									break;
								}
								memset(content, '\0', bytes+1);

								if(fread(content, sizeof(char), bytes, fp) == -1) {
									logprintf(LOG_ERR, "cannot read config file: %s", ds18b20_w1slave);
									fclose(fp);
									break;
								}
								fclose(fp);
								w1valid = 0;
								char *pch = strtok(content, "\n=: ");
								x = 0;
								while(pch) {
									if(strlen(pch) > 2) {
										if(x == 1 && strstr(pch, "YES")) {
											w1valid = 1;
										}
										if(x == 2) {	
											w1temp = atoi(pch)+temp_offset;
										}
										x++;
									}
									pch = strtok(NULL, "\n=: ");
								}

								if(w1valid) {
									ds18b20->message = json_mkobject();
									
									JsonNode *code = json_mkobject();
									
									json_append_member(code, "id", json_mkstring(id[y]));
									json_append_member(code, "temperature", json_mknumber(w1temp));
									
									json_append_member(ds18b20->message, "message", code);
									json_append_member(ds18b20->message, "origin", json_mkstring("receiver"));
									json_append_member(ds18b20->message, "protocol", json_mkstring(ds18b20->id));
									
									pilight.broadcast(ds18b20->id, ds18b20->message);
									json_delete(ds18b20->message);
									ds18b20->message = NULL;
								}
							}
						}
					}
					closedir(d);
				} else {
					logprintf(LOG_ERR, "1-wire device %s does not exists", ds18b20_sensor);
				}
			}
		}
	}
	if(ds18b20_sensor) {
		sfree((void *)&ds18b20_sensor);
	}
	if(content) {
		sfree((void *)&content);
	}
	for(y=0;y<nrid;y++) {
		sfree((void *)&id[y]);
	}
	sfree((void *)&id);
	ds18b20_threads--;
	return (void *)NULL;
}
Esempio n. 4
0
void *lircParse(void *param) {
	struct protocol_threads_t *node = (struct protocol_threads_t *)param;
	struct sockaddr_un addr;
	struct timeval timeout;
	
	int nrloops = 0, n = -1, bytes = 0;
	char recvBuff[BUFFER_SIZE];
	fd_set fdsread;	
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;

	/* Clear the server address */
    memset(&addr, '\0', sizeof(addr));
	memset(&recvBuff, '\0', BUFFER_SIZE);
	
	lirc_threads++;

	while(lirc_loop) {
		if(lirc_sockfd > -1) {
			close(lirc_sockfd);
			lirc_sockfd = -1;
		}

		if(path_exists(lirc_socket) == EXIT_SUCCESS) {
			/* Try to open a new socket */
			if((lirc_sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
				logprintf(LOG_DEBUG, "could not create Lirc socket");
				break;
			}

			addr.sun_family=AF_UNIX;
			strcpy(addr.sun_path, lirc_socket);
			
			/* Connect to the server */
			if(connect(lirc_sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
				protocol_thread_wait(node, 3, &nrloops);
				continue;
			}

			while(lirc_loop) {
				FD_ZERO(&fdsread);
				FD_SET((unsigned long)lirc_sockfd, &fdsread);

				do {
					n = select(lirc_sockfd+1, &fdsread, NULL, NULL, &timeout);
				} while(n == -1 && errno == EINTR && lirc_loop);

				if(lirc_loop == 0) {
					break;
				}
				
				if(path_exists(lirc_socket) == EXIT_FAILURE) {
					break;
				}

				if(n == -1) {
					break;
				} else if(n == 0) {
					usleep(10000);
				} else if(n > 0) {
					if(FD_ISSET((unsigned long)lirc_sockfd, &fdsread)) {
						bytes = (int)recv(lirc_sockfd, recvBuff, BUFFER_SIZE, 0);
						if(bytes <= 0) {
							break;
						} else {
							int x = 0, nrspace = 0;
							for(x=0;x<bytes;x++) {
								if(recvBuff[x] == ' ') {
									nrspace++;
								}
							}
							if(nrspace >= 3) {
								char *id = strtok(recvBuff, " ");
								char *rep = strtok(NULL, " ");
								char *btn = strtok(NULL, " ");
								char *remote = strtok(NULL, " ");
								if(strstr(remote, "\n") != NULL) {
									remote[strlen(remote)-1] = '\0';
								}
							
								lirc->message = json_mkobject();
								JsonNode *code = json_mkobject();
								json_append_member(code, "id", json_mkstring(id));
								json_append_member(code, "repeat", json_mkstring(rep));
								json_append_member(code, "button", json_mkstring(btn));
								json_append_member(code, "remote", json_mkstring(remote));
													
								json_append_member(lirc->message, "message", code);
								json_append_member(lirc->message, "origin", json_mkstring("receiver"));
								json_append_member(lirc->message, "protocol", json_mkstring(lirc->id));
													
								pilight.broadcast(lirc->id, lirc->message);
								json_delete(lirc->message);
								lirc->message = NULL;
							}
							memset(recvBuff, '\0', BUFFER_SIZE);
						}
					}
				}
			}
		}
	}

	if(lirc_sockfd > -1) {
		close(lirc_sockfd);
		lirc_sockfd = -1;
	}
	
	lirc_threads--;
	return (void *)NULL;
}
Esempio n. 5
0
void *programParse(void *param) {
	struct protocol_threads_t *pnode = (struct protocol_threads_t *)param;
	struct JsonNode *json = (struct JsonNode *)pnode->param;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;
	struct JsonNode *jchild1 = NULL;
	char *prog = NULL, *args = NULL, *stopcmd = NULL, *startcmd = NULL;
	
	int interval = 1, nrloops = 0, currentstate = 0, laststate = -1;
	int pid = 0;
	double itmp = 0;

	program_threads++;

	json_find_string(json, "program", &prog);	
	json_find_string(json, "arguments", &args);
	json_find_string(json, "stop-command", &stopcmd);
	json_find_string(json, "start-command", &startcmd);
	
	struct programs_t *lnode = malloc(sizeof(struct programs_t));	
	lnode->wait = 0;
	lnode->pth = 0;
	
	if(args && strlen(args) > 0) {
		if(!(lnode->arguments = malloc(strlen(args)+1))) {
			logprintf(LOG_ERR, "out of memory");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->arguments, args);
	} else {
		lnode->arguments = NULL;
	}

	if(prog) {
		if(!(lnode->program = malloc(strlen(prog)+1))) {
			logprintf(LOG_ERR, "out of memory");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->program, prog);
	} else {
		lnode->program = NULL;
	}

	if(stopcmd) {
		if(!(lnode->stop = malloc(strlen(stopcmd)+1))) {
			logprintf(LOG_ERR, "out of memory");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->stop, stopcmd);
	} else {
		lnode->stop = NULL;
	}

	if(startcmd) {
		if(!(lnode->start = malloc(strlen(startcmd)+1))) {
			logprintf(LOG_ERR, "out of memory");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->start, startcmd);
	} else {
		lnode->start = NULL;
	}

	if((jid = json_find_member(json, "id"))) {
		jchild = json_first_child(jid);
		while(jchild) {
			jchild1 = json_first_child(jchild);
			while(jchild1) {
				if(strcmp(jchild1->key, "name") == 0) {
					if(!(lnode->name = malloc(strlen(jchild1->string_)+1))) {
						logprintf(LOG_ERR, "out of memory");
						exit(EXIT_FAILURE);
					}
					strcpy(lnode->name, jchild1->string_);
				}
				jchild1 = jchild1->next;
			}
			jchild = jchild->next;
		}
	}

	struct programs_t *tmp = programs;
	if(tmp) {
		while(tmp->next != NULL) {
			tmp = tmp->next;
		}
		tmp->next = lnode;
	} else {
		lnode->next = tmp;
		programs = lnode;
	}
	
	if(json_find_number(json, "poll-interval", &itmp) == 0)
		interval = (int)round(itmp);

	while(program_loop) {
		if(protocol_thread_wait(pnode, interval, &nrloops) == ETIMEDOUT) {
			if(lnode->wait == 0) {
				program->message = json_mkobject();

				JsonNode *code = json_mkobject();
				json_append_member(code, "name", json_mkstring(lnode->name));

				if((pid = (int)findproc(lnode->program, lnode->arguments)) > 0) {
					currentstate = 1;
					json_append_member(code, "state", json_mkstring("running"));
					json_append_member(code, "pid", json_mknumber((int)pid));
				} else {
					currentstate = 0;
					json_append_member(code, "state", json_mkstring("stopped"));
					json_append_member(code, "pid", json_mknumber(0));
				}

				json_append_member(program->message, "message", code);
				json_append_member(program->message, "origin", json_mkstring("receiver"));
				json_append_member(program->message, "protocol", json_mkstring(program->id));

				if(currentstate != laststate) {
					laststate = currentstate;									
					pilight.broadcast(program->id, program->message);
				}

				json_delete(program->message);
				program->message = NULL;
			}
		}
	}

	program_threads--;
	return (void *)NULL;
}
Esempio n. 6
0
void *xbmcParse(void *param) {
	struct protocol_threads_t *node = (struct protocol_threads_t *)param;
	struct JsonNode *json = (struct JsonNode *)node->param;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;
	struct JsonNode *jchild1 = NULL;
	struct sockaddr_in serv_addr;
	struct xbmc_data_t *xnode = malloc(sizeof(struct xbmc_data_t));

	char recvBuff[BUFFER_SIZE], action[10], media[15];
	char *m = NULL, *t = NULL;
	char shut[] = "shutdown";
	char home[] = "home";
	char none[] = "none";
	int nrloops = 0, bytes = 0, n = 0, has_server = 0;
	int has_port = 0, reset = 1, maxfd = 0;
	fd_set fdsread;	
	struct timeval timeout;
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;

	if(!xnode) {
		logprintf(LOG_ERR, "out of memory");
		exit(EXIT_FAILURE);
	}	
	
	/* Clear the server address */
    memset(&serv_addr, '\0', sizeof(serv_addr));
	memset(&recvBuff, '\0', BUFFER_SIZE);
	memset(&action, '\0', 10);
	memset(&media, '\0', 15);
	
	xbmc_threads++;

	if((jid = json_find_member(json, "id"))) {
		jchild = json_first_child(jid);
		while(jchild) {
			jchild1 = json_first_child(jchild);

			while(jchild1) {
				if(strcmp(jchild1->key, "server") == 0) {
					if(!(xnode->server = malloc(strlen(jchild1->string_)+1))) {
						logprintf(LOG_ERR, "out of memory");
						exit(EXIT_FAILURE);
					}
					strcpy(xnode->server, jchild1->string_);
					has_server = 1;
				}
				if(strcmp(jchild1->key, "port") == 0) {
					xnode->port = (int)round(jchild1->number_);
					has_port = 1;
				}
				jchild1 = jchild1->next;
			}
			if(has_server == 1 && has_port == 1) {
				xnode->sockfd = -1;
				xnode->next = xbmc_data;
				xbmc_data = xnode;
			} else {
				if(has_server == 1) {
					sfree((void *)&xnode->server);
				}
				sfree((void *)&xnode);
				xnode = NULL;
			}
			jchild = jchild->next;
		}
	}

	if(!xnode) {
		return 0;
	}

	while(xbmc_loop) {

		if(reset == 1) {
			xbmcCreateMessage(xnode->server, xnode->port, shut, none);
			reset = 0;
		}

		if(xnode->sockfd > -1) {
			close(xnode->sockfd);
			xnode->sockfd = -1;
		}

		/* Try to open a new socket */
		if((xnode->sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
			logprintf(LOG_DEBUG, "could not create XBMC socket");
			break;
		}

		serv_addr.sin_family = AF_INET;
		serv_addr.sin_port = htons((unsigned short)xnode->port);
		inet_pton(AF_INET, xnode->server, &serv_addr.sin_addr);	
		
		/* Connect to the server */
		if(connect(xnode->sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
			protocol_thread_wait(node, 3, &nrloops);
			continue;
		} else {
			xbmcCreateMessage(xnode->server, xnode->port, home, none);
			reset = 1;
		}

		struct xbmc_data_t *xtmp = xbmc_data;
		while(xtmp) {
			if(xtmp->sockfd > -1) {
				if(maxfd < xtmp->sockfd) {
					maxfd = xtmp->sockfd;
				}
			}
			xtmp = xtmp->next;
		}
		
		while(xbmc_loop) {
			FD_ZERO(&fdsread);
			FD_SET((unsigned long)xnode->sockfd, &fdsread);

			do {
				n = select(maxfd+1, &fdsread, NULL, NULL, &timeout);
			} while(n == -1 && errno == EINTR && xbmc_loop);

			if(xbmc_loop == 0) {
				break;
			}

			if(n == -1) {			
				break;
			} else if(n == 0) {
				usleep(10000);
			} else if(n > 0) {
				if(FD_ISSET((unsigned long)xnode->sockfd, &fdsread)) {
					bytes = (int)recv(xnode->sockfd, recvBuff, BUFFER_SIZE, 0);
					if(bytes <= 0) {
						break;
					} else {
						if(json_validate(recvBuff) == true) {
							JsonNode *joutput = json_decode(recvBuff);
							JsonNode *params = NULL;
							JsonNode *data = NULL;
							JsonNode *item = NULL;

							if(json_find_string(joutput, "method", &m) == 0) {
								if(strcmp(m, "GUI.OnScreensaverActivated") == 0) {
									strcpy(media, "screensaver");
									strcpy(action, "active");
								} else if(strcmp(m, "GUI.OnScreensaverDeactivated") == 0) {
									strcpy(media, "screensaver");
									strcpy(action, "inactive");
								} else {
									if((params = json_find_member(joutput, "params")) != NULL) {
										if((data = json_find_member(params, "data")) != NULL) {
											if((item = json_find_member(data, "item")) != NULL) {
												if(json_find_string(item, "type", &t) == 0) {
													xbmc->message = json_mkobject();

													strcpy(media, t);
													if(strcmp(m, "Player.OnPlay") == 0) {
														strcpy(action, "play");
													} else if(strcmp(m, "Player.OnStop") == 0) {
														strcpy(action, home);
														strcpy(media, none);
													} else if(strcmp(m, "Player.OnPause") == 0) {
														strcpy(action, "pause");
													}
												}
											}
										}
									}
								}
								if(strlen(media) > 0 && strlen(action) > 0) {
									xbmcCreateMessage(xnode->server, xnode->port, action, media);
									reset = 1;
								}							
							}
							json_delete(joutput);
						}
						memset(recvBuff, '\0', BUFFER_SIZE);
						memset(&action, '\0', 10);
						memset(&media, '\0', 15);
					}
				}
			}
		}
	}	
	
	xbmc_threads--;
	return (void *)NULL;
}
Esempio n. 7
0
static void *thread(void *param) {
	struct protocol_threads_t *pnode = (struct protocol_threads_t *)param;
	struct JsonNode *json = (struct JsonNode *)pnode->param;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;
	char *state = NULL, *onquery = NULL, *offquery = NULL, *onsuccess = NULL, *offsuccess = NULL;
	char *errresponse = NULL, *onuri = NULL, *offuri = NULL, *response = NULL, *method = NULL;
	int interval = 1, nrloops = 0;
	double itmp = 0;

	threads++;

	json_find_string(json, "method", &method);
	json_find_string(json, "on_uri", &onuri);
	json_find_string(json, "off_uri", &offuri);
	json_find_string(json, "on_query", &onquery);
	json_find_string(json, "off_query", &offquery);
	json_find_string(json, "on_success", &onsuccess);
	json_find_string(json, "off_success", &offsuccess);
	json_find_string(json, "err_response", &errresponse);
	json_find_string(json, "response", &response);
	json_find_string(json, "state", &state);

	struct settings_t *lnode = MALLOC(sizeof(struct settings_t));
	lnode->wait = 0;
	lnode->hasthread = 0;
	memset(&lnode->pth, '\0', sizeof(pthread_t));

	if(strcmp(state, "running") == 0) {
		lnode->newstate = 1;
	} else {
		lnode->newstate = 0;
	}
	
	lnode->id = 0;
	if((jid = json_find_member(json, "id"))) {
		jchild = json_first_child(jid);
		if(json_find_number(jchild, "id", &itmp) == 0)
			lnode->id = (int)round(itmp);
	}

	if(strcmp(method, "GET") != 0 && strcmp(method, "POST") != 0) {
		logprintf(LOG_ERR, "Webswitch %i: method must be either \"GET\" or \"POST\"", lnode->id);
		exit(EXIT_FAILURE);
	}
	
	
	if(method != NULL && strlen(method) > 0) {
		if((lnode->method = MALLOC(strlen(method)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->method, method);
	} else {
		lnode->method = NULL;
	}
	
	if(onuri != NULL && strlen(onuri) > 0) {
		if((lnode->on_uri = MALLOC(strlen(onuri)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->on_uri, onuri);
	} else {
		lnode->on_uri = NULL;
	}
	
	if(offuri != NULL && strlen(offuri) > 0) {
		if((lnode->off_uri = MALLOC(strlen(offuri)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->off_uri, offuri);
	} else {
		lnode->off_uri = NULL;
	}
	
	if(onquery != NULL && strlen(onquery) > 0) {
		if((lnode->on_query = MALLOC(strlen(onquery)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->on_query, onquery);
	} else {
		lnode->on_query = NULL;
	}

	if(offquery != NULL && strlen(offquery) > 0) {
		if((lnode->off_query = MALLOC(strlen(offquery)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->off_query, offquery);
	} else {
		lnode->off_query = NULL;
	}

	if(onsuccess != NULL && strlen(onsuccess) > 0) {
		if((lnode->on_success = MALLOC(strlen(onsuccess)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->on_success, onsuccess);
	} else {
		lnode->on_success = NULL;
	}
	
	if(offsuccess != NULL && strlen(offsuccess) > 0) {
		if((lnode->off_success = MALLOC(strlen(offsuccess)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->off_success, offsuccess);
	} else {
		lnode->off_success = NULL;
	}

	if(errresponse != NULL && strlen(errresponse) > 0) {
		if((lnode->err_response = MALLOC(strlen(errresponse)+1)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->err_response, errresponse);
	} else {
		lnode->err_response = NULL;
	}

	if(response != NULL && strlen(response) > 0) {
		if((lnode->response = MALLOC(strlen(response)+1)) == NULL) {
		//if((lnode->response = MALLOC(BUFFER_SIZE)) == NULL) {
		fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(lnode->response, response);
	} else {
		lnode->response = NULL;
	}

	lnode->thread = pnode;
	lnode->laststate = -1;
	lnode->currentstate = -1;

	lnode->next = settings;
	settings = lnode;

	if(json_find_number(json, "poll-interval", &itmp) == 0)
		interval = (int)round(itmp);

	while(loop) {

	if(protocol_thread_wait(pnode, interval, &nrloops) == ETIMEDOUT) {
			pthread_mutex_lock(&lock);
			if(lnode->wait == 0) {
				struct JsonNode *message = json_mkobject();

				JsonNode *code = json_mkobject();
				json_append_member(code, "id", json_mknumber(lnode->id, 0));
				json_append_member(code, "response", json_mkstring(lnode->response));
				if(lnode->currentstate == -1 && lnode->laststate == -1) {
					lnode->laststate=lnode->newstate;
				}
				if(lnode->newstate == 1) {
					lnode->currentstate = 1;
					json_append_member(code, "state", json_mkstring("running"));
				} else {
					lnode->currentstate = 0;
					json_append_member(code, "state", json_mkstring("stopped"));
				}
				json_append_member(message, "message", code);
				json_append_member(message, "origin", json_mkstring("receiver"));
				json_append_member(message, "protocol", json_mkstring(webswitch->id));

				if(lnode->currentstate != lnode->laststate) {
					lnode->laststate = lnode->currentstate;

					if(pilight.broadcast != NULL ) {
						pilight.broadcast(webswitch->id, message, PROTOCOL);
					}
				}
				json_delete(message);
				message = NULL;
			}
			pthread_mutex_unlock(&lock);
		}
	}
	pthread_mutex_unlock(&lock);

	threads--;
	return (void *)NULL;
}