예제 #1
0
static GVariant *
parse_json (JsonNode *node,
            const GVariantType *type,
            GError **error)
{
  const GVariantType *element_type;
  const gchar *str;

  if (!g_variant_type_is_definite (type))
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                   "Indefinite type '%.*s' is not supported",
                   (int)g_variant_type_get_string_length (type),
                   g_variant_type_peek_string (type));
      return NULL;
    }

  if (g_variant_type_is_basic (type))
    {
      if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_BOOLEAN, error))
            return g_variant_new_boolean (json_node_get_boolean (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_byte (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_int16 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_uint16 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_int32 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_uint32 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_int64 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_uint64 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, NULL))
            return g_variant_new_double (json_node_get_int (node));
          else if (check_type (node, JSON_NODE_VALUE, G_TYPE_DOUBLE, error))
            return g_variant_new_double (json_node_get_double (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_STRING, error))
            return g_variant_new_string (json_node_get_string (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_STRING, error))
            {
              str = json_node_get_string (node);
              if (g_variant_is_object_path (str))
                return g_variant_new_object_path (str);
              else
                {
                  g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                               "Invalid object path '%s'", str);
                  return NULL;
                }
            }
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_STRING, error))
            {
              str = json_node_get_string (node);
              if (g_variant_is_signature (str))
                return g_variant_new_signature (str);
              else
                {
                  g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                               "Invalid signature '%s'", str);
                  return NULL;
                }
            }
        }
      else
        {
          parse_not_supported (type, error);
        }
    }
  else if (g_variant_type_is_variant (type))
    {
      return parse_json_variant (node, error);
    }
  else if (g_variant_type_is_array (type))
    {
      element_type = g_variant_type_element (type);
      if (g_variant_type_is_dict_entry (element_type))
        return parse_json_dictionary (node, element_type, error);
      else
        return parse_json_array (node, element_type, error);
    }
  else if (g_variant_type_is_tuple (type))
    {
      return parse_json_tuple (node, g_variant_type_first (type), error);
    }
  else
    {
      parse_not_supported (type, error);
    }

  return NULL;
}
예제 #2
0
파일: demo.c 프로젝트: LeSpocky/DSLogic
static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
                      const struct sr_channel *ch,
                      const struct sr_channel_group *cg)
{
    (void) cg;

    struct dev_context *const devc = sdi->priv;

	switch (id) {
	case SR_CONF_SAMPLERATE:
		*data = g_variant_new_uint64(devc->cur_samplerate);
		break;
	case SR_CONF_LIMIT_SAMPLES:
        *data = g_variant_new_uint64(devc->limit_samples_show);
		break;
	case SR_CONF_LIMIT_MSEC:
		*data = g_variant_new_uint64(devc->limit_msec);
		break;
    case SR_CONF_DEVICE_MODE:
        *data = g_variant_new_int16(sdi->mode);
        break;
    case SR_CONF_TEST:
        *data = g_variant_new_boolean(FALSE);
        break;
    case SR_CONF_INSTANT:
        *data = g_variant_new_boolean(devc->instant);
        break;
    case SR_CONF_PATTERN_MODE:
        *data = g_variant_new_string(pattern_strings[devc->sample_generator]);
		break;
    case SR_CONF_MAX_HEIGHT:
        *data = g_variant_new_string(maxHeights[devc->max_height]);
        break;
    case SR_CONF_MAX_HEIGHT_VALUE:
        *data = g_variant_new_byte(devc->max_height);
        break;
    case SR_CONF_VPOS:
        *data = g_variant_new_double(ch->vpos);
        break;
    case SR_CONF_VDIV:
        *data = g_variant_new_uint64(ch->vdiv);
        break;
    case SR_CONF_FACTOR:
        *data = g_variant_new_uint64(ch->vfactor);
        break;
    case SR_CONF_TIMEBASE:
        *data = g_variant_new_uint64(devc->timebase);
        break;
    case SR_CONF_COUPLING:
        *data = g_variant_new_byte(ch->coupling);
        break;
    case SR_CONF_TRIGGER_VALUE:
        *data = g_variant_new_byte(ch->trig_value);
        break;
    case SR_CONF_EN_CH:
        *data = g_variant_new_boolean(ch->enabled);
        break;
    case SR_CONF_DATALOCK:
        *data = g_variant_new_boolean(devc->data_lock);
        break;
    case SR_CONF_MAX_DSO_SAMPLERATE:
        *data = g_variant_new_uint64(DEMO_MAX_DSO_SAMPLERATE);
        break;
    case SR_CONF_MAX_DSO_SAMPLELIMITS:
        *data = g_variant_new_uint64(DEMO_MAX_DSO_DEPTH);
        break;
    case SR_CONF_HW_DEPTH:
        *data = g_variant_new_uint64(DEMO_MAX_LOGIC_DEPTH);
        break;
    case SR_CONF_DSO_BITS:
        *data = g_variant_new_byte(devc->dso_bits);
        break;
    case SR_CONF_VLD_CH_NUM:
        *data = g_variant_new_int16(NUM_PROBES);
        break;
    default:
		return SR_ERR_NA;
	}

	return SR_OK;
}
GVariant* js_to_dbus(JSContextRef ctx,
                     const JSValueRef jsvalue,
                     const GVariantType* sig,
                     JSValueRef *exception)
{
    if (g_variant_type_is_array(sig)) {
        GVariantBuilder builder;
        g_variant_builder_init(&builder, sig);
        JSPropertyNameArrayRef array = JSObjectCopyPropertyNames(ctx,
                (JSObjectRef)jsvalue);

        const GVariantType* child_sig = g_variant_type_element(sig);

        if (g_variant_type_is_dict_entry(child_sig)) {
            const GVariantType* key_sig = g_variant_type_first(child_sig);
            const GVariantType* value_sig = g_variant_type_next(key_sig);
            for (size_t i=0; i < JSPropertyNameArrayGetCount(array); i++) {
                if (filter_function_child(ctx, jsvalue, i)) {
                    continue;
                }

                g_variant_builder_open(&builder, child_sig);
                JSValueRef key = JSValueMakeString(ctx,
                        JSPropertyNameArrayGetNameAtIndex(array, i));
                JSValueRef value = JSObjectGetPropertyAtIndex(ctx,
                        (JSObjectRef)jsvalue, i, NULL);
                g_variant_builder_add_value(&builder,
                        js_to_dbus(ctx, key, key_sig, exception));
                g_variant_builder_add_value(&builder,
                        js_to_dbus(ctx, value, value_sig, exception));
                g_variant_builder_close(&builder);
            }
            return g_variant_builder_end(&builder);

        } else {
            GVariantBuilder builder;
            g_variant_builder_init(&builder, sig);
            JSPropertyNameArrayRef array = JSObjectCopyPropertyNames(ctx,
                    (JSObjectRef)jsvalue);
            const GVariantType* child_sig = g_variant_type_element(sig);
            for (size_t i=0; i < JSPropertyNameArrayGetCount(array); i++) {
                if (filter_array_child(ctx, array, i)) {
                    continue;
                }
                g_variant_builder_add_value(&builder,
                    js_to_dbus(ctx,
                        JSObjectGetPropertyAtIndex(ctx,
                                                   (JSObjectRef)jsvalue, i,
                                                   NULL),
                        child_sig, exception));
            }
            JSPropertyNameArrayRelease(array);
            return g_variant_builder_end(&builder);
        }
    } else if (g_variant_type_is_tuple(sig)) {
        GVariantBuilder builder;
        g_variant_builder_init(&builder, sig);
        JSPropertyNameArrayRef array = JSObjectCopyPropertyNames(ctx,
                (JSObjectRef)jsvalue);
        const GVariantType* current_sig = g_variant_type_first(sig);
        for (size_t i=0; i < JSPropertyNameArrayGetCount(array); i++) {
            if (filter_array_child(ctx, array, i)) {
                continue;
            }
            g_variant_builder_add_value(&builder,
                js_to_dbus(ctx,
                    JSObjectGetPropertyAtIndex(ctx, (JSObjectRef)jsvalue, i,
                                               NULL),
                    current_sig, exception));
            current_sig = g_variant_type_next(current_sig);
        }
        JSPropertyNameArrayRelease(array);
        return g_variant_builder_end(&builder);
    } else {
        switch (g_variant_type_peek_string(sig)[0]) {
            case 'y':
                return g_variant_new_byte(JSValueToNumber(ctx, jsvalue,
                                                          exception));
            case 'n':
                return g_variant_new_int16(JSValueToNumber(ctx, jsvalue,
                                                           exception));
            case 'q':
                return g_variant_new_uint16(JSValueToNumber(ctx, jsvalue,
                                                            exception));
            case 'i':
                return g_variant_new_int32(JSValueToNumber(ctx, jsvalue,
                                                           exception));
            case 'u':
                return g_variant_new_uint32(JSValueToNumber(ctx, jsvalue,
                                                            exception));
            case 'x':
                return g_variant_new_int64(JSValueToNumber(ctx, jsvalue,
                                                           exception));
            case 't':
                return g_variant_new_uint64(JSValueToNumber(ctx, jsvalue,
                                                            exception));
            case 'd':
                return g_variant_new_double(JSValueToNumber(ctx, jsvalue,
                                                            exception));
            case 'h':
                return g_variant_new_handle(JSValueToNumber(ctx, jsvalue,
                                                            exception));
            case 'b':
                return g_variant_new_boolean(JSValueToBoolean(ctx, jsvalue));

            case 's': {
                    char* v = jsvalue_to_cstr(ctx, jsvalue);
                    GVariant* r = g_variant_new_string(v);
                    g_free(v);
                    return r;
                }

            case 'v': {
                    //TODO:
                    /*g_variant_new_variant()*/
                    g_assert_not_reached();
                }
        }
    }
    g_assert_not_reached();
}