更新ota功能
authorp15692025051 <846603105@qq.com>
Fri, 15 Aug 2025 05:56:57 +0000 (13:56 +0800)
committerp15692025051 <846603105@qq.com>
Fri, 15 Aug 2025 05:56:57 +0000 (13:56 +0800)
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/ai_VC.py

index cd3f295..d6210c2 100644 (file)
@@ -221,7 +221,7 @@ class AI_UART():
         EventMesh.subscribe("led_b_open",self.led_b_open)
         EventMesh.subscribe("led_rgb_open",self.led_rgb_open)
         EventMesh.subscribe("led_close",self.led_close)
-        EventMesh.subscribe("up_datat_bt",self.up_datat_bt)
+        EventMesh.subscribe("bt_ota_run",self.bt_ota_run)#
         EventMesh.subscribe("start_ota",self.start_ota)
         EventMesh.subscribe("end_ota",self.end_ota)
         EventMesh.subscribe("send_ble_adv_switch",self.send_ble_adv_switch)
@@ -500,7 +500,7 @@ class AI_UART():
         EventMesh.publish("pwk_key_release")
 
     def start_ota(self,event=None,msg=None):#通过串口给蓝牙升级
-
+        EventMesh.publish("wakeup_BT") 
         print("start_ota!!!")
         print("start_ota!!!")
         json_data = \
@@ -519,7 +519,7 @@ class AI_UART():
         print("start_ota count:{}!!!".format(count))
 
     def end_ota(self,event=None,msg=None):#通过串口给蓝牙升级
-
+        EventMesh.publish("wakeup_BT") 
         print("end_ota!!!")
         print("end_ota!!!")
         json_data = \
@@ -537,10 +537,11 @@ class AI_UART():
             utime.sleep_ms(1)
         print("end_ota count:{}!!!".format(count))
 
-    def up_datat_bt(self,event=None,msg=None):#通过串口给蓝牙升级
-        print("up_datat_bt!!!:url:{}".format(msg))
-        print("up_datat_bt!!!")
-        print("up_datat_bt!!!")
+    def bt_ota_run(self,event=None,msg=None):#通过串口给蓝牙升级
+        EventMesh.publish("wakeup_BT") 
+        print("bt_ota_run!!!:url:{}".format(msg))
+        print("bt_ota_run!!!")
+        print("bt_ota_run!!!")
         self.bt_ota_flag = 1
         self.read_ack_flag = 0
         
@@ -551,32 +552,51 @@ class AI_UART():
         json_str  =ujson.dumps(json_data)
         self.AI_uart.write(json_str)
         print(json_str)
-        count =100
+        count =2000#等20秒,擦除flash
         while True:
             count -=1
             if count <=0 or self.read_ack_flag == 1:
                 break
-            utime.sleep_ms(1)
+            utime.sleep_ms(10)
+        print("ota_start:count:{}".format(count))
         headers_T = {"Content-Type":"application/octet-stream"}
         response = request.get(url = msg,headers=headers_T,decode = False)
+        offset = 0
+        checksum = 0
         for i in response.content:
-            self.read_ack_flag = 0
+            EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠
+            
+            
             data = base64.b64encode(i)
-            json_data = \
-            {
-                "event": "ota_data",
-                "update":data
-            }
-            json_str  =ujson.dumps(json_data)
-            self.AI_uart.write(json_str)
-            #utime.sleep_ms(100)
-            count =500
-            while True:
-                count -=1
-                if count <=0 or self.read_ack_flag == 1:
+            send_count = 5#最大重发5次,没ack或者返回error重发
+            while 1:
+                send_count-=1
+                self.read_ack_flag = 0
+                checksum = 0
+                # 对每个字节进行异或运算
+                for byte in i:
+                    checksum ^= byte  # 等价于 checksum = checksum ^ byte
+                json_data = \
+                {
+                    "event": "ota_data",
+                    "update":data,
+                    "offset":offset,
+                    "checksum":checksum 
+                }
+                print("offset:{},checksum:{}".format(offset,checksum))
+                json_str  =ujson.dumps(json_data)
+                self.AI_uart.write(json_str)
+                #utime.sleep_ms(100)
+                count =300
+                while True:
+                    count -=1
+                    if count <=0 or self.read_ack_flag != 0 :
+                        break
+                    utime.sleep_ms(10)
+                print("ota_data:{} count:{}".format(len(json_str),count))
+                if send_count <=0 or self.read_ack_flag == 1:
                     break
-                utime.sleep_ms(10)
-            print("ota_data:{} count:{}".format(len(json_str),count))
+            offset+=len(i)
         self.read_ack_flag = 0
         json_data = \
         {
@@ -641,6 +661,8 @@ class AI_dev(Abstract):
             #4G唤醒蓝牙中断
             self.PIN_wakeup_bt =Pin(Pin.GPIO21, Pin.OUT, Pin.PULL_DISABLE, 0)
             #蓝牙开电
+            self.PIN_BT_ON =Pin(Pin.GPIO31, Pin.OUT, Pin.PULL_DISABLE, 0)
+            utime.sleep_ms(200)
             self.PIN_BT_ON =Pin(Pin.GPIO31, Pin.OUT, Pin.PULL_DISABLE, 1)
             #马达
             self.PIN_MOT=Pin(Pin.GPIO36, Pin.OUT, Pin.PULL_DISABLE, 1)
@@ -1624,6 +1646,7 @@ class OTA():
         #EventMesh.subscribe("min_sys_ota",self.min_sys_ota)
         self.downloadFileVoURL1= ""
         self.downloadFileVoURL2= ""
+        self.downloadFileVoURLBT=""
     #小系统升级,返回0,没有升级;返回,1有升级,正在执行升级;返回2,电压不够
     def min_sys_ota(self,event=None,msg=None):
         print("min_sys_ota")
@@ -1636,6 +1659,7 @@ class OTA():
         ret_str =0
         self.downloadFileVoURL1= ""
         self.downloadFileVoURL2= ""
+        self.downloadFileVoURLBT=""
         try:
             sourceVersion = versions.Build_Time
             sourceVersion = "7PIN-"+sourceVersion[:10]+"-"+sourceVersion[11:-3]
@@ -1661,13 +1685,14 @@ class OTA():
                     try:
                         if data["code"] == 20007:#有新版本
                             if "fileList"in data["data"]:
-                                if len(data["data"]["fileList"]) == 2:
+                                #if len(data["data"]["fileList"]) == 2:
                                     for fileList in data["data"]["fileList"]:
                                         if fileList["fileName"] == "dfota_2.bin":
                                             self.downloadFileVoURL2 = fileList["url"]
                                         elif fileList["fileName"] == "dfota_1.bin":
                                             self.downloadFileVoURL1 = fileList["url"]
-                            
+                                        elif fileList["fileName"] == "update.ufw":
+                                            self.downloadFileVoURLBT = fileList["url"]
                         if data["code"] == 20006:#已是最新版本
                             ret_str = 0
                                     
@@ -1681,20 +1706,37 @@ class OTA():
             if response != None:
                 response.close()
         
-        print("min_sys_ota:downloadFileVoURL1:{},\ndownloadFileVoURL2:{}".format(self.downloadFileVoURL1,self.downloadFileVoURL2))
+        print("min_sys_ota:downloadFileVoURL1:{},\ndownloadFileVoURL2:{},\ndownloadFileVoURLBT{}".format(self.downloadFileVoURL1,self.downloadFileVoURL2,self.downloadFileVoURLBT))
         print("min_sys_ota end") 
-        if self.downloadFileVoURL2!="" and self.downloadFileVoURL1!="":
+        if self.downloadFileVoURL2!="" and self.downloadFileVoURL1!="" and self.downloadFileVoURLBT !="":
             battery = EventMesh.publish("get_battery")
             if battery>=40:
                 self.ota_timer = osTimer()
-                self.ota_timer.start(15000, 0, self.ota_timer_cb)
+                #self.ota_timer.start(15000, 0, self.ota_timer_cb)
+                self.AI_client_VC_recv_id = _thread.start_new_thread(self.ota_task,())
                 ret_str =  1  
             else:
                 ret_str =  2
         return ret_str
     
-    def ota_timer_cb(self, *args, **kwargs):
+    # def ota_timer_cb(self, *args, **kwargs):
+    #     EventMesh.publish("publish_AI_btnInfo",{"func":self.usb_event})
+
+    def ota_task(self):
         import fota
+        utime.sleep_ms(2000)
+        count = 30#最多等待30秒
+        while 1:
+            count-=1
+            VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行
+            if VC_satrt_flag == None:
+                VC_satrt_flag = 0
+            if VC_satrt_flag == 0 or count<=0:
+                break
+            utime.sleep_ms(100)
+        
+        EventMesh.publish("bt_ota_run",self.downloadFileVoURLBT) #升级蓝牙
+        utime.sleep_ms(30000)
         fota_obj = fota() 
         fota_obj.httpDownload(url1=self.downloadFileVoURL1,url2=self.downloadFileVoURL2)
     #ota升级结果返回,开机时调用一次即可