/work/toxcore/events/group_peer_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_Group_Peer_Status { |
28 | | uint32_t group_number; |
29 | | uint32_t peer_id; |
30 | | Tox_User_Status status; |
31 | | }; |
32 | | |
33 | | static void tox_event_group_peer_status_set_group_number(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, uint32_t group_number) |
34 | 2 | { |
35 | 2 | assert(group_peer_status != nullptr); |
36 | 2 | group_peer_status->group_number = group_number; |
37 | 2 | } |
38 | | uint32_t tox_event_group_peer_status_get_group_number(const Tox_Event_Group_Peer_Status *group_peer_status) |
39 | 0 | { |
40 | 0 | assert(group_peer_status != nullptr); |
41 | 0 | return group_peer_status->group_number; |
42 | 0 | } |
43 | | |
44 | | static void tox_event_group_peer_status_set_peer_id(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, uint32_t peer_id) |
45 | 2 | { |
46 | 2 | assert(group_peer_status != nullptr); |
47 | 2 | group_peer_status->peer_id = peer_id; |
48 | 2 | } |
49 | | uint32_t tox_event_group_peer_status_get_peer_id(const Tox_Event_Group_Peer_Status *group_peer_status) |
50 | 0 | { |
51 | 0 | assert(group_peer_status != nullptr); |
52 | 0 | return group_peer_status->peer_id; |
53 | 0 | } |
54 | | |
55 | | static void tox_event_group_peer_status_set_status(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, Tox_User_Status status) |
56 | 2 | { |
57 | 2 | assert(group_peer_status != nullptr); |
58 | 2 | group_peer_status->status = status; |
59 | 2 | } |
60 | | Tox_User_Status tox_event_group_peer_status_get_status(const Tox_Event_Group_Peer_Status *group_peer_status) |
61 | 1 | { |
62 | 1 | assert(group_peer_status != nullptr); |
63 | 1 | return group_peer_status->status; |
64 | 1 | } |
65 | | |
66 | | static void tox_event_group_peer_status_construct(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status) |
67 | 24 | { |
68 | 24 | *group_peer_status = (Tox_Event_Group_Peer_Status) { |
69 | 24 | 0 |
70 | 24 | }; |
71 | 24 | } |
72 | | static void tox_event_group_peer_status_destruct(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, const Memory *_Nonnull mem) |
73 | 24 | { |
74 | 24 | return; |
75 | 24 | } |
76 | | |
77 | | bool tox_event_group_peer_status_pack( |
78 | | const Tox_Event_Group_Peer_Status *event, Bin_Pack *bp) |
79 | 20 | { |
80 | 20 | return bin_pack_array(bp, 3) |
81 | 20 | && bin_pack_u32(bp, event->group_number) |
82 | 20 | && bin_pack_u32(bp, event->peer_id) |
83 | 20 | && tox_user_status_pack(event->status, bp); |
84 | 20 | } |
85 | | |
86 | | static bool tox_event_group_peer_status_unpack_into(Tox_Event_Group_Peer_Status *_Nonnull event, Bin_Unpack *_Nonnull bu) |
87 | 22 | { |
88 | 22 | assert(event != nullptr); |
89 | 22 | if (!bin_unpack_array_fixed(bu, 3, nullptr)) { |
90 | 3 | return false; |
91 | 3 | } |
92 | | |
93 | 19 | return bin_unpack_u32(bu, &event->group_number) |
94 | 19 | && bin_unpack_u32(bu, &event->peer_id) |
95 | 19 | && tox_user_status_unpack(&event->status, bu); |
96 | 22 | } |
97 | | |
98 | | /***************************************************** |
99 | | * |
100 | | * :: new/free/add/get/size/unpack |
101 | | * |
102 | | *****************************************************/ |
103 | | |
104 | | const Tox_Event_Group_Peer_Status *tox_event_get_group_peer_status(const Tox_Event *event) |
105 | 0 | { |
106 | 0 | return event->type == TOX_EVENT_GROUP_PEER_STATUS ? event->data.group_peer_status : nullptr; |
107 | 0 | } |
108 | | |
109 | | Tox_Event_Group_Peer_Status *tox_event_group_peer_status_new(const Memory *mem) |
110 | 25 | { |
111 | 25 | Tox_Event_Group_Peer_Status *const group_peer_status = |
112 | 25 | (Tox_Event_Group_Peer_Status *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Status)); |
113 | | |
114 | 25 | if (group_peer_status == nullptr) { |
115 | 1 | return nullptr; |
116 | 1 | } |
117 | | |
118 | 24 | tox_event_group_peer_status_construct(group_peer_status); |
119 | 24 | return group_peer_status; |
120 | 25 | } |
121 | | |
122 | | void tox_event_group_peer_status_free(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem) |
123 | 25 | { |
124 | 25 | if (group_peer_status != nullptr) { |
125 | 24 | tox_event_group_peer_status_destruct(group_peer_status, mem); |
126 | 24 | } |
127 | 25 | mem_delete(mem, group_peer_status); |
128 | 25 | } |
129 | | |
130 | | static Tox_Event_Group_Peer_Status *_Nullable tox_events_add_group_peer_status(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
131 | 2 | { |
132 | 2 | Tox_Event_Group_Peer_Status *const group_peer_status = tox_event_group_peer_status_new(mem); |
133 | | |
134 | 2 | if (group_peer_status == nullptr) { |
135 | 0 | return nullptr; |
136 | 0 | } |
137 | | |
138 | 2 | Tox_Event event; |
139 | 2 | event.type = TOX_EVENT_GROUP_PEER_STATUS; |
140 | 2 | event.data.group_peer_status = group_peer_status; |
141 | | |
142 | 2 | if (!tox_events_add(events, &event)) { |
143 | 0 | tox_event_group_peer_status_free(group_peer_status, mem); |
144 | 0 | return nullptr; |
145 | 0 | } |
146 | 2 | return group_peer_status; |
147 | 2 | } |
148 | | |
149 | | bool tox_event_group_peer_status_unpack( |
150 | | Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem) |
151 | 23 | { |
152 | 23 | assert(event != nullptr); |
153 | 23 | assert(*event == nullptr); |
154 | 23 | *event = tox_event_group_peer_status_new(mem); |
155 | | |
156 | 23 | if (*event == nullptr) { |
157 | 1 | return false; |
158 | 1 | } |
159 | | |
160 | 22 | return tox_event_group_peer_status_unpack_into(*event, bu); |
161 | 23 | } |
162 | | |
163 | | static Tox_Event_Group_Peer_Status *_Nullable tox_event_group_peer_status_alloc(Tox_Events_State *_Nonnull state) |
164 | 2 | { |
165 | 2 | if (state->events == nullptr) { |
166 | 0 | return nullptr; |
167 | 0 | } |
168 | | |
169 | 2 | Tox_Event_Group_Peer_Status *group_peer_status = tox_events_add_group_peer_status(state->events, state->mem); |
170 | | |
171 | 2 | if (group_peer_status == nullptr) { |
172 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
173 | 0 | return nullptr; |
174 | 0 | } |
175 | | |
176 | 2 | return group_peer_status; |
177 | 2 | } |
178 | | |
179 | | /***************************************************** |
180 | | * |
181 | | * :: event handler |
182 | | * |
183 | | *****************************************************/ |
184 | | |
185 | | void tox_events_handle_group_peer_status( |
186 | | Tox *_Nonnull tox, |
187 | | uint32_t group_number, |
188 | | uint32_t peer_id, |
189 | | Tox_User_Status status, |
190 | | void *_Nullable user_data) |
191 | 2 | { |
192 | 2 | Tox_Events_State *state = tox_events_alloc(user_data); |
193 | 2 | Tox_Event_Group_Peer_Status *group_peer_status = tox_event_group_peer_status_alloc(state); |
194 | | |
195 | 2 | if (group_peer_status == nullptr) { |
196 | 0 | return; |
197 | 0 | } |
198 | | |
199 | 2 | tox_event_group_peer_status_set_group_number(group_peer_status, group_number); |
200 | 2 | tox_event_group_peer_status_set_peer_id(group_peer_status, peer_id); |
201 | 2 | tox_event_group_peer_status_set_status(group_peer_status, status); |
202 | 2 | } |
203 | | |
204 | | void tox_events_handle_group_peer_status_dispatch(Tox *tox, const Tox_Event_Group_Peer_Status *event, void *user_data) |
205 | 0 | { |
206 | 0 | if (tox->group_peer_status_callback == nullptr) { |
207 | 0 | return; |
208 | 0 | } |
209 | | |
210 | 0 | tox_unlock(tox); |
211 | 0 | tox->group_peer_status_callback(tox, event->group_number, event->peer_id, event->status, user_data); |
212 | 0 | tox_lock(tox); |
213 | 0 | } |