log.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "log.h"
  2. #include "rtc.h"
  3. #include "ringfs.h"
  4. #include "spi_flash.h"
  5. #include "FreeRTOS.h"
  6. #include "task.h"
  7. #include "semphr.h"
  8. #include <string.h>
  9. char logFileBuf[FILE_BUF_MAX_LEN];
  10. char name_login[50];
  11. extern const char* logsStrShortRu[];
  12. bool flUpdateLog = false;
  13. static bool fLogInit = false; // Флаг инициализации журнала
  14. #define LOG_TIME 1000*60*10
  15. static int op_sector_erase(struct ringfs_flash_partition *flash, int address) {
  16. (void)flash;
  17. int ret;
  18. ret = spi_flash_erase_sector(address, 0);
  19. return ret;
  20. }
  21. static ssize_t op_program(struct ringfs_flash_partition *flash, int address, const void *data, size_t size) {
  22. (void)flash;
  23. int ret;
  24. ret = spi_flash_write(address, data, size, 0);
  25. return ret;
  26. }
  27. static ssize_t op_read(struct ringfs_flash_partition *flash, int address, void *data, size_t size) {
  28. (void)flash;
  29. int ret;
  30. ret = spi_flash_read(address, data, size, 0);
  31. return ret;
  32. }
  33. static struct ringfs_flash_partition ringfs_flash = {
  34. .sector_offset = LOG_FLASH_SECTOR_OFFSET,
  35. .sector_erase = op_sector_erase,
  36. .program = op_program,
  37. .read = op_read,
  38. };
  39. static struct ringfs fs;
  40. static struct ringfs_flash_partition ringfs_flash2 = {
  41. .sector_offset = ALARM_LOG_FLASH_SECTOR_OFFSET,
  42. .sector_erase = op_sector_erase,
  43. .program = op_program,
  44. .read = op_read,
  45. };
  46. static struct ringfs fs2;
  47. static SemaphoreHandle_t log_mutex;
  48. /**
  49. * @brief Отключает журнал для безопасной перезагрузки
  50. */
  51. bool LOG_Disable(void)
  52. {
  53. if (fLogInit) {
  54. /* Ожидаем завершения работы с журнал */
  55. if ( xSemaphoreTake(log_mutex, 10000) == pdTRUE ) {
  56. //fLogInit = false;
  57. //xSemaphoreGive(logMutex);
  58. return true;
  59. }
  60. else {
  61. return false;
  62. }
  63. }
  64. else {
  65. return true;
  66. }
  67. }
  68. void log_task(void* params)
  69. {
  70. for(;;){
  71. flUpdateLog = true;
  72. vTaskDelay(LOG_TIME);
  73. /*vTaskDelay(50);
  74. log_event_data(LOG_SYSTEM_BOOT, "Администратор");
  75. log_add(")215.7;215.7;220.5;000;50.1;2.30;25.0;00000001;");*/
  76. }
  77. }
  78. void log_init(bool format) {
  79. DBG printf(">>> Event log\n");
  80. if (!spi_flash_desc.present)
  81. return;
  82. ringfs_flash.sector_size = spi_flash_desc.sector_size;
  83. ringfs_flash.sector_count = SECTOR_COUNT;
  84. ringfs_init(&fs, &ringfs_flash, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  85. if (format || ringfs_scan(&fs) != 0){
  86. DBG printf("FAT1 false\r\n");
  87. ringfs_format(&fs);
  88. }
  89. DBG printf("FAT1 true\r\n");
  90. ringfs_flash2.sector_size = spi_flash_desc.sector_size;
  91. ringfs_flash2.sector_count = SECTOR_COUNT;
  92. ringfs_init(&fs2, &ringfs_flash2, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  93. if (format || ringfs_scan(&fs2) != 0){
  94. DBG printf("FAT2 false\r\n");
  95. ringfs_format(&fs2);
  96. }
  97. DBG printf("FAT2 true\r\n");
  98. fLogInit = true;
  99. log_mutex = xSemaphoreCreateMutex();
  100. xTaskCreate(log_task, ( char * ) "log_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  101. }
  102. int capacity_flash = 0;
  103. int count_flash = 0;
  104. int log_test(void) {
  105. int ret;
  106. log_entry_t entry;
  107. log_init(false);
  108. capacity_flash = ringfs_capacity(&fs);
  109. count_flash = ringfs_count_exact(&fs);
  110. DBG printf("\tCapacity: %d\n", capacity_flash);
  111. DBG printf("\tCount: %d\n", count_flash);
  112. DBG printf("\tAppending ");
  113. // ret = log_event(LOG_SYSTEM_DEFCONFIG, 0, 0);
  114. DBG printf("%s\n", ret == 0 ? "ok" : "error");
  115. if (ret == 0)
  116. return -1;
  117. // ret = log_event(LOG_SYSTEM_DEFCONFIG, 0, 512);
  118. entry.timestamp = 0;
  119. entry.type = 0;
  120. DBG printf("\tFetching ");
  121. if (log_fetch(&entry, portMAX_DELAY) == 0){
  122. DBG printf("ok, time=%d, type=%d\n", entry.timestamp, entry.type);
  123. log_fetch(&entry, portMAX_DELAY);
  124. entry.timestamp = 0;
  125. entry.type = 0;
  126. log_fetch(&entry, portMAX_DELAY);
  127. entry.timestamp = 0;
  128. entry.type = 0;
  129. log_fetch(&entry, portMAX_DELAY);
  130. entry.timestamp = 0;
  131. entry.type = 0;
  132. log_fetch(&entry, portMAX_DELAY);
  133. return 0;
  134. }
  135. else {
  136. DBG printf("fail\n");
  137. return -1;
  138. }
  139. DBG printf("\tDiscarding ");
  140. if (log_discard(&entry,portMAX_DELAY) == 0)
  141. DBG printf("ok\n");
  142. else {
  143. DBG printf("fail\n");
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. int log_append(log_entry_t *entry) {
  149. int ret;
  150. TM_RTC_t data;
  151. ret = xSemaphoreTake( log_mutex, portMAX_DELAY );
  152. if (ret == pdFALSE)
  153. return ret;
  154. if (!entry->timestamp){
  155. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  156. entry->timestamp = data.unix;
  157. }
  158. if(entry->type == LOG_VALUE)
  159. ringfs_append(&fs, entry);
  160. else
  161. ringfs_append(&fs2, entry);
  162. xSemaphoreGive(log_mutex);
  163. return ret;
  164. }
  165. int log_fetch(log_entry_t *entry, uint32_t timeout) {
  166. int ret;
  167. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  168. if (ret == pdFALSE)
  169. return ret;
  170. if(entry->type == LOG_VALUE)
  171. ret = ringfs_fetch(&fs, entry);
  172. else
  173. ret = ringfs_fetch(&fs2, entry);
  174. xSemaphoreGive(log_mutex);
  175. return ret;
  176. }
  177. int log_rewind(log_entry_t *entry, uint32_t timeout) {
  178. int ret;
  179. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  180. if (ret == pdFALSE)
  181. return ret;
  182. if(entry->type == LOG_VALUE)
  183. ret = ringfs_rewind(&fs);
  184. else
  185. ret = ringfs_rewind(&fs2);
  186. xSemaphoreGive(log_mutex);
  187. return ret;
  188. }
  189. int log_discard(log_entry_t *entry, uint32_t timeout) {
  190. int ret;
  191. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  192. if (ret == pdFALSE)
  193. return ret;
  194. if(entry->type == LOG_VALUE)
  195. ret = ringfs_discard(&fs);
  196. else
  197. ret = ringfs_discard(&fs2);
  198. xSemaphoreGive(log_mutex);
  199. return ret;
  200. }
  201. void log_event_data(log_type_t type, char *data)
  202. {
  203. log_entry_t entry_data;
  204. entry_data.timestamp = 0;
  205. entry_data.type = type;
  206. strncpy(entry_data.data, data, 49);
  207. if (fLogInit)
  208. log_append(&entry_data);
  209. }
  210. void log_add(char *log_data)
  211. {
  212. char buf_value[50];
  213. uint8_t i, len;
  214. memset(buf_value, 0, 50);
  215. len = strlen(log_data);
  216. if (len != UPS_DATA_STRING_SIZE) {
  217. //len = UPS_DATA_STRING_SIZE;
  218. return;
  219. }
  220. strncpy(buf_value, log_data, len);
  221. DBG printf("UPS log data: %s\r\n", log_data);
  222. buf_value[0] = '\"';
  223. for(i = 0; i < len; i++)
  224. {
  225. if(buf_value[i] == ' ')
  226. buf_value[i] = ';';
  227. }
  228. buf_value[len - 1] = ';';
  229. if(fLogInit){
  230. if(fs.write.slot>67)
  231. {
  232. log_entry_t entry_data;
  233. entry_data.timestamp = 0;
  234. log_event_data(LOG_VALUE, buf_value);
  235. }
  236. else
  237. log_event_data(LOG_VALUE, buf_value);
  238. }
  239. }
  240. /**
  241. * @brief Возвращает true если журнал проинициализирован
  242. */
  243. bool LOG_IsInit()
  244. {
  245. return fLogInit;
  246. }
  247. /**
  248. * @brief Возвращает общее количество страниц
  249. */
  250. uint32_t LOG_GetPageCount(void)
  251. {
  252. return (((ringfs_count_estimate(&fs)) / 10) + 1);
  253. }
  254. uint32_t LOG_GetTotalSTRCount(void)
  255. {
  256. return ringfs_count_estimate(&fs);
  257. }
  258. void LOG_GetPage_tabs(char *str, uint32_t page)
  259. {
  260. TM_RTC_t rtc_data;
  261. log_entry_t entry;
  262. char buf[20];
  263. uint8_t i;
  264. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  265. memset(buf, 0, 20);
  266. for(i=0; i < 10; i++){
  267. fs.cursor_position = start - 10*(page-1) - 1 - i;
  268. if(fs.cursor_position < 0)
  269. break;
  270. else{
  271. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  272. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  273. }
  274. entry.type = LOG_VALUE;
  275. log_fetch(&entry, portMAX_DELAY);
  276. entry.data[49] = 0;
  277. strncat(str, entry.data, strlen(entry.data));
  278. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  279. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  280. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  281. strcat(str, buf);
  282. strcat(str, "\",");
  283. strcat(str, "\r\n");
  284. }
  285. }
  286. void LOG_GetPage(char *str, uint32_t page)
  287. {
  288. TM_RTC_t rtc_data;
  289. log_entry_t entry;
  290. char buf[20];
  291. uint8_t i;
  292. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  293. memset(buf, 0, 20);
  294. for(i=0; i < 10; i++){
  295. fs.cursor_position = start - 10*(page-1) - 1 - i;
  296. if(fs.cursor_position < 0)
  297. break;
  298. else{
  299. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  300. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  301. }
  302. entry.type = LOG_VALUE;
  303. log_fetch(&entry, portMAX_DELAY);
  304. entry.data[49] = 0;
  305. strncat(str, entry.data, strlen(entry.data));
  306. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  307. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  308. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  309. strcat(str, buf);
  310. strcat(str, "\",");
  311. }
  312. }
  313. uint32_t LOG_GetData(int ptr, char *str, uint32_t size, bool start)
  314. {
  315. TM_RTC_t rtc_data;
  316. log_entry_t entry;
  317. char buf[20];
  318. uint8_t i;
  319. entry.type = LOG_VALUE;
  320. if(start)
  321. log_rewind(&entry, portMAX_DELAY);
  322. fs.cursor_position = ptr/STRING_SIZE;
  323. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  324. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  325. for(i = 0; i < size/STRING_SIZE; i++)
  326. {
  327. entry.type = LOG_VALUE;
  328. log_fetch(&entry, portMAX_DELAY);
  329. entry.data[49] = 0;
  330. strncat(str, &entry.data[1], (strlen(entry.data) - 1));
  331. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  332. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  333. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  334. strcat(str, buf);
  335. strcat(str, "\n");
  336. }
  337. return strlen(str);
  338. }
  339. /**
  340. * @brief Возвращает общее количество страниц
  341. */
  342. uint32_t History_GetPageCount(void)
  343. {
  344. return (((ringfs_count_estimate(&fs2)) / 10) + 1);
  345. }
  346. uint32_t History_GetTotalSTRCount(void)
  347. {
  348. return ringfs_count_estimate(&fs2);
  349. }
  350. void History_GetPage(char *str, uint32_t page)
  351. {
  352. TM_RTC_t rtc_data;
  353. log_entry_t entry;
  354. char buf[20];
  355. uint8_t i;
  356. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  357. memset(buf, 0, 20);
  358. for(i=0; i < 10; i++){
  359. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  360. if(fs2.cursor_position < 0)
  361. break;
  362. else{
  363. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  364. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  365. }
  366. entry.type = LOG_LOGIN;
  367. log_fetch(&entry, portMAX_DELAY);
  368. strcat(str, "\"");
  369. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  370. strcat(str, ";");
  371. entry.data[49] = 0;
  372. strncat(str, entry.data, (strlen(entry.data) ));
  373. strcat(str, ";");
  374. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  375. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  376. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  377. strcat(str, buf);
  378. strcat(str, "\",");
  379. }
  380. }
  381. void History_GetPage_tabs(char *str, uint32_t page)
  382. {
  383. TM_RTC_t rtc_data;
  384. log_entry_t entry;
  385. char buf[50];
  386. uint8_t i;
  387. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  388. memset(buf, 0, 50);
  389. for(i=0; i < 10; i++){
  390. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  391. if(fs2.cursor_position < 0)
  392. break;
  393. else{
  394. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  395. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  396. }
  397. entry.type = LOG_LOGIN;
  398. log_fetch(&entry, portMAX_DELAY);
  399. //sprintf(buf, "%-50s", logsStrShortRu[entry.type]);
  400. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  401. for(uint8_t j = 0; j < (25 - strlen(logsStrShortRu[entry.type])/2); j ++)
  402. strcat(str, " ");
  403. strcat(str, "\t");
  404. memset(buf, 0, 50);
  405. entry.data[49] = 0;
  406. strncat(str, entry.data, (strlen(entry.data) ));
  407. for(uint8_t j = 0; j < (25 - strlen(entry.data)/2); j ++)
  408. strcat(str, " ");
  409. strcat(str, "\t");
  410. memset(buf, 0, 50);
  411. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  412. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  413. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  414. strcat(str, buf);
  415. strcat(str, "\r\n");
  416. }
  417. }
  418. uint32_t History_GetData(int ptr, char *str, uint32_t size, bool start)
  419. {
  420. TM_RTC_t rtc_data;
  421. log_entry_t entry;
  422. char buf[20];
  423. char temp_str[FILE_BUF_MAX_LEN];
  424. uint8_t i;
  425. uint16_t len;
  426. entry.type = LOG_LOGIN;
  427. if(start)
  428. log_rewind(&entry, portMAX_DELAY);
  429. fs2.cursor_position = ptr/STRING_SIZE_HISTORY;
  430. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs2.slots_per_sector)%fs2.flash->sector_count;
  431. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  432. for(i = 0; i < size/STRING_SIZE_HISTORY; i++)
  433. {
  434. memset(temp_str, 0, 100);
  435. log_fetch(&entry, portMAX_DELAY);
  436. strncat(temp_str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type])));
  437. strcat(temp_str, ";");
  438. entry.data[49] = 0;
  439. strncat(temp_str, entry.data, (strlen(entry.data)));
  440. strcat(temp_str, ";");
  441. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  442. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  443. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  444. strcat(temp_str, buf);
  445. len = strlen(temp_str);
  446. if (len <= STRING_SIZE_HISTORY - 1)
  447. {
  448. memset(&temp_str[len], ' ', STRING_SIZE_HISTORY - len - 1);
  449. strcat(temp_str, "\n");
  450. }
  451. else
  452. {
  453. temp_str[STRING_SIZE - 1] = 0xa;
  454. }
  455. strncat(str, temp_str, STRING_SIZE_HISTORY);
  456. }
  457. return strlen(str);
  458. }