cook-c100/src/app/Module/wjh_OTAUpgrades.c
2024-08-20 19:34:19 +08:00

164 lines
5.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* wjh_OTAUpgrades.c
*
* Created on: 2024年7月29日
* Author: wu
*/
#include "wjh_OTAConfig.h"
#include "uartctrl.h"
#include "timer_hard.h"
static u8 OTA_DEVICE_TYPE_NAME[][30] = {
"",
"handset",//手持设备
"cabinet",//柜体设备
"sensor"//传感设备
};
//==============================================================================
// 功能JSON转义方法供AT指令使用的格式
// 参数:要转义的字符串
// 返回:转义后的字符(需要手动释放)
// =============================================================================
static u8 *escapeString(const char *input)
{
u32 len = strlen(input);
u8 *escaped = (char *)malloc(2 * len + 1); // 分配足够的空间来存储转义后的字符串
if (escaped == NULL)
{
printf("内存分配失败\n");
return NULL;
}
u32 j = 0;
for (u32 i = 0; i < len; i++)
{
if (input[i] == '\"' || input[i] == '\\')
{
escaped[j++] = '\\'; // 添加反斜杠
}
escaped[j++] = input[i];
}
escaped[j] = '\0'; // 添加字符串结束符
return escaped;
}
static void timerisr(struct Timer *timer)
{
printf("10秒到了");
//发送数据
struct W0802WifiMqttSendMessageInfo messageInfo;
u8 topicName[WIFI_MQTT_TOPIC_MAX_LENGTH];
sprintf(topicName,OTA_MQTT_SEND_INFO_TOPIC,OTA_DEVICE_SN_DEFAULT);
// u8 message[WIFI_MQTT_MESSAGE_MAX_LENGTH];
// gbkTOutf8(message,OTA_DEVICE_SN_DEFAULT);
messageInfo.topicName = topicName;
messageInfo.qos = 1;
messageInfo.message = OTA_DEVICE_SN_DEFAULT;
messageInfo.keepState = WIFI_MQTT_KEEP_MESSAGE_CLOSE;
WifiMqttSendLongMessage(&messageInfo,1000,4);
u32 num = 0;
struct W0802WifiMqttMessage getMess[5];
WifiMqttGetMessage(&num,&getMess,5,1000);
if(num != 0){
wjh_TipCreate("消息获取成功");
printf("进行请求");
for(u32 i = 0;i < num;i ++){
printf("%s",getMess[i].topicName);
u8 responseTopic[80];
sprintf(responseTopic,OTA_MQTT_RECEIVE_COMMAND_TOPIC,OTA_DEVICE_TYPE_NAME[OTA_DEVICE_TYPE_DEFAULT],OTA_DEVICE_SN_DEFAULT);
if(strcmp(getMess[i].topicName,responseTopic) == 0){
u8 idString[80];
u8 *p = strstr(getMess[i].message,"id");
p += 5;
u32 ws = 0;
while(*p != '\"'){
idString[ws ++] = *p ++;
}
idString[ws] = 0;
OTA_MQTT_DeviceResponse(idString,"0TA升级设备执行成功!");
u8 hashCode[80];
p = strstr(getMess[i].message,"binHashCode");
p += 16;
ws = 0;
while(*p != '\"'&&*p != '\\'){
hashCode[ws ++] = *p ++;
}
hashCode[ws] = 0;
wjh_TipChange("准备进行文件请求");
OTA_HTTP_GetOTAFile(hashCode);
}
}
}
}
// =============================================================================
// 功能OTA升级的有关WIFI模块的初始化包括WIFI串口的串口配置
// 参数:无
// 返回true下载成功false下载失败
// =============================================================================
bool_t OTA_Initialize(void){
wjh_TipCreate("窗口初始化");
WifiInit("/dev/UART0", CN_UART_BAUD_115200, CN_UART_DATABITS_8, CN_UART_STOPBITS_1, CN_UART_PARITY_NONE);
wjh_TipChange("正在连接WIFI");
//wifi连接
OTA_WIFI_Connect(OTA_WIFI_NAME_DEFAULT,OTA_WIFI_PWD_DEFAULT);
wjh_TipChange("WIFI连接成功");
printf("wifi连接成功");
//向后台发送注册数据
struct OTA_DEVICE_REGISTER requestInfo;
requestInfo.deviceName = OTA_DEVICE_NAME_DEFAULT;
requestInfo.deviceType = OTA_DEVICE_TYPE_DEFAULT;
requestInfo.SN = OTA_DEVICE_SN_DEFAULT;
requestInfo.runningStatus = 1;
while(!OTA_HTTP_DeviceRegister(requestInfo)){
printf("注册失败");
DJY_EventDelay(500 * 1000);
}
printf("注册成功");
wjh_TipChange("设备注册成功");
u8 clientId[WIFI_MQTT_CLIENT_MAX_LENGTH];
sprintf(clientId,OTA_MQTT_CLIENTID_DEFAULT,OTA_DEVICE_SN_DEFAULT);
//MQTT客户信息配置
while(!OTA_MQTT_ClientConfig(clientId,OTA_MQTT_USERNAME_DEFAULT,OTA_MQTT_PWD_DEFAULT)){
printf("信息修改失败");
DJY_EventDelay(500 * 1000);
}
wjh_TipChange("MQTT客户端信息修改成功");
// DJY_EventDelay(2000 * 1000);
//MQTT主机连接
while(!OTA_MQTT_Connect(OTA_MQTT_ADDRESS_DEFAULT,OTA_MQTT_PORT_DEFAULT,OTA_MQTT_REQUEST_MODE)){
printf("MQTT连接失败");
DJY_EventDelay(500 * 1000);
}
wjh_TipChange("MQTT连接成功");
DJY_EventDelay(2000 * 1000);
u8 topicName1[WIFI_MQTT_TOPIC_MAX_LENGTH];
sprintf(topicName1,OTA_MQTT_RECEIVE_COMMAND_TOPIC,OTA_DEVICE_TYPE_NAME[OTA_DEVICE_TYPE_DEFAULT],OTA_DEVICE_SN_DEFAULT);
while(!WifiMqttSubscribe(topicName1,WIFI_MQTT_QOS_1,1000)){
printf("订阅失败");
DJY_EventDelay(500 * 1000);
}
wjh_TipChange("MQTT订阅成功");
DJY_EventDelay(2000 * 1000);
printf("mqtt成功");
wjh_TipChange("OTA初始化成功");
DJY_EventDelay(2000 * 1000);
wjh_TipDelete();
struct Timer *timer;
timer = Timer_Create("Timer1",OTA_WIFI_POLL_TIME,timerisr);
Timer_Ctrl(timer,EN_TIMER_STARTCOUNT,0);
}