log.c 14 KB

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