]> git.defcon.no Git - test-repo/blob - door_works.c
Ok.
[test-repo] / door_works.c
1 #include <termios.h> /* Terminal I/O support */
2 #include <errno.h> /* Error number definitions */
3 #include <string.h> /* String function definitions */
4 #include <stdio.h> /* Standard I/O */
5 #include <fcntl.h> /* File descriptor manipulation */
6
7 #define DEVICE "/dev/ttyUSB0"
8
9 int open_port ( char* device )
10 {
11 int f;
12 struct termios tio;
13
14 f = open ( DEVICE, O_RDWR | O_NOCTTY ); // Not using O_NDELAY, that caused ERR11: temp unavail.
15 if ( f < 0 )
16 {
17 perror(DEVICE);
18 return(-1);
19 }
20
21 // Fixed settings: 9600bps, 8 bits,
22 // no parity, no flow-control,
23 // local mode, read enabled
24 tio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
25 tio.c_cflag &= ~CRTSCTS;
26 tio.c_iflag = IGNPAR;
27 tio.c_oflag = 0;
28 tio.c_lflag = 0; // Non-canonical read, no other options.
29 tio.c_cc[VMIN] = 3; // Expect 3 characters, always.
30 tio.c_cc[VTIME] = 60; // Wait for a max of 6 seconds
31
32 tcsetattr(f,TCSANOW,&tio); // Apply this now.
33
34 return f;
35 }
36
37 int send_cmd ( int fd, char cmd )
38 {
39 int res;
40 char buf[7];
41 char status;
42
43 tcflush(fd, TCIFLUSH); // Flush the serial input buffer.
44
45 res = write( fd, &cmd , 1 ); // Send the command to the device
46 if ( res < 0 ) return -1; // and return on failure.
47
48 res = read( fd, buf, 5 );
49 if ( res < 0 )
50 {
51 printf("SERIAL read error %d: %s\n", errno, strerror(errno));
52 return -1;
53 }
54 if ( res == 0 )
55 {
56 printf ("Null read\n");
57 return -1;
58 }
59
60 buf[res] = 0x00;
61
62 // A response is supposed to be precisely three characters.
63 if ( res != 3 ) {
64 return -1;
65 }
66
67 // And the first is supposed to be an echo of the given command.
68 if ( buf[0] != cmd )
69 {
70 printf("Error: echoed command not correct\n");
71 return -1;
72 }
73
74 // The second is (a ':' is ignored, and the third is checked.
75 // The only valid responses are Locked, Open and Moving.
76 status = buf[2];
77
78 if ( (status == 'O') || (status == 'L') || (status == 'M') )
79 return status;
80 else return -1;
81 }
82
83 int get_state ( int fd )
84 {
85 return send_cmd( fd, 's'); // Yashure. KISS
86 }
87
88 int set_state ( int fd, int state )
89 {
90 int res;
91 switch ( state )
92 {
93 case 0:
94 res = send_cmd ( fd, 'o');
95 break;
96 case 1:
97 res = send_cmd ( fd, 'l');
98 break;
99 }
100 if ( (res != 'L') && ( res != 'O' ) ) return -1; // FAIL
101 // And, yes, I will call a status "Moving" a failed state-change attempt
102 return 0;
103 }
104
105
106 int main ( int argc, char** argv )
107 {
108 int fd;
109 int res;
110 int c;
111 char buf[255];
112
113 fd = open_port( DEVICE );
114 if ( fd < 0 )
115 {
116 perror(DEVICE);
117 return(-1);
118 }
119
120 printf("Enter command character, followed by enter/return\n");
121 printf("Valid commands are:\n l - Lock\n o - Open \n s - Status\n i - Invalid simulation\n");
122 printf("ESC Enter exits");
123 printf("rdy>");
124 for(;;)
125 {
126 res = getchar();
127 if ( res == 10 ) continue;
128
129 if ( res == 27 ) return 1;
130 switch( res )
131 {
132 case 'l':
133 printf("Locking...\n");
134 if ( set_state( fd, 1 ) ) printf ( "set_state() failed\n");
135 break;
136 case 'o':
137 printf("Opening...\n");
138 if ( set_state( fd, 0 ) ) printf ( "set_state() failed\n");
139 break;
140 case 's':
141 printf("Requesting status...\n");
142 res = get_state( fd );
143 switch ( res )
144 {
145 case 'O':
146 printf("Open\n");
147 break;
148 case 'L':
149 printf("Locked\n");
150 break;
151 case 'M':
152 printf("Moving\n");
153 break;
154 default:
155 printf("Unknown status %d\n", res);
156 }
157 break;
158 case 'i':
159 printf("Sending invalid command...\n");
160 res = send_cmd( fd, (char)res );
161 printf("Back from attempt. Got %d back\n", res);
162 default:
163 break;
164 }
165 printf("rdy>");
166 }
167 }
168
169