Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/events/self_connection_status.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2023-2024 The TokTok team.
3
 */
4
5
#include "events_alloc.h"
6
7
#include <assert.h>
8
9
#include "../bin_pack.h"
10
#include "../bin_unpack.h"
11
#include "../ccompat.h"
12
#include "../mem.h"
13
#include "../tox.h"
14
#include "../tox_events.h"
15
#include "../tox_pack.h"
16
#include "../tox_unpack.h"
17
18
19
/*****************************************************
20
 *
21
 * :: struct and accessors
22
 *
23
 *****************************************************/
24
25
26
struct Tox_Event_Self_Connection_Status {
27
    Tox_Connection connection_status;
28
};
29
30
non_null()
31
static void tox_event_self_connection_status_set_connection_status(Tox_Event_Self_Connection_Status *self_connection_status,
32
        Tox_Connection connection_status)
33
985
{
34
985
    assert(self_connection_status != nullptr);
35
985
    self_connection_status->connection_status = connection_status;
36
985
}
37
Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status *self_connection_status)
38
24
{
39
24
    assert(self_connection_status != nullptr);
40
24
    return self_connection_status->connection_status;
41
24
}
42
43
non_null()
44
static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status)
45
1.07k
{
46
1.07k
    *self_connection_status = (Tox_Event_Self_Connection_Status) {
47
1.07k
        TOX_CONNECTION_NONE
48
1.07k
    };
49
1.07k
}
50
non_null()
51
static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem)
52
1.07k
{
53
1.07k
    return;
54
1.07k
}
55
56
bool tox_event_self_connection_status_pack(
57
    const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp)
58
138
{
59
138
    return tox_connection_pack(event->connection_status, bp);
60
138
}
61
62
non_null()
63
static bool tox_event_self_connection_status_unpack_into(
64
    Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu)
65
94
{
66
94
    assert(event != nullptr);
67
94
    return tox_connection_unpack(&event->connection_status, bu);
68
94
}
69
70
71
/*****************************************************
72
 *
73
 * :: new/free/add/get/size/unpack
74
 *
75
 *****************************************************/
76
77
const Tox_Event_Self_Connection_Status *tox_event_get_self_connection_status(const Tox_Event *event)
78
0
{
79
0
    return event->type == TOX_EVENT_SELF_CONNECTION_STATUS ? event->data.self_connection_status : nullptr;
80
0
}
81
82
Tox_Event_Self_Connection_Status *tox_event_self_connection_status_new(const Memory *mem)
83
1.08k
{
84
1.08k
    Tox_Event_Self_Connection_Status *const self_connection_status =
85
1.08k
        (Tox_Event_Self_Connection_Status *)mem_alloc(mem, sizeof(Tox_Event_Self_Connection_Status));
86
87
1.08k
    if (self_connection_status == nullptr) {
88
9
        return nullptr;
89
9
    }
90
91
1.07k
    tox_event_self_connection_status_construct(self_connection_status);
92
1.07k
    return self_connection_status;
93
1.08k
}
94
95
void tox_event_self_connection_status_free(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem)
96
1.07k
{
97
1.07k
    if (self_connection_status != nullptr) {
98
1.07k
        tox_event_self_connection_status_destruct(self_connection_status, mem);
99
1.07k
    }
100
1.07k
    mem_delete(mem, self_connection_status);
101
1.07k
}
102
103
non_null()
104
static Tox_Event_Self_Connection_Status *tox_events_add_self_connection_status(Tox_Events *events, const Memory *mem)
105
993
{
106
993
    Tox_Event_Self_Connection_Status *const self_connection_status = tox_event_self_connection_status_new(mem);
107
108
993
    if (self_connection_status == nullptr) {
109
8
        return nullptr;
110
8
    }
111
112
985
    Tox_Event event;
113
985
    event.type = TOX_EVENT_SELF_CONNECTION_STATUS;
114
985
    event.data.self_connection_status = self_connection_status;
115
116
985
    tox_events_add(events, &event);
117
985
    return self_connection_status;
118
993
}
119
120
bool tox_event_self_connection_status_unpack(
121
    Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem)
122
95
{
123
95
    assert(event != nullptr);
124
95
    assert(*event == nullptr);
125
95
    *event = tox_event_self_connection_status_new(mem);
126
127
95
    if (*event == nullptr) {
128
1
        return false;
129
1
    }
130
131
94
    return tox_event_self_connection_status_unpack_into(*event, bu);
132
95
}
133
134
non_null()
135
static Tox_Event_Self_Connection_Status *tox_event_self_connection_status_alloc(void *user_data)
136
1.00k
{
137
1.00k
    Tox_Events_State *state = tox_events_alloc(user_data);
138
1.00k
    assert(state != nullptr);
139
140
1.00k
    if (state->events == nullptr) {
141
8
        return nullptr;
142
8
    }
143
144
993
    Tox_Event_Self_Connection_Status *self_connection_status = tox_events_add_self_connection_status(state->events, state->mem);
145
146
993
    if (self_connection_status == nullptr) {
147
8
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
148
8
        return nullptr;
149
8
    }
150
151
985
    return self_connection_status;
152
993
}
153
154
155
/*****************************************************
156
 *
157
 * :: event handler
158
 *
159
 *****************************************************/
160
161
162
void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connection_status,
163
        void *user_data)
164
1.00k
{
165
1.00k
    Tox_Event_Self_Connection_Status *self_connection_status = tox_event_self_connection_status_alloc(user_data);
166
167
1.00k
    if (self_connection_status == nullptr) {
168
16
        return;
169
16
    }
170
171
985
    tox_event_self_connection_status_set_connection_status(self_connection_status, connection_status);
172
985
}