/work/toxcore/events/conference_connected.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 | | |
18 | | /***************************************************** |
19 | | * |
20 | | * :: struct and accessors |
21 | | * |
22 | | *****************************************************/ |
23 | | |
24 | | struct Tox_Event_Conference_Connected { |
25 | | uint32_t conference_number; |
26 | | }; |
27 | | |
28 | | static void tox_event_conference_connected_set_conference_number(Tox_Event_Conference_Connected *_Nonnull conference_connected, uint32_t conference_number) |
29 | 29 | { |
30 | 29 | assert(conference_connected != nullptr); |
31 | 29 | conference_connected->conference_number = conference_number; |
32 | 29 | } |
33 | | uint32_t tox_event_conference_connected_get_conference_number(const Tox_Event_Conference_Connected *conference_connected) |
34 | 0 | { |
35 | 0 | assert(conference_connected != nullptr); |
36 | 0 | return conference_connected->conference_number; |
37 | 0 | } |
38 | | |
39 | | static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *_Nonnull conference_connected) |
40 | 52 | { |
41 | 52 | *conference_connected = (Tox_Event_Conference_Connected) { |
42 | 52 | 0 |
43 | 52 | }; |
44 | 52 | } |
45 | | static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *_Nonnull conference_connected, const Memory *_Nonnull mem) |
46 | 52 | { |
47 | 52 | return; |
48 | 52 | } |
49 | | |
50 | | bool tox_event_conference_connected_pack( |
51 | | const Tox_Event_Conference_Connected *event, Bin_Pack *bp) |
52 | 46 | { |
53 | 46 | return bin_pack_u32(bp, event->conference_number); |
54 | 46 | } |
55 | | |
56 | | static bool tox_event_conference_connected_unpack_into(Tox_Event_Conference_Connected *_Nonnull event, Bin_Unpack *_Nonnull bu) |
57 | 23 | { |
58 | 23 | assert(event != nullptr); |
59 | 23 | return bin_unpack_u32(bu, &event->conference_number); |
60 | 23 | } |
61 | | |
62 | | /***************************************************** |
63 | | * |
64 | | * :: new/free/add/get/size/unpack |
65 | | * |
66 | | *****************************************************/ |
67 | | |
68 | | const Tox_Event_Conference_Connected *tox_event_get_conference_connected(const Tox_Event *event) |
69 | 0 | { |
70 | 0 | return event->type == TOX_EVENT_CONFERENCE_CONNECTED ? event->data.conference_connected : nullptr; |
71 | 0 | } |
72 | | |
73 | | Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory *mem) |
74 | 54 | { |
75 | 54 | Tox_Event_Conference_Connected *const conference_connected = |
76 | 54 | (Tox_Event_Conference_Connected *)mem_alloc(mem, sizeof(Tox_Event_Conference_Connected)); |
77 | | |
78 | 54 | if (conference_connected == nullptr) { |
79 | 2 | return nullptr; |
80 | 2 | } |
81 | | |
82 | 52 | tox_event_conference_connected_construct(conference_connected); |
83 | 52 | return conference_connected; |
84 | 54 | } |
85 | | |
86 | | void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) |
87 | 53 | { |
88 | 53 | if (conference_connected != nullptr) { |
89 | 52 | tox_event_conference_connected_destruct(conference_connected, mem); |
90 | 52 | } |
91 | 53 | mem_delete(mem, conference_connected); |
92 | 53 | } |
93 | | |
94 | | static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
95 | 30 | { |
96 | 30 | Tox_Event_Conference_Connected *const conference_connected = tox_event_conference_connected_new(mem); |
97 | | |
98 | 30 | if (conference_connected == nullptr) { |
99 | 1 | return nullptr; |
100 | 1 | } |
101 | | |
102 | 29 | Tox_Event event; |
103 | 29 | event.type = TOX_EVENT_CONFERENCE_CONNECTED; |
104 | 29 | event.data.conference_connected = conference_connected; |
105 | | |
106 | 29 | if (!tox_events_add(events, &event)) { |
107 | 0 | tox_event_conference_connected_free(conference_connected, mem); |
108 | 0 | return nullptr; |
109 | 0 | } |
110 | 29 | return conference_connected; |
111 | 29 | } |
112 | | |
113 | | bool tox_event_conference_connected_unpack( |
114 | | Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) |
115 | 24 | { |
116 | 24 | assert(event != nullptr); |
117 | 24 | assert(*event == nullptr); |
118 | 24 | *event = tox_event_conference_connected_new(mem); |
119 | | |
120 | 24 | if (*event == nullptr) { |
121 | 1 | return false; |
122 | 1 | } |
123 | | |
124 | 23 | return tox_event_conference_connected_unpack_into(*event, bu); |
125 | 24 | } |
126 | | |
127 | | static Tox_Event_Conference_Connected *tox_event_conference_connected_alloc(void *_Nonnull user_data) |
128 | 30 | { |
129 | 30 | Tox_Events_State *state = tox_events_alloc(user_data); |
130 | 30 | assert(state != nullptr); |
131 | | |
132 | 30 | if (state->events == nullptr) { |
133 | 0 | return nullptr; |
134 | 0 | } |
135 | | |
136 | 30 | Tox_Event_Conference_Connected *conference_connected = tox_events_add_conference_connected(state->events, state->mem); |
137 | | |
138 | 30 | if (conference_connected == nullptr) { |
139 | 1 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
140 | 1 | return nullptr; |
141 | 1 | } |
142 | | |
143 | 29 | return conference_connected; |
144 | 30 | } |
145 | | |
146 | | /***************************************************** |
147 | | * |
148 | | * :: event handler |
149 | | * |
150 | | *****************************************************/ |
151 | | |
152 | | void tox_events_handle_conference_connected( |
153 | | Tox *tox, uint32_t conference_number, |
154 | | void *user_data) |
155 | 30 | { |
156 | 30 | Tox_Event_Conference_Connected *conference_connected = tox_event_conference_connected_alloc(user_data); |
157 | | |
158 | 30 | if (conference_connected == nullptr) { |
159 | 1 | return; |
160 | 1 | } |
161 | | |
162 | 29 | tox_event_conference_connected_set_conference_number(conference_connected, conference_number); |
163 | 29 | } |