From 9f8f95f44e3350808e473ef8e430576942af66dd Mon Sep 17 00:00:00 2001 From: Phiber2000 Date: Sun, 10 Jul 2016 21:55:56 +0200 Subject: [PATCH] Fix daemon to allow usage of all addresses, Fix array write out of range Added/fixed examples Changed state address calculation Cleanup code format --- daemon.cpp | 399 ++++++++++++++++++++-------------------- webinterface/config.php | 16 +- 2 files changed, 214 insertions(+), 201 deletions(-) diff --git a/daemon.cpp b/daemon.cpp index 3734073..34ed988 100644 --- a/daemon.cpp +++ b/daemon.cpp @@ -12,22 +12,26 @@ * xxxxx encoding * 00001 for first channel * yy plug - * 01 for plug A + * 16 for plug 1 + * 08 for plug 2 + * 04 for plug 3 + * 02 for plug 4 + * 01 for plug 5 * z action * 0:off|1:on|2:status * - * Examples of remote actions - * Switch plug 01 on 00001 to on - * echo 101111011 | nc localhost 11337 + * Examples of remote actions + * Switch plug A on 00001 to on + * echo 100001161 | nc localhost 11337 * - * Switch plug 01 on 00001 to off - * echo 101111010 | nc localhost 11337 + * Switch plug B on 00001 to off + * echo 100001080 | nc localhost 11337 * - * Get status of plug 01 on 00001 - * echo 101111012 | nc localhost 11337 + * Get status of plug A on 00001 + * echo 100001162 | nc localhost 11337 * - * Switch intertechno plug 2 on group 2 to on - * echo 2221 | nc localhost 11337 + * Switch intertechno plug 2 on group 2 to on + * echo 202021 | nc localhost 11337 * */ @@ -49,22 +53,22 @@ int main(int argc, char* argv[]) { * Setup wiringPi and RCSwitch * set high priority scheduling */ - if (wiringPiSetup () == -1){ - return 1; - } - piHiPri(20); - mySwitch = RCSwitch(); + if (wiringPiSetup () == -1) { + return 1; + } + piHiPri(20); + mySwitch = RCSwitch(); mySwitch.setPulseLength(300); usleep(50000); mySwitch.enableTransmit(0); - nPlugs=1110; - int nState[nPlugs]; - nTimeout=0; - memset(nState, 0, sizeof(nState)); + nPlugs=1024; + int nState[nPlugs]; + nTimeout=0; + memset(nState, 0, sizeof(nState)); - /** - * setup socket - */ + /** + * setup socket + */ int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; @@ -76,52 +80,52 @@ int main(int argc, char* argv[]) { serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); - // receiving socket + // receiving socket sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0){ - error("ERROR opening socket"); + if (sockfd < 0) { + error("ERROR opening socket"); } - if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ - error("ERROR on binding"); + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + error("ERROR on binding"); } // sending socket newsockfd = socket(AF_INET, SOCK_STREAM, 0); - if (newsockfd < 0) - error("ERROR opening socket"); + if (newsockfd < 0) { + error("ERROR opening socket"); + } /* * start listening */ while (true) { - listen(sockfd,5); - clilen = sizeof(cli_addr); - newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); - if (newsockfd < 0){ - error("ERROR on accept"); - } - bzero(buffer,256); + listen(sockfd,5); + clilen = sizeof(cli_addr); + newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); + if (newsockfd < 0) { + error("ERROR on accept"); + } + bzero(buffer,256); n = read(newsockfd,buffer,255); - if (n < 0){ - error("ERROR reading from socket"); - } + if (n < 0) { + error("ERROR reading from socket"); + } /* * get values */ - printf("message: %s\n", buffer); - if (strlen(buffer) >= 5) { - nSys = buffer[0]-48; - switch (nSys){ - //normal elro - case 1:{ - for (int i=1; i<6; i++) { - nGroup[i-1] = buffer[i]; - } - nGroup[6] = '\0'; - for (int i=6;i<8; i++) { - nSwitchNumber = buffer[i]-48; - } - nAction = buffer[8]-48; - nTimeout=0; + printf("message: %s\n", buffer); + if (strlen(buffer) >= 5) { + nSys = buffer[0]-48; + switch (nSys) { + //normal elro + case 1:{ + for (int i=1; i<6; i++) { + nGroup[i-1] = buffer[i]; + } + nGroup[5] = '\0'; + nSwitchNumber = (buffer[6]-48)*10; + nSwitchNumber += (buffer[7]-48); + nAction = buffer[8]-48; + nTimeout=0; printf("nSys: %i\n", nSys); printf("nGroup: %s\n", nGroup); printf("nSwitchNumber: %i\n", nSwitchNumber); @@ -131,20 +135,20 @@ int main(int argc, char* argv[]) { if (strlen(buffer) >= 11) nTimeout = nTimeout*10+buffer[10]-48; if (strlen(buffer) >= 12) nTimeout = nTimeout*10+buffer[11]-48; - /** - * handle messages - */ - int nAddr = getAddr(nGroup, nSwitchNumber); + /** + * handle messages + */ + int nAddr = getAddr(nGroup, nSwitchNumber); printf("nAddr: %i\n", nAddr); printf("nPlugs: %i\n", nPlugs); - char msg[13]; - if (nAddr > nPlugs) { + char msg[13]; + if (nAddr > 1023 || nAddr < 1) { printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); n = write(newsockfd,"2",1); - } - else { + } + else { switch (nAction) { - //OFF + //OFF case 0:{ //piThreadCreate(switchOff); mySwitch.switchOffBinary(nGroup, nSwitchNumber); @@ -154,8 +158,8 @@ int main(int argc, char* argv[]) { n = write(newsockfd,msg,1); break; } - //ON - case 1:{ + //ON + case 1:{ //piThreadCreate(switchOn); mySwitch.switchOnBinary(nGroup, nSwitchNumber); nState[nAddr] = 1; @@ -165,195 +169,190 @@ int main(int argc, char* argv[]) { break; } //STATUS - case 2:{ + case 2:{ sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); sprintf(msg, "%d", nState[nAddr]); n = write(newsockfd,msg,1); break; } - } + } } break; } - //Intertechno - case 2:{ - nGroup[0] = buffer[1]; - - nGroup[1] = '\0'; - for (int i=2;i<4; i++) { - nSwitchNumber = buffer[i]-48; - } - nAction = buffer[4]-48; - nTimeout=0; - printf("nSys: %i\n", nSys); + //Intertechno + case 2:{ + nGroup[0] = buffer[1]; + nGroup[1] = buffer[2]; + nGroup[2] = '\0'; + nSwitchNumber = (buffer[3]-48)*10; + nSwitchNumber += (buffer[4]-48); + nAction = buffer[5]-48; + nTimeout=0; + printf("nSys: %i\n", nSys); printf("nGroup: %s\n", nGroup); printf("nSwitchNumber: %i\n", nSwitchNumber); printf("nAction: %i\n", nAction); int nAddr = getAddr(nGroup, nSwitchNumber); printf("nAddr: %i\n", nAddr); printf("nPlugs: %i\n", nPlugs); - char msg[13]; - if (nAddr > nPlugs) { - printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); - n = write(newsockfd,"2",1); - } - else { + char msg[13]; + if (nAddr > 16 || nAddr < 1) { + printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); + n = write(newsockfd,"2",1); + } + else { printf("computing systemcode for Intertechno Type B house[%s] unit[%i] ... ",nGroup, nSwitchNumber); char pSystemCode[14]; - switch(atoi(nGroup)){ - // house/family code A=1 - P=16 - case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } - case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } - case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } - case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } - case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } - case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } - case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } - case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } - case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } - case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } - case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } - case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } - case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } - case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } - case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } - case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } + switch (atoi(nGroup)) { + // house/family code A=1 - P=16 + case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } + case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } + case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } + case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } + case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } + case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } + case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } + case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } + case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } + case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } + case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } + case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } + case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } + case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } + case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } + case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } default:{ printf("systemCode[%s] is unsupported\n", nGroup); return -1; } } printf("got systemCode[%s] ",nGroup); - switch(nSwitchNumber){ - // unit/group code 01-16 - case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } - case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } - case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } - case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } - case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } - case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } - case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } - case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } - case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } - case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } - case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } - case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } - case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } - case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } - case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } - case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } + switch (nSwitchNumber) { + // unit/group code 01-16 + case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } + case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } + case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } + case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } + case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } + case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } + case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } + case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } + case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } + case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } + case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } + case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } + case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } + case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } + case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } + case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } default:{ printf("unitCode[%i] is unsupported\n", nSwitchNumber); - return -1; + return -1; } } strcat(pSystemCode,"0F"); // mandatory bits - switch(nAction){ + switch(nAction) { case 0:{ strcat(pSystemCode,"F0"); - mySwitch.sendTriState(pSystemCode); - printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); - nState[nAddr] = 0; - //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - sprintf(msg, "%d", nState[nAddr]); - n = write(newsockfd,msg,1); - break; + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + nState[nAddr] = 0; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; } case 1:{ - strcat(pSystemCode,"FF"); - mySwitch.sendTriState(pSystemCode); - printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); - nState[nAddr] = 1; - //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - sprintf(msg, "%d", nState[nAddr]); - n = write(newsockfd,msg,1); - break; - } - case 2:{ - sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - sprintf(msg, "%d", nState[nAddr]); - n = write(newsockfd,msg,1); - break; - } - default:{ - printf("command[%i] is unsupported\n", nAction); - return -1; - } - } + strcat(pSystemCode,"FF"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + nState[nAddr] = 1; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + case 2:{ + sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + default:{ + printf("command[%i] is unsupported\n", nAction); + return -1; + } + } } - break; - } - default:{ - printf("wrong systemkey!\n"); - } - } + break; + } + default:{ + printf("wrong systemkey!\n"); + } + } } - else { - printf("message corrupted or incomplete"); + else { + printf("message corrupted or incomplete"); } - if (n < 0){ + if (n < 0) { error("ERROR writing to socket"); close(newsockfd); - } - } + } + } - /** - * terminate - */ - //close(newsockfd); - close(sockfd); - return 0; + /** + * terminate + */ + //close(newsockfd); + close(sockfd); + return 0; } /** * error output */ -void error(const char *msg) -{ - perror(msg); - exit(1); +void error(const char *msg) { + perror(msg); + exit(1); } /** * calculate the array address of the power state */ int getAddr(const char* nGroup, int nSwitchNumber) { - int bin = atoi(nGroup); - int base = 1; - int dec = 0; - int digit; - - while(bin!=0) - { - digit=bin%10; - dec=dec+digit*base; - bin=bin/10; - base=base*2; - } + int tempgroup = atoi(nGroup); + int group = 0; + for (int i = 0; i < 5; i++) { + if ((tempgroup % 10) == 1) { + group = group | (1 << i); + } + tempgroup = tempgroup / 10; + } - return (dec-1)*5+nSwitchNumber-1; + int switchnr = nSwitchNumber & 0b00011111; + int result = (group << 5) | switchnr; + return result; } PI_THREAD(switchOn) { - printf("switchOnThread: %d\n", nTimeout); - char tGroup[6]; - int tSwitchNumber; - memcpy(tGroup, nGroup, sizeof(tGroup)); - tSwitchNumber = nSwitchNumber; - sleep(nTimeout*60); - mySwitch.switchOnBinary(tGroup, tSwitchNumber); - return 0; + printf("switchOnThread: %d\n", nTimeout); + char tGroup[6]; + int tSwitchNumber; + memcpy(tGroup, nGroup, sizeof(tGroup)); + tSwitchNumber = nSwitchNumber; + sleep(nTimeout*60); + mySwitch.switchOnBinary(tGroup, tSwitchNumber); + return 0; } PI_THREAD(switchOff) { - printf("switchOffThread: %d\n", nTimeout); - char tGroup[6]; - int tSwitchNumber; - memcpy(tGroup, nGroup, sizeof(tGroup)); - tSwitchNumber = nSwitchNumber; - sleep(nTimeout*60); - mySwitch.switchOffBinary(tGroup, tSwitchNumber); - return 0; + printf("switchOffThread: %d\n", nTimeout); + char tGroup[6]; + int tSwitchNumber; + memcpy(tGroup, nGroup, sizeof(tGroup)); + tSwitchNumber = nSwitchNumber; + sleep(nTimeout*60); + mySwitch.switchOffBinary(tGroup, tSwitchNumber); + return 0; } - diff --git a/webinterface/config.php b/webinterface/config.php index 0f70d53..c23ee2c 100644 --- a/webinterface/config.php +++ b/webinterface/config.php @@ -22,6 +22,20 @@ * */ $config=array( - array("1", "00100", "01", "PowerPlug1"), + /* + * Elro + */ + array("1", "00001", "16", "Nr. 1"), + array("1", "00001", "08", "Nr. 2"), + array("1", "00001", "04", "Nr. 3"), + array("1", "00001", "02", "Nr. 4"), + array("1", "00001", "01", "Nr. 5"), + /* + * Intertech + */ + array("2", "01", "01", "IT 1"), + array("2", "01", "02", "IT 2"), + array("2", "01", "03", "IT 3"), + array("2", "01", "04", "IT 4"), ) ?>