/work/toxcore/events/group_peer_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_Group_Peer_Status { |
27 | | uint32_t group_number; |
28 | | uint32_t peer_id; |
29 | | Tox_User_Status status; |
30 | | }; |
31 | | |
32 | | static void tox_event_group_peer_status_set_group_number(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, uint32_t group_number) |
33 | 38 | { |
34 | 38 | assert(group_peer_status != nullptr); |
35 | 38 | group_peer_status->group_number = group_number; |
36 | 38 | } |
37 | | uint32_t tox_event_group_peer_status_get_group_number(const Tox_Event_Group_Peer_Status *group_peer_status) |
38 | 1 | { |
39 | 1 | assert(group_peer_status != nullptr); |
40 | 1 | return group_peer_status->group_number; |
41 | 1 | } |
42 | | |
43 | | static void tox_event_group_peer_status_set_peer_id(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, uint32_t peer_id) |
44 | 38 | { |
45 | 38 | assert(group_peer_status != nullptr); |
46 | 38 | group_peer_status->peer_id = peer_id; |
47 | 38 | } |
48 | | uint32_t tox_event_group_peer_status_get_peer_id(const Tox_Event_Group_Peer_Status *group_peer_status) |
49 | 1 | { |
50 | 1 | assert(group_peer_status != nullptr); |
51 | 1 | return group_peer_status->peer_id; |
52 | 1 | } |
53 | | |
54 | | static void tox_event_group_peer_status_set_status(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, Tox_User_Status status) |
55 | 38 | { |
56 | 38 | assert(group_peer_status != nullptr); |
57 | 38 | group_peer_status->status = status; |
58 | 38 | } |
59 | | Tox_User_Status tox_event_group_peer_status_get_status(const Tox_Event_Group_Peer_Status *group_peer_status) |
60 | 1 | { |
61 | 1 | assert(group_peer_status != nullptr); |
62 | 1 | return group_peer_status->status; |
63 | 1 | } |
64 | | |
65 | | static void tox_event_group_peer_status_construct(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status) |
66 | 61 | { |
67 | 61 | *group_peer_status = (Tox_Event_Group_Peer_Status) { |
68 | 61 | 0 |
69 | 61 | }; |
70 | 61 | } |
71 | | static void tox_event_group_peer_status_destruct(Tox_Event_Group_Peer_Status *_Nonnull group_peer_status, const Memory *_Nonnull mem) |
72 | 61 | { |
73 | 61 | return; |
74 | 61 | } |
75 | | |
76 | | bool tox_event_group_peer_status_pack( |
77 | | const Tox_Event_Group_Peer_Status *event, Bin_Pack *bp) |
78 | 20 | { |
79 | 20 | return bin_pack_array(bp, 3) |
80 | 20 | && bin_pack_u32(bp, event->group_number) |
81 | 20 | && bin_pack_u32(bp, event->peer_id) |
82 | 20 | && tox_user_status_pack(event->status, bp); |
83 | 20 | } |
84 | | |
85 | | static bool tox_event_group_peer_status_unpack_into(Tox_Event_Group_Peer_Status *_Nonnull event, Bin_Unpack *_Nonnull bu) |
86 | 22 | { |
87 | 22 | assert(event != nullptr); |
88 | 22 | if (!bin_unpack_array_fixed(bu, 3, nullptr)) { |
89 | 3 | return false; |
90 | 3 | } |
91 | | |
92 | 19 | return bin_unpack_u32(bu, &event->group_number) |
93 | 19 | && bin_unpack_u32(bu, &event->peer_id) |
94 | 19 | && tox_user_status_unpack(&event->status, bu); |
95 | 22 | } |
96 | | |
97 | | /***************************************************** |
98 | | * |
99 | | * :: new/free/add/get/size/unpack |
100 | | * |
101 | | *****************************************************/ |
102 | | |
103 | | const Tox_Event_Group_Peer_Status *tox_event_get_group_peer_status(const Tox_Event *event) |
104 | 0 | { |
105 | 0 | return event->type == TOX_EVENT_GROUP_PEER_STATUS ? event->data.group_peer_status : nullptr; |
106 | 0 | } |
107 | | |
108 | | Tox_Event_Group_Peer_Status *tox_event_group_peer_status_new(const Memory *mem) |
109 | 63 | { |
110 | 63 | Tox_Event_Group_Peer_Status *const group_peer_status = |
111 | 63 | (Tox_Event_Group_Peer_Status *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Status)); |
112 | | |
113 | 63 | if (group_peer_status == nullptr) { |
114 | 2 | return nullptr; |
115 | 2 | } |
116 | | |
117 | 61 | tox_event_group_peer_status_construct(group_peer_status); |
118 | 61 | return group_peer_status; |
119 | 63 | } |
120 | | |
121 | | void tox_event_group_peer_status_free(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem) |
122 | 62 | { |
123 | 62 | if (group_peer_status != nullptr) { |
124 | 61 | tox_event_group_peer_status_destruct(group_peer_status, mem); |
125 | 61 | } |
126 | 62 | mem_delete(mem, group_peer_status); |
127 | 62 | } |
128 | | |
129 | | static Tox_Event_Group_Peer_Status *tox_events_add_group_peer_status(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
130 | 40 | { |
131 | 40 | Tox_Event_Group_Peer_Status *const group_peer_status = tox_event_group_peer_status_new(mem); |
132 | | |
133 | 40 | if (group_peer_status == nullptr) { |
134 | 1 | return nullptr; |
135 | 1 | } |
136 | | |
137 | 39 | Tox_Event event; |
138 | 39 | event.type = TOX_EVENT_GROUP_PEER_STATUS; |
139 | 39 | event.data.group_peer_status = group_peer_status; |
140 | | |
141 | 39 | if (!tox_events_add(events, &event)) { |
142 | 1 | tox_event_group_peer_status_free(group_peer_status, mem); |
143 | 1 | return nullptr; |
144 | 1 | } |
145 | 38 | return group_peer_status; |
146 | 39 | } |
147 | | |
148 | | bool tox_event_group_peer_status_unpack( |
149 | | Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem) |
150 | 23 | { |
151 | 23 | assert(event != nullptr); |
152 | 23 | assert(*event == nullptr); |
153 | 23 | *event = tox_event_group_peer_status_new(mem); |
154 | | |
155 | 23 | if (*event == nullptr) { |
156 | 1 | return false; |
157 | 1 | } |
158 | | |
159 | 22 | return tox_event_group_peer_status_unpack_into(*event, bu); |
160 | 23 | } |
161 | | |
162 | | static Tox_Event_Group_Peer_Status *tox_event_group_peer_status_alloc(void *_Nonnull user_data) |
163 | 41 | { |
164 | 41 | Tox_Events_State *state = tox_events_alloc(user_data); |
165 | 41 | assert(state != nullptr); |
166 | | |
167 | 41 | if (state->events == nullptr) { |
168 | 1 | return nullptr; |
169 | 1 | } |
170 | | |
171 | 40 | Tox_Event_Group_Peer_Status *group_peer_status = tox_events_add_group_peer_status(state->events, state->mem); |
172 | | |
173 | 40 | if (group_peer_status == nullptr) { |
174 | 2 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
175 | 2 | return nullptr; |
176 | 2 | } |
177 | | |
178 | 38 | return group_peer_status; |
179 | 40 | } |
180 | | |
181 | | /***************************************************** |
182 | | * |
183 | | * :: event handler |
184 | | * |
185 | | *****************************************************/ |
186 | | |
187 | | void tox_events_handle_group_peer_status( |
188 | | Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_User_Status status, |
189 | | void *user_data) |
190 | 41 | { |
191 | 41 | Tox_Event_Group_Peer_Status *group_peer_status = tox_event_group_peer_status_alloc(user_data); |
192 | | |
193 | 41 | if (group_peer_status == nullptr) { |
194 | 3 | return; |
195 | 3 | } |
196 | | |
197 | 38 | tox_event_group_peer_status_set_group_number(group_peer_status, group_number); |
198 | 38 | tox_event_group_peer_status_set_peer_id(group_peer_status, peer_id); |
199 | 38 | tox_event_group_peer_status_set_status(group_peer_status, status); |
200 | 38 | } |