Fix Context ID assignment 21/19621/1
authorSuresh Kumar N <suresh.n@samsung.com>
Wed, 9 Apr 2014 03:48:12 +0000 (09:18 +0530)
committerDongchul Lim <dc7.lim@samsung.com>
Thu, 17 Apr 2014 02:03:23 +0000 (11:03 +0900)
Change-Id: I54daa8b5b9bab6f1a6947cbe2753466e8900d996

src/co_ps.c

index 3d0ee00..dc26233 100755 (executable)
@@ -60,7 +60,7 @@ static TelReturn __dispatcher(CoreObject *co,
        tcore_check_return_value_assert(po->ops != NULL, TEL_RETURN_INVALID_PARAMETER);
 
        ps = po->ops;
-       co_context = ( CoreObject **)request;
+       co_context = (CoreObject **)request;
 
        switch (command) {
        case TCORE_COMMAND_PS_DEFINE_CONTEXT:
@@ -76,7 +76,7 @@ static TelReturn __dispatcher(CoreObject *co,
                        return __ps_deactivate_context (co, *co_context, cb, (void *)user_data);
 
        default:
-               err("Unsupported Command: [0x%x]");
+               err("Unsupported Command: [0x%x]", command);
                return TEL_RETURN_INVALID_PARAMETER;
        }
 
@@ -154,71 +154,74 @@ static gboolean  __ps_is_context_active(CoreObject *o, CoreObject *ps_context)
 
        CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_PS, FALSE);
 
-       dbg("Context [%p] to be checked for activeness", ps_context);
+       dbg("Context [%p] to be checked for Activeness", ps_context);
+
        po = tcore_object_ref_object(o);
        tcore_check_return_value_assert (po != NULL, FALSE);
+
        for (idx_cid = 1; idx_cid <= TCORE_PS_MAX_CID; idx_cid++) {
                if (po->context_id[idx_cid].cid) {
                        CoreObject *s_context;
                        GSList *contexts = po->context_id[idx_cid].contexts;
                        while (contexts) {
-                       s_context = contexts->data;
-                       if (s_context) {
-                               if (ps_context == s_context){
-                                               dbg("Context[%p] found with context ID[%d]",s_context, idx_cid);
-                               return TRUE;
+                               s_context = contexts->data;
+                               if (s_context) {
+                                               if (ps_context == s_context) {
+                                               dbg("Context: [%p] found with context ID: [%d] - [ACTIVE]",
+                                                       s_context, idx_cid);
+                                               return TRUE;
                                        }
-                       }
-                       contexts = g_slist_next(contexts);
+                               }
+                               contexts = g_slist_next(contexts);
                        }
                }
        }
 
-       dbg("Cannot find context (%p) ", ps_context);
+       dbg("Cannot find context: [%p] - [INACTIVE]", ps_context);
        return FALSE;
 }
 
 static gboolean __ps_is_apn_duplicated(CoreObject *o, CoreObject *ps_context, guint *cid)
 {
+       PrivateObject *po = NULL;
        gchar *t_apn = NULL;
-
        guint idx_cid = 0;
-       PrivateObject *po = NULL;
 
        dbg("Entered");
        CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_PS, FALSE);
 
        po = tcore_object_ref_object(o);
        tcore_check_return_value_assert (po != NULL, FALSE);
-       if (FALSE == tcore_context_get_apn(ps_context, &t_apn)){
-               dbg("No apn present for the given context");
+       if (FALSE == tcore_context_get_apn(ps_context, &t_apn)) {
+               dbg("No APN present for the given context");
                return FALSE;
        }
 
        for (idx_cid = 1; idx_cid <= TCORE_PS_MAX_CID; idx_cid++) {
-               if( po->context_id[idx_cid].cid == 0)
-                       continue;
-
-               CoreObject *s_context = NULL;
+               CoreObject *s_context;
                gchar *s_apn = NULL;
-               GSList *contexts = po->context_id[idx_cid].contexts;
+               GSList *contexts;
+
+               if (po->context_id[idx_cid].cid == 0)
+                       continue;
 
+               contexts = po->context_id[idx_cid].contexts;
                for (; contexts; contexts = contexts->next) {
                        s_context = contexts->data;
-                       if ((s_context == NULL) || (ps_context == s_context)){
+                       if ((s_context == NULL) || (ps_context == s_context))
                                continue;
-                       }
 
-                       if (FALSE == tcore_context_get_apn(s_context, &s_apn)){
+                       if (FALSE == tcore_context_get_apn(s_context, &s_apn)) {
                                tcore_free(s_apn);
                                continue;
                        }
 
                        if (g_strcmp0(t_apn, s_apn) == 0) {
-                               dbg("target and source have same APN");
-                               tcore_context_cp_service_info(
-                                       ps_context, s_context);
+                               dbg("Target and Source have same APN");
+
+                               tcore_context_cp_service_info(ps_context, s_context);
                                *cid = idx_cid;
+
                                tcore_free(t_apn);
                                tcore_free(s_apn);
                                return TRUE;
@@ -229,12 +232,13 @@ static gboolean __ps_is_apn_duplicated(CoreObject *o, CoreObject *ps_context, gu
                }
        }
 
+       dbg("No Duplicate APN present for the given context: [%p]", ps_context);
        tcore_free(t_apn);
        return FALSE;
 }
 
 CoreObject *tcore_ps_new(TcorePlugin *plugin,
-                       TcorePsOps *ops, TcoreHal *hal)
+       TcorePsOps *ops, TcoreHal *hal)
 {
        CoreObject *co = NULL;
        PrivateObject *po = NULL;
@@ -303,7 +307,7 @@ gboolean tcore_ps_set_online(CoreObject *o, gboolean state)
        tcore_check_return_value_assert (po != NULL, FALSE);
 
        po->online = state;
-       dbg("ps status = %d", po->online);
+       dbg("PS Status: [%s]", (po->online ? "ONLINE" : "OFFLINE"));
 
        return TRUE;
 }
@@ -322,31 +326,39 @@ gboolean tcore_ps_assign_context_id(CoreObject *o, CoreObject *context, guint ci
 
        if (cid == 0) {
                /* Automatic assign */
-               dbg("Automatic assigning of context id ");
+               dbg("Automatic assigning of CID");
                for (idx = 1; idx <= TCORE_PS_MAX_CID; idx++) {
                        if (po->context_id[idx].cid == 0) {
                                po->context_id[idx].cid = idx;
-                               po->context_id[idx].contexts = g_slist_append(po->context_id[idx].contexts, context);
-                               dbg("assign contexts(%p) in cid(%d)", context, idx);
+
+                               po->context_id[idx].contexts =
+                                       g_slist_append(po->context_id[idx].contexts, context);
+
+                               dbg("Assign CID [%d] to context: [%p]", idx, context);
                                return tcore_context_set_id(context, idx);
                        }
                        else {
-                               dbg("cid[%d] is not null", idx);
+                               dbg("CID: [%d] is not available", idx);
                        }
                }
 
-               dbg("can't find empty cid");
+               dbg("Can't find free CID");
        }
        else {
                /* Manual assign */
+               dbg("Manual assigning of CID: [%d]", cid);
                if (po->context_id[cid].cid == cid) {
-                       po->context_id[cid].contexts = g_slist_append(po->context_id[cid].contexts, context);
+                       po->context_id[cid].contexts =
+                               g_slist_append(po->context_id[cid].contexts, context);
+
+                       dbg("Assign CID [%d] to context: [%p]", cid, context);
                        return tcore_context_set_id(context, cid);
                }
                else {
-                       dbg("cid: [%d] is not null", cid);
+                       dbg("CID: [%d] is not available", cid);
                }
        }
+
        dbg("Exiting");
        return FALSE;
 }
@@ -354,7 +366,7 @@ gboolean tcore_ps_assign_context_id(CoreObject *o, CoreObject *context, guint ci
 gboolean tcore_ps_clear_context_id(CoreObject *o, CoreObject *context)
 {
        PrivateObject *po = NULL;
-       guint i = 0, cnt = 0;
+       guint cid = 0, cnt = 0;
 
        CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_PS, FALSE);
        CORE_OBJECT_CHECK_RETURN(context, CORE_OBJECT_TYPE_PS_CONTEXT, FALSE);
@@ -362,16 +374,17 @@ gboolean tcore_ps_clear_context_id(CoreObject *o, CoreObject *context)
        po = tcore_object_ref_object(o);
        tcore_check_return_value_assert (po != NULL, FALSE);
 
-       if (FALSE == tcore_context_get_id(context, &i))
+       if (FALSE == tcore_context_get_id(context, &cid))
                return FALSE;
 
-       if ((i == 0) || (i > TCORE_PS_MAX_CID))
+       if ((cid == 0) || (cid > TCORE_PS_MAX_CID))
                return FALSE;
 
-       po->context_id[i].contexts = g_slist_remove(po->context_id[i].contexts, context);
-       cnt = g_slist_length(po->context_id[i].contexts);
-       if (cnt <= 0)
-               po->context_id[i].cid = 0;
+       /* Remove context */
+       po->context_id[cid].contexts = g_slist_remove(po->context_id[cid].contexts, context);
+       cnt = g_slist_length(po->context_id[cid].contexts);
+       if (cnt == 0)
+               po->context_id[cid].cid = 0;
 
        return tcore_context_set_id(context, 0);
 }
@@ -435,41 +448,47 @@ static TelReturn __ps_define_context(CoreObject *o, CoreObject *ps_context,
        tcore_check_return_value_assert(po != NULL, TEL_RETURN_INVALID_PARAMETER);
        tcore_check_return_value_assert(ps_context != NULL, TEL_RETURN_INVALID_PARAMETER);
 
-       if (TRUE == __ps_is_context_active(o, ps_context)){
+       if (TRUE == __ps_is_context_active(o, ps_context)) {
                dbg("Return, requested context is already in active state");
                return TEL_RETURN_SUCCESS;
        }
+
        /*
-        * Check if apn of requested context is same as any active context
-        * if yes simply append context to list of that context Id
+        * Check if APN of requested context is same as any active context
+        * if yes simply append context to list of that Context ID
         */
-
        if (TRUE == __ps_is_apn_duplicated(o, ps_context, &cid)) {
-               po->context_id[cid].contexts = g_slist_append(po->context_id[cid].contexts, ps_context);
-               tcore_context_set_id(ps_context, &cid);
-               dbg("Duplicate APN present,added context[%p] to list of cid[%d]", ps_context, cid);
+               /* Append to Context ID list */
+               po->context_id[cid].contexts =
+                       g_slist_append(po->context_id[cid].contexts, ps_context);
+
+               /* Set Context ID */
+               tcore_context_set_id(ps_context, cid);
+               dbg("Duplicate APN present, added context[%p] to list of CID: [%d]",
+                       ps_context, cid);
+
                return TEL_RETURN_SUCCESS;
        }
 
-       /* New context to be defined assgin context id */
+       /* New context to be defined assgin Context ID */
        if (TRUE == tcore_context_get_id(ps_context, &cid)) {
-               dbg(" Current context [%p] has context ID[%d]", ps_context, cid);
-               if (cid == 0){
-                       if (tcore_ps_assign_context_id(o, ps_context, 0) != TRUE){
+               dbg("Current context [%p] has context ID: [%d]", ps_context, cid);
+               if (cid == 0) {
+                       if (tcore_ps_assign_context_id(o, ps_context, 0) != TRUE) {
                                err("Failed to assign context id to context");
                                return TEL_RETURN_FAILURE;
                        }
                }
        } else {
-               err("Failed to get context Id for context[%p]", ps_context);
+               err("Failed to get Context ID for context: [%p]", ps_context);
                return TEL_RETURN_FAILURE;
        }
 
        if (FALSE == tcore_context_get_id(ps_context, &cid))
                return TEL_RETURN_FAILURE;
-       dbg("context: [%p] cid: [%d]", ps_context, cid);
+       dbg("Context: [%p] CID: [%d]", ps_context, cid);
 
-       dbg("Sending define request to modem");
+       dbg("Sending 'Define Context' request to modem");
        return po->ops->define_context(o, ps_context, cb, user_data);
 }
 
@@ -478,7 +497,6 @@ static TelReturn __ps_activate_context(CoreObject *o, CoreObject *ps_context,
 {
        PrivateObject *po = NULL;
        TcoreContextState context_state = TCORE_CONTEXT_STATE_DEACTIVATED;
-       guint cid = 0;
 
        dbg("Entered");
 
@@ -489,17 +507,17 @@ static TelReturn __ps_activate_context(CoreObject *o, CoreObject *ps_context,
        tcore_check_return_value_assert(ps_context != NULL, TEL_RETURN_INVALID_PARAMETER);
 
        if (!po->online) {
-               err("PS service is not online");
+               err("PS service is NOT Online");
                return TEL_RETURN_OPERATION_NOT_SUPPORTED;
        }
 
        if (FALSE ==  __ps_is_context_active(o, ps_context)) {
-               err("Context[%p] is not defined yet", ps_context);
+               err("Context: [%p] is NOT yet Defined!!!", ps_context);
                return TEL_RETURN_INVALID_PARAMETER;
        }
 
        (void)tcore_context_get_state(ps_context, &context_state);
-       dbg("Context[%p] current state[%d] ", ps_context, context_state);
+       dbg("Context: [%p] Current state: [%d] ", ps_context, context_state);
 
        if (context_state == TCORE_CONTEXT_STATE_ACTIVATED)
                return TEL_RETURN_SUCCESS;
@@ -508,8 +526,10 @@ static TelReturn __ps_activate_context(CoreObject *o, CoreObject *ps_context,
        else if (context_state == TCORE_CONTEXT_STATE_DEACTIVATING)
                return TEL_RETURN_FAILURE;
 
+       /* Set state to ACTIVATING */
        (void)tcore_context_set_state(ps_context, TCORE_CONTEXT_STATE_ACTIVATING);
-       dbg("sending activate command for context[%p]", ps_context);
+
+       dbg("Sending 'Activate Context' request for context: [%p]", ps_context);
        return po->ops->activate_context(o, ps_context, cb, user_data);
 }
 
@@ -518,6 +538,7 @@ static TelReturn __ps_deactivate_context(CoreObject *o, CoreObject *ps_context,
 {
        PrivateObject *po = NULL;
        TcoreContextState context_state = TCORE_CONTEXT_STATE_DEACTIVATED;
+       guint cid = 0;
 
        CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_PS, TEL_RETURN_INVALID_PARAMETER);
 
@@ -529,12 +550,26 @@ static TelReturn __ps_deactivate_context(CoreObject *o, CoreObject *ps_context,
                dbg("PS service is not online");
                return TEL_RETURN_OPERATION_NOT_SUPPORTED;
        }
-       if (FALSE ==  __ps_is_context_active(o, ps_context)){
+
+       if (FALSE ==  __ps_is_context_active(o, ps_context)) {
                dbg("Context is not in active state");
                return TEL_RETURN_INVALID_PARAMETER;
        }
 
+       (void)tcore_context_get_id(ps_context, &cid);
+       if (g_slist_length(po->context_id[cid].contexts) > 1) {
+               /* Remove context from list and return Success */
+               po->context_id[cid].contexts =
+                       g_slist_remove(po->context_id[cid].contexts, ps_context);
+
+               /* Set duplicated apn profile state to deactivated */
+               tcore_context_set_state(ps_context, TCORE_CONTEXT_STATE_DEACTIVATED);
+
+               return TEL_RETURN_SUCCESS;
+       }
+
        (void)tcore_context_get_state(ps_context, &context_state);
+       dbg("Context: [%p] State: [%d]", ps_context, context_state);
        if (context_state == TCORE_CONTEXT_STATE_DEACTIVATED)
                return TEL_RETURN_SUCCESS;
        else if (context_state == TCORE_CONTEXT_STATE_DEACTIVATING)
@@ -544,7 +579,7 @@ static TelReturn __ps_deactivate_context(CoreObject *o, CoreObject *ps_context,
 
        tcore_context_set_state(ps_context, TCORE_CONTEXT_STATE_DEACTIVATING);
 
-       dbg("Sending deactivation request");
+       dbg("Sending 'Deactivate Context' request for context: [%p]", ps_context);
        return po->ops->deactivate_context(o, ps_context, cb , user_data);
 }
 
@@ -565,20 +600,24 @@ gboolean tcore_ps_is_active_apn(CoreObject *o, const char *apn)
                if (g_slist_length(contexts) == 0)
                        continue;
 
-               ifcontexts->data == NULL) {
+               if (contexts->data == NULL) {
                        contexts = g_slist_next(contexts);
                        continue;
                }
-               s_context = (gchar *)(contexts->data);
+
+               s_context = (CoreObject *)(contexts->data);
                tcore_context_get_apn(s_context, &s_apn);
-               if( 0 == g_strcmp0(apn, s_apn)){
+               if (0 == g_strcmp0(apn, s_apn)) {
+                       dbg("'%s' APN is - [ACTIVE]", apn);
                        tcore_free(s_apn);
                        return TRUE;
                }
+
                tcore_free(s_apn);
                s_apn = NULL;
                contexts = g_slist_next(contexts);
        }
 
+       dbg("'%s' APN - [INACTIVE]", apn);
        return FALSE;
 }