Modify download_set_network_type()
[platform/core/api/url-download.git] / src / download-wrapping.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <system_info.h>
11
12 #include <dlog.h>
13 #include <download.h>
14 #include <download-provider-interface.h>
15
16 #define DEBUG_MSG
17 #ifdef DEBUG_MSG
18 #include <dlog.h>
19 #ifdef LOG_TAG
20 #undef LOG_TAG
21 #endif
22 #define LOG_TAG "TIZEN_N_URL_DOWNLOAD"
23 #define TRACE_ERROR(format, ARG...)  \
24 { \
25 LOGE(format, ##ARG); \
26 }
27 #define TRACE_STRERROR(format, ARG...)  \
28 { \
29 LOGE(format" [%s]", ##ARG, strerror(errno)); \
30 }
31 #define TRACE_INFO(format, ARG...)  \
32 { \
33 LOGI(format, ##ARG); \
34 }
35 #else
36 #define TRACE_DEBUG_MSG(format, ARG...) ;
37 #endif
38
39 #define TELEPHONY_FEATURE       "tizen.org/feature/network.telephony"
40 #define WIFI_FEATURE                    "tizen.org/feature/network.wifi"
41 #define WIFI_DIRECT_FEATURE     "tizen.org/feature/network.wifi.direct"
42
43 /////////////////////// APIs /////////////////////////////////
44
45 int download_create(int *download_id)
46 {
47         TRACE_INFO("");
48         if (download_id == NULL)
49                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
50         return dp_interface_create(download_id);
51 }
52
53 int download_destroy(int download_id)
54 {
55         TRACE_INFO("");
56         return dp_interface_destroy(download_id);
57 }
58
59 int download_start(int download_id)
60 {
61         TRACE_INFO("");
62         return dp_interface_start(download_id);
63 }
64
65 int download_pause(int download_id)
66 {
67         TRACE_INFO("");
68         return dp_interface_pause(download_id);
69 }
70
71 int download_cancel(int download_id)
72 {
73         TRACE_INFO("");
74         return dp_interface_cancel(download_id);
75 }
76
77
78 int download_set_url(int download_id, const char *url)
79 {
80         TRACE_INFO("");
81         if (url == NULL)
82                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
83         return dp_interface_set_url(download_id, url);
84 }
85
86
87 int download_get_url(int download_id, char **url)
88 {
89         TRACE_INFO("");
90         if (url == NULL)
91                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
92         return dp_interface_get_url(download_id, url);
93 }
94
95 int download_set_network_type(int download_id,
96                                                 download_network_type_e net_type)
97 {
98         TRACE_INFO("");
99
100         int bValue = 0;
101         int bIsTelephonyFeatureSupported =0;
102         int bIsWifiFeatureSupported = 0;
103         int bIsWifiDirectFeatureSupported = 0;
104
105         bValue = system_info_get_platform_bool (TELEPHONY_FEATURE, &bIsTelephonyFeatureSupported);
106         bValue = system_info_get_platform_bool (WIFI_FEATURE, &bIsWifiFeatureSupported);
107         bValue = system_info_get_platform_bool (WIFI_DIRECT_FEATURE, &bIsWifiDirectFeatureSupported);
108
109         switch (net_type)
110         {
111                 case DOWNLOAD_NETWORK_DATA_NETWORK:
112                         if ( !bIsTelephonyFeatureSupported )
113                         {
114                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
115                         }
116                         break;
117                 case DOWNLOAD_NETWORK_WIFI:
118                         if ( !bIsWifiFeatureSupported )
119                         {
120                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
121                         }
122                         break;
123                 case DOWNLOAD_NETWORK_WIFI_DIRECT:
124                         if ( !bIsWifiDirectFeatureSupported )
125                         {
126                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
127                         }
128                         break;
129                 case DOWNLOAD_NETWORK_ALL:
130                         if ( !bIsTelephonyFeatureSupported && !bIsWifiFeatureSupported && !bIsWifiDirectFeatureSupported )
131                         {
132                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
133                         }
134                         break;
135         }
136         /////////////////////////
137         return dp_interface_set_network_type(download_id, (int)net_type);
138 }
139
140 int download_get_network_type(int download_id,
141                                                         download_network_type_e *net_type)
142 {
143         TRACE_INFO("");
144
145         if (net_type == NULL) {
146                 TRACE_ERROR("Parameter NULL Check");
147                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
148         }
149         int network_type = DOWNLOAD_ADAPTOR_NETWORK_ALL;
150         int ret = dp_interface_get_network_type(download_id, &network_type);
151         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
152                 *net_type = network_type;
153         return ret;
154 }
155
156 int download_set_network_bonding(int download_id, bool enable)
157 {
158         TRACE_INFO("");
159         return dp_interface_set_network_bonding(download_id, (int)enable);
160 }
161
162 int download_get_network_bonding(int download_id, bool *enable)
163 {
164         int is_set = 0;
165         TRACE_INFO("");
166         if (enable == NULL) {
167                 TRACE_ERROR("Parameter NULL Check");
168                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
169         }
170         int ret = dp_interface_get_network_bonding(download_id, &is_set);
171         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
172                 *enable = (bool)is_set;
173         return ret;
174 }
175
176 int download_set_destination(int download_id, const char *path)
177 {
178         TRACE_INFO("");
179         if (path == NULL)
180                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
181         return dp_interface_set_destination(download_id, path);
182 }
183
184
185 int download_get_destination(int download_id, char **path)
186 {
187         TRACE_INFO("");
188         if (path == NULL)
189                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
190         return dp_interface_get_destination(download_id, path);
191 }
192
193 int download_set_file_name(int download_id, const char *file_name)
194 {
195         TRACE_INFO("");
196         if (file_name == NULL)
197                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
198         return dp_interface_set_file_name(download_id, file_name);
199 }
200
201 int download_get_file_name(int download_id, char **file_name)
202 {
203         TRACE_INFO("");
204         if (file_name == NULL)
205                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
206         return dp_interface_get_file_name(download_id, file_name);
207 }
208
209 int download_get_downloaded_file_path(int download_id, char **path)
210 {
211         TRACE_INFO("");
212         if (path == NULL)
213                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
214         return dp_interface_get_downloaded_file_path(download_id, path);
215 }
216
217 int download_add_http_header_field(int download_id, const char *field,
218         const char *value)
219 {
220         TRACE_INFO("");
221         if (field == NULL || value == NULL)
222                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
223         return
224                 dp_interface_add_http_header_field(download_id, field, value);
225 }
226
227 int download_get_http_header_field(int download_id,
228         const char *field, char **value)
229 {
230         TRACE_INFO("");
231         if (field == NULL || value == NULL)
232                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
233         return
234                 dp_interface_get_http_header_field(download_id, field, value);
235 }
236
237 int download_get_http_header_field_list(int download_id, char ***fields,
238         int *length)
239 {
240         TRACE_INFO("");
241         if (fields == NULL || length == NULL)
242                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
243         return dp_interface_get_http_header_field_list(download_id, fields,
244                 length);
245 }
246
247 int download_remove_http_header_field(int download_id,
248         const char *field)
249 {
250         TRACE_INFO("");
251         if (field == NULL)
252                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
253         return dp_interface_remove_http_header_field(download_id, field);
254 }
255
256 // download_state_changed_cb is different with dp_interface_state_changed_cb.
257 int download_set_state_changed_cb(int download_id,
258         download_state_changed_cb callback, void* user_data)
259 {
260         TRACE_INFO("");
261         return dp_interface_set_state_changed_cb(download_id,
262                 (dp_interface_state_changed_cb)callback, user_data);
263 }
264
265 int download_unset_state_changed_cb(int download_id)
266 {
267         TRACE_INFO("");
268         return dp_interface_unset_state_changed_cb(download_id);
269 }
270
271 // download_progress_cb is same with dp_interface_progress_cb.
272 int download_set_progress_cb(int download_id,
273         download_progress_cb callback, void *user_data)
274 {
275         TRACE_INFO("");
276         return dp_interface_set_progress_cb(download_id,
277                         (dp_interface_progress_cb)callback, user_data);
278 }
279
280 int download_unset_progress_cb(int download_id)
281 {
282         TRACE_INFO("");
283         return dp_interface_unset_progress_cb(download_id);
284 }
285
286 int download_get_state(int download_id, download_state_e *state)
287 {
288         int statecode = 0;
289         TRACE_INFO("");
290         if (state == NULL) {
291                 TRACE_ERROR("Parameter NULL Check");
292                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
293         }
294         int ret = dp_interface_get_state(download_id, &statecode);
295         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
296                 *state = statecode;
297         return ret;
298 }
299
300 int download_get_temp_path(int download_id, char **temp_path)
301 {
302         TRACE_INFO("");
303         if (temp_path == NULL)
304                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
305         return dp_interface_get_temp_path(download_id, temp_path);
306 }
307
308 int download_get_content_name(int download_id, char **content_name)
309 {
310         TRACE_INFO("");
311         if (content_name == NULL)
312                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
313         return dp_interface_get_content_name(download_id, content_name);
314 }
315
316 int download_get_content_size(int download_id,
317         unsigned long long *content_size)
318 {
319         TRACE_INFO("");
320         if (content_size == NULL)
321                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
322         return dp_interface_get_content_size(download_id, content_size);
323 }
324
325 int download_get_mime_type(int download_id, char **mime_type)
326 {
327         TRACE_INFO("");
328         if (mime_type == NULL)
329                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
330         return dp_interface_get_mime_type(download_id, mime_type);
331 }
332
333 int download_set_auto_download(int download_id, bool enable)
334 {
335         TRACE_INFO("");
336         return dp_interface_set_auto_download(download_id, (int)enable);
337 }
338
339 int download_get_auto_download(int download_id, bool *enable)
340 {
341         int is_set = 0;
342         TRACE_INFO("");
343         if (enable == NULL) {
344                 TRACE_ERROR("Parameter NULL Check");
345                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
346         }
347         int ret = dp_interface_get_auto_download(download_id, &is_set);
348         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
349                 *enable = (bool)is_set;
350         return ret;
351 }
352
353 int download_get_error(int download_id, download_error_e *error)
354 {
355         int errorcode = 0;
356         TRACE_INFO("");
357         if (error == NULL) {
358                 TRACE_ERROR("Parameter NULL Check");
359                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
360         }
361         int ret = dp_interface_get_error(download_id, &errorcode);
362         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
363                 *error = errorcode;
364         return ret;
365 }
366
367 int download_get_http_status(int download_id, int *http_status)
368 {
369         TRACE_INFO("");
370         if (http_status == NULL)
371                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
372         return dp_interface_get_http_status(download_id, http_status);
373 }
374
375 int download_set_notification_app_control(int download_id, download_notification_app_control_type_e type, app_control_h handle)
376 {
377         TRACE_INFO("");
378         if (handle == NULL)
379                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
380         return dp_interface_set_notification_service_handle(download_id, (int)type, handle);
381 }
382
383 int download_get_notification_app_control(int download_id, download_notification_app_control_type_e type, app_control_h *handle)
384 {
385         TRACE_INFO("");
386         if (handle == NULL)
387                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
388         return dp_interface_get_notification_service_handle(download_id, (int)type, handle);
389 }
390
391 int download_set_notification_title(int download_id, const char *title)
392 {
393         TRACE_INFO("");
394         if (title == NULL)
395                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
396         return dp_interface_set_notification_title(download_id, title);
397 }
398
399 int download_get_notification_title(int download_id, char **title)
400 {
401         TRACE_INFO("");
402         if (title == NULL)
403                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
404         return dp_interface_get_notification_title(download_id, title);
405 }
406
407 int download_set_notification_description(int download_id, const char *description)
408 {
409         TRACE_INFO("");
410         if (description == NULL)
411                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
412         return dp_interface_set_notification_description(download_id, description);
413 }
414
415 int download_get_notification_description(int download_id, char **description)
416 {
417         TRACE_INFO("");
418         if (description == NULL)
419                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
420         return dp_interface_get_notification_description(download_id, description);
421 }
422
423 int download_set_notification_type(int download_id, download_notification_type_e type)
424 {
425         TRACE_INFO("");
426         return dp_interface_set_notification_type(download_id, (int)type);
427 }
428
429 int download_get_notification_type(int download_id, download_notification_type_e *type)
430 {
431         int noti_type = 0;
432         TRACE_INFO("");
433         if (type == NULL) {
434                 TRACE_ERROR("Parameter NULL Check");
435                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
436         }
437         int ret = dp_interface_get_notification_type(download_id, &noti_type);
438         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
439                 *type = (download_notification_type_e)noti_type;
440         return ret;
441 }
442
443 int download_get_etag(int download_id, char **etag)
444 {
445         TRACE_INFO("");
446         if (etag == NULL)
447                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
448         return dp_interface_get_etag(download_id, etag);
449 }
450
451 int download_set_temp_file_path(int download_id, char *path)
452 {
453         TRACE_INFO("");
454         if (path == NULL)
455                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
456         return dp_interface_set_temp_file_path(download_id, path);
457 }