yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * remserial |
| 3 | * Copyright (C) 2000 Paul Davis |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <termios.h> |
| 22 | #include <unistd.h> |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include "../zlog_com.h" |
| 26 | |
| 27 | /** |
| 28 | * ºê¶¨Òå |
| 29 | */ |
| 30 | #define CFLG 0 |
| 31 | #define IFLG 1 |
| 32 | #define OFLG 2 |
| 33 | #define LFLG 3 |
| 34 | #define RFLG 4 |
| 35 | #define BFLG 5 |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Íⲿ±äÁ¿ÒýÓà |
| 40 | */ |
| 41 | extern int errno; |
| 42 | |
| 43 | /** |
| 44 | * È«¾Ö±äÁ¿¶¨Òå |
| 45 | */ |
| 46 | static struct sttyset { |
| 47 | char *name; |
| 48 | int which; |
| 49 | int mask; |
| 50 | int value; |
| 51 | } sttynames[] = { |
| 52 | { "raw", RFLG, 0, 0}, |
| 53 | { "0", BFLG, 0, B0}, |
| 54 | { "50", BFLG, 0, B50}, |
| 55 | { "75", BFLG, 0, B75}, |
| 56 | { "110", BFLG, 0, B110}, |
| 57 | { "134", BFLG, 0, B134}, |
| 58 | { "150", BFLG, 0, B150}, |
| 59 | { "200", BFLG, 0, B200}, |
| 60 | { "300", BFLG, 0, B300}, |
| 61 | { "600", BFLG, 0, B600}, |
| 62 | { "1200", BFLG, 0, B1200}, |
| 63 | { "1800", BFLG, 0, B1800}, |
| 64 | { "2400", BFLG, 0, B2400}, |
| 65 | { "4800", BFLG, 0, B4800}, |
| 66 | { "9600", BFLG, 0, B9600}, |
| 67 | { "19200", BFLG, 0, B19200}, |
| 68 | { "38400", BFLG, 0, B38400}, |
| 69 | #ifdef B57600 |
| 70 | { "57600", BFLG, 0, B57600}, |
| 71 | #endif |
| 72 | #ifdef B115200 |
| 73 | { "115200", BFLG, 0, B115200}, |
| 74 | #endif |
| 75 | #ifdef B230400 |
| 76 | { "230400", BFLG, 0, B230400}, |
| 77 | #endif |
| 78 | { "cs7", CFLG, CSIZE, CS7}, |
| 79 | { "cs8", CFLG, CSIZE, CS8}, |
| 80 | { "cstopb", CFLG, CSTOPB, CSTOPB}, |
| 81 | { "cread", CFLG, CREAD, CREAD}, |
| 82 | { "parenb", CFLG, PARENB, PARENB}, |
| 83 | { "parodd", CFLG, PARODD, PARODD}, |
| 84 | { "hubcl", CFLG, HUPCL, HUPCL}, |
| 85 | { "clocal", CFLG, CLOCAL, CLOCAL}, |
| 86 | #ifdef CRTSCTS |
| 87 | { "crtscts", CFLG, CRTSCTS, CRTSCTS}, |
| 88 | #endif |
| 89 | #ifdef ORTSFL |
| 90 | { "ortsfl", CFLG, ORTSFL, ORTSFL}, |
| 91 | #endif |
| 92 | #ifdef CTSFLOW |
| 93 | { "ctsflow", CFLG, CTSFLOW, CTSFLOW}, |
| 94 | #endif |
| 95 | #ifdef RTSFLOW |
| 96 | { "rtsflow", CFLG, RTSFLOW, RTSFLOW}, |
| 97 | #endif |
| 98 | { "ignbrk", IFLG, IGNBRK, IGNBRK}, |
| 99 | { "brkint", IFLG, BRKINT, BRKINT}, |
| 100 | { "ignpar", IFLG, IGNPAR, IGNPAR}, |
| 101 | { "parmrk", IFLG, PARMRK, PARMRK}, |
| 102 | { "inpck", IFLG, INPCK, INPCK}, |
| 103 | { "istrip", IFLG, ISTRIP, ISTRIP}, |
| 104 | { "inlcr", IFLG, INLCR, INLCR}, |
| 105 | { "igncr", IFLG, IGNCR, IGNCR}, |
| 106 | { "icrnl", IFLG, ICRNL, ICRNL}, |
| 107 | #ifdef IUCLC // Missing on OSX, FreeBSD |
| 108 | { "iuclc", IFLG, IUCLC, IUCLC}, |
| 109 | #endif |
| 110 | { "ixon", IFLG, IXON, IXON}, |
| 111 | { "ixany", IFLG, IXANY, IXANY}, |
| 112 | { "ixoff", IFLG, IXOFF, IXOFF}, |
| 113 | #ifdef IMAXBEL |
| 114 | { "imaxbel", IFLG, IMAXBEL, IMAXBEL}, |
| 115 | #endif |
| 116 | { "opost", OFLG, OPOST, OPOST}, |
| 117 | #ifdef ILCUC // Missing on OSX, FreeBSD |
| 118 | { "olcuc", OFLG, OLCUC, OLCUC}, |
| 119 | #endif |
| 120 | { "onlcr", OFLG, ONLCR, ONLCR}, |
| 121 | { "ocrnl", OFLG, OCRNL, OCRNL}, |
| 122 | { "onocr", OFLG, ONOCR, ONOCR}, |
| 123 | { "onlret", OFLG, ONLRET, ONLRET}, |
| 124 | { "ofil", OFLG, OFILL, OFILL}, |
| 125 | { "ofdel", OFLG, OFDEL, OFDEL}, |
| 126 | { "nl0", OFLG, NLDLY, NL0}, |
| 127 | { "nl1", OFLG, NLDLY, NL1}, |
| 128 | { "cr0", OFLG, CRDLY, CR0}, |
| 129 | { "cr1", OFLG, CRDLY, CR1}, |
| 130 | { "cr2", OFLG, CRDLY, CR2}, |
| 131 | { "cr3", OFLG, CRDLY, CR3}, |
| 132 | { "tab0", OFLG, TABDLY, TAB0}, |
| 133 | { "tab1", OFLG, TABDLY, TAB1}, |
| 134 | { "tab2", OFLG, TABDLY, TAB2}, |
| 135 | { "tab3", OFLG, TABDLY, TAB3}, |
| 136 | { "bs0", OFLG, BSDLY, BS0}, |
| 137 | { "bs1", OFLG, BSDLY, BS1}, |
| 138 | { "vt0", OFLG, VTDLY, VT0}, |
| 139 | { "vt1", OFLG, VTDLY, VT1}, |
| 140 | { "ff0", OFLG, FFDLY, FF0}, |
| 141 | { "ff1", OFLG, FFDLY, FF1}, |
| 142 | { "isig", LFLG, ISIG, ISIG}, |
| 143 | { "icanon", LFLG, ICANON, ICANON}, |
| 144 | #ifdef XCASE // Missing on OSX, FreeBSD |
| 145 | { "xcase", LFLG, XCASE, XCASE}, |
| 146 | #endif |
| 147 | { "echo", LFLG, ECHO, ECHO}, |
| 148 | { "echoe", LFLG, ECHOE, ECHOE}, |
| 149 | { "echok", LFLG, ECHOK, ECHOK}, |
| 150 | { "echonl", LFLG, ECHONL, ECHONL}, |
| 151 | { "noflsh", LFLG, NOFLSH, NOFLSH}, |
| 152 | { "tostop", LFLG, TOSTOP, TOSTOP}, |
| 153 | #ifdef ECHOCTL |
| 154 | { "echoctl", LFLG, ECHOCTL, ECHOCTL}, |
| 155 | #endif |
| 156 | #ifdef ECHOPRT |
| 157 | { "echoprt", LFLG, ECHOPRT, ECHOPRT}, |
| 158 | #endif |
| 159 | #ifdef ECHOKE |
| 160 | { "echoke", LFLG, ECHOKE, ECHOKE}, |
| 161 | #endif |
| 162 | #ifdef FLUSHO |
| 163 | { "flusho", LFLG, FLUSHO, FLUSHO}, |
| 164 | #endif |
| 165 | #ifdef PENDIN |
| 166 | { "pendin", LFLG, PENDIN, PENDIN}, |
| 167 | #endif |
| 168 | { "iexten", LFLG, IEXTEN, IEXTEN}, |
| 169 | #ifdef TOSTOP |
| 170 | { "tostop", LFLG, TOSTOP, TOSTOP}, |
| 171 | #endif |
| 172 | { NULL, 0, 0, 0} |
| 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * @brief ÉèÖÃttyÉ豸µÄ²¨ÌØÂÊ |
| 177 | * @param[in] term¡¢p¡¢turnon |
| 178 | * @return void |
| 179 | * @note |
| 180 | * @see |
| 181 | */ |
| 182 | static void set_this_tty(struct termios *term, struct sttyset *p, int turnon) |
| 183 | { |
| 184 | /* |
| 185 | pdebug(5,"set_this_tty: setting %s on? %d\n",p->name,turnon); |
| 186 | */ |
| 187 | switch (p->which) { |
| 188 | case CFLG: |
| 189 | term->c_cflag &= ~(p->mask); |
| 190 | if (turnon) |
| 191 | term->c_cflag |= p->value; |
| 192 | break; |
| 193 | case IFLG: |
| 194 | term->c_iflag &= ~(p->mask); |
| 195 | if (turnon) |
| 196 | term->c_iflag |= p->value; |
| 197 | break; |
| 198 | case OFLG: |
| 199 | term->c_oflag &= ~(p->mask); |
| 200 | if (turnon) |
| 201 | term->c_oflag |= p->value; |
| 202 | break; |
| 203 | case LFLG: |
| 204 | term->c_lflag &= ~(p->mask); |
| 205 | if (turnon) |
| 206 | term->c_lflag |= p->value; |
| 207 | break; |
| 208 | case RFLG: |
| 209 | term->c_iflag = 0; |
| 210 | term->c_oflag = 0; |
| 211 | term->c_lflag = 0; |
| 212 | term->c_cc[VMIN] = 1; |
| 213 | term->c_cc[VTIME] = 0; |
| 214 | break; |
| 215 | case BFLG: |
| 216 | cfsetispeed(term, p->value); |
| 217 | cfsetospeed(term, p->value); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @brief ÉèÖÃttyÉ豸µÄ²¨ÌØÂÊ |
| 224 | * @param[in] fdÉ豸ÎļþÃèÊö·û |
| 225 | * @param[in] settings ²¨ÌØÂÊÖµ |
| 226 | * @return ³É¹¦·µ»Ø0£¬Ê§°Ü·µ»Ø-1 |
| 227 | * @note |
| 228 | * @see |
| 229 | */ |
| 230 | int set_tty(int fd, char *settings) |
| 231 | { |
| 232 | register char *p; |
| 233 | register char *s; |
| 234 | struct termios term; |
| 235 | register int i; |
| 236 | int mode; |
| 237 | |
| 238 | printf("set_tty: baud rate is %s, and the default baud is %s\n", settings, ZLOG_DEFAULT_TTYBAUD); |
| 239 | |
| 240 | if (tcgetattr(fd,&term) == -1) { |
| 241 | /* |
| 242 | pdebug(4,"set_tty: cannot get settings for fd %d, error %d\n", |
| 243 | fd,errno); |
| 244 | */ |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | s = strdup(settings); |
| 249 | p = strtok(s, " \t\n"); |
| 250 | while (p) { |
| 251 | mode = 1; |
| 252 | if (*p == '-') { |
| 253 | mode = 0; |
| 254 | p++; |
| 255 | } |
| 256 | for (i=0; sttynames[i].name; i++) { |
| 257 | if (!strcmp(p,sttynames[i].name)) { |
| 258 | set_this_tty(&term,&sttynames[i],mode); |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | p = strtok(NULL, " \t\n"); |
| 263 | } |
| 264 | free(s); |
| 265 | if (tcsetattr(fd, TCSANOW, &term) == -1) { |
| 266 | /* |
| 267 | pdebug(4,"set_tty: cannot get settings for fd %d error %d\n", |
| 268 | fd,errno); |
| 269 | */ |
| 270 | return -1; |
| 271 | } |
| 272 | else |
| 273 | return 0; |
| 274 | } |
| 275 | |