Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/events/friend_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_Friend_Status {
27
    uint32_t friend_number;
28
    Tox_User_Status status;
29
};
30
31
static void tox_event_friend_status_set_friend_number(Tox_Event_Friend_Status *_Nonnull friend_status, uint32_t friend_number)
32
984
{
33
984
    assert(friend_status != nullptr);
34
984
    friend_status->friend_number = friend_number;
35
984
}
36
uint32_t tox_event_friend_status_get_friend_number(const Tox_Event_Friend_Status *friend_status)
37
0
{
38
0
    assert(friend_status != nullptr);
39
0
    return friend_status->friend_number;
40
0
}
41
42
static void tox_event_friend_status_set_status(Tox_Event_Friend_Status *_Nonnull friend_status, Tox_User_Status status)
43
984
{
44
984
    assert(friend_status != nullptr);
45
984
    friend_status->status = status;
46
984
}
47
Tox_User_Status tox_event_friend_status_get_status(const Tox_Event_Friend_Status *friend_status)
48
0
{
49
0
    assert(friend_status != nullptr);
50
0
    return friend_status->status;
51
0
}
52
53
static void tox_event_friend_status_construct(Tox_Event_Friend_Status *_Nonnull friend_status)
54
1.02k
{
55
1.02k
    *friend_status = (Tox_Event_Friend_Status) {
56
1.02k
        0
57
1.02k
    };
58
1.02k
}
59
static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *_Nonnull friend_status, const Memory *_Nonnull mem)
60
1.02k
{
61
1.02k
    return;
62
1.02k
}
63
64
bool tox_event_friend_status_pack(
65
    const Tox_Event_Friend_Status *event, Bin_Pack *bp)
66
34
{
67
34
    return bin_pack_array(bp, 2)
68
34
           && bin_pack_u32(bp, event->friend_number)
69
34
           && tox_user_status_pack(event->status, bp);
70
34
}
71
72
static bool tox_event_friend_status_unpack_into(Tox_Event_Friend_Status *_Nonnull event, Bin_Unpack *_Nonnull bu)
73
30
{
74
30
    assert(event != nullptr);
75
30
    if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
76
2
        return false;
77
2
    }
78
79
28
    return bin_unpack_u32(bu, &event->friend_number)
80
28
           && tox_user_status_unpack(&event->status, bu);
81
30
}
82
83
/*****************************************************
84
 *
85
 * :: new/free/add/get/size/unpack
86
 *
87
 *****************************************************/
88
89
const Tox_Event_Friend_Status *tox_event_get_friend_status(const Tox_Event *event)
90
0
{
91
0
    return event->type == TOX_EVENT_FRIEND_STATUS ? event->data.friend_status : nullptr;
92
0
}
93
94
Tox_Event_Friend_Status *tox_event_friend_status_new(const Memory *mem)
95
1.03k
{
96
1.03k
    Tox_Event_Friend_Status *const friend_status =
97
1.03k
        (Tox_Event_Friend_Status *)mem_alloc(mem, sizeof(Tox_Event_Friend_Status));
98
99
1.03k
    if (friend_status == nullptr) {
100
10
        return nullptr;
101
10
    }
102
103
1.02k
    tox_event_friend_status_construct(friend_status);
104
1.02k
    return friend_status;
105
1.03k
}
106
107
void tox_event_friend_status_free(Tox_Event_Friend_Status *friend_status, const Memory *mem)
108
1.02k
{
109
1.02k
    if (friend_status != nullptr) {
110
1.02k
        tox_event_friend_status_destruct(friend_status, mem);
111
1.02k
    }
112
1.02k
    mem_delete(mem, friend_status);
113
1.02k
}
114
115
static Tox_Event_Friend_Status *tox_events_add_friend_status(Tox_Events *_Nonnull events, const Memory *_Nonnull mem)
116
1.00k
{
117
1.00k
    Tox_Event_Friend_Status *const friend_status = tox_event_friend_status_new(mem);
118
119
1.00k
    if (friend_status == nullptr) {
120
9
        return nullptr;
121
9
    }
122
123
992
    Tox_Event event;
124
992
    event.type = TOX_EVENT_FRIEND_STATUS;
125
992
    event.data.friend_status = friend_status;
126
127
992
    if (!tox_events_add(events, &event)) {
128
8
        tox_event_friend_status_free(friend_status, mem);
129
8
        return nullptr;
130
8
    }
131
984
    return friend_status;
132
992
}
133
134
bool tox_event_friend_status_unpack(
135
    Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem)
136
31
{
137
31
    assert(event != nullptr);
138
31
    assert(*event == nullptr);
139
31
    *event = tox_event_friend_status_new(mem);
140
141
31
    if (*event == nullptr) {
142
1
        return false;
143
1
    }
144
145
30
    return tox_event_friend_status_unpack_into(*event, bu);
146
31
}
147
148
static Tox_Event_Friend_Status *tox_event_friend_status_alloc(void *_Nonnull user_data)
149
1.00k
{
150
1.00k
    Tox_Events_State *state = tox_events_alloc(user_data);
151
1.00k
    assert(state != nullptr);
152
153
1.00k
    if (state->events == nullptr) {
154
7
        return nullptr;
155
7
    }
156
157
1.00k
    Tox_Event_Friend_Status *friend_status = tox_events_add_friend_status(state->events, state->mem);
158
159
1.00k
    if (friend_status == nullptr) {
160
17
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
161
17
        return nullptr;
162
17
    }
163
164
984
    return friend_status;
165
1.00k
}
166
167
/*****************************************************
168
 *
169
 * :: event handler
170
 *
171
 *****************************************************/
172
173
void tox_events_handle_friend_status(
174
    Tox *tox, uint32_t friend_number, Tox_User_Status status,
175
    void *user_data)
176
1.00k
{
177
1.00k
    Tox_Event_Friend_Status *friend_status = tox_event_friend_status_alloc(user_data);
178
179
1.00k
    if (friend_status == nullptr) {
180
24
        return;
181
24
    }
182
183
984
    tox_event_friend_status_set_friend_number(friend_status, friend_number);
184
984
    tox_event_friend_status_set_status(friend_status, status);
185
984
}