Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/events/friend_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_Friend_Connection_Status {
27
    uint32_t friend_number;
28
    Tox_Connection connection_status;
29
};
30
31
non_null()
32
static void tox_event_friend_connection_status_set_friend_number(Tox_Event_Friend_Connection_Status *friend_connection_status,
33
        uint32_t friend_number)
34
1.28k
{
35
1.28k
    assert(friend_connection_status != nullptr);
36
1.28k
    friend_connection_status->friend_number = friend_number;
37
1.28k
}
38
uint32_t tox_event_friend_connection_status_get_friend_number(const Tox_Event_Friend_Connection_Status *friend_connection_status)
39
33
{
40
33
    assert(friend_connection_status != nullptr);
41
33
    return friend_connection_status->friend_number;
42
33
}
43
44
non_null()
45
static void tox_event_friend_connection_status_set_connection_status(Tox_Event_Friend_Connection_Status *friend_connection_status,
46
        Tox_Connection connection_status)
47
1.28k
{
48
1.28k
    assert(friend_connection_status != nullptr);
49
1.28k
    friend_connection_status->connection_status = connection_status;
50
1.28k
}
51
Tox_Connection tox_event_friend_connection_status_get_connection_status(const Tox_Event_Friend_Connection_Status *friend_connection_status)
52
33
{
53
33
    assert(friend_connection_status != nullptr);
54
33
    return friend_connection_status->connection_status;
55
33
}
56
57
non_null()
58
static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status)
59
1.32k
{
60
1.32k
    *friend_connection_status = (Tox_Event_Friend_Connection_Status) {
61
1.32k
        0
62
1.32k
    };
63
1.32k
}
64
non_null()
65
static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem)
66
1.31k
{
67
1.31k
    return;
68
1.31k
}
69
70
bool tox_event_friend_connection_status_pack(
71
    const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp)
72
62
{
73
62
    return bin_pack_array(bp, 2)
74
62
           && bin_pack_u32(bp, event->friend_number)
75
62
           && tox_connection_pack(event->connection_status, bp);
76
62
}
77
78
non_null()
79
static bool tox_event_friend_connection_status_unpack_into(
80
    Tox_Event_Friend_Connection_Status *event, Bin_Unpack *bu)
81
45
{
82
45
    assert(event != nullptr);
83
45
    if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
84
1
        return false;
85
1
    }
86
87
44
    return bin_unpack_u32(bu, &event->friend_number)
88
44
           && tox_connection_unpack(&event->connection_status, bu);
89
45
}
90
91
92
/*****************************************************
93
 *
94
 * :: new/free/add/get/size/unpack
95
 *
96
 *****************************************************/
97
98
const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status(const Tox_Event *event)
99
0
{
100
0
    return event->type == TOX_EVENT_FRIEND_CONNECTION_STATUS ? event->data.friend_connection_status : nullptr;
101
0
}
102
103
Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem)
104
1.33k
{
105
1.33k
    Tox_Event_Friend_Connection_Status *const friend_connection_status =
106
1.33k
        (Tox_Event_Friend_Connection_Status *)mem_alloc(mem, sizeof(Tox_Event_Friend_Connection_Status));
107
108
1.33k
    if (friend_connection_status == nullptr) {
109
9
        return nullptr;
110
9
    }
111
112
1.32k
    tox_event_friend_connection_status_construct(friend_connection_status);
113
1.32k
    return friend_connection_status;
114
1.33k
}
115
116
void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem)
117
1.31k
{
118
1.31k
    if (friend_connection_status != nullptr) {
119
1.31k
        tox_event_friend_connection_status_destruct(friend_connection_status, mem);
120
1.31k
    }
121
1.31k
    mem_delete(mem, friend_connection_status);
122
1.31k
}
123
124
non_null()
125
static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_status(Tox_Events *events, const Memory *mem)
126
1.29k
{
127
1.29k
    Tox_Event_Friend_Connection_Status *const friend_connection_status = tox_event_friend_connection_status_new(mem);
128
129
1.29k
    if (friend_connection_status == nullptr) {
130
8
        return nullptr;
131
8
    }
132
133
1.28k
    Tox_Event event;
134
1.28k
    event.type = TOX_EVENT_FRIEND_CONNECTION_STATUS;
135
1.28k
    event.data.friend_connection_status = friend_connection_status;
136
137
1.28k
    tox_events_add(events, &event);
138
1.28k
    return friend_connection_status;
139
1.29k
}
140
141
bool tox_event_friend_connection_status_unpack(
142
    Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem)
143
46
{
144
46
    assert(event != nullptr);
145
46
    assert(*event == nullptr);
146
46
    *event = tox_event_friend_connection_status_new(mem);
147
148
46
    if (*event == nullptr) {
149
1
        return false;
150
1
    }
151
152
45
    return tox_event_friend_connection_status_unpack_into(*event, bu);
153
46
}
154
155
non_null()
156
static Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_alloc(void *user_data)
157
1.30k
{
158
1.30k
    Tox_Events_State *state = tox_events_alloc(user_data);
159
1.30k
    assert(state != nullptr);
160
161
1.30k
    if (state->events == nullptr) {
162
10
        return nullptr;
163
10
    }
164
165
1.29k
    Tox_Event_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events, state->mem);
166
167
1.29k
    if (friend_connection_status == nullptr) {
168
8
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
169
8
        return nullptr;
170
8
    }
171
172
1.28k
    return friend_connection_status;
173
1.29k
}
174
175
176
/*****************************************************
177
 *
178
 * :: event handler
179
 *
180
 *****************************************************/
181
182
183
void tox_events_handle_friend_connection_status(Tox *tox, uint32_t friend_number, Tox_Connection connection_status,
184
        void *user_data)
185
1.30k
{
186
1.30k
    Tox_Event_Friend_Connection_Status *friend_connection_status = tox_event_friend_connection_status_alloc(user_data);
187
188
1.30k
    if (friend_connection_status == nullptr) {
189
18
        return;
190
18
    }
191
192
1.28k
    tox_event_friend_connection_status_set_friend_number(friend_connection_status, friend_number);
193
1.28k
    tox_event_friend_connection_status_set_connection_status(friend_connection_status, connection_status);
194
1.28k
}