history_file.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: history_file.c
  4. 文件版本: 01.01
  5. 创建作者:
  6. 创建日期: 2016-08-09
  7. 功能说明: 历史数据文件操作接口实现文件
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include "head.h"
  12. #ifdef LINUX_KERNEL_APP
  13. #include "history_file.h"
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/fs.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kthread.h>
  22. #include <linux/delay.h>
  23. #include <linux/wait.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/io.h>
  26. #include <asm/ioctls.h>
  27. #include <linux/syscalls.h>
  28. #include <termios.h>
  29. #else
  30. #include "head.h"
  31. #include "history_file.h"
  32. #include <linux/fs.h>
  33. #include <linux/sched.h>
  34. #include <linux/wait.h>
  35. #include <asm/ioctls.h>
  36. #include <termios.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <dirent.h>
  40. #endif
  41. /******************************************************************************
  42. 函数名称: hf_get_filecnt_from_tmpfile
  43. 函数版本: 01.01
  44. 创建作者:
  45. 创建日期: 2016-08-09
  46. 函数说明: 从临时文件中获取文件个数
  47. 参数说明:
  48. tmpfile: 临时文件
  49. cols: 文件名占用的字节数
  50. 返回值: 成功返回文件个数
  51. 失败返回0
  52. 修改记录:
  53. */
  54. static int hf_get_filecnt_from_tmpfile(char *tmpfile, int cols)
  55. {
  56. int ret = 0;
  57. struct stat s;
  58. // 获取文件属性
  59. if (sys_newstat(tmpfile, &s))
  60. {
  61. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmpfile);
  62. return 0;
  63. }
  64. // 因为每行的字节数字是固定的,所以除以每行的字节数就可得出文件个数
  65. ret = s.st_size / (cols + 1);
  66. return ret;
  67. }
  68. /******************************************************************************
  69. 函数名称: hf_get_file_content
  70. 函数版本: 01.01
  71. 创建作者:
  72. 创建日期: 2016-08-09
  73. 函数说明: 获取文件内容
  74. 参数说明:
  75. filename: 文件名
  76. p_file_len: 返回的文件长度
  77. 返回值: 成功返回文件内容
  78. 失败返回NULL
  79. 修改记录:
  80. */
  81. static char *hf_get_file_content(char *filename, int *p_file_len)
  82. {
  83. char *p = NULL;
  84. int fd;
  85. struct stat s;
  86. int ret;
  87. // 获取文件属性
  88. if (sys_newstat(filename, &s))
  89. {
  90. rt_printf("%s:sys_newstat(%s) error\n", __func__, filename);
  91. return NULL;
  92. }
  93. *p_file_len = s.st_size;
  94. p = rt_malloc(s.st_size + 1); // 多一个字节,最后一个字节存放0,便于字符查找方便
  95. if (!p)
  96. {
  97. rt_printf("%s:rt_malloc eror\r\n", __func__);
  98. return NULL;
  99. }
  100. fd = sys_open(filename, O_RDONLY, 0);
  101. if (fd < 0)
  102. {
  103. rt_printf("sys_open(%s) error\r\n", filename);
  104. rt_free(p);
  105. return NULL;
  106. }
  107. ret = sys_read(fd, p, s.st_size);
  108. if (ret != s.st_size)
  109. {
  110. rt_printf("%s:sys_read error\r\n", __func__);
  111. rt_free(p);
  112. sys_close(fd);
  113. return NULL;
  114. }
  115. sys_close(fd);
  116. p[s.st_size] = 0;
  117. return p;
  118. }
  119. /******************************************************************************
  120. 函数名称: hf_get_timedfile_content
  121. 函数版本: 01.01
  122. 创建作者:
  123. 创建日期: 2016-08-09
  124. 函数说明: 获取录波文件的内容
  125. 参数说明:
  126. tmpfile: 临时文件
  127. cols: 文件名占用的字节数
  128. 返回值: 成功返回文件个数
  129. 失败返回0
  130. 修改记录:
  131. */
  132. static char *hf_get_timedfile_content(char *dir, int filename, int part, int *p_part_len)
  133. {
  134. #if 1
  135. char *p = NULL;
  136. struct dir_file_ext_struct *p_dfs_ext = NULL;
  137. int i = 0;
  138. int file_cnt = 0;
  139. char tmp[128];
  140. int bfind = 0;
  141. p_dfs_ext = hf_get_dir_file_ext(dir, &file_cnt, HF_FRZ_101_TMP);
  142. if (!p_dfs_ext)
  143. {
  144. return NULL;
  145. }
  146. for (i = 0; i < file_cnt; i++)
  147. {
  148. if (filename == (i + HF_FIXPT_FILENAME_N))
  149. {
  150. bfind = 1;
  151. break;
  152. }
  153. }
  154. if (!bfind)
  155. {
  156. goto END;
  157. }
  158. sprintf(tmp, "%s%s", dir, p_dfs_ext[i].file_name);
  159. p = hf_get_file_content(tmp, p_part_len);
  160. END:
  161. rt_free(p_dfs_ext);
  162. return p;
  163. #else
  164. char *p = NULL;
  165. char *p2;
  166. char *pstr;
  167. char *cmd_ls[] = {"/app/m", "ls", dir, HF_TIMED_101_TMP, NULL};
  168. char *envp[] = {NULL};
  169. char tmp[128];
  170. char tmp_filename[64];
  171. char cr_line[] = {'\r', '\n', 0};
  172. int cnt;
  173. int fd;
  174. int i;
  175. int ret;
  176. int filelen = 0;
  177. int addr;
  178. // 调用shell脚本,生成文件列表
  179. ret = call_usermodehelper("/app/m", cmd_ls, envp, UMH_WAIT_PROC);
  180. // 文件个数
  181. cnt = hf_get_filecnt_from_tmpfile(HF_TIMED_101_TMP, HF_FIXPT_FILENAME_LEN);
  182. fd = sys_open(HF_TIMED_101_TMP, O_RDONLY, 0);
  183. if (fd < 0)
  184. {
  185. return NULL;
  186. }
  187. for (i = 0; i < cnt; i++)
  188. {
  189. memset(tmp_filename, 0, sizeof(tmp_filename));
  190. ret = sys_read(fd, tmp_filename, HF_FIXPT_FILENAME_LEN + 1);
  191. if (ret != HF_FIXPT_FILENAME_LEN + 1)
  192. {
  193. sys_close(fd);
  194. }
  195. tmp_filename[HF_FIXPT_FILENAME_LEN] = 0;
  196. addr = simple_strtoul(tmp_filename, NULL, 10);
  197. if (addr == filename)
  198. {
  199. sys_close(fd);
  200. fd = 0;
  201. sprintf(tmp, "%s%s", dir, tmp_filename);
  202. p = hf_get_file_content(tmp, &filelen);
  203. if (p)
  204. {
  205. // 读文件头
  206. pstr = strstr(p, cr_line);
  207. if (!pstr)
  208. {
  209. rt_free(p);
  210. return NULL;
  211. }
  212. pstr += 2;
  213. pstr = strstr(pstr, cr_line);
  214. if (!pstr)
  215. {
  216. rt_free(p);
  217. return NULL;
  218. }
  219. pstr += 2;
  220. for (i = 0; i < (part - 1); i++)
  221. {
  222. if (pstr >= (p + filelen))
  223. {
  224. rt_free(p);
  225. return NULL;
  226. }
  227. pstr = strstr(pstr, cr_line);
  228. if (!pstr)
  229. {
  230. rt_free(p);
  231. return NULL;
  232. }
  233. pstr += 2;
  234. }
  235. if (*pstr == '\n') // '/A' ?
  236. {
  237. rt_free(p);
  238. return NULL;
  239. }
  240. p2 = pstr;
  241. pstr = strstr(pstr, cr_line);
  242. if (!pstr)
  243. {
  244. rt_free(p);
  245. return NULL;
  246. }
  247. *p_part_len = (int)(pstr - p2);
  248. memcpy(p, p2, *p_part_len);
  249. }
  250. break;
  251. }
  252. }
  253. if (fd > 0)
  254. {
  255. sys_close(fd);
  256. }
  257. rt_file_del(HF_TIMED_101_TMP);
  258. return p;
  259. #endif
  260. }
  261. /******************************************************************************
  262. 函数名称: hf_get_frzfile_content
  263. 函数版本: 01.01
  264. 创建作者:
  265. 创建日期: 2016-08-09
  266. 函数说明: 获取录波文件的内容
  267. 参数说明:
  268. tmpfile: 临时文件
  269. cols: 文件名占用的字节数
  270. 返回值: 成功返回文件个数
  271. 失败返回0
  272. 修改记录:
  273. */
  274. static char *hf_get_frzfile_content(char *dir, int filename, int part, int *p_part_len)
  275. {
  276. char *p = NULL;
  277. struct dir_file_ext_struct *p_dfs_ext = NULL;
  278. int i = 0;
  279. int file_cnt = 0;
  280. char tmp[128];
  281. int bfind = 0;
  282. p_dfs_ext = hf_get_dir_file_ext(dir, &file_cnt, HF_FRZ_101_TMP);
  283. if (!p_dfs_ext)
  284. {
  285. return NULL;
  286. }
  287. for (i = 0; i < file_cnt; i++)
  288. {
  289. if (filename == (i + HF_FRZ_FILENAME_N))
  290. {
  291. bfind = 1;
  292. break;
  293. }
  294. }
  295. if (!bfind)
  296. {
  297. goto END;
  298. }
  299. sprintf(tmp, "%s%s", dir, p_dfs_ext[i].file_name);
  300. p = hf_get_file_content(tmp, p_part_len);
  301. END:
  302. rt_free(p_dfs_ext);
  303. return p;
  304. }
  305. /******************************************************************************
  306. 函数名称: hf_get_wavefile_content
  307. 函数版本: 01.01
  308. 创建作者:
  309. 创建日期: 2016-08-09
  310. 函数说明: 获取录波文件的内容
  311. 参数说明:
  312. tmpfile: 临时文件
  313. cols: 文件名占用的字节数
  314. 返回值: 成功返回文件个数
  315. 失败返回0
  316. 修改记录:
  317. */
  318. static char *hf_get_wavefile_content(char *dir, int filename, int part, int *p_part_len)
  319. {
  320. char *m_path = "/app/m";
  321. char *p = NULL;
  322. char *cmd_ls[] = {m_path, "ls", dir, HF_WAVE_TMP, NULL};
  323. char *envp[] = {NULL};
  324. char tmp[128];
  325. char tmp_filename[64];
  326. int cnt;
  327. int fd;
  328. int i;
  329. int ret;
  330. int addr;
  331. // 调用shell脚本,生成文件列表
  332. #ifdef CPU_FUXI
  333. memset(tmp, 0, sizeof(tmp));
  334. sprintf(tmp, " ls %s > %s", dir, HF_WAVE_TMP);
  335. system(tmp);
  336. #else
  337. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  338. #endif
  339. // 文件个数
  340. cnt = hf_get_filecnt_from_tmpfile(HF_WAVE_TMP, HF_WAVE_FILE_LENGTH);
  341. fd = sys_open(HF_WAVE_TMP, O_RDONLY, 0);
  342. if (fd < 0)
  343. {
  344. return NULL;
  345. }
  346. for (i = 0; i < cnt; i++)
  347. {
  348. memset(tmp_filename, 0, sizeof(tmp_filename));
  349. ret = sys_read(fd, tmp_filename, HF_WAVE_FILE_LENGTH + 1);
  350. if (ret != HF_WAVE_FILE_LENGTH + 1)
  351. {
  352. sys_close(fd);
  353. }
  354. tmp_filename[HF_WAVE_FILE_LENGTH] = 0;
  355. addr = simple_strtoul(tmp_filename, NULL, 16);
  356. if (addr == filename)
  357. {
  358. // cfg文件
  359. if ((part == 1) && strstr(tmp_filename, ".cfg"))
  360. {
  361. sprintf(tmp, "%s%s", dir, tmp_filename);
  362. p = hf_get_file_content(tmp, p_part_len);
  363. break;
  364. }
  365. else if ((part == 2) && strstr(tmp_filename, ".dat")) // dat文件
  366. {
  367. sprintf(tmp, "%s%s", dir, tmp_filename);
  368. p = hf_get_file_content(tmp, p_part_len);
  369. break;
  370. }
  371. }
  372. }
  373. sys_close(fd);
  374. rt_file_del(HF_WAVE_TMP);
  375. return p;
  376. }
  377. /******************************************************************************
  378. 函数名称: hf_get_max_minfile_content
  379. 函数版本: 01.01
  380. 创建作者:
  381. 创建日期: 2016-08-09
  382. 函数说明: 获取录波文件的内容
  383. 参数说明:
  384. tmpfile: 临时文件
  385. cols: 文件名占用的字节数
  386. 返回值: 成功返回文件个数
  387. 失败返回0
  388. 修改记录:
  389. */
  390. static char *hf_get_max_minfile_content(char *dir, int filename, int part, int *p_part_len)
  391. {
  392. #if 1
  393. char *p = NULL;
  394. struct dir_file_ext_struct *p_dfs_ext = NULL;
  395. int i = 0;
  396. int file_cnt = 0;
  397. char tmp[128];
  398. int bfind = 0;
  399. p_dfs_ext = hf_get_dir_file_ext(dir, &file_cnt, HF_MAX_MIN_101_TMP);
  400. if (!p_dfs_ext)
  401. {
  402. return NULL;
  403. }
  404. for (i = 0; i < file_cnt; i++)
  405. {
  406. if (filename == (i + HF_EXV_FILENAME_N))
  407. {
  408. bfind = 1;
  409. break;
  410. }
  411. }
  412. if (!bfind)
  413. {
  414. goto END;
  415. }
  416. sprintf(tmp, "%s%s", dir, p_dfs_ext[i].file_name);
  417. p = hf_get_file_content(tmp, p_part_len);
  418. END:
  419. rt_free(p_dfs_ext);
  420. return p;
  421. #else
  422. char *p = NULL;
  423. char *cmd_ls[] = {"/app/m", "ls", dir, HF_MAX_MIN_101_TMP, NULL};
  424. char *envp[] = {NULL};
  425. char tmp[128];
  426. char tmp_filename[64];
  427. int cnt;
  428. int fd;
  429. int i;
  430. int ret;
  431. int addr;
  432. // 调用shell脚本,生成文件列表
  433. ret = call_usermodehelper("/app/m", cmd_ls, envp, UMH_WAIT_PROC);
  434. // 文件个数
  435. cnt = hf_get_filecnt_from_tmpfile(HF_MAX_MIN_101_TMP, HF_EXV_FILENAME_LEN);
  436. fd = sys_open(HF_MAX_MIN_101_TMP, O_RDONLY, 0);
  437. if (fd < 0)
  438. {
  439. return NULL;
  440. }
  441. for (i = 0; i < cnt; i++)
  442. {
  443. memset(tmp_filename, 0, sizeof(tmp_filename));
  444. ret = sys_read(fd, tmp_filename, HF_EXV_FILENAME_LEN + 1);
  445. if (ret != HF_EXV_FILENAME_LEN + 1)
  446. {
  447. sys_close(fd);
  448. return NULL;
  449. }
  450. tmp_filename[HF_EXV_FILENAME_LEN] = 0;
  451. addr = simple_strtoul(tmp_filename, NULL, 10);
  452. if (addr == filename)
  453. {
  454. sprintf(tmp, "%s%s", dir, tmp_filename);
  455. p = hf_get_file_content(tmp, p_part_len);
  456. break;
  457. }
  458. }
  459. sys_close(fd);
  460. rt_file_del(HF_MAX_MIN_101_TMP);
  461. return p;
  462. #endif
  463. }
  464. /******************************************************************************
  465. 函数名称: hf_str_to_ts
  466. 函数版本: 01.01
  467. 创建作者:
  468. 创建日期: 2016-08-09
  469. 函数说明: 字符串时间转struct timespec 结构时间
  470. 参数说明:
  471. p_strtime: 字符串时间(比如20141114_192135_830)
  472. p_ts: struct timespec时间
  473. 返回值: 返回0
  474. 修改记录:
  475. */
  476. int hf_str_to_ts(char *p_strtime, struct timespec *p_ts)
  477. {
  478. char tmp[8];
  479. struct rtc_time_t rtc;
  480. rt_printf("%s\r\n", p_strtime);
  481. memset(tmp, 0, sizeof(tmp));
  482. memcpy(tmp, p_strtime, 4);
  483. rtc.year = simple_strtoul(tmp, NULL, 10) - 2000;
  484. memset(tmp, 0, sizeof(tmp));
  485. memcpy(tmp, p_strtime + 4, 2);
  486. rtc.month = simple_strtoul(tmp, NULL, 10);
  487. memset(tmp, 0, sizeof(tmp));
  488. memcpy(tmp, p_strtime + 6, 2);
  489. rtc.day = simple_strtoul(tmp, NULL, 10);
  490. memset(tmp, 0, sizeof(tmp));
  491. memcpy(tmp, p_strtime + 9, 2);
  492. rtc.hour = simple_strtoul(tmp, NULL, 10);
  493. memset(tmp, 0, sizeof(tmp));
  494. memcpy(tmp, p_strtime + 11, 2);
  495. rtc.min = simple_strtoul(tmp, NULL, 10);
  496. memset(tmp, 0, sizeof(tmp));
  497. memcpy(tmp, p_strtime + 13, 2);
  498. rtc.ms = simple_strtoul(tmp, NULL, 10);
  499. rtc.ms *= 1000;
  500. memset(tmp, 0, sizeof(tmp));
  501. memcpy(tmp, p_strtime + 16, 3);
  502. rtc.ms += simple_strtoul(tmp, NULL, 10);
  503. rtc_to_timespec(&rtc, p_ts);
  504. return 0;
  505. }
  506. /******************************************************************************
  507. 函数名称: hf_gen_dfs_wave
  508. 函数版本: 01.01
  509. 创建作者:
  510. 创建日期: 2016-08-09
  511. 函数说明: 生成 dir_file_struct *指针(用于多文件的情况)
  512. 参数说明:
  513. dir: 目录
  514. file_name: 文件名(字符串)
  515. file_name_n: 文件名(数字)
  516. tmpfile: 临时文件 。主要是用于得到目录文件的个数和文件名称
  517. name_len: 名字长度。比如极值数据,112~141.它的长度就是3。比如定点数据22~51,它的长度就是2
  518. 返回值: 如果获取成功,则得到struct dir_file_struct 的指针。使用完毕应当释放内存。
  519. 如果获取失败,则返回NULL.
  520. 修改记录:
  521. */
  522. static struct dir_file_struct *hf_gen_dfs_wave(char *dir, char *tmpfile, int *p_file_cnt)
  523. {
  524. struct dir_file_struct *p_dfs = NULL;
  525. int ret;
  526. char tmp[128];
  527. struct stat s;
  528. int i;
  529. int len = HF_WAVE_FILE_LENGTH;
  530. ret = hf_get_filecnt_from_tmpfile(tmpfile, len);
  531. *p_file_cnt = ret;
  532. if (ret > 0)
  533. {
  534. int fd;
  535. char tmp2[128];
  536. int cnt;
  537. p_dfs = rt_malloc(ret * sizeof(struct dir_file_struct));
  538. if (!p_dfs)
  539. {
  540. return NULL;
  541. }
  542. memset(tmp, 0, sizeof(tmp));
  543. fd = sys_open(tmpfile, O_RDONLY, 0);
  544. if (fd < 0)
  545. {
  546. rt_printf("sys_open(%s) error\r\n", tmpfile);
  547. rt_free(p_dfs);
  548. return NULL;
  549. }
  550. cnt = ret;
  551. for (i = 0; i < cnt; i++)
  552. {
  553. ret = sys_read(fd, tmp, len + 1); // 包括换行符
  554. if (ret != (len + 1))
  555. {
  556. rt_printf("sys_read error\r\n");
  557. rt_free(p_dfs);
  558. sys_close(fd);
  559. }
  560. tmp[28] = 0;
  561. p_dfs[i].file_name = simple_strtoul(tmp, NULL, 16);
  562. sprintf(tmp2, "%s%s", dir, tmp);
  563. if (sys_newstat(tmp2, &s))
  564. {
  565. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmp2);
  566. rt_free(p_dfs);
  567. sys_close(fd);
  568. }
  569. p_dfs[i].file_size = s.st_size;
  570. p_dfs[i].file_time.tv_nsec = 0;
  571. hf_str_to_ts(tmp + 5, &p_dfs[i].file_time);
  572. }
  573. sys_close(fd);
  574. }
  575. return p_dfs;
  576. }
  577. /******************************************************************************
  578. 函数名称: hf_gen_dfs_ext
  579. 函数版本: 01.01
  580. 创建作者:
  581. 创建日期: 2016-08-09
  582. 函数说明: 生成 dir_file_struct *指针(用于多文件的情况)
  583. 参数说明:
  584. dir: 目录
  585. file_name: 文件名(字符串)
  586. file_name_n: 文件名(数字)
  587. tmpfile: 临时文件 。主要是用于得到目录文件的个数和文件名称
  588. name_len: 名字长度。比如极值数据,112~141.它的长度就是3。比如定点数据22~51,它的长度就是2
  589. 返回值: 如果获取成功,则得到struct dir_file_struct 的指针。使用完毕应当释放内存。
  590. 如果获取失败,则返回NULL.
  591. 修改记录:
  592. */
  593. #if 1
  594. static struct dir_file_struct *hf_gen_dfs_ext(char *dir, int *p_file_cnt, char *tmpfile, int filename_base, int filename_max)
  595. {
  596. struct dir_file_struct *p_dfs = NULL;
  597. struct dir_file_ext_struct *p_dfs_ext = NULL;
  598. int i = 0;
  599. p_dfs_ext = hf_get_dir_file_ext(dir, p_file_cnt, tmpfile);
  600. if (!p_dfs_ext)
  601. {
  602. return NULL;
  603. }
  604. p_dfs = rt_malloc((*p_file_cnt) * sizeof(struct dir_file_struct));
  605. if (!p_dfs)
  606. {
  607. rt_free(p_dfs_ext);
  608. return NULL;
  609. }
  610. for (i = 0; i < *p_file_cnt; i++)
  611. {
  612. if (((filename_base + i) >= filename_base) && ((filename_base + i) <= filename_max))
  613. {
  614. p_dfs[i].file_name = filename_base + i;
  615. p_dfs[i].file_size = p_dfs_ext[i].file_size;
  616. p_dfs[i].file_time = p_dfs_ext[i].file_time;
  617. strcpy(p_dfs[i].name, p_dfs_ext[i].file_name);
  618. }
  619. }
  620. rt_free(p_dfs_ext);
  621. return p_dfs;
  622. }
  623. #else
  624. static struct dir_file_struct *hf_gen_dfs_ext(char *dir, char *file_name, int file_name_n, char *tmpfile, int name_len, int *p_file_cnt)
  625. {
  626. struct dir_file_struct *p_dfs = NULL;
  627. int ret;
  628. char tmp[128];
  629. int len = strlen(file_name);
  630. struct stat s;
  631. int i;
  632. file_name_n = file_name_n;
  633. ret = hf_get_filecnt_from_tmpfile(tmpfile, len);
  634. *p_file_cnt = ret;
  635. if (ret > 0)
  636. {
  637. int fd;
  638. char tmp2[128];
  639. int cnt;
  640. p_dfs = rt_malloc(ret * sizeof(struct dir_file_struct));
  641. if (!p_dfs)
  642. {
  643. return NULL;
  644. }
  645. memset(tmp, 0, sizeof(tmp));
  646. fd = sys_open(tmpfile, O_RDONLY, 0);
  647. if (fd < 0)
  648. {
  649. rt_printf("sys_open(%s) error\r\n", tmpfile);
  650. rt_free(p_dfs);
  651. return NULL;
  652. }
  653. cnt = ret;
  654. for (i = 0; i < cnt; i++)
  655. {
  656. ret = sys_read(fd, tmp, len + name_len + 1); // 包括换行符
  657. if (ret != (len + name_len + 1))
  658. {
  659. rt_printf("sys_read error\r\n");
  660. rt_free(p_dfs);
  661. sys_close(fd);
  662. }
  663. tmp[len + name_len] = 0;
  664. p_dfs[i].file_name = simple_strtoul(tmp, NULL, 10);
  665. sprintf(tmp2, "%s%s", dir, tmp);
  666. if (sys_newstat(tmp2, &s))
  667. {
  668. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmp2);
  669. rt_free(p_dfs);
  670. sys_close(fd);
  671. }
  672. p_dfs[i].file_size = s.st_size;
  673. p_dfs[i].file_time.tv_nsec = 0;
  674. p_dfs[i].file_time.tv_sec = s.st_ctime + 8 * 60 * 60;
  675. }
  676. sys_close(fd);
  677. }
  678. return p_dfs;
  679. }
  680. #endif
  681. /******************************************************************************
  682. 函数名称: hf_gen_dfs
  683. 函数版本: 01.01
  684. 创建作者:
  685. 创建日期: 2016-08-09
  686. 函数说明: 生成 dir_file_struct *指针
  687. 参数说明:
  688. dir: 目录
  689. file_name: 文件名(字符串)
  690. file_name_n: 文件名(数字)
  691. 返回值: 如果获取成功,则得到struct dir_file_struct 的指针。使用完毕应当释放内存。
  692. 如果获取失败,则返回NULL.
  693. 修改记录:
  694. */
  695. static struct dir_file_struct *hf_gen_dfs(char *dir, char *file_name, int file_name_n)
  696. {
  697. struct dir_file_struct *p_dfs = NULL;
  698. struct stat s;
  699. char tmp[128];
  700. #if 0
  701. if(file_cnt==1)
  702. {
  703. #endif
  704. sprintf(tmp, "%s%s", dir, file_name);
  705. // 获取文件属性
  706. if (sys_newstat(tmp, &s))
  707. {
  708. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmp);
  709. return NULL;
  710. }
  711. p_dfs = rt_malloc(sizeof(struct dir_file_struct));
  712. if (!p_dfs)
  713. {
  714. return NULL;
  715. }
  716. p_dfs->file_name = file_name_n;
  717. p_dfs->file_size = s.st_size;
  718. p_dfs->file_time.tv_nsec = 0;
  719. p_dfs->file_time.tv_sec = s.st_ctime + 8 * 60 * 60;
  720. strcpy(p_dfs->name, file_name);
  721. #if 0
  722. }
  723. else
  724. {
  725. p_dfs=rt_malloc(sizeof(struct dir_file_struct)*file_cnt);
  726. if(!p_dfs)
  727. {
  728. return NULL;
  729. }
  730. for(i=0;i<file_cnt;i++)
  731. {
  732. sprintf(tmp, "%s%03d%s", dir, file_cnt, file_name);
  733. //获取文件属性
  734. if(sys_newstat(tmp,&s))
  735. {
  736. rt_printf("%s:sys_newstat(%s) error\n",__func__, tmp);
  737. return NULL;
  738. }
  739. p_dfs[i].file_name = file_name_n+i;
  740. p_dfs[i].file_size=s.st_size;
  741. p_dfs[i].file_time.tv_nsec=0;
  742. p_dfs[i].file_time.tv_sec=s.st_ctime+8*60*60;
  743. }
  744. }
  745. #endif
  746. return p_dfs;
  747. }
  748. /******************************************************************************
  749. 函数名称: hf_get_file_len
  750. 函数版本: 01.01
  751. 创建作者:
  752. 创建日期: 2016-08-09
  753. 函数说明: 获取文件长度
  754. 参数说明:
  755. filename:文件名
  756. 返回值: 返回文件长度.
  757. 修改记录:
  758. */
  759. static int hf_get_file_len(char *filename)
  760. {
  761. struct stat s;
  762. // 获取文件属性
  763. if (sys_newstat(filename, &s))
  764. {
  765. rt_printf("%s:sys_newstat(%s) error\n", __func__, filename);
  766. return 0;
  767. }
  768. return (int)s.st_size;
  769. }
  770. /******************************************************************************
  771. 函数名称: hf_get_dir_file
  772. 函数版本: 01.01
  773. 创建作者:
  774. 创建日期: 2016-08-09
  775. 函数说明: 获取目录文件.
  776. 参数说明:
  777. dir: 目录
  778. p_file_cnt: 返回的文件个数
  779. 返回值: 如果获取成功,则得到*p_file_cnt 个struct dir_file_struct 的指针。使用完毕应当释放内存。
  780. 如果获取失败,则返回NULL.
  781. 修改记录:
  782. */
  783. struct dir_file_struct *hf_get_dir_file(char *dir, int *p_file_cnt)
  784. {
  785. char *m_path = "/app/m";
  786. #ifdef CPU_FUXI
  787. char tmp[128];
  788. #endif
  789. int ret;
  790. int len;
  791. char *cmd_ls[] = {m_path, "ls", dir, NULL, NULL};
  792. char *envp[] = {NULL};
  793. struct dir_file_struct *p_dfs = NULL;
  794. len = strlen(dir);
  795. // SOE文件(101)
  796. if (strncmp(dir, HF_SOE_101_DIR, len) == 0)
  797. {
  798. *p_file_cnt = 1;
  799. p_dfs = hf_gen_dfs(HF_SOE_101_DIR, HF_SOE_FILENAME, HF_SOE_FILENAME_N);
  800. }
  801. else if (strncmp(dir, HF_CO_101_DIR, len) == 0) // yk文件(101)
  802. {
  803. *p_file_cnt = 1;
  804. p_dfs = hf_gen_dfs(HF_CO_101_DIR, HF_YK_FILENAME, HF_YK_FILENAME_N);
  805. }
  806. else if (strncmp(dir, HF_FIXPT_101_DIR, len) == 0) // 定点文件(101)
  807. {
  808. #ifdef CPU_FUXI
  809. memset(tmp, 0, sizeof(tmp));
  810. sprintf(tmp, " ls %s > %s", dir, HF_TIMED_101_TMP);
  811. system(tmp);
  812. #else
  813. cmd_ls[3] = HF_TIMED_101_TMP;
  814. // 调用shell脚本,生成文件列表
  815. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  816. #endif
  817. p_dfs = hf_gen_dfs_ext(HF_FIXPT_101_DIR, p_file_cnt, HF_TIMED_101_TMP, HF_FIXPT_FILENAME_N, HF_FIXPT_FILENAME_N_MAX);
  818. rt_file_del(HF_TIMED_101_TMP);
  819. }
  820. else if (strncmp(dir, HF_EXV_101_DIR, len) == 0) // 极值文件(101)
  821. {
  822. #ifdef CPU_FUXI
  823. memset(tmp, 0, sizeof(tmp));
  824. sprintf(tmp, " ls %s > %s", dir, HF_MAX_MIN_101_TMP);
  825. system(tmp);
  826. #else
  827. cmd_ls[3] = HF_MAX_MIN_101_TMP;
  828. // 调用shell脚本,生成文件列表
  829. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  830. #endif
  831. p_dfs = hf_gen_dfs_ext(HF_EXV_101_DIR, p_file_cnt, HF_MAX_MIN_101_TMP, HF_EXV_FILENAME_N, HF_EXV_FILENAME_N_MAX);
  832. rt_file_del(HF_MAX_MIN_101_TMP);
  833. }
  834. else if (strncmp(dir, HF_FRZ_101_DIR, len) == 0) // 定点文件(101)
  835. {
  836. #ifdef CPU_FUXI
  837. memset(tmp, 0, sizeof(tmp));
  838. sprintf(tmp, " ls %s > %s", dir, HF_FRZ_101_TMP);
  839. system(tmp);
  840. #else
  841. cmd_ls[3] = HF_FRZ_101_TMP;
  842. // 调用shell脚本,生成文件列表
  843. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  844. #endif
  845. p_dfs = hf_gen_dfs_ext(HF_FRZ_101_DIR, p_file_cnt, HF_FRZ_101_TMP, HF_FRZ_FILENAME_N, HF_FRZ_FILENAME_N_MAX);
  846. rt_file_del(HF_FRZ_101_TMP);
  847. }
  848. else if (strncmp(dir, HF_FLOWREV_101_DIR, len) == 0)
  849. {
  850. *p_file_cnt = 1;
  851. p_dfs = hf_gen_dfs(HF_FLOWREV_101_DIR, HF_FLOWREV_FILENAME, HF_FLOWREV_FILENAME_N);
  852. }
  853. else if (strncmp(dir, HF_LOG_101_DIR, len) == 0)
  854. {
  855. *p_file_cnt = 1;
  856. p_dfs = hf_gen_dfs(HF_LOG_101_DIR, HF_LOG_FILENAME, HF_LOG_FILENAME_N);
  857. }
  858. else if (strncmp(dir, HF_WAVE_DIR, len) == 0) // 录波文件
  859. {
  860. #ifdef CPU_FUXI
  861. memset(tmp, 0, sizeof(tmp));
  862. sprintf(tmp, " ls %s > %s", dir, HF_WAVE_TMP);
  863. system(tmp);
  864. #else
  865. cmd_ls[3] = HF_WAVE_TMP;
  866. // 调用shell脚本,生成文件列表
  867. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  868. #endif
  869. p_dfs = hf_gen_dfs_wave(HF_WAVE_DIR, HF_WAVE_TMP, p_file_cnt);
  870. rt_file_del(HF_WAVE_TMP);
  871. }
  872. return p_dfs;
  873. }
  874. /******************************************************************************
  875. 函数名称: hf_get_max_minfile_len
  876. 函数版本: 01.01
  877. 创建作者:
  878. 创建日期: 2016-08-09
  879. 函数说明: 获取极值文件大小
  880. 参数说明:
  881. dir: 目录
  882. file_name: 文件名
  883. 返回值: 返回文件长度
  884. 修改记录:
  885. */
  886. int hf_get_max_minfile_len(char *dir, int file_name)
  887. {
  888. #if 1
  889. int len = 0;
  890. struct dir_file_ext_struct *p_dfs_ext = NULL;
  891. int i = 0;
  892. int file_cnt = 0;
  893. char tmp[128];
  894. int bfind = 0;
  895. p_dfs_ext = hf_get_dir_file_ext(dir, &file_cnt, HF_MAX_MIN_101_TMP);
  896. if (!p_dfs_ext)
  897. {
  898. return 0;
  899. }
  900. for (i = 0; i < file_cnt; i++)
  901. {
  902. if (file_name == (i + HF_EXV_FILENAME_N))
  903. {
  904. bfind = 1;
  905. break;
  906. }
  907. }
  908. if (!bfind)
  909. {
  910. goto END;
  911. }
  912. sprintf(tmp, "%s%s", dir, p_dfs_ext[i].file_name);
  913. len = hf_get_file_len(tmp);
  914. END:
  915. rt_free(p_dfs_ext);
  916. return len;
  917. #else
  918. char *cmd_ls[] = {"/app/m", "ls", dir, HF_MAX_MIN_101_TMP, NULL};
  919. char *envp[] = {NULL};
  920. char tmp[128];
  921. char tmp_filename[64];
  922. int cnt;
  923. int fd;
  924. int i;
  925. int ret;
  926. int addr;
  927. int len = 0;
  928. // 调用shell脚本,生成文件列表
  929. ret = call_usermodehelper("/app/m", cmd_ls, envp, UMH_WAIT_PROC);
  930. // 文件个数
  931. cnt = hf_get_filecnt_from_tmpfile(HF_MAX_MIN_101_TMP, HF_EXV_FILENAME_LEN);
  932. fd = sys_open(HF_MAX_MIN_101_TMP, O_RDONLY, 0);
  933. if (fd < 0)
  934. {
  935. return 0;
  936. }
  937. for (i = 0; i < cnt; i++)
  938. {
  939. memset(tmp_filename, 0, sizeof(tmp_filename));
  940. ret = sys_read(fd, tmp_filename, HF_EXV_FILENAME_LEN + 1);
  941. if (ret != HF_EXV_FILENAME_LEN + 1)
  942. {
  943. sys_close(fd);
  944. return 0;
  945. }
  946. tmp_filename[HF_EXV_FILENAME_LEN] = 0;
  947. addr = simple_strtoul(tmp_filename, NULL, 10);
  948. if (addr == file_name)
  949. {
  950. sprintf(tmp, "%s%s", dir, tmp_filename);
  951. len = hf_get_file_len(tmp);
  952. break;
  953. }
  954. }
  955. sys_close(fd);
  956. rt_file_del(HF_MAX_MIN_101_TMP);
  957. return len;
  958. #endif
  959. }
  960. /******************************************************************************
  961. 函数名称: hf_get_wavefile_len
  962. 函数版本: 01.01
  963. 创建作者:
  964. 创建日期: 2016-08-09
  965. 函数说明: 获取录波文件大小
  966. 参数说明:
  967. dir: 目录
  968. file_name: 文件名
  969. 返回值: 返回文件长度
  970. 修改记录:
  971. */
  972. int hf_get_wavefile_len(char *dir, int file_name)
  973. {
  974. char *m_path = "/app/m";
  975. char *cmd_ls[] = {m_path, "ls", dir, HF_WAVE_TMP, NULL};
  976. char *envp[] = {NULL};
  977. char tmp[128];
  978. char tmp_filename[64];
  979. int cnt;
  980. int fd;
  981. int i;
  982. int ret;
  983. int addr;
  984. int len = 0;
  985. int cfg_dat = 0;
  986. #ifdef CPU_FUXI
  987. memset(tmp, 0, sizeof(tmp));
  988. sprintf(tmp, " ls %s > %s", dir, HF_WAVE_TMP);
  989. system(tmp);
  990. #else
  991. // 调用shell脚本,生成文件列表
  992. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  993. #endif
  994. // 文件个数
  995. cnt = hf_get_filecnt_from_tmpfile(HF_WAVE_TMP, HF_WAVE_FILE_LENGTH);
  996. fd = sys_open(HF_WAVE_TMP, O_RDONLY, 0);
  997. if (fd < 0)
  998. {
  999. return 0;
  1000. }
  1001. for (i = 0; i < cnt; i++)
  1002. {
  1003. memset(tmp_filename, 0, sizeof(tmp_filename));
  1004. ret = sys_read(fd, tmp_filename, HF_WAVE_FILE_LENGTH + 1);
  1005. if (ret != HF_WAVE_FILE_LENGTH + 1)
  1006. {
  1007. sys_close(fd);
  1008. return 0;
  1009. }
  1010. tmp_filename[HF_WAVE_FILE_LENGTH] = 0;
  1011. addr = simple_strtoul(tmp_filename, NULL, 16);
  1012. if (addr == file_name)
  1013. {
  1014. cfg_dat++;
  1015. sprintf(tmp, "%s%s", dir, tmp_filename);
  1016. len += hf_get_file_len(tmp);
  1017. if (cfg_dat == 2)
  1018. break;
  1019. }
  1020. }
  1021. sys_close(fd);
  1022. rt_file_del(HF_WAVE_TMP);
  1023. return len;
  1024. }
  1025. /******************************************************************************
  1026. 函数名称: hf_get_timedfile_len
  1027. 函数版本: 01.01
  1028. 创建作者:
  1029. 创建日期: 2016-08-09
  1030. 函数说明: 获取定点文件大小
  1031. 参数说明:
  1032. dir: 目录
  1033. file_name: 文件名
  1034. 返回值: 返回文件长度
  1035. 修改记录:
  1036. */
  1037. int hf_get_timedfile_len(char *dir, int file_name, int *partnum)
  1038. {
  1039. #if 1
  1040. char *p = NULL;
  1041. int len = 0;
  1042. struct dir_file_ext_struct *p_dfs_ext = NULL;
  1043. int i = 0;
  1044. int file_cnt = 0;
  1045. char tmp[128];
  1046. int bfind = 0;
  1047. int filelen = 0;
  1048. p_dfs_ext = hf_get_dir_file_ext(dir, &file_cnt, HF_TIMED_101_TMP);
  1049. if (!p_dfs_ext)
  1050. {
  1051. return 0;
  1052. }
  1053. for (i = 0; i < file_cnt; i++)
  1054. {
  1055. if (file_name == (i + HF_FIXPT_FILENAME_N))
  1056. {
  1057. bfind = 1;
  1058. break;
  1059. }
  1060. }
  1061. if (!bfind)
  1062. {
  1063. goto END;
  1064. }
  1065. sprintf(tmp, "%s%s", dir, p_dfs_ext[i].file_name);
  1066. len = hf_get_file_len(tmp);
  1067. p = hf_get_file_content(tmp, &filelen);
  1068. if (p)
  1069. {
  1070. int linecnt = 0;
  1071. char *pp = p;
  1072. char cr_line[] = {'\r', '\n', 0};
  1073. while (pp)
  1074. {
  1075. pp = strstr(pp, cr_line);
  1076. if (!pp)
  1077. {
  1078. break;
  1079. }
  1080. linecnt++;
  1081. pp += 2;
  1082. }
  1083. linecnt -= 2;
  1084. if (linecnt < 0)
  1085. {
  1086. linecnt = 0;
  1087. }
  1088. *partnum = linecnt;
  1089. rt_free(p);
  1090. }
  1091. END:
  1092. rt_free(p_dfs_ext);
  1093. return len;
  1094. #else
  1095. char *p = NULL;
  1096. char *cmd_ls[] = {"/app/m", "ls", dir, HF_TIMED_101_TMP, NULL};
  1097. char *envp[] = {NULL};
  1098. char tmp[128];
  1099. char tmp_filename[64];
  1100. int cnt;
  1101. int fd;
  1102. int i;
  1103. int ret;
  1104. int addr;
  1105. int len = 0;
  1106. int filelen;
  1107. // 调用shell脚本,生成文件列表
  1108. ret = call_usermodehelper("/app/m", cmd_ls, envp, UMH_WAIT_PROC);
  1109. // 文件个数
  1110. cnt = hf_get_filecnt_from_tmpfile(HF_TIMED_101_TMP, HF_FIXPT_FILENAME_LEN);
  1111. fd = sys_open(HF_TIMED_101_TMP, O_RDONLY, 0);
  1112. if (fd < 0)
  1113. {
  1114. return 0;
  1115. }
  1116. for (i = 0; i < cnt; i++)
  1117. {
  1118. memset(tmp_filename, 0, sizeof(tmp_filename));
  1119. ret = sys_read(fd, tmp_filename, HF_FIXPT_FILENAME_LEN + 1);
  1120. if (ret != HF_FIXPT_FILENAME_LEN + 1)
  1121. {
  1122. sys_close(fd);
  1123. return 0;
  1124. }
  1125. tmp_filename[HF_FIXPT_FILENAME_LEN] = 0;
  1126. addr = simple_strtoul(tmp_filename, NULL, 10);
  1127. if (addr == file_name)
  1128. {
  1129. sprintf(tmp, "%s%s", dir, tmp_filename);
  1130. len = hf_get_file_len(tmp);
  1131. p = hf_get_file_content(tmp, &filelen);
  1132. if (p)
  1133. {
  1134. int linecnt = 0;
  1135. char *pp = p;
  1136. char cr_line[] = {'\r', '\n', 0};
  1137. while (pp)
  1138. {
  1139. pp = strstr(pp, cr_line);
  1140. if (!pp)
  1141. {
  1142. break;
  1143. }
  1144. linecnt++;
  1145. pp += 2;
  1146. }
  1147. linecnt -= 2;
  1148. if (linecnt < 0)
  1149. {
  1150. linecnt = 0;
  1151. }
  1152. *partnum = linecnt;
  1153. rt_free(p);
  1154. }
  1155. break;
  1156. }
  1157. }
  1158. sys_close(fd);
  1159. rt_file_del(HF_TIMED_101_TMP);
  1160. return len;
  1161. #endif
  1162. }
  1163. int hf_get_frzfile_len(char *dir, int file_name, int *partnum)
  1164. {
  1165. char *p = NULL;
  1166. int len = 0;
  1167. struct dir_file_ext_struct *p_dfs_ext = NULL;
  1168. int i = 0;
  1169. int file_cnt = 0;
  1170. char tmp[128];
  1171. int bfind = 0;
  1172. int filelen = 0;
  1173. p_dfs_ext = hf_get_dir_file_ext(dir, &file_cnt, HF_FRZ_101_TMP);
  1174. if (!p_dfs_ext)
  1175. {
  1176. return 0;
  1177. }
  1178. for (i = 0; i < file_cnt; i++)
  1179. {
  1180. if (file_name == (i + HF_FRZ_FILENAME_N))
  1181. {
  1182. bfind = 1;
  1183. break;
  1184. }
  1185. }
  1186. if (!bfind)
  1187. {
  1188. goto END;
  1189. }
  1190. sprintf(tmp, "%s%s", dir, p_dfs_ext[i].file_name);
  1191. len = hf_get_file_len(tmp);
  1192. p = hf_get_file_content(tmp, &filelen);
  1193. if (p)
  1194. {
  1195. int linecnt = 0;
  1196. char *pp = p;
  1197. char cr_line[] = {'\r', '\n', 0};
  1198. while (pp)
  1199. {
  1200. pp = strstr(pp, cr_line);
  1201. if (!pp)
  1202. {
  1203. break;
  1204. }
  1205. linecnt++;
  1206. pp += 2;
  1207. }
  1208. linecnt -= 2;
  1209. if (linecnt < 0)
  1210. {
  1211. linecnt = 0;
  1212. }
  1213. *partnum = linecnt;
  1214. rt_free(p);
  1215. }
  1216. END:
  1217. rt_free(p_dfs_ext);
  1218. return len;
  1219. }
  1220. /******************************************************************************
  1221. 函数名称: hf_get_file_part
  1222. 函数版本: 01.01
  1223. 创建作者:
  1224. 创建日期: 2016-08-09
  1225. 函数说明: 获取特定目录下某个文件节的信息
  1226. 参数说明:
  1227. dir: 目录
  1228. file_name: 文件名
  1229. part: 节名
  1230. part_len: 节长度
  1231. 返回值: 如果获取成功,则得到节内容和节长度
  1232. 如果获取失败,则返回NULL.
  1233. 修改记录:
  1234. */
  1235. char *hf_get_file_part(char *dir, int file_name, int part, int *part_len)
  1236. {
  1237. char *p = NULL;
  1238. char tmp[128];
  1239. int len = strlen(dir);
  1240. if (strncmp(dir, HF_SOE_101_DIR, len) == 0)
  1241. {
  1242. sprintf(tmp, "%s%s", dir, HF_SOE_FILENAME);
  1243. p = hf_get_file_content(tmp, part_len);
  1244. }
  1245. else if (strncmp(dir, HF_CO_101_DIR, len) == 0)
  1246. {
  1247. sprintf(tmp, "%s%s", dir, HF_YK_FILENAME);
  1248. p = hf_get_file_content(tmp, part_len);
  1249. }
  1250. else if (strncmp(dir, HF_EXV_101_DIR, len) == 0)
  1251. {
  1252. // sprintf(tmp, "%s%03d%s", dir, file_name, HF_EXV_FILENAME);
  1253. // p=hf_get_file_content(tmp, part_len);
  1254. p = hf_get_max_minfile_content(dir, file_name, part, part_len);
  1255. }
  1256. else if (strncmp(dir, HF_FIXPT_101_DIR, len) == 0)
  1257. {
  1258. p = hf_get_timedfile_content(dir, file_name, part, part_len);
  1259. }
  1260. else if (strncmp(dir, HF_FRZ_101_DIR, len) == 0)
  1261. {
  1262. p = hf_get_frzfile_content(dir, file_name, part, part_len);
  1263. }
  1264. else if (strncmp(dir, HF_FLOWREV_101_DIR, len) == 0)
  1265. {
  1266. sprintf(tmp, "%s%s", dir, HF_FLOWREV_FILENAME);
  1267. p = hf_get_file_content(tmp, part_len);
  1268. }
  1269. else if (strncmp(dir, HF_LOG_101_DIR, len) == 0)
  1270. {
  1271. sprintf(tmp, "%s%s", dir, HF_LOG_FILENAME);
  1272. p = hf_get_file_content(tmp, part_len);
  1273. }
  1274. else if (strncmp(dir, HF_WAVE_DIR, len) == 0)
  1275. {
  1276. p = hf_get_wavefile_content(dir, file_name, part, part_len);
  1277. }
  1278. return p;
  1279. }
  1280. /******************************************************************************
  1281. 函数名称: hf_get_file_inf
  1282. 函数版本: 01.01
  1283. 创建作者:
  1284. 创建日期: 2016-08-09
  1285. 函数说明: 获取目录文件.
  1286. 参数说明:
  1287. dir: 目录
  1288. file_name: 文件名
  1289. partnum: 节个数
  1290. 返回值: 如果获取成功,则返回 文件长度、节个数
  1291. 如果获取失败,则返回NULL.
  1292. 修改记录:
  1293. */
  1294. int hf_get_file_inf(char *dir, int file_name, int *partnum)
  1295. {
  1296. char tmp[128];
  1297. int len = strlen(dir);
  1298. int ret = 0;
  1299. if (strncmp(dir, HF_SOE_101_DIR, len) == 0)
  1300. {
  1301. sprintf(tmp, "%s%s", dir, HF_SOE_FILENAME);
  1302. ret = hf_get_file_len(tmp);
  1303. *partnum = 1;
  1304. }
  1305. else if (strncmp(dir, HF_CO_101_DIR, len) == 0)
  1306. {
  1307. sprintf(tmp, "%s%s", dir, HF_YK_FILENAME);
  1308. ret = hf_get_file_len(tmp);
  1309. *partnum = 1;
  1310. }
  1311. else if (strncmp(dir, HF_EXV_101_DIR, len) == 0)
  1312. {
  1313. ret = hf_get_max_minfile_len(dir, file_name);
  1314. *partnum = 1;
  1315. }
  1316. else if (strncmp(dir, HF_FIXPT_101_DIR, len) == 0)
  1317. {
  1318. ret = hf_get_timedfile_len(dir, file_name, partnum);
  1319. }
  1320. else if (strncmp(dir, HF_FRZ_101_DIR, len) == 0)
  1321. {
  1322. ret = hf_get_frzfile_len(dir, file_name, partnum);
  1323. }
  1324. else if (strncmp(dir, HF_FLOWREV_101_DIR, len) == 0)
  1325. {
  1326. sprintf(tmp, "%s%s", dir, HF_FLOWREV_FILENAME);
  1327. ret = hf_get_file_len(tmp);
  1328. *partnum = 1;
  1329. }
  1330. else if (strncmp(dir, HF_LOG_101_DIR, len) == 0)
  1331. {
  1332. sprintf(tmp, "%s%s", dir, HF_LOG_FILENAME);
  1333. ret = hf_get_file_len(tmp);
  1334. *partnum = 1;
  1335. }
  1336. else if (strncmp(dir, HF_WAVE_DIR, len) == 0)
  1337. {
  1338. ret = hf_get_wavefile_len(dir, file_name);
  1339. *partnum = 2;
  1340. }
  1341. return ret;
  1342. }
  1343. #define DIR_FILE_MAX 256
  1344. bool check_dir_enable(char *namestr)
  1345. {
  1346. if ((strstr(namestr, "EXV") != NULL) || (strstr(namestr, "FIXPT") != NULL) || (strstr(namestr, "FLOWREV") != NULL) || (strstr(namestr, "FRZ") != NULL))
  1347. {
  1348. return false;
  1349. }
  1350. else
  1351. {
  1352. return true;
  1353. }
  1354. }
  1355. void hf_get_dir_file_ext_depth(char *dir, int *p_file_cnt, char *tmpfile, struct dir_file_ext_struct *p_dfs)
  1356. {
  1357. char *m_path = "/app/m";
  1358. char *cmd_ls[] = {m_path, "ls", dir, tmpfile, NULL};
  1359. char *envp[] = {NULL};
  1360. int fd;
  1361. struct stat s;
  1362. char *p = NULL;
  1363. char *pp;
  1364. char *pp2;
  1365. char end_A[] = {0x0A, 0};
  1366. int ret;
  1367. int file_cnt = 0;
  1368. int i = 0;
  1369. char tmp[128];
  1370. int index = 0;
  1371. if (check_dir_enable(dir) == false)
  1372. return;
  1373. // 调用shell脚本,生成文件列表
  1374. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  1375. // 获取文件属性
  1376. if (sys_newstat(tmpfile, &s))
  1377. {
  1378. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmpfile);
  1379. rt_file_del(tmpfile);
  1380. return;
  1381. }
  1382. if (s.st_size == 0)
  1383. {
  1384. rt_file_del(tmpfile);
  1385. return;
  1386. }
  1387. p = rt_malloc(s.st_size + 1);
  1388. if (!p)
  1389. {
  1390. rt_file_del(tmpfile);
  1391. return;
  1392. }
  1393. fd = sys_open(tmpfile, O_RDONLY, 0);
  1394. if (fd < 0)
  1395. {
  1396. rt_free(p);
  1397. rt_file_del(tmpfile);
  1398. return;
  1399. }
  1400. memset(p, 0, s.st_size + 1);
  1401. ret = sys_read(fd, p, s.st_size);
  1402. sys_close(fd);
  1403. // rt_file_del(tmpfile);
  1404. if (ret != s.st_size)
  1405. {
  1406. rt_printf("%s:sys_read error\n", __func__);
  1407. rt_free(p);
  1408. return;
  1409. }
  1410. pp = p;
  1411. // 求目录下的文件个数
  1412. while (pp)
  1413. {
  1414. pp = strstr(pp, end_A);
  1415. if (!pp)
  1416. {
  1417. break;
  1418. }
  1419. pp++;
  1420. file_cnt++;
  1421. }
  1422. if (file_cnt <= 0)
  1423. {
  1424. rt_free(p);
  1425. return;
  1426. }
  1427. pp = p;
  1428. for (i = 0; i < file_cnt; i++)
  1429. {
  1430. pp2 = strstr(pp, end_A);
  1431. if (!pp2)
  1432. {
  1433. break;
  1434. }
  1435. index = *p_file_cnt;
  1436. if ((index < DIR_FILE_MAX) && ((pp2 - pp) < 64))
  1437. {
  1438. memcpy(p_dfs[index].file_name, pp, pp2 - pp);
  1439. p_dfs[index].file_name[pp2 - pp] = '\0'; // 增加结束
  1440. sprintf(tmp, "%s/%s", dir, p_dfs[index].file_name);
  1441. // 获取文件属性
  1442. if (sys_newstat(tmp, &s))
  1443. {
  1444. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmp);
  1445. rt_free(p);
  1446. return;
  1447. }
  1448. if (S_ISDIR(s.st_mode)) // 目录文件
  1449. {
  1450. hf_get_dir_file_ext_depth(tmp, p_file_cnt, tmpfile, p_dfs);
  1451. }
  1452. else
  1453. {
  1454. p_dfs[index].file_size = s.st_size;
  1455. p_dfs[index].file_time.tv_nsec = 0;
  1456. p_dfs[index].file_time.tv_sec = s.st_ctime + 8 * 60 * 60;
  1457. *p_file_cnt = index + 1;
  1458. rt_printf("文件数为*p_file_cnt = %d\r\n", *p_file_cnt);
  1459. }
  1460. }
  1461. pp2++;
  1462. pp = pp2;
  1463. }
  1464. rt_free(p);
  1465. }
  1466. struct dir_file_ext_struct *hf_get_dir_file_ext_all(char *dir, int *p_file_cnt, char *tmpfile)
  1467. {
  1468. struct dir_file_ext_struct *p_dfs = NULL;
  1469. int p_file_cnt1 = 0;
  1470. p_dfs = rt_malloc(DIR_FILE_MAX * sizeof(struct dir_file_ext_struct));
  1471. if (!p_dfs)
  1472. {
  1473. rt_printf("hf_get_dir_file_ext_all malloc fail\r\n");
  1474. return NULL;
  1475. }
  1476. memset(p_dfs, 0, sizeof(DIR_FILE_MAX * sizeof(struct dir_file_ext_struct)));
  1477. hf_get_dir_file_ext_depth(dir, &p_file_cnt1, tmpfile, p_dfs);
  1478. *p_file_cnt = p_file_cnt1;
  1479. return p_dfs;
  1480. }
  1481. /******************************************************************************
  1482. 函数名称: hf_get_dir_file_ext
  1483. 函数版本: 01.01
  1484. 创建作者:
  1485. 创建日期: 2016-08-09
  1486. 函数说明: 获取目录文件.
  1487. 参数说明:
  1488. dir: 目录
  1489. p_file_cnt: 返回的文件个数
  1490. tmpfile: 指定一个临时文件
  1491. 返回值: 如果获取成功,则得到*p_file_cnt 个struct dir_file_ext_struct 的指针。使用完毕应当释放内存。
  1492. 如果获取失败,则返回NULL.
  1493. 修改记录:
  1494. */
  1495. struct dir_file_ext_struct *hf_get_dir_file_ext(char *dir, int *p_file_cnt, char *tmpfile)
  1496. {
  1497. char *m_path = "/app/m";
  1498. char *cmd_ls[] = {m_path, "ls", dir, tmpfile, NULL};
  1499. char *envp[] = {NULL};
  1500. struct dir_file_ext_struct *p_dfs = NULL;
  1501. int fd;
  1502. struct stat s;
  1503. char *p = NULL;
  1504. char *pp;
  1505. char *pp2;
  1506. char end_A[] = {0x0A, 0};
  1507. int ret;
  1508. int file_cnt = 0;
  1509. int i = 0;
  1510. char tmp[128];
  1511. #ifdef CPU_FUXI
  1512. memset(tmp, 0, sizeof(tmp));
  1513. sprintf(tmp, " ls %s > %s", dir, tmpfile);
  1514. system(tmp);
  1515. #else
  1516. // 调用shell脚本,生成文件列表
  1517. ret = call_usermodehelper(m_path, cmd_ls, envp, UMH_WAIT_PROC);
  1518. #endif
  1519. // 获取文件属性
  1520. if (sys_newstat(tmpfile, &s))
  1521. {
  1522. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmpfile);
  1523. rt_file_del(tmpfile);
  1524. return NULL;
  1525. }
  1526. if (s.st_size == 0)
  1527. {
  1528. rt_file_del(tmpfile);
  1529. return NULL;
  1530. }
  1531. p = rt_malloc(s.st_size + 1);
  1532. if (!p)
  1533. {
  1534. rt_file_del(tmpfile);
  1535. return NULL;
  1536. }
  1537. fd = sys_open(tmpfile, O_RDONLY, 0);
  1538. if (fd < 0)
  1539. {
  1540. rt_free(p);
  1541. rt_file_del(tmpfile);
  1542. return NULL;
  1543. }
  1544. memset(p, 0, s.st_size + 1);
  1545. ret = sys_read(fd, p, s.st_size);
  1546. sys_close(fd);
  1547. rt_file_del(tmpfile);
  1548. if (ret != s.st_size)
  1549. {
  1550. rt_printf("%s:sys_read error\n", __func__);
  1551. rt_free(p);
  1552. return NULL;
  1553. }
  1554. pp = p;
  1555. // 求目录下的文件个数
  1556. while (pp)
  1557. {
  1558. pp = strstr(pp, end_A);
  1559. if (!pp)
  1560. {
  1561. break;
  1562. }
  1563. pp++;
  1564. file_cnt++;
  1565. }
  1566. *p_file_cnt = file_cnt;
  1567. if (file_cnt <= 0)
  1568. {
  1569. rt_free(p);
  1570. return NULL;
  1571. }
  1572. p_dfs = rt_malloc(file_cnt * sizeof(struct dir_file_ext_struct));
  1573. if (!p_dfs)
  1574. {
  1575. rt_free(p);
  1576. return NULL;
  1577. }
  1578. pp = p;
  1579. memset(p_dfs, 0, file_cnt * sizeof(struct dir_file_ext_struct));
  1580. for (i = 0; i < file_cnt; i++)
  1581. {
  1582. pp2 = strstr(pp, end_A);
  1583. if (!pp2)
  1584. {
  1585. break;
  1586. }
  1587. memcpy(p_dfs[i].file_name, pp, pp2 - pp);
  1588. sprintf(tmp, "%s%s", dir, p_dfs[i].file_name);
  1589. // 获取文件属性
  1590. if (sys_newstat(tmp, &s))
  1591. {
  1592. rt_printf("%s:sys_newstat(%s) error\n", __func__, tmp);
  1593. rt_free(p);
  1594. rt_free(p_dfs);
  1595. return NULL;
  1596. }
  1597. p_dfs[i].file_size = s.st_size;
  1598. p_dfs[i].file_time.tv_nsec = 0;
  1599. p_dfs[i].file_time.tv_sec = s.st_ctime + 8 * 60 * 60;
  1600. pp2++;
  1601. pp = pp2;
  1602. }
  1603. rt_free(p);
  1604. return p_dfs;
  1605. }
  1606. /******************************下面是国网最新标准文件传输的内容*********************************/
  1607. /* *********************************************************************
  1608. 获取目录信息
  1609. id :目录ID 暂时不知怎么用
  1610. dir :目录名称,若为空,则为默认目录
  1611. cf: =0 全部目录 ==1 满足搜索条件的信息
  1612. ts te 搜索开始及结束时间
  1613. */
  1614. struct dir_file_struct *hf_get_dir_file_n(uint32_t id, char *dir, char cf, struct rtc_time_t *ts, struct rtc_time_t *te, int *p_file_cnt)
  1615. {
  1616. struct dir_file_ext_struct *p_dfes;
  1617. struct dir_file_struct *p_dfs;
  1618. struct timespec ts1, ts2;
  1619. int cnt;
  1620. int i, j;
  1621. id = id;
  1622. if (!dir)
  1623. {
  1624. p_dfes = hf_get_dir_file_ext(HF_DEFAULT_PATH, p_file_cnt, "/tmp/gdfn.tmp");
  1625. }
  1626. else
  1627. {
  1628. #ifdef FUN_DIR_DEPTH
  1629. if (strstr(dir, "HISTORY"))
  1630. {
  1631. p_dfes = hf_get_dir_file_ext_all(dir, p_file_cnt, "/tmp/gdfn.tmp");
  1632. }
  1633. else
  1634. {
  1635. p_dfes = hf_get_dir_file_ext(dir, p_file_cnt, "/tmp/gdfn.tmp");
  1636. }
  1637. #else
  1638. p_dfes = hf_get_dir_file_ext(dir, p_file_cnt, "/tmp/gdfn.tmp");
  1639. #endif
  1640. }
  1641. if (!p_dfes)
  1642. {
  1643. return NULL;
  1644. }
  1645. if (cf == 0)
  1646. {
  1647. p_dfs = rt_malloc((*p_file_cnt) * sizeof(struct dir_file_struct));
  1648. if (!p_dfs)
  1649. {
  1650. rt_printf("%s: rt_malloc error \n", __func__);
  1651. rt_free(p_dfes);
  1652. return NULL;
  1653. }
  1654. for (i = 0; i < *p_file_cnt; i++)
  1655. {
  1656. memcpy(p_dfs[i].name, p_dfes[i].file_name, 64);
  1657. p_dfs[i].file_size = p_dfes[i].file_size;
  1658. p_dfs[i].file_time = p_dfes[i].file_time;
  1659. }
  1660. }
  1661. else
  1662. {
  1663. int c1, c2;
  1664. rtc_to_timespec(ts, &ts1);
  1665. rtc_to_timespec(te, &ts2);
  1666. cnt = 0;
  1667. // 查找符合条件的文件
  1668. for (i = 0; i < *p_file_cnt; i++)
  1669. {
  1670. c1 = (p_dfes[i].file_time.tv_sec > ts1.tv_sec) || ((p_dfes[i].file_time.tv_sec == ts1.tv_sec) && (p_dfes[i].file_time.tv_nsec >= ts1.tv_nsec));
  1671. c2 = (p_dfes[i].file_time.tv_sec < ts2.tv_sec) || ((p_dfes[i].file_time.tv_sec == ts2.tv_sec) && (p_dfes[i].file_time.tv_nsec <= ts2.tv_nsec));
  1672. if (c1 && c2)
  1673. {
  1674. cnt++;
  1675. }
  1676. }
  1677. p_dfs = rt_malloc(cnt * sizeof(struct dir_file_struct));
  1678. if (!p_dfs)
  1679. {
  1680. rt_printf("%s: rt_malloc error \n", __func__);
  1681. rt_free(p_dfes);
  1682. return NULL;
  1683. }
  1684. j = 0;
  1685. // 查找符合条件的文件
  1686. for (i = 0; i < *p_file_cnt; i++)
  1687. {
  1688. c1 = (p_dfes[i].file_time.tv_sec > ts1.tv_sec) || ((p_dfes[i].file_time.tv_sec == ts1.tv_sec) && (p_dfes[i].file_time.tv_nsec >= ts1.tv_nsec));
  1689. c2 = (p_dfes[i].file_time.tv_sec < ts2.tv_sec) || ((p_dfes[i].file_time.tv_sec == ts2.tv_sec) && (p_dfes[i].file_time.tv_nsec <= ts2.tv_nsec));
  1690. if (c1 && c2)
  1691. {
  1692. memcpy(p_dfs[j].name, p_dfes[i].file_name, 64);
  1693. p_dfs[j].file_size = p_dfes[i].file_size;
  1694. p_dfs[j].file_time = p_dfes[i].file_time;
  1695. j++;
  1696. }
  1697. }
  1698. *p_file_cnt = cnt;
  1699. }
  1700. rt_free(p_dfes);
  1701. return p_dfs;
  1702. }
  1703. /******************************************************************************
  1704. 函数名称: hf_get_file_inf_n
  1705. 函数版本: 01.01
  1706. 创建作者:
  1707. 创建日期: 2016-08-09
  1708. 函数说明: 根据文件名称,获取文件内容,将 文件ID及文件长度赋值 ,返回文件内容指针
  1709. 参数说明:
  1710. filename : 文件名称
  1711. id :文件ID ,如何定义,暂时没有规定,可自主定义,保证文件ID的唯一即可,
  1712. filelenth : 文件长度
  1713. 返回值:文件内容的指针索引
  1714. 修改记录:
  1715. */
  1716. char *hf_get_file_inf_n(char *file_name, uint32_t *id, int *filelenth)
  1717. {
  1718. int fd;
  1719. struct stat s;
  1720. char *p;
  1721. // 获取文件属性
  1722. if (sys_newstat(file_name, &s))
  1723. {
  1724. rt_printf("%s: sys_newstat(%s)error \n", __func__, file_name);
  1725. return NULL;
  1726. }
  1727. #ifdef HISFILE_ID
  1728. *id = (uint32_t)s.st_ino;
  1729. #else
  1730. *id = 0; // 将文件ID默认为 0
  1731. #endif
  1732. *filelenth = (int)s.st_size;
  1733. fd = sys_open(file_name, O_RDONLY, 0);
  1734. if (fd < 0)
  1735. {
  1736. rt_printf("%s:sys_open(%s)error \n", __func__, file_name);
  1737. return NULL;
  1738. }
  1739. p = rt_malloc(s.st_size);
  1740. if (!p)
  1741. {
  1742. sys_close(fd);
  1743. return NULL;
  1744. }
  1745. if (sys_read(fd, p, s.st_size) != s.st_size)
  1746. {
  1747. rt_printf("%s:sys_read error \n", __func__);
  1748. rt_free(p);
  1749. sys_close(fd);
  1750. return NULL;
  1751. }
  1752. sys_close(fd);
  1753. return p;
  1754. }
  1755. /******************************************************************************
  1756. 函数名称: hf_init
  1757. 函数版本: 01.01
  1758. 创建作者:
  1759. 创建日期: 2016-08-09
  1760. 函数说明: 初始化历史文件
  1761. 参数说明:
  1762. 返回值:成功返回0,失败返回-1;
  1763. 修改记录:
  1764. */
  1765. int hf_init(void)
  1766. {
  1767. rt_file_mkdir(HF_HISTORY_DIR);
  1768. rt_file_mkdir(HF_SOE_101_DIR);
  1769. rt_file_mkdir(HF_CO_101_DIR);
  1770. rt_file_mkdir(HF_EXV_101_DIR);
  1771. rt_file_mkdir(HF_FIXPT_101_DIR);
  1772. rt_file_mkdir(HF_FRZ_101_DIR);
  1773. rt_file_mkdir(HF_FLOWREV_101_DIR);
  1774. rt_file_mkdir(HF_LOG_101_DIR);
  1775. // 录波文件目录HF_WAVE_DIR在录波文件实现文件Record.c中创建
  1776. rt_file_mkdir(HF_HISTORY_DB_INFO);
  1777. return 0;
  1778. }
  1779. /******************************************************************************
  1780. 函数名称: hf_init
  1781. 函数版本: 01.01
  1782. 创建作者:
  1783. 创建日期: 2016-08-09
  1784. 函数说明: 初始化历史文件
  1785. 参数说明:
  1786. 返回值:成功返回0,失败返回-1;
  1787. 修改记录:
  1788. */
  1789. int hf_exit(void)
  1790. {
  1791. return 0;
  1792. }
  1793. #ifdef ADD_MULTIPLE_FILE_WR_FUN
  1794. int ll_init(void)
  1795. {
  1796. rt_file_mkdir(LL_LINELOSS_DIR);
  1797. rt_file_mkdir(LL_FIXD_101_DIR);
  1798. rt_file_mkdir(LL_RAND_101_DIR);
  1799. rt_file_mkdir(LL_FRZD_101_DIR);
  1800. rt_file_mkdir(LL_SHARPD_101_DIR);
  1801. rt_file_mkdir(LL_MONTHD_101_DIR);
  1802. rt_file_mkdir(LL_EVENTD_101_DIR);
  1803. rt_file_mkdir(HF_HISTORY_DB_INFO);
  1804. return 0;
  1805. }
  1806. #endif
  1807. void testhf(void)
  1808. {
  1809. struct dir_file_struct *p_dfs;
  1810. int file_cnt;
  1811. struct rtc_time_t tm;
  1812. int i;
  1813. p_dfs = hf_get_dir_file(HF_FIXPT_101_DIR, &file_cnt);
  1814. if (p_dfs)
  1815. {
  1816. rt_printf("目录%s\r\n", HF_FIXPT_101_DIR);
  1817. rt_printf("文件个数:%d p=%p\r\n", file_cnt, p_dfs);
  1818. for (i = 0; i < file_cnt; i++)
  1819. {
  1820. rt_printf("文件名称:%d\r\n", p_dfs[i].file_name);
  1821. rt_printf("名称:%s\r\n", p_dfs[i].name);
  1822. rt_printf("文件大小:%d\r\n", p_dfs[i].file_size);
  1823. timespec_to_rtc(p_dfs[i].file_time, &tm, 0);
  1824. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n", tm.year, tm.month, tm.day, tm.hour, tm.min, tm.ms / 1000);
  1825. }
  1826. rt_printf("\r\n\r\n");
  1827. rt_free(p_dfs);
  1828. }
  1829. #if 0
  1830. struct dir_file_ext_struct *p_dfs;
  1831. int file_cnt;
  1832. struct rtc_time_t tm;
  1833. int i;
  1834. p_dfs=hf_get_dir_file_ext("/app/data/rec_wave/", &file_cnt, "/tmp/123");
  1835. if(p_dfs)
  1836. {
  1837. rt_printf("目录%s\r\n", "/app/data/rec_wave/");
  1838. rt_printf("文件个数:%d\r\n", file_cnt);
  1839. for(i=0;i<file_cnt;i++)
  1840. {
  1841. rt_printf("文件名称:%s\r\n", p_dfs[i].file_name);
  1842. rt_printf("文件大小:%d\r\n", p_dfs[i].file_size);
  1843. timespec_to_rtc(p_dfs[i].file_time, &tm, 0);
  1844. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1845. rt_printf("\r\n\r\n");
  1846. }
  1847. rt_free(p_dfs);
  1848. }
  1849. #endif
  1850. #if 0
  1851. struct dir_file_struct *p_dfs;
  1852. int file_cnt;
  1853. char tmp[128];
  1854. struct timespec ts;
  1855. struct rtc_time_t tm;
  1856. char *p;
  1857. int part_len=0;
  1858. int len = 0;
  1859. len=hf_get_file_inf(HF_SOE_101_DIR, 20, &part_len);
  1860. rt_printf("%s len=%d, part_len=%d\r\n", HF_SOE_101_DIR, len, part_len);
  1861. len=hf_get_file_inf(HF_CO_101_DIR, 21, &part_len);
  1862. rt_printf("%s len=%d, part_len=%d\r\n", HF_CO_101_DIR, len, part_len);
  1863. len=hf_get_file_inf(HF_EXV_101_DIR, 112, &part_len);
  1864. rt_printf("%s len=%d, part_len=%d\r\n", HF_EXV_101_DIR, len, part_len);
  1865. len=hf_get_file_inf(HF_FIXPT_101_DIR, 23, &part_len);
  1866. rt_printf("%s len=%d, part_len=%d\r\n", HF_FIXPT_101_DIR, len, part_len);
  1867. len=hf_get_file_inf(HF_WAVE_DIR, 0x010A , &part_len);
  1868. rt_printf("%s len=%d, part_len=%d\r\n", HF_WAVE_DIR, len, part_len);
  1869. #endif
  1870. #if 0
  1871. p_dfs=hf_get_dir_file(HF_SOE_101_DIR, &file_cnt);
  1872. if(p_dfs)
  1873. {
  1874. rt_printf("目录%s\r\n", HF_SOE_101_DIR);
  1875. rt_printf("文件个数:%d\r\n", file_cnt);
  1876. rt_printf("文件名称:%d\r\n", p_dfs->file_name);
  1877. rt_printf("文件大小:%d\r\n", p_dfs->file_size);
  1878. timespec_to_rtc(p_dfs->file_time, &tm, 0);
  1879. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1880. rt_printf("\r\n\r\n");
  1881. rt_free(p_dfs);
  1882. }
  1883. p_dfs=hf_get_dir_file(HF_SOE_104_DIR, &file_cnt);
  1884. if(p_dfs)
  1885. {
  1886. rt_printf("目录%s\r\n", HF_SOE_104_DIR);
  1887. rt_printf("文件个数:%d\r\n", file_cnt);
  1888. rt_printf("文件名称:%d\r\n", p_dfs->file_name);
  1889. rt_printf("文件大小:%d\r\n", p_dfs->file_size);
  1890. timespec_to_rtc(p_dfs->file_time, &tm, 0);
  1891. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1892. rt_printf("\r\n\r\n");
  1893. rt_free(p_dfs);
  1894. }
  1895. p_dfs=hf_get_dir_file(HF_CO_101_DIR, &file_cnt);
  1896. if(p_dfs)
  1897. {
  1898. rt_printf("目录%s\r\n", HF_CO_101_DIR);
  1899. rt_printf("文件个数:%d\r\n", file_cnt);
  1900. rt_printf("文件名称:%d\r\n", p_dfs->file_name);
  1901. rt_printf("文件大小:%d\r\n", p_dfs->file_size);
  1902. timespec_to_rtc(p_dfs->file_time, &tm, 0);
  1903. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1904. rt_printf("\r\n\r\n");
  1905. rt_free(p_dfs);
  1906. }
  1907. p_dfs=hf_get_dir_file(HF_CO_104_DIR, &file_cnt);
  1908. if(p_dfs)
  1909. {
  1910. rt_printf("目录%s\r\n", HF_CO_104_DIR);
  1911. rt_printf("文件个数:%d\r\n", file_cnt);
  1912. rt_printf("文件名称:%d\r\n", p_dfs->file_name);
  1913. rt_printf("文件大小:%d\r\n", p_dfs->file_size);
  1914. timespec_to_rtc(p_dfs->file_time, &tm, 0);
  1915. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1916. rt_printf("\r\n\r\n");
  1917. rt_free(p_dfs);
  1918. }
  1919. p_dfs=hf_get_dir_file(HF_EXV_101_DIR, &file_cnt);
  1920. if(p_dfs)
  1921. {
  1922. rt_printf("目录%s\r\n", HF_EXV_101_DIR);
  1923. rt_printf("文件个数:%d\r\n", file_cnt);
  1924. for(i=0;i<file_cnt;i++)
  1925. {
  1926. rt_printf("文件名称:%d\r\n", p_dfs[i].file_name);
  1927. rt_printf("文件大小:%d\r\n", p_dfs[i].file_size);
  1928. timespec_to_rtc(p_dfs[i].file_time, &tm, 0);
  1929. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1930. }
  1931. rt_printf("\r\n\r\n");
  1932. rt_free(p_dfs);
  1933. }
  1934. p_dfs=hf_get_dir_file(HF_EXV_104_DIR, &file_cnt);
  1935. if(p_dfs)
  1936. {
  1937. rt_printf("目录%s\r\n", HF_EXV_104_DIR);
  1938. rt_printf("文件个数:%d\r\n", file_cnt);
  1939. for(i=0;i<file_cnt;i++)
  1940. {
  1941. rt_printf("文件名称:%d\r\n", p_dfs[i].file_name);
  1942. rt_printf("文件大小:%d\r\n", p_dfs[i].file_size);
  1943. timespec_to_rtc(p_dfs[i].file_time, &tm, 0);
  1944. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1945. }
  1946. rt_printf("\r\n\r\n");
  1947. rt_free(p_dfs);
  1948. }
  1949. p_dfs=hf_get_dir_file(HF_FIXPT_101_DIR, &file_cnt);
  1950. if(p_dfs)
  1951. {
  1952. rt_printf("目录%s\r\n", HF_FIXPT_101_DIR);
  1953. rt_printf("文件个数:%d\r\n", file_cnt);
  1954. for(i=0;i<file_cnt;i++)
  1955. {
  1956. rt_printf("文件名称:%d\r\n", p_dfs[i].file_name);
  1957. rt_printf("文件大小:%d\r\n", p_dfs[i].file_size);
  1958. timespec_to_rtc(p_dfs[i].file_time, &tm, 0);
  1959. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1960. }
  1961. rt_printf("\r\n\r\n");
  1962. rt_free(p_dfs);
  1963. }
  1964. p_dfs=hf_get_dir_file(HF_FIXPT_104_DIR, &file_cnt);
  1965. if(p_dfs)
  1966. {
  1967. rt_printf("目录%s\r\n", HF_FIXPT_104_DIR);
  1968. rt_printf("文件个数:%d\r\n", file_cnt);
  1969. for(i=0;i<file_cnt;i++)
  1970. {
  1971. rt_printf("文件名称:%d\r\n", p_dfs[i].file_name);
  1972. rt_printf("文件大小:%d\r\n", p_dfs[i].file_size);
  1973. timespec_to_rtc(p_dfs[i].file_time, &tm, 0);
  1974. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu \r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000);
  1975. }
  1976. rt_printf("\r\n\r\n");
  1977. rt_free(p_dfs);
  1978. }
  1979. p_dfs=hf_get_dir_file(HF_WAVE_DIR, &file_cnt);
  1980. if(p_dfs)
  1981. {
  1982. rt_printf("目录%s\r\n", HF_WAVE_DIR);
  1983. rt_printf("文件个数:%d\r\n", file_cnt);
  1984. for(i=0;i<file_cnt;i++)
  1985. {
  1986. rt_printf("文件名称:%d\r\n", p_dfs[i].file_name);
  1987. rt_printf("文件大小:%d\r\n", p_dfs[i].file_size);
  1988. timespec_to_rtc(p_dfs[i].file_time, &tm, 0);
  1989. rt_printf("创建时间:20%02hu-%02hu-%02hu %02hu:%02hu:%02hu %02hu\r\n",tm.year,tm.month,tm.day,tm.hour,tm.min,tm.ms/1000, tm.ms%1000);
  1990. }
  1991. rt_printf("\r\n\r\n");
  1992. rt_free(p_dfs);
  1993. }
  1994. int part_len=0;
  1995. int i=0;
  1996. char *p;
  1997. p=hf_get_file_part(HF_SOE_101_DIR, HF_SOE_FILENAME_N, 1, &part_len);
  1998. if(p)
  1999. {
  2000. rt_printf("%s\r\n", HF_SOE_101_DIR);
  2001. for(i=0;i<part_len;i++)
  2002. {
  2003. rt_printf("%02x ", p[i]);
  2004. }
  2005. rt_printf("\r\n\r\n");
  2006. rt_free(p);
  2007. }
  2008. p=hf_get_file_part(HF_SOE_104_DIR, HF_SOE_FILENAME_N, 1, &part_len);
  2009. if(p)
  2010. {
  2011. rt_printf("%s\r\n", HF_SOE_104_DIR);
  2012. for(i=0;i<part_len;i++)
  2013. {
  2014. rt_printf("%02x ", p[i]);
  2015. }
  2016. rt_printf("\r\n\r\n");
  2017. rt_free(p);
  2018. }
  2019. p=hf_get_file_part(HF_CO_101_DIR, HF_YK_FILENAME_N, 1, &part_len);
  2020. if(p)
  2021. {
  2022. rt_printf("%s\r\n", HF_CO_101_DIR);
  2023. for(i=0;i<part_len;i++)
  2024. {
  2025. rt_printf("%02x ", p[i]);
  2026. }
  2027. rt_printf("\r\n\r\n");
  2028. rt_free(p);
  2029. }
  2030. p=hf_get_file_part(HF_CO_104_DIR, HF_YK_FILENAME_N, 1, &part_len);
  2031. if(p)
  2032. {
  2033. rt_printf("%s\r\n", HF_CO_104_DIR);
  2034. for(i=0;i<part_len;i++)
  2035. {
  2036. rt_printf("%02x ", p[i]);
  2037. }
  2038. rt_printf("\r\n\r\n");
  2039. rt_free(p);
  2040. }
  2041. p=hf_get_file_part(HF_EXV_101_DIR, HF_EXV_FILENAME_N, 1, &part_len);
  2042. if(p)
  2043. {
  2044. rt_printf("%s%d%s\r\n", HF_EXV_101_DIR, HF_EXV_FILENAME_N,HF_EXV_FILENAME);
  2045. for(i=0;i<part_len;i++)
  2046. {
  2047. rt_printf("%02x ", p[i]);
  2048. }
  2049. rt_printf("\r\n\r\n");
  2050. rt_free(p);
  2051. }
  2052. p=hf_get_file_part(HF_EXV_104_DIR, 112, 1, &part_len);
  2053. if(p)
  2054. {
  2055. rt_printf("%s%d%s\r\n", HF_EXV_104_DIR, HF_EXV_FILENAME_N+4,HF_EXV_FILENAME);
  2056. for(i=0;i<part_len;i++)
  2057. {
  2058. rt_printf("%02x ", p[i]);
  2059. }
  2060. rt_printf("\r\n\r\n");
  2061. rt_free(p);
  2062. }
  2063. p=hf_get_file_part(HF_FIXPT_101_DIR, HF_FIXPT_FILENAME_N, 2, &part_len);
  2064. if(p)
  2065. {
  2066. rt_printf("%s%d%s\r\n", HF_FIXPT_101_DIR, HF_FIXPT_FILENAME_N,HF_FIXPT_FILENAME);
  2067. for(i=0;i<part_len;i++)
  2068. {
  2069. rt_printf("%02x ", p[i]);
  2070. }
  2071. rt_printf("\r\n\r\n");
  2072. rt_free(p);
  2073. }
  2074. p=hf_get_file_part(HF_FIXPT_104_DIR, 26, 3, &part_len);
  2075. if(p)
  2076. {
  2077. rt_printf("%s%d%s\r\n", HF_FIXPT_104_DIR, HF_FIXPT_FILENAME_N,HF_FIXPT_FILENAME);
  2078. for(i=0;i<part_len;i++)
  2079. {
  2080. rt_printf("%02x ", p[i]);
  2081. }
  2082. rt_printf("\r\n\r\n");
  2083. rt_free(p);
  2084. }
  2085. p=hf_get_file_part(HF_WAVE_DIR, 267, 1, &part_len);
  2086. if(p)
  2087. {
  2088. rt_printf("%s%04x \r\n", HF_WAVE_DIR, 267);
  2089. for(i=0;i<part_len;i++)
  2090. {
  2091. rt_printf("%02x ", p[i]);
  2092. }
  2093. rt_printf("\r\n\r\n");
  2094. rt_free(p);
  2095. }
  2096. p=hf_get_file_part(HF_WAVE_DIR, 267, 2, &part_len);
  2097. if(p)
  2098. {
  2099. rt_printf("%s%04x \r\n", HF_WAVE_DIR, 267);
  2100. for(i=0;i<part_len;i++)
  2101. {
  2102. rt_printf("%02x ", p[i]);
  2103. }
  2104. rt_printf("\r\n\r\n");
  2105. rt_free(p);
  2106. }
  2107. #endif
  2108. }