-
Notifications
You must be signed in to change notification settings - Fork 16
/
svc.c
473 lines (423 loc) · 14.7 KB
/
svc.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Greybus Simulator: SVC CPort protocol
*
* Copyright 2015 Google Inc.
* Copyright 2015 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
#include <fcntl.h>
#include <pthread.h>
#include <linux/fs.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "gbsim.h"
struct gbsim_svc *svc;
int svc_get_next_intf_id(struct gbsim_svc *s)
{
struct gbsim_interface *intf;
int intf_id = 1;
start_again:
TAILQ_FOREACH(intf, &s->intfs, intf_node) {
if (intf_id == intf->interface_id) {
intf_id++;
goto start_again;
}
}
return intf_id;
}
static int svc_handler_request(uint16_t cport_id, uint16_t hd_cport_id,
void *rbuf, size_t rsize, void *tbuf,
size_t tsize)
{
struct op_msg *op_req = rbuf;
struct op_msg *op_rsp = tbuf;
struct gb_operation_msg_hdr *oph = &op_req->header;
struct gb_svc_intf_device_id_request *svc_dev_id;
struct gb_svc_conn_create_request *svc_conn_create;
struct gb_svc_conn_destroy_request *svc_conn_destroy;
struct gb_svc_dme_peer_get_request *dme_get_request;
struct gb_svc_dme_peer_get_response *dme_get_response;
struct gb_svc_dme_peer_set_request *dme_set_request;
struct gb_svc_dme_peer_set_response *dme_set_response;
struct gb_svc_route_create_request *svc_route_create;
struct gb_svc_route_destroy_request *svc_route_destroy;
struct gb_svc_pwrmon_rail_count_get_response *svc_pwrmon_rail_count_get_response;
struct gb_svc_intf_vsys_response *svc_intf_vsys_response;
struct gb_svc_intf_refclk_response *svc_intf_refclk_response;
struct gb_svc_intf_unipro_response *svc_intf_unipro_response;
struct gb_svc_intf_activate_response *svc_intf_activate_response;
struct gb_svc_intf_resume_response *svc_intf_resume_response;
struct gb_svc_intf_set_pwrm_response *svc_intf_set_pwrm_response;
struct gbsim_interface *intf;
struct gbsim_connection *connection;
uint16_t ap_intf_id, ap_cport_id, mod_intf_id, mod_cport_id;
uint16_t message_size = sizeof(*oph);
uint8_t intf_id = 0;
size_t payload_size = 0;
switch (oph->type) {
case GB_SVC_TYPE_PROTOCOL_VERSION:
gbsim_error("%s: Protocol Version request not supported\n",
__func__);
break;
case GB_SVC_TYPE_INTF_DEVICE_ID:
svc_dev_id = &op_req->svc_intf_device_id_request;
gbsim_debug("SVC assign device id (%hhu %hhu) response\n",
svc_dev_id->intf_id, svc_dev_id->device_id);
break;
case GB_SVC_TYPE_CONN_CREATE:
svc_conn_create = &op_req->svc_conn_create_request;
ap_intf_id = le16toh(svc_conn_create->intf1_id);
ap_cport_id = le16toh(svc_conn_create->cport1_id);
mod_intf_id = le16toh(svc_conn_create->intf2_id);
mod_cport_id = le16toh(svc_conn_create->cport2_id);
gbsim_debug("SVC connection create request (%hu %hu):(%hu %hu) response\n",
ap_intf_id, ap_cport_id, mod_intf_id, mod_cport_id);
intf = interface_get_by_id(svc, mod_intf_id);
if (!intf) {
gbsim_error("SVC No interface: %hu\n", mod_intf_id);
break;
}
connection = allocate_connection(intf, mod_cport_id,
ap_cport_id);
if (!connection) {
gbsim_error("Failed to allocate connection: (%hu %hu):(%hu %hu)\n",
ap_intf_id, ap_cport_id, mod_intf_id,
mod_cport_id);
return -ENOMEM;
}
connection_set_protocol(connection, mod_cport_id);
break;
case GB_SVC_TYPE_CONN_DESTROY:
svc_conn_destroy = &op_req->svc_conn_destroy_request;
ap_intf_id = le16toh(svc_conn_destroy->intf1_id);
ap_cport_id = le16toh(svc_conn_destroy->cport1_id);
mod_intf_id = le16toh(svc_conn_destroy->intf2_id);
mod_cport_id = le16toh(svc_conn_destroy->cport2_id);
gbsim_debug("SVC connection destroy request (%hu %hu):(%hu %hu) response\n",
ap_intf_id, ap_cport_id, mod_intf_id, mod_cport_id);
connection = connection_find(ap_cport_id);
free_connection(connection);
break;
case GB_SVC_TYPE_DME_PEER_GET:
payload_size = sizeof(*dme_get_response);
dme_get_request = &op_req->svc_dme_peer_get_request;
dme_get_response = &op_rsp->svc_dme_peer_get_response;
dme_get_response->result_code = 0;
dme_get_response->attr_value = 0x0126;
gbsim_debug("SVC dme peer get (%hhu %hu %hu) request\n",
dme_get_request->intf_id, dme_get_request->attr,
dme_get_request->selector);
gbsim_debug("SVC dme peer get (%hu %u) response\n",
dme_get_response->result_code,
dme_get_response->attr_value);
break;
case GB_SVC_TYPE_DME_PEER_SET:
payload_size = sizeof(*dme_set_response);
dme_set_request = &op_req->svc_dme_peer_set_request;
dme_set_response = &op_rsp->svc_dme_peer_set_response;
dme_set_response->result_code = 0;
gbsim_debug("SVC dme peer set (%hhu %hu %hu %u) request\n",
dme_set_request->intf_id, dme_set_request->attr,
dme_set_request->selector, dme_set_request->value);
gbsim_debug("SVC dme peer set (%hu) response\n",
dme_set_response->result_code);
break;
case GB_SVC_TYPE_ROUTE_CREATE:
svc_route_create = &op_req->svc_route_create_request;
gbsim_debug("SVC route create request (%hhu %u):(%hhu %u) response\n",
svc_route_create->intf1_id, svc_route_create->dev1_id,
svc_route_create->intf2_id, svc_route_create->dev2_id);
break;
case GB_SVC_TYPE_ROUTE_DESTROY:
svc_route_destroy = &op_req->svc_route_destroy_request;
gbsim_debug("SVC route destroy request (%hhu:%hhu) response\n",
svc_route_destroy->intf1_id,
svc_route_destroy->intf2_id);
break;
case GB_SVC_TYPE_PING:
gbsim_debug("SVC ping request response\n");
break;
case GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET:
payload_size = sizeof(*svc_pwrmon_rail_count_get_response);
svc_pwrmon_rail_count_get_response = &op_rsp->svc_pwrmon_rail_count_get_response;
svc_pwrmon_rail_count_get_response->rail_count = 0;
break;
case GB_SVC_TYPE_INTF_VSYS_ENABLE:
payload_size = sizeof(*svc_intf_vsys_response);
svc_intf_vsys_response = &op_rsp->svc_intf_vsys_response;
svc_intf_vsys_response->result_code = 0;
break;
case GB_SVC_TYPE_INTF_VSYS_DISABLE:
payload_size = sizeof(*svc_intf_vsys_response);
svc_intf_vsys_response = &op_rsp->svc_intf_vsys_response;
svc_intf_vsys_response->result_code = 0;
intf_id = op_req->svc_intf_vsys_request.intf_id;
intf = interface_get_by_id(svc, intf_id);
if (!intf)
return -ENODEV;
interface_free(svc, intf);
break;
case GB_SVC_TYPE_INTF_REFCLK_ENABLE:
payload_size = sizeof(*svc_intf_refclk_response);
svc_intf_refclk_response = &op_rsp->svc_intf_refclk_response;
svc_intf_refclk_response->result_code = 0;
break;
case GB_SVC_TYPE_INTF_REFCLK_DISABLE:
payload_size = sizeof(*svc_intf_refclk_response);
svc_intf_refclk_response = &op_rsp->svc_intf_refclk_response;
svc_intf_refclk_response->result_code = 0;
break;
case GB_SVC_TYPE_INTF_UNIPRO_ENABLE:
payload_size = sizeof(*svc_intf_unipro_response);
svc_intf_unipro_response = &op_rsp->svc_intf_unipro_response;
svc_intf_unipro_response->result_code = 0;
break;
case GB_SVC_TYPE_INTF_UNIPRO_DISABLE:
payload_size = sizeof(*svc_intf_unipro_response);
svc_intf_unipro_response = &op_rsp->svc_intf_unipro_response;
svc_intf_unipro_response->result_code = 0;
break;
case GB_SVC_TYPE_INTF_ACTIVATE:
payload_size = sizeof(*svc_intf_activate_response);
svc_intf_activate_response = &op_rsp->svc_intf_activate_response;
svc_intf_activate_response->intf_type = GB_SVC_INTF_TYPE_GREYBUS;
break;
case GB_SVC_TYPE_INTF_RESUME:
payload_size = sizeof(*svc_intf_resume_response);
svc_intf_resume_response = &op_rsp->svc_intf_resume_response;
svc_intf_resume_response->status = GB_SVC_OP_SUCCESS;
break;
case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
break;
case GB_SVC_TYPE_INTF_SET_PWRM:
payload_size = sizeof(*svc_intf_set_pwrm_response);
svc_intf_set_pwrm_response = &op_rsp->svc_intf_set_pwrm_response;
svc_intf_set_pwrm_response->result_code = 0;
break;
case GB_SVC_TYPE_MODULE_INSERTED:
case GB_SVC_TYPE_MODULE_REMOVED:
case GB_SVC_TYPE_INTF_RESET:
default:
gbsim_error("%s: Request not supported (%d)\n", __func__,
oph->type);
return -EINVAL;
}
message_size += payload_size;
return send_response(hd_cport_id, op_rsp, message_size,
oph->operation_id, oph->type,
PROTOCOL_STATUS_SUCCESS);
}
static int svc_handler_response(uint16_t cport_id, uint16_t hd_cport_id,
void *rbuf, size_t rsize)
{
struct op_msg *op_rsp = rbuf;
struct gb_operation_msg_hdr *oph = &op_rsp->header;
int ret;
/* Must be AP's svc protocol's cport */
if (cport_id != GB_SVC_CPORT_ID || cport_id != hd_cport_id) {
gbsim_error("%s: Error: cport-id-mismatch (%d %d %d)", __func__,
cport_id, hd_cport_id, GB_SVC_CPORT_ID);
return -EINVAL;
}
switch (oph->type & ~OP_RESPONSE) {
case GB_SVC_TYPE_PROTOCOL_VERSION:
gbsim_debug("%s: Version major-%d minor-%d\n", __func__,
op_rsp->svc_version_response.major,
op_rsp->svc_version_response.minor);
/* Version request successful, send hello msg */
ret = svc_request_send(GB_SVC_TYPE_SVC_HELLO, AP_INTF_ID);
if (ret) {
gbsim_error("%s: Failed to send svc hello request (%d)\n",
__func__, ret);
return ret;
}
break;
case GB_SVC_TYPE_SVC_HELLO:
/*
* AP's SVC cport is ready now, start scanning for module
* hotplug.
*/
ret = inotify_start(svc, hotplug_basedir);
if (ret < 0)
gbsim_error("Failed to start inotify thread\n");
break;
case GB_SVC_TYPE_MODULE_REMOVED:
break;
case GB_SVC_TYPE_MODULE_INSERTED:
case GB_SVC_TYPE_INTF_RESET:
break;
default:
gbsim_error("%s: Response not supported (%d)\n", __func__,
oph->type & ~OP_RESPONSE);
return -EINVAL;
}
return 0;
}
int svc_handler(struct gbsim_connection *connection, void *rbuf,
size_t rsize, void *tbuf, size_t tsize)
{
struct op_msg *op = rbuf;
struct gb_operation_msg_hdr *oph = &op->header;
uint16_t cport_id = connection->cport_id;
uint16_t hd_cport_id = connection->hd_cport_id;
if (oph->type & OP_RESPONSE)
return svc_handler_response(cport_id, hd_cport_id, rbuf, rsize);
else
return svc_handler_request(cport_id, hd_cport_id, rbuf, rsize,
tbuf, tsize);
}
char *svc_get_operation(uint8_t type)
{
switch (type) {
case GB_REQUEST_TYPE_INVALID:
return "GB_SVC_TYPE_INVALID";
case GB_SVC_TYPE_PROTOCOL_VERSION:
return "GB_SVC_TYPE_PROTOCOL_VERSION";
case GB_SVC_TYPE_SVC_HELLO:
return "GB_SVC_TYPE_SVC_HELLO";
case GB_SVC_TYPE_INTF_DEVICE_ID:
return "GB_SVC_TYPE_INTF_DEVICE_ID";
case GB_SVC_TYPE_MODULE_INSERTED:
return "GB_SVC_TYPE_MODULE_INSERTED";
case GB_SVC_TYPE_MODULE_REMOVED:
return "GB_SVC_TYPE_MODULE_REMOVED";
case GB_SVC_TYPE_INTF_RESET:
return "GB_SVC_TYPE_INTF_RESET";
case GB_SVC_TYPE_CONN_CREATE:
return "GB_SVC_TYPE_CONN_CREATE";
case GB_SVC_TYPE_CONN_DESTROY:
return "GB_SVC_TYPE_CONN_DESTROY";
case GB_SVC_TYPE_DME_PEER_GET:
return "GB_SVC_TYPE_DME_PEER_GET";
case GB_SVC_TYPE_DME_PEER_SET:
return "GB_SVC_TYPE_DME_PEER_SET";
case GB_SVC_TYPE_ROUTE_CREATE:
return "GB_SVC_TYPE_ROUTE_CREATE";
case GB_SVC_TYPE_ROUTE_DESTROY:
return "GB_SVC_TYPE_ROUTE_DESTROY";
case GB_SVC_TYPE_PING:
return "GB_SVC_TYPE_PING";
case GB_SVC_TYPE_TIMESYNC_ENABLE:
return "GB_SVC_TYPE_TIMESYNC_ENABLE";
case GB_SVC_TYPE_TIMESYNC_DISABLE:
return "GB_SVC_TYPE_TIMESYNC_DISABLE";
case GB_SVC_TYPE_TIMESYNC_AUTHORITATIVE:
return "GB_SVC_TYPE_TIMESYNC_AUTHORITATIVE";
case GB_SVC_TYPE_INTF_SET_PWRM:
return "GB_SVC_TYPE_INTF_SET_PWRM";
case GB_SVC_TYPE_INTF_EJECT:
return "GB_SVC_TYPE_INTF_EJECT";
case GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET:
return "GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET";
case GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET:
return "GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET";
case GB_SVC_TYPE_PWRMON_SAMPLE_GET:
return "GB_SVC_TYPE_PWRMON_SAMPLE_GET";
case GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET:
return "GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET";
case GB_SVC_TYPE_TIMESYNC_WAKE_PINS_ACQUIRE:
return "GB_SVC_TYPE_TIMESYNC_WAKE_PINS_ACQUIRE";
case GB_SVC_TYPE_TIMESYNC_WAKE_PINS_RELEASE:
return "GB_SVC_TYPE_TIMESYNC_WAKE_PINS_RELEASE";
case GB_SVC_TYPE_TIMESYNC_PING:
return "GB_SVC_TYPE_TIMESYNC_PING";
case GB_SVC_TYPE_INTF_VSYS_ENABLE:
return "GB_SVC_TYPE_INTF_VSYS_ENABLE";
case GB_SVC_TYPE_INTF_VSYS_DISABLE:
return "GB_SVC_TYPE_INTF_VSYS_DISABLE";
case GB_SVC_TYPE_INTF_REFCLK_ENABLE:
return "GB_SVC_TYPE_INTF_REFCLK_ENABLE";
case GB_SVC_TYPE_INTF_REFCLK_DISABLE:
return "GB_SVC_TYPE_INTF_REFCLK_DISABLE";
case GB_SVC_TYPE_INTF_UNIPRO_ENABLE:
return "GB_SVC_TYPE_INTF_UNIPRO_ENABLE";
case GB_SVC_TYPE_INTF_UNIPRO_DISABLE:
return "GB_SVC_TYPE_INTF_UNIPRO_DISABLE";
case GB_SVC_TYPE_INTF_ACTIVATE:
return "GB_SVC_TYPE_INTF_ACTIVATE";
case GB_SVC_TYPE_INTF_RESUME:
return "GB_SVC_TYPE_INTF_RESUME";
case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
return "GB_SVC_TYPE_INTF_MAILBOX_EVENT";
default:
return "(Unknown operation)";
}
}
int svc_request_send(uint8_t type, uint8_t intf_id)
{
struct op_msg msg = { };
struct gb_operation_msg_hdr *oph = &msg.header;
struct gb_svc_version_request *version_request;
struct gb_svc_hello_request *hello_request;
struct gb_svc_module_inserted_request *inserted;
struct gb_svc_module_removed_request *removed;
struct gb_svc_intf_reset_request *reset;
uint16_t message_size = sizeof(*oph);
size_t payload_size;
switch (type) {
case GB_SVC_TYPE_PROTOCOL_VERSION:
payload_size = sizeof(*version_request);
version_request = &msg.svc_version_request;
version_request->major = GB_SVC_VERSION_MAJOR;
version_request->minor = GB_SVC_VERSION_MINOR;
break;
case GB_SVC_TYPE_SVC_HELLO:
payload_size = sizeof(*hello_request);
hello_request = &msg.hello_request;
hello_request->endo_id = htole16(ENDO_ID);
hello_request->interface_id = AP_INTF_ID;
break;
case GB_SVC_TYPE_MODULE_INSERTED:
payload_size = sizeof(*inserted);
inserted = &msg.svc_module_inserted_request;
inserted->primary_intf_id = intf_id;
inserted->intf_count = 1;
inserted->flags = 0;
break;
case GB_SVC_TYPE_MODULE_REMOVED:
payload_size = sizeof(*removed);
removed = &msg.svc_module_removed_request;
removed->primary_intf_id = intf_id;
break;
case GB_SVC_TYPE_INTF_RESET:
payload_size = sizeof(*reset);
reset = &msg.svc_intf_reset_request;
reset->intf_id = intf_id;
break;
default:
gbsim_error("svc operation type %02x not supported\n", type);
return -EINVAL;
}
message_size += payload_size;
return send_request(GB_SVC_CPORT_ID, &msg, message_size, 1, type);
}
int svc_init(void)
{
struct gbsim_connection *connection;
svc = calloc(1, sizeof(*svc));
if (!svc)
return -ENOMEM;
TAILQ_INIT(&svc->intfs);
/* init svc->ap interface */
svc->intf = interface_alloc(svc, 0);
if (!svc->intf)
return -ENOMEM;
connection = allocate_connection(svc->intf, GB_SVC_CPORT_ID,
GB_SVC_CPORT_ID);
if (!connection)
return -ENOMEM;
connection_set_protocol(connection, GB_SVC_CPORT_ID);
return 0;
}
void svc_exit(void)
{
if (svc && svc->intf)
interface_free(svc, svc->intf);
free(svc);
}