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