1/*
2 * Copyright 2020, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15#include <errno.h>
16
17#include <utils/util.h>
18#include <platsupport/interface_types.h>
19
20#define __PS_INTERFACE_REG_VALID_ARGS(func) \
21    if (!interface_registration_ops) { ZF_LOGE("interface_ops is NULL!"); return -EINVAL; } \
22    if (!interface_registration_ops->cookie) { ZF_LOGE("cookie in interface_ops is NULL!"); return -EINVAL; } \
23    if (!interface_registration_ops->func) { ZF_LOGE(#func " is not supported!"); return -ENOSYS; }
24
25typedef int (*ps_interface_register_fn_t)(void *cookie, ps_interface_type_t interface_type, void *interface_instance,
26                                          char **properties);
27
28typedef int (*ps_interface_unregister_fn_t)(void *cookie, ps_interface_type_t interface_type, void *interface_instance);
29
30#define PS_INTERFACE_FOUND_MATCH 0
31#define PS_INTERFACE_NO_MATCH 1
32
33/*
34 * Returns:
35 *  - Negative on error
36 *  - PS_INTERFACE_FOUND_MATCH on match
37 *  - PS_INTERFACE_NO_MATCH on no match
38 */
39typedef int (*ps_interface_search_handler_fn_t)(void *handler_data, void *interface_instance, char **properties);
40
41typedef int (*ps_interface_find_fn_t)(void *cookie, ps_interface_type_t interface_type,
42                                      ps_interface_search_handler_fn_t handler, void *handler_data);
43
44typedef struct {
45    void *cookie;
46    ps_interface_register_fn_t interface_register_fn;
47    ps_interface_unregister_fn_t interface_unregister_fn;
48    ps_interface_find_fn_t interface_find_fn;
49} ps_interface_registration_ops_t;
50
51static inline int ps_interface_register(ps_interface_registration_ops_t *interface_registration_ops,
52                                        ps_interface_type_t interface_type, void *interface_instance, char **properties)
53{
54    __PS_INTERFACE_REG_VALID_ARGS(interface_register_fn);
55    return interface_registration_ops->interface_register_fn(interface_registration_ops->cookie, interface_type,
56                                                             interface_instance, properties);
57}
58
59static inline int ps_interface_unregister(ps_interface_registration_ops_t *interface_registration_ops,
60                                          ps_interface_type_t interface_type, void *interface_instance)
61{
62    __PS_INTERFACE_REG_VALID_ARGS(interface_unregister_fn);
63    return interface_registration_ops->interface_unregister_fn(interface_registration_ops->cookie,
64                                                               interface_type, interface_instance);
65}
66
67static inline int ps_interface_find(ps_interface_registration_ops_t *interface_registration_ops,
68                                    ps_interface_type_t interface_type, ps_interface_search_handler_fn_t handler,
69                                    void *handler_data)
70{
71    __PS_INTERFACE_REG_VALID_ARGS(interface_find_fn);
72    return interface_registration_ops->interface_find_fn(interface_registration_ops->cookie, interface_type,
73                                                         handler, handler_data);
74}
75