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