device: split console_fd handling away

Split console_fd handling to a separate source file to simplify
device.c.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
This commit is contained in:
Dmitry Baryshkov
2020-08-04 18:39:35 +03:00
committed by Bjorn Andersson
parent a2d9cb9969
commit 793969b11d
5 changed files with 85 additions and 34 deletions

View File

@@ -11,7 +11,7 @@ LDFLAGS := -ludev -lyaml
CLIENT_SRCS := cdba.c circ_buf.c
CLIENT_OBJS := $(CLIENT_SRCS:.c=.o)
SERVER_SRCS := cdba-server.c cdb_assist.c circ_buf.c conmux.c device.c device_parser.c fastboot.c alpaca.c
SERVER_SRCS := cdba-server.c cdb_assist.c circ_buf.c conmux.c device.c device_parser.c fastboot.c alpaca.c console.c
SERVER_OBJS := $(SERVER_SRCS:.c=.o)
$(CLIENT): $(CLIENT_OBJS)

70
console.c Normal file
View File

@@ -0,0 +1,70 @@
/*
* Copyright (c) 2020, Linaro Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/file.h>
#include <sys/stat.h>
#include <err.h>
#include <unistd.h>
#include "cdba-server.h"
#include "device.h"
static int console_data(int fd, void *data)
{
struct msg hdr;
char buf[128];
ssize_t n;
n = read(fd, buf, sizeof(buf));
if (n < 0)
return n;
hdr.type = MSG_CONSOLE;
hdr.len = n;
write(STDOUT_FILENO, &hdr, sizeof(hdr));
write(STDOUT_FILENO, buf, n);
return 0;
}
void console_open(struct device *device)
{
device->console_fd = tty_open(device->console_dev, &device->console_tios);
if (device->console_fd < 0)
err(1, "failed to open %s", device->console_dev);
watch_add_readfd(device->console_fd, console_data, device);
}
int console_write(struct device *device, const void *buf, size_t len)
{
return write(device->console_fd, buf, len);;
}

9
console.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef __CONSOLE_H__
#define __CONSOLE_H__
#include "device.h"
void console_open(struct device *device);
int console_write(struct device *device, const void *buf, size_t len);
#endif

View File

@@ -42,6 +42,7 @@
#include "cdba-server.h"
#include "device.h"
#include "fastboot.h"
#include "console.h"
#include "list.h"
#define ARRAY_SIZE(x) ((sizeof(x)/sizeof((x)[0])))
@@ -78,33 +79,6 @@ static void device_lock(struct device *device)
err(1, "failed to lock lockfile %s", lock);
}
static int device_console_data(int fd, void *data)
{
struct msg hdr;
char buf[128];
ssize_t n;
n = read(fd, buf, sizeof(buf));
if (n < 0)
return n;
hdr.type = MSG_CONSOLE;
hdr.len = n;
write(STDOUT_FILENO, &hdr, sizeof(hdr));
write(STDOUT_FILENO, buf, n);
return 0;
}
static void console_open(struct device *device)
{
device->console_fd = tty_open(device->console_dev, &device->console_tios);
if (device->console_fd < 0)
err(1, "failed to open %s", device->console_dev);
watch_add_readfd(device->console_fd, device_console_data, device);
}
struct device *device_open(const char *board,
struct fastboot_ops *fastboot_ops)
{
@@ -175,13 +149,9 @@ int device_write(struct device *device, const void *buf, size_t len)
if (!device)
return 0;
if (device->console_fd) {
return write(device->console_fd, buf, len);;
} else {
assert(device->write);
assert(device->write);
return device->write(device, buf, len);
}
return device->write(device, buf, len);
}
void device_fastboot_boot(struct device *device)

View File

@@ -36,6 +36,7 @@
#include "alpaca.h"
#include "cdb_assist.h"
#include "conmux.h"
#include "console.h"
#define TOKEN_LENGTH 16384
@@ -119,6 +120,7 @@ static void parse_board(struct device_parser *dp)
dev->power_off = alpaca_power_off;
} else if (!strcmp(key, "console")) {
dev->console_dev = strdup(value);
dev->write = console_write;
} else if (!strcmp(key, "voltage")) {
dev->voltage = strtoul(value, NULL, 10);
} else if (!strcmp(key, "fastboot")) {