📄 oldswitch.c
字号:
#include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> #include <sys/types.h>#include <sys/stat.h>#include <netinet/in.h>#include <sys/socket.h>#include <netdb.h>#include <signal.h>#include <arpa/inet.h>#include <stdlib.h>#include <time.h>#include <sys/ioctl.h>#include <asm/etraxgpio.h>#include <syslog.h> #include <stdarg.h>#include <net/if.h>#define RX_BUFFER_LEN 1024int GetCommand(int local_port, char *rxbuffer) { int cs; int s; int sockfl; int size_csa; struct sockaddr_in csa; struct sockaddr_in sa; int rc; int yes = 1; int x; int rx_timeout=1000; int rxpointer=0; rxbuffer[rxpointer]=0; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(local_port); sa.sin_addr.s_addr = INADDR_ANY; // Crea un socket s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { printf("Error opening control socket\n"); return -1; } // Socket in non blocking mode sockfl = fcntl(s, F_GETFL, 0); fcntl(s, F_SETFL, sockfl | O_NONBLOCK); setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); rc = bind(s, (struct sockaddr *)&sa, sizeof(sa)); if (rc) { printf("Error binding control socket\n"); return -1; } rc = listen(s, 1); if (rc) { printf("Error listening control socket\n"); return -1; } while(1) { size_csa = sizeof(csa); cs = accept(s, (struct sockaddr *)&csa, &size_csa); if (cs > 0) { sockfl = fcntl(cs, F_GETFL, 0); fcntl(cs, F_SETFL, sockfl | O_NONBLOCK); // Data read loop for (x=(rx_timeout*10000-1);x>0;x--) { rc = read(cs, &rxbuffer[rxpointer], sizeof(char)); if (rc>0) { rxpointer+=rc; rxbuffer[rxpointer]=0; if (rxpointer>=(RX_BUFFER_LEN-256)) { printf("Rx buffer overflow\n"); return -1; } } if (rc==0) { close(s); return rxpointer; } if (rc<-1) { printf("Socket read error %d\n",rc); return -1; } } printf("Rx timeout\n"); close(s); return -1; } } printf("Rx socket accept timeout\n"); return -1;} int main(void) { int fda; int port=7000; char rxbuffer[RX_BUFFER_LEN]; // Open the I/O Port A // DL1 is on line 3 of port A if ((fda = open("/dev/gpioa", O_RDWR)) < 0) { printf("/dev/gpioa error\n"); return 1; } system("killall statusled"); // Turn off DL1 ioctl(fda, _IO(ETRAXGPIO_IOCTYPE, IO_SETBITS), 1 << 3); while(1) { if (GetCommand(port,rxbuffer)) { printf("%s\n",rxbuffer); if (strstr (rxbuffer,"DL1")!=NULL) { if (strstr(rxbuffer,"on")!=NULL) { ioctl(fda, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS), 1 << 3); } else { ioctl(fda, _IO(ETRAXGPIO_IOCTYPE, IO_SETBITS), 1 << 3); } } } } return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -