config-server.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <stdbool.h>
  18. #include <inttypes.h>
  19. #include <stdbool.h>
  20. #include <sys/uio.h>
  21. #include <wordexp.h>
  22. #include <glib.h>
  23. #include "tools/mesh/config-model.h"
  24. #include "src/shared/util.h"
  25. #include "src/shared/shell.h"
  26. #include "tools/mesh-gatt/mesh-net.h"
  27. #include "tools/mesh-gatt/keys.h"
  28. #include "tools/mesh-gatt/net.h"
  29. #include "tools/mesh-gatt/node.h"
  30. #include "tools/mesh-gatt/prov-db.h"
  31. #include "tools/mesh-gatt/util.h"
  32. static bool server_msg_recvd(uint16_t src, uint8_t *data,
  33. uint16_t len, void *user_data)
  34. {
  35. uint32_t opcode;
  36. uint8_t msg[32];
  37. struct mesh_node *node;
  38. uint16_t primary;
  39. uint32_t mod_id;
  40. uint16_t ele_addr;
  41. uint8_t ele_idx;
  42. struct mesh_publication pub;
  43. int m, n;
  44. if (mesh_opcode_get(data, len, &opcode, &n)) {
  45. len -= n;
  46. data += n;
  47. } else
  48. return false;
  49. node = node_get_local_node();
  50. if (!node)
  51. return true;
  52. n = 0;
  53. switch (opcode) {
  54. default:
  55. return false;
  56. case OP_CONFIG_DEFAULT_TTL_SET:
  57. if (len != 1 || data[0] > TTL_MASK || data[0] == 1)
  58. return true;
  59. if (data[0] <= TTL_MASK) {
  60. node_set_default_ttl(node, data[0]);
  61. prov_db_node_set_ttl(node, data[0]);
  62. }
  63. /* Fall Through */
  64. case OP_CONFIG_DEFAULT_TTL_GET:
  65. n = mesh_opcode_set(OP_CONFIG_DEFAULT_TTL_STATUS, msg);
  66. msg[n++] = node_get_default_ttl(node);
  67. break;
  68. case OP_CONFIG_MODEL_PUB_SET:
  69. if (len != 11 && len != 13)
  70. return true;
  71. bt_shell_printf("Set publication\n");
  72. ele_addr = get_le16(data);
  73. mod_id = get_le16(data + 9);
  74. if (len == 14)
  75. mod_id = (mod_id << 16) | get_le16(data + 11);
  76. else
  77. mod_id |= 0xffff0000;
  78. pub.u.addr16 = get_le16(data + 2);
  79. pub.app_idx = get_le16(data + 4);
  80. pub.ttl = data[6];
  81. pub.period = data[7];
  82. m = (data[7] & 0x3f);
  83. switch (data[7] >> 6) {
  84. case 0:
  85. bt_shell_printf("Period: %d ms\n", m * 100);
  86. break;
  87. case 2:
  88. m *= 10;
  89. /* fall through */
  90. case 1:
  91. bt_shell_printf("Period: %d sec\n", m);
  92. break;
  93. case 3:
  94. bt_shell_printf("Period: %d min\n", m * 10);
  95. break;
  96. }
  97. pub.retransmit = data[8];
  98. bt_shell_printf("Retransmit count: %d\n", data[8] >> 5);
  99. bt_shell_printf("Retransmit Interval Steps: %d\n",
  100. data[8] & 0x1f);
  101. ele_idx = ele_addr - node_get_primary(node);
  102. if (node_model_pub_set(node, ele_idx, mod_id, &pub)) {
  103. prov_db_node_set_model_pub(node, ele_idx, mod_id,
  104. node_model_pub_get(node, ele_idx, mod_id));
  105. }
  106. break;
  107. }
  108. if (!n)
  109. return true;
  110. primary = node_get_primary(node);
  111. if (src != primary)
  112. net_access_layer_send(node_get_default_ttl(node), primary,
  113. src, APP_IDX_DEV, msg, n);
  114. else
  115. node_local_data_handler(primary, src, node_get_iv_index(node),
  116. node_get_sequence_number(node),
  117. APP_IDX_DEV, msg, n);
  118. return true;
  119. }
  120. static struct mesh_model_ops server_cbs = {
  121. server_msg_recvd,
  122. NULL,
  123. NULL,
  124. NULL
  125. };
  126. bool config_server_init(void)
  127. {
  128. if (!node_local_model_register(PRIMARY_ELEMENT_IDX,
  129. CONFIG_SERVER_MODEL_ID,
  130. &server_cbs, NULL))
  131. return false;
  132. return true;
  133. }