Coverage Report

Created: 2026-10-24 08:16

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