예제 #1
0
/* Init serial input */
int in_serial_init(struct flb_config *config)
{
    int fd;
    int ret;
    struct flb_in_serial_config *ctx;

    ctx = calloc(1, sizeof(struct flb_in_serial_config));
    if (!ctx) {
        perror("calloc");
        return -1;
    }

    if (!config->file) {
        flb_utils_error_c("serial input plugin needs configuration file");
        return -1;
    }

    serial_config_read(ctx, config->file);

    /* set context */
    ret = flb_input_set_context("serial", ctx, config);
    if (ret == -1) {
        flb_utils_error_c("Could not set configuration for"
                "serial input plugin");
    }

    if (ret == -1) {
        flb_utils_error_c("Could not set collector for serial input plugin");
    }

    /* initialize MessagePack buffers */
    msgpack_sbuffer_init(&ctx->mp_sbuf);
    msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);

    tcgetattr(fd, &ctx->tio_orig);
    memset(&ctx->tio, 0, sizeof(ctx->tio));
    switch (atoi(ctx->bitrate)) {
        case 1200:
            ctx->tio.c_cflag = B1200;
            break;
        case 2400:
            ctx->tio.c_cflag = B2400;
            break;
        case 4800:
            ctx->tio.c_cflag = B4800;
            break;
        case 9600:
            ctx->tio.c_cflag = B9600;
            break;
        case 19200:
            ctx->tio.c_cflag = B19200;
            break;
        case 38400:
            ctx->tio.c_cflag = B38400;
            break;

#ifdef __LINUX__
        case 576000:
            ctx->tio.c_cflag = B576000;
            break;
        case 115200:
            ctx->tio.c_cflag = B115200;
            break;
#endif

        default:
            flb_utils_error_c("Invalid bitrate for serial plugin");
    }

    ctx->tio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD;
    ctx->tio.c_iflag = IGNPAR | IGNCR;
    ctx->tio.c_oflag = 0;
    ctx->tio.c_lflag = ICANON;

    /* open device */
    fd = open(ctx->file, O_RDWR | O_NOCTTY);
    if (fd == -1) {
        perror("open");
        flb_utils_error_c("Could not open serial port device");
    }
    ctx->fd = fd;

    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &ctx->tio);

    /* Set our collector based on a file descriptor event */
    ret = flb_input_set_collector_event("serial",
                                        in_serial_collect,
                                        ctx->fd,
                                        config);
    return 0;
}
예제 #2
0
/* Init serial input */
int in_serial_init(struct flb_config *config)
{
    int fd;
    int ret;
    struct flb_in_serial_config *ctx;

    ctx = calloc(1, sizeof(struct flb_in_serial_config));
    if (!ctx) {
        perror("calloc");
        return -1;
    }

    if (!config->file) {
        flb_utils_error_c("serial input plugin needs configuration file");
        return -1;
    }

    serial_config_read(ctx, config->file);

    /* set context */
    ret = flb_input_set_context("serial", ctx, config);
    if (ret == -1) {
        flb_utils_error_c("Could not set configuration for"
                "serial input plugin");
    }

    if (ret == -1) {
        flb_utils_error_c("Could not set collector for serial input plugin");
    }

    /* initialize MessagePack buffers */
    msgpack_sbuffer_init(&ctx->mp_sbuf);
    msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);

    tcgetattr(fd, &ctx->tio_orig);
    memset(&ctx->tio, 0, sizeof(ctx->tio));
    ctx->tio.c_cflag = ctx->tio.c_ispeed = ctx->tio.c_ospeed = atoi(ctx->bitrate);
    ctx->tio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD;
    ctx->tio.c_iflag = IGNPAR | IGNCR;
    ctx->tio.c_oflag = 0;
    ctx->tio.c_lflag = ICANON;

    /* open device */
    fd = open(ctx->file, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (fd == -1) {
        perror("open");
        flb_utils_error_c("Could not open serial port device");
    }
    ctx->fd = fd;

    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &ctx->tio);

#if __linux__
    /* Set our collector based on a file descriptor event */
    ret = flb_input_set_collector_event("serial",
                                        in_serial_collect,
                                        ctx->fd,
                                        config);
#else
    /* Set our collector based on a timer event */
    ret = flb_input_set_collector_time("serial",
                                       in_serial_collect,
                                       IN_SERIAL_COLLECT_SEC,
                                       IN_SERIAL_COLLECT_NSEC,
                                       config);
#endif

    return 0;
}
예제 #3
0
/* Init serial input */
int in_serial_init(struct flb_input_instance *in,
                   struct flb_config *config, void *data)
{
    int fd;
    int ret;
    int br;
    struct flb_in_serial_config *ctx;
    (void) data;

    ctx = calloc(1, sizeof(struct flb_in_serial_config));
    if (!ctx) {
        perror("calloc");
        return -1;
    }

    if (!serial_config_read(ctx, in)) {
        return -1;
    }

    /* set context */
    flb_input_set_context(in, ctx);

    /* initialize MessagePack buffers */
    msgpack_sbuffer_init(&ctx->mp_sbuf);
    msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);

    /* open device */
    fd = open(ctx->file, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (fd == -1) {
        perror("open");
        flb_utils_error_c("Could not open serial port device");
    }
    ctx->fd = fd;

    /* Store original settings */
    tcgetattr(fd, &ctx->tio_orig);

    /* Reset for new... */
    memset(&ctx->tio, 0, sizeof(ctx->tio));
    tcgetattr(fd, &ctx->tio);

    br = atoi(ctx->bitrate);
    cfsetospeed(&ctx->tio, (speed_t) flb_serial_speed(br));
    cfsetispeed(&ctx->tio, (speed_t) flb_serial_speed(br));

    /* Settings */
    ctx->tio.c_cflag     &=  ~PARENB;        /* 8N1 */
    ctx->tio.c_cflag     &=  ~CSTOPB;
    ctx->tio.c_cflag     &=  ~CSIZE;
    ctx->tio.c_cflag     |=  CS8;
    ctx->tio.c_cflag     &=  ~CRTSCTS;       /* No flow control */
    ctx->tio.c_cc[VMIN]   =  ctx->min_bytes; /* Min number of bytes to read  */
    ctx->tio.c_cflag     |=  CREAD | CLOCAL; /* Enable READ & ign ctrl lines */

    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &ctx->tio);

#if __linux__
    /* Set our collector based on a file descriptor event */
    ret = flb_input_set_collector_event(in,
                                        in_serial_collect,
                                        ctx->fd,
                                        config);
#else
    /* Set our collector based on a timer event */
    ret = flb_input_set_collector_time(in,
                                       in_serial_collect,
                                       IN_SERIAL_COLLECT_SEC,
                                       IN_SERIAL_COLLECT_NSEC,
                                       config);
#endif

    return ret;
}