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 bIsTelephonyFeatureSupported = 0;
101         int bIsWifiFeatureSupported = 0;
102         int bIsWifiDirectFeatureSupported = 0;
103
104         system_info_get_platform_bool (TELEPHONY_FEATURE, &bIsTelephonyFeatureSupported);
105         system_info_get_platform_bool (WIFI_FEATURE, &bIsWifiFeatureSupported);
106         system_info_get_platform_bool (WIFI_DIRECT_FEATURE, &bIsWifiDirectFeatureSupported);
107
108         switch (net_type)
109         {
110                 case DOWNLOAD_NETWORK_DATA_NETWORK:
111                         if ( !bIsTelephonyFeatureSupported )
112                         {
113                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
114                         }
115                         break;
116                 case DOWNLOAD_NETWORK_WIFI:
117                         if ( !bIsWifiFeatureSupported )
118                         {
119                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
120                         }
121                         break;
122                 case DOWNLOAD_NETWORK_WIFI_DIRECT:
123                         if ( !bIsWifiDirectFeatureSupported )
124                         {
125                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
126                         }
127                         break;
128                 case DOWNLOAD_NETWORK_ALL:
129                         if ( !bIsTelephonyFeatureSupported && !bIsWifiFeatureSupported && !bIsWifiDirectFeatureSupported )
130                         {
131                                 return DOWNLOAD_ERROR_NOT_SUPPORTED;
132                         }
133                         break;
134         }
135
136         return dp_interface_set_network_type(download_id, (int)net_type);
137 }
138
139 int download_get_network_type(int download_id,
140                                                         download_network_type_e *net_type)
141 {
142         TRACE_INFO("");
143
144         if (net_type == NULL) {
145                 TRACE_ERROR("Parameter NULL Check");
146                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
147         }
148         int network_type = DOWNLOAD_ADAPTOR_NETWORK_ALL;
149         int ret = dp_interface_get_network_type(download_id, &network_type);
150         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
151                 *net_type = network_type;
152         return ret;
153 }
154
155 int download_set_network_bonding(int download_id, bool enable)
156 {
157         TRACE_INFO("");
158         return dp_interface_set_network_bonding(download_id, (int)enable);
159 }
160
161 int download_get_network_bonding(int download_id, bool *enable)
162 {
163         int is_set = 0;
164         TRACE_INFO("");
165         if (enable == NULL) {
166                 TRACE_ERROR("Parameter NULL Check");
167                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
168         }
169         int ret = dp_interface_get_network_bonding(download_id, &is_set);
170         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
171                 *enable = (bool)is_set;
172         return ret;
173 }
174
175 int download_set_destination(int download_id, const char *path)
176 {
177         TRACE_INFO("");
178         if (path == NULL)
179                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
180         return dp_interface_set_destination(download_id, path);
181 }
182
183
184 int download_get_destination(int download_id, char **path)
185 {
186         TRACE_INFO("");
187         if (path == NULL)
188                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
189         return dp_interface_get_destination(download_id, path);
190 }
191
192 int download_set_file_name(int download_id, const char *file_name)
193 {
194         TRACE_INFO("");
195         if (file_name == NULL)
196                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
197         return dp_interface_set_file_name(download_id, file_name);
198 }
199
200 int download_get_file_name(int download_id, char **file_name)
201 {
202         TRACE_INFO("");
203         if (file_name == NULL)
204                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
205         return dp_interface_get_file_name(download_id, file_name);
206 }
207
208 int download_get_downloaded_file_path(int download_id, char **path)
209 {
210         TRACE_INFO("");
211         if (path == NULL)
212                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
213         return dp_interface_get_downloaded_file_path(download_id, path);
214 }
215
216 int download_add_http_header_field(int download_id, const char *field,
217         const char *value)
218 {
219         TRACE_INFO("");
220         if (field == NULL || value == NULL)
221                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
222         return
223                 dp_interface_add_http_header_field(download_id, field, value);
224 }
225
226 int download_get_http_header_field(int download_id,
227         const char *field, char **value)
228 {
229         TRACE_INFO("");
230         if (field == NULL || value == NULL)
231                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
232         return
233                 dp_interface_get_http_header_field(download_id, field, value);
234 }
235
236 int download_get_http_header_field_list(int download_id, char ***fields,
237         int *length)
238 {
239         TRACE_INFO("");
240         if (fields == NULL || length == NULL)
241                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
242         return dp_interface_get_http_header_field_list(download_id, fields,
243                 length);
244 }
245
246 int download_remove_http_header_field(int download_id,
247         const char *field)
248 {
249         TRACE_INFO("");
250         if (field == NULL)
251                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
252         return dp_interface_remove_http_header_field(download_id, field);
253 }
254
255 // download_state_changed_cb is different with dp_interface_state_changed_cb.
256 int download_set_state_changed_cb(int download_id,
257         download_state_changed_cb callback, void* user_data)
258 {
259         TRACE_INFO("");
260         return dp_interface_set_state_changed_cb(download_id,
261                 (dp_interface_state_changed_cb)callback, user_data);
262 }
263
264 int download_unset_state_changed_cb(int download_id)
265 {
266         TRACE_INFO("");
267         return dp_interface_unset_state_changed_cb(download_id);
268 }
269
270 // download_progress_cb is same with dp_interface_progress_cb.
271 int download_set_progress_cb(int download_id,
272         download_progress_cb callback, void *user_data)
273 {
274         TRACE_INFO("");
275         return dp_interface_set_progress_cb(download_id,
276                         (dp_interface_progress_cb)callback, user_data);
277 }
278
279 int download_unset_progress_cb(int download_id)
280 {
281         TRACE_INFO("");
282         return dp_interface_unset_progress_cb(download_id);
283 }
284
285 int download_get_state(int download_id, download_state_e *state)
286 {
287         int statecode = 0;
288         TRACE_INFO("");
289         if (state == NULL) {
290                 TRACE_ERROR("Parameter NULL Check");
291                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
292         }
293         int ret = dp_interface_get_state(download_id, &statecode);
294         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
295                 *state = statecode;
296         return ret;
297 }
298
299 int download_get_temp_path(int download_id, char **temp_path)
300 {
301         TRACE_INFO("");
302         if (temp_path == NULL)
303                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
304         return dp_interface_get_temp_path(download_id, temp_path);
305 }
306
307 int download_get_content_name(int download_id, char **content_name)
308 {
309         TRACE_INFO("");
310         if (content_name == NULL)
311                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
312         return dp_interface_get_content_name(download_id, content_name);
313 }
314
315 int download_get_content_size(int download_id,
316         unsigned long long *content_size)
317 {
318         TRACE_INFO("");
319         if (content_size == NULL)
320                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
321         return dp_interface_get_content_size(download_id, content_size);
322 }
323
324 int download_get_mime_type(int download_id, char **mime_type)
325 {
326         TRACE_INFO("");
327         if (mime_type == NULL)
328                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
329         return dp_interface_get_mime_type(download_id, mime_type);
330 }
331
332 int download_set_auto_download(int download_id, bool enable)
333 {
334         TRACE_INFO("");
335         return dp_interface_set_auto_download(download_id, (int)enable);
336 }
337
338 int download_get_auto_download(int download_id, bool *enable)
339 {
340         int is_set = 0;
341         TRACE_INFO("");
342         if (enable == NULL) {
343                 TRACE_ERROR("Parameter NULL Check");
344                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
345         }
346         int ret = dp_interface_get_auto_download(download_id, &is_set);
347         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
348                 *enable = (bool)is_set;
349         return ret;
350 }
351
352 int download_get_error(int download_id, download_error_e *error)
353 {
354         int errorcode = 0;
355         TRACE_INFO("");
356         if (error == NULL) {
357                 TRACE_ERROR("Parameter NULL Check");
358                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
359         }
360         int ret = dp_interface_get_error(download_id, &errorcode);
361         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
362                 *error = errorcode;
363         return ret;
364 }
365
366 int download_get_http_status(int download_id, int *http_status)
367 {
368         TRACE_INFO("");
369         if (http_status == NULL)
370                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
371         return dp_interface_get_http_status(download_id, http_status);
372 }
373
374 int download_set_notification_app_control(int download_id, download_notification_app_control_type_e type, app_control_h handle)
375 {
376         TRACE_INFO("");
377         if (handle == NULL)
378                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
379         return dp_interface_set_notification_service_handle(download_id, (int)type, handle);
380 }
381
382 int download_get_notification_app_control(int download_id, download_notification_app_control_type_e type, app_control_h *handle)
383 {
384         TRACE_INFO("");
385         if (handle == NULL)
386                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
387         return dp_interface_get_notification_service_handle(download_id, (int)type, handle);
388 }
389
390 int download_set_notification_title(int download_id, const char *title)
391 {
392         TRACE_INFO("");
393         if (title == NULL)
394                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
395         return dp_interface_set_notification_title(download_id, title);
396 }
397
398 int download_get_notification_title(int download_id, char **title)
399 {
400         TRACE_INFO("");
401         if (title == NULL)
402                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
403         return dp_interface_get_notification_title(download_id, title);
404 }
405
406 int download_set_notification_description(int download_id, const char *description)
407 {
408         TRACE_INFO("");
409         if (description == NULL)
410                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
411         return dp_interface_set_notification_description(download_id, description);
412 }
413
414 int download_get_notification_description(int download_id, char **description)
415 {
416         TRACE_INFO("");
417         if (description == NULL)
418                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
419         return dp_interface_get_notification_description(download_id, description);
420 }
421
422 int download_set_notification_type(int download_id, download_notification_type_e type)
423 {
424         TRACE_INFO("");
425         return dp_interface_set_notification_type(download_id, (int)type);
426 }
427
428 int download_get_notification_type(int download_id, download_notification_type_e *type)
429 {
430         int noti_type = 0;
431         TRACE_INFO("");
432         if (type == NULL) {
433                 TRACE_ERROR("Parameter NULL Check");
434                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
435         }
436         int ret = dp_interface_get_notification_type(download_id, &noti_type);
437         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
438                 *type = (download_notification_type_e)noti_type;
439         return ret;
440 }
441
442 int download_get_etag(int download_id, char **etag)
443 {
444         TRACE_INFO("");
445         if (etag == NULL)
446                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
447         return dp_interface_get_etag(download_id, etag);
448 }
449
450 int download_set_temp_file_path(int download_id, char *path)
451 {
452         TRACE_INFO("");
453         if (path == NULL)
454                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
455         return dp_interface_set_temp_file_path(download_id, path);
456 }