ble_cmd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "ble_cmd.h"
  2. #include "ble.h"
  3. #include "uart.h"
  4. #include <stdint.h>
  5. #include <string.h>
  6. BLE_CMD_ASYN g_ble_cmd_asyn_table[] = {
  7. {0x05, ble_exec_cmd_asyn},
  8. {0x07, ble_exec_cmd07_asyn},
  9. {0x08, ble_exec_cmd_asyn},
  10. {0x0d, ble_exec_cmd_asyn},
  11. {0x0e, ble_exec_cmd_asyn},
  12. {0x18, ble_exec_cmd_asyn},
  13. {0x41, ble_exec_cmd_asyn},
  14. };
  15. int32_t ble_send_to_ec95(uint8_t cmd, uint8_t data[], uint8_t length)
  16. {
  17. uint8_t s_data[EC95_MTU + 4] = {0};
  18. int32_t len = 0;
  19. if (length > EC95_MTU)
  20. {
  21. return 2;
  22. }
  23. s_data[len++] = 0x01;
  24. s_data[len++] = 0xFC;
  25. s_data[len++] = cmd;
  26. s_data[len++] = length;
  27. for (int32_t i = 0; i < length; i++)
  28. {
  29. s_data[len++] = data[i];
  30. }
  31. usart_send_it(&usart2_context, s_data, len);
  32. return 0;
  33. }
  34. int32_t ble_cmd_exec(uint8_t cmd, uint8_t idata[], int32_t ilen, uint8_t odata[], int32_t *olen, int32_t ms)
  35. {
  36. int32_t ret;
  37. ret = ble_send_to_ec95(cmd, idata, ilen);
  38. if (ret != 0)
  39. {
  40. return 1;
  41. }
  42. ret = ble_data_recv(odata, *olen, ms);
  43. if (ret <= 0)
  44. {
  45. return 2;
  46. }
  47. return 0;
  48. }
  49. int32_t ble_exec_cmd05(REQ_CMD05 *req)
  50. {
  51. RSP_COMM_CMD rsp;
  52. int32_t ret;
  53. int32_t len = sizeof(RSP_COMM_CMD);
  54. ret = ble_cmd_exec(0x05, (uint8_t *)req, sizeof(REQ_CMD05), (uint8_t *)&rsp, &len, CMD_T0);
  55. if (ret != 0)
  56. {
  57. return -1;
  58. }
  59. return rsp.status;
  60. }
  61. int32_t ble_exec_cmd07(REQ_CMD07 *req, int32_t timeout)
  62. {
  63. RSP_COMM_CMD rsp;
  64. int32_t ret;
  65. int32_t len = sizeof(RSP_COMM_CMD);
  66. ret = ble_cmd_exec(0x07, (uint8_t *)req->name, strlen(req->name), (uint8_t *)&rsp, &len, timeout);
  67. if (ret != 0)
  68. {
  69. return -1;
  70. }
  71. return rsp.status;
  72. }
  73. int32_t ble_exec_cmd08(REQ_CMD08 *req, RSP_CMD08 *rsp)
  74. {
  75. int32_t ret;
  76. int32_t len = sizeof(RSP_CMD08);
  77. ret = ble_cmd_exec(0x08, (uint8_t *)req, sizeof(REQ_CMD08), (uint8_t *)rsp, &len, CMD_T0);
  78. if (ret != 0)
  79. {
  80. return -1;
  81. }
  82. return rsp->hdr.status;
  83. }
  84. int32_t ble_exec_cmd0a(REQ_CMD0A *req, RSP_CMD0A *rsp)
  85. {
  86. int32_t ret;
  87. int32_t len = sizeof(RSP_CMD0A);
  88. ret = ble_cmd_exec(0x0A, (uint8_t *)req, sizeof(REQ_CMD0A), (uint8_t *)rsp, &len, CMD_T0);
  89. if (ret != 0)
  90. {
  91. return -1;
  92. }
  93. return rsp->hdr.status;
  94. }
  95. int32_t ble_exec_cmd0b(REQ_CMD0B *req)
  96. {
  97. RSP_COMM_CMD rsp;
  98. int32_t ret;
  99. int32_t len = sizeof(RSP_COMM_CMD);
  100. ret = ble_cmd_exec(0x0B, (uint8_t *)req, sizeof(REQ_CMD0B), (uint8_t *)&rsp, &len, CMD_T0);
  101. if (ret != 0)
  102. {
  103. return -1;
  104. }
  105. return rsp.status;
  106. }
  107. int32_t ble_exec_cmd0c(REQ_CMD0C *req, RSP_CMD0C *rsp)
  108. {
  109. int32_t ret;
  110. int32_t len = sizeof(RSP_CMD0C);
  111. ret = ble_cmd_exec(0x0C, (uint8_t *)req, sizeof(REQ_CMD0C), (uint8_t *)rsp, &len, CMD_T0);
  112. if (ret != 0)
  113. {
  114. return -1;
  115. }
  116. return rsp->hdr.status;
  117. }
  118. int32_t ble_exec_cmd0d(uint8_t save_flg, uint8_t *adv_data, uint8_t adv_data_len)
  119. {
  120. RSP_COMM_CMD rsp;
  121. int32_t ret;
  122. uint8_t req[24] = {0};
  123. int32_t len = 0;
  124. req[len++] = save_flg;
  125. req[len++] = 1 + adv_data_len;
  126. req[len++] = 0xFF;
  127. memcpy(&req[len], adv_data, adv_data_len);
  128. len += adv_data_len;
  129. ret = ble_cmd_exec(0x0D, (uint8_t *)req, len, (uint8_t *)&rsp, &len, CMD_T0);
  130. if (ret != 0)
  131. {
  132. return -1;
  133. }
  134. return adv_data_len;
  135. }
  136. int32_t ble_exec_cmd0e(REQ_CMD0E *req, RSP_CMD0E *rsp)
  137. {
  138. int32_t ret;
  139. int32_t len = sizeof(RSP_CMD0E);
  140. ret = ble_cmd_exec(0x0E, (uint8_t *)req, sizeof(REQ_CMD0E), (uint8_t *)rsp, &len, CMD_T0);
  141. if (ret != 0)
  142. {
  143. return -1;
  144. }
  145. return rsp->hdr.status;
  146. }
  147. int32_t ble_exec_cmd18(REQ_CMD18 *req, RSP_CMD18 *rsp)
  148. {
  149. int32_t ret;
  150. int32_t len = sizeof(RSP_CMD18);
  151. ret = ble_cmd_exec(0x0E, (uint8_t *)req, sizeof(REQ_CMD18), (uint8_t *)rsp, &len, CMD_T0);
  152. if (ret != 0)
  153. {
  154. return -1;
  155. }
  156. return rsp->hdr.status;
  157. }
  158. int32_t ble_exec_cmd_asyn(uint8_t cmd, uint8_t *data, uint8_t length)
  159. {
  160. int32_t ret;
  161. ret = ble_send_to_ec95(cmd, (uint8_t *)data, length);
  162. if (ret != 0)
  163. {
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. int32_t ble_exec_cmd07_asyn(uint8_t cmd, uint8_t *data, uint8_t length)
  169. {
  170. int32_t ret;
  171. REQ_CMD07 *req = (REQ_CMD07 *)data;
  172. ret = ble_send_to_ec95(0x07, (uint8_t *)req->name, strlen(req->name));
  173. if (ret != 0)
  174. {
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. int32_t countof_asyn_cmd_table(void)
  180. {
  181. return sizeof(g_ble_cmd_asyn_table) / sizeof(BLE_CMD_ASYN);
  182. }