-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
199 lines (174 loc) · 5.21 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "config.h"
#include <libmicro/can.h>
#include <libmicro/can-tcp.h>
#include <libmicro/can-uart.h>
#include <libmicro/debug.h>
#include "cansole.h"
#include "cmds-base.h"
#include "cmds-test.h"
#include "cmds-borg.h"
#include "cmds-mood.h"
#include "cmds-musicd.h"
#include "cmds-flash.h"
#include "cmds-treppenblink.h"
#include "cmds-canir.h"
#include "cmds-cantemp.h"
#include "cmds-powercommander.h"
#include "cmds-bastel.h"
#include "cmds-powermeter.h"
#include "cmds-gateway.h"
// Atmel ; LAP includes
// #include "config.h"
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
// can-posix.c
void can_init_posix(cann_conn_t *aconn);
/**
* Available commands array
*/
typedef struct {
void (*fkt)(int, char**);
char *cmd;
char *sig;
char *desc;
} cmd_t;
cmd_t cmds[] = {
{ &cmd_loopback, "loopback", "loopback [0|1]", "Enable/disable loopback mode" },
{ &cmd_test, "test", "test <subcommand>", "Test commands" },
{ &cmd_packet, "packet", "packet <src> <dst> <data>", "Send arbitrary packets" },
{ &cmd_dump, "dump", "dump", "Packet dump from CAN bus" },
{ &cmd_reset, "reset", "reset <addr>", "Send reset to <addr>" },
{ &cmd_ping, "ping", "ping <addr>", "Send ping to <addr>" },
{ &cmd_flash, "flash", "flash <addr> <file>" ,"flash file to device"},
{ &cmd_lamp, "lamp", "lamp <addr> <lamp> <value>" ,"set lamp on device to value"},
{ &cmd_borg, "borg", "borg <subcommand>" ,"control bord device"},
{ &cmd_mood, "mood", "mood <subcommand>" ,"control mood device"},
{ &cmd_cansole, "cansole", "cansole <addr> <chan> <subchannel>", "connect to cansole at given address, channel and subchannel" },
{ &cmd_musicd, "musicd", "musicd", "starts a music control daemon in foreground" },
{ &cmd_treppenblink, "treppenblink", "treppenblink", "mode des treppenblinks" },
{ &cmd_canir, "canir", "canir", "control thinks supposed to accept ir-commands only via can" },
{ &cmd_powercommander, "powercommander", "powercommander", "powercommander class object function value ... wiki->Powercommander" },
{ &cmd_cantemp, "cantemp", "cantemp", "Temperatur fuer Sensor" },
{ &cmd_bastel, "bastelcmd", "bastelcmd", "bastelcontrol class object function value ... wiki->Bastelraum" },
{ &cmd_canpowermeter, "powermeter", "powermeter", "Energieverbrauch des Labors" },
{ &cmd_gateway, "gw", "gw <subcommand>", "CAN-Gateway Control (ping currently)" },
{ NULL, NULL, NULL, NULL }
};
static char *progname;
static char *optstring = "hv::S:s:p:";
struct option longopts[] =
{
{ "help", no_argument, NULL, 'h' },
{ "verbose", optional_argument, NULL, 'v' },
{ "server", required_argument, NULL, 's' },
{ "serial", required_argument, NULL, 'S' },
{ "port", required_argument, NULL, 'p' },
{ NULL, 0, NULL, 0 }
};
void help()
{
cmd_t *ncmd;
printf ("\nUsage: %s [OPTIONS] <COMMAND>\n", progname);
puts ( "\nOptions:\n\n"
" -h, --help display this help and exit\n"
" -v, --verbose be more verbose and display a CAN packet dump\n"
" -s, --server HOST use specified server (default: localhost)\n"
" -p, --port PORT use specified TCP/IP port (default: 2342)\n"
" -S, --serial PORT use specified serial port\n\n"
"Commands:\n");
ncmd = cmds;
while(ncmd->fkt) {
printf( " %-30s %s\n", ncmd->sig, ncmd->desc );
ncmd++;
}
printf( "\n" );
}
/**
* Main
*/
int main(int argc, char *argv[])
{
char *tcpport = "2342"; // TCP Port
char *server = "localhost"; //Sieht kaputt aus
char *serial = NULL;
int optc;
progname = argv[0];
int option_index = 0;
while ((optc = getopt_long(argc, argv, optstring, longopts, &option_index)) != EOF)
{
switch (optc)
{
case 'v':
if (optarg)
debug_level = atoi(optarg);
else
debug_level = 3;
break;
case 'S':
serial = optarg;
break;
case 's':
server = optarg;
break;
case 'p':
tcpport = optarg;
break;
case 'h':
help();
exit(EXIT_SUCCESS);
default:
help();
exit(EXIT_FAILURE);
}
} // while
cann_conn_t *conn = NULL;
//init debugging to console, otherwise we'll experience crashes...
debug_init(NULL);
if (optind == argc) {
help();
exit(EXIT_FAILURE);
}
char *arg = argv[optind];
cmd_t *cmd = cmds;
while (cmd->fkt) {
if (!strcmp(arg, cmd->cmd)) {
//connect only if a valid command has been detected
if (serial) {
debug(1, "Trying to establish CAN communication via serial %s", serial);
canu_init(serial);
can_init_posix(NULL); // use serial
} else {
debug(1, "Trying to establish CAN communication via cand (%s:%s)", server, tcpport);
conn = cann_connect(server, tcpport);
can_init_posix(conn); // use specified connection to cand
}
(*(cmd->fkt))(argc-optind, &(argv[optind]));
goto disconnect;
}
cmd++;
}
debug(0, "Command not understood");
help();
goto done;
disconnect:
//cann_close(0);
usleep(1000); //sleep a little, so last data on socket can be sent before closing
if (conn) {
close(conn->fd);
//cann_close_errors();
}
free(conn);
done:
debug_close();
return 0;
}