onoff-model.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <readline/readline.h>
  23. #include <readline/history.h>
  24. #include <glib.h>
  25. #include "src/shared/shell.h"
  26. #include "src/shared/util.h"
  27. #include "tools/mesh-gatt/mesh-net.h"
  28. #include "tools/mesh-gatt/keys.h"
  29. #include "tools/mesh-gatt/net.h"
  30. #include "tools/mesh-gatt/node.h"
  31. #include "tools/mesh-gatt/prov-db.h"
  32. #include "tools/mesh-gatt/util.h"
  33. #include "tools/mesh-gatt/onoff-model.h"
  34. static uint8_t trans_id;
  35. static uint16_t onoff_app_idx = APP_IDX_INVALID;
  36. static int client_bind(uint16_t app_idx, int action)
  37. {
  38. if (action == ACTION_ADD) {
  39. if (onoff_app_idx != APP_IDX_INVALID) {
  40. return MESH_STATUS_INSUFF_RESOURCES;
  41. } else {
  42. onoff_app_idx = app_idx;
  43. bt_shell_printf("On/Off client model: new binding"
  44. " %4.4x\n", app_idx);
  45. }
  46. } else {
  47. if (onoff_app_idx == app_idx)
  48. onoff_app_idx = APP_IDX_INVALID;
  49. }
  50. return MESH_STATUS_SUCCESS;
  51. }
  52. static void print_remaining_time(uint8_t remaining_time)
  53. {
  54. uint8_t step = (remaining_time & 0xc0) >> 6;
  55. uint8_t count = remaining_time & 0x3f;
  56. int secs = 0, msecs = 0, minutes = 0, hours = 0;
  57. switch (step) {
  58. case 0:
  59. msecs = 100 * count;
  60. secs = msecs / 1000;
  61. msecs -= (secs * 1000);
  62. break;
  63. case 1:
  64. secs = 1 * count;
  65. minutes = secs / 60;
  66. secs -= (minutes * 60);
  67. break;
  68. case 2:
  69. secs = 10 * count;
  70. minutes = secs / 60;
  71. secs -= (minutes * 60);
  72. break;
  73. case 3:
  74. minutes = 10 * count;
  75. hours = minutes / 60;
  76. minutes -= (hours * 60);
  77. break;
  78. default:
  79. break;
  80. }
  81. bt_shell_printf("\n\t\tRemaining time: %d hrs %d mins %d secs %d"
  82. " msecs\n", hours, minutes, secs, msecs);
  83. }
  84. static bool client_msg_recvd(uint16_t src, uint8_t *data,
  85. uint16_t len, void *user_data)
  86. {
  87. uint32_t opcode;
  88. int n;
  89. if (mesh_opcode_get(data, len, &opcode, &n)) {
  90. len -= n;
  91. data += n;
  92. } else
  93. return false;
  94. bt_shell_printf("On Off Model Message received (%d) opcode %x\n",
  95. len, opcode);
  96. print_byte_array("\t",data, len);
  97. switch (opcode) {
  98. default:
  99. return false;
  100. case OP_GENERIC_ONOFF_STATUS:
  101. if (len != 1 && len != 3)
  102. break;
  103. bt_shell_printf("Node %4.4x: Off Status present = %s",
  104. src, data[0] ? "ON" : "OFF");
  105. if (len == 3) {
  106. bt_shell_printf(", target = %s",
  107. data[1] ? "ON" : "OFF");
  108. print_remaining_time(data[2]);
  109. } else
  110. bt_shell_printf("\n");
  111. break;
  112. }
  113. return true;
  114. }
  115. static uint32_t target;
  116. static uint32_t parms[8];
  117. static uint32_t read_input_parameters(int argc, char *argv[])
  118. {
  119. uint32_t i;
  120. if (!argc)
  121. return 0;
  122. --argc;
  123. ++argv;
  124. if (!argc || argv[0][0] == '\0')
  125. return 0;
  126. memset(parms, 0xff, sizeof(parms));
  127. for (i = 0; i < sizeof(parms)/sizeof(parms[0]) && i < (unsigned) argc;
  128. i++) {
  129. sscanf(argv[i], "%x", &parms[i]);
  130. if (parms[i] == 0xffffffff)
  131. break;
  132. }
  133. return i;
  134. }
  135. static void cmd_set_node(int argc, char *argv[])
  136. {
  137. uint32_t dst;
  138. char *end;
  139. dst = strtol(argv[1], &end, 16);
  140. if (end != (argv[1] + 4)) {
  141. bt_shell_printf("Bad unicast address %s: "
  142. "expected format 4 digit hex\n", argv[1]);
  143. target = UNASSIGNED_ADDRESS;
  144. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  145. } else {
  146. bt_shell_printf("Controlling ON/OFF for node %4.4x\n", dst);
  147. target = dst;
  148. set_menu_prompt("on/off", argv[1]);
  149. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  150. }
  151. }
  152. static bool send_cmd(uint8_t *buf, uint16_t len)
  153. {
  154. struct mesh_node *node = node_get_local_node();
  155. uint8_t ttl;
  156. if(!node)
  157. return false;
  158. ttl = node_get_default_ttl(node);
  159. return net_access_layer_send(ttl, node_get_primary(node),
  160. target, onoff_app_idx, buf, len);
  161. }
  162. static void cmd_get_status(int argc, char *argv[])
  163. {
  164. uint16_t n;
  165. uint8_t msg[32];
  166. struct mesh_node *node;
  167. if (IS_UNASSIGNED(target)) {
  168. bt_shell_printf("Destination not set\n");
  169. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  170. }
  171. node = node_find_by_addr(target);
  172. if (!node)
  173. return;
  174. n = mesh_opcode_set(OP_GENERIC_ONOFF_GET, msg);
  175. if (!send_cmd(msg, n)) {
  176. bt_shell_printf("Failed to send \"GENERIC ON/OFF GET\"\n");
  177. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  178. }
  179. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  180. }
  181. static void cmd_set(int argc, char *argv[])
  182. {
  183. uint16_t n;
  184. uint8_t msg[32];
  185. struct mesh_node *node;
  186. if (IS_UNASSIGNED(target)) {
  187. bt_shell_printf("Destination not set\n");
  188. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  189. }
  190. node = node_find_by_addr(target);
  191. if (!node)
  192. return;
  193. if ((read_input_parameters(argc, argv) != 1) &&
  194. parms[0] != 0 && parms[0] != 1) {
  195. bt_shell_printf("Bad arguments: Expecting \"0\" or \"1\"\n");
  196. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  197. }
  198. n = mesh_opcode_set(OP_GENERIC_ONOFF_SET, msg);
  199. msg[n++] = parms[0];
  200. msg[n++] = trans_id++;
  201. if (!send_cmd(msg, n)) {
  202. bt_shell_printf("Failed to send \"GENERIC ON/OFF SET\"\n");
  203. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  204. }
  205. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  206. }
  207. static const struct bt_shell_menu onoff_menu = {
  208. .name = "onoff",
  209. .desc = "On/Off Model Submenu",
  210. .entries = {
  211. {"target", "<unicast>", cmd_set_node,
  212. "Set node to configure"},
  213. {"get", NULL, cmd_get_status,
  214. "Get ON/OFF status"},
  215. {"onoff", "<0/1>", cmd_set,
  216. "Send \"SET ON/OFF\" command"},
  217. {} },
  218. };
  219. static struct mesh_model_ops client_cbs = {
  220. client_msg_recvd,
  221. client_bind,
  222. NULL,
  223. NULL
  224. };
  225. bool onoff_client_init(uint8_t ele)
  226. {
  227. if (!node_local_model_register(ele, GENERIC_ONOFF_CLIENT_MODEL_ID,
  228. &client_cbs, NULL))
  229. return false;
  230. bt_shell_add_submenu(&onoff_menu);
  231. return true;
  232. }