#include "common.h" #include "elog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "cfg.h" #include "sys.h" #undef LOG_TAG #define LOG_TAG "main" GlobalDeviceManager __globalDeviceManage={0}; GlobalDeviceManager __globalDeviceManage2={0}; int ntp_sync_flag=0; unsigned long __globalSxIdx; _User_Log _GlobalUserLoginInfo; UserInfo _GlobalUserInfoNew; char _GlobalUserToken[64]; MenuAuthority _GlobalMenuAuthority; NTPManager _globalNTPManager; SNMPManager _globalSNMPManager; int SMS_SEND_COUNT = 0; char DS_QueryDate[32]; void printf_global_device_info(GlobalDeviceInfo* _globalDeviceInfo) { log_d("******global device info********* "); log_d("product.id:%d ", _globalDeviceInfo->product.id); log_d("product.number:%s ", _globalDeviceInfo->product.number); log_d("product.name:%s ", _globalDeviceInfo->product.name); log_d("product.ype:%d ", _globalDeviceInfo->product.type); log_d("product.pwr_type:%d ", _globalDeviceInfo->product.pwr_type); log_d("product.version:%s ", _globalDeviceInfo->product.version); log_d("product.count:%d ", _globalDeviceInfo->product.count); log_d("product.samp_time:%d ", _globalDeviceInfo->product.samp_time); log_d("product.save_time:%d ", _globalDeviceInfo->product.save_time); log_d("cascade.mode:%d ", _globalDeviceInfo->cascade.mode); log_d("cascade.addr:%d ", _globalDeviceInfo->cascade.addr); log_d("cascade.baudrate:%d ", _globalDeviceInfo->cascade.baudrate); log_d("relay.path:%s ", _globalDeviceInfo->relay.path); log_d("******************************** "); } void printf_global_power_manage_info(GlobalPowerManger* _globalPowerManger) { printf("******global power manage info********* "); printf("product_id:%d ", _globalPowerManger->product_id); printf("product_ch_id:%d ", _globalPowerManger->product_ch_id); printf("product_ch_name:%s ", _globalPowerManger->product_ch_name); printf("product_ch_type:%d ", _globalPowerManger->product_ch_type); printf("product_ch_status:%d ", _globalPowerManger->product_ch_status); printf("product_ch_NF_status:%d ", _globalPowerManger->product_ch_NF_status); printf("product_ch_start_delay:%d ", _globalPowerManger->product_ch_start_delay); printf("product_ch_stop_delay:%d ", _globalPowerManger->product_ch_stop_delay); printf("product_vol_upper_enable:%d ", _globalPowerManger->global_over_manager.product_vol_upper_enable); printf("product_vol_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_vol_upper_threshold); printf("product_vol_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_vol_over_upper_threshold_ctrl); printf("product_vol_over_upper_threshold_para:%d ", _globalPowerManger->global_over_manager.product_vol_over_upper_threshold_ctrl_para); printf("product_vol_lower_enable:%d ", _globalPowerManger->global_over_manager.product_vol_lower_enable); printf("product_vol_lower_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_vol_lower_threshold); printf("product_vol_over_lower_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_vol_over_lower_threshold_ctrl); printf("product_vol_over_lower_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_vol_over_lower_threshold_ctrl_para); printf("product_cur_upper_enable:%d ", _globalPowerManger->global_over_manager.product_cur_upper_enable); printf("product_cur_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_cur_upper_threshold); printf("product_cur_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_cur_over_upper_threshold_ctrl); printf("product_cur_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_cur_over_upper_threshold_ctrl_para); printf("product_pwr_upper_enable:%d ", _globalPowerManger->global_over_manager.product_pwr_upper_enable); printf("product_pwr_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_pwr_upper_threshold); printf("product_pwr_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_pwr_over_upper_threshold_ctrl); printf("product_pwr_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_pwr_over_upper_threshold_ctrl_para); printf("product_pwrcon_upper_enable:%d ", _globalPowerManger->global_over_manager.product_pwrcon_upper_enable); printf("product_pwrcon_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_pwrcon_upper_threshold); printf("product_pwrcon_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_pwrcon_over_upper_threshold_ctrl); printf("product_pwrcon_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_pwrcon_over_upper_threshold_ctrl_para); printf("******************************** "); } //打印电源信息 void printf_power_info(GlobalPowerInfo* _globalPowerInfo) { struct list_head *pos; GlobalPowerInfo* _temp; int i = 0; list_for_each_entry(_temp, &_globalPowerInfo->list, list) { printf("i:%d ", i++); printf("samp_time:%s ", _temp->samp_time); printf("product_id:%d ", _temp->product_id); } } void INIT_LIST_HEAD(struct list_head *list) { list->next = list->prev = list; } /** * EasyLogger demo */ // void test_elog(void) { // uint8_t buf[256]= {0}; // int i = 0; // for (i = 0; i < sizeof(buf); i++) // { // buf[i] = i; // } // while(true) { // /* test log output for all level */ // log_a("Hello EasyLogger!"); // log_e("Hello EasyLogger!"); // log_w("Hello EasyLogger!"); // log_i("Hello EasyLogger!"); // log_d("Hello EasyLogger!"); // log_v("Hello EasyLogger!"); // // elog_raw("Hello EasyLogger!"); // elog_hexdump("test", 16, buf, sizeof(buf)); // sleep(5); // } // } #if(1) void initLogger(void) { setbuf(stdout, NULL); elog_init(); elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL); elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC); elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC); elog_set_text_color_enabled(true); elog_start(); log_i("Smart Pdu Application Running!"); } #endif /// @brief 获取系统mem大小 /// @param totalSize /// @return int getTotalMemorySize(unsigned int* totalSize) { struct sysinfo memInfo; if (sysinfo(&memInfo) != -1) { *totalSize = memInfo.totalram; // printf("Total RAM size: %lu bytes\n", memInfo.totalram); // printf("Free RAM size: %lu bytes\n", memInfo.freeram); } else { *totalSize = 0 ; return -1; } return 0; } /// @brief 返回设备IP地址 /// @param dev 网口名 /// @return char* getLocalIpAddress(char* dev) { int fd; struct ifreq ifr; static char ip_addr[64]; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET; strncpy(ifr.ifr_name, dev, IFNAMSIZ-1); ioctl(fd, SIOCGIFADDR, &ifr); close(fd); char *p=inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); if(!p) { return NULL; } strcpy(ip_addr, p); return ip_addr; } // 获取MAC地址的函数 char* get_mac_address(const char *iface_name) { int fd; struct ifreq ifr; unsigned char *mac; static char mac_str[32]; snprintf(mac_str, sizeof(mac_str), "%02d:%02d:%02d:%02d:%02d:%02d", 0, 0, 0, 0, 0, 0); // 创建一个socket if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket error"); return mac_str; } // 设置接口名称 strncpy(ifr.ifr_name, iface_name, IFNAMSIZ - 1); // 获取MAC地址 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { perror("ioctl error"); close(fd); return mac_str; } // ifr.ifr_hwaddr.sa_data是一个包含硬件地址的字节数组 mac = (unsigned char *)ifr.ifr_hwaddr.sa_data; // 将MAC地址转换为字符串格式 snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); // 关闭socket close(fd); return mac_str; } char *getLocalMask(char *dev) { int fd; struct ifreq ifr; static char ip_addr[64]; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET; strncpy(ifr.ifr_name, dev, IFNAMSIZ-1); ioctl(fd, SIOCGIFNETMASK, &ifr); close(fd); char *p =inet_ntoa(((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr); strcpy(ip_addr, p); return ip_addr; } // 将32位整数IP地址转换为点分十进制格式 static char* int_to_ip(unsigned int ip) { struct in_addr addr; static char ip_addr[64]; addr.s_addr = ip; char *p = inet_ntoa(addr); strcpy(ip_addr, p); return ip_addr; } char *getLocalGateway(char *dev) { FILE *fp; char line[256]; char iface[16]; unsigned int destination, gateway, mask; int flags, refcnt, use, metric, mtu, window, irtt; unsigned int gate_way = 0; fp = fopen("/proc/net/route", "r"); if (fp == NULL) { perror("fopen /proc/net/route"); return NULL; } //printf("Gateways from /proc/net/route:\n"); //printf("%-12s %-15s %-15s %-15s %s\n", // "Interface", "Destination", "Gateway", "Netmask", "Flags"); //printf("------------ --------------- --------------- --------------- -----\n"); // 跳过第一行标题 fgets(line, sizeof(line), fp); while (fgets(line, sizeof(line), fp)) { int count = sscanf(line, "%15s %x %x %x %d %d %d %x %d %d %d", iface, &destination, &gateway, &mask, &flags, &refcnt, &use, &metric, &mtu, &window, &irtt); if (count >= 4) { // 显示默认网关(目标为0.0.0.0)或所有路由 // printf("%-12s %-15s %-15s %-15s 0x%04X\n", // iface, // int_to_ip(destination), // int_to_ip(gateway), // int_to_ip(mask), // flags); // int_to_ip(destination) //sprintf(gate_way,"%s",gateway); gate_way = destination; } } fclose(fp); return int_to_ip(gate_way); } /// @brief 获取磁盘使用率 /// @param reused void get_disk_occupy(char ** reused) { const unsigned short MAXBUFSIZE = 1024; char currentDirectoryPath[ MAXBUFSIZE ]; getcwd(currentDirectoryPath, MAXBUFSIZE); //printf("当前目录:%s\n",currentDirectoryPath); char cmd[50] = "df "; strcat(cmd, currentDirectoryPath); //printf("%s\n",cmd); char buffer[MAXBUFSIZE]; FILE* pipe = popen(cmd, "r"); char fileSys[20]; char blocks[20]; char used[20]; char free[20]; char percent[10]; char moment[20]; if (!pipe) { return; } if (fgets(buffer, sizeof(buffer), pipe) != NULL) { sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment); } if (fgets(buffer, sizeof(buffer), pipe) != NULL) { sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment); } //printf("desk used:%s\n",percent); strcpy(*reused, percent); return; } static int set_rtc(time_t t) { int r=0,fd; // #if (CHIP_TYPE == CHIP_T113s) // system("hwclock -w -f /dev/rtc0"); // #else struct rtc_time *rtc; struct tm *tm = gmtime(&t); fd = open("/dev/rtc0", O_RDONLY); if (fd < 0) { perror("open rtc failed\n"); return -1; } rtc = (struct rtc_time*)tm; if (ioctl(fd, RTC_SET_TIME, rtc) < 0) { perror("ioctl RTC_SET_TIME failed\n"); r = -1; } else { printf("set rtc ok!\n"); } close(fd); //#endif return r; } #define MY_MINUTE 60 #define MY_HOUR (60*MY_MINUTE) #define MY_DAY (24*MY_HOUR) #define MY_YEAR (365*MY_DAY) static int my_month[12] = { 0, MY_DAY*(31), MY_DAY*(31+29), MY_DAY*(31+29+31), MY_DAY*(31+29+31+30), MY_DAY*(31+29+31+30+31), MY_DAY*(31+29+31+30+31+30), MY_DAY*(31+29+31+30+31+30+31), MY_DAY*(31+29+31+30+31+30+31+31), MY_DAY*(31+29+31+30+31+30+31+31+30), MY_DAY*(31+29+31+30+31+30+31+31+30+31), MY_DAY*(31+29+31+30+31+30+31+31+30+31+30) }; time_t mktime2(struct tm *t, int tzone) { time_t res,year; year = t->tm_year - 70; /* magic offsets (y+1) needed to get leapyears right.*/ res = MY_YEAR*year + MY_DAY*((year+1)/4); res += my_month[t->tm_mon]; /* and (y+2) here. If it wasn't a leap-year, we have to adjust */ if (t->tm_mon>1 && ((year+2)%4)) res -= MY_DAY; res += MY_DAY*(t->tm_mday-1); res += MY_HOUR*t->tm_hour; res += MY_MINUTE*t->tm_min; res += t->tm_sec; res -= tzone*3600; return res; } time_t mktime0(void) { struct tm t; t.tm_year = 70; t.tm_mon = 0; t.tm_mday = 1; t.tm_hour = 0; t.tm_min = 0; t.tm_sec = 0; return mktime(&t); } int set_time(struct tm *t, int tzone) { if(!t) { return -1; } t->tm_year -= 1900; t->tm_mon -= 1; time_t utc = mktime2(t, tzone); stime(&utc); set_rtc(utc); return 0; } int set_utctime(time_t t) { stime(&t); set_rtc(t); #if (CHIP_TYPE == CHIP_T113s) sleep(1); system("hwclock --show"); system("hwclock -s"); #endif return 0; } int get_tzone(void) { int tz = 0; #if (CHIP_TYPE == CHIP_T113s) time_t t1,t2; struct tm *tm_local,*tm_utc; time(&t1); t2=t1; tm_local = localtime(&t1); t1=mktime(tm_local); tm_utc = gmtime(&t2); t2=mktime(tm_utc); tz = (t1 - t2)/3600; log_d("tz = %d t1=%d t2=%d\n",tz,t1,t2); #else time_t now = time(NULL); struct tm *local = localtime(&now); time_t utc = mktime2(local, 0); tz = -((now - utc)/3600); #endif return tz; } int set_tzone(int tzone) { int r,tz; char buf[256]; tz = get_tzone(); #if 0 switch(zone) { case -11: sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Midway /etc/localtime"); break; case -10: sprintf(buf, "ln -sf /usr/share/zoneinfo/HST /etc/localtime"); break; case -9: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Adak /etc/localtime"); break; case -8: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Boise /etc/localtime"); break; case -7: sprintf(buf, "ln -sf /usr/share/zoneinfo/MST /etc/localtime"); break; case -6: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime"); break; case -5: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime"); break; case -4: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Halifax /etc/localtime"); break; case -3: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Maceio /etc/localtime"); break; case -2: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Noronha /etc/localtime"); break; case -1: sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Scoresbysund /etc/localtime"); break; case 0: sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime"); break; case 1: sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime"); break; case 2: sprintf(buf, "ln -sf /usr/share/zoneinfo/EET /etc/localtime"); break; case 3: sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime"); break; case 4: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Amman /etc/localtime"); break; case 5: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Karachi /etc/localtime"); break; case 6: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Dhaka /etc/localtime"); break; case 7: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Bangkok /etc/localtime"); break; case 8: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime"); break; case 9: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime"); break; case 10: sprintf(buf, "ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime"); break; case 11: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Sakhalin /etc/localtime"); break; case 12: sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Fiji /etc/localtime"); break; default: return -1; } #else #if (CHIP_TYPE == CHIP_T113s) sprintf(buf, "ln -sf /usr/share/zoneinfo/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone)); #else sprintf(buf, "ln -sf /usr/share/zoneinfo/Etc/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone)); #endif #endif log_i("set_zone: %s", buf); system(buf); if(tz!=tzone) { log_e("old tzone: %d new tzone %d\n",tz,tzone); sys_reboot(); } return 0; } void print_time(char *s, struct tm *tm) { 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); } void print_utc(char *s, time_t t) { struct tm *tm=gmtime(&t); tm->tm_year += 1900; tm->tm_mon += 1; print_time(s, tm); } int set_time_zone(struct tm *t, int zone) { int r; if(!t) { return -1; } r = set_time(t, zone); if(r) { log_e("__1__ set_time failed\n"); } r = set_tzone(zone); if(r) { log_e("__1__ set_tzone failed\n"); } return r; } int set_utctime_zone(time_t t, int zone) { int r; r = set_utctime(t); if(r) { log_e("__2__ set_time failed\n"); } r = set_tzone(zone); if(r) { log_e("__2__ set_zone failed\n"); } return r; } int get_time_str(char *s, int len, time_t t) { struct tm *tm=localtime(&t); 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); } int get_time_str2(char *s, int len, time_t t) { struct tm *tm=localtime(&t); 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); } int get_curtime_str(char *s, int len) { time_t t=time(NULL); struct tm *tm=localtime(&t); 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); } int get_curtime_str2(char *s, int len) { time_t t=time(NULL); struct tm *tm=localtime(&t); 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); } typedef struct { uint16_t year; uint8_t mon; uint8_t day; uint8_t hour; uint8_t min; uint8_t sec; }datetime_t; int get_bldtime(char *buf, int len) { char mon[10]={0}; datetime_t dt; char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; sscanf(__DATE__, "%s %hhu %hu", mon,&dt.day,&dt.year); sscanf(__TIME__, "%hhu:%hhu:%hhu", &dt.hour,&dt.min,&dt.sec); for(int i=0;i<12;i++) { if(strcmp(mon, mon2[i])==0) { dt.mon = i+1; break; } } return snprintf(buf,len,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec); } int get_bldtime2(char *dst, char *ori) { char mon[10]={0}; datetime_t dt; char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; sscanf(ori, "%s %hhu %hu %hhu:%hhu:%hhu", mon,&dt.day,&dt.year,&dt.hour,&dt.min,&dt.sec); for(int i=0;i<12;i++) { if(strcmp(mon, mon2[i])==0) { dt.mon = i+1; break; } } return sprintf(dst,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec); } int is_process_running(const char *process_name) { DIR *dir; struct dirent *entry; char path[1035]; // /proc/[pid]/cmdline 的最大路径长度(/proc/ + PID(最多5位) + /cmdline + \0) char buffer[4096]; // 假设cmdline不会超过这个长度 FILE *file; dir = opendir("/proc"); if (dir == NULL) { perror("opendir"); return -1; } while ((entry = readdir(dir)) != NULL) { // 忽略非数字目录(即非进程目录) if (!isdigit(entry->d_name[0])) { continue; } // 构造cmdline文件的完整路径 snprintf(path, sizeof(path), "/proc/%s/cmdline", entry->d_name); // 打开cmdline文件 file = fopen(path, "r"); if (file == NULL) { continue; // 忽略无法打开的文件 } // 读取cmdline文件的内容 if (fgets(buffer, sizeof(buffer), file) != NULL) { // 去除cmdline中的空字符(进程名与参数间用空字符分隔) size_t len = strlen(buffer); while (len > 0 && buffer[len - 1] == '\0') { buffer[--len] = '\0'; } // 比较进程名(这里假设process_name是完整的命令行,但你可以修改逻辑来仅比较进程名) if (strstr(buffer, process_name) != NULL) { fclose(file); closedir(dir); return 1; // 进程已启动 } } fclose(file); } closedir(dir); return 0; // 进程未启动 } // 辅助函数:判断是否是闰年 int is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 辅助函数:获取某月的天数 int days_in_month(int year, int month) { int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && is_leap_year(year)) return 29; return days_per_month[month]; } // 函数:增加一天到日期 void add_day_to_date(char *date_str, int *year, int *month, int *day) { (*day)++; if (*day > days_in_month(*year, *month)) { (*day) = 1; (*month)++; if (*month > 12) { (*month) = 1; (*year)++; } } sprintf(date_str, "%04d-%02d-%02d", *year, *month, *day); } // 函数:将 UTF-8 字符串转换为 Unicode 字符串(十六进制表示) char* utf8_to_unicode_string(const char* utf8_str) { // 计算 Unicode 字符串的最大可能长度(每个字符最多 4 个十六进制字符 + 空格) size_t max_len = strlen(utf8_str) * 4 * 2; // 每个字节最多 4 个十六进制字符,加上空格 char* unicode_str = malloc(max_len + 1); // +1 for null terminator if (!unicode_str) { return NULL; // 内存分配失败 } size_t pos = 0; while (*utf8_str) { uint32_t code_point = 0; unsigned char c = *utf8_str; // 根据 UTF-8 编码规则解析 if ((c & 0x80) == 0) { // 单字节(ASCII) code_point = c; utf8_str++; } else if ((c & 0xE0) == 0xC0) { // 双字节 code_point = ((c & 0x1F) << 6) | (utf8_str[1] & 0x3F); utf8_str += 2; } else if ((c & 0xF0) == 0xE0) { // 三字节(常见于中文) code_point = ((c & 0x0F) << 12) | ((utf8_str[1] & 0x3F) << 6) | (utf8_str[2] & 0x3F); utf8_str += 3; } else { // 其他情况(如四字节) // 简单处理,实际需更复杂的逻辑 utf8_str++; continue; } // 将 Unicode 码点格式化为十六进制字符串 pos += sprintf(unicode_str + pos, "%04X", code_point); } // 移除最后一个空格(可选) if (pos > 0 && unicode_str[pos - 1] == ' ') { unicode_str[pos - 1] = '\0'; } else { unicode_str[pos] = '\0'; } return unicode_str; } // 将中文字符串转换为 UCS2 编码(十六进制格式) int chinese_to_ucs2(const char* utf8_str, char* ucs2_hex, size_t ucs2_hex_size) { iconv_t cd = iconv_open("UCS-2BE", "UTF-8"); // 转换为大端序 UCS2 if (cd == (iconv_t)-1) { perror("iconv_open 失败"); return -1; } size_t in_len = strlen(utf8_str); size_t out_len = in_len * 4; // 每个 UTF-8 字符最多转换为 4 字节 UCS2 char* in_buf = (char*)utf8_str; char* out_buf = malloc(out_len); if (!out_buf) { perror("内存分配失败"); iconv_close(cd); return -1; } char* out_ptr = out_buf; if (iconv(cd, &in_buf, &in_len, &out_ptr, &out_len) == (size_t)-1) { perror("iconv 转换失败"); free(out_buf); iconv_close(cd); return -1; } // 将 UCS2 字节流格式化为十六进制字符串 size_t hex_len = 0; for (size_t i = 0; i < (out_ptr - out_buf) / 2; i++) { // UCS2 是 2 字节 unsigned char byte1 = out_buf[i * 2]; unsigned char byte2 = out_buf[i * 2 + 1]; hex_len += snprintf(ucs2_hex + hex_len, ucs2_hex_size - hex_len, "%02X%02X", byte1, byte2); } ucs2_hex[hex_len] = '\0'; free(out_buf); iconv_close(cd); return 0; } void SetMenuAuthority(int RoleID) { //超级管理员 if (RoleID == 1) { _GlobalMenuAuthority.PowerEnabled = true; _GlobalMenuAuthority.SensorEnabled = true; _GlobalMenuAuthority.AlarmEnabled = true; _GlobalMenuAuthority.LogEnabled = true; _GlobalMenuAuthority.UserInfoEnabled = true; _GlobalMenuAuthority.CommEnabled = true; _GlobalMenuAuthority.ServiceEnabled = true; _GlobalMenuAuthority.MaintenanceEnabled = true; // 1、电源监控菜单 _GlobalMenuAuthority.MonitorallEnabled = true; _GlobalMenuAuthority.ChannelstatusEnabled = true; _GlobalMenuAuthority.SwitchtatusEnabled = true; _GlobalMenuAuthority.MonitorhistoryEnabled = true; // 2、传感器管理菜单 _GlobalMenuAuthority.SensorStatusEnabled = true; _GlobalMenuAuthority.SensorHistorylEnabled = true; // 3、告警管理菜单 _GlobalMenuAuthority.XtjgEnabled = true; _GlobalMenuAuthority.YzszEnabled = true; // 4、日志信息菜单 _GlobalMenuAuthority.CzrzEnabled = true; _GlobalMenuAuthority.YxrzEnabled = true; // 5、用户管理菜单 _GlobalMenuAuthority.UserEnabled = true; _GlobalMenuAuthority.RoleEnabled = false; _GlobalMenuAuthority.MenuEnabled = false; // 6、通信管理菜单 _GlobalMenuAuthority.WkszEnabled = true; _GlobalMenuAuthority.RsszEnabled = true; if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable) _GlobalMenuAuthority.CellularEnabled = true; else _GlobalMenuAuthority.CellularEnabled = false; if (__globalDeviceManage._globalDevInfo.wifi.WIFI_enable) _GlobalMenuAuthority.WifiEnabled = true; else _GlobalMenuAuthority.WifiEnabled = false; // 7、服务管理菜单 _GlobalMenuAuthority.SmtpszEnabled = true; _GlobalMenuAuthority.SnmpszEnabled = true; _GlobalMenuAuthority.MqszEnabled = true; _GlobalMenuAuthority.NtpszEnabled = true; #ifndef NOT_USE_CONTROL _GlobalMenuAuthority.DsszEnabled = true; _GlobalMenuAuthority.SxszEnabled = true; _GlobalMenuAuthority.FzszEnabled = true; #else _GlobalMenuAuthority.DsszEnabled = false; _GlobalMenuAuthority.SxszEnabled = false; _GlobalMenuAuthority.FzszEnabled = false; #endif // 8、设备维护菜单 _GlobalMenuAuthority.SbcqEnabled = true; #ifndef NOT_USE_CONTROL _GlobalMenuAuthority.GjsjEnabled = true; #else _GlobalMenuAuthority.GjsjEnabled = false; #endif _GlobalMenuAuthority.KzbsjEnabled = false; _GlobalMenuAuthority.HhccszEnabled = true; _GlobalMenuAuthority.PzdrdcEnabled = true; _GlobalMenuAuthority.ShfwEnabled = true; _GlobalMenuAuthority.GysbEnabled = true; _GlobalMenuAuthority.BroadcastEnable = true; } // 管理员 else if (RoleID == 2) { _GlobalMenuAuthority.PowerEnabled = true; _GlobalMenuAuthority.SensorEnabled = true; _GlobalMenuAuthority.AlarmEnabled = true; _GlobalMenuAuthority.LogEnabled = true; _GlobalMenuAuthority.UserInfoEnabled = true; _GlobalMenuAuthority.CommEnabled = true; _GlobalMenuAuthority.ServiceEnabled = true; _GlobalMenuAuthority.MaintenanceEnabled = true; // 1、电源监控菜单 _GlobalMenuAuthority.MonitorallEnabled = true; _GlobalMenuAuthority.ChannelstatusEnabled = true; _GlobalMenuAuthority.SwitchtatusEnabled = false; _GlobalMenuAuthority.MonitorhistoryEnabled = true; // 2、传感器管理菜单 _GlobalMenuAuthority.SensorStatusEnabled = true; _GlobalMenuAuthority.SensorHistorylEnabled = true; // 3、告警管理菜单 _GlobalMenuAuthority.XtjgEnabled = true; _GlobalMenuAuthority.YzszEnabled = true; // 4、日志信息菜单 _GlobalMenuAuthority.CzrzEnabled = false; _GlobalMenuAuthority.YxrzEnabled = true; // 5、用户管理菜单 _GlobalMenuAuthority.UserEnabled = true; _GlobalMenuAuthority.RoleEnabled = false; _GlobalMenuAuthority.MenuEnabled = false; // 6、通信管理菜单 _GlobalMenuAuthority.WkszEnabled = false; _GlobalMenuAuthority.RsszEnabled = true; if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable) _GlobalMenuAuthority.CellularEnabled = true; else _GlobalMenuAuthority.CellularEnabled = false; if (__globalDeviceManage._globalDevInfo.wifi.WIFI_enable) _GlobalMenuAuthority.WifiEnabled = true; else _GlobalMenuAuthority.WifiEnabled = false; // 7、服务管理菜单 _GlobalMenuAuthority.SmtpszEnabled = false; _GlobalMenuAuthority.SnmpszEnabled = false; _GlobalMenuAuthority.MqszEnabled = false; _GlobalMenuAuthority.NtpszEnabled = true; #ifndef NOT_USE_CONTROL _GlobalMenuAuthority.DsszEnabled = true; _GlobalMenuAuthority.SxszEnabled = true; _GlobalMenuAuthority.FzszEnabled = true; #else _GlobalMenuAuthority.DsszEnabled = false; _GlobalMenuAuthority.SxszEnabled = false; _GlobalMenuAuthority.FzszEnabled = false; #endif // 8、设备维护菜单 _GlobalMenuAuthority.SbcqEnabled = true; #if (CHIP_TYPE == CHIP_T113s) _GlobalMenuAuthority.GjsjEnabled = false; #else _GlobalMenuAuthority.GjsjEnabled = true; #endif _GlobalMenuAuthority.KzbsjEnabled = false; _GlobalMenuAuthority.HhccszEnabled = false; _GlobalMenuAuthority.PzdrdcEnabled = true; _GlobalMenuAuthority.ShfwEnabled = false; _GlobalMenuAuthority.GysbEnabled = true; _GlobalMenuAuthority.BroadcastEnable = true; } //普通用户 else { _GlobalMenuAuthority.PowerEnabled = true; _GlobalMenuAuthority.SensorEnabled = true; _GlobalMenuAuthority.AlarmEnabled = false; _GlobalMenuAuthority.LogEnabled = false; _GlobalMenuAuthority.UserInfoEnabled = false; _GlobalMenuAuthority.CommEnabled = false; _GlobalMenuAuthority.ServiceEnabled = false; _GlobalMenuAuthority.MaintenanceEnabled = false; // 1、电源监控菜单 _GlobalMenuAuthority.MonitorallEnabled = true; _GlobalMenuAuthority.ChannelstatusEnabled = true; _GlobalMenuAuthority.SwitchtatusEnabled = false; _GlobalMenuAuthority.MonitorhistoryEnabled = false; // 2、传感器管理菜单 _GlobalMenuAuthority.SensorStatusEnabled = true; _GlobalMenuAuthority.SensorHistorylEnabled = false; // 3、告警管理菜单 _GlobalMenuAuthority.XtjgEnabled = false; _GlobalMenuAuthority.YzszEnabled = false; // 4、日志信息菜单 _GlobalMenuAuthority.CzrzEnabled = false; _GlobalMenuAuthority.YxrzEnabled = false; // 5、用户管理菜单 _GlobalMenuAuthority.UserEnabled = false; _GlobalMenuAuthority.RoleEnabled = false; _GlobalMenuAuthority.MenuEnabled = false; // 6、通信管理菜单 _GlobalMenuAuthority.WkszEnabled = false; _GlobalMenuAuthority.RsszEnabled = false; _GlobalMenuAuthority.CellularEnabled = false; _GlobalMenuAuthority.WifiEnabled = false; // 7、服务管理菜单 _GlobalMenuAuthority.SmtpszEnabled = false; _GlobalMenuAuthority.SnmpszEnabled = false; _GlobalMenuAuthority.MqszEnabled = false; _GlobalMenuAuthority.NtpszEnabled = false; _GlobalMenuAuthority.DsszEnabled = false; _GlobalMenuAuthority.SxszEnabled = false; _GlobalMenuAuthority.FzszEnabled = false; // 8、设备维护菜单 _GlobalMenuAuthority.SbcqEnabled = false; _GlobalMenuAuthority.GjsjEnabled = false; _GlobalMenuAuthority.KzbsjEnabled = false; _GlobalMenuAuthority.HhccszEnabled = false; _GlobalMenuAuthority.PzdrdcEnabled = false; _GlobalMenuAuthority.ShfwEnabled = false; _GlobalMenuAuthority.GysbEnabled = false; } }