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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2021 Intel Corporation. All rights reserved.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <ell/ell.h>
#include "mesh/mesh-defs.h"
#include "mesh/dbus.h"
#include "mesh/mesh-io.h"
#include "mesh/mesh-io-api.h"
#include "mesh/mesh-io-unit.h"
struct mesh_io_private {
struct mesh_io *io;
struct l_io *sio;
void *user_data;
char *unique_name;
struct l_timeout *tx_timeout;
struct l_queue *rx_regs;
struct l_queue *tx_pkts;
struct sockaddr_un addr;
int fd;
uint16_t interval;
};
struct pvt_rx_reg {
mesh_io_recv_func_t cb;
void *user_data;
uint8_t len;
uint8_t filter[0];
};
struct process_data {
struct mesh_io_private *pvt;
const uint8_t *data;
uint8_t len;
struct mesh_io_recv_info info;
};
struct tx_pkt {
struct mesh_io_send_info info;
bool delete;
uint8_t len;
uint8_t pkt[30];
};
struct tx_pattern {
const uint8_t *data;
uint8_t len;
};
static uint32_t get_instant(void)
{
struct timeval tm;
uint32_t instant;
gettimeofday(&tm, NULL);
instant = tm.tv_sec * 1000;
instant += tm.tv_usec / 1000;
return instant;
}
static uint32_t instant_remaining_ms(uint32_t instant)
{
instant -= get_instant();
return instant;
}
static void process_rx_callbacks(void *v_reg, void *v_rx)
{
struct pvt_rx_reg *rx_reg = v_reg;
struct process_data *rx = v_rx;
if (!memcmp(rx->data, rx_reg->filter, rx_reg->len))
rx_reg->cb(rx_reg->user_data, &rx->info, rx->data, rx->len);
}
static void process_rx(struct mesh_io_private *pvt, int8_t rssi,
uint32_t instant, const uint8_t *addr,
const uint8_t *data, uint8_t len)
{
struct process_data rx = {
.pvt = pvt,
.data = data,
.len = len,
.info.instant = instant,
.info.addr = addr,
.info.chan = 7,
.info.rssi = rssi,
};
l_queue_foreach(pvt->rx_regs, process_rx_callbacks, &rx);
}
static bool incoming(struct l_io *sio, void *user_data)
{
struct mesh_io_private *pvt = user_data;
uint32_t instant;
uint8_t buf[31];
size_t size;
instant = get_instant();
size = recv(pvt->fd, buf, sizeof(buf), MSG_DONTWAIT);
if (size > 9 && buf[0]) {
process_rx(pvt, -20, instant, NULL, buf + 1, (uint8_t)size);
} else if (size == 1 && !buf[0] && pvt->unique_name) {
/* Return DBUS unique name */
size = strlen(pvt->unique_name);
if (size > sizeof(buf) - 2)
return true;
buf[0] = 0;
memcpy(buf + 1, pvt->unique_name, size + 1);
if (send(pvt->fd, buf, size + 2, MSG_DONTWAIT) < 0)
l_error("Failed to send(%d)", errno);
}
return true;
}
static bool find_by_ad_type(const void *a, const void *b)
{
const struct tx_pkt *tx = a;
uint8_t ad_type = L_PTR_TO_UINT(b);
return !ad_type || ad_type == tx->pkt[0];
}
static bool find_by_pattern(const void *a, const void *b)
{
const struct tx_pkt *tx = a;
const struct tx_pattern *pattern = b;
if (tx->len < pattern->len)
return false;
return (!memcmp(tx->pkt, pattern->data, pattern->len));
}
static void free_socket(struct mesh_io_private *pvt)
{
l_io_destroy(pvt->sio);
close(pvt->fd);
unlink(pvt->addr.sun_path);
}
static void hello_callback(struct l_dbus_message *msg, void *user_data)
{
struct mesh_io_private *pvt = user_data;
pvt->unique_name = l_strdup(l_dbus_message_get_destination(msg));
l_debug("User-Daemon unique name: %s", pvt->unique_name);
}
static void get_name(struct l_timeout *timeout, void *user_data)
{
struct mesh_io_private *pvt = user_data;
struct l_dbus *dbus = dbus_get_bus();
struct l_dbus_message *msg;
l_timeout_remove(timeout);
if (!dbus) {
l_timeout_create_ms(20, get_name, pvt, NULL);
return;
}
/* Retrieve unique name */
msg = l_dbus_message_new_method_call(dbus, "org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"GetId");
l_dbus_message_set_arguments(msg, "");
l_dbus_send_with_reply(dbus, msg, hello_callback, pvt, NULL);
}
static void unit_up(void *user_data)
{
struct mesh_io_private *pvt = user_data;
l_debug("Started io-unit");
if (pvt->io && pvt->io->ready)
pvt->io->ready(pvt->user_data, true);
l_timeout_create_ms(1, get_name, pvt, NULL);
}
static bool unit_init(struct mesh_io *io, void *opt, void *user_data)
{
struct mesh_io_private *pvt;
char *sk_path;
size_t size;
l_debug("Starting Unit test IO");
if (!io || io->pvt)
return false;
sk_path = (char *) opt;
pvt = l_new(struct mesh_io_private, 1);
pvt->addr.sun_family = AF_LOCAL;
snprintf(pvt->addr.sun_path, sizeof(pvt->addr.sun_path), "%s",
sk_path);
pvt->fd = socket(PF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (pvt->fd < 0)
goto fail;
unlink(pvt->addr.sun_path);
size = offsetof(struct sockaddr_un, sun_path) +
strlen(pvt->addr.sun_path);
if (bind(pvt->fd, (struct sockaddr *) &pvt->addr, size) < 0)
goto fail;
/* Setup socket handlers */
pvt->sio = l_io_new(pvt->fd);
if (!l_io_set_read_handler(pvt->sio, incoming, pvt, NULL))
goto fail;
pvt->rx_regs = l_queue_new();
pvt->tx_pkts = l_queue_new();
pvt->io = io;
pvt->user_data = user_data;
io->pvt = pvt;
l_idle_oneshot(unit_up, pvt, NULL);
return true;
fail:
l_error("Failed to bind Unit Test socket");
free_socket(pvt);
l_free(pvt);
return false;
}
static bool unit_destroy(struct mesh_io *io)
{
struct mesh_io_private *pvt = io->pvt;
if (!pvt)
return true;
l_free(pvt->unique_name);
l_timeout_remove(pvt->tx_timeout);
l_queue_destroy(pvt->rx_regs, l_free);
l_queue_destroy(pvt->tx_pkts, l_free);
free_socket(pvt);
l_free(pvt);
io->pvt = NULL;
return true;
}
static bool unit_caps(struct mesh_io *io, struct mesh_io_caps *caps)
{
struct mesh_io_private *pvt = io->pvt;
if (!pvt || !caps)
return false;
caps->max_num_filters = 255;
caps->window_accuracy = 50;
return true;
}
static bool simple_match(const void *a, const void *b)
{
return a == b;
}
static void send_pkt(struct mesh_io_private *pvt, struct tx_pkt *tx,
uint16_t interval)
{
if (send(pvt->fd, tx->pkt, tx->len, MSG_DONTWAIT) < 0)
l_error("Failed to send(%d)", errno);
if (tx->delete) {
l_queue_remove_if(pvt->tx_pkts, simple_match, tx);
l_free(tx);
}
}
static void tx_to(struct l_timeout *timeout, void *user_data)
{
struct mesh_io_private *pvt = user_data;
struct tx_pkt *tx;
uint16_t ms;
uint8_t count;
if (!pvt)
return;
tx = l_queue_pop_head(pvt->tx_pkts);
if (!tx) {
l_timeout_remove(timeout);
pvt->tx_timeout = NULL;
return;
}
if (tx->info.type == MESH_IO_TIMING_TYPE_GENERAL) {
ms = tx->info.u.gen.interval;
count = tx->info.u.gen.cnt;
if (count != MESH_IO_TX_COUNT_UNLIMITED)
tx->info.u.gen.cnt--;
} else {
ms = 25;
count = 1;
}
tx->delete = !!(count == 1);
send_pkt(pvt, tx, ms);
if (count == 1) {
/* Recalculate wakeup if we are responding to POLL */
tx = l_queue_peek_head(pvt->tx_pkts);
if (tx && tx->info.type == MESH_IO_TIMING_TYPE_POLL_RSP) {
ms = instant_remaining_ms(tx->info.u.poll_rsp.instant +
tx->info.u.poll_rsp.delay);
}
} else
l_queue_push_tail(pvt->tx_pkts, tx);
if (timeout) {
pvt->tx_timeout = timeout;
l_timeout_modify_ms(timeout, ms);
} else
pvt->tx_timeout = l_timeout_create_ms(ms, tx_to, pvt, NULL);
}
static void tx_worker(void *user_data)
{
struct mesh_io_private *pvt = user_data;
struct tx_pkt *tx;
uint32_t delay;
tx = l_queue_peek_head(pvt->tx_pkts);
if (!tx)
return;
switch (tx->info.type) {
case MESH_IO_TIMING_TYPE_GENERAL:
if (tx->info.u.gen.min_delay == tx->info.u.gen.max_delay)
delay = tx->info.u.gen.min_delay;
else {
l_getrandom(&delay, sizeof(delay));
delay %= tx->info.u.gen.max_delay -
tx->info.u.gen.min_delay;
delay += tx->info.u.gen.min_delay;
}
break;
case MESH_IO_TIMING_TYPE_POLL:
if (tx->info.u.poll.min_delay == tx->info.u.poll.max_delay)
delay = tx->info.u.poll.min_delay;
else {
l_getrandom(&delay, sizeof(delay));
delay %= tx->info.u.poll.max_delay -
tx->info.u.poll.min_delay;
delay += tx->info.u.poll.min_delay;
}
break;
case MESH_IO_TIMING_TYPE_POLL_RSP:
/* Delay until Instant + Delay */
delay = instant_remaining_ms(tx->info.u.poll_rsp.instant +
tx->info.u.poll_rsp.delay);
if (delay > 255)
delay = 0;
break;
default:
return;
}
if (!delay)
tx_to(pvt->tx_timeout, pvt);
else if (pvt->tx_timeout)
l_timeout_modify_ms(pvt->tx_timeout, delay);
else
pvt->tx_timeout = l_timeout_create_ms(delay, tx_to, pvt, NULL);
}
static bool send_tx(struct mesh_io *io, struct mesh_io_send_info *info,
const uint8_t *data, uint16_t len)
{
struct mesh_io_private *pvt = io->pvt;
struct tx_pkt *tx;
bool sending = false;
if (!info || !data || !len || len > sizeof(tx->pkt))
return false;
tx = l_new(struct tx_pkt, 1);
memcpy(&tx->info, info, sizeof(tx->info));
memcpy(&tx->pkt, data, len);
tx->len = len;
if (info->type == MESH_IO_TIMING_TYPE_POLL_RSP)
l_queue_push_head(pvt->tx_pkts, tx);
else {
sending = !l_queue_isempty(pvt->tx_pkts);
l_queue_push_tail(pvt->tx_pkts, tx);
}
if (!sending) {
l_timeout_remove(pvt->tx_timeout);
pvt->tx_timeout = NULL;
l_idle_oneshot(tx_worker, pvt, NULL);
}
return true;
}
static bool tx_cancel(struct mesh_io *io, const uint8_t *data, uint8_t len)
{
struct mesh_io_private *pvt = io->pvt;
struct tx_pkt *tx;
if (!data)
return false;
if (len == 1) {
do {
tx = l_queue_remove_if(pvt->tx_pkts, find_by_ad_type,
L_UINT_TO_PTR(data[0]));
l_free(tx);
} while (tx);
} else {
struct tx_pattern pattern = {
.data = data,
.len = len
};
do {
tx = l_queue_remove_if(pvt->tx_pkts, find_by_pattern,
&pattern);
l_free(tx);
} while (tx);
}
if (l_queue_isempty(pvt->tx_pkts)) {
l_timeout_remove(pvt->tx_timeout);
pvt->tx_timeout = NULL;
}
return true;
}
static bool recv_register(struct mesh_io *io, const uint8_t *filter,
uint8_t len, mesh_io_recv_func_t cb, void *user_data)
{
return true;
}
static bool recv_deregister(struct mesh_io *io, const uint8_t *filter,
uint8_t len)
{
return true;
}
const struct mesh_io_api mesh_io_unit = {
.init = unit_init,
.destroy = unit_destroy,
.caps = unit_caps,
.send = send_tx,
.reg = recv_register,
.dereg = recv_deregister,
.cancel = tx_cancel,
};
|