把httpdownload的接口单独提取出来
authorSnickerSun <snickersun@163.com>
Mon, 5 Aug 2024 03:03:37 +0000 (11:03 +0800)
committerSnickerSun <snickersun@163.com>
Mon, 5 Aug 2024 03:03:37 +0000 (11:03 +0800)
code/sdk/drivers/peripheral/utils/custom_http_download.h [new file with mode: 0644]
code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c

diff --git a/code/sdk/drivers/peripheral/utils/custom_http_download.h b/code/sdk/drivers/peripheral/utils/custom_http_download.h
new file mode 100644 (file)
index 0000000..76d72b6
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef __CUSTOM_HTTP_DOWNLOAD_H_
+#define __CUSTOM_HTTP_DOWNLOAD_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+#include "errcode.h"
+#include "lwip/apps/http_client.h"
+
+/**
+ * uint8_t *server, 
+ * uint16_t port,
+ * uint8_t *uri, 
+ * uint8_t *otadatapath, 
+ * httpc_result_fn callback_result_fn, 
+ * httpc_headers_done_fn callback_headers_done_fn,  
+ * httpc_file_rx_fn callback_file_rx_fn
+*/
+errcode_t http_single_download_start( uint8_t *server, uint16_t port,uint8_t *uri, uint8_t *otadatapath, httpc_result_fn callback_result_fn, httpc_headers_done_fn callback_headers_done_fn,  httpc_file_rx_fn callback_file_rx_fn);
+/**
+ * stop函数要在result里面执行,表示下载结束
+*/
+errcode_t http_single_download_stop(void);
+
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif
\ No newline at end of file
index fc3638f..9753f74 100644 (file)
@@ -7,7 +7,6 @@
 #include "custom_utils.h"
 #include "debug_print.h"
 #include "custom_runtime.h"
-#include "lwip/altcp_tls.h"
 #include "ConfigModelC.h"
 #ifdef SUPPORT_A5133_SERVICE
 #include "service/a5133_service.h"
@@ -93,28 +92,10 @@ static uint32_t custom_utils_getsn(uint8_t *para, uint32_t paraLen, char *strCom
     return 0;
 }
 
-
+#include "lwip/altcp_tls.h"
 #include "lwip/apps/http_client.h"
-void httpc_download_result(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err)
-{
-    wstp_print("httpc_download_result , httpc_result_t %d, rx_content_len %d , srv_res %d, err %d\n",
-        httpc_result,rx_content_len,srv_res,err);
-    //通讯结束,休眠cat1
-    uapi_cat1_keep_wakeup(0);
-    return ;
-}
-static uint32_t file_total_len = 0;
-err_t httpc_download_headers_done(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, u32_t content_len)
-{
-    wstp_print("httpc_download_headers_done , hdr_len %d ,content_len %d\n",hdr_len,content_len);
-    file_total_len = content_len;
-    return 0;
-}
-err_t httpc_download_file_recv_ui(httpc_state_t *connection, u32_t rx_len,u32_t rx_total_len)
-{
-    wstp_print("httpc_download_file_recv_ui recv %d bytes total received %d filelen %d\r\n",rx_len,rx_total_len,file_total_len);
-    return 0;
-}
+#define USING_HTTPS
+#ifdef USING_HTTPS
 
 // SSL CA PEMs  
 /* AWS */
@@ -153,6 +134,107 @@ const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
 "-----END CERTIFICATE-----\n";
 
 altcp_allocator_t *g_allocator = NULL;
+#endif
+static uint8_t g_http_download_started=0;
+/**
+ * uint8_t *server, 
+ * uint16_t port,
+ * uint8_t *uri, 
+ * uint8_t *otadatapath, 
+ * httpc_result_fn callback_result_fn, 
+ * httpc_headers_done_fn callback_headers_done_fn,  
+ * httpc_file_rx_fn callback_file_rx_fn
+*/
+errcode_t http_single_download_start( uint8_t *server, uint16_t port,uint8_t *uri, uint8_t *otadatapath, httpc_result_fn callback_result_fn, httpc_headers_done_fn callback_headers_done_fn,  httpc_file_rx_fn callback_file_rx_fn)
+{
+
+    if( server == NULL || uri == NULL || otadatapath == NULL)
+    {
+        wstp_print("http_single_download_start  failed, param is null \n");
+        return ERRCODE_FAIL;
+    }
+    if(g_http_download_started==1)
+    {
+        wstp_print("http_single_download_start  failed, already started \n");
+        return ERRCODE_FAIL;
+    }else{
+        g_http_download_started = 1;
+    }
+     //通讯开始,唤醒cat1
+    uapi_cat1_keep_wakeup(1);
+    httpc_connection_t settings = {0};
+
+
+    
+    #ifdef USING_HTTPS
+    
+    if (strncmp("https",(char*)uri,5)==0)
+    {
+        size_t calen = sizeof(SSL_CA_PEM);
+        struct altcp_tls_config *tlsconfig = altcp_tls_create_config_client((const uint8_t *)SSL_CA_PEM,calen);
+        wstp_print("custom_start_ota_download pa perm %s  \n======len %d \r\n",SSL_CA_PEM,calen);
+        g_allocator = malloc(sizeof(altcp_allocator_t));
+        if (g_allocator==NULL) return ERRCODE_MALLOC;
+        g_allocator->alloc = altcp_tls_alloc;
+        g_allocator->arg = (void*)tlsconfig;
+        settings.altcp_allocator=g_allocator;    
+    }
+    #endif
+
+
+    settings.result_fn=callback_result_fn;
+    settings.file_rx_fn = callback_file_rx_fn;
+    settings.headers_done_fn=callback_headers_done_fn;
+
+    settings.use_post = 0 ;
+
+    httpc_state_t* connection=NULL;
+    err_t rett =  httpc_get_file_dns_to_disk(
+                    (const char*) server, 
+                    port,
+                    (const char*) uri,
+                    (const httpc_connection_t *)&settings,
+                     NULL, 
+                     (const char*) otadatapath, 
+                     &connection);
+    if (rett== ERRCODE_SUCC)
+    {
+        g_http_download_started = 1;
+    }
+    return rett;
+}
+
+/***
+ * stop函数要在result里面执行,表示下载结束
+ * */
+errcode_t http_single_download_stop(void)
+{
+    //通讯结束,休眠cat1
+    uapi_cat1_keep_wakeup(0);
+    g_http_download_started = 0;
+    return ERRCODE_SUCC;
+}
+void httpc_download_result(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err)
+{
+    wstp_print("httpc_download_result , httpc_result_t %d, rx_content_len %d , srv_res %d, err %d\n",
+        httpc_result,rx_content_len,srv_res,err);
+
+    http_single_download_stop();
+    return ;
+}
+static uint32_t file_total_len = 0;
+err_t httpc_download_headers_done(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, u32_t content_len)
+{
+    wstp_print("httpc_download_headers_done , hdr_len %d ,content_len %d\n",hdr_len,content_len);
+    file_total_len = content_len;
+    return 0;
+}
+err_t httpc_download_file_recv_ui(httpc_state_t *connection, u32_t rx_len,u32_t rx_total_len)
+{
+    wstp_print("httpc_download_file_recv_ui recv %d bytes total received %d filelen %d\r\n",rx_len,rx_total_len,file_total_len);
+    return 0;
+}
+
 static uint32_t custom_start_ota_download(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
 {
     UNUSED(strCommonCmd);
@@ -178,39 +260,8 @@ static uint32_t custom_start_ota_download(uint8_t *para, uint32_t paraLen, char
     uint8_t *uri = (uint8_t*)argv[2];
     uint8_t *otadatapath = (uint8_t*)argv[3];
     wstp_print("custom_start_ota_download %s:%d %s to %s\r\n",server,port,uri,otadatapath);
-    httpc_connection_t settings = {0};
-
-
-    #if 0
-    size_t calen = sizeof(SSL_CA_PEM);
-    struct altcp_tls_config *tlsconfig = altcp_tls_create_config_client((const uint8_t *)SSL_CA_PEM,calen);
-    wstp_print("custom_start_ota_download pa perm %s  \n======len %d \r\n",SSL_CA_PEM,calen);
-
-
-    g_allocator = malloc(sizeof(altcp_allocator_t));
-    if (g_allocator==NULL) return ERRCODE_MALLOC;
-    g_allocator->alloc = altcp_tls_alloc;
-    g_allocator->arg = (void*)tlsconfig;
-
-    settings.altcp_allocator=g_allocator;
-    #endif
-
-
-    settings.result_fn=httpc_download_result;
-    settings.file_rx_fn = httpc_download_file_recv_ui;
-    settings.headers_done_fn=httpc_download_headers_done;
-
-    settings.use_post = 0 ;
-
-    httpc_state_t* connection=NULL;
-    err_t rett =  httpc_get_file_dns_to_disk(
-                    (const char*) server, 
-                    port,
-                    (const char*) uri,
-                    (const httpc_connection_t *)&settings,
-                     NULL, 
-                     (const char*) otadatapath, 
-                     &connection);
+ 
+    err_t rett = http_single_download_start(server,port,uri,otadatapath,httpc_download_result,httpc_download_headers_done,httpc_download_file_recv_ui);
 
     wstp_print("custom_start_ota_download %d \r\n",rett);
     return ERRCODE_SUCC;