static int gry_put(CableHandle* h, uint8_t *data, uint32_t len) { BOOL fSuccess; DWORD nBytesWritten; OVERLAPPED ol; memset(&ol, 0, sizeof(OVERLAPPED)); fSuccess = WriteFile(hCom, data, len, &nBytesWritten, &ol); while(HasOverlappedIoCompleted(&ol) == FALSE) Sleep(0); fSuccess = GetOverlappedResult(hCom, &ol, &nBytesWritten, FALSE); if (!fSuccess) { ticables_warning("WriteFile"); return ERR_WRITE_ERROR; } else if (nBytesWritten == 0) { ticables_warning("WriteFile"); return ERR_WRITE_TIMEOUT; } else if (nBytesWritten < len) { ticables_warning("WriteFile"); return ERR_WRITE_ERROR; } return 0; }
static int dev_put(CableHandle* h, uint8_t *data, uint32_t len) { int ret; int bytesdone = 0; do { ret = write(dev_fd, (void *)(data + bytesdone), len - bytesdone); if(ret == -ETIMEDOUT) { ticables_warning("write (%s).\n", usb_strerror()); return ERR_WRITE_TIMEOUT; } else if(ret == -EPIPE) { ticables_warning("write (%s).\n", usb_strerror()); return ERR_WRITE_ERROR; } else if(ret < 0) { ticables_warning("write (%s).\n", usb_strerror()); return ERR_WRITE_ERROR; } bytesdone += ret; } while(bytesdone < len); return 0; }
static int gry_get(CableHandle* h, uint8_t *data, uint32_t len) { BOOL fSuccess; DWORD nBytesRead; OVERLAPPED ol; uint32_t i; for(i = 0; i < len;) { memset(&ol, 0, sizeof(OVERLAPPED)); fSuccess = ReadFile(hCom, data + i, len - i, &nBytesRead, &ol); while(HasOverlappedIoCompleted(&ol) == FALSE) Sleep(0); fSuccess = GetOverlappedResult(hCom, &ol, &nBytesRead, FALSE); if (!fSuccess) { ticables_warning("ReadFile"); return ERR_READ_ERROR; } else if (nBytesRead == 0) { ticables_warning("ReadFile"); return ERR_READ_TIMEOUT; } i += nBytesRead; } printf("get : %i %i %i\n", fSuccess, nBytesRead, len); return 0; }
int linux_check_tty(const char *devname) { int fd; #ifdef HAVE_LINUX_SERIAL_H struct serial_struct serinfo; memset(&serinfo, 0, sizeof(serinfo)); #endif ticables_info(_("Check for tty support:")); #ifdef HAVE_LINUX_SERIAL_H ticables_info(_(" tty support: available.")); #else ticables_info(_(" tty support: not compiled.")); return ERR_TTDEV; #endif // check for node usability ticables_info(_("Check for tty usability:")); if(check_for_node_usability(devname) == -1) { ticables_warning(_("not usable")); return ERR_TTDEV; } #ifdef HAVE_LINUX_SERIAL_H // check for device availability fd = open(devname, 0); if (fd == -1) { ticables_warning(_("unable to open serial device '%s'"), devname); return ERR_TTDEV; } serinfo.reserved_char[0] = 0; if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) { ticables_warning(_("Error running TIOCGSERIAL ioctl on device %s - %s"), devname, strerror(errno)); close(fd); return ERR_TTDEV; } if(serinfo.type == PORT_UNKNOWN || serinfo.type == PORT_MAX) { ticables_info(_(" is useable: no")); close(fd); return ERR_TTDEV; } ticables_info(_(" is useable: yes")); close(fd); #endif return 0; }
static int dev_open(CableHandle *h) { int mask = O_RDWR | O_SYNC; if ((dev_fd = open(h->device, mask)) == -1) { ticables_warning(_("unable to open this device: %s.\n"), h->device); return ERR_LIBUSB_OPEN; } #ifdef HAVE_LINUX_TICABLE_H { int arg = 0; if (ioctl(dev_fd, IOCTL_TIUSB_GET_MAXPS, &arg) == -1) { ticables_warning(_("unable to get max packet size (ioctl).\n")); //return ERR_IOCTL; } else { max_ps2 = arg; } } #endif #ifdef HAVE_LINUX_TICABLE_H { int arg = h->timeout; if (ioctl(dev_fd, IOCTL_TIUSB_TIMEOUT, arg) == -1) { ticables_warning(_("unable to set timeout (ioctl).\n")); //return ERR_IOCTL; } } #endif #ifdef HAVE_LINUX_TICABLE_H { int arg = 0; if (ioctl(dev_fd, IOCTL_TIUSB_RESET_PIPES, arg) == -1) { ticables_warning(_("unable to reset pipes (ioctl).\n")); //return ERR_IOCTL; } } #endif /* Clear buffers */ nBytesRead2 = 0; return 0; }
static void tigl_get_product(char * string, size_t maxlen, struct libusb_device *dev) { libusb_device_handle *han; int ret; struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); string[0] = 0; if (r < 0) { ticables_critical("failed to get device descriptor"); } if (desc.iProduct) { if (!libusb_open(dev, &han)) { ret = libusb_get_string_descriptor_ascii(han, desc.iProduct, (unsigned char *) string, maxlen); libusb_close(han); if (ret <= 0) { ticables_warning("libusb_get_string_descriptor_ascii (%s).\n", tigl_strerror(ret)); } } // else do nothing. } }
static int add_data_code(uint16_t code) { unsigned int i; for (i = 0; i < dcf; i++) { if (data_code_found[i] == code) { return 0; } } if (i < (sizeof(data_code_found)/sizeof(data_code_found[0])) - 1) { data_code_found[++i] = code; } else { static int warn_add_data_code = 0; if (!warn_add_data_code) { ticables_warning("DUSB protocol interpreter: no room left in data_code_found array."); warn_add_data_code++; } } dcf = i; return i; }
static int add_pkt_type(uint8_t type) { unsigned int i; for (i = 0; i < ptf; i++) { if (pkt_type_found[i] == type) { return 0; } } if (i < (sizeof(pkt_type_found)/sizeof(pkt_type_found[0])) - 1) { pkt_type_found[++i] = type; } else { static int warn_add_pkt_type = 0; if (!warn_add_pkt_type) { ticables_warning("DUSB protocol interpreter: no room left in pkt_type_found array."); warn_add_pkt_type++; } } ptf = i; return i; }
int linux_check_tiusb(const char *devname) { int fd; int arg; // check for node usability ticables_info(_("Check for tiusb usability:")); if(check_for_node_usability(devname) == -1) return ERR_TTDEV; #ifdef HAVE_LINUX_TICABLE_H // check for device availability fd = open(devname, 0); if (fd == -1) { ticables_warning("unable to open USB device '%s'", devname); return ERR_TTDEV; } if (ioctl(fd, IOCTL_TIUSB_GET_MAXPS, &arg) == -1) return ERR_TTDEV; ticables_info(_(" is useable: yes")); close(fd); return 0; #else fd = arg = -1; ticables_info(_(" tiusb support: not compiled.")); return ERR_TTDEV; #endif }
int bsd_check_tty(const char *devname) { int fd; ticables_info(_("Check for tty support:")); ticables_info(_(" tty support: available.")); // check for node usability ticables_info(_("Check for tty usability:")); if(check_for_node_usability(devname) == -1) return ERR_TTDEV; // check for device availability fd = open(devname, 0); if (fd == -1) { ticables_warning("unable to open serial device '%s'", devname); return ERR_TTDEV; } ticables_info(_(" is useable: yes")); close(fd); return 0; }
static int add_addr(uint16_t* array, uint16_t addr, int *count) { int i; for(i = 0; i < *count; i++) if(array[i] == addr) return 0; if (i < ARRAY_SIZE - 1) { array[++i] = addr; } else { static int warn_add_addr; if (!warn_add_addr++) { ticables_warning("NSP protocol interpreter: no room left in addr_found array."); } } *count = i; return i; }
static int vti_open(CableHandle *h) { int i; /* Get a unique (if possible) key */ for (i = 0; i < 2; i++) { if ((ipc_key[i] = ftok("/tmp", i)) == -1) { ticables_warning("unable to get unique key (ftok).\n"); return ERR_VTI_IPCKEY; } } /* Open a shared memory segment */ for (i = 0; i < 2; i++) { if ((shmid[i] = shmget(ipc_key[i], sizeof(LinkBuffer), IPC_CREAT | 0666)) == -1) { ticables_warning("unable to open shared memory (shmget).\n"); return ERR_VTI_SHMGET; } } /* Attach the shm */ for (i = 0; i < 2; i++) { if ((shm[i] = shmat(shmid[i], NULL, 0)) == NULL) { ticables_warning("unable to attach shared memory (shmat).\n"); return ERR_VTI_SHMAT; } } /* Swap shm */ send_buf[0] = shm[0]; // 0 -> 1: writing recv_buf[0] = shm[1]; // 0 <- 1: reading send_buf[1] = shm[1]; // 1 -> 0: writing recv_buf[1] = shm[0]; // 1 <- 0: reading for (i = 0; i < 2; i++) shm[i]->start = shm[i]->end = 0; return 0; }
static int dev_get_(CableHandle *h, uint8_t *data) { int ret = 0; /* Read up to 32/64 bytes and store them in a buffer for subsequent accesses */ if (nBytesRead2 <= 0) { do { ret = read(dev_fd, (void *) rBuf2, max_ps2); if (ret == 0) ticables_warning("weird, read returns without any data and/or error; retrying..."); } while(!ret); if(ret == -ETIMEDOUT) { ticables_warning("read (%s).\n", usb_strerror()); nBytesRead2 = 0; return ERR_READ_TIMEOUT; } else if(ret == -EPIPE) { ticables_warning("read (%s).\n", usb_strerror()); nBytesRead2 = 0; return ERR_READ_ERROR; } else if(ret < 0) { ticables_warning("read (%s).\n", usb_strerror()); nBytesRead2 = 0; return ERR_READ_ERROR; } nBytesRead2 = ret; rBufPtr2 = rBuf2; } *data = *rBufPtr2++; nBytesRead2--; return 0; }
int par_io_wr(int dev_fd, uint8_t data) { if(ioctl (dev_fd, PPWDATA, &data) == -1) { ticables_warning(_("ioctl failed on parallel device: can't write on data lines.")); return ERR_WRITE_ERROR; } return 0; }
int ser_io_rd(int dev_fd) { unsigned int flags = 0; if (ioctl(dev_fd, TIOCMGET, &flags) == -1) { ticables_warning(_("ioctl failed on serial device.")); return ERR_TTY_IOCTL; } return (flags & TIOCM_CTS ? 0x10 : 0) | (flags & TIOCM_DSR ? 0x20 : 0); }
int par_io_close(int dev_fd) { // release access if (ioctl(dev_fd, PPRELEASE) == -1) { ticables_warning(_("ioctl failed on parallel device: can't release parport.")); return ERR_PPT_IOCTL; } // and close return close(dev_fd); }
int par_io_rd(int dev_fd) { uint8_t data; if (ioctl(dev_fd, PPRSTATUS, &data) == -1) { ticables_warning(_("ioctl failed on parallel device: can't read status lines.")); return ERR_PPT_IOCTL; } return data; }
static int gry_reset(CableHandle *h) { BOOL fSuccess; fSuccess = PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR); if (!fSuccess) { ticables_warning("PurgeComm"); return ERR_GRY_PURGECOMM; } return 0; }
static int vti_close(CableHandle *h) { int i; /* Detach segment */ for (i = 0; i < 2; i++) { if (shmdt(shm[i]) == -1) { ticables_warning("shmdt\n"); return ERR_VTI_SHMDT; } /* and destroy it */ if (shmctl(shmid[i], IPC_RMID, NULL) == -1) { ticables_warning("shmctl\n"); return ERR_VTI_SHMCTL; } } return 0; }
int ser_io_open(const char *dev_name, int *dev_fd) { #ifndef O_SYNC # define O_SYNC O_FSYNC #endif *dev_fd = open(dev_name, O_RDWR | O_SYNC); if (*dev_fd == -1) { ticables_warning("unable to open serial device '%s'", dev_name); return ERR_TTY_OPEN; } return 0; }
int par_io_open(const char *dev_name, int *dev_fd) { int mode; // Open device *dev_fd = open(dev_name, O_RDWR); if (*dev_fd == -1) { ticables_warning("unable to open parallel device '%s'.", dev_name); return ERR_PPT_OPEN; } // Ask for exclusive access if (ioctl(*dev_fd, PPEXCL) == -1) { ticables_warning(_("ioctl failed on parallel device: can't claim exclusive access.")); ticables_warning("we will do without that."); } // Claim access if (ioctl(*dev_fd, PPCLAIM) == -1) { ticables_warning(_("ioctl failed on parallel device: can't claim parport.")); return ERR_PPT_IOCTL; } // Change transfer mode //mode = PARPORT_MODE_PCSPP; mode = IEEE1284_MODE_COMPAT; if (ioctl(*dev_fd, PPSETMODE, &mode) == -1) { ticables_warning(_("ioctl failed on parallel device: can't change transfer mode.")); return ERR_PPT_IOCTL; } return 0; }
int ser_io_wr(int dev_fd, uint8_t data) { unsigned int flags = 0; flags |= (data & 2) ? TIOCM_RTS : 0; flags |= (data & 1) ? TIOCM_DTR : 0; if (ioctl(dev_fd, TIOCMSET, &flags) == -1) { ticables_warning(_("ioctl failed on serial device.")); return ERR_TTY_IOCTL; } return 0; }
static int dev_reset(CableHandle *h) { #ifdef HAVE_LINUX_TICABLE_H { int arg = 0; if (ioctl(dev_fd, IOCTL_TIUSB_RESET_PIPES, arg) == -1) { ticables_warning(_("unable to reset pipes (ioctl).\n")); //return ERR_IOCTL; } } #endif return 0; }
int linux_check_parport(const char *devname) { int fd; ticables_info(_("Check for parport support:")); #ifdef HAVE_LINUX_PARPORT_H ticables_info(_(" parport support: available.")); #else ticables_info(_(" parport support: not compiled.")); return ERR_PPDEV; #endif // check for node usability ticables_info(_("Check for parport usability:")); if(check_for_node_usability(devname) == -1) return ERR_PPDEV; #ifdef HAVE_LINUX_PARPORT_H // check for device availability fd = open(devname, 0); if (fd == -1) { ticables_warning("unable to open parallel device '%s'.", devname); return ERR_PPDEV; } if (ioctl(fd, PPCLAIM) == -1) { ticables_info(_(" is useable: no")); close(fd); return ERR_PPDEV; } ticables_info(_(" is useable: yes")); if (ioctl(fd, PPRELEASE) == -1) { ticables_info("unable to release parallel device '%s'", devname); } close(fd); #endif return 0; }
static int gry_set_device(CableHandle *h, const char * device) { if (device != NULL) { char * device2 = strdup(device); if (device2 != NULL) { free(h->device); h->device = device2; } else { ticables_warning(_("unable to set device %s.\n"), device); } return 0; } return ERR_ILLEGAL_ARG; }
static int gry_timeout(CableHandle *h) { BOOL fSuccess; COMMTIMEOUTS cto; cto.ReadIntervalTimeout = 0; cto.ReadTotalTimeoutMultiplier = 0; cto.ReadTotalTimeoutConstant = 100 * h->timeout; cto.WriteTotalTimeoutMultiplier = 0; cto.WriteTotalTimeoutConstant = 100 * h->timeout; fSuccess = SetCommTimeouts(hCom, &cto); if (!fSuccess) { ticables_warning("SetCommTimeouts"); return ERR_GRY_SETCOMMTIMEOUT; } return 0; }
static int find_string_in_proc(char *entry, char *str) { FILE *f; char buffer[80]; int found = 0; f = fopen(entry, "rt"); if (f == NULL) { ticables_warning("can't open '%s'", entry); return -1; } while (!feof(f)) { fscanf(f, "%s", buffer); if (strstr(buffer, str)) found = 1; } fclose(f); return found; }
static const char* tigl_get_product(struct libusb_device *dev) { libusb_device_handle *han; int ret; static unsigned char string[64]; struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { ticables_error("failed to get device descriptor"); return ""; } if (desc.iProduct) { if (!libusb_open(dev, &han)) { ret = libusb_get_string_descriptor_ascii(han, desc.iProduct, string, sizeof(string)); libusb_close(han); if (ret > 0) { return (const char *) string; } else { ticables_warning("libusb_get_string_descriptor_ascii (%s).\n", tigl_strerror(ret)); return ""; } } else { return ""; } } return (const char *)string; }
/* Attempt to find if an user is attached to a group. - user [in] : a user name - group [in] : a group name */ static int search_for_user_in_group(const char *user, const char *group) { FILE *f; char buffer[256]; const char *entry = "/etc/group"; f = fopen(entry, "rt"); if (f == NULL) { ticables_warning(_("can't open '%s'."), entry); return -1; } while (!feof(f)) { if (!fgets(buffer, 256, f)) break; if (strstr(buffer, group)) { if(strstr(buffer, user)) { fclose(f); return 0; } else { fclose(f); return -1; } } } fclose(f); return -1; }
static int check_for_node_usability(const char *pathname) { struct stat st; if(!access(pathname, F_OK)) { ticables_info(_(" node %s: exists"), pathname); } else { ticables_info(_(" node %s: does not exist"), pathname); ticables_info(_(" => you will have to create the node.")); return -1; } if(!stat(pathname, &st)) { ticables_info(_(" permissions/user/group:%s%s %s"), get_attributes(st.st_mode), get_user_name(st.st_uid), get_group_name(st.st_gid)); } else { ticables_warning("can't stat '%s'.", pathname); return -1; } if(getuid() == st.st_uid) { ticables_info(_(" user can r/w on device: yes")); return 0; } else { ticables_info(_(" user can r/w on device: no")); } if((st.st_mode & S_IROTH) && (st.st_mode & S_IWOTH)) { ticables_info(_(" others can r/w on device: yes")); } else { char *user, *group; ticables_info(_(" others can r/w on device: no")); user = strdup(get_user_name(getuid())); group = strdup(get_group_name(st.st_gid)); if(!search_for_user_in_group(user, group)) { ticables_info(_(" is the user '%s' in the group '%s': yes"), user, group); } else { ticables_info(_(" is the user '%s' in the group '%s': no"), user, group); ticables_info(_(" => you should add your username at the group '%s' in '/etc/group'"), group); ticables_info(_(" => you will have to restart your session, too"), group); free(user); free(group); return -1; } free(user); free(group); } return 0; }