u_ble.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "u_ble.h"
  2. #include "u_main.h"
  3. //#include "u_fifo.h"
  4. #include "u_global.h"
  5. //struct pack_info
  6. //{
  7. // //uint8_t id;
  8. // volatile uint32_t time;
  9. // uint16_t len;
  10. // uint8_t otime_count;
  11. // uint8_t data[PROTO_DATA_MAX_LEN];
  12. //};
  13. //struct pack_info now_send_pack;
  14. //#include "co_math.h"
  15. uint16_t crc_16(uint8_t *pbuff, uint32_t len)
  16. {
  17. uint32_t i,j;
  18. uint8_t ds;
  19. uint16_t crc = 0xffff;
  20. static uint16_t poly[2] = {0, 0xa001};
  21. for(j = len; j > 0; j--)
  22. {
  23. ds = *pbuff++;
  24. for(i = 0; i < 8; i++)
  25. {
  26. crc = (crc >> 1) ^ poly[(crc ^ ds ) & 1];
  27. ds = ds >> 1;
  28. }
  29. }
  30. return crc;
  31. }
  32. /*
  33. 数据长度值 =协议头(1) + 字段类型(1)+数据长度(1)+数据域长度
  34. CRC16双字节 =协议头+ 字段类型+ 数据长度值+数据域数据
  35. */
  36. extern void decode_app_cmd(uint8_t cmd, uint8_t *data, uint8_t len);
  37. void u_ble_data_recv(uint8_t *ble_data, uint16_t recv_len)
  38. {
  39. //uart_send(ble_data, recv_len);
  40. //ble_data_send(ble_data, recv_len);
  41. uint8_t i = 0;
  42. uint16_t crc, tmp;
  43. if(recv_len>=4)
  44. {
  45. tmp = ble_data[recv_len-1];
  46. tmp = (tmp<<8) | ble_data[recv_len-2];
  47. crc = crc_16(ble_data, recv_len - 2);
  48. #ifdef DEBUG_DRV_APP
  49. U_UART_PRINTF("\r\nrx: ");
  50. for(i = 0; i < recv_len; i++)
  51. {
  52. U_UART_PRINTF("%02X ",ble_data[i]);
  53. }
  54. U_UART_PRINTF("\r\n");
  55. #endif
  56. if(tmp == crc&&ble_data[0]==0xA0&&(recv_len-2)==ble_data[2])//判断crc 数据长度 帧头是否满足
  57. {
  58. decode_app_cmd(ble_data[1], &ble_data[3], ble_data[2]);
  59. }
  60. }
  61. }
  62. #if 0
  63. //{APP_USER_MESS_IND, (ke_msg_func_t)app_user_mess_handler},
  64. struct app_user_rsp
  65. {
  66. uint8_t defult;
  67. };
  68. void send_xxx_rwip(void *arg)
  69. {
  70. struct app_user_rsp *rsp = KE_MSG_ALLOC(APP_USER_MESS_IND, //要发送的消息ID
  71. TASK_APP, //目标消息ID
  72. TASK_NONE, app_user_rsp); //发送消息的ID ,申请的内存大小
  73. rsp->defult = 0;
  74. ke_msg_send(rsp); //发送消息
  75. }
  76. static int u_xxx_handler(ke_msg_id_t const msgid,
  77. void const *param,
  78. ke_task_id_t const dest_id,
  79. ke_task_id_t const src_id)
  80. {
  81. struct app_user_rsp *arg = (struct app_user_rsp*)param;
  82. UART_PRINTF("param_addr = %d", param);
  83. UART_PRINTF("dest_id = %d, src_id = %d \r\n", dest_id, src_id);
  84. return KE_MSG_CONSUMED;
  85. }
  86. #endif