common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. #include "common.h"
  2. #include "elog.h"
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <sys/sysinfo.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/socket.h>
  10. #include <net/if.h>
  11. #include <arpa/inet.h>
  12. #include <sys/vfs.h>
  13. #include <sys/ioctl.h>
  14. #include <linux/rtc.h>
  15. #include "cfg.h"
  16. #include "sys.h"
  17. #undef LOG_TAG
  18. #define LOG_TAG "main"
  19. GlobalDeviceManager __globalDeviceManage={0};
  20. GlobalDeviceManager __globalDeviceManage2={0};
  21. int ntp_sync_flag=0;
  22. unsigned long __globalSxIdx;
  23. _User_Log _GlobalUserLoginInfo;
  24. UserInfo _GlobalUserInfoNew;
  25. char _GlobalUserToken[64];
  26. MenuAuthority _GlobalMenuAuthority;
  27. NTPManager _globalNTPManager;
  28. SNMPManager _globalSNMPManager;
  29. void printf_global_device_info(GlobalDeviceInfo* _globalDeviceInfo)
  30. {
  31. log_d("******global device info********* ");
  32. log_d("product.id:%d ", _globalDeviceInfo->product.id);
  33. log_d("product.number:%s ", _globalDeviceInfo->product.number);
  34. log_d("product.name:%s ", _globalDeviceInfo->product.name);
  35. log_d("product.ype:%d ", _globalDeviceInfo->product.type);
  36. log_d("product.pwr_type:%d ", _globalDeviceInfo->product.pwr_type);
  37. log_d("product.version:%s ", _globalDeviceInfo->product.version);
  38. log_d("product.count:%d ", _globalDeviceInfo->product.count);
  39. log_d("product.samp_time:%d ", _globalDeviceInfo->product.samp_time);
  40. log_d("product.save_time:%d ", _globalDeviceInfo->product.save_time);
  41. log_d("cascade.mode:%d ", _globalDeviceInfo->cascade.mode);
  42. log_d("cascade.addr:%d ", _globalDeviceInfo->cascade.addr);
  43. log_d("cascade.baudrate:%d ", _globalDeviceInfo->cascade.baudrate);
  44. log_d("relay.path:%s ", _globalDeviceInfo->relay.path);
  45. log_d("******************************** ");
  46. }
  47. void printf_global_power_manage_info(GlobalPowerManger* _globalPowerManger)
  48. {
  49. printf("******global power manage info********* ");
  50. printf("product_id:%d ", _globalPowerManger->product_id);
  51. printf("product_ch_id:%d ", _globalPowerManger->product_ch_id);
  52. printf("product_ch_name:%s ", _globalPowerManger->product_ch_name);
  53. printf("product_ch_type:%d ", _globalPowerManger->product_ch_type);
  54. printf("product_ch_status:%d ", _globalPowerManger->product_ch_status);
  55. printf("product_ch_NF_status:%d ", _globalPowerManger->product_ch_NF_status);
  56. printf("product_ch_start_delay:%d ", _globalPowerManger->product_ch_start_delay);
  57. printf("product_ch_stop_delay:%d ", _globalPowerManger->product_ch_stop_delay);
  58. printf("product_vol_upper_enable:%d ", _globalPowerManger->global_over_manager.product_vol_upper_enable);
  59. printf("product_vol_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_vol_upper_threshold);
  60. printf("product_vol_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_vol_over_upper_threshold_ctrl);
  61. printf("product_vol_over_upper_threshold_para:%d ", _globalPowerManger->global_over_manager.product_vol_over_upper_threshold_ctrl_para);
  62. printf("product_vol_lower_enable:%d ", _globalPowerManger->global_over_manager.product_vol_lower_enable);
  63. printf("product_vol_lower_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_vol_lower_threshold);
  64. printf("product_vol_over_lower_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_vol_over_lower_threshold_ctrl);
  65. printf("product_vol_over_lower_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_vol_over_lower_threshold_ctrl_para);
  66. printf("product_cur_upper_enable:%d ", _globalPowerManger->global_over_manager.product_cur_upper_enable);
  67. printf("product_cur_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_cur_upper_threshold);
  68. printf("product_cur_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_cur_over_upper_threshold_ctrl);
  69. printf("product_cur_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_cur_over_upper_threshold_ctrl_para);
  70. printf("product_pwr_upper_enable:%d ", _globalPowerManger->global_over_manager.product_pwr_upper_enable);
  71. printf("product_pwr_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_pwr_upper_threshold);
  72. printf("product_pwr_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_pwr_over_upper_threshold_ctrl);
  73. printf("product_pwr_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_pwr_over_upper_threshold_ctrl_para);
  74. printf("product_pwrcon_upper_enable:%d ", _globalPowerManger->global_over_manager.product_pwrcon_upper_enable);
  75. printf("product_pwrcon_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_pwrcon_upper_threshold);
  76. printf("product_pwrcon_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_pwrcon_over_upper_threshold_ctrl);
  77. printf("product_pwrcon_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_pwrcon_over_upper_threshold_ctrl_para);
  78. printf("******************************** ");
  79. }
  80. //打印电源信息
  81. void printf_power_info(GlobalPowerInfo* _globalPowerInfo)
  82. {
  83. struct list_head *pos;
  84. GlobalPowerInfo* _temp;
  85. int i = 0;
  86. list_for_each_entry(_temp, &_globalPowerInfo->list, list)
  87. {
  88. printf("i:%d ", i++);
  89. printf("samp_time:%s ", _temp->samp_time);
  90. printf("product_id:%d ", _temp->product_id);
  91. }
  92. }
  93. void INIT_LIST_HEAD(struct list_head *list)
  94. {
  95. list->next = list->prev = list;
  96. }
  97. /**
  98. * EasyLogger demo
  99. */
  100. // void test_elog(void) {
  101. // uint8_t buf[256]= {0};
  102. // int i = 0;
  103. // for (i = 0; i < sizeof(buf); i++)
  104. // {
  105. // buf[i] = i;
  106. // }
  107. // while(true) {
  108. // /* test log output for all level */
  109. // log_a("Hello EasyLogger!");
  110. // log_e("Hello EasyLogger!");
  111. // log_w("Hello EasyLogger!");
  112. // log_i("Hello EasyLogger!");
  113. // log_d("Hello EasyLogger!");
  114. // log_v("Hello EasyLogger!");
  115. // // elog_raw("Hello EasyLogger!");
  116. // elog_hexdump("test", 16, buf, sizeof(buf));
  117. // sleep(5);
  118. // }
  119. // }
  120. #if(1)
  121. void initLogger(void)
  122. {
  123. setbuf(stdout, NULL);
  124. elog_init();
  125. elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
  126. elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  127. elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  128. elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  129. elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  130. elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  131. elog_set_text_color_enabled(true);
  132. elog_start();
  133. log_i("Smart Pdu Application Running!");
  134. }
  135. #endif
  136. /// @brief 获取系统mem大小
  137. /// @param totalSize
  138. /// @return
  139. int getTotalMemorySize(unsigned int* totalSize)
  140. {
  141. struct sysinfo memInfo;
  142. if (sysinfo(&memInfo) != -1) {
  143. *totalSize = memInfo.totalram;
  144. // printf("Total RAM size: %lu bytes\n", memInfo.totalram);
  145. // printf("Free RAM size: %lu bytes\n", memInfo.freeram);
  146. }
  147. else
  148. {
  149. *totalSize = 0 ;
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. /// @brief 返回设备IP地址
  155. /// @param dev 网口名
  156. /// @return
  157. char* getLocalIpAddress(char* dev)
  158. {
  159. int fd;
  160. struct ifreq ifr;
  161. static char ip_addr[64];
  162. fd = socket(AF_INET, SOCK_DGRAM, 0);
  163. ifr.ifr_addr.sa_family = AF_INET;
  164. strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
  165. ioctl(fd, SIOCGIFADDR, &ifr);
  166. close(fd);
  167. char *p=inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
  168. if(!p) {
  169. return NULL;
  170. }
  171. strcpy(ip_addr, p);
  172. return ip_addr;
  173. }
  174. // 获取MAC地址的函数
  175. char* get_mac_address(const char *iface_name)
  176. {
  177. int fd;
  178. struct ifreq ifr;
  179. unsigned char *mac;
  180. static char mac_str[32];
  181. snprintf(mac_str, sizeof(mac_str), "%02d:%02d:%02d:%02d:%02d:%02d",
  182. 0, 0, 0, 0, 0, 0);
  183. // 创建一个socket
  184. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  185. perror("socket error");
  186. return mac_str;
  187. }
  188. // 设置接口名称
  189. strncpy(ifr.ifr_name, iface_name, IFNAMSIZ - 1);
  190. // 获取MAC地址
  191. if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
  192. perror("ioctl error");
  193. close(fd);
  194. return mac_str;
  195. }
  196. // ifr.ifr_hwaddr.sa_data是一个包含硬件地址的字节数组
  197. mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
  198. // 将MAC地址转换为字符串格式
  199. snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X",
  200. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  201. // 关闭socket
  202. close(fd);
  203. return mac_str;
  204. }
  205. /// @brief 获取磁盘使用率
  206. /// @param reused
  207. void get_disk_occupy(char ** reused)
  208. {
  209. const unsigned short MAXBUFSIZE = 1024;
  210. char currentDirectoryPath[ MAXBUFSIZE ];
  211. getcwd(currentDirectoryPath, MAXBUFSIZE);
  212. //printf("当前目录:%s\n",currentDirectoryPath);
  213. char cmd[50] = "df ";
  214. strcat(cmd, currentDirectoryPath);
  215. //printf("%s\n",cmd);
  216. char buffer[MAXBUFSIZE];
  217. FILE* pipe = popen(cmd, "r");
  218. char fileSys[20];
  219. char blocks[20];
  220. char used[20];
  221. char free[20];
  222. char percent[10];
  223. char moment[20];
  224. if (!pipe)
  225. {
  226. return;
  227. }
  228. if (fgets(buffer, sizeof(buffer), pipe) != NULL)
  229. {
  230. sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
  231. }
  232. if (fgets(buffer, sizeof(buffer), pipe) != NULL)
  233. {
  234. sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
  235. }
  236. //printf("desk used:%s\n",percent);
  237. strcpy(*reused, percent);
  238. return;
  239. }
  240. static int set_rtc(time_t t)
  241. {
  242. int r=0,fd;
  243. // #if (CHIP_TYPE == CHIP_T113s)
  244. // system("hwclock -w -f /dev/rtc0");
  245. // #else
  246. struct rtc_time *rtc;
  247. struct tm *tm = gmtime(&t);
  248. fd = open("/dev/rtc0", O_RDONLY);
  249. if (fd < 0) {
  250. perror("open rtc failed\n");
  251. return -1;
  252. }
  253. rtc = (struct rtc_time*)tm;
  254. if (ioctl(fd, RTC_SET_TIME, rtc) < 0) {
  255. perror("ioctl RTC_SET_TIME failed\n");
  256. r = -1;
  257. }
  258. else {
  259. printf("set rtc ok!\n");
  260. }
  261. close(fd);
  262. //#endif
  263. return r;
  264. }
  265. #define MY_MINUTE 60
  266. #define MY_HOUR (60*MY_MINUTE)
  267. #define MY_DAY (24*MY_HOUR)
  268. #define MY_YEAR (365*MY_DAY)
  269. static int my_month[12] = {
  270. 0,
  271. MY_DAY*(31),
  272. MY_DAY*(31+29),
  273. MY_DAY*(31+29+31),
  274. MY_DAY*(31+29+31+30),
  275. MY_DAY*(31+29+31+30+31),
  276. MY_DAY*(31+29+31+30+31+30),
  277. MY_DAY*(31+29+31+30+31+30+31),
  278. MY_DAY*(31+29+31+30+31+30+31+31),
  279. MY_DAY*(31+29+31+30+31+30+31+31+30),
  280. MY_DAY*(31+29+31+30+31+30+31+31+30+31),
  281. MY_DAY*(31+29+31+30+31+30+31+31+30+31+30)
  282. };
  283. time_t mktime2(struct tm *t, int tzone)
  284. {
  285. time_t res,year;
  286. year = t->tm_year - 70;
  287. /* magic offsets (y+1) needed to get leapyears right.*/
  288. res = MY_YEAR*year + MY_DAY*((year+1)/4);
  289. res += my_month[t->tm_mon];
  290. /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
  291. if (t->tm_mon>1 && ((year+2)%4))
  292. res -= MY_DAY;
  293. res += MY_DAY*(t->tm_mday-1);
  294. res += MY_HOUR*t->tm_hour;
  295. res += MY_MINUTE*t->tm_min;
  296. res += t->tm_sec;
  297. res -= tzone*3600;
  298. return res;
  299. }
  300. time_t mktime0(void)
  301. {
  302. struct tm t;
  303. t.tm_year = 70;
  304. t.tm_mon = 0;
  305. t.tm_mday = 1;
  306. t.tm_hour = 0;
  307. t.tm_min = 0;
  308. t.tm_sec = 0;
  309. return mktime(&t);
  310. }
  311. int set_time(struct tm *t, int tzone)
  312. {
  313. if(!t) {
  314. return -1;
  315. }
  316. t->tm_year -= 1900;
  317. t->tm_mon -= 1;
  318. time_t utc = mktime2(t, tzone);
  319. stime(&utc);
  320. set_rtc(utc);
  321. return 0;
  322. }
  323. int set_utctime(time_t t)
  324. {
  325. stime(&t);
  326. set_rtc(t);
  327. #if (CHIP_TYPE == CHIP_T113s)
  328. sleep(1);
  329. system("hwclock --show");
  330. system("hwclock -s");
  331. #endif
  332. return 0;
  333. }
  334. int get_tzone(void)
  335. {
  336. int tz = 0;
  337. #if (CHIP_TYPE == CHIP_T113s)
  338. time_t t1,t2;
  339. struct tm *tm_local,*tm_utc;
  340. time(&t1);
  341. t2=t1;
  342. tm_local = localtime(&t1);
  343. t1=mktime(tm_local);
  344. tm_utc = gmtime(&t2);
  345. t2=mktime(tm_utc);
  346. tz = (t1 - t2)/3600;
  347. #else
  348. time_t now = time(NULL);
  349. struct tm local_time = *localtime(&now);
  350. time_t utc = mktime(&local_time);
  351. tz = (now - utc)/3600;
  352. #endif
  353. return tz;
  354. }
  355. int set_tzone(int tzone)
  356. {
  357. int r,tz;
  358. char buf[256];
  359. tz = get_tzone();
  360. #if 0
  361. switch(zone) {
  362. case -11:
  363. sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Midway /etc/localtime");
  364. break;
  365. case -10:
  366. sprintf(buf, "ln -sf /usr/share/zoneinfo/HST /etc/localtime");
  367. break;
  368. case -9:
  369. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Adak /etc/localtime");
  370. break;
  371. case -8:
  372. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Boise /etc/localtime");
  373. break;
  374. case -7:
  375. sprintf(buf, "ln -sf /usr/share/zoneinfo/MST /etc/localtime");
  376. break;
  377. case -6:
  378. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime");
  379. break;
  380. case -5:
  381. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime");
  382. break;
  383. case -4:
  384. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Halifax /etc/localtime");
  385. break;
  386. case -3:
  387. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Maceio /etc/localtime");
  388. break;
  389. case -2:
  390. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Noronha /etc/localtime");
  391. break;
  392. case -1:
  393. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Scoresbysund /etc/localtime");
  394. break;
  395. case 0:
  396. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime");
  397. break;
  398. case 1:
  399. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime");
  400. break;
  401. case 2:
  402. sprintf(buf, "ln -sf /usr/share/zoneinfo/EET /etc/localtime");
  403. break;
  404. case 3:
  405. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime");
  406. break;
  407. case 4:
  408. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Amman /etc/localtime");
  409. break;
  410. case 5:
  411. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Karachi /etc/localtime");
  412. break;
  413. case 6:
  414. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Dhaka /etc/localtime");
  415. break;
  416. case 7:
  417. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Bangkok /etc/localtime");
  418. break;
  419. case 8:
  420. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime");
  421. break;
  422. case 9:
  423. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime");
  424. break;
  425. case 10:
  426. sprintf(buf, "ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime");
  427. break;
  428. case 11:
  429. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Sakhalin /etc/localtime");
  430. break;
  431. case 12:
  432. sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Fiji /etc/localtime");
  433. break;
  434. default:
  435. return -1;
  436. }
  437. #else
  438. #if (CHIP_TYPE == CHIP_T113s)
  439. sprintf(buf, "ln -sf /usr/share/zoneinfo/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone));
  440. #else
  441. sprintf(buf, "ln -sf /usr/share/zoneinfo/Etc/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone));
  442. #endif
  443. #endif
  444. log_i("set_zone: %s", buf);
  445. system(buf);
  446. if(tz!=tzone) {
  447. printf("tz %d tzone %d\n",tz,tzone);
  448. sys_restart_app();
  449. }
  450. return 0;
  451. }
  452. void print_time(char *s, struct tm *tm)
  453. {
  454. printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  455. }
  456. void print_utc(char *s, time_t t)
  457. {
  458. struct tm *tm=gmtime(&t);
  459. tm->tm_year += 1900;
  460. tm->tm_mon += 1;
  461. print_time(s, tm);
  462. }
  463. int set_time_zone(struct tm *t, int zone)
  464. {
  465. int r;
  466. if(!t) {
  467. return -1;
  468. }
  469. r = set_time(t, zone);
  470. if(r) {
  471. log_e("__1__ set_time failed\n");
  472. }
  473. r = set_tzone(zone);
  474. if(r) {
  475. log_e("__1__ set_tzone failed\n");
  476. }
  477. return r;
  478. }
  479. int set_utctime_zone(time_t t, int zone)
  480. {
  481. int r;
  482. r = set_utctime(t);
  483. if(r) {
  484. log_e("__2__ set_time failed\n");
  485. }
  486. r = set_tzone(zone);
  487. if(r) {
  488. log_e("__2__ set_zone failed\n");
  489. }
  490. return r;
  491. }
  492. int get_time_str(char *s, int len, time_t t)
  493. {
  494. struct tm *tm=localtime(&t);
  495. return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d.000",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  496. }
  497. int get_time_str2(char *s, int len, time_t t)
  498. {
  499. struct tm *tm=localtime(&t);
  500. return snprintf(s, len, "%04d%02d%02d%02d%02d%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  501. }
  502. int get_curtime_str(char *s, int len)
  503. {
  504. time_t t=time(NULL);
  505. struct tm *tm=localtime(&t);
  506. return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  507. }
  508. int get_curtime_str2(char *s, int len)
  509. {
  510. time_t t=time(NULL);
  511. struct tm *tm=localtime(&t);
  512. return snprintf(s, len, "%02d.%02d %02d:%02d:%02d", tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  513. }
  514. char* getUserNamebyToken(char* Token)
  515. {
  516. char* userName = NULL;
  517. if (Token==NULL)
  518. {
  519. return NULL;
  520. }
  521. _User_Log *_UserInfoTemp = NULL;
  522. list_for_each_entry(_UserInfoTemp, &_GlobalUserLoginInfo.list, list)
  523. {
  524. if (_UserInfoTemp == NULL)
  525. break;
  526. if (strcmp(_UserInfoTemp->user_token, Token) == 0)
  527. {
  528. userName=_UserInfoTemp->user_name;
  529. }
  530. }
  531. return userName;
  532. }
  533. typedef struct {
  534. uint16_t year;
  535. uint8_t mon;
  536. uint8_t day;
  537. uint8_t hour;
  538. uint8_t min;
  539. uint8_t sec;
  540. }datetime_t;
  541. int get_bldtime(char *buf, int len)
  542. {
  543. char mon[10]={0};
  544. datetime_t dt;
  545. char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  546. sscanf(__DATE__, "%s %hhu %hu", mon,&dt.day,&dt.year);
  547. sscanf(__TIME__, "%hhu:%hhu:%hhu", &dt.hour,&dt.min,&dt.sec);
  548. for(int i=0;i<12;i++) {
  549. if(strcmp(mon, mon2[i])==0) {
  550. dt.mon = i+1; break;
  551. }
  552. }
  553. return snprintf(buf,len,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec);
  554. }
  555. int get_bldtime2(char *dst, char *ori)
  556. {
  557. char mon[10]={0};
  558. datetime_t dt;
  559. char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  560. sscanf(ori, "%s %hhu %hu %hhu:%hhu:%hhu", mon,&dt.day,&dt.year,&dt.hour,&dt.min,&dt.sec);
  561. for(int i=0;i<12;i++) {
  562. if(strcmp(mon, mon2[i])==0) {
  563. dt.mon = i+1; break;
  564. }
  565. }
  566. return sprintf(dst,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec);
  567. }
  568. int is_process_running(const char *process_name)
  569. {
  570. DIR *dir;
  571. struct dirent *entry;
  572. char path[1035]; // /proc/[pid]/cmdline 的最大路径长度(/proc/ + PID(最多5位) + /cmdline + \0)
  573. char buffer[4096]; // 假设cmdline不会超过这个长度
  574. FILE *file;
  575. dir = opendir("/proc");
  576. if (dir == NULL) {
  577. perror("opendir");
  578. return -1;
  579. }
  580. while ((entry = readdir(dir)) != NULL) {
  581. // 忽略非数字目录(即非进程目录)
  582. if (!isdigit(entry->d_name[0])) {
  583. continue;
  584. }
  585. // 构造cmdline文件的完整路径
  586. snprintf(path, sizeof(path), "/proc/%s/cmdline", entry->d_name);
  587. // 打开cmdline文件
  588. file = fopen(path, "r");
  589. if (file == NULL) {
  590. continue; // 忽略无法打开的文件
  591. }
  592. // 读取cmdline文件的内容
  593. if (fgets(buffer, sizeof(buffer), file) != NULL) {
  594. // 去除cmdline中的空字符(进程名与参数间用空字符分隔)
  595. size_t len = strlen(buffer);
  596. while (len > 0 && buffer[len - 1] == '\0') {
  597. buffer[--len] = '\0';
  598. }
  599. // 比较进程名(这里假设process_name是完整的命令行,但你可以修改逻辑来仅比较进程名)
  600. if (strstr(buffer, process_name) != NULL) {
  601. fclose(file);
  602. closedir(dir);
  603. return 1; // 进程已启动
  604. }
  605. }
  606. fclose(file);
  607. }
  608. closedir(dir);
  609. return 0; // 进程未启动
  610. }
  611. bool CheckLoginStatus(char* Token)
  612. {
  613. //return true;//先不处理401授权错误,待前端处理后再加 20240625 lyz
  614. // 没有用户登录
  615. int nLoginNum=0;
  616. _User_Log *_UserInfoTemp = NULL;
  617. list_for_each_entry(_UserInfoTemp, &_GlobalUserLoginInfo.list, list)
  618. {
  619. if (_UserInfoTemp == NULL)
  620. break;
  621. nLoginNum++;
  622. }
  623. if (nLoginNum==0)
  624. {
  625. return false;
  626. }
  627. // 根据Token查询不到登录的用户
  628. if (Token != NULL && strcmp(Token, "") != 0 && getUserNamebyToken(Token) == NULL)
  629. {
  630. return false;
  631. }
  632. return true;
  633. // return __globalDeviceManage._globalDevInfo.login_status;
  634. }
  635. // 辅助函数:判断是否是闰年
  636. int is_leap_year(int year) {
  637. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  638. }
  639. // 辅助函数:获取某月的天数
  640. int days_in_month(int year, int month) {
  641. int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  642. if (month == 2 && is_leap_year(year)) return 29;
  643. return days_per_month[month];
  644. }
  645. // 函数:增加一天到日期
  646. void add_day_to_date(char *date_str, int *year, int *month, int *day) {
  647. (*day)++;
  648. if (*day > days_in_month(*year, *month)) {
  649. (*day) = 1;
  650. (*month)++;
  651. if (*month > 12) {
  652. (*month) = 1;
  653. (*year)++;
  654. }
  655. }
  656. sprintf(date_str, "%04d-%02d-%02d", *year, *month, *day);
  657. }
  658. void SetMenuAuthority(int RoleID)
  659. {
  660. //超级管理员
  661. if (RoleID == 1)
  662. {
  663. _GlobalMenuAuthority.PowerEnabled = true;
  664. _GlobalMenuAuthority.SensorEnabled = true;
  665. _GlobalMenuAuthority.AlarmEnabled = true;
  666. _GlobalMenuAuthority.LogEnabled = true;
  667. _GlobalMenuAuthority.UserInfoEnabled = true;
  668. _GlobalMenuAuthority.CommEnabled = true;
  669. _GlobalMenuAuthority.ServiceEnabled = true;
  670. _GlobalMenuAuthority.MaintenanceEnabled = true;
  671. // 1、电源监控菜单
  672. _GlobalMenuAuthority.MonitorallEnabled = true;
  673. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  674. _GlobalMenuAuthority.SwitchtatusEnabled = true;
  675. _GlobalMenuAuthority.MonitorhistoryEnabled = true;
  676. // 2、传感器管理菜单
  677. _GlobalMenuAuthority.SensorStatusEnabled = true;
  678. _GlobalMenuAuthority.SensorHistorylEnabled = true;
  679. // 3、告警管理菜单
  680. _GlobalMenuAuthority.XtjgEnabled = true;
  681. _GlobalMenuAuthority.YzszEnabled = true;
  682. // 4、日志信息菜单
  683. _GlobalMenuAuthority.CzrzEnabled = true;
  684. _GlobalMenuAuthority.YxrzEnabled = true;
  685. // 5、用户管理菜单
  686. _GlobalMenuAuthority.UserEnabled = true;
  687. _GlobalMenuAuthority.RoleEnabled = false;
  688. _GlobalMenuAuthority.MenuEnabled = false;
  689. // 6、通信管理菜单
  690. _GlobalMenuAuthority.WkszEnabled = true;
  691. _GlobalMenuAuthority.RsszEnabled = true;
  692. // 7、服务管理菜单
  693. _GlobalMenuAuthority.SmtpszEnabled = true;
  694. _GlobalMenuAuthority.SnmpszEnabled = true;
  695. _GlobalMenuAuthority.MqszEnabled = true;
  696. _GlobalMenuAuthority.NtpszEnabled = true;
  697. #ifndef NOT_USE_CONTROL
  698. _GlobalMenuAuthority.DsszEnabled = true;
  699. _GlobalMenuAuthority.SxszEnabled = true;
  700. _GlobalMenuAuthority.FzszEnabled = true;
  701. #else
  702. _GlobalMenuAuthority.DsszEnabled = false;
  703. _GlobalMenuAuthority.SxszEnabled = false;
  704. _GlobalMenuAuthority.FzszEnabled = false;
  705. #endif
  706. // 8、设备维护菜单
  707. _GlobalMenuAuthority.SbcqEnabled = true;
  708. #ifndef NOT_USE_CONTROL
  709. _GlobalMenuAuthority.GjsjEnabled = true;
  710. #else
  711. _GlobalMenuAuthority.GjsjEnabled = false;
  712. #endif
  713. _GlobalMenuAuthority.KzbsjEnabled = true;
  714. _GlobalMenuAuthority.HhccszEnabled = true;
  715. _GlobalMenuAuthority.PzdrdcEnabled = true;
  716. _GlobalMenuAuthority.ShfwEnabled = true;
  717. _GlobalMenuAuthority.GysbEnabled = true;
  718. }
  719. // 管理员
  720. else if (RoleID == 2)
  721. {
  722. _GlobalMenuAuthority.PowerEnabled = true;
  723. _GlobalMenuAuthority.SensorEnabled = true;
  724. _GlobalMenuAuthority.AlarmEnabled = true;
  725. _GlobalMenuAuthority.LogEnabled = true;
  726. _GlobalMenuAuthority.UserInfoEnabled = true;
  727. _GlobalMenuAuthority.CommEnabled = true;
  728. _GlobalMenuAuthority.ServiceEnabled = true;
  729. _GlobalMenuAuthority.MaintenanceEnabled = true;
  730. // 1、电源监控菜单
  731. _GlobalMenuAuthority.MonitorallEnabled = true;
  732. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  733. _GlobalMenuAuthority.SwitchtatusEnabled = false;
  734. _GlobalMenuAuthority.MonitorhistoryEnabled = true;
  735. // 2、传感器管理菜单
  736. _GlobalMenuAuthority.SensorStatusEnabled = true;
  737. _GlobalMenuAuthority.SensorHistorylEnabled = true;
  738. // 3、告警管理菜单
  739. _GlobalMenuAuthority.XtjgEnabled = true;
  740. _GlobalMenuAuthority.YzszEnabled = true;
  741. // 4、日志信息菜单
  742. _GlobalMenuAuthority.CzrzEnabled = false;
  743. _GlobalMenuAuthority.YxrzEnabled = true;
  744. // 5、用户管理菜单
  745. _GlobalMenuAuthority.UserEnabled = true;
  746. _GlobalMenuAuthority.RoleEnabled = false;
  747. _GlobalMenuAuthority.MenuEnabled = false;
  748. // 6、通信管理菜单
  749. _GlobalMenuAuthority.WkszEnabled = false;
  750. _GlobalMenuAuthority.RsszEnabled = true;
  751. // 7、服务管理菜单
  752. _GlobalMenuAuthority.SmtpszEnabled = false;
  753. _GlobalMenuAuthority.SnmpszEnabled = false;
  754. _GlobalMenuAuthority.MqszEnabled = false;
  755. _GlobalMenuAuthority.NtpszEnabled = true;
  756. #ifndef NOT_USE_CONTROL
  757. _GlobalMenuAuthority.DsszEnabled = true;
  758. _GlobalMenuAuthority.SxszEnabled = true;
  759. _GlobalMenuAuthority.FzszEnabled = true;
  760. #else
  761. _GlobalMenuAuthority.DsszEnabled = false;
  762. _GlobalMenuAuthority.SxszEnabled = false;
  763. _GlobalMenuAuthority.FzszEnabled = false;
  764. #endif
  765. // 8、设备维护菜单
  766. _GlobalMenuAuthority.SbcqEnabled = true;
  767. #ifndef NOT_USE_CONTROL
  768. _GlobalMenuAuthority.GjsjEnabled = true;
  769. #else
  770. _GlobalMenuAuthority.GjsjEnabled = false;
  771. #endif
  772. _GlobalMenuAuthority.KzbsjEnabled = true;
  773. _GlobalMenuAuthority.HhccszEnabled = false;
  774. _GlobalMenuAuthority.PzdrdcEnabled = true;
  775. _GlobalMenuAuthority.ShfwEnabled = false;
  776. _GlobalMenuAuthority.GysbEnabled = true;
  777. }
  778. //普通用户
  779. else
  780. {
  781. _GlobalMenuAuthority.PowerEnabled = true;
  782. _GlobalMenuAuthority.SensorEnabled = true;
  783. _GlobalMenuAuthority.AlarmEnabled = false;
  784. _GlobalMenuAuthority.LogEnabled = false;
  785. _GlobalMenuAuthority.UserInfoEnabled = false;
  786. _GlobalMenuAuthority.CommEnabled = false;
  787. _GlobalMenuAuthority.ServiceEnabled = false;
  788. _GlobalMenuAuthority.MaintenanceEnabled = false;
  789. // 1、电源监控菜单
  790. _GlobalMenuAuthority.MonitorallEnabled = true;
  791. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  792. _GlobalMenuAuthority.SwitchtatusEnabled = false;
  793. _GlobalMenuAuthority.MonitorhistoryEnabled = false;
  794. // 2、传感器管理菜单
  795. _GlobalMenuAuthority.SensorStatusEnabled = true;
  796. _GlobalMenuAuthority.SensorHistorylEnabled = false;
  797. // 3、告警管理菜单
  798. _GlobalMenuAuthority.XtjgEnabled = false;
  799. _GlobalMenuAuthority.YzszEnabled = false;
  800. // 4、日志信息菜单
  801. _GlobalMenuAuthority.CzrzEnabled = false;
  802. _GlobalMenuAuthority.YxrzEnabled = false;
  803. // 5、用户管理菜单
  804. _GlobalMenuAuthority.UserEnabled = false;
  805. _GlobalMenuAuthority.RoleEnabled = false;
  806. _GlobalMenuAuthority.MenuEnabled = false;
  807. // 6、通信管理菜单
  808. _GlobalMenuAuthority.WkszEnabled = false;
  809. _GlobalMenuAuthority.RsszEnabled = false;
  810. // 7、服务管理菜单
  811. _GlobalMenuAuthority.SmtpszEnabled = false;
  812. _GlobalMenuAuthority.SnmpszEnabled = false;
  813. _GlobalMenuAuthority.MqszEnabled = false;
  814. _GlobalMenuAuthority.NtpszEnabled = false;
  815. _GlobalMenuAuthority.DsszEnabled = false;
  816. _GlobalMenuAuthority.SxszEnabled = false;
  817. _GlobalMenuAuthority.FzszEnabled = false;
  818. // 8、设备维护菜单
  819. _GlobalMenuAuthority.SbcqEnabled = false;
  820. _GlobalMenuAuthority.GjsjEnabled = false;
  821. _GlobalMenuAuthority.KzbsjEnabled = false;
  822. _GlobalMenuAuthority.HhccszEnabled = false;
  823. _GlobalMenuAuthority.PzdrdcEnabled = false;
  824. _GlobalMenuAuthority.ShfwEnabled = false;
  825. _GlobalMenuAuthority.GysbEnabled = false;
  826. }
  827. }