summaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/src/interface.c
blob: 3e6bc08542539bee0f1a033f36c21722dfa09cfe (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
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
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file interface.c
 * \brief Implementation of interface base class.
 */

#include <stdlib.h>
#include <string.h>
#include "facelet.h"
#include "interface.h"
#include "util/map.h"

TYPEDEF_MAP_H(interface_ops_map, const char *, const interface_ops_t *);
TYPEDEF_MAP(interface_ops_map, const char *, const interface_ops_t *, strcmp, string_snprintf, generic_snprintf);

static interface_ops_map_t * interface_ops_map = NULL;

int
interface_register(const interface_ops_t * ops)
{
    if (!interface_ops_map) {
        interface_ops_map = interface_ops_map_create();
        if (!interface_ops_map)
            return -1;
    }
    interface_ops_map_add(interface_ops_map, ops->type, ops);
    return 0;
}

interface_t *
interface_create(const char * name, const char * type)
{

    const interface_ops_t * ops = NULL;
    int rc = interface_ops_map_get(interface_ops_map, type, &ops);
    if (rc < 0) {
        printf("Interface type not found %s\n", type);
        return NULL;
    }

    interface_t * interface = malloc(sizeof(interface_t));
    if (!interface)
        return NULL;

    interface->name = strdup(name);
    /* this should use type */
    interface->ops = ops;
    interface->callback = NULL;
    interface->callback_data = NULL;
    interface->data = NULL;

    return interface;
}

void
interface_free(interface_t * interface)
{
    free(interface->name);
    free(interface);
}

void
_interface_set_callback(interface_t * interface, callback_t callback, void * callback_data)
{
    interface->callback = callback;
    interface->callback_data = callback_data;
}

int
interface_initialize(interface_t * interface, void * cfg)
{
    if (!interface->ops->initialize)
        return -1;
    return interface->ops->initialize(interface, cfg);
}

int
interface_finalize(interface_t * interface)
{
    if (!interface->ops->finalize)
        return -1;
    return interface->ops->finalize(interface);
}

int
interface_on_event(interface_t * interface, const facelet_t * facelet)
{
    if (!interface->ops->on_event)
        return -1;
    return interface->ops->on_event(interface, facelet);
}