Coverage Report

Created: 2024-01-26 01:52

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