Ejemplo n.º 1
0
Archivo: test-fact.c Proyecto: jusa/ohm
static void test_fact_store_to_string (void) {
	void* p;
	p = NULL;
	{
		OhmFactStore* fs;
		OhmFact* fact;
		OhmFact* _tmp0;
		char* s;
		fs = ohm_fact_store_new ();
		fact = ohm_fact_new ("org.test.match");
		p = fact;
		g_object_add_weak_pointer (G_OBJECT (fact), &p);
		ohm_fact_set (fact, "field1", ohm_value_from_string ("test1"));
		ohm_fact_set (fact, "field2", ohm_value_from_int (42));
		ohm_fact_store_insert (fs, fact);
		_tmp0 = NULL;
		fact = (_tmp0 = ohm_fact_new ("org.test.match"), (fact == NULL ? NULL : (fact = (g_object_unref (fact), NULL))), _tmp0);
		ohm_fact_set (fact, "field3", ohm_value_from_int (42));
		ohm_fact_store_insert (fs, fact);
		s = ohm_fact_store_to_string (fs);
		g_assert (strstr (s, "field1 = ") != NULL);
		g_assert (strstr (s, "field2 = 42") != NULL);
		g_assert (p != NULL);
		(fs == NULL ? NULL : (fs = (g_object_unref (fs), NULL)));
		(fact == NULL ? NULL : (fact = (g_object_unref (fact), NULL)));
		s = (g_free (s), NULL);
	}
	g_assert (p == NULL);
}
Ejemplo n.º 2
0
static void profile_value_change(const char *profile, const char *key,
                                 const char *val, const char *type, void *dummy)
{

    /* A value has changed in the currently active value */

    OhmFactStore *fs = ohm_fact_store_get_fact_store();
    OhmFact *fact = NULL;

    /* get the previous fact with the profile name */
    GSList *list = ohm_fact_store_get_facts_by_name(fs, FACTSTORE_PROFILE);
    
    (void) profile;
    (void) type;
    (void) dummy;

    OHM_DEBUG(DBG_PROFILE, "profile value change: '%s', '%s'", key, val);

    if (g_slist_length(list) != 1) {
        OHM_DEBUG(DBG_PROFILE, "Error: there isn't a unique profile fact");
        return;
    }
    fact = list->data;
    if (fact && key) {
        GValue *gval = NULL;
        
        gval = ohm_fact_get(fact, key);
        if (gval &&
                G_VALUE_TYPE(gval) == G_TYPE_STRING &&
                strcmp(val, g_value_get_string(gval)) == 0) {
        
            /* the value is already there, no need to trigger an update */
            return;
        }

        gval = NULL;

        /* change the value */
        if (val)
            gval = ohm_value_from_string(val);

        OHM_DEBUG(DBG_PROFILE, "changing key %s with new value '%s'", key, val);
        ohm_fact_set(fact, key, gval);
    }
    else {
        OHM_DEBUG(DBG_PROFILE, "Error, no facts or empty key");
    }

    return;
}
Ejemplo n.º 3
0
Archivo: test-fact.c Proyecto: jusa/ohm
static void test_fact_structure_to_string (void) {
	OhmStructure* s;
	char* _tmp0;
	char* _tmp1;
	s = ohm_structure_new ("org.freedesktop.ohm.test");
	ohm_structure_set (s, "field1", ohm_value_from_string ("test1"));
	ohm_structure_set (s, "field2", ohm_value_from_int (42));
	_tmp0 = NULL;
	g_assert (_vala_strcmp0 ((_tmp0 = ohm_structure_to_string (s)), "org.freedesktop.ohm.test (field1 = \"test1\", field2 = 42)") == 0);
	_tmp0 = (g_free (_tmp0), NULL);
	ohm_structure_set (s, "field2", NULL);
	_tmp1 = NULL;
	g_assert (_vala_strcmp0 ((_tmp1 = ohm_structure_to_string (s)), "org.freedesktop.ohm.test (field1 = \"test1\")") == 0);
	_tmp1 = (g_free (_tmp1), NULL);
	(s == NULL ? NULL : (s = (g_object_unref (s), NULL)));
}
Ejemplo n.º 4
0
static void set_field(OhmFact *fact, fsif_fldtype_t type,char *name,void *vptr)
{
    fsif_value_t *v = (fsif_value_t *)vptr;
    GValue       *gv;

    switch (type) {
    case fldtype_string:    gv = ohm_value_from_string(v->string);      break;
    case fldtype_integer:   gv = ohm_value_from_int(v->integer);        break;
    case fldtype_unsignd:   gv = ohm_value_from_unsigned(v->unsignd);   break;
    case fldtype_floating:  gv = ohm_value_from_double(v->floating);    break;
    case fldtype_time:      gv = ohm_value_from_time(v->time);          break;
    default:          OHM_ERROR("resource: invalid type for %s", name); return;
    }

    ohm_fact_set(fact, name, gv);
}
Ejemplo n.º 5
0
static int load_field(FILE *fp, char *key, size_t size, GValue **value)
{
    char val[256], *e;
    size_t len;
    int i;
    double d;

    if (fgets(key, size, fp) == NULL)
        return ENOENT;

    if ((len = strlen(key)) == 0 || key[len - 1] != '\n')
        return EINVAL;

    key[len - 1] = '\0';
    
    if (fgets(val, sizeof(val), fp) == NULL)
        return EINVAL;

    if ((len = strlen(val)) < 2 || val[1] != ':' || val[len - 1] != '\n')
        return EINVAL;

    val[len - 1] = '\0';

    switch (val[0]) {
    case 's':
        *value = ohm_value_from_string(val + 2);
        break;
    case 'i':
        i = (int)strtol(val + 2, &e, 10);
        if (e != NULL && *e)
            return EINVAL;
        *value = ohm_value_from_int(i);
        break;
    case 'f':
        d = strtod(val + 2, &e);
        if (e != NULL && *e)
            return EINVAL;
        *value = ohm_value_from_double(d);
        break;
    default:
        return EINVAL;
    }

    return 0;
}
Ejemplo n.º 6
0
Archivo: test-fact.c Proyecto: jusa/ohm
static void _structure_set_get (OhmStructure* s) {
	GValue* v;
	GValue* vq;
	g_return_if_fail (OHM_IS_STRUCTURE (s));
	g_assert (_vala_strcmp0 (ohm_structure_get_name (s), "org.freedesktop.ohm.test") == 0);
	g_assert (((void*) ohm_structure_get (s, "field1")) == NULL);
	ohm_structure_set (s, "field1", ohm_value_from_string ("test1"));
	ohm_structure_set (s, "field2", ohm_value_from_int (42));
	v = ((GValue*) ohm_structure_get (s, "field1"));
	g_assert (_vala_strcmp0 (g_value_get_string (v), "test1") == 0);
	v = ((GValue*) ohm_structure_get (s, "field2"));
	g_assert (g_value_get_int (v) == 42);
	vq = ((GValue*) ohm_structure_qget (s, g_quark_from_string ("field2")));
	g_assert (v == vq);
	ohm_structure_set (s, "field2", NULL);
	v = ((GValue*) ohm_structure_get (s, "field2"));
	g_assert (v == NULL);
}
Ejemplo n.º 7
0
/********************
 * create_variable
 ********************/
static int
create_variable(dres_t *dres, char *name, dres_init_t *fields)
{
    dres_init_t  *init;
    char         *field;
    dres_value_t *value;
    OhmFactStore *store = ohm_get_fact_store();
    OhmFact      *fact;
    GValue       *gval;

    if (store == NULL)
        return EINVAL;

    if ((fact = ohm_fact_new(name)) == NULL)
        return ENOMEM;

    for (init = fields; init != NULL; init = init->next) {
        field = init->field.name;
        value = &init->field.value;
        switch (value->type) {
        case DRES_TYPE_INTEGER: gval = ohm_value_from_int(value->v.i);    break;
        case DRES_TYPE_DOUBLE:  gval = ohm_value_from_double(value->v.d); break;
        case DRES_TYPE_STRING:  gval = ohm_value_from_string(value->v.s); break;
        case DRES_TYPE_UNKNOWN:
            DRES_ERROR("Missing field initialiser for fact field %s:%s.",
                       name, field);
            return EINVAL;
        default:
            DRES_ERROR("Invalid field initialiser for fact field %s:%s.",
                       name, field);
            return EINVAL;
        }

        ohm_fact_set(fact, field, gval);
    }

    if (!ohm_fact_store_insert(store, fact))
        return EINVAL;

    return 0;

    (void)dres;
}
Ejemplo n.º 8
0
Archivo: test-fact.c Proyecto: jusa/ohm
static void test_fact_store_insert_remove (void) {
	void* p;
	void* pfs;
	void* pf;
	p = NULL;
	pfs = NULL;
	pf = NULL;
	{
		OhmFactStore* fs;
		OhmFact* fact1;
		OhmFact* fact2;
		fs = ohm_fact_store_new ();
		pfs = fs;
		g_object_add_weak_pointer (G_OBJECT (fs), &pfs);
		fact1 = ohm_fact_new ("org.test.fact1");
		ohm_fact_set (fact1, "field1", ohm_value_from_string ("test1"));
		ohm_fact_set (fact1, "field2", ohm_value_from_int (42));
		p = fact1;
		g_object_add_weak_pointer (G_OBJECT (fact1), &p);
		fact2 = ohm_fact_new ("org.test.fact2");
		ohm_fact_set (fact2, "field1", ohm_value_from_string ("test2"));
		ohm_fact_set (fact2, "field2", ohm_value_from_int (42));
		/* should not complain, does not exists*/
		ohm_fact_store_remove (fs, fact1);
		/* add+remove the same fact*/
		ohm_fact_store_insert (fs, fact1);
		ohm_fact_store_insert (fs, fact1);
		ohm_fact_store_remove (fs, fact1);
		g_assert (g_slist_length (ohm_fact_store_get_facts_by_name (fs, "org.test.fact1")) == 0);
		ohm_fact_store_insert (fs, fact1);
		ohm_fact_store_insert (fs, fact2);
		g_assert (g_slist_length (ohm_fact_store_get_facts_by_name (fs, "org.test.fact1")) == 1);
		g_assert (g_slist_length (ohm_fact_store_get_facts_by_name (fs, "org.test.fact2")) == 1);
		ohm_fact_store_remove (fs, fact2);
		g_assert (g_slist_length (ohm_fact_store_get_facts_by_name (fs, "org.test.fact1")) == 1);
		g_assert (g_slist_length (ohm_fact_store_get_facts_by_name (fs, "org.test.fact2")) == 0);
		{
			gint i;
			i = 0;
			for (; i < 100; i++) {
				OhmFact* fact;
				char* _tmp1;
				char* _tmp0;
				fact = ohm_fact_new ("org.test.fact1");
				_tmp1 = NULL;
				_tmp0 = NULL;
				ohm_fact_set (fact, "alloc", ohm_value_from_string ((_tmp1 = g_strconcat ("test", (_tmp0 = g_strdup_printf ("%i", i)), NULL))));
				_tmp1 = (g_free (_tmp1), NULL);
				_tmp0 = (g_free (_tmp0), NULL);
				ohm_fact_store_insert (fs, fact);
				if (pf == NULL) {
					pf = fact;
					g_object_add_weak_pointer (G_OBJECT (fact), &pf);
				}
				(fact == NULL ? NULL : (fact = (g_object_unref (fact), NULL)));
			}
		}
		g_assert (g_slist_length (ohm_fact_store_get_facts_by_name (fs, "org.test.fact1")) == 101);
		g_assert (p != NULL);
		g_assert (pfs != NULL);
		g_assert (pf != NULL);
		(fs == NULL ? NULL : (fs = (g_object_unref (fs), NULL)));
		(fact1 == NULL ? NULL : (fact1 = (g_object_unref (fact1), NULL)));
		(fact2 == NULL ? NULL : (fact2 = (g_object_unref (fact2), NULL)));
	}
	g_assert (p == NULL);
	g_assert (pfs == NULL);
	g_assert (pf == NULL);
}
Ejemplo n.º 9
0
Archivo: test-fact.c Proyecto: jusa/ohm
static void test_fact_pattern_match_fields (void) {
	OhmPattern* p;
	OhmFact* match;
	OhmFact* match2;
	OhmFact* nomatch;
	OhmPatternMatch* _tmp0;
	OhmPatternMatch* _tmp1;
	OhmPatternMatch* _tmp2;
	OhmPatternMatch* _tmp3;
	OhmPatternMatch* m;
	OhmPatternMatch* _tmp4;
	OhmPatternMatch* _tmp5;
	OhmPatternMatch* _tmp6;
	OhmPatternMatch* _tmp7;
	p = ohm_pattern_new ("org.test.match");
	match = ohm_fact_new ("org.test.match");
	match2 = ohm_fact_new ("org.test.match");
	nomatch = ohm_fact_new ("org.test.nomatch");
	ohm_fact_set (match, "field1", ohm_value_from_string ("test1"));
	ohm_fact_set (match, "field2", ohm_value_from_int (42));
	ohm_fact_set (match2, "field2", ohm_value_from_int (42));
	_tmp0 = NULL;
	g_assert ((_tmp0 = ohm_pattern_match (p, match, OHM_FACT_STORE_EVENT_LOOKUP)) != NULL);
	(_tmp0 == NULL ? NULL : (_tmp0 = (g_object_unref (_tmp0), NULL)));
	_tmp1 = NULL;
	g_assert ((_tmp1 = ohm_pattern_match (p, match2, OHM_FACT_STORE_EVENT_LOOKUP)) != NULL);
	(_tmp1 == NULL ? NULL : (_tmp1 = (g_object_unref (_tmp1), NULL)));
	ohm_structure_set (OHM_STRUCTURE (p), "field2", ohm_value_from_int (42));
	_tmp2 = NULL;
	g_assert ((_tmp2 = ohm_pattern_match (p, match, OHM_FACT_STORE_EVENT_LOOKUP)) != NULL);
	(_tmp2 == NULL ? NULL : (_tmp2 = (g_object_unref (_tmp2), NULL)));
	_tmp3 = NULL;
	g_assert ((_tmp3 = ohm_pattern_match (p, match2, OHM_FACT_STORE_EVENT_LOOKUP)) != NULL);
	(_tmp3 == NULL ? NULL : (_tmp3 = (g_object_unref (_tmp3), NULL)));
	m = ohm_pattern_match (p, match2, OHM_FACT_STORE_EVENT_LOOKUP);
	g_assert (ohm_pattern_match_get_fact (m) == match2);
	g_assert (ohm_pattern_match_get_pattern (m) == p);
	g_assert (ohm_pattern_match_get_event (m) == OHM_FACT_STORE_EVENT_LOOKUP);
	ohm_structure_set (OHM_STRUCTURE (p), "field1", ohm_value_from_string ("test1"));
	_tmp4 = NULL;
	g_assert ((_tmp4 = ohm_pattern_match (p, match, OHM_FACT_STORE_EVENT_LOOKUP)) != NULL);
	(_tmp4 == NULL ? NULL : (_tmp4 = (g_object_unref (_tmp4), NULL)));
	_tmp5 = NULL;
	g_assert ((_tmp5 = ohm_pattern_match (p, match2, OHM_FACT_STORE_EVENT_LOOKUP)) == NULL);
	(_tmp5 == NULL ? NULL : (_tmp5 = (g_object_unref (_tmp5), NULL)));
	ohm_structure_set (OHM_STRUCTURE (p), "field1", ohm_value_from_string ("notest1"));
	_tmp6 = NULL;
	g_assert ((_tmp6 = ohm_pattern_match (p, match, OHM_FACT_STORE_EVENT_LOOKUP)) == NULL);
	(_tmp6 == NULL ? NULL : (_tmp6 = (g_object_unref (_tmp6), NULL)));
	_tmp7 = NULL;
	g_assert ((_tmp7 = ohm_pattern_match (p, match2, OHM_FACT_STORE_EVENT_LOOKUP)) == NULL);
	(_tmp7 == NULL ? NULL : (_tmp7 = (g_object_unref (_tmp7), NULL)));
	(p == NULL ? NULL : (p = (g_object_unref (p), NULL)));
	(match == NULL ? NULL : (match = (g_object_unref (match), NULL)));
	(match2 == NULL ? NULL : (match2 = (g_object_unref (match2), NULL)));
	(nomatch == NULL ? NULL : (nomatch = (g_object_unref (nomatch), NULL)));
	(m == NULL ? NULL : (m = (g_object_unref (m), NULL)));

	return;
	(void)test_fact_pattern_match_instance;
}
Ejemplo n.º 10
0
static gboolean profile_create_fact(const char *profile, profileval_t *values)
{

    OhmFactStore *fs = ohm_fact_store_get_fact_store();
    GSList *list = NULL;
    OhmFact *fact = NULL;
    GValue *gval = NULL;

    if (!profile)
        return FALSE;

    /* get the previous fact with the profile name */

    list = ohm_fact_store_get_facts_by_name(fs, FACTSTORE_PROFILE);

    if (g_slist_length(list) > 1) {
        OHM_DEBUG(DBG_PROFILE, "Error: multiple profile facts");
        return FALSE;
    }

    if (g_slist_length(list) == 1) {
        fact = list->data;

        if (fact) {
            GSList *fields = NULL, *e = NULL;
            gboolean process = TRUE;

            /* remove existing fields */
            do {
                fields = ohm_fact_get_fields(fact);
                gboolean found = FALSE;

                for (e = fields; e != NULL; e = g_slist_next(e)) {
                    GQuark qk = (GQuark)GPOINTER_TO_INT(e->data);
                    const gchar *field_name = g_quark_to_string(qk);
                    ohm_fact_del(fact, field_name);
                    found = TRUE;
                    break;
                }

                if (!found)
                    process = FALSE;

            } while (process);
        }
    }
    else {
        /* no previous fact */
        OHM_DEBUG(DBG_PROFILE, "Creating a new profile fact");
        fact = ohm_fact_new(FACTSTORE_PROFILE);
        /* put the fact in the factstore -- this way we have the same
         * update semantics (update called on each key) */
        ohm_fact_store_insert(fs, fact); /* TODO: check return */
    }

    /* fill the fact with the profile name and the values */

    OHM_DEBUG(DBG_PROFILE, "setting key %s with value %s", PROFILE_NAME_KEY, profile);
    gval = ohm_value_from_string(profile);
    ohm_fact_set(fact, PROFILE_NAME_KEY, gval);

    if (values) {
        while (values->pv_key) {
            if (values->pv_val) {
                OHM_DEBUG(DBG_PROFILE, "setting key %s with value %s",
                          values->pv_key, values->pv_val);
                gval = ohm_value_from_string(values->pv_val);
                ohm_fact_set(fact, values->pv_key, gval);
            }
            values++;
        }
    }

    OHM_DEBUG(DBG_PROFILE, "created fact: fs: %p, fact: %p", fs, fact);

    profile_save_state(fact);
    
    return TRUE;
}
Ejemplo n.º 11
0
static gboolean bt_state_changed(const gchar *type,
        const gchar *path,
        const gchar *state)
{
    OhmFactStore *fs = ohm_fact_store_get_fact_store();
    gchar *prev_state = NULL;
    OhmFact *bt_connected = bt_get_connected(path);
    gboolean run_dres = FALSE;
    GValue *gval_state;
    gboolean bt_audio_connected = FALSE;

    //OHM_DEBUG(DBG_BT, "type: %s, state: %s", type, state);

    if (strcmp(type, BT_TYPE_AUDIO) == 0) {
        if (bt_connected) {
            gval_state = ohm_value_from_string(state);
            ohm_fact_set(bt_connected, type, gval_state);
        }

        if (strcmp(state, BT_STATE_CONNECTED_S) == 0) {
            /* Get a2dp and hsp status if AudioSink or Headset changed to
             * connected or playing before Audio state changed to connected. */
            get_properties(path, BT_INTERFACE_A2DP, get_properties_cb);
            get_properties(path, BT_INTERFACE_HSP, get_properties_cb);
        }

        return TRUE;
    }

    if(bt_connected) {
        gval_state = ohm_fact_get(bt_connected, BT_TYPE_AUDIO);
        if (gval_state != NULL && G_VALUE_TYPE(gval_state) == G_TYPE_STRING) {
            bt_audio_connected =
                strcmp(g_value_get_string(gval_state), BT_STATE_CONNECTED_S) == 0 ? TRUE : FALSE;
        }
    }

    /* In pulseaudio module-bluetooth-device is loaded after BT Audio interface
     * gets connected. Need to wait until then to be able to route audio. */
    if ((strcmp(state, BT_STATE_CONNECTED_S) == 0 || strcmp(state, BT_STATE_PLAYING_S) == 0)
            && !bt_audio_connected) {
        OHM_DEBUG(DBG_BT, "type: %s, state: %s transition not allowed.", type, state);
        return TRUE;
    }


    /* Type is either HSP or A2DP. HFP is distinguished from HSW by a
     * flag in the BT fact. */

    if (!bt_connected) {
        GValue *gval = NULL;

        /* first time: create a new fact */
        /* TODO: check that this doesn't leak memory! */
        bt_connected = ohm_fact_new(BT_DEVICE);

        /* TODO: set the bthsp and bta2dp fields to "na" or "unknown"
         * values */
        if (bt_connected == NULL) {
            OHM_DEBUG(DBG_BT, "could not create the BT fact!");
            goto error;
        }
        else {

            /* add the object path to the bluetooth fact in order to
             * remember the device */

            gval = ohm_value_from_string(path);
            ohm_fact_set(bt_connected, "bt_path", gval);

            ohm_fact_store_insert(fs, bt_connected);
        }
    }
    else {
        gval_state = ohm_fact_get(bt_connected, type);

        if (gval_state != NULL &&
                G_VALUE_TYPE(gval_state) == G_TYPE_STRING) {
            /* copy the value so that we can overwrite the one in the
             * fact */
            prev_state = g_strdup(g_value_get_string(gval_state));
        }
    }

    OHM_DEBUG(DBG_BT, "type: %s, prev_state: %s, state: %s",
            type, prev_state ? prev_state : "NULL", state);

    gval_state = ohm_value_from_string(state);
    ohm_fact_set(bt_connected, type, gval_state);

    if (strcmp(type, BT_TYPE_HSP) == 0) {
        /* check if we already have the information about the accurate
         * mono profile status */
        if (!hfp_status_defined(bt_connected) ||
                !hsp_status_defined(bt_connected)) {

            /* We don't know the HFP or HSP status yet. Process the dres
             * only after we know the status. */

            OHM_DEBUG(DBG_BT, "querying HFP/HSP state for device %s", path);

            if (prev_state) {
                GValue *gval_prev_state = ohm_value_from_string(prev_state);
                ohm_fact_set(bt_connected, "bthsp_prev_state", gval_prev_state);
            }

            if (get_properties(path, BT_INTERFACE_DEVICE, get_properties_update_fact_cb)) {
                /* continue processing in the callback */
                goto end;
            }
        }
    }

    OHM_DEBUG(DBG_BT, "running state transition from %s to %s from BT status_changed cb",
            prev_state ? prev_state : "NULL", state ? state : "NULL");

    if (prev_state && state && strcmp(prev_state, BT_STATE_CONNECTING_S) == 0
        && strcmp(state, BT_STATE_PLAYING_S) == 0) {
        /* When state transition is not allowed state might change to playing.
         * In this case state change is from connecting to playing, and connected state
         * transition is not done. We need manually do it first */
        run_dres = bt_state_transition(type, path,
            map_to_state(prev_state), map_to_state(BT_STATE_CONNECTED_S));

        if (run_dres)
            dres_all();
    }

    run_dres = bt_state_transition(type, path, 
            map_to_state(prev_state), map_to_state(state));

    if (run_dres)
        dres_all();

end:
    g_free(prev_state);
    return TRUE;

error:

    return FALSE;
}