|
| 1 | +/* |
| 2 | + * Copyright (C)2019 Intel Corporation |
| 3 | + * SPDX-License-Identifier: BSD-3-Clause |
| 4 | + */ |
| 5 | + |
| 6 | +#include <errno.h> |
| 7 | +#include <fcntl.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | +#include <termios.h> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | +#define SOS_REQ "shutdown" |
| 15 | +#define UOS_ACK "acked" |
| 16 | +#define BUFF_SIZE 16U |
| 17 | +#define MSG_SIZE 8U |
| 18 | +#define NODE_SIZE 3U |
| 19 | + |
| 20 | +enum nodetype { |
| 21 | + NODE_UNKNOWN = 0, |
| 22 | + NODE_UOS_SERVER, |
| 23 | + NODE_SOS_CLIENT, |
| 24 | +}; |
| 25 | + |
| 26 | +int set_serial_interface_attributes(int fd, int speed) |
| 27 | +{ |
| 28 | + struct termios tty; |
| 29 | + |
| 30 | + if (tcgetattr(fd, &tty) < 0) { |
| 31 | + printf("Error from tcgetattr: %s\n", strerror(errno)); |
| 32 | + return errno; |
| 33 | + } |
| 34 | + |
| 35 | + cfsetospeed(&tty, (speed_t)speed); |
| 36 | + cfsetispeed(&tty, (speed_t)speed); |
| 37 | + |
| 38 | + /* set input-mode */ |
| 39 | + tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); |
| 40 | + |
| 41 | + /* set output-mode */ |
| 42 | + tty.c_oflag &= ~OPOST; |
| 43 | + |
| 44 | + /* set control-mode */ |
| 45 | + tty.c_cflag |= (CLOCAL | CREAD); |
| 46 | + tty.c_cflag &= ~CSIZE; |
| 47 | + tty.c_cflag |= CS8; |
| 48 | + tty.c_cflag &= ~PARENB; |
| 49 | + tty.c_cflag &= ~CSTOPB; |
| 50 | + tty.c_cflag &= ~CRTSCTS; |
| 51 | + |
| 52 | + /* set local-mode */ |
| 53 | + tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); |
| 54 | + |
| 55 | + /* block until one char read, set next char's timeout */ |
| 56 | + tty.c_cc[VMIN] = 1; |
| 57 | + tty.c_cc[VTIME] = 1; |
| 58 | + |
| 59 | + if (tcsetattr(fd, TCSANOW, &tty) != 0) { |
| 60 | + printf("Error from tcsetattr: %s\n", strerror(errno)); |
| 61 | + return errno; |
| 62 | + } |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +int main(int argc, char *argv[]) |
| 67 | +{ |
| 68 | + char *devname_uos = ""; |
| 69 | + int fd_uos = 0; |
| 70 | + unsigned char recvbuf[BUFF_SIZE]; |
| 71 | + enum nodetype node = NODE_UNKNOWN; |
| 72 | + |
| 73 | + if (argc <= 2) { |
| 74 | + printf("Too few options. Example: [./life_mngr uos /dev/ttyS1].\n"); |
| 75 | + return -EINVAL; |
| 76 | + } |
| 77 | + |
| 78 | + if (strncmp("uos", argv[1], NODE_SIZE) == 0) { |
| 79 | + node = NODE_UOS_SERVER; |
| 80 | + } else if (strncmp("sos", argv[1], NODE_SIZE) == 0) { |
| 81 | + node = NODE_SOS_CLIENT; |
| 82 | + } else { |
| 83 | + printf("Invalid param. Example: [./life_mngr uos /dev/ttyS1].\n"); |
| 84 | + return -EINVAL; |
| 85 | + } |
| 86 | + |
| 87 | + if (node == NODE_UOS_SERVER) { |
| 88 | + devname_uos = argv[2]; |
| 89 | + fd_uos = open(devname_uos, O_RDWR | O_NOCTTY | O_SYNC); |
| 90 | + if (fd_uos < 0) { |
| 91 | + printf("Error opening %s: %s\n", devname_uos, strerror(errno)); |
| 92 | + return errno; |
| 93 | + } |
| 94 | + set_serial_interface_attributes(fd_uos, B115200); |
| 95 | + } |
| 96 | + |
| 97 | + /* UOS-server wait for shutdown from SOS */ |
| 98 | + do { |
| 99 | + if (node == NODE_UOS_SERVER) { |
| 100 | + memset(recvbuf, 0, sizeof(recvbuf)); |
| 101 | + read(fd_uos, recvbuf, sizeof(recvbuf)); |
| 102 | + |
| 103 | + if (strncmp(SOS_REQ, recvbuf, MSG_SIZE) == 0) { |
| 104 | + write(fd_uos, UOS_ACK, sizeof(UOS_ACK)); |
| 105 | + printf("SOS start shutdown\n"); |
| 106 | + system("poweroff"); |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + } while (1); |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
0 commit comments