blob: a9ca3060d11e995a18817df65ab663c307ecd3f7 [file] [log] [blame]
yq.wang99db6f52024-09-12 01:58:48 -07001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <string.h>
8#include <errno.h>
9#include <arpa/inet.h>
10
11#include "agnss_http_download.h"
12#include "mbtk_log.h"
13#include "mbtk_http.h"
14
15#define HTTP_RESULT_SUCCESS 0
16#define HTTP_RESULT_FAIL -1
17
18static char eph_file_path[128] = {0};
19static int eph_file_fd = -1;
20
21static void http_data_cb_func(int session_id, mbtk_http_data_type_enum type, void *data,int data_len)
22{
23 int ret = 0;
24
25 if(type == MBTK_HTTP_DATA_HEADER)
26 {
27 LOGD("Header(%d):%s", data_len, (char*)data);
28 if(eph_file_fd > 0)
29 {
30 return;
31 }
32 unlink(eph_file_path);
33 eph_file_fd = open(eph_file_path, O_RDWR|O_CREAT|O_TRUNC, 0644);
34 if (eph_file_fd < 0)
35 {
36 LOGD("file open error");
37 }
38 else
39 {
40 LOGD("agnss file open: %d", eph_file_fd);
41 }
42 }
43 else if(type == MBTK_HTTP_DATA_CONTENT)
44 {
45 LOGD("http Data(%d)", data_len);
46
47 ret = write(eph_file_fd, (char*)data, data_len);
48 if (ret < 0)
49 {
50 LOGE("%s: error writing to file!", __FUNCTION__);
51 }
52 else if (ret < data_len)
53 {
54 LOGD("%s: wrote less the buffer size!", __FUNCTION__);
55 }
56 else
57 {
58 //
59 }
60 }
61 else
62 {
63 LOGD(">>>>>Complete<<<<<");
64 if(eph_file_fd <= 0)
65 {
66 return;
67 }
68 close(eph_file_fd);
69 eph_file_fd = -1;
70 }
71}
72
73int eph_data_from_http_get(char *host, const char *path)
74{
75 if(host == NULL || path == NULL || strlen(path) == 0)
76 {
77 LOGE("eph_data_from_http_get param is error.");
78 }
79
80 int http_handle = mbtk_http_handle_get(TRUE, http_data_cb_func);
81 if(http_handle < 0)
82 {
83 LOGE("mbtk_http_handle_get() fail.");
84 return HTTP_RESULT_FAIL;
85 }
86 int http_session = mbtk_http_session_create(http_handle, HTTP_OPTION_GET, HTTP_VERSION_1_1);
87 if(http_handle < 0)
88 {
89 LOGE("mbtk_http_session_create() fail.");
90 goto error;
91 }
92
93 memcpy(eph_file_path, path, strlen(path));
94 if(mbtk_http_session_url_set(http_handle, http_session, host))
95 {
96 LOGE("mbtk_http_session_url_set() fail.\n");
97 goto error;
98 }
99 if(mbtk_http_session_start(http_handle, http_session))
100 {
101 LOGE("mbtk_http_session_start() fail.\n");
102 goto error;
103 }
104 if(mbtk_http_handle_free(http_handle))
105 {
106 LOGE("mbtk_http_handle_free() fail.");
107 return HTTP_RESULT_FAIL;
108 }
109
110 return HTTP_RESULT_SUCCESS;
111error:
112 mbtk_http_handle_free(http_handle);
113 return HTTP_RESULT_FAIL;
114}
115