]> git.defcon.no Git - hm-trp-tool/blob - set_speed.c
Adding QT4-based GUI tool. More-or-less useable
[hm-trp-tool] / set_speed.c
1 #include <errno.h> /* Error number definitions */
2 #include <string.h> /* String function definitions */
3 #include <stdio.h> /* Standard I/O */
4 #include <unistd.h> /* sleep */
5 #include <stdlib.h> /* malloc, free */
6
7 #include "hm-trp.h"
8 #include "serial.h"
9
10 #define CBUFFER_SIZE 32
11
12 int main ( int argc, char** argv )
13 {
14 int fd;
15 int res;
16 int r, c, f;
17 long rate;
18
19 unsigned char buf[CBUFFER_SIZE];
20
21 config_t* config = malloc(sizeof(config_t));
22 bzero(config, sizeof(config_t));
23
24 if ( ! argv[1] )
25 {
26 printf("Serial port device required as argument, e.g. /dev/ttyUSB0\n");
27 return 1;
28 }
29
30 if ( ! argv[2] )
31 {
32 printf("Desired bit-rate required. Use RS232 rates only.\n");
33 return 1;
34 }
35
36 rate = atol( argv[2] );
37 for ( r = 0; r <= count_rates; r++ )
38 if ( port_rate_value[r] == rate ) { rate = r; break; }
39
40 if ( r >= count_rates ) { printf("Invalid data rate requested\n"); return 1; }
41
42 printf("Looking for device on port %s\n", argv[1]);
43
44 for ( r = 0; r <= count_rates; r++ )
45 {
46 fd = open_port( argv[1], port_rates[r] );
47 if ( fd < 0 )
48 {
49 perror(argv[1]);
50 return(-1);
51 }
52
53 if ( read_config( fd, config ) == 1 )
54 {
55 printf("Found device at baud-rate %d.\n", port_rate_value[r] );
56
57 if ( r == rate && ( config->air_rate == config->uart_rate ) )
58 {
59 printf("Data rate is already set to %d\n", port_rate_value[r] );
60 return 0;
61 }
62
63 write_cmd( fd, cmd_air_rate );
64 write_uint32_t( fd, port_rate_value[rate] );
65
66 if ( ! read_ok( fd ) )
67 {
68 printf("Air rate NOT set\n");
69 return 1;
70 }
71
72 write_cmd( fd, cmd_uart_rate );
73 write_uint32_t( fd, port_rate_value[rate] );
74
75 read_ok( fd ); // Clear buffer.
76
77 printf("Device UART and air data rate set to %d\n", port_rate_value[rate] );
78 return(0);
79 }
80 close(fd);
81 }
82 printf("Unable to find device on %s\n", argv[1]);
83 return(1);
84 }
85