// // Serial Receive Program // // cc -o sirexec sirexec.c -lwiringPi // #include #include #include #include #include #include #include #include #include #define DEBUG 0 #define BAUDRATE 4800 #define INTERVAL 0 // Re define message structure typedef struct mymsgbuf { long mtype; long code; } mess_t; typedef struct { long code; char* command; } keymap; // // get IR code // // 1,XXXXXX->0 // 0,abcdef->0xabcdef // long getIR(char * buf) { char work[10]; char *endptr; long x1,x2,x3; if (buf[0] == '1') return 0; memset(work,0,sizeof(work)); memcpy(work,&buf[2],4); x1=strtol(work, &endptr, 16); // printf("x1=%x\n",x1); memcpy(work,&buf[6],4); x2=strtol(work, &endptr, 16); // printf("x2=%x\n",x2); x3=(x1<<16)+x2; // printf("x3=%x\n",x3); return x3; } int loadconfig(char * path, keymap * map) { FILE* fp; int read; char* line = NULL; char* tmp; size_t alloc; int i = 0; char work[10]; char *endptr; long x1,x2,x3; fp = fopen(path, "r"); if (fp == NULL) return -1; while ((read = getline(&line, &alloc, fp)) != -1) { // printf("read=%d\n",read); if (read <= 1) continue; line[read-1]=0; // printf("line=[%s]\n",line); if (line[0] == '#') continue; /* fetch keycode*/ memcpy(work,&line[0],4); x1=strtol(work, &endptr, 16); // printf("x1=%x\n",x1); memcpy(work,&line[4],4); x2=strtol(work, &endptr, 16); // printf("x2=%x\n",x2); x3=(x1<<16)+x2; // printf("x3=%x\n",x3); map[i].code = x3; /* fetch keycode*/ tmp = strtok(line, ","); if (tmp == NULL) continue; /* and command */ tmp = strtok(NULL, ","); map[i].command = tmp; // printf("map[%d].command=[%s]\n",i,map[i].command); /* cleanup for next pass */ i++; line = NULL; } fclose(fp); return i; } int readSerial(int fd, char * buff, int blen, int timeout) { unsigned long endTime; int ch; int pos = 0; if(DEBUG)printf("millis=%d\n",millis()); endTime = millis () + timeout; if(DEBUG)printf("endTime=%d\n",endTime); buff[pos] = 0; while (1) { if (millis () > endTime) return -1; if (serialDataAvail (fd)) { ch = serialGetchar (fd); if(DEBUG)printf (" -> %02x\n", ch); if (ch == 0x0d) { } else if (ch == 0x0a) { return pos; } else { if (pos < blen) buff[pos++] = ch; if (pos < blen) buff[pos] = 0; } } // end if } // end while } int main(int argc, char **argv) { int qid; key_t msgkey; pid_t pid; mess_t buf; int msglen; int cont; char *device; char *config; int readlen; char buff[64]; keymap map[255] = {0}; int maps; int i; int fd; long ircode; long savecode=0; int beDebug = 0; if (argc < 2) { printf("%s /dev/ttyS config\n",argv[0]); return 0; } device=argv[1]; config=argv[2]; if (argc == 4) { if (strcmp(argv[3],"debug") == 0) beDebug = 1; } if(beDebug)printf("device=[%s] config=[%s] beDebug=%d\n",device,config,beDebug); if (getuid() != 0) printf("You are not root! This may not work...\n"); // if (beDaemon) daemon(0,0); maps=loadconfig(config,map); if(DEBUG)printf("maps=%d\n",maps); if (maps <= 0) { printf("Error:llegal config file\n"); return 0; } for(i=0;i 0) sleep(INTERVAL); } msgctl(qid, IPC_RMID, 0); return 0; }