proc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "nrf_drv_spis.h"
  2. #include "ble_gap.h"
  3. #include "type.h"
  4. #include "queue.h"
  5. #include "ver.h"
  6. #include "crc.h"
  7. #include "spi.h"
  8. #include "flash.h"
  9. #include "nrf_delay.h"
  10. #include "main.h"
  11. #include "md5.h"
  12. #include "ble_dtm.h"
  13. #include "proc.h"
  14. #include "sys_lib.h"
  15. //DTM TEST
  16. extern uint8_t dtm_flag;
  17. uint8_t ble_cfg_flag;
  18. uint8_t stack_flag = 1; // 1:open 0:close
  19. extern volatile bool spis_eint_on;
  20. queue_process_func g_s_ble_send_func, g_s_spi_send_func;
  21. void spis_process_callback_register(queue_process_func ble_send_func, queue_process_func spi_send_func)
  22. {
  23. if((NULL != ble_send_func) && (NULL != spi_send_func))
  24. {
  25. g_s_ble_send_func = ble_send_func;
  26. g_s_spi_send_func = spi_send_func;
  27. }
  28. }
  29. void spis_transfer_data(uint8_t cmd, uint8_t* src, uint16_t len)
  30. {
  31. uint8_t tx_buf[MAX_BUF] = {0};
  32. pack_header_t* pHeader = (pack_header_t*)tx_buf;
  33. uint16_t header_len = sizeof(pack_header_t);
  34. uint16_t crc;
  35. uint16_t pack_len = len;
  36. spis_pin_reset();
  37. memset(tx_buf, 0, MAX_BUF);
  38. pHeader->ident[0] = 0xF1;
  39. pHeader->ident[1] = 0xF1;
  40. pHeader->len = 3 + len; //包长度+指令ID+数据内容+校验码
  41. pHeader->message_code = cmd;
  42. if((len>0)&&(len<=MAX_BUF-header_len-2))
  43. {
  44. memcpy(&tx_buf[header_len], src, len);
  45. }
  46. crc = make_crc(&tx_buf[2], header_len-2+pack_len);
  47. tx_buf[header_len + pack_len] = (crc >> 8) & 0xFF;
  48. tx_buf[header_len + pack_len + 1] = crc & 0xFF;
  49. pack_len = header_len + pack_len + 2; //头+数据内容+crc
  50. DLOG("spi_tx_data:%s,len:%d", SYS_LibHex2Str(tx_buf, pack_len), pack_len);
  51. g_s_spi_send_func(tx_buf, pack_len);
  52. }
  53. void spis_version_report(void)
  54. {
  55. uint8_t ver = get_version();
  56. spis_transfer_data(BLE_CMD_VER, &ver, 1);
  57. }
  58. void spis_ble_heart(void)
  59. {
  60. spis_transfer_data(BLE_CMD_HEART, NULL, 0);
  61. }
  62. void spis_ota_resend(void)
  63. {
  64. }
  65. void small_big_transfer16(uint16_t *data)
  66. {
  67. uint8_t *p_data = (uint8_t *)data;
  68. uint8_t a = p_data[0];
  69. uint8_t b = p_data[1];
  70. p_data[0] = b;
  71. p_data[1] = a;
  72. }
  73. static uint8_t tx_buf[MAX_BUF];
  74. void spis_process_data(uint8_t* data, uint16_t length)
  75. {
  76. pack_header_t* pHeader = (pack_header_t*)data;
  77. uint16_t check_crc = 0, pack_crc;
  78. uint8_t pack_len;
  79. uint8_t pack_cmd;
  80. uint8_t header_len = sizeof(pack_header_t);
  81. uint8_t crc_index;
  82. memset(tx_buf, 0, MAX_BUF);
  83. pack_cmd = pHeader->message_code;
  84. pack_len = pHeader->len;
  85. crc_index = header_len-2+pack_len-3;
  86. check_crc = make_crc(&data[2], crc_index);
  87. memcpy(&pack_crc, &data[crc_index+2], 2);
  88. small_big_transfer16(&pack_crc);
  89. if(check_crc != pack_crc)
  90. {
  91. return;
  92. }
  93. if(pHeader->message_code == BLE_CMD_VER)
  94. {
  95. }
  96. else if(pack_cmd == BLE_CMD_TRANSFER)
  97. {
  98. DLOG("cmd_transfer:%s,len:%d", SYS_LibHex2Str(&data[header_len], pack_len-3), pack_len-3);
  99. memcpy(tx_buf, &data[header_len], pack_len-3);
  100. g_s_ble_send_func(tx_buf, pack_len-3);
  101. }
  102. else if(pHeader->message_code == BLE_CMD_CFG)
  103. {
  104. memcpy(tx_buf, &data[header_len], pack_len-3);
  105. ble_configure_all(tx_buf, pack_len-3);
  106. }
  107. else if(pHeader->message_code == BLE_CMD_DTM_STACK_CHECK)
  108. {
  109. if(ble_cfg_flag)
  110. {
  111. spis_transfer_data(BLE_CMD_DTM_STACK_CHECK, NULL, 0);
  112. }
  113. }
  114. else if(pHeader->message_code == BLE_CMD_DTM_STACK_CLOSE)
  115. {
  116. dtm_flag = 1;
  117. }
  118. else if(pHeader->message_code == BLE_CMD_DTM_TX_ON)
  119. {
  120. dtm_flag = 2;
  121. }
  122. else if(pHeader->message_code == BLE_CMD_DTM_TX_OFF)
  123. {
  124. dtm_flag = 3;
  125. }
  126. else if(pHeader->message_code == BLE_CMD_HEART)
  127. {
  128. spis_transfer_data(BLE_CMD_HEART, NULL, 0);
  129. }
  130. else
  131. {
  132. NRF_LOG_PRINTF("SPI Process Non Data \r\n");
  133. }
  134. }
  135. uint16_t spi_tlv_get_value(uint16_t id, void* input,uint16_t inputLen, void* output)
  136. {
  137. uint8_t* tmp;
  138. uint16_t curr = 0;
  139. uint16_t len = 0;
  140. uint8_t* end;
  141. tmp = input;
  142. end = (uint8_t*)input + inputLen;
  143. while (tmp + 4 <= end)
  144. {
  145. curr = spi_byte2short(tmp);
  146. tmp += 2;
  147. len = spi_byte2short(tmp);
  148. tmp += 2;
  149. if( tmp + len > end )
  150. return 0;
  151. if (curr == id)
  152. {
  153. memcpy(output, tmp, len);
  154. return len;
  155. }
  156. tmp += len;
  157. }
  158. return 0;
  159. }
  160. uint16_t spi_byte2short(uint8_t* bytes)
  161. {
  162. uint16_t result = bytes[1] & 0xFF;
  163. result |= ((bytes[0] << 8) & 0xFF00);
  164. return result;
  165. }