-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPSME_MsgEnv.cpp
More file actions
241 lines (189 loc) · 6.38 KB
/
Copy pathIPSME_MsgEnv.cpp
File metadata and controls
241 lines (189 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// IPSME_MsgEnv.cpp (sd-bus, class-based)
//
// Created by dev on 2021-10-27.
// Copyright © 2021 Root Interface. All rights reserved.
//
#include <cstdio>
#include <cstring>
#include <systemd/sd-bus.h>
#include "IPSME_MsgEnv.h"
static const char* kpsz_OBJ_PATH = "/dev/IPSME";
static const char* kpsz_INTERFACE = "dev.IPSME";
static const char* kpsz_SIGNAL = "IPSME";
//----------------------------------------------------------------------------------------------------------------
IPSME_MsgEnv::IPSME_MsgEnv()
{
int i_r = sd_bus_open_system(&_p_bus);
if (i_r < 0) {
fprintf(stderr, "IPSME_MsgEnv: failed to connect to system bus: %s\n", strerror(-i_r));
_p_bus = nullptr;
}
}
IPSME_MsgEnv::~IPSME_MsgEnv()
{
if (_p_slot)
_p_slot = sd_bus_slot_unref(_p_slot);
if (_p_bus)
_p_bus = sd_bus_unref(_p_bus);
}
//----------------------------------------------------------------------------------------------------------------
// The message m passed to the callback is only borrowed; do not unref it here.
int IPSME_MsgEnv::_match_trampoline(sd_bus_message* p_m, void* userdata, sd_bus_error* /*p_err*/)
{
IPSME_MsgEnv* p_self = reinterpret_cast<IPSME_MsgEnv*>(userdata);
if (! p_self || ! p_self->_p_callback)
return 0;
const char* psz = nullptr;
int i_r = sd_bus_message_read_basic(p_m, SD_BUS_TYPE_STRING, &psz);
if (i_r < 0) {
fprintf(stderr, "IPSME_MsgEnv: failed to read signal arg: %s\n", strerror(-i_r));
return 0;
}
p_self->_p_callback(psz, p_self->_p_void);
return 0;
}
bool IPSME_MsgEnv::subscribe(tp_callback p_callback, void* p_void)
{
if (! _p_bus)
return false;
_p_callback = p_callback;
_p_void = p_void;
int i_r = sd_bus_match_signal(
_p_bus,
&_p_slot,
nullptr, // const char *sender
kpsz_OBJ_PATH, // const char *path
nullptr, // const char *interface
nullptr, // const char *member
&_match_trampoline,
this
);
if (i_r < 0) {
fprintf(stderr, "IPSME_MsgEnv: failed to add match rule: %s\n", strerror(-i_r));
return false;
}
return true;
}
bool IPSME_MsgEnv::unsubscribe(tp_callback /*p_callback*/)
{
if (_p_slot)
_p_slot = sd_bus_slot_unref(_p_slot);
_p_callback = nullptr;
_p_void = nullptr;
return true;
}
//----------------------------------------------------------------------------------------------------------------
// Build a fresh dev.IPSME signal message; the caller appends its own arg(s) then sd_bus_send()s it.
// Returns nullptr on failure. (Parity with the old namespace API.)
sd_bus_message* IPSME_MsgEnv::sd_bus_message_new()
{
if (! _p_bus)
return nullptr;
sd_bus_message* p_m = nullptr;
int i_r = sd_bus_message_new_signal(
_p_bus,
&p_m,
kpsz_OBJ_PATH, // const char *path
kpsz_INTERFACE, // const char *interface
kpsz_SIGNAL // const char *member
);
if (i_r < 0) {
fprintf(stderr, "IPSME_MsgEnv: failed to create signal: %s\n", strerror(-i_r));
return nullptr;
}
return p_m;
}
// Send p_m on the bus and unref it (consumes the reference). Returns the sd_bus_send result (<0 = error).
IPSME_MsgEnv::RET_TYPE IPSME_MsgEnv::sd_bus_send(sd_bus_message* p_m)
{
if (! _p_bus || ! p_m)
return -1;
int i_r = ::sd_bus_send(_p_bus, p_m, nullptr); // ::-qualified -> the libsystemd free function
sd_bus_message_unref(p_m);
if (i_r < 0)
fprintf(stderr, "IPSME_MsgEnv: failed to send signal: %s\n", strerror(-i_r));
return i_r;
}
//----------------------------------------------------------------------------------------------------------------
// According to <systemd/sd-bus.h> on Ubuntu 20.04, the function sd_bus_emit_signalv() does NOT exist.
// sd_bus_message_appendv() DOES exist and isn't documented on https://www.freedesktop.org/software/systemd/man/sd-bus.html
// but HERE: https://www.freedesktop.org/software/systemd/man/sd_bus_message_append.html#
// RET_TYPE IPSME_MsgEnv::publish(const char* psz_types, va_list va_l_argptr)
// {
// _CLEANUP_(sd_bus_message_unrefp) sd_bus_message* p_m= nullptr;
// // printf("%s: %s \n", __func__, "va_list");
// int i_r;
// i_r= sd_bus_message_new_signal(
// p_bus_,
// &p_m,
// kpsz_OBJ_PATH, // const char *path,
// kpsz_INTERFACE, // const char *interface
// kpsz_SIGNAL // const char *member
// );
// if (i_r < 0)
// return i_r;
// i_r= sd_bus_message_appendv(p_m, psz_types, va_l_argptr);
// if (i_r < 0) {
// fprintf(stderr, "Failed to append va_list arguments: %s\n", strerror(-i_r));
// sd_bus_message_unref(p_m);
// return i_r;
// }
// uint64_t ui64_cookie; // message identifier
// // bus only needs to be set when the message is sent to a different bus than the one it's attached to
// i_r= sd_bus_send(nullptr, p_m, &ui64_cookie);
// return i_r;
// }
// RET_TYPE IPSME_MsgEnv::publish(const char* psz_types, ...)
// {
// // printf("%s: %s \n", __func__, "...");
// va_list va_l_args;
// va_start(va_l_args, psz_types); // types last known argument
// int i_r= IPSME_MsgEnv::publish(psz_types, va_l_args);
// va_end(va_l_args);
// return i_r;
// }
// RET_TYPE IPSME_MsgEnv::publish(std::string str_msg)
// {
// _CLEANUP_(sd_bus_message_unrefp) sd_bus_message* p_m= IPSME_MsgEnv::sd_bus_message_new();
//
// if (! p_m)
// return EXIT_FAILURE;
//
// // printf("%s: [%s] \n", __func__, str_msg.c_str());
//
// int i_r;
//
// i_r = sd_bus_message_append_basic(p_m, SD_BUS_TYPE_STRING, str_msg.c_str());
// if (i_r < 0) {
// fprintf(stderr, "Failed to append string argument: %s\n", strerror(-i_r));
// sd_bus_message_unref(p_m);
// return i_r;
// }
//
// i_r= IPSME_MsgEnv::sd_bus_send(p_m);
//
// return i_r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
// }
bool IPSME_MsgEnv::publish(t_MSG msg)
{
sd_bus_message* p_m = sd_bus_message_new();
if (! p_m)
return false;
int i_r = sd_bus_message_append(p_m, "s", msg);
if (i_r < 0) {
fprintf(stderr, "IPSME_MsgEnv: failed to append string arg: %s\n", strerror(-i_r));
sd_bus_message_unref(p_m);
return false;
}
return sd_bus_send(p_m) >= 0; // member sd_bus_send() unrefs p_m
}
//----------------------------------------------------------------------------------------------------------------
void IPSME_MsgEnv::process_msgs(int /*i_timeout*/)
{
if (! _p_bus)
return;
// drain: process everything pending without blocking, then flush outgoing.
while (sd_bus_process(_p_bus, nullptr) > 0) {}
sd_bus_flush(_p_bus);
}