common.c 32 KB

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