-
Notifications
You must be signed in to change notification settings - Fork 2
/
serial.c
273 lines (234 loc) · 7.55 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (C) 2020 Rhizomatica <[email protected]>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*
* Rhizo-HF-Connector
*
*/
#include <sys/types.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <strings.h>
#include <asm/termbits.h>
#include <errno.h>
#include <threads.h>
#include <stdint.h>
#include <pthread.h>
#include "uuardopd.h"
#include "serial.h"
extern controller_conn *radio_conn;
struct baudrate {
char *name;
int termios_code;
int nonstd_speed;
int bootrom_code;
int xram_records;
};
int open_serial_port(char *ttyport)
{
int target_fd = open(ttyport, O_RDWR|O_NONBLOCK);
if (target_fd < 0)
{
fprintf(stderr, "open() serial port error\n");
perror(ttyport);
exit(EXIT_FAILURE);
}
ioctl(target_fd, TIOCEXCL);
return target_fd;
}
struct baudrate baud_rate_table[] = {
/* the first listed rate will be our default */
{"115200", B115200, 0, 0, 100},
{"57600", B57600, 0, 1, 100},
{"38400", B38400, 0, 2, 100},
{"19200", B19200, 0, 4, 50},
/* Non-standard high baud rates */
{"812500", BOTHER, 812500, -1, 1000},
{"406250", BOTHER, 406250, -1, 500},
{"203125", BOTHER, 203125, -1, 250},
/* table search terminator */
{NULL, B0, 0, -1, 0},
};
struct baudrate *find_baudrate_by_name(char *srch_name)
{
struct baudrate *br;
for (br = baud_rate_table; br->name; br++)
if (!strcmp(br->name, srch_name))
break;
if (br->name)
return(br);
else
{
fprintf(stderr, "error: baud rate \"%s\" not known\n", srch_name);
return(NULL);
}
}
struct baudrate *set_serial_baudrate(struct baudrate *br, int target_fd)
{
struct termios2 target_termios;
target_termios.c_iflag = IGNBRK;
target_termios.c_oflag = 0;
target_termios.c_cflag = br->termios_code | CLOCAL|HUPCL|CREAD|CS8;
target_termios.c_lflag = 0;
target_termios.c_cc[VMIN] = 1;
target_termios.c_cc[VTIME] = 0;
target_termios.c_ispeed = br->nonstd_speed;
target_termios.c_ospeed = br->nonstd_speed;
if (ioctl(target_fd, TCSETSF2, &target_termios) < 0) {
fprintf(stderr, "ioctl() TCSETSF2 error\n");
perror("TCSETSF2");
exit(1);
}
return br;
}
void set_fixed_baudrate(char *baudname, int target_fd)
{
struct baudrate *br;
br = find_baudrate_by_name(baudname);
if (!br)
exit(1); /* error msg already printed */
set_serial_baudrate(br, target_fd);
}
void key_on(int serial_fd, int radio_type)
{
if (radio_type == RADIO_TYPE_ICOM)
{
int key_on_size = 8;
uint8_t key_on[8];
key_on[0] = 0xFE;
key_on[1] = 0xFE;
key_on[2] = 0x88;
key_on[3] = 0xE0;
key_on[4] = 0x1C;
key_on[5] = 0x00;
key_on[6] = 0x01;
key_on[7] = 0xFD;
write(serial_fd, key_on, key_on_size);
}
if (radio_type == RADIO_TYPE_UBITX)
{
int key_on_size = 5;
uint8_t key_on[5];
key_on[0] = 0x00;
key_on[1] = 0x00;
key_on[2] = 0x00;
key_on[3] = 0x00;
key_on[4] = 0x08;
write(serial_fd, key_on, key_on_size);
}
if (radio_type == RADIO_TYPE_SHM)
{
pthread_mutex_lock(&radio_conn->cmd_mutex);
radio_conn->service_command[0] = radio_conn->service_command[1] = radio_conn->service_command[2] = radio_conn->service_command[3] = 0x00;
radio_conn->service_command[4] = 0x08;
pthread_cond_signal(&radio_conn->cmd_condition);
pthread_mutex_unlock(&radio_conn->cmd_mutex);
}
}
void key_off(int serial_fd, int radio_type)
{
if (radio_type == RADIO_TYPE_ICOM)
{
int key_off_size = 8;
uint8_t key_off[8];
key_off[0] = 0xFE;
key_off[1] = 0xFE;
key_off[2] = 0x88;
key_off[3] = 0xE0;
key_off[4] = 0x1C;
key_off[5] = 0x00;
key_off[6] = 0x00;
key_off[7] = 0xFD;
write(serial_fd, key_off, key_off_size);
}
if (radio_type == RADIO_TYPE_UBITX)
{
int key_off_size = 5;
uint8_t key_off[5];
key_off[0] = 0x00;
key_off[1] = 0x00;
key_off[2] = 0x00;
key_off[3] = 0x00;
key_off[4] = 0x88;
write(serial_fd, key_off, key_off_size);
}
if (radio_type == RADIO_TYPE_SHM)
{
pthread_mutex_lock(&radio_conn->cmd_mutex);
radio_conn->service_command[0] = radio_conn->service_command[1] = radio_conn->service_command[2] = radio_conn->service_command[3] = 0x00;
radio_conn->service_command[4] = 0x88;
pthread_cond_signal(&radio_conn->cmd_condition);
pthread_mutex_unlock(&radio_conn->cmd_mutex);
}
}
void connected_led_on(int serial_fd, int radio_type)
{
if (radio_type == RADIO_TYPE_SHM)
{
pthread_mutex_lock(&radio_conn->cmd_mutex);
radio_conn->service_command[0] = 0x01; // led on
radio_conn->service_command[1] = radio_conn->service_command[2] = radio_conn->service_command[3] = 0x00;
radio_conn->service_command[4] = 0xa0; // CMD_SET_CONNECTED_STATUS
pthread_cond_signal(&radio_conn->cmd_condition);
pthread_mutex_unlock(&radio_conn->cmd_mutex);
// read response... no
}
}
void connected_led_off(int serial_fd, int radio_type)
{
if (radio_type == RADIO_TYPE_SHM)
{
pthread_mutex_lock(&radio_conn->cmd_mutex);
radio_conn->service_command[0] = 0x00; // led off
radio_conn->service_command[1] = radio_conn->service_command[2] = radio_conn->service_command[3] = 0x00;
radio_conn->service_command[4] = 0xa0; // CMD_SET_CONNECTED_STATUS
pthread_cond_signal(&radio_conn->cmd_condition);
pthread_mutex_unlock(&radio_conn->cmd_mutex);
// read response... no
}
}
void sys_led_on(int serial_fd, int radio_type)
{
if (radio_type == RADIO_TYPE_SHM)
{
pthread_mutex_lock(&radio_conn->cmd_mutex);
radio_conn->service_command[0] = 0x01; // led on
radio_conn->service_command[1] = radio_conn->service_command[2] = radio_conn->service_command[3] = 0x00;
radio_conn->service_command[4] = 0x9e; // CMD_SET_LED_STATUS
pthread_cond_signal(&radio_conn->cmd_condition);
pthread_mutex_unlock(&radio_conn->cmd_mutex);
// read response... no
}
}
void sys_led_off(int serial_fd, int radio_type)
{
if (radio_type == RADIO_TYPE_SHM)
{
pthread_mutex_lock(&radio_conn->cmd_mutex);
radio_conn->service_command[0] = 0x00; // led off
radio_conn->service_command[1] = radio_conn->service_command[2] = radio_conn->service_command[3] = 0x00;
radio_conn->service_command[4] = 0x9e; // CMD_SET_LED_STATUS
pthread_cond_signal(&radio_conn->cmd_condition);
pthread_mutex_unlock(&radio_conn->cmd_mutex);
// read response... no
}
}