blob: 17f742f1ae7e0c59eb0a3a144e9ce842259ddc21 [file] [log] [blame]
Julian Hall2dcd69c2020-11-23 18:05:04 +01001/*
2 * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include "service_provider.h"
8#include <protocols/rpc/common/packed-c/status.h>
9#include <stddef.h>
10
11static const struct service_handler *find_handler(const struct service_provider *sp,
12 uint32_t opcode)
13{
14 const struct service_handler *handler = NULL;
15 size_t index = 0;
16
17 if (sp->num_handlers) {
18 while (index < sp->num_handlers) {
19 if (service_handler_get_opcode(&sp->handlers[index]) == opcode) {
20 handler = &sp->handlers[index];
21 break;
22 }
23 ++index;
24 }
25 }
26
27 return handler;
28}
29
30static rpc_status_t receive(struct call_ep *base_ep, struct call_req *req)
31{
32 rpc_status_t rpc_status;
33 struct service_provider *sp = NULL;
34 const struct service_handler *handler = NULL;
35
36 sp = (struct service_provider*)((char*)base_ep - offsetof(struct service_provider, base));
37 handler = find_handler(sp, call_req_get_opcode(req));
38
39 if (handler) {
40
41 req->serializer = sp->default_serializer;
42 rpc_status = service_handler_invoke(handler, base_ep->context, req);
43 }
44 else {
45
46 rpc_status = TS_RPC_ERROR_INVALID_OPCODE;
47 }
48
49 return rpc_status;
50}
51
52void service_provider_init(struct service_provider *sp, void *context,
53 const struct service_handler *handlers,
54 size_t num_handlers)
55{
56 sp->base.receive = receive;
57 sp->base.context = context;
58
59 sp->handlers = handlers;
60 sp->num_handlers = num_handlers;
61
62 sp->default_serializer = NULL;
63}