Code Sync with Private GIT
[profile/ivi/download-provider.git] / src / download-provider-ipc.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <errno.h>
7
8 #include "download-provider-ipc.h"
9 #include "download-provider-log.h"
10 #include "bundle.h"
11
12 int ipc_receive_header(int fd)
13 {
14         if (fd <= 0)
15                 return -1;
16
17         download_controls msgheader = 0;
18         if (read(fd, &msgheader, sizeof(download_controls)) < 0) {
19                 TRACE_DEBUG_MSG("failed to read message header (%s)",
20                                 strerror(errno));
21                 return -1;
22         }
23         return msgheader;
24 }
25
26 int ipc_send_stateinfo(download_clientinfo *clientinfo)
27 {
28         if (!clientinfo || clientinfo->clientfd <= 0)
29                 return -1;
30
31         download_state_info stateinfo;
32         download_controls type = DOWNLOAD_CONTROL_GET_STATE_INFO;
33         memset(&stateinfo, 0x00, sizeof(download_state_info));
34         stateinfo.state = clientinfo->state;
35         stateinfo.err = clientinfo->err;
36
37         // send control
38         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
39                 TRACE_DEBUG_MSG("failed to send message header (%s)",
40                                 strerror(errno));
41                 return -1;
42         }
43         if (send(clientinfo->clientfd, &stateinfo, sizeof(download_state_info), 0) < 0) {
44                 TRACE_DEBUG_MSG("failed to send message header (%s)",
45                                 strerror(errno));
46                 return -1;
47         }
48         return type;
49 }
50
51 int ipc_send_request_stateinfo(download_clientinfo *clientinfo)
52 {
53         if (!clientinfo || clientinfo->clientfd <= 0)
54                 return -1;
55
56         download_request_state_info requeststateinfo;
57         download_controls type = DOWNLOAD_CONTROL_GET_REQUEST_STATE_INFO;
58         memset(&requeststateinfo, 0x00, sizeof(download_request_state_info));
59         requeststateinfo.stateinfo.state = clientinfo->state;
60         requeststateinfo.stateinfo.err = clientinfo->err;
61         if (clientinfo->requestinfo)
62                 requeststateinfo.requestid = clientinfo->requestinfo->requestid;
63
64         // send control
65         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
66                 TRACE_DEBUG_MSG("failed to send message header (%s)",
67                                 strerror(errno));
68                 return -1;
69         }
70         if (send
71                 (clientinfo->clientfd, &requeststateinfo, sizeof(download_request_state_info),
72                 0) < 0) {
73                 TRACE_DEBUG_MSG("failed to send message header (%s)",
74                                 strerror(errno));
75                 return -1;
76         }
77         return type;
78 }
79
80 int ipc_send_downloadinfo(download_clientinfo *clientinfo)
81 {
82         if (!clientinfo || clientinfo->clientfd <= 0
83                 || !clientinfo->downloadinfo)
84                 return -1;
85
86         download_controls type = DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO;
87         // send control
88         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
89                 TRACE_DEBUG_MSG("failed to send message header (%s)",
90                                 strerror(errno));
91                 return -1;
92         }
93         if (send
94                 (clientinfo->clientfd, clientinfo->downloadinfo,
95                 sizeof(download_content_info), 0) < 0) {
96                 TRACE_DEBUG_MSG("failed to send message header (%s)",
97                                 strerror(errno));
98                 return -1;
99         }
100         return type;
101 }
102
103 int ipc_send_downloadinginfo(download_clientinfo *clientinfo)
104 {
105         if (!clientinfo || clientinfo->clientfd <= 0
106                 || !clientinfo->downloadinginfo)
107                 return -1;
108
109         download_controls type = DOWNLOAD_CONTROL_GET_DOWNLOADING_INFO;
110         // send control
111         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
112                 TRACE_DEBUG_MSG("failed to send message header (%s)",
113                                 strerror(errno));
114                 return -1;
115         }
116         if (send
117                 (clientinfo->clientfd, clientinfo->downloadinginfo,
118                 sizeof(downloading_state_info), 0) < 0) {
119                 TRACE_DEBUG_MSG("failed to send message header (%s)",
120                                 strerror(errno));
121                 return -1;
122         }
123         return type;
124 }
125
126 extern int service_import_from_bundle(service_h service, bundle *data);
127 int ipc_receive_request_msg(download_clientinfo *clientinfo)
128 {
129         if (!clientinfo || clientinfo->clientfd <= 0)
130                 return -1;
131
132         if (!clientinfo->requestinfo)
133                 clientinfo->requestinfo =
134                         (download_request_info *) calloc(1, sizeof(download_request_info));
135
136         if (!clientinfo->requestinfo)
137                 return -1;
138
139         // read reqeust structure
140         if (read
141                 (clientinfo->clientfd, clientinfo->requestinfo,
142                 sizeof(download_request_info)) < 0) {
143                 TRACE_DEBUG_MSG("failed to read message header");
144                 return -1;
145         }
146         if (clientinfo->requestinfo->client_packagename.length > 1) {
147                 clientinfo->requestinfo->client_packagename.str =
148                         (char *)
149                         calloc((clientinfo->requestinfo->client_packagename.length +
150                                 1), sizeof(char));
151                 if (read
152                         (clientinfo->clientfd,
153                         clientinfo->requestinfo->client_packagename.str,
154                         clientinfo->requestinfo->client_packagename.length *
155                         sizeof(char)) < 0) {
156                         TRACE_DEBUG_MSG
157                                 ("failed to read message header client_app_id(%s)",
158                                 strerror(errno));
159                         return -1;
160                 }
161                 clientinfo->requestinfo->client_packagename.str[clientinfo->
162                                                                 requestinfo->
163                                                                 client_packagename.
164                                                                 length] = '\0';
165                 TRACE_DEBUG_MSG("request client_packagename [%s]",
166                                 clientinfo->requestinfo->client_packagename.
167                                 str);
168         }
169         if (clientinfo->requestinfo->url.length > 1) {
170                 clientinfo->requestinfo->url.str =
171                         (char *)calloc((clientinfo->requestinfo->url.length + 1),
172                                         sizeof(char));
173                 if (read
174                         (clientinfo->clientfd, clientinfo->requestinfo->url.str,
175                         clientinfo->requestinfo->url.length * sizeof(char)) < 0) {
176                         TRACE_DEBUG_MSG("failed to read message header url(%s)",
177                                         strerror(errno));
178                         return -1;
179                 }
180                 clientinfo->requestinfo->url.str[clientinfo->requestinfo->url.
181                                                         length] = '\0';
182                 TRACE_DEBUG_MSG("request url [%s]",
183                                 clientinfo->requestinfo->url.str);
184         }
185         if (clientinfo->requestinfo->install_path.length > 1) {
186                 clientinfo->requestinfo->install_path.str =
187                         (char *)
188                         calloc((clientinfo->requestinfo->install_path.length + 1),
189                                 sizeof(char));
190                 if (read
191                         (clientinfo->clientfd,
192                         clientinfo->requestinfo->install_path.str,
193                         clientinfo->requestinfo->install_path.length *
194                         sizeof(char)) < 0) {
195                         TRACE_DEBUG_MSG
196                                 ("failed to read message header install_path(%s)",
197                                 strerror(errno));
198                         return -1;
199                 }
200                 clientinfo->requestinfo->install_path.str[clientinfo->
201                                                                 requestinfo->
202                                                                 install_path.length] =
203                         '\0';
204                 TRACE_DEBUG_MSG("request install_path [%s]",
205                                 clientinfo->requestinfo->install_path.str);
206         }
207         if (clientinfo->requestinfo->filename.length > 1) {
208                 clientinfo->requestinfo->filename.str =
209                         (char *)
210                         calloc((clientinfo->requestinfo->filename.length + 1),
211                                 sizeof(char));
212                 if (read
213                         (clientinfo->clientfd,
214                         clientinfo->requestinfo->filename.str,
215                         clientinfo->requestinfo->filename.length * sizeof(char)) <
216                         0) {
217                         TRACE_DEBUG_MSG
218                                 ("failed to read message header filename(%s)",
219                                 strerror(errno));
220                         return -1;
221                 }
222                 clientinfo->requestinfo->filename.str[clientinfo->requestinfo->
223                                                                 filename.length] = '\0';
224                 TRACE_DEBUG_MSG("request filename [%s]",
225                                 clientinfo->requestinfo->filename.str);
226         }
227         if (clientinfo->requestinfo->service_data.length > 1) {
228                 bundle_raw *raw_data = NULL;
229                 int len = 0;
230                 bundle *b = NULL;
231                 service_h service_handle;
232                 clientinfo->requestinfo->service_data.str =
233                         (char *)
234                         calloc((clientinfo->requestinfo->service_data.length + 1),
235                                 sizeof(char));
236                 if (read
237                         (clientinfo->clientfd,
238                         clientinfo->requestinfo->service_data.str,
239                         clientinfo->requestinfo->service_data.length * sizeof(char)) <
240                         0) {
241                         TRACE_DEBUG_MSG
242                                 ("failed to read message header filename(%s)",
243                                 strerror(errno));
244                         return -1;
245                 }
246                 clientinfo->requestinfo->service_data.str[clientinfo->requestinfo->
247                                                                 service_data.length] = '\0';
248                 raw_data = (bundle_raw *)clientinfo->requestinfo->service_data.str;
249                 len = clientinfo->requestinfo->service_data.length;
250                 if ((b = bundle_decode(raw_data, len)) == NULL) {
251                         TRACE_DEBUG_MSG("Failed to decode bundle raw data");
252                         return -1;
253                 }
254                 if (service_create(&service_handle) < 0) {
255                         TRACE_DEBUG_MSG("Failed to create service handle");
256                         bundle_free(b);
257                         return -1;
258                 }
259                 if (service_import_from_bundle(service_handle, b) < 0) {
260                         TRACE_DEBUG_MSG("Failed to import service handle");
261                         bundle_free(b);
262                         service_destroy(service_handle);
263                         return -1;
264                 }
265
266                 clientinfo->service_handle = service_handle;
267                 char *pkg = NULL;
268                 service_get_package(service_handle, &pkg);
269                 if (pkg) {
270                         TRACE_DEBUG_MSG("operation### [%s]",pkg);
271                         free(pkg);
272                 }
273
274                 bundle_free(b);
275         }
276         if (clientinfo->requestinfo->headers.rows) {
277                 clientinfo->requestinfo->headers.str =
278                         (download_flexible_string *) calloc(clientinfo->requestinfo->headers.
279                                                 rows, sizeof(download_flexible_string));
280                 int i = 0;
281                 for (i = 0; i < clientinfo->requestinfo->headers.rows; i++) {
282                         if (read
283                                 (clientinfo->clientfd,
284                                 &clientinfo->requestinfo->headers.str[i],
285                                 sizeof(download_flexible_string)) < 0) {
286                                 TRACE_DEBUG_MSG
287                                         ("failed to read message header headers(%s)",
288                                         strerror(errno));
289                                 return -1;
290                         }
291                         if (clientinfo->requestinfo->headers.str[i].length > 1) {
292                                 TRACE_DEBUG_MSG("headers[%d] length[%d]", i,
293                                                 clientinfo->requestinfo->
294                                                 headers.str[i].length);
295                                 clientinfo->requestinfo->headers.str[i].str =
296                                         (char *)
297                                         calloc((clientinfo->requestinfo->headers.
298                                                 str[i].length + 1), sizeof(char));
299                                 if (read
300                                         (clientinfo->clientfd,
301                                         clientinfo->requestinfo->headers.str[i].
302                                         str,
303                                         clientinfo->requestinfo->headers.str[i].
304                                         length * sizeof(char)) < 0) {
305                                         TRACE_DEBUG_MSG
306                                                 ("failed to read message header headers(%s)",
307                                                 strerror(errno));
308                                         return -1;
309                                 }
310                                 clientinfo->requestinfo->headers.str[i].
311                                         str[clientinfo->requestinfo->headers.str[i].
312                                         length] = '\0';
313                                 TRACE_DEBUG_MSG("headers[%d][%s]", i,
314                                                 clientinfo->requestinfo->
315                                                 headers.str[i].str);
316                         }
317                 }
318         }
319         return 0;
320 }