UDSInterface.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /********************************************************************************
  2. * 常州易控汽车电子股份有限公司
  3. * (c) Copyright 2009-2015 ECTEK
  4. * 保留所有权利
  5. ********************************************************************************
  6. * 工程名称:整车管理
  7. * 文件名: UDSInterface.c
  8. * 功能描述 : UDS管理
  9. * 引用记录:
  10. * 备注:
  11. * 修订记录:
  12. * 日期 版本 作者 描述
  13. * 2017.07.03 00.01.00 李中 第一版
  14. *******************************************************************************/
  15. #include "UDSInterface.h"
  16. #include "HardwareLib.h"
  17. #include "CANNet.h"
  18. #include "EOLData.h"
  19. uint8 UDSInterface_CheckServiceCondition(uint8 idxSIDUB, uint8 stSubFuncUB)
  20. {
  21. (void)stSubFuncUB;
  22. uint8 bResultUB=1;
  23. switch(idxSIDUB)
  24. {
  25. case _UDS_INDEX_COMMAND_SECURITYACCESS:
  26. /*27*/
  27. bResultUB=1;
  28. break;
  29. case _UDS_INDEX_COMMAND_ECURESET:
  30. /*ECU Reset*/
  31. bResultUB=1;
  32. break;
  33. case _UDS_INDEX_COMMAND_WRITEDATABYIDENTIFIER:
  34. /*Write Data By Identifier*/
  35. bResultUB=1;
  36. break;
  37. case _UDS_INDEX_COMMAND_CONTROLDTCSETTING:
  38. /*Control DTC Setting*/
  39. bResultUB=1;
  40. break;
  41. case _UDS_INDEX_COMMAND_STARTDIAGNOSTICSESSION:
  42. /*Start Diagnostic Session*/
  43. // if(stSubFuncUB==_UDS_INDEX_DIAG_PROGRAMMING)
  44. // {
  45. // //必须先经过检查编程预条件
  46. // if(UDSInterface_bPrgPrecondChkedUB==1)
  47. // {
  48. // bResultUB=1;
  49. // }
  50. // else
  51. // {
  52. // bResultUB=0;
  53. // }
  54. // }
  55. // else if(stSubFuncUB==_UDS_INDEX_DIAG_DEFAULT)
  56. // {
  57. // UDSInterface_bPrgPrecondChkedUB=0;
  58. // }
  59. // else
  60. // {
  61. // bResultUB=1;
  62. // }
  63. bResultUB=1;
  64. break;
  65. default:
  66. bResultUB=1;
  67. break;
  68. }
  69. return bResultUB;
  70. }
  71. void UDSInterface_GetSeed(uint8 idxSecLvlUB, uint8 * dataSeedUBP)
  72. {
  73. // uint32 datUI;
  74. switch(idxSecLvlUB)
  75. {
  76. case _UDS_INDEX_SEC_EXTENTED:
  77. if(idxSecLvlUB==UDS_GetSecLvl())
  78. {
  79. /*如果请求的安全等级已解锁,则发送数值为0的种子*/
  80. dataSeedUBP[0]=0;
  81. dataSeedUBP[1]=0;
  82. dataSeedUBP[2]=0;
  83. dataSeedUBP[3]=0;
  84. }
  85. else
  86. {
  87. dataSeedUBP[0]=11;//11
  88. dataSeedUBP[1]=22;//22
  89. dataSeedUBP[2]=33;//33
  90. dataSeedUBP[3]=44;//44
  91. }
  92. break;
  93. case _UDS_INDEX_SEC_ANTITHEFT:
  94. if(idxSecLvlUB==UDS_GetSecLvl())
  95. {
  96. /*如果请求的安全等级已解锁,则发送数值为0的种子*/
  97. dataSeedUBP[0]=0;
  98. dataSeedUBP[1]=0;
  99. dataSeedUBP[2]=0;
  100. dataSeedUBP[3]=0;
  101. }
  102. else
  103. {
  104. dataSeedUBP[0]=0x55;
  105. dataSeedUBP[1]=0x66;
  106. dataSeedUBP[2]=0x77;
  107. dataSeedUBP[3]=0x88;
  108. }
  109. break;
  110. default:
  111. dataSeedUBP[0]=0x00;
  112. dataSeedUBP[1]=0x00;
  113. dataSeedUBP[2]=0x00;
  114. dataSeedUBP[3]=0x00;
  115. break;
  116. }
  117. }
  118. void UDSInterface_GetKey(uint8 idxSecLvlUB, uint8 * dataSeedUBP, uint8 * dataKeyUBP)
  119. {
  120. uint8 Count; //循环次数
  121. uint8 Dir; //循环移位方向,0左移-1右移
  122. uint8 Pattern; //模式匹配
  123. uint8 Select; //选择MASK
  124. uint8 i; //循环计数
  125. uint32 seed; //中间转换SEED
  126. uint32 mask[4]={0x5DEC1836,0x5DEC1836,0x5DEC1836,0x5DEC1836}; //mask表
  127. switch(idxSecLvlUB)
  128. {
  129. case _UDS_INDEX_SEC_EXTENTED:
  130. case _UDS_INDEX_SEC_ANTITHEFT:
  131. Count=dataSeedUBP[1]&0x0F;
  132. Select=(dataSeedUBP[3]&0x30)>>4;
  133. seed=((uint32)dataSeedUBP[0]<<24)+((uint32)dataSeedUBP[1]<<16)+((uint32)dataSeedUBP[2]<<8)+((uint32)dataSeedUBP[3]);
  134. for(i=0;i<Count;i++)
  135. {
  136. Pattern=((uint8)seed)&0x07;
  137. Dir=(((uint8)seed)&0x08)>>3;
  138. if(Pattern!=0b101)
  139. {
  140. if(Dir==1)
  141. {
  142. seed=seed/2+((seed&0x00000001)<<31);//右移
  143. }
  144. else
  145. {
  146. seed=seed*2+((seed&0x80000000)>>31);//左移
  147. }
  148. }
  149. else
  150. {
  151. break;
  152. }
  153. }
  154. seed=seed^mask[Select];
  155. for(i=0;i<4;i++)
  156. {
  157. dataKeyUBP[i]=(uint8)(seed>>((3-i)*8));
  158. }
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. /*0x31 例程控制回调函数样例*/
  165. uint8 UDSInterface_RoutineTestCallback(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP)
  166. {
  167. if(numRecvLenUW>3)
  168. {
  169. switch(datRecvBuffUBP[0]&0x7F) //suppressPosRspMsgIndicationBit supported
  170. {
  171. case _UDS_ROUTINECONTROL_START:
  172. break;
  173. case _UDS_ROUTINECONTROL_STOP:
  174. break;
  175. case _UDS_ROUTINECONTROL_REQUESTRESULTS:
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. else
  182. {
  183. return _UDS_INDEX_ERROR_INVALIDFORMAT;
  184. }
  185. *numSendLenUWP=4;
  186. datSendBuffUBP[0]=0x11;
  187. datSendBuffUBP[1]=0x22;
  188. datSendBuffUBP[2]=0x33;
  189. datSendBuffUBP[3]=0x44;
  190. return 0;
  191. }
  192. /*0x2F 例程控制回调函数样例*/
  193. uint8 UDSInterface_IOControlCallback(uint16 numRecvLenUW, uint8 * datRecvBuffUBP, uint16 * numSendLenUWP, uint8 * datSendBuffUBP)
  194. {
  195. if(numRecvLenUW>3)
  196. {
  197. switch(datRecvBuffUBP[0]&0x7F) //suppressPosRspMsgIndicationBit supported
  198. {
  199. case _UDS_ROUTINECONTROL_START:
  200. break;
  201. case _UDS_ROUTINECONTROL_STOP:
  202. break;
  203. case _UDS_ROUTINECONTROL_REQUESTRESULTS:
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. else
  210. {
  211. return _UDS_INDEX_ERROR_INVALIDFORMAT;
  212. }
  213. *numSendLenUWP=4;
  214. datSendBuffUBP[0]=0x11;
  215. datSendBuffUBP[1]=0x22;
  216. datSendBuffUBP[2]=0x33;
  217. datSendBuffUBP[3]=0x44;
  218. return 0;
  219. }