/work/toxcore/events/group_voice_state.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_Group_Voice_State { |
27 | | uint32_t group_number; |
28 | | Tox_Group_Voice_State voice_state; |
29 | | }; |
30 | | |
31 | | non_null() |
32 | | static void tox_event_group_voice_state_set_group_number(Tox_Event_Group_Voice_State *group_voice_state, |
33 | | uint32_t group_number) |
34 | 16 | { |
35 | 16 | assert(group_voice_state != nullptr); |
36 | 16 | group_voice_state->group_number = group_number; |
37 | 16 | } |
38 | | uint32_t tox_event_group_voice_state_get_group_number(const Tox_Event_Group_Voice_State *group_voice_state) |
39 | 4 | { |
40 | 4 | assert(group_voice_state != nullptr); |
41 | 4 | return group_voice_state->group_number; |
42 | 4 | } |
43 | | |
44 | | non_null() |
45 | | static void tox_event_group_voice_state_set_voice_state(Tox_Event_Group_Voice_State *group_voice_state, |
46 | | Tox_Group_Voice_State voice_state) |
47 | 16 | { |
48 | 16 | assert(group_voice_state != nullptr); |
49 | 16 | group_voice_state->voice_state = voice_state; |
50 | 16 | } |
51 | | Tox_Group_Voice_State tox_event_group_voice_state_get_voice_state(const Tox_Event_Group_Voice_State *group_voice_state) |
52 | 4 | { |
53 | 4 | assert(group_voice_state != nullptr); |
54 | 4 | return group_voice_state->voice_state; |
55 | 4 | } |
56 | | |
57 | | non_null() |
58 | | static void tox_event_group_voice_state_construct(Tox_Event_Group_Voice_State *group_voice_state) |
59 | 56 | { |
60 | 56 | *group_voice_state = (Tox_Event_Group_Voice_State) { |
61 | 56 | 0 |
62 | 56 | }; |
63 | 56 | } |
64 | | non_null() |
65 | | static void tox_event_group_voice_state_destruct(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) |
66 | 56 | { |
67 | 56 | return; |
68 | 56 | } |
69 | | |
70 | | bool tox_event_group_voice_state_pack( |
71 | | const Tox_Event_Group_Voice_State *event, Bin_Pack *bp) |
72 | 48 | { |
73 | 48 | return bin_pack_array(bp, 2) |
74 | 48 | && bin_pack_u32(bp, event->group_number) |
75 | 48 | && tox_group_voice_state_pack(event->voice_state, bp); |
76 | 48 | } |
77 | | |
78 | | non_null() |
79 | | static bool tox_event_group_voice_state_unpack_into( |
80 | | Tox_Event_Group_Voice_State *event, Bin_Unpack *bu) |
81 | 40 | { |
82 | 40 | assert(event != nullptr); |
83 | 40 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
84 | 2 | return false; |
85 | 2 | } |
86 | | |
87 | 38 | return bin_unpack_u32(bu, &event->group_number) |
88 | 38 | && tox_group_voice_state_unpack(&event->voice_state, bu); |
89 | 40 | } |
90 | | |
91 | | |
92 | | /***************************************************** |
93 | | * |
94 | | * :: new/free/add/get/size/unpack |
95 | | * |
96 | | *****************************************************/ |
97 | | |
98 | | const Tox_Event_Group_Voice_State *tox_event_get_group_voice_state(const Tox_Event *event) |
99 | 0 | { |
100 | 0 | return event->type == TOX_EVENT_GROUP_VOICE_STATE ? event->data.group_voice_state : nullptr; |
101 | 0 | } |
102 | | |
103 | | Tox_Event_Group_Voice_State *tox_event_group_voice_state_new(const Memory *mem) |
104 | 57 | { |
105 | 57 | Tox_Event_Group_Voice_State *const group_voice_state = |
106 | 57 | (Tox_Event_Group_Voice_State *)mem_alloc(mem, sizeof(Tox_Event_Group_Voice_State)); |
107 | | |
108 | 57 | if (group_voice_state == nullptr) { |
109 | 1 | return nullptr; |
110 | 1 | } |
111 | | |
112 | 56 | tox_event_group_voice_state_construct(group_voice_state); |
113 | 56 | return group_voice_state; |
114 | 57 | } |
115 | | |
116 | | void tox_event_group_voice_state_free(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) |
117 | 57 | { |
118 | 57 | if (group_voice_state != nullptr) { |
119 | 56 | tox_event_group_voice_state_destruct(group_voice_state, mem); |
120 | 56 | } |
121 | 57 | mem_delete(mem, group_voice_state); |
122 | 57 | } |
123 | | |
124 | | non_null() |
125 | | static Tox_Event_Group_Voice_State *tox_events_add_group_voice_state(Tox_Events *events, const Memory *mem) |
126 | 16 | { |
127 | 16 | Tox_Event_Group_Voice_State *const group_voice_state = tox_event_group_voice_state_new(mem); |
128 | | |
129 | 16 | if (group_voice_state == nullptr) { |
130 | 0 | return nullptr; |
131 | 0 | } |
132 | | |
133 | 16 | Tox_Event event; |
134 | 16 | event.type = TOX_EVENT_GROUP_VOICE_STATE; |
135 | 16 | event.data.group_voice_state = group_voice_state; |
136 | | |
137 | 16 | tox_events_add(events, &event); |
138 | 16 | return group_voice_state; |
139 | 16 | } |
140 | | |
141 | | bool tox_event_group_voice_state_unpack( |
142 | | Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem) |
143 | 41 | { |
144 | 41 | assert(event != nullptr); |
145 | 41 | assert(*event == nullptr); |
146 | 41 | *event = tox_event_group_voice_state_new(mem); |
147 | | |
148 | 41 | if (*event == nullptr) { |
149 | 1 | return false; |
150 | 1 | } |
151 | | |
152 | 40 | return tox_event_group_voice_state_unpack_into(*event, bu); |
153 | 41 | } |
154 | | |
155 | | non_null() |
156 | | static Tox_Event_Group_Voice_State *tox_event_group_voice_state_alloc(void *user_data) |
157 | 16 | { |
158 | 16 | Tox_Events_State *state = tox_events_alloc(user_data); |
159 | 16 | assert(state != nullptr); |
160 | | |
161 | 16 | if (state->events == nullptr) { |
162 | 0 | return nullptr; |
163 | 0 | } |
164 | | |
165 | 16 | Tox_Event_Group_Voice_State *group_voice_state = tox_events_add_group_voice_state(state->events, state->mem); |
166 | | |
167 | 16 | if (group_voice_state == nullptr) { |
168 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
169 | 0 | return nullptr; |
170 | 0 | } |
171 | | |
172 | 16 | return group_voice_state; |
173 | 16 | } |
174 | | |
175 | | |
176 | | /***************************************************** |
177 | | * |
178 | | * :: event handler |
179 | | * |
180 | | *****************************************************/ |
181 | | |
182 | | |
183 | | void tox_events_handle_group_voice_state(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, |
184 | | void *user_data) |
185 | 16 | { |
186 | 16 | Tox_Event_Group_Voice_State *group_voice_state = tox_event_group_voice_state_alloc(user_data); |
187 | | |
188 | 16 | if (group_voice_state == nullptr) { |
189 | 0 | return; |
190 | 0 | } |
191 | | |
192 | 16 | tox_event_group_voice_state_set_group_number(group_voice_state, group_number); |
193 | 16 | tox_event_group_voice_state_set_voice_state(group_voice_state, voice_state); |
194 | 16 | } |