// main int main(int argc, char* argv[]){ ComPortHandle comPort; int go = TRUE; char* dev; char a; a='\0'; dev=&a; if(argc<2){//No port specified at commandline so search for attached devices dev=scandev(); if(strcmp(dev,"")!=0){ //printf("Attempting to open port...%s\n",dev); comPort = OpenComPort(dev); } else{ printf("Failed to find attached device.\n"); return FALSE; } } else{//Open port specified at commandline printf("Attempting to open port...%s\n",argv[1]); comPort = OpenComPort(argv[1]); } if(comPort > 0){ //printf("Connected. \n\n"); /* while(go){//continue until user chooses to exit unsigned int command; command = 0xc1; usleep(500000);//short sleep between commands go=CommandDialog(comPort,command); }*/ unsigned int command; command = 0xd2; CommandDialog(comPort,command); // printf("EXITING\n"); CloseComPort(comPort); } return 0; }
/* ----------------------------------Main----------------------------------- */ int main(int argc, char* argv[]){ ComPortHandle comPort; int go = TRUE; char* dev; char a; a='\0'; dev=&a; if(argc<2) { //No port specified at commandline so search for attached devices dev=scandev(); if(strcmp(dev,"")!=0) { //printf("Attempting to open port...%s\n",dev); comPort = OpenComPort(dev); } else { printf("Failed to find attached device.\n"); return FALSE; } } else { //Open port specified at commandline printf("Attempting to open port...%s\n",argv[1]); comPort = OpenComPort(argv[1]); } if(comPort > 0) { unsigned int command; command = 0xd2; CommandDialog(comPort,command); CloseComPort(comPort); } return 0; }
void detect_consoles(void) { FILE *fc; if ((fc = fopen("/proc/consoles", "r"))) { char fbuf[16]; int maj, min; DIR *dir; dir = opendir("/dev"); if (!dir) goto out; while ((fscanf(fc, "%*s %*s (%[^)]) %d:%d", &fbuf[0], &maj, &min) == 3)) { struct console *restrict tail; char * name; if (!strchr(fbuf, 'E')) continue; comparedev = makedev(maj, min); name = scandev(dir); if (!name) continue; if (posix_memalign((void*)&tail, sizeof(void*), alignof(typeof(struct console))) != 0) perror("memory allocation"); tail->next = (struct console*)0; tail->tty = name; if (!consoles) consoles = tail; else consoles->next = tail; } closedir(dir); out: fclose(fc); }
/* * Try to detect the real device(s) used for the system console * /dev/console if but only if /dev/console is used. On Linux * this can be more than one device, e.g. a serial line as well * as a virtual console as well as a simple printer. * * Returns 1 if stdout and stderr should be reconnected and 0 * otherwise. */ int detect_consoles(const char *device, int fallback) { int fd, ret = 0; #ifdef __linux__ char *attrib, *cmdline; FILE *fc; #endif if (!device || *device == '\0') fd = dup(fallback); else { fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); ret = 1; } if (fd >= 0) { DIR *dir; char *name; struct stat st; #ifdef TIOCGDEV unsigned int devnum; #endif if (fstat(fd, &st) < 0) { close(fd); goto fallback; } comparedev = st.st_rdev; if (ret && (fstat(fallback, &st) < 0 || comparedev != st.st_rdev)) dup2(fd, fallback); #ifdef __linux__ /* * Check if the device detection for Linux system console should be used. */ if (comparedev == makedev(TTYAUX_MAJOR, 0)) { /* /dev/tty */ close(fd); device = "/dev/tty"; goto fallback; } if (comparedev == makedev(TTYAUX_MAJOR, 1)) { /* /dev/console */ close(fd); goto console; } if (comparedev == makedev(TTYAUX_MAJOR, 2)) { /* /dev/ptmx */ close(fd); device = "/dev/tty"; goto fallback; } if (comparedev == makedev(TTY_MAJOR, 0)) { /* /dev/tty0 */ struct vt_stat vt; if (ioctl(fd, VT_GETSTATE, &vt) < 0) { close(fd); goto fallback; } comparedev = makedev(TTY_MAJOR, (int)vt.v_active); } #endif #ifdef TIOCGDEV if (ioctl (fd, TIOCGDEV, &devnum) < 0) { close(fd); goto fallback; } comparedev = (dev_t)devnum; #endif close(fd); dir = opendir("/dev"); if (!dir) goto fallback; name = scandev(dir); if (name) consalloc(name); closedir(dir); if (!consoles) goto fallback; return ret; } #ifdef __linux__ console: /* * Detection of devices used for Linux system consolei using * the /proc/consoles API with kernel 2.6.38 and higher. */ if ((fc = fopen("/proc/consoles", "re"))) { char fbuf[16]; int maj, min; DIR *dir; dir = opendir("/dev"); if (!dir) { fclose(fc); goto fallback; } while ((fscanf(fc, "%*s %*s (%[^)]) %d:%d", &fbuf[0], &maj, &min) == 3)) { char * name; if (!strchr(fbuf, 'E')) continue; comparedev = makedev(maj, min); name = scandev(dir); if (!name) continue; consalloc(name); } closedir(dir); fclose(fc); return ret; } /* * Detection of devices used for Linux system console using * the sysfs /sys/class/tty/ API with kernel 2.6.37 and higher. */ if ((attrib = actattr("console"))) { char *words = attrib, *token; DIR *dir; dir = opendir("/dev"); if (!dir) { free(attrib); goto fallback; } while ((token = strsep(&words, " \t\r\n"))) { char * name; if (*token == '\0') continue; comparedev = devattr(token); if (comparedev == makedev(TTY_MAJOR, 0)) { char *tmp = actattr(token); if (!tmp) continue; comparedev = devattr(tmp); free(tmp); } name = scandev(dir); if (!name) continue; consalloc(name); } closedir(dir); free(attrib); if (!consoles) goto fallback; return ret; } /* * Detection of devices used for Linux system console using * kernel parameter on the kernels command line. */ if ((cmdline = oneline("/proc/cmdline"))) { char *words= cmdline, *token; DIR *dir; dir = opendir("/dev"); if (!dir) { free(cmdline); goto fallback; } while ((token = strsep(&words, " \t\r\n"))) { #ifdef TIOCGDEV unsigned int devnum; #else struct vt_stat vt; struct stat st; #endif char *colon, *name; if (*token != 'c') continue; if (strncmp(token, "console=", 8) != 0) continue; token += 8; if (strcmp(token, "brl") == 0) token += 4; if ((colon = strchr(token, ','))) *colon = '\0'; if (asprintf(&name, "/dev/%s", token) < 0) continue; if ((fd = open(name, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC)) < 0) { free(name); continue; } free(name); #ifdef TIOCGDEV if (ioctl (fd, TIOCGDEV, &devnum) < 0) { close(fd); continue; } comparedev = (dev_t)devnum; #else if (fstat(fd, &st) < 0) { close(fd); continue; } comparedev = st.st_rdev; if (comparedev == makedev(TTY_MAJOR, 0)) { if (ioctl(fd, VT_GETSTATE, &vt) < 0) { close(fd); continue; } comparedev = makedev(TTY_MAJOR, (int)vt.v_active); } #endif close(fd); name = scandev(dir); if (!name) continue; consalloc(name); } closedir(dir); free(cmdline); /* * Detection of the device used for Linux system console using * the ioctl TIOCGDEV if available (e.g. official 2.6.38). */ if (!consoles) { #ifdef TIOCGDEV unsigned int devnum; const char *name; if (!device || *device == '\0') fd = dup(fallback); else fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); if (fd < 0) goto fallback; if (ioctl (fd, TIOCGDEV, &devnum) < 0) { close(fd); goto fallback; } comparedev = (dev_t)devnum; close(fd); if (device && *device != '\0') name = device; else name = ttyname(fallback); if (!name) name = "/dev/tty1"; consalloc(strdup(name)); if (consoles) { if (!device || *device == '\0') consoles->fd = fallback; return ret; } #endif goto fallback; } return ret; } #endif /* __linux __ */ fallback: if (fallback >= 0) { const char *name; if (device && *device != '\0') name = device; else name = ttyname(fallback); if (!name) name = "/dev/tty"; consalloc(strdup(name)); if (consoles) consoles->fd = fallback; } return ret; }