common.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. char *getLocalMask(char *dev)
  208. {
  209. int fd;
  210. struct ifreq ifr;
  211. static char ip_addr[64];
  212. fd = socket(AF_INET, SOCK_DGRAM, 0);
  213. ifr.ifr_addr.sa_family = AF_INET;
  214. strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
  215. ioctl(fd, SIOCGIFNETMASK, &ifr);
  216. close(fd);
  217. char *p =inet_ntoa(((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr);
  218. strcpy(ip_addr, p);
  219. return ip_addr;
  220. }
  221. // 将32位整数IP地址转换为点分十进制格式
  222. static char* int_to_ip(unsigned int ip) {
  223. struct in_addr addr;
  224. static char ip_addr[64];
  225. addr.s_addr = ip;
  226. char *p = inet_ntoa(addr);
  227. strcpy(ip_addr, p);
  228. return ip_addr;
  229. }
  230. char *getLocalGateway(char *dev)
  231. {
  232. FILE *fp;
  233. char line[256];
  234. char iface[16];
  235. unsigned int destination, gateway, mask;
  236. int flags, refcnt, use, metric, mtu, window, irtt;
  237. unsigned int gate_way = 0;
  238. fp = fopen("/proc/net/route", "r");
  239. if (fp == NULL) {
  240. perror("fopen /proc/net/route");
  241. return NULL;
  242. }
  243. //printf("Gateways from /proc/net/route:\n");
  244. //printf("%-12s %-15s %-15s %-15s %s\n",
  245. // "Interface", "Destination", "Gateway", "Netmask", "Flags");
  246. //printf("------------ --------------- --------------- --------------- -----\n");
  247. // 跳过第一行标题
  248. fgets(line, sizeof(line), fp);
  249. while (fgets(line, sizeof(line), fp)) {
  250. int count = sscanf(line, "%15s %x %x %x %d %d %d %x %d %d %d",
  251. iface, &destination, &gateway, &mask,
  252. &flags, &refcnt, &use, &metric, &mtu, &window, &irtt);
  253. if (count >= 4) {
  254. // 显示默认网关(目标为0.0.0.0)或所有路由
  255. // printf("%-12s %-15s %-15s %-15s 0x%04X\n",
  256. // iface,
  257. // int_to_ip(destination),
  258. // int_to_ip(gateway),
  259. // int_to_ip(mask),
  260. // flags);
  261. // int_to_ip(destination)
  262. //sprintf(gate_way,"%s",gateway);
  263. gate_way = destination;
  264. }
  265. }
  266. fclose(fp);
  267. return int_to_ip(gate_way);
  268. }
  269. /// @brief 获取磁盘使用率
  270. /// @param reused
  271. void get_disk_occupy(char ** reused)
  272. {
  273. const unsigned short MAXBUFSIZE = 1024;
  274. char currentDirectoryPath[ MAXBUFSIZE ];
  275. getcwd(currentDirectoryPath, MAXBUFSIZE);
  276. //printf("当前目录:%s\n",currentDirectoryPath);
  277. char cmd[50] = "df ";
  278. strcat(cmd, currentDirectoryPath);
  279. //printf("%s\n",cmd);
  280. char buffer[MAXBUFSIZE];
  281. FILE* pipe = popen(cmd, "r");
  282. char fileSys[20];
  283. char blocks[20];
  284. char used[20];
  285. char free[20];
  286. char percent[10];
  287. char moment[20];
  288. if (!pipe)
  289. {
  290. return;
  291. }
  292. if (fgets(buffer, sizeof(buffer), pipe) != NULL)
  293. {
  294. sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
  295. }
  296. if (fgets(buffer, sizeof(buffer), pipe) != NULL)
  297. {
  298. sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
  299. }
  300. //printf("desk used:%s\n",percent);
  301. strcpy(*reused, percent);
  302. return;
  303. }
  304. static int set_rtc(time_t t)
  305. {
  306. int r=0,fd;
  307. // #if (CHIP_TYPE == CHIP_T113s)
  308. // system("hwclock -w -f /dev/rtc0");
  309. // #else
  310. struct rtc_time *rtc;
  311. struct tm *tm = gmtime(&t);
  312. fd = open("/dev/rtc0", O_RDONLY);
  313. if (fd < 0) {
  314. perror("open rtc failed\n");
  315. return -1;
  316. }
  317. rtc = (struct rtc_time*)tm;
  318. if (ioctl(fd, RTC_SET_TIME, rtc) < 0) {
  319. perror("ioctl RTC_SET_TIME failed\n");
  320. r = -1;
  321. }
  322. else {
  323. printf("set rtc ok!\n");
  324. }
  325. close(fd);
  326. //#endif
  327. return r;
  328. }
  329. #define MY_MINUTE 60
  330. #define MY_HOUR (60*MY_MINUTE)
  331. #define MY_DAY (24*MY_HOUR)
  332. #define MY_YEAR (365*MY_DAY)
  333. static int my_month[12] = {
  334. 0,
  335. MY_DAY*(31),
  336. MY_DAY*(31+29),
  337. MY_DAY*(31+29+31),
  338. MY_DAY*(31+29+31+30),
  339. MY_DAY*(31+29+31+30+31),
  340. MY_DAY*(31+29+31+30+31+30),
  341. MY_DAY*(31+29+31+30+31+30+31),
  342. MY_DAY*(31+29+31+30+31+30+31+31),
  343. MY_DAY*(31+29+31+30+31+30+31+31+30),
  344. MY_DAY*(31+29+31+30+31+30+31+31+30+31),
  345. MY_DAY*(31+29+31+30+31+30+31+31+30+31+30)
  346. };
  347. time_t mktime2(struct tm *t, int tzone)
  348. {
  349. time_t res,year;
  350. year = t->tm_year - 70;
  351. /* magic offsets (y+1) needed to get leapyears right.*/
  352. res = MY_YEAR*year + MY_DAY*((year+1)/4);
  353. res += my_month[t->tm_mon];
  354. /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
  355. if (t->tm_mon>1 && ((year+2)%4))
  356. res -= MY_DAY;
  357. res += MY_DAY*(t->tm_mday-1);
  358. res += MY_HOUR*t->tm_hour;
  359. res += MY_MINUTE*t->tm_min;
  360. res += t->tm_sec;
  361. res -= tzone*3600;
  362. return res;
  363. }
  364. time_t mktime0(void)
  365. {
  366. struct tm t;
  367. t.tm_year = 70;
  368. t.tm_mon = 0;
  369. t.tm_mday = 1;
  370. t.tm_hour = 0;
  371. t.tm_min = 0;
  372. t.tm_sec = 0;
  373. return mktime(&t);
  374. }
  375. int set_time(struct tm *t, int tzone)
  376. {
  377. if(!t) {
  378. return -1;
  379. }
  380. t->tm_year -= 1900;
  381. t->tm_mon -= 1;
  382. time_t utc = mktime2(t, tzone);
  383. stime(&utc);
  384. set_rtc(utc);
  385. return 0;
  386. }
  387. int set_utctime(time_t t)
  388. {
  389. stime(&t);
  390. set_rtc(t);
  391. #if (CHIP_TYPE == CHIP_T113s)
  392. sleep(1);
  393. system("hwclock --show");
  394. system("hwclock -s");
  395. #endif
  396. return 0;
  397. }
  398. int get_tzone(void)
  399. {
  400. int tz = 0;
  401. #if (CHIP_TYPE == CHIP_T113s)
  402. time_t t1,t2;
  403. struct tm *tm_local,*tm_utc;
  404. time(&t1);
  405. t2=t1;
  406. tm_local = localtime(&t1);
  407. t1=mktime(tm_local);
  408. tm_utc = gmtime(&t2);
  409. t2=mktime(tm_utc);
  410. tz = (t1 - t2)/3600;
  411. log_d("tz = %d t1=%d t2=%d\n",tz,t1,t2);
  412. #else
  413. time_t now = time(NULL);
  414. struct tm *local = localtime(&now);
  415. time_t utc = mktime2(local, 0);
  416. tz = -((now - utc)/3600);
  417. #endif
  418. return tz;
  419. }
  420. int set_tzone(int tzone)
  421. {
  422. int r,tz;
  423. char buf[256];
  424. tz = get_tzone();
  425. #if 0
  426. switch(zone) {
  427. case -11:
  428. sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Midway /etc/localtime");
  429. break;
  430. case -10:
  431. sprintf(buf, "ln -sf /usr/share/zoneinfo/HST /etc/localtime");
  432. break;
  433. case -9:
  434. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Adak /etc/localtime");
  435. break;
  436. case -8:
  437. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Boise /etc/localtime");
  438. break;
  439. case -7:
  440. sprintf(buf, "ln -sf /usr/share/zoneinfo/MST /etc/localtime");
  441. break;
  442. case -6:
  443. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime");
  444. break;
  445. case -5:
  446. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime");
  447. break;
  448. case -4:
  449. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Halifax /etc/localtime");
  450. break;
  451. case -3:
  452. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Maceio /etc/localtime");
  453. break;
  454. case -2:
  455. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Noronha /etc/localtime");
  456. break;
  457. case -1:
  458. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Scoresbysund /etc/localtime");
  459. break;
  460. case 0:
  461. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime");
  462. break;
  463. case 1:
  464. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime");
  465. break;
  466. case 2:
  467. sprintf(buf, "ln -sf /usr/share/zoneinfo/EET /etc/localtime");
  468. break;
  469. case 3:
  470. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime");
  471. break;
  472. case 4:
  473. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Amman /etc/localtime");
  474. break;
  475. case 5:
  476. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Karachi /etc/localtime");
  477. break;
  478. case 6:
  479. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Dhaka /etc/localtime");
  480. break;
  481. case 7:
  482. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Bangkok /etc/localtime");
  483. break;
  484. case 8:
  485. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime");
  486. break;
  487. case 9:
  488. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime");
  489. break;
  490. case 10:
  491. sprintf(buf, "ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime");
  492. break;
  493. case 11:
  494. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Sakhalin /etc/localtime");
  495. break;
  496. case 12:
  497. sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Fiji /etc/localtime");
  498. break;
  499. default:
  500. return -1;
  501. }
  502. #else
  503. #if (CHIP_TYPE == CHIP_T113s)
  504. sprintf(buf, "ln -sf /usr/share/zoneinfo/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone));
  505. #else
  506. sprintf(buf, "ln -sf /usr/share/zoneinfo/Etc/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone));
  507. #endif
  508. #endif
  509. log_i("set_zone: %s", buf);
  510. system(buf);
  511. if(tz!=tzone) {
  512. log_e("old tzone: %d new tzone %d\n",tz,tzone);
  513. sys_reboot();
  514. }
  515. return 0;
  516. }
  517. void print_time(char *s, struct tm *tm)
  518. {
  519. 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);
  520. }
  521. void print_utc(char *s, time_t t)
  522. {
  523. struct tm *tm=gmtime(&t);
  524. tm->tm_year += 1900;
  525. tm->tm_mon += 1;
  526. print_time(s, tm);
  527. }
  528. int set_time_zone(struct tm *t, int zone)
  529. {
  530. int r;
  531. if(!t) {
  532. return -1;
  533. }
  534. r = set_time(t, zone);
  535. if(r) {
  536. log_e("__1__ set_time failed\n");
  537. }
  538. r = set_tzone(zone);
  539. if(r) {
  540. log_e("__1__ set_tzone failed\n");
  541. }
  542. return r;
  543. }
  544. int set_utctime_zone(time_t t, int zone)
  545. {
  546. int r;
  547. r = set_utctime(t);
  548. if(r) {
  549. log_e("__2__ set_time failed\n");
  550. }
  551. r = set_tzone(zone);
  552. if(r) {
  553. log_e("__2__ set_zone failed\n");
  554. }
  555. return r;
  556. }
  557. int get_time_str(char *s, int len, time_t t)
  558. {
  559. struct tm *tm=localtime(&t);
  560. 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);
  561. }
  562. int get_time_str2(char *s, int len, time_t t)
  563. {
  564. struct tm *tm=localtime(&t);
  565. 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);
  566. }
  567. int get_curtime_str(char *s, int len)
  568. {
  569. time_t t=time(NULL);
  570. struct tm *tm=localtime(&t);
  571. 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);
  572. }
  573. int get_curtime_str2(char *s, int len)
  574. {
  575. time_t t=time(NULL);
  576. struct tm *tm=localtime(&t);
  577. 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);
  578. }
  579. typedef struct {
  580. uint16_t year;
  581. uint8_t mon;
  582. uint8_t day;
  583. uint8_t hour;
  584. uint8_t min;
  585. uint8_t sec;
  586. }datetime_t;
  587. int get_bldtime(char *buf, int len)
  588. {
  589. char mon[10]={0};
  590. datetime_t dt;
  591. char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  592. sscanf(__DATE__, "%s %hhu %hu", mon,&dt.day,&dt.year);
  593. sscanf(__TIME__, "%hhu:%hhu:%hhu", &dt.hour,&dt.min,&dt.sec);
  594. for(int i=0;i<12;i++) {
  595. if(strcmp(mon, mon2[i])==0) {
  596. dt.mon = i+1; break;
  597. }
  598. }
  599. return snprintf(buf,len,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec);
  600. }
  601. int get_bldtime2(char *dst, char *ori)
  602. {
  603. char mon[10]={0};
  604. datetime_t dt;
  605. char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  606. sscanf(ori, "%s %hhu %hu %hhu:%hhu:%hhu", mon,&dt.day,&dt.year,&dt.hour,&dt.min,&dt.sec);
  607. for(int i=0;i<12;i++) {
  608. if(strcmp(mon, mon2[i])==0) {
  609. dt.mon = i+1; break;
  610. }
  611. }
  612. return sprintf(dst,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec);
  613. }
  614. int is_process_running(const char *process_name)
  615. {
  616. DIR *dir;
  617. struct dirent *entry;
  618. char path[1035]; // /proc/[pid]/cmdline 的最大路径长度(/proc/ + PID(最多5位) + /cmdline + \0)
  619. char buffer[4096]; // 假设cmdline不会超过这个长度
  620. FILE *file;
  621. dir = opendir("/proc");
  622. if (dir == NULL) {
  623. perror("opendir");
  624. return -1;
  625. }
  626. while ((entry = readdir(dir)) != NULL) {
  627. // 忽略非数字目录(即非进程目录)
  628. if (!isdigit(entry->d_name[0])) {
  629. continue;
  630. }
  631. // 构造cmdline文件的完整路径
  632. snprintf(path, sizeof(path), "/proc/%s/cmdline", entry->d_name);
  633. // 打开cmdline文件
  634. file = fopen(path, "r");
  635. if (file == NULL) {
  636. continue; // 忽略无法打开的文件
  637. }
  638. // 读取cmdline文件的内容
  639. if (fgets(buffer, sizeof(buffer), file) != NULL) {
  640. // 去除cmdline中的空字符(进程名与参数间用空字符分隔)
  641. size_t len = strlen(buffer);
  642. while (len > 0 && buffer[len - 1] == '\0') {
  643. buffer[--len] = '\0';
  644. }
  645. // 比较进程名(这里假设process_name是完整的命令行,但你可以修改逻辑来仅比较进程名)
  646. if (strstr(buffer, process_name) != NULL) {
  647. fclose(file);
  648. closedir(dir);
  649. return 1; // 进程已启动
  650. }
  651. }
  652. fclose(file);
  653. }
  654. closedir(dir);
  655. return 0; // 进程未启动
  656. }
  657. // 辅助函数:判断是否是闰年
  658. int is_leap_year(int year) {
  659. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  660. }
  661. // 辅助函数:获取某月的天数
  662. int days_in_month(int year, int month) {
  663. int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  664. if (month == 2 && is_leap_year(year)) return 29;
  665. return days_per_month[month];
  666. }
  667. // 函数:增加一天到日期
  668. void add_day_to_date(char *date_str, int *year, int *month, int *day) {
  669. (*day)++;
  670. if (*day > days_in_month(*year, *month)) {
  671. (*day) = 1;
  672. (*month)++;
  673. if (*month > 12) {
  674. (*month) = 1;
  675. (*year)++;
  676. }
  677. }
  678. sprintf(date_str, "%04d-%02d-%02d", *year, *month, *day);
  679. }
  680. // 函数:将 UTF-8 字符串转换为 Unicode 字符串(十六进制表示)
  681. char* utf8_to_unicode_string(const char* utf8_str)
  682. {
  683. // 计算 Unicode 字符串的最大可能长度(每个字符最多 4 个十六进制字符 + 空格)
  684. size_t max_len = strlen(utf8_str) * 4 * 2; // 每个字节最多 4 个十六进制字符,加上空格
  685. char* unicode_str = malloc(max_len + 1); // +1 for null terminator
  686. if (!unicode_str) {
  687. return NULL; // 内存分配失败
  688. }
  689. size_t pos = 0;
  690. while (*utf8_str) {
  691. uint32_t code_point = 0;
  692. unsigned char c = *utf8_str;
  693. // 根据 UTF-8 编码规则解析
  694. if ((c & 0x80) == 0) { // 单字节(ASCII)
  695. code_point = c;
  696. utf8_str++;
  697. } else if ((c & 0xE0) == 0xC0) { // 双字节
  698. code_point = ((c & 0x1F) << 6) | (utf8_str[1] & 0x3F);
  699. utf8_str += 2;
  700. } else if ((c & 0xF0) == 0xE0) { // 三字节(常见于中文)
  701. code_point = ((c & 0x0F) << 12) | ((utf8_str[1] & 0x3F) << 6) | (utf8_str[2] & 0x3F);
  702. utf8_str += 3;
  703. } else { // 其他情况(如四字节)
  704. // 简单处理,实际需更复杂的逻辑
  705. utf8_str++;
  706. continue;
  707. }
  708. // 将 Unicode 码点格式化为十六进制字符串
  709. pos += sprintf(unicode_str + pos, "%04X", code_point);
  710. }
  711. // 移除最后一个空格(可选)
  712. if (pos > 0 && unicode_str[pos - 1] == ' ') {
  713. unicode_str[pos - 1] = '\0';
  714. } else {
  715. unicode_str[pos] = '\0';
  716. }
  717. return unicode_str;
  718. }
  719. // 将中文字符串转换为 UCS2 编码(十六进制格式)
  720. int chinese_to_ucs2(const char* utf8_str, char* ucs2_hex, size_t ucs2_hex_size)
  721. {
  722. iconv_t cd = iconv_open("UCS-2BE", "UTF-8"); // 转换为大端序 UCS2
  723. if (cd == (iconv_t)-1) {
  724. perror("iconv_open 失败");
  725. return -1;
  726. }
  727. size_t in_len = strlen(utf8_str);
  728. size_t out_len = in_len * 4; // 每个 UTF-8 字符最多转换为 4 字节 UCS2
  729. char* in_buf = (char*)utf8_str;
  730. char* out_buf = malloc(out_len);
  731. if (!out_buf) {
  732. perror("内存分配失败");
  733. iconv_close(cd);
  734. return -1;
  735. }
  736. char* out_ptr = out_buf;
  737. if (iconv(cd, &in_buf, &in_len, &out_ptr, &out_len) == (size_t)-1) {
  738. perror("iconv 转换失败");
  739. free(out_buf);
  740. iconv_close(cd);
  741. return -1;
  742. }
  743. // 将 UCS2 字节流格式化为十六进制字符串
  744. size_t hex_len = 0;
  745. for (size_t i = 0; i < (out_ptr - out_buf) / 2; i++) { // UCS2 是 2 字节
  746. unsigned char byte1 = out_buf[i * 2];
  747. unsigned char byte2 = out_buf[i * 2 + 1];
  748. hex_len += snprintf(ucs2_hex + hex_len, ucs2_hex_size - hex_len, "%02X%02X", byte1, byte2);
  749. }
  750. ucs2_hex[hex_len] = '\0';
  751. free(out_buf);
  752. iconv_close(cd);
  753. return 0;
  754. }
  755. void SetMenuAuthority(int RoleID)
  756. {
  757. //超级管理员
  758. if (RoleID == 1)
  759. {
  760. _GlobalMenuAuthority.PowerEnabled = true;
  761. _GlobalMenuAuthority.SensorEnabled = true;
  762. _GlobalMenuAuthority.AlarmEnabled = true;
  763. _GlobalMenuAuthority.LogEnabled = true;
  764. _GlobalMenuAuthority.UserInfoEnabled = true;
  765. _GlobalMenuAuthority.CommEnabled = true;
  766. _GlobalMenuAuthority.ServiceEnabled = true;
  767. _GlobalMenuAuthority.MaintenanceEnabled = true;
  768. // 1、电源监控菜单
  769. _GlobalMenuAuthority.MonitorallEnabled = true;
  770. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  771. _GlobalMenuAuthority.SwitchtatusEnabled = true;
  772. _GlobalMenuAuthority.MonitorhistoryEnabled = true;
  773. // 2、传感器管理菜单
  774. _GlobalMenuAuthority.SensorStatusEnabled = true;
  775. _GlobalMenuAuthority.SensorHistorylEnabled = true;
  776. // 3、告警管理菜单
  777. _GlobalMenuAuthority.XtjgEnabled = true;
  778. _GlobalMenuAuthority.YzszEnabled = true;
  779. // 4、日志信息菜单
  780. _GlobalMenuAuthority.CzrzEnabled = true;
  781. _GlobalMenuAuthority.YxrzEnabled = true;
  782. // 5、用户管理菜单
  783. _GlobalMenuAuthority.UserEnabled = true;
  784. _GlobalMenuAuthority.RoleEnabled = false;
  785. _GlobalMenuAuthority.MenuEnabled = false;
  786. // 6、通信管理菜单
  787. _GlobalMenuAuthority.WkszEnabled = true;
  788. _GlobalMenuAuthority.RsszEnabled = true;
  789. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  790. _GlobalMenuAuthority.CellularEnabled = true;
  791. else
  792. _GlobalMenuAuthority.CellularEnabled = false;
  793. if (__globalDeviceManage._globalDevInfo.wifi.WIFI_enable)
  794. _GlobalMenuAuthority.WifiEnabled = true;
  795. else
  796. _GlobalMenuAuthority.WifiEnabled = false;
  797. // 7、服务管理菜单
  798. _GlobalMenuAuthority.SmtpszEnabled = true;
  799. _GlobalMenuAuthority.SnmpszEnabled = true;
  800. _GlobalMenuAuthority.MqszEnabled = true;
  801. _GlobalMenuAuthority.NtpszEnabled = true;
  802. #ifndef NOT_USE_CONTROL
  803. _GlobalMenuAuthority.DsszEnabled = true;
  804. _GlobalMenuAuthority.SxszEnabled = true;
  805. _GlobalMenuAuthority.FzszEnabled = true;
  806. #else
  807. _GlobalMenuAuthority.DsszEnabled = false;
  808. _GlobalMenuAuthority.SxszEnabled = false;
  809. _GlobalMenuAuthority.FzszEnabled = false;
  810. #endif
  811. // 8、设备维护菜单
  812. _GlobalMenuAuthority.SbcqEnabled = true;
  813. #ifndef NOT_USE_CONTROL
  814. _GlobalMenuAuthority.GjsjEnabled = true;
  815. #else
  816. _GlobalMenuAuthority.GjsjEnabled = false;
  817. #endif
  818. _GlobalMenuAuthority.KzbsjEnabled = false;
  819. _GlobalMenuAuthority.HhccszEnabled = true;
  820. _GlobalMenuAuthority.PzdrdcEnabled = true;
  821. _GlobalMenuAuthority.ShfwEnabled = true;
  822. _GlobalMenuAuthority.GysbEnabled = true;
  823. _GlobalMenuAuthority.BroadcastEnable = true;
  824. }
  825. // 管理员
  826. else if (RoleID == 2)
  827. {
  828. _GlobalMenuAuthority.PowerEnabled = true;
  829. _GlobalMenuAuthority.SensorEnabled = true;
  830. _GlobalMenuAuthority.AlarmEnabled = true;
  831. _GlobalMenuAuthority.LogEnabled = true;
  832. _GlobalMenuAuthority.UserInfoEnabled = true;
  833. _GlobalMenuAuthority.CommEnabled = true;
  834. _GlobalMenuAuthority.ServiceEnabled = true;
  835. _GlobalMenuAuthority.MaintenanceEnabled = true;
  836. // 1、电源监控菜单
  837. _GlobalMenuAuthority.MonitorallEnabled = true;
  838. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  839. _GlobalMenuAuthority.SwitchtatusEnabled = false;
  840. _GlobalMenuAuthority.MonitorhistoryEnabled = true;
  841. // 2、传感器管理菜单
  842. _GlobalMenuAuthority.SensorStatusEnabled = true;
  843. _GlobalMenuAuthority.SensorHistorylEnabled = true;
  844. // 3、告警管理菜单
  845. _GlobalMenuAuthority.XtjgEnabled = true;
  846. _GlobalMenuAuthority.YzszEnabled = true;
  847. // 4、日志信息菜单
  848. _GlobalMenuAuthority.CzrzEnabled = false;
  849. _GlobalMenuAuthority.YxrzEnabled = true;
  850. // 5、用户管理菜单
  851. _GlobalMenuAuthority.UserEnabled = true;
  852. _GlobalMenuAuthority.RoleEnabled = false;
  853. _GlobalMenuAuthority.MenuEnabled = false;
  854. // 6、通信管理菜单
  855. _GlobalMenuAuthority.WkszEnabled = false;
  856. _GlobalMenuAuthority.RsszEnabled = true;
  857. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  858. _GlobalMenuAuthority.CellularEnabled = true;
  859. else
  860. _GlobalMenuAuthority.CellularEnabled = false;
  861. if (__globalDeviceManage._globalDevInfo.wifi.WIFI_enable)
  862. _GlobalMenuAuthority.WifiEnabled = true;
  863. else
  864. _GlobalMenuAuthority.WifiEnabled = false;
  865. // 7、服务管理菜单
  866. _GlobalMenuAuthority.SmtpszEnabled = false;
  867. _GlobalMenuAuthority.SnmpszEnabled = false;
  868. _GlobalMenuAuthority.MqszEnabled = false;
  869. _GlobalMenuAuthority.NtpszEnabled = true;
  870. #ifndef NOT_USE_CONTROL
  871. _GlobalMenuAuthority.DsszEnabled = true;
  872. _GlobalMenuAuthority.SxszEnabled = true;
  873. _GlobalMenuAuthority.FzszEnabled = true;
  874. #else
  875. _GlobalMenuAuthority.DsszEnabled = false;
  876. _GlobalMenuAuthority.SxszEnabled = false;
  877. _GlobalMenuAuthority.FzszEnabled = false;
  878. #endif
  879. // 8、设备维护菜单
  880. _GlobalMenuAuthority.SbcqEnabled = true;
  881. #if (CHIP_TYPE == CHIP_T113s)
  882. _GlobalMenuAuthority.GjsjEnabled = false;
  883. #else
  884. _GlobalMenuAuthority.GjsjEnabled = true;
  885. #endif
  886. _GlobalMenuAuthority.KzbsjEnabled = false;
  887. _GlobalMenuAuthority.HhccszEnabled = false;
  888. _GlobalMenuAuthority.PzdrdcEnabled = true;
  889. _GlobalMenuAuthority.ShfwEnabled = false;
  890. _GlobalMenuAuthority.GysbEnabled = true;
  891. _GlobalMenuAuthority.BroadcastEnable = true;
  892. }
  893. //普通用户
  894. else
  895. {
  896. _GlobalMenuAuthority.PowerEnabled = true;
  897. _GlobalMenuAuthority.SensorEnabled = true;
  898. _GlobalMenuAuthority.AlarmEnabled = false;
  899. _GlobalMenuAuthority.LogEnabled = false;
  900. _GlobalMenuAuthority.UserInfoEnabled = false;
  901. _GlobalMenuAuthority.CommEnabled = false;
  902. _GlobalMenuAuthority.ServiceEnabled = false;
  903. _GlobalMenuAuthority.MaintenanceEnabled = false;
  904. // 1、电源监控菜单
  905. _GlobalMenuAuthority.MonitorallEnabled = true;
  906. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  907. _GlobalMenuAuthority.SwitchtatusEnabled = false;
  908. _GlobalMenuAuthority.MonitorhistoryEnabled = false;
  909. // 2、传感器管理菜单
  910. _GlobalMenuAuthority.SensorStatusEnabled = true;
  911. _GlobalMenuAuthority.SensorHistorylEnabled = false;
  912. // 3、告警管理菜单
  913. _GlobalMenuAuthority.XtjgEnabled = false;
  914. _GlobalMenuAuthority.YzszEnabled = false;
  915. // 4、日志信息菜单
  916. _GlobalMenuAuthority.CzrzEnabled = false;
  917. _GlobalMenuAuthority.YxrzEnabled = false;
  918. // 5、用户管理菜单
  919. _GlobalMenuAuthority.UserEnabled = false;
  920. _GlobalMenuAuthority.RoleEnabled = false;
  921. _GlobalMenuAuthority.MenuEnabled = false;
  922. // 6、通信管理菜单
  923. _GlobalMenuAuthority.WkszEnabled = false;
  924. _GlobalMenuAuthority.RsszEnabled = false;
  925. _GlobalMenuAuthority.CellularEnabled = false;
  926. _GlobalMenuAuthority.WifiEnabled = false;
  927. // 7、服务管理菜单
  928. _GlobalMenuAuthority.SmtpszEnabled = false;
  929. _GlobalMenuAuthority.SnmpszEnabled = false;
  930. _GlobalMenuAuthority.MqszEnabled = false;
  931. _GlobalMenuAuthority.NtpszEnabled = false;
  932. _GlobalMenuAuthority.DsszEnabled = false;
  933. _GlobalMenuAuthority.SxszEnabled = false;
  934. _GlobalMenuAuthority.FzszEnabled = false;
  935. // 8、设备维护菜单
  936. _GlobalMenuAuthority.SbcqEnabled = false;
  937. _GlobalMenuAuthority.GjsjEnabled = false;
  938. _GlobalMenuAuthority.KzbsjEnabled = false;
  939. _GlobalMenuAuthority.HhccszEnabled = false;
  940. _GlobalMenuAuthority.PzdrdcEnabled = false;
  941. _GlobalMenuAuthority.ShfwEnabled = false;
  942. _GlobalMenuAuthority.GysbEnabled = false;
  943. }
  944. }