forked from projectara/gbsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
153 lines (130 loc) · 2.8 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
/*
* Greybus Simulator
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <usbg/usbg.h>
#include "gbsim.h"
int bbb_backend = 0;
int i2c_adapter = 0;
int uart_portno = 0;
int uart_count = 0;
char *hotplug_basedir;
int verbose = 0;
static usbg_state *s;
static usbg_gadget *g;
static struct sigaction sigact;
struct gbsim_info info;
static void cleanup(void)
{
printf("cleaning up\n");
sigemptyset(&sigact.sa_mask);
uart_cleanup();
gadget_cleanup(s, g);
functionfs_cleanup();
svc_exit();
}
static void signal_handler(int sig)
{
if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM)
cleanup();
}
static void signals_init(void)
{
sigact.sa_handler = signal_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, (struct sigaction *)NULL);
sigaction(SIGHUP, &sigact, (struct sigaction *)NULL);
sigaction(SIGTERM, &sigact, (struct sigaction *)NULL);
}
int main(int argc, char *argv[])
{
int ret = -EINVAL;
int o;
while ((o = getopt(argc, argv, ":bh:i:u:U:v")) != -1) {
switch (o) {
case 'b':
bbb_backend = 1;
printf("bbb_backend %d\n", bbb_backend);
break;
case 'h':
hotplug_basedir = optarg;
printf("hotplug_basedir %s\n", hotplug_basedir);
break;
case 'i':
i2c_adapter = atoi(optarg);
printf("i2c_adapter %d\n", i2c_adapter);
break;
case 'u':
uart_portno = atoi(optarg);
printf("uart_portno %d\n", uart_portno);
break;
case 'U':
uart_count = atoi(optarg);
printf("uart_count %d\n", uart_count);
break;
case 'v':
verbose = 1;
printf("verbose %d\n", verbose);
break;
case ':':
if (optopt == 'i')
gbsim_error("i2c_adapter required\n");
else if (optopt == 'h')
gbsim_error("hotplug_basedir required\n");
else if (optopt == 'u')
gbsim_error("uart_portno required\n");
else if (optopt == 'U')
gbsim_error("uart_count required\n");
else
gbsim_error("-%c requires an argument\n",
optopt);
return 1;
case '?':
if (isprint(optopt))
gbsim_error("unknown option -%c'\n", optopt);
else
gbsim_error("unknown option character 0x%02x\n",
optopt);
return 1;
default:
abort();
}
}
if (!hotplug_basedir) {
gbsim_error("hotplug directory not specified, aborting\n");
return 1;
}
signals_init();
TAILQ_INIT(&info.cports);
ret = gadget_create(&s, &g);
if (ret < 0)
goto out;
ret = functionfs_init();
if (ret < 0)
goto out;
ret = gadget_enable(g);
if (ret < 0)
goto out;
/* Protocol handlers */
svc_init();
gpio_init();
i2c_init();
i2s_init();
uart_init();
sdio_init();
loopback_init();
ret = functionfs_loop();
out:
return ret;
}