Add some patches 07/250707/3 accepted/tizen/unified/20210201.055431 accepted/tizen/unified/20210203.145513 submit/tizen/20210112.005356 submit/tizen/20210131.225751 submit/tizen/20210203.045513
authorhyunuk.tak <hyunuk.tak@samsung.com>
Mon, 4 Jan 2021 04:19:31 +0000 (13:19 +0900)
committerhyunuk.tak <hyunuk.tak@samsung.com>
Mon, 11 Jan 2021 09:02:40 +0000 (18:02 +0900)
Fix circleCI script
Undefined OS type , OS updation for single device ID fix
OS_update in plugin bug fix
Fix to not to use docker image
Fix device detection failure due to payload
Change the plugin path to RO area
Fix stop detection event
Fix user absence event
Fix set service detection cycle functionality
Remove implicit call of stop_detection before start_detection
Send unique payloads to plugins in registered device list
Add scan mode functionality for start detection
Terminate ua-manager if no client is referencing it

Change-Id: I5bd6d8d421071d534991818d3e0128c5d68ce8d6
Signed-off-by: hyunuk.tak <hyunuk.tak@samsung.com>
43 files changed:
.circleci/.gbs.conf [deleted file]
.circleci/config.yml [deleted file]
include/ua-api.h
include/ua-internal.h
packaging/ua-manager.conf
packaging/ua-manager.spec
ua-api/include/ua-common.h
ua-api/include/ua-event-handler.h
ua-api/src/ua-api.c
ua-api/src/ua-common.c
ua-api/src/ua-event-handler.c
ua-api/src/ua-request-sender.c
ua-daemon/CMakeLists.txt
ua-daemon/data/ua_db.sql
ua-daemon/include/ua-manager-common.h
ua-daemon/include/ua-manager-core.h
ua-daemon/include/ua-manager-database.h
ua-daemon/include/ua-plugin-manager.h
ua-daemon/src/pm/ua-ble-plugin-handler.c [deleted file]
ua-daemon/src/pm/ua-cloud-plugin-handler.c
ua-daemon/src/pm/ua-light-plugin-handler.c [deleted file]
ua-daemon/src/pm/ua-motion-plugin-handler.c [deleted file]
ua-daemon/src/pm/ua-plugin-handler.c [new file with mode: 0644]
ua-daemon/src/pm/ua-plugin-manager.c
ua-daemon/src/pm/ua-pm-util.c
ua-daemon/src/pm/ua-vendor-plugin-manager.c
ua-daemon/src/pm/ua-wifi-plugin-handler.c [deleted file]
ua-daemon/src/ua-manager-adv-db.c
ua-daemon/src/ua-manager-app-db.c [new file with mode: 0644]
ua-daemon/src/ua-manager-common.c
ua-daemon/src/ua-manager-core.c [changed mode: 0644->0755]
ua-daemon/src/ua-manager-db.c
ua-daemon/src/ua-manager-device-db.c
ua-daemon/src/ua-manager-device-service-db.c
ua-daemon/src/ua-manager-event-sender.c
ua-daemon/src/ua-manager-main.c
ua-daemon/src/ua-manager-payload-db.c
ua-daemon/src/ua-manager-request-handler.c [changed mode: 0644->0755]
ua-daemon/src/ua-manager-service-db.c
ua-daemon/src/ua-manager-user-db.c
ua-plugins/include/ua-cloud-plugin.h
ua-plugins/include/ua-plugin.h
ua-plugins/include/ua-vendor-plugin.h

diff --git a/.circleci/.gbs.conf b/.circleci/.gbs.conf
deleted file mode 100644 (file)
index 427fe45..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-[general]
-profile = profile.tizen
-
-[profile.tizen]
-obs = obs.tizen
-repos = repo.tizen_base, repo.unified
-buildroot = ~/GBS-ROOT/
-
-[obs.tizen]
-url = https://api.tizen.org
-
-[repo.tizen_base]
-url=http://download.tizen.org/snapshots/tizen/base/latest/repos/standard/packages/
-
-[repo.unified]
-url=http://download.tizen.org/snapshots/tizen/unified/latest/repos/standard/packages/
-
diff --git a/.circleci/config.yml b/.circleci/config.yml
deleted file mode 100644 (file)
index 059c76a..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-version: 2
-jobs:
-  build:
-    machine: true
-    steps:
-      - run: no_proxy=165.213.149.164 curl -sL http://165.213.149.164/scripts/set_proxy_setting.sh | sudo -E bash -
-      - checkout
-      - run:
-          name: Install gbs
-          command: |
-            echo "deb [trusted=yes] http://download.tizen.org/tools/archive/19.03/Ubuntu_16.04/ /" | sudo tee /etc/apt/sources.list.d/tizen.list
-            sudo apt-get update
-            sudo apt-get install -y gbs
-            find /proc/sys/fs/binfmt_misc/ -not -name status -not -name register -type f -exec sh -c "echo -1 | sudo tee {}" \;
-      - run:
-          name: Build tizen gbs
-          command: |
-            gbs -c ~/project/.circleci/.gbs.conf build -A armv7l --clean 
-   
index 26ab5c2..b4fa365 100644 (file)
@@ -95,6 +95,12 @@ typedef enum {
  * @brief Max. application id length.
  * @since_tizen 5.5
  */
+#define UAM_APP_KEY_MAX_STRING_LEN 254
+
+/**
+ * @brief Max. app key length.
+ * @since_tizen 5.5
+ */
 #define UAM_APP_ID_MAX_STRING_LEN 100
 
 /**
@@ -169,6 +175,7 @@ typedef enum {
        UAM_EVENT_SERVICE_REGISTERED, /**< Service registered */
        UAM_EVENT_SERVICE_UNREGISTERED, /**< Service registered */
        UAM_EVENT_SENSOR_STATUS_CHANGED, /**< Sensor status changed */
+       UAM_EVENT_DAEMON_TERMINATED, /**< UAM Daemon terminated */
        UAM_EVENT_MAX, /**< Max. event number */
 } uam_event_e;
 
@@ -491,11 +498,12 @@ typedef enum {
  */
 typedef struct {
        char primary_key; /** Primary Key */
-       char device_icon; /** Device icon */
-       char secondary_key; /** Purpose */
+       char device_type; /** Device type */
+       char secondary_key; /** Secondary Key */
+       char ext_val1; /** Ext value */
+       char ext_val2; /** Ext value */
        int device_uid_len; /** Device uid length */
        char device_uid[UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN + 1]; /** DEVICE_UID */
-       char bt_mac[UAM_BT_MAC_ADDRESS_STRING_LEN]; /** BT MAC Address */
        char user_data[UAM_PAYLOAD_USER_DATA_MAX_LEN + 1]; /** User Data */
 } uam_ble_payload_s;
 
@@ -511,6 +519,7 @@ typedef struct {
        char device_id[UAM_DEVICE_ID_MAX_STRING_LEN]; /**< Device's uniquie ID */
        gboolean discriminant; /**< Determines whether to judge PRESENCE/ABSENCE */
        unsigned long long last_seen; /**< Latest seen time when device was discoverd */
+       int app_num; /**<*App Number*/
 } uam_device_info_s;
 
 /**
@@ -533,6 +542,7 @@ typedef struct {
 typedef struct {
        char account[UAM_USER_ACCOUNT_MAX_STRING_LEN]; /**< User account */
        char name[UAM_USER_NAME_MAX_STRING_LEN]; /**< User name */
+       int app_num; /**< App identity */
 } uam_user_info_s;
 
 /**
@@ -543,6 +553,7 @@ typedef struct {
        char name[UAM_SERVICE_MAX_STRING_LEN]; /**< Service name */
        unsigned int presence_threshold;
        unsigned int absence_threshold;
+       int app_num; /**< App identity */
 } uam_service_info_s;
 
 /**
index b9e7156..c6f5e6a 100644 (file)
@@ -32,6 +32,9 @@ extern "C" {
 #define UAM_SERVICE_DBUS "org.freedesktop.DBus"
 #define UAM_INTERFACE_DBUS "org.freedesktop.DBus"
 
+#define UAM_PRIVILEGE_PUBLIC "http://tizen.org/privilege/userawareness"
+#define UAM_PRIVILEGE_PLATFORM "http://tizen.org/privilege/userawareness.admin"
+
 #define FOREACH_REQUEST(REQUEST) \
        REQUEST(UAM_REQUEST_GET_AVAILABLE_SENSORS) \
        REQUEST(UAM_REQUEST_IS_SENSOR_READY) \
index d98d82b..d4eacda 100644 (file)
@@ -10,9 +10,6 @@
         <allow own="net.uamd"/>
         <allow send_destination="net.uamd"/>
     </policy>
-    <policy group="users">
-        <allow send_destination="net.uamd" send_path="/net/uamd" send_member="uam_request"/>
-    </policy>
     <policy context="default">
         <deny own="net.uamd"/>
         <deny send_destination="net.uamd"/>
index 37f5f04..492f992 100644 (file)
@@ -1,6 +1,6 @@
 Name:       ua-manager
 Summary:    User awareness manager
-Version:    0.14.0
+Version:    0.14.11
 Release:    1
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
@@ -21,6 +21,9 @@ BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  pkgconfig(sqlite3)
 BuildRequires:  pkgconfig(bundle)
 BuildRequires:  pkgconfig(aul)
+BuildRequires:  pkgconfig(cynara-client)
+BuildRequires:  pkgconfig(cynara-creds-gdbus)
+
 %if 0%{?gtests:1}
 BuildRequires:  pkgconfig(gmock)
 %endif
index 7f96ff5..056a12a 100644 (file)
@@ -108,6 +108,7 @@ do { \
        } while (0)
 
 GDBusConnection *_uam_get_gdbuam_conn(void);
+void _uam_close_gdbuam_conn();
 
 const char *_uam_request_to_str(unsigned int req);
 const char *_uam_event_to_str(unsigned int event);
index 7c061c1..e5b1644 100644 (file)
@@ -24,6 +24,8 @@ extern "C" {
 
 int _uam_register_event_handler(uam_event_cb event_cb, void *user_data);
 int _uam_unregister_event_handler(void);
+void _uam_register_name_owner_changed(void);
+void _uam_unregister_name_owner_changed(void);
 
 #ifdef __cplusplus
 }
index 8d2a20b..b9e4e7e 100644 (file)
@@ -26,23 +26,27 @@ typedef struct {
        void *user_data;
 } uam_callback_info_t;
 
+gboolean is_uam_active = FALSE;
+
 static uam_callback_info_t cb_info;
 
 UAM_EXPORT_API int _uam_init(uam_event_cb cb, void *user_data)
 {
        FUNC_ENTRY;
        int ret;
-
+       UAM_DBG("is_uam_active = %d", is_uam_active);
        ret = _uam_sync_enable(TRUE);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_sync_enable(TRUE) failed");
                return ret;
        }
+       is_uam_active = TRUE;
 
        ret = _uam_register_event_handler(cb, user_data);
-       if (UAM_ERROR_NONE == ret) {
+       if (UAM_ERROR_NONE == ret || UAM_ERROR_ALREADY_REGISTERED == ret) {
                cb_info.callback = cb;
                cb_info.user_data = user_data;
+               return UAM_ERROR_NONE;
        }
 
        FUNC_EXIT;
@@ -54,12 +58,15 @@ UAM_EXPORT_API int _uam_deinit(void)
        FUNC_ENTRY;
        int ret;
 
+       ret = _uam_unregister_event_handler();
+
+       if (UAM_ERROR_NONE != ret && UAM_ERROR_NOT_REGISTERED != ret)
+               return ret;
+
        ret = _uam_sync_enable(FALSE);
        if (UAM_ERROR_NONE != ret)
                return ret;
 
-       ret = _uam_unregister_event_handler();
-
        FUNC_EXIT;
        return ret;
 }
index e2282ec..6a96333 100644 (file)
 #include "ua-internal.h"
 
 #include "ua-common.h"
-
+#include "ua-request-sender.h"
+#include "ua-event-handler.h"
 static const char *request_string[] = {
        FOREACH_REQUEST(GENERATE_REQUEST_STRING)
 };
 
 static GDBusConnection *gdbuam_conn = NULL;
 
-static GDBusConnection *__uam_get_private_gdbuam_conn(void)
+static GDBusConnection *__uam_get_shared_gdbuam_conn(void)
 {
        FUNC_ENTRY;
        GError *error = NULL;
-       char *address;
-       GDBusConnection *private_conn = NULL;
-
-       address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
-       if (address == NULL) {
-               UAM_ERR("Failed to get bus address");
-               if (error) {
-                       UAM_ERR("Error: %s", error->message);
-                       g_clear_error(&error);
-               }
-               return NULL;
-       }
+       GDBusConnection *shared_conn = NULL;
 
-       private_conn = g_dbus_connection_new_for_address_sync(address,
-                       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
-                       G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
-                       NULL, /* GDBusAuthObserver */
-                       NULL,
-                       &error);
-       g_free(address);
-       if (!private_conn) {
+       shared_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (!shared_conn) {
                UAM_ERR("Unable to connect to dbus");
                if (error) {
                        UAM_ERR("Error: %s", error->message);
@@ -60,7 +44,7 @@ static GDBusConnection *__uam_get_private_gdbuam_conn(void)
        }
 
        FUNC_EXIT;
-       return private_conn;
+       return shared_conn;
 }
 
 GDBusConnection *_uam_get_gdbuam_conn(void)
@@ -69,17 +53,36 @@ GDBusConnection *_uam_get_gdbuam_conn(void)
 
        if (gdbuam_conn) {
                if (g_dbus_connection_is_closed(gdbuam_conn))
-                       gdbuam_conn = __uam_get_private_gdbuam_conn();
+                       gdbuam_conn = __uam_get_shared_gdbuam_conn();
 
                FUNC_EXIT;
                return gdbuam_conn;
        }
 
-       gdbuam_conn = __uam_get_private_gdbuam_conn();
+       gdbuam_conn = __uam_get_shared_gdbuam_conn();
        FUNC_EXIT;
        return gdbuam_conn;
 }
 
+void _uam_close_gdbuam_conn()
+{
+       FUNC_ENTRY;
+
+       _uam_unregister_name_owner_changed();
+
+       if (gdbuam_conn) {
+               _uam_gdbuam_deinit_proxy();
+
+               if (!g_dbus_connection_is_closed(gdbuam_conn))
+                       g_dbus_connection_flush_sync(gdbuam_conn, NULL, NULL);
+
+               g_object_unref(gdbuam_conn);
+               gdbuam_conn = NULL;
+       }
+
+       FUNC_EXIT;
+}
+
 const char *_uam_request_to_str(unsigned int req)
 {
        retv_if(UAM_REQUEST_MAX <= req, NULL);
index d79b59d..e035209 100644 (file)
@@ -18,6 +18,7 @@
 #include "ua-api.h"
 #include "ua-internal.h"
 #include "ua-common.h"
+#include "ua-event-handler.h"
 
 typedef struct {
        guint id;
@@ -27,6 +28,7 @@ typedef struct {
 
 static gboolean is_registered = FALSE;
 static uam_event_handler_data_t *event_handler_data = NULL;
+static guint owner_sig_id;
 
 static void __uam_copy_sensor_data(uam_sensor_info_s *sensor_info, int status,
        unsigned int bitmask, unsigned long long timestamp, int accuracy, int count, double *values)
@@ -41,9 +43,8 @@ static void __uam_copy_sensor_data(uam_sensor_info_s *sensor_info, int status,
        sensor_info->timestamp = timestamp;
        sensor_info->accuracy = accuracy;
        sensor_info->count = count;
-       for (int i = 0; i < UAM_SENSOR_MAX_VALUES; i++) {
+       for (int i = 0; i < UAM_SENSOR_MAX_VALUES; i++)
                sensor_info->values[i] = values[i];
-       }
 
        UAM_INFO("status [%d] sensor bitmask [%u] timestamp [%llu] accuracy [%d]" \
                "count [%d] value0 [%f] value1 [%f] value2 [%f] value3 [%f]",
@@ -374,6 +375,70 @@ static void __uam_event_handler(GDBusConnection *connection,
        FUNC_EXIT;
 }
 
+static void __uam_name_owner_changed(GDBusConnection *connection,
+               const gchar *sender_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *signal_name,
+               GVariant *parameters,
+               gpointer user_data)
+{
+       extern gboolean is_uam_active;
+       const char *name = NULL;
+       const char *old_owner = NULL;
+       const char *new_owner = NULL;
+       int event;
+       int result = UAM_ERROR_NONE;
+
+       g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
+       if (g_strcmp0(name, UAM_DBUS_NAME) == 0 &&
+                       (new_owner != NULL && *new_owner == '\0')) {
+               UAM_DBG("uam is terminated");
+
+               is_uam_active = FALSE;
+               event = UAM_EVENT_DAEMON_TERMINATED;
+
+               if (event_handler_data && event_handler_data->cb)
+                       __uam_send_event(event, result, NULL, event_handler_data->cb, NULL);
+               _uam_unregister_event_handler();
+
+               _uam_close_gdbuam_conn();
+
+       }
+}
+
+void _uam_register_name_owner_changed(void)
+{
+       GDBusConnection *conn;
+       conn = _uam_get_gdbuam_conn();
+
+       if (conn == NULL) {
+               UAM_ERR("unable to get the bus");
+               return;
+       }
+
+       if(owner_sig_id != 0)
+               return;
+
+       owner_sig_id = g_dbus_connection_signal_subscribe(conn,
+                       UAM_DBUS_NAME, UAM_SERVICE_DBUS,
+                       NAME_OWNER_CHANGED, NULL, UAM_DBUS_NAME, 0,
+                       __uam_name_owner_changed, NULL, NULL);
+}
+
+void _uam_unregister_name_owner_changed(void)
+{
+       GDBusConnection *conn;
+       conn = _uam_get_gdbuam_conn();
+       if (conn == NULL) {
+               UAM_ERR("Unable to get the BUS");
+               return;
+       }
+
+       g_dbus_connection_signal_unsubscribe(conn, owner_sig_id);
+       owner_sig_id = 0;
+}
+
 int _uam_register_event_handler(uam_event_cb event_cb, void *user_data)
 {
        FUNC_ENTRY;
@@ -399,6 +464,8 @@ int _uam_register_event_handler(uam_event_cb event_cb, void *user_data)
                return UAM_ERROR_OUT_OF_MEMORY;
        }
 
+       _uam_register_name_owner_changed();
+
        is_registered = TRUE;
 
        FUNC_EXIT;
@@ -413,6 +480,8 @@ int _uam_unregister_event_handler(void)
        retv_if(FALSE == is_registered, UAM_ERROR_NOT_REGISTERED);
        is_registered = FALSE;
 
+       _uam_unregister_name_owner_changed();
+
        retv_if(NULL == event_handler_data, UAM_ERROR_INTERNAL);
 
        conn = _uam_get_gdbuam_conn();
index ecedd25..a8c9b01 100644 (file)
@@ -139,21 +139,35 @@ int _uam_sync_enable(int enable)
                        g_variant_new("(i)", enable), NULL,
                        G_DBUS_CALL_FLAGS_NONE,
                        timeout, NULL, &error);
+
        if (variant) {
                g_variant_get(variant, "(i)", &result);
+
                UAM_DBG("%s status [0x%x] %s",
                        (enable) ? "enable" : "disable", result,
                        _uam_error_to_str(result));
+
                g_variant_unref(variant);
+
+               /* On deinit close gdbus conn */
+               if (!enable)
+                       _uam_close_gdbuam_conn();
+               else
+                       _uam_register_name_owner_changed();
+
        } else if (error) {
                UAM_ERR("D-Bus API failure: errCode[%x], message[%s]",
                                        error->code, error->message);
+
                if (strstr(error->message, "AccessDenied"))
                        result = UAM_ERROR_PERMISSION_DENIED;
                else
                        result = UAM_ERROR_INTERNAL;
+
                g_error_free(error);
-               return result ;
+
+               /* On dbus failure Close gdbus conn */
+               _uam_close_gdbuam_conn();
        }
 
        return result;
@@ -170,6 +184,7 @@ int _uam_sync_request(
 {
        FUNC_ENTRY;
        int result = UAM_ERROR_NONE;
+       extern gboolean is_uam_active;
        gint timeout;
        GDBusProxy *proxy = NULL;
        GVariant *ret = NULL;
@@ -185,6 +200,9 @@ int _uam_sync_request(
        UAM_INFO_C("Request function : %s (0x%x)",
                        _uam_request_to_str(req_func), req_func);
 
+       if (is_uam_active == FALSE)
+               return UAM_ERROR_INTERNAL;
+
        proxy = __uam_gdbuam_get_proxy();
        retv_if(NULL == proxy, UAM_ERROR_INTERNAL);
 
@@ -286,7 +304,6 @@ static void __uam_async_request_cb(
                        out_param = g_array_new(TRUE, TRUE, sizeof(gchar));
                        __uam_fill_garray_from_variant(param1, out_param);
                        g_variant_unref(param1);
-                       g_array_unref(out_param);
                }
 
                /* Callback should be invoked only in case of failure */
@@ -333,6 +350,7 @@ int _uam_async_request(
 {
        FUNC_ENTRY;
        int result = UAM_ERROR_NONE;
+       extern gboolean is_uam_active;
        uam_req_info_t *req_info;
        gint timeout = -1;
        GDBusProxy *proxy;
@@ -345,6 +363,9 @@ int _uam_async_request(
        UAM_INFO_C("Request function : %s (0x%x)",
                        _uam_request_to_str(req_func), req_func);
 
+       if (is_uam_active == FALSE)
+               return UAM_ERROR_INTERNAL;
+
        proxy = __uam_gdbuam_get_proxy();
        retv_if(NULL == proxy, UAM_ERROR_INTERNAL);
 
index 9b3b6dd..8ae9d3d 100644 (file)
@@ -13,16 +13,14 @@ SET(SRCS
        src/ua-manager-db.c
        src/ua-manager-device-db.c
        src/ua-manager-user-db.c
+       src/ua-manager-app-db.c
        src/ua-manager-service-db.c
        src/ua-manager-device-service-db.c
        src/ua-manager-payload-db.c
        src/ua-manager-adv-db.c
        src/pm/ua-plugin-manager.c
        src/pm/ua-pm-util.c
-       src/pm/ua-ble-plugin-handler.c
-       src/pm/ua-wifi-plugin-handler.c
-       src/pm/ua-light-plugin-handler.c
-       src/pm/ua-motion-plugin-handler.c
+       src/pm/ua-plugin-handler.c
        src/pm/ua-cloud-plugin-handler.c
        src/pm/ua-vendor-plugin-manager.c
        src/pm/ua-vendor-plugin-handler.c
@@ -45,6 +43,8 @@ SET(PKG_MODULES
        sqlite3
        bundle
        aul
+       cynara-client
+       cynara-creds-gdbus
        )
 
 INCLUDE(FindPkgConfig)
index c3984dc..bba700e 100644 (file)
@@ -22,7 +22,9 @@ CREATE TABLE IF NOT EXISTS userdata (
   name TEXT,
   user_id INTEGER PRIMARY KEY AUTOINCREMENT,
   account TEXT,
-  UNIQUE (account)
+  app_num INTEGER,
+  FOREIGN KEY(app_num) REFERENCES apps(app_num),
+  UNIQUE (account, app_num)
 );
 
 CREATE TABLE IF NOT EXISTS devices (
@@ -36,8 +38,10 @@ CREATE TABLE IF NOT EXISTS devices (
   presence_state INTEGER,
   os_type INTEGER,
   discriminant INTEGER,
+  app_num INTEGER,
   FOREIGN KEY(user_id) REFERENCES userdata(user_id),
-  UNIQUE (device_id, tech_type)
+  FOREIGN KEY(app_num) REFERENCES apps(app_num),
+  UNIQUE (device_id, tech_type, app_num)
 );
 
 CREATE TABLE IF NOT EXISTS payloads (
@@ -49,8 +53,10 @@ CREATE TABLE IF NOT EXISTS payloads (
   device_icon TEXT,
   user_data TEXT,
   device_number INTEGER,
+  app_num INTEGER,
   FOREIGN KEY(device_number) REFERENCES devices(device_number),
-  UNIQUE (primary_key, secondary_key, device_uid)
+  FOREIGN KEY(app_num) REFERENCES apps(app_num),
+  UNIQUE (primary_key, secondary_key, device_uid, app_num)
 );
 
 CREATE TABLE IF NOT EXISTS services (
@@ -59,7 +65,9 @@ CREATE TABLE IF NOT EXISTS services (
   cycle INTEGER,
   presence_threshold INTEGER,
   absence_threshold INTEGER,
-  UNIQUE (service_name)
+  app_num INTEGER,
+  FOREIGN KEY(app_num) REFERENCES apps(app_num),
+  UNIQUE (service_name, app_num)
 );
 
 CREATE TABLE IF NOT EXISTS device_services (
@@ -68,15 +76,26 @@ CREATE TABLE IF NOT EXISTS device_services (
   payload_number INTEGER,
   discriminant INTEGER,
   last_seen LONG,
+  app_num INTEGER,
   FOREIGN KEY(device_number) REFERENCES devices(device_number),
   FOREIGN KEY(service_number) REFERENCES services(service_number),
   FOREIGN KEY(payload_number) REFERENCES payloads(payload_number),
-  PRIMARY KEY(device_number, service_number)
+  FOREIGN KEY(app_num) REFERENCES apps(app_num),
+  PRIMARY KEY(device_number, service_number),
+  UNIQUE (device_number, service_number, app_num)
 );
 
 CREATE TABLE IF NOT EXISTS ibeacon_adv (
   ibeacon_id INTEGER PRIMARY KEY AUTOINCREMENT,
   ibeacon_adv TEXT,
   adv_len INTEGER,
-  UNIQUE (ibeacon_adv)
+  app_num INTEGER,
+  FOREIGN KEY(app_num) REFERENCES apps(app_num),
+  UNIQUE (ibeacon_adv, app_num)
+);
+
+CREATE TABLE IF NOT EXISTS apps (
+  app_num INTEGER PRIMARY KEY AUTOINCREMENT,
+  app_key TEXT,
+  UNIQUE (app_key)
 );
index 9da4270..612ce34 100644 (file)
@@ -54,9 +54,9 @@ extern "C" {
 #define FUNC_ENTRY UAM_DBG("+")
 #define FUNC_EXIT UAM_DBG("-")
 
-#define SENSOR_PLUGIN_DIR tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "network/ua/lib/sensor")
-#define VENDOR_PLUGIN_DIR tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "network/ua/lib/vendor")
-#define CLOUD_PLUGIN_DIR tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "network/ua/lib/cloud")
+#define SENSOR_PLUGIN_DIR tzplatform_mkpath(TZ_SYS_RO_SHARE, "network/ua/lib/sensor")
+#define VENDOR_PLUGIN_DIR tzplatform_mkpath(TZ_SYS_RO_SHARE, "network/ua/lib/vendor")
+#define CLOUD_PLUGIN_DIR tzplatform_mkpath(TZ_SYS_RO_SHARE, "network/ua/lib/cloud")
 
 #define UAM_VERSION "0.1"
 
@@ -76,6 +76,12 @@ do { \
        } \
 } while (0)
 
+#define free_n_null(ptr) \
+do { \
+               g_free(*ptr); \
+               *ptr = NULL; \
+} while (0)
+
 typedef struct {
        int result;
        int function;
@@ -85,12 +91,22 @@ typedef struct {
        gpointer data;
 } uam_request_context_t;
 
+typedef struct {
+       unsigned int app_num;
+       char *app_key;
+       char *sender;
+} uam_sender_app_t;
+
 int _uam_manager_register(void);
 
 void _uam_manager_unregister(void);
 
 GSList *_uam_manager_get_request_list(void);
 
+GSList **_uam_manager_get_sender_apps(void);
+
+void _uam_get_sender_from_app_num(const int app_num, char **dest);
+
 void _uam_manager_remove_req_ctxt_from_list(
                uam_request_context_t *req_info);
 
@@ -132,6 +148,8 @@ size_t _uam_get_file_size(const char* filename);
 
 unsigned long long _uam_get_timestamp(void);
 
+void _uam_manager_terminate(void);
+
 #ifdef __cplusplus
 }
 #endif
index 077f7a0..5fc3d10 100644 (file)
@@ -52,18 +52,21 @@ typedef struct {
        int absence_threshold; /**< ABSENCE threshold */
        GSList *monitors; /**< Monitor handle list */
        GSList *dev_techs; /**< Device list */
+       int app_num; /** Application Primary Key */
 } uam_db_service_info_t;
 
 typedef struct {
        char primary_key; /** Primary key */
-       char device_icon; /** Device icon */
-       char secondary_key; /** Purpose */
+       char device_type; /** Device type */
+       char secondary_key; /** Secondary key */
+       char ext_val1; /** Extension value 1 */
+       char ext_val2; /** Extension value 2 */
        int device_uid_len; /** Device uid length */
        char *device_uid; /** DEVICE_UID */
        char *user_data; /** User data */
-       char *bt_mac; /** BT MAC Address */
        char *device_id; /**< Device ID */
        uam_tech_type_e tech_type; /**< Connectivity type */
+       int app_num; /** Application Primary Key */
 } uam_db_payload_info_t;
 
 typedef struct {
@@ -73,6 +76,7 @@ typedef struct {
        int discriminant; /**< Service dependent discriminant */
        unsigned long long last_seen; /**< Service dependent last seen time */
        uam_db_payload_info_t *payload; /**device service specific payload info */
+       int app_num; /** Application Primary Key */
 } uam_svc_dev_info_t;
 
 typedef struct {
@@ -89,6 +93,7 @@ typedef struct {
        struct uam_db_device_info *device; /**< Device info which stored in the DB */
        GSList *svc_list; /**< Service list */
        GSList *svc_dev_list; /**< Service device list */
+       int os; /**< OS type */
 } uam_db_tech_info_t;
 
 typedef struct uam_db_device_info {
@@ -98,6 +103,7 @@ typedef struct uam_db_device_info {
        unsigned int supported_techs; /**< bitmask to check how many connectivities are supported */
        struct uam_db_user_info *user; /**< Owner infomation for these devices */
        gboolean discriminant; /**< Determines whether to judge PRESENCE/ABSENCE */
+       int app_num; /**< Identity of respective app */
 } uam_db_device_info_t;
 
 typedef struct uam_db_user_info {
@@ -106,51 +112,67 @@ typedef struct uam_db_user_info {
        char *account; /**< Account */
        GSList *devices; /**< Device list */
        unsigned long long last_seen; /**< Last seen time */
+       int app_num; /**< Identity of respective app */
 } uam_db_user_info_t;
 
+typedef struct uam_db_app_info {
+       int app_num; /**< App ID */
+       char *app_key; /**< App Key */
+} uam_db_app_info_t;
 
 typedef enum {
        UAM_ACTIVE_DEVICE_FOUND = 0x01, /**< Found devices using ARPing */
        UAM_ACTIVE_SCAN_COMPLETED = 0x02 /**< Active ARPing done */
 } uam_active_scan_event_e;
 
-int _uam_core_add_user(int *user_id, const char *account, const char *name);
+int _uam_core_add_user(int *user_id, const char *account, const char *name,
+       const int app_num);
 
-int _uam_core_remove_user(const char *account);
+int _uam_core_remove_user(const char *account, const int app_num);
 
 int _uam_core_update_user(uam_user_info_s *user);
 
+int _uam_core_add_app(int *app_num, const char *app_key);
+
+int _uam_core_remove_app(const char *app_key);
+
+int _uam_core_update_app(uam_app_info_s *app);
+
 int _uam_core_add_device(const char *account,
-       const uam_device_info_s *dev_info);
+       uam_device_info_s *dev_info);
 
 int _uam_core_remove_device(const char *account,
-       const uam_device_info_s *dev_info);
+       const uam_device_info_s *dev_info, const int app_num);
 
 int _uam_core_remove_device_by_device_id(const char *device_id,
-       int tech_type);
+       int tech_type, const int app_num);
 
-int _uam_core_remove_device_by_mac(const char *mac);
+int _uam_core_remove_device_by_mac(const char *mac, const int app_num);
 
-int _uam_core_get_default_user(uam_user_info_s *user_info);
+int _uam_core_get_default_user(uam_user_info_s *user_info, const int app_num);
 
-int _uam_core_get_users(int *count, uam_user_info_s **user_list);
+int _uam_core_get_users(int *count, uam_user_info_s **user_list,
+       const int app_num);
 
-int _uam_core_get_user_by_account(const char *account, uam_user_info_s *user);
+int _uam_core_get_user_by_account(const char *account, uam_user_info_s *user,
+       const int app_num);
 
 int _uam_core_get_devcie_by_device_id(
-               const char *device_id, int tech_type, uam_device_info_s *device);
+               const char *device_id, int tech_type, uam_device_info_s *device, const int app_num);
 
-int _uam_core_get_devcie_by_mac(const char *mac, uam_device_info_s *device);
+int _uam_core_get_device_by_mac(const char *mac, uam_device_info_s *device,
+       const int app_num);
 
 int _uam_core_get_user_by_device_id(const char *device_id,
-       uam_user_info_s *user);
+               uam_user_info_s *user, const int app_num);
 
-int _uam_core_get_user_by_mac(const char *mac, uam_user_info_s *user);
+int _uam_core_get_user_by_mac(const char *mac, uam_user_info_s *user,
+       const int app_num);
 
-int _uam_core_get_devices(int *count, uam_device_info_s **device_list);
+int _uam_core_get_devices(int *count, uam_device_info_s **device_list, const int app_num);
 
 int _uam_core_get_user_devices(const char *account,
-               int *count, uam_device_info_s **device_list);
+               int *count, uam_device_info_s **device_list, const int app_num);
 
 int _uam_core_is_device_added(uam_device_info_s *dev, gboolean *is_added);
 
@@ -186,7 +208,7 @@ void _uam_core_deinit(void);
 void _uam_core_handle_sensor_ready(unsigned int sensor, gboolean is_ready);
 
 int _uam_core_handle_device_added(int status,
-               int user_id, const uam_device_info_s *dev_info);
+               int user_id, uam_device_info_s *dev_info);
 
 int _uam_core_handle_presence_detected(unsigned int sensor,
                int user_id, void *sensor_info, uam_ble_payload_s *payload);
@@ -212,60 +234,63 @@ int _uam_core_stop_active_device_scan(char *sender, unsigned int sensors);
 void _uam_core_handle_active_device(uam_active_scan_event_e event,
        unsigned int sensor, const uam_device_info_s *dev_info);
 
-int _uam_core_get_default_service(uam_service_info_s *service_info);
+int _uam_core_get_default_service(
+       uam_service_info_s *service_info, const int app_num);
 
-int _uam_core_register_service(uam_service_info_s *svc);
+int _uam_core_register_service(uam_service_info_s *svc, const int app_num);
 
 int _uam_core_update_service(uam_service_info_s *svc);
 
-int _uam_core_unregister_service(const char *svc_name);
+int _uam_core_unregister_service(const char *svc_name, const int app_num);
 
 int _uam_core_get_service_devices(const char *svc_name, int *count,
-       uam_device_info_s **device_list);
+       uam_device_info_s **device_list, const int app_num);
 
 int _uam_core_get_service_users(const char *svc_name, int *count,
-       uam_user_info_s **user_list);
+       uam_user_info_s **user_list, const int app_num);
 
-int _uam_core_get_services(int *count, uam_service_info_s **service_list);
+int _uam_core_get_services(int *count, uam_service_info_s **service_list, const int app_num);
 
-int _uam_core_service_add_user(const char *service_name, const char *account);
+int _uam_core_service_add_user(const char *service_name, const char *account, const int app_num);
 
-int _uam_core_service_remove_user(const char *service, const char *account);
+int _uam_core_service_remove_user(const char *service, const char *account, const int app_num);
 
 int _uam_core_service_add_device(const char *service_name,
-       const char *device_id, int tech_type);
+       const char *device_id, int tech_type, const int app_num);
 
 int _uam_core_service_remove_device(const char *service_name,
-       const char *device_id, int tech_type);
+       const char *device_id, int tech_type, const int app_num);
 
 int _uam_core_service_set_device_discriminant(const char *service_name,
-       const char *device_id, int tech_type, gboolean discriminant);
+       const char *device_id, int tech_type, gboolean discriminant, const int app_num);
 
 int _uam_core_service_get_device_discriminant(const char *service_name,
-       const char *device_id, int tech_type, gboolean *discriminant);
+       const char *device_id, int tech_type, gboolean *discriminant, const int app_num);
 
 int _uam_core_service_get_device_last_seen(const char *svc_name,
-       const char *device_id, int tech_type, unsigned long long *last_seen);
+       const char *device_id, int tech_type, unsigned long long *last_seen, const int app_num);
 
 int _uam_core_set_service_detection_cycle(const char *svc_name,
-       unsigned int cycle);
+       unsigned int cycle, const int app_num);
 
 int _uam_core_get_service_detection_cycle(const char *svc_name,
-       unsigned int *cycle);
+       unsigned int *cycle, const int app_num);
 
-int _uam_core_update_device(const uam_device_info_s *a_device);
+int _uam_core_update_device(uam_device_info_s *a_device);
 
-int _uam_core_add_ibeacon_adv(unsigned int adv_len, const char *iadv);
+int _uam_core_add_ibeacon_adv(unsigned int adv_len, const char *iadv, const int app_num);
 
 void _uam_core_handle_status_changed(unsigned int sensor, void *info);
 
 int _uam_core_add_payload(uam_ble_payload_s *payload, const char *device_id,
-       int tech_type);
+       int tech_type, const int app_num);
 
 int _uam_core_service_add_payload(uam_ble_payload_s *payload,
-               const char *svc_name);
+               const char *svc_name, const int app_num);
+
+int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list, const int app_num);
 
-int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list);
+void _uam_core_get_app_num_from_app_key(const char* app_key, int *app_num);
 
 #ifdef __cplusplus
 }
index dec1760..9c9c591 100644 (file)
@@ -30,7 +30,6 @@ extern "C" {
 #ifndef SQLITE_BUSY_TIMEOUT
 #define SQLITE_BUSY_TIMEOUT 500000
 #endif
-
 #define SQLITE_MAX_RETRY 10
 
 /* Helper macros */
@@ -87,6 +86,7 @@ typedef struct db_user_info {
        int user_id;
        char name[UAM_USER_NAME_MAX_STRING_LEN];
        char account[UAM_USER_ACCOUNT_MAX_STRING_LEN];
+       int app_num;
 } db_user_info_t;
 
 /* db init/deinit */
@@ -94,25 +94,54 @@ int _uam_user_db_initialize(void);
 int _uam_user_db_deinitialize(void);
 
 /* select operations */
-GSList *_uam_db_get_all_users(void);
+GSList *_uam_db_get_all_users(const int app_num);
 int _uam_db_get_user(int user_id, db_user_info_t *info);
 
 /* delete operations */
 int _uam_db_delete_by_user_id(int user_id);
-int _uam_user_db_clear(void);
+int _uam_user_db_clear(const int app_num);
 
 /* insert */
-int _uam_db_insert_user_info(int* user_id, const char *name, const char *account);
+int _uam_db_insert_user_info(int* user_id, const char *name, const char *account, const int app_num);
 
 /* update */
 int _uam_db_update_user_info(uam_db_user_info_t *user);
 
+/* APP QUERIES */
+
+typedef struct db_app_info {
+       int app_num;
+       char app_key[UAM_APP_KEY_MAX_STRING_LEN];
+} db_app_info_t;
+
+/* db init/deinit */
+int _uam_app_db_initialize(void);
+int _uam_app_db_deinitialize(void);
+
+/* select operations */
+GSList *_uam_db_get_all_apps(void);
+int _uam_db_get_app(int app_num, db_app_info_t *info);
+
+/* delete operations */
+int _uam_db_delete_by_app_num(int app_num);
+int _uam_app_db_clear(void);
+
+/* insert */
+int _uam_db_insert_app_info(unsigned int* app_num, const char *app_key);
+
+/* update */
+int _uam_db_update_app_info(uam_db_app_info_t *app);
+
+
+
 /* DEVICE QUERIES */
+
 typedef struct {
        int user_id;
        uam_device_info_s dev_info;
        int presence_state;
        unsigned long long last_seen;
+       int app_num;
 } db_device_info_t;
 
 /* db init/deinit */
@@ -120,33 +149,33 @@ int _uam_device_db_initialize(void);
 int _uam_device_db_deinitialize(void);
 
 /* select operations */
-GSList *_uam_device_db_get_all_devices(void);
+GSList *_uam_device_db_get_all_devices(const int app_num);
 int _uam_device_db_get_device(
-       char *device_id, int tech_type, char *address, db_device_info_t *info);
+       char *device_id, int tech_type, char *address, db_device_info_t *info, const int app_num);
 int _uam_db_get_device_number(
-       const char *device_id, int tech_type, const char *address, int *device_number);
+       const char *device_id, int tech_type, const char *address, int *device_number, const int app_num);
 
 /* delete operations */
-int _uam_device_db_clear(void);
+int _uam_device_db_clear(const int app_num);
 int _uam_device_db_delete_device_info(
-       const char *device_id, int tech_type, const char *address);
+       const char *device_id, int tech_type, const char *address, const int app_num);
 
 /* insert */
 int _uam_device_db_insert_device_info(
        int user_id, const uam_device_info_s *dev_info, int presence_state,
-       unsigned long long last_seen);
+       unsigned long long last_seen, const int app_num);
 
 /* update */
 int _uam_device_db_update_device_last_seen(
-       char *device_id, int tech_type, char *address, unsigned long long last_seen);
+       char *device_id, int tech_type, char *address, unsigned long long last_seen, const int app_num);
 int _uam_device_db_update_device_presence(
-       char *device_id, int tech_type, char *address, int presence_state);
+       char *device_id, int tech_type, char *address, int presence_state, const int app_num);
 int _uam_device_db_update_device_ip_address(
-       char *device_id, int tech_type, char *ip_address);
+       char *device_id, int tech_type, char *ip_address, const int app_num);
 int _uam_device_db_update_device_mac_address(
-       char *device_id, int tech_type, char *address);
+       char *device_id, int tech_type, char *address, const int app_num);
 int _uam_device_db_update_device(char *device_id, int tech_type,
-       char *address, char *ip, char os_type, char discriminant);
+       char *address, char *ip, char os_type, char discriminant, const int app_num);
 
 /* SERVICE QUERIES */
 
@@ -156,6 +185,7 @@ typedef struct {
        int cycle;
        int presence_threshold;
        int absence_threshold;
+       int app_num;
 } db_service_info_t;
 
 /* db init/deinit */
@@ -163,20 +193,20 @@ int _uam_service_db_initialize(void);
 int _uam_service_db_deinitialize(void);
 
 /* select operations */
-int _uam_db_get_service_info(const char *service_name, db_service_info_t *info);
-GSList *_uam_service_db_get_all_services(void);
+int _uam_db_get_service_info(const char *service_name, db_service_info_t *info, const int app_num);
+GSList *_uam_service_db_get_all_services(const int app_num);
 
 /* delete operations */
-int _uam_db_delete_service_info(const char *service_name);
-int _uam_service_db_clear(void);
+int _uam_db_delete_service_info(const char *service_name, const int app_num);
+int _uam_service_db_clear(const int app_num);
 
 /* insert */
 int _uam_db_insert_service_info(
-       int *service_number, uam_service_info_s *svc, int cycle);
+       int *service_number, uam_service_info_s *svc, int cycle, const int app_num);
 
 /* update */
-int _uam_db_update_service_cycle(const char *service_name, int cycle);
-int _uam_db_update_service_info(uam_db_service_info_t *service);
+int _uam_db_update_service_cycle(const char *service_name, int cycle, const int app_num);
+int _uam_db_update_service_info(uam_db_service_info_t *service, const int app_num);
 
 
 /* DEVICE_SERVICE QUERIES */
@@ -188,6 +218,7 @@ typedef struct {
        int discriminant; /**< Discriminant for each service */
        unsigned long long last_seen; /**< Last seen time for each device of each service */
        uam_ble_payload_s payload_info; /** Payload info for this device service */
+       int app_num; /** Application Primary Key */
 } db_svc_dev_info_t;
 
 /* db init/deinit */
@@ -195,40 +226,40 @@ int _uam_device_service_db_initialize(void);
 int _uam_device_service_db_deinitialize(void);
 
 /* select operations */
-GSList *_uam_db_get_service(int device_number);
+GSList *_uam_db_get_service(int device_number, const int app_num);
 GSList *_uam_db_get_device_services(
-       const char *device_id, int tech_type, const char *address);
+       const char *device_id, int tech_type, const char *address, const int app_num);
 int _uam_db_get_device_services_count(const char *device_id, int tech_type,
-       const char *address, int *count);
-GSList *_uam_db_get_service_devices_info();
-GSList *_uam_db_get_service_devices_payloads_info();
+       const char *address, const int app_num, int *count);
+GSList *_uam_db_get_service_devices_info(const int app_num);
+GSList *_uam_db_get_service_devices_payloads_info(const int app_num);
 
 /* delete operations */
-int _uam_device_service_db_clear(void);
-int _uam_db_delete_service_number(int service_number);
+int _uam_device_service_db_clear(const int app_num);
+int _uam_db_delete_service_number(int service_number, const int app_num);
 int _uam_db_delete_device_service_number(
-       int device_number, int service_number);
-int _uam_db_delete_device_service(const char *service_name);
+       int device_number, int service_number, const int app_num);
+int _uam_db_delete_device_service(const char *service_name, const int app_num);
 int _uam_db_delete_device_service_info(const char *device_id, int tech_type,
-               const char *address, const char *service_name);
+               const char *address, const char *service_name, const int app_num);
 int _uam_db_delete_device_service_mapping(const char *device_id, int tech_type,
-               const char *address);
+               const char *address, const int app_num);
 
 /* insert */
 int _uam_db_insert_service(int device_number, int service_number,
-       gboolean discriminant, unsigned long long last_seen);
+       gboolean discriminant, unsigned long long last_seen, const int app_num);
 int _uam_db_insert_device_service_info(const char *device_id, int tech_type,
        const char *address, const char *service_name, int cycle, gboolean discriminant,
-       unsigned long long last_seen);
+       unsigned long long last_seen, const int app_num);
 
 /* update */
 int _uam_db_update_device_service_discriminant(const char *device_id,
-       int tech_type, const char *address, const char *service_name, gboolean discriminant);
+       int tech_type, const char *address, const char *service_name, gboolean discriminant, const int app_num);
 int _uam_db_update_device_service_last_seen(const char *device_id,
        int tech_type, const char *address, const char *service_name,
-       unsigned long long last_seen);
+       unsigned long long last_seen, const int app_num);
 int _uam_db_update_device_service_payload_info(
-       uam_ble_payload_s *payload, char *service_name);
+       uam_ble_payload_s *payload, char *service_name, const int app_num);
 
 /* IBEACON_ADV QUERIES */
 
@@ -236,6 +267,7 @@ typedef struct {
        int ibeacon_id;
        int adv_len;
        char iadv[UAM_IBEACON_ADV_MAX_LEN + 1];
+       int app_num;
 } db_adv_info_t;
 
 /* db init/deinit */
@@ -243,13 +275,13 @@ int _uam_adv_db_initialize(void);
 int _uam_adv_db_deinitialize(void);
 
 /* insert */
-int _uam_db_insert_adv_info(unsigned int adv_len, const char *ibeacon_adv);
+int _uam_db_insert_adv_info(unsigned int adv_len, const char *ibeacon_adv, const int app_num);
 
 /* select operations */
-GSList *_uam_db_get_all_advs(void);
+GSList *_uam_db_get_all_advs(const int app_num);
 
 /* delete operations */
-int _uam_adv_db_clear(void);
+int _uam_adv_db_clear(const int app_num);
 
 
 /* PAYLOAD QUERIES */
@@ -257,6 +289,7 @@ typedef struct {
        uam_ble_payload_s payload_info;
        char device_id[UAM_DEVICE_ID_MAX_STRING_LEN];
        uam_tech_type_e type;
+       int app_num;
 } db_payload_info_t;
 
 /* db init/deinit */
@@ -266,22 +299,22 @@ int _uam_payload_db_deinitialize(void);
 /* insert */
 int _uam_db_insert_payload_info(
        const char *device_id, int tech_type,
-       const char *address, uam_ble_payload_s *payload);
+       const char *address, uam_ble_payload_s *payload, const int app_num);
 
 /* delete operations */
-int _uam_payload_db_clear(void);
+int _uam_payload_db_clear(const int app_num);
 
 /* select operations */
-GSList *_uam_db_get_all_payloads(void);
+GSList *_uam_db_get_all_payloads(const int app_num);
 int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
-               int *device_number);
+               int *device_number, const int app_num);
 int _uam_db_get_payload_number(uam_ble_payload_s *payload,
-               int *payload_number);
+               int *payload_number, const int app_num);
 
 /* update */
 int _uam_db_update_payload_info(
        const char *device_id, int tech_type,
-       const char *address, uam_ble_payload_s *payload);
+       const char *address, uam_ble_payload_s *payload, const int app_num);
 
 #ifdef __cplusplus
 }
index 6ee3b3a..26126fd 100644 (file)
@@ -38,6 +38,10 @@ void _uam_pm_deinit(void);
 
 unsigned int _uam_pm_get_avaliable_sensors(void);
 
+unsigned int _uam_pm_get_user_sensors(void);
+
+unsigned int _uam_pm_get_env_sensors(void);
+
 gboolean _uam_pm_is_sensor_ready(unsigned int sensor_bitmask);
 
 int _uam_pm_register_device(int user_id, const uam_device_info_s *dev);
@@ -48,7 +52,7 @@ int _uam_pm_start_detection(unsigned int detection_mode, unsigned int sensor_bit
 
 int _uam_pm_stop_detection(unsigned int detection_mode, unsigned int sensor_bitmask);
 
-int _uam_pm_set_registered_devices(GSList *devices);
+int _uam_pm_set_registered_devices(GSList *devices, unsigned int bitmask);
 
 int _uam_pm_set_low_power_mode(unsigned int sensors, gboolean mode);
 
diff --git a/ua-daemon/src/pm/ua-ble-plugin-handler.c b/ua-daemon/src/pm/ua-ble-plugin-handler.c
deleted file mode 100644 (file)
index aab1afe..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License")
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <ua-plugin.h>
-#include <ua-manager-core.h>
-#include <ua-manager-database.h>
-#include <ua-plugin-manager.h>
-
-#include "ua-pm-util.h"
-
-static void ble_state_changed_callback(int state)
-{
-       FUNC_ENTRY;
-       gboolean is_ready = FALSE;
-
-       if (UAS_STATE_READY == state)
-               is_ready = TRUE;
-
-       _uam_core_handle_sensor_ready(UAM_SENSOR_BITMASK_BLE, is_ready);
-
-       FUNC_EXIT;
-}
-
-static void ble_lpm_detection_callback(uas_detection_type_e type, void *sensor_info)
-{
-       FUNC_ENTRY;
-       int ret = UAM_ERROR_NONE;
-
-       /* Currently, in lpm mode only Presence is detected */
-       if (UAS_PRESENCE == type) {
-               UAM_DBG("Presence detected");
-               ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_BLE,
-                               -1, NULL, NULL);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_WARN("_uam_core_handle_presence_detected failed");
-       } else {
-               UAM_WARN("Unlikely Absence detected");
-       }
-
-       FUNC_EXIT;
-}
-
-static void ble_detection_state_changed_cb(int state)
-{
-       FUNC_ENTRY;
-
-       if (state)
-               _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_BLE);
-       else
-               _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_BLE);
-
-       FUNC_EXIT;
-}
-
-static void ble_device_detection_callback(uas_detection_type_e type, uas_device_info_t *device)
-{
-       FUNC_ENTRY;
-       uam_device_info_s *dev_info;
-       uam_ble_payload_s *payload = NULL;
-       int ret = UAM_ERROR_NONE;
-
-       ret_if(NULL == device);
-
-       dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
-       ret_if(NULL == dev_info);
-
-       if (device->payload) {
-               payload = g_new0(uam_ble_payload_s, 1);
-               _pm_util_uas_payload_to_uam_payload(payload, device->payload);
-       }
-
-       if (UAS_PRESENCE == type) {
-               UAM_DBG("BLE Presence detected for %s", dev_info->device_id);
-               ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_BLE,
-                               device->user_id, dev_info, payload);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_WARN("_uam_core_handle_presence_detected failed");
-       } else {
-               UAM_DBG("Absence detected for %s", dev_info->device_id);
-               _uam_core_handle_absence_detected(UAM_SENSOR_BITMASK_BLE, device->user_id, dev_info);
-       }
-
-       g_free(dev_info);
-       g_free(payload);
-       FUNC_EXIT;
-}
-
-static void ble_device_added_callback(int status, uas_device_info_t *device)
-{
-       FUNC_ENTRY;
-       uam_device_info_s *dev_info;
-
-       ret_if(NULL == device);
-
-       dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
-       ret_if(NULL == dev_info);
-
-       dev_info->type = UAM_TECH_TYPE_BLE;
-       UAM_INFO("Status: %s", _pm_util_uas_status_to_str(status));
-       if (UAS_STATUS_SUCCESS != status)
-               status = UAM_ERROR_INTERNAL;
-       else
-               status = UAM_ERROR_NONE;
-
-       _uam_core_handle_device_added(status, device->user_id, dev_info);
-       g_free(dev_info);
-       FUNC_EXIT;
-}
-
-uas_callbacks_t ble_cbs = {
-       .state_changed_cb = ble_state_changed_callback,
-       .detection_state_cb = ble_detection_state_changed_cb,
-       .detection_status_cb = NULL,
-       .detected_cb = ble_lpm_detection_callback,
-       .device_detected_cb = ble_device_detection_callback,
-       .device_added_cb = ble_device_added_callback,
-       .device_active_scan_cb = NULL,
-};
index 37282a5..565da71 100644 (file)
@@ -65,14 +65,23 @@ static void __uam_to_uap_device_info(uap_device_info_t *dest,
        FUNC_EXIT;
 }
 
-static int add_user(const char *account, const char *name)
+static int add_user(const char *account, const char *name, const char *app_id)
 {
        FUNC_ENTRY;
        int user_id;
+       int app_num = -1;
        retv_if(NULL == account, UAP_STATUS_INVALID_PARAMS);
        retv_if(NULL == name, UAP_STATUS_INVALID_PARAMS);
 
-       if (UAM_ERROR_NONE != _uam_core_add_user(&user_id, account, name)) {
+       _uam_core_get_app_num_from_app_key(app_id, &app_num);
+
+       if (app_num == -1) {
+               UAM_ERR("add_user failed");
+               return UAP_STATUS_FAIL;
+       }
+
+       //TODO: How to decide app_num here
+       if (UAM_ERROR_NONE != _uam_core_add_user(&user_id, account, name, app_num)) {
                UAM_ERR("_uam_core_add_user() failed");
                return UAP_STATUS_FAIL;
        }
@@ -81,12 +90,21 @@ static int add_user(const char *account, const char *name)
        return UAP_STATUS_SUCCESS;
 }
 
-static int remove_user(const char *account)
+static int remove_user(const char *account, const char *app_id)
 {
        FUNC_ENTRY;
+       int app_num = -1;
        retv_if(NULL == account, UAP_STATUS_INVALID_PARAMS);
 
-       if (UAM_ERROR_NONE != _uam_core_remove_user(account)) {
+       _uam_core_get_app_num_from_app_key(app_id, &app_num);
+
+       if (app_num == -1) {
+               UAM_ERR("remove_user failed");
+               return UAP_STATUS_FAIL;
+       }
+
+       //TODO: How to decide app_num here
+       if (UAM_ERROR_NONE != _uam_core_remove_user(account, app_num)) {
                UAM_ERR("_uam_core_remove_user() failed");
                return UAP_STATUS_FAIL;
        }
@@ -95,15 +113,24 @@ static int remove_user(const char *account)
        return UAP_STATUS_SUCCESS;
 }
 
-static int add_device(const char *account, uap_device_info_t *dev_info)
+static int add_device(const char *account, uap_device_info_t *dev_info, const char *app_id)
 {
        FUNC_ENTRY;
        int ret;
        uam_device_info_s device;
        uam_user_info_s user;
+       int app_num = -1;
+
+       _uam_core_get_app_num_from_app_key(app_id, &app_num);
+
+       if (app_num ==  -1) {
+               UAM_ERR("add_device failed");
+               return UAP_STATUS_FAIL;
+       }
 
        if (NULL == account) {
-               if (UAM_ERROR_NONE != _uam_core_get_default_user(&user)) {
+               //TODO: issue of finding app_num here
+               if (UAM_ERROR_NONE != _uam_core_get_default_user(&user, app_num)) {
                        UAM_ERR("_uam_core_get_default_user failed");
                        return UAP_STATUS_FAIL;
                }
@@ -124,15 +151,22 @@ static int add_device(const char *account, uap_device_info_t *dev_info)
        return ret;
 }
 
-static int remove_device(const char *device_id, int type)
+static int remove_device(const char *device_id, int type, const char *app_id)
 {
        FUNC_ENTRY;
-
+       int app_num = -1;
        retv_if(NULL == device_id, UAP_STATUS_INVALID_PARAMS);
        retv_if(UAP_TECH_TYPE_NONE >= type, UAM_ERROR_INVALID_PARAMETER);
        retv_if(UAP_TECH_TYPE_P2P < type, UAM_ERROR_INVALID_PARAMETER);
 
-       if (UAM_ERROR_NONE != _uam_core_remove_device_by_device_id(device_id, type)) {
+       _uam_core_get_app_num_from_app_key(app_id, &app_num);
+
+       if (app_num == -1) {
+               UAM_ERR("remove_device fail");
+               return UAP_STATUS_FAIL;
+       }
+
+       if (UAM_ERROR_NONE != _uam_core_remove_device_by_device_id(device_id, type, app_num)) {
                UAM_ERR("_uam_core_remove_user() failed");
                return UAP_STATUS_FAIL;
        }
@@ -156,7 +190,7 @@ static uac_callbacks_t cloud_cb = {
        .reset_cb = reset_ua_db
 };
 
-void _uam_cloud_update_registered_devices(void)
+void _uam_cloud_update_registered_devices()
 {
        FUNC_ENTRY;
        int indx;
@@ -170,8 +204,8 @@ void _uam_cloud_update_registered_devices(void)
        api = (uac_api_t *)cloud_plugin->api;
        ret_if(NULL == api);
        ret_if(NULL == api->set_registered_devices);
-
-       if (UAM_ERROR_NONE != _uam_core_get_devices(&num_devices, &devices)) {
+       //TODO: How to decide app_num here
+       if (UAM_ERROR_NONE != _uam_core_get_devices(&num_devices, &devices, -1)) {
                UAM_ERR("_uam_core_get_devices() failed");
        } else {
                dev_list = g_new0(uap_device_info_t, num_devices);
diff --git a/ua-daemon/src/pm/ua-light-plugin-handler.c b/ua-daemon/src/pm/ua-light-plugin-handler.c
deleted file mode 100644 (file)
index 5bb2ed9..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License")
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <ua-plugin.h>
-#include <ua-plugin-manager.h>
-#include "ua-pm-util.h"
-
-void light_state_changed_callback(int state)
-{
-       FUNC_ENTRY;
-
-       UAM_DBG("Light: %s", state == UAS_STATE_READY ? "READY" : "NOT_READY");
-
-       FUNC_EXIT;
-}
-
-static void light_detection_state_changed_cb(int state)
-{
-       FUNC_ENTRY;
-
-       if (state)
-               _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_LIGHT);
-       else
-               _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_LIGHT);
-
-       FUNC_EXIT;
-}
-
-static void light_detection_status_changed_cb(uas_detection_type_e type,
-       void *sensor_info)
-{
-       FUNC_ENTRY;
-
-       uas_sensor_info_t *info = NULL;
-       uam_sensor_info_s *light_info = NULL;
-
-       UAM_DBG("Light: %s", type == UAS_PRESENCE ? "PRESENCE" : "ABSENCE");
-
-       if (sensor_info)
-               info = sensor_info;
-       light_info = _pm_util_uas_sensor_info_to_uam_sensor_info(info);
-
-       _uam_core_handle_status_changed(UAM_SENSOR_BITMASK_LIGHT, light_info);
-
-       g_free(light_info);
-
-       FUNC_EXIT;
-}
-
-void light_detection_callback(uas_detection_type_e type, void *sensor_info)
-{
-       FUNC_ENTRY;
-       uas_sensor_info_t *info;
-       uam_sensor_info_s *light_info = NULL;
-       int ret = UAM_ERROR_NONE;
-
-       UAM_DBG("Light: %s", type == UAS_PRESENCE ? "PRESENCE" : "ABSENCE");
-
-       info = sensor_info;
-       light_info = _pm_util_uas_sensor_info_to_uam_sensor_info(info);
-
-       if (UAS_PRESENCE == type) {
-               UAM_DBG("Presence detected by light sensor");
-               ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_LIGHT,
-                               0, light_info, NULL);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_WARN("_uam_core_handle_presence_detected failed");
-       } else {
-               UAM_DBG("Absence detected by light sensor");
-               _uam_core_handle_absence_detected(UAM_SENSOR_BITMASK_LIGHT, 0, light_info);
-       }
-
-       g_free(light_info);
-
-       FUNC_EXIT;
-}
-
-uas_callbacks_t light_cbs = {
-       .state_changed_cb = light_state_changed_callback,
-       .detection_state_cb = light_detection_state_changed_cb,
-       .detection_status_cb = light_detection_status_changed_cb,
-       .detected_cb = light_detection_callback,
-       .device_detected_cb = NULL,
-       .device_added_cb = NULL,
-       .device_active_scan_cb = NULL,
-};
diff --git a/ua-daemon/src/pm/ua-motion-plugin-handler.c b/ua-daemon/src/pm/ua-motion-plugin-handler.c
deleted file mode 100644 (file)
index 0bf1848..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License")
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <ua-plugin.h>
-#include <ua-plugin-manager.h>
-#include "ua-pm-util.h"
-
-void motion_state_changed_callback(int state)
-{
-       FUNC_ENTRY;
-
-       UAM_DBG("Motion: %s", state == UAS_STATE_READY ? "READY" : "NOT_READY");
-
-       FUNC_EXIT;
-}
-
-static void motion_detection_state_changed_cb(int state)
-{
-       FUNC_ENTRY;
-
-       if (state)
-               _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_MOTION);
-       else
-               _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_MOTION);
-
-       FUNC_EXIT;
-}
-
-static void motion_detection_status_changed_cb(uas_detection_type_e type,
-       void *sensor_info)
-{
-       FUNC_ENTRY;
-
-       uas_sensor_info_t *info = NULL;
-       uam_sensor_info_s *motion_info = NULL;
-
-       UAM_DBG("Motion: %s", type == UAS_PRESENCE ? "PRESENCE" : "ABSENCE");
-
-       if (sensor_info)
-               info = sensor_info;
-       motion_info = _pm_util_uas_sensor_info_to_uam_sensor_info(info);
-
-       _uam_core_handle_status_changed(UAM_SENSOR_BITMASK_MOTION, motion_info);
-
-       g_free(motion_info);
-
-       FUNC_EXIT;
-}
-
-void motion_detection_callback(uas_detection_type_e type, void *sensor_info)
-{
-       FUNC_ENTRY;
-       uas_sensor_info_t *info = NULL;
-       uam_sensor_info_s *motion_info = NULL;
-       int ret = UAM_ERROR_NONE;
-
-       UAM_DBG("Motion: %s", type == UAS_PRESENCE ? "PRESENCE" : "ABSENCE");
-
-       if (sensor_info)
-               info = sensor_info;
-       motion_info = _pm_util_uas_sensor_info_to_uam_sensor_info(info);
-
-       if (UAS_PRESENCE == type) {
-               UAM_DBG("Presence detected by motion sensor");
-               ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_MOTION,
-                       0, motion_info, NULL);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_WARN("_uam_core_handle_presence_detected failed");
-       } else {
-               UAM_DBG("Absence detected by motion sensor");
-               _uam_core_handle_absence_detected(UAM_SENSOR_BITMASK_MOTION, 0, motion_info);
-       }
-
-       g_free(motion_info);
-
-       FUNC_EXIT;
-}
-
-uas_callbacks_t motion_cbs = {
-       .state_changed_cb = motion_state_changed_callback,
-       .detection_state_cb = motion_detection_state_changed_cb,
-       .detection_status_cb = motion_detection_status_changed_cb,
-       .detected_cb = motion_detection_callback,
-       .device_detected_cb = NULL,
-       .device_added_cb = NULL,
-       .device_active_scan_cb = NULL,
-};
diff --git a/ua-daemon/src/pm/ua-plugin-handler.c b/ua-daemon/src/pm/ua-plugin-handler.c
new file mode 100644 (file)
index 0000000..a5151ad
--- /dev/null
@@ -0,0 +1,232 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * @author: Abhay Agarwal <ay.agarwal@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <ua-plugin.h>
+#include <ua-manager-core.h>
+#include <ua-manager-database.h>
+#include <ua-plugin-manager.h>
+
+#include "ua-pm-util.h"
+
+static void state_changed_callback(int id, int state)
+{
+       FUNC_ENTRY;
+       gboolean is_ready = FALSE;
+       unsigned int sensor;
+
+       sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+       UAM_DBG("sensor: 0x%2.2X %s", sensor,
+               state == UAS_STATE_READY ? "READY" : "NOT_READY");
+
+       if (UAS_STATE_READY == state)
+               is_ready = TRUE;
+
+       _uam_core_handle_sensor_ready(sensor, is_ready);
+
+       FUNC_EXIT;
+}
+
+static void detection_state_callback(int id, int state)
+{
+       FUNC_ENTRY;
+       unsigned int sensor;
+
+       sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+       UAM_DBG("sensor: 0x%2.2X %s", sensor,
+               state == 1 ? "Detection started" : "Detection stopped");
+
+       if (state)
+               _uam_core_handle_detection_started(sensor);
+       else
+               _uam_core_handle_detection_stopped(sensor);
+
+       FUNC_EXIT;
+}
+
+static void detection_status_callback(int id,
+       uas_detection_type_e type, void *sensor_info)
+{
+       FUNC_ENTRY;
+       uas_sensor_info_t *info = NULL;
+       uam_sensor_info_s *uam_sensor_info = NULL;
+       unsigned int sensor;
+
+       sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+       UAM_DBG("sensor: 0x%2.2X status %s", sensor, type == UAS_PRESENCE ?
+               "Presence" : "Absence");
+
+       if (sensor_info)
+               info = sensor_info;
+       uam_sensor_info = _pm_util_uas_sensor_info_to_uam_sensor_info(info);
+
+       _uam_core_handle_status_changed(sensor, uam_sensor_info);
+
+       g_free(uam_sensor_info);
+
+       FUNC_EXIT;
+}
+
+static void detected_callback(int id,
+       uas_detection_type_e type, void *sensor_info)
+{
+       FUNC_ENTRY;
+       int ret = UAM_ERROR_NONE;
+       uas_sensor_info_t *info = NULL;
+       uam_sensor_info_s *uam_sensor_info = NULL;
+       unsigned int sensor;
+
+       sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+       UAM_DBG("sensor: 0x%2.2X %s", sensor, type == UAS_PRESENCE ?
+                       "Presence Detected" : "Absence Detected");
+
+       if (sensor_info)
+               info = sensor_info;
+       uam_sensor_info = _pm_util_uas_sensor_info_to_uam_sensor_info(info);
+
+       if (UAS_PRESENCE == type) {
+               UAM_DBG("Presence detected");
+               ret = _uam_core_handle_presence_detected(sensor,
+                               0, uam_sensor_info, NULL);
+               if (UAM_ERROR_NONE != ret)
+                       UAM_WARN("_uam_core_handle_presence_detected failed");
+       } else {
+               UAM_WARN("Absence detected");
+               _uam_core_handle_absence_detected(sensor, 0, uam_sensor_info);
+       }
+
+       g_free(uam_sensor_info);
+       FUNC_EXIT;
+}
+
+static void device_detected_callback(int id,
+       uas_detection_type_e type, uas_device_info_t *device)
+{
+       FUNC_ENTRY;
+       uam_device_info_s *dev_info;
+       uam_ble_payload_s *payload = NULL;
+       int ret = UAM_ERROR_NONE;
+       unsigned int sensor;
+
+       sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+
+       UAM_DBG("sensor: 0x%2.2X Device %s Detected", sensor,
+               type == UAS_PRESENCE ? "Presence" : "Absence");
+
+       ret_if(NULL == device);
+
+       dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
+       ret_if(NULL == dev_info);
+
+       if (device->payload) {
+               payload = g_new0(uam_ble_payload_s, 1);
+               _pm_util_uas_payload_to_uam_payload(payload, device->payload);
+       }
+
+       if (UAS_PRESENCE == type) {
+               UAM_DBG("Presence detected for device %s on sensor 0x%2.2X",
+                       dev_info->device_id, sensor);
+
+               ret = _uam_core_handle_presence_detected(sensor,
+                               device->user_id, dev_info, payload);
+               if (UAM_ERROR_NONE != ret)
+                       UAM_WARN("_uam_core_handle_presence_detected failed");
+
+       } else {
+               UAM_DBG("Absence detected for device %s on sensor 0x%2.2X",
+                       dev_info->device_id, sensor);
+               _uam_core_handle_absence_detected(sensor,
+                                               device->user_id, dev_info);
+       }
+
+       g_free(dev_info);
+       g_free(payload);
+
+       FUNC_EXIT;
+}
+
+static void device_added_callback(int id,
+       int status, uas_device_info_t *device)
+{
+       FUNC_ENTRY;
+       uam_device_info_s *dev_info;
+       unsigned int sensor;
+
+       sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+       UAM_DBG("sensor: 0x%2.2X Device Added %s", sensor,
+               status == UAS_STATUS_SUCCESS ? "Success" : "Failed");
+
+       ret_if(NULL == device);
+
+       dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
+       ret_if(NULL == dev_info);
+
+       dev_info->type = _pm_util_uas_plugin_id_to_tech_type(id);
+       UAM_INFO("Status: %s", _pm_util_uas_status_to_str(status));
+
+       if (UAS_STATUS_SUCCESS != status)
+               status = UAM_ERROR_INTERNAL;
+       else
+               status = UAM_ERROR_NONE;
+
+       _uam_core_handle_device_added(status, device->user_id, dev_info);
+       g_free(dev_info);
+
+       FUNC_EXIT;
+}
+
+static void device_active_scan_callback(int id,
+       uas_active_scan_event_e event, const uas_device_info_t *device)
+{
+       FUNC_ENTRY;
+       uam_device_info_s *dev_info = NULL;
+       uam_active_scan_event_e event_info;
+       unsigned int sensor;
+
+       sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+       UAM_DBG("sensor: 0x%2.2X Active scan callback", sensor);
+
+       if (UAS_ACTIVE_DEVICE_FOUND == event) {
+               ret_if(NULL == device);
+
+               dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
+               ret_if(NULL == dev_info);
+
+               dev_info->type = UAM_TECH_TYPE_WIFI;
+       }
+
+       event_info = _pm_util_uas_scan_event_to_uam_scan_event(event);
+
+       _uam_core_handle_active_device(event_info, sensor, dev_info);
+
+       g_free(dev_info);
+       FUNC_EXIT;
+}
+
+uas_callbacks_t plugin_cbs = {
+       .state_changed_cb = state_changed_callback,
+       .detection_state_cb = detection_state_callback,
+       .detection_status_cb = detection_status_callback,
+       .detected_cb = detected_callback,
+       .device_detected_cb = device_detected_callback,
+       .device_added_cb = device_added_callback,
+       .device_active_scan_cb = device_active_scan_callback,
+};
index 48faa00..1eb9240 100644 (file)
 #include <dlfcn.h>
 #include <ua-plugin.h>
 #include <ua-plugin-manager.h>
+#include <ua-vendor-plugin.h>
 #include <tzplatform_config.h>
 
 #include "ua-pm-util.h"
 
 static unsigned int available_sensors = 0;
+static unsigned int user_sensors = 0;
+static unsigned int env_sensors = 0;
 
-extern const uas_callbacks_t ble_cbs;
-extern const uas_callbacks_t wifi_cbs;
-extern const uas_callbacks_t light_cbs;
-extern const uas_callbacks_t motion_cbs;
+extern const uas_callbacks_t plugin_cbs;
 
 typedef struct {
        void *handle;
@@ -181,61 +181,31 @@ static int __init_sensor_plugins(void)
        for (id = UAS_PLUGIN_ID_BLE; id < UAS_PLUGIN_ID_MAX; id++) {
                uam_sensor_plugin_info_t *plugin = plugins[id];
                int status;
+               int sensor;
 
                if (!plugin || !plugin->api) {
                        UAM_WARN("Plugin with ID: %s is NULL", _uam_get_sensor_str(id));
                        continue;
                }
 
-               switch (id) {
-               case UAS_PLUGIN_ID_BLE:
-                       status = plugin->api->init(&ble_cbs);
-                       if (UAS_STATUS_SUCCESS != status) {
-                               /* Deinit plugin */
-                               continue;
-                       }
-
-                       plugin->capability = UAS_NOT_SUPPORT_USER;
-                       if (UAS_STATUS_SUCCESS != plugin->api->get_capability(
-                                               &plugin->capability))
-                               plugin->capability = UAS_NOT_SUPPORT_USER;
-
-                       available_sensors |= UAM_SENSOR_BITMASK_BLE;
-                       break;
-               case UAS_PLUGIN_ID_WIFI:
-                       status = plugin->api->init(&wifi_cbs);
-                       if (UAS_STATUS_SUCCESS != status) {
-                               /* Deinit plugin */
-                               continue;
-                       }
+               status = plugin->api->init(&plugin_cbs);
+               if (UAS_STATUS_SUCCESS != status) {
+                       /* Deinit plugin */
+                       continue;
+               }
 
+               plugin->capability = UAS_NOT_SUPPORT_USER;
+               if (UAS_STATUS_SUCCESS != plugin->api->get_capability(
+                                       &plugin->capability))
                        plugin->capability = UAS_NOT_SUPPORT_USER;
-                       if (UAS_STATUS_SUCCESS != plugin->api->get_capability(
-                                               &plugin->capability))
-                               plugin->capability = UAS_NOT_SUPPORT_USER;
 
-                       available_sensors |= UAM_SENSOR_BITMASK_WIFI;
-                       break;
+               sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+               available_sensors |= sensor;
 
-               case UAS_PLUGIN_ID_LIGHT:
-                       status = plugin->api->init(&light_cbs);
-                       if (UAS_STATUS_SUCCESS != status) {
-                               /* Deinit plugin */
-                               continue;
-                       }
-
-                       available_sensors |= UAM_SENSOR_BITMASK_LIGHT;
-                       break;
-               case UAS_PLUGIN_ID_MOTION:
-                       status = plugin->api->init(&motion_cbs);
-                       if (UAS_STATUS_SUCCESS != status) {
-                               /* Deinit plugin */
-                               continue;
-                       }
-
-                       available_sensors |= UAM_SENSOR_BITMASK_MOTION;
-                       break;
-               }
+               if (plugin->capability == UAS_SUPPORT_USER)
+                       user_sensors |= sensor;
+               else
+                       env_sensors |= sensor;
 
                /* If atleast one sensor init is success, return success */
                ret = UAM_ERROR_NONE;
@@ -271,6 +241,8 @@ static void __deinit_sensor_plugins(void)
        }
 
        available_sensors = 0;
+       user_sensors = 0;
+       env_sensors = 0;
        FUNC_EXIT;
 }
 
@@ -318,40 +290,64 @@ static int __get_uas_error_code(int error)
        }
 }
 
+/* Only unique devices in list.
+ * Time complexity is n^2. Any other approach? */
 static uas_device_info_t *__get_uas_device_list(int id, GSList *devices, int *num_devices)
 {
        FUNC_ENTRY;
+       uam_db_device_info_t *dev;
+       uam_db_device_info_t *dev1;
+       uas_device_info_t *uas_dev;
        uas_device_info_t *dev_list;
        GSList *l;
-       int len;
+       GSList *l1;
+       GSList *unique_dev = NULL;
+       int len = 0;
        int count = 0;
-       unsigned int tech_type;
 
+       unsigned int tech_type = _pm_util_uas_plugin_id_to_tech_type(id);
+       retv_if(UAM_TECH_TYPE_NONE == tech_type, NULL);
        retv_if(NULL == devices, NULL);
+       retv_if(NULL == num_devices, NULL);
 
-       tech_type = _pm_util_uas_plugin_id_to_tech_type(id);
-       retv_if(UAM_TECH_TYPE_NONE == tech_type, NULL);
+       for (l = devices; l; l = g_slist_next(l)) {
+               dev = l->data;
+               if (!dev || !(dev->supported_techs & tech_type))
+                       continue;
 
-       len = g_slist_length(devices);
-       dev_list = g_new0(uas_device_info_t, len);
+               gboolean unique = TRUE;
+               for (l1 = unique_dev; l1; l1 = g_slist_next(l1)) {
+                       dev1 = l1->data;
+                       if (!dev1 || g_strcmp0(dev->device_id, dev1->device_id))
+                               continue;
+
+                       unique = FALSE;
+                       break;
+               }
+
+               if (unique) {
+                       unique_dev = g_slist_prepend(unique_dev, dev);
+                       len++;
+               }
+       }
 
-       for (l = devices; NULL != l; l = g_slist_next(l)) {
-               uam_db_device_info_t *dev = l->data;
-               uas_device_info_t *device = &(dev_list[count]);
+       dev_list = g_new0(uas_device_info_t, len);
 
+       for (l = unique_dev; l; l = g_slist_next(l)) {
+               dev = l->data;
+               uas_dev = &(dev_list[count]);
                if (!dev)
                        continue;
 
-               if (dev->supported_techs & tech_type) {
-                       _pm_util_uam_db_dev_to_uas_dev(tech_type, dev, &device);
-                       device->supported_techs = dev->supported_techs;
-                       count++;
-               }
+               _pm_util_uam_db_dev_to_uas_dev(tech_type, dev, &uas_dev);
+               uas_dev->supported_techs = dev->supported_techs;
+               count++;
        }
+       UAM_INFO("%d device present in device list for tech %d", count, tech_type);
 
        *num_devices = count;
+       g_slist_free(unique_dev);
        FUNC_EXIT;
-
        return dev_list;
 }
 
@@ -379,14 +375,22 @@ void _uam_pm_deinit(void)
 
 unsigned int _uam_pm_get_avaliable_sensors(void)
 {
-       FUNC_ENTRY;
-
        UAM_DBG("Available sensors: 0x%8.8X", available_sensors);
-
-       FUNC_EXIT;
        return available_sensors;
 }
 
+unsigned int _uam_pm_get_user_sensors(void)
+{
+       UAM_DBG("User sensors: 0x%8.8X", user_sensors);
+       return user_sensors;
+}
+
+unsigned int _uam_pm_get_env_sensors(void)
+{
+       UAM_DBG("Env sensors: 0x%8.8X", env_sensors);
+       return env_sensors;
+}
+
 gboolean _uam_pm_is_sensor_ready(unsigned int sensor_bitmask)
 {
        FUNC_ENTRY;
@@ -475,7 +479,7 @@ int _uam_pm_unregister_device(int user_id, const uam_device_info_s *dev)
        return UAM_ERROR_NONE;
 }
 
-int _uam_pm_set_registered_devices(GSList *devices)
+int _uam_pm_set_registered_devices(GSList *devices, unsigned int bitmask)
 {
        FUNC_ENTRY;
        int id;
@@ -484,6 +488,10 @@ int _uam_pm_set_registered_devices(GSList *devices)
 
        for (id = UAS_PLUGIN_ID_BLE; id < UAS_PLUGIN_ID_MAX; id++) {
                uam_sensor_plugin_info_t *plugin = plugins[id];
+               unsigned int sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
+
+               if (!(sensor & bitmask))
+                       continue;
 
                if (!plugin || !plugin->api)
                        continue;
@@ -511,6 +519,8 @@ int _uam_pm_start_detection(unsigned int detection_mode, unsigned int bitmask)
        FUNC_ENTRY;
        int id;
        int status;
+       int scan_mode = UAS_SCAN_MODE_LOW_LATENCY;
+       extern uam_vendor_plugin_info_t* vendor_plugin;
        int ret = UAM_ERROR_INTERNAL;
        uas_detection_type_e type;
 
@@ -525,6 +535,13 @@ int _uam_pm_start_detection(unsigned int detection_mode, unsigned int bitmask)
                type = UAS_PRESENCE | UAS_ABSENCE;
        }
 
+       if (vendor_plugin && vendor_plugin->api)
+               vendor_plugin->api->get_scan_level(&scan_mode);
+       else
+               UAM_ERR("vendor plugin not INITIALISED");
+
+       UAM_INFO("The scan_level is [%d]", scan_mode);
+
        for (id = UAS_PLUGIN_ID_BLE; id < UAS_PLUGIN_ID_MAX; id++) {
                uam_sensor_plugin_info_t *plugin = plugins[id];
                unsigned int sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id);
@@ -535,7 +552,7 @@ int _uam_pm_start_detection(unsigned int detection_mode, unsigned int bitmask)
                if (!plugin || !plugin->api)
                        continue;
 
-               status = plugin->api->start_detection(type);
+               status = plugin->api->start_detection(type, scan_mode);
 
                if (UAS_STATUS_SUCCESS != status && UAS_STATUS_ALREADY_DONE != status) {
                        UAM_ERR("plugin->start_detection(%d) failed for %d", type, id);
index ed4cf30..0db6427 100644 (file)
@@ -136,13 +136,13 @@ void _pm_util_uam_db_payload_to_uas_payload(
 {
        if (src_payload) {
                dst_payload->primary_key = src_payload->primary_key;
-               dst_payload->device_icon = src_payload->device_icon;
+               dst_payload->device_type = src_payload->device_type;
                dst_payload->secondary_key = src_payload->secondary_key;
+               dst_payload->ext_val1 = src_payload->ext_val1;
+               dst_payload->ext_val2 = src_payload->ext_val2;
                dst_payload->device_uid_len = src_payload->device_uid_len;
                dst_payload->device_uid = g_memdup((src_payload->device_uid),
                                                src_payload->device_uid_len);
-               dst_payload->bt_mac = g_memdup((src_payload->bt_mac),
-                                               UAM_BT_MAC_ADDRESS_STRING_LEN);
        }
 }
 
@@ -150,23 +150,25 @@ void _pm_util_uas_payload_to_uam_payload(
                uam_ble_payload_s *dst_payload, uas_payload_info_t *src_payload)
 {
        if (src_payload) {
-               int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
+               int user_data_len;
+
                dst_payload->primary_key = src_payload->primary_key;
-               dst_payload->device_icon = src_payload->device_icon;
+               dst_payload->device_type = src_payload->device_type;
                dst_payload->secondary_key = src_payload->secondary_key;
+               dst_payload->ext_val1 = src_payload->ext_val1;
+               dst_payload->ext_val2 = src_payload->ext_val2;
                dst_payload->device_uid_len = src_payload->device_uid_len;
                memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
-               memset(dst_payload->bt_mac, 0, UAM_BT_MAC_ADDRESS_STRING_LEN);
-               memset(dst_payload->user_data, 0, user_data_len);
                if (src_payload->device_uid)
                        memcpy(dst_payload->device_uid,
                        src_payload->device_uid, src_payload->device_uid_len);
+
+               user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN
+                                       - 1 - src_payload->device_uid_len;
+               memset(dst_payload->user_data, 0, user_data_len);
                if (src_payload->user_data)
                        memcpy(dst_payload->user_data,
                        src_payload->user_data, user_data_len);
-               if (src_payload->bt_mac)
-                       memcpy(dst_payload->bt_mac,
-                       src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN);
        }
 }
 
@@ -186,6 +188,24 @@ void _pm_util_uas_device_info_free(uas_device_info_t *device)
        FUNC_EXIT;
 }
 
+static gint __compare_payload(gconstpointer data, gconstpointer user_data)
+{
+       const uam_db_payload_info_t *list_payload = data;
+       const uam_db_payload_info_t *payload = user_data;
+
+       retv_if(NULL == list_payload, -1);
+       retv_if(NULL == payload, -1);
+
+       if ((list_payload->primary_key == payload->primary_key) &&
+               (list_payload->secondary_key == payload->secondary_key) &&
+               (list_payload->device_uid_len == payload->device_uid_len) &&
+               (0 == memcmp(list_payload->device_uid, payload->device_uid,
+                       payload->device_uid_len)))
+               return 0;
+
+       return -1;
+}
+
 void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type,
                uam_db_device_info_t *dev, uas_device_info_t **device)
 {
@@ -205,12 +225,12 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type,
                memset(*device, 0x00, sizeof(uas_device_info_t));
 
        (*device)->user_id = dev->user->user_id;
-       (*device)->os = dev->os;
        (*device)->device_id = g_strdup(dev->device_id);
 
        for (l = dev->tech_list; NULL != l; l = g_slist_next(l)) {
                uam_db_tech_info_t *tech = l->data;
-               GSList *l1;
+               GSList *unique_payloads = NULL;
+               GSList *l1, *l2;
 
                if (!tech || !tech->addresses)
                        continue;
@@ -218,6 +238,7 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type,
                if (tech_type != tech->tech_type)
                        continue;
 
+               (*device)->os = tech->os;
                (*device)->discriminant = tech->discriminant;
                (*device)->num_addr = g_slist_length(tech->addresses);
                (*device)->addr_list = g_new0(uas_address_info_t, (*device)->num_addr);
@@ -231,21 +252,34 @@ void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type,
                        (*device)->addr_list[i++].address = g_strdup(addr->address);
                }
 
-               /** update payload list */
-               len = g_slist_length(tech->svc_dev_list);
-               (*device)->num_payload = len;
-               (*device)->payload = g_new0(uas_payload_info_t, len);
-               i = 0;
+               /* Find unique payload list for the device */
                for (l1 = tech->svc_dev_list; NULL != l1; l1 = g_slist_next(l1)) {
                        uam_svc_dev_info_t *svc_dev = l1->data;
                        uam_db_payload_info_t *payload = svc_dev->payload;
 
                        if (!payload)
                                continue;
+                       l2 = g_slist_find_custom(unique_payloads, payload, __compare_payload);
+                       if (!l2)
+                               unique_payloads = g_slist_prepend(unique_payloads, payload);
+               }
+
+               len = g_slist_length(unique_payloads);
+               (*device)->num_payload = len;
+               (*device)->payload = g_new0(uas_payload_info_t, len);
+               i = 0;
+               UAM_INFO("%d payload present for device %s tech %d", len, dev->device_id, tech->tech_type);
+               for (l1 = unique_payloads; NULL != l1; l1 = g_slist_next(l1)) {
+                       uam_db_payload_info_t *payload = l1->data;
+
+                       if (!payload)
+                               continue;
 
                        _pm_util_uam_db_payload_to_uas_payload(&((*device)->payload[i]), payload);
                        i++;
                }
+               g_slist_free(unique_payloads);
+
        }
 
        FUNC_EXIT;
@@ -301,7 +335,6 @@ uas_device_info_t *_pm_util_uam_dev_info_to_uas_dev_info(const uam_device_info_s
                UAM_WARN("device->num_addr = %d", device->num_addr);
 
        device->device_id = g_strdup(dev->device_id);
-
        device->addr_list = g_new0(uas_address_info_t, device->num_addr);
        if (mac) {
                device->addr_list[i].type = type;
index 22237de..f40efc4 100644 (file)
 
 extern const uav_callbacks_t vendor_cbs;
 
-typedef struct {
-       void *handle;
-       uav_module_t *module;
-       uap_api_t *api;
-} uam_vendor_plugin_info_t;
-
-static uam_vendor_plugin_info_t* vendor_plugin = NULL;
+uam_vendor_plugin_info_t* vendor_plugin = NULL;
 
 static void __deinit_vendor_plugin(void)
 {
diff --git a/ua-daemon/src/pm/ua-wifi-plugin-handler.c b/ua-daemon/src/pm/ua-wifi-plugin-handler.c
deleted file mode 100644 (file)
index 53f5df2..0000000
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License")
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <ua-plugin.h>
-#include <ua-manager-core.h>
-#include <ua-plugin-manager.h>
-
-#include "ua-pm-util.h"
-
-static void wifi_state_changed_callback(int state)
-{
-       FUNC_ENTRY;
-       gboolean is_ready = FALSE;
-
-       if (UAS_STATE_READY == state)
-               is_ready = TRUE;
-
-       _uam_core_handle_sensor_ready(UAM_SENSOR_BITMASK_WIFI, is_ready);
-
-       FUNC_EXIT;
-}
-
-static void wifi_lpm_detection_callback(uas_detection_type_e type, void *sensor_info)
-{
-       FUNC_ENTRY;
-       int ret = UAM_ERROR_NONE;
-
-       /* Currently, in lpm mode only Presence is detected */
-       if (UAS_PRESENCE == type) {
-               UAM_DBG("Presence detected");
-               ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_WIFI,
-                               -1, NULL, NULL);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_WARN("_uam_core_handle_presence_detected failed");
-       } else {
-               UAM_WARN("Unlikely Absence detected");
-       }
-
-       FUNC_EXIT;
-}
-
-static void wifi_detection_state_changed_cb(int state)
-{
-       FUNC_ENTRY;
-
-       if (state)
-               _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_WIFI);
-       else
-               _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_WIFI);
-
-       FUNC_EXIT;
-}
-
-static void wifi_device_detection_callback(uas_detection_type_e type, uas_device_info_t *device)
-{
-       FUNC_ENTRY;
-       uam_device_info_s *dev_info;
-       int ret = UAM_ERROR_NONE;
-
-       ret_if(NULL == device);
-
-       dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
-       ret_if(NULL == dev_info);
-
-       UAM_INFO("dev_info->mac [%s]", dev_info->mac);
-
-       if (UAS_PRESENCE == type) {
-               UAM_DBG("Presence detected for %s", dev_info->device_id);
-               ret = _uam_core_handle_presence_detected(UAM_SENSOR_BITMASK_WIFI,
-                               device->user_id, dev_info, NULL);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_WARN("_uam_core_handle_presence_detected failed");
-       } else {
-               UAM_DBG("Absence detected for %s", dev_info->device_id);
-               _uam_core_handle_absence_detected(UAM_SENSOR_BITMASK_WIFI, device->user_id, dev_info);
-       }
-
-       g_free(dev_info);
-       FUNC_EXIT;
-}
-
-static void wifi_device_added_callback(int status, uas_device_info_t *device)
-{
-       FUNC_ENTRY;
-       uam_device_info_s *dev_info;
-
-       ret_if(NULL == device);
-
-       dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
-       ret_if(NULL == dev_info);
-
-       dev_info->type = UAM_TECH_TYPE_WIFI;
-       UAM_INFO("Status: %s", _pm_util_uas_status_to_str(status));
-       if (UAS_STATUS_SUCCESS != status)
-               status = UAM_ERROR_INTERNAL;
-       else
-               status = UAM_ERROR_NONE;
-
-       UAM_INFO("[%d]", device->user_id);
-       _uam_core_handle_device_added(status, device->user_id, dev_info);
-       g_free(dev_info);
-       FUNC_EXIT;
-}
-
-static void wifi_active_scan_callback(uas_active_scan_event_e event,
-               const uas_device_info_t *device)
-{
-       FUNC_ENTRY;
-       uam_device_info_s *dev_info = NULL;
-       uam_active_scan_event_e event_info;
-
-       if (UAS_ACTIVE_DEVICE_FOUND == event) {
-               ret_if(NULL == device);
-
-               dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device);
-               ret_if(NULL == dev_info);
-
-               dev_info->type = UAM_TECH_TYPE_WIFI;
-       }
-
-       event_info = _pm_util_uas_scan_event_to_uam_scan_event(event);
-
-       _uam_core_handle_active_device(event_info, UAM_SENSOR_BITMASK_WIFI, dev_info);
-
-       g_free(dev_info);
-       FUNC_EXIT;
-}
-
-uas_callbacks_t wifi_cbs = {
-       .state_changed_cb = wifi_state_changed_callback,
-       .detection_state_cb = wifi_detection_state_changed_cb,
-       .detection_status_cb = NULL,
-       .detected_cb = wifi_lpm_detection_callback,
-       .device_detected_cb = wifi_device_detection_callback,
-       .device_added_cb = wifi_device_added_callback,
-       .device_active_scan_cb = wifi_active_scan_callback,
-};
index 4b753db..8077bd5 100644 (file)
 #include "ua-manager-common.h"
 #include "ua-manager-database.h"
 
-#define SELECT_ALL_ADVS "SELECT ibeacon_id, adv_len, ibeacon_adv FROM ibeacon_adv"
+#define SELECT_ALL_ADVS "SELECT ibeacon_id, adv_len, ibeacon_adv, app_num FROM ibeacon_adv " \
+       "WHERE app_num = ? "
 
-#define INSERT_IBEACON_ADV "insert into ibeacon_adv (ibeacon_adv, adv_len) " \
-       "values (?, ?)"
+#define SELECT_ADVS_WITHOUT_APP_NUM "SELECT ibeacon_id, adv_len, ibeacon_adv, app_num FROM ibeacon_adv"
 
-#define DELETE_ALL_ADV_DATA "delete from ibeacon_adv "
+#define INSERT_IBEACON_ADV "insert into ibeacon_adv (ibeacon_adv, adv_len, app_num) " \
+       "values (?, ?, ?)"
+
+#define DELETE_ALL_ADV_DATA "delete from ibeacon_adv WHERE app_num = ?"
+
+#define DELETE_ADVS_WITHOUT_APP_NUM "delete from ibeacon_adv"
 
 /* DELETE statements */
 static sqlite3_stmt *delete_all_adv_data;
+static sqlite3_stmt *delete_advs_without_app_num;
 
 /* SELECT statements */
 static sqlite3_stmt *select_all_advs;
+static sqlite3_stmt *select_advs_without_app_num;
 
 /* INSERT statements */
 static sqlite3_stmt *insert_ibeacon_adv;
@@ -47,6 +54,7 @@ static void __uam_adv_finalize_delete(void)
        FUNC_ENTRY;
 
        FINALIZE(delete_all_adv_data);
+       FINALIZE(delete_advs_without_app_num);
 
        FUNC_EXIT;
 }
@@ -56,6 +64,7 @@ static void __uam_adv_finalize_select(void)
        FUNC_ENTRY;
 
        FINALIZE(select_all_advs);
+       FINALIZE(select_advs_without_app_num);
 
        FUNC_EXIT;
 }
@@ -82,6 +91,8 @@ static int __uam_adv_prepare_delete(sqlite3 *db)
 
        PREPARE_QUERY(rc, db, delete_all_adv_data,
                DELETE_ALL_ADV_DATA, __uam_adv_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_advs_without_app_num,
+               DELETE_ADVS_WITHOUT_APP_NUM, __uam_adv_finalize_delete);
 
        initialized = 1;
        FUNC_EXIT;
@@ -102,6 +113,8 @@ static int __uam_adv_prepare_select(sqlite3 *db)
 
        PREPARE_QUERY(rc, db, select_all_advs,
                SELECT_ALL_ADVS, __uam_adv_finalize_select);
+       PREPARE_QUERY(rc, db, select_advs_without_app_num,
+               SELECT_ADVS_WITHOUT_APP_NUM, __uam_adv_finalize_select);
 
        initialized = 1;
        FUNC_EXIT;
@@ -164,7 +177,6 @@ int _uam_adv_db_deinitialize(void)
        retv_if(NULL == database_handle, UAM_ERROR_NONE);
 
        __uam_adv_table_info_finalize();
-       sqlite3_close(database_handle);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -185,7 +197,7 @@ handle_error:
        return UAM_ERROR_DB_FAILED;
 }
 
-int _uam_db_insert_adv_info(unsigned int adv_len, const char *iadv)
+int _uam_db_insert_adv_info(unsigned int adv_len, const char *iadv, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -193,13 +205,16 @@ int _uam_db_insert_adv_info(unsigned int adv_len, const char *iadv)
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == iadv, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
-       UAM_INFO("%u-%s-0x%0x", adv_len, iadv, iadv[30]);
+       UAM_INFO("%u-%s-0x%0x-%d", adv_len, iadv, iadv[30], app_num);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, iadv, adv_len, SQLITE_TRANSIENT),
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 2, adv_len),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 3, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -220,14 +235,23 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_db_get_all_advs(void)
+GSList *_uam_db_get_all_advs(const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_all_advs;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *adv_list = NULL;
        db_adv_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = select_advs_without_app_num;
+       } else {
+               stmt = select_all_advs;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        do {
                sql_ret = sqlite3_step(stmt);
 
@@ -238,11 +262,12 @@ GSList *_uam_db_get_all_advs(void)
                        info = g_new0(db_adv_info_t, 1);
                        info->ibeacon_id = sqlite3_column_int(stmt, 0);
                        info->adv_len = sqlite3_column_int(stmt, 1);
+                       info->app_num = sqlite3_column_int(stmt, 3);
                        memset(info->iadv, 0, UAM_IBEACON_ADV_MAX_LEN + 1);
                        memcpy(info->iadv, (char *)sqlite3_column_text(stmt, 2),
                                UAM_IBEACON_ADV_MAX_LEN);
 
-                       UAM_INFO("%d-%d-%s", info->ibeacon_id, info->adv_len, info->iadv);
+                       UAM_INFO("%d-%d-%s-%d", info->ibeacon_id, info->adv_len, info->iadv, info->app_num);
 
                        adv_list = g_slist_append(adv_list, info);
                        break;
@@ -253,17 +278,28 @@ GSList *_uam_db_get_all_advs(void)
                }
        } while (sql_ret == SQLITE_ROW);
 
+handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_db_get_all_advs failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return adv_list;
 }
 
-int _uam_adv_db_clear(void)
+int _uam_adv_db_clear(const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_all_adv_data;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = delete_advs_without_app_num;
+       } else {
+               stmt = delete_all_adv_data;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
                UAM_ERR("Failed to delete adv data [%d:%s]",
diff --git a/ua-daemon/src/ua-manager-app-db.c b/ua-daemon/src/ua-manager-app-db.c
new file mode 100644 (file)
index 0000000..52923a7
--- /dev/null
@@ -0,0 +1,452 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include <sqlite3.h>
+#include <dlfcn.h>
+
+#include "ua-manager-common.h"
+#include "ua-manager-database.h"
+
+#define SELECT_ALL_APPS_DATA "SELECT app_num, app_key FROM apps"
+
+#define SELECT_APP_DATA "SELECT app_num, app_key FROM apps WHERE app_num = ?"
+
+#define SELECT_MAX_APP_ID "SELECT MAX(app_num) FROM apps"
+
+#define INSERT_APP_INFO "insert into apps (app_key) values (?)"
+
+#define DELETE_ALL_APPS_DATA "delete from apps "
+
+#define DELETE_APP_INFO "delete from apps WHERE app_num = ? "
+
+#define UPDATE_APP_INFO "UPDATE apps SET app_key = ? WHERE app_num = ?"
+
+/* DELETE statements */
+static sqlite3_stmt *delete_all_apps_data;
+static sqlite3_stmt *delete_app_info;
+
+/* SELECT statements */
+static sqlite3_stmt *select_all_apps_data;
+static sqlite3_stmt *select_app_data;
+static sqlite3_stmt *select_max_app_num;
+
+/* INSERT statements */
+static sqlite3_stmt *insert_app_info;
+
+/* UPDATE statements */
+static sqlite3_stmt *update_app_info;
+
+extern sqlite3 *database_handle;
+static int max_app_num;
+
+static void __uam_finalize_delete(void)
+{
+       FUNC_ENTRY;
+
+       FINALIZE(delete_all_apps_data);
+       FINALIZE(delete_app_info);
+
+       FUNC_EXIT;
+}
+
+static void __uam_finalize_select(void)
+{
+       FUNC_ENTRY;
+
+       FINALIZE(select_all_apps_data);
+       FINALIZE(select_app_data);
+       FINALIZE(select_max_app_num);
+
+       FUNC_EXIT;
+}
+
+static void __uam_finalize_insert(void)
+{
+       FUNC_ENTRY;
+
+       FINALIZE(insert_app_info);
+
+       FUNC_EXIT;
+}
+
+static void __uam_finalize_update(void)
+{
+       FUNC_ENTRY;
+
+       FINALIZE(update_app_info);
+
+       FUNC_EXIT;
+}
+
+static int __uam_prepare_delete(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+       PREPARE_QUERY(rc, db, delete_all_apps_data,
+               DELETE_ALL_APPS_DATA, __uam_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_app_info,
+       DELETE_APP_INFO, __uam_finalize_delete);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_prepare_select(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+       PREPARE_QUERY(rc, db, select_all_apps_data,
+       SELECT_ALL_APPS_DATA, __uam_finalize_select);
+       PREPARE_QUERY(rc, db, select_app_data,
+       SELECT_APP_DATA, __uam_finalize_select);
+       PREPARE_QUERY(rc, db, select_max_app_num,
+               SELECT_MAX_APP_ID, __uam_finalize_select);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_prepare_insert(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+       PREPARE_QUERY(rc, db, insert_app_info,
+               INSERT_APP_INFO, __uam_finalize_insert);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_prepare_update(sqlite3 *db)
+{
+       FUNC_ENTRY;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               FUNC_EXIT;
+               return SQLITE_OK;
+       }
+
+       PREPARE_QUERY(rc, db, update_app_info,
+               UPDATE_APP_INFO, __uam_finalize_update);
+
+       initialized = 1;
+       FUNC_EXIT;
+       return rc;
+}
+
+static int __uam_table_appsinfo_prepare(sqlite3 *db)
+{
+       FUNC_ENTRY;
+
+       int error_code = UAM_ERROR_NONE;
+
+       if (db == NULL) {
+               FUNC_EXIT;
+               return UAM_ERROR_DB_FAILED;
+       }
+
+       DB_ACTION(__uam_prepare_delete(db), error_code, handle_error);
+       DB_ACTION(__uam_prepare_select(db), error_code, handle_error);
+       DB_ACTION(__uam_prepare_update(db), error_code, handle_error);
+       DB_ACTION(__uam_prepare_insert(db), error_code, handle_error);
+
+handle_error:
+       FUNC_EXIT;
+       return error_code;
+}
+
+static void __uam_table_appsinfo_finalize(void)
+{
+       FUNC_ENTRY;
+       __uam_finalize_delete();
+       __uam_finalize_select();
+       __uam_finalize_insert();
+       FUNC_EXIT;
+}
+
+int _uam_db_get_max_app_num(void)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = select_max_app_num;
+       int sql_ret = SQLITE_OK;
+
+       do {
+               sql_ret = sqlite3_step(stmt);
+
+               switch (sql_ret) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       max_app_num = sqlite3_column_int(stmt, 0);
+                       UAM_INFO("max_app_num = %d", max_app_num);
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       error_code = UAM_ERROR_DB_FAILED;
+                       UAM_ERR("Failed to select max app id [%d:%s]",
+                               sql_ret, sqlite3_errmsg(database_handle));
+                       }
+       } while (sql_ret == SQLITE_ROW);
+
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+int _uam_app_db_initialize(void)
+{
+       FUNC_ENTRY;
+
+       EXEC(UAM_ERROR_NONE, __uam_table_appsinfo_prepare(database_handle), handle_error);
+
+       if (_uam_db_get_max_app_num() != UAM_ERROR_NONE)
+               goto handle_error;
+
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+
+handle_error:
+       _uam_app_db_deinitialize();
+       FUNC_EXIT;
+       return UAM_ERROR_DB_FAILED;
+}
+
+int _uam_app_db_deinitialize(void)
+{
+       FUNC_ENTRY;
+
+       retv_if(NULL == database_handle, UAM_ERROR_NONE);
+
+       __uam_table_appsinfo_finalize();
+
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+}
+
+int _uam_db_insert_app_info(unsigned int *app_num, const char *app_key)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = insert_app_info;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(NULL == app_num, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("%d-%s", *app_num, app_key);
+
+       DB_ACTION(sqlite3_bind_text(stmt, 1, app_key, -1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to insert app info [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+
+       if (_uam_db_get_max_app_num() != UAM_ERROR_NONE)
+               goto handle_error;
+
+       *app_num = max_app_num;
+       UAM_DBG("App info inserted [%d]", *app_num);
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+int _uam_db_delete_by_app_num(int app_num)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = delete_app_info;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("AppId: %d", app_num);
+
+       DB_ACTION(sqlite3_bind_int(stmt, 1, app_num), error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to delete app info [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+       } else
+               UAM_DBG("App info deleted");
+
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+int _uam_db_get_app(int app_num, db_app_info_t *info)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = select_app_data;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(!info, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("AppId: %d", app_num);
+
+       DB_ACTION(sqlite3_bind_int(stmt, 1, app_num), error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to select app info [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+       } else {
+               info = g_new0(db_app_info_t, 1);
+               info->app_num = sqlite3_column_int(stmt, 0);
+               g_strlcpy(info->app_key, (char *)sqlite3_column_text(stmt, 1),
+                       UAM_APP_KEY_MAX_STRING_LEN);
+               UAM_INFO("%d-%s", info->app_num, info->app_key);
+               UAM_DBG("App info found");
+       }
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
+GSList *_uam_db_get_all_apps(void)
+{
+       FUNC_ENTRY;
+       sqlite3_stmt *stmt = select_all_apps_data;
+       GSList *app_list = NULL;
+       db_app_info_t *info = NULL;
+       int sql_ret = SQLITE_OK;
+
+       do {
+               sql_ret = sqlite3_step(stmt);
+
+               switch (sql_ret) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       info = g_new0(db_app_info_t, 1);
+                       info->app_num = sqlite3_column_int(stmt, 0);
+                       g_strlcpy(info->app_key, (char *)sqlite3_column_text(stmt, 1),
+                               UAM_APP_KEY_MAX_STRING_LEN);
+
+                       UAM_INFO("%d-%s", info->app_num, info->app_key);
+                       app_list = g_slist_append(app_list, info);
+                       break;
+
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate app info [%d:%s]",
+                               sql_ret, sqlite3_errmsg(database_handle));
+                       }
+       } while (sql_ret == SQLITE_ROW);
+
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return app_list;
+}
+
+int _uam_app_db_clear(void)
+{
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = delete_all_apps_data;
+       int sql_ret = SQLITE_OK;
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to delete App data [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+       UAM_DBG("App data deleted ");
+
+handle_error:
+       sqlite3_reset(stmt);
+       return error_code;
+}
+
+int _uam_db_update_app_info(uam_db_app_info_t *app)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = update_app_info;
+       int sql_ret = SQLITE_OK;
+
+       retv_if(NULL == app, UAM_ERROR_INVALID_PARAMETER);
+
+       UAM_INFO("%s", app->app_key);
+
+       DB_ACTION(sqlite3_bind_text(stmt, 1, app->app_key, -1, SQLITE_TRANSIENT),
+               error_code, handle_error);
+
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret != SQLITE_DONE) {
+               UAM_ERR("Failed to update app info [%d:%s]",
+                       sql_ret, sqlite3_errmsg(database_handle));
+               error_code = UAM_ERROR_DB_FAILED;
+               goto handle_error;
+       }
+
+       UAM_DBG("App info updated");
+
+handle_error:
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
index 7acab7b..8e2622b 100644 (file)
@@ -29,31 +29,14 @@ static const char *request_string[] = {
 
 static GDBusConnection *g_dbus_conn = NULL;
 
-static GDBusConnection *__uam_manager_get_private_g_dbus_conn(void)
+static GDBusConnection *__uam_manager_get_shared_g_dbus_conn(void)
 {
        FUNC_ENTRY;
        GError *error = NULL;
-       char *address;
-       GDBusConnection *private_conn = NULL;
+       GDBusConnection *shared_conn = NULL;
 
-       address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
-       if (address == NULL) {
-               UAM_ERR("Failed to get bus address");
-               if (error) {
-                       UAM_ERR("Error: %s", error->message);
-                       g_clear_error(&error);
-               }
-               return NULL;
-       }
-
-       private_conn = g_dbus_connection_new_for_address_sync(address,
-                       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
-                       G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
-                       NULL, /* GDBusAuthObserver */
-                       NULL,
-                       &error);
-       g_free(address);
-       if (!private_conn) {
+       shared_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (!shared_conn) {
                UAM_ERR("Unable to connect to dbus");
                if (error) {
                        UAM_ERR("Error: %s", error->message);
@@ -63,7 +46,7 @@ static GDBusConnection *__uam_manager_get_private_g_dbus_conn(void)
        }
 
        FUNC_EXIT;
-       return private_conn;
+       return shared_conn;
 }
 
 GDBusConnection *_uam_manager_get_gdbus_conn(void)
@@ -72,13 +55,13 @@ GDBusConnection *_uam_manager_get_gdbus_conn(void)
 
        if (g_dbus_conn) {
                if (g_dbus_connection_is_closed(g_dbus_conn))
-                       g_dbus_conn = __uam_manager_get_private_g_dbus_conn();
+                       g_dbus_conn = __uam_manager_get_shared_g_dbus_conn();
 
                FUNC_EXIT;
                return g_dbus_conn;
        }
 
-       g_dbus_conn = __uam_manager_get_private_g_dbus_conn();
+       g_dbus_conn = __uam_manager_get_shared_g_dbus_conn();
        FUNC_EXIT;
        return g_dbus_conn;
 }
old mode 100644 (file)
new mode 100755 (executable)
index 149bb03..ffad383
 #include "ua-internal.h"
 #include "ua-manager-common.h"
 #include "ua-plugin-manager.h"
-
 #include "ua-manager-database.h"
 #include "ua-cloud-plugin-handler.h"
-
 #include "ua-vendor-plugin-manager.h"
 
 #define UAM_MAX_USERS 255
 #define USER_ACCOUNT_DEFAULT "default@default.com"
+#define USER_ACCOUNT_DEFAULT_SUFFIX "@default.com"
 #define USER_NAME_DEFAULT "default"
+#define UAM_APP_NUM_MAX_LEN 50
 
 typedef struct {
        char *name;
@@ -50,15 +50,78 @@ static GSList *devices; /* List of devices - uam_db_device_info_t */
 static GSList *services; /* List of services -  uam_db_service_info_t */
 static GSList *svc_devs; /* List of service device mapping -  uam_svc_dev_info_t */
 static GSList *payloads; /* List of payloads - uam_db_payload_info_t */
+static GSList *temp_devices; /* List of devices whichi will be temporary and \
+                               will be cleared in the end of addition \
+                               uam_db_device_info_t */
 
 static GSList *monitors; /* List of monitoring apps - uam_monitor_info_t */
 static GSList *scanners; /* List of scanning apps -  uam_scanner_info_t */
 
 static guint detection_timer = 0;
 static unsigned int detection_window;
-
 static unsigned int detecting_sensors = 0;
 
+/* Utility functions to print */
+static void __print_service(gpointer data, gpointer user_data)
+{
+       uam_db_service_info_t *service = data;
+       uam_db_tech_info_t *tech = user_data;
+
+       ret_if(NULL == tech);
+       ret_if(NULL == service);
+
+       UAM_DBG("DevId: %s, Type: %d, Svc: %s, Cycle: %d",
+                       tech->device->device_id, tech->tech_type,
+                       service->name, service->cycle);
+}
+
+static void __print_svc_dev(gpointer data, gpointer user_data)
+{
+       uam_svc_dev_info_t *svc_dev = data;
+
+       ret_if(NULL == svc_dev);
+
+       UAM_DBG("DevId: %s, Svc: %s, payload primary key: %d",
+                       svc_dev->device_id, svc_dev->service,
+                       svc_dev->payload->primary_key);
+}
+
+void __print_payload(const uam_ble_payload_s *payload)
+{
+       ret_if(NULL == payload);
+       int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
+
+       UAM_DBG("Payload primary key: [0x%2.2X], secondary_key: [0x%2.2X], " \
+               "device_type: [0x%2.2X] device uid len: [%d]",
+               payload->primary_key, payload->secondary_key, payload->device_type,
+               payload->device_uid_len);
+
+       for (int i = 0; i < payload->device_uid_len; i++)
+               UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
+
+       for (int i = 0; i < user_data_len; i++)
+               UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
+}
+
+void __print_db_payload(const uam_db_payload_info_t *payload)
+{
+       ret_if(NULL == payload);
+
+       int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
+
+       UAM_DBG("Payload primary key: [0x%2.2X], secondary_key: [0x%2.2X], " \
+               "device type: [0x%2.2X] device uid len: [%d]",
+               payload->primary_key, payload->secondary_key, payload->device_type,
+               payload->device_uid_len);
+
+       for (int i = 0; i < payload->device_uid_len; i++)
+               UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
+
+       for (int i = 0; i < user_data_len; i++)
+               UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
+}
+
+/* Utility functions to compare */
 static gint __compare_user_account(gconstpointer data, gconstpointer user_data)
 {
        const uam_db_user_info_t *user = data;
@@ -71,19 +134,40 @@ static gint __compare_user_account(gconstpointer data, gconstpointer user_data)
        return g_strcmp0(user->account, account);
 }
 
+static gint __compare_user(gconstpointer data, gconstpointer user_data)
+{
+       const uam_db_user_info_t *user = data;
+       const uam_db_user_info_t *temp_user = user_data;
+
+       retv_if(NULL == user, -1);
+       retv_if(NULL == user->account, -1);
+       retv_if(NULL == temp_user, -1);
+       retv_if(NULL == temp_user->account, -1);
+
+       UAM_DBG("user list app_num %d account %s " \
+                       "temp user app_num %d account %s",
+                       user->app_num, user->account,
+                       temp_user->app_num, temp_user->account);
+
+       if (user->app_num != temp_user->app_num)
+               return -1;
+
+       return g_strcmp0(user->account, temp_user->account);
+}
+
 static gint __compare_user_id(gconstpointer data, gconstpointer user_data)
 {
        const uam_db_user_info_t *user = data;
        const int *id = user_data;
 
-       UAM_INFO("[%d][%d]", *id, user->user_id);
-
        retv_if(NULL == user, -1);
        retv_if(NULL == user->account, -1);
        retv_if(NULL == id, -1);
 
+       UAM_INFO("[%d][%d]", *id, user->user_id);
+
        if (*id != user->user_id)
-               return 1;
+               return -1;
 
        return 0;
 }
@@ -100,34 +184,6 @@ static gint __compare_svc_name(gconstpointer data, gconstpointer user_data)
        return g_strcmp0(service->name, svc_name);
 }
 
-void __print_payload(const uam_ble_payload_s *payload)
-{
-       ret_if(NULL == payload);
-       int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
-       UAM_DBG("Payload primary key: [%d], secondary_key: [%d], device_icon: [%d] " \
-               "device uid len: [%d]",
-               payload->primary_key, payload->secondary_key, payload->device_icon,
-               payload->device_uid_len);
-       for (int i = 0; i < payload->device_uid_len; i++)
-               UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
-       for (int i = 0; i < user_data_len; i++)
-               UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
-}
-
-void __print_db_payload(const uam_db_payload_info_t *payload)
-{
-       ret_if(NULL == payload);
-       int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
-       UAM_DBG("Payload primary key: [%d], secondary_key: [%d], device_icon: [%d] " \
-               "device uid len: [%d]",
-               payload->primary_key, payload->secondary_key, payload->device_icon,
-               payload->device_uid_len);
-       for (int i = 0; i < payload->device_uid_len; i++)
-               UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
-       for (int i = 0; i < user_data_len; i++)
-               UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
-}
-
 static gint __compare_payload(gconstpointer data, gconstpointer user_data)
 {
        const uam_db_payload_info_t *db_payload = data;
@@ -149,6 +205,87 @@ static gint __compare_payload(gconstpointer data, gconstpointer user_data)
        return -1;
 }
 
+static gint __compare_device(gconstpointer data, gconstpointer user_data)
+{
+       FUNC_ENTRY;
+       const uam_db_device_info_t *device = data;
+       const uam_db_device_info_t *temp_device = user_data;
+
+       retv_if(NULL == device, -1);
+       retv_if(NULL == device->device_id, -1);
+       retv_if(NULL == temp_device, -1);
+       retv_if(device->app_num != temp_device->app_num, -1);
+
+       FUNC_EXIT;
+       return g_strcmp0(device->device_id, temp_device->device_id);
+}
+
+static gint __compare_device_id(gconstpointer data, gconstpointer user_data)
+{
+       FUNC_ENTRY;
+       const uam_db_device_info_t *device = data;
+       const char *dev_id = user_data;
+
+       retv_if(NULL == device, -1);
+       retv_if(NULL == device->device_id, -1);
+       retv_if(NULL == dev_id, -1);
+
+       FUNC_EXIT;
+       return g_strcmp0(device->device_id, dev_id);
+}
+
+static gint __compare_device_id_and_appnum(gconstpointer data, gconstpointer user_data)
+{
+       FUNC_ENTRY;
+       const uam_db_device_info_t *device = data;
+       const uam_device_info_s *dev_info = user_data;
+
+       retv_if(NULL == device, -1);
+       retv_if(NULL == device->device_id, -1);
+       retv_if(NULL == dev_info->device_id, -1);
+       retv_if(device->app_num != dev_info->app_num, -1);
+
+       FUNC_EXIT;
+       return g_strcmp0(device->device_id, dev_info->device_id);
+}
+
+static gint __compare_tech_type(gconstpointer data, gconstpointer user_data)
+{
+       FUNC_ENTRY;
+       const uam_db_tech_info_t *tech = data;
+       const int *type = user_data;
+
+       retv_if(NULL == tech, -1);
+       retv_if(NULL == type, -1);
+
+       FUNC_EXIT;
+       return ((*type == tech->tech_type) ? 0 : 1);
+}
+
+static gint __compare_app_key(gconstpointer data, gconstpointer user_data)
+{
+       const uam_sender_app_t *app_data = data;
+       const char *app_key = user_data;
+
+       retv_if(NULL == app_data, -1);
+       retv_if(NULL == app_data->app_key, -1);
+       retv_if(NULL == app_key, -1);
+
+       return g_strcmp0(app_data->app_key, app_key);
+}
+
+/* Utility functions to get pm info */
+unsigned int _uam_core_get_env_sensors()
+{
+       return _uam_pm_get_env_sensors();
+}
+
+unsigned int _uam_core_get_user_sensors()
+{
+       return _uam_pm_get_user_sensors();
+}
+
+/* Utility functions to free memory */
 static void __free_address_info(gpointer data)
 {
        FUNC_ENTRY;
@@ -192,112 +329,267 @@ static void __free_user_device(gpointer data)
 {
        FUNC_ENTRY;
        uam_db_device_info_t *device = data;
-
        ret_if(NULL == device);
-
        /* Remove device data from global device list */
        devices = g_slist_remove(devices, device);
 
        /* Free allocated memory */
        g_free(device->device_id);
+
        g_slist_free_full(device->tech_list, __free_dev_tech_info);
+
        g_free(device);
 
        FUNC_EXIT;
 }
 
-static void __send_device_event(int err, int event, const uam_device_info_s *dev_info)
+static void __free_user_info(gpointer data)
 {
        FUNC_ENTRY;
-       GVariant *param;
+       uam_db_user_info_t *user = data;
 
-       UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(event));
-       /* Send device event to application */
-       param = g_variant_new("(iiisss)",
-                       err,
-                       dev_info->operating_system,
-                       dev_info->type,
-                       dev_info->mac,
-                       dev_info->ipv4_addr,
-                       dev_info->device_id);
-       if (UAM_ERROR_NONE != _uam_manager_send_event(NULL, event, param))
-               UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event));
+       ret_if(NULL == user);
 
+       g_free(user->name);
+       g_free(user->account);
+       g_free(user);
        FUNC_EXIT;
 }
 
-static void __print_service(gpointer data, gpointer user_data)
+static void __free_service_info(gpointer data)
 {
        FUNC_ENTRY;
-       uam_db_service_info_t *service = data;
-       uam_db_tech_info_t *tech = user_data;
-
-       ret_if(NULL == tech);
-       ret_if(NULL == service);
+       uam_db_service_info_t *svc = data;
 
-       UAM_DBG("DevId: %s, Type: %d, Svc: %s, Cycle: %d",
-                       tech->device->device_id, tech->tech_type,
-                       service->name, service->cycle);
+       ret_if(NULL == svc);
 
+       g_free(svc->name);
+       g_free(svc);
        FUNC_EXIT;
 }
 
-static void __print_svc_dev(gpointer data, gpointer user_data)
+static void __free_monitor_info(gpointer data)
 {
        FUNC_ENTRY;
-       uam_svc_dev_info_t *svc_dev = data;
+       uam_monitor_info_t *monitor = data;
 
-       ret_if(NULL == svc_dev);
+       ret_if(NULL == monitor);
 
-       UAM_DBG("DevId: %s, Svc: %s, payload primary key: %d",
-                       svc_dev->device_id, svc_dev->service,
-                       svc_dev->payload->primary_key);
+       g_free(monitor->name);
+       g_free(monitor);
+       FUNC_EXIT;
+}
 
+static void __free_scanner_info(gpointer data)
+{
+       FUNC_ENTRY;
+       uam_scanner_info_t *scanner = data;
+
+       ret_if(NULL == scanner);
+
+       g_free(scanner->name);
+       g_free(scanner);
        FUNC_EXIT;
 }
 
-static void __add_service_to_dev_tech_mapping(
-               uam_db_tech_info_t *tech, uam_db_service_info_t *service)
+/* Utility functions to copy */
+static void __uam_core_copy_addr(uam_device_info_s *device, uam_db_address_info_t *addr)
+{
+       switch (addr->addr_type) {
+       case UAM_ADDR_TYPE_BLE:
+       case UAM_ADDR_TYPE_BT:
+       case UAM_ADDR_TYPE_P2P:
+       case UAM_ADDR_TYPE_WIFI:
+               g_strlcpy(device->mac, addr->address,
+                               UAM_MAC_ADDRESS_STRING_LEN);
+               break;
+       case UAM_ADDR_TYPE_IPv4:
+               g_strlcpy(device->ipv4_addr,
+                               addr->address,
+                               UAM_IP_ADDRESS_MAX_STRING_LEN);
+               break;
+       default:
+               UAM_WARN("Unknown address type %d", addr->addr_type);
+       }
+}
+
+static int __copy_tech_info_to_device_info(uam_db_tech_info_t *tech, uam_device_info_s *device)
 {
        FUNC_ENTRY;
+       GSList *l;
 
-       ret_if(NULL == tech);
-       ret_if(NULL == service);
+       retv_if(NULL == tech, UAM_ERROR_INVALID_PARAMETER);
 
-       tech->svc_list = g_slist_append(tech->svc_list, service);
-       service->dev_techs = g_slist_append(service->dev_techs, tech);
-       g_slist_foreach(tech->svc_list, __print_service, tech);
+       memset(device, 0x00, sizeof(uam_device_info_s));
+       for (l = tech->addresses; NULL != l; l = g_slist_next(l)) {
+               uam_db_address_info_t *addr = l->data;
+
+               if (!addr)
+                       continue;
+
+               __uam_core_copy_addr(device, addr);
+       }
+
+       device->operating_system = tech->os;  // make it display the tech->os as the device->os
+       g_strlcpy(device->device_id, tech->device->device_id,
+               UAM_DEVICE_ID_MAX_STRING_LEN);
+       device->type = tech->tech_type;
+       device->discriminant = tech->discriminant;
+       device->last_seen = tech->last_seen;
+       device->app_num = tech->device->app_num;
 
        FUNC_EXIT;
+       return UAM_ERROR_NONE;
 }
 
-static void __add_svc_dev_to_dev_tech_mapping(
-               uam_db_tech_info_t *tech, uam_svc_dev_info_t *svc_dev)
+static void __uam_copy_db_payload_info(uam_ble_payload_s *dst_payload,
+       uam_db_payload_info_t *src_payload)
 {
        FUNC_ENTRY;
 
-       ret_if(NULL == tech);
-       ret_if(NULL == svc_dev);
+       int user_data_len = 0;
+       ret_if(NULL == src_payload);
 
-       tech->svc_dev_list = g_slist_append(tech->svc_dev_list, svc_dev);
-       g_slist_foreach(tech->svc_dev_list, __print_svc_dev, NULL);
+       dst_payload->primary_key = src_payload->primary_key;
+       dst_payload->device_type = src_payload->device_type;
+       dst_payload->secondary_key = src_payload->secondary_key;
+       dst_payload->ext_val1 = src_payload->ext_val1;
+       dst_payload->ext_val2 = src_payload->ext_val2;
+       dst_payload->device_uid_len = src_payload->device_uid_len;
+
+       memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
+       if (src_payload->device_uid)
+               memcpy(dst_payload->device_uid,
+               src_payload->device_uid, src_payload->device_uid_len);
+
+       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
+       ret_if(0 == user_data_len);
+       memset(dst_payload->user_data, 0, user_data_len);
+       if (src_payload->user_data)
+               memcpy(dst_payload->user_data,
+               src_payload->user_data, user_data_len);
 
        FUNC_EXIT;
+       return;
 }
 
-static void __remove_service_to_dev_tech_mapping(
-               uam_db_tech_info_t *tech, uam_db_service_info_t *service)
+static void __uam_copy_uam_payload_info(
+               uam_db_payload_info_t *dst_payload, uam_ble_payload_s *src_payload)
 {
        FUNC_ENTRY;
 
-       ret_if(NULL == tech);
-       ret_if(NULL == service);
+       int user_data_len = 0;
+       ret_if(NULL == src_payload);
 
-       tech->svc_list = g_slist_remove(tech->svc_list, service);
-       service->dev_techs = g_slist_remove(service->dev_techs, tech);
-       g_slist_foreach(tech->svc_list, __print_service, tech);
+       dst_payload->primary_key = src_payload->primary_key;
+       dst_payload->device_type = src_payload->device_type;
+       dst_payload->secondary_key = src_payload->secondary_key;
+       dst_payload->ext_val1 = src_payload->ext_val1;
+       dst_payload->ext_val2 = src_payload->ext_val2;
+       dst_payload->device_uid_len = src_payload->device_uid_len;
+       dst_payload->device_uid = g_memdup(&(src_payload->device_uid),
+                               src_payload->device_uid_len);
+
+       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
+       ret_if(0 == user_data_len);
+       dst_payload->user_data = g_memdup(&(src_payload->user_data),
+                               user_data_len);
 
        FUNC_EXIT;
+       return;
+}
+
+static void __uam_copy_uam_db_service_info(
+               uam_service_info_s *dst, uam_db_service_info_t *src)
+{
+       FUNC_ENTRY;
+       ret_if(NULL == src);
+       ret_if(NULL == dst);
+
+       memset(dst, 0x00, sizeof(uam_service_info_s));
+       g_strlcpy(dst->name, src->name, UAM_SERVICE_MAX_STRING_LEN);
+       dst->presence_threshold = src->presence_threshold;
+       dst->absence_threshold = src->absence_threshold;
+       dst->app_num = src->app_num;
+
+       return;
+}
+
+static void __uam_copy_uam_service_info(
+               uam_db_service_info_t *dst, uam_service_info_s *src)
+{
+       FUNC_ENTRY;
+       ret_if(NULL == src);
+       ret_if(NULL == dst);
+
+       dst->name = g_strdup(src->name);
+       dst->presence_threshold = src->presence_threshold;
+       dst->absence_threshold = src->absence_threshold;
+       dst->app_num = src->app_num;
+
+       return;
+}
+
+static void __uam_copy_db_service_info(
+               uam_db_service_info_t *dst, db_service_info_t *src)
+{
+       FUNC_ENTRY;
+       ret_if(NULL == src);
+       ret_if(NULL == dst);
+
+       dst->name = g_strdup(src->service_name);
+       dst->presence_threshold = src->presence_threshold;
+       dst->absence_threshold = src->absence_threshold;
+       dst->app_num = src->app_num;
+       dst->cycle = src->cycle;
+
+       return;
+}
+
+/* Utility functions to search */
+GSList* __search_user(const char *account, int app_num)
+{
+       /* Search user in existing user list */
+       GSList *l;
+       uam_db_user_info_t *temp_user;
+
+       temp_user = g_new0(uam_db_user_info_t, 1);
+       temp_user->account = g_strdup(account);
+       temp_user->app_num = app_num;
+
+       l = g_slist_find_custom(users, temp_user, __compare_user);
+
+       free_n_null(&(temp_user->account));
+       free_n_null(&temp_user);
+
+       return l;
+}
+
+GSList* __search_device(const char *device_id, int app_num)
+{
+       /* Search device in existing device list */
+       GSList *l;
+       uam_db_device_info_t *temp_dev;
+
+       temp_dev = g_new0(uam_db_device_info_t, 1);
+       temp_dev->device_id = g_strdup(device_id);
+       temp_dev->app_num = app_num;
+
+       l = g_slist_find_custom(devices, temp_dev, __compare_device);
+       free_n_null(&(temp_dev->device_id));
+       free_n_null(&temp_dev);
+
+       return l;
+}
+
+static gboolean __is_mac_addr(uam_addr_type_e addr_type)
+{
+       if (addr_type == UAM_ADDR_TYPE_BT ||
+               addr_type == UAM_ADDR_TYPE_BLE ||
+               addr_type == UAM_ADDR_TYPE_WIFI ||
+               addr_type == UAM_ADDR_TYPE_P2P)
+               return 1;
+       return 0;
 }
 
 static char *__get_mac_addr(uam_db_tech_info_t *tech)
@@ -313,10 +605,7 @@ static char *__get_mac_addr(uam_db_tech_info_t *tech)
                if (!addr)
                        continue;
 
-               if (addr->addr_type == UAM_ADDR_TYPE_BT ||
-                               addr->addr_type == UAM_ADDR_TYPE_BLE ||
-                               addr->addr_type == UAM_ADDR_TYPE_WIFI ||
-                               addr->addr_type == UAM_ADDR_TYPE_P2P)
+               if (__is_mac_addr(addr->addr_type))
                        return addr->address;
        }
 
@@ -324,52 +613,145 @@ static char *__get_mac_addr(uam_db_tech_info_t *tech)
        return NULL;
 }
 
-static void __uam_core_copy_addr(uam_device_info_s *device, uam_db_address_info_t *addr)
+static uam_monitor_info_t *__uam_find_monitor(GSList *monitor_list,
+               const char *name, const char *svc_name, uam_pm_detection_mode_e mode)
 {
-       switch (addr->addr_type) {
-       case UAM_ADDR_TYPE_BLE:
-       case UAM_ADDR_TYPE_BT:
-       case UAM_ADDR_TYPE_P2P:
-       case UAM_ADDR_TYPE_WIFI:
-               g_strlcpy(device->mac, addr->address,
-                               UAM_MAC_ADDRESS_STRING_LEN);
-               break;
-       case UAM_ADDR_TYPE_IPv4:
-               g_strlcpy(device->ipv4_addr,
-                               addr->address,
-                               UAM_IP_ADDRESS_MAX_STRING_LEN);
-               break;
+//     FUNC_ENTRY;
+       GSList *l;
+
+       retv_if(NULL == name, NULL);
+       retv_if(NULL == monitor_list, NULL);
+
+       for (l = monitor_list; NULL != l; l = g_slist_next(l)) {
+               uam_monitor_info_t *monitor = l->data;
+
+               if (!monitor || !monitor->name ||
+                               !monitor->service || !monitor->service->name)
+                       continue;
+
+               if ((mode == monitor->mode) &&
+                               (0 == g_strcmp0(monitor->name, name)) &&
+                               (0 == g_strcmp0(monitor->service->name, svc_name))) {
+                       UAM_DBG("Monitoring application found in list");
+                       return monitor;
+               }
+       }
+
+//     FUNC_EXIT;
+       return NULL;
+}
+
+/* Utility functions to convert info */
+static unsigned int __uam_core_tech_type_to_addr_type(uam_tech_type_e tech_type)
+{
+       switch (tech_type) {
+       case UAM_TECH_TYPE_BLE:
+               return UAM_ADDR_TYPE_BLE;
+       case UAM_TECH_TYPE_BT:
+               return UAM_ADDR_TYPE_BT;
+       case UAM_TECH_TYPE_P2P:
+               return UAM_ADDR_TYPE_P2P;
+       case UAM_TECH_TYPE_WIFI:
+               return UAM_ADDR_TYPE_WIFI;
        default:
-               UAM_WARN("Unknown address type %d", addr->addr_type);
+               UAM_ERR("Unknown tech type: %d", tech_type);
+               return 0;
        }
 }
 
-static int __copy_tech_info_to_device_info(uam_db_tech_info_t *tech, uam_device_info_s *device)
+static unsigned int __uam_core_sensor_to_tech_type(unsigned int sensor)
+{
+       switch (sensor) {
+       case UAM_SENSOR_BITMASK_BLE:
+               return UAM_TECH_TYPE_BLE;
+       case UAM_SENSOR_BITMASK_WIFI:
+               return UAM_TECH_TYPE_WIFI;
+       default:
+               UAM_ERR("Unknown sensor: %d", sensor);
+               return 0;
+       }
+}
+
+static unsigned int __uam_core_tech_type_to_sensor(unsigned int tech_type)
+{
+       switch (tech_type) {
+       case UAM_TECH_TYPE_BLE:
+               return UAM_SENSOR_BITMASK_BLE;
+       case UAM_TECH_TYPE_WIFI:
+               return UAM_SENSOR_BITMASK_WIFI;
+       default:
+               UAM_ERR("Unknown tech type: %d", tech_type);
+               return 0;
+       }
+}
+
+/* Other Utility functions */
+static void __send_device_event(int err, int event, const uam_device_info_s *dev_info)
 {
        FUNC_ENTRY;
-       GSList *l;
+       char *dest = NULL;
+       GVariant *param;
 
-       retv_if(NULL == tech, UAM_ERROR_INVALID_PARAMETER);
+       UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(event));
+       /* Send device event to application */
+       param = g_variant_new("(iiisss)",
+                       err,
+                       dev_info->operating_system,
+                       dev_info->type,
+                       dev_info->mac,
+                       dev_info->ipv4_addr,
+                       dev_info->device_id);
 
-       memset(device, 0x00, sizeof(uam_device_info_s));
-       for (l = tech->addresses; NULL != l; l = g_slist_next(l)) {
-               uam_db_address_info_t *addr = l->data;
+       _uam_get_sender_from_app_num(dev_info->app_num, &dest);
 
-               if (!addr)
-                       continue;
+       if (UAM_ERROR_NONE != _uam_manager_send_event(dest, event, param))
+               UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event));
 
-               __uam_core_copy_addr(device, addr);
-       }
+       FUNC_EXIT;
+}
 
-       device->operating_system = tech->device->os;
-       g_strlcpy(device->device_id, tech->device->device_id,
-               UAM_DEVICE_ID_MAX_STRING_LEN);
-       device->type = tech->tech_type;
-       device->discriminant = tech->discriminant;
-       device->last_seen = tech->last_seen;
+static void __add_service_to_dev_tech_mapping(
+               uam_db_tech_info_t *tech, uam_db_service_info_t *service)
+{
+       FUNC_ENTRY;
+
+       ret_if(NULL == tech);
+       ret_if(NULL == service);
+
+       tech->svc_list = g_slist_append(tech->svc_list, service);
+       service->dev_techs = g_slist_append(service->dev_techs, tech);
+       g_slist_foreach(tech->svc_list, __print_service, tech);
+
+       FUNC_EXIT;
+}
+
+static void __add_svc_dev_to_dev_tech_mapping(
+               uam_db_tech_info_t *tech, uam_svc_dev_info_t *svc_dev)
+{
+       FUNC_ENTRY;
+
+       ret_if(NULL == tech);
+       ret_if(NULL == svc_dev);
+
+       tech->svc_dev_list = g_slist_append(tech->svc_dev_list, svc_dev);
+       g_slist_foreach(tech->svc_dev_list, __print_svc_dev, NULL);
+
+       FUNC_EXIT;
+}
+
+static void __remove_service_to_dev_tech_mapping(
+               uam_db_tech_info_t *tech, uam_db_service_info_t *service)
+{
+       FUNC_ENTRY;
+
+       ret_if(NULL == tech);
+       ret_if(NULL == service);
+
+       tech->svc_list = g_slist_remove(tech->svc_list, service);
+       service->dev_techs = g_slist_remove(service->dev_techs, tech);
+       g_slist_foreach(tech->svc_list, __print_service, tech);
 
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
 }
 
 static int _uam_remove_user_device(uam_db_device_info_t *device)
@@ -409,7 +791,7 @@ static int _uam_remove_user_device(uam_db_device_info_t *device)
 
                /* Remove device from database */
                ret = _uam_device_db_delete_device_info(
-                                       dev_info.device_id, dev_info.type, dev_info.mac);
+                                       dev_info.device_id, dev_info.type, dev_info.mac, dev_info.app_num);
                if (UAM_ERROR_NONE != ret) {
                        UAM_ERR("_uam_device_db_delete_device_info failed");
                        __uam_db_end_transaction(0);
@@ -422,12 +804,14 @@ static int _uam_remove_user_device(uam_db_device_info_t *device)
        return ret;
 }
 
-static int __free_uam_db_user_info(gpointer data)
+static int __remove_uam_db_user_info(gpointer data)
 {
        FUNC_ENTRY;
-       uam_db_user_info_t *user = data;
+       uam_db_user_info_t **ptr = data;
+       uam_db_user_info_t *user = *ptr;
        GSList *l;
        int ret = UAM_ERROR_NONE;
+       unsigned int bitmask;
 
        for (l = user->devices; NULL != l; l = g_slist_next(l)) {
                uam_db_device_info_t *device = l->data;
@@ -440,49 +824,24 @@ static int __free_uam_db_user_info(gpointer data)
 
        g_slist_free_full(user->devices, __free_user_device);
 
-       /* Set/update registered device list to plugins */
-       if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices))
-               UAM_ERR("_uam_pm_set_registered_devices failed");
-
-       /* Set/update registered device list to cloud plugin */
-       _uam_cloud_update_registered_devices();
-
-       g_free(user->name);
-       g_free(user->account);
-       g_free(user);
-
-       FUNC_EXIT;
-       return ret;
-}
-
-static gint __compare_device_id(gconstpointer data, gconstpointer user_data)
-{
-       FUNC_ENTRY;
-       const uam_db_device_info_t *device = data;
-       const char *dev_id = user_data;
-
-       retv_if(NULL == device, -1);
-       retv_if(NULL == device->device_id, -1);
-       retv_if(NULL == dev_id, -1);
+       bitmask = _uam_core_get_user_sensors();
 
-       FUNC_EXIT;
-       return g_strcmp0(device->device_id, dev_id);
-}
+       /* Set/update registered device list to plugins */
+       if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices, bitmask))
+               UAM_ERR("_uam_pm_set_registered_devices failed");
 
-static gint __compare_tech_type(gconstpointer data, gconstpointer user_data)
-{
-       FUNC_ENTRY;
-       const uam_db_tech_info_t *tech = data;
-       const int *type = user_data;
+       /* Set/update registered device list to cloud plugin */
+       _uam_cloud_update_registered_devices();
 
-       retv_if(NULL == tech, -1);
-       retv_if(NULL == type, -1);
+       __free_user_info(user);
+       *ptr = NULL;
 
        FUNC_EXIT;
-       return ((*type == tech->tech_type) ? 0 : 1);
+       return ret;
 }
 
-static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac)
+static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac,
+               const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -491,7 +850,7 @@ static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac)
                uam_db_device_info_t *dev = l->data;
                GSList *l1;
 
-               if (!dev)
+               if (!dev || dev->app_num != app_num)
                        continue;
 
                for (l1 = dev->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
@@ -507,11 +866,8 @@ static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac)
                                if (!addr)
                                        continue;
 
-                               if (UAM_ADDR_TYPE_BLE == addr->addr_type ||
-                                               UAM_ADDR_TYPE_BT == addr->addr_type ||
-                                               UAM_ADDR_TYPE_P2P == addr->addr_type ||
-                                               UAM_ADDR_TYPE_WIFI == addr->addr_type)
-                                       if (!strcasecmp(addr->address, mac)) {
+                               if (__is_mac_addr(addr->addr_type) &&
+                                               !strcasecmp(addr->address, mac)) {
                                                UAM_DBG("Device found Mac: %s, type: %d",
                                                                addr->address, addr->addr_type);
                                                return tech;
@@ -524,8 +880,81 @@ static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac)
        return NULL;
 }
 
+static void __get_default_service_name(int app_num, gchar **dest)
+{
+       gchar buf[UAM_APP_NUM_MAX_LEN];
+       memset(buf, 0x00, sizeof(buf));
+       g_snprintf(buf, UAM_APP_NUM_MAX_LEN, "%d", app_num);
+       *dest = g_strjoin(".", UAM_SERVICE_DEFAULT, buf, NULL);
+}
+
+static void __get_default_user(int app_num, gchar **account, gchar **user_name)
+{
+       gchar buf[UAM_APP_NUM_MAX_LEN];
+       memset(buf, 0x00, sizeof(buf));
+       g_snprintf(buf, UAM_APP_NUM_MAX_LEN, "%d", app_num);
+
+       if (user_name != NULL) {
+               *user_name = g_strjoin(".", USER_NAME_DEFAULT, buf, NULL);
+               UAM_INFO("Default username: [%s]", *user_name);
+               if (account != NULL) {
+                       *account = g_strjoin(NULL, *user_name, USER_ACCOUNT_DEFAULT_SUFFIX, NULL);
+                       UAM_INFO("Default account: [%s]", *account);
+               }
+       }
+}
+
+/* Give count of devices matching given details. Mac would be compared if
+ * non-null, otherwise upto tech_type would be matched.
+ */
+static void _uam_device_match_count(const char *device_id, int tech_type,
+       const char *mac, int *count)
+{
+       FUNC_ENTRY;
+       GSList *l;
+       uam_db_device_info_t *device;
+       uam_db_tech_info_t *tech;
+
+       ret_if(NULL == count);
+       *count = 0;
+       ret_if(NULL == device_id);
+
+       for (l = devices; l; l = g_slist_next(l)) {
+               device = l->data;
+               if (!device || !(device->supported_techs & tech_type) ||
+                               g_strcmp0(device->device_id, device_id))
+                       continue;
+
+               if (!mac) {
+                       (*count)++;
+                       continue;
+               }
+
+               GSList *l1;
+               for (l1 = device->tech_list; l1; l1 = g_slist_next(l1)) {
+                       tech = l1->data;
+                       GSList *l2;
+                       if (!tech)
+                               continue;
+
+                       for (l2 = tech->addresses; l2; l2 = g_slist_next(l2)) {
+                               uam_db_address_info_t *addr = l2->data;
+                               if (!addr)
+                                       continue;
+
+                               if (__is_mac_addr(addr->addr_type) &&
+                                               !strcasecmp(addr->address, mac))
+                                               (*count)++;
+                       }
+               }
+       }
+       UAM_DBG("device match count [%d]", *count);
+       FUNC_EXIT;
+}
+
 static int __get_uam_db_dev_list_to_uam_dev_list(
-               GSList *db_dev_list, uam_device_info_s **device_list, int *count)
+               GSList *db_dev_list, uam_device_info_s **device_list,
+               int *count, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -533,12 +962,13 @@ static int __get_uam_db_dev_list_to_uam_dev_list(
        int ret = UAM_ERROR_NONE;
 
        *count = 0;
+
        /* Calculate num devices first */
        for (l = db_dev_list; NULL != l; l = g_slist_next(l)) {
                uam_db_device_info_t *db_info = l->data;
                GSList *l1;
 
-               if (!db_info || !db_info->device_id)
+               if (!db_info || !db_info->device_id || db_info->app_num != app_num)
                        continue;
 
                for (l1 = db_info->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
@@ -550,6 +980,7 @@ static int __get_uam_db_dev_list_to_uam_dev_list(
                        (*count)++;
                }
        }
+
        UAM_INFO("Count = %d", *count);
 
        *device_list = g_new0(uam_device_info_s, *count);
@@ -559,7 +990,7 @@ static int __get_uam_db_dev_list_to_uam_dev_list(
                uam_db_device_info_t *db_info = l->data;
                GSList *l1;
 
-               if (!db_info || !db_info->device_id)
+               if (!db_info || !db_info->device_id || db_info->app_num != app_num)
                        continue;
 
                for (l1 = db_info->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
@@ -583,35 +1014,29 @@ static int __get_uam_db_dev_list_to_uam_dev_list(
        return ret;
 }
 
-static uam_monitor_info_t *__uam_find_monitor(GSList *monitor_list,
-               const char *name, const char *svc_name, uam_pm_detection_mode_e mode)
+unsigned int _uam_core_get_active_sensors(int detection_mode)
 {
-//     FUNC_ENTRY;
+       FUNC_ENTRY;
+       unsigned int sensors = 0;
        GSList *l;
 
-       retv_if(NULL == name, NULL);
-       retv_if(NULL == monitor_list, NULL);
+       retv_if((UAM_DETECT_PRESENCE != detection_mode) &&
+                       (UAM_DETECT_ABSENCE != detection_mode), 0);
 
-       for (l = monitor_list; NULL != l; l = g_slist_next(l)) {
+       for (l = monitors; NULL != l; l = g_slist_next(l)) {
                uam_monitor_info_t *monitor = l->data;
 
-               if (!monitor || !monitor->name ||
-                               !monitor->service || !monitor->service->name)
+               if (!monitor || !monitor->name || (detection_mode != monitor->mode))
                        continue;
 
-               if ((mode == monitor->mode) &&
-                               (0 == g_strcmp0(monitor->name, name)) &&
-                               (0 == g_strcmp0(monitor->service->name, svc_name))) {
-                       UAM_DBG("Monitoring application found in list");
-                       return monitor;
-               }
+               sensors |= monitor->sensors;
        }
 
-//     FUNC_EXIT;
-       return NULL;
+       FUNC_EXIT;
+       return sensors;
 }
 
-unsigned int _uam_core_get_active_sensors(int detection_mode)
+unsigned int _uam_core_get_active_env_sensors(int detection_mode)
 {
        FUNC_ENTRY;
        unsigned int sensors = 0;
@@ -626,45 +1051,76 @@ unsigned int _uam_core_get_active_sensors(int detection_mode)
                if (!monitor || !monitor->name || (detection_mode != monitor->mode))
                        continue;
 
-               sensors |= monitor->sensors;
+               sensors |= monitor->sensors & _uam_core_get_env_sensors();
        }
 
        FUNC_EXIT;
        return sensors;
 }
 
-unsigned int _uam_core_get_env_sensors()
+static GSList *_uam_core_find_svc_dev_list(uam_device_info_s *dev_info)
 {
        FUNC_ENTRY;
-       unsigned int sensors = 0;
+       uam_svc_dev_info_t *svc_dev = NULL;
+       GSList *l;
+       GSList *svc_dev_list = NULL;
+
+       retv_if(NULL == dev_info, NULL);
+
+       /*
+        * Iterate over the svc_devs and find each service device information
+        */
+       for (l = svc_devs; NULL != l; l = g_slist_next(l)) {
+               svc_dev = l->data;
+
+               if (!svc_dev || !svc_dev->device_id || !svc_dev->service)
+                       continue;
 
-       sensors |= UAM_SENSOR_BITMASK_MOTION;
-       sensors |= UAM_SENSOR_BITMASK_LIGHT;
+               if ((0 == g_strcmp0(svc_dev->device_id, dev_info->device_id)) &&
+                               (svc_dev->tech_type == dev_info->type)) {
+                       svc_dev_list = g_slist_append(svc_dev_list, svc_dev);
+                       UAM_DBG("Service %s found for device in list",
+                                                       svc_dev->service);
+               }
+       }
 
        FUNC_EXIT;
-       return sensors;
+       return svc_dev_list;
 }
 
-unsigned int _uam_core_get_active_env_sensors(int detection_mode)
+void _uam_core_get_app_num_from_app_key(const char* app_key, int *app_num)
 {
-       FUNC_ENTRY;
-       unsigned int sensors = 0;
-       GSList *l;
+       GSList *l = NULL;
+       uam_sender_app_t *sender_app = NULL;
+       GSList *sender_apps = *(_uam_manager_get_sender_apps());
 
-       retv_if((UAM_DETECT_PRESENCE != detection_mode) &&
-                       (UAM_DETECT_ABSENCE != detection_mode), 0);
+       l = g_slist_find_custom(sender_apps, app_key, __compare_app_key);
 
-       for (l = monitors; NULL != l; l = g_slist_next(l)) {
-               uam_monitor_info_t *monitor = l->data;
+       if (l == NULL) {
+               UAM_DBG("App key not found in the list");
+               return;
+       }
+       sender_app = l->data;
+       *app_num = sender_app->app_num;
+       UAM_DBG("app_num is [%d]", *app_num);
+}
 
-               if (!monitor || !monitor->name || (detection_mode != monitor->mode))
-                       continue;
+int __uam_core_get_app_num_from_sec_list_devices(const uam_device_info_s *dev_info)
+{
+       FUNC_ENTRY;
+       uam_db_device_info_t *temp_device;
+       GSList *l = NULL;
 
-               sensors |= monitor->sensors & _uam_core_get_env_sensors();
-       }
+       retv_if(NULL == dev_info, 0);
 
+       l = g_slist_find_custom(temp_devices, dev_info->device_id, __compare_device_id);
+       if (NULL != l) {
+               temp_device = l->data;
+               return temp_device->app_num;
+       } else {
+               return 0;
+       }
        FUNC_EXIT;
-       return sensors;
 }
 
 static GSList *__convert_db_svc_list_to_uam_svc_list(GSList *db_svc_list)
@@ -692,8 +1148,7 @@ static GSList *__convert_db_svc_list_to_uam_svc_list(GSList *db_svc_list)
                                db_svc->service_name, __compare_svc_name);
                if (!l1) {
                        service = g_new0(uam_db_service_info_t, 1);
-                       service->name = g_strdup(db_svc->service_name);
-                       service->cycle = db_svc->cycle;
+                       __uam_copy_db_service_info(service, db_svc);
                        services = g_slist_append(services, service);
                } else
                        service = l1->data;
@@ -705,92 +1160,61 @@ static GSList *__convert_db_svc_list_to_uam_svc_list(GSList *db_svc_list)
        return svc_list;
 }
 
-static void __uam_copy_db_payload_info(uam_ble_payload_s *dst_payload,
-       uam_db_payload_info_t *src_payload)
+static void __uam_core_add_dev_to_sec_list_devices(const uam_device_info_s *dev_info)
 {
        FUNC_ENTRY;
+       uam_db_device_info_t *temp_device;
+       GSList *l;
 
-       int user_data_len = 0;
-       ret_if(NULL == src_payload);
-
-       dst_payload->primary_key = src_payload->primary_key;
-       dst_payload->device_icon = src_payload->device_icon;
-       dst_payload->secondary_key = src_payload->secondary_key;
-       dst_payload->device_uid_len = src_payload->device_uid_len;
+       ret_if(NULL == dev_info);
 
-       memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
-       memset(dst_payload->bt_mac, 0, UAM_BT_MAC_ADDRESS_STRING_LEN);
-       if (src_payload->device_uid)
-               memcpy(dst_payload->device_uid,
-               src_payload->device_uid, src_payload->device_uid_len);
-       if (src_payload->bt_mac)
-               memcpy(dst_payload->bt_mac,
-               src_payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN);
+       /* create a new entry in list if the app_num is different */
+       l = g_slist_find_custom(temp_devices, dev_info, __compare_device_id_and_appnum);
+       if (NULL != l) {
+               temp_device = l->data;
+       } else {
+               temp_device = g_new0(uam_db_device_info_t, 1);
+               temp_device->device_id = g_strdup(dev_info->device_id);
+               temp_device->os = dev_info->operating_system;
+               temp_device->discriminant = dev_info->discriminant;
+               temp_device->app_num = dev_info->app_num;
 
-       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
-       ret_if(0 == user_data_len);
-       memset(dst_payload->user_data, 0, user_data_len);
-       if (src_payload->user_data)
-               memcpy(dst_payload->user_data,
-               src_payload->user_data, user_data_len);
+               /* Add device to global temp_devices list */
+               temp_devices = g_slist_prepend(temp_devices, temp_device);
+       }
+       temp_device->supported_techs |= dev_info->type;
 
        FUNC_EXIT;
-       return;
 }
 
-static void __uam_copy_uam_payload_info(
-               uam_db_payload_info_t *dst_payload, uam_ble_payload_s *src_payload)
+static void __uam_core_remove_dev_from_sec_list_devices(
+                       const uam_device_info_s *dev_info)
 {
        FUNC_ENTRY;
+       uam_db_device_info_t *temp_device;
+       GSList *l = NULL;
 
-       int user_data_len = 0;
-       ret_if(NULL == src_payload);
-
-       dst_payload->primary_key = src_payload->primary_key;
-       dst_payload->device_icon = src_payload->device_icon;
-       dst_payload->secondary_key = src_payload->secondary_key;
-       dst_payload->device_uid_len = src_payload->device_uid_len;
-       dst_payload->device_uid = g_memdup(&(src_payload->device_uid),
-                               src_payload->device_uid_len);
-       dst_payload->bt_mac = g_memdup(&(src_payload->bt_mac),
-                               UAM_BT_MAC_ADDRESS_STRING_LEN);
-
-       user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
-       ret_if(0 == user_data_len);
-       dst_payload->user_data = g_memdup(&(src_payload->user_data),
-                               user_data_len);
-
-       FUNC_EXIT;
-       return;
-}
-
-static GSList *_uam_core_find_svc_dev_list(uam_device_info_s *dev_info)
-{
-       FUNC_ENTRY;
-       uam_svc_dev_info_t *svc_dev = NULL;
-       GSList *l;
-       GSList *svc_dev_list = NULL;
+       ret_if(NULL == dev_info);
 
-       retv_if(NULL == dev_info, NULL);
+       l = g_slist_find_custom(temp_devices, dev_info->device_id,
+                                               __compare_device_id);
+       if (NULL != l) {
+               temp_device = l->data;
 
-       /*
-        * Iterate over the svc_devs and find each service device information
-        */
-       for (l = svc_devs; NULL != l; l = g_slist_next(l)) {
-               svc_dev = l->data;
+               /* update device */
+               temp_device->supported_techs &= ~(dev_info->type);
 
-               if (!svc_dev || !svc_dev->device_id || !svc_dev->service)
-                       continue;
+               if (temp_device->supported_techs == 0) {
+                       /* Remove device from global temp_devices list */
+                       temp_devices = g_slist_remove(temp_devices, temp_device);
 
-               if ((0 == g_strcmp0(svc_dev->device_id, dev_info->device_id)) &&
-                               (svc_dev->tech_type == dev_info->type)) {
-                       svc_dev_list = g_slist_append(svc_dev_list, svc_dev);
-                       UAM_DBG("Service found for device in list");
+                       /* free memory of temp_device */
+                       g_free(temp_device->device_id);
+                       g_free(temp_device);
                }
        }
 
        FUNC_EXIT;
-       return svc_dev_list;
 }
 
 static void __uam_core_add_dev_to_list(
@@ -806,8 +1230,11 @@ static void __uam_core_add_dev_to_list(
        ret_if(NULL == dev_info);
        ret_if(NULL == user);
 
+       UAM_DBG("device-id %s", dev_info->device_id);
+
        l = g_slist_find_custom(devices, dev_info->device_id, __compare_device_id);
        if (NULL != l) {
+               UAM_DBG("The device already exists");
                device = l->data;
                ret_if(user != device->user);
                ret_if(device->supported_techs & dev_info->type);
@@ -815,8 +1242,9 @@ static void __uam_core_add_dev_to_list(
                if (device->os != dev_info->operating_system) {
                        UAM_INFO("device->os: %d, dev_info->operating_system: %d",
                                        device->os, dev_info->operating_system);
+
                        /* Update device OS type */
-                       if (UAM_OS_TYPE_INVALID == device->os || UAM_OS_TYPE_UNDEFINED == device->os)
+                       if (UAM_OS_TYPE_INVALID == device->os)
                                device->os = dev_info->operating_system;
                        else
                                UAM_WARN("Strange - OS types did not match, need to check");
@@ -827,6 +1255,7 @@ static void __uam_core_add_dev_to_list(
                device->os = dev_info->operating_system;
                device->discriminant = dev_info->discriminant;
                device->user = user;
+               device->app_num = dev_info->app_num;
 
                /* Add device to global device list */
                devices = g_slist_append(devices, device);
@@ -837,6 +1266,7 @@ static void __uam_core_add_dev_to_list(
 
        tech = g_new0(uam_db_tech_info_t, 1);
        tech->tech_type = dev_info->type;
+       tech->os = dev_info->operating_system;
        tech->presence_state = presence_state;
        tech->last_seen = last_seen;
        tech->device = device;
@@ -864,29 +1294,10 @@ static void __uam_core_add_dev_to_list(
                uam_db_address_info_t *addr;
 
                addr = g_new0(uam_db_address_info_t, 1);
-               addr->address = g_strdup(dev_info->mac);
-
-               switch (dev_info->type) {
-               case UAM_TECH_TYPE_BLE:
-                       addr->addr_type = UAM_ADDR_TYPE_BLE;
-                       break;
-               case UAM_TECH_TYPE_BT:
-                       addr->addr_type = UAM_ADDR_TYPE_BT;
-                       break;
-               case UAM_TECH_TYPE_P2P:
-                       addr->addr_type = UAM_ADDR_TYPE_P2P;
-                       break;
-               case UAM_TECH_TYPE_WIFI:
-                       addr->addr_type = UAM_ADDR_TYPE_WIFI;
-                       break;
-               default:
-                       UAM_ERR("Unknown tech type: %d", dev_info->type);
-                       g_free(addr->address);
-                       g_free(addr);
-                       addr = NULL;
-               }
-
                if (addr) {
+                       addr->address = g_strdup(dev_info->mac);
+                       addr->addr_type = __uam_core_tech_type_to_addr_type(dev_info->type);
+
                        UAM_DBG("MAC address %s added for tech type: %d",
                                        dev_info->mac, dev_info->type);
                        tech->addresses = g_slist_append(tech->addresses, addr);
@@ -896,13 +1307,8 @@ static void __uam_core_add_dev_to_list(
        if (0 < strlen(dev_info->ipv4_addr)) {
                uam_db_address_info_t *addr;
 
-               if (UAM_TECH_TYPE_BLE == dev_info->type ||
-                               UAM_TECH_TYPE_BT == dev_info->type)
-                       UAM_WARN("IPv4 address %s added for tech type: %d",
-                                       dev_info->ipv4_addr, dev_info->type);
-               else
-                       UAM_DBG("IPv4 address %s added for tech type: %d",
-                                       dev_info->ipv4_addr, dev_info->type);
+               UAM_DBG("IPv4 address %s added for tech type: %d",
+                               dev_info->ipv4_addr, dev_info->type);
 
                addr = g_new0(uam_db_address_info_t, 1);
                addr->addr_type = UAM_ADDR_TYPE_IPv4;
@@ -914,52 +1320,62 @@ static void __uam_core_add_dev_to_list(
        FUNC_EXIT;
 }
 
-int _uam_core_add_user(int *user_id, const char *account, const char *name)
+int _uam_core_add_user(int *user_id, const char *account, const char *name,
+               const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
        uam_db_user_info_t *user;
+       char *dest = NULL;
        int ret = UAM_ERROR_NONE;
 
-       l = g_slist_find_custom(users, account, __compare_user_account);
+       retv_if(!user_id, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(!account, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(!name, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
+
+       l = __search_user(account, app_num);
        retv_if((NULL != l) && (l->data != NULL), UAM_ERROR_ALREADY_REGISTERED);
 
        user = g_new0(uam_db_user_info_t, 1);
-
        /* Add user to database */
-       ret = _uam_db_insert_user_info(&(user->user_id), name, account);
+       ret = _uam_db_insert_user_info(&(user->user_id), name, account, app_num);
        if (UAM_ERROR_NONE != ret) {
+               __free_user_info(user);
                UAM_ERR("_uam_db_insert_user_info failed [%d]", ret);
-               g_free(user);
                return ret;
        }
 
        user->name = g_strdup(name);
        user->account = g_strdup(account);
+       user->app_num = app_num;
        user->devices = NULL;
+       users = g_slist_append(users, user);
 
        *user_id = user->user_id;
-
-       users = g_slist_append(users, user);
+       _uam_get_sender_from_app_num(app_num, &dest);
 
        /* Send user added event to application */
-       if (UAM_ERROR_NONE != _uam_manager_send_event(NULL,
+       if (UAM_ERROR_NONE != _uam_manager_send_event(dest,
                                UAM_EVENT_USER_ADDED, g_variant_new("(iss)",
-                                       UAM_ERROR_NONE, user->account, user->name)))
+                               UAM_ERROR_NONE, user->account, user->name)))
                UAM_ERR("Failed to send UAM_EVENT_USER_ADDED");
 
+       g_free(dest);
+
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_remove_user(const char *account)
+int _uam_core_remove_user(const char *account, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
        uam_db_user_info_t *user;
+       char *dest = NULL;
        int ret = UAM_ERROR_NONE;
 
-       l = g_slist_find_custom(users, account, __compare_user_account);
+       l = __search_user(account, app_num);
        retv_if((NULL == l), UAM_ERROR_NOT_REGISTERED);
        user = l->data;
 
@@ -971,14 +1387,15 @@ int _uam_core_remove_user(const char *account)
        }
 
        users = g_slist_remove(users, user);
+       _uam_get_sender_from_app_num(app_num, &dest);
 
        /* Send user removed event to application */
-       if (UAM_ERROR_NONE != _uam_manager_send_event(NULL,
+       if (UAM_ERROR_NONE != _uam_manager_send_event(dest,
                                UAM_EVENT_USER_REMOVED, g_variant_new("(iss)",
-                                       UAM_ERROR_NONE, user->account, user->name)))
+                               UAM_ERROR_NONE, user->account, user->name)))
                UAM_ERR("Failed to send UAM_EVENT_USER_REMOVED");
 
-       ret = __free_uam_db_user_info((gpointer)user);
+       ret = __remove_uam_db_user_info((gpointer)&user);
        if (UAM_ERROR_NONE != ret)
                UAM_WARN("_free_uam_db_user_info failed");
 
@@ -995,7 +1412,7 @@ int _uam_core_update_user(uam_user_info_s *user)
        retv_if(NULL == user, UAM_ERROR_INVALID_PARAMETER);
 
        /* Retrieve user from list */
-       l = g_slist_find_custom(users, user->account, __compare_user_account);
+       l = __search_user(user->account, user->app_num);
        retv_if((NULL == l) || (l->data == NULL), UAM_ERROR_NOT_REGISTERED);
 
        user_info = l->data;
@@ -1011,7 +1428,7 @@ int _uam_core_update_user(uam_user_info_s *user)
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_add_device(const char *account, const uam_device_info_s *dev_info)
+int _uam_core_add_device(const char *account, uam_device_info_s *dev_info)
 {
        FUNC_ENTRY;
        int ret;
@@ -1022,19 +1439,24 @@ int _uam_core_add_device(const char *account, const uam_device_info_s *dev_info)
        retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == dev_info, UAM_ERROR_INVALID_PARAMETER);
 
-       l = g_slist_find_custom(users, account, __compare_user_account);
-       retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
+       /* Search user to link with device */
+       l = __search_user(account, dev_info->app_num);
+       retv_if(l == NULL, UAM_ERROR_INVALID_PARAMETER);
        user = l->data;
 
-       l = g_slist_find_custom(devices, dev_info->device_id, __compare_device_id);
+       /* Search if device already exist */
+       l = __search_device(dev_info->device_id, dev_info->app_num);
        if (NULL != l) {
+               UAM_DBG("The device id was found in the current list");
                device = l->data;
                retv_if(user != device->user, UAM_ERROR_ALREADY_REGISTERED);
                retv_if((dev_info->type & device->supported_techs),
                                UAM_ERROR_ALREADY_REGISTERED);
+               retv_if(dev_info->app_num != device->app_num, UAM_ERROR_INVALID_PARAMETER);
        }
 
-       UAM_INFO("[%d]", user->user_id);
+       UAM_INFO("device to be added for user_id [%d]", user->user_id);
+       __uam_core_add_dev_to_sec_list_devices(dev_info);
 
        ret = _uam_pm_register_device(user->user_id, dev_info);
        if (UAM_ERROR_NONE != ret) {
@@ -1057,7 +1479,7 @@ int _uam_core_is_device_added(uam_device_info_s *dev, gboolean *is_added)
        retv_if(NULL == is_added, UAM_ERROR_INVALID_PARAMETER);
 
        *is_added = FALSE;
-       l = g_slist_find_custom(devices, dev->device_id, __compare_device_id);
+       l = __search_device(dev->device_id, dev->app_num);
        if (NULL == l)
                goto done;
 
@@ -1081,11 +1503,8 @@ int _uam_core_is_device_added(uam_device_info_s *dev, gboolean *is_added)
                        if (!addr)
                                continue;
 
-                       if (UAM_ADDR_TYPE_BLE == addr->addr_type ||
-                                       UAM_ADDR_TYPE_BT == addr->addr_type ||
-                                       UAM_ADDR_TYPE_P2P == addr->addr_type ||
-                                       UAM_ADDR_TYPE_WIFI == addr->addr_type)
-                               if (!strcasecmp(addr->address, dev->mac)) {
+                       if (__is_mac_addr(addr->addr_type) &&
+                                       !strcasecmp(addr->address,dev->mac)) {
                                        *is_added = TRUE;
                                        break;
                                }
@@ -1100,14 +1519,15 @@ done:
 }
 
 static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
-       const uam_device_info_s *dev_info, uam_db_tech_info_t *tech)
+       const uam_device_info_s *dev_info, uam_db_tech_info_t *tech, const int app_num)
 {
        int ret;
        GSList *l;
        int count = 0;
+       unsigned int bitmask;
 
        ret = _uam_db_get_device_services_count(dev_info->device_id,
-                               dev_info->type, dev_info->mac, &count);
+                               dev_info->type, dev_info->mac, dev_info->app_num, &count);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_db_get_device_services_count failed with %s",
                                _uam_manager_error_to_str(ret));
@@ -1120,11 +1540,18 @@ static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
                return ret;
        }
 
-       ret = _uam_pm_unregister_device(user_id, dev_info);
-       if (UAM_ERROR_NONE != ret) {
-               UAM_ERR("_uam_pm_unregister_device failed with %s",
-                               _uam_manager_error_to_str(ret));
-               return ret;
+       _uam_device_match_count(dev_info->device_id, dev_info->type,
+               dev_info->mac, &count);
+       /* If no copies present then remove from plugin too.
+        * Note: Discriminant is not considered for current logic.
+        */
+       if (count == 1) {
+               ret = _uam_pm_unregister_device(user_id, dev_info);
+               if (UAM_ERROR_NONE != ret) {
+                       UAM_ERR("_uam_pm_unregister_device failed with %s",
+                                       _uam_manager_error_to_str(ret));
+                       return ret;
+               }
        }
 
        /* Send device removed event to application */
@@ -1132,8 +1559,8 @@ static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
 
        retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
        /* Remove device from database */
-       ret = _uam_device_db_delete_device_info(
-                               dev_info->device_id, dev_info->type, dev_info->mac);
+       ret = _uam_device_db_delete_device_info(dev_info->device_id, dev_info->type,
+                               dev_info->mac, dev_info->app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_device_db_delete_device_info failed");
                __uam_db_end_transaction(0);
@@ -1146,13 +1573,15 @@ static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
 
                if (!svc || !svc->name)
                        continue;
+               //TODO: check _uam_core_service_remove_device for app_num updates
                ret = _uam_core_service_remove_device(svc->name,
-                               dev_info->device_id, dev_info->type);
+                               dev_info->device_id, dev_info->type, app_num);
                if (UAM_ERROR_NONE != ret)
                        UAM_ERR("_uam_device_db_delete_device_info failed");
        }
        __uam_db_end_transaction(1);
 
+       bitmask = __uam_core_tech_type_to_sensor(tech->tech_type);
        /* Remove tech info from device's tech list */
        device->tech_list = g_slist_remove(device->tech_list, tech);
        device->supported_techs &= ~(tech->tech_type);
@@ -1165,12 +1594,12 @@ static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
 
                /* Remove device  from user's device list */
                device->user->devices = g_slist_remove(device->user->devices, device);
-
                __free_user_device(device);
        }
 
+
        /* Set/update registered device list to plugins */
-       if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices))
+       if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices, bitmask))
                UAM_ERR("_uam_pm_set_registered_devices failed");
 
        /* Set/update registered device list to cloud plugin */
@@ -1181,7 +1610,7 @@ static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
 }
 
 int _uam_core_remove_device(const char *account,
-       const uam_device_info_s *dev_info)
+       const uam_device_info_s *dev_info, const int app_num)
 {
        FUNC_ENTRY;
        int ret;
@@ -1194,14 +1623,15 @@ int _uam_core_remove_device(const char *account,
        retv_if(NULL == dev_info, UAM_ERROR_INVALID_PARAMETER);
 
        /* Retrieve user from list */
-       l = g_slist_find_custom(users, account, __compare_user_account);
+       l = __search_user(account, dev_info->app_num);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
        user = l->data;
 
        /* Retrieve device from list */
-       l = g_slist_find_custom(devices, dev_info->device_id, __compare_device_id);
+       l = __search_device(dev_info->device_id, dev_info->app_num);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
        device = l->data;
+
        retv_if(user != device->user, UAM_ERROR_INVALID_PARAMETER);
        retv_if(!(device->supported_techs & dev_info->type), UAM_ERROR_INVALID_PARAMETER);
 
@@ -1211,14 +1641,14 @@ int _uam_core_remove_device(const char *account,
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
        tech = l->data;
 
-       ret = __uam_remove_device(user->user_id, device, dev_info, tech);
+       ret = __uam_remove_device(user->user_id, device, dev_info, tech, app_num);
 
        FUNC_EXIT;
        return ret ;
 }
 
 int _uam_core_remove_device_by_device_id(const char *device_id,
-       int tech_type)
+       int tech_type, const int app_num)
 {
        FUNC_ENTRY;
        int ret;
@@ -1232,7 +1662,7 @@ int _uam_core_remove_device_by_device_id(const char *device_id,
        retv_if(UAM_TECH_TYPE_P2P < tech_type, UAM_ERROR_INVALID_PARAMETER);
 
        /* Retrieve device from list */
-       l = g_slist_find_custom(devices, device_id, __compare_device_id);
+       l = __search_device(device_id, app_num);
        retv_if(NULL == l, UAM_ERROR_NOT_REGISTERED);
        device = l->data;
        retv_if(!(device->supported_techs & tech_type), UAM_ERROR_NOT_REGISTERED);
@@ -1244,16 +1674,16 @@ int _uam_core_remove_device_by_device_id(const char *device_id,
        tech = l->data;
 
        /* Get dev_info */
-       ret = _uam_core_get_devcie_by_device_id(device_id, tech_type, &dev_info);
+       ret = _uam_core_get_devcie_by_device_id(device_id, tech_type, &dev_info, app_num);
        retv_if(UAM_ERROR_NONE != ret, UAM_ERROR_NOT_REGISTERED);
 
-       ret = __uam_remove_device(device->user->user_id, device, &dev_info, tech);
+       ret = __uam_remove_device(device->user->user_id, device, &dev_info, tech, dev_info.app_num);
 
        FUNC_EXIT;
        return ret;
 }
 
-int _uam_core_remove_device_by_mac(const char *mac)
+int _uam_core_remove_device_by_mac(const char *mac, const int app_num)
 {
        FUNC_ENTRY;
        uam_db_tech_info_t *tech;
@@ -1263,7 +1693,7 @@ int _uam_core_remove_device_by_mac(const char *mac)
 
        retv_if(NULL == mac, UAM_ERROR_INVALID_PARAMETER);
 
-       tech = __get_tech_info_by_mac(mac);
+       tech = __get_tech_info_by_mac(mac, app_num);
        retv_if(NULL == tech, UAM_ERROR_NOT_FOUND);
        device = tech->device;
 
@@ -1273,13 +1703,13 @@ int _uam_core_remove_device_by_mac(const char *mac)
                return ret;
        }
 
-       ret = __uam_remove_device(device->user->user_id, device, &dev_info, tech);
+       ret = __uam_remove_device(device->user->user_id, device, &dev_info, tech, app_num);
 
        FUNC_EXIT;
        return ret;
 }
 
-int _uam_core_update_device(const uam_device_info_s *a_device)
+int _uam_core_update_device(uam_device_info_s *a_device)
 {
        FUNC_ENTRY;
 
@@ -1289,14 +1719,25 @@ int _uam_core_update_device(const uam_device_info_s *a_device)
        uam_device_info_s temp;
        uam_db_user_info_t *user = NULL;
        uam_db_device_info_t *device = NULL;
+       uam_db_device_info_t *temp_dev = NULL;
        uam_db_tech_info_t *tech = NULL;
 
+       temp_dev = g_new0(uam_db_device_info_t, 1);
+       temp_dev->device_id = a_device->device_id;
+       temp_dev->app_num = a_device->app_num;
+       UAM_DBG("temp_device device id %s", temp_dev->device_id);
+       UAM_DBG("temp_device  app_num is %d", temp_dev->app_num);
+
        /* Find all tech-devices in users' devices */
        for (l = users; NULL != l; l = g_slist_next(l)) {
                user = l->data;
 
+               UAM_DBG("user->app_num %d a_device->app_num %d", user->app_num , a_device->app_num);
+               if (user->app_num != a_device->app_num)
+                       continue;
+
                l1 = g_slist_find_custom(user->devices,
-                               a_device->device_id, __compare_device_id);
+                               temp_dev, __compare_device);
                if (NULL == l1) {
                        UAM_DBG("Valid user_id [%d] but Invalid device_id [%s]",
                                        user->user_id, a_device->device_id);
@@ -1312,7 +1753,6 @@ int _uam_core_update_device(const uam_device_info_s *a_device)
 
                /* Update discriminant for devices */
                device->discriminant = a_device->discriminant;
-               device->os = a_device->operating_system;
 
                l2 = g_slist_find_custom(device->tech_list,
                                &(a_device->type), __compare_tech_type);
@@ -1329,71 +1769,96 @@ int _uam_core_update_device(const uam_device_info_s *a_device)
 
                /* Update discriminant for device-tech */
                tech->discriminant = a_device->discriminant;
+               tech->os = a_device->operating_system;
 
                /* Update device's updated information to database */
                ret = __copy_tech_info_to_device_info(tech, &temp);
+
                if (UAM_ERROR_NONE != ret) {
                        UAM_ERR("__copy_tech_info_to_device_info failed [%d]", ret);
-                       return ret;
+                       goto done;
                }
 
                ret = _uam_device_db_update_device(temp.device_id, temp.type,
-                               temp.mac, temp.ipv4_addr, temp.operating_system, temp.discriminant);
+                               temp.mac, temp.ipv4_addr, temp.operating_system, temp.discriminant, temp.app_num);
                if (UAM_ERROR_NONE != ret) {
                        UAM_ERR("_uam_device_db_update_device failed [%d]", ret);
-                       return ret;
+                       goto done;
                }
        }
 
        /* Set/update registered device list to plugins */
-       ret = _uam_pm_set_registered_devices(devices);
+       ret = _uam_pm_set_registered_devices(devices,
+                       __uam_core_tech_type_to_sensor(a_device->type));
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_pm_set_registered_devices failed [%d]", ret);
-               return ret;
+               goto done;
        }
 
        /* Set/update registered device list to cloud plugin */
        _uam_cloud_update_registered_devices();
 
+done:
+       free_n_null(&temp_dev);
        FUNC_EXIT;
        return ret;
 }
 
-int _uam_core_get_default_user(uam_user_info_s *user_info)
+int _uam_core_get_default_user(uam_user_info_s *user_info, const int app_num)
 {
        FUNC_ENTRY;
-       int ret;
+       int ret = UAM_ERROR_NONE;
        int user_id;
        GSList *l;
        uam_db_user_info_t *user;
+       gchar *default_user_name = NULL;
+       gchar *default_account_name = NULL;
 
        retv_if(NULL == user_info, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
 
-       ret = _uam_core_add_user(&user_id, USER_ACCOUNT_DEFAULT, USER_NAME_DEFAULT);
-       if ((UAM_ERROR_NONE != ret) && (UAM_ERROR_ALREADY_REGISTERED != ret)) {
-               UAM_ERR("_uam_core_add_user failed with %s", _uam_manager_error_to_str(ret));
-               return ret;
+       __get_default_user(app_num, &default_account_name, &default_user_name);
+       UAM_DBG("default user account: [%s] name: [%s]",
+               default_account_name, default_user_name);
+
+       l = __search_user(default_account_name, app_num);
+       if (NULL == l) {
+               ret = _uam_core_add_user(&user_id, default_account_name,
+                       default_user_name, app_num);
+
+               if ((UAM_ERROR_NONE != ret) && (UAM_ERROR_ALREADY_REGISTERED != ret)) {
+                       UAM_ERR("_uam_core_add_user failed with %s", _uam_manager_error_to_str(ret));
+                       goto done;
+               }
+       } else {
+               user = l->data;
+               memset(user_info, 0x00, sizeof(uam_user_info_s));
+               g_strlcpy(user_info->account, user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
+               g_strlcpy(user_info->name, user->name, UAM_USER_NAME_MAX_STRING_LEN);
+               user_info->app_num = user->app_num;
        }
 
-       l = g_slist_find_custom(users, USER_ACCOUNT_DEFAULT, __compare_user_account);
-       retv_if(NULL == l, UAM_ERROR_INTERNAL);
-       user = l->data;
-
-       memset(user_info, 0x00, sizeof(uam_user_info_s));
-       g_strlcpy(user_info->account, user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
-       g_strlcpy(user_info->name, user->name, UAM_USER_NAME_MAX_STRING_LEN);
+done:
+       free_n_null(&default_user_name);
+       free_n_null(&default_account_name);
 
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return ret;
 }
 
-int _uam_core_get_users(int *count, uam_user_info_s **user_list)
+int _uam_core_get_users(int *count, uam_user_info_s **user_list,
+               const int app_num)
 {
        FUNC_ENTRY;
-       guint size;
+       guint size = 0;
        GSList *l;
 
-       size = g_slist_length(users);
+       for (l = users; l; l = g_slist_next(l)) {
+               uam_db_user_info_t *db_info = l->data;
+               if (db_info && db_info->app_num == app_num)
+                       size++;
+       }
+
        *user_list = g_new0(uam_user_info_s, size);
        *count = 0;
 
@@ -1401,7 +1866,7 @@ int _uam_core_get_users(int *count, uam_user_info_s **user_list)
        for (l = users; l; l = g_slist_next(l)) {
                uam_db_user_info_t *db_info = l->data;
 
-               if (!db_info || !db_info->account)
+               if (!db_info || !db_info->account || db_info->app_num != app_num)
                        continue;
 
                g_strlcpy((*user_list)[*count].account,
@@ -1409,6 +1874,7 @@ int _uam_core_get_users(int *count, uam_user_info_s **user_list)
                if (db_info->name)
                        g_strlcpy((*user_list)[*count].name,
                                        db_info->name, UAM_USER_NAME_MAX_STRING_LEN);
+               (*user_list)[*count].app_num = db_info->app_num;
 
                *count += 1;
        }
@@ -1418,29 +1884,47 @@ int _uam_core_get_users(int *count, uam_user_info_s **user_list)
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_user_by_account(const char *account, uam_user_info_s *user)
+int _uam_core_get_app_num_from_userid(int user_id, int *app_num)
+{
+       GSList *l;
+       for (l = users; l; l = g_slist_next(l)) {
+               uam_db_user_info_t *db_info = l->data;
+
+               if (!db_info || !db_info->account)
+                       continue;
+               if (db_info->user_id == user_id) {
+                       *app_num = db_info->app_num;
+                       break;
+               }
+       }
+       return UAM_ERROR_NONE;
+}
+
+int _uam_core_get_user_by_account(const char *account, uam_user_info_s *user,
+               const int app_num)
 {
        FUNC_ENTRY;
        uam_db_user_info_t *db_info;
        GSList *l;
 
        retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(!user, UAM_ERROR_INVALID_PARAMETER);
 
-       l = g_slist_find_custom(users, account, __compare_user_account);
+       l = __search_user(account, app_num);
        retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
        db_info = l->data;
 
        g_strlcpy(user->account, db_info->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
        if (db_info->name)
                g_strlcpy(user->name, db_info->name, UAM_USER_NAME_MAX_STRING_LEN);
-
+       user->app_num = db_info->app_num;
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
 int _uam_core_get_devcie_by_device_id(
-               const char *device_id, int tech_type, uam_device_info_s *device)
+               const char *device_id, int tech_type, uam_device_info_s *device, const int app_num)
 {
        FUNC_ENTRY;
        uam_db_device_info_t *db_info;
@@ -1451,7 +1935,7 @@ int _uam_core_get_devcie_by_device_id(
        retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
        retv_if(UAM_TECH_TYPE_P2P < tech_type, UAM_ERROR_INVALID_PARAMETER);
 
-       l = g_slist_find_custom(devices, device_id, __compare_device_id);
+       l = __search_device(device_id, app_num);
        retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
        db_info = l->data;
        retv_if(!(tech_type & db_info->supported_techs), UAM_ERROR_NOT_FOUND);
@@ -1479,6 +1963,7 @@ int _uam_core_get_devcie_by_device_id(
                                UAM_DEVICE_ID_MAX_STRING_LEN);
                device->type = tech->tech_type;
                device->discriminant = tech->discriminant;
+               device->app_num = db_info->app_num;
        }
 
        retv_if(UAM_TECH_TYPE_NONE == device->type, UAM_ERROR_NOT_FOUND);
@@ -1487,7 +1972,8 @@ int _uam_core_get_devcie_by_device_id(
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_devcie_by_mac(const char *mac, uam_device_info_s *device)
+int _uam_core_get_device_by_mac(const char *mac, uam_device_info_s *device,
+               const int app_num)
 {
        FUNC_ENTRY;
        int ret;
@@ -1495,7 +1981,7 @@ int _uam_core_get_devcie_by_mac(const char *mac, uam_device_info_s *device)
 
        retv_if(NULL == mac, UAM_ERROR_INVALID_PARAMETER);
 
-       tech = __get_tech_info_by_mac(mac);
+       tech = __get_tech_info_by_mac(mac, app_num);
        retv_if(NULL == tech, UAM_ERROR_NOT_FOUND);
 
        ret = __copy_tech_info_to_device_info(tech, device);
@@ -1508,68 +1994,74 @@ int _uam_core_get_devcie_by_mac(const char *mac, uam_device_info_s *device)
        return ret;
 }
 
-int _uam_core_get_user_by_device_id(const char *device_id, uam_user_info_s *user)
+int _uam_core_get_user_by_device_id(const char *device_id,
+               uam_user_info_s *user, const int app_num)
 {
        FUNC_ENTRY;
        uam_db_device_info_t *dev;
        GSList *l;
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(!user, UAM_ERROR_INVALID_PARAMETER);
 
-       l = g_slist_find_custom(devices, device_id, __compare_device_id);
+       l = __search_device(device_id, app_num);
        retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
        dev = l->data;
 
        g_strlcpy(user->account, dev->user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
        if (dev->user->name)
                g_strlcpy(user->name, dev->user->name, UAM_USER_NAME_MAX_STRING_LEN);
+       user->app_num = dev->user->app_num;
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_user_by_mac(const char *mac, uam_user_info_s *user)
+int _uam_core_get_user_by_mac(const char *mac, uam_user_info_s *user,
+               const int app_num)
 {
        FUNC_ENTRY;
        uam_db_tech_info_t *tech;
 
        retv_if(NULL == mac, UAM_ERROR_INVALID_PARAMETER);
 
-       tech = __get_tech_info_by_mac(mac);
+       tech = __get_tech_info_by_mac(mac, app_num);
        retv_if(NULL == tech, UAM_ERROR_NOT_FOUND);
 
        g_strlcpy(user->account, tech->device->user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
        if (tech->device->user->name)
                g_strlcpy(user->name, tech->device->user->name, UAM_USER_NAME_MAX_STRING_LEN);
+       user->app_num = tech->device->user->app_num;
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_devices(int *count, uam_device_info_s **device_list)
+int _uam_core_get_devices(int *count, uam_device_info_s **device_list, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
 
-       ret = __get_uam_db_dev_list_to_uam_dev_list(devices, device_list, count);
+       UAM_DBG("app_num %d", app_num);
+       ret = __get_uam_db_dev_list_to_uam_dev_list(devices, device_list, count, app_num);
 
        FUNC_EXIT;
        return ret;
 }
 
 int _uam_core_get_user_devices(const char *account,
-               int *count, uam_device_info_s **device_list)
+               int *count, uam_device_info_s **device_list, const int app_num)
 {
        FUNC_ENTRY;
        uam_db_user_info_t *user;
        GSList *l;
        int ret = UAM_ERROR_NONE;
 
-       l = g_slist_find_custom(users, account, __compare_user_account);
+       l = __search_user(account, app_num);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
        user = l->data;
 
-       ret = __get_uam_db_dev_list_to_uam_dev_list(user->devices, device_list, count);
+       ret = __get_uam_db_dev_list_to_uam_dev_list(user->devices, device_list, count, app_num);
 
        FUNC_EXIT;
        return ret;
@@ -1598,7 +2090,7 @@ gboolean _uam_core_is_sensor_ready(unsigned int sensor)
 }
 
 static uam_svc_dev_info_t *_uam_core_find_svc_dev_info(const char *device_id,
-       uam_tech_type_e tech_type, const char *svc_name)
+       uam_tech_type_e tech_type, const char *svc_name, const int app_num)
 {
        FUNC_ENTRY;
        uam_svc_dev_info_t *svc_dev = NULL;
@@ -1612,6 +2104,7 @@ static uam_svc_dev_info_t *_uam_core_find_svc_dev_info(const char *device_id,
 
                if ((0 == g_strcmp0(svc_dev->device_id, device_id)) &&
                                (0 == g_strcmp0(svc_dev->service, svc_name)) &&
+                               (svc_dev->app_num == app_num) &&
                                (svc_dev->tech_type == tech_type)) {
                        UAM_DBG("Service device found in list");
                        return svc_dev;
@@ -1624,7 +2117,7 @@ static uam_svc_dev_info_t *_uam_core_find_svc_dev_info(const char *device_id,
 
 static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e tech_type,
        const char *svc_name, gboolean discriminant, unsigned long long last_seen,
-       uam_ble_payload_s *payload_info)
+       uam_ble_payload_s *payload_info, const int app_num)
 {
        FUNC_ENTRY;
        uam_svc_dev_info_t *svc = NULL;
@@ -1634,14 +2127,19 @@ static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e
        retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
 
-       svc = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name);
+       svc = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
        if (!svc) {
                svc = g_new0(uam_svc_dev_info_t, 1);
                svc->device_id = g_strdup(device_id);
+               svc->app_num = app_num;
                svc->tech_type = tech_type;
                svc->service = g_strdup(svc_name);
+
                svc->payload = g_new0(uam_db_payload_info_t, 1);
-               __uam_copy_uam_payload_info(svc->payload, payload_info);
+               if (payload_info) {
+                       __uam_copy_uam_payload_info(svc->payload, payload_info);
+               }
+
                svc_devs = g_slist_append(svc_devs, svc);
        }
 
@@ -1649,20 +2147,23 @@ static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e
                svc->discriminant = discriminant;
        if (last_seen > 0)
                svc->last_seen = last_seen;
-       if (payload_info)
+
+       if (payload_info) {
+               /* TODO free existing payload structure */
                __uam_copy_uam_payload_info(svc->payload, payload_info);
+       }
 
        UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d] " \
-               "last_seen [%llu] payload primary key [%d]",
+               "last_seen [%llu] payload primary key [%d] app number [%d]",
                svc->service, svc->device_id, svc->tech_type, svc->discriminant,
-               svc->last_seen, svc->payload->primary_key);
+               svc->last_seen, svc->payload->primary_key, svc->app_num);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
 static int _uam_core_update_svc_dev_info_discriminant(const char *device_id,
-       uam_tech_type_e tech_type, const char *svc_name, gboolean discriminant)
+       uam_tech_type_e tech_type, const char *svc_name, gboolean discriminant, const int app_num)
 {
        FUNC_ENTRY;
        uam_svc_dev_info_t *svc = NULL;
@@ -1672,25 +2173,26 @@ static int _uam_core_update_svc_dev_info_discriminant(const char *device_id,
        retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
 
-       svc = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name);
+       svc = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
        if (!svc) {
                svc = g_new0(uam_svc_dev_info_t, 1);
                svc->device_id = g_strdup(device_id);
                svc->tech_type = tech_type;
+               svc->app_num = app_num;
                svc->service = g_strdup(svc_name);
                svc_devs = g_slist_append(svc_devs, svc);
        }
 
        svc->discriminant = discriminant;
 
-       UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d]]",
-               svc->service, svc->device_id, svc->tech_type, svc->discriminant);
+       UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d] app_num [%d]",
+               svc->service, svc->device_id, svc->tech_type, svc->discriminant, svc->app_num);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_service_add_user(const char *svc_name, const char *account)
+int _uam_core_service_add_user(const char *svc_name, const char *account, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -1702,13 +2204,14 @@ int _uam_core_service_add_user(const char *svc_name, const char *account)
        retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
 
        /* Retrieve user from list */
-       l = g_slist_find_custom(users, account, __compare_user_account);
+       l = __search_user(account, app_num);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
        user = l->data;
 
        /* Retrieve service from list */
        l = g_slist_find_custom(services, svc_name, __compare_svc_name);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
+
        service = l->data;
 
        retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
@@ -1735,17 +2238,19 @@ int _uam_core_service_add_user(const char *svc_name, const char *account)
                        __add_service_to_dev_tech_mapping(tech, service);
 
                        mac = __get_mac_addr(tech);
+
                        /* Insert device service info to db */
                        ret = _uam_db_insert_device_service_info(device->device_id,
                                                tech->tech_type, mac, service->name, service->cycle,
-                                               device->discriminant, 0);
+                                               device->discriminant, 0, app_num);
                        if (UAM_ERROR_NONE != ret) {
                                UAM_WARN("Device service addition to persistent DB failed");
                                __uam_db_end_transaction(0);
                                return ret;
                        }
+
                        ret = _uam_core_update_svc_dev_info(device->device_id, tech->tech_type,
-                                               service->name, device->discriminant, 0, NULL);
+                                               service->name, device->discriminant, 0, NULL, app_num);
                        if (UAM_ERROR_NONE != ret) {
                                UAM_WARN("Device service addition to service device mapping failed");
                                __uam_db_end_transaction(0);
@@ -1760,19 +2265,21 @@ int _uam_core_service_add_user(const char *svc_name, const char *account)
        return ret;
 }
 
-int _uam_core_service_remove_user(const char *svc_name, const char *account)
+int _uam_core_service_remove_user(const char *svc_name, const char *account, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
        uam_db_user_info_t *user;
-       int ret;
+       int ret = UAM_ERROR_NONE;
 
        retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
 
        /* Retrieve user from list */
-       l = g_slist_find_custom(users, account, __compare_user_account);
+       l = __search_user(account, app_num);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
+
        user = l->data;
 
        /* Retrieve service from list */
@@ -1807,7 +2314,7 @@ int _uam_core_service_remove_user(const char *svc_name, const char *account)
                        mac = __get_mac_addr(tech);
                        /* Remove service-device from DB */
                        ret = _uam_db_delete_device_service_info(device->device_id,
-                                       tech->tech_type, mac, svc_name);
+                                       tech->tech_type, mac, svc_name, app_num);
                        if (UAM_ERROR_NONE != ret) {
                                UAM_WARN("Device service removal from persistent DB failed");
                                __uam_db_end_transaction(0);
@@ -1863,7 +2370,7 @@ static uam_db_tech_info_t *__uam_core_get_dev_tech_info(const char *device_id, i
 }
 
 int _uam_core_service_add_device(const char *svc_name, const char *device_id,
-       int tech_type)
+       int tech_type, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -1887,25 +2394,26 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id,
        /* Retrieve service from list */
        l = g_slist_find_custom(services, svc_name, __compare_svc_name);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
+
        service = l->data;
 
        __add_service_to_dev_tech_mapping(tech_info, service);
 
        _uam_core_update_svc_dev_info(device_id, tech_type,
-               service->name, -1, -1, NULL);
-       svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, service->name);
+               service->name, -1, -1, NULL, app_num);
+       svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, service->name, app_num);
        __add_svc_dev_to_dev_tech_mapping(tech_info, svc_dev);
 
        mac = __get_mac_addr(tech_info);
        /* Insert device service info to db */
        ret = _uam_db_insert_device_service_info(device_id, tech_type, mac,
-                               service->name, service->cycle, tech_info->discriminant, 0);
+                               service->name, service->cycle, tech_info->discriminant, 0, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device service addition to persistent DB failed");
                return ret;
        }
        ret = _uam_core_update_svc_dev_info(device_id, tech_type, service->name,
-                       tech_info->discriminant, 0, NULL);
+                       tech_info->discriminant, 0, NULL, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device service addition to svc dev mapping failed");
                return ret;
@@ -1916,7 +2424,7 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id,
 }
 
 int _uam_core_service_remove_device(const char *svc_name,
-       const char *device_id, int tech_type)
+       const char *device_id, int tech_type, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -1944,7 +2452,7 @@ int _uam_core_service_remove_device(const char *svc_name,
        mac = __get_mac_addr(tech_info);
        /* Remove service-device from DB */
        ret = _uam_db_delete_device_service_info(
-                       device_id, tech_type, mac, svc_name);
+                       device_id, tech_type, mac, svc_name, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("Device service removal from persistent DB failed");
                return ret;
@@ -1955,7 +2463,7 @@ int _uam_core_service_remove_device(const char *svc_name,
 }
 
 int _uam_core_service_set_device_discriminant(const char *svc_name,
-               const char *device_id, int tech_type, gboolean discriminant)
+               const char *device_id, int tech_type, gboolean discriminant, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -1980,13 +2488,13 @@ int _uam_core_service_set_device_discriminant(const char *svc_name,
        mac = __get_mac_addr(tech_info);
        /* Insert device service info to db */
        ret = _uam_db_update_device_service_discriminant(device_id,
-                               tech_type, mac, service->name, discriminant);
+                               tech_type, mac, service->name, discriminant, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device service discriminant update to persistent DB failed");
                return ret;
        }
        ret = _uam_core_update_svc_dev_info_discriminant(device_id,
-                               tech_type, service->name, discriminant);
+                               tech_type, service->name, discriminant, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device service discriminant mapping update failed");
                return ret;
@@ -1997,7 +2505,7 @@ int _uam_core_service_set_device_discriminant(const char *svc_name,
 }
 
 int _uam_core_service_get_device_discriminant(const char *svc_name,
-               const char *device_id, int tech_type, gboolean *discriminant)
+               const char *device_id, int tech_type, gboolean *discriminant, const int app_num)
 {
        FUNC_ENTRY;
        uam_svc_dev_info_t *svc_dev;
@@ -2007,7 +2515,7 @@ int _uam_core_service_get_device_discriminant(const char *svc_name,
        retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
        retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
 
-       svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name);
+       svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
        retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER);
        *discriminant = (gboolean)svc_dev->discriminant;
 
@@ -2016,7 +2524,7 @@ int _uam_core_service_get_device_discriminant(const char *svc_name,
 }
 
 int _uam_core_service_get_device_last_seen(const char *svc_name,
-               const char *device_id, int tech_type, unsigned long long *last_seen)
+               const char *device_id, int tech_type, unsigned long long *last_seen, const int app_num)
 {
        FUNC_ENTRY;
        uam_svc_dev_info_t *svc_dev;
@@ -2026,7 +2534,7 @@ int _uam_core_service_get_device_last_seen(const char *svc_name,
        retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
        retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
 
-       svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name);
+       svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
        retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER);
        *last_seen = svc_dev->last_seen;
 
@@ -2034,7 +2542,7 @@ int _uam_core_service_get_device_last_seen(const char *svc_name,
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int new_cycle)
+int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int new_cycle, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -2051,20 +2559,24 @@ int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int new
 
        elapsed_time = service->cycle - service->remaining_time;
        service->cycle = new_cycle;
-       if (new_cycle < elapsed_time)
-               service->remaining_time = 0;
-       else
-               service->remaining_time = new_cycle - elapsed_time;
+
+       if (service->monitors) {
+               UAM_DBG("Detection ongoing, Update service remaining time");
+               if (new_cycle < elapsed_time)
+                       service->remaining_time = 0;
+               else
+                       service->remaining_time = new_cycle - elapsed_time;
+       }
 
        /* Update service detection cycle in DB */
-       if (UAM_ERROR_NONE != _uam_db_update_service_cycle(svc_name, new_cycle))
+       if (UAM_ERROR_NONE != _uam_db_update_service_cycle(svc_name, new_cycle, app_num))
                UAM_WARN("Service cycle updation to persistent DB failed");
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_service_detection_cycle(const char *svc_name, unsigned int *cycle)
+int _uam_core_get_service_detection_cycle(const char *svc_name, unsigned int *cycle, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -2075,6 +2587,7 @@ int _uam_core_get_service_detection_cycle(const char *svc_name, unsigned int *cy
 
        l = g_slist_find_custom(services, svc_name, __compare_svc_name);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
+
        service = l->data;
        *cycle = service->cycle;
 
@@ -2114,15 +2627,6 @@ static gboolean __start_detection(gpointer data)
                        _uam_core_get_active_env_sensors(UAM_DETECT_ABSENCE);
        UAM_DBG("Active envionmental sensors: 0x%8.8X", env_sensors);
 
-       if (0 != env_sensors) {
-               /* stop detection on active envionmental sensors*/
-               ret = _uam_pm_stop_detection(UAM_DETECT_PRESENCE | UAM_DETECT_ABSENCE,
-                       env_sensors);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_ERR("Failed with error: %s (0x%4.4X)",
-                                       _uam_manager_error_to_str(ret), ret);
-       }
-
        for (l = services; NULL != l; l = g_slist_next(l)) {
                service = l->data;
                if (!service || !service->monitors)
@@ -2187,13 +2691,8 @@ static int __uam_core_start_detection(int detection_type,
 
        l = g_slist_find_custom(services, svc_name, __compare_svc_name);
        if (!l) {
-               uam_db_service_info_t *default_svc = g_new0(uam_db_service_info_t, 1);
-               default_svc->name = g_strdup(UAM_SERVICE_DEFAULT);
-               default_svc->cycle = UAM_DETECTION_CYCLE_DEFAULT;
-               services = g_slist_append(services, default_svc);
-               l = services;
+               UAM_ERR("Service not found");
        }
-
        retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
        retv_if(NULL == l->data, UAM_ERROR_INTERNAL);
 
@@ -2400,6 +2899,7 @@ int _uam_core_init(void)
        GSList *db_adv_list;
        GSList *db_svc_dev_list;
        GSList *db_payload_list;
+       GSList *db_sender_apps;
        GSList *l;
 
        /* Init database */
@@ -2411,7 +2911,7 @@ int _uam_core_init(void)
                UAM_ERR("_uam_pm_set_detection_window(%d) failed", detection_window);
 
        /* Fetch user list */
-       db_users = _uam_db_get_all_users();
+       db_users = _uam_db_get_all_users(-1);
        if (!db_users) {
                UAM_INFO_C("No users in database");
        } else {
@@ -2426,6 +2926,7 @@ int _uam_core_init(void)
                        user->user_id = info->user_id;
                        user->name = g_strdup(info->name);
                        user->account = g_strdup(info->account);
+                       user->app_num = info->app_num;
                        user->devices = NULL;
 
                        users = g_slist_prepend(users, user);
@@ -2433,33 +2934,24 @@ int _uam_core_init(void)
        }
 
        /* Fetch service list */
-       db_svc_list = _uam_service_db_get_all_services();
+       db_svc_list = _uam_service_db_get_all_services(-1);
        if (!db_svc_list) {
                UAM_INFO_C("No services in database");
        } else {
                for (l = db_svc_list; NULL != l; l = g_slist_next(l)) {
                        db_service_info_t *db_svc = l->data;
                        uam_db_service_info_t *service;
-                       GSList *l1;
 
                        if (!db_svc)
                                continue;
-
-                       l1 = g_slist_find_custom(services,
-                                       db_svc->service_name, __compare_svc_name);
-                       if (!l1) {
-                               service = g_new0(uam_db_service_info_t, 1);
-                               service->name = g_strdup(db_svc->service_name);
-                               service->cycle = db_svc->cycle;
-                               service->presence_threshold = db_svc->presence_threshold;
-                               service->absence_threshold = db_svc->absence_threshold;
-                               services = g_slist_append(services, service);
-                       }
+                       service = g_new0(uam_db_service_info_t, 1);
+                       __uam_copy_db_service_info(service, db_svc);
+                       services = g_slist_append(services, service);
                }
        }
 
        /* Fetch payload list */
-       db_payload_list = _uam_db_get_all_payloads();
+       db_payload_list = _uam_db_get_all_payloads(-1);
        if (!db_payload_list) {
                UAM_INFO_C("No device payload in database");
        } else {
@@ -2484,8 +2976,7 @@ int _uam_core_init(void)
        }
 
        /* Fetch svc dev list */
-
-       db_svc_dev_list = _uam_db_get_service_devices_info();
+       db_svc_dev_list = _uam_db_get_service_devices_info(-1);
        if (!db_svc_dev_list) {
                UAM_INFO_C("No service devices in database");
        } else {
@@ -2494,26 +2985,26 @@ int _uam_core_init(void)
 
                        _uam_core_update_svc_dev_info(db_svc->device_id, db_svc->type,
                                        db_svc->svc, db_svc->discriminant, db_svc->last_seen,
-                                       &(db_svc->payload_info));
+                                       &(db_svc->payload_info), db_svc->app_num);
                }
        }
        g_slist_free_full(db_svc_dev_list, g_free);
 
-       db_svc_dev_list = _uam_db_get_service_devices_payloads_info();
+       db_svc_dev_list = _uam_db_get_service_devices_payloads_info(-1);
        if (!db_svc_dev_list) {
-               UAM_INFO_C("No service devices in database");
+               UAM_INFO_C("No service devices payloads in database");
        } else {
                for (l = db_svc_dev_list; NULL != l; l = g_slist_next(l)) {
                        db_svc_dev_info_t *db_svc = l->data;
 
                        _uam_core_update_svc_dev_info(db_svc->device_id, db_svc->type,
                                        db_svc->svc, db_svc->discriminant, db_svc->last_seen,
-                                       &(db_svc->payload_info));
+                                       &(db_svc->payload_info), db_svc->app_num);
                }
        }
 
        /* Fetch device list */
-       db_devices = _uam_device_db_get_all_devices();
+       db_devices = _uam_device_db_get_all_devices(-1);
        if (!db_devices) {
                UAM_INFO_C("No Devices registered in database");
        } else {
@@ -2534,12 +3025,16 @@ int _uam_core_init(void)
                                continue;
                        }
                        user = l1->data;
+                       UAM_DBG("db_info->app_num %d", db_info->app_num);
+                       UAM_DBG("dev_info.app_num %d", db_info->dev_info.app_num);
+                       db_info->dev_info.app_num = db_info->app_num;
 
                        /* Fetch device services from DB */
                        l1 = _uam_db_get_device_services(
                                db_info->dev_info.device_id,
                                db_info->dev_info.type,
-                               db_info->dev_info.mac);
+                               db_info->dev_info.mac,
+                               db_info->app_num);
 
                        svc_list = __convert_db_svc_list_to_uam_svc_list(l1);
 
@@ -2551,7 +3046,7 @@ int _uam_core_init(void)
        }
 
        /* Fetch iBeacon adv list */
-       db_adv_list = _uam_db_get_all_advs();
+       db_adv_list = _uam_db_get_all_advs(-1);
        if (!db_adv_list) {
                UAM_INFO_C("No iBeacon adv in database");
        } else {
@@ -2561,16 +3056,47 @@ int _uam_core_init(void)
                }
        }
 
+       /* Fetch sender_apps list */
+       db_sender_apps = _uam_db_get_all_apps();
+       if (!db_sender_apps) {
+               UAM_INFO_C("No sender_apps in database");
+       } else {
+               GSList **sender_apps = _uam_manager_get_sender_apps();
+               uam_sender_app_t *sender_app = NULL;
+               for (l = db_sender_apps; l; l = g_slist_next(l)) {
+                       db_app_info_t *db_sender_app = l->data;
+                       if (!db_sender_app) {
+                               UAM_WARN("db_sender_apps list has NULL data element");
+                               continue;
+                       }
+
+                       sender_app = g_malloc0(sizeof(uam_sender_app_t));
+                       if (sender_app) {
+                               sender_app->app_num = db_sender_app->app_num;
+                               sender_app->app_key = g_strdup(db_sender_app->app_key);
+                               *sender_apps = g_slist_prepend(*sender_apps, sender_app);
+                       } else {
+                               UAM_ERR("Memory Allocation error");
+                               return UAM_ERROR_OUT_OF_MEMORY;
+                       }
+               }
+       }
+
+
        g_slist_free_full(db_devices, g_free);
        g_slist_free_full(db_users, g_free);
        g_slist_free_full(db_svc_list, g_free);
        g_slist_free_full(db_adv_list, g_free);
        g_slist_free_full(db_svc_dev_list, g_free);
        g_slist_free_full(db_payload_list, g_free);
+       g_slist_free_full(db_sender_apps, g_free);
 
        if (devices) {
+               unsigned int bitmask;
+               bitmask = _uam_core_get_user_sensors();
+
                /* Set/update registered device list to plugins */
-               if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices))
+               if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices, bitmask))
                        UAM_ERR("_uam_pm_set_registered_devices failed");
 
                /* Set/update registered device list to cloud plugin */
@@ -2617,9 +3143,7 @@ void _uam_core_deinit(void)
 
                if (!service)
                        continue;
-
-               g_free(service->name);
-               g_free(service);
+               __free_service_info(service);
        }
        g_slist_free(services);
        services = NULL;
@@ -2646,12 +3170,24 @@ void _uam_core_deinit(void)
                if (!payload)
                        continue;
                g_free(payload->device_uid);
-               g_free(payload->bt_mac);
                g_free(payload);
        }
        g_slist_free(payloads);
        payloads = NULL;
 
+       /* Release allocated memory for sender_apps*/
+       GSList *sender_apps = *(_uam_manager_get_sender_apps());
+       for (l = sender_apps; l; l = g_slist_next(l)) {
+               uam_sender_app_t *sender_app = l->data;
+
+               if (!sender_app)
+                       continue;
+               g_free(sender_app->sender);
+               g_free(sender_app->app_key);
+               g_free(sender_app);
+       }
+       g_slist_free(sender_apps);
+
        FUNC_EXIT;
 }
 
@@ -2671,7 +3207,7 @@ void _uam_core_handle_sensor_ready(unsigned int sensor, gboolean is_ready)
 }
 
 int _uam_core_handle_device_added(int status,
-               int user_id, const uam_device_info_s *dev_info)
+               int user_id, uam_device_info_s *dev_info)
 {
        FUNC_ENTRY;
 
@@ -2682,8 +3218,19 @@ int _uam_core_handle_device_added(int status,
        uam_db_user_info_t *user = NULL;
        uam_svc_dev_info_t *svc_dev = NULL;
        uam_db_service_info_t *service = NULL;
+       gchar *default_service_name = NULL;
+       int value = 0;
+
+       UAM_INFO("user_id is [%d]", user_id);
+
+       value = __uam_core_get_app_num_from_sec_list_devices(dev_info);
+       UAM_DBG("The app_num is %d", value);
+       if (value != 0)
+               dev_info->app_num = value;
+       else
+               UAM_DBG("could not find app_num");
 
-       UAM_INFO("[%d]", user_id);
+       __uam_core_remove_dev_from_sec_list_devices(dev_info);
 
        /* Send reply over dbus for add device API */
        l = _uam_manager_get_request_list();
@@ -2707,7 +3254,6 @@ int _uam_core_handle_device_added(int status,
                                        dev->device_id, dev_info->device_id);
                        continue;
                }
-
                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                g_array_append_vals(out_param, dev_info, sizeof(uam_device_info_s));
                _uam_manager_method_return(info->context, out_param, status);
@@ -2723,24 +3269,22 @@ int _uam_core_handle_device_added(int status,
                __send_device_event(status, UAM_EVENT_DEVICE_ADDED, dev_info);
                return status;
        }
-
        if (0 > user_id) {
                int id = 0;
+               gchar *default_user_account = NULL;
+               __get_default_user(dev_info->app_num, &default_user_account, NULL);
+
                l = g_slist_find_custom(users,
-                               USER_ACCOUNT_DEFAULT, __compare_user_account);
+                               default_user_account, __compare_user_account);
                if (NULL == l) {
-                       ret = _uam_core_add_user(&id, USER_ACCOUNT_DEFAULT, USER_NAME_DEFAULT);
-                       if (UAM_ERROR_NONE != ret) {
-                               UAM_ERR("_uam_core_add_user failed with %s",
-                                               _uam_manager_error_to_str(ret));
-                               __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
-                               return ret;
-                       }
+                       UAM_ERR("default user not found for account %s", default_user_account);
+                       return ret;
                }
                user_id = id;
        }
+
        l = g_slist_find_custom(users, &user_id, __compare_user_id);
-       if (NULL == l) {
+       if (NULL == l || NULL == l->data) {
                UAM_ERR("Invalid user Id: %d", user_id);
                ret = UAM_ERROR_NOT_FOUND;
                __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
@@ -2748,55 +3292,59 @@ int _uam_core_handle_device_added(int status,
        }
        user = l->data;
 
-       /** updates for svc dev*/
-       ret = _uam_core_update_svc_dev_info(dev_info->device_id, dev_info->type,
-                               UAM_SERVICE_DEFAULT, dev_info->discriminant, 0, NULL);
-       if (UAM_ERROR_NONE != ret) {
-               UAM_WARN("Device service mappiing update failed");
-               __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
-               return ret;
-       }
+       /* Get default service */
+       __get_default_service_name(dev_info->app_num, &default_service_name);
 
-       /* Get default service and add it to device's service list by default */
-       l = g_slist_find_custom(services, UAM_SERVICE_DEFAULT, __compare_svc_name);
+       l = g_slist_find_custom(services, default_service_name, __compare_svc_name);
        if (!l) {
-               service = g_new0(uam_db_service_info_t, 1);
-               service->name = g_strdup(UAM_SERVICE_DEFAULT);
-               service->cycle = UAM_DETECTION_CYCLE_DEFAULT;
-               services = g_slist_append(services, service);
+               UAM_WARN("Default service not found");
        } else {
                service = l->data;
        }
 
+       /** updates for svc dev*/
+       ret = _uam_core_update_svc_dev_info(dev_info->device_id, dev_info->type,
+                               default_service_name, dev_info->discriminant,
+                               0, NULL, dev_info->app_num);
+       if (UAM_ERROR_NONE != ret) {
+               UAM_WARN("Device service mapping update failed");
+               __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
+               goto done;
+       }
+
        svc_list = g_slist_append(svc_list, service);
        svc_dev = _uam_core_find_svc_dev_info(dev_info->device_id,
-                       dev_info->type, UAM_SERVICE_DEFAULT);
+                       dev_info->type, default_service_name, dev_info->app_num);
        svc_dev_list = g_slist_append(svc_dev_list, svc_dev);
        __uam_core_add_dev_to_list(user, dev_info, UAM_PRESENCE_STATE_PRESENT,
                        dev_info->last_seen, svc_list, svc_dev_list);
 
        /** Start database transaction */
-       retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
+       ret = __uam_db_begin_transaction();
+       if (UAM_ERROR_NONE != ret) {
+               UAM_WARN("__uam_db_begin_transaction failed");
+               goto done;
+       }
 
        /* Add device to database */
        ret = _uam_device_db_insert_device_info(user->user_id, dev_info,
-                       UAM_PRESENCE_STATE_PRESENT, dev_info->last_seen);
+                       UAM_PRESENCE_STATE_PRESENT, dev_info->last_seen, dev_info->app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device addition to persistent DB failed");
                __uam_db_end_transaction(0);
-               return ret;
+               goto done;
        }
 
        /* Insert device service info to db */
        ret =  _uam_db_insert_device_service_info(
                                dev_info->device_id, dev_info->type, dev_info->mac,
-                               UAM_SERVICE_DEFAULT, UAM_DETECTION_CYCLE_DEFAULT,
-                               dev_info->discriminant, 0);
+                               default_service_name, UAM_DETECTION_CYCLE_DEFAULT,
+                               dev_info->discriminant, 0, dev_info->app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device service addition to persistent DB failed");
                __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
                __uam_db_end_transaction(0);
-               return ret;
+               goto done;
        }
        __uam_db_end_transaction(1);
        /** End database transaction */
@@ -2805,18 +3353,20 @@ int _uam_core_handle_device_added(int status,
        __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
 
        /* Set/update registered device list to plugins */
-       ret = _uam_pm_set_registered_devices(devices);
+       ret = _uam_pm_set_registered_devices(devices,
+                       __uam_core_tech_type_to_sensor(dev_info->type));
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_pm_set_registered_devices failed");
-               return ret;
+               goto done;
        }
 
-       _uam_cloud_send_device_added(status, (user ? user->account : NULL), dev_info);
+       _uam_cloud_send_device_added(status, user->account, dev_info);
 
        /* Set/update registered device list to cloud plugin */
        if (UAM_ERROR_NONE == status)
                _uam_cloud_update_registered_devices();
-
+done:
+       free_n_null(&default_service_name);
        FUNC_EXIT;
        return ret;
 }
@@ -2988,7 +3538,7 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor,
 
                UAM_DBG("Check service device discriminant");
                svc_dev = _uam_core_find_svc_dev_info(dev_info->device_id,
-                               dev_info->type, svc->name);
+                               dev_info->type, svc->name, dev_info->app_num);
                if (!svc_dev || !svc_dev->discriminant)
                        continue;
 
@@ -3042,7 +3592,7 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor,
                if (live_monitoring) {
                        svc_dev->last_seen = tech->last_seen;
                        ret = _uam_db_update_device_service_last_seen(dev_info->device_id,
-                                       dev_info->type, dev_info->mac, svc->name, tech->last_seen);
+                                       dev_info->type, dev_info->mac, svc->name, tech->last_seen, dev_info->app_num);
                        if (UAM_ERROR_NONE != ret)
                                UAM_WARN("_uam_db_update_device_service_last_seen failed");
                }
@@ -3051,6 +3601,59 @@ void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor,
        FUNC_EXIT;
 }
 
+static int __uam_core_update_device_addr(uam_db_address_info_t *addr,
+                                       uam_device_info_s *dev_info)
+{
+       FUNC_ENTRY;
+       int ret = UAM_ERROR_NONE;
+
+       retv_if(!addr, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(!dev_info, UAM_ERROR_INVALID_PARAMETER);
+
+       switch(addr->addr_type) {
+       case UAM_ADDR_TYPE_IPv4:
+               if (strcasecmp(addr->address, dev_info->ipv4_addr)) {
+                       UAM_DBG("Old IPv4: %s, New IPv4: %s",
+                               addr->address, dev_info->ipv4_addr);
+
+                       g_free(addr->address);
+                       addr->address = g_strdup(dev_info->ipv4_addr);
+
+                       /* Update IP address in DB */
+                       ret = _uam_device_db_update_device_ip_address(dev_info->device_id,
+                                       dev_info->type, dev_info->ipv4_addr, dev_info->app_num);
+                       if (UAM_ERROR_NONE != ret) {
+                               UAM_WARN("_uam_device_db_update_device_ip_address failed");
+                               return ret;
+                       }
+               }
+               break;
+       case UAM_ADDR_TYPE_WIFI:
+               if (strcasecmp(addr->address, dev_info->mac)) {
+                       UAM_DBG("Old MAC: %s, New MAC: %s",
+                               addr->address, dev_info->mac);
+
+                       g_free(addr->address);
+                       addr->address = g_strdup(dev_info->mac);
+
+                       /* Update address in DB */
+                       ret = _uam_device_db_update_device_mac_address(dev_info->device_id,
+                                       dev_info->type, dev_info->mac, dev_info->app_num);
+                       if (UAM_ERROR_NONE != ret) {
+                               UAM_WARN("_uam_device_db_update_device_mac_address failed");
+                               __uam_db_end_transaction(0);
+                               return ret;
+                       }
+               }
+               break;
+       default:
+               UAM_DBG("Unknown addr type: %d", addr->addr_type);
+       }
+
+       FUNC_EXIT;
+       return ret;
+}
+
 int _uam_core_handle_presence_detected(unsigned int sensor,
                int user_id, void *info, uam_ble_payload_s *payload)
 {
@@ -3067,10 +3670,15 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
 
        UAM_INFO("sensor: 0x%8.8X, user_id: %d", sensor, user_id);
 
-       if (info && (UAM_SENSOR_BITMASK_LIGHT == sensor || UAM_SENSOR_BITMASK_MOTION == sensor))
+       if (info && (sensor & _uam_core_get_env_sensors())) {
+               /* Received info is sensor information */
                sensor_info = info;
-       if (info && (UAM_SENSOR_BITMASK_BLE == sensor || UAM_SENSOR_BITMASK_WIFI == sensor))
+       }
+
+       if (info && (sensor & _uam_core_get_user_sensors())) {
+               /* Received info is device information */
                dev_info = info;
+       }
 
        _uam_vpm_send_presence_detection_event(sensor);
 
@@ -3081,6 +3689,9 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
 
        retv_if(0 > user_id, UAM_ERROR_INVALID_PARAMETER);
 
+       /* fetch the app_num info for the corresponding user_id */
+       _uam_core_get_app_num_from_userid(user_id, &dev_info->app_num);
+
        l = g_slist_find_custom(users, &user_id, __compare_user_id);
        if (NULL == l) {
                UAM_ERR("Invalid user_id [%d]", user_id);
@@ -3096,6 +3707,7 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
                return UAM_ERROR_INVALID_PARAMETER;
        }
        device = l->data;
+
        if (!(device->supported_techs & dev_info->type)) {
                UAM_ERR("Valid device_id [%s] but Invalid tech type [%d]",
                                dev_info->device_id, dev_info->type);
@@ -3114,68 +3726,34 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
        tech->last_seen = dev_info->last_seen;
 
        /** Update payload info in list with user data */
-       l = g_slist_find_custom(payloads, payload, __compare_payload);
-       if (l)
-               __uam_copy_uam_payload_info(l->data, payload);
+       if (payload) {
+               l = g_slist_find_custom(payloads, payload, __compare_payload);
+               if (l)
+                       __uam_copy_uam_payload_info(l->data, payload);
+       }
 
        retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_INVALID_PARAMETER);
 
        /* Check if IP/MAC address was updated then update in DB */
        if (UAM_TECH_TYPE_WIFI == dev_info->type) {
-
                for (l = tech->addresses; NULL != l; l = g_slist_next(l)) {
                        uam_db_address_info_t *addr = l->data;
 
                        if (NULL == addr)
                                continue;
 
-                       if (UAM_ADDR_TYPE_IPv4 == addr->addr_type) {
-
-                               if (strcasecmp(addr->address, dev_info->ipv4_addr)) {
-                                       UAM_DBG("Old IPv4: %s, New IPv4: %s",
-                                               addr->address, dev_info->ipv4_addr);
-
-                                       g_free(addr->address);
-                                       addr->addr_type = UAM_ADDR_TYPE_IPv4;
-                                       addr->address = g_strdup(dev_info->ipv4_addr);
-
-                                       /* Update IP address in DB */
-                                       ret = _uam_device_db_update_device_ip_address(dev_info->device_id,
-                                                       dev_info->type, dev_info->ipv4_addr);
-                                       if (UAM_ERROR_NONE != ret) {
-                                               UAM_WARN("_uam_device_db_update_device_ip_address failed");
-                                               __uam_db_end_transaction(0);
-                                               return ret;
-                                       }
-                               }
-                       }
-
-                       if (UAM_ADDR_TYPE_WIFI == addr->addr_type) {
-
-                               if (strcasecmp(addr->address, dev_info->mac)) {
-                                       UAM_DBG("Old MAC: %s, New MAC: %s",
-                                               addr->address, dev_info->mac);
-
-                                       g_free(addr->address);
-                                       addr->addr_type = UAM_ADDR_TYPE_WIFI;
-                                       addr->address = g_strdup(dev_info->mac);
-
-                                       /* Update address in DB */
-                                       ret = _uam_device_db_update_device_mac_address(dev_info->device_id,
-                                                       dev_info->type, dev_info->mac);
-                                       if (UAM_ERROR_NONE != ret) {
-                                               UAM_WARN("_uam_device_db_update_device_mac_address failed");
-                                               __uam_db_end_transaction(0);
-                                               return ret;
-                                       }
-                               }
+                       ret = __uam_core_update_device_addr(addr, dev_info);
+                       if (UAM_ERROR_NONE != ret) {
+                               UAM_WARN("_uam_device_db_update_device_ip_address failed");
+                               __uam_db_end_transaction(0);
+                               return ret;
                        }
                }
        }
 
        /* Update database (presence state & timestamp) */
        ret = _uam_device_db_update_device_last_seen(dev_info->device_id,
-                               dev_info->type, dev_info->mac, dev_info->last_seen);
+                               dev_info->type, dev_info->mac, dev_info->last_seen, dev_info->app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("_uam_device_db_update_device_last_seen failed");
                __uam_db_end_transaction(0);
@@ -3183,7 +3761,7 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
        }
 
        ret = _uam_device_db_update_device_presence(dev_info->device_id,
-                               dev_info->type, dev_info->mac, tech->presence_state);
+                               dev_info->type, dev_info->mac, tech->presence_state, dev_info->app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("_uam_device_db_update_device_presence failed");
                __uam_db_end_transaction(0);
@@ -3191,15 +3769,18 @@ int _uam_core_handle_presence_detected(unsigned int sensor,
        }
 
        /* Update database (payload user data) */
-       ret = _uam_db_update_payload_info(dev_info->device_id,
-                               dev_info->type, dev_info->mac, payload);
-       if (UAM_ERROR_NONE != ret) {
-               UAM_WARN("_uam_db_update_payload_user_data failed");
-               __uam_db_end_transaction(0);
-               return ret;
+       if (payload) {
+               ret = _uam_db_update_payload_info(dev_info->device_id,
+                                       dev_info->type, dev_info->mac, payload, dev_info->app_num);
+               if (UAM_ERROR_NONE != ret) {
+                       UAM_WARN("_uam_db_update_payload_user_data failed");
+                       __uam_db_end_transaction(0);
+                       return ret;
+               }
        }
 
        __uam_db_end_transaction(1);
+
        /* Send user presence event and update service_device timestamp */
        __send_user_presence_event(tech, sensor, dev_info, payload);
 
@@ -3227,6 +3808,12 @@ static void __send_user_absence_event(uam_tech_type_e type, unsigned int sensor)
                if (!svc || !svc->monitors || !svc->dev_techs)
                        continue;
 
+               UAM_INFO("service [%s] remaining time [%d] cycle [%d]",
+                               svc->name, svc->remaining_time, svc->cycle);
+
+               if (!(svc->remaining_time == svc->cycle))
+                       continue;
+
                for (l1 = svc->dev_techs; NULL != l1; l1 = g_slist_next(l1)) {
                        uam_db_tech_info_t *tech = l1->data;
 
@@ -3320,10 +3907,15 @@ void _uam_core_handle_absence_detected(unsigned int sensor,
 
        UAM_INFO("sensor: 0x%8.8X, user_id: %d", sensor, user_id);
 
-       if (info && (UAM_SENSOR_BITMASK_LIGHT == sensor || UAM_SENSOR_BITMASK_MOTION == sensor))
+       if (info && (sensor & _uam_core_get_env_sensors())) {
+               /* received info is sensor information */
                sensor_info = info;
-       if (info && (UAM_SENSOR_BITMASK_BLE == sensor || UAM_SENSOR_BITMASK_WIFI == sensor))
+       }
+
+       if (info && (sensor & _uam_core_get_user_sensors())) {
+               /* received info is device information */
                dev_info = info;
+       }
 
        if (NULL == dev_info) {
                __send_sensor_absence_event(sensor_info, sensor);
@@ -3366,7 +3958,7 @@ void _uam_core_handle_absence_detected(unsigned int sensor,
 
        /* Update database (presence state) */
        if (UAM_ERROR_NONE != _uam_device_db_update_device_presence(dev_info->device_id,
-                               dev_info->type, dev_info->mac, tech->presence_state))
+                               dev_info->type, dev_info->mac, tech->presence_state, dev_info->app_num))
                UAM_WARN("_uam_device_db_update_device_presence failed");
 
        FUNC_EXIT;
@@ -3393,59 +3985,11 @@ void _uam_core_cleanup_monitor(char *name)
        }
 }
 
-static void __free_user_info(gpointer data)
-{
-       FUNC_ENTRY;
-       uam_db_user_info_t *user = data;
-
-       ret_if(NULL == user);
-
-       g_free(user->name);
-       g_free(user->account);
-       g_free(user);
-       FUNC_EXIT;
-}
-
-static void __free_service_info(gpointer data)
-{
-       FUNC_ENTRY;
-       uam_db_service_info_t *svc = data;
-
-       ret_if(NULL == svc);
-
-       g_free(svc->name);
-       g_free(svc);
-       FUNC_EXIT;
-}
-
-static void __free_monitor_info(gpointer data)
-{
-       FUNC_ENTRY;
-       uam_monitor_info_t *monitor = data;
-
-       ret_if(NULL == monitor);
-
-       g_free(monitor->name);
-       g_free(monitor);
-       FUNC_EXIT;
-}
-
-static void __free_scanner_info(gpointer data)
-{
-       FUNC_ENTRY;
-       uam_scanner_info_t *scanner = data;
-
-       ret_if(NULL == scanner);
-
-       g_free(scanner->name);
-       g_free(scanner);
-       FUNC_EXIT;
-}
-
 void _uam_core_reset_database(void)
 {
        FUNC_ENTRY;
        int ret;
+       unsigned int bitmask;
 
        ret = _uam_db_clear();
        if (UAM_ERROR_NONE != ret) {
@@ -3470,8 +4014,9 @@ void _uam_core_reset_database(void)
        g_slist_free_full(scanners, __free_scanner_info);
        scanners = NULL;
 
+       bitmask = _uam_core_get_user_sensors();
        /* Set/update registered device list to plugins */
-       ret = _uam_pm_set_registered_devices(devices);
+       ret = _uam_pm_set_registered_devices(devices, bitmask);
        if (UAM_ERROR_NONE != ret)
                UAM_ERR("_uam_pm_set_registered_devices failed with %s",
                                _uam_manager_error_to_str(ret));
@@ -3493,6 +4038,7 @@ void _uam_core_handle_detection_started(unsigned int sensor)
        detecting_sensors |= sensor;
        active_sensors |= _uam_core_get_active_sensors(UAM_DETECT_PRESENCE);
        active_sensors |= _uam_core_get_active_sensors(UAM_DETECT_ABSENCE);
+
        if (active_sensors == detecting_sensors) {
                /* Send detection started event */
                _uam_manager_send_event(NULL, UAM_EVENT_DETECTION_STARTED, NULL);
@@ -3514,10 +4060,7 @@ void _uam_core_handle_detection_stopped(unsigned int sensor)
                        sensor, detecting_sensors);
        detecting_sensors &= ~sensor;
 
-       if (UAM_SENSOR_BITMASK_BLE == sensor)
-               type = UAM_TECH_TYPE_BLE;
-       else if (UAM_SENSOR_BITMASK_WIFI == sensor)
-               type = UAM_TECH_TYPE_WIFI;
+       type = __uam_core_sensor_to_tech_type(sensor);
 
        if (UAM_TECH_TYPE_NONE != type)
                __send_user_absence_event(type, sensor);
@@ -3532,11 +4075,12 @@ void _uam_core_handle_detection_stopped(unsigned int sensor)
                        uam_db_service_info_t *service = mon->service;
                        cycle_state = UAM_DETECTION_CYCLE_END;
 
-                       UAM_DBG("mon->sensors[0x%X], [%s]->remaining_time: %d", mon->sensors, service->name, service->remaining_time);
+                       UAM_DBG("mon->sensors[0x%X], [%s]->remaining_time: %d",
+                               mon->sensors, service->name, service->remaining_time);
                        if (!(mon->sensors & _uam_core_get_env_sensors())) {
                                if (service->remaining_time < service->cycle) {
                                        UAM_DBG("service->remaining_time < service->cycle, return");
-                                       return;
+                                       continue;
                                }
                        } else if (service->remaining_time > UAM_DETECTION_CYCLE_MIN) {
                                cycle_state = UAM_DETECTION_CYCLE_MID;
@@ -3742,7 +4286,7 @@ void _uam_core_handle_active_device(uam_active_scan_event_e event,
        FUNC_EXIT;
 }
 
-int _uam_core_register_service(uam_service_info_s *svc)
+int _uam_core_register_service(uam_service_info_s *svc, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -3756,21 +4300,21 @@ int _uam_core_register_service(uam_service_info_s *svc)
        retv_if((NULL != l) && (l->data != NULL), UAM_ERROR_ALREADY_REGISTERED);
 
        service = g_new0(uam_db_service_info_t, 1);
-       service->name = g_strdup(svc->name);
+       __uam_copy_uam_service_info(service, svc);
        service->cycle = UAM_DETECTION_CYCLE_DEFAULT;
-       service->presence_threshold = svc->presence_threshold;
-       service->absence_threshold = svc->absence_threshold;
 
        /* Add service to database */
-       if (UAM_ERROR_NONE != _uam_db_insert_service_info(&service_number, svc, service->cycle)) {
+       if (UAM_ERROR_NONE != _uam_db_insert_service_info(&service_number,
+                                       svc, service->cycle, app_num)) {
                UAM_ERR("_uam_db_insert_service_info failed");
-               g_free(service);
+               __free_service_info(service);
                return UAM_ERROR_DB_FAILED;
        }
 
        services = g_slist_append(services, service);
 
        /* Send service registered event to application */
+       /* <TO-DO> send event only to particular sender */
        if (UAM_ERROR_NONE != _uam_manager_send_event(NULL,
                                UAM_EVENT_SERVICE_REGISTERED, g_variant_new("(is)",
                                        UAM_ERROR_NONE, service->name)))
@@ -3797,7 +4341,7 @@ int _uam_core_update_service(uam_service_info_s *svc)
        service->absence_threshold = svc->absence_threshold;
 
        /* Update service to database */
-       if (UAM_ERROR_NONE != _uam_db_update_service_info(service)) {
+       if (UAM_ERROR_NONE != _uam_db_update_service_info(service, svc->app_num)) {
                UAM_ERR("_uam_db_update_service_info failed");
                return UAM_ERROR_DB_FAILED;
        }
@@ -3806,43 +4350,49 @@ int _uam_core_update_service(uam_service_info_s *svc)
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_default_service(uam_service_info_s *service_info)
+int _uam_core_get_default_service(uam_service_info_s *service_info,
+                               const int app_num)
 {
        FUNC_ENTRY;
-       int ret;
+       int ret = UAM_ERROR_NONE;
        GSList *l;
        uam_db_service_info_t *service;
+       gchar *default_service_name = NULL;
 
        retv_if(NULL == service_info, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
 
-       l = g_slist_find_custom(services, UAM_SERVICE_DEFAULT, __compare_svc_name);
+       /* Get the default service name specific to app */
+       __get_default_service_name(app_num, &default_service_name);
+       UAM_DBG("default_service_name is [%s]", default_service_name);
 
+       l = g_slist_find_custom(services, default_service_name, __compare_svc_name);
        if (NULL == l) {
-               // insert default service
+               /* Register default service */
                memset(service_info, 0x00, sizeof(uam_service_info_s));
-               g_strlcpy(service_info->name, UAM_SERVICE_DEFAULT, UAM_SERVICE_MAX_STRING_LEN);
+
+               service_info->app_num = app_num;
+               g_strlcpy(service_info->name, default_service_name, UAM_SERVICE_MAX_STRING_LEN);
                service_info->presence_threshold = UAM_PRESENCE_THRESHOLD_DEFAULT;
                service_info->absence_threshold = UAM_ABSENCE_THRESHOLD_DEFAULT;
 
-               ret = _uam_core_register_service(service_info);
+               ret = _uam_core_register_service(service_info, service_info->app_num);
                if ((UAM_ERROR_NONE != ret) && (UAM_ERROR_ALREADY_REGISTERED != ret)) {
                        UAM_ERR("_uam_core_register_service failed with %s", _uam_manager_error_to_str(ret));
-                       return ret;
+                       goto done;
                }
-               l = g_slist_find_custom(services, UAM_SERVICE_DEFAULT, __compare_svc_name);
+       } else {
+               service = l->data;
+               __uam_copy_uam_db_service_info(service_info, service);
        }
 
-       retv_if(NULL == l, UAM_ERROR_INTERNAL);
-       service = l->data;
-
-       memset(service_info, 0x00, sizeof(uam_service_info_s));
-       g_strlcpy(service_info->name, service->name, UAM_SERVICE_MAX_STRING_LEN);
-
+done:
+       free_n_null(&default_service_name);
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return ret;
 }
 
-int _uam_core_unregister_service(const char *svc_name)
+int _uam_core_unregister_service(const char *svc_name, const int app_num)
 {
        FUNC_ENTRY;
        GSList *l;
@@ -3862,7 +4412,7 @@ int _uam_core_unregister_service(const char *svc_name)
        }
 
        /* Remove service from database */
-       if (UAM_ERROR_NONE != _uam_db_delete_service_info(service->name)) {
+       if (UAM_ERROR_NONE != _uam_db_delete_service_info(service->name, app_num)) {
                UAM_ERR("_uam_db_delete_service_info failed");
                return UAM_ERROR_DB_FAILED;
        }
@@ -3954,7 +4504,7 @@ static int __get_service_dev_list(
 }
 
 int _uam_core_get_service_devices(const char *svc_name,
-               int *count, uam_device_info_s **device_list)
+               int *count, uam_device_info_s **device_list, const int app_num)
 {
        FUNC_ENTRY;
        uam_db_service_info_t *service;
@@ -4017,7 +4567,7 @@ static void __get_service_user_list(
 }
 
 int _uam_core_get_service_users(const char *svc_name,
-               int *count, uam_user_info_s **user_list)
+               int *count, uam_user_info_s **user_list, const int app_num)
 {
        FUNC_ENTRY;
        uam_db_service_info_t *service;
@@ -4033,7 +4583,7 @@ int _uam_core_get_service_users(const char *svc_name,
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_services(int *count, uam_service_info_s **service_list)
+int _uam_core_get_services(int *count, uam_service_info_s **service_list, const int app_num)
 {
        FUNC_ENTRY;
        guint size;
@@ -4047,7 +4597,7 @@ int _uam_core_get_services(int *count, uam_service_info_s **service_list)
        for (l = services; l; l = g_slist_next(l)) {
                uam_db_service_info_t *db_info = l->data;
 
-               if (!db_info || !db_info->name)
+               if (!db_info || !db_info->name || db_info->app_num != app_num)
                        continue;
 
                g_strlcpy((*service_list)[*count].name,
@@ -4062,7 +4612,7 @@ int _uam_core_get_services(int *count, uam_service_info_s **service_list)
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_add_ibeacon_adv(unsigned int adv_len, const char *iadv)
+int _uam_core_add_ibeacon_adv(unsigned int adv_len, const char *iadv, const int app_num)
 {
        FUNC_ENTRY;
        int ret;
@@ -4070,7 +4620,7 @@ int _uam_core_add_ibeacon_adv(unsigned int adv_len, const char *iadv)
        UAM_INFO("adv_len = %u, iadv = 0x%0x:0x%0x:0x%0x", adv_len,
                iadv[0], iadv[1], iadv[2]);
 
-       ret = _uam_db_insert_adv_info(adv_len, iadv);
+       ret = _uam_db_insert_adv_info(adv_len, iadv, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_db_insert_adv_info failed");
                return ret;
@@ -4109,7 +4659,7 @@ void _uam_core_handle_status_changed(unsigned int sensor, void *info)
 }
 
 int _uam_core_add_payload(uam_ble_payload_s *payload,
-               const char *device_id, int tech_type)
+               const char *device_id, int tech_type, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
@@ -4122,7 +4672,7 @@ int _uam_core_add_payload(uam_ble_payload_s *payload,
        retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
        retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
 
-       // <TO-DO> check if payload already exist
+       /* <TO-DO> check if payload already exist */
        __print_payload(payload);
 
        tech_info = __uam_core_get_dev_tech_info(device_id, tech_type);
@@ -4133,10 +4683,11 @@ int _uam_core_add_payload(uam_ble_payload_s *payload,
        __uam_copy_uam_payload_info(db_payload, payload);
        db_payload->device_id = g_strdup(device_id);
        db_payload->tech_type = tech_type;
+       db_payload->app_num = app_num;
        payloads = g_slist_append(payloads, db_payload);
 
        /*** Add payload to database ***/
-       ret = _uam_db_insert_payload_info(device_id, tech_type, mac, payload);
+       ret = _uam_db_insert_payload_info(device_id, tech_type, mac, payload, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_db_insert_payload_info failed");
                return ret;
@@ -4147,7 +4698,7 @@ int _uam_core_add_payload(uam_ble_payload_s *payload,
 }
 
 int _uam_core_service_add_payload(uam_ble_payload_s *payload,
-               const char *svc_name)
+               const char *svc_name, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
@@ -4174,19 +4725,20 @@ int _uam_core_service_add_payload(uam_ble_payload_s *payload,
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
 
        svc_dev = _uam_core_find_svc_dev_info(db_payload->device_id,
-               db_payload->tech_type, service->name);
+               db_payload->tech_type, service->name, app_num);
        retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER);
 
        /**Update svc_dev*/
        _uam_core_update_svc_dev_info(db_payload->device_id, db_payload->tech_type,
-               service->name, -1, -1, payload);
+               service->name, -1, -1, payload, app_num);
 
        /* Set/update registered device list to plugins */
-       if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices))
+       if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices,
+                       __uam_core_tech_type_to_sensor(db_payload->tech_type)))
                UAM_ERR("_uam_pm_set_registered_devices failed");
 
        /*** Add payload service mapping to database ***/
-       ret = _uam_db_update_device_service_payload_info(payload, service->name);
+       ret = _uam_db_update_device_service_payload_info(payload, service->name, app_num);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_db_update_device_service_payload_info failed");
                return ret;
@@ -4196,7 +4748,7 @@ int _uam_core_service_add_payload(uam_ble_payload_s *payload,
        return UAM_ERROR_NONE;
 }
 
-int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list)
+int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list, const int app_num)
 {
        FUNC_ENTRY;
        guint size;
@@ -4212,6 +4764,9 @@ int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list)
 
                if (!db_info)
                        continue;
+               UAM_DBG("db_info->app_num is [%d]", db_info->app_num);
+               if (db_info->app_num != app_num)
+                       continue;
                __uam_copy_db_payload_info(&((*payload_list)[*count]), db_info);
                __print_db_payload(db_info);
                *count += 1;
index 71e10fb..383b5dd 100644 (file)
 #include "ua-manager-common.h"
 #include "ua-manager-database.h"
 
+#define VERSION 1
+
 #define UAM_DB_USERDATA_TABLE "userdata" /**< User data DB table name */
 #define UAM_DB_DEVICES_TABLE "devices" /**< Device DB table name */
 #define UAM_DB_SERVICES_TABLE "services" /**< Service DB table name */
 #define UAM_DB_DEVICE_SERVICES_TABLE "device_services" /**< Device services DB table name */
 #define UAM_DB_IBEACON_ADV_TABLE "ibeacon_adv" /**< iBeacon adv DB table name */
 #define UAM_DB_PAYLOADS_TABLE "payloads" /**< payload DB table name */
+#define UAM_DB_APPDATA_TABLE "apps" /**< apps DB table name */
+
 
 #define CREATE_USERDATA_TABLE "CREATE TABLE IF NOT EXISTS userdata ( " \
        "name TEXT, " \
        "user_id INTEGER PRIMARY KEY AUTOINCREMENT, " \
        "account TEXT, " \
-       "UNIQUE (account) " \
+       "app_num INTEGER, " \
+       "FOREIGN KEY(app_num) REFERENCES apps(app_num), " \
+       "UNIQUE (account, app_num) " \
        "); "
 
 #define CREATE_DEVICES_TABLE "CREATE TABLE IF NOT EXISTS devices ( " \
        "presence_state INTEGER, " \
        "os_type INTEGER, " \
        "discriminant INTEGER, " \
+       "app_num INTEGER, " \
        "FOREIGN KEY(user_id) REFERENCES userdata(user_id), " \
-       "UNIQUE (device_id, tech_type, address) " \
+       "FOREIGN KEY(app_num) REFERENCES apps(app_num), " \
+       "UNIQUE (device_id, tech_type, app_num) " \
        "); "
 
 #define CREATE_PAYLOADS_TABLE "CREATE TABLE IF NOT EXISTS payloads ( " \
        "device_icon TEXT, " \
        "user_data TEXT, " \
        "device_number INTEGER, " \
+       "app_num INTEGER, " \
        "FOREIGN KEY(device_number) REFERENCES devices(device_number), " \
-       "UNIQUE (primary_key, secondary_key, device_uid) " \
+       "FOREIGN KEY(app_num) REFERENCES apps(app_num), " \
+       "UNIQUE (primary_key, secondary_key, device_uid, app_num) " \
        "); "
 
 #define CREATE_SERVICES_TABLE "CREATE TABLE IF NOT EXISTS services ( " \
@@ -73,7 +83,9 @@
        "cycle INTEGER, " \
        "presence_threshold INTEGER, " \
        "absence_threshold INTEGER, " \
-       "UNIQUE (service_name) " \
+       "app_num INTEGER, " \
+       "FOREIGN KEY(app_num) REFERENCES apps(app_num), " \
+       "UNIQUE (service_name, app_num) " \
        "); "
 
 #define CREATE_DEVICE_SERVICES_TABLE "CREATE TABLE IF NOT EXISTS device_services ( " \
        "payload_number INTEGER, " \
        "discriminant INTEGER, " \
        "last_seen LONG, " \
+       "app_num INTEGER, " \
        "FOREIGN KEY(device_number) REFERENCES devices(device_number), " \
        "FOREIGN KEY(service_number) REFERENCES services(service_number), " \
        "FOREIGN KEY(payload_number) REFERENCES payloads(payload_number), " \
-       "PRIMARY KEY(device_number, service_number) " \
+       "FOREIGN KEY(app_num) REFERENCES apps(app_num), " \
+       "PRIMARY KEY(device_number, service_number, app_num) " \
        "); "
 
 #define CREATE_IBEACON_ADV_TABLE "CREATE TABLE IF NOT EXISTS ibeacon_adv ( " \
        "ibeacon_id INTEGER PRIMARY KEY AUTOINCREMENT, " \
        "ibeacon_adv TEXT, " \
        "adv_len INTEGER, " \
-       "UNIQUE (ibeacon_adv) " \
+       "app_num INTEGER, " \
+       "UNIQUE (ibeacon_adv, app_num) " \
        "); "
 
-#define VERSION 1
+#define CREATE_APPDATA_TABLE "CREATE TABLE IF NOT EXISTS apps ( " \
+       "app_num INTEGER PRIMARY KEY AUTOINCREMENT, " \
+       "app_key TEXT, " \
+       "UNIQUE (app_key) " \
+       "); "
 
 sqlite3 *database_handle;
 static sqlite3_stmt *select_version;
@@ -413,6 +432,9 @@ static int __uam_db_check_table_creation(void)
        if (!__uam_db_is_table_existing(UAM_DB_PAYLOADS_TABLE))
                retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_PAYLOADS_TABLE), error_code);
 
+       if (!__uam_db_is_table_existing(UAM_DB_APPDATA_TABLE))
+               retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_APPDATA_TABLE), error_code);
+
        UAM_DBG("Successfully verified table creation");
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -500,6 +522,7 @@ int _uam_db_initialize(void)
        EXEC(UAM_ERROR_NONE, _uam_device_service_db_initialize(), handle_error);
        EXEC(UAM_ERROR_NONE, _uam_adv_db_initialize(), handle_error);
        EXEC(UAM_ERROR_NONE, _uam_payload_db_initialize(), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_app_db_initialize(), handle_error);
 
        transaction_cnt = 0;
        FUNC_EXIT;
@@ -523,6 +546,9 @@ int _uam_db_deinitialize(void)
        _uam_device_service_db_deinitialize();
        _uam_adv_db_deinitialize();
        _uam_payload_db_deinitialize();
+       _uam_app_db_deinitialize();
+
+       sqlite3_close(database_handle);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -533,12 +559,13 @@ int _uam_db_clear(void)
        int error_code = UAM_ERROR_DB_FAILED;
        retv_if(NULL == database_handle, UAM_ERROR_DB_FAILED);
 
-       EXEC(UAM_ERROR_NONE, _uam_user_db_clear(), handle_error);
-       EXEC(UAM_ERROR_NONE, _uam_device_db_clear(), handle_error);
-       EXEC(UAM_ERROR_NONE, _uam_service_db_clear(), handle_error);
-       EXEC(UAM_ERROR_NONE, _uam_device_service_db_clear(), handle_error);
-       EXEC(UAM_ERROR_NONE, _uam_adv_db_clear(), handle_error);
-       EXEC(UAM_ERROR_NONE, _uam_payload_db_clear(), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_user_db_clear(-1), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_device_db_clear(-1), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_service_db_clear(-1), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_device_service_db_clear(-1), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_adv_db_clear(-1), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_payload_db_clear(-1), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_app_db_clear(), handle_error);
        UAM_DBG("Table data deleted ");
 
        error_code = UAM_ERROR_NONE;
index 9ee9ddf..f1ec061 100644 (file)
 
 #define SELECT_ALL_DEVICES "SELECT device_id, tech_type, address, " \
        "ip_address, last_seen, presence_state, os_type, " \
-       "user_id, discriminant FROM devices"
+       "user_id, discriminant, app_num FROM devices WHERE app_num = ?"
+
+#define SELECT_DEVICES_WITHOUT_APP_NUM "SELECT device_id, tech_type, address, " \
+       "ip_address, last_seen, presence_state, os_type, " \
+       "user_id, discriminant, app_num FROM devices"
 
 #define SELECT_DEVICE "SELECT device_id, tech_type, address, " \
        "ip_address, last_seen, presence_state, os_type, " \
        "user_id, discriminant FROM devices " \
-       "WHERE device_id = ? AND tech_type = ? AND address = ?"
+       "WHERE device_id = ? AND tech_type = ? AND address = ? AND app_num = ?"
 
 #define SELECT_DEVICE_NUMBER "SELECT device_number FROM devices " \
-       "WHERE device_id = ? AND tech_type = ? AND address = ?"
+       "WHERE device_id = ? AND tech_type = ? AND address = ? AND app_num = ?"
 
 #define INSERT_DEVICE "insert into devices " \
        "(device_id, tech_type, address, ip_address, last_seen, " \
-       "presence_state, os_type, user_id, discriminant)" \
-       "values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
+       "presence_state, os_type, user_id, discriminant, app_num)" \
+       "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
 
 #define UPDATE_LAST_SEEN "UPDATE devices " \
-       "SET last_seen = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
+       "SET last_seen = ? WHERE device_id = ? AND tech_type = ? AND address = ? AND app_num = ?"
 
 #define UPDATE_PRESENCE "UPDATE devices " \
-       "SET presence_state = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
+       "SET presence_state = ? WHERE device_id = ? AND tech_type = ? AND address = ? AND app_num = ?"
 
 #define UPDATE_IP_ADDRESS "UPDATE devices " \
-       "SET ip_address = ? WHERE device_id = ? AND tech_type = ?"
+       "SET ip_address = ? WHERE device_id = ? AND tech_type = ? AND app_num = ?"
 
 #define UPDATE_MAC_ADDRESS "UPDATE devices " \
-       "SET address = ? WHERE device_id = ? AND tech_type = ?"
+       "SET address = ? WHERE device_id = ? AND tech_type = ? AND app_num = ?"
 
 #define UPDATE_DEVICE "UPDATE devices " \
        "SET (os_type, discriminant, ip_address) = (?, ?, ?) " \
-       "WHERE device_id = ? AND tech_type = ? AND address = ?"
+       "WHERE device_id = ? AND tech_type = ? AND address = ? AND app_num = ?"
 
-#define DELETE_ALL_DEVICES "delete from devices "
+#define DELETE_ALL_DEVICES "delete from devices WHERE app_num = ?"
+
+#define DELETE_DEVICES_WITHOUT_APP_NUM "delete from devices"
 
 #define DELETE_DEVICE_INFO "delete from devices " \
-       "WHERE device_id = ? AND tech_type = ? AND address = ?"
+       "WHERE device_id = ? AND tech_type = ? AND address = ? AND app_num = ?"
 
 static void __uam_device_finalize_delete(void);
 
 /* DELETE statements */
 static sqlite3_stmt *delete_all_devices;
 static sqlite3_stmt *delete_device_info;
+static sqlite3_stmt *delete_devices_without_app_num;
 
 /* SELECT statements */
 static sqlite3_stmt *select_all_devices;
 static sqlite3_stmt *select_device;
 static sqlite3_stmt *select_device_number;
+static sqlite3_stmt *select_devices_without_app_num;
 
 /* UPDATE statements */
 static sqlite3_stmt *update_last_seen;
@@ -91,6 +99,7 @@ static void __uam_device_finalize_delete(void)
 
        FINALIZE(delete_all_devices);
        FINALIZE(delete_device_info);
+       FINALIZE(delete_devices_without_app_num);
 
        FUNC_EXIT;
 }
@@ -102,6 +111,7 @@ static void __uam_device_finalize_select(void)
        FINALIZE(select_all_devices);
        FINALIZE(select_device);
        FINALIZE(select_device_number);
+       FINALIZE(select_devices_without_app_num);
 
        FUNC_EXIT;
 }
@@ -140,9 +150,11 @@ static int __uam_device_prepare_delete(sqlite3 *db)
        }
 
        PREPARE_QUERY(rc, db, delete_all_devices,
-               DELETE_ALL_DEVICES,     __uam_device_finalize_delete);
+               DELETE_ALL_DEVICES, __uam_device_finalize_delete);
        PREPARE_QUERY(rc, db, delete_device_info,
                DELETE_DEVICE_INFO, __uam_device_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_devices_without_app_num,
+               DELETE_DEVICES_WITHOUT_APP_NUM, __uam_device_finalize_delete);
 
        initialized = 1;
        FUNC_EXIT;
@@ -166,6 +178,8 @@ static int __uam_device_prepare_select(sqlite3 *db)
                SELECT_DEVICE, __uam_device_finalize_select);
        PREPARE_QUERY(rc, db, select_device_number,
                SELECT_DEVICE_NUMBER, __uam_device_finalize_select);
+       PREPARE_QUERY(rc, db, select_devices_without_app_num,
+               SELECT_DEVICES_WITHOUT_APP_NUM, __uam_device_finalize_select);
 
        initialized = 1;
        FUNC_EXIT;
@@ -256,7 +270,6 @@ int _uam_device_db_deinitialize(void)
        retv_if(NULL == database_handle, UAM_ERROR_NONE);
 
        __uam_device_table_devicesinfo_finalize();
-       sqlite3_close(database_handle);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -278,7 +291,7 @@ handle_error:
 }
 
 int _uam_device_db_update_device_ip_address(char *device_id, int tech_type,
-       char *ip_address)
+       char *ip_address, const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_ip_address;
@@ -286,6 +299,7 @@ int _uam_device_db_update_device_ip_address(char *device_id, int tech_type,
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == ip_address, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, ip_address, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
@@ -293,6 +307,8 @@ int _uam_device_db_update_device_ip_address(char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -311,7 +327,7 @@ handle_error:
 }
 
 int _uam_device_db_update_device_mac_address(char *device_id, int tech_type,
-       char *address)
+       char *address, const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_mac_address;
@@ -319,6 +335,7 @@ int _uam_device_db_update_device_mac_address(char *device_id, int tech_type,
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, address, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
@@ -326,6 +343,8 @@ int _uam_device_db_update_device_mac_address(char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -344,7 +363,7 @@ handle_error:
 }
 
 int _uam_device_db_update_device_presence(char *device_id, int tech_type,
-       char *address, int presence_state)
+       char *address, int presence_state, const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_presence;
@@ -352,6 +371,7 @@ int _uam_device_db_update_device_presence(char *device_id, int tech_type,
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_int(stmt, 1, presence_state),
                error_code, handle_error);
@@ -361,6 +381,8 @@ int _uam_device_db_update_device_presence(char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 5, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -379,7 +401,7 @@ handle_error:
 }
 
 int _uam_device_db_update_device_last_seen(char *device_id, int tech_type,
-                                         char *address, unsigned long long last_seen)
+                                         char *address, unsigned long long last_seen, const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_last_seen;
@@ -387,6 +409,7 @@ int _uam_device_db_update_device_last_seen(char *device_id, int tech_type,
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_int64(stmt, 1, last_seen),
                error_code, handle_error);
@@ -396,6 +419,8 @@ int _uam_device_db_update_device_last_seen(char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 5, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -414,7 +439,7 @@ handle_error:
 }
 
 int _uam_device_db_update_device(char *device_id, int tech_type,
-       char *address, char *ip, char os_type, char discriminant)
+       char *address, char *ip, char os_type, char discriminant, const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_device;
@@ -422,6 +447,7 @@ int _uam_device_db_update_device(char *device_id, int tech_type,
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_int(stmt, 1, os_type),
                error_code, handle_error);
@@ -435,6 +461,9 @@ int _uam_device_db_update_device(char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 6, address, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 7, app_num),
+               error_code, handle_error);
+
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -453,7 +482,7 @@ handle_error:
 }
 
 int _uam_device_db_insert_device_info(int user_id,
-       const uam_device_info_s *dev_info, int presence_state, unsigned long long last_seen)
+       const uam_device_info_s *dev_info, int presence_state, unsigned long long last_seen, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -461,11 +490,12 @@ int _uam_device_db_insert_device_info(int user_id,
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == dev_info, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
-       UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d", dev_info->device_id,
+       UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d-%d", dev_info->device_id,
                        dev_info->type, dev_info->mac, dev_info->ipv4_addr,
                        last_seen, presence_state, dev_info->operating_system, dev_info->discriminant,
-                       user_id);
+                       user_id, app_num);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, dev_info->device_id, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
@@ -485,6 +515,8 @@ int _uam_device_db_insert_device_info(int user_id,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 9, dev_info->discriminant),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 10, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -503,13 +535,15 @@ handle_error:
 }
 
 int _uam_device_db_delete_device_info(const char *device_id, int tech_type,
-       const char *address)
+       const char *address, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = delete_device_info;
        int sql_ret = SQLITE_OK;
 
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
+
        UAM_INFO("DeviceId: %s", device_id);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, device_id, -1, SQLITE_STATIC),
@@ -518,9 +552,12 @@ int _uam_device_db_delete_device_info(const char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
+
 
        retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
-       error_code = _uam_db_delete_device_service_mapping(device_id, tech_type, address);
+       error_code = _uam_db_delete_device_service_mapping(device_id, tech_type, address, app_num);
        if (UAM_ERROR_NONE != error_code) {
                UAM_ERR("Failed to delete device service mapping");
                __uam_db_end_transaction(0);
@@ -546,7 +583,7 @@ handle_error:
 }
 
 int _uam_device_db_get_device(char *device_id, int tech_type, char *address,
-               db_device_info_t *info)
+               db_device_info_t *info, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -554,6 +591,7 @@ int _uam_device_db_get_device(char *device_id, int tech_type, char *address,
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == info, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        UAM_INFO("device id: %s", device_id);
 
@@ -563,6 +601,9 @@ int _uam_device_db_get_device(char *device_id, int tech_type, char *address,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
+
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -604,7 +645,7 @@ handle_error:
 }
 
 int _uam_db_get_device_number(const char *device_id, int tech_type,
-       const char *address, int *device_number)
+       const char *address, int *device_number, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -613,6 +654,7 @@ int _uam_db_get_device_number(const char *device_id, int tech_type,
 
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        UAM_INFO("UserId: %s", device_id);
 
@@ -622,6 +664,8 @@ int _uam_db_get_device_number(const char *device_id, int tech_type,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
 
        do {
                sql_ret = sqlite3_step(stmt);
@@ -647,14 +691,23 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_device_db_get_all_devices(void)
+GSList *_uam_device_db_get_all_devices(const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_all_devices;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *device_list = NULL;
        db_device_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = select_devices_without_app_num;
+       } else {
+               stmt = select_all_devices;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        do {
                sql_ret = sqlite3_step(stmt);
 
@@ -675,8 +728,9 @@ GSList *_uam_device_db_get_all_devices(void)
                        info->dev_info.operating_system = sqlite3_column_int(stmt, 6);
                        info->user_id = sqlite3_column_int(stmt, 7);
                        info->dev_info.discriminant = sqlite3_column_int(stmt, 8);
+                       info->app_num = sqlite3_column_int(stmt, 9);
 
-                       UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d",
+                       UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d-%d",
                                info->dev_info.device_id,
                                info->dev_info.type,
                                info->dev_info.mac,
@@ -685,28 +739,40 @@ GSList *_uam_device_db_get_all_devices(void)
                                info->presence_state,
                                info->dev_info.operating_system,
                                info->user_id,
-                               info->dev_info.discriminant);
+                               info->dev_info.discriminant,
+                               info->app_num);
 
-                       device_list = g_slist_append(device_list, info);
+                               device_list = g_slist_append(device_list, info);
                        break;
                case SQLITE_ERROR:
                default:
                        UAM_ERR("Failed to enumerate device info [%d:%s]",
                                sql_ret, sqlite3_errmsg(database_handle));
-               }
+                       }
        } while (sql_ret == SQLITE_ROW);
 
+handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_device_db_get_all_devices failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return device_list;
 }
 
-int _uam_device_db_clear(void)
+int _uam_device_db_clear(const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_all_devices;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = delete_devices_without_app_num;
+       } else {
+               stmt = delete_all_devices;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
                UAM_ERR("Failed to delete device data [%d:%s]",
index db58c11..2667848 100644 (file)
 #include "ua-manager-common.h"
 #include "ua-manager-database.h"
 
-#define SELECT_SERVICE "SELECT S.service_number, S.service_name, S.cycle " \
+#define SELECT_SERVICE "SELECT S.service_number, S.service_name, S.cycle, S.app_num " \
+       "FROM device_services as D JOIN services as S USING(service_number) " \
+       "where D.device_number = ? and D.app_num = ?"
+
+#define SELECT_SERVICE_WITHOUT_APP_NUM "SELECT S.service_number, S.service_name, S.cycle, S.app_num " \
        "FROM device_services as D JOIN services as S USING(service_number) " \
        "where D.device_number = ?"
 
 #define SELECT_DEVICE_SERVICES_COUNT "SELECT count(*) " \
+       "FROM device_services where device_number = ? AND app_num = ?"
+
+#define SELECT_DEVICE_SERVICES_COUNT_WITHOUT_APP_NUM "SELECT count(*) " \
        "FROM device_services where device_number = ?"
 
 #define SELECT_DEVICE_SERVICES_PAYLOADS \
        "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen, " \
        "P.primary_key, P.secondary_key, P.device_uid_len, P.device_uid, P.device_icon " \
        "FROM device_services as X JOIN services as S USING(service_number) JOIN devices as D " \
+       "USING(device_number) JOIN payloads as P USING(payload_number) where D.app_num = ?"
+
+#define SELECT_DEVICE_SERVICES_PAYLOADS_WITHOUT_APP_NUM \
+       "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen, " \
+       "P.primary_key, P.secondary_key, P.device_uid_len, P.device_uid, P.device_icon " \
+       "FROM device_services as X JOIN services as S USING(service_number) JOIN devices as D " \
        "USING(device_number) JOIN payloads as P USING(payload_number)"
 
 #define SELECT_DEVICE_SERVICES \
-       "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen " \
+       "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen, X.app_num " \
+       "FROM device_services as X JOIN services as S USING(service_number) JOIN devices as D " \
+       "USING(device_number) where D.app_num = ?"
+
+#define SELECT_DEVICE_SERVICES_WITHOUT_APP_NUM \
+       "SELECT D.device_id, D.tech_type, S.service_name, X.discriminant, X.last_seen, X.app_num " \
        "FROM device_services as X JOIN services as S USING(service_number) JOIN devices as D " \
        "USING(device_number)"
 
 #define INSERT_DEVICE_SERVICE "insert into device_services " \
-       "(device_number, service_number, discriminant, last_seen) values (?, ?, ?, ?)"
+       "(device_number, service_number, discriminant, last_seen, app_num) values (?, ?, ?, ?, ?)"
+
+#define DELETE_ALL_DEVICE_SERVICES "delete from device_services where app_num = ?"
 
-#define DELETE_ALL_DEVICE_SERVICES "delete from device_services "
+#define DELETE_ALL_DEVICE_SERVICES_WITHOUT_APP_NUM "delete from device_services"
 
 #define DELETE_DEVICE_SERVICE "delete from device_services " \
+       "WHERE device_number = ? and service_number = ? and app_num = ?"
+
+#define DELETE_DEVICE_SERVICE_WITHOUT_APP_NUM "delete from device_services " \
        "WHERE device_number = ? and service_number = ?"
 
 #define DELETE_DEVICE "delete from device_services " \
+       "WHERE device_number = ? and app_num = ?"
+
+#define DELETE_DEVICE_WITHOUT_APP_NUM "delete from device_services " \
        "WHERE device_number = ?"
 
 #define DELETE_SERVICE "delete from device_services " \
+       "WHERE service_number = ? and app_num = ?"
+
+#define DELETE_SERVICE_WITHOUT_APP_NUM "delete from device_services " \
        "WHERE service_number = ?"
 
 #define UPDATE_DEVICE_SERVICE_DISCRIMINANT "UPDATE device_services " \
-       "SET discriminant = ? WHERE device_number = ? AND service_number = ?"
+       "SET discriminant = ? WHERE device_number = ? AND service_number = ? AND app_num = ?"
 
 #define UPDATE_DEVICE_SERVICE_LAST_SEEN "UPDATE device_services " \
-       "SET last_seen = ? WHERE device_number = ? AND service_number = ?"
+       "SET last_seen = ? WHERE device_number = ? AND service_number = ? AND app_num = ?"
 
 #define UPDATE_DEVICE_SERVICE_PAYLOAD "UPDATE device_services " \
-       "SET payload_number = ? WHERE device_number = ? AND service_number = ?"
+       "SET payload_number = ? WHERE device_number = ? AND service_number = ? AND app_num = ?"
 
 /* DELETE statements */
 static sqlite3_stmt *delete_all_device_services;
 static sqlite3_stmt *delete_service;
 static sqlite3_stmt *delete_device_service;
 static sqlite3_stmt *delete_device;
+static sqlite3_stmt *delete_all_device_services_without_app_num;
+static sqlite3_stmt *delete_service_without_app_num;
+static sqlite3_stmt *delete_device_service_without_app_num;
+static sqlite3_stmt *delete_device_without_app_num;
+
 
 /* SELECT statements */
 static sqlite3_stmt *select_service;
 static sqlite3_stmt *select_device_services_count;
 static sqlite3_stmt *select_device_services;
 static sqlite3_stmt *select_device_services_payloads;
+static sqlite3_stmt *select_service_without_app_num;
+static sqlite3_stmt *select_device_services_count_without_app_num;
+static sqlite3_stmt *select_device_services_without_app_num;
+static sqlite3_stmt *select_device_services_payloads_without_app_num;
+
 
 /* INSERT statements */
 static sqlite3_stmt *insert_device_service;
@@ -95,6 +134,10 @@ static void __uam_device_service_finalize_delete(void)
        FINALIZE(delete_service);
        FINALIZE(delete_device_service);
        FINALIZE(delete_device);
+       FINALIZE(delete_all_device_services_without_app_num);
+       FINALIZE(delete_service_without_app_num);
+       FINALIZE(delete_device_service_without_app_num);
+       FINALIZE(delete_device_without_app_num);
 
        FUNC_EXIT;
 }
@@ -107,6 +150,10 @@ static void __uam_device_service_finalize_select(void)
        FINALIZE(select_device_services_count);
        FINALIZE(select_device_services);
        FINALIZE(select_device_services_payloads);
+       FINALIZE(select_service_without_app_num);
+       FINALIZE(select_device_services_count_without_app_num);
+       FINALIZE(select_device_services_without_app_num);
+       FINALIZE(select_device_services_payloads_without_app_num);
 
        FUNC_EXIT;
 }
@@ -150,6 +197,14 @@ static int __uam_device_service_prepare_delete(sqlite3 *db)
                DELETE_DEVICE_SERVICE, __uam_device_service_finalize_delete);
        PREPARE_QUERY(rc, db, delete_device,
                DELETE_DEVICE, __uam_device_service_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_all_device_services_without_app_num,
+               DELETE_ALL_DEVICE_SERVICES_WITHOUT_APP_NUM, __uam_device_service_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_service_without_app_num,
+               DELETE_SERVICE_WITHOUT_APP_NUM, __uam_device_service_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_device_service_without_app_num,
+               DELETE_DEVICE_SERVICE_WITHOUT_APP_NUM, __uam_device_service_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_device_without_app_num,
+               DELETE_DEVICE_WITHOUT_APP_NUM, __uam_device_service_finalize_delete);
 
        initialized = 1;
        FUNC_EXIT;
@@ -175,6 +230,14 @@ static int __uam_device_service_prepare_select(sqlite3 *db)
                SELECT_DEVICE_SERVICES, __uam_device_service_finalize_select);
        PREPARE_QUERY(rc, db, select_device_services_payloads,
                SELECT_DEVICE_SERVICES_PAYLOADS, __uam_device_service_finalize_select);
+       PREPARE_QUERY(rc, db, select_service_without_app_num,
+               SELECT_SERVICE_WITHOUT_APP_NUM, __uam_device_service_finalize_select);
+       PREPARE_QUERY(rc, db, select_device_services_count_without_app_num,
+               SELECT_DEVICE_SERVICES_COUNT_WITHOUT_APP_NUM, __uam_device_service_finalize_select);
+       PREPARE_QUERY(rc, db, select_device_services_without_app_num,
+               SELECT_DEVICE_SERVICES_WITHOUT_APP_NUM, __uam_device_service_finalize_select);
+       PREPARE_QUERY(rc, db, select_device_services_payloads_without_app_num,
+               SELECT_DEVICE_SERVICES_PAYLOADS_WITHOUT_APP_NUM, __uam_device_service_finalize_select);
 
        initialized = 1;
        FUNC_EXIT;
@@ -262,7 +325,6 @@ int _uam_device_service_db_deinitialize(void)
        retv_if(NULL == database_handle, UAM_ERROR_NONE);
 
        __uam_device_service_table_devicesinfo_finalize();
-       sqlite3_close(database_handle);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -285,7 +347,7 @@ handle_error:
 }
 
 int _uam_db_insert_service(int device_number, int service_number,
-       gboolean discriminant, unsigned long long last_seen)
+       gboolean discriminant, unsigned long long last_seen, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -298,6 +360,8 @@ int _uam_db_insert_service(int device_number, int service_number,
        DB_ACTION(sqlite3_bind_int(stmt, 2, service_number), error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 3, discriminant), error_code, handle_error);
        DB_ACTION(sqlite3_bind_int64(stmt, 4, last_seen), error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 5, app_num), error_code, handle_error);
+
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -307,8 +371,8 @@ int _uam_db_insert_service(int device_number, int service_number,
                goto handle_error;
        }
 
-       UAM_DBG("Device-service info inserted [%d] [%d] [%d] [%llu]", device_number,
-                       service_number, discriminant, last_seen);
+       UAM_DBG("Device-service info inserted [%d] [%d] [%d] [%llu] [%d]", device_number,
+                       service_number, discriminant, last_seen, app_num);
 
 handle_error:
        sqlite3_reset(stmt);
@@ -318,34 +382,33 @@ handle_error:
 
 int _uam_db_insert_device_service_info(const char *device_id, int tech_type,
        const char *address, const char *service_name, int cycle, gboolean discriminant,
-       unsigned long long last_seen)
+       unsigned long long last_seen, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
        db_service_info_t info;
        int service_number = 0;
 
-       UAM_INFO("%s-%d-%s-%s-%d", device_id, tech_type, address, service_name, cycle);
+       UAM_INFO("%s-%d-%s-%s-%d-%d", device_id, tech_type, address, service_name, cycle, app_num);
 
        int device_number = 0;
-       ret = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       ret = _uam_db_get_device_number(device_id, tech_type, address,
+               &device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
-
-       ret = _uam_db_get_service_info(service_name, &info);
+       ret = _uam_db_get_service_info(service_name, &info, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
        service_number = info.service_number;
-
        UAM_INFO("%d-%d", device_number, service_number);
 
-       ret = _uam_db_insert_service(device_number, service_number, discriminant, last_seen);
+       ret = _uam_db_insert_service(device_number, service_number, discriminant, last_seen, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       UAM_DBG("Device-service info inserted [%d] [%d] [%d] [%llu]", device_number,
-                       service_number, discriminant, last_seen);
+       UAM_DBG("Device-service info inserted [%d] [%d] [%d] [%llu] [%d]", device_number,
+                       service_number, discriminant, last_seen, app_num);
 
 handle_error:
        FUNC_EXIT;
@@ -353,7 +416,7 @@ handle_error:
 }
 
 static int __uam_db_update_discriminant(int device_number,
-       int service_number, gboolean discriminant)
+       int service_number, gboolean discriminant, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -367,6 +430,7 @@ static int __uam_db_update_discriminant(int device_number,
                        error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 3, service_number),
                        error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num), error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -376,8 +440,8 @@ static int __uam_db_update_discriminant(int device_number,
                goto handle_error;
        }
 
-       UAM_DBG("Device-service info updated [%d] [%d] [%d] ",
-               device_number, service_number, discriminant);
+       UAM_DBG("Device-service info updated [%d] [%d] [%d] [%d]",
+               device_number, service_number, discriminant, app_num);
 
 handle_error:
        sqlite3_reset(stmt);
@@ -386,7 +450,7 @@ handle_error:
 }
 
 static int __uam_db_update_last_seen(int device_number,
-       int service_number, unsigned long long last_seen)
+       int service_number, unsigned long long last_seen, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -400,6 +464,7 @@ static int __uam_db_update_last_seen(int device_number,
                        error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 3, service_number),
                        error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num), error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -409,8 +474,8 @@ static int __uam_db_update_last_seen(int device_number,
                goto handle_error;
        }
 
-       UAM_DBG("Device-service info updated [%d] [%d] [%llu]",
-               device_number, service_number, last_seen);
+       UAM_DBG("Device-service info updated [%d] [%d] [%llu] [%d]",
+               device_number, service_number, last_seen, app_num);
 
 handle_error:
        sqlite3_reset(stmt);
@@ -420,7 +485,7 @@ handle_error:
 
 int _uam_db_update_device_service_discriminant(const char *device_id,
                int tech_type, const char *address, const char *service_name,
-               gboolean discriminant)
+               gboolean discriminant, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
@@ -430,21 +495,22 @@ int _uam_db_update_device_service_discriminant(const char *device_id,
        UAM_INFO("%s-%d-%s-%s-%d", device_id, tech_type, address, service_name, discriminant);
 
        int device_number = 0;
-       ret = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       ret = _uam_db_get_device_number(device_id, tech_type, address,
+               &device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       ret = _uam_db_get_service_info(service_name, &info);
+       ret = _uam_db_get_service_info(service_name, &info, app_num);
        if (UAM_ERROR_NONE == ret)
                service_number = info.service_number;
        else
                goto handle_error;
 
-       ret = __uam_db_update_discriminant(device_number, service_number, discriminant);
+       ret = __uam_db_update_discriminant(device_number, service_number, discriminant, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       UAM_DBG("Device-service info updated [%d] [%d]", device_number, service_number);
+       UAM_DBG("Device-service info updated [%d] [%d] [%d]", device_number, service_number, app_num);
 
 handle_error:
        FUNC_EXIT;
@@ -453,7 +519,7 @@ handle_error:
 
 int _uam_db_update_device_service_last_seen(const char *device_id,
        int tech_type, const char *address, const char *service_name,
-       unsigned long long last_seen)
+       unsigned long long last_seen, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
@@ -465,22 +531,22 @@ int _uam_db_update_device_service_last_seen(const char *device_id,
 
        int device_number = 0;
        ret = _uam_db_get_device_number(device_id, tech_type, address,
-                       &device_number);
+                       &device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       ret = _uam_db_get_service_info(service_name, &info);
+       ret = _uam_db_get_service_info(service_name, &info, app_num);
        if (UAM_ERROR_NONE == ret)
                service_number = info.service_number;
        else
                goto handle_error;
 
-       ret = __uam_db_update_last_seen(device_number, service_number, last_seen);
+       ret = __uam_db_update_last_seen(device_number, service_number, last_seen, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       UAM_DBG("Device-service last_seen updated [%d] [%d]", device_number,
-                       service_number);
+       UAM_DBG("Device-service last_seen updated [%d] [%d] [%d]", device_number,
+                       service_number, app_num);
 
 handle_error:
        FUNC_EXIT;
@@ -488,12 +554,20 @@ handle_error:
 }
 
 
-int _uam_device_service_db_clear(void)
+int _uam_device_service_db_clear(const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_all_device_services;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = delete_all_device_services_without_app_num;
+       } else {
+               stmt = delete_all_device_services;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
                UAM_ERR("Failed to delete device data [%d:%s]",
@@ -505,20 +579,26 @@ int _uam_device_service_db_clear(void)
        UAM_DBG("User data deleted ");
 
 handle_error:
-
+       UAM_ERR("UAM_ERROR_DB_FAILED bind command failed");
        sqlite3_reset(stmt);
        return error_code;
 }
 
-int _uam_db_delete_service_number(int service_number)
+int _uam_db_delete_service_number(int service_number, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_service;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
-
        UAM_INFO("Service Number: %d", service_number);
 
+       if (app_num == -1) {
+               stmt = delete_service_without_app_num;
+       } else {
+               stmt = delete_service;
+               DB_ACTION(sqlite3_bind_int(stmt, 2, app_num), error_code, handle_error);
+       }
+
        DB_ACTION(sqlite3_bind_int(stmt, 1, service_number), error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
@@ -536,15 +616,22 @@ handle_error:
 }
 
 int _uam_db_delete_device_service_number(int device_number,
-       int service_number)
+       int service_number, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_device_service;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
        UAM_INFO("Device Number: %d Service Number: %d", device_number, service_number);
 
+       if (app_num == -1) {
+               stmt = delete_device_service_without_app_num;
+       } else {
+               stmt = delete_device_service;
+               DB_ACTION(sqlite3_bind_int(stmt, 3, app_num), error_code, handle_error);
+       }
+
        DB_ACTION(sqlite3_bind_int(stmt, 1, device_number), error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 2, service_number), error_code, handle_error);
 
@@ -562,15 +649,22 @@ handle_error:
        return error_code;
 }
 
-int _uam_db_delete_device_number(int device_number)
+int _uam_db_delete_device_number(int device_number, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_device;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
        UAM_INFO("Device Number: %d", device_number);
 
+       if (app_num == -1) {
+               stmt = delete_device_without_app_num;
+       } else {
+               stmt = delete_device;
+               DB_ACTION(sqlite3_bind_int(stmt, 2, app_num), error_code, handle_error);
+       }
+
        DB_ACTION(sqlite3_bind_int(stmt, 1, device_number), error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
@@ -587,7 +681,7 @@ handle_error:
        return error_code;
 }
 
-int _uam_db_delete_device_service(const char *service_name)
+int _uam_db_delete_device_service(const char *service_name, const int app_num)
 {
        FUNC_ENTRY;
        db_service_info_t info;
@@ -596,14 +690,14 @@ int _uam_db_delete_device_service(const char *service_name)
        int error_code = UAM_ERROR_NONE;
        UAM_INFO("%s", service_name);
 
-       ret = _uam_db_get_service_info(service_name, &info);
+       ret = _uam_db_get_service_info(service_name, &info, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
        service_number = info.service_number;
 
        UAM_INFO("%d", service_number);
 
-       ret = _uam_db_delete_service_number(service_number);
+       ret = _uam_db_delete_service_number(service_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
@@ -615,7 +709,7 @@ handle_error:
 }
 
 int _uam_db_delete_device_service_info(const char *device_id, int tech_type,
-               const char *address, const char *service_name)
+               const char *address, const char *service_name, const int app_num)
 {
        FUNC_ENTRY;
        db_service_info_t info;
@@ -626,18 +720,19 @@ int _uam_db_delete_device_service_info(const char *device_id, int tech_type,
 
        UAM_INFO("%s-%d-%s-%s", device_id, tech_type, address, service_name);
 
-       ret = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       ret = _uam_db_get_device_number(device_id, tech_type, address,
+               &device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       ret = _uam_db_get_service_info(service_name, &info);
+       ret = _uam_db_get_service_info(service_name, &info, app_num);
        if (UAM_ERROR_NONE != ret)
                        goto handle_error;
        service_number = info.service_number;
 
        UAM_INFO("%d-%d", device_number, service_number);
 
-       ret = _uam_db_delete_device_service_number(device_number, service_number);
+       ret = _uam_db_delete_device_service_number(device_number, service_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
@@ -649,7 +744,7 @@ handle_error:
 }
 
 int _uam_db_delete_device_service_mapping(const char *device_id,
-       int tech_type, const char *address)
+       int tech_type, const char *address, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
@@ -658,13 +753,14 @@ int _uam_db_delete_device_service_mapping(const char *device_id,
 
        UAM_INFO("%s-%d-%s", device_id, tech_type, address);
 
-       ret = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       ret = _uam_db_get_device_number(device_id, tech_type, address,
+               &device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
        UAM_INFO("%d", device_number);
 
-       ret = _uam_db_delete_device_number(device_number);
+       ret = _uam_db_delete_device_number(device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
@@ -675,14 +771,22 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_db_get_service(int device_number)
+GSList *_uam_db_get_service(int device_number, const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_service;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *service_list = NULL;
        db_service_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = select_service_without_app_num;
+       } else {
+               stmt = select_service;
+               DB_ACTION(sqlite3_bind_int(stmt, 2, app_num), error_code, handle_error);
+       }
+
        if (sqlite3_bind_int(stmt, 1, device_number) != SQLITE_OK)
                goto handle_error;
 
@@ -697,8 +801,9 @@ GSList *_uam_db_get_service(int device_number)
                        g_strlcpy(info->service_name, (char *)sqlite3_column_text(stmt, 1),
                                UAM_SERVICE_MAX_STRING_LEN);
                        info->cycle = sqlite3_column_int(stmt, 2);
+                       info->app_num = sqlite3_column_int(stmt, 3);
 
-                       UAM_INFO("%d-%s-%d", info->service_number, info->service_name, info->cycle);
+                       UAM_INFO("%d-%s-%d-%d", info->service_number, info->service_name, info->cycle, info->app_num);
                        service_list = g_slist_append(service_list, info);
                        break;
                case SQLITE_ERROR:
@@ -709,25 +814,28 @@ GSList *_uam_db_get_service(int device_number)
        } while (sql_ret == SQLITE_ROW);
 
 handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_db_get_service failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return service_list;
 }
 
 GSList *_uam_db_get_device_services(const char *device_id, int tech_type,
-       const char *address)
+       const char *address, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
        int device_number = 0;
-       UAM_INFO("%s-%d-%s", device_id, tech_type, address);
+       UAM_INFO("%s-%d-%s-%d", device_id, tech_type, address, app_num);
 
        GSList *service_list = NULL;
-       ret = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       ret = _uam_db_get_device_number(device_id, tech_type, address,
+               &device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       service_list = _uam_db_get_service(device_number);
+       service_list = _uam_db_get_service(device_number, app_num);
 
 handle_error:
        FUNC_EXIT;
@@ -735,7 +843,7 @@ handle_error:
 }
 
 int _uam_db_get_device_services_count(const char *device_id, int tech_type,
-       const char *address, int *svc_count)
+       const char *address, const int app_num, int *svc_count)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -747,10 +855,18 @@ int _uam_db_get_device_services_count(const char *device_id, int tech_type,
        retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
        UAM_INFO("%s-%d-%s", device_id, tech_type, address);
 
-       error_code = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       error_code = _uam_db_get_device_number(device_id, tech_type, address,
+                       &device_number, app_num);
        if (UAM_ERROR_NONE != error_code)
                goto handle_error;
 
+       if (app_num == -1) {
+               stmt = select_device_services_count_without_app_num;
+       } else {
+               stmt = select_device_services_count;
+               DB_ACTION(sqlite3_bind_int(stmt, 2, app_num), error_code, handle_error);
+       }
+
        DB_ACTION(sqlite3_bind_int(stmt, 1, device_number),
                error_code, handle_error);
 
@@ -778,16 +894,24 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_db_get_service_devices_payloads_info()
+GSList *_uam_db_get_service_devices_payloads_info(const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_device_services_payloads;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *svc_dev_list = NULL;
        db_svc_dev_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
        char *buf = NULL;
        int uid_len = 0;
 
+       if (app_num == -1) {
+               stmt = select_device_services_payloads_without_app_num;
+       } else {
+               stmt = select_device_services_payloads;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num), error_code, handle_error);
+       }
+
        do {
                sql_ret = sqlite3_step(stmt);
 
@@ -815,7 +939,7 @@ GSList *_uam_db_get_service_devices_payloads_info()
                                memcpy(info->payload_info.device_uid, buf,
                                        info->payload_info.device_uid_len);
                        buf = (char *)sqlite3_column_text(stmt, 9);
-                       info->payload_info.device_icon = buf ? *buf : 1;
+                       info->payload_info.device_type = buf ? *buf : 1;
 
                        UAM_INFO("device id:%s-%d-%s-%d-%llu " \
                                "primary key: %d secondary key: %d",
@@ -835,21 +959,30 @@ GSList *_uam_db_get_service_devices_payloads_info()
                                sql_ret, sqlite3_errmsg(database_handle));
                }
        } while (sql_ret == SQLITE_ROW);
-
+handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_db_get_service_devices_payloads_info failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return svc_dev_list;
 }
 
-
-GSList *_uam_db_get_service_devices_info()
+GSList *_uam_db_get_service_devices_info(const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_device_services;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *svc_dev_list = NULL;
        db_svc_dev_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = select_device_services_without_app_num;
+       } else {
+               stmt = select_device_services;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num), error_code, handle_error);
+       }
+
        do {
                sql_ret = sqlite3_step(stmt);
 
@@ -866,13 +999,15 @@ GSList *_uam_db_get_service_devices_info()
                                UAM_SERVICE_MAX_STRING_LEN);
                        info->discriminant = sqlite3_column_int(stmt, 3);
                        info->last_seen = sqlite3_column_int64(stmt, 4);
+                       info->app_num = sqlite3_column_int(stmt, 5);
 
-                       UAM_INFO("device id:%s-%d-%s-%d-%llu ",
+                       UAM_INFO("device id:%s-%d-%s-%d-%llu-%d ",
                                info->device_id,
                                info->type,
                                info->svc,
                                info->discriminant,
-                               info->last_seen);
+                               info->last_seen,
+                               info->app_num);
 
                        svc_dev_list = g_slist_append(svc_dev_list, info);
                        break;
@@ -883,13 +1018,16 @@ GSList *_uam_db_get_service_devices_info()
                }
        } while (sql_ret == SQLITE_ROW);
 
+handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_db_get_service_devices_info failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return svc_dev_list;
 }
 
 static int __uam_db_update_dev_svc_payload(int device_number,
-       int service_number, int payload_number)
+       int service_number, int payload_number, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -902,6 +1040,7 @@ static int __uam_db_update_dev_svc_payload(int device_number,
        DB_ACTION(sqlite3_bind_int(stmt, 1, payload_number), error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 2, device_number), error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 3, service_number), error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num), error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -921,7 +1060,7 @@ handle_error:
 }
 
 int _uam_db_update_device_service_payload_info(
-       uam_ble_payload_s *payload, char *service_name)
+       uam_ble_payload_s *payload, char *service_name, const int app_num)
 {
        FUNC_ENTRY;
        int ret = UAM_ERROR_NONE;
@@ -933,16 +1072,16 @@ int _uam_db_update_device_service_payload_info(
        retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == service_name, UAM_ERROR_INVALID_PARAMETER);
 
-       ret = _uam_db_get_service_info(service_name, &info);
+       ret = _uam_db_get_service_info(service_name, &info, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
        service_number = info.service_number;
 
-       ret = _uam_db_get_payload_device_number(payload, &device_number);
+       ret = _uam_db_get_payload_device_number(payload, &device_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
-       ret = _uam_db_get_payload_number(payload, &payload_number);
+       ret = _uam_db_get_payload_number(payload, &payload_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
@@ -951,7 +1090,7 @@ int _uam_db_update_device_service_payload_info(
                        device_number, payload_number);
 
        ret = __uam_db_update_dev_svc_payload(device_number,
-               service_number, payload_number);
+               service_number, payload_number, app_num);
        if (UAM_ERROR_NONE != ret)
                goto handle_error;
 
index c7aa805..6c0daee 100644 (file)
@@ -119,8 +119,7 @@ static void __uam_resume_app(const char *dest, int event)
        FUNC_EXIT;
 }
 
-int _uam_manager_send_event(
-               const char *dest, int event, GVariant *param)
+int _uam_manager_send_event(const char *dest, int event, GVariant *param)
 {
        FUNC_ENTRY;
        char *signal;
index 3ac98a4..f05caec 100644 (file)
@@ -15,6 +15,8 @@
  *
  */
 
+#include <stdlib.h>
+#include <sys/signalfd.h>
 #include "ua-api.h"
 #include "ua-internal.h"
 #include "ua-manager-common.h"
 GMainLoop *main_loop;
 static gboolean is_initialized = FALSE;
 
-static void __uam_signal_handler(
-               int signo, siginfo_t *info, void *data)
+static void __uam_manager_cleanup(void)
+{
+       FUNC_ENTRY;
+
+       UAM_INFO("Cleanup ua-manager");
+       if (is_initialized) {
+               /* Deinit ua-manager core */
+               _uam_core_deinit();
+
+               /* Deinit plugins */
+               _uam_pm_deinit();
+
+               /* unregister ua-manager*/
+               _uam_manager_unregister();
+       }
+
+       FUNC_EXIT;
+}
+
+static gboolean __uam_manager_terminate_idle_cb(gpointer user_data)
+{
+    FUNC_ENTRY;
+
+    __uam_manager_cleanup();
+
+    UAM_INFO("Quit g_main loop");
+
+    if (main_loop)
+        g_main_loop_quit(main_loop);
+
+    UAM_INFO("ua-manager terminated");
+
+    FUNC_EXIT;
+
+    return FALSE;
+}
+
+void _uam_manager_terminate(void)
+{
+       FUNC_ENTRY;
+
+       UAM_INFO("Terminate ua-manager");
+
+       if (!is_initialized)
+               return;
+
+       is_initialized = FALSE;
+
+       g_idle_add((GSourceFunc)__uam_manager_terminate_idle_cb, NULL);
+
+       FUNC_EXIT;
+}
+
+static gboolean __uam_signal_handler(GIOChannel *channel, GIOCondition cond,
+               gpointer user_data)
+{
+       FUNC_ENTRY;
+
+       struct signalfd_siginfo si;
+       ssize_t result;
+       int fd;
+
+       if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
+               return FALSE;
+
+       fd = g_io_channel_unix_get_fd(channel);
+
+       result = read(fd, &si, sizeof(si));
+       if (result != sizeof(si))
+               return FALSE;
+
+       switch (si.ssi_signo) {
+       case SIGINT:
+       case SIGTERM:
+       case SIGSYS:
+               UAM_INFO("Signal Received is [%d], Terminate the UAM now", si.ssi_signo);
+               FUNC_EXIT;
+               _uam_manager_terminate();
+       }
+
+       FUNC_EXIT;
+       return TRUE;
+}
+
+
+static guint __uam_setup_signalfd(void)
 {
-       UAM_WARN("signal [%d] is sent by [%d]", signo, info->si_pid);
-       if (SIGTERM == signo || SIGSYS == signo || SIGINT == signo)
-               g_main_loop_quit(main_loop);
+       FUNC_ENTRY;
+
+       GIOChannel *channel;
+       guint source;
+       sigset_t mask;
+       int fd;
+
+       sigemptyset(&mask);
+       sigaddset(&mask, SIGINT);
+       sigaddset(&mask, SIGTERM);
+       sigaddset(&mask, SIGSYS);
+
+       if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
+               FUNC_EXIT;
+               return 0;
+       }
+
+       fd = signalfd(-1, &mask, 0);
+       if (fd < 0) {
+               FUNC_EXIT;
+               return 0;
+       }
+
+       channel = g_io_channel_unix_new(fd);
+
+       g_io_channel_set_close_on_unref(channel, TRUE);
+       g_io_channel_set_encoding(channel, NULL, NULL);
+       g_io_channel_set_buffered(channel, FALSE);
+
+       source = g_io_add_watch(channel,
+                               G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+                               __uam_signal_handler, NULL);
+
+       g_io_channel_unref(channel);
+
+       FUNC_EXIT;
+       return source;
 }
 
 static void __uam_glib_log(
@@ -62,37 +182,22 @@ static int __uam_manager_init(void)
                return result;
        }
 
-       /* TODO: Read conf file and set detection parameters */
+       is_initialized = TRUE;
 
        FUNC_EXIT;
        return result;
 }
 
-static void __uam_manager_cleanup(void)
-{
-       FUNC_ENTRY;
-
-       _uam_manager_unregister();
-
-       _uam_pm_deinit();
-
-       _uam_core_deinit();
-
-       FUNC_EXIT;
-}
-
 int main(void)
 {
        FUNC_ENTRY;
-       struct sigaction sa;
+       guint signal = 0;
+       UAM_INFO_C("### Starting the ua-manager daemon");
 
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = __uam_signal_handler;
-       sa.sa_flags = SA_SIGINFO;
-       sigaction(SIGINT, &sa, NULL);
-       sigaction(SIGTERM, &sa, NULL);
+       signal = __uam_setup_signalfd();
 
-       UAM_INFO_C("### Starting the ua-manager daemon");
+       if (!signal)
+               return EXIT_FAILURE;
 
        retv_if(TRUE == is_initialized, 0);
 
@@ -109,8 +214,6 @@ int main(void)
        if (main_loop != NULL)
                g_main_loop_unref(main_loop);
 
-       __uam_manager_cleanup();
-
        FUNC_EXIT;
        return 0;
 }
index 7b9c74c..3d53fa1 100644 (file)
 
 #define SELECT_ALL_PAYLOADS "SELECT P.primary_key, P.secondary_key, " \
        "P.device_uid_len, P.device_uid, P.device_icon, D.device_id, " \
-       "D.tech_type, P.user_data " \
+       "D.tech_type, P.user_data, P.app_num " \
+       "FROM payloads as P JOIN devices as D USING(device_number) WHERE P.app_num = ?"
+
+#define SELECT_PAYLOADS_WITHOUT_APP_NUM "SELECT P.primary_key, P.secondary_key, " \
+       "P.device_uid_len, P.device_uid, P.device_icon, D.device_id, " \
+       "D.tech_type, P.user_data, P.app_num " \
        "FROM payloads as P JOIN devices as D USING(device_number)"
 
 #define SELECT_PAYLOAD_DEVICE "SELECT device_number FROM payloads " \
-       "where primary_key = ? AND secondary_key = ? AND device_uid = ?"
+       "where primary_key = ? AND secondary_key = ? AND device_uid = ? AND app_num = ?"
 
 #define SELECT_PAYLOAD_NUMBER "SELECT payload_number FROM payloads " \
-       "where primary_key = ? AND secondary_key = ? AND device_uid = ?"
+       "where primary_key = ? AND secondary_key = ? AND device_uid = ? AND app_num = ?"
 
 #define INSERT_PAYLOAD "insert into payloads (primary_key, " \
-       "secondary_key, device_uid_len, device_uid, device_icon, device_number) " \
-       "values (?, ?, ?, ?, ?, ?)"
+       "secondary_key, device_uid_len, device_uid, device_icon, device_number, app_num) " \
+       "values (?, ?, ?, ?, ?, ?, ?)"
+
+#define DELETE_ALL_PAYLOADS "delete from payloads WHERE app_num = ?"
 
-#define DELETE_ALL_PAYLOADS "delete from payloads"
+#define DELETE_PAYLOADS_WITHOUT_APP_NUM "delete from payloads"
 
 #define UPDATE_PAYLOAD "UPDATE payloads " \
        "SET device_icon = ?, user_data = ? WHERE primary_key = ? AND " \
        "secondary_key = ? AND device_uid_len = ? AND " \
-       "device_uid = ? AND device_number = ?"
+       "device_uid = ? AND device_number = ? AND app_num = ?"
 
 /* DELETE statements */
 static sqlite3_stmt *delete_all_payloads;
+static sqlite3_stmt *delete_payloads_without_app_num;
 
 /* SELECT statements */
 static sqlite3_stmt *select_all_payloads;
 static sqlite3_stmt *select_payload_device;
 static sqlite3_stmt *select_payload_number;
+static sqlite3_stmt *select_payloads_without_app_num;
 
 /* UPDATE statements */
 static sqlite3_stmt *update_payload_info;
@@ -67,6 +76,7 @@ static void __uam_payload_finalize_delete(void)
        FUNC_ENTRY;
 
        FINALIZE(delete_all_payloads);
+       FINALIZE(delete_payloads_without_app_num);
 //     FINALIZE(delete_payload_info);
 
        FUNC_EXIT;
@@ -76,9 +86,10 @@ static void __uam_payload_finalize_select(void)
 {
        FUNC_ENTRY;
 
-//     FINALIZE(select_all_payloads);
+       FINALIZE(select_all_payloads);
        FINALIZE(select_payload_device);
        FINALIZE(select_payload_number);
+       FINALIZE(select_payloads_without_app_num);
 
        FUNC_EXIT;
 }
@@ -114,6 +125,8 @@ static int __uam_payload_prepare_delete(sqlite3 *db)
 
        PREPARE_QUERY(rc, db, delete_all_payloads,
                DELETE_ALL_PAYLOADS, __uam_payload_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_payloads_without_app_num,
+               DELETE_PAYLOADS_WITHOUT_APP_NUM, __uam_payload_finalize_delete);
 //     PREPARE_QUERY(rc, db, delete_payload_info,
 //             DELETE_PAYLOAD_INFO, __uam_payload_finalize_delete);
 
@@ -139,6 +152,8 @@ static int __uam_payload_prepare_select(sqlite3 *db)
                SELECT_PAYLOAD_DEVICE, __uam_payload_finalize_select);
        PREPARE_QUERY(rc, db, select_payload_number,
                SELECT_PAYLOAD_NUMBER, __uam_payload_finalize_select);
+       PREPARE_QUERY(rc, db, select_payloads_without_app_num,
+               SELECT_PAYLOADS_WITHOUT_APP_NUM, __uam_payload_finalize_select);
 
        initialized = 1;
        FUNC_EXIT;
@@ -222,7 +237,6 @@ int _uam_payload_db_deinitialize(void)
        retv_if(NULL == database_handle, UAM_ERROR_NONE);
 
        __uam_payload_table_info_finalize();
-       sqlite3_close(database_handle);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -245,7 +259,7 @@ handle_error:
 
 int _uam_db_insert_payload_info(
        const char *device_id, int tech_type,
-       const char *address, uam_ble_payload_s *payload)
+       const char *address, uam_ble_payload_s *payload, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -253,11 +267,12 @@ int _uam_db_insert_payload_info(
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        UAM_INFO("%s-%d-%s", device_id, tech_type, address);
 
        int device_number = 0;
-       error_code = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       error_code = _uam_db_get_device_number(device_id, tech_type, address, &device_number, app_num);
        if (UAM_ERROR_NONE != error_code)
                goto handle_error;
 
@@ -269,10 +284,12 @@ int _uam_db_insert_payload_info(
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 4, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT),
                error_code, handle_error);
-       DB_ACTION(sqlite3_bind_text(stmt, 5, &(payload->device_icon), 1, SQLITE_TRANSIENT),
+       DB_ACTION(sqlite3_bind_text(stmt, 5, &(payload->device_type), 1, SQLITE_TRANSIENT),
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 6, device_number),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 7, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -282,10 +299,10 @@ int _uam_db_insert_payload_info(
                goto handle_error;
        }
 
-       UAM_DBG("Device-payload info inserted [%d] primary_key: [%d] " \
-               "secondary_key: [%d] device_icon: [%d]", device_number,
+       UAM_DBG("Device-payload info inserted [%d] primary_key: [0x%2.2X] " \
+               "secondary_key: [0x%2.2X] device_type: [0x%2.2X]", device_number,
                payload->primary_key, payload->secondary_key,
-               payload->device_icon);
+               payload->device_type);
 
 handle_error:
        sqlite3_reset(stmt);
@@ -294,7 +311,7 @@ handle_error:
 }
 
 int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
-               int *device_number)
+               int *device_number, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -302,6 +319,7 @@ int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, &(payload->primary_key), 1, SQLITE_TRANSIENT),
                error_code, handle_error);
@@ -309,6 +327,8 @@ int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 3, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
 
        do {
                sql_ret = sqlite3_step(stmt);
@@ -335,7 +355,7 @@ handle_error:
 }
 
 int _uam_db_get_payload_number(uam_ble_payload_s *payload,
-               int *payload_number)
+               int *payload_number, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -343,6 +363,7 @@ int _uam_db_get_payload_number(uam_ble_payload_s *payload,
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, &(payload->primary_key), 1, SQLITE_TRANSIENT),
                error_code, handle_error);
@@ -350,6 +371,8 @@ int _uam_db_get_payload_number(uam_ble_payload_s *payload,
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 3, payload->device_uid, payload->device_uid_len, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
 
        do {
                sql_ret = sqlite3_step(stmt);
@@ -376,12 +399,19 @@ handle_error:
 }
 
 
-int _uam_payload_db_clear(void)
+int _uam_payload_db_clear(const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_all_payloads;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = delete_payloads_without_app_num;
+       } else {
+               stmt = delete_all_payloads;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
                UAM_ERR("Failed to delete payload data [%d:%s]",
@@ -398,10 +428,11 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_db_get_all_payloads(void)
+GSList *_uam_db_get_all_payloads(const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_all_payloads;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *payload_list = NULL;
        db_payload_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
@@ -409,6 +440,14 @@ GSList *_uam_db_get_all_payloads(void)
        int uid_len = 0;
        int user_data_len = 0;
 
+       if (app_num == -1) {
+               stmt = select_payloads_without_app_num;
+       } else {
+               stmt = select_all_payloads;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        do {
                sql_ret = sqlite3_step(stmt);
                switch (sql_ret) {
@@ -430,10 +469,11 @@ GSList *_uam_db_get_all_payloads(void)
                                memcpy(info->payload_info.device_uid, buf,
                                        info->payload_info.device_uid_len);
                        buf = (char *)sqlite3_column_text(stmt, 4);
-                       info->payload_info.device_icon = buf ? *buf : 1;
+                       info->payload_info.device_type = buf ? *buf : 1;
                        g_strlcpy(info->device_id, (char *)sqlite3_column_text(stmt, 5),
                                UAM_DEVICE_ID_MAX_STRING_LEN);
                        info->type = sqlite3_column_int(stmt, 6);
+                       info->app_num = sqlite3_column_int(stmt, 7);
 
                        user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - info->payload_info.device_uid_len;
 
@@ -463,6 +503,9 @@ GSList *_uam_db_get_all_payloads(void)
                }
        } while (sql_ret == SQLITE_ROW);
 
+handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_db_get_all_payloads failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return payload_list;
@@ -470,7 +513,7 @@ GSList *_uam_db_get_all_payloads(void)
 
 int _uam_db_update_payload_info(
        const char *device_id, int tech_type,
-       const char *address, uam_ble_payload_s *payload)
+       const char *address, uam_ble_payload_s *payload, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -479,15 +522,16 @@ int _uam_db_update_payload_info(
        int user_data_len = 0;
 
        retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        UAM_INFO("%s-%d-%s", device_id, tech_type, address);
 
        int device_number = 0;
-       error_code = _uam_db_get_device_number(device_id, tech_type, address, &device_number);
+       error_code = _uam_db_get_device_number(device_id, tech_type, address, &device_number, app_num);
        if (UAM_ERROR_NONE != error_code)
                goto handle_error;
 
-       DB_ACTION(sqlite3_bind_text(stmt, 1, &(payload->device_icon), 1, SQLITE_TRANSIENT),
+       DB_ACTION(sqlite3_bind_text(stmt, 1, &(payload->device_type), 1, SQLITE_TRANSIENT),
                error_code, handle_error);
        user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
        DB_ACTION(sqlite3_bind_text(stmt, 2, payload->user_data, user_data_len, SQLITE_TRANSIENT),
@@ -503,6 +547,8 @@ int _uam_db_update_payload_info(
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 7, device_number),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 8, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -513,9 +559,9 @@ int _uam_db_update_payload_info(
        }
 
        UAM_DBG("Device-payload info updated [%d] primary_key: [%d] " \
-               "secondary_key: [%d] device_icon: [%d]", device_number,
+               "secondary_key: [%d] device_type: [%d]", device_number,
                payload->primary_key, payload->secondary_key,
-               payload->device_icon);
+               payload->device_type);
        for (int i = 0; i < user_data_len; i++)
                UAM_DBG("payload user data [0x%2.2X]", payload->user_data[i]);
 
old mode 100644 (file)
new mode 100755 (executable)
index cf149af..671a640
 #include "ua-internal.h"
 #include "ua-manager-common.h"
 #include "ua-manager-core.h"
+#include "ua-manager-database.h"
+#include <cynara-client.h>
+#include <cynara-creds-gdbus.h>
+#include <aul.h>
+
+#define UAM_PRIVLEVEL_PUBLIC   0
+#define UAM_PRIVLEVEL_PLATFORM 1
+
+#define UAM_SYNC_FUNCTION      0
+#define UAM_ASYNC_FUNCTION     1
 
 extern GMainLoop *main_loop;
 
 /* For maintaining Application Sync API call requests */
 static GSList *request_list = NULL;
 
+/* For maintaining the app list */
+static GSList *uam_app_list = NULL;
+
+static GSList *sender_apps = NULL;
+
+static guint source_id = 0;
+
 static GDBusConnection *uam_manager_conn;
 static guint g_dbus_object_id = 0;
 static guint owner_id = 0;
 static guint owner_sig_id = 0;
 
+static cynara *p_cynara;
+static cynara_configuration *conf;
+
 static const gchar uam_manager_introspection_xml[] =
 "<node name='/net/uamd'>"
 "      <interface name='net.uamd'>"
@@ -48,8 +68,72 @@ static const gchar uam_manager_introspection_xml[] =
 "      </interface>"
 "</node>";
 
+typedef struct {
+       uam_request_e request;
+       int priv_level;
+       int function_type;
+} uam_request_table_s;
+
+static uam_request_table_s request_table[] = {
+       { UAM_REQUEST_GET_AVAILABLE_SENSORS, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_IS_SENSOR_READY, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DEFAULT_USER, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_ADD_USER, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_DELETE_USER, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_UPDATE_USER, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_USER_BY_ACCOUNT, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_USER_BY_DEVICE_ID, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_USER_BY_MAC, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_USERS, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_ADD_DEVICE, UAM_PRIVLEVEL_PUBLIC, UAM_ASYNC_FUNCTION },
+       { UAM_REQUEST_DELETE_DEVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_UPDATE_DEVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_DELETE_DEVICE_BY_DEVICE_ID, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_DELETE_DEVICE_BY_MAC, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DEVICE_BY_DEVICE_ID, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DEVICE_BY_MAC, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DEVICES, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_USER_DEVICES, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_IS_DEVICE_ADDED, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_SET_DETECTION_THRESHOLD, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_START_PRESENCE_DETECTION, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_STOP_PRESENCE_DETECTION, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_START_ABSENCE_DETECTION, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_STOP_ABSENCE_DETECTION, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_SET_LOW_POWER_MODE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_REGISTER_APP, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_DEREGISTER_APP, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_ADD_USER_TO_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_REMOVE_USER_FROM_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_ADD_DEVICE_TO_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_SET_DEVICE_SERVICES_DISCRIMINANT, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DEVICE_SERVICES_DISCRIMINANT, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DEVICE_SERVICES_LAST_SEEN, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_SET_SERVICE_DETECTION_CYCLE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_SERVICE_DETECTION_CYCLE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DETECTION_WINDOW, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_SET_DETECTION_WINDOW, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_RESET_DB, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_DEFAULT_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_REGISTER_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_UPDATE_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_UNREGISTER_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_SERVICE_DEVICES, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_SERVICE_USERS, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_SERVICES, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_ADD_IBEACON_ADV, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_ADD_PAYLOAD, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_ADD_PAYLOAD_TO_SERVICE, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+       { UAM_REQUEST_GET_PAYLOADS, UAM_PRIVLEVEL_PUBLIC, UAM_SYNC_FUNCTION },
+};
+
 #define UAM_ASYNC_REQUEST_TIMEOUT 11 /* 11 sec */
 
+static gboolean __schedule_uam_terminate(gpointer user_data);
+
 static void __uam_manager_copy_params(
                GVariant *in_param, void *value, int size)
 {
@@ -63,6 +147,251 @@ static void __uam_manager_copy_params(
        FUNC_EXIT;
 }
 
+GSList **_uam_manager_get_sender_apps(void)
+{
+       return &sender_apps;
+}
+
+static gint __compare_sender_param(gconstpointer data, gconstpointer user_data)
+{
+       const uam_sender_app_t *app_data = data;
+       const char *sender = user_data;
+
+       retv_if(NULL == app_data, -1);
+       retv_if(NULL == app_data->sender, -1);
+       retv_if(NULL == sender, -1);
+
+       return g_strcmp0(app_data->sender, sender);
+}
+
+static gint __compare_app_key_param(gconstpointer data, gconstpointer user_data)
+{
+       const uam_sender_app_t *app_data = data;
+       const char *app_key = user_data;
+
+       retv_if(NULL == app_data, -1);
+       retv_if(NULL == app_data->app_key, -1);
+       retv_if(NULL == app_key, -1);
+
+       return g_strcmp0(app_data->app_key, app_key);
+}
+
+static gint __compare_app_num_param(gconstpointer data, gconstpointer user_data)
+{
+       const uam_sender_app_t *app_data = data;
+       const int *app_num = user_data;
+
+       retv_if(NULL == app_data, -1);
+       retv_if(NULL == app_num, -1);
+       retv_if(app_data->app_num != *app_num, -1);
+       return 0;
+}
+
+static int __uam_get_sender_client_creds(const char *unique_name, char **credentials)
+{
+       int ret_val;
+       char *client_creds = NULL;
+       enum cynara_client_creds client_creds_method = CLIENT_METHOD_SMACK;
+       char err_msg[256] = {0, };
+
+       retv_if(uam_manager_conn == NULL, UAM_ERROR_INTERNAL);
+       retv_if(unique_name == NULL, UAM_ERROR_INVALID_PARAMETER);
+
+       ret_val = cynara_creds_get_default_client_method(&client_creds_method);
+       if (ret_val != CYNARA_API_SUCCESS) {
+               cynara_strerror(ret_val, err_msg, sizeof(err_msg));
+               UAM_ERR("Fail to get default client method: %s", err_msg);
+               return UAM_ERROR_INTERNAL;
+       }
+
+       ret_val = cynara_creds_gdbus_get_client(uam_manager_conn,
+               unique_name, client_creds_method, &client_creds);
+       if (ret_val != CYNARA_API_SUCCESS) {
+               cynara_strerror(ret_val, err_msg, sizeof(err_msg));
+               UAM_ERR("Fail to get client credential: %s", err_msg);
+               return UAM_ERROR_INTERNAL;
+       }
+
+       UAM_DBG("Client creds: %s", client_creds);
+
+       *credentials = g_strdup(client_creds);
+       g_free(client_creds);
+       return UAM_ERROR_NONE;
+}
+
+static int __uam_get_sender_user_creds(const char *unique_name, char **credentials)
+{
+       int ret_val;
+       char *user_creds = NULL;
+       enum cynara_user_creds user_creds_method = USER_METHOD_UID;
+       char err_msg[256] = {0, };
+
+       retv_if(uam_manager_conn == NULL, UAM_ERROR_INTERNAL);
+       retv_if(unique_name == NULL, UAM_ERROR_INVALID_PARAMETER);
+
+       ret_val = cynara_creds_get_default_user_method(&user_creds_method);
+       if (ret_val != CYNARA_API_SUCCESS) {
+               cynara_strerror(ret_val, err_msg, sizeof(err_msg));
+               UAM_ERR("Fail to get default user method: %s", err_msg);
+               return UAM_ERROR_INTERNAL;
+       }
+
+       ret_val = cynara_creds_gdbus_get_user(uam_manager_conn,
+               unique_name, user_creds_method, &user_creds);
+       if (ret_val != CYNARA_API_SUCCESS) {
+               cynara_strerror(ret_val, err_msg, sizeof(err_msg));
+               UAM_ERR("Fail to get user credential: %s", err_msg);
+               return UAM_ERROR_INTERNAL;
+       }
+
+       UAM_DBG("User creds: %s", user_creds);
+
+       *credentials = g_strdup(user_creds);
+       g_free(user_creds);
+       return UAM_ERROR_NONE;
+}
+
+static int __uam_get_sender_pid(const char *unique_name, pid_t *pid)
+{
+       int ret;
+       char err_msg[256] = {0, };
+
+       retv_if(uam_manager_conn == NULL, UAM_ERROR_INTERNAL);
+       retv_if(unique_name == NULL, UAM_ERROR_INVALID_PARAMETER);
+
+       ret = cynara_creds_gdbus_get_pid(uam_manager_conn, unique_name, pid);
+       if (ret != CYNARA_API_SUCCESS) {
+               cynara_strerror(ret, err_msg, sizeof(err_msg));
+               UAM_ERR("Fail to get user credential: %s", err_msg);
+               return UAM_ERROR_NOT_FOUND;
+       }
+       UAM_DBG("Sender pid: %d", *pid);
+
+       return UAM_ERROR_NONE;
+}
+
+static int __uam_get_sender_app_id(const char *unique_name, char *app_id, int len)
+{
+       int ret;
+       pid_t pid = 0;
+
+       retv_if(uam_manager_conn == NULL, UAM_ERROR_INTERNAL);
+       retv_if(unique_name == NULL, UAM_ERROR_INVALID_PARAMETER);
+
+       ret = __uam_get_sender_pid(unique_name, &pid);
+       if (ret != UAM_ERROR_NONE) {
+               UAM_ERR("Fail to get the sender pid");
+               return ret;
+       }
+
+       ret = aul_app_get_appid_bypid(pid, app_id, len);
+       if (ret != AUL_R_OK) {
+               UAM_DBG("app_id not found");
+               return UAM_ERROR_NOT_FOUND;
+       }
+       UAM_DBG("Sender app_id: %s", app_id);
+
+       return UAM_ERROR_NONE;
+}
+
+static int _uam_get_sender_app_key(char **app_key, const char *sender)
+{
+       FUNC_ENTRY;
+       int ret = UAM_ERROR_NONE;
+       char app_id[256] = { 0, };
+
+       UAM_DBG("Sender %s", sender);
+
+       ret = __uam_get_sender_app_id((const char *)sender, app_id, sizeof(app_id));
+       if (UAM_ERROR_NONE == ret) {
+               *app_key = g_strdup(app_id);
+       } else if (UAM_ERROR_NOT_FOUND == ret) {
+               ret = __uam_get_sender_client_creds(sender, app_key);
+               if (UAM_ERROR_NONE != ret) {
+                       UAM_ERR("Failed to get sender credentials");
+                       return ret;
+               }
+       } else {
+               UAM_ERR("Failed to get sender credentials");
+               return ret;
+       }
+
+       UAM_DBG("Sender app_key: %s", *app_key);
+
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+}
+
+void _uam_get_sender_from_app_num(const int app_num, char **sender)
+{
+       FUNC_ENTRY;
+       GSList *l1;
+       ret_if(sender == NULL);
+       *sender = NULL;
+
+       l1 = g_slist_find_custom(sender_apps, &app_num, __compare_app_num_param);
+       ret_if(l1 == NULL);
+       uam_sender_app_t *app_data = l1->data;
+       *sender = g_strdup(app_data->sender);
+
+       UAM_DBG("app_num[%d] has sender[%s]", app_num, *sender);
+       FUNC_EXIT;
+}
+
+static int __sender_to_app_num_operations(unsigned int *app_num, const char *sender)
+{
+       FUNC_ENTRY;
+       GSList * l1 = NULL;
+       int ret;
+
+       l1 = g_slist_find_custom(sender_apps, sender, __compare_sender_param);
+       if (!l1) {
+               char *app_key = NULL;
+               ret = _uam_get_sender_app_key(&app_key, sender);
+               if (ret != UAM_ERROR_NONE || !app_key) {
+                       UAM_ERR("_uam_get_sender_app_key() has error[%s]",
+                                       _uam_manager_error_to_str(ret));
+                       g_free(app_key);
+                       return ret;
+               }
+
+               l1 = g_slist_find_custom(sender_apps, app_key, __compare_app_key_param);
+               if (!l1) {
+                       ret = _uam_db_insert_app_info(app_num, app_key);
+                       if (ret != UAM_ERROR_NONE) {
+                               UAM_ERR("sender_app addition to persistent DB failed[%s]",
+                                               _uam_manager_error_to_str(ret));
+                               g_free(app_key);
+                               return ret;
+                       }
+
+                       uam_sender_app_t *sender_app = g_malloc0(sizeof(uam_sender_app_t));
+                       if (!sender_app) {
+                               UAM_ERR("Memory allocation error");
+                               g_free(app_key);
+                               return UAM_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       sender_app->sender = g_strdup(sender);
+                       sender_app->app_key = g_strdup(app_key);
+                       sender_app->app_num = *app_num;
+                       sender_apps = g_slist_prepend(sender_apps, sender_app);
+               } else {
+                       uam_sender_app_t *sender_app = l1->data;
+                       g_free(sender_app->sender);
+                       sender_app->sender = g_strdup(sender);
+                       *app_num = sender_app->app_num;
+               }
+               g_free(app_key);
+       } else {
+               uam_sender_app_t *sender_app = l1->data;
+               *app_num = sender_app->app_num;
+       }
+
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+}
+
 static int __uam_manager_sync_request_handler(
                GDBusMethodInvocation *context,
                int function,
@@ -75,9 +404,16 @@ static int __uam_manager_sync_request_handler(
        FUNC_ENTRY;
        int result = UAM_ERROR_NOT_SUPPORTED;
        char *sender = NULL;
+       unsigned int app_num = 0;
 
        sender = (char*)g_dbus_method_invocation_get_sender(context);
 
+       __sender_to_app_num_operations(&app_num, sender);
+       UAM_DBG("sender[%s], app_num[%u]", sender, app_num);
+
+       if (app_num <= 0)
+               UAM_ERR("app_num[%d] is invalid", app_num);
+
        switch (function) {
        case UAM_REQUEST_GET_AVAILABLE_SENSORS: {
                unsigned int sensor_bitmask = 0;
@@ -103,7 +439,7 @@ static int __uam_manager_sync_request_handler(
        case UAM_REQUEST_GET_DEFAULT_USER: {
                uam_user_info_s user;
 
-               result = _uam_core_get_default_user(&user);
+               result = _uam_core_get_default_user(&user, app_num);
                g_array_append_vals(*out_param1,
                                &user, sizeof(uam_user_info_s));
                break;
@@ -116,7 +452,7 @@ static int __uam_manager_sync_request_handler(
                account = (char *)g_variant_get_data(in_param1);
                name = (char *)g_variant_get_data(in_param2);
 
-               result = _uam_core_add_user(&user_id, account, name);
+               result = _uam_core_add_user(&user_id, account, name, app_num);
 
                UAM_DBG("UserID: [%d] Account: [%s], Name: [%s]", user_id, account, name);
                break;
@@ -127,7 +463,7 @@ static int __uam_manager_sync_request_handler(
                account = (char *)g_variant_get_data(in_param1);
                UAM_DBG("Account: [%s]", account);
 
-               result = _uam_core_remove_user(account);
+               result = _uam_core_remove_user(account, app_num);
                break;
        }
        case UAM_REQUEST_UPDATE_USER: {
@@ -135,6 +471,7 @@ static int __uam_manager_sync_request_handler(
 
                __uam_manager_copy_params(in_param1,
                                &user, sizeof(uam_user_info_s));
+               user.app_num = app_num;
                UAM_DBG("Account: [%s] Name: [%s]", user.account, user.name);
 
                result = _uam_core_update_user(&user);
@@ -148,10 +485,9 @@ static int __uam_manager_sync_request_handler(
                account = (char *)g_variant_get_data(in_param1);
                UAM_DBG("Account: [%s]", account);
 
-               result = _uam_core_get_user_by_account(account, &user);
+               result = _uam_core_get_user_by_account(account, &user, app_num);
                if (UAM_ERROR_NONE == result)
-                       g_array_append_vals(*out_param1,
-                                       &user, sizeof(uam_user_info_s));
+                       g_array_append_vals(*out_param1, &user, sizeof(uam_user_info_s));
                break;
        }
        case UAM_REQUEST_GET_USER_BY_DEVICE_ID: {
@@ -161,10 +497,9 @@ static int __uam_manager_sync_request_handler(
                device_id = (char *)g_variant_get_data(in_param1);
                UAM_DBG("device_id: [%s]", device_id);
 
-               result = _uam_core_get_user_by_device_id(device_id, &user);
+               result = _uam_core_get_user_by_device_id(device_id, &user, app_num);
                if (UAM_ERROR_NONE == result)
-                       g_array_append_vals(*out_param1,
-                                       &user, sizeof(uam_user_info_s));
+                       g_array_append_vals(*out_param1, &user, sizeof(uam_user_info_s));
                break;
        }
        case UAM_REQUEST_GET_USER_BY_MAC: {
@@ -174,17 +509,16 @@ static int __uam_manager_sync_request_handler(
                mac = (char *)g_variant_get_data(in_param1);
                UAM_DBG("MAC: [%s]", mac);
 
-               result = _uam_core_get_user_by_mac(mac, &user);
+               result = _uam_core_get_user_by_mac(mac, &user, app_num);
                if (UAM_ERROR_NONE == result)
-                       g_array_append_vals(*out_param1,
-                                       &user, sizeof(uam_user_info_s));
+                       g_array_append_vals(*out_param1, &user, sizeof(uam_user_info_s));
                break;
        }
        case UAM_REQUEST_GET_USERS: {
                int count = 0;
                uam_user_info_s *users = NULL;
 
-               result = _uam_core_get_users(&count, &users);
+               result = _uam_core_get_users(&count, &users, app_num);
                if (UAM_ERROR_NONE == result) {
                        int indx;
 
@@ -202,6 +536,8 @@ static int __uam_manager_sync_request_handler(
 
                __uam_manager_copy_params(in_param1,
                                &dev_info, sizeof(uam_device_info_s));
+
+               dev_info.app_num = app_num;
                UAM_DBG("DeviceId: [%s], Type: %d, MAC: [%s], discriminant: [%s]",
                                dev_info.device_id, dev_info.type, dev_info.mac,
                                dev_info.discriminant ? "True" : "False");
@@ -218,10 +554,12 @@ static int __uam_manager_sync_request_handler(
                account = (char *)g_variant_get_data(in_param1);
                __uam_manager_copy_params(in_param2,
                                &dev_info, sizeof(uam_device_info_s));
+
+               dev_info.app_num = app_num;
                UAM_DBG("Account: [%s], Type: %d, MAC: [%s]",
                                account, dev_info.type, dev_info.mac);
 
-               result = _uam_core_remove_device(account, &dev_info);
+               result = _uam_core_remove_device(account, &dev_info, app_num);
                break;
        }
        case UAM_REQUEST_UPDATE_DEVICE: {
@@ -229,9 +567,10 @@ static int __uam_manager_sync_request_handler(
 
                __uam_manager_copy_params(in_param1,
                                &dev_info, sizeof(uam_device_info_s));
-               UAM_DBG("Device ID: [%s], Type: %d, MAC: [%s], discriminant: [%s]",
+               dev_info.app_num = app_num;
+               UAM_DBG("Device ID: [%s], Type: %d, MAC: [%s], discriminant: [%s], app_num [%d]",
                                dev_info.device_id, dev_info.type, dev_info.mac,
-                               dev_info.discriminant ? "True" : "False");
+                               dev_info.discriminant ? "True" : "False", dev_info.app_num);
 
                result = _uam_core_update_device(&dev_info);
                break;
@@ -244,7 +583,7 @@ static int __uam_manager_sync_request_handler(
                __uam_manager_copy_params(in_param2, &tech_type, sizeof(int));
                UAM_DBG("device_id: [%s], tech_type: %d", device_id, tech_type);
 
-               result = _uam_core_remove_device_by_device_id(device_id, tech_type);
+               result = _uam_core_remove_device_by_device_id(device_id, tech_type, app_num);
                break;
        }
        case UAM_REQUEST_DELETE_DEVICE_BY_MAC: {
@@ -253,7 +592,7 @@ static int __uam_manager_sync_request_handler(
                mac = (char *)g_variant_get_data(in_param1);
                UAM_DBG("MAC: [%s]", mac);
 
-               result = _uam_core_remove_device_by_mac(mac);
+               result = _uam_core_remove_device_by_mac(mac, app_num);
                break;
        }
        case UAM_REQUEST_GET_DEVICE_BY_DEVICE_ID: {
@@ -265,7 +604,7 @@ static int __uam_manager_sync_request_handler(
                __uam_manager_copy_params(in_param2, &tech_type, sizeof(int));
                UAM_DBG("device_id: [%s], tech_type: %d", device_id, tech_type);
 
-               result = _uam_core_get_devcie_by_device_id(device_id, tech_type, &device);
+               result = _uam_core_get_devcie_by_device_id(device_id, tech_type, &device, app_num);
                if (UAM_ERROR_NONE == result)
                        g_array_append_vals(*out_param1,
                                        &device, sizeof(uam_device_info_s));
@@ -278,7 +617,7 @@ static int __uam_manager_sync_request_handler(
                mac = (char *)g_variant_get_data(in_param1);
                UAM_DBG("mac: [%s]", mac);
 
-               result = _uam_core_get_devcie_by_mac(mac, &device);
+               result = _uam_core_get_device_by_mac(mac, &device, app_num);
                if (UAM_ERROR_NONE == result)
                        g_array_append_vals(*out_param1,
                                        &device, sizeof(uam_device_info_s));
@@ -288,7 +627,7 @@ static int __uam_manager_sync_request_handler(
                int count = 0;
                uam_device_info_s *devices = NULL;
 
-               result = _uam_core_get_devices(&count, &devices);
+               result = _uam_core_get_devices(&count, &devices, app_num);
                if (UAM_ERROR_NONE == result) {
                        int indx;
 
@@ -308,7 +647,7 @@ static int __uam_manager_sync_request_handler(
                account = (char *)g_variant_get_data(in_param1);
                UAM_DBG("Account: [%s]", account);
 
-               result = _uam_core_get_user_devices(account, &count, &devices);
+               result = _uam_core_get_user_devices(account, &count, &devices, app_num);
                if (UAM_ERROR_NONE == result) {
                        int indx;
 
@@ -440,7 +779,7 @@ static int __uam_manager_sync_request_handler(
                account = (char *)g_variant_get_data(in_param2);
                UAM_DBG("Account: [%s], Service: [%s]", account, svc_name);
 
-               result = _uam_core_service_add_user(svc_name, account);
+               result = _uam_core_service_add_user(svc_name, account, app_num);
                break;
        }
        case UAM_REQUEST_REMOVE_USER_FROM_SERVICE: {
@@ -451,7 +790,7 @@ static int __uam_manager_sync_request_handler(
                account = (char *)g_variant_get_data(in_param2);
                UAM_DBG("Account: [%s], Service: [%s]", account, svc_name);
 
-               result = _uam_core_service_remove_user(svc_name, account);
+               result = _uam_core_service_remove_user(svc_name, account, app_num);
                break;
        }
        case UAM_REQUEST_ADD_DEVICE_TO_SERVICE: {
@@ -465,7 +804,7 @@ static int __uam_manager_sync_request_handler(
                UAM_DBG("Service: [%s] device_id: [%s], tech_type: %d",
                                svc_name, device_id, tech_type);
 
-               result = _uam_core_service_add_device(svc_name, device_id, tech_type);
+               result = _uam_core_service_add_device(svc_name, device_id, tech_type, app_num);
                break;
        }
        case UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE: {
@@ -479,7 +818,7 @@ static int __uam_manager_sync_request_handler(
                UAM_DBG("Service: [%s] device_id: [%s], tech_type: %d",
                                svc_name, device_id, tech_type);
 
-               result = _uam_core_service_remove_device(svc_name, device_id, tech_type);
+               result = _uam_core_service_remove_device(svc_name, device_id, tech_type, app_num);
                break;
        }
        case UAM_REQUEST_SET_DEVICE_SERVICES_DISCRIMINANT: {
@@ -496,7 +835,7 @@ static int __uam_manager_sync_request_handler(
                                svc_name, device_id, tech_type, discriminant);
 
                result = _uam_core_service_set_device_discriminant(svc_name, device_id,
-                                       tech_type, discriminant);
+                                       tech_type, discriminant, app_num);
                break;
        }
        case UAM_REQUEST_GET_DEVICE_SERVICES_DISCRIMINANT: {
@@ -509,7 +848,7 @@ static int __uam_manager_sync_request_handler(
                device_id = (char *)g_variant_get_data(in_param2);
                __uam_manager_copy_params(in_param3, &tech_type, sizeof(int));
                result = _uam_core_service_get_device_discriminant(svc_name, device_id,
-                                       tech_type, &discriminant);
+                                       tech_type, &discriminant, app_num);
                if (UAM_ERROR_NONE == result)
                        g_array_append_vals(*out_param1, &discriminant, sizeof(gboolean));
 
@@ -527,7 +866,7 @@ static int __uam_manager_sync_request_handler(
                device_id = (char *)g_variant_get_data(in_param2);
                __uam_manager_copy_params(in_param3, &tech_type, sizeof(int));
                result = _uam_core_service_get_device_last_seen(svc_name, device_id,
-                                       tech_type, &last_seen);
+                                       tech_type, &last_seen, app_num);
                if (UAM_ERROR_NONE == result)
                        g_array_append_vals(*out_param1, &last_seen, sizeof(unsigned long long));
 
@@ -543,7 +882,7 @@ static int __uam_manager_sync_request_handler(
                __uam_manager_copy_params(in_param2, &cycle, sizeof(unsigned int));
                UAM_DBG("Service: [%s], Cycle [%d]", svc_name, cycle);
 
-               result = _uam_core_set_service_detection_cycle(svc_name, cycle);
+               result = _uam_core_set_service_detection_cycle(svc_name, cycle, app_num);
                break;
        }
        case UAM_REQUEST_GET_SERVICE_DETECTION_CYCLE: {
@@ -553,7 +892,7 @@ static int __uam_manager_sync_request_handler(
                svc_name = (char *)g_variant_get_data(in_param1);
                UAM_DBG("Service: [%s]", svc_name);
 
-               result = _uam_core_get_service_detection_cycle(svc_name, &cycle);
+               result = _uam_core_get_service_detection_cycle(svc_name, &cycle, app_num);
                if (UAM_ERROR_NONE == result)
                        g_array_append_vals(*out_param1, &cycle, sizeof(unsigned int));
 
@@ -584,26 +923,27 @@ static int __uam_manager_sync_request_handler(
        }
        case UAM_REQUEST_GET_DEFAULT_SERVICE: {
                uam_service_info_s service;
-
-               result = _uam_core_get_default_service(&service);
+               service.app_num = app_num;
+               result = _uam_core_get_default_service(&service, app_num);
                g_array_append_vals(*out_param1,
                                &service, sizeof(uam_service_info_s));
                break;
        }
        case UAM_REQUEST_REGISTER_SERVICE: {
                uam_service_info_s svc;
+               svc.app_num = app_num;
 
                __uam_manager_copy_params(in_param1,
                                &svc, sizeof(uam_service_info_s));
                UAM_DBG("Name: [%s] Threshold presence: [%d] absence: [%d]",
                                svc.name, svc.presence_threshold, svc.absence_threshold);
-
-               result = _uam_core_register_service(&svc);
+               result = _uam_core_register_service(&svc, app_num);
                break;
 
        }
        case UAM_REQUEST_UPDATE_SERVICE: {
                uam_service_info_s svc;
+               svc.app_num = app_num;
 
                __uam_manager_copy_params(in_param1,
                                &svc, sizeof(uam_service_info_s));
@@ -620,7 +960,7 @@ static int __uam_manager_sync_request_handler(
                svc_name = (char *)g_variant_get_data(in_param1);
                UAM_DBG("Name: [%s]", svc_name);
 
-               result = _uam_core_unregister_service(svc_name);
+               result = _uam_core_unregister_service(svc_name, app_num);
                break;
 
        }
@@ -632,7 +972,7 @@ static int __uam_manager_sync_request_handler(
                svc_name = (char *)g_variant_get_data(in_param1);
                UAM_DBG("Service: [%s]", svc_name);
 
-               result = _uam_core_get_service_devices(svc_name, &count, &devices);
+               result = _uam_core_get_service_devices(svc_name, &count, &devices, app_num);
                if (UAM_ERROR_NONE == result) {
                        int indx;
 
@@ -652,7 +992,7 @@ static int __uam_manager_sync_request_handler(
                svc_name = (char *)g_variant_get_data(in_param1);
                UAM_DBG("Service: [%s]", svc_name);
 
-               result = _uam_core_get_service_users(svc_name, &count, &users);
+               result = _uam_core_get_service_users(svc_name, &count, &users, app_num);
                if (UAM_ERROR_NONE == result) {
                        int indx;
 
@@ -668,7 +1008,7 @@ static int __uam_manager_sync_request_handler(
                int count = 0;
                uam_service_info_s *services = NULL;
 
-               result = _uam_core_get_services(&count, &services);
+               result = _uam_core_get_services(&count, &services, app_num);
                if (UAM_ERROR_NONE == result) {
                        int indx;
 
@@ -687,7 +1027,7 @@ static int __uam_manager_sync_request_handler(
                __uam_manager_copy_params(in_param1, &adv_len, sizeof(unsigned int));
                iadv = (char *)g_variant_get_data(in_param2);
 
-               result = _uam_core_add_ibeacon_adv(adv_len, iadv);
+               result = _uam_core_add_ibeacon_adv(adv_len, iadv, app_num);
                break;
        }
        case UAM_REQUEST_ADD_PAYLOAD: {
@@ -704,7 +1044,7 @@ static int __uam_manager_sync_request_handler(
                UAM_DBG("Payload primary key: [%d] device_id: [%s], tech_type: %d",
                        payload.primary_key, device_id, tech_type);
 
-               result = _uam_core_add_payload(&payload, device_id, tech_type);
+               result = _uam_core_add_payload(&payload, device_id, tech_type, app_num);
 
                break;
        }
@@ -717,14 +1057,14 @@ static int __uam_manager_sync_request_handler(
                UAM_DBG("Payload primary key: [%d], Service [%s]",
                                payload.primary_key, svc_name);
 
-               result = _uam_core_service_add_payload(&payload, svc_name);
+               result = _uam_core_service_add_payload(&payload, svc_name, app_num);
                break;
        }
        case UAM_REQUEST_GET_PAYLOADS: {
                int count = 0;
                uam_ble_payload_s *payloads = NULL;
 
-               result = _uam_core_get_payloads(&count, &payloads);
+               result = _uam_core_get_payloads(&count, &payloads, app_num);
                if (UAM_ERROR_NONE == result) {
                        int indx;
 
@@ -762,11 +1102,14 @@ static gboolean __uam_manager_is_request_pending(int function, void *data)
                        switch (function) {
                        case UAM_REQUEST_ADD_DEVICE:
                        {
-                               const char *device_id = (const char *)data;
+                               uam_device_info_s *device_info = data;
+                               const char *device_id = device_info->device_id;
                                uam_device_info_s *dev_info = (uam_device_info_s *)info->data;
 
-                               if (!g_strcmp0(device_id,
-                                              dev_info->device_id))
+                               /* compare device type and device id for same app */
+                               if ((device_info->app_num == dev_info->app_num) &&
+                                       (device_info->type == dev_info->type) &&
+                                       !g_strcmp0(device_id, dev_info->device_id))
                                        return TRUE;
                        }
                        break;
@@ -780,6 +1123,72 @@ static gboolean __uam_manager_is_request_pending(int function, void *data)
        return FALSE;
 }
 
+static void __uam_save_app_sender(gchar *sender)
+{
+       GSList* l = uam_app_list;
+       while (l) {
+               if (!g_strcmp0(l->data, sender)) {
+                       UAM_DBG("App already in the sender list. return");
+                       return;
+               }
+               l = l->next;
+       }
+       uam_app_list = g_slist_prepend(uam_app_list, sender);
+}
+
+static gboolean __uam_remove_app_sender(gchar *sender)
+{
+       GSList *l = uam_app_list;
+       gchar* name = NULL;
+       while (l) {
+               if (g_strcmp0(l->data, sender) == 0) {
+                       UAM_DBG("App found in the list");
+                       name = l->data;
+                       break;
+               }
+               l = l->next;
+       }
+       if (l && l->data) {
+               uam_app_list = g_slist_remove(uam_app_list, l->data);
+               g_free(name);
+               return TRUE;
+       }
+       return FALSE;
+}
+
+static void __uam_manager_handle_pending_req(uam_request_context_t *info)
+{
+       FUNC_ENTRY;
+
+       if (!info)
+               return;
+
+       switch (info->function) {
+
+       case UAM_REQUEST_ADD_DEVICE: {
+               uam_device_info_s *dev;
+               GArray *out_param;
+               UAM_INFO("Handle UAM_REQUEST_ADD_DEVICE");
+
+               dev = info->data;
+               if (!dev)
+                       UAM_WARN("info->data is NULL");
+
+               out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+               g_array_append_vals(out_param, dev, sizeof(uam_device_info_s));
+               _uam_manager_method_return(info->context, out_param, info->result);
+
+               break;
+       }
+       default:
+               break;
+
+       }
+
+       FUNC_EXIT;
+       return;
+}
+
 static gboolean __uam_request_context_timeout_cb(gpointer p)
 {
        FUNC_ENTRY;
@@ -787,6 +1196,10 @@ static gboolean __uam_request_context_timeout_cb(gpointer p)
        uam_request_context_t *info = p;
        retv_if(info == NULL, G_SOURCE_REMOVE);
 
+       /* Handle request timeout */
+       info->result = UAM_ERROR_TIMED_OUT;
+       __uam_manager_handle_pending_req(info);
+
        /* Remove unhandled async-request */
        _uam_manager_remove_req_ctxt_from_list(info);
 
@@ -806,7 +1219,7 @@ static void __uam_manager_save_request_context(
 
        info = g_malloc0(sizeof(uam_request_context_t));
        if (!info) {
-               UAM_ERR("Failed to allocated memory [%s][%d]", sender, function);
+               UAM_ERR("Failed to allocate memory [%s][%d]", sender, function);
                return;
        }
        info->context = context;
@@ -815,13 +1228,12 @@ static void __uam_manager_save_request_context(
        if (!info->sender) {
                g_free(info);
                info = NULL;
-               UAM_ERR("Failed to allocated memory [%s][%d]", sender, function);
+               UAM_ERR("Failed to allocate memory [%s][%d]", sender, function);
                return;
        }
        info->function = function;
        info->data = data;
        request_list = g_slist_append(request_list, info);
-
        info->tid = _uam_add_timer_full(TIMER_RES_SEC, G_PRIORITY_DEFAULT,
                UAM_ASYNC_REQUEST_TIMEOUT, __uam_request_context_timeout_cb, info, NULL);
 
@@ -840,9 +1252,13 @@ static int __uam_manager_async_request_handler(
        FUNC_ENTRY;
        int result = UAM_ERROR_NOT_SUPPORTED;
        char *sender = NULL;
+       unsigned int app_num = 0;
 
        sender = (char*)g_dbus_method_invocation_get_sender(context);
 
+       __sender_to_app_num_operations(&app_num, sender);
+       UAM_DBG("sender[%s], app_num[%u]", sender, app_num);
+
        switch (function) {
        case UAM_REQUEST_ADD_DEVICE: {
                const char *account;
@@ -851,13 +1267,14 @@ static int __uam_manager_async_request_handler(
                account = (char *)g_variant_get_data(in_param1);
                __uam_manager_copy_params(in_param2,
                                &dev_info, sizeof(uam_device_info_s));
-               UAM_DBG("Account: [%s], Type: %d, MAC: [%s] IP: [%s], ID: [%s], Discriminant: [%s]",
+               dev_info.app_num = app_num;
+               UAM_DBG("Account: [%s], Type: %d, MAC: [%s] IP: [%s], ID: [%s],"\
+                               " app_num: [%d], Discriminant: [%s]",
                                account, dev_info.type, dev_info.mac,
-                               dev_info.ipv4_addr, dev_info.device_id,
+                               dev_info.ipv4_addr, dev_info.device_id, dev_info.app_num,
                                dev_info.discriminant ? "true" : "false");
 
-               if (__uam_manager_is_request_pending(UAM_REQUEST_ADD_DEVICE,
-                                                    dev_info.device_id)) {
+               if (__uam_manager_is_request_pending(UAM_REQUEST_ADD_DEVICE, &dev_info)) {
                        UAM_ERR("Another request is already in progress");
                        result = UAM_ERROR_NOW_IN_PROGRESS;
                        break;
@@ -868,6 +1285,9 @@ static int __uam_manager_async_request_handler(
                        __uam_manager_save_request_context(
                                        context, result, sender, function,
                                        g_memdup(&dev_info, sizeof(uam_device_info_s)));
+               } else {
+                       g_array_append_vals(*out_param1,
+                                       &dev_info, sizeof(uam_device_info_s));
                }
                break;
        }
@@ -882,64 +1302,98 @@ static int __uam_manager_async_request_handler(
        return result;
 }
 
-static gboolean __uam_manager_is_sync_function(int function)
+static int __uam_manager_get_function_type(int function)
 {
-       switch (function) {
-       case UAM_REQUEST_GET_AVAILABLE_SENSORS:
-       case UAM_REQUEST_IS_SENSOR_READY:
-       case UAM_REQUEST_GET_DEFAULT_USER:
-       case UAM_REQUEST_ADD_USER:
-       case UAM_REQUEST_DELETE_USER:
-       case UAM_REQUEST_UPDATE_USER:
-       case UAM_REQUEST_GET_USER_BY_ACCOUNT:
-       case UAM_REQUEST_GET_USER_BY_DEVICE_ID:
-       case UAM_REQUEST_GET_USER_BY_MAC:
-       case UAM_REQUEST_GET_USERS:
-       case UAM_REQUEST_IS_DEVICE_ADDED:
-       case UAM_REQUEST_DELETE_DEVICE:
-       case UAM_REQUEST_UPDATE_DEVICE:
-       case UAM_REQUEST_DELETE_DEVICE_BY_DEVICE_ID:
-       case UAM_REQUEST_DELETE_DEVICE_BY_MAC:
-       case UAM_REQUEST_GET_DEVICE_BY_DEVICE_ID:
-       case UAM_REQUEST_GET_DEVICE_BY_MAC:
-       case UAM_REQUEST_GET_DEVICES:
-       case UAM_REQUEST_GET_USER_DEVICES:
-       case UAM_REQUEST_SET_DETECTION_THRESHOLD:
-       case UAM_REQUEST_START_PRESENCE_DETECTION:
-       case UAM_REQUEST_STOP_PRESENCE_DETECTION:
-       case UAM_REQUEST_START_ABSENCE_DETECTION:
-       case UAM_REQUEST_STOP_ABSENCE_DETECTION:
-       case UAM_REQUEST_SET_LOW_POWER_MODE:
-       case UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES:
-       case UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES:
-       case UAM_REQUEST_REGISTER_APP:
-       case UAM_REQUEST_DEREGISTER_APP:
-       case UAM_REQUEST_ADD_USER_TO_SERVICE:
-       case UAM_REQUEST_REMOVE_USER_FROM_SERVICE:
-       case UAM_REQUEST_ADD_DEVICE_TO_SERVICE:
-       case UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE:
-       case UAM_REQUEST_SET_DEVICE_SERVICES_DISCRIMINANT:
-       case UAM_REQUEST_GET_DEVICE_SERVICES_DISCRIMINANT:
-       case UAM_REQUEST_SET_SERVICE_DETECTION_CYCLE:
-       case UAM_REQUEST_GET_SERVICE_DETECTION_CYCLE:
-       case UAM_REQUEST_GET_DETECTION_WINDOW:
-       case UAM_REQUEST_SET_DETECTION_WINDOW:
-       case UAM_REQUEST_RESET_DB:
-       case UAM_REQUEST_GET_DEFAULT_SERVICE:
-       case UAM_REQUEST_REGISTER_SERVICE:
-       case UAM_REQUEST_UPDATE_SERVICE:
-       case UAM_REQUEST_UNREGISTER_SERVICE:
-       case UAM_REQUEST_GET_SERVICE_DEVICES:
-       case UAM_REQUEST_GET_SERVICE_USERS:
-       case UAM_REQUEST_GET_SERVICES:
-       case UAM_REQUEST_ADD_IBEACON_ADV:
-       case UAM_REQUEST_ADD_PAYLOAD:
-       case UAM_REQUEST_ADD_PAYLOAD_TO_SERVICE:
-       case UAM_REQUEST_GET_PAYLOADS:
-               return TRUE;
-       default:
+       return request_table[function].function_type;
+}
+
+static int __uam_manager_get_priv_level(int function)
+{
+       return request_table[function].priv_level;
+}
+
+gboolean __uam_manager_check_privilege(int function,
+                                       const char *unique_name)
+{
+       int ret_val;
+       gboolean result = TRUE;
+       char *client_creds = NULL;
+       char *user_creds = NULL;
+       char *client_session = "";
+
+       retv_if(unique_name == NULL, FALSE);
+
+       retv_if(__uam_get_sender_client_creds(unique_name,
+               &client_creds) != UAM_ERROR_NONE, FALSE);
+
+       if (__uam_get_sender_user_creds(unique_name,
+               &user_creds) != UAM_ERROR_NONE) {
+               g_free(client_creds);
                return FALSE;
        }
+
+       UAM_DBG("%s, %s, %s", unique_name, client_creds, user_creds);
+
+       if (UAM_PRIVLEVEL_PUBLIC == __uam_manager_get_priv_level(function)) {
+               ret_val = cynara_check(p_cynara, client_creds,
+                       client_session, user_creds, UAM_PRIVILEGE_PUBLIC);
+
+               UAM_INFO("Client Credentials [%s]", client_creds);
+               if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
+                       UAM_ERR("Fail to access: %s", UAM_PRIVILEGE_PUBLIC);
+                       result = FALSE;
+               }
+       } else {
+               ret_val = cynara_check(p_cynara, client_creds,
+                       client_session, user_creds, UAM_PRIVILEGE_PLATFORM);
+
+               if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
+                       UAM_ERR("Fail to access: %s", UAM_PRIVILEGE_PLATFORM);
+                       result = FALSE;
+               }
+       }
+
+       g_free(client_creds);
+       g_free(user_creds);
+
+       return result;
+}
+
+int _uam_manager_cynara_init(void)
+{
+       int result;
+       char err_msg[256] = {0, };
+
+       retv_if(p_cynara != NULL, UAM_ERROR_ALREADY_DONE);
+
+       result = cynara_initialize(&p_cynara, conf);
+
+       if (result != CYNARA_API_SUCCESS) {
+               cynara_strerror(result, err_msg, sizeof(err_msg));
+               UAM_ERR("Fail to initialize cynara: [%s]", err_msg);
+               return UAM_ERROR_INTERNAL;
+       }
+
+       return UAM_ERROR_NONE;
+}
+
+void _uam_manager_cynara_deinit(void)
+{
+       int result;
+       char err_msg[256] = {0, };
+
+       ret_if(p_cynara == NULL);
+
+       result = cynara_finish(p_cynara);
+
+       if (result != CYNARA_API_SUCCESS) {
+               cynara_strerror(result, err_msg, sizeof(err_msg));
+               UAM_ERR("Fail to finish cynara: [%s]", err_msg);
+               return;
+       }
+
+       p_cynara = NULL;
+       conf = NULL;
 }
 
 static void __uam_manager_method(
@@ -954,6 +1408,7 @@ static void __uam_manager_method(
 {
        FUNC_ENTRY;
        int result = UAM_ERROR_NONE;
+       gboolean is_deinit = FALSE;
 
        UAM_DBG("Sender[%s] Method[%s] Path[%s] Interface[%s]",
                        sender, method_name, object_path, interface_name);
@@ -963,7 +1418,10 @@ static void __uam_manager_method(
 
                g_variant_get(parameters, "(i)", &enable);
                UAM_INFO("ua-manager %s", enable == TRUE ? "enabled" : "disabled");
-
+               if (enable == TRUE)
+                       __uam_save_app_sender(g_strdup(sender));
+               else
+                       is_deinit = __uam_remove_app_sender((gchar *)sender);
                g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", result));
 
        } else if (0 == g_strcmp0(method_name, "uam_request")) {
@@ -980,10 +1438,18 @@ static void __uam_manager_method(
 
                out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
 
+               if (__uam_manager_check_privilege(function, (const char *)sender) == FALSE) {
+                       UAM_ERR("Client don't have the privilege to excute this function");
+                       /* TODO:
+                       result = UAM_ERROR_PERMISSION_DENIED;
+                       goto reply;
+                       */
+               }
+
                UAM_DBG("Request function = [%s (0x%4.4X)]",
                                _uam_manager_request_to_str(function), function);
 
-               if (__uam_manager_is_sync_function(function)) {
+               if (UAM_SYNC_FUNCTION == __uam_manager_get_function_type(function)) {
                        result = __uam_manager_sync_request_handler(invocation, function,
                                        in_param1, in_param2, in_param3, in_param4, &out_param1);
                } else {
@@ -995,6 +1461,9 @@ static void __uam_manager_method(
                        }
                }
 
+/* TODO:
+reply:
+*/
                if (UAM_ERROR_NONE != result) {
                        UAM_ERR("Request [%s (0x%4.4X)] is failed with [%s (0x%4.4X])",
                                        _uam_manager_request_to_str(function), function,
@@ -1010,6 +1479,18 @@ done:
                g_variant_unref(in_param4);
                g_array_unref(out_param1);
        }
+       /* Cleanup monitor info if the App uses deinit */
+       if (is_deinit) {
+               _uam_core_cleanup_monitor((char *)sender);
+
+               if (g_slist_length(uam_app_list) == 0) {
+                       if (source_id == 0) {
+                               UAM_INFO("No App referencing the ua-manager, Terminate the UAM");
+                               source_id = g_timeout_add_full(G_PRIORITY_HIGH, 5000, __schedule_uam_terminate, NULL, NULL);
+                       }
+
+               }
+       }
 
        FUNC_EXIT;
        return;
@@ -1096,6 +1577,16 @@ static void __uam_manager_name_lost_cb(
        /*TODO: Terminate daemon */
 }
 
+static gboolean __schedule_uam_terminate(gpointer user_data)
+{
+       if (g_slist_length(uam_app_list) == 0) {
+               UAM_DBG("UA Manager not referenced Terminate the UAM");
+               _uam_manager_terminate();
+       }
+       source_id = 0;
+       return FALSE;
+}
+
 static void __uam_manager_owner_change_cb(GDBusConnection *connection,
                const gchar *sender_name,
                const gchar *object_path,
@@ -1107,13 +1598,17 @@ static void __uam_manager_owner_change_cb(GDBusConnection *connection,
        const char *name = NULL;
        const char *old_owner = NULL;
        const char *new_owner = NULL;
+       gboolean is_app_removed = FALSE;
 
        g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
 
        if ('\0' == *new_owner) {
+               is_app_removed = __uam_remove_app_sender((gchar *)name);
                /* Clean up client info */
                _uam_core_cleanup_monitor((char *)name);
        }
+       if (g_slist_length(uam_app_list) == 0 && is_app_removed)
+               source_id = g_timeout_add_full(G_PRIORITY_HIGH, 5000, __schedule_uam_terminate, NULL, NULL);
 }
 
 static void __uam_manager_bus_acquired_cb(
@@ -1142,10 +1637,10 @@ static void __uam_manager_bus_acquired_cb(
                        NAME_OWNER_CHANGED, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
                        __uam_manager_owner_change_cb, NULL, NULL);
 
-       UAM_DBG("owner_sig_id: [%d]", owner_sig_id);
+       UAM_INFO("owner_sig_id: [%d]", owner_sig_id);
 
        uam_manager_conn = connection;
-
+       g_object_ref(uam_manager_conn);
        FUNC_EXIT;
 }
 
@@ -1153,6 +1648,8 @@ int _uam_manager_register(void)
 {
        FUNC_ENTRY;
 
+       retv_if(UAM_ERROR_NONE != _uam_manager_cynara_init(), UAM_ERROR_INTERNAL);
+
        owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
                        UAM_DBUS_NAME,
                        G_BUS_NAME_OWNER_FLAGS_REPLACE,
@@ -1160,23 +1657,35 @@ int _uam_manager_register(void)
                        __uam_manager_name_acquired_cb,
                        __uam_manager_name_lost_cb,
                        NULL, NULL);
-       retv_if(0 == owner_id, UAM_ERROR_INTERNAL);
+       if (0 == owner_id)
+               goto fail;
 
        UAM_DBG("owner_id is [%d]", owner_id);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
+
+fail:
+       if (uam_manager_conn) {
+               g_object_unref(uam_manager_conn);
+               uam_manager_conn = NULL;
+       }
+       return UAM_ERROR_INTERNAL;
 }
 
 void _uam_manager_unregister(void)
 {
        FUNC_ENTRY;
 
+       _uam_manager_cynara_deinit();
+
        if (uam_manager_conn) {
                g_dbus_connection_signal_unsubscribe(uam_manager_conn, owner_sig_id);
                __uam_manager_unregister_object(uam_manager_conn);
-               g_object_unref(uam_manager_conn);
                g_bus_unown_name(owner_id);
+               g_dbus_connection_flush_sync(uam_manager_conn, NULL, NULL);
+               g_object_unref(uam_manager_conn);
+               uam_manager_conn = NULL;
        }
 
        FUNC_EXIT;
@@ -1216,7 +1725,8 @@ void _uam_manager_remove_req_ctxt_from_list(uam_request_context_t *req_info)
                }
 
                if ((strcasecmp(info->sender, req_info->sender) == 0) &&
-                       req_info->function == info->function) {
+                       (req_info->function == info->function) &&
+                       (req_info->data == info->data)) {
                        _uam_remove_timer(info->tid);
                        g_free(info->sender);
                        g_free(info->data);
@@ -1231,4 +1741,3 @@ void _uam_manager_remove_req_ctxt_from_list(uam_request_context_t *req_info)
 
        FUNC_EXIT;
 }
-
index d68576a..188d7bf 100644 (file)
 #include "ua-manager-database.h"
 
 #define SELECT_ALL_SERVICES "SELECT service_number, service_name, " \
-       "cycle, presence_threshold, absence_threshold FROM services"
+       "cycle, presence_threshold, absence_threshold, app_num FROM services WHERE app_num = ?"
+
+#define SELECT_SERVICES_WITHOUT_APP_NUM "SELECT service_number, service_name, " \
+       "cycle, presence_threshold, absence_threshold, app_num FROM services"
 
 #define SELECT_SERVICE "SELECT service_number, service_name, " \
        "cycle, presence_threshold, absence_threshold FROM services " \
-       "where service_name = ?"
+       "where service_name = ? AND app_num = ? "
 
 #define INSERT_SERVICE "insert into services (service_name, cycle, " \
-       "presence_threshold, absence_threshold) " \
-       "values (?, ?, ?, ?)"
+       "presence_threshold, absence_threshold, app_num) " \
+       "values (?, ?, ?, ?, ?)"
 
-#define UPDATE_CYCLE "UPDATE services SET cycle = ? WHERE service_name = ?"
+#define UPDATE_CYCLE "UPDATE services SET cycle = ? WHERE service_name = ? AND app_num = ?"
 
 #define UPDATE_SERVICE "UPDATE services SET (presence_threshold, absence_threshold) " \
-       "= (?, ?) WHERE service_name = ?"
+       "= (?, ?) WHERE service_name = ? AND app_num = ?"
+
+#define DELETE_ALL_SERVICES "delete from services WHERE app_num = ?"
 
-#define DELETE_ALL_SERVICES "delete from services"
+#define DELETE_SERVICES_WITHOUT_APP_NUM "delete from services"
 
-#define DELETE_SERVICE_INFO "delete from services WHERE service_name = ?"
+#define DELETE_SERVICE_INFO "delete from services WHERE service_name = ? AND app_num = ?"
 
 /* DELETE statements */
 static sqlite3_stmt *delete_all_services;
 static sqlite3_stmt *delete_service_info;
+static sqlite3_stmt *delete_services_without_app_num;
 
 /* SELECT statements */
 static sqlite3_stmt *select_all_services;
 static sqlite3_stmt *select_service;
+static sqlite3_stmt *select_services_without_app_num;
 
 /* UPDATE statements */
 static sqlite3_stmt *update_cycle;
@@ -67,6 +74,7 @@ static void __uam_service_finalize_delete(void)
 
        FINALIZE(delete_all_services);
        FINALIZE(delete_service_info);
+       FINALIZE(delete_services_without_app_num);
 
        FUNC_EXIT;
 }
@@ -77,6 +85,7 @@ static void __uam_service_finalize_select(void)
 
        FINALIZE(select_all_services);
        FINALIZE(select_service);
+       FINALIZE(select_services_without_app_num);
 
        FUNC_EXIT;
 }
@@ -115,6 +124,8 @@ static int __uam_service_prepare_delete(sqlite3 *db)
                DELETE_ALL_SERVICES, __uam_service_finalize_delete);
        PREPARE_QUERY(rc, db, delete_service_info,
                DELETE_SERVICE_INFO, __uam_service_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_services_without_app_num,
+               DELETE_SERVICES_WITHOUT_APP_NUM, __uam_service_finalize_delete);
 
        initialized = 1;
        FUNC_EXIT;
@@ -136,6 +147,8 @@ static int __uam_service_prepare_select(sqlite3 *db)
                SELECT_ALL_SERVICES, __uam_service_finalize_select);
        PREPARE_QUERY(rc, db, select_service,
                SELECT_SERVICE, __uam_service_finalize_select);
+       PREPARE_QUERY(rc, db, select_services_without_app_num,
+               SELECT_SERVICES_WITHOUT_APP_NUM, __uam_service_finalize_select);
 
        initialized = 1;
        FUNC_EXIT;
@@ -220,7 +233,6 @@ int _uam_service_db_deinitialize(void)
        retv_if(NULL == database_handle, UAM_ERROR_NONE);
 
        __uam_service_table_servicesinfo_finalize();
-       sqlite3_close(database_handle);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -242,7 +254,7 @@ handle_error:
 }
 
 int _uam_db_insert_service_info(
-       int *service_number, uam_service_info_s *svc, int cycle)
+       int *service_number, uam_service_info_s *svc, int cycle, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -250,9 +262,10 @@ int _uam_db_insert_service_info(
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == svc->name, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
-       UAM_INFO("%s-%d-%d-%d", svc->name, cycle, svc->presence_threshold,
-                               svc->absence_threshold);
+       UAM_INFO("%s-%d-%d-%d-%d", svc->name, cycle, svc->presence_threshold,
+                               svc->absence_threshold, app_num);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, svc->name, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
@@ -262,6 +275,8 @@ int _uam_db_insert_service_info(
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_int(stmt, 4, svc->absence_threshold),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 5, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -279,7 +294,7 @@ handle_error:
        return error_code;
 }
 
-int _uam_db_update_service_info(uam_db_service_info_t *svc)
+int _uam_db_update_service_info(uam_db_service_info_t *svc, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
@@ -287,6 +302,7 @@ int _uam_db_update_service_info(uam_db_service_info_t *svc)
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == svc->name, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        UAM_INFO("%s-%d-%d-%d", svc->name, svc->cycle, svc->presence_threshold,
                                svc->absence_threshold);
@@ -297,6 +313,9 @@ int _uam_db_update_service_info(uam_db_service_info_t *svc)
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 3, svc->name, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 4, app_num),
+               error_code, handle_error);
+
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -314,21 +333,24 @@ handle_error:
        return error_code;
 }
 
-int _uam_db_delete_service_info(const char *service_name)
+int _uam_db_delete_service_info(const char *service_name, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = delete_service_info;
        int sql_ret = SQLITE_OK;
 
-       error_code = _uam_db_delete_device_service(service_name);
+       error_code = _uam_db_delete_device_service(service_name, app_num);
        if (UAM_ERROR_NONE != error_code) {
                UAM_ERR("_uam_db_delete_device_service failed");
                return error_code;
        }
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_STATIC),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 2, app_num),
+               error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -344,12 +366,20 @@ handle_error:
        return error_code;
 }
 
-int _uam_service_db_clear(void)
+int _uam_service_db_clear(const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_all_services;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = delete_services_without_app_num;
+       } else {
+               stmt = delete_all_services;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
                UAM_ERR("Failed to delete service data [%d:%s]",
@@ -366,19 +396,21 @@ handle_error:
        return error_code;
 }
 
-int _uam_db_update_service_cycle(const char *service_name, int cycle)
+int _uam_db_update_service_cycle(const char *service_name, int cycle, const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = update_cycle;
        int sql_ret = SQLITE_OK;
 
        retv_if(NULL == service_name, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        DB_ACTION(sqlite3_bind_int64(stmt, 1, cycle),
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 2, service_name, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
-
+       DB_ACTION(sqlite3_bind_int(stmt, 3, app_num),
+               error_code, handle_error);
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
                UAM_ERR("Failed to update service cycle [%d:%s]",
@@ -395,7 +427,7 @@ handle_error:
        return error_code;
 }
 
-int _uam_db_get_service_info(const char *service_name, db_service_info_t *info)
+int _uam_db_get_service_info(const char *service_name, db_service_info_t *info, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NOT_FOUND;
@@ -404,11 +436,14 @@ int _uam_db_get_service_info(const char *service_name, db_service_info_t *info)
 
        retv_if(!service_name, UAM_ERROR_INVALID_PARAMETER);
        retv_if(!info, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 > app_num, UAM_ERROR_INVALID_PARAMETER);
 
        UAM_INFO("Service Name: %s", service_name);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_STATIC),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 2, app_num),
+               error_code, handle_error);
 
        do {
                sql_ret = sqlite3_step(stmt);
@@ -441,14 +476,23 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_service_db_get_all_services(void)
+GSList *_uam_service_db_get_all_services(const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_all_services;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *service_list = NULL;
        db_service_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = select_services_without_app_num;
+       } else {
+               stmt = select_all_services;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        do {
                sql_ret = sqlite3_step(stmt);
                switch (sql_ret) {
@@ -462,9 +506,10 @@ GSList *_uam_service_db_get_all_services(void)
                        info->cycle = sqlite3_column_int(stmt, 2);
                        info->presence_threshold = sqlite3_column_int(stmt, 3);
                        info->absence_threshold = sqlite3_column_int(stmt, 4);
+                       info->app_num = sqlite3_column_int(stmt, 5);
 
-                       UAM_INFO("%d-%s-%d-%d-%d", info->service_number, info->service_name,
-                                info->cycle, info->presence_threshold, info->absence_threshold);
+                       UAM_INFO("%d-%s-%d-%d-%d-%d", info->service_number, info->service_name,
+                               info->cycle, info->presence_threshold, info->absence_threshold, info->app_num);
 
                        service_list = g_slist_append(service_list, info);
                        break;
@@ -475,6 +520,9 @@ GSList *_uam_service_db_get_all_services(void)
                }
        } while (sql_ret == SQLITE_ROW);
 
+handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_service_db_get_all_services failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return service_list;
index 1773113..5c3b999 100644 (file)
 #include "ua-manager-common.h"
 #include "ua-manager-database.h"
 
-#define SELECT_ALL_USERS_DATA "SELECT user_id, name, account FROM userdata"
+#define SELECT_ALL_USERS_DATA_WITHOUT_APP_NUM "SELECT user_id, name, account, app_num FROM userdata"
+
+#define SELECT_ALL_USERS_DATA "SELECT user_id, name, account, app_num FROM userdata WHERE app_num = ?"
 
 #define SELECT_USER_DATA "SELECT user_id, name, account FROM userdata WHERE user_id = ?"
 
 #define SELECT_MAX_USER_ID "SELECT MAX(user_id) FROM userdata"
 
-#define INSERT_USER_INFO "insert into userdata (name, account) values (?, ?)"
+#define INSERT_USER_INFO "insert into userdata (name, account, app_num) values (?, ?, ?)"
+
+#define DELETE_ALL_USERS_DATA "delete from userdata WHERE app_num = ?"
 
-#define DELETE_ALL_USERS_DATA "delete from userdata "
+#define DELETE_ALL_USERS_DATA_WITHOUT_APP_NUM "delete from userdata"
 
 #define DELETE_USER_INFO "delete from userdata WHERE user_id = ? "
 
-#define UPDATE_USER_INFO "UPDATE userdata SET name = ? WHERE account = ?"
+#define UPDATE_USER_INFO "UPDATE userdata SET name = ? WHERE account = ? AND " \
+       "app_num = ?"
 
 /* DELETE statements */
 static sqlite3_stmt *delete_all_users_data;
 static sqlite3_stmt *delete_user_info;
+static sqlite3_stmt *delete_all_users_data_without_app_num;
 
 /* SELECT statements */
 static sqlite3_stmt *select_all_users_data;
 static sqlite3_stmt *select_user_data;
 static sqlite3_stmt *select_max_user_id;
+static sqlite3_stmt *select_all_users_data_without_app_num;
 
 /* INSERT statements */
 static sqlite3_stmt *insert_user_info;
@@ -64,6 +71,7 @@ static void __uam_finalize_delete(void)
 
        FINALIZE(delete_all_users_data);
        FINALIZE(delete_user_info);
+       FINALIZE(delete_all_users_data_without_app_num);
 
        FUNC_EXIT;
 }
@@ -75,6 +83,7 @@ static void __uam_finalize_select(void)
        FINALIZE(select_all_users_data);
        FINALIZE(select_user_data);
        FINALIZE(select_max_user_id);
+       FINALIZE(select_all_users_data_without_app_num);
 
        FUNC_EXIT;
 }
@@ -112,6 +121,8 @@ static int __uam_prepare_delete(sqlite3 *db)
                DELETE_ALL_USERS_DATA, __uam_finalize_delete);
        PREPARE_QUERY(rc, db, delete_user_info,
                DELETE_USER_INFO, __uam_finalize_delete);
+       PREPARE_QUERY(rc, db, delete_all_users_data_without_app_num,
+               DELETE_ALL_USERS_DATA_WITHOUT_APP_NUM, __uam_finalize_delete);
 
        initialized = 1;
        FUNC_EXIT;
@@ -135,6 +146,8 @@ static int __uam_prepare_select(sqlite3 *db)
                SELECT_USER_DATA, __uam_finalize_select);
        PREPARE_QUERY(rc, db, select_max_user_id,
                SELECT_MAX_USER_ID, __uam_finalize_select);
+       PREPARE_QUERY(rc, db, select_all_users_data_without_app_num,
+               SELECT_ALL_USERS_DATA_WITHOUT_APP_NUM, __uam_finalize_select);
 
        initialized = 1;
        FUNC_EXIT;
@@ -228,6 +241,7 @@ int _uam_db_get_max_user_id(void)
                        break;
                case SQLITE_ERROR:
                default:
+                       error_code = UAM_ERROR_DB_FAILED;
                        UAM_ERR("Failed to select max user id [%d:%s]",
                                sql_ret, sqlite3_errmsg(database_handle));
                }
@@ -263,27 +277,29 @@ int _uam_user_db_deinitialize(void)
        retv_if(NULL == database_handle, UAM_ERROR_NONE);
 
        __uam_table_usersinfo_finalize();
-       sqlite3_close(database_handle);
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
 
-int _uam_db_insert_user_info(int *user_id, const char *name, const char *account)
+int _uam_db_insert_user_info(int *user_id, const char *name, const char *account, const int app_num)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = insert_user_info;
        int sql_ret = SQLITE_OK;
 
+       retv_if(NULL == user_id, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
+       retv_if(0 >= app_num, UAM_ERROR_INVALID_PARAMETER);
 
-       UAM_INFO("%s-%s", name, account);
+       UAM_INFO("%s-%s-%d", name, account, app_num);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, name ? name : "",
                                -1, SQLITE_TRANSIENT), error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 2, account, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 3, app_num), error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
@@ -371,14 +387,23 @@ handle_error:
        return error_code;
 }
 
-GSList *_uam_db_get_all_users(void)
+GSList *_uam_db_get_all_users(const int app_num)
 {
        FUNC_ENTRY;
-       sqlite3_stmt *stmt = select_all_users_data;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt;
        GSList *user_list = NULL;
        db_user_info_t *info = NULL;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = select_all_users_data_without_app_num;
+       } else {
+               stmt = select_all_users_data;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
+
        do {
                sql_ret = sqlite3_step(stmt);
 
@@ -392,9 +417,10 @@ GSList *_uam_db_get_all_users(void)
                                UAM_USER_NAME_MAX_STRING_LEN);
                        g_strlcpy(info->account, (char *)sqlite3_column_text(stmt, 2),
                                UAM_USER_ACCOUNT_MAX_STRING_LEN);
+                       info->app_num = sqlite3_column_int(stmt, 3);
 
-                       UAM_INFO("%d-%s-%s", info->user_id, info->name,
-                                       info->account);
+                       UAM_INFO("%d-%s-%s-%d", info->user_id, info->name,
+                                       info->account, info->app_num);
                        user_list = g_slist_append(user_list, info);
                        break;
                case SQLITE_ERROR:
@@ -404,17 +430,27 @@ GSList *_uam_db_get_all_users(void)
                }
        } while (sql_ret == SQLITE_ROW);
 
+handle_error:
+       if (error_code != UAM_ERROR_NONE)
+               UAM_ERR("_uam_db_get_all_users failed");
        sqlite3_reset(stmt);
        FUNC_EXIT;
        return user_list;
 }
 
-int _uam_user_db_clear(void)
+int _uam_user_db_clear(const int app_num)
 {
        int error_code = UAM_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_all_users_data;
+       sqlite3_stmt *stmt;
        int sql_ret = SQLITE_OK;
 
+       if (app_num == -1) {
+               stmt = delete_all_users_data_without_app_num;
+       } else {
+               stmt = delete_all_users_data;
+               DB_ACTION(sqlite3_bind_int(stmt, 1, app_num),
+                       error_code, handle_error);
+       }
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
                UAM_ERR("Failed to delete user data [%d:%s]",
@@ -438,12 +474,13 @@ int _uam_db_update_user_info(uam_db_user_info_t *user)
 
        retv_if(NULL == user, UAM_ERROR_INVALID_PARAMETER);
 
-       UAM_INFO("%s-%s", user->account, user->name);
+       UAM_INFO("%s-%s-%d", user->account, user->name, user->app_num);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, user->name, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
        DB_ACTION(sqlite3_bind_text(stmt, 2, user->account, -1, SQLITE_TRANSIENT),
                error_code, handle_error);
+       DB_ACTION(sqlite3_bind_int(stmt, 3, user->app_num), error_code, handle_error);
 
        sql_ret = sqlite3_step(stmt);
        if (sql_ret != SQLITE_DONE) {
index 63bb802..21c8255 100644 (file)
@@ -30,14 +30,14 @@ extern "C" {
  * [Param] account - User account
  * [Param] name - User name
  */
-typedef int (*add_user_callback)(const char *account, const char *name);
+typedef int (*add_user_callback)(const char *account, const char *name, const char *app_id);
 
 /*
  * DIRECTION: Cloud-plugin ---> ua-manager
  * This callback will be invoked to request user deletion from ua database.
  * [Param] account - User account
  */
-typedef int (*remove_user_callback)(const char *account);
+typedef int (*remove_user_callback)(const char *account, const char *app_id);
 
 /*
  * DIRECTION: Cloud-plugin ---> ua-manager
@@ -45,7 +45,7 @@ typedef int (*remove_user_callback)(const char *account);
  * [Param] account - Device's user account
  * [Param] dev_info - Device information
  */
-typedef int (*add_device_callback)(const char *account, uap_device_info_t *dev_info);
+typedef int (*add_device_callback)(const char *account, uap_device_info_t *dev_info, const char *app_id);
 
 /*
  * DIRECTION: Cloud-plugin ---> ua-manager
@@ -53,7 +53,7 @@ typedef int (*add_device_callback)(const char *account, uap_device_info_t *dev_i
  * [Param] device_id - Unique device Id
  * [Param] type - Device's technology type (BLE/WIFI/...)
  */
-typedef int (*remove_device_callback)(const char* device_id, int type);
+typedef int (*remove_device_callback)(const char* device_id, int type, const char *app_id);
 
 /*
  * DIRECTION: Cloud-plugin ---> ua-manager
index 20fdbfe..b36fa16 100644 (file)
@@ -25,6 +25,13 @@ extern "C" {
 /* To be used in case user_id is not known */
 #define USER_ID_UNKNOWN_USER -1
 
+/* UA Plugin Scan Modes */
+typedef enum {
+       UAS_SCAN_MODE_BALANCED = 0x00,
+       UAS_SCAN_MODE_LOW_LATENCY,
+       UAS_SCAN_MODE_LOW_ENERGY
+} uas_scan_mode_e;
+
 /* UA Plugin states */
 typedef enum {
        UAS_STATE_NOT_READY = 0x00,
@@ -102,11 +109,12 @@ typedef struct {
 /* Device ble payload information structure */
 typedef struct {
        char primary_key; /** Primary Key */
-       char device_icon; /** Device icon */
+       char device_type; /** Device type */
        char secondary_key; /** Secondary key */
+       char ext_val1; /** Extension value 1 */
+       char ext_val2; /** Extension value 2 */
        int device_uid_len; /** Device uid len */
        char *device_uid; /** DEVICE_UID */
-       char *bt_mac; /** BT MAC Address */
        char *user_data; /** User data */
 } uas_payload_info_t;
 
@@ -140,35 +148,36 @@ typedef enum {
 } uas_active_scan_event_e;
 
 /* Callback to be invoked on plug-in's state changes (Ready <-> Not Ready) */
-typedef void (*uas_state_changed_callback)(int state);
+typedef void (*uas_state_changed_callback)(int id, int state);
 
 /*
- * Callback to be invoked on Presen/Absence detection is started/stopped by plug-in's
+ * Callback to be invoked on Presence/Absence detection is started/stopped by plug-in's
  * [Param] state - 0 = detection stopped, 1 = detection started.
  */
-typedef void (*uas_detection_state_changed_callback)(int state);
+typedef void (*uas_detection_state_changed_callback)(int id, int state);
 
 /*
- * Callback to be invoked on Presen/Absence detection status during detection operation
+ * Callback to be invoked on Presence/Absence detection status change during detection operation
  * by plug-in's which do not support User identification.
  */
-typedef void (*uas_detection_status_changed_callback)(uas_detection_type_e type,
-       void *sensor_info);
+typedef void (*uas_detection_status_changed_callback)(int id,
+       uas_detection_type_e type, void *sensor_info);
 
 /*
- * Callback to be invoked on Presen/Absence detection by plug-in's which do
+ * Callback to be invoked on Presence/Absence detection by plug-in's which do
  * not support User identification.
  */
-typedef void (*uas_detection_callback)(uas_detection_type_e type, void *sensor_info);
+typedef void (*uas_detection_callback)(int id,
+       uas_detection_type_e type, void *sensor_info);
 
 /*
- * Callback to be invoked on Presen/Absence detection by plug-in's which
+ * Callback to be invoked on Presence/Absence detection by plug-in's which
  * support User identification.
  *
  * [Param] device - Device for which Presence/Absence is detected.
  */
-typedef void (*uas_device_detection_callback)(uas_detection_type_e type,
-       uas_device_info_t *device);
+typedef void (*uas_device_detection_callback)(int id,
+       uas_detection_type_e type, uas_device_info_t *device);
 
 /*
  * Callback to be invoked in response to add_device() API
@@ -176,7 +185,8 @@ typedef void (*uas_device_detection_callback)(uas_detection_type_e type,
  * [Param] status - Operation status Success/Fail
  * [Param] device - Device for which add_device() was invoked.
  */
-typedef void (*uas_device_added_callback)(int status, uas_device_info_t *device);
+typedef void (*uas_device_added_callback)(int id,
+       int status, uas_device_info_t *device);
 
 /*
  * Callback to be invoked in response to search_active_devices() API
@@ -185,7 +195,7 @@ typedef void (*uas_device_added_callback)(int status, uas_device_info_t *device)
  * [Param] device - Found registerd device info if event is UAS_ACTIVE_DEVICE_FOUND
  * or, NULL if event is UAS_ACTIVE_SCAN_COMPLETED.
  */
-typedef void (*uas_device_active_scan_callback)(
+typedef void (*uas_device_active_scan_callback)(int id,
                uas_active_scan_event_e event, const uas_device_info_t *device);
 
 
@@ -253,7 +263,7 @@ typedef struct {
         *  presence and absence simultaneously, we can set type like following:
         *  -> type = UAS_PRESENCE | UAS_ABSENCE.
         */
-       int (*start_detection)(unsigned int detection_type);
+       int (*start_detection)(unsigned int detection_type, int scan_mode);
 
        /* [Sync API] To stop (User) Presence/Absence detection.
         *
index 87b5ebf..28a1f24 100644 (file)
@@ -104,6 +104,12 @@ typedef struct {
         * [Param] sensor - Which sensor detect event.
         */
        void (*presence_detected_cb)(unsigned int sensor);
+
+       /*
+        * [Sync API] This function is used the get scan_mode.
+        * [Param] scan_mode (LOW_POWER, BALANCED, LOW_LATENCY)
+        */
+       int (*get_scan_level)(int *scan_mode);
 } uap_api_t;
 
 typedef enum {
@@ -127,6 +133,12 @@ typedef struct {
        int (*deinit)(void);
 } uav_module_t;
 
+typedef struct {
+       void *handle;
+       uav_module_t *module;
+       uap_api_t *api;
+} uam_vendor_plugin_info_t;
+
 #define UAV_MODULE_ADD(id, name, author, version, init, deinit) \
        uav_module_t uap_module = { id, name, author, version, init, deinit };