eqemu
view src/dev.cc @ 2:48dce4ee4850
the fake device is working
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 15 Jul 2014 14:37:51 +0300 |
parents | |
children | 977bc1cb055b |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <signal.h>
5 #include <limits.h>
6 #include <string>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <sys/wait.h>
10 #include <fcntl.h>
11 #include <termios.h>
12 #include "dev.h"
13 #include "timer.h"
15 int customer, ticket;
16 static int report_inputs, cmd_echo;
17 static long last_ticket_msec = LONG_MIN;
19 static void runcmd(const char *cmd);
21 static int fd = -1;
22 static FILE *fp;
23 static std::string cur_line;
25 int start_dev(const char *devpath)
26 {
27 if((fd = open(devpath, O_RDWR | O_NONBLOCK)) == -1) {
28 fprintf(stderr, "failed to open device: %s: %s\n", devpath, strerror(errno));
29 return -1;
30 }
31 if(isatty(fd)) {
32 struct termios term;
34 if(tcgetattr(fd, &term) == -1) {
35 perror("failed to retrieve terminal attributes");
36 stop_dev();
37 return -1;
38 }
39 term.c_cflag = CS8 | CLOCAL;
40 term.c_iflag &= ~(IXON | IXOFF);
41 term.c_lflag = 0;
43 cfsetispeed(&term, B38400);
44 cfsetospeed(&term, B38400);
46 if(tcsetattr(fd, TCSANOW, &term) == -1) {
47 perror("failed to set terminal attributes");
48 stop_dev();
49 return -1;
50 }
51 }
53 if(!(fp = fdopen(fd, "r+"))) {
54 perror("failed to attach an I/O stream to the device file\n");
55 stop_dev();
56 return -1;
57 }
58 setvbuf(fp, 0, _IONBF, 0);
60 return fd;
61 }
63 void stop_dev()
64 {
65 if(fp)
66 fclose(fp);
67 else if(fd >= 0)
68 close(fd);
69 }
72 void proc_dev_input()
73 {
74 int rdbytes;
75 char buf[256];
76 static bool skip_line;
78 while((rdbytes = read(fd, buf, sizeof buf - 1)) > 0) {
79 buf[rdbytes] = 0;
81 /* ignore our own crap */
82 if(memcmp(buf, "OK,", 3) == 0 || memcmp(buf, "ERR,", 4) == 0) {
83 skip_line = true;
84 }
86 for(int i=0; i<rdbytes; i++) {
87 if(buf[i] == '\n' || buf[i] == '\r') {
88 if(!cur_line.empty()) {
89 runcmd(cur_line.c_str());
90 cur_line.clear();
91 }
92 skip_line = false;
93 } else {
94 if(!skip_line) {
95 cur_line.push_back(buf[i]);
96 }
97 }
98 }
99 }
100 }
102 void issue_ticket()
103 {
104 ticket++;
105 last_ticket_msec = get_msec();
106 if(report_inputs) {
107 fprintf(fp, "ticket: %d\n", ticket);
108 }
109 }
111 void next_customer()
112 {
113 if(customer < ticket) {
114 customer++;
115 last_ticket_msec = LONG_MIN;
116 if(report_inputs) {
117 fprintf(fp, "customer: %d\n", customer);
118 }
119 }
120 }
122 #define VERSTR \
123 "Queue system emulator v0.1 by John Tsiombikas <nuclear@member.fsf.org>"
125 static void runcmd(const char *cmd)
126 {
127 printf("DBG: runcmd(\"%s\")\n", cmd);
129 switch(cmd[0]) {
130 case 'e':
131 cmd_echo = !cmd_echo;
132 fprintf(fp, "OK,turning echo %s\n", cmd_echo ? "on" : "off");
133 break;
135 case 'i':
136 report_inputs = !report_inputs;
137 fprintf(fp, "OK,turning input reports %s\n", report_inputs ? "on" : "off");
138 break;
140 case 'v':
141 fprintf(fp, "OK,%s\n", VERSTR);
142 break;
144 case 'r':
145 fprintf(fp, "OK,reseting queues\n");
146 customer = 0;
147 ticket = 0;
148 last_ticket_msec = LONG_MIN;
149 break;
151 case 't':
152 fprintf(fp, "OK,ticket: %d\r\n", ticket);
153 break;
155 case 'c':
156 fprintf(fp, "OK,customer: %d\r\n", customer);
157 break;
159 case 'q':
160 fprintf(fp, "OK,issuing queue ticket\n");
161 issue_ticket();
162 break;
164 case 'n':
165 fprintf(fp, "OK,next customer\n");
166 next_customer();
167 break;
169 case 'h':
170 fprintf(fp, "OK,commands: (e)cho, (v)ersion, (t)icket, (c)ustomer, "
171 "(n)ext, (q)ueue, (r)eset, (i)nput-reports, (h)elp.\n");
172 break;
174 default:
175 fprintf(fp, "ERR,unknown command: %s\n", cmd);
176 }
177 }