Coverage Report

Created: 2025-10-08 19:34

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