]> git.defcon.no Git - hm-trp-tool/blob - hm-trp.c
I forgot, all source needs README and licensing
[hm-trp-tool] / hm-trp.c
1 #include "hm-trp.h"
2
3 #include <stdio.h>
4
5 #ifndef CBUFFER_SIZE
6 #define CBUFFER_SIZE 32
7 #endif
8
9 const int count_devtype = 4;
10 const char* dev_name[4] = {
11 "HM-TRP-433",
12 "HM-TRP-470",
13 "HM-TRP-868",
14 "HM-TRP-915"
15 };
16
17 const uint32_t freq_min[4] = {
18 414 * 1e6,
19 450 * 1e6,
20 849 * 1e6,
21 895 * 1e6
22 };
23 const uint32_t freq_max[4] = {
24 454 * 1e6,
25 490 * 1e6,
26 889 * 1e6,
27 935 * 1e6
28 };
29 const uint32_t freq_default[4] = {
30 434 * 1e6,
31 470 * 1e6,
32 869 * 1e6,
33 915 * 1e6
34 };
35
36 const int count_recv_bw = 12;
37 // Values selected somewhat randomly...
38 const int recv_bw[12] = {
39 30,
40 50,
41 75,
42 90,
43 105,
44 120,
45 150,
46 175,
47 200,
48 250,
49 300,
50 620
51 };
52
53 const int count_freq_dev = 10;
54 // Values selected somewhat randomly...
55 const int freq_dev[10] = { // Values as kHz ..
56 10,
57 20,
58 35,
59 50,
60 55,
61 80,
62 110,
63 120,
64 145,
65 160
66 };
67
68 const int count_powerlevel = 8;
69 const int powerlevel[8] = { 1, 2, 5, 8, 11, 14, 17, 20 };
70
71 const int count_rates = 9;
72 const int port_rate_value[9] = {
73 1200,
74 1800,
75 2400,
76 4800,
77 9600,
78 19200,
79 38400,
80 57600,
81 115200
82 };
83
84 const int port_rates[9] = {
85 B1200,
86 B1800,
87 B2400,
88 B4800,
89 B9600,
90 B19200,
91 B38400,
92 B57600,
93 B115200
94 };
95
96 const char cmd_reset[3] = { 0xAA, 0xFA, 0xF0 };
97 const char cmd_config[3] = { 0xAA, 0xFA, 0xE1 };
98 const char cmd_frequency[3] = { 0xAA, 0xFA, 0xD2 };
99 const char cmd_air_rate[3] = { 0xAA, 0xFA, 0xC3 };
100 const char cmd_bw[3] = { 0xAA, 0xFA, 0xB4 };
101 const char cmd_deviation[3] = { 0xAA, 0xFA, 0xA5 };
102 const char cmd_power[3] = { 0xAA, 0xFA, 0x96 };
103 const char cmd_uart_rate[3] = { 0xAA, 0xFA, 0x1E };
104 const char cmd_RSSI[3] = { 0xAA, 0xFA, 0x87 };
105 const char cmd_SNR[3] = { 0xAA, 0xFA, 0x78 };
106
107 int write_uint32_t ( int f, uint32_t v )
108 {
109 int i;
110 unsigned char buf[4];
111 v = __bswap_32(v);
112 for ( i = 0; i < 4; i++ )
113 buf[i] = (v>>(8*i) & 0xff );
114
115 return write( f, buf, 4 );
116 }
117
118 int write_uint16_t ( int f, uint16_t v )
119 {
120 int i;
121 unsigned char buf[2];
122 v = __bswap_16(v);
123 for ( i = 0; i < 2; i++ )
124 buf[i] = (v>>(8*i) & 0xff );
125
126 return write( f, buf, 2 );
127 }
128
129 int write_uint8_t ( int f, uint8_t v )
130 {
131 int i;
132 unsigned char buf[2];
133
134 //for ( i = 0; i < 2; i++ )
135 // buf[i] = (v>>(8*i) & 0xff );
136
137 return write( f, &v, 1 );
138 }
139
140 int write_cmd ( int f, const char* buf )
141 {
142 return write( f, buf, 3 ); // Send the command to the device
143 }
144
145 int read_config ( int fd, config_t * config )
146 {
147
148 unsigned char buf[CBUFFER_SIZE];
149 int res;
150
151 res = write_cmd( fd, cmd_config ); // Send the command to the device
152 if ( res < 0 ) return(-1); // and die on failure..
153
154 int i = 0;
155 uint8_t* tmp_config = (uint8_t*)config;
156 do {
157 bzero(buf, CBUFFER_SIZE);
158 res = read( fd, buf, 1 );
159
160 if ( res )
161 {
162 *tmp_config++ = (uint8_t)buf[0];
163 // Make sure wo don't overflow the config struct.
164 if ( (void*)tmp_config > ((void*)config+sizeof(config_t)) ) return(-2);
165 }
166
167 } while ( res > 0 );
168
169 if ( res < 0 )
170 {
171 return -1;
172 }
173
174 if ( config->freq )
175 {
176 config->freq = __bswap_32( config->freq );
177 config->air_rate = __bswap_32( config->air_rate );
178 config->bw = __bswap_16( config->bw );
179 config->uart_rate = __bswap_32( config->uart_rate );
180 return 1;
181 }
182
183 return 0; // No errors, but no valid config
184 }
185
186 int read_ok ( int fd )
187 {
188 unsigned char ok[5];
189 int i = 0;
190 unsigned char c;
191 bzero(ok, 5);
192
193 while( (read( fd, &c, 1 ) > 0 ) && ( i < 4) ) ok[i++] = c;
194 if ( strcmp( ok, "OK\r\n" ) == 0 ) return 1;
195 return 0;
196 }