Code Examples

Publishing an Event

* Here is an application that publishes Event "System.Power.BatteryLevel.Changed".
#include <stdio.h>
#include <limo-event-delivery.h>


int main (int argc, char **argv)
{
    int handle;
    int error_code;

    if ((error_code=EvtSysLibraryOpen(&handle))!=0 )
    {
        fprintf(stderr,     (): %#X\n", error_code);
        return error_code;
    }

    if ((error_code= EvtSysEventPublish(
                handle,
                "Test.Power.BatteryLevel.Changed",  // Event ID
                5,                  // BatteryLevel
                false))!=0 )        // ConnectedToCharger
    {
        fprintf(stderr, "Error EvtSysEventPublish():%#X \n", error_code);
        return error_code;
    }

    if ((error_code= EvtSysLibraryClose(handle))!=0)
    {
        fprintf(stderr, "Error event_finish(): %#X\n", error_code);
        return error_code;
    }

    return 0;
}

Subscribing to an Event

Here is an application that subscribes to Event "System.Power.BatteryLevel.Changed".

#include <stdio.h>
#include <glib.h>
#include <string.h>
#include <limo-event-delivery.h>


bool on_batterylevelchanged (
        EvtSysEventSourceId_t src_id,
        const char* event_id,
        const GArray* properties,
        void* priv_data)
{
    EvtSysEventProperty_t* e;

    printf("Got Event %s from application (%d %d %s %s)\n",
            event_id, src_id.app_id, src_id.inst_id, src_id.name1, src_id.name2);

    printf("priv_data=%s\n", (char*) priv_data);

    // Get properties
    e = &g_array_index (properties, EvtSysEventProperty_t, 0);
    printf("Battery level = %d\n", e->value.i);

    e = &g_array_index (properties, EvtSysEventProperty_t, 1);
    printf("Connected to charger = %s\n", e->value.b ? "T" : "F");

    // We've done with this Event
    return true;
}


int main (int argc, char **argv)
{
    GMainLoop* mainloop;
    int handle;
    unsigned int subscription_id;
    int error_code;
    char* priv_data = "First subscription";

    if ((error_code= EvtSysLibraryOpen(&handle)!=0)
    {
        fprintf(stderr, "Error EvtSysEventInit():%#X \n", error_code);
        return error_code;
    }

    if ((error_code= EvtSysEventSubscribe (
                handle,
                "Test.Power.BatteryLevel.Changed",  // Event ID
                "(BatteryLevel < 10) and (ConnectedToCharger = false)",// Filter expression
                on_batterylevelchanged, // Event handler
                (void*) priv_data,      // Priv data
                &subscription_id))!=0)  // Subscription ID
    {
        fprintf(stderr, "Error EvtSysEventSubscribe():%#X \n", error_code);
        return error_code;
    }

    mainloop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(mainloop);

    if ((error_code= EvtSysLibraryClose(handle, &error_code))!=0)
    {
        fprintf(stderr, "Error: event_finish() %#X\n", error_code);
        return error_code;
    }

    return 0;
}

Unsubscribing to an Event

Here is an application that unsubscribes to Event "System.Power.BatteryLevel.Changed".

#include <stdio.h>
#include <glib.h>
#include <limo-event-delivery.h>


int main (int argc, char **argv)
{
    int handle;
    unsigned int subscription_id;
    int error_code;
    
    if ((error_code= EvtSysLibraryOpen(&handle)!=0)
    {
        fprintf(stderr, "Error EvtSysEventInit():%#X \n", error_code);
        return error_code;
    }

    if ((error_code= EvtSysEventUnsubscribe (
                handle, subscription_id))!=0) // Subscription ID
    {
        fprintf(stderr, "Error EvtSysEventSubscribe():%#X \n", error_code);
        return error_code;
    }

    if ((error_code= EvtSysLibraryClose(handle, &error_code))!=0)
    {
        fprintf(stderr, "Error: event_finish() %#X\n", error_code);
        return error_code;
    }

    return 0;
}


Generated on Mon Mar 31 01:01:00 2008 by  doxygen 1.5.4