From 0c3c767b2a7d7dac2e7f7c55f6f3bbea14de76f7 Mon Sep 17 00:00:00 2001 From: helloyifa <447523396@qq.com> Date: Fri, 10 Oct 2025 17:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=97=A7=E7=89=88=E6=9C=ACMQ?= =?UTF-8?q?TT=E7=9A=84=E4=BF=AE=E6=94=B9=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ali_mqtt/mqtt/iot_export_mqtt.h | 1 + bk_idk/components/ali_mqtt/mqtt/mqtt_client.c | 11 +++ .../components/ali_mqtt/mqtt/mqtt_instance.h | 1 + .../config/bk7258/bk7258_partitions.csv | 2 +- .../lock_lfvx/main/include/mqtt_instance.h | 4 +- .../thirdparty/lock_lfvx/main/mqtt_instance.c | 19 ++++- .../thirdparty/lock_lfvx/main/poka_client.c | 80 ++++++++----------- 7 files changed, 68 insertions(+), 50 deletions(-) mode change 100755 => 100644 bk_idk/components/ali_mqtt/mqtt/iot_export_mqtt.h mode change 100755 => 100644 bk_idk/components/ali_mqtt/mqtt/mqtt_client.c mode change 100755 => 100644 bk_idk/components/ali_mqtt/mqtt/mqtt_instance.h mode change 100755 => 100644 projects/thirdparty/lock_lfvx/main/include/mqtt_instance.h mode change 100755 => 100644 projects/thirdparty/lock_lfvx/main/mqtt_instance.c mode change 100755 => 100644 projects/thirdparty/lock_lfvx/main/poka_client.c diff --git a/bk_idk/components/ali_mqtt/mqtt/iot_export_mqtt.h b/bk_idk/components/ali_mqtt/mqtt/iot_export_mqtt.h old mode 100755 new mode 100644 index 202894f0..acf7bc5a --- a/bk_idk/components/ali_mqtt/mqtt/iot_export_mqtt.h +++ b/bk_idk/components/ali_mqtt/mqtt/iot_export_mqtt.h @@ -70,6 +70,7 @@ typedef enum { /* MQTT packet buffer overflow which the remaining space less than to receive byte */ IOTX_MQTT_EVENT_BUFFER_OVERFLOW = 13, + IOTX_MQTT_EVENT_PING_RESP = 14, } iotx_mqtt_event_type_t; /* topic information */ diff --git a/bk_idk/components/ali_mqtt/mqtt/mqtt_client.c b/bk_idk/components/ali_mqtt/mqtt/mqtt_client.c old mode 100755 new mode 100644 index e4f82806..228c8be0 --- a/bk_idk/components/ali_mqtt/mqtt/mqtt_client.c +++ b/bk_idk/components/ali_mqtt/mqtt/mqtt_client.c @@ -1350,6 +1350,17 @@ static int iotx_mc_cycle(iotx_mc_client_t *c, iotx_time_t *timer) case PINGRESP: { rc = SUCCESS_RETURN; log_info("receive ping response!\n"); + //iotx_mc_set_client_state(c, IOTX_MQTT_EVENT_PING_RESP); + /* handle callback function */ + //helloyifa + if (NULL != c->handle_event.h_fp) { + iotx_mqtt_event_msg_t msg; + msg.event_type = IOTX_MQTT_EVENT_PING_RESP; + msg.msg = NULL; + c->handle_event.h_fp(c->handle_event.pcontext, + c, + &msg); + } break; } default: diff --git a/bk_idk/components/ali_mqtt/mqtt/mqtt_instance.h b/bk_idk/components/ali_mqtt/mqtt/mqtt_instance.h old mode 100755 new mode 100644 index 39fe6862..b1a0507f --- a/bk_idk/components/ali_mqtt/mqtt/mqtt_instance.h +++ b/bk_idk/components/ali_mqtt/mqtt/mqtt_instance.h @@ -4,6 +4,7 @@ enum { MQTT_INSTANCE_EVENT_DISCONNECTED = 0, MQTT_INSTANCE_EVENT_CONNECTED = 1, + MQTT_INSTANCE_EVENT_PING_RESP = 2, }; diff --git a/projects/thirdparty/lock_lfvx/config/bk7258/bk7258_partitions.csv b/projects/thirdparty/lock_lfvx/config/bk7258/bk7258_partitions.csv index 3c14219c..dc3f45a0 100755 --- a/projects/thirdparty/lock_lfvx/config/bk7258/bk7258_partitions.csv +++ b/projects/thirdparty/lock_lfvx/config/bk7258/bk7258_partitions.csv @@ -5,7 +5,7 @@ bootloader,,68k,code,TRUE,FALSE app,,2584K,code,TRUE,FALSE app1,,408k,code,TRUE,FALSE app2,,136k,code,TRUE,FALSE -download,,1792K,data,TRUE, +download,,3196K,data,TRUE, usr_config,,68K,data,TRUE, easyflash,,8K,data,TRUE, rf_firmware,0x3fe000,4k,data,TRUE, diff --git a/projects/thirdparty/lock_lfvx/main/include/mqtt_instance.h b/projects/thirdparty/lock_lfvx/main/include/mqtt_instance.h old mode 100755 new mode 100644 index e75cc955..d44abd77 --- a/projects/thirdparty/lock_lfvx/main/include/mqtt_instance.h +++ b/projects/thirdparty/lock_lfvx/main/include/mqtt_instance.h @@ -43,7 +43,9 @@ void mqtt_remove_instance(); int mqtt_set_instance(void* mqtt_t); int mqtt_init_instance_poka2(char* host,int port,char *client, char *user, char *password, int maxMsgSize); - +/** + */ +int mqtt_update_reconnect_userinfo(char *client, char *user, char *password); int mqtt_init_instance_poka(char* host,int port,char *client, char *user, char *password, int maxMsgSize); /** * @brief Initialize the mqtt singleton instance. diff --git a/projects/thirdparty/lock_lfvx/main/mqtt_instance.c b/projects/thirdparty/lock_lfvx/main/mqtt_instance.c old mode 100755 new mode 100644 index 21aaddfa..845b3e18 --- a/projects/thirdparty/lock_lfvx/main/mqtt_instance.c +++ b/projects/thirdparty/lock_lfvx/main/mqtt_instance.c @@ -24,7 +24,8 @@ static void *mqtt_client = NULL; static void *mqtt_rbuf = NULL; static void *mqtt_wbuf = NULL; - +static iotx_conn_info_pt pconn_info; +static iotx_mqtt_param_t mqtt_params; static int abort_request = 0; @@ -74,6 +75,10 @@ static void event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt m LOGE("MQTT_INSTANCE_EVENT_PING_RESP !!!\n"); event = MQTT_INSTANCE_EVENT_PING_RESP; break; + case IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS: + //LOGE("IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS !!!\n"); + event = MQTT_INSTANCE_EVENT_CONNECTED; + break; default: return; @@ -113,8 +118,7 @@ int mqtt_init_instance_poka(char* host,int port,char *client, char *user, char * // IOT_OpenLog("masterslave"); // IOT_SetLogLevel(IOT_LOG_DEBUG); - iotx_conn_info_pt pconn_info; - iotx_mqtt_param_t mqtt_params; + int ret = IOT_SetupConnInfo(user, user, user, (void **)&pconn_info); if (ret != SUCCESS_RETURN) @@ -160,6 +164,15 @@ fail: mqtt_deinit_instance(); return -1; } + +int mqtt_update_reconnect_userinfo(char *client, char *user, char *password){ + //if(mqtt_params){ + mqtt_params.client_id = client; + mqtt_params.username = user; + mqtt_params.password = password; + //} + return 0; +} /* * 1: mqtt instance have been init * 0: mqtt instance init success diff --git a/projects/thirdparty/lock_lfvx/main/poka_client.c b/projects/thirdparty/lock_lfvx/main/poka_client.c old mode 100755 new mode 100644 index 4ee2aa3a..b0c88238 --- a/projects/thirdparty/lock_lfvx/main/poka_client.c +++ b/projects/thirdparty/lock_lfvx/main/poka_client.c @@ -85,7 +85,9 @@ static char iot_message_id [64] = {0}; static poka_client_cb_ops_t *event_cb; -static beken_timer_t timer_reboot_handle; +static beken_timer_t timer_update_auth_handle; + +static int mqtt_is_connected = 0; char * poka_client_get_device_id(){ return ilock_device_id; @@ -93,7 +95,9 @@ char * poka_client_get_device_id(){ char * poka_client_get_message_id(){ return iot_message_id; } - +int poka_client_is_connected(){ + return mqtt_is_connected; +} /** * * @@ -497,7 +501,7 @@ void mqtt_subscribe_invoke_callback(char *topic, int topic_len, void *payload, i if (event_cb != NULL){ event_cb->event_cloud(); //复位定时器 - rtos_reload_timer(&timer_reboot_handle); + //rtos_reload_timer(&timer_update_auth_handle); } @@ -943,16 +947,8 @@ void mqtt_update_connectinfo(){ poka_client_gen_password(mqtt_password,sizeof(mqtt_password),SECUREID, SECUREKEY,timestamp); BK_LOGE(TAG,"\r\nupdate mqtt_password: %s\n", mqtt_username); - iotx_mqtt_param_t mqtt_params; - /* Initialize MQTT parameter */ - memset(&mqtt_params, 0x0, sizeof(mqtt_params)); - mqtt_params.client_id = ilock_device_id; - mqtt_params.username = mqtt_username; - mqtt_params.password = mqtt_password; + mqtt_update_reconnect_userinfo(ilock_device_id, mqtt_username, mqtt_password); - //IOT_MQTT_Update_mqtt_param(mqtt_get_instance(),&mqtt_params); - - } @@ -960,45 +956,38 @@ void mqtt_update_connectinfo(){ void mqtt_event_callback(int event, void *ctx){ //BK_LOGE(TAG,"mqtt_event_callback event:%d\n",event); if(event == MQTT_INSTANCE_EVENT_DISCONNECTED){ - BK_LOGE(TAG,"mqtt disconnect! \n"); - //mqtt_update_connectinfo(); - //poka_client_unsubscribe_topic(PRODUCTID,ilock_device_id); - //mqtt_deinit_instance(); - //poka_client_init(); - if(ble_boarding_get_state() == BOARDING_STATE_SCAN || ble_boarding_get_state() == BOARDING_STATE_SETWIFI){ - - }else{ - //BK_LOGE(TAG,"MQTT ERROR REBOOT! \n"); - //bk_reboot(); - } - + BK_LOGE(TAG,"mqtt disconnect! \n"); + mqtt_is_connected = 0; } else if(event == MQTT_INSTANCE_EVENT_CONNECTED){ - BK_LOGE(TAG,"mqtt connected!\n"); - if (event_cb != NULL) + //BK_LOGE(TAG,"mqtt connected!\n"); + mqtt_is_connected = 1; + if (event_cb != NULL && event_cb->event_link_state!= NULL) event_cb->event_link_state(event); } else if(event == MQTT_INSTANCE_EVENT_PING_RESP){ BK_LOGE(TAG,"mqtt recv ping respone\n"); //复位定时器 - rtos_reload_timer(&timer_reboot_handle); - - } + rtos_reload_timer(&timer_update_auth_handle); + } } void poka_client_set_callback(poka_client_cb_ops_t *cb_ops){ event_cb = cb_ops; } -static void reboot_timer_handle( void *arg ) +static void timer_update_auth_info_callback( void *arg ) { - BK_LOGE(TAG,"mqtt recv ping respone timeout--> REBOOT! \n"); - bk_reboot(); + //BK_LOGE(TAG,"mqtt recv ping respone timeout--> RECONNECT! \n"); + //bk_reboot(); + //poka_client_reconnect(); + mqtt_update_connectinfo(); + rtos_reload_timer(&timer_update_auth_handle); } void poka_stop_reboot_timer() { - rtos_stop_timer(&timer_reboot_handle); + rtos_stop_timer(&timer_update_auth_handle); } void poka_client_init(void){ @@ -1017,7 +1006,7 @@ void poka_client_init(void){ BK_LOGE(TAG,"\r\nmqtt_username: %s\n", mqtt_username); poka_client_gen_password(mqtt_password,sizeof(mqtt_password),SECUREID, SECUREKEY,timestamp); - BK_LOGE(TAG,"\r\nmqtt_password: %s\n", mqtt_username); + BK_LOGE(TAG,"\r\nmqtt_password: %s\n", mqtt_password); mqtt_set_event_cb(&mqtt_event_callback, NULL); mqtt_init_instance_poka(TMQTT_SERVER,TMQTT_PORT,ilock_device_id, mqtt_username, mqtt_password, 1024*4); @@ -1025,19 +1014,20 @@ void poka_client_init(void){ // "YCx4b0m6Uv", "version=2018-10-31&res=products%2FYCx4b0m6Uv%2Fdevices%2F867698041071583&et=1745719251&method=sha1&sign=Qd56NvGRqQDtxUTciKKE9J%2BZvZY%3D", 1024*4); poka_client_subscribe_topic(PRODUCTID,ilock_device_id); - //启动网络检测定时器,如果两分钟 没有收到PING 回应自动重启 - rtos_init_timer(&timer_reboot_handle, ILOCK_MQTT_PINGPONG_TIMEOUT*1000, reboot_timer_handle, 0); - rtos_start_timer(&timer_reboot_handle); - -} - -void poka_client_reconnect(){ - //poka_client_unsubscribe_topic(PRODUCTID,ilock_device_id); - mqtt_deinit_instance(); - poka_client_init(); + //定时计算用户名和密码 用于断线时自动重连 账户密码和时间戳相关 超过5分钟原有的账号密码已经失效 + rtos_init_timer(&timer_update_auth_handle, 60*1000, timer_update_auth_info_callback, 0); + rtos_start_timer(&timer_update_auth_handle); } void poka_client_deinit(void){ - + + if (rtos_is_timer_init(&timer_update_auth_handle)) { + if (rtos_is_timer_running(&timer_update_auth_handle)) + { + rtos_stop_timer(&timer_update_auth_handle); + } + rtos_deinit_timer(&timer_update_auth_handle); + } + mqtt_deinit_instance(); } \ No newline at end of file