Skip to content

Commit 2f1caa2

Browse files
committed
tty: split tty_open to a separate file
Split the function tty_open() to a separate file. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent f6bd2d0 commit 2f1caa2

File tree

9 files changed

+55
-33
lines changed

9 files changed

+55
-33
lines changed

cdba-server.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,6 @@ static const char *username;
2626

2727
struct device *selected_device;
2828

29-
int tty_open(const char *tty, struct termios *old)
30-
{
31-
struct termios tios;
32-
int ret;
33-
int fd;
34-
35-
fd = open(tty, O_RDWR | O_NOCTTY | O_EXCL);
36-
if (fd < 0)
37-
err(1, "unable to open \"%s\"", tty);
38-
39-
ret = tcgetattr(fd, old);
40-
if (ret < 0)
41-
err(1, "unable to retrieve \"%s\" tios", tty);
42-
43-
memset(&tios, 0, sizeof(tios));
44-
tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
45-
tios.c_iflag = IGNPAR;
46-
tios.c_oflag = 0;
47-
48-
tcflush(fd, TCIFLUSH);
49-
50-
ret = tcsetattr(fd, TCSANOW, &tios);
51-
if (ret < 0)
52-
err(1, "unable to update \"%s\" tios", tty);
53-
54-
return fd;
55-
}
56-
5729
static void fastboot_opened(struct fastboot *fb, void *data)
5830
{
5931
const uint8_t one = 1;

cdba-server.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#include "cdba.h"
88

9-
int tty_open(const char *tty, struct termios *old);
10-
119
void cdba_send_buf(int type, size_t len, const void *buf);
1210
#define cdba_send(type) cdba_send_buf(type, 0, NULL)
1311

console.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "cdba-server.h"
1515
#include "device.h"
16+
#include "tty.h"
1617
#include "watch.h"
1718

1819
struct console {

drivers/alpaca.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include <termios.h>
1818
#include <unistd.h>
1919

20-
#include "cdba-server.h"
2120
#include "device.h"
21+
#include "tty.h"
2222

2323
struct alpaca {
2424
int alpaca_fd;

drivers/cdb_assist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#include <termios.h>
1818
#include <unistd.h>
1919

20-
#include "cdba-server.h"
2120
#include "device.h"
2221
#include "status.h"
22+
#include "tty.h"
2323
#include "watch.h"
2424

2525
struct cdb_assist {

drivers/qcomlt_dbg.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "cdba-server.h"
2222
#include "device.h"
2323
#include "status.h"
24+
#include "tty.h"
2425
#include "watch.h"
2526

2627
enum qcomlt_parse_state {

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ server_srcs = ['cdba-server.c',
8989
'ppps.c',
9090
'status.c',
9191
'status-cmd.c',
92-
'watch.c']
92+
'watch.c',
93+
'tty.c']
9394

9495
build_server = true
9596
foreach d: server_deps

tty.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2016-2018, Linaro Ltd.
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: BSD-3-Clause
6+
*/
7+
8+
#include <fcntl.h>
9+
#include <termios.h>
10+
#include <unistd.h>
11+
#include <err.h>
12+
#include <string.h>
13+
14+
#include "tty.h"
15+
16+
int tty_open(const char *tty, struct termios *old)
17+
{
18+
struct termios tios;
19+
int ret;
20+
int fd;
21+
22+
fd = open(tty, O_RDWR | O_NOCTTY | O_EXCL);
23+
if (fd < 0)
24+
err(1, "unable to open \"%s\"", tty);
25+
26+
ret = tcgetattr(fd, old);
27+
if (ret < 0)
28+
err(1, "unable to retrieve \"%s\" tios", tty);
29+
30+
memset(&tios, 0, sizeof(tios));
31+
tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
32+
tios.c_iflag = IGNPAR;
33+
tios.c_oflag = 0;
34+
35+
tcflush(fd, TCIFLUSH);
36+
37+
ret = tcsetattr(fd, TCSANOW, &tios);
38+
if (ret < 0)
39+
err(1, "unable to update \"%s\" tios", tty);
40+
41+
return fd;
42+
}

tty.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef __TTY_H__
2+
#define __TTY_H__
3+
4+
struct termios;
5+
int tty_open(const char *tty, struct termios *old);
6+
7+
#endif

0 commit comments

Comments
 (0)