/work/toxcore/events/file_recv_control.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_File_Recv_Control { |
28 | | uint32_t friend_number; |
29 | | uint32_t file_number; |
30 | | Tox_File_Control control; |
31 | | }; |
32 | | |
33 | | static void tox_event_file_recv_control_set_friend_number(Tox_Event_File_Recv_Control *_Nonnull file_recv_control, uint32_t friend_number) |
34 | 4 | { |
35 | 4 | assert(file_recv_control != nullptr); |
36 | 4 | file_recv_control->friend_number = friend_number; |
37 | 4 | } |
38 | | uint32_t tox_event_file_recv_control_get_friend_number(const Tox_Event_File_Recv_Control *file_recv_control) |
39 | 0 | { |
40 | 0 | assert(file_recv_control != nullptr); |
41 | 0 | return file_recv_control->friend_number; |
42 | 0 | } |
43 | | |
44 | | static void tox_event_file_recv_control_set_file_number(Tox_Event_File_Recv_Control *_Nonnull file_recv_control, uint32_t file_number) |
45 | 4 | { |
46 | 4 | assert(file_recv_control != nullptr); |
47 | 4 | file_recv_control->file_number = file_number; |
48 | 4 | } |
49 | | uint32_t tox_event_file_recv_control_get_file_number(const Tox_Event_File_Recv_Control *file_recv_control) |
50 | 0 | { |
51 | 0 | assert(file_recv_control != nullptr); |
52 | 0 | return file_recv_control->file_number; |
53 | 0 | } |
54 | | |
55 | | static void tox_event_file_recv_control_set_control(Tox_Event_File_Recv_Control *_Nonnull file_recv_control, Tox_File_Control control) |
56 | 4 | { |
57 | 4 | assert(file_recv_control != nullptr); |
58 | 4 | file_recv_control->control = control; |
59 | 4 | } |
60 | | Tox_File_Control tox_event_file_recv_control_get_control(const Tox_Event_File_Recv_Control *file_recv_control) |
61 | 2 | { |
62 | 2 | assert(file_recv_control != nullptr); |
63 | 2 | return file_recv_control->control; |
64 | 2 | } |
65 | | |
66 | | static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *_Nonnull file_recv_control) |
67 | 41 | { |
68 | 41 | *file_recv_control = (Tox_Event_File_Recv_Control) { |
69 | 41 | 0 |
70 | 41 | }; |
71 | 41 | } |
72 | | static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *_Nonnull file_recv_control, const Memory *_Nonnull mem) |
73 | 41 | { |
74 | 41 | return; |
75 | 41 | } |
76 | | |
77 | | bool tox_event_file_recv_control_pack( |
78 | | const Tox_Event_File_Recv_Control *event, Bin_Pack *bp) |
79 | 26 | { |
80 | 26 | return bin_pack_array(bp, 3) |
81 | 26 | && bin_pack_u32(bp, event->friend_number) |
82 | 26 | && bin_pack_u32(bp, event->file_number) |
83 | 26 | && tox_file_control_pack(event->control, bp); |
84 | 26 | } |
85 | | |
86 | | static bool tox_event_file_recv_control_unpack_into(Tox_Event_File_Recv_Control *_Nonnull event, Bin_Unpack *_Nonnull bu) |
87 | 37 | { |
88 | 37 | assert(event != nullptr); |
89 | 37 | if (!bin_unpack_array_fixed(bu, 3, nullptr)) { |
90 | 4 | return false; |
91 | 4 | } |
92 | | |
93 | 33 | return bin_unpack_u32(bu, &event->friend_number) |
94 | 33 | && bin_unpack_u32(bu, &event->file_number) |
95 | 33 | && tox_file_control_unpack(&event->control, bu); |
96 | 37 | } |
97 | | |
98 | | /***************************************************** |
99 | | * |
100 | | * :: new/free/add/get/size/unpack |
101 | | * |
102 | | *****************************************************/ |
103 | | |
104 | | const Tox_Event_File_Recv_Control *tox_event_get_file_recv_control(const Tox_Event *event) |
105 | 0 | { |
106 | 0 | return event->type == TOX_EVENT_FILE_RECV_CONTROL ? event->data.file_recv_control : nullptr; |
107 | 0 | } |
108 | | |
109 | | Tox_Event_File_Recv_Control *tox_event_file_recv_control_new(const Memory *mem) |
110 | 42 | { |
111 | 42 | Tox_Event_File_Recv_Control *const file_recv_control = |
112 | 42 | (Tox_Event_File_Recv_Control *)mem_alloc(mem, sizeof(Tox_Event_File_Recv_Control)); |
113 | | |
114 | 42 | if (file_recv_control == nullptr) { |
115 | 1 | return nullptr; |
116 | 1 | } |
117 | | |
118 | 41 | tox_event_file_recv_control_construct(file_recv_control); |
119 | 41 | return file_recv_control; |
120 | 42 | } |
121 | | |
122 | | void tox_event_file_recv_control_free(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem) |
123 | 42 | { |
124 | 42 | if (file_recv_control != nullptr) { |
125 | 41 | tox_event_file_recv_control_destruct(file_recv_control, mem); |
126 | 41 | } |
127 | 42 | mem_delete(mem, file_recv_control); |
128 | 42 | } |
129 | | |
130 | | static Tox_Event_File_Recv_Control *_Nullable tox_events_add_file_recv_control(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
131 | 4 | { |
132 | 4 | Tox_Event_File_Recv_Control *const file_recv_control = tox_event_file_recv_control_new(mem); |
133 | | |
134 | 4 | if (file_recv_control == nullptr) { |
135 | 0 | return nullptr; |
136 | 0 | } |
137 | | |
138 | 4 | Tox_Event event; |
139 | 4 | event.type = TOX_EVENT_FILE_RECV_CONTROL; |
140 | 4 | event.data.file_recv_control = file_recv_control; |
141 | | |
142 | 4 | if (!tox_events_add(events, &event)) { |
143 | 0 | tox_event_file_recv_control_free(file_recv_control, mem); |
144 | 0 | return nullptr; |
145 | 0 | } |
146 | 4 | return file_recv_control; |
147 | 4 | } |
148 | | |
149 | | bool tox_event_file_recv_control_unpack( |
150 | | Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem) |
151 | 38 | { |
152 | 38 | assert(event != nullptr); |
153 | 38 | assert(*event == nullptr); |
154 | 38 | *event = tox_event_file_recv_control_new(mem); |
155 | | |
156 | 38 | if (*event == nullptr) { |
157 | 1 | return false; |
158 | 1 | } |
159 | | |
160 | 37 | return tox_event_file_recv_control_unpack_into(*event, bu); |
161 | 38 | } |
162 | | |
163 | | static Tox_Event_File_Recv_Control *_Nullable tox_event_file_recv_control_alloc(Tox_Events_State *_Nonnull state) |
164 | 4 | { |
165 | 4 | if (state->events == nullptr) { |
166 | 0 | return nullptr; |
167 | 0 | } |
168 | | |
169 | 4 | Tox_Event_File_Recv_Control *file_recv_control = tox_events_add_file_recv_control(state->events, state->mem); |
170 | | |
171 | 4 | if (file_recv_control == nullptr) { |
172 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
173 | 0 | return nullptr; |
174 | 0 | } |
175 | | |
176 | 4 | return file_recv_control; |
177 | 4 | } |
178 | | |
179 | | /***************************************************** |
180 | | * |
181 | | * :: event handler |
182 | | * |
183 | | *****************************************************/ |
184 | | |
185 | | void tox_events_handle_file_recv_control( |
186 | | Tox *_Nonnull tox, |
187 | | uint32_t friend_number, |
188 | | uint32_t file_number, |
189 | | Tox_File_Control control, |
190 | | void *_Nullable user_data) |
191 | 4 | { |
192 | 4 | Tox_Events_State *state = tox_events_alloc(user_data); |
193 | 4 | Tox_Event_File_Recv_Control *file_recv_control = tox_event_file_recv_control_alloc(state); |
194 | | |
195 | 4 | if (file_recv_control == nullptr) { |
196 | 0 | return; |
197 | 0 | } |
198 | | |
199 | 4 | tox_event_file_recv_control_set_friend_number(file_recv_control, friend_number); |
200 | 4 | tox_event_file_recv_control_set_file_number(file_recv_control, file_number); |
201 | 4 | tox_event_file_recv_control_set_control(file_recv_control, control); |
202 | 4 | } |
203 | | |
204 | | void tox_events_handle_file_recv_control_dispatch(Tox *tox, const Tox_Event_File_Recv_Control *event, void *user_data) |
205 | 0 | { |
206 | 0 | if (tox->file_recv_control_callback == nullptr) { |
207 | 0 | return; |
208 | 0 | } |
209 | | |
210 | 0 | tox_unlock(tox); |
211 | 0 | tox->file_recv_control_callback(tox, event->friend_number, event->file_number, event->control, user_data); |
212 | 0 | tox_lock(tox); |
213 | 0 | } |