common.c 33 KB

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