Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings

Browse | Submit A New Snippet | Create A Package

 

libconic connection event handler w/ roaming

Type:
Function
Category:
Other
License:
GNU Library Public License
Language:
C
 
Description:

Handling only CON_IC_STATUS_CONNECTED and _DISCONNECTED can leave the application in a false disconnected state.

Problem: When switching from GPRS to/from WLAN, we receive STATUS_CONNECTED for the new IAP first, then STATUS_DISCONNECTED for the previous IAP.

In STATUS_DISCONNECTING, save disconnecting IAP ID and set self->connected to FALSE

STATUS_CONNECTED sets self->connected to TRUE

if STATUS_DISCONNECTED is called with the same IAP ID and self->connected has reverted to TRUE, ignore the event

Versions Of This Snippet::

Hauke Lampe
Snippet ID Download Version Date Posted Author Delete
110.0.02010-06-04 20:12Hauke Lampe

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :0.0.0


static void
thingy_widget_connection_event(ConIcConnection *connection,
  ConIcConnectionEvent *event, gpointer user_data)
{   
    ThingyWidget *self = THINGY_WIDGET (user_data);

    g_debug(WIDGET_NAME": thingy_widget_connection_event()");

    // libconic signalled a connection state change

    ConIcConnectionStatus status = con_ic_connection_event_get_status(event);
    const gchar *iap_id = con_ic_event_get_iap_id(CON_IC_EVENT(event));

    // Problem: When switching from GPRS to/from WLAN
    // we receive STATUS_CONNECTED for the new IAP first,
    // then STATUS_DISCONNECTED for the previous IAP

    // in STATUS_DISCONNECTING, save disconnecting IAP ID and
    // set self->connected to FALSE
    // STATUS_CONNECTED sets self->connected to TRUE
    // if STATUS_DISCONNECTED is called with the same IAP ID
    // and self->connected has reverted to TRUE, ignore the event

    switch (status) {
        case CON_IC_STATUS_DISCONNECTING:

            if (self->disconnecting_iap) {
                g_free(self->disconnecting_iap);
                self->disconnecting_iap = NULL;
            }

            // save disconnecting IAP ID
            if (iap_id)
                self->disconnecting_iap = g_strdup(iap_id);

            self->connected = FALSE;

            break;

        case CON_IC_STATUS_CONNECTED:

            // we are online now

            if (!self->connected) {
                self->connected = TRUE;
                g_debug(WIDGET_NAME": new connection state: online");

                // start timer
                start_update_timer(self);

                // force redraw to remove disconnection overlay
                force_redraw(self);
            }

            break;

        case CON_IC_STATUS_DISCONNECTED:

            if (iap_id && self->disconnecting_iap && self->connected &&
                    !strcmp(iap_id, self->disconnecting_iap))
            {   

                g_debug(WIDGET_NAME": ignoring offline event from disconnecting IAP");

            } else {

                // we are offline now

                self->connected = FALSE;
                g_debug(WIDGET_NAME": new connection state: offline");

                // don't stop timer here
                // it will deactivate itsef if !connected
                // frequent stop/start would prevent it from firing

                // force redraw to add disconnection overlay
                force_redraw(self);
            }

            if (self->disconnecting_iap) {
                g_free(self->disconnecting_iap);
                self->disconnecting_iap = NULL;
            }

            break;

        case CON_IC_STATUS_NETWORK_UP:

            // nothing to do here
            break;
    }
}



        ConIcConnection *connection;
        gint connection_handler;
        gboolean connected;


        // Create connection object
        self->connection = con_ic_connection_new ();
        self->connection_handler = 0;

        // set connected to FALSE, initial automatic event may set it to TRUE
        self->connected = FALSE;
        // Connect signal to receive connection events
        self->connection_handler = g_signal_connect (self->connection, "connection-event", G_CALLBACK (thingy_widget_connection_event), self);
        // Enable automatic events
        g_object_set (self->connection, "automatic-connection-events", TRUE, NULL);
        g_debug(WIDGET_NAME": connection monitoring enabled");


		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..


Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment