static int checkValues(struct JsonNode *code) { int dimlevel = -1; int max = 15; int min = 0; double itmp = -1; if(json_find_number(code, "dimlevel-maximum", &itmp) == 0) max = (int)round(itmp); if(json_find_number(code, "dimlevel-minimum", &itmp) == 0) min = (int)round(itmp); if(json_find_number(code, "dimlevel", &itmp) == 0) dimlevel = (int)round(itmp); if(min > max) { return 1; } if(dimlevel != -1) { if(dimlevel < min || dimlevel > max) { return 1; } else { return 0; } } return 0; }
static int createCode(struct JsonNode *code) { int id = -1; int unit = -1; int state = -1; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "down", &itmp) == 0) state=0; else if(json_find_number(code, "up", &itmp) == 0) state=1; if(id == -1 || unit == -1 || state == -1) { logprintf(LOG_ERR, "arctech_screen_old: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 31 || id < 0) { logprintf(LOG_ERR, "arctech_screen_old: invalid id range"); return EXIT_FAILURE; } else if(unit > 15 || unit < 0) { logprintf(LOG_ERR, "arctech_screen_old: invalid unit range"); return EXIT_FAILURE; } else { createMessage(id, unit, state); clearCode(); createUnit(unit); createId(id); createState(state); createFooter(); arctech_screen_old->rawlen = RAW_LENGTH; } return EXIT_SUCCESS; }
static int createCode(struct JsonNode *code) { int systemcode = -1; int programcode = -1; int state = -1; double itmp = 0; if(json_find_number(code, "systemcode", &itmp) == 0) systemcode = (int)round(itmp); if(json_find_number(code, "programcode", &itmp) == 0) programcode = (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(systemcode == -1 || programcode == -1 || state == -1) { logprintf(LOG_ERR, "rsl366: insufficient number of arguments"); return EXIT_FAILURE; } else if(systemcode > 4 || systemcode < 0) { logprintf(LOG_ERR, "rsl366: invalid systemcode range"); return EXIT_FAILURE; } else if(programcode > 4 || programcode < 0) { logprintf(LOG_ERR, "rsl366: invalid programcode range"); return EXIT_FAILURE; } else { createMessage(systemcode, programcode, state); clearCode(); createSystemCode(systemcode); createProgramCode(programcode); createState(state); createFooter(); rsl366->rawlen = RAW_LENGTH; } return EXIT_SUCCESS; }
static int elroHECreateCode(JsonNode *code) { int systemcode = -1; int unitcode = -1; int state = -1; double itmp = 0; if(json_find_number(code, "systemcode", &itmp) == 0) systemcode = (int)round(itmp); if(json_find_number(code, "unitcode", &itmp) == 0) unitcode = (int)round(itmp); if(json_find_number(code, "off", &itmp) == 0) state=1; else if(json_find_number(code, "on", &itmp) == 0) state=0; if(systemcode == -1 || unitcode == -1 || state == -1) { logprintf(LOG_ERR, "elro_he: insufficient number of arguments"); return EXIT_FAILURE; } else if(systemcode > 31 || systemcode < 0) { logprintf(LOG_ERR, "elro_he: invalid systemcode range"); return EXIT_FAILURE; } else if(unitcode > 31 || unitcode < 0) { logprintf(LOG_ERR, "elro_he: invalid unitcode range"); return EXIT_FAILURE; } else { elroHECreateMessage(systemcode, unitcode, state); elroHEClearCode(); elroHECreateSystemCode(systemcode); elroHECreateUnitCode(unitcode); elroHECreateState(state); elroHECreateFooter(); } return EXIT_SUCCESS; }
static int selectremoteCreateCode(JsonNode *code) { int id = -1; int state = -1; double itmp = 0; if(json_find_number(code, "id", &itmp) == 0) id = (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(id == -1 || state == -1) { logprintf(LOG_ERR, "selectremote: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 7 || id < 0) { logprintf(LOG_ERR, "selectremote: invalid id range"); return EXIT_FAILURE; } else { selectremoteCreateMessage(id, state); selectremoteClearCode(); selectremoteCreateId(id); selectremoteCreateState(state); selectremoteCreateFooter(); } return EXIT_SUCCESS; }
static int arctechSwOldCreateCode(JsonNode *code) { int id = -1; int unit = -1; int state = -1; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (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(id == -1 || unit == -1 || state == -1) { logprintf(LOG_ERR, "arctech_switch_old: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 31 || id < 0) { logprintf(LOG_ERR, "arctech_switch_old: invalid id range"); return EXIT_FAILURE; } else if(unit > 15 || unit < 0) { logprintf(LOG_ERR, "arctech_switch_old: invalid unit range"); return EXIT_FAILURE; } else { arctechSwOldCreateMessage(id, unit, state); arctechSwOldClearCode(); arctechSwOldCreateUnit(unit); arctechSwOldCreateId(id); arctechSwOldCreateState(state); arctechSwOldCreateFooter(); } return EXIT_SUCCESS; }
static int x10CreateCode(JsonNode *code) { char id[4] = {'\0'}; int state = -1; double itmp = -1; char *stmp = NULL; strcpy(id, "-1"); if(json_find_string(code, "id", &stmp) == 0) strcpy(id, stmp); if(json_find_number(code, "off", &itmp) == 0) state=1; else if(json_find_number(code, "on", &itmp) == 0) state=0; if(strcmp(id, "-1") == 0 || state == -1) { logprintf(LOG_ERR, "x10: insufficient number of arguments"); return EXIT_FAILURE; } else if((int)(id[0]) < 65 || (int)(id[0]) > 80) { logprintf(LOG_ERR, "x10: invalid id range"); return EXIT_FAILURE; } else if(atoi(&id[1]) < 0 || atoi(&id[1]) > 16) { logprintf(LOG_ERR, "x10: invalid id range"); return EXIT_FAILURE; } else { x10CreateMessage(id, state); x10ClearCode(); x10CreateLetter((int)id[0]); x10CreateNumber(atoi(&id[1])); x10CreateState(state); x10CreateFooter(); } return EXIT_SUCCESS; }
int impulsCreateCode(JsonNode *code) { int systemcode = -1; int programcode = -1; int state = -1; int tmp; json_find_number(code, "systemcode", &systemcode); json_find_number(code, "programcode", &programcode); if(json_find_number(code, "off", &tmp) == 0) state=0; else if(json_find_number(code, "on", &tmp) == 0) state=1; if(systemcode == -1 || programcode == -1 || state == -1) { logprintf(LOG_ERR, "impuls: insufficient number of arguments"); return EXIT_FAILURE; } else if(systemcode > 31 || systemcode < 0) { logprintf(LOG_ERR, "impuls: invalid systemcode range"); return EXIT_FAILURE; } else if(programcode > 31 || programcode < 0) { logprintf(LOG_ERR, "impuls: invalid programcode range"); return EXIT_FAILURE; } else { impulsCreateMessage(systemcode, programcode, state); impulsClearCode(); impulsCreateSystemCode(systemcode); impulsCreateProgramCode(programcode); impulsCreateState(state); impulsCreateFooter(); } return EXIT_SUCCESS; }
static int checkValues(struct JsonNode *jvalues) { struct JsonNode *jid = NULL; if((jid = json_find_member(jvalues, "id"))) { struct settings_t *snode = NULL; struct JsonNode *jchild = NULL; struct JsonNode *jchild1 = NULL; double channel = -1, id = -1; int match = 0; jchild = json_first_child(jid); while(jchild) { jchild1 = json_first_child(jchild); while(jchild1) { if(strcmp(jchild1->key, "channel") == 0) { channel = jchild1->number_; } if(strcmp(jchild1->key, "id") == 0) { id = jchild1->number_; } jchild1 = jchild1->next; } jchild = jchild->next; } struct settings_t *tmp = settings; while(tmp) { if(fabs(tmp->id-id) < EPSILON && fabs(tmp->channel-channel) < EPSILON) { match = 1; break; } tmp = tmp->next; } if(match == 0) { if((snode = MALLOC(sizeof(struct settings_t))) == NULL) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } snode->id = id; snode->channel = channel; snode->temp = 0; snode->humi = 0; json_find_number(jvalues, "temperature-offset", &snode->temp); json_find_number(jvalues, "humidity-offset", &snode->humi); snode->next = settings; settings = snode; } } return 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; int id = 0, state = 0, nstate = 0; double itmp = 0.0; threads++; if((jid = json_find_member(json, "id"))) { jchild = json_first_child(jid); if(json_find_number(jchild, "gpio", &itmp) == 0) { id = (int)round(itmp); pinMode(id, PINMODE_INPUT); state = digitalRead(id); } } createMessage(id, state); while(loop) { nstate = digitalRead(id); if(nstate != state) { state = nstate; createMessage(id, state); } usleep(100000); } threads--; return (void *)NULL; }
static int rc101CreateCode(JsonNode *code) { int id = -1; int state = -1; int unit = -1; int all = -1; double itmp = 0; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "all", &itmp) == 0) all = (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(all == 1 && state == 1) { unit = 6; state = 0; } if(all == 1 && state == 0) { unit = 7; state = 1; } if(id == -1 || state == -1 || unit == -1) { logprintf(LOG_ERR, "rc101: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 1048575 || id < 0) { logprintf(LOG_ERR, "rc101: invalid id range"); return EXIT_FAILURE; } else if(unit > 4 || unit < 0) { rc101CreateMessage(id, state, unit, all); rc101ClearCode(); rc101CreateId(id); rc101CreateState(state); if(unit > -1) { rc101CreateUnit(unit); } rc101CreateFooter(); } return EXIT_SUCCESS; }
static int createCode(struct JsonNode *code) { int id = -1; int unit = -1; int state = -1; int all = 0; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "all", &itmp) == 0) all = 1; if(json_find_number(code, "off", &itmp) == 0) state=0; if(json_find_number(code, "on", &itmp) == 0) state=1; if(id == -1 || (unit == -1 && all == 0) || (state == -1 && all == 1)) { logprintf(LOG_ERR, "beamish_switch: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 65535 || id < 1) { logprintf(LOG_ERR, "beamish_switch: invalid id range"); return EXIT_FAILURE; } else if((unit > 4 || unit < 1) && all == 0) { logprintf(LOG_ERR, "beamish_switch: invalid unit range"); return EXIT_FAILURE; } else { if(all == 1 && state == 1) unit = 5; if(all == 1 && state == 0) unit = 6; createMessage(id, unit, state, all); clearCode(); createId(id); unit = map[unit]; createUnit(unit); createFooter(); beamish_switch->rawlen = RAW_LENGTH; } return EXIT_SUCCESS; }
static int createCode(JsonNode *code) { int id = -1; int systemcode = -1; int unit = -1; int state = -1; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "systemcode", &itmp) == 0) systemcode = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "off", &itmp) == 0) state=1; if(json_find_number(code, "on", &itmp) == 0) state=0; if(id == -1 || systemcode == -1 || unit == -1 || state == -1) { logprintf(LOG_ERR, "daycom: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 63 || id < 0) { logprintf(LOG_ERR, "daycom: invalid id range"); return EXIT_FAILURE; } else if(systemcode > 16999 || systemcode < 0) { logprintf(LOG_ERR, "daycom: invalid systemcode range"); return EXIT_FAILURE; } else if(unit > 7 || unit < 0) { logprintf(LOG_ERR, "daycom: invalid unit range"); return EXIT_FAILURE; } else { createMessage(id, systemcode, unit, state); clearCode(); createId(id); createSystemCode(systemcode); createState(state); createUnit(unit); createFooter(); daycom->rawlen = RAW_LENGTH; state = 0; } return EXIT_SUCCESS; }
static int arctechSrCreateCode(JsonNode *code) { int id = -1; int unit = -1; int state = -1; int all = 0; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "all", &itmp) == 0) all = (int)round(itmp); if(json_find_number(code, "down", &itmp) == 0) state=0; else if(json_find_number(code, "up", &itmp) == 0) state=1; if(id == -1 || (unit == -1 && all == 0) || state == -1) { logprintf(LOG_ERR, "arctech_screen: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 67108863 || id < 1) { logprintf(LOG_ERR, "arctech_screen: invalid id range"); return EXIT_FAILURE; } else if((unit > 15 || unit < 0) && all == 0) { logprintf(LOG_ERR, "arctech_screen: invalid unit range"); return EXIT_FAILURE; } else { if(unit == -1 && all == 1) { unit = 0; } arctechSrCreateMessage(id, unit, state, all); arctechSrCreateStart(); arctechSrClearCode(); arctechSrCreateId(id); arctechSrCreateAll(all); arctechSrCreateState(state); arctechSrCreateUnit(unit); arctechSrCreateFooter(); } return EXIT_SUCCESS; }
static int quiggGT7000CreateCode(JsonNode *code) { int unit = -1; int id = -1; int state = -1; int all = 0; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "all", &itmp) == 0) all = (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(id==-1 || (unit==-1 && all==0) || state==-1) { logprintf(LOG_ERR, "quigg_gt7000: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 4095 || id < 0) { logprintf(LOG_ERR, "quigg_gt7000: invalid programm code id range"); return EXIT_FAILURE; } else if((unit > 3 || unit < 0) && all == 0) { logprintf(LOG_ERR, "quigg_gt7000: invalid button code unit range"); return EXIT_FAILURE; } else { if(unit == -1 && all == 1) { unit = 4; } quiggGT7000CreateMessage(id, state, unit, all); quiggGT7000ClearCode(); quiggGT7000CreateId(id); quiggGT7000CreateUnit(unit); quiggGT7000CreateState(state); quiggGT7000CreateParity(); quiggGT7000CreateFooter(); } return EXIT_SUCCESS; }
static int cleverwattsCreateCode(JsonNode *code) { int id = -1; int unit = -1; int state = -1; int all = 0; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "all", &itmp) == 0) all = (int)round(itmp); if(json_find_number(code, "off", &itmp) == 0) state=1; else if(json_find_number(code, "on", &itmp) == 0) state=0; if(id == -1 || (unit == -1 && all == 0) || state == -1) { logprintf(LOG_ERR, "cleverwatts: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 1048575 || id < 1) { logprintf(LOG_ERR, "cleverwatts: invalid id range"); return EXIT_FAILURE; } else if((unit > 3 || unit < 0) && all == 0) { logprintf(LOG_ERR, "cleverwatts: invalid unit range"); return EXIT_FAILURE; } else { if(unit == -1 && all == 1) { unit = 3; } cleverwattsCreateMessage(id, unit, state, all ^ 1); cleverwattsClearCode(); cleverwattsCreateId(id); cleverwattsCreateState(state); cleverwattsCreateUnit(unit); cleverwattsCreateAll(all); cleverwattsCreateFooter(); } return EXIT_SUCCESS; }
int genSwitchCreateCode(JsonNode *code) { int id = -1; int state = -1; int tmp; json_find_number(code, "id", &id); if(json_find_number(code, "off", &tmp) == 0) state=0; else if(json_find_number(code, "on", &tmp) == 0) state=1; if(id == -1 || state == -1) { logprintf(LOG_ERR, "generic_switch: insufficient number of arguments"); return EXIT_FAILURE; } else { genSwitchCreateMessage(id, state); } return EXIT_SUCCESS; }
static int genScreenCreateCode(JsonNode *code) { int id = -1; int state = -1; double itmp = 0; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "down", &itmp) == 0) state=0; else if(json_find_number(code, "up", &itmp) == 0) state=1; if(id == -1 || state == -1) { logprintf(LOG_ERR, "generic_screen: insufficient number of arguments"); return EXIT_FAILURE; } else { genScreenCreateMessage(id, state); } return EXIT_SUCCESS; }
int clarusSwCreateCode(JsonNode *code) { char id[3] = {'\0'}; int unit = -1; int state = -1; double itmp; char *stmp; strcpy(id, "-1"); if(json_find_string(code, "id", &stmp) == 0) strcpy(id, stmp); if(json_find_number(code, "off", &itmp) == 0) state=0; else if(json_find_number(code, "on", &itmp) == 0) state=1; if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(strcmp(id, "-1") == 0 || unit == -1 || state == -1) { logprintf(LOG_ERR, "clarus_switch: insufficient number of arguments"); return EXIT_FAILURE; } else if((int)(id[0]) < 65 || (int)(id[0]) > 70) { logprintf(LOG_ERR, "clarus_switch: invalid id range"); return EXIT_FAILURE; } else if(atoi(&id[1]) < 0 || atoi(&id[1]) > 31) { logprintf(LOG_ERR, "clarus_switch: invalid id range"); return EXIT_FAILURE; } else if(unit > 63 || unit < 0) { logprintf(LOG_ERR, "clarus_switch: invalid unit range"); return EXIT_FAILURE; } else { clarusSwCreateMessage(id, unit, ((state == 2 || state == 1) ? 2 : 0)); clarusSwClearCode(); clarusSwCreateUnit(unit); clarusSwCreateId(id); clarusSwCreateState(state); clarusSwCreateFooter();; } return EXIT_SUCCESS; }
int homeEasyOldCreateCode(JsonNode *code) { int systemcode = -1; int unitcode = -1; int state = -1; int all = -1; int tmp; json_find_number(code, "systemcode", &systemcode); json_find_number(code, "unitcode", &unitcode); json_find_number(code, "all", &all); if(json_find_number(code, "off", &tmp) == 0) state=0; else if(json_find_number(code, "on", &tmp) == 0) state=1; if(systemcode == -1 || (unitcode == -1 && all == 0) || state == -1) { logprintf(LOG_ERR, "home_easy_old: insufficient number of arguments"); return EXIT_FAILURE; } else if(systemcode > 15 || systemcode < 0) { logprintf(LOG_ERR, "home_easy_old: invalid systemcode range"); return EXIT_FAILURE; } else if((unitcode > 15 || unitcode < 0) && all == 0) { logprintf(LOG_ERR, "arctech_switch: invalid unit range"); return EXIT_FAILURE; } else { if(unitcode == -1 && all == 1) { unitcode = 15; } homeEasyOldCreateMessage(systemcode, unitcode, state, all); homeEasyOldClearCode(); homeEasyOldCreateStart(); homeEasyOldCreateSystemCode(systemcode); homeEasyOldCreateUnitCode(unitcode); homeEasyOldCreateAll(all); homeEasyOldCreateState(state); homeEasyOldCreateFooter(); } return EXIT_SUCCESS; }
static int checkValues(JsonNode *code) { int height = 300; double itmp = -1; if(json_find_number(code, "gui-image-height", &itmp) == 0) height = (int)round(itmp); if(height <= 0) { logprintf(LOG_ERR, "Generic webcam gui-image-height cannot <= 0"); return 1; } return 0; }
int arctechDimCreateCode(JsonNode *code) { int id = -1; int unit = -1; int state = -1; int all = 0; int dimlevel = -1; int max = 0; int min = 15; int tmp; protocol_setting_get_number(arctech_dimmer, "min", &min); protocol_setting_get_number(arctech_dimmer, "max", &max); json_find_number(code, "id", &id); json_find_number(code, "unit", &unit); json_find_number(code, "dimlevel", &dimlevel); json_find_number(code, "all", &all); if(json_find_number(code, "off", &tmp) == 0) state=0; else if(json_find_number(code, "on", &tmp) == 0) state=1; if(id == -1 || (unit == -1 && all == 0) || (dimlevel == -1 && state == -1)) { logprintf(LOG_ERR, "arctech_dimmer: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 67108863 || id < 1) { logprintf(LOG_ERR, "arctech_dimmer: invalid id range"); return EXIT_FAILURE; } else if((unit > 15 || unit < 0) && all == 0) { logprintf(LOG_ERR, "arctech_dimmer: invalid unit range"); return EXIT_FAILURE; } else if(dimlevel != -1 && (dimlevel > max || dimlevel < min)) { logprintf(LOG_ERR, "arctech_dimmer: invalid dimlevel range"); return EXIT_FAILURE; } else if(dimlevel >= 0 && state == 0) { logprintf(LOG_ERR, "arctech_dimmer: dimlevel and state cannot be combined"); return EXIT_FAILURE; } else { if(unit == -1 && all == 1) { unit = 0; } if(dimlevel >= 0) { state = -1; } arctechDimCreateMessage(id, unit, state, all, dimlevel); arctechDimCreateStart(); arctechDimClearCode(); arctechDimCreateId(id); arctechDimCreateAll(all); arctechDimCreateState(state); arctechDimCreateUnit(unit); if(dimlevel > -1) { arctechDimCreateDimlevel(dimlevel); } arctechDimCreateFooter(); } return EXIT_SUCCESS; }
static int checkValues(struct JsonNode *jvalues) { double readonly = 0.0; char *platform = GPIO_PLATFORM; if(config_setting_get_string("gpio-platform", 0, &platform) != 0) { logprintf(LOG_ERR, "no gpio-platform configured"); return -1; } if(strcmp(platform, "none") == 0) { FREE(platform); logprintf(LOG_ERR, "no gpio-platform configured"); return -1; } if(wiringXSetup(platform, logprintf1) < 0) { FREE(platform); return -1; } FREE(platform); struct JsonNode *jid = NULL; if((jid = json_find_member(jvalues, "id"))) { struct JsonNode *jchild = NULL; struct JsonNode *jchild1 = NULL; jchild = json_first_child(jid); while(jchild) { jchild1 = json_first_child(jchild); while(jchild1) { if(strcmp(jchild1->key, "gpio") == 0) { if(wiringXValidGPIO((int)round(jchild1->number_)) != 0) { return -1; } } jchild1 = jchild1->next; } jchild = jchild->next; } } if(json_find_number(jvalues, "readonly", &readonly) == 0) { if((int)readonly != 1) { return -1; } } return 0; }
static int createCode(struct JsonNode *code) { int id = -1; int unit = -1; int state = -1; int all = 0; int learn = -1; double itmp = -1; if(json_find_number(code, "id", &itmp) == 0) id = (int)round(itmp); if(json_find_number(code, "unit", &itmp) == 0) unit = (int)round(itmp); if(json_find_number(code, "all", &itmp) == 0) all = (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(json_find_number(code, "learn", &itmp) == 0) learn = 1; if(all > 0 && learn > -1) { logprintf(LOG_ERR, "arctech_switch: all and learn cannot be combined"); return EXIT_FAILURE; } else if(id == -1 || (unit == -1 && all == 0) || state == -1) { logprintf(LOG_ERR, "arctech_switch: insufficient number of arguments"); return EXIT_FAILURE; } else if(id > 67108863 || id < 1) { logprintf(LOG_ERR, "arctech_switch: invalid id range"); return EXIT_FAILURE; } else if((unit > 15 || unit < 0) && all == 0) { logprintf(LOG_ERR, "arctech_switch: invalid unit range"); return EXIT_FAILURE; } else { if(unit == -1 && all == 1) { unit = 0; } createMessage(id, unit, state, all, learn); createStart(); clearCode(); createId(id); createAll(all); createState(state); createUnit(unit); createFooter(); arctech_switch->rawlen = RAW_LENGTH; } return EXIT_SUCCESS; }
int arctechDimCheckValues(JsonNode *code) { int dimlevel = -1; int max = 0; int min = 15; protocol_setting_get_number(arctech_dimmer, "min", &min); protocol_setting_get_number(arctech_dimmer, "max", &max); if(min > max) { return 1; } if(json_find_number(code, "dimlevel", &dimlevel) == 0) { if(dimlevel != -1 && (dimlevel < min || dimlevel > max)) { return 1; } else { return 0; } } return 0; }
int relayCreateCode(JsonNode *code) { int gpio = -1; int state = -1; int tmp; char *def = NULL; int free_def = 0; int have_error = 0; relay->rawlen = 0; if(protocol_setting_get_string(relay, "default", &def) != 0) { def = malloc(4); free_def = 1; strcpy(def, "off"); } json_find_number(code, "gpio", &gpio); if(json_find_number(code, "off", &tmp) == 0) state=0; else if(json_find_number(code, "on", &tmp) == 0) state=1; if(gpio == -1 || state == -1) { logprintf(LOG_ERR, "relay: insufficient number of arguments"); have_error = 1; goto clear; } else if(gpio > 20 || gpio < 0) { logprintf(LOG_ERR, "relay: invalid gpio range"); have_error = 1; goto clear; } else { if(strstr(progname, "daemon") != 0) { if(wiringPiSetup() < 0) { logprintf(LOG_ERR, "unable to setup wiringPi") ; return EXIT_FAILURE; } else { 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); } } } relayCreateMessage(gpio, state); goto clear; } } clear: if(free_def) sfree((void *)&def); if(have_error) { return EXIT_FAILURE; } else { return EXIT_SUCCESS; } }
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; }
int main(int argc, char **argv) { unsigned short port = 0; char* server = NULL; unsigned short stats = 0; struct ssdp_list_t *ssdp_list = NULL; atomicinit(); gc_attach(main_gc); gc_catch(); if(server != NULL && port > 0) { if((sockfd = socket_connect(server, port)) == -1){ printf("error: could not connect to pilight-daemon\n"); return EXIT_FAILURE; } } else if(ssdp_seek(&ssdp_list) == -1){ printf("error: no pilight ssdp connections found\n"); goto close; } else{ if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1){ printf("error: could not connect to pilight-daemon\n"); 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; if(iniInit("config.ini")) // get values from config.ini goto close; display(0, 0, 0); // init display unsigned short filteropt = 1; int lastmin = -1, firstrx = 0; 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); } if(filteropt == 1){ char *pr = NULL; double id = 0.0; double ch = 0.0; struct JsonNode *jmessage = json_find_member(jcontent, "message"); json_find_string(jcontent, "protocol", &pr); json_find_number(jmessage, "id", &id); json_find_number(jmessage, "channel", &ch); int j = 0; for(j=0; j<stations; j++){ // step through protocol filters if(strcmp(station[j].pr, "-")==0 || strcmp(station[j].pr, pr)==0){ // protocol not specified or found if(j == 0){ // datetime protocol is the first if(firstrx){ double min = 0.0; json_find_number(jmessage, "minute", &min); if((int)min != lastmin){ display(-1, 0.0, 0.0); lastmin = (int)min; } } } else if(strcmp(station[j].id, "-")==0 || (int)id==atoi(station[j].id)){ // id not specified or found if(strcmp(station[j].ch, "-")==0 || (int)ch==atoi(station[j].ch)){ // channel not specified or found double temp = 0.0, humi = 0.0; json_find_number(jmessage, "temperature", &temp); json_find_number(jmessage, "humidity", &humi); display(j-1, temp, humi); firstrx = 1; break; } } } } } else{ char *content = json_stringify(jcontent, "\t"); printf("%s\n", content); json_free(content); } json_delete(jcontent); } array_free(&array, n); } close: if(sockfd > 0) socket_close(sockfd); if(recvBuff != NULL){ FREE(recvBuff); recvBuff = NULL; } iniClean(); return EXIT_SUCCESS; }
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; }
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; } }