OpenCPN Partial API docs
Loading...
Searching...
No Matches
ocpnhelper.c
1/***************************************************************************
2 * Copyright (C) 2006 by dsr *
3 * dsr@mshome.net *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
20
21#include <unistd.h>
22#include <stdlib.h>
23#include <syslog.h>
24#include <signal.h>
25#include <errno.h>
26#include <ctype.h>
27#include <fcntl.h>
28#include <string.h>
29#include <stdarg.h>
30#include <setjmp.h>
31#include <stdio.h>
32#include <assert.h>
33#include <pwd.h>
34#include <stdbool.h>
35
36int main(int argc, char *argv[]) {
37 int option, debuglevel;
38
39 int inF, ouF, bytes;
40 char line[512];
41
42 while ((option = getopt(argc, argv, "D:SBUV")) != -1) {
43 switch (option) {
44 case 'B': {
45 inF = open("/proc/tty/driver/usb-serial", O_RDONLY);
46 if (inF == -1) inF = open("/proc/tty/driver/usbserial", O_RDONLY);
47
48 if (inF != -1) {
49 if ((ouF = open("/var/tmp/usbserial", O_WRONLY | O_CREAT | O_TRUNC,
50 0777)) != -1) {
51 while ((bytes = read(inF, line, sizeof(line))) > 0)
52 write(ouF, line, bytes);
53 close(ouF);
54 }
55 close(inF);
56 }
57 break;
58 }
59
60 case 'S': {
61 inF = open("/proc/tty/driver/serial", O_RDONLY);
62
63 if (inF != -1) {
64 if ((ouF = open("/var/tmp/serial", O_WRONLY | O_CREAT | O_TRUNC,
65 0777)) != -1) {
66 while ((bytes = read(inF, line, sizeof(line))) > 0)
67 write(ouF, line, bytes);
68 close(ouF);
69 }
70 close(inF);
71 }
72 break;
73 }
74
75 case 'U': {
76 /* Kill the helper files */
77 unlink("/var/tmp/usbserial");
78 unlink("/var/tmp/serial");
79 break;
80 }
81
82 case 'D': {
83 debuglevel = (int)strtol(optarg, 0, 0);
84 break;
85 }
86
87 case 'V': {
88 (void)printf("ocpnhelper %s\n", VERSION);
89 exit(0);
90 }
91
92 case 'h':
93 case '?':
94 default: {
95 // usage();
96 exit(0);
97 }
98 }
99 }
100
101 return 0;
102}