wm_ble_mesh_gen_level_server.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "wm_bt_config.h"
  2. #if (WM_MESH_INCLUDED == CFG_ON)
  3. #include <assert.h>
  4. #include "mesh/mesh.h"
  5. /* BLE */
  6. #include "nimble/ble.h"
  7. #include "host/ble_hs.h"
  8. #include "mesh/glue.h"
  9. #include "wm_bt_util.h"
  10. #include "wm_ble_mesh.h"
  11. #include "wm_ble_mesh_gen_level_server.h"
  12. struct bt_mesh_model_pub gen_level_pub_srv;
  13. static int16_t gen_level_state;
  14. static void gen_level_status(struct bt_mesh_model *model,
  15. struct bt_mesh_msg_ctx *ctx)
  16. {
  17. struct os_mbuf *msg = NET_BUF_SIMPLE(4);
  18. TLS_BT_APPL_TRACE_DEBUG("#mesh-level STATUS\r\n");
  19. bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_2(0x82, 0x08));
  20. net_buf_simple_add_le16(msg, gen_level_state);
  21. if(bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
  22. TLS_BT_APPL_TRACE_DEBUG("#mesh-level STATUS: send status failed\r\n");
  23. }
  24. os_mbuf_free_chain(msg);
  25. }
  26. static void gen_level_get(struct bt_mesh_model *model,
  27. struct bt_mesh_msg_ctx *ctx,
  28. struct os_mbuf *buf)
  29. {
  30. TLS_BT_APPL_TRACE_DEBUG("#mesh-level GET\r\n");
  31. gen_level_status(model, ctx);
  32. }
  33. static void gen_level_set(struct bt_mesh_model *model,
  34. struct bt_mesh_msg_ctx *ctx,
  35. struct os_mbuf *buf)
  36. {
  37. int16_t level;
  38. level = (int16_t) net_buf_simple_pull_le16(buf);
  39. TLS_BT_APPL_TRACE_DEBUG("#mesh-level SET: level=%d\r\n", level);
  40. gen_level_status(model, ctx);
  41. gen_level_state = level;
  42. TLS_BT_APPL_TRACE_DEBUG("#mesh-level: level=%d\r\n", gen_level_state);
  43. }
  44. static void gen_level_set_unack(struct bt_mesh_model *model,
  45. struct bt_mesh_msg_ctx *ctx,
  46. struct os_mbuf *buf)
  47. {
  48. int16_t level;
  49. level = (int16_t) net_buf_simple_pull_le16(buf);
  50. TLS_BT_APPL_TRACE_DEBUG("#mesh-level SET-UNACK: level=%d\r\n", level);
  51. gen_level_state = level;
  52. TLS_BT_APPL_TRACE_DEBUG("#mesh-level: level=%d\r\n", gen_level_state);
  53. }
  54. static void gen_delta_set(struct bt_mesh_model *model,
  55. struct bt_mesh_msg_ctx *ctx,
  56. struct os_mbuf *buf)
  57. {
  58. int16_t delta_level;
  59. delta_level = (int16_t) net_buf_simple_pull_le16(buf);
  60. TLS_BT_APPL_TRACE_DEBUG("#mesh-level DELTA-SET: delta_level=%d\r\n", delta_level);
  61. gen_level_status(model, ctx);
  62. gen_level_state += delta_level;
  63. TLS_BT_APPL_TRACE_DEBUG("#mesh-level: level=%d\r\n", gen_level_state);
  64. }
  65. static void gen_delta_set_unack(struct bt_mesh_model *model,
  66. struct bt_mesh_msg_ctx *ctx,
  67. struct os_mbuf *buf)
  68. {
  69. int16_t delta_level;
  70. delta_level = (int16_t) net_buf_simple_pull_le16(buf);
  71. TLS_BT_APPL_TRACE_DEBUG("#mesh-level DELTA-SET: delta_level=%d\r\n", delta_level);
  72. gen_level_state += delta_level;
  73. TLS_BT_APPL_TRACE_DEBUG("#mesh-level: level=%d\r\n", gen_level_state);
  74. }
  75. static void gen_move_set(struct bt_mesh_model *model,
  76. struct bt_mesh_msg_ctx *ctx,
  77. struct os_mbuf *buf)
  78. {
  79. }
  80. static void gen_move_set_unack(struct bt_mesh_model *model,
  81. struct bt_mesh_msg_ctx *ctx,
  82. struct os_mbuf *buf)
  83. {
  84. }
  85. const struct bt_mesh_model_op gen_level_op[] = {
  86. { BT_MESH_MODEL_OP_2(0x82, 0x05), 0, gen_level_get },
  87. { BT_MESH_MODEL_OP_2(0x82, 0x06), 3, gen_level_set },
  88. { BT_MESH_MODEL_OP_2(0x82, 0x07), 3, gen_level_set_unack },
  89. { BT_MESH_MODEL_OP_2(0x82, 0x09), 5, gen_delta_set },
  90. { BT_MESH_MODEL_OP_2(0x82, 0x0a), 5, gen_delta_set_unack },
  91. { BT_MESH_MODEL_OP_2(0x82, 0x0b), 3, gen_move_set },
  92. { BT_MESH_MODEL_OP_2(0x82, 0x0c), 3, gen_move_set_unack },
  93. BT_MESH_MODEL_OP_END,
  94. };
  95. #endif