can_board.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. struct file * 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. rt_file_llseek(pfile,0,SEEK_END);
  141. len = pfile->f_pos;
  142. rt_file_llseek(pfile,0,SEEK_SET);
  143. // 分配内存
  144. buf = rt_malloc(len);
  145. if((buf) == NULL)
  146. {
  147. rt_file_close(pfile,0);
  148. return -2;
  149. }
  150. // 读出内容
  151. pos = 0;
  152. if(rt_file_read(pfile,buf,len,&pos) != len)
  153. {
  154. rt_file_close(pfile,0);
  155. rt_free(buf);
  156. return -3;
  157. }
  158. // 关闭文件
  159. rt_file_close(pfile,0);
  160. #ifdef CAN_BOARD_DEBUG
  161. rt_printf("读can_brd.csv\r\n");
  162. #endif
  163. num = get_part_line(buf, (char*)dst_buf, CAN_BOARD_NUM+1, sizeof(dst_buf[0]));
  164. line_num = (num > CAN_BOARD_NUM) ? CAN_BOARD_NUM : num;
  165. #ifdef CAN_BOARD_DEBUG
  166. rt_printf("行数=%d\r\n", num);
  167. rt_printf("用于匹配行数=%d\r\n", line_num);
  168. #endif
  169. #if 1
  170. for(i=1; i<line_num; i++)//i=1,从第二行获取
  171. {
  172. #ifdef CAN_BOARD_DEBUG
  173. rt_printf("%s\r\n",&dst_buf[i][0]);
  174. #endif
  175. str_len = strlen((char*)&dst_buf[i][0]);
  176. if(strncmp(&dst_buf[i][0], "ID", 2) == 0)
  177. {
  178. #ifdef CAN_BOARD_DEBUG
  179. rt_printf("找到匹配行=%d\r\n", i);
  180. #endif
  181. ppos1 = strchr((char*)&dst_buf[i][0], ',');
  182. if(ppos1)
  183. {
  184. ppos1++;
  185. ppos2 = strchr(ppos1, ',');
  186. if(ppos2)
  187. {
  188. id_len = (u32)ppos2 - (u32)ppos1;
  189. #ifdef CAN_BOARD_DEBUG
  190. rt_printf("id_len=%d\r\n", id_len);
  191. #endif
  192. if (id_len > (sizeof(can_board[i-1].chip_id)*2))
  193. {
  194. rt_printf("第%d行 ID号过长,不能超过32个字符串\r\n", i);
  195. continue;
  196. }
  197. memset(can_board[i-1].chip_id, 0, sizeof(can_board[i-1].chip_id));
  198. HexStr2Bytes(ppos1, (u8*)can_board[i-1].chip_id, id_len);
  199. #ifdef CAN_BOARD_DEBUG
  200. for(j=0; j<CHIP_ID_SIZE; j++)
  201. rt_printf("chip_id[%d]=%x\r\n", j, can_board[i-1].chip_id[j]);
  202. #endif
  203. }
  204. else
  205. continue;
  206. }
  207. else
  208. continue;
  209. ppos2 = strchr(ppos1, ',');
  210. if(ppos2)
  211. {
  212. slot = atoi(ppos2+1);
  213. if(slot >= 0)
  214. {
  215. can_board[i-1].slot = slot;
  216. can_board[i-1].file_have = 1;
  217. can_board_enable = 1;
  218. #ifdef CAN_BOARD_DEBUG
  219. rt_printf("can_board[%d].slot=%d\r\n", i-1, can_board[i-1].slot);
  220. #endif
  221. }
  222. else
  223. rt_printf("文件板卡号有误\r\n");
  224. }
  225. else
  226. continue;
  227. }
  228. #if 0
  229. for(k=0; k<sizeof(can_board[0].chip_id); k++)
  230. rt_printf("chip_id[%d]=%d ", k, can_board[i-1].chip_id[k]);
  231. rt_printf("\r\n");
  232. rt_printf("can_board[%d].slot=%d\r\n", i-1, can_board[i-1].slot);
  233. #endif
  234. }
  235. #endif
  236. return 0;
  237. }
  238. /******************************************************************************
  239. 函数名称: equ_get_can_slot
  240. 函数版本: 01.00
  241. 创建作者: oujie
  242. 创建日期: 2020-08-08
  243. 函数说明: 通过子板的chipid查找插槽号。
  244. 参数说明:
  245. chip_id 子板id。
  246. id_size id大小
  247. 返回值: 插槽号
  248. 修改记录:
  249. */
  250. struct can_board_res* equ_get_can_slot(u8 give_slot, u8 type, u8* chip_id, u8 id_size)
  251. {
  252. #if defined(BSP_DTU3) || defined(BSP_DTU2)
  253. u32 i;
  254. for(i=0; i<CAN_BOARD_NUM; i++)
  255. {
  256. if(can_board[i].file_have && ((give_slot != can_board[0].slot)
  257. || (give_slot == 0) || (give_slot == 0xff))) //子板应答卡号为0,需要从发,防止子板复位
  258. {
  259. if(memcmp((u8*)can_board[i].chip_id, chip_id, id_size) == 0)
  260. {
  261. rt_printf("和文件中的ID匹配\r\n");
  262. return &can_board[i];
  263. }
  264. }
  265. }
  266. #else //分布式、FTU不需要配置文件
  267. u8 slot;
  268. //子板应答卡号为0,需要从发,防止子板复位
  269. if((give_slot != can_board[0].slot)
  270. || (give_slot == 0) || (give_slot == 0xff))
  271. {
  272. slot = equ_get_slot_by_type(type);
  273. if(slot)
  274. {
  275. can_board[0].slot = slot;
  276. return &can_board[0];
  277. }
  278. else
  279. {
  280. #ifdef CAN_BOARD_DEBUG
  281. rt_printf("未配置对应的板卡资源,板卡类型:%d\r\n", type);
  282. #endif
  283. }
  284. }
  285. #endif
  286. return NULL;
  287. }
  288. u8 is_can_slot_valid(u8 i)
  289. {
  290. return can_board[i].given_slot;
  291. }
  292. u8 get_can_slot(u8 i)
  293. {
  294. return can_board[i].slot;
  295. }
  296. void can_id_printf(void)
  297. {
  298. u8 i;
  299. char buf[64] = {0};
  300. char* name = NULL;
  301. for(i=0; i<CAN_BOARD_NUM; i++)
  302. {
  303. if(can_board[i].given_slot)
  304. {
  305. Hex2Str((const char*)can_board[i].chip_id, buf, CHIP_ID_SIZE*4);
  306. name = get_can_name(can_board[i].type);
  307. if(name)
  308. rt_printf("\r\nCAN子板%d:名称 :%s\r\n", i+1, name);
  309. else
  310. rt_printf("\r\nCAN子板%d:名称 :无\r\n", i+1);
  311. rt_printf(" ID :%s\r\n", buf);
  312. rt_printf(" 板卡号 :%d\r\n", can_board[i].slot);
  313. }
  314. else
  315. rt_printf("CAN子板%d:无\r\n", i+1);
  316. }
  317. }
  318. char* get_can_name(u8 type)
  319. {
  320. u8 i;
  321. for(i=0; i<CAN_NAME_NUM; i++)
  322. {
  323. if(type == can_board_name[i].type)
  324. return can_board_name[i].name;
  325. }
  326. return NULL;
  327. }
  328. int get_can_type_from_slot(u8 slot)
  329. {
  330. u8 i;
  331. for(i=0; i<CAN_BOARD_NUM; i++)
  332. {
  333. if(can_board[i].slot == slot)
  334. {
  335. if(can_board[i].type)
  336. {
  337. return can_board[i].type;
  338. }
  339. else
  340. {
  341. return -1;
  342. }
  343. }
  344. }
  345. return -1;
  346. }
  347. void can_brd_error_set(void)
  348. {
  349. u8 i;
  350. for(i=0; i<CAN_BOARD_NUM; i++)
  351. {
  352. if(BOARD_TYPE_METERING == can_board[i].type)
  353. {
  354. if(can_board[i].slot)
  355. {
  356. #if defined CAN_SLAVE_BOARD && defined METERING_ENERGY
  357. if(soe_check(EV_CAN_METERING_ERR)==false)
  358. {
  359. soe_record_ev(EV_CAN_METERING_ERR, 1, can_board[i].slot,0,0 );
  360. }
  361. #endif
  362. }
  363. }
  364. }
  365. }
  366. void can_brd_error_clear(u8 slot)
  367. {
  368. u8 type;
  369. type = get_can_type_from_slot(slot);
  370. if(BOARD_TYPE_METERING == type)
  371. {
  372. #if defined CAN_SLAVE_BOARD && defined METERING_ENERGY
  373. if(soe_check(EV_CAN_METERING_ERR)==true)
  374. {
  375. soe_record_ev(EV_CAN_METERING_ERR, 0, slot,0,0 );
  376. }
  377. #endif
  378. }
  379. }
  380. //#endif
  381. //#endif
  382. /*------------------------------- 宏定义 --------------------------------------
  383. */