display_ctrl.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * @Description:
  3. * @Version: 2.0
  4. * @Author: Seven
  5. * @Date: 2022-11-22 11:15:13
  6. * @LastEditors: Seven
  7. * @LastEditTime: 2022-12-06 13:47:20
  8. */
  9. #include "display_ctrl.h"
  10. #include "includes.h"
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "macro_common.h"
  14. uint8_t IAP_PROT_VER = IAP_VER1;
  15. uint8_t XSP_ADDRESS = 0x81;
  16. static uint8_t g_xspSn[16] = {0};
  17. static uint8_t encrybuf[256] = {0};
  18. static uint8_t _tempBuff[256] = {0};
  19. static uint8_t _xspSendBuff[256] = {0};
  20. static uint8_t rcvxspDa[256] = {0};
  21. static uint8_t decrybuf[128] = {0};
  22. xsp_comm_info_t xsp_comm_info[1];
  23. txsp_info_t t_xsp_info;
  24. ota_xsp_info_t ota_xsp_info;
  25. uint8_t const display_code[]={ //共阴——标准字库,外部是共阳接法。
  26. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  27. 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
  28. //black10 - H J K L N o P U t G Q r M y
  29. 0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
  30. // 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1 .
  31. 0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46,0x80
  32. };
  33. //密钥组数组,(16+1)*16, KEY0根据显示模块传出的密钥序号,从该数组中提取而来,
  34. //提取方法为:以密钥序号为偏移量,连续提取16字节,作为认证基础密钥KEY0
  35. const uint8_t keyGroup[272]={
  36. 0xE7,0x5F,0x2F,0xE9,0x37,0x94,0x3D,0xA7,0x24,0x2A,0x54,0x2B,0x7A,0xF5,0x4D,0xE4,
  37. 0xAB,0x9C,0xCF,0xC4,0xB0,0x67,0xF4,0xBD,0x9F,0x7D,0x33,0xDA,0xEB,0x81,0x71,0x90,
  38. 0x15,0x17,0xDF,0xD7,0xC9,0xA1,0x57,0x1B,0x48,0xA8,0x62,0x95,0xC5,0x5E,0x00,0x9A,
  39. 0x46,0x8D,0xF1,0x74,0x09,0x51,0x1A,0x7E,0xB6,0x2D,0x0D,0x8E,0x85,0xA3,0xF6,0xBE,
  40. 0xC3,0xA2,0x11,0x4E,0xAD,0xD8,0x39,0xCC,0x96,0x7C,0xE5,0x38,0x72,0xAE,0xB4,0xE3,
  41. 0xE8,0xBB,0x3F,0x58,0xF8,0x16,0x93,0x87,0x3C,0x5C,0xDC,0x31,0x44,0xF0,0x10,0xEE,
  42. 0xB9,0xED,0xD2,0xF9,0x56,0x40,0x01,0xE2,0x8C,0x50,0x3A,0x75,0x7B,0xFA,0xCD,0x52,
  43. 0x14,0x27,0xB3,0x3E,0x88,0xAF,0x21,0x2E,0xE0,0x1D,0xB7,0x23,0xB5,0xFE,0x36,0x4C,
  44. 0x6C,0xDE,0x92,0x25,0xF3,0x70,0x6A,0xD3,0x35,0xFC,0x86,0x83,0xC7,0x77,0x13,0xAA,
  45. 0x06,0x0B,0x6F,0xEA,0xD1,0x66,0x98,0x0E,0x43,0x0F,0x79,0xA6,0x3B,0x30,0xC0,0xC8,
  46. 0x55,0x82,0x9B,0x91,0x68,0x6B,0xFD,0x7F,0xDB,0xE1,0x47,0x26,0x28,0xBF,0x8F,0x60,
  47. 0x5D,0x59,0xFB,0xEC,0x05,0x5B,0x08,0x02,0xC2,0xE6,0xAC,0x45,0x32,0xB2,0x34,0x07,
  48. 0x1F,0xD4,0x6E,0xD6,0x5A,0x64,0x22,0xC1,0xA9,0xB1,0x9E,0xEF,0xBA,0xB8,0x1C,0x9D,
  49. 0x12,0x97,0x29,0xBC,0x49,0x0C,0xDD,0x89,0x69,0x4B,0x73,0x78,0xCE,0xC6,0x18,0xCB,
  50. 0xD9,0xF7,0x2C,0x84,0x61,0x80,0x8A,0x19,0x20,0x42,0xFF,0x03,0x76,0x6D,0x4F,0x41,
  51. 0x8B,0x65,0xCA,0x63,0xD5,0x99,0xA5,0x4A,0x1E,0x04,0xA0,0xA4,0x0A,0x53,0xD0,0xF2,
  52. 0xE7,0x5F,0x2F,0xE9,0x37,0x94,0x3D,0xA7,0x24,0x2A,0x54,0x2B,0x7A,0xF5,0x4D,0xE4
  53. };
  54. const uint8_t XSP_KEY1[16]={0xCB,0x37,0x13,0x37,0xF5,0xFF,0x28,0x12,0xCC,0xEA,0x04,0xE9,0x74,0xBF,0x54,0xB1};
  55. uint8_t SN[8]={'N','S','1','2','3','4','5','6'};//{0x31, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36};//
  56. //KYE1为SN采用母密钥以TDES算法分散而来,调试时,母密钥暂定为:
  57. const uint8_t FatherKey[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
  58. /* 取反函数 */
  59. int reverse_get(uint8_t* buffer, uint8_t* result,uint8_t data_len)
  60. {
  61. int ret = 0;
  62. int i;
  63. for(i = 0; i < data_len; i++)
  64. {
  65. *(result+i) = ~(*(buffer+i));
  66. }
  67. return ret;
  68. }
  69. /* 比较两个字符串 */
  70. static int _my_strcmp(uint8_t *buff1, uint8_t *buff2, uint8_t len)
  71. {
  72. int i, ret = 1;
  73. for(i = 0; i < len; i++)
  74. {
  75. if(buff1[i] != buff2[i])
  76. {
  77. return ret;
  78. }
  79. }
  80. ret = 0;
  81. return ret;
  82. }
  83. /* 密钥计算 用R1^R2 得到R值 */
  84. int key_com(uint8_t *input1,uint8_t *input2,uint8_t *output,uint8_t len)
  85. {
  86. int ret = 0;
  87. int i;
  88. for(i = 0; i < len; i++)
  89. {
  90. output[i] = input1[i]^input2[i];
  91. }
  92. return ret;
  93. }
  94. /* 根据R值 得到密密钥 KEY */
  95. //int key_get(uint8_t *rVal, uint8_t *)
  96. /* 根据SN得到一串16个字节的数据 */
  97. void get_sn_key(uint8_t *sn, uint8_t *outSn)
  98. {
  99. int i = 0;
  100. for(i=0;i<8;i++){
  101. outSn[i] = sn[i];
  102. outSn[i+8] = ~sn[i];
  103. }
  104. }
  105. /* 双向认证接收参数 */
  106. int issue_rcv_comm(void *puser, uint8_t *para, uint8_t size,void *key)
  107. {
  108. int ret;
  109. uint16_t crc;
  110. uint16_t* p_crc;
  111. // uint8_t temp_buff[64];
  112. xsp_comm_info_t *p_xsp = (xsp_comm_info_t *)puser;
  113. memset(_tempBuff,0,sizeof(_tempBuff));
  114. crc = CRC16_get(para, size-2);
  115. p_crc = (uint16_t *)&para[size-2];
  116. *p_crc = htons(*p_crc);
  117. if(crc == *p_crc)
  118. {
  119. // if(para[0] != XSP_RESULT_SUCCESS){
  120. // printf("认证结果错误 errorcode = %d\r\n",para[0]);
  121. // ret = para[0];
  122. // return ret;
  123. // }
  124. if(para[0] == 1) // 认证第一步返回结果
  125. {
  126. printf("rcv step1\n");
  127. reverse_get(para+1, _tempBuff, 16);//将R1取反得/R1
  128. ret = _my_strcmp(p_xsp->issue_key.Authent_R1,_tempBuff,16);//比较接收的/R1和发送的R1是否相同
  129. if(ret)return ret; //接收数据错误
  130. memcpy(p_xsp->issue_key.Authent_R2,&(para[17]),16); // 保存第二步的认证结果
  131. p_xsp->issue_key.Authen_steps = 0x02; //认证步骤置为2
  132. ret = XSP_RESULT_SUCCESS;
  133. }
  134. else if(para[0] == 2){ //认证第二步返回结果
  135. printf("rcv step2\n");
  136. p_xsp->issue_key.Authen_steps = 0x03; // 认证成功
  137. key_com(p_xsp->issue_key.Authent_R1,p_xsp->issue_key.Authent_R2,p_xsp->issue_key.key,16);//密钥计算
  138. memset(g_xspSn,0,sizeof(g_xspSn));
  139. memset(encrybuf,0,sizeof(encrybuf));
  140. data_dump("sn",SN,8);
  141. get_sn_key((uint8_t*)SN,g_xspSn);
  142. DesEncryptFunc(g_xspSn,encrybuf,(uint8_t*)FatherKey,16,16);//用母密钥和SN加密, 得到KEY1
  143. data_dump("get key1:",encrybuf,16);
  144. data_dump("get R:",p_xsp->issue_key.key,16);
  145. DesEncryptFunc(p_xsp->issue_key.key,key,encrybuf,16,16); // 用KEY1和R加密,得到KEY
  146. data_dump("get key:",key,16);
  147. ret = XSP_RESULT_SUCCESS;
  148. }
  149. else
  150. {
  151. ret = XSP_RESULT_RCVERR;
  152. }
  153. }
  154. else
  155. {
  156. ret = XSP_RESULT_PCRCRERROR;// para[0];
  157. }
  158. // printf("rcv c1 ret1 = %02x\n",ret);
  159. return ret;
  160. }
  161. /* 双向认证下发参数Issue parameters */
  162. int _Issue_para(uint8_t step,uint8_t *para, void *puser)
  163. {
  164. int i;
  165. int ret = 0;
  166. uint16_t crc;
  167. // uint8_t temp_buff[40];
  168. //issue_info_t *temp = (issue_info_t *)puser;
  169. xsp_comm_info_t *p_xsp = (xsp_comm_info_t *)puser;
  170. memset(para, 0, 40);
  171. memset(_tempBuff,0,sizeof(_tempBuff));
  172. if(step == 1)
  173. {
  174. printf("send step1\n");
  175. p_xsp->issue_key.Authen_steps = 0x01;
  176. for(i = 0; i < 16; i++)
  177. {
  178. p_xsp->issue_key.Authent_R1[i] = rand()%0xff; //l6byte随机数(R1)
  179. }
  180. *para = 0x01;
  181. *(para+1) = 0x01; //认证步骤
  182. memcpy(para+2,p_xsp->issue_key.Authent_R1,16); //前16字节为R1,后16字节为0
  183. crc = CRC16_get(para,38);
  184. para[38] = (crc>>8)&0x00ff;
  185. para[39] = crc&0x00ff;
  186. }
  187. else if(step == 2)
  188. {
  189. printf("send step2\n");
  190. reverse_get(p_xsp->issue_key.Authent_R2, _tempBuff, 16); //将接收的R2取反
  191. *para = 0x01;
  192. *(para+1) = 0x02; //认证步骤
  193. memcpy(para+18,_tempBuff,16);//前16字节为0,后16字节为取反的R2
  194. crc = CRC16_get(para,38);
  195. //crc = htons(crc);
  196. para[38] = (crc>>8)&0x00ff;
  197. para[39] = crc&0x00ff;
  198. }
  199. return ret;
  200. }
  201. /*---------------------------------------------------------------------------
  202. * 随机数与参数的异或
  203. * _buff: 参数
  204. * _buffLen : 参数的长度
  205. * _rnd: 随机数
  206. * _rndLen: 随机数的长度
  207. * --------------------------------------------------------------------------*/
  208. static void randomOperate(uint8 * _buff,uint32 _buffLen,uint8 * _rnd,uint32 _rndLen)
  209. {
  210. uint32 i = 0;
  211. uint32 tempIndex = 0;
  212. // return;
  213. tempIndex=0;
  214. for(i=0;i<_buffLen;i++)
  215. {
  216. _buff[i]^=_rnd[tempIndex];
  217. tempIndex++;
  218. if(tempIndex>=_rndLen)
  219. tempIndex=0;
  220. }
  221. }
  222. /*---------------------------------------------------------------------------
  223. * 指令 C0发送
  224. * 下发的结构:帧头(1)+帧长度(1)+帧号(1)+SN码(4)+指令码(1)+参数[n]+CRC校验码[2]
  225. *---------------------------------------------------------------------------*/
  226. int xsp_0xC0_send(uint32_t sn)
  227. {
  228. // uint8_t send_buf[64];
  229. uint8_t send_size, *fparm;
  230. uint16_t *p_crc;
  231. txspCommFrame_t *p_xsp_tx = (txspCommFrame_t *)_xspSendBuff;
  232. memset(_xspSendBuff, 0, sizeof(_xspSendBuff));
  233. p_crc = (uint16_t *)&_xspSendBuff[sizeof(txspCommFrame_t) + 2];
  234. p_xsp_tx->fhead = 0xbb;
  235. p_xsp_tx->flen = XSP_CMD_HEADLEN+2; //长度码为命令码、帧号、参数和校验码的字节数之和
  236. p_xsp_tx->findex = 0x81; //帧号标识本帧的特征信息,帧号0x81表示单帧命令;
  237. p_xsp_tx->faddress = sn;
  238. p_xsp_tx->fcmd = 0xC0;
  239. fparm = &_xspSendBuff[sizeof(txspCommFrame_t)];
  240. *(fparm+1) = 0x00;
  241. *(fparm+2) = 0x00;
  242. *p_crc = CRC16_get(&p_xsp_tx->fhead, p_xsp_tx->flen);//校验码为帧号、命令码和参数逐字节的逻辑和
  243. *p_crc = htons(*p_crc);
  244. send_size = p_xsp_tx->flen + 2;
  245. _ytsf_data_code(_xspSendBuff, &send_size); /* 编码发送 */
  246. data_dump("xsp send", (uint8_t *)p_xsp_tx, send_size);
  247. // memcpy(puser,p_xsp_tx,send_size);
  248. RS485_TX();
  249. uart_msg_send(UART3_ID,(char *)_xspSendBuff,send_size);
  250. delay_us(100);
  251. RS485_RX();
  252. msg_para.Uart = UART3_ID;
  253. timeout_setValue(&ota_xsp_info.tt_uart,OTA_XSP_UART_TIMEOUT,1);
  254. return send_size;
  255. }
  256. /*----------------------------------------------------------------------
  257. * 双向认证,发送认证
  258. * 帧头(1)+帧长度(1)+帧号(1)+SN码(4)+指令码(1)+参数[n]+CRC校验码[2]
  259. *
  260. *----------------------------------------------------------------------*/
  261. int xsp_0xC1_send(uint32_t sn,uint8_t step,void *puser, uint8_t key_num)
  262. {
  263. // uint8_t send_buf[64];
  264. uint8_t send_size;//, *fparm;
  265. uint16_t *p_crc;
  266. uint8_t para_buf[64];//,encrybuf[64];
  267. txspCommFrame_t *p_xsp_tx = (txspCommFrame_t *)_xspSendBuff;
  268. xsp_comm_info_t *p_xsp = (xsp_comm_info_t *)puser;
  269. memset(_xspSendBuff, 0, sizeof(_xspSendBuff));
  270. // p_crc = (uint16_t *)&send_buf[sizeof(txspCommFrame_t) + size];
  271. p_xsp_tx->fhead = 0xbb;
  272. p_xsp_tx->flen = XSP_CMD_HEADLEN+40; //长度码为命令码、帧号、参数和校验码的字节数之和
  273. p_xsp_tx->findex = 0x81; //帧号标识本帧的特征信息,帧号0x81表示单帧命令;
  274. p_xsp_tx->faddress = sn;
  275. p_xsp_tx->fcmd = 0xC1;
  276. // fparm = &_xspSendBuff[sizeof(txspCommFrame_t)];
  277. _Issue_para(step,para_buf,p_xsp);//设置认证参数部分
  278. /*参数部分处理:先将参数部分加密,然后计算CRC校验,最后整体进行转义编码*/
  279. send_size = XSP_CMD_HEADLEN;
  280. // printf("\r\n key = %x\r\n",key_num);
  281. data_dump("send keyGroup",(uint8_t*)&keyGroup[key_num],16);
  282. data_dump("send not desencrpt",para_buf,40);
  283. DesEncryptFunc(para_buf,encrybuf,(uint8_t*)&keyGroup[key_num],40,16);//参数加密
  284. memcpy(_xspSendBuff+send_size,encrybuf,40);//将加密后的参数拼接
  285. send_size+=40;
  286. p_crc = (uint16_t *)&_xspSendBuff[sizeof(txspCommFrame_t)+40];
  287. *p_crc = CRC16_get(_xspSendBuff, send_size);//
  288. *p_crc = htons(*p_crc);
  289. send_size += 2;
  290. _ytsf_data_code(_xspSendBuff, &send_size); /*编码发送 */
  291. // memcpy(sendDa,(char*)_xspSendBuff,send_size);
  292. //data_dump("xsp send", (uint8_t *)send_buf, send_size);
  293. RS485_TX();
  294. uart_msg_send(UART3_ID,(char *)_xspSendBuff,send_size);
  295. delay_us(100);
  296. RS485_RX();
  297. msg_para.Uart = UART3_ID;
  298. timeout_setValue(&ota_xsp_info.tt_uart,OTA_XSP_UART_TIMEOUT,1);
  299. return send_size;
  300. }
  301. /*----------------------------------------------------------------------------------------
  302. * 加密命令发送
  303. * 指令格式:帧头(1)+帧长度(1)+帧号(1)+SN码(4)+指令码(1)+参数[n]+CRC校验码[2]
  304. * ----------------------------------------------------------------------------------------*/
  305. int xsp_send_comm(uint8_t *key, uint8_t cmd, uint32_t sn, uint8_t *data, uint8_t size)
  306. {
  307. uint8_t send_size;//, *fparm;
  308. uint16_t *p_crc;
  309. txspCommFrame_t *p_xsp_tx = (txspCommFrame_t *)_xspSendBuff;
  310. memset(_xspSendBuff, 0, sizeof(_xspSendBuff));
  311. p_crc = (uint16_t *)&_xspSendBuff[sizeof(txspCommFrame_t) + size];
  312. p_xsp_tx->fhead = 0xbb;
  313. p_xsp_tx->flen = XSP_CMD_HEADLEN+size; //长度码为命令码、帧号、参数和校验码的字节数之和
  314. p_xsp_tx->findex = 0x81; //帧号标识本帧的特征信息,帧号Ox81H表示单帧命令;
  315. p_xsp_tx->faddress = sn;
  316. p_xsp_tx->fcmd = cmd;
  317. // fparm = &_xspSendBuff[sizeof(txspCommFrame_t)];
  318. /*
  319. 参数部分处理:先将参数部分加密,然后计算CRC校验,最后整体进行转义编码
  320. */
  321. send_size = XSP_CMD_HEADLEN;
  322. // 以下三行测试代码
  323. // data_dump("send key :",key,16);
  324. // memcpy(_xspSendBuff+send_size,data,size);
  325. // data_dump("send data :",_xspSendBuff,size+send_size);
  326. // printf("1\n");
  327. DesEncryptFunc(data,encrybuf,key,size,16);//参数加密
  328. // printf("2\n");
  329. memcpy(_xspSendBuff+send_size,encrybuf,size);//将加密后的参数拼接
  330. // printf("3\n");
  331. send_size+=size;
  332. *p_crc = CRC16_get(_xspSendBuff, send_size);//校验码
  333. *p_crc = htons(*p_crc);
  334. send_size+=2;
  335. // printf("4\n");
  336. _ytsf_data_code(_xspSendBuff, &send_size); /*编码发送 */
  337. // printf("5\n");
  338. // data_dump("xsp send",_xspSendBuff,send_size);
  339. RS485_TX();
  340. uart_msg_send(UART3_ID,(char *)_xspSendBuff,send_size);
  341. delay_us(100);
  342. RS485_RX();
  343. msg_para.Uart = UART3_ID;
  344. timeout_setValue(&ota_xsp_info.tt_uart,OTA_XSP_UART_TIMEOUT,1);
  345. // memcpy(puser,_xspSendBuff,send_size);
  346. return send_size;
  347. }
  348. /* 读取数据(0xC3)命令下发 */
  349. int xsp_0xC3_send(uint32_t sn, uint8_t *key)
  350. {
  351. int i;//,j;
  352. uint16_t crc;
  353. uint8_t send_size = 0;
  354. memset(_tempBuff,0,sizeof(_tempBuff));
  355. //读取随机数
  356. for(i = 0; i < 4; i++)
  357. {
  358. _tempBuff[2+i] = rand()%0xFF;
  359. }
  360. crc = CRC16_get(_tempBuff,6);
  361. _tempBuff[6] = (crc>>8)&0x00ff;
  362. _tempBuff[7] = crc&0x00ff;
  363. randomOperate(_tempBuff,2,&(_tempBuff[2]),4);/* 操作随机数循环异或 */
  364. send_size = xsp_send_comm(key,0xC3,sn,_tempBuff,8);
  365. return send_size;
  366. }
  367. ///////////////////////////////////iap指令发送////////////////////////
  368. //取sn的十进制ASCII码后8字节
  369. void get_sn_dec_ascii(uint32_t sn, uint8_t *data)
  370. {
  371. uint32_t t_sn = sn;
  372. uint8_t temp[10]={'0','0','0','0','0','0','0','0','0','0'};
  373. uint8_t t_data = 0;
  374. uint8_t i;
  375. for(i = 0; i < 8; i++)
  376. {
  377. if(t_sn == 0)
  378. break;
  379. t_data = t_sn%10;
  380. temp[7-i] = t_data+'0';
  381. t_sn /= 10;
  382. }
  383. memcpy(data, temp, 8);
  384. // for(i = 0; i < 10; i++)
  385. // {
  386. // if(t_sn == 0)
  387. // break;
  388. // t_data = t_sn%10;
  389. // temp[i] = t_data+'0';
  390. // t_sn /= 10;
  391. // }
  392. // for(i = 0; i < 8; i++)
  393. // {
  394. // data[i] = temp[7-i];
  395. // }
  396. }
  397. //计算iap密钥
  398. void get_iap_key(uint32_t sn, uint8_t *key)
  399. {
  400. uint8_t key_0[8] = {0};
  401. uint8_t key_1[16] = {0};
  402. get_sn_dec_ascii(sn,key_0);
  403. data_dump("xsp sn",key_0,8);
  404. memcpy(key_1,keyGroup,16);
  405. randomOperate(key_1,16,key_0,8);
  406. memcpy(key,key_1,16);
  407. data_dump("xsp key iap",key_1,16);
  408. }
  409. /*---------------------------------------------------------------------
  410. *Hub指令发送
  411. *subIns[1byte]+ paraLen[1byte] + para[n bytes] + rnd[4bytes] + pcrc[2bytes]
  412. *
  413. -----------------------------------------------------------------------*/
  414. int xsp_hub_send(uint32_t sn, uint8_t cmd, uint8_t *key, uint8_t *data, uint8_t datalen)
  415. {
  416. uint8_t i = 0;
  417. uint16_t crc,send_size = 0;
  418. memset(_tempBuff,0,sizeof(_tempBuff));
  419. _tempBuff[0] = cmd;
  420. _tempBuff[1] = datalen;
  421. memcpy(_tempBuff+2,data,datalen);
  422. send_size = datalen + 2;
  423. for(i = 0; i < 4; i++)//随机数
  424. {
  425. _tempBuff[send_size+i] = rand()%0xff;
  426. }
  427. crc = CRC16_get(_tempBuff,send_size+4);
  428. _tempBuff[send_size+4] = (crc>>8)&0x00ff;
  429. _tempBuff[send_size+5] = crc&0x00ff;
  430. // data_dump("hub",_tempBuff,send_size+6);
  431. randomOperate(_tempBuff,send_size,&(_tempBuff[send_size]),4);/* 操作随机数循环异或 */
  432. send_size += 6;
  433. // data_dump("hub rand",_tempBuff,send_size);
  434. send_size = xsp_send_comm(key,0xCE,sn,_tempBuff,send_size);
  435. return send_size;
  436. }
  437. /*---------------------------------------------------------------------
  438. *获取当前IAP相关信息(0x01)
  439. *iapIns[1]+reserved[4] + rnd[1] + crc16[2]
  440. *
  441. -----------------------------------------------------------------------*/
  442. int xsp_iap_0x01_send(uint32_t sn, uint8_t *key_iap, uint8_t *key_hub)
  443. {
  444. uint16_t crc,send_size = 0;
  445. memset(_tempBuff,0,sizeof(_tempBuff));
  446. _tempBuff[0] = 0x01;
  447. _tempBuff[5] = rand()%0xff;//随机数
  448. crc = CRC16_get(_tempBuff,6);
  449. _tempBuff[6] = (crc>>8)&0x00ff;
  450. _tempBuff[7] = crc&0x00ff;
  451. data_dump("iap 01",_tempBuff,8);
  452. // randomOperate(_tempBuff,5,&(_tempBuff[5]),1);/* 操作随机数循环异或 */
  453. // data_dump("iap 01 rand",_tempBuff,8);
  454. DesEncryptFunc(_tempBuff,encrybuf,key_iap,8,16);//参数加密
  455. data_dump("iap 01 encrypt",encrybuf,8);
  456. send_size = 8;
  457. send_size = xsp_hub_send(sn,0x10,key_hub,encrybuf,send_size);
  458. return send_size;
  459. }
  460. /*---------------------------------------------------------------------
  461. *擦除指令(0x02)
  462. *iapIns[1] + startAddr[4] + totalPages[4] + reserved[4] + rnd[1] + crc16[2]
  463. *
  464. -----------------------------------------------------------------------*/
  465. int xsp_iap_0x02_send(uint32_t sn, uint8_t *key_iap, uint8_t *key_hub, uint32_t startAddr, uint32_t totalPages)
  466. {
  467. uint16_t crc,send_size = 0;
  468. memset(_tempBuff,0,sizeof(_tempBuff));
  469. _tempBuff[0] = 0x02;
  470. memcpy(_tempBuff+1,(uint8_t *)&startAddr,4);
  471. memcpy(_tempBuff+5,(uint8_t *)&totalPages,4);
  472. send_size = 13;
  473. _tempBuff[send_size] = rand()%0xff;//随机数
  474. crc = CRC16_get(_tempBuff,send_size+1);
  475. _tempBuff[send_size+1] = (crc>>8)&0x00ff;
  476. _tempBuff[send_size+2] = crc&0x00ff;
  477. data_dump("iap 02 send",_tempBuff,send_size+3);
  478. // randomOperate(_tempBuff,send_size,&(_tempBuff[send_size]),1);/* 操作随机数循环异或 */
  479. send_size += 3;
  480. DesEncryptFunc(_tempBuff,encrybuf,key_iap,send_size,16);//参数加密
  481. send_size = xsp_hub_send(sn,0x02,key_hub,encrybuf,send_size);
  482. return send_size;
  483. }
  484. /*---------------------------------------------------------------------
  485. *编程指令(0x03)
  486. *iapIns[1] + u32Len[1] + startAddr[4] + prgData[n] + reserved[m] + rnd[1] + crc16[2]
  487. *
  488. -----------------------------------------------------------------------*/
  489. int xsp_iap_0x03_send(uint32_t sn, uint8_t *key_iap, uint8_t *key_hub, uint8_t datalen, uint32_t startAddr)
  490. {
  491. uint32_t iap_startaddr = 0;
  492. uint32_t data_len = datalen;
  493. uint16_t crc,send_size = 0;
  494. uint8_t reserve_size = 0;
  495. uint8_t temp[OTA_XSP_PACK_LEN] = {0};
  496. memset(_tempBuff,0,sizeof(_tempBuff));
  497. printf("send pkgs<%d/%d>\n",ota_xsp_pack_info.total_pkgs,ota_xsp_info.iap_0x03_num+1);
  498. if(ota_xsp_info.iap_0x03_num == ota_xsp_pack_info.total_pkgs-1)//最后一包计算实际长度
  499. {
  500. data_len = (ota_xsp_pack_info.total_bytes-(ota_xsp_info.iap_0x03_num*OTA_XSP_PACK_LEN))/4;
  501. }
  502. Flash_BufferRead(ota_xsp_pack_info.app3_startaddr+ota_xsp_info.iap_0x03_num*OTA_XSP_PACK_LEN,temp,data_len*4);
  503. iap_startaddr = ota_xsp_pack_info.startaddr + (ota_xsp_info.iap_0x03_num*OTA_XSP_PACK_LEN);
  504. _tempBuff[0] = 0x03;
  505. _tempBuff[1] = data_len;
  506. memcpy(_tempBuff+2,(uint8_t *)&iap_startaddr,4);
  507. memcpy(_tempBuff+6,temp,data_len*4);
  508. send_size = data_len*4+6;
  509. reserve_size = ((send_size+3)%8==0) ? 0 : (8-((send_size+3)%8));
  510. send_size += reserve_size;
  511. _tempBuff[send_size] = rand()%0xff;//随机数
  512. crc = CRC16_get(_tempBuff,send_size+1);
  513. _tempBuff[send_size+1] = (crc>>8)&0x00ff;
  514. _tempBuff[send_size+2] = crc&0x00ff;
  515. data_dump("iap 03 send",_tempBuff,send_size+3);
  516. // randomOperate(_tempBuff,send_size,&(_tempBuff[send_size]),1);/* 操作随机数循环异或 */
  517. send_size += 3;
  518. DesEncryptFunc(_tempBuff,encrybuf,key_iap,send_size,16);//参数加密
  519. send_size = xsp_hub_send(sn,0x03,key_hub,encrybuf,send_size);
  520. return send_size;
  521. }
  522. /*---------------------------------------------------------------------
  523. *校验指令(0x04)
  524. *iapIns[1] + startAddr[4] + byteLen[4] + crcVeri[2] + reserved[2] + rnd[1] + crc16[2]
  525. *
  526. -----------------------------------------------------------------------*/
  527. int xsp_iap_0x04_send(uint32_t sn, uint8_t *key_iap, uint8_t *key_hub, uint32_t startAddr, uint32_t byteLen, uint16_t crcVer)
  528. {
  529. uint16_t crc,send_size = 0;
  530. memset(_tempBuff,0,sizeof(_tempBuff));
  531. _tempBuff[0] = 0x04;
  532. memcpy(_tempBuff+1,(uint8_t *)&startAddr,4);
  533. memcpy(_tempBuff+5,(uint8_t *)&byteLen,4);
  534. crcVer = htons(crcVer);
  535. memcpy(_tempBuff+9,(uint8_t *)&crcVer,2);
  536. send_size = 13;
  537. _tempBuff[send_size] = rand()%0xff;//随机数
  538. crc = CRC16_get(_tempBuff,send_size+1);
  539. _tempBuff[send_size+1] = (crc>>8)&0x00ff;
  540. _tempBuff[send_size+2] = crc&0x00ff;
  541. data_dump("iap 04 send",_tempBuff,send_size+3);
  542. // randomOperate(_tempBuff,send_size,&(_tempBuff[send_size]),1);/* 操作随机数循环异或 */
  543. send_size += 3;
  544. DesEncryptFunc(_tempBuff,encrybuf,key_iap,send_size,16);//参数加密
  545. send_size = xsp_hub_send(sn,0x04,key_hub,encrybuf,send_size);
  546. return send_size;
  547. }
  548. /*---------------------------------------------------------------------
  549. *结束指令(0x07)
  550. *iapIns[1] + appBase[4] + iapUnitime[4] + reserved[4] + rnd[1] + crc16[2]
  551. *
  552. -----------------------------------------------------------------------*/
  553. int xsp_iap_0x07_send(uint32_t sn, uint8_t *key_iap, uint8_t *key_hub, uint32_t appBase, uint32_t iapUnitime)
  554. {
  555. uint16_t crc,send_size = 0;
  556. memset(_tempBuff,0,sizeof(_tempBuff));
  557. _tempBuff[0] = 0x07;
  558. memcpy(_tempBuff+1,(uint8_t *)&appBase,4);
  559. memcpy(_tempBuff+5,(uint8_t *)&iapUnitime,4);
  560. send_size = 13;
  561. _tempBuff[send_size] = rand()%0xff;//随机数
  562. crc = CRC16_get(_tempBuff,send_size+1);
  563. _tempBuff[send_size+1] = (crc>>8)&0x00ff;
  564. _tempBuff[send_size+2] = crc&0x00ff;
  565. data_dump("iap 07 send",_tempBuff,send_size+3);
  566. // randomOperate(_tempBuff,send_size,&(_tempBuff[send_size]),1);/* 操作随机数循环异或 */
  567. send_size += 3;
  568. DesEncryptFunc(_tempBuff,encrybuf,key_iap,send_size,16);//参数加密
  569. send_size = xsp_hub_send(sn,0x07,key_hub,encrybuf,send_size);
  570. return send_size;
  571. }
  572. /*---------------------------------------------------------------------
  573. *复位指令(0x0F)
  574. *iapIns[1] + reserved[4] + rnd[1] + crc16[2]
  575. *
  576. -----------------------------------------------------------------------*/
  577. int xsp_iap_0x0F_send(uint32_t sn, uint8_t *key_iap, uint8_t *key_hub)
  578. {
  579. uint16_t crc,send_size = 0;
  580. memset(_tempBuff,0,sizeof(_tempBuff));
  581. _tempBuff[0] = 0x0F;
  582. _tempBuff[5] = rand()%0xff;//随机数
  583. crc = CRC16_get(_tempBuff,6);
  584. _tempBuff[6] = (crc>>8)&0x00ff;
  585. _tempBuff[7] = crc&0x00ff;
  586. data_dump("iap 0F send",_tempBuff,8);
  587. // randomOperate(_tempBuff,5,&(_tempBuff[5]),1);/* 操作随机数循环异或 */
  588. DesEncryptFunc(_tempBuff,encrybuf,key_iap,8,16);//参数加密
  589. send_size = 8;
  590. send_size = xsp_hub_send(sn,0x0F,key_hub,encrybuf,send_size);
  591. return send_size;
  592. }
  593. /////////////////////////////////////////////////////接收处理////////////////////////////////////////////////////////////
  594. /*--------------------------------------------------------------------
  595. * 获取信息(0xC0)接收
  596. * 读取结果 (1);密钥序号 (1);硬件版本 (2);
  597. * 软件版本 (4);编译信息 (12); 屏幕SN(8)
  598. *
  599. *
  600. * --------------------------------------------------------------------*/
  601. int xsp_0xC0_rcv(void *puser, uint8_t *data, uint8_t data_len)
  602. {
  603. //uint8_t tax_data = *(data);
  604. int ret = XSP_RESULT_SUCCESS;
  605. uint32_t sn = 0;
  606. xsp_comm_info_t *p_xsp = (xsp_comm_info_t *)puser;
  607. p_xsp->xsp_info.KeySequence_number = *(data);
  608. memcpy(p_xsp->xsp_info.Hardware_version, data + 1, 2);
  609. memcpy(p_xsp->xsp_info.Software_version, data + 3, 4);
  610. memcpy(p_xsp->xsp_info.Compile_info, data + 7, 12);
  611. memcpy(p_xsp->xsp_info.protocolVer,data+27,2);//协议版本
  612. memcpy(p_xsp->xsp_info.devType,data+34,2);//设备类型
  613. data[27] = 0;
  614. memcpy(p_xsp->xsp_info.xsp_sn,data+19,8);
  615. sn = HextoDec((char*)(data+19));
  616. sprintf((char*)SN,"%08u",sn);
  617. // memcpy((char*)SN,data+19,8);
  618. ret = XSP_RESULT_SUCCESS;
  619. return ret;
  620. }
  621. /*--------------------------------------------------------------------
  622. * 解析认证(0xC1)接收
  623. * 认证结果 (1);认证步骤 (1);认证参数 (32);
  624. * 预留参数 (4); PCRC(2)
  625. *
  626. *
  627. * --------------------------------------------------------------------*/
  628. int xsp_0xC1_rcv(void *puser, uint8_t *data, uint8_t data_len)
  629. {
  630. // uint8_t Decrypt[64] = {0};
  631. int ret = 0;
  632. memset(encrybuf,0,sizeof(encrybuf));
  633. DesDecryptFunc(data,encrybuf,(uint8_t*)&keyGroup[ota_xsp_info.txsp_info.curkey],40,16); //解密
  634. data_dump("DesDecryptFunc", (uint8_t *)encrybuf, 40);
  635. ret = issue_rcv_comm(xsp_comm_info,encrybuf,40,puser);
  636. // printf("rcv c1 ret2 = %02x\n",ret);
  637. return ret;
  638. }
  639. /*--------------------------------------------------------------------
  640. * 信息指令(0x01)接收
  641. * ispIns[1] + iapRslt[1] + iapTimes[1] + iapStatus[1] + iapVer[4] + curAppBase[4] +
  642. * chipUUID[12] + rnd1[1] + reserved[4] + rnd[1] + crc16[2]
  643. *
  644. * --------------------------------------------------------------------*/
  645. int xsp_iap_0x01_rcv(void *puser, uint8_t *data, uint8_t data_len)
  646. {
  647. int ret = XSP_IAP_RESULT_SUCCESS;
  648. xsp_iap_0x01_info_t *p_iap_rx = (xsp_iap_0x01_info_t *)data;
  649. if(p_iap_rx->iapRslt != XSP_IAP_RESULT_SUCCESS)//执行结果错误
  650. {
  651. ret = p_iap_rx->iapRslt;
  652. return ret;
  653. }
  654. printf("iaptimes:%u,iapver:0x%08x,curappbase:0x%08x\n",p_iap_rx->iapTimes,p_iap_rx->iapVer,p_iap_rx->curAppBase);
  655. memcpy(puser,data,data_len-8);
  656. return ret;
  657. }
  658. /*--------------------------------------------------------------------
  659. * 擦除指令(0x02)接收
  660. * iapIns[1] + iapRslt[1]+ reserved[3]+ rnd[1] + crc16[2]
  661. *
  662. * --------------------------------------------------------------------*/
  663. int xsp_iap_0x02_rcv(uint8_t *data, uint8_t data_len)
  664. {
  665. int ret;
  666. ret = data[1];
  667. return ret;
  668. }
  669. /*--------------------------------------------------------------------
  670. * hub指令(0xCE)接收
  671. * subIns[1byte]+ paraLen[1byte] + para[n bytes] + rnd[4bytes] + pcrc[2bytes]
  672. *
  673. * --------------------------------------------------------------------*/
  674. int xsp_hub_rcv(uint8_t *key, uint8_t *data, uint16_t len)
  675. {
  676. int ret = XSP_RESULT_RCVERR;
  677. uint16_t *p_crc, crc;
  678. uint8_t size = 0;
  679. uint8_t index = 0;
  680. // uint8_t randDa[1] = {0};
  681. crc = CRC16_get(data, len-2);//校验码
  682. if(IAP_PROT_VER == IAP_VER1)
  683. {
  684. //老版
  685. memset(decrybuf,0,sizeof(decrybuf));
  686. memcpy(decrybuf,data,len-2);
  687. crc = CRC16_get(decrybuf, len);//校验码
  688. }
  689. // crc = CRC16_get(data, len-2);//校验码
  690. p_crc = (uint16_t *)&data[len-2];
  691. *p_crc = htons(*p_crc);
  692. if(crc != *p_crc)
  693. {
  694. printf("crc error\n");
  695. ret = XSP_RESULT_RCVERR;
  696. return ret;
  697. }
  698. index = 1;
  699. if(IAP_PROT_VER == IAP_VER1)
  700. {
  701. //老版
  702. if(data[0] != XSP_RESULT_SUCCESS)
  703. {
  704. printf("result error\n");
  705. ret = XSP_RESULT_RCVERR;
  706. return ret;
  707. }
  708. index = 2;
  709. }
  710. //iap指令参数部分
  711. memset(rcvxspDa,0,sizeof(rcvxspDa));
  712. size = data[index];
  713. DesDecryptFunc(data+index+1,rcvxspDa,key,size,16); //解密
  714. // memcpy(randDa,rcvxspDa+size-3,1);
  715. // randomOperate(rcvxspDa,size-3,randDa,1);
  716. //iap指令参数部分
  717. crc = CRC16_get(rcvxspDa, size-2);//校验码
  718. p_crc = (uint16_t *)&rcvxspDa[size-2];
  719. *p_crc = htons(*p_crc);
  720. if(crc != *p_crc)
  721. {
  722. ret = XSP_RESULT_RCVERR;
  723. return ret;
  724. }
  725. // switch(data[1])
  726. // {
  727. // case 0x10://iap指令
  728. switch(rcvxspDa[0])
  729. {
  730. case 0x01://信息指令
  731. printf("iap cmd = %02x\n",1);
  732. ret = xsp_iap_0x01_rcv(&ota_xsp_info.iap_0x01_info,rcvxspDa,size);
  733. printf("ret = %02x\n",ret);
  734. if(ret == XSP_IAP_RESULT_SUCCESS)
  735. {
  736. printf("iap 01 success\n");
  737. rcv_iap_0x01_handle(&ota_xsp_info.iap_0x01_info);
  738. printf("startaddr = 0x%08x\n",ota_xsp_pack_info.startaddr);
  739. // device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  740. ota_xsp_info.xsp_ota_step++;
  741. }
  742. break;
  743. case 0x02://擦除指令
  744. ret = rcvxspDa[1];
  745. printf("iap cmd = %02x,ret = %02x\n",2,ret);
  746. if(ret == XSP_IAP_RESULT_SUCCESS)
  747. {
  748. ota_xsp_info.iap_0x03_num = 0;
  749. ota_xsp_info.xsp_ota_step++;
  750. }
  751. break;
  752. case 0x03://编程指令
  753. ret = rcvxspDa[1];
  754. printf("iap cmd = %02x,ret = %02x\n",3,ret);
  755. if(ret == XSP_IAP_RESULT_SUCCESS)
  756. {
  757. ota_xsp_info.iap_0x03_num++;
  758. printf("03 num=%d,total=%d\n",ota_xsp_info.iap_0x03_num,ota_xsp_pack_info.total_pkgs);
  759. // ota_xsp_info.xsp_ota_step++;
  760. // ota_xsp_info.iap_0x04_num = 0;
  761. if(ota_xsp_info.iap_0x03_num == ota_xsp_pack_info.total_pkgs)
  762. {
  763. ota_xsp_info.xsp_ota_step++;
  764. ota_xsp_info.iap_0x03_num = 0;
  765. ota_xsp_info.iap_0x04_num = 0;
  766. }
  767. }
  768. break;
  769. case 0x04://校验指令
  770. ret = rcvxspDa[1];
  771. printf("iap cmd = %02x,ret = %02x\n",4,ret);
  772. if(ret == XSP_IAP_RESULT_SUCCESS)
  773. {
  774. // ota_xsp_info.iap_0x04_num++;
  775. // if(ota_xsp_info.iap_0x04_num == 4)
  776. // {
  777. // ota_xsp_info.xsp_ota_step++;
  778. // ota_xsp_info.iap_0x04_num = 0;
  779. // device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  780. // }
  781. ota_xsp_info.xsp_ota_step++;
  782. // device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  783. }
  784. break;
  785. case 0x07://结束指令
  786. ret = rcvxspDa[1];
  787. printf("iap cmd = %02x,ret = %02x\n",7,ret);
  788. if(ret == XSP_IAP_RESULT_SUCCESS)
  789. {
  790. ota_xsp_info.xsp_ota_step++;
  791. // device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  792. }
  793. break;
  794. case 0x0F://复位指令,无返回
  795. break;
  796. default:
  797. break;
  798. }
  799. // break;
  800. // default:
  801. // printf("iap cmd error\n");
  802. // break;
  803. // }
  804. return ret;
  805. }
  806. /*--------------------------------------------------------------------
  807. * 显示屏指令接收解析
  808. * 帧头[1]+帧长度[1]+帧号[1] +SN 码[4]+指令码[1]+参数[n]+CRC校验码[2]
  809. *
  810. * --------------------------------------------------------------------*/
  811. int xsp_rcv_comm(uint8_t *key, uint8_t *data, uint8_t len)
  812. {
  813. int ret;
  814. uint8_t *fparm;
  815. uint16_t *p_crc, crc;
  816. uint8_t randDa[4] = {0};
  817. uint8_t temp[5] = {0};
  818. uint32_t appver = 0;
  819. uint16_t data_size=0,devtype=0,protver=0;
  820. txspCommFrame_t *p_xsp_rx = (txspCommFrame_t *)data;
  821. // _ytsf_data_decode(data, &len); /*接收解码 */
  822. crc = CRC16_get(&p_xsp_rx->fhead, p_xsp_rx->flen);
  823. p_crc = (uint16_t *)&data[p_xsp_rx->flen];
  824. *p_crc = ntohs(*p_crc);
  825. if(p_xsp_rx->fhead != 0xBB || crc != *p_crc) {
  826. ret = XSP_RESULT_HEADCRC;
  827. ota_xsp_info.error_num++;
  828. printf("crc err\n");
  829. return ret;
  830. }
  831. printf("rcv cmd = 0x%02x, sn = %10u\r\n",p_xsp_rx->fcmd,p_xsp_rx->faddress);
  832. fparm = &data[sizeof(txspCommFrame_t)];
  833. if(p_xsp_rx->fcmd == 0xce)
  834. {
  835. if(IAP_PROT_VER==IAP_VER2)//新版
  836. {
  837. ret = fparm[0];
  838. if(ret != XSP_RESULT_SUCCESS){
  839. printf("执行结果错误 %d\r\n",ret);
  840. ota_xsp_info.error_num++;
  841. // ota_xsp_info.xsp_ota_begin = 1;
  842. return ret;
  843. }
  844. fparm++;
  845. }
  846. }
  847. else
  848. {
  849. ret = fparm[0];
  850. if(ret != XSP_RESULT_SUCCESS){
  851. printf("执行结果错误 %d\r\n",ret);
  852. ota_xsp_info.error_num++;
  853. // ota_xsp_info.xsp_ota_begin = 1;
  854. return ret;
  855. }
  856. fparm++;
  857. }
  858. switch(p_xsp_rx->fcmd)
  859. {
  860. case 0xC0:
  861. ret = xsp_0xC0_rcv(xsp_comm_info,fparm,p_xsp_rx->flen-XSP_CMD_HEADLEN-1);
  862. // ota_xsp_info.xsp_ota_begin = 1;
  863. if(ret== XSP_RESULT_SUCCESS){
  864. ota_xsp_info.txsp_info.curkey = xsp_comm_info[0].xsp_info.KeySequence_number;
  865. memcpy(temp,xsp_comm_info[0].xsp_info.Software_version,4);
  866. appver = HextoDec((char*)(temp));
  867. printf("keygroup:%d\n",ota_xsp_info.txsp_info.curkey);
  868. printf("appver:%08x\n",appver);
  869. //协议版本
  870. protver = (xsp_comm_info[0].xsp_info.protocolVer[0]-'0')*10+(xsp_comm_info[0].xsp_info.protocolVer[1]-'0');
  871. printf("protver=%d\n",protver);
  872. if(protver >= 16)
  873. {
  874. devtype = xsp_comm_info[0].xsp_info.devType[0];
  875. devtype = (devtype<<8)|xsp_comm_info[0].xsp_info.devType[1];
  876. printf("devtype=%04X, xsp_type=%04X\n",devtype,ota_xsp_info.txsp_info.xsp_type);
  877. if(devtype != ota_xsp_info.txsp_info.xsp_type)//设备类型不匹配
  878. {
  879. ota_xsp_info.error_num = 4;
  880. ota_xsp_info.xsp_ota_step = 9;//用于返回升级错误码
  881. return ret;
  882. }
  883. }
  884. if(p_xsp_rx->flen == 0x27 && (ota_xsp_info.txsp_info.xsp_sn==10000121||ota_xsp_info.txsp_info.xsp_sn==10000122))
  885. {
  886. IAP_PROT_VER = IAP_VER1;
  887. }
  888. else
  889. {
  890. IAP_PROT_VER = IAP_VER2;
  891. }
  892. printf("iap ver:%d\n",IAP_PROT_VER);
  893. ota_xsp_info.xsp_ota_step++;
  894. ota_xsp_info.error_num = 0;
  895. }
  896. else
  897. {
  898. ota_xsp_info.error_num++;
  899. }
  900. break;
  901. case 0xC1:
  902. ret = xsp_0xC1_rcv(key,fparm,p_xsp_rx->flen-XSP_CMD_HEADLEN-1);
  903. // ota_xsp_info.xsp_ota_begin = 1;
  904. printf("ret = %02x\n",ret);
  905. if(ret== XSP_RESULT_SUCCESS)
  906. {
  907. ota_xsp_info.xsp_ota_step++;
  908. ota_xsp_info.error_num=0;
  909. }
  910. else
  911. {
  912. ota_xsp_info.error_num++;
  913. }
  914. break;
  915. case 0xCE://应答没有没有操作结果
  916. // ota_xsp_info.xsp_ota_begin = 1;
  917. memset(_tempBuff,0,sizeof(_tempBuff));
  918. data_dump("key",key,16);
  919. if(IAP_PROT_VER == IAP_VER2)
  920. {
  921. data_size = p_xsp_rx->flen-XSP_CMD_HEADLEN-1;//新版
  922. }
  923. else if(IAP_PROT_VER == IAP_VER1)
  924. {
  925. data_size = p_xsp_rx->flen-XSP_CMD_HEADLEN;//老版
  926. }
  927. DesDecryptFunc(fparm,_tempBuff,key,data_size,16); //解密
  928. data_dump("hub decrypt",_tempBuff,data_size);
  929. if(IAP_PROT_VER == IAP_VER2)
  930. {
  931. //循环异或(新版)
  932. memcpy(randDa,_tempBuff+data_size-6,4);
  933. randomOperate(_tempBuff,data_size-6,randDa,4);
  934. }
  935. // data_dump("hub decrypt rand",_tempBuff,data_size);
  936. //
  937. ret = xsp_hub_rcv(ota_xsp_info.txsp_info.key_iap,_tempBuff,data_size);
  938. if(ret == XSP_IAP_RESULT_SUCCESS)
  939. {
  940. ota_xsp_info.error_num = 0;
  941. }
  942. else
  943. {
  944. ota_xsp_info.error_num++;
  945. }
  946. break;
  947. default:
  948. // ota_xsp_info.xsp_ota_begin = 1;
  949. ota_xsp_info.error_num++;
  950. break;
  951. }
  952. return ret;
  953. }