#ifndef __MOZIT_CORE_H #define __MOZIT_CORE_H #include #include #include #include "../include/mysql.sdk.h" /****************************************************************** ** Copyright (c) 2009 Wuhan Technology Co., Ltd . ** FileName: core.c ** Author: BigHead ** Mail: jsrenyw@icloud.com ** Editor: bighead ** Date: 2020-11-10 13:34 ** Version: 2020-11-10 ** File Description: 业务逻辑核心代码实现部分 ******************************************************************/ #include "../include/debug.h" #include "mqtta.h" #include "regfunc.h" #include "myqueue.h" #include #include "list.h" #include #include #define EACH_PACK_SIZE 1024 #define FILE_BYTES 1024*300 #define UNUSED(x) (void)(x) #define NUM_THREADS 2 ArrQueue *m_pque;//任务队列 struct my_thread_info { int which; }; typedef union{ char c_char[4]; int i_int; }char2int; typedef union{ char c_char[2]; short i_short; }char2short; /*************************远程升级***************************/ typedef struct ota_info { char ota_mode; // ota 模式 0:差分 1:全量 char old_file_name[48]; // 旧文件名 uint32_t old_file_len; // 旧文件的长度 uint32_t old_file_crc; // 旧文件crc32 校验值 char new_file_name[48]; // 新文件名 uint32_t new_file_len; // 新文件的长度 uint32_t new_file_crc; // 新文件crc32 校验值 uint8_t ota_state; // ota 状态 uint32_t ota_file_len; // ota 文件的长度 uint32_t ota_file_crc; // ota 文件的crc32校验值 uint16_t ota_file_total_packets; // ota文件的总的包数 uint16_t ota_file_packet_len; // ota文件每包的数据长度 uint8_t buf[FILE_BYTES];//升级报文 } ota_info; typedef struct upgrade_user_info { char mac[30]; //mac char ota_file_name[48]; //升级文件名 } upgrade_user_info; /****************************************************************** * Function Name: core_init * Arguments: * Return Value: void * Date: 2020-11-10 * Editor: bighead * Description: 程序启动时刻,调用该初始化函数仅 一次,该函数不允许重复调用 ******************************************************************/ int core_init( void); /****************************************************************** * Function Name: core_heart * Arguments: * Return Value: void * Date: 2020-11-10 * Editor: bighead * Description: 每次主线程心跳调用, 约200ms一次, 可以调整频率在decode.c ******************************************************************/ int core_heart( unsigned int nowtime); /****************************************************************** * Function Name: core_heart_1s * Arguments: * Return Value: void * Date: 2020-11-10 * Editor: bighead * Description: 每次主线程心跳调用, 约1s一次,不超过2s, 在core_heart心跳函数之后调用 * 可以调整频率在decode.c ******************************************************************/ int core_heart_1s( unsigned int nowtime); /****************************************************************** * Function Name: db_init * Arguments: * Return Value: void * Date: 2020-11-10 * Editor: bighead * Description: 程序启动时刻,调用该初始化函数多次,初始化不同mysql 连接 ******************************************************************/ int db_init(MYSQL *db); int core_do_test(void); int core_stats(void); /****************************************************************** * Function Name: SendCmdFromDb * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 扫描数据库发送指令 ******************************************************************/ int SendCmdFromDb(void); /****************************************************************** * Function Name: TimeSyncBroadcast * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 时间同步广播 ******************************************************************/ int TimeSyncBroadcast(void); /****************************************************************** * Function Name: ByteToHexStr * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 1.字节流转换为十六进制字符串 ******************************************************************/ void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen); /****************************************************************** * Function Name: Hex2Str * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 2.字节流转换为十六进制字符串 ******************************************************************/ void Hex2Str( const char *sSrc, char *sDest, int nSrcLen ); /****************************************************************** * Function Name: HexStrToByte * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 十六进制字符串转换为字节流 ******************************************************************/ void HexStrToByte(const char* source, unsigned char* dest, int sourceLen); /****************************************************************** * Function Name: EndianSwap * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 改变字节顺序 ******************************************************************/ void EndianSwap(unsigned char *pData, int startIndex, int length); /****************************************************************** * Function Name: GetCurrentNYTable * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 获取当前月份历史表 ******************************************************************/ char* GetCurrentNYTable(void); /****************************************************************** * Function Name: GetCurrentTime * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 获取系统时间年月日 时分秒 ******************************************************************/ char* GetCurrentTime(void); /****************************************************************** * Function Name: IsRelayAlarmCode * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 判断是否属于报警code ******************************************************************/ int IsRelayAlarmCode(int relayCode); /****************************************************************** * Function Name: RelayAlarm * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 继电器报警 ******************************************************************/ int RelayAlarm(char* mac,char* subMac,int relayCode,MYSQL* _db); /****************************************************************** * Function Name: find_char * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 字符串中查找指定字符的个数 ******************************************************************/ int find_char(char str[], char substr[]); /****************************************************************** * Function Name: find_string * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 从主题中查找指定'/'分割的段 ******************************************************************/ char* find_string(char* topicName, int section); /****************************************************************** * Function Name: sendtoqueue * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 将任务放入队列 ******************************************************************/ void* sendtoqueue(ArrQueue *pQueue,char* stringSql); /****************************************************************** * Function Name: getqueue * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 将任务从队列中取出 ******************************************************************/ void* getqueue(void *data); /****************************************************************** * Function Name: CreateDbHandThread * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 创建任务线程处理数据库执行 ******************************************************************/ void CreateDbHandThread(void); /*************************远程升级*********************************************/ /****************************************************************** * Function Name: init_crc32_tab * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 生成CRC查询表 ******************************************************************/ void init_crc32_tab(void); /****************************************************************** * Function Name: get_crc32 * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 获得CRC ******************************************************************/ uint32_t get_crc32(uint32_t crcinit, uint8_t *bs, uint32_t bssize); /****************************************************************** * Function Name: 获取缓存中的crc32 * Arguments: * Return Value: void * Date: 2022-04-29 * Editor: chw * Description: 获取缓存中的crc32 ******************************************************************/ int32_t calc_crc32_from_buff(const char *buff, int len, uint32_t *uiCrcValue); /****************************************************************** * Function Name: readfile_from_db * Arguments: * Return Value: void * Date: 2022-04-29 * Editor: chw * Description: 从数据库读取升级文件 ******************************************************************/ unsigned char *readfile_from_db(const char *filename, MYSQL *_db,int *file_length_bytes); /****************************************************************** * Function Name: GetFileNameByMac * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 根据mac查询对应升级文件名 ******************************************************************/ char* GetFileNameByMac(list_node* user_list,char* mac); /****************************************************************** * Function Name: UpgradeFileAddFromDB * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 新增升级文件映射节点 ******************************************************************/ void UpgradeFileAddFromDB(list_node* f_list,char* filename,MYSQL *_db,int ota_mode); /****************************************************************** * Function Name: GetOtaInfoByFileName * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 根据文件名和包序号查询分包数据 ******************************************************************/ ota_info* GetOtaInfoByFileName(list_node* f_list,char* filename); /****************************************************************** * Function Name: GetUpgradeFileFromDB * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 数据库查询升级文件名 ******************************************************************/ void GetUpgradeFileFromDB(char *mac,MYSQL *_db,char* filename); /****************************************************************** * Function Name: core_alarm_gateway * Arguments: * Return Value: void * Date: 2023-12-5 * Editor: cc * Description: 扫描网关设备数据 ******************************************************************/ int CoreAlarmGateway(void); /****************************************************************** * Function Name: core_alarm_gateway * Arguments: * Return Value: void * Date: 2023-12-8 * Editor: cc * Description: 扫描节点设备数据 ******************************************************************/ int CoreAlarmNode(void); /****************************************************************** * Function Name: ElectricAlarm * Arguments: * Return Value: void * Date: 2023-12-7 * Editor: cc * Description: 电流数据告警判断 ******************************************************************/ int ElectricAlarm(char* mac,char* subMac,char* gatewayName,char* nodeName,int alarmCode,char* alarmStr,MYSQL* _db); /****************************************************************** * @brief 比较两个时间字符串的大小 * * @param time1 时间字符串1,格式为 "HH:MM" * @param time2 时间字符串2,格式为 "HH:MM" * * @return 返回值为整数: * -1 表示 time1 小于 time2 * 0 表示 time1 等于 time2 * 1 表示 time1 大于 time2 * 0 也可能表示解析失败或转换失败 * * @details 该函数将两个时间字符串解析为 struct tm 结构,并使用 mktime 函数将其转换为 time_t 类型, * 然后比较这两个时间,返回相应的比较结果。如果解析或转换失败,函数返回 0。 ******************************************************************/ int compareTimes(const char *time1, const char *time2); /** * @brief 比较两个时间的函数 * * @param t1 第一个时间结构体 * @param t2 第二个时间结构体 * * @return 负数表示t1 < t2,正数表示t1 > t2,零表示t1 == t2 * * @details 该函数将两个时间结构体t1和t2转换为秒数,然后比较这两个秒数以确定它们的相对顺序。 * 函数返回一个整数,用于指示t1相对于t2的顺序。负数表示t1在t2之前,正数表示t1在t2之后,零表示它们相等。 * * @note 该函数不会修改输入的时间结构体。 */ int compareModifiedTimes(struct tm t1, struct tm t2); /** * @brief 比较给定时间和系统当前时间的函数 * * @param t1 给定的时间结构体 * * @return 负数表示给定时间在系统当前时间之前,正数表示给定时间在系统当前时间之后,零表示它们相等 * * @details 该函数将给定时间结构体t1转换为秒数,并获取系统当前时间转换为秒数,然后比较这两个秒数以确定它们的相对顺序。 * 函数返回一个整数,用于指示给定时间相对于系统当前时间的顺序。负数表示给定时间在系统当前时间之前,正数表示 * 给定时间在系统当前时间之后,零表示它们相等。 * * @note 该函数不会修改输入的时间结构体。 */ int compareWithCurrentTime(struct tm t1); //更新时间和拉合闸的时间点比较 int compareWithUpdateTime(const char *timeString,const char *time1,const char *time2,const char *time3,const char *time4); #ifdef UPGRADE_BY_FILE /****************************************************************** * Function Name: calc_file_crc32 * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 获得文件CRC 现已从数据库读取,暂不用 ******************************************************************/ int32_t calc_file_crc32(const char *pFileName, uint32_t *uiCrcValue); /****************************************************************** * Function Name: GetFileLength * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 获取文件长度 现已从数据库读取,暂不用 ******************************************************************/ int GetFileLength(char* file_path); /****************************************************************** * Function Name: read_myfile * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 现已从数据库读取,暂不用 ******************************************************************/ //unsigned char *read_myfile(const char *path, int *file_size); /****************************************************************** * Function Name: UpgradeUserAdd * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 增加用户 现已从数据库读取,暂不用 ******************************************************************/ //void UpgradeUserAdd(list_node* u_list,char* mac,char* file_name); /****************************************************************** * Function Name: UpgradeFileAdd * Arguments: * Return Value: void * Date: 2022-04-02 * Editor: chw * Description: 新增升级文件映射节点 现已从数据库读取,暂不用 ******************************************************************/ //void UpgradeFileAdd(list_node* f_list,char* filename,int ota_mode); #endif #endif /* #ifndef __MOZITI_CORE_H */