416fc4e8efbadef8232cf6156703ba5ad64e1a68
[framework/multimedia/libmedia-service.git] / src / common / media-info.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <errno.h>
22 #include <unistd.h>
23 #include <arpa/inet.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <sys/socket.h>
28 #include <util-func.h>
29 #include "media-info.h"
30 #include "media-info-debug.h"
31 #include "media-info-env.h"
32 #include "media-info-util.h"
33 #include "media-svc.h"
34 #include "audio-svc.h"
35 #include "audio-svc-error.h"
36
37 #define TIMEOUT_SEC             10
38
39 int mediainfo_open(void)
40 {
41         mediainfo_dbg("");
42         int err = -1;
43
44         err = minfo_init();
45         if (err < MB_SVC_ERROR_NONE) {
46                 mediainfo_dbg("minfo_init falis");
47         }
48
49         err = audio_svc_open();
50         if (err < AUDIO_SVC_ERROR_NONE) {
51                 mediainfo_dbg("audio_svc_open falis");
52         }
53
54         if (err < 0) return MEDIA_INFO_ERROR_DATABASE_CONNECT;
55
56         mediainfo_dbg("Success");
57         return err;
58 }
59
60 int mediainfo_close(void)
61 {
62         mediainfo_dbg("");
63         int err = -1;
64
65         err = minfo_finalize();
66         if (err < MB_SVC_ERROR_NONE) {
67                 mediainfo_dbg("minfo_finalize falis");
68         }
69
70         err = audio_svc_close();
71         if (err < AUDIO_SVC_ERROR_NONE) {
72                 mediainfo_dbg("audio_svc_close falis");
73         }
74
75         if (err < 0) return MEDIA_INFO_ERROR_DATABASE_DISCONNECT;
76
77         mediainfo_dbg("Success");
78         return err;
79 }
80
81 int mediainfo_connect_db_with_handle(sqlite3 **db_handle)
82 {
83         mediainfo_dbg("");
84         int err = -1;
85         err =
86             db_util_open(MEDIA_INFO_DATABASE_NAME, db_handle,
87                          DB_UTIL_REGISTER_HOOK_METHOD);
88
89         if (SQLITE_OK != err) {
90                 *db_handle = NULL;
91
92                 return MEDIA_INFO_ERROR_DATABASE_CONNECT;
93         }
94
95         return MEDIA_INFO_ERROR_NONE;
96 }
97
98 int mediainfo_disconnect_db_with_handle(sqlite3 *db_handle)
99 {
100         mediainfo_dbg("");
101         int err = -1;
102         err = db_util_close(db_handle);
103
104         if (SQLITE_OK != err) {
105                 db_handle = NULL;
106
107                 return MEDIA_INFO_ERROR_DATABASE_DISCONNECT;
108         }
109
110         return MEDIA_INFO_ERROR_NONE;
111 }
112
113 #define MINFO_REGISTER_PORT 1001
114
115 static bool _mediainfo_is_valid_path(const char *path)
116 {
117        if (path == NULL)
118                return false;
119
120         if (strncmp(path, MEDIAINFO_PHONE_ROOT_PATH, strlen(MEDIAINFO_PHONE_ROOT_PATH)) == 0) {
121                 return true;
122         } else if (strncmp(path, MEDIAINFO_MMC_ROOT_PATH, strlen(MEDIAINFO_MMC_ROOT_PATH)) == 0) {
123                 return true;
124         } else
125                 return false;
126
127        return true;
128 }
129
130 DEPRECATED_API int mediainfo_register_file(const char *file_full_path)
131 {
132         int exist;
133         int err;
134         int sockfd;
135         int recv_msg = MEDIA_INFO_ERROR_NONE;
136         int server_addr_size;
137         struct sockaddr_in server_addr;
138         struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
139
140         if(!_mediainfo_is_valid_path(file_full_path)) {
141                 mediainfo_dbg("Invalid path : %s", file_full_path);
142                 return MEDIA_INFO_ERROR_INVALID_PATH;
143         }
144
145         exist = open(file_full_path, O_RDONLY);
146         if(exist < 0) {
147                 mediainfo_dbg("Not exist path : %s", file_full_path);
148                 return MEDIA_INFO_ERROR_INVALID_PATH;
149         }
150         close(exist);
151
152         sockfd = socket(PF_INET, SOCK_DGRAM, 0);
153         if(sockfd < 0)
154         {
155                 mediainfo_dbg("socket create fail");
156                 return MEDIA_INFO_ERROR_SOCKET_CONN;
157         }
158
159         /*add timeout : timeout is 10 sec.*/
160         if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
161                 mediainfo_dbg("setsockopt failed");
162                 return MEDIA_INFO_ERROR_SOCKET_CONN;
163         }
164
165         memset(&server_addr, 0, sizeof(server_addr));
166         server_addr.sin_family = AF_INET;
167         server_addr.sin_port = htons(MINFO_REGISTER_PORT);
168         server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
169
170         err = sendto(sockfd, file_full_path, strlen(file_full_path), 0, (struct sockaddr*)&server_addr, sizeof(server_addr));
171         if (err < 0) {
172                 mediainfo_dbg("sendto error");
173                 perror("sendto error : ");
174                 return MEDIA_INFO_ERROR_SOCKET_SEND;
175         } else {
176                 mediainfo_dbg("SEND OK");
177         }
178
179         server_addr_size = sizeof(server_addr);
180         err = recvfrom(sockfd, &recv_msg, sizeof(recv_msg), 0 , (struct sockaddr*)&server_addr, (socklen_t *)&server_addr_size);
181         if (err < 0) {
182                 if (errno == EWOULDBLOCK) {
183                         mediainfo_dbg("recvfrom timeout");
184                         return MEDIA_INFO_ERROR_SOCKET_RECEIVE_TIMEOUT;
185                 } else {
186                         mediainfo_dbg("recvfrom error");
187                         perror("recvfrom error : ");
188                         return MEDIA_INFO_ERROR_SOCKET_RECEIVE;
189                 }
190         } else {
191                 mediainfo_dbg("RECEIVE OK");
192                 mediainfo_dbg("client receive: %d", recv_msg);
193         }       
194         
195         close(sockfd);
196
197         return recv_msg;
198 }
199
200 DEPRECATED_API int mediainfo_list_new(minfo_list *list)
201 {
202         *list = g_array_new(TRUE, TRUE, sizeof(char*));
203
204         return MB_SVC_ERROR_NONE;
205 }
206
207 DEPRECATED_API int mediainfo_list_add(minfo_list list, const char* file_full_path)
208 {
209         mediainfo_dbg("");
210
211         if (!list) {
212                 mediainfo_dbg("list == NULL");
213                 return MB_SVC_ERROR_INVALID_PARAMETER;
214         }
215
216         if (!file_full_path) {
217                 mediainfo_dbg("file_full_path == NULL");
218                 return MB_SVC_ERROR_INVALID_PARAMETER;
219         }
220
221         minfo_list ret_list = NULL;
222         char *path = strdup(file_full_path);
223
224         int len = list->len + 1;
225         int i;
226         char *data = NULL;
227
228         ret_list = g_array_append_val(list, path);
229         if(ret_list == NULL) {
230                 mediainfo_dbg("g_array_append_val fails");
231                 return MB_SVC_ERROR_UNKNOWN;
232         }
233
234         list = ret_list;
235
236         for(i = 0; i < len; i++) {
237                 data = g_array_index(list, char*, i);
238                 mediainfo_dbg("%d, %s", i, data);
239         }
240
241         return MB_SVC_ERROR_NONE;
242 }
243
244 DEPRECATED_API int mediainfo_list_free(minfo_list list)
245 {
246         if (!list)
247                 return MB_SVC_ERROR_INVALID_PARAMETER;
248
249         int len = list->len + 1;
250         int i;
251         char *data = NULL;
252
253         for(i = 0; i < len; i++) {
254                 data = g_array_index(list, char*, i);
255                 free(data);
256         }
257
258         g_array_free(list, TRUE);
259
260         return MB_SVC_ERROR_NONE;
261 }
262
263 DEPRECATED_API int mediainfo_register_files(const minfo_list list)
264 {
265         if (!list)
266                 return MB_SVC_ERROR_INVALID_PARAMETER;
267
268         int len = list->len + 1;
269         int i;
270         char *data;
271
272         for(i = 0; i < len; i++) {
273                 data = g_array_index(list, char*, i);
274                 mediainfo_register_file(data);
275         }
276
277         return MB_SVC_ERROR_NONE;
278 }