Coverage Report

Created: 2026-10-24 08:16

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