Code Sync with Private GIT
[profile/ivi/download-provider.git] / src / download-provider-receiver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/socket.h>
4 #include <sys/un.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <sys/time.h>
9
10 #include <glib.h>
11
12 #include <net_connection.h>
13
14 #include "download-provider-config.h"
15 #include "download-provider-log.h"
16 #include "download-provider-pthread.h"
17 #include "download-provider-notification.h"
18 #include "download-provider-ipc.h"
19 #include "download-provider-db.h"
20 #include "download-provider-utils.h"
21
22 #include "download-agent-defs.h"
23 #include "download-agent-interface.h"
24
25 int _init_agent(void);
26 void _deinit_agent(void);
27 static void __downloading_info_cb(user_downloading_info_t *download_info,
28                                         void *user_data);
29 static void __download_info_cb(user_download_info_t *download_info,
30                                         void *user_data);
31 static void __notify_cb(user_notify_info_t *notify_info, void *user_data);
32 static int __change_error(int err);
33 static int __change_state(da_state state);
34
35 void TerminateDaemon(int signo);
36
37 pthread_attr_t g_download_provider_thread_attr;
38 fd_set g_download_provider_socket_readset;
39 fd_set g_download_provider_socket_exceptset;
40
41 void *_start_download(void *args)
42 {
43         int da_ret = -1;
44         int req_dl_id = -1;
45
46         download_clientinfo_slot *clientinfoslot =
47                 (download_clientinfo_slot *) args;
48         if (!clientinfoslot) {
49                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo_slot");
50                 return 0;
51         }
52         download_clientinfo *clientinfo =
53                 (download_clientinfo *) clientinfoslot->clientinfo;
54         if (!clientinfo) {
55                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo");
56                 return 0;
57         }
58
59         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
60         clientinfo->state = DOWNLOAD_STATE_READY;
61         clientinfo->err = DOWNLOAD_ERROR_NONE;
62         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
63
64         // call start_download() of download-agent
65         if (clientinfo->requestinfo->headers.rows > 0) {
66                 int len = 0;
67                 int i = 0;
68                 char **req_header = NULL;
69                 len = clientinfo->requestinfo->headers.rows;
70                 req_header = calloc(len, sizeof(char *));
71                 if (!req_header) {
72                         TRACE_DEBUG_MSG("fail to calloc");
73                         return 0;
74                 }
75                 for (i = 0; i < len; i++)
76                         req_header[i] =
77                                 strdup(clientinfo->requestinfo->headers.str[i].str);
78                 if (clientinfo->requestinfo->install_path.length > 1) {
79                         if (clientinfo->requestinfo->filename.length > 1)
80                                 da_ret =
81                                         da_start_download_with_extension(clientinfo->requestinfo->
82                                                         url.str, &req_dl_id,
83                                                         DA_FEATURE_REQUEST_HEADER,
84                                                         req_header, &len,
85                                                         DA_FEATURE_INSTALL_PATH,
86                                                         clientinfo->requestinfo->install_path.str,
87                                                         DA_FEATURE_FILE_NAME,
88                                                         clientinfo->requestinfo->filename.str,
89                                                         DA_FEATURE_USER_DATA,
90                                                         (void *)clientinfoslot,
91                                                         NULL);
92                         else
93                                 da_ret =
94                                         da_start_download_with_extension(clientinfo->requestinfo->
95                                                         url.str, &req_dl_id,
96                                                         DA_FEATURE_REQUEST_HEADER,
97                                                         req_header, &len,
98                                                         DA_FEATURE_INSTALL_PATH,
99                                                         clientinfo->requestinfo->install_path.str,
100                                                         DA_FEATURE_USER_DATA,
101                                                         (void *)clientinfoslot,
102                                                         NULL);
103                 } else {
104                         if (clientinfo->requestinfo->filename.length > 1)
105                                 da_ret =
106                                         da_start_download_with_extension(clientinfo->requestinfo->
107                                                         url.str, &req_dl_id,
108                                                         DA_FEATURE_REQUEST_HEADER,
109                                                         req_header, &len,
110                                                         DA_FEATURE_FILE_NAME,
111                                                         clientinfo->requestinfo->filename.str,
112                                                         DA_FEATURE_USER_DATA,
113                                                         (void *)clientinfoslot,
114                                                         NULL);
115                         else
116                                 da_ret =
117                                         da_start_download_with_extension(clientinfo->requestinfo->
118                                                         url.str, &req_dl_id,
119                                                         DA_FEATURE_REQUEST_HEADER,
120                                                         req_header, &len,
121                                                         DA_FEATURE_USER_DATA,
122                                                         (void *)clientinfoslot,
123                                                         NULL);
124                 }
125                 for (i = 0; i < len; i++) {
126                         if (req_header[i])
127                                 free(req_header[i]);
128                 }
129         } else {
130                 if (clientinfo->requestinfo->install_path.length > 1) {
131                         if (clientinfo->requestinfo->filename.length > 1)
132                                 da_ret =
133                                         da_start_download_with_extension(clientinfo->requestinfo->
134                                                         url.str, &req_dl_id,
135                                                         DA_FEATURE_INSTALL_PATH,
136                                                         clientinfo->requestinfo->install_path.str,
137                                                         DA_FEATURE_FILE_NAME,
138                                                         clientinfo->requestinfo->filename.str,
139                                                         DA_FEATURE_USER_DATA,
140                                                         (void *)clientinfoslot,
141                                                         NULL);
142                         else
143                                 da_ret =
144                                         da_start_download_with_extension(clientinfo->requestinfo->
145                                                         url.str, &req_dl_id,
146                                                         DA_FEATURE_INSTALL_PATH,
147                                                         clientinfo->requestinfo->install_path.str,
148                                                         DA_FEATURE_USER_DATA,
149                                                         (void *)clientinfoslot,
150                                                         NULL);
151                 } else {
152                         if (clientinfo->requestinfo->filename.length > 1)
153                                 da_ret =
154                                         da_start_download_with_extension(clientinfo->requestinfo->
155                                                         url.str, &req_dl_id,
156                                                         DA_FEATURE_FILE_NAME,
157                                                         clientinfo->requestinfo->filename.str,
158                                                         DA_FEATURE_USER_DATA,
159                                                         (void *)clientinfoslot,
160                                                         NULL);
161                         else
162                                 da_ret =
163                                         da_start_download_with_extension(clientinfo->requestinfo->
164                                                         url.str, &req_dl_id,
165                                                         DA_FEATURE_USER_DATA,
166                                                         (void *)clientinfoslot,
167                                                         NULL);
168                 }
169         }
170
171         // if start_download() return error cause of maximun download limitation, set state to DOWNLOAD_STATE_PENDED.
172         if (da_ret == DA_ERR_ALREADY_MAX_DOWNLOAD) {
173                 TRACE_DEBUG_INFO_MSG("change to pended request [%d]", da_ret);
174                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
175                 clientinfo->state = DOWNLOAD_STATE_PENDED;
176                 clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
177                 download_provider_db_requestinfo_update_column(clientinfo,
178                                                                         DOWNLOAD_DB_STATE);
179                 ipc_send_request_stateinfo(clientinfo);
180                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
181                 return 0;
182         } else if (da_ret != DA_RESULT_OK) {
183                 TRACE_DEBUG_INFO_MSG("Fail to request start [%d]", da_ret);
184                 /* FIXME : need to seperate in detail according to error return values */
185                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
186                 clientinfo->state = DOWNLOAD_STATE_FAILED;
187                 clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
188                 download_provider_db_requestinfo_remove(clientinfo->
189                                                         requestinfo->requestid);
190                 ipc_send_request_stateinfo(clientinfo);
191                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
192                 return 0;
193         }
194
195         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
196
197         TRACE_DEBUG_INFO_MSG("started download [%d]", da_ret);
198
199         clientinfo->req_id = req_dl_id;
200         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
201         clientinfo->err = DOWNLOAD_ERROR_NONE;
202
203         download_provider_db_requestinfo_update_column(clientinfo,
204                                                                 DOWNLOAD_DB_STATE);
205
206         // sync return  // client should be alive till this line at least.
207         ipc_send_request_stateinfo(clientinfo);
208
209         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
210         return 0;
211 }
212
213 int _handle_new_connection(download_clientinfo_slot *clientinfo_list, download_clientinfo *request_clientinfo)
214 {
215         uint searchslot = 0;
216
217         // NULL - checking
218         if (!clientinfo_list || !request_clientinfo ) {
219                 TRACE_DEBUG_MSG("NULL-CHECK");
220                 return -1;
221         }
222
223         CLIENT_MUTEX_INIT(&(request_clientinfo->client_mutex), NULL);
224
225 #ifdef SO_PEERCRED
226         socklen_t cr_len =
227                 sizeof(request_clientinfo->credentials);
228         if (getsockopt
229                 (request_clientinfo->clientfd, SOL_SOCKET,
230                 SO_PEERCRED, &request_clientinfo->credentials,
231                 &cr_len) == 0) {
232                 TRACE_DEBUG_INFO_MSG
233                         ("Client Info : pid=%d, uid=%d, gid=%d\n",
234                         request_clientinfo->credentials.pid,
235                         request_clientinfo->credentials.uid,
236                         request_clientinfo->credentials.gid);
237         }
238 #endif
239
240         download_controls type =
241                 ipc_receive_header(request_clientinfo->clientfd);
242         TRACE_DEBUG_INFO_MSG("[ACCEPT] HEADER : [%d] ", type);
243         // first of all, receive requestinfo .
244         if (type <= 0 || ipc_receive_request_msg(request_clientinfo) < 0) {
245                 TRACE_DEBUG_MSG("Ignore this connection, Invalid command");
246                 clear_clientinfo(request_clientinfo);
247                 return -1;
248         }
249
250         if (type == DOWNLOAD_CONTROL_STOP
251                 || type == DOWNLOAD_CONTROL_GET_STATE_INFO) {
252                 // get requestid from socket.
253                 if (request_clientinfo->requestinfo
254                         && request_clientinfo->requestinfo->requestid > 0) {
255                         // search requestid in slots.
256                         int searchindex = get_same_request_slot_index
257                                                         (clientinfo_list,
258                                                         request_clientinfo->requestinfo->requestid);
259                         if (type == DOWNLOAD_CONTROL_STOP) {
260                                 TRACE_DEBUG_INFO_MSG("Request : DOWNLOAD_CONTROL_STOP");
261                                 if (searchindex >= 0) {
262                                         if (da_cancel_download
263                                                 (clientinfo_list[searchindex].clientinfo->req_id)
264                                                 == DA_RESULT_OK) {
265                                                 request_clientinfo->state = DOWNLOAD_STATE_STOPPED;
266                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
267                                                 if (clientinfo_list[searchindex].clientinfo->requestinfo
268                                                         && clientinfo_list[searchindex].clientinfo->requestinfo->notification)
269                                                         set_downloadedinfo_appfw_notification(clientinfo_list[searchindex].clientinfo);
270                                                 download_provider_db_requestinfo_remove(request_clientinfo->requestinfo->requestid);
271                                                 download_provider_db_history_new(clientinfo_list[searchindex].clientinfo);
272                                         } else {
273                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
274                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
275                                         }
276                                 } else { // no found
277                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
278                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
279                                 }
280                                 ipc_send_stateinfo(request_clientinfo);
281                         } else if (type == DOWNLOAD_CONTROL_GET_STATE_INFO) {
282                                 // search slots/downloading db/history db
283                                 if (searchindex > 0) { // exist in slots (memory)
284                                         request_clientinfo->state =
285                                                 clientinfo_list[searchindex].clientinfo->state;
286                                         request_clientinfo->err =
287                                                 clientinfo_list[searchindex].clientinfo->err;
288                                 } else {  //search downloading db or history db
289                                         download_dbinfo* dbinfo =
290                                                 download_provider_db_get_info(
291                                                         request_clientinfo->requestinfo->requestid);
292                                         if (dbinfo) { // found in downloading db..it means crashed job
293                                                 request_clientinfo->state = DOWNLOAD_STATE_PENDED;
294                                                 request_clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
295                                         } else { // no exist in downloading db
296                                                 dbinfo = download_provider_db_history_get_info(
297                                                         request_clientinfo->requestinfo->requestid);
298                                                 if (!dbinfo) { // no info anywhere
299                                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
300                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
301                                                 } else { //history info
302                                                         request_clientinfo->state = dbinfo->state;
303                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
304                                                 }
305                                         }
306                                         download_provider_db_info_free(dbinfo);
307                                 }
308                                 ipc_send_stateinfo(request_clientinfo);
309                                 // estabilish the spec of return value.
310                         }
311                 }
312                 clear_clientinfo(request_clientinfo);
313                 return 0;
314         }
315
316         if (type != DOWNLOAD_CONTROL_START) {
317                 TRACE_DEBUG_MSG
318                         ("Now, DOWNLOAD_CONTROL_START is only supported");
319                 clear_clientinfo(request_clientinfo);
320                 return -1;
321         }
322
323         // check whether requestinfo has requestid or not.
324         if (request_clientinfo->requestinfo
325                 && request_clientinfo->requestinfo->requestid > 0) {
326                 // search same request id.
327                 int searchindex = get_same_request_slot_index(clientinfo_list,
328                                                 request_clientinfo->requestinfo->requestid);
329                 if (searchindex < 0) {
330                         TRACE_DEBUG_INFO_MSG("Not Found Same Request ID");
331                         // Invalid id
332                         request_clientinfo->state = DOWNLOAD_STATE_FAILED;
333                         request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
334                         ipc_send_request_stateinfo(request_clientinfo);
335                         clear_clientinfo(request_clientinfo);
336                         return 0;
337                 } else {        // found request id. // how to deal etag ?
338                         // connect to slot.
339                         TRACE_DEBUG_INFO_MSG("Found Same Request ID slot[%d]", searchindex);
340                         CLIENT_MUTEX_LOCK(&(request_clientinfo->client_mutex));
341                         // close previous socket.
342                         if (clientinfo_list[searchindex].clientinfo->clientfd > 0)
343                                 close(clientinfo_list[searchindex].clientinfo->clientfd);
344                         // change to new socket.
345                         clientinfo_list[searchindex].clientinfo->clientfd =
346                                 request_clientinfo->clientfd;
347                         ipc_send_request_stateinfo(clientinfo_list[searchindex].clientinfo);
348                         // update some info.
349                         clientinfo_list[searchindex].clientinfo->requestinfo->callbackinfo =
350                                 request_clientinfo->requestinfo->callbackinfo;
351                         clientinfo_list[searchindex].clientinfo->requestinfo->notification =
352                                 request_clientinfo->requestinfo->notification;
353                         request_clientinfo->clientfd = 0;       // prevent to not be disconnected.
354                         CLIENT_MUTEX_UNLOCK(&(request_clientinfo->client_mutex));
355                         clear_clientinfo(request_clientinfo);
356                         return 0;
357                 }
358         }
359
360         // new request.
361         searchslot = get_empty_slot_index(clientinfo_list);
362         if (searchslot < 0) {
363                 TRACE_DEBUG_MSG("download-provider is busy, try later");
364                 clear_clientinfo(request_clientinfo);
365                 sleep(5);       // provider need the time of refresh.
366                 return -1;
367         }
368         // create new unique id, and insert info to DB.
369         if (request_clientinfo->requestinfo
370                 && request_clientinfo->requestinfo->requestid <= 0) {
371                 request_clientinfo->requestinfo->requestid =
372                         get_download_request_id();
373                 if (download_provider_db_requestinfo_new
374                         (request_clientinfo) < 0) {
375                         clear_clientinfo(request_clientinfo);
376                         sleep(5);       // provider need the time of refresh.
377                         return -1;
378                 }
379         }
380
381         clientinfo_list[searchslot].clientinfo = request_clientinfo;
382
383         TRACE_DEBUG_INFO_MSG("New Connection slot [%d] max [%d] max once [%d]",
384                                                                                         searchslot,
385                                                                                         MAX_CLIENT,
386                                                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
387
388         if (get_downloading_count(clientinfo_list) >=
389                 DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
390                 CLIENT_MUTEX_LOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
391                 // deal as pended job.
392                 clientinfo_list[searchslot].clientinfo->state = DOWNLOAD_STATE_PENDED;
393                 clientinfo_list[searchslot].clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
394                 download_provider_db_requestinfo_update_column
395                         (clientinfo_list[searchslot].clientinfo,
396                         DOWNLOAD_DB_STATE);
397                 ipc_send_request_stateinfo(clientinfo_list[searchslot].clientinfo);
398                 TRACE_DEBUG_INFO_MSG ("Pended Request is saved to [%d/%d]",
399                         searchslot, MAX_CLIENT);
400                 CLIENT_MUTEX_UNLOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
401                 sleep(5);       // provider need the time of refresh.
402         } else {
403                 // create thread for receiving the reqeust info from client.
404                 // and if possible, it will create the thread for listening the event.
405                 if (pthread_create
406                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
407                         &g_download_provider_thread_attr, _start_download,
408                         &clientinfo_list[searchslot]) != 0) {
409                         TRACE_DEBUG_INFO_MSG("failed to call pthread_create for client");
410                         TRACE_DEBUG_INFO_MSG("Change to pended job");
411                         CLIENT_MUTEX_LOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
412                         clientinfo_list[searchslot].clientinfo->state =
413                                 DOWNLOAD_STATE_PENDED;
414                         clientinfo_list[searchslot].clientinfo->err =
415                                 DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
416                         download_provider_db_requestinfo_update_column
417                                 (clientinfo_list[searchslot].clientinfo,
418                                 DOWNLOAD_DB_STATE);
419                         ipc_send_request_stateinfo(clientinfo_list[searchslot].clientinfo);
420                         CLIENT_MUTEX_UNLOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
421                         sleep(5);       // provider need the time of refresh.
422                 }
423         }
424         return 0;
425 }
426
427 int _handle_client_request(download_clientinfo* clientinfo)
428 {
429         int da_ret = 0;
430         int msgType = 0;
431
432         // NULL - checking
433         if (!clientinfo) {
434                 TRACE_DEBUG_MSG("NULL-CHECK");
435                 return -1;
436         }
437
438         switch (msgType = ipc_receive_header(clientinfo->clientfd)) {
439         case DOWNLOAD_CONTROL_STOP:
440                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_STOP");
441                 da_ret = da_cancel_download(clientinfo->req_id);
442                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
443                 if (da_ret != DA_RESULT_OK) {
444                         /* FIXME : need to seperate in detail according to error return values */
445                         clientinfo->state = DOWNLOAD_STATE_FAILED;
446                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
447                         TRACE_DEBUG_MSG("Fail to request cancel [%d]", da_ret);
448                 } else {
449                         clientinfo->state = DOWNLOAD_STATE_STOPPED;
450                         clientinfo->err = DOWNLOAD_ERROR_NONE;
451                         if (clientinfo->requestinfo) {
452                                 if (clientinfo->requestinfo->notification)
453                                         set_downloadedinfo_appfw_notification(clientinfo);
454                                 download_provider_db_requestinfo_remove(clientinfo->
455                                         requestinfo->requestid);
456                         }
457                         download_provider_db_history_new(clientinfo);
458                 }
459                 ipc_send_stateinfo(clientinfo);
460                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
461                 break;
462         case DOWNLOAD_CONTROL_PAUSE:
463                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_PAUSE");
464                 da_ret = da_suspend_download(clientinfo->req_id);
465                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
466                 if (da_ret != DA_RESULT_OK) {
467                         /* FIXME : need to seperate in detail according to error return values */
468                         clientinfo->state = DOWNLOAD_STATE_FAILED;
469                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
470                         TRACE_DEBUG_MSG("Fail to request suspend [%d]", da_ret);
471                 } else {
472                         clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
473                         clientinfo->err = DOWNLOAD_ERROR_NONE;
474                 }
475                 ipc_send_stateinfo(clientinfo);
476                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
477                 break;
478         case DOWNLOAD_CONTROL_RESUME:
479                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_RESUME");
480                 da_ret = da_resume_download(clientinfo->req_id);
481                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
482                 if (da_ret != DA_RESULT_OK) {
483                         /* FIXME : need to seperate in detail according to error return values */
484                         clientinfo->state = DOWNLOAD_STATE_FAILED;
485                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
486                         TRACE_DEBUG_MSG("Fail to request resume [%d]", da_ret);
487                 } else {
488                         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
489                         clientinfo->err = DOWNLOAD_ERROR_NONE;
490                 }
491                 ipc_send_stateinfo(clientinfo);
492                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
493                 break;
494         case DOWNLOAD_CONTROL_GET_STATE_INFO:   // sync call
495                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_STATE_INFO");
496                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
497                 ipc_send_stateinfo(clientinfo);
498                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
499                 break;
500         case DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO:        // sync call
501                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO");
502                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
503                 ipc_send_downloadinfo(clientinfo);
504                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
505                 break;
506         case -1:
507         case 0:
508                 TRACE_DEBUG_MSG("(Closed Socket ) terminate event thread (%d)",
509                         msgType);
510                 // bloken socket... it seems the client is dead or closed by agent thread.
511                 // close socket, this will break the loop. and terminate this thread.
512                 clear_socket(clientinfo);
513                 return -1;
514         default:
515                 TRACE_DEBUG_MSG("Unknow message [%d]", msgType);
516                 return -1;
517         }
518         return 0;
519 }
520
521 void *run_manage_download_server(void *args)
522 {
523         int listenfd = 0;       // main socket to be albe to listen the new connection
524         int maxfd;
525         int ret = 0;
526         fd_set rset, exceptset;
527         struct timeval timeout;
528         long flexible_timeout;
529         download_clientinfo_slot *clientinfo_list;
530         uint searchslot = 0;
531         uint count_downloading_threads = 0;
532         download_clientinfo *request_clientinfo;
533         int check_retry = 1;
534         int i = 0;
535
536         socklen_t clientlen;
537         struct sockaddr_un listenaddr, clientaddr;
538
539         GMainLoop *mainloop = (GMainLoop *) args;
540
541         ret = _init_agent();
542         if (ret != DOWNLOAD_ERROR_NONE) {
543                 TRACE_DEBUG_MSG("failed to init agent");
544                 TerminateDaemon(SIGTERM);
545                 return 0;
546         }
547         clear_downloadinginfo_appfw_notification();
548
549         if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
550                 TRACE_DEBUG_MSG("failed to create socket");
551                 TerminateDaemon(SIGTERM);
552                 return 0;
553         }
554
555         bzero(&listenaddr, sizeof(listenaddr));
556         listenaddr.sun_family = AF_UNIX;
557         strcpy(listenaddr.sun_path, DOWNLOAD_PROVIDER_IPC);
558
559         if (bind(listenfd, (struct sockaddr *)&listenaddr, sizeof listenaddr) !=
560                 0) {
561                 TRACE_DEBUG_MSG("failed to call bind");
562                 TerminateDaemon(SIGTERM);
563                 return 0;
564         }
565
566         if (chmod(listenaddr.sun_path, 0777) < 0) {
567                 TRACE_DEBUG_MSG
568                         ("failed to change the permission of socket file");
569                 TerminateDaemon(SIGTERM);
570                 return 0;
571         }
572
573         if (listen(listenfd, MAX_CLIENT) != 0) {
574                 TRACE_DEBUG_MSG("failed to call listen");
575                 TerminateDaemon(SIGTERM);
576                 return 0;
577         }
578
579         maxfd = listenfd;
580         TRACE_DEBUG_INFO_MSG("Ready to listen IPC [%d][%s]", listenfd,
581                         DOWNLOAD_PROVIDER_IPC);
582
583         // allocation the array structure for managing the clients.
584         clientinfo_list =
585                 (download_clientinfo_slot *) calloc(MAX_CLIENT,
586                                                         sizeof(download_clientinfo_slot));
587         if (clientinfo_list == NULL) {
588                 TRACE_DEBUG_MSG("failed to allocate the memory for client list");
589                 TerminateDaemon(SIGTERM);
590                 return 0;
591         }
592
593         if (pthread_attr_init(&g_download_provider_thread_attr) != 0) {
594                 TRACE_DEBUG_MSG("failed to call pthread_attr_init for client");
595                 TerminateDaemon(SIGTERM);
596                 return 0;
597         }
598         if (pthread_attr_setdetachstate(&g_download_provider_thread_attr,
599                                                                         PTHREAD_CREATE_DETACHED) != 0) {
600                 TRACE_DEBUG_MSG("failed to set detach option");
601                 TerminateDaemon(SIGTERM);
602                 return 0;
603         }
604
605         flexible_timeout = DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
606
607         FD_ZERO(&g_download_provider_socket_readset);
608         FD_ZERO(&g_download_provider_socket_exceptset);
609         FD_SET(listenfd, &g_download_provider_socket_readset);
610         FD_SET(listenfd, &g_download_provider_socket_exceptset);
611
612         while (g_main_loop_is_running(mainloop)) {
613
614                 // clean slots
615                 for (i=0; i < MAX_CLIENT; i++) {
616                         if (!clientinfo_list[i].clientinfo)
617                                 continue;
618                         // clear slot.
619                         if (clientinfo_list[i].clientinfo->state >= DOWNLOAD_STATE_FINISHED) {
620                                 clear_clientinfoslot(&clientinfo_list[i]);
621                                 continue;
622                         }
623                 }
624
625                 rset = g_download_provider_socket_readset;
626                 exceptset = g_download_provider_socket_exceptset;
627
628                 memset(&timeout, 0x00, sizeof(struct timeval));
629                 timeout.tv_sec = flexible_timeout;
630
631                 if (select((maxfd + 1), &rset, 0, &exceptset, &timeout) < 0) {
632                         TRACE_DEBUG_MSG
633                                 ("select error, provider can't receive any request from client.");
634                         TerminateDaemon(SIGTERM);
635                         break;
636                 }
637
638                 for (i=0; i < MAX_CLIENT; i++) {  // find the socket received the packet.
639                         if (!clientinfo_list[i].clientinfo)
640                                 continue;
641                         // ignore it is not started yet.
642                         if (clientinfo_list[i].clientinfo->state <= DOWNLOAD_STATE_READY)
643                                 continue;
644                         // ignore if finished
645                         if (clientinfo_list[i].clientinfo->state >= DOWNLOAD_STATE_FINISHED)
646                                 continue;
647                         //Even if no socket, downloading should be progressed.
648                         if (clientinfo_list[i].clientinfo->clientfd <= 0)
649                                 continue;
650                         if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &rset) > 0) {
651                                 TRACE_DEBUG_INFO_MSG("FD_ISSET [%d] readset slot[%d]",
652                                         clientinfo_list[i].clientinfo->clientfd, i);
653                                 _handle_client_request(clientinfo_list[i].clientinfo);
654                         } else if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &exceptset) > 0) {
655                                 TRACE_DEBUG_MSG("FD_ISSET [%d] exceptset slot[%d]", clientinfo_list[i].clientinfo->clientfd, i);
656                                 clear_clientinfoslot(&clientinfo_list[i]);
657                         }
658                 } // MAX_CLIENT
659
660                 if (FD_ISSET(listenfd, &exceptset) > 0) {
661                         TRACE_DEBUG_MSG("meet listenfd Exception of socket");
662                         TerminateDaemon(SIGTERM);
663                         break;
664                 } else if (FD_ISSET(listenfd, &rset) > 0) { // new connection
665                         TRACE_DEBUG_INFO_MSG("FD_ISSET listenfd rset");
666                         // reset timeout.
667                         flexible_timeout =
668                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
669                         // ready the buffer.
670                         request_clientinfo =
671                                 (download_clientinfo *) calloc(1,
672                                                         sizeof(download_clientinfo));
673                         if (!request_clientinfo) {
674                                 TRACE_DEBUG_MSG
675                                         ("download-provider can't allocate the memory, try later");
676                                 clientlen = sizeof(clientaddr);
677                                 int clientfd = accept(listenfd,
678                                         (struct sockaddr *)&clientaddr, &clientlen);
679                                 close(clientfd);        // disconnect.
680                                 sleep(5);       // provider need the time of refresh.
681                                 continue;
682                         }
683                         // accept client.
684                         clientlen = sizeof(clientaddr);
685                         request_clientinfo->clientfd = accept(listenfd,
686                                                                 (struct sockaddr*)&clientaddr,
687                                                                 &clientlen);
688                         if (request_clientinfo->clientfd < 0) {
689                                 clear_clientinfo(request_clientinfo);
690                                 sleep(5);       // provider need the time of refresh.
691                                 continue;
692                         }
693                         if (_handle_new_connection(clientinfo_list, request_clientinfo) < 0) {
694                                 sleep(1);
695                                 continue;
696                         }
697                         // after starting the download by DA, event thread will start to get the event from client.
698                         if (request_clientinfo && request_clientinfo->clientfd > 0) {
699                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_readset);      // add new descriptor to set
700                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_exceptset);
701                                 if (request_clientinfo->clientfd > maxfd )
702                                         maxfd = request_clientinfo->clientfd;   /* for select */
703                         }
704                 }
705
706                 if (i >= MAX_CLIENT) { // timeout
707                         // If there is better solution to be able to know
708                         // the number of downloading threads, replace below rough codes.
709                         count_downloading_threads =
710                                 get_downloading_count(clientinfo_list);
711                         // check whether the number of downloading is already maximum.
712                         if (count_downloading_threads >=
713                                 DA_MAX_DOWNLOAD_REQ_AT_ONCE)
714                                 continue;
715
716                         // search pended request
717                         for (searchslot = 0; searchslot < MAX_CLIENT;
718                                 searchslot++) {
719                                 if (clientinfo_list[searchslot].clientinfo) {
720                                         if (clientinfo_list[searchslot].clientinfo->state ==
721                                                 DOWNLOAD_STATE_PENDED) {
722                                                 TRACE_DEBUG_INFO_MSG
723                                                         ("Retry Pended Request [%d/%d] state [%d/%d]",
724                                                         searchslot, MAX_CLIENT,
725                                                         count_downloading_threads,
726                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
727                                                 // create thread for restarting the pended download.
728                                                 if (pthread_create
729                                                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
730                                                         &g_download_provider_thread_attr,
731                                                         _start_download,
732                                                         &clientinfo_list[searchslot]) != 0) {
733                                                         TRACE_DEBUG_MSG
734                                                                 ("failed to call pthread_create for client");
735                                                 }
736                                                 count_downloading_threads++;
737                                                 usleep(1000);   // sleep in busy state.
738                                                 break;
739                                         }
740                                 }
741                         }
742
743                         if (check_retry
744                                 && count_downloading_threads <
745                                 DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
746                                 // Auto re-download feature. ethernet may be connected with other downloading items.
747                                 connection_h network_handle = NULL;
748                                 if (connection_create(&network_handle) < 0) {
749                                         TRACE_DEBUG_MSG
750                                                 ("Failed connection_create");
751                                         continue;
752                                 }
753
754                                 connection_ethernet_state_e system_network_state
755                                         = CONNECTION_ETHERNET_STATE_DISCONNECTED;
756                                 if (connection_get_ethernet_state(network_handle,
757                                                                                         &system_network_state) !=
758                                         CONNECTION_ERROR_NONE)
759                                         TRACE_DEBUG_MSG
760                                                 ("Failed connection_get_ethernet_state");
761                                 TRACE_DEBUG_INFO_MSG
762                                         ("ethernet check result : [%d]", (int)system_network_state);
763
764                                 connection_cellular_state_e system_cellular_state
765                                         = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
766                                 if (connection_get_cellular_state(network_handle,
767                                                                                         &system_cellular_state) !=
768                                         CONNECTION_ERROR_NONE)
769                                         TRACE_DEBUG_MSG
770                                                 ("Failed connection_get_ethernet_state");
771                                 TRACE_DEBUG_INFO_MSG
772                                         ("cellula check result : [%d]", (int)system_cellular_state);
773
774                                 connection_wifi_state_e system_wifi_state
775                                         = CONNECTION_WIFI_STATE_DEACTIVATED;
776                                 if (connection_get_wifi_state(network_handle,
777                                                                                         &system_wifi_state) !=
778                                         CONNECTION_ERROR_NONE)
779                                         TRACE_DEBUG_MSG
780                                                 ("Failed connection_get_ethernet_state");
781                                 TRACE_DEBUG_INFO_MSG
782                                         ("wifi check result : [%d]", (int)system_wifi_state);
783
784                                 if (connection_destroy(network_handle) !=
785                                         CONNECTION_ERROR_NONE)
786                                         TRACE_DEBUG_MSG
787                                                 ("Failed connection_destroy");
788
789                                 if (!(system_network_state
790                                                 == CONNECTION_ETHERNET_STATE_CONNECTED
791                                         || system_cellular_state
792                                                 == CONNECTION_CELLULAR_STATE_AVAILABLE
793                                         || system_wifi_state
794                                                 == CONNECTION_WIFI_STATE_CONNECTED))
795                                         continue;
796
797                                 // check auto-retrying list regardless state. pended state is also included to checking list.
798                                 int i = 0;
799                                 download_dbinfo_list *db_list =
800                                         download_provider_db_get_list(DOWNLOAD_STATE_NONE);
801                                 if (!db_list || db_list->count <= 0) {
802                                         TRACE_DEBUG_INFO_MSG
803                                                 ("provider does not need to check DB anymore. in this life.");
804                                         check_retry = 0;        // provider does not need to check DB anymore. in this life.
805                                         if (db_list)
806                                                 download_provider_db_list_free(db_list);
807                                         continue;
808                                 }
809                                 for (i = 0;
810                                         count_downloading_threads <
811                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE
812                                         && i < db_list->count; i++) {
813                                         if (db_list->item[i].requestid <= 0)
814                                                 continue;
815                                         if (get_same_request_slot_index
816                                                 (clientinfo_list,db_list->item[i].requestid) < 0) {
817                                                 // not found requestid in memory
818                                                 TRACE_DEBUG_INFO_MSG
819                                                         ("Retry download [%d]",
820                                                         db_list->item[i].requestid);
821                                                 //search empty slot. copy db info to slot.
822                                                 searchslot =
823                                                         get_empty_slot_index(clientinfo_list);
824                                                 if (searchslot < 0) {
825                                                         TRACE_DEBUG_INFO_MSG
826                                                                 ("download-provider is busy, try later");
827                                                         flexible_timeout =
828                                                                 flexible_timeout * 2;
829                                                         break;
830                                                 }
831                                                 // allocte requestinfo to empty slot.
832                                                 request_clientinfo =
833                                                         (download_clientinfo *)
834                                                         calloc(1, sizeof(download_clientinfo));
835                                                 if (!request_clientinfo)
836                                                         continue;
837                                                 request_clientinfo->requestinfo
838                                                         =
839                                                         download_provider_db_get_requestinfo
840                                                         (&db_list->item[i]);
841                                                 if (!request_clientinfo->requestinfo) {
842                                                         free(request_clientinfo);
843                                                         request_clientinfo = NULL;
844                                                         continue;
845                                                 }
846
847                                                 clientinfo_list[searchslot].clientinfo =
848                                                         request_clientinfo;
849
850                                                 TRACE_DEBUG_INFO_MSG
851                                                         ("Retry download [%d/%d][%d/%d]",
852                                                         searchslot, MAX_CLIENT,
853                                                         count_downloading_threads,
854                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
855                                                 if (pthread_create
856                                                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
857                                                         &g_download_provider_thread_attr,
858                                                         _start_download,
859                                                         &clientinfo_list[searchslot])
860                                                         != 0) {
861                                                         TRACE_DEBUG_MSG
862                                                                 ("failed to call pthread_create for client");
863                                                         clientinfo_list[searchslot].clientinfo->state =
864                                                                 DOWNLOAD_STATE_PENDED;
865                                                         clientinfo_list[searchslot].clientinfo->err =
866                                                                 DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
867                                                         sleep(5);
868                                                 }
869                                                 count_downloading_threads++;
870                                                 usleep(1000);   // sleep in busy state.
871                                         }
872                                 }
873                                 if (i >= db_list->count)        // do not search anymore.
874                                         check_retry = 0;
875                                 download_provider_db_list_free(db_list);
876                         }
877
878                         // save system resource (CPU)
879                         if (check_retry == 0 && count_downloading_threads == 0
880                                 && flexible_timeout <
881                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
882                                 flexible_timeout = flexible_timeout * 2;
883                         if (flexible_timeout >
884                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
885                                 flexible_timeout =
886                                         DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL;
887                         TRACE_DEBUG_INFO_MSG("Next Timeout after [%ld] sec",
888                                         flexible_timeout);
889
890                 } // if (i >= MAX_CLIENT) { // timeout
891         }
892
893         FD_CLR(listenfd, &rset);
894         FD_CLR(listenfd, &exceptset);
895         FD_CLR(listenfd, &g_download_provider_socket_readset);
896         FD_CLR(listenfd, &g_download_provider_socket_exceptset);
897
898         // close accept socket.
899         if (listenfd)
900                 close(listenfd);
901
902         _deinit_agent();
903
904         // close all sockets for client. .. 
905         // client thread will terminate by itself through catching this closing.
906         for (searchslot = 0; searchslot < MAX_CLIENT; searchslot++)
907                 if (clientinfo_list[searchslot].clientinfo)
908                         clear_clientinfoslot(&clientinfo_list[searchslot]);
909
910         if (clientinfo_list)
911                 free(clientinfo_list);
912
913         pthread_exit(NULL);
914         return 0;
915 }
916
917 void __download_info_cb(user_download_info_t *download_info, void *user_data)
918 {
919         int len = 0;
920         if (!user_data) {
921                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
922                 return;
923         }
924         download_clientinfo_slot *clientinfoslot =
925                 (download_clientinfo_slot *) user_data;
926         download_clientinfo *clientinfo =
927                 (download_clientinfo *) clientinfoslot->clientinfo;
928         if (!clientinfo) {
929                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
930                 return;
931         }
932         TRACE_DEBUG_INFO_MSG("id[%d],size[%lu]",
933                         download_info->da_dl_req_id, download_info->file_size);
934
935         if (clientinfo->req_id != download_info->da_dl_req_id) {
936                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
937                                 clientinfo->req_id,
938                                 download_info->da_dl_req_id);
939                 return;
940         }
941         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
942         if (!clientinfo->downloadinfo)
943                 clientinfo->downloadinfo =
944                         (download_content_info *) calloc(1, sizeof(download_content_info));
945         if (clientinfo->downloadinfo)
946                 clientinfo->downloadinfo->file_size = download_info->file_size;
947         if (download_info->file_type) {
948                 TRACE_DEBUG_INFO_MSG("mime[%s]", download_info->file_type);
949
950                 len = strlen(download_info->file_type);
951                 if (len > (DP_MAX_STR_LEN - 1))
952                         len = DP_MAX_STR_LEN - 1;
953                 if (clientinfo->downloadinfo) {
954                         strncpy(clientinfo->downloadinfo->mime_type,
955                                 download_info->file_type, len);
956                         download_provider_db_requestinfo_update_column
957                                 (clientinfo, DOWNLOAD_DB_MIMETYPE);
958                 }
959         }
960         if (download_info->tmp_saved_path) {
961                 char *str = NULL;
962                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->tmp_saved_path);
963                 clientinfo->tmp_saved_path =
964                         strdup(download_info->tmp_saved_path);
965                 download_provider_db_requestinfo_update_column(clientinfo,
966                                                                         DOWNLOAD_DB_SAVEDPATH);
967                 str = strrchr(download_info->tmp_saved_path, '/');
968                 if (str) {
969                         str++;
970                         len = strlen(str);
971                         if (len > (DP_MAX_STR_LEN - 1))
972                                 len = DP_MAX_STR_LEN - 1;
973                         if (clientinfo->downloadinfo) {
974                                 strncpy(clientinfo->downloadinfo->content_name,
975                                         str, len);
976                                 download_provider_db_requestinfo_update_column
977                                         (clientinfo, DOWNLOAD_DB_FILENAME);
978                                 TRACE_DEBUG_INFO_MSG("content_name[%s]",
979                                                 clientinfo->downloadinfo->
980                                                 content_name);
981                         }
982                 }
983         }
984
985         if (clientinfo->requestinfo->callbackinfo.started)
986                 ipc_send_downloadinfo(clientinfo);
987
988         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
989 }
990
991 void __downloading_info_cb(user_downloading_info_t *download_info,
992                            void *user_data)
993 {
994         int len = 0;
995         if (!user_data) {
996                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
997                 return;
998         }
999         download_clientinfo_slot *clientinfoslot =
1000                 (download_clientinfo_slot *) user_data;
1001         download_clientinfo *clientinfo =
1002                 (download_clientinfo *) clientinfoslot->clientinfo;
1003         if (!clientinfo) {
1004                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1005                 return;
1006         }
1007         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1008         if (clientinfo->req_id != download_info->da_dl_req_id) {
1009                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1010                                 clientinfo->req_id,
1011                                 download_info->da_dl_req_id);
1012                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1013                 return;
1014         }
1015         if (!clientinfo->downloadinginfo)
1016                 clientinfo->downloadinginfo = (downloading_state_info *) calloc(1,
1017                                                                         sizeof(downloading_state_info));
1018         if (clientinfo->downloadinginfo)
1019                 clientinfo->downloadinginfo->received_size =
1020                         download_info->total_received_size;
1021         if (download_info->saved_path) {
1022                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->saved_path);
1023                 len = strlen(download_info->saved_path);
1024                 if (len > (DP_MAX_PATH_LEN - 1))
1025                         len = DP_MAX_PATH_LEN - 1;
1026                 if (clientinfo->downloadinginfo)
1027                         strncpy(clientinfo->downloadinginfo->saved_path,
1028                                 download_info->saved_path, len);
1029                 /* FIXME : This should be reviewd again after smack rules is applied */
1030                 if (chown(clientinfo->downloadinginfo->saved_path,
1031                                 clientinfo->credentials.uid,
1032                                 clientinfo->credentials.gid) < 0)
1033                         TRACE_DEBUG_INFO_MSG("Fail to chown [%s]", strerror(errno));
1034         }
1035
1036         static size_t updated_second;
1037         time_t tt = time(NULL);
1038         struct tm *localTime = localtime(&tt);
1039
1040         if (updated_second != localTime->tm_sec || download_info->saved_path) { // every 1 second.
1041                 if (clientinfo->requestinfo
1042                         && clientinfo->requestinfo->notification)
1043                         set_downloadinginfo_appfw_notification(clientinfo);
1044                 if (clientinfo->requestinfo->callbackinfo.progress)
1045                         ipc_send_downloadinginfo(clientinfo);
1046                 updated_second = localTime->tm_sec;
1047         }
1048         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1049 }
1050
1051 void __notify_cb(user_notify_info_t *notify_info, void *user_data)
1052 {
1053         if (!user_data) {
1054                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
1055                 return;
1056         }
1057         download_clientinfo_slot *clientinfoslot =
1058                 (download_clientinfo_slot *) user_data;
1059         download_clientinfo *clientinfo =
1060                 (download_clientinfo *) clientinfoslot->clientinfo;
1061         if (!clientinfo) {
1062                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1063                 return;
1064         }
1065
1066         TRACE_DEBUG_INFO_MSG("id[%d],state[%d],err[%d]",
1067                         notify_info->da_dl_req_id,
1068                         notify_info->state, notify_info->err);
1069         if (clientinfo->req_id != notify_info->da_dl_req_id) {
1070                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1071                                 clientinfo->req_id, notify_info->da_dl_req_id);
1072                 return;
1073         }
1074
1075         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1076
1077         clientinfo->state = __change_state(notify_info->state);
1078         clientinfo->err = __change_error(notify_info->err);
1079         if (clientinfo->state == DOWNLOAD_STATE_FINISHED ||
1080                         clientinfo->state == DOWNLOAD_STATE_FAILED) {
1081                 if (clientinfo->requestinfo) {
1082                         if (clientinfo->requestinfo->notification)
1083                                 set_downloadedinfo_appfw_notification(clientinfo);
1084                         download_provider_db_requestinfo_remove(clientinfo->
1085                                 requestinfo->requestid);
1086                 }
1087                 download_provider_db_history_new(clientinfo);
1088                 TRACE_DEBUG_INFO_MSG("[TEST]Finish clientinfo[%p],fd[%d]",
1089                         clientinfo, clientinfo->clientfd);
1090         }
1091
1092         TRACE_DEBUG_INFO_MSG("state[%d]", clientinfo->state);
1093         ipc_send_stateinfo(clientinfo);
1094
1095         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1096 }
1097
1098 int __change_state(da_state state)
1099 {
1100         int ret = DOWNLOAD_STATE_NONE;
1101         switch (state) {
1102         case DA_STATE_WAITING:
1103         case DA_STATE_DOWNLOAD_STARTED:
1104                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOAD_STARTED");
1105                 ret = DOWNLOAD_STATE_READY;
1106                 break;
1107         case DA_STATE_DOWNLOADING:
1108                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOADING");
1109                 ret = DOWNLOAD_STATE_DOWNLOADING;
1110                 break;
1111         case DA_STATE_DOWNLOAD_COMPLETE:
1112                 TRACE_DEBUG_INFO_MSG("DA_STATE_COMPLETE");
1113                 ret = DOWNLOAD_STATE_INSTALLING;
1114                 break;
1115         case DA_STATE_CANCELED:
1116                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED");
1117                 ret = DOWNLOAD_STATE_STOPPED;
1118                 break;
1119         case DA_STATE_CANCELED_ALL:
1120                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED_ALL");
1121                 break;
1122         case DA_STATE_SUSPENDED:
1123                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED");
1124                 ret = DOWNLOAD_STATE_PAUSED;
1125                 break;
1126         case DA_STATE_SUSPENDED_ALL:
1127                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED_ALL");
1128                 break;
1129         case DA_STATE_RESUMED:
1130                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED");
1131                 ret = DOWNLOAD_STATE_DOWNLOADING;
1132                 break;
1133         case DA_STATE_RESUMED_ALL:
1134                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED_ALL");
1135                 break;
1136         case DA_STATE_FINISHED:
1137                 TRACE_DEBUG_INFO_MSG("DA_STATE_FINISHED");
1138                 ret = DOWNLOAD_STATE_FINISHED;
1139                 break;
1140         case DA_STATE_FAILED:
1141                 TRACE_DEBUG_INFO_MSG("DA_STATE_FAILED");
1142                 ret = DOWNLOAD_STATE_FAILED;
1143                 break;
1144         default:
1145                 break;
1146         }
1147         return ret;
1148 }
1149
1150 int __change_error(int err)
1151 {
1152         int ret = DOWNLOAD_ERROR_UNKOWN;
1153         switch (err) {
1154         case DA_RESULT_OK:
1155                 ret = DOWNLOAD_ERROR_NONE;
1156                 break;
1157         case DA_ERR_INVALID_ARGUMENT:
1158                 ret = DOWNLOAD_ERROR_INVALID_PARAMETER;
1159                 break;
1160         case DA_ERR_FAIL_TO_MEMALLOC:
1161                 ret = DOWNLOAD_ERROR_OUT_OF_MEMORY;
1162                 break;
1163         case DA_ERR_UNREACHABLE_SERVER:
1164                 ret = DOWNLOAD_ERROR_NETWORK_UNREACHABLE;
1165                 break;
1166         case DA_ERR_HTTP_TIMEOUT:
1167                 ret = DOWNLOAD_ERROR_CONNECTION_TIMED_OUT;
1168                 break;
1169         case DA_ERR_DISK_FULL:
1170                 ret = DOWNLOAD_ERROR_NO_SPACE;
1171                 break;
1172         case DA_ERR_INVALID_STATE:
1173                 ret = DOWNLOAD_ERROR_INVALID_STATE;
1174                 break;
1175         case DA_ERR_NETWORK_FAIL:
1176                 ret = DOWNLOAD_ERROR_CONNECTION_FAILED;
1177                 break;
1178         case DA_ERR_INVALID_URL:
1179                 ret = DOWNLOAD_ERROR_INVALID_URL;
1180                 break;
1181         case DA_ERR_INVALID_INSTALL_PATH:
1182                 ret = DOWNLOAD_ERROR_INVALID_DESTINATION;
1183                 break;
1184         case DA_ERR_ALREADY_MAX_DOWNLOAD:
1185                 ret = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
1186                 break;
1187         case DA_ERR_FAIL_TO_INSTALL_FILE:
1188                 ret = DOWNLOAD_ERROR_INSTALL_FAIL;
1189                 break;
1190         case DA_ERR_FAIL_TO_CREATE_THREAD:
1191         case DA_ERR_FAIL_TO_OBTAIN_MUTEX:
1192         case DA_ERR_FAIL_TO_ACCESS_FILE:
1193         case DA_ERR_FAIL_TO_GET_CONF_VALUE:
1194         case DA_ERR_FAIL_TO_ACCESS_STORAGE:
1195         case DA_ERR_DLOPEN_FAIL:
1196                 ret = DOWNLOAD_ERROR_IO_ERROR;
1197                 break;
1198         }
1199         return ret;
1200 }
1201
1202 int _init_agent()
1203 {
1204         int da_ret = 0;
1205         da_client_cb_t da_cb = {
1206                 __notify_cb,
1207                 __download_info_cb,
1208                 __downloading_info_cb
1209         };
1210         da_ret = da_init(&da_cb, DA_DOWNLOAD_MANAGING_METHOD_AUTO);
1211         if (da_ret != DA_RESULT_OK) {
1212                 return DOWNLOAD_ERROR_FAIL_INIT_AGENT;
1213         }
1214         return DOWNLOAD_ERROR_NONE;
1215 }
1216
1217 void _deinit_agent()
1218 {
1219         da_deinit();
1220 }