aboutsummaryrefslogtreecommitdiff
path: root/components/service/common/provider/service_provider.c
blob: 17f742f1ae7e0c59eb0a3a144e9ce842259ddc21 (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
/*
 * Copyright (c) 2020, 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 call_ep *base_ep, 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*)base_ep - offsetof(struct service_provider, base));
	handler = find_handler(sp, call_req_get_opcode(req));

    if (handler) {

        req->serializer = sp->default_serializer;
        rpc_status = service_handler_invoke(handler, base_ep->context, 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->base.receive = receive;
	sp->base.context = context;

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

	sp->default_serializer = NULL;
}