aboutsummaryrefslogtreecommitdiff
path: root/src/modules/bt_cfg/serial_config.hpp
blob: 523ea938cb331cde054f18ade84b0733279825fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once

#include <termios.h>

void
termios_raw_mode(struct termios & mode) {
	mode.c_iflag = 0;
	mode.c_oflag = 0;
	mode.c_lflag = 0;

	mode.c_cflag &= ~CSIZE;
	mode.c_cflag |= CS8;
	mode.c_cflag &= ~CSTOPB;
	mode.c_cflag &= ~(PARENB | PARODD);
	mode.c_cflag |= CREAD;
	mode.c_cflag |= CLOCAL;
}

bool
serial_set_raw(int fd) {
	struct termios mode;
	bool ok = tcgetattr(fd, &mode) == 0;
	if (ok)
	{
		termios_raw_mode(mode);
		ok = tcsetattr(fd, TCSANOW, &mode) == 0;
	}
	return ok;
}

bool
serial_set_speed(int fd, speed_t speed) {
	struct termios mode;
	return tcgetattr(fd, &mode) == 0
		and cfsetspeed(&mode, speed) == 0
		and tcsetattr(fd, TCSANOW, &mode) == 0;
}