/work/toxcore/events/friend_connection_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_Friend_Connection_Status { |
28 | | uint32_t friend_number; |
29 | | Tox_Connection connection_status; |
30 | | }; |
31 | | |
32 | | static void tox_event_friend_connection_status_set_friend_number(Tox_Event_Friend_Connection_Status *_Nonnull friend_connection_status, uint32_t friend_number) |
33 | 450 | { |
34 | 450 | assert(friend_connection_status != nullptr); |
35 | 450 | friend_connection_status->friend_number = friend_number; |
36 | 450 | } |
37 | | uint32_t tox_event_friend_connection_status_get_friend_number(const Tox_Event_Friend_Connection_Status *friend_connection_status) |
38 | 0 | { |
39 | 0 | assert(friend_connection_status != nullptr); |
40 | 0 | return friend_connection_status->friend_number; |
41 | 0 | } |
42 | | |
43 | | static void tox_event_friend_connection_status_set_connection_status(Tox_Event_Friend_Connection_Status *_Nonnull friend_connection_status, Tox_Connection connection_status) |
44 | 450 | { |
45 | 450 | assert(friend_connection_status != nullptr); |
46 | 450 | friend_connection_status->connection_status = connection_status; |
47 | 450 | } |
48 | | Tox_Connection tox_event_friend_connection_status_get_connection_status(const Tox_Event_Friend_Connection_Status *friend_connection_status) |
49 | 3 | { |
50 | 3 | assert(friend_connection_status != nullptr); |
51 | 3 | return friend_connection_status->connection_status; |
52 | 3 | } |
53 | | |
54 | | static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *_Nonnull friend_connection_status) |
55 | 488 | { |
56 | 488 | *friend_connection_status = (Tox_Event_Friend_Connection_Status) { |
57 | 488 | 0 |
58 | 488 | }; |
59 | 488 | } |
60 | | static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *_Nonnull friend_connection_status, const Memory *_Nonnull mem) |
61 | 488 | { |
62 | 488 | return; |
63 | 488 | } |
64 | | |
65 | | bool tox_event_friend_connection_status_pack( |
66 | | const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp) |
67 | 54 | { |
68 | 54 | return bin_pack_array(bp, 2) |
69 | 54 | && bin_pack_u32(bp, event->friend_number) |
70 | 54 | && tox_connection_pack(event->connection_status, bp); |
71 | 54 | } |
72 | | |
73 | | static bool tox_event_friend_connection_status_unpack_into(Tox_Event_Friend_Connection_Status *_Nonnull event, Bin_Unpack *_Nonnull bu) |
74 | 38 | { |
75 | 38 | assert(event != nullptr); |
76 | 38 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
77 | 2 | return false; |
78 | 2 | } |
79 | | |
80 | 36 | return bin_unpack_u32(bu, &event->friend_number) |
81 | 36 | && tox_connection_unpack(&event->connection_status, bu); |
82 | 38 | } |
83 | | |
84 | | /***************************************************** |
85 | | * |
86 | | * :: new/free/add/get/size/unpack |
87 | | * |
88 | | *****************************************************/ |
89 | | |
90 | | const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status(const Tox_Event *event) |
91 | 0 | { |
92 | 0 | return event->type == TOX_EVENT_FRIEND_CONNECTION_STATUS ? event->data.friend_connection_status : nullptr; |
93 | 0 | } |
94 | | |
95 | | Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem) |
96 | 489 | { |
97 | 489 | Tox_Event_Friend_Connection_Status *const friend_connection_status = |
98 | 489 | (Tox_Event_Friend_Connection_Status *)mem_alloc(mem, sizeof(Tox_Event_Friend_Connection_Status)); |
99 | | |
100 | 489 | if (friend_connection_status == nullptr) { |
101 | 1 | return nullptr; |
102 | 1 | } |
103 | | |
104 | 488 | tox_event_friend_connection_status_construct(friend_connection_status); |
105 | 488 | return friend_connection_status; |
106 | 489 | } |
107 | | |
108 | | void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) |
109 | 489 | { |
110 | 489 | if (friend_connection_status != nullptr) { |
111 | 488 | tox_event_friend_connection_status_destruct(friend_connection_status, mem); |
112 | 488 | } |
113 | 489 | mem_delete(mem, friend_connection_status); |
114 | 489 | } |
115 | | |
116 | | static Tox_Event_Friend_Connection_Status *_Nullable tox_events_add_friend_connection_status(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
117 | 450 | { |
118 | 450 | Tox_Event_Friend_Connection_Status *const friend_connection_status = tox_event_friend_connection_status_new(mem); |
119 | | |
120 | 450 | if (friend_connection_status == nullptr) { |
121 | 0 | return nullptr; |
122 | 0 | } |
123 | | |
124 | 450 | Tox_Event event; |
125 | 450 | event.type = TOX_EVENT_FRIEND_CONNECTION_STATUS; |
126 | 450 | event.data.friend_connection_status = friend_connection_status; |
127 | | |
128 | 450 | if (!tox_events_add(events, &event)) { |
129 | 0 | tox_event_friend_connection_status_free(friend_connection_status, mem); |
130 | 0 | return nullptr; |
131 | 0 | } |
132 | 450 | return friend_connection_status; |
133 | 450 | } |
134 | | |
135 | | bool tox_event_friend_connection_status_unpack( |
136 | | Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) |
137 | 39 | { |
138 | 39 | assert(event != nullptr); |
139 | 39 | assert(*event == nullptr); |
140 | 39 | *event = tox_event_friend_connection_status_new(mem); |
141 | | |
142 | 39 | if (*event == nullptr) { |
143 | 1 | return false; |
144 | 1 | } |
145 | | |
146 | 38 | return tox_event_friend_connection_status_unpack_into(*event, bu); |
147 | 39 | } |
148 | | |
149 | | static Tox_Event_Friend_Connection_Status *_Nullable tox_event_friend_connection_status_alloc(Tox_Events_State *_Nonnull state) |
150 | 450 | { |
151 | 450 | if (state->events == nullptr) { |
152 | 0 | return nullptr; |
153 | 0 | } |
154 | | |
155 | 450 | Tox_Event_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events, state->mem); |
156 | | |
157 | 450 | if (friend_connection_status == nullptr) { |
158 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
159 | 0 | return nullptr; |
160 | 0 | } |
161 | | |
162 | 450 | return friend_connection_status; |
163 | 450 | } |
164 | | |
165 | | /***************************************************** |
166 | | * |
167 | | * :: event handler |
168 | | * |
169 | | *****************************************************/ |
170 | | |
171 | | void tox_events_handle_friend_connection_status( |
172 | | Tox *_Nonnull tox, |
173 | | uint32_t friend_number, |
174 | | Tox_Connection connection_status, |
175 | | void *_Nullable user_data) |
176 | 450 | { |
177 | 450 | Tox_Events_State *state = tox_events_alloc(user_data); |
178 | 450 | Tox_Event_Friend_Connection_Status *friend_connection_status = tox_event_friend_connection_status_alloc(state); |
179 | | |
180 | 450 | if (friend_connection_status == nullptr) { |
181 | 0 | return; |
182 | 0 | } |
183 | | |
184 | 450 | tox_event_friend_connection_status_set_friend_number(friend_connection_status, friend_number); |
185 | 450 | tox_event_friend_connection_status_set_connection_status(friend_connection_status, connection_status); |
186 | 450 | } |
187 | | |
188 | | void tox_events_handle_friend_connection_status_dispatch(Tox *tox, const Tox_Event_Friend_Connection_Status *event, void *user_data) |
189 | 0 | { |
190 | 0 | if (tox->friend_connection_status_callback == nullptr) { |
191 | 0 | return; |
192 | 0 | } |
193 | | |
194 | 0 | tox_unlock(tox); |
195 | 0 | tox->friend_connection_status_callback(tox, event->friend_number, event->connection_status, user_data); |
196 | 0 | tox_lock(tox); |
197 | 0 | } |