/*
* Contacts Service
*
- * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
*
- * Contact: Dohyung Jin <dh.jin@samsung.com>
- * Jongwon Lee <gogosing.lee@samsung.com>
- * Donghee Ye <donghee.ye@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*
*/
-#include <errno.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <unistd.h>
-#include <pims-ipc-data.h>
-
#include "contacts.h"
#include "ctsvc_internal.h"
-#include "ctsvc_socket.h"
-#include "ctsvc_mutex.h"
-#include "ctsvc_inotify.h"
#include "ctsvc_client_ipc.h"
-
-static int ctsvc_connection = 0;
-
-static __thread int ctsvc_connection_on_thread = 0;
+#include "ctsvc_client_utils.h"
+#include "ctsvc_client_handle.h"
+#include "ctsvc_client_service_helper.h"
API int contacts_connect_with_flags(unsigned int flags)
{
CTS_FN_CALL;
- int ret = CONTACTS_ERROR_NONE;
-
- ret = contacts_connect2();
- if (ret == CONTACTS_ERROR_NONE)
- return ret;
-
- if (flags & CONTACTS_CONNECT_FLAG_RETRY) {
- int i;
- int waiting_time = 500;
- for (i=0;i<9;i++) {
- usleep(waiting_time * 1000);
- DBG("retry cnt=%d, ret=%x, %d",(i+1), ret, waiting_time);
- ret = contacts_connect2();
- if (ret == CONTACTS_ERROR_NONE)
- break;
- if (6 < i)
- waiting_time += 30000;
- else
- waiting_time *= 2;
+ int ret;
+ contacts_h contact = NULL;
+ unsigned int id = ctsvc_client_get_pid();
+
+ ret = ctsvc_client_handle_get_p_with_id(id, &contact);
+ if (CONTACTS_ERROR_NO_DATA == ret) {
+ ret = ctsvc_client_handle_create(id, &contact);
+ if (CONTACTS_ERROR_NONE != ret) {
+ ERR("ctsvc_client_handle_create() Fail(%d)", ret);
+ if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
+ return CONTACTS_ERROR_INTERNAL;
+ return ret;
}
+ } else if (CONTACTS_ERROR_NONE != ret) {
+ ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
+ return ret;
}
+ ret = ctsvc_client_connect_with_flags(contact, flags);
+ if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
+ || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
+ return CONTACTS_ERROR_IPC;
+
return ret;
}
-API int contacts_connect2()
+API int contacts_connect(void)
{
CTS_FN_CALL;
int ret;
-
- ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
- if (0 == ctsvc_connection) {
-
- ret = ctsvc_ipc_connect();
- if (ret != CONTACTS_ERROR_NONE) {
- CTS_ERR("ctsvc_ipc_connect() Failed(%d)", ret);
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
- return ret;
- }
-
- ret = ctsvc_socket_init();
- if (ret != CONTACTS_ERROR_NONE) {
- CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
+ contacts_h contact = NULL;
+ unsigned int id = ctsvc_client_get_pid();
+
+ ret = ctsvc_client_handle_get_p_with_id(id, &contact);
+ if (CONTACTS_ERROR_NO_DATA == ret) {
+ ret = ctsvc_client_handle_create(id, &contact);
+ if (CONTACTS_ERROR_NONE != ret) {
+ ERR("ctsvc_client_handle_create() Fail(%d)", ret);
+ if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
+ return CONTACTS_ERROR_INTERNAL;
return ret;
}
-
- ret = ctsvc_inotify_init();
- if (ret != CONTACTS_ERROR_NONE) {
- CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
- ctsvc_socket_final();
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
- return ret;
- }
-
- ctsvc_view_uri_init();
- ctsvc_ipc_create_for_change_subscription();
+ } else if (CONTACTS_ERROR_NONE != ret) {
+ ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
+ return ret;
}
- else
- CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
+ ret = ctsvc_client_connect(contact);
+ if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
+ || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
+ return CONTACTS_ERROR_IPC;
- ctsvc_connection++;
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
-
- return CONTACTS_ERROR_NONE;
+ return ret;
}
-API int contacts_disconnect2()
+API int contacts_disconnect(void)
{
- int ret;
-
CTS_FN_CALL;
+ int ret;
+ contacts_h contact = NULL;
+ unsigned int id = ctsvc_client_get_pid();
- ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
- if (1 == ctsvc_connection) {
- ctsvc_ipc_destroy_for_change_subscription();
-
- ret = ctsvc_ipc_disconnect();
- if (ret != CONTACTS_ERROR_NONE) {
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
- CTS_ERR("ctsvc_ipc_disconnect() Failed(%d)", ret);
- return ret;
- }
-
- ctsvc_view_uri_deinit();
- ctsvc_inotify_close();
- ctsvc_socket_final();
- }
- else if (1 < ctsvc_connection)
- CTS_DBG("System : connection count is %d", ctsvc_connection);
- else {
- CTS_DBG("System : please call contacts_connect2(), connection count is (%d)", ctsvc_connection);
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
- return CONTACTS_ERROR_INVALID_PARAMETER;
- }
+ ret = ctsvc_client_handle_get_p_with_id(id, &contact);
+ RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
+ RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
- ctsvc_connection--;
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
+ ret = ctsvc_client_disconnect(contact);
+ WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect() Fail(%d)", ret);
+ if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
+ ret = CONTACTS_ERROR_IPC;
- return CONTACTS_ERROR_NONE;
+ return ret;
}
-API int contacts_connect_on_thread()
+API int contacts_connect_on_thread(void)
{
+ CTS_FN_CALL;
int ret;
-
- ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
-
- ret = ctsvc_ipc_connect_on_thread();
- if (ret != CONTACTS_ERROR_NONE) {
- CTS_ERR("ctsvc_ipc_connect_on_thread() Failed(%d)", ret);
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
+ contacts_h contact = NULL;
+ unsigned int id = ctsvc_client_get_tid();
+
+ ret = ctsvc_client_handle_get_p_with_id(id, &contact);
+ if (CONTACTS_ERROR_NO_DATA == ret) {
+ ret = ctsvc_client_handle_create(id, &contact);
+ if (CONTACTS_ERROR_NONE != ret) {
+ ERR("ctsvc_client_handle_create() Fail(%d)", ret);
+ if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
+ return CONTACTS_ERROR_INTERNAL;
+ return ret;
+ }
+ } else if (CONTACTS_ERROR_NONE != ret) {
+ ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
return ret;
}
- if (0 < ctsvc_connection_on_thread)
- CTS_DBG("System : Contacts service has been already connected");
-
- ctsvc_connection_on_thread++;
-
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
+ ret = ctsvc_client_connect_on_thread(contact);
+ WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_connect_on_thread() Fail(%d)", ret);
+ if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
+ || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
+ return CONTACTS_ERROR_IPC;
- return CONTACTS_ERROR_NONE;
+ return ret;
}
-API int contacts_disconnect_on_thread()
+API int contacts_disconnect_on_thread(void)
{
+ CTS_FN_CALL;
int ret;
+ contacts_h contact = NULL;
+ unsigned int id = ctsvc_client_get_tid();
- ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
-
- ret = ctsvc_ipc_disconnect_on_thread();
- if (ret != CONTACTS_ERROR_NONE) {
- CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
- return ret;
- }
-
- if (1 == ctsvc_connection_on_thread) {
- CTS_DBG("System : connection_on_thread was destroyed successfully");
- }
- else if (1 < ctsvc_connection_on_thread) {
- CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
- }
- else {
- CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
- return CONTACTS_ERROR_INVALID_PARAMETER;
- }
+ ret = ctsvc_client_handle_get_p_with_id(id, &contact);
+ RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
+ RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
- ctsvc_connection_on_thread--;
+ ret = ctsvc_client_disconnect_on_thread(contact);
+ WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect_on_thread() Fail(%d)", ret);
+ if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
+ ret = CONTACTS_ERROR_IPC;
- ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
-
- return CONTACTS_ERROR_NONE;
+ return ret;
}
+