// License: CC0 (https://creativecommons.org/publicdomain/zero/1.0/) // // To the extent possible under law, all copyright and related or neighboring rights are waived // on this file. // This work is published from: United States. // // Example usage: // // socat -d -d pty,raw,echo=0,link=/tmp/ttyPIUich pty,raw,echo=0,link=/tmp/ttyPIU // ./ichthyic-passthrough -i /tmp/ttyPIUich -I /dev/i2c-1 // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ICHTHYIC_MAX_BUF 256 #define ICHTHYIC_VERSION "0.1.0" #define ICHTHYIC_DEFAULT_I2C_ADDR 0x08 int g_verbose = 0; struct option g_long_option[] = { {"pty", required_argument, 0, 'i'}, {"i2c", required_argument, 0, 'I'}, {"log", required_argument, 0, 'L'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; typedef struct ichthyic_type { uint8_t *i2c_fn; int i2c_adapter; int i2c_worker_addr; int i2c_fd; uint32_t i2c_from_buf_n; uint8_t *i2c_from_buf; uint32_t i2c_to_buf_n; uint8_t *i2c_to_buf; } ichthyic_t; void ichthyic_init(ichthyic_t *ich) { ich->i2c_fn = (uint8_t *)malloc(sizeof(uint8_t)*ICHTHYIC_MAX_BUF); ich->i2c_from_buf = (uint8_t *)malloc(sizeof(uint8_t)*ICHTHYIC_MAX_BUF); ich->i2c_to_buf = (uint8_t *)malloc(sizeof(uint8_t)*ICHTHYIC_MAX_BUF); ich->i2c_from_buf[0] = '\0'; ich->i2c_fn[0] = '\0'; ich->i2c_adapter = -1; ich->i2c_worker_addr = -1; ich->i2c_fd = -1; ich->i2c_to_buf_n = 0; ich->i2c_to_buf[0] = '\0'; ich->i2c_from_buf_n = 0; ich->i2c_from_buf[0] = '\0'; } void ichthyic_clear(ichthyic_t *ich) { if (ich) { if (ich->i2c_fn) { free(ich->i2c_fn); } if (ich->i2c_from_buf) { free(ich->i2c_from_buf); } if (ich->i2c_to_buf) { free(ich->i2c_to_buf); } } } void ichthyic_free(ichthyic_t *ich) { ichthyic_clear(ich); if (ich) { free(ich); } } ichthyic_t *ichthyic_alloc() { ichthyic_t *ich; ich = malloc(sizeof(ichthyic_t)); ichthyic_init(ich); return ich; } int ichthyic_i2c_appendbuf(ichthyic_t *ich, uint8_t *buf, uint32_t nbuf) { int i, p; p = ich->i2c_to_buf_n; for (i=0, p=ich->i2c_to_buf_n; (ii2c_to_buf[p] = buf[i]; } ich->i2c_to_buf[p] = '\0'; ich->i2c_to_buf_n = p; } int ichthyic_i2c_queuebuf(ichthyic_t *ich, uint8_t *buf, uint32_t nbuf) { int i; for (i=0; ii2c_to_buf[i] = buf[i]; } ich->i2c_to_buf[nbuf] = '\0'; ich->i2c_to_buf_n = nbuf; } int ichthyic_i2c_read(ichthyic_t *ich) { int32_t res, dn=0; res = i2c_smbus_read_byte(ich->i2c_fd); if (res<0) { return -1; } if (res==0) { return 0; } dn=1; while (ich->i2c_from_buf_n < (ICHTHYIC_MAX_BUF-1)) { ich->i2c_from_buf[ich->i2c_from_buf_n] = res; ich->i2c_from_buf_n++; ich->i2c_from_buf[ich->i2c_from_buf_n] = '\0'; res = i2c_smbus_read_byte(ich->i2c_fd); if (res<0) { return -1; } if (res==0) { break; } dn++; } return dn; } int ichthyic_i2c_write(ichthyic_t *ich) { int i; int32_t res; for (i=0; ich->i2c_to_buf[i]; i++) { res = i2c_smbus_write_byte(ich->i2c_fd, ich->i2c_to_buf[i]); } ich->i2c_to_buf_n = 0; ich->i2c_to_buf[0] = '\0'; } int ichthyic_i2c_writebuf(ichthyic_t *ich, uint8_t *buf, uint32_t nbuf) { uint32_t i; int32_t res; for (i=0; ii2c_fd, buf[i]); } } void show_version(FILE *fp) { fprintf(fp, "ichthyic-passthrough version: %s\n", ICHTHYIC_VERSION); } void show_help(FILE *fp, char *i2c_fn, char *pty_fn) { show_version(fp); fprintf(fp, "\nusage:\n\n ichthyic-passthrough [-i pty] [-I i2c] [-L logfn] [-h] [-v] [-V]\n\n"); fprintf(fp, " [-D] use defaults (i2c:%s, pty:%s)\n", i2c_fn, pty_fn); fprintf(fp, " [-i pty] pty (--pty)\n"); fprintf(fp, " [-I i2c] I2C device interface (--i2c)\n"); fprintf(fp, " [-L logfn] log file (--log)\n"); fprintf(fp, " [-v] verbosity level (can be used multiple times to increase verbosity) (--verbose)\n"); fprintf(fp, " [-V] print out version and exit (--version)\n"); fprintf(fp, " [-h] help (this screen) (--help)\n"); fprintf(fp, "\n"); } void _print_buf(FILE *fp, char *buf, int buf_n) { int i; for (i=0; i= ' ') && (buf[i] <= '~')) { fprintf(fp,"%c", buf[i]); } else { fprintf(fp, "<%x>", buf[i]); } } fprintf(fp, "\n"); fflush(fp); } int main(int argc, char **argv) { int i, ret, fd, adapter_nr, ch, dn; int opt_idx; char fn[32]; int worker_addr = 0x08; struct timespec _sleepy, _sleepy_rem; char *pty_fn; int pty_fd=-1, pty_fd_flag = 0; int _n=0, _buf_n=32, _s=0; char _buf[32]; FILE *log_fp = stdout; char *log_fn; int _ok = 0; ichthyic_t ich; pty_fn = (char *)malloc(sizeof(char)*ICHTHYIC_MAX_BUF); log_fn = (char *)malloc(sizeof(char)*ICHTHYIC_MAX_BUF); ichthyic_init(&ich); ich.i2c_worker_addr = ICHTHYIC_DEFAULT_I2C_ADDR; ich.i2c_adapter = 1; snprintf(ich.i2c_fn, 31, "/dev/i2c-%i", ich.i2c_adapter); g_verbose = 0; pty_fn[0] = '\0'; log_fn[0] = '\0'; while ((ch = getopt_long(argc, argv, "hi:I:L:vVD", g_long_option, &opt_idx)) >= 0) { switch(ch) { case 0: break; case 'D': _ok = 1; break; case 'i': strncpy(pty_fn, optarg, ICHTHYIC_MAX_BUF-1); pty_fn[ICHTHYIC_MAX_BUF-1] = '\0'; _ok=1; break; case 'I': strncpy(ich.i2c_fn, optarg, ICHTHYIC_MAX_BUF-1); ich.i2c_fn[ICHTHYIC_MAX_BUF-1] = '\0'; _ok=1; break; case 'L': strncpy(log_fn, optarg, ICHTHYIC_MAX_BUF-1); log_fn[ICHTHYIC_MAX_BUF-1] = '\0'; break; case 'v': g_verbose++; _ok=1; break; case 'V': show_version(stdout); exit(0); case 'h': show_help(stdout, ich.i2c_fn, pty_fn); exit(0); default: show_help(stderr, ich.i2c_fn, pty_fn); exit(-1); break; } } if (_ok==0) { show_help(stderr, ich.i2c_fn, pty_fn); exit(-1); } if ((g_verbose > 0) && (strlen(log_fn) > 0)) { log_fp = fopen(log_fn, "w"); if (!log_fp) { perror(log_fn); exit(-1); } } // 1000000000 ns in 1 sec // _sleepy.tv_sec = 0; _sleepy.tv_nsec = 1000000; // stdin // if (strlen((const char *)pty_fn) == 0) { pty_fd = 0; } else { pty_fd = open(pty_fn, O_RDWR); if (pty_fd < 0) { perror(pty_fn); exit(-1); } } pty_fd_flag = fcntl(pty_fd, F_GETFL, 0); fcntl(pty_fd, F_SETFL, pty_fd_flag | O_NONBLOCK); ich.i2c_fd = open(ich.i2c_fn, O_RDWR); if (ich.i2c_fd < 0) { perror(ich.i2c_fn); exit(-1); } if (ioctl(ich.i2c_fd, I2C_SLAVE, ich.i2c_worker_addr) < 0) { perror(ich.i2c_fn); exit(-2); } while (1) { _n = read(pty_fd, _buf, _buf_n); if (_n<0) { // pty_fd is non blocking, so only non-EAGAIN // errors are real // if (errno != EAGAIN) { perror("stdin:"); } } // A 0-byte read means the file descriptor has been closed // else if (_n==0) { if (g_verbose>0) { fprintf(log_fp, "# input closed, cleaning up\n"); fflush(log_fp); } break; } // Otherwise we have actual data that we want to append // to our buffer // if (_n>0) { if (g_verbose > 0) { fprintf(log_fp, "#[%i]:", _n); for (i=0; i<_n; i++) { fprintf(log_fp, "%c", _buf[i]); } fprintf(log_fp, "\n"); fflush(log_fp); } ichthyic_i2c_appendbuf(&ich, _buf, _n); } // Write any data to our I2C Line // ichthyic_i2c_write(&ich); // And read any I2C data that might be ready // ret = ichthyic_i2c_read(&ich); if (ret<0) { perror("i2c read"); } else if (ret > 0) { if (g_verbose > 1) { fprintf(log_fp, "## read +%i (%i,%i) i2c bytes\n", ret, ret, ich.i2c_from_buf_n); fflush(log_fp); } } if (ich.i2c_from_buf_n > 0) { // If our buffer has a lf or cr at the end, flush it // if ((ich.i2c_from_buf[ ich.i2c_from_buf_n-1 ] == '\r') || (ich.i2c_from_buf[ ich.i2c_from_buf_n-1 ] == '\n')) { _n = ich.i2c_from_buf_n; _s = 0; do { dn = write(pty_fd, ich.i2c_from_buf + _s, _n); if (dn<0) { perror("write error"); exit(-1); } _n -= dn; _s += dn; } while (_n > 0); if (g_verbose > 0) { fprintf(log_fp, "# [%i]>:", ich.i2c_from_buf_n); _print_buf(log_fp, ich.i2c_from_buf, ich.i2c_from_buf_n); } ich.i2c_from_buf[0] = '\0'; ich.i2c_from_buf_n = 0; } // If we've hit the maximum size of our buffer, flush it regardless // else if (ich.i2c_from_buf_n >= (ICHTHYIC_MAX_BUF-1)) { _n = ich.i2c_from_buf_n; _s = 0; do { dn = write(pty_fd, ich.i2c_from_buf + _s, _n); if (dn<0) { perror("write error while flushing"); exit(-1); } _n -= dn; _s += dn; } while (_n > 0); if (g_verbose > 0) { fprintf(log_fp, "# i2c_from_buf_n (%i) == %i, flushing: ", ich.i2c_from_buf_n, ICHTHYIC_MAX_BUF); _print_buf(log_fp, ich.i2c_from_buf, ich.i2c_from_buf_n); } ich.i2c_from_buf[0] = '\0'; ich.i2c_from_buf_n = 0; } } nanosleep(&_sleepy, &_sleepy_rem); } close(ich.i2c_fd); ichthyic_clear(&ich); if (log_fp != stdout) { fclose(log_fp); } }