forked from projectara/gbsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cport.c
289 lines (248 loc) · 7.33 KB
/
cport.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Greybus Simulator
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
#include <stdlib.h>
#include <stdio.h>
#include <linux/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "gbsim.h"
#define ES1_MSG_SIZE (2 * 1024)
/* Receive buffer for all data arriving from the AP */
static char cport_rbuf[ES1_MSG_SIZE];
static char cport_tbuf[ES1_MSG_SIZE];
struct gbsim_cport *cport_find(uint16_t cport_id)
{
struct gbsim_cport *cport;
TAILQ_FOREACH(cport, &info.cports, cnode)
if (cport->hd_cport_id == cport_id)
return cport;
return NULL;
}
void allocate_cport(uint16_t cport_id, uint16_t hd_cport_id, int protocol_id)
{
struct gbsim_cport *cport;
cport = malloc(sizeof(*cport));
cport->id = cport_id;
cport->hd_cport_id = hd_cport_id;
cport->protocol = protocol_id;
TAILQ_INSERT_TAIL(&info.cports, cport, cnode);
}
void free_cport(struct gbsim_cport *cport)
{
TAILQ_REMOVE(&info.cports, cport, cnode);
free(cport);
}
void free_cports(void)
{
struct gbsim_cport *cport;
/*
* Linux doesn't have a foreach_safe version of tailq and so the dirty
* trick of 'goto again'.
*/
again:
TAILQ_FOREACH(cport, &info.cports, cnode) {
if (cport->hd_cport_id == GB_SVC_CPORT_ID)
continue;
free_cport(cport);
goto again;
}
reset_hd_cport_id();
}
static void get_protocol_operation(uint16_t cport_id, char **protocol,
char **operation, uint8_t type)
{
struct gbsim_cport *cport;
cport = cport_find(cport_id);
if (!cport) {
*protocol = "N/A";
*operation = "N/A";
return;
}
switch (cport->protocol) {
case GREYBUS_PROTOCOL_CONTROL:
*protocol = "CONTROL";
*operation = control_get_operation(type);
break;
case GREYBUS_PROTOCOL_SVC:
*protocol = "SVC";
*operation = svc_get_operation(type);
break;
case GREYBUS_PROTOCOL_GPIO:
*protocol = "GPIO";
*operation = gpio_get_operation(type);
break;
case GREYBUS_PROTOCOL_I2C:
*protocol = "I2C";
*operation = i2c_get_operation(type);
break;
case GREYBUS_PROTOCOL_UART:
*protocol = "UART";
*operation = uart_get_operation(type);
break;
case GREYBUS_PROTOCOL_LOOPBACK:
*protocol = "LOOPBACK";
*operation = loopback_get_operation(type);
break;
case GREYBUS_PROTOCOL_PWM:
*protocol = "PWM";
*operation = pwm_get_operation(type);
break;
case GREYBUS_PROTOCOL_SDIO:
*protocol = "SDIO";
*operation = sdio_get_operation(type);
break;
case GREYBUS_PROTOCOL_I2S_MGMT:
*protocol = "I2S_MGMT";
*operation = i2s_mgmt_get_operation(type);
break;
case GREYBUS_PROTOCOL_I2S_RECEIVER:
*protocol = "I2S_RECEIVER";
*operation = i2s_data_get_operation(type);
break;
case GREYBUS_PROTOCOL_I2S_TRANSMITTER:
*protocol = "I2S_TRANSMITTER";
*operation = i2s_data_get_operation(type);
break;
default:
*protocol = "(Unknown protocol)";
*operation = "(Unknown operation)";
break;
}
}
static int send_msg_to_ap(struct op_msg *op, uint16_t hd_cport_id,
uint16_t message_size, uint16_t id, uint8_t type,
uint8_t result)
{
char *protocol, *operation;
ssize_t nbytes;
op->header.size = htole16(message_size);
op->header.operation_id = id;
op->header.type = type;
op->header.result = result;
/* Store the cport id in the header pad bytes */
op->header.pad[0] = hd_cport_id & 0xff;
op->header.pad[1] = (hd_cport_id >> 8) & 0xff;
get_protocol_operation(hd_cport_id, &protocol, &operation,
type & ~OP_RESPONSE);
if (type & OP_RESPONSE)
gbsim_debug("Module -> AP CPort %hu %s %s response\n",
hd_cport_id, protocol, operation);
else
gbsim_debug("Module -> AP CPort %hu %s %s request\n",
hd_cport_id, protocol, operation);
/* Send the response to the AP */
if (verbose)
gbsim_dump(op, message_size);
nbytes = write(to_ap, op, message_size);
if (nbytes < 0)
return nbytes;
return 0;
}
int send_response(struct op_msg *op, uint16_t hd_cport_id,
uint16_t message_size, struct gb_operation_msg_hdr *oph,
uint8_t result)
{
return send_msg_to_ap(op, hd_cport_id, message_size, oph->operation_id,
oph->type | OP_RESPONSE, result);
}
int send_request(struct op_msg *op, uint16_t hd_cport_id,
uint16_t message_size, uint16_t id, uint8_t type)
{
return send_msg_to_ap(op, hd_cport_id, message_size, id, type, 0);
}
static int cport_recv_handler(struct gbsim_cport *cport,
void *rbuf, size_t rsize,
void *tbuf, size_t tsize)
{
switch (cport->protocol) {
case GREYBUS_PROTOCOL_CONTROL:
return control_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_SVC:
return svc_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_GPIO:
return gpio_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_I2C:
return i2c_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_UART:
return uart_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_PWM:
return pwm_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_SDIO:
return sdio_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_I2S_MGMT:
return i2s_mgmt_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_I2S_RECEIVER:
case GREYBUS_PROTOCOL_I2S_TRANSMITTER:
return i2s_data_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
case GREYBUS_PROTOCOL_LOOPBACK:
return loopback_handler(cport->id, cport->hd_cport_id, rbuf, rsize, tbuf, tsize);
default:
gbsim_error("handler not found for cport %u\n", cport->id);
return -EINVAL;
}
}
static void recv_handler(void *rbuf, size_t rsize, void *tbuf, size_t tsize)
{
struct gb_operation_msg_hdr *hdr = rbuf;
uint16_t hd_cport_id;
struct gbsim_cport *cport;
char *protocol, *operation, *type;
int ret;
if (rsize < sizeof(*hdr)) {
gbsim_error("short message received\n");
return;
}
/* Retreive the cport id stored in the header pad bytes */
hd_cport_id = hdr->pad[1] << 8 | hdr->pad[0];
cport = cport_find(hd_cport_id);
if (!cport) {
gbsim_error("message received for unknown cport id %u\n",
hd_cport_id);
return;
}
type = hdr->type & OP_RESPONSE ? "response" : "request";
get_protocol_operation(hd_cport_id, &protocol, &operation,
hdr->type & ~OP_RESPONSE);
/* FIXME: can identify module from our cport connection */
gbsim_debug("AP -> Module %hhu CPort %hu %s %s %s\n",
cport_to_module_id(hd_cport_id), cport->id,
protocol, operation, type);
if (verbose)
gbsim_dump(rbuf, rsize);
/* clear the cport id stored in the header pad bytes */
hdr->pad[0] = 0;
hdr->pad[1] = 0;
ret = cport_recv_handler(cport, rbuf, rsize, tbuf, tsize);
if (ret)
gbsim_debug("cport_recv_handler() returned %d\n", ret);
}
void recv_thread_cleanup(void *arg)
{
cleanup_endpoint(to_ap, "to_ap");
cleanup_endpoint(from_ap, "from_ap");
}
/*
* Repeatedly perform blocking reads to receive messages arriving
* from the AP.
*/
void *recv_thread(void *param)
{
while (1) {
ssize_t rsize;
rsize = read(from_ap, cport_rbuf, ES1_MSG_SIZE);
if (rsize < 0) {
gbsim_error("error %zd receiving from AP\n", rsize);
return NULL;
}
recv_handler(cport_rbuf, rsize, cport_tbuf, sizeof(cport_tbuf));
memset(cport_rbuf, 0, sizeof(cport_rbuf));
memset(cport_tbuf, 0, sizeof(cport_tbuf));
}
}