int main(int argc, char **argv) { if(signal(SIGINT, safe_exit) == SIG_ERR) SIGFAIL() if(signal(SIGQUIT, safe_exit) == SIG_ERR) SIGFAIL() if(signal(SIGTERM, safe_exit) == SIG_ERR) SIGFAIL() appname = argv[0]; setconfigdefaults(); if(parseargs(argc, argv) != 0) { return(1); } #ifdef DEBUG (void)printf("name: %s\n", config.name); (void)printf("host: %s\n", config.host); (void)printf("port: %s\n", config.port); (void)printf("limit: %d\n", (int)config.limit); #endif if(createandconnectsocket() != 0) { cleanup(); return(1); } while(rcvmsg() == 0 && quit == 0) { if(processmsg() != 0) { break; } } cleanup(); return(0); }
/** * Process an incoming character. If it's 0xA4 and not escaped, it's part of an incoming message. * Also check for bytes indicating message length, the checksum at the end of the message, and so on. * Basically make sure we get an entire message and when we do, pass it on for further processing to * determine the type of message and content. If a message is incomplete or doesn't have the right * checksum (rare), then it is just discarded. */ void HDListen::handlebyte(unsigned char cIn) {//protected switch(msgstate) { case 0: // wait for start if(cIn!=0xA4) return; msgstate = 1; msglen = 0; msgcount = 0; checksum = 0xA4; esc = false; break; case 1: // wait length msglen = cIn; checksum += cIn; msgstate = 2; break; case 2: // read msg if(cIn==0x1B && !esc) { esc = true; return; } if (esc) { esc = false; if (cIn == 0x48) cIn = 0xA4; } if(msgcount==msglen) { if(cIn==(checksum & 0xFF)) { processmsg(); } else { dprintf("bad checksum, checksum is %02x\n\r",cIn); ddump("msg",message,msgcount); dprintf("calc checksum, checksum is %02x\n\r",checksum & 0xFF); } msgstate=0; } else { checksum += cIn; message[msgcount++] = cIn; } break; } }