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