66e94ad707772a7e424c8fb73ffdfe2f2dcd7dda
[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
11 int ipc_receive_header(int fd)
12 {
13         if (fd <= 0)
14                 return -1;
15
16         download_controls msgheader = 0;
17         if (read(fd, &msgheader, sizeof(download_controls)) < 0) {
18                 TRACE_DEBUG_MSG("failed to read message header (%s)",
19                                 strerror(errno));
20                 return -1;
21         }
22         return msgheader;
23 }
24
25 int ipc_send_stateinfo(download_clientinfo *clientinfo)
26 {
27         if (!clientinfo || clientinfo->clientfd <= 0)
28                 return -1;
29
30         download_state_info stateinfo;
31         download_controls type = DOWNLOAD_CONTROL_GET_STATE_INFO;
32         memset(&stateinfo, 0x00, sizeof(download_state_info));
33         stateinfo.state = clientinfo->state;
34         stateinfo.err = clientinfo->err;
35
36         // send control
37         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
38                 TRACE_DEBUG_MSG("failed to send message header (%s)",
39                                 strerror(errno));
40                 return -1;
41         }
42         if (send(clientinfo->clientfd, &stateinfo, sizeof(download_state_info), 0) < 0) {
43                 TRACE_DEBUG_MSG("failed to send message header (%s)",
44                                 strerror(errno));
45                 return -1;
46         }
47         return type;
48 }
49
50 int ipc_send_request_stateinfo(download_clientinfo *clientinfo)
51 {
52         if (!clientinfo || clientinfo->clientfd <= 0)
53                 return -1;
54
55         download_request_state_info requeststateinfo;
56         download_controls type = DOWNLOAD_CONTROL_GET_REQUEST_STATE_INFO;
57         memset(&requeststateinfo, 0x00, sizeof(download_request_state_info));
58         requeststateinfo.stateinfo.state = clientinfo->state;
59         requeststateinfo.stateinfo.err = clientinfo->err;
60         if (clientinfo->requestinfo)
61                 requeststateinfo.requestid = clientinfo->requestinfo->requestid;
62
63         // send control
64         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
65                 TRACE_DEBUG_MSG("failed to send message header (%s)",
66                                 strerror(errno));
67                 return -1;
68         }
69         if (send
70                 (clientinfo->clientfd, &requeststateinfo, sizeof(download_request_state_info),
71                 0) < 0) {
72                 TRACE_DEBUG_MSG("failed to send message header (%s)",
73                                 strerror(errno));
74                 return -1;
75         }
76         return type;
77 }
78
79 int ipc_send_downloadinfo(download_clientinfo *clientinfo)
80 {
81         if (!clientinfo || clientinfo->clientfd <= 0
82                 || !clientinfo->downloadinfo)
83                 return -1;
84
85         download_controls type = DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO;
86         // send control
87         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
88                 TRACE_DEBUG_MSG("failed to send message header (%s)",
89                                 strerror(errno));
90                 return -1;
91         }
92         if (send
93                 (clientinfo->clientfd, clientinfo->downloadinfo,
94                 sizeof(download_content_info), 0) < 0) {
95                 TRACE_DEBUG_MSG("failed to send message header (%s)",
96                                 strerror(errno));
97                 return -1;
98         }
99         return type;
100 }
101
102 int ipc_send_downloadinginfo(download_clientinfo *clientinfo)
103 {
104         if (!clientinfo || clientinfo->clientfd <= 0
105                 || !clientinfo->downloadinginfo)
106                 return -1;
107
108         download_controls type = DOWNLOAD_CONTROL_GET_DOWNLOADING_INFO;
109         // send control
110         if (send(clientinfo->clientfd, &type, sizeof(download_controls), 0) < 0) {
111                 TRACE_DEBUG_MSG("failed to send message header (%s)",
112                                 strerror(errno));
113                 return -1;
114         }
115         if (send
116                 (clientinfo->clientfd, clientinfo->downloadinginfo,
117                 sizeof(downloading_state_info), 0) < 0) {
118                 TRACE_DEBUG_MSG("failed to send message header (%s)",
119                                 strerror(errno));
120                 return -1;
121         }
122         return type;
123 }
124
125 int ipc_receive_request_msg(download_clientinfo *clientinfo)
126 {
127         if (!clientinfo || clientinfo->clientfd <= 0)
128                 return -1;
129
130         if (!clientinfo->requestinfo)
131                 clientinfo->requestinfo =
132                         (download_request_info *) calloc(1, sizeof(download_request_info));
133
134         if (!clientinfo->requestinfo)
135                 return -1;
136
137         // read reqeust structure
138         if (read
139                 (clientinfo->clientfd, clientinfo->requestinfo,
140                 sizeof(download_request_info)) < 0) {
141                 TRACE_DEBUG_MSG("failed to read message header");
142                 return -1;
143         }
144         if (clientinfo->requestinfo->client_packagename.length > 0) {
145                 clientinfo->requestinfo->client_packagename.str =
146                         (char *)
147                         calloc((clientinfo->requestinfo->client_packagename.length +
148                                 1), sizeof(char));
149                 if (read
150                         (clientinfo->clientfd,
151                         clientinfo->requestinfo->client_packagename.str,
152                         clientinfo->requestinfo->client_packagename.length *
153                         sizeof(char)) < 0) {
154                         TRACE_DEBUG_MSG
155                                 ("failed to read message header client_app_id(%s)",
156                                 strerror(errno));
157                         return -1;
158                 }
159                 clientinfo->requestinfo->client_packagename.str[clientinfo->
160                                                                 requestinfo->
161                                                                 client_packagename.
162                                                                 length] = '\0';
163                 TRACE_DEBUG_MSG("request client_packagename [%s]",
164                                 clientinfo->requestinfo->client_packagename.
165                                 str);
166         }
167         if (clientinfo->requestinfo->url.length > 0) {
168                 clientinfo->requestinfo->url.str =
169                         (char *)calloc((clientinfo->requestinfo->url.length + 1),
170                                         sizeof(char));
171                 if (read
172                         (clientinfo->clientfd, clientinfo->requestinfo->url.str,
173                         clientinfo->requestinfo->url.length * sizeof(char)) < 0) {
174                         TRACE_DEBUG_MSG("failed to read message header url(%s)",
175                                         strerror(errno));
176                         return -1;
177                 }
178                 clientinfo->requestinfo->url.str[clientinfo->requestinfo->url.
179                                                         length] = '\0';
180                 TRACE_DEBUG_MSG("request url [%s]",
181                                 clientinfo->requestinfo->url.str);
182         }
183         if (clientinfo->requestinfo->install_path.length > 0) {
184                 clientinfo->requestinfo->install_path.str =
185                         (char *)
186                         calloc((clientinfo->requestinfo->install_path.length + 1),
187                                 sizeof(char));
188                 if (read
189                         (clientinfo->clientfd,
190                         clientinfo->requestinfo->install_path.str,
191                         clientinfo->requestinfo->install_path.length *
192                         sizeof(char)) < 0) {
193                         TRACE_DEBUG_MSG
194                                 ("failed to read message header install_path(%s)",
195                                 strerror(errno));
196                         return -1;
197                 }
198                 clientinfo->requestinfo->install_path.str[clientinfo->
199                                                                 requestinfo->
200                                                                 install_path.length] =
201                         '\0';
202                 TRACE_DEBUG_MSG("request install_path [%s]",
203                                 clientinfo->requestinfo->install_path.str);
204         }
205         if (clientinfo->requestinfo->filename.length > 0) {
206                 clientinfo->requestinfo->filename.str =
207                         (char *)
208                         calloc((clientinfo->requestinfo->filename.length + 1),
209                                 sizeof(char));
210                 if (read
211                         (clientinfo->clientfd,
212                         clientinfo->requestinfo->filename.str,
213                         clientinfo->requestinfo->filename.length * sizeof(char)) <
214                         0) {
215                         TRACE_DEBUG_MSG
216                                 ("failed to read message header filename(%s)",
217                                 strerror(errno));
218                         return -1;
219                 }
220                 clientinfo->requestinfo->filename.str[clientinfo->requestinfo->
221                                                                 filename.length] = '\0';
222                 TRACE_DEBUG_MSG("request filename [%s]",
223                                 clientinfo->requestinfo->filename.str);
224         }
225         if (clientinfo->requestinfo->headers.rows) {
226                 clientinfo->requestinfo->headers.str =
227                         (download_flexible_string *) calloc(clientinfo->requestinfo->headers.
228                                                 rows, sizeof(download_flexible_string));
229                 int i = 0;
230                 for (i = 0; i < clientinfo->requestinfo->headers.rows; i++) {
231                         if (read
232                                 (clientinfo->clientfd,
233                                 &clientinfo->requestinfo->headers.str[i],
234                                 sizeof(download_flexible_string)) < 0) {
235                                 TRACE_DEBUG_MSG
236                                         ("failed to read message header headers(%s)",
237                                         strerror(errno));
238                                 return -1;
239                         }
240                         if (clientinfo->requestinfo->headers.str[i].length > 0) {
241                                 TRACE_DEBUG_MSG("headers[%d] length[%d]", i,
242                                                 clientinfo->requestinfo->
243                                                 headers.str[i].length);
244                                 clientinfo->requestinfo->headers.str[i].str =
245                                         (char *)
246                                         calloc((clientinfo->requestinfo->headers.
247                                                 str[i].length + 1), sizeof(char));
248                                 if (read
249                                         (clientinfo->clientfd,
250                                         clientinfo->requestinfo->headers.str[i].
251                                         str,
252                                         clientinfo->requestinfo->headers.str[i].
253                                         length * sizeof(char)) < 0) {
254                                         TRACE_DEBUG_MSG
255                                                 ("failed to read message header headers(%s)",
256                                                 strerror(errno));
257                                         return -1;
258                                 }
259                                 clientinfo->requestinfo->headers.str[i].
260                                         str[clientinfo->requestinfo->headers.str[i].
261                                         length] = '\0';
262                                 TRACE_DEBUG_MSG("headers[%d][%s]", i,
263                                                 clientinfo->requestinfo->
264                                                 headers.str[i].str);
265                         }
266                 }
267         }
268         return 0;
269 }