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