4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin at samsung.com>, Sangchul Lee <sc11.lee at samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/types.h>
27 #include <mm_session_private.h>
34 #define EXPORT_API __attribute__((__visibility__("default")))
35 #define MAX_FILE_LENGTH 256
37 int g_session_type = -1;
40 int mm_session_init(int sessiontype)
43 return mm_session_init_ex(sessiontype, NULL, NULL);
48 int mm_session_init_ex(int sessiontype, session_callback_fn callback, void* user_param)
50 int result = MM_ERROR_NONE;
52 bool do_not_update_session_info = false;
55 debug_log("type : %d", sessiontype);
57 if (sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
58 debug_error("Invalid argument %d",sessiontype);
59 return MM_ERROR_INVALID_ARGUMENT;
62 result = _mm_session_util_read_type(-1, <ype);
63 if (MM_ERROR_INVALID_HANDLE != result) {
64 if ((ltype == MM_SESSION_TYPE_MEDIA_RECORD) && sessiontype == MM_SESSION_TYPE_MEDIA) {
65 /* already set by mm-camcorder, mm-sound(pcm in), keep going */
66 do_not_update_session_info = true;
68 debug_error("Session already initialized. Please finish current session first");
69 return MM_ERROR_POLICY_DUPLICATED;
73 /* Monitor Callback */
74 if (NULL == callback) {
75 debug_warning("Null callback function");
77 debug_warning("It was deprecated, do not use monitor callback, callback(%p), user_param(%p)", callback, user_param);
80 g_session_type = sessiontype;
82 if (!do_not_update_session_info) {
83 result = _mm_session_util_write_type(-1, sessiontype);
84 if (MM_ERROR_NONE != result) {
85 debug_error("Write type failed");
97 int mm_session_update_option(session_update_type_t update_type, int options)
99 int result = MM_ERROR_NONE;
103 debug_log("update_type: %d(0:Add, 1:Remove), options: %x", update_type, options);
105 if (update_type < 0 || update_type >= MM_SESSION_UPDATE_TYPE_NUM) {
106 debug_error("Invalid update_type value(%d)", update_type);
107 return MM_ERROR_INVALID_ARGUMENT;
110 debug_error("Invalid options value(%x)", options);
111 return MM_ERROR_INVALID_ARGUMENT;
114 result = _mm_session_util_read_information(-1, <ype, &loption);
116 debug_error("failed to _mm_session_util_read_information(), ret(%x)", result);
119 debug_log("[current] session_type: %d, session_option: %x", ltype, loption);
121 if (update_type == MM_SESSION_UPDATE_TYPE_ADD) {
123 } else if (update_type == MM_SESSION_UPDATE_TYPE_REMOVE) {
127 result = _mm_session_util_write_information(-1, ltype, loption);
129 debug_error("failed to _mm_session_util_write_information(), ret(%x)", result);
133 debug_log("[updated] session_type: %d, session_option: %x", ltype, loption);
136 return MM_ERROR_NONE;
140 int mm_session_add_watch_callback(int watchevent, int watchstate, watch_callback_fn callback, void* user_param)
146 return MM_ERROR_NOT_SUPPORT_API;
150 int mm_session_get_current_type(int *sessiontype)
152 int result = MM_ERROR_NONE;
157 if (sessiontype == NULL) {
158 debug_error("input argument is NULL\n");
159 return MM_ERROR_INVALID_ARGUMENT;
162 result = _mm_session_util_read_type(-1, <ype);
163 if (result == MM_ERROR_NONE) {
164 debug_log("Current process session type = [%d]\n", ltype);
165 *sessiontype = ltype;
167 debug_error("failed to get current process session type!!\n");
176 int mm_session_get_current_information(int *session_type, int *session_options)
178 int result = MM_ERROR_NONE;
184 if (session_type == NULL) {
185 debug_error("input argument is NULL\n");
186 return MM_ERROR_INVALID_ARGUMENT;
189 result = _mm_session_util_read_information(-1, <ype, &loption);
190 if (result == MM_ERROR_NONE) {
191 debug_log("Current process session type = [%d], options = [%x]\n", ltype, loption);
192 *session_type = ltype;
193 *session_options = loption;
195 debug_error("failed to get current process session type, option!!\n");
204 int mm_session_finish(void)
206 int result = MM_ERROR_NONE;
207 int sessiontype = MM_SESSION_TYPE_MEDIA;
211 result = _mm_session_util_read_type(-1, &sessiontype);
212 if (MM_ERROR_NONE != result) {
213 debug_error("Can not read current type");
217 /* Check monitor handle */
218 result = _mm_session_util_delete_information(-1);
219 if(result != MM_ERROR_NONE)
224 return MM_ERROR_NONE;
228 int mm_session_remove_watch_callback(int watchevent, int watchstate)
234 return MM_ERROR_NOT_SUPPORT_API;
238 int mm_session_set_subsession(mm_subsession_t subsession, mm_subsession_option_t option)
244 return MM_ERROR_NOT_SUPPORT_API;
248 int mm_session_get_subsession(mm_subsession_t *subsession)
254 return MM_ERROR_NOT_SUPPORT_API;
258 int mm_session_set_subevent(mm_session_sub_t subevent)
264 return MM_ERROR_NOT_SUPPORT_API;
268 int mm_session_get_subevent(mm_session_sub_t *subevent)
274 return MM_ERROR_NOT_SUPPORT_API;
278 int mm_session_reset_resumption_info(void)
284 return MM_ERROR_NOT_SUPPORT_API;
288 int _mm_session_util_delete_information(int app_pid)
291 char filename[MAX_FILE_LENGTH];
296 mypid = (pid_t)app_pid;
298 ////// DELETE SESSION TYPE /////////
299 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
300 if(-1 == unlink(filename))
301 return MM_ERROR_FILE_NOT_FOUND;
302 ////// DELETE SESSION TYPE /////////
304 return MM_ERROR_NONE;
308 int _mm_session_util_write_type(int app_pid, int sessiontype)
312 char filename[MAX_FILE_LENGTH];
314 if(sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
315 return MM_ERROR_INVALID_ARGUMENT;
321 mypid = (pid_t)app_pid;
323 ////// WRITE SESSION TYPE /////////
324 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
325 fd = open(filename, O_WRONLY | O_CREAT, 0644 );
327 debug_error("open() failed with %d",errno);
328 return MM_ERROR_FILE_WRITE;
330 sessiontype = sessiontype << 16;
331 write(fd, &sessiontype, sizeof(int));
332 if(0 > fchmod (fd, 00777)) {
333 debug_error("fchmod failed with %d", errno);
335 debug_warning("write sessiontype(%d) to /tmp/mm_session_%d", sessiontype >> 16, mypid);
338 ////// WRITE SESSION TYPE /////////
340 return MM_ERROR_NONE;
344 int _mm_session_util_read_type(int app_pid, int *sessiontype)
348 char filename[MAX_FILE_LENGTH];
352 if(sessiontype == NULL)
353 return MM_ERROR_INVALID_ARGUMENT;
358 mypid = (pid_t)app_pid;
360 ////// READ SESSION TYPE /////////
361 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
362 fd = open(filename, O_RDONLY);
364 return MM_ERROR_INVALID_HANDLE;
366 read(fd, sessiontype, sizeof(int));
367 *sessiontype = *sessiontype >> 16;
368 debug_warning("read sessiontype(%d) from /tmp/mm_session_%d", *sessiontype, mypid);
370 ////// READ SESSION TYPE /////////
374 return MM_ERROR_NONE;
378 int _mm_session_util_write_information(int app_pid, int session_type, int flags)
382 char filename[MAX_FILE_LENGTH];
385 if(session_type < MM_SESSION_TYPE_MEDIA || session_type >= MM_SESSION_TYPE_NUM) {
386 return MM_ERROR_INVALID_ARGUMENT;
389 return MM_ERROR_INVALID_ARGUMENT;
395 mypid = (pid_t)app_pid;
398 ////// WRITE SESSION INFO /////////
399 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
400 fd = open(filename, O_WRONLY | O_CREAT, 0644 );
402 debug_error("open() failed with %d",errno);
403 return MM_ERROR_FILE_WRITE;
406 result_info = (flags) | (session_type << 16);
407 write(fd, &result_info, sizeof(int));
408 if(0 > fchmod (fd, 00777)) {
409 debug_error("fchmod failed with %d", errno);
411 debug_warning("write session information(%x) to /tmp/mm_session_%d", result_info, mypid);
414 ////// WRITE SESSION INFO /////////
416 return MM_ERROR_NONE;
420 int _mm_session_util_read_information(int app_pid, int *session_type, int *flags)
424 char filename[MAX_FILE_LENGTH];
429 if(session_type == NULL || flags == NULL) {
430 return MM_ERROR_INVALID_ARGUMENT;
436 mypid = (pid_t)app_pid;
439 ////// READ SESSION INFO /////////
440 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
441 fd = open(filename, O_RDONLY);
443 return MM_ERROR_INVALID_HANDLE;
445 read(fd, &result_info, sizeof(int));
446 *session_type = result_info >> 16;
447 *flags = result_info & 0x0000ffff;
449 debug_warning("read session_type(%d), session_option(%x) from /tmp/mm_session_%d", *session_type, *flags, mypid);
451 ////// READ SESSION INFO /////////
455 return MM_ERROR_NONE;
458 __attribute__ ((destructor))
459 void __mmsession_finalize(void)
463 _mm_session_util_delete_information(-1);
468 __attribute__ ((constructor))
469 void __mmsession_initialize(void)