aicare_handler.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "aicare_handler.h"
  2. #include "aicare_config.h"
  3. #include "os_mem.h"
  4. #define AICARE_LOG printf
  5. /*
  6. vid: 0x005E
  7. uint32_t key[4] = {0xEE7F92D2, 0x446A4386, 0xBE4319B1, 0x34BC10B9}
  8. 握手指令 payload 示例(演示固定值):
  9. App 下发明文:
  10. HEX: <00010203 04050607 08090A0B 0C0D0E0F>
  11. 下位机 反馈密文:
  12. HEX: <81BEB013 AEEEFFB0 86494180 7D9A84E1>
  13. 握手指令 payload 示例(随机值):
  14. App 下发明文:
  15. HEX: <84536762 D5B882EA 117BFE43 A49A0407>
  16. 下位机 反馈密文:
  17. HEX: <AA206138 585F7E63 31299D3C 6FBE658F>
  18. */
  19. static uint32_t aicare_key[4] = { 0xEE7F92D2, 0x446A4386, 0xBE4319B1, 0x34BC10B9 };
  20. //## 加密算法
  21. static void encrypt_tea(uint32_t *v, uint32_t *key) {
  22. uint32_t v0 = v[0], v1 = v[1], sum = 0, i=0;
  23. uint32_t delta = 0x9e3779b9;
  24. uint32_t k0 = key[0], k1 = key[1], k2 = key[2], k3 = key[3];
  25. for (i = 0; i < 32; i++) {
  26. sum += delta;
  27. uint32_t a=(v1 << 4);
  28. uint32_t b=(v1 >> 5);
  29. v0 += (a + k0) ^ (v1 + sum) ^ (b + k1);
  30. uint32_t c=(v0 << 4);
  31. uint32_t d=(v0 >> 5);
  32. v1 += (c + k2) ^ (v0 + sum) ^ (d + k3);
  33. }
  34. v[0] = v0;
  35. v[1] = v1;
  36. }
  37. //16byte加密
  38. static void encrypt(uint8_t *p,uint32_t *key) {
  39. union u8_to_u32 {
  40. uint8_t u8_data[16];
  41. uint32_t u32_data[4];
  42. }v={0};
  43. memcpy((uint8_t *) &v.u8_data, p, sizeof(v));
  44. encrypt_tea(&v.u32_data[0], key);
  45. encrypt_tea(&v.u32_data[2], key);
  46. memcpy(p, (uint8_t *) &v.u8_data, sizeof(v));
  47. }
  48. static uint16_t aicare_sum_check(uint8_t *pbuff, uint16_t len)
  49. {
  50. if((len==0) || (pbuff==NULL))
  51. return 0;
  52. uint16_t temp = 0;
  53. for(uint16_t j = 0; j < len; j++)
  54. {
  55. temp += pbuff[j];
  56. }
  57. return temp;
  58. }
  59. void aicare_app_data_decode(uint8_t protocol_flag,uint8_t frame_num,uint8_t frame_sum,uint16_t data_len,uint8_t *buf);
  60. void aicare_app_data_encode(uint8_t protocol_flag,uint8_t frame_num,uint8_t frame_sum,uint16_t data_len,uint8_t *buf);
  61. void aicare_app_ack_encode(uint8_t protocol_flag,uint8_t frame_num,uint8_t frame_sum,uint16_t data_len,uint8_t *buf,uint8_t rsp);
  62. void aicare_app_data_recv(uint8_t *data, uint16_t len)
  63. {
  64. uint16_t src_len = ((data[6]<<8)|data[7]);
  65. uint16_t src_check = ((data[4]<<8)|data[5]);
  66. if((data[0]==0xdd) && ((src_len+8)==len))
  67. {
  68. uint16_t check_val=aicare_sum_check(&data[8],src_len);
  69. //AICARE_LOG("check_val:%x\r\n",check_val);
  70. if(src_check == check_val)
  71. {
  72. aicare_app_data_decode(data[1],data[2],data[3],src_len,&data[8]);
  73. }
  74. }
  75. }
  76. void aicare_app_data_decode(uint8_t protocol_flag,uint8_t frame_num,uint8_t frame_sum,uint16_t data_len,uint8_t *buf)
  77. {
  78. aicare_tlv_t tlv;
  79. tlv.type = (buf[0]<<8)|buf[1];
  80. tlv.status = buf[2];
  81. tlv.length = (buf[3]<<8)|buf[4];
  82. tlv.data = &buf[5];
  83. AICARE_LOG("protocol_flag:%x frame_num:%d frame_sum:%d data_len:%d\r\n",protocol_flag,frame_num,frame_sum,data_len);
  84. switch(tlv.type)
  85. {
  86. case AICARE_HANDSHAKE1_CMD:
  87. if((tlv.length==0x0015) && (protocol_flag==0x01))
  88. {
  89. encrypt(tlv.data,aicare_key); //加密
  90. tlv.length +=1;
  91. buf[3] = tlv.length>>8;
  92. buf[4] = tlv.length;
  93. aicare_app_ack_encode(protocol_flag,frame_num,frame_sum,data_len,&buf[0],0x00);
  94. }
  95. else if((tlv.length==0x0005) && (protocol_flag==0x11)) //app ack
  96. {
  97. //rsp handshake2
  98. }
  99. break;
  100. case AICARE_HANDSHAKE2_CMD:
  101. encrypt(tlv.data,aicare_key); //加密
  102. tlv.length +=1;
  103. buf[3] = tlv.length>>8;
  104. buf[4] = tlv.length;
  105. aicare_app_ack_encode(protocol_flag,frame_num,frame_sum,data_len,&buf[0],0x00);
  106. break;
  107. }
  108. }
  109. void aicare_app_ack_encode(uint8_t protocol_flag,uint8_t frame_num,uint8_t frame_sum,uint16_t data_len,uint8_t *buf,uint8_t rsp)
  110. {
  111. if(buf==NULL)return;
  112. uint16_t dst_len = data_len+8+1;
  113. uint8_t *pack_buf=os_malloc(dst_len);
  114. if(pack_buf==NULL)return;
  115. pack_buf[0]=0xdd;
  116. pack_buf[1]=protocol_flag|0x10;//ack
  117. pack_buf[2]=frame_num;
  118. pack_buf[3]=frame_sum;
  119. data_len+=1;
  120. pack_buf[6]=data_len>>8;
  121. pack_buf[7]=data_len;
  122. memcpy(&pack_buf[8],buf,data_len);
  123. pack_buf[dst_len-1]=rsp;
  124. uint16_t check = aicare_sum_check(&pack_buf[8],data_len);
  125. pack_buf[4]=check>>8;
  126. pack_buf[5]=check;
  127. show_reg(pack_buf,dst_len,1);
  128. aicare_send_notify(pack_buf,dst_len);
  129. os_free(pack_buf);
  130. pack_buf=NULL;
  131. AICARE_LOG("protocol_flag:%x frame_num:%d frame_sum:%d data_len:%d\r\n",protocol_flag,frame_num,frame_sum,data_len);
  132. }
  133. void aicare_app_data_encode(uint8_t protocol_flag,uint8_t frame_num,uint8_t frame_sum,uint16_t data_len,uint8_t *buf)
  134. {
  135. if(buf==NULL)return;
  136. uint16_t dst_len = data_len+8;
  137. uint8_t *pack_buf=os_malloc(dst_len);
  138. if(pack_buf==NULL)return;
  139. pack_buf[0]=0xdd;
  140. pack_buf[1]=protocol_flag;
  141. pack_buf[2]=frame_num;
  142. pack_buf[3]=frame_sum;
  143. pack_buf[6]=data_len>>8;
  144. pack_buf[7]=data_len;
  145. memcpy(&pack_buf[8],buf,data_len);
  146. uint16_t check = aicare_sum_check(&pack_buf[8],data_len);
  147. pack_buf[4]=check>>8;
  148. pack_buf[5]=check;
  149. AICARE_LOG("encode data:\r\n");
  150. show_reg(pack_buf,dst_len,1);
  151. aicare_send_notify(pack_buf,dst_len);
  152. os_free(pack_buf);
  153. pack_buf=NULL;
  154. }