Esempio n. 1
0
int wiringXSetup(void) {
#ifndef _WIN32	
	if(wiringXLog == NULL) {
		wiringXLog = _fprintf;
	}

	if(wiringXSupported() == 0) {
		if(setup == -2) {
			hummingboardInit();
			raspberrypiInit();
			bananapiInit();
			ci20Init();
			radxaInit();
			odroidInit();

			int match = 0;
			struct platform_t *tmp = platforms;
			while(tmp) {
				if(tmp->identify() >= 0) {
					platform = tmp;
					match = 1;
					break;
				}
				tmp = tmp->next;
			}

			if(match == 0) {
				wiringXLog(LOG_ERR, "hardware not supported");
				wiringXGC();
				return -1;
			} else {
				wiringXLog(LOG_DEBUG, "running on a %s", platform->name);
			}
			setup = platform->setup();
			return setup;
		} else {
			return setup;
		}
	}
#endif
	return -1;
}
Esempio n. 2
0
static int createCode(JsonNode *code) {
	int free_def = 0;
	int gpio = -1;
	int state = -1;
	double itmp = -1;
	char *def = NULL;
	int have_error = 0;

	relay->rawlen = 0;
	if(json_find_string(code, "default-state", &def) != 0) {
		if((def = MALLOC(4)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(def, "off");
		free_def = 1;
	}

	if(json_find_number(code, "gpio", &itmp) == 0)
		gpio = (int)round(itmp);
	if(json_find_number(code, "off", &itmp) == 0)
		state=0;
	else if(json_find_number(code, "on", &itmp) == 0)
		state=1;

	if(gpio == -1 || state == -1) {
		logprintf(LOG_ERR, "relay: insufficient number of arguments");
		have_error = 1;
		goto clear;
	} else if(wiringXSupported() == 0) {
		if(wiringXSetup() < 0) {
			logprintf(LOG_ERR, "unable to setup wiringX") ;
			return EXIT_FAILURE;
		} else {
			if(wiringXValidGPIO(gpio) != 0) {
				logprintf(LOG_ERR, "relay: invalid gpio range");
				have_error = 1;
				goto clear;
			} else {
				if(strstr(progname, "daemon") != NULL) {
					pinMode(gpio, OUTPUT);
					if(strcmp(def, "off") == 0) {
						if(state == 1) {
							digitalWrite(gpio, LOW);
						} else if(state == 0) {
							digitalWrite(gpio, HIGH);
						}
					} else {
						if(state == 0) {
							digitalWrite(gpio, LOW);
						} else if(state == 1) {
							digitalWrite(gpio, HIGH);
						}
					}
				} else {
					wiringXGC();
				}
			createMessage(gpio, state);
			goto clear;
			}
		}
	}

clear:
	if(free_def == 1) {
		FREE(def);
	}
	if(have_error) {
		return EXIT_FAILURE;
	} else {
		return EXIT_SUCCESS;
	}
}
Esempio n. 3
0
static int checkValues(JsonNode *code) {
	char *def = NULL;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;
	int free_def = 0;
	double itmp = -1;

	if(json_find_string(code, "default-state", &def) != 0) {
		if((def = MALLOC(4)) == NULL) {
			fprintf(stderr, "out of memory\n");
			exit(EXIT_FAILURE);
		}
		strcpy(def, "off");
		free_def = 1;
	}
	if(strcmp(def, "on") != 0 && strcmp(def, "off") != 0) {
		if(free_def == 1) {
			FREE(def);
		}
		return 1;
	}

	/* Get current relay state and validate GPIO number */
	if((jid = json_find_member(code, "id")) != NULL) {
		if((jchild = json_find_element(jid, 0)) != NULL) {
			if(json_find_number(jchild, "gpio", &itmp) == 0) {
				if(wiringXSupported() == 0) {
					int gpio = (int)itmp;
					int state = -1;
					if(wiringXSetup() < 0) {
						logprintf(LOG_ERR, "unable to setup wiringX") ;
						return -1;
					} else if(wiringXValidGPIO(gpio) != 0) {
						logprintf(LOG_ERR, "relay: invalid gpio range");
						return -1;
					} else {
						pinMode(gpio, INPUT);
						state = digitalRead(gpio);
						if(strcmp(def, "on") == 0) {
							state ^= 1;
						}

						relay->message = json_mkobject();
						JsonNode *code = json_mkobject();
						json_append_member(code, "gpio", json_mknumber(gpio, 0));
						if(state == 1) {
							json_append_member(code, "state", json_mkstring("on"));
						} else {
							json_append_member(code, "state", json_mkstring("off"));
						}

						json_append_member(relay->message, "message", code);
						json_append_member(relay->message, "origin", json_mkstring("sender"));
						json_append_member(relay->message, "protocol", json_mkstring(relay->id));
						if(pilight.broadcast != NULL) {
							pilight.broadcast(relay->id, relay->message, PROTOCOL);
						}
						json_delete(relay->message);
						relay->message = NULL;
					}
				}
			}
		}
	}

	if(free_def == 1) {
		FREE(def);
	}
	return 0;
}