u_ble.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. tmp = ble_data[recv_len-1];
  44. tmp = (tmp<<8) | ble_data[recv_len-2];
  45. crc = crc_16(ble_data, recv_len - 2);
  46. U_UART_PRINTF("\r\nrx: ");
  47. for(i = 0; i < recv_len; i++)
  48. {
  49. U_UART_PRINTF("%02X ",ble_data[i]);
  50. }
  51. U_UART_PRINTF("\r\n");
  52. if(tmp == crc&&ble_data[0]==0xA0&&(recv_len-2)==ble_data[2])//判断crc 数据长度 帧头是否满足
  53. {
  54. decode_app_cmd(ble_data[1], &ble_data[3], ble_data[2]);
  55. }
  56. }
  57. void ble_data_send(uint8_t* buf, uint8_t len)
  58. {
  59. }
  60. #if 0
  61. //{APP_USER_MESS_IND, (ke_msg_func_t)app_user_mess_handler},
  62. struct app_user_rsp
  63. {
  64. uint8_t defult;
  65. };
  66. void send_xxx_rwip(void *arg)
  67. {
  68. struct app_user_rsp *rsp = KE_MSG_ALLOC(APP_USER_MESS_IND, //要发送的消息ID
  69. TASK_APP, //目标消息ID
  70. TASK_NONE, app_user_rsp); //发送消息的ID ,申请的内存大小
  71. rsp->defult = 0;
  72. ke_msg_send(rsp); //发送消息
  73. }
  74. static int u_xxx_handler(ke_msg_id_t const msgid,
  75. void const *param,
  76. ke_task_id_t const dest_id,
  77. ke_task_id_t const src_id)
  78. {
  79. struct app_user_rsp *arg = (struct app_user_rsp*)param;
  80. UART_PRINTF("param_addr = %d", param);
  81. UART_PRINTF("dest_id = %d, src_id = %d \r\n", dest_id, src_id);
  82. return KE_MSG_CONSUMED;
  83. }
  84. #endif