2 eqemu - electronic queue system emulator
3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>,
4 Eleni-Maria Stea <eleni@mutantstargoat.com>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 void post_redisplay(); // defined in main.cc
36 static int report_inputs, cmd_echo;
37 static long last_ticket_msec = LONG_MIN;
39 static void runcmd(const char *cmd);
43 static std::string cur_line;
45 int start_dev(const char *devpath)
47 if((fd = open(devpath, O_RDWR | O_NONBLOCK)) == -1) {
48 fprintf(stderr, "failed to open device: %s: %s\n", devpath, strerror(errno));
54 if(tcgetattr(fd, &term) == -1) {
55 perror("failed to retrieve terminal attributes");
59 term.c_cflag = CS8 | CLOCAL;
60 term.c_iflag &= ~(IXON | IXOFF);
63 cfsetispeed(&term, B38400);
64 cfsetospeed(&term, B38400);
66 if(tcsetattr(fd, TCSANOW, &term) == -1) {
67 perror("failed to set terminal attributes");
73 if(!(fp = fdopen(fd, "r+"))) {
74 perror("failed to attach an I/O stream to the device file\n");
78 setvbuf(fp, 0, _IONBF, 0);
96 static bool skip_line;
98 while((rdbytes = read(fd, buf, sizeof buf - 1)) > 0) {
101 /* ignore our own crap */
102 if(memcmp(buf, "OK,", 3) == 0 || memcmp(buf, "ERR,", 4) == 0) {
106 for(int i=0; i<rdbytes; i++) {
107 if(buf[i] == '\n' || buf[i] == '\r') {
108 if(!cur_line.empty()) {
109 runcmd(cur_line.c_str());
115 cur_line.push_back(buf[i]);
125 last_ticket_msec = get_msec();
127 fprintf(fp, "ticket: %d\n", ticket);
135 if(customer < ticket) {
137 last_ticket_msec = LONG_MIN;
139 fprintf(fp, "customer: %d\n", customer);
146 #define TICKET_SHOW_DUR 1000
148 int get_display_number()
150 if(get_msec() - last_ticket_msec < TICKET_SHOW_DUR) {
156 int get_led_state(int led)
158 int ledon = get_msec() - last_ticket_msec < TICKET_SHOW_DUR ? 0 : 1;
159 return led == ledon ? 1 : 0;
163 "Queue system emulator v0.1"
165 static void runcmd(const char *cmd)
167 printf("DBG: runcmd(\"%s\")\n", cmd);
171 cmd_echo = !cmd_echo;
172 fprintf(fp, "OK,turning echo %s\n", cmd_echo ? "on" : "off");
176 report_inputs = !report_inputs;
177 fprintf(fp, "OK,turning input reports %s\n", report_inputs ? "on" : "off");
181 fprintf(fp, "OK,%s\n", VERSTR);
185 fprintf(fp, "OK,reseting queues\n");
188 last_ticket_msec = LONG_MIN;
193 fprintf(fp, "OK,ticket: %d\r\n", ticket);
197 fprintf(fp, "OK,customer: %d\r\n", customer);
201 fprintf(fp, "OK,issuing queue ticket\n");
206 fprintf(fp, "OK,next customer\n");
211 fprintf(fp, "OK,commands: (e)cho, (v)ersion, (t)icket, (c)ustomer, "
212 "(n)ext, (q)ueue, (r)eset, (i)nput-reports, (h)elp.\n");
216 fprintf(fp, "ERR,unknown command: %s\n", cmd);