| 1 |
// Copyright 2019-2022, Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: BSL-1.0 |
| 3 |
/*! |
| 4 |
* @file |
| 5 |
* @brief USB Serial implementation for OpenGloves. |
| 6 |
* @author Daniel Willmott <[email protected]> |
| 7 |
* @ingroup drv_opengloves |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <string.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#include <fcntl.h> |
| 15 |
#include <errno.h> |
| 16 |
#include <termios.h> |
| 17 |
#include <sys/ioctl.h> |
| 18 |
|
| 19 |
#include "opengloves_serial.h" |
| 20 |
|
| 21 |
#include "util/u_misc.h" |
| 22 |
|
| 23 |
|
| 24 |
static int |
| 25 |
opengloves_serial_read(struct opengloves_communication_device *ocdev, char *data, size_t length) |
| 26 |
{ |
| 27 |
struct opengloves_serial_device *osdev = (struct opengloves_serial_device *)ocdev; |
| 28 |
int ret = read(osdev->fd, data, length); |
| 29 |
|
| 30 |
return ret; |
| 31 |
} |
| 32 |
|
| 33 |
static int |
| 34 |
opengloves_serial_write(struct opengloves_communication_device *ocdev, const char *data, size_t length) |
| 35 |
{ |
| 36 |
struct opengloves_serial_device *osdev = (struct opengloves_serial_device *)ocdev; |
| 37 |
|
| 38 |
return write(osdev->fd, data, length); |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
opengloves_serial_destroy(struct opengloves_communication_device *ocdev) |
| 43 |
{ |
| 44 |
struct opengloves_serial_device *osdev = (struct opengloves_serial_device *)ocdev; |
| 45 |
|
| 46 |
close(osdev->fd); |
| 47 |
free(osdev); |
| 48 |
} |
| 49 |
|
| 50 |
int |
| 51 |
opengloves_serial_open(const char *path, struct opengloves_communication_device **out_comm_dev) |
| 52 |
{ |
| 53 |
int fd = open(path, O_RDWR); |
| 54 |
|
| 55 |
// error opening file |
| 56 |
if (fd < 0) { |
| 57 |
return -errno; |
| 58 |
} |
| 59 |
|
| 60 |
// read existing settings |
| 61 |
struct termios tty; |
| 62 |
if (tcgetattr(fd, &tty) != 0) { |
| 63 |
return -errno; |
| 64 |
} |
| 65 |
|
| 66 |
tty.c_cflag &= ~PARENB; |
| 67 |
tty.c_cflag &= ~CSTOPB; |
| 68 |
tty.c_cflag &= ~CSIZE; |
| 69 |
tty.c_cflag |= CS8; |
| 70 |
tty.c_cflag |= CREAD | CLOCAL; |
| 71 |
tty.c_cflag &= ~CRTSCTS; |
| 72 |
|
| 73 |
tty.c_lflag &= ~ICANON; |
| 74 |
tty.c_lflag &= ~ECHO; |
| 75 |
tty.c_lflag &= ~ECHOE; |
| 76 |
tty.c_lflag &= ~ECHONL; |
| 77 |
tty.c_lflag &= ~ISIG; |
| 78 |
tty.c_iflag &= ~(IXON | IXOFF | IXANY); |
| 79 |
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); |
| 80 |
|
| 81 |
tty.c_oflag &= ~OPOST; |
| 82 |
tty.c_oflag &= ~ONLCR; |
| 83 |
|
| 84 |
tty.c_cc[VTIME] = 10; |
| 85 |
tty.c_cc[VMIN] = 0; |
| 86 |
|
| 87 |
// baud rates |
| 88 |
cfsetispeed(&tty, B115200); |
| 89 |
cfsetospeed(&tty, B115200); |
| 90 |
|
| 91 |
if (tcsetattr(fd, TCSANOW, &tty) != 0) { |
| 92 |
return -errno; |
| 93 |
} |
| 94 |
|
| 95 |
struct opengloves_serial_device *osdev = U_TYPED_CALLOC(struct opengloves_serial_device); |
| 96 |
|
| 97 |
osdev->base.read = opengloves_serial_read; |
| 98 |
osdev->base.write = opengloves_serial_write; |
| 99 |
osdev->base.destroy = opengloves_serial_destroy; |
| 100 |
|
| 101 |
osdev->fd = fd; |
| 102 |
|
| 103 |
*out_comm_dev = &osdev->base; |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |