arrange the request priority
[profile/ivi/download-provider.git] / src / download-provider-utils.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <sys/time.h>
7 #include <sys/socket.h>
8
9 #include <net_connection.h>
10
11 #include "download-provider-config.h"
12 #include "download-provider-notification.h"
13 #include "download-provider-pthread.h"
14 #include "download-provider-log.h"
15
16 extern fd_set g_download_provider_socket_readset;
17 extern fd_set g_download_provider_socket_exceptset;
18
19 int get_download_request_id(void)
20 {
21         int uniquetime = 0;
22         struct timeval tval;
23         static int last_uniquetime = 0;
24
25         do {
26                 uniquetime = (int)time(NULL);
27                 gettimeofday(&tval, NULL);
28                 if (tval.tv_usec == 0)
29                         uniquetime = uniquetime + (tval.tv_usec + 1) % 0xfffff;
30                 else
31                         uniquetime = uniquetime + tval.tv_usec;
32                 TRACE_DEBUG_INFO_MSG("ID : %d", uniquetime);
33         } while (last_uniquetime == uniquetime);
34         last_uniquetime = uniquetime;   // store
35         return uniquetime;
36 }
37
38 void clear_socket(download_clientinfo *clientinfo)
39 {
40         if (!clientinfo)
41                 return;
42         if (clientinfo->clientfd) {
43                 FD_CLR(clientinfo->clientfd, &g_download_provider_socket_readset);
44                 FD_CLR(clientinfo->clientfd, &g_download_provider_socket_exceptset);
45                 shutdown(clientinfo->clientfd, 0);
46                 fdatasync(clientinfo->clientfd);
47                 close(clientinfo->clientfd);
48                 clientinfo->clientfd = 0;
49         }
50 }
51
52 void clear_clientinfo(download_clientinfo *clientinfo)
53 {
54         TRACE_DEBUG_INFO_MSG("[TEST] clear [%p]",clientinfo);
55         // clear this slot
56         if (!clientinfo)
57                 return;
58
59         clear_socket(clientinfo);
60
61         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
62         if (clientinfo->requestinfo) {
63                 clientinfo->requestinfo->requestid = 0;
64                 if (clientinfo->requestinfo->client_packagename.length > 0
65                         && clientinfo->requestinfo->client_packagename.str)
66                         free(clientinfo->requestinfo->client_packagename.str);
67                 clientinfo->requestinfo->client_packagename.str = NULL;
68                 if (clientinfo->requestinfo->url.str)
69                         free(clientinfo->requestinfo->url.str);
70                 clientinfo->requestinfo->url.str = NULL;
71                 if (clientinfo->requestinfo->install_path.str)
72                         free(clientinfo->requestinfo->install_path.str);
73                 clientinfo->requestinfo->install_path.str = NULL;
74                 if (clientinfo->requestinfo->filename.str)
75                         free(clientinfo->requestinfo->filename.str);
76                 clientinfo->requestinfo->filename.str = NULL;
77                 if (clientinfo->requestinfo->service_data.str)
78                         free(clientinfo->requestinfo->service_data.str);
79                 clientinfo->requestinfo->service_data.str = NULL;
80                 if (clientinfo->requestinfo->headers.rows) {
81                         int i = 0;
82                         for (i = 0; i < clientinfo->requestinfo->headers.rows;
83                                 i++) {
84                                 if (clientinfo->requestinfo->headers.str[i].str)
85                                         free(clientinfo->requestinfo->headers.
86                                                 str[i].str);
87                                 clientinfo->requestinfo->headers.str[i].str =
88                                         NULL;
89                         }
90                         free(clientinfo->requestinfo->headers.str);
91                         clientinfo->requestinfo->headers.str = NULL;
92                 }
93                 free(clientinfo->requestinfo);
94                 clientinfo->requestinfo = NULL;
95         }
96         if (clientinfo->downloadinginfo)
97                 free(clientinfo->downloadinginfo);
98         clientinfo->downloadinginfo = NULL;
99         if (clientinfo->downloadinfo)
100                 free(clientinfo->downloadinfo);
101         clientinfo->downloadinfo = NULL;
102         if (clientinfo->tmp_saved_path)
103                 free(clientinfo->tmp_saved_path);
104         clientinfo->tmp_saved_path = NULL;
105         if (clientinfo->ui_notification_handle || clientinfo->service_handle)
106                 destroy_appfw_notification(clientinfo);
107         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
108         CLIENT_MUTEX_DESTROY(&(clientinfo->client_mutex));
109         memset(clientinfo, 0x00, sizeof(download_clientinfo));
110         free(clientinfo);
111         clientinfo = NULL;
112 }
113
114 void clear_clientinfoslot(download_clientinfo_slot *clientinfoslot)
115 {
116         // clear this slot
117         if (!clientinfoslot)
118                 return;
119         download_clientinfo *clientinfo =
120                 (download_clientinfo *) clientinfoslot->clientinfo;
121         clear_clientinfo(clientinfo);
122         clientinfoslot->clientinfo = NULL;
123 }
124
125 int get_downloading_count(download_clientinfo_slot *clientinfo_list)
126 {
127         int count = 0;
128         int i = 0;
129         for (i = 0; i < MAX_CLIENT; i++) {
130                 if (clientinfo_list[i].clientinfo) {
131                         if (clientinfo_list[i].clientinfo->state ==
132                                 DOWNLOAD_STATE_DOWNLOADING
133                                 || clientinfo_list[i].clientinfo->state ==
134                                 DOWNLOAD_STATE_INSTALLING
135                                 || clientinfo_list[i].clientinfo->state ==
136                                 DOWNLOAD_STATE_READY)
137                                 count++;
138                 }
139         }
140         return count;
141 }
142
143 int get_same_request_slot_index(download_clientinfo_slot *clientinfo_list,
144                                 int requestid)
145 {
146         int i = 0;
147
148         if (!clientinfo_list || !requestid)
149                 return -1;
150
151         for (i = 0; i < MAX_CLIENT; i++) {
152                 if (clientinfo_list[i].clientinfo
153                         && clientinfo_list[i].clientinfo->requestinfo) {
154                         if (clientinfo_list[i].clientinfo->requestinfo->
155                                 requestid == requestid) {
156                                 return i;
157                         }
158                 }
159         }
160         return -1;
161 }
162
163 int get_empty_slot_index(download_clientinfo_slot *clientinfo_list)
164 {
165         int i = 0;
166
167         if (!clientinfo_list)
168                 return -1;
169
170         for (i = 0; i < MAX_CLIENT; i++)
171                 if (!clientinfo_list[i].clientinfo)
172                         return i;
173         return -1;
174 }
175
176 int get_pended_slot_index(download_clientinfo_slot *clientinfo_list)
177 {
178         int i = 0;
179
180         if (!clientinfo_list)
181                 return -1;
182
183         for (i = 0; i < MAX_CLIENT; i++)
184                 if (clientinfo_list[i].clientinfo
185                         && clientinfo_list[i].clientinfo->state
186                                 == DOWNLOAD_STATE_PENDED)
187                         return i;
188         return -1;
189 }
190
191 int get_network_status()
192 {
193         connection_h network_handle = NULL;
194         if (connection_create(&network_handle) < 0) {
195                 TRACE_DEBUG_MSG("Failed connection_create");
196                 return -1;
197         }
198
199         connection_ethernet_state_e system_network_state
200                 = CONNECTION_ETHERNET_STATE_DISCONNECTED;
201         if (connection_get_ethernet_state(network_handle,
202                 &system_network_state) != CONNECTION_ERROR_NONE)
203                 TRACE_DEBUG_MSG("Failed connection_get_ethernet_state");
204         TRACE_DEBUG_INFO_MSG
205                 ("ethernet check result : [%d]", (int)system_network_state);
206
207         connection_cellular_state_e system_cellular_state
208                 = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
209         if (connection_get_cellular_state(network_handle,
210                 &system_cellular_state) != CONNECTION_ERROR_NONE)
211                 TRACE_DEBUG_MSG("Failed connection_get_ethernet_state");
212         TRACE_DEBUG_INFO_MSG
213                 ("cellula check result : [%d]", (int)system_cellular_state);
214
215         connection_wifi_state_e system_wifi_state
216                 = CONNECTION_WIFI_STATE_DEACTIVATED;
217         if (connection_get_wifi_state(network_handle,
218                 &system_wifi_state) != CONNECTION_ERROR_NONE)
219                 TRACE_DEBUG_MSG("Failed connection_get_ethernet_state");
220         TRACE_DEBUG_INFO_MSG
221                 ("wifi check result : [%d]", (int)system_wifi_state);
222
223         if (connection_destroy(network_handle) != CONNECTION_ERROR_NONE)
224                 TRACE_DEBUG_MSG("Failed connection_destroy");
225
226         if (!(system_network_state == CONNECTION_ETHERNET_STATE_CONNECTED
227                 || system_cellular_state == CONNECTION_CELLULAR_STATE_AVAILABLE
228                 || system_wifi_state == CONNECTION_WIFI_STATE_CONNECTED))
229                 return -1;
230         return 0;
231         }