-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathloopback.c
229 lines (201 loc) · 5.19 KB
/
loopback.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
/*
* Greybus Simulator
*
* Copyright 2015 Google Inc.
* Copyright 2015 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <libsoc_gpio.h>
#include <linux/fs.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include "gbsim.h"
/* Misc */
#define GB_LOOPBACK_MAX 4
#define GB_OPERATION_DATA_SIZE_MAX \
(0x800 - sizeof(struct gb_loopback_transfer_request))
enum {
LOOPBACK_FSM_IDLE = 0,
LOOPBACK_FSM_PING_HOST,
LOOPBACK_FSM_TRANSFER_HOST,
LOOPBACK_FSM_SINK_HOST,
};
struct gb_loopback {
uint16_t cport_id;
uint16_t hd_cport_id;
uint8_t id;
bool init;
pthread_mutex_t loopback_data;
uint8_t module_id;
uint32_t ms;
size_t size;
int state;
};
static struct gb_loopback gblb;
static bool terminate_thread;
static int thread_started;
static int port_count;
static pthread_t loopback_pthread;
static pthread_barrier_t loopback_barrier;
static int gb_loopback_ping_host(struct gb_loopback *gblbp)
{
/* TBD */
return 0;
}
static int gb_loopback_transfer_host(struct gb_loopback *gblbp, size_t size)
{
/* TBD */
return 0;
}
static int gb_loopback_sink_host(struct gb_loopback *gblbp, size_t size)
{
/* TBD */
return 0;
}
/* Analog based on the firmware loop */
static void *loopback_thread(void *param)
{
int state;
pthread_barrier_wait(&loopback_barrier);
while (!terminate_thread) {
if (!gblb.init)
state = LOOPBACK_FSM_IDLE;
else
state = gblb.state;
switch (state) {
case LOOPBACK_FSM_PING_HOST:
gb_loopback_ping_host(&gblb);
break;
case LOOPBACK_FSM_TRANSFER_HOST:
gb_loopback_transfer_host(&gblb, gblb.size);
break;
case LOOPBACK_FSM_SINK_HOST:
gb_loopback_sink_host(&gblb, gblb.size);
break;
case LOOPBACK_FSM_IDLE:
default:
sleep(1);
continue;
}
}
gbsim_info("Loopback thread exit\n");
pthread_exit(NULL);
return NULL;
}
static void loopback_init_port(uint8_t module_id, uint16_t cport_id,
uint16_t hd_cport_id, uint8_t id)
{
gblb.module_id = module_id;
gblb.cport_id = cport_id;
gblb.hd_cport_id = hd_cport_id;
gblb.id = id;
gblb.init = true;
gbsim_debug("Loopback Module %u Cport %hu HDCport %hu index %d\n",
module_id, cport_id, hd_cport_id, port_count);
}
int loopback_handler(struct gbsim_connection *connection, void *rbuf,
size_t rsize, void *tbuf, size_t tsize)
{
char data[GB_OPERATION_DATA_SIZE_MAX];
struct gb_operation_msg_hdr *oph;
struct op_msg *op_req = rbuf;
struct op_msg *op_rsp = (struct op_msg *)data;
size_t payload_size = 0;
__le32 len;
uint16_t message_size;
uint16_t cport_id = connection->cport_id;
uint16_t hd_cport_id = connection->hd_cport_id;
uint8_t module_id;
uint8_t result = PROTOCOL_STATUS_SUCCESS;
struct gb_loopback_transfer_request *request;
struct gb_loopback_transfer_response *response = &op_rsp->loopback_xfer_resp;
module_id = cport_to_module_id(cport_id);
oph = (struct gb_operation_msg_hdr *)&op_req->header;
/* Associate the module_id and cport_id with the device fd */
loopback_init_port(module_id, cport_id, hd_cport_id, oph->operation_id);
switch (oph->type) {
case GB_LOOPBACK_TYPE_PING:
break;
case GB_LOOPBACK_TYPE_TRANSFER:
request = &op_req->loopback_xfer_req;
gbsim_debug("%s: LOOPBACK xfer rx %u\n", __func__,
request->len);
if (request->len > GB_OPERATION_DATA_SIZE_MAX) {
gbsim_error("Module %hhu -> AP Cport %hu rx %u bytes\n",
module_id, cport_id, request->len);
result = PROTOCOL_STATUS_INVALID;
} else {
len = le32toh(request->len);
response->len = htole32(len);
memcpy(&response->data, request->data, len);
payload_size = sizeof(*response) + len;
}
break;
case GB_LOOPBACK_TYPE_SINK:
request = &op_req->loopback_xfer_req;
gbsim_debug("%s: LOOPBACK sink rx %u\n", __func__,
request->len);
break;
case GB_REQUEST_TYPE_CPORT_SHUTDOWN:
payload_size = 0;
break;
default:
return -EINVAL;
}
message_size = sizeof(struct gb_operation_msg_hdr) + payload_size;
return send_response(hd_cport_id, op_rsp, message_size,
oph->operation_id, oph->type, result);
}
char *loopback_get_operation(uint8_t type)
{
switch (type) {
case GB_REQUEST_TYPE_INVALID:
return "GB_LOOPBACK_TYPE_INVALID";
case GB_REQUEST_TYPE_CPORT_SHUTDOWN:
return "GB_REQUEST_TYPE_CPORT_SHUTDOWN";
case GB_LOOPBACK_TYPE_PING:
return "GB_LOOPBACK_TYPE_PING";
case GB_LOOPBACK_TYPE_TRANSFER:
return "GB_LOOPBACK_TYPE_TRANSFER";
case GB_LOOPBACK_TYPE_SINK:
return "GB_LOOPBACK_TYPE_SINK";
default:
return "(Unknown operation)";
}
}
void loopback_cleanup(void)
{
if (thread_started) {
/* signal termination */
terminate_thread = true;
/* sync */
pthread_join(loopback_pthread, NULL);
pthread_barrier_destroy(&loopback_barrier);
}
}
void loopback_init(void)
{
int ret;
/* Init thread */
pthread_barrier_init(&loopback_barrier, 0, 2);
ret = pthread_create(&loopback_pthread, NULL, loopback_thread, NULL);
if (ret < 0) {
perror("can't create loopback thread");
loopback_cleanup();
return;
}
/* Sync thread startup */
thread_started = 1;
pthread_barrier_wait(&loopback_barrier);
}