at_ps: Implement deactivate_ps_context and cgev_notify
authorGuillaume Zajac <guillaume.zajac@linux.intel.com>
Fri, 30 Nov 2012 13:40:58 +0000 (14:40 +0100)
committerwootak.jung <wootak.jung@samsung.com>
Sun, 24 Mar 2013 06:49:31 +0000 (15:49 +0900)
Tel-plugin-packetservice will take care about recovering
context core_object with CID and deactivating network interface

Change-Id: Id5d586e981e0e1da58680f43e45855508860fa03

src/at_ps.c

index 2fa5435..6d4c24c 100644 (file)
@@ -52,20 +52,17 @@ static void on_confirmation_ps_message_send(TcorePending *p, gboolean result,
        dbg("Sending %s", (result == TRUE) ? "OK" : "FAIL");
 }
 
-static void notify_context_deactivated(CoreObject *co_ps,
-                                       CoreObject *ps_context)
+static void notify_context_deactivated(CoreObject *co_ps, unsigned int cid)
 {
        struct tnoti_ps_call_status data_resp = {0};
-       unsigned int cid = tcore_context_get_id(ps_context);
-       Server *server;
+       TcorePlugin *p = tcore_object_ref_plugin(co_ps);
+       Server *server = tcore_plugin_ref_server(p);
 
        dbg("Enter");
 
        data_resp.context_id = cid;
        data_resp.state = PS_DATA_CALL_NOT_CONNECTED;
 
-       server = tcore_plugin_ref_server(tcore_object_ref_plugin(co_ps));
-
        tcore_server_send_notification(server, co_ps, TNOTI_PS_CALL_STATUS,
                                        sizeof(struct tnoti_ps_call_status),
                                        &data_resp);
@@ -188,6 +185,42 @@ static void get_pdp_address(CoreObject *co_ps, CoreObject *ps_context)
        dbg("Exit");
 }
 
+static void on_response_set_pdp_context_deactivate(TcorePending *p,
+                                                       int data_len,
+                                                       const void *data,
+                                                       void *user_data)
+{
+       CoreObject *co_ps = tcore_pending_ref_core_object(p);
+       const TcoreATResponse *resp = data;
+       CoreObject *ps_context = user_data;
+       unsigned int cid = tcore_context_get_id(ps_context);
+
+       dbg("Enter");
+
+       /*
+        * AT+CGACT = 0 is returning NO CARRIER or an error. Just test if the
+        * response contains NO CARRIER else decode CME error.
+        */
+       if (resp->lines != NULL) {
+               const char *line;
+
+               line = (const char *)resp->lines->data;
+               if (g_strcmp0(line, "NO CARRIER") != 0) {
+                       /* TODO: Decode CME error */
+                       err("%s", line);
+                       err("Context %d has not been deactivated", cid);
+
+                       goto out;
+               }
+
+       }
+
+       notify_context_deactivated(co_ps, cid);
+
+out:
+       dbg("Exit");
+}
+
 static void on_response_set_pdp_context_activate(TcorePending *p, int data_len,
                                                        const void *data,
                                                        void *user_data)
@@ -202,9 +235,11 @@ static void on_response_set_pdp_context_activate(TcorePending *p, int data_len,
                dbg("Response OK");
                get_pdp_address(co_ps, ps_context);
        } else {
+               unsigned int cid = tcore_context_get_id(ps_context);
+
                /* TODO: Manage CME errors */
                err("Response NOK");
-               notify_context_deactivated(co_ps, ps_context);
+               notify_context_deactivated(co_ps, cid);
        }
 
        dbg("Exit");
@@ -226,7 +261,7 @@ static TReturn set_pdp_context(CoreObject *co_ps, CoreObject *ps_context,
        if (activate == TRUE)
                cb = on_response_set_pdp_context_activate;
        else
-               return TCORE_RETURN_ENOSYS;
+               cb = on_response_set_pdp_context_deactivate;
 
        cmd_str = g_strdup_printf("AT+CGACT=%d,%d", activate ? 1 : 0, cid);
 
@@ -236,7 +271,7 @@ static TReturn set_pdp_context(CoreObject *co_ps, CoreObject *ps_context,
                                        on_confirmation_ps_message_send, NULL)
                                        != TCORE_RETURN_SUCCESS) {
                err("Failed to prepare and send AT request");
-               notify_context_deactivated(co_ps, ps_context);
+               notify_context_deactivated(co_ps, cid);
                ret =  TCORE_RETURN_FAILURE;
        }
 
@@ -261,8 +296,9 @@ static void on_response_define_pdp_context(TcorePending *p, int data_len,
                dbg("Response OK");
                set_pdp_context(co_ps, ps_context, ACTIVATE);
        } else {
+               unsigned int cid = tcore_context_get_id(ps_context);
                err("Response NOK");
-               notify_context_deactivated(co_ps, ps_context);
+               notify_context_deactivated(co_ps, cid);
        }
 
        dbg("Exit");
@@ -335,7 +371,7 @@ static TReturn define_pdp_context(CoreObject *co_ps, CoreObject *ps_context)
        err("Failed to prepare and send AT request");
 
 error:
-       notify_context_deactivated(co_ps, ps_context);
+       notify_context_deactivated(co_ps, cid);
 
 out:
 
@@ -344,6 +380,51 @@ out:
        return ret;
 }
 
+
+static gboolean on_cgev_notification(CoreObject *co_ps, const void *data,
+                                       void *user_data)
+{
+       GSList *tokens = NULL;
+       GSList *lines = (GSList *)data;
+       const char *line = lines->data;
+       char *noti_data = NULL;
+       unsigned int cid;
+
+       dbg("Enter");
+
+       if (NULL == line)
+               goto out;
+
+       dbg("Lines->data :%s", line);
+
+       tokens = tcore_at_tok_new(line);
+       if (g_slist_length(tokens) != 3)
+               goto out;
+
+       noti_data = g_slist_nth_data(tokens, 0);
+
+       /* Only care about NW context deactivation */
+       if (g_str_has_prefix(noti_data, "NW DEACT") == FALSE)
+               goto out;
+
+       noti_data = g_slist_nth_data(tokens, 1);
+       dbg("PDP Address: %s", noti_data);
+
+       noti_data = g_slist_nth_data(tokens, 2);
+       cid = (unsigned int)atoi(noti_data);
+       dbg("Context %d deactivated", cid);
+
+       notify_context_deactivated(co_ps, cid);
+
+out:
+       if (tokens != NULL)
+               tcore_at_tok_free(tokens);
+
+       dbg("Exit");
+
+       return TRUE;
+}
+
 static TReturn activate_ps_context(CoreObject *co_ps, CoreObject *ps_context,
                                        void *user_data)
 {
@@ -353,7 +434,7 @@ static TReturn activate_ps_context(CoreObject *co_ps, CoreObject *ps_context,
 static TReturn deactivate_ps_context(CoreObject *co_ps, CoreObject *ps_context,
                                        void *user_data)
 {
-       return TCORE_RETURN_ENOSYS;
+       return set_pdp_context(co_ps, ps_context, DEACTIVATE);
 }
 
 static struct tcore_ps_operations ps_ops = {
@@ -369,6 +450,8 @@ gboolean at_ps_init(TcorePlugin *p)
        if (NULL == co_ps)
                return FALSE;
 
+       tcore_object_add_callback(co_ps, "+CGEV", on_cgev_notification, NULL);
+
        return TRUE;
 }