aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_InterfaceGeneric.c
blob: 1914dcef86a574361d61c014eb863279769e8e69 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2017 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.
 */

#include <config.h>
#include <stdio.h>

#include <ccnx/api/control/cpi_InterfaceGeneric.h>
#include <LongBow/runtime.h>
#include <parc/algol/parc_Memory.h>
#include <string.h>

struct cpi_interface_generic {
    unsigned ifidx;
    CPIInterfaceStateType state;
    CPIAddressList *addresses;
};

CPIInterfaceGeneric *
cpiInterfaceGeneric_Create(unsigned ifidx, CPIAddressList *addresses)
{
    assertNotNull(addresses, "Parameter addresses must be non-null");

    CPIInterfaceGeneric *generic = parcMemory_AllocateAndClear(sizeof(CPIInterfaceGeneric));
    assertNotNull(generic, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIInterfaceGeneric));
    generic->ifidx = ifidx;
    generic->state = CPI_IFACE_UNKNOWN;
    generic->addresses = addresses;
    return generic;
}

CPIInterfaceGeneric *
cpiInterfaceGeneric_Copy(const CPIInterfaceGeneric *original)
{
    assertNotNull(original, "Parameter original must be non-null");

    CPIInterfaceGeneric *generic = parcMemory_AllocateAndClear(sizeof(CPIInterfaceGeneric));
    assertNotNull(generic, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIInterfaceGeneric));
    generic->ifidx = original->ifidx;
    generic->state = original->state;
    generic->addresses = cpiAddressList_Copy(original->addresses);
    return generic;
}

void
cpiInterfaceGeneric_Destroy(CPIInterfaceGeneric **genericPtr)
{
    assertNotNull(genericPtr, "Parameter must be non-null double pointer");
    assertNotNull(*genericPtr, "Parameter must dereference to non-null pointer");

    CPIInterfaceGeneric *generic = *genericPtr;
    cpiAddressList_Destroy(&generic->addresses);
    parcMemory_Deallocate((void **) &generic);
    *genericPtr = NULL;
}

void
cpiInterfaceGeneric_SetState(CPIInterfaceGeneric *generic, CPIInterfaceStateType state)
{
    assertNotNull(generic, "Parameter must be non-null pointer");
    generic->state = state;
}

unsigned
cpiInterfaceGeneric_GetIndex(const CPIInterfaceGeneric *generic)
{
    assertNotNull(generic, "Parameter must be non-null pointer");
    return generic->ifidx;
}

const CPIAddressList *
cpiInterfaceGeneric_GetAddresses(const CPIInterfaceGeneric *generic)
{
    assertNotNull(generic, "Parameter must be non-null pointer");
    return generic->addresses;
}

CPIInterfaceStateType
cpiInterfaceGeneric_GetState(const CPIInterfaceGeneric *generic)
{
    assertNotNull(generic, "Parameter must be non-null pointer");
    return generic->state;
}

bool
cpiInterfaceGeneric_Equals(const CPIInterfaceGeneric *a, const CPIInterfaceGeneric *b)
{
    assertNotNull(a, "Parameter a must be non-null");
    assertNotNull(b, "Parameter b must be non-null");

    if (a == b) {
        return true;
    }

    if (a->ifidx == b->ifidx) {
        if (a->state == b->state) {
            if (cpiAddressList_Equals(a->addresses, b->addresses)) {
                return true;
            }
        }
    }
    return false;
}

PARCBufferComposer *
cpiInterfaceGeneric_BuildString(const CPIInterfaceGeneric *interface, PARCBufferComposer *composer)
{
    char *addressString = cpiAddressList_ToString(interface->addresses);
    parcBufferComposer_Format(composer, "%5d %4s %s",
                              interface->ifidx,
                              cpiInterfaceStateType_ToString(interface->state),
                              addressString
                              );
    parcMemory_Deallocate((void **) &addressString);
    return composer;
}