mesh-io.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2018 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <ell/ell.h>
  14. #include "lib/bluetooth.h"
  15. #include "mesh/mesh-defs.h"
  16. #include "mesh/mesh-io.h"
  17. #include "mesh/mesh-io-api.h"
  18. /* List of Mesh-IO Type headers */
  19. #include "mesh/mesh-io-generic.h"
  20. #include "mesh/mesh-io-unit.h"
  21. /* List of Supported Mesh-IO Types */
  22. static const struct mesh_io_table table[] = {
  23. {MESH_IO_TYPE_GENERIC, &mesh_io_generic},
  24. {MESH_IO_TYPE_UNIT_TEST, &mesh_io_unit},
  25. };
  26. static struct l_queue *io_list;
  27. static bool match_by_io(const void *a, const void *b)
  28. {
  29. return a == b;
  30. }
  31. static bool match_by_type(const void *a, const void *b)
  32. {
  33. const struct mesh_io *io = a;
  34. const enum mesh_io_type type = L_PTR_TO_UINT(b);
  35. return io->type == type;
  36. }
  37. struct mesh_io *mesh_io_new(enum mesh_io_type type, void *opts,
  38. mesh_io_ready_func_t cb, void *user_data)
  39. {
  40. const struct mesh_io_api *api = NULL;
  41. struct mesh_io *io;
  42. uint16_t i;
  43. for (i = 0; i < L_ARRAY_SIZE(table); i++) {
  44. if (table[i].type == type) {
  45. api = table[i].api;
  46. break;
  47. }
  48. }
  49. io = l_queue_find(io_list, match_by_type, L_UINT_TO_PTR(type));
  50. if (!api || !api->init || io)
  51. return NULL;
  52. io = l_new(struct mesh_io, 1);
  53. io->type = type;
  54. io->api = api;
  55. if (!api->init(io, opts, cb, user_data))
  56. goto fail;
  57. if (!io_list)
  58. io_list = l_queue_new();
  59. if (l_queue_push_head(io_list, io))
  60. return io;
  61. fail:
  62. if (api->destroy)
  63. api->destroy(io);
  64. l_free(io);
  65. return NULL;
  66. }
  67. void mesh_io_destroy(struct mesh_io *io)
  68. {
  69. io = l_queue_remove_if(io_list, match_by_io, io);
  70. if (io && io->api && io->api->destroy)
  71. io->api->destroy(io);
  72. l_free(io);
  73. if (l_queue_isempty(io_list)) {
  74. l_queue_destroy(io_list, NULL);
  75. io_list = NULL;
  76. }
  77. }
  78. bool mesh_io_get_caps(struct mesh_io *io, struct mesh_io_caps *caps)
  79. {
  80. io = l_queue_find(io_list, match_by_io, io);
  81. if (io && io->api && io->api->caps)
  82. return io->api->caps(io, caps);
  83. return false;
  84. }
  85. bool mesh_io_register_recv_cb(struct mesh_io *io, const uint8_t *filter,
  86. uint8_t len, mesh_io_recv_func_t cb,
  87. void *user_data)
  88. {
  89. io = l_queue_find(io_list, match_by_io, io);
  90. if (io && io->api && io->api->reg)
  91. return io->api->reg(io, filter, len, cb, user_data);
  92. return false;
  93. }
  94. bool mesh_io_deregister_recv_cb(struct mesh_io *io, const uint8_t *filter,
  95. uint8_t len)
  96. {
  97. io = l_queue_find(io_list, match_by_io, io);
  98. if (io && io->api && io->api->dereg)
  99. return io->api->dereg(io, filter, len);
  100. return false;
  101. }
  102. bool mesh_io_send(struct mesh_io *io, struct mesh_io_send_info *info,
  103. const uint8_t *data, uint16_t len)
  104. {
  105. io = l_queue_find(io_list, match_by_io, io);
  106. if (!io)
  107. io = l_queue_peek_head(io_list);
  108. if (io && io->api && io->api->send)
  109. return io->api->send(io, info, data, len);
  110. return false;
  111. }
  112. bool mesh_io_send_cancel(struct mesh_io *io, const uint8_t *pattern,
  113. uint8_t len)
  114. {
  115. io = l_queue_find(io_list, match_by_io, io);
  116. if (io && io->api && io->api->cancel)
  117. return io->api->cancel(io, pattern, len);
  118. return false;
  119. }