aboutsummaryrefslogtreecommitdiff
path: root/components/service/common/provider/service_provider.c
blob: f11c3036f9f4232f89d14788f1d810e309c80574 (plain)
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
/*
 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "service_provider.h"
#include <protocols/rpc/common/packed-c/status.h>
#include <stddef.h>

static const struct service_handler *find_handler(const struct service_provider *sp,
							  uint32_t opcode)
{
	const struct service_handler *handler = NULL;
	size_t index = 0;

	if (sp->num_handlers) {
		while (index < sp->num_handlers) {
			if (service_handler_get_opcode(&sp->handlers[index]) == opcode) {
				handler = &sp->handlers[index];
				break;
			}
			++index;
		}
	}

	return handler;
}

static rpc_status_t receive(struct rpc_interface *rpc_iface, struct call_req *req)
{
	rpc_status_t rpc_status;
	struct service_provider *sp = NULL;
	const struct service_handler *handler = NULL;

	sp = (struct service_provider*)((char*)rpc_iface - offsetof(struct service_provider, iface));
	handler = find_handler(sp, call_req_get_opcode(req));

	if (handler) {

		 rpc_status = service_handler_invoke(handler, rpc_iface->context, req);
	}
	else if (sp->successor) {

		rpc_status = rpc_interface_receive(sp->successor, req);
	}
	else {

		rpc_status = TS_RPC_ERROR_INVALID_OPCODE;
	}

	return rpc_status;
}

void service_provider_init(struct service_provider *sp, void *context,
				 const struct service_handler *handlers,
				 size_t num_handlers)
{
	sp->iface.receive = receive;
	sp->iface.context = context;

	sp->handlers = handlers;
	sp->num_handlers = num_handlers;

	sp->successor = NULL;
}