can_board.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /********************************************************************
  2. 版权所有:
  3. 文件版本: V1.00
  4. 文件名称: can_board.c
  5. 生成日期: 2020年4月20日
  6. 作 者: oujie
  7. 使用范围:
  8. 功 能:通过协议与can子板通信处理程序(非硬件确定canid)
  9. 更新信息:
  10. 更新日志1:
  11. 修改者:
  12. 修改日期:
  13. 修改内容:
  14. 修改原因:
  15. *********************************************************************/
  16. /*------------------------------- 头文件 --------------------------------------
  17. */
  18. #include "head.h"
  19. //#ifndef CAN_DISABLE
  20. //#ifdef CAN_SLAVE_BOARD
  21. const CAN_BOARD_NAME can_board_name[] =
  22. {
  23. {BOARD_TYPE_METERING, "计量板"}
  24. };
  25. #define CAN_NAME_NUM (sizeof(can_board_name)/sizeof(can_board_name[0]))
  26. struct can_board_res can_board[CAN_BOARD_NUM];
  27. u8 can_board_enable;
  28. int get_line_string(char* src_buf, char* dst_buf, u16 len)
  29. {
  30. u16 i = 0;
  31. while(i < len)
  32. {
  33. if(strncmp(&src_buf[i], "\r\n", 2) == 0)
  34. return (i+2);
  35. dst_buf[i] = src_buf[i];
  36. i++;
  37. }
  38. return -1;
  39. }
  40. //读n行字符串
  41. int get_part_line(char* src_buf, char* dst_buf, u16 n, u16 line_max)
  42. {
  43. int len = 0;
  44. int i = 0;
  45. int total_len = 0;
  46. while(i < n)
  47. {
  48. len = get_line_string(src_buf+total_len, dst_buf + i*line_max, line_max);
  49. if(len == -1)
  50. break;
  51. total_len += len;
  52. i++;
  53. }
  54. return i;
  55. }
  56. void Hex2Str( const char *sSrc, char *sDest, int nSrcLen )
  57. {
  58. int i;
  59. char szTmp[3];
  60. for( i = 0; i < nSrcLen; i++ )
  61. {
  62. sprintf( szTmp, "%02X", (unsigned char) sSrc[i] );
  63. memcpy( &sDest[i * 2], szTmp, 2 );
  64. }
  65. return ;
  66. }
  67. //十六进制字符串转换为字节流
  68. int HexStr2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)
  69. {
  70. int i;
  71. for (i = 0; i < nSrcLength; i += 2)
  72. {
  73. // 输出高4位
  74. if ((*pSrc >= '0') && (*pSrc <= '9'))
  75. {
  76. *pDst = (*pSrc - '0') << 4;
  77. }
  78. else
  79. {
  80. *pDst = (*pSrc - 'A' + 10) << 4;
  81. }
  82. pSrc++;
  83. // 输出低4位
  84. if ((*pSrc>='0') && (*pSrc<='9'))
  85. {
  86. *pDst |= *pSrc - '0';
  87. }
  88. else
  89. {
  90. *pDst |= *pSrc - 'A' + 10;
  91. }
  92. pSrc++;
  93. //TRACE("%s",pSrc);
  94. pDst++;
  95. //TRACE("%s",pDst);
  96. }
  97. // 返回目标数据长度
  98. rt_printf("\r\n");
  99. return (nSrcLength / 2);
  100. }
  101. /******************************************************************************
  102. 函数名称: equ_init_can_file_res
  103. 函数版本: 01.00
  104. 创建作者: xxxxxx
  105. 创建日期: 2020-04-21
  106. 函数说明: 从can板卡资源文件装载can板卡资源。
  107. 参数说明:
  108. 返回值:
  109. 0: 成功
  110. 其它: 失败
  111. 修改记录:
  112. */
  113. u32 chipid[3] = {0};
  114. u32 chipid_hex[3] = {0};
  115. int equ_init_can_file_res(void)
  116. {
  117. u32 len;
  118. u8 *buf;
  119. char* ppos1;
  120. char* ppos2;
  121. // u8 line_buf[128] = {0};
  122. u8 dst_buf[CAN_BOARD_NUM+1][128] = {{0}};
  123. u8 num, str_len, line_num;
  124. u8 i = 0;
  125. #ifdef CAN_BOARD_DEBUG
  126. u8 j = 0;
  127. #endif
  128. int pfile;
  129. loff_t pos;
  130. int slot;
  131. u8 id_len;
  132. // 打开文件
  133. pfile = rt_file_open("/app/data/can_brd.csv",O_RDONLY ,0);
  134. if(IS_ERR(pfile))
  135. {
  136. rt_printf("未找到can_brd.csv文件\r\n");
  137. return -1;
  138. }
  139. // 得到文件长度
  140. len = rt_file_getfile_size(pfile);
  141. // 分配内存
  142. buf = rt_malloc(len);
  143. if((buf) == NULL)
  144. {
  145. rt_file_close(pfile,0);
  146. return -2;
  147. }
  148. // 读出内容
  149. pos = 0;
  150. if(rt_file_read(pfile,buf,len,&pos) != len)
  151. {
  152. rt_file_close(pfile,0);
  153. rt_free(buf);
  154. return -3;
  155. }
  156. // 关闭文件
  157. rt_file_close(pfile,0);
  158. #ifdef CAN_BOARD_DEBUG
  159. rt_printf("读can_brd.csv\r\n");
  160. #endif
  161. num = get_part_line(buf, (char*)dst_buf, CAN_BOARD_NUM+1, sizeof(dst_buf[0]));
  162. line_num = (num > CAN_BOARD_NUM) ? CAN_BOARD_NUM : num;
  163. #ifdef CAN_BOARD_DEBUG
  164. rt_printf("行数=%d\r\n", num);
  165. rt_printf("用于匹配行数=%d\r\n", line_num);
  166. #endif
  167. #if 1
  168. for(i=1; i<line_num; i++)//i=1,从第二行获取
  169. {
  170. #ifdef CAN_BOARD_DEBUG
  171. rt_printf("%s\r\n",&dst_buf[i][0]);
  172. #endif
  173. str_len = strlen((char*)&dst_buf[i][0]);
  174. if(strncmp(&dst_buf[i][0], "ID", 2) == 0)
  175. {
  176. #ifdef CAN_BOARD_DEBUG
  177. rt_printf("找到匹配行=%d\r\n", i);
  178. #endif
  179. ppos1 = strchr((char*)&dst_buf[i][0], ',');
  180. if(ppos1)
  181. {
  182. ppos1++;
  183. ppos2 = strchr(ppos1, ',');
  184. if(ppos2)
  185. {
  186. id_len = (u32)ppos2 - (u32)ppos1;
  187. #ifdef CAN_BOARD_DEBUG
  188. rt_printf("id_len=%d\r\n", id_len);
  189. #endif
  190. if (id_len > (sizeof(can_board[i-1].chip_id)*2))
  191. {
  192. rt_printf("第%d行 ID号过长,不能超过32个字符串\r\n", i);
  193. continue;
  194. }
  195. memset(can_board[i-1].chip_id, 0, sizeof(can_board[i-1].chip_id));
  196. HexStr2Bytes(ppos1, (u8*)can_board[i-1].chip_id, id_len);
  197. #ifdef CAN_BOARD_DEBUG
  198. for(j=0; j<CHIP_ID_SIZE; j++)
  199. rt_printf("chip_id[%d]=%x\r\n", j, can_board[i-1].chip_id[j]);
  200. #endif
  201. }
  202. else
  203. continue;
  204. }
  205. else
  206. continue;
  207. ppos2 = strchr(ppos1, ',');
  208. if(ppos2)
  209. {
  210. slot = atoi(ppos2+1);
  211. if(slot >= 0)
  212. {
  213. can_board[i-1].slot = slot;
  214. can_board[i-1].file_have = 1;
  215. can_board_enable = 1;
  216. #ifdef CAN_BOARD_DEBUG
  217. rt_printf("can_board[%d].slot=%d\r\n", i-1, can_board[i-1].slot);
  218. #endif
  219. }
  220. else
  221. rt_printf("文件板卡号有误\r\n");
  222. }
  223. else
  224. continue;
  225. }
  226. #if 0
  227. for(k=0; k<sizeof(can_board[0].chip_id); k++)
  228. rt_printf("chip_id[%d]=%d ", k, can_board[i-1].chip_id[k]);
  229. rt_printf("\r\n");
  230. rt_printf("can_board[%d].slot=%d\r\n", i-1, can_board[i-1].slot);
  231. #endif
  232. }
  233. #endif
  234. return 0;
  235. }
  236. /******************************************************************************
  237. 函数名称: equ_get_can_slot
  238. 函数版本: 01.00
  239. 创建作者: oujie
  240. 创建日期: 2020-08-08
  241. 函数说明: 通过子板的chipid查找插槽号。
  242. 参数说明:
  243. chip_id 子板id。
  244. id_size id大小
  245. 返回值: 插槽号
  246. 修改记录:
  247. */
  248. struct can_board_res* equ_get_can_slot(u8 give_slot, u8 type, u8* chip_id, u8 id_size)
  249. {
  250. #if defined(BSP_DTU3) || defined(BSP_DTU2) || defined(BSP_DTU5)
  251. u32 i;
  252. for(i=0; i<CAN_BOARD_NUM; i++)
  253. {
  254. if(can_board[i].file_have && ((give_slot != can_board[0].slot)
  255. || (give_slot == 0) || (give_slot == 0xff))) //子板应答卡号为0,需要从发,防止子板复位
  256. {
  257. if(memcmp((u8*)can_board[i].chip_id, chip_id, id_size) == 0)
  258. {
  259. rt_printf("和文件中的ID匹配\r\n");
  260. return &can_board[i];
  261. }
  262. }
  263. }
  264. #else //分布式、FTU不需要配置文件
  265. u8 slot;
  266. //子板应答卡号为0,需要从发,防止子板复位
  267. if((give_slot != can_board[0].slot)
  268. || (give_slot == 0) || (give_slot == 0xff))
  269. {
  270. slot = equ_get_slot_by_type(type);
  271. if(slot)
  272. {
  273. can_board[0].slot = slot;
  274. return &can_board[0];
  275. }
  276. else
  277. {
  278. #ifdef CAN_BOARD_DEBUG
  279. rt_printf("未配置对应的板卡资源,板卡类型:%d\r\n", type);
  280. #endif
  281. }
  282. }
  283. #endif
  284. return NULL;
  285. }
  286. u8 is_can_slot_valid(u8 i)
  287. {
  288. return can_board[i].given_slot;
  289. }
  290. u8 get_can_slot(u8 i)
  291. {
  292. return can_board[i].slot;
  293. }
  294. void can_id_printf(void)
  295. {
  296. u8 i;
  297. char buf[64] = {0};
  298. char* name = NULL;
  299. for(i=0; i<CAN_BOARD_NUM; i++)
  300. {
  301. if(can_board[i].given_slot)
  302. {
  303. Hex2Str((const char*)can_board[i].chip_id, buf, CHIP_ID_SIZE*4);
  304. name = get_can_name(can_board[i].type);
  305. if(name)
  306. rt_printf("\r\nCAN子板%d:名称 :%s\r\n", i+1, name);
  307. else
  308. rt_printf("\r\nCAN子板%d:名称 :无\r\n", i+1);
  309. rt_printf(" ID :%s\r\n", buf);
  310. rt_printf(" 板卡号 :%d\r\n", can_board[i].slot);
  311. }
  312. else
  313. rt_printf("CAN子板%d:无\r\n", i+1);
  314. }
  315. }
  316. char* get_can_name(u8 type)
  317. {
  318. u8 i;
  319. for(i=0; i<CAN_NAME_NUM; i++)
  320. {
  321. if(type == can_board_name[i].type)
  322. return can_board_name[i].name;
  323. }
  324. return NULL;
  325. }
  326. int get_can_type_from_slot(u8 slot)
  327. {
  328. u8 i;
  329. for(i=0; i<CAN_BOARD_NUM; i++)
  330. {
  331. if(can_board[i].slot == slot)
  332. {
  333. if(can_board[i].type)
  334. {
  335. return can_board[i].type;
  336. }
  337. else
  338. {
  339. return -1;
  340. }
  341. }
  342. }
  343. return -1;
  344. }
  345. void can_brd_error_set(void)
  346. {
  347. u8 i;
  348. for(i=0; i<CAN_BOARD_NUM; i++)
  349. {
  350. if(BOARD_TYPE_METERING == can_board[i].type)
  351. {
  352. if(can_board[i].slot)
  353. {
  354. #if defined CAN_SLAVE_BOARD && defined METERING_ENERGY
  355. if(soe_check(EV_CAN_METERING_ERR)==false)
  356. {
  357. soe_record_ev(EV_CAN_METERING_ERR, 1, can_board[i].slot,0,0 );
  358. }
  359. #endif
  360. }
  361. }
  362. }
  363. }
  364. void can_brd_error_clear(u8 slot)
  365. {
  366. u8 type;
  367. type = get_can_type_from_slot(slot);
  368. if(BOARD_TYPE_METERING == type)
  369. {
  370. #if defined CAN_SLAVE_BOARD && defined METERING_ENERGY
  371. if(soe_check(EV_CAN_METERING_ERR)==true)
  372. {
  373. soe_record_ev(EV_CAN_METERING_ERR, 0, slot,0,0 );
  374. }
  375. #endif
  376. }
  377. }
  378. //#endif
  379. //#endif
  380. /*------------------------------- 宏定义 --------------------------------------
  381. */