Example #1
1
static long long rtkit_get_int_property(DBusConnection *connection, const char* propname, long long* propval) {
        DBusMessage *m = NULL, *r = NULL;
        DBusMessageIter iter, subiter;
        dbus_int64_t i64;
        dbus_int32_t i32;
        DBusError error;
        int current_type;
        long long ret;
        const char * interfacestr = "org.freedesktop.RealtimeKit1";

        dbus_error_init(&error);

        if (!(m = dbus_message_new_method_call(
                              RTKIT_SERVICE_NAME,
                              RTKIT_OBJECT_PATH,
                              "org.freedesktop.DBus.Properties",
                              "Get"))) {
                ret = -ENOMEM;
                goto finish;
        }

        if (!dbus_message_append_args(
                            m,
                            DBUS_TYPE_STRING, &interfacestr,
                            DBUS_TYPE_STRING, &propname,
                            DBUS_TYPE_INVALID)) {
                ret = -ENOMEM;
                goto finish;
        }

        if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
                ret = translate_error(error.name);
                goto finish;
        }

        if (dbus_set_error_from_message(&error, r)) {
                ret = translate_error(error.name);
                goto finish;
        }

        ret = -EBADMSG;
        dbus_message_iter_init(r, &iter);
        while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID) {

                if (current_type == DBUS_TYPE_VARIANT) {
                        dbus_message_iter_recurse(&iter, &subiter);

                        while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID) {

                                if (current_type == DBUS_TYPE_INT32) {
                                        dbus_message_iter_get_basic(&subiter, &i32);
                                        *propval = i32;
                                        ret = 0;
                                }

                                if (current_type == DBUS_TYPE_INT64) {
                                        dbus_message_iter_get_basic(&subiter, &i64);
                                        *propval = i64;
                                        ret = 0;
                                }

                                dbus_message_iter_next (&subiter);
                         }
                }
                dbus_message_iter_next (&iter);
        }

finish:

        if (m)
                dbus_message_unref(m);

        if (r)
                dbus_message_unref(r);

        dbus_error_free(&error);

        return ret;
}
Example #2
0
static gboolean parse_player_metadata(struct media_player *mp,
							DBusMessageIter *iter)
{
	DBusMessageIter dict;
	DBusMessageIter var;
	int ctype;
	gboolean title = FALSE;
	uint64_t uid;

	ctype = dbus_message_iter_get_arg_type(iter);
	if (ctype != DBUS_TYPE_ARRAY)
		return FALSE;

	dbus_message_iter_recurse(iter, &dict);

	if (mp->track != NULL)
		g_hash_table_unref(mp->track);

	mp->track = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
								g_free);

	while ((ctype = dbus_message_iter_get_arg_type(&dict)) !=
							DBUS_TYPE_INVALID) {
		DBusMessageIter entry;
		const char *key;

		if (ctype != DBUS_TYPE_DICT_ENTRY)
			return FALSE;

		dbus_message_iter_recurse(&dict, &entry);
		if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
			return FALSE;

		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);

		if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
			return FALSE;

		dbus_message_iter_recurse(&entry, &var);

		if (strcasecmp(key, "xesam:title") == 0) {
			if (!parse_string_metadata(mp, "Title", &var))
				return FALSE;
			title = TRUE;
		} else if (strcasecmp(key, "xesam:artist") == 0) {
			if (!parse_array_metadata(mp, "Artist", &var))
				return FALSE;
		} else if (strcasecmp(key, "xesam:album") == 0) {
			if (!parse_string_metadata(mp, "Album", &var))
				return FALSE;
		} else if (strcasecmp(key, "xesam:genre") == 0) {
			if (!parse_array_metadata(mp, "Genre", &var))
				return FALSE;
		} else if (strcasecmp(key, "mpris:length") == 0) {
			if (!parse_int64_metadata(mp, "Duration", &var))
				return FALSE;
		} else if (strcasecmp(key, "xesam:trackNumber") == 0) {
			if (!parse_int32_metadata(mp, "TrackNumber", &var))
				return FALSE;
		} else
			DBG("%s not supported, ignoring", key);

		dbus_message_iter_next(&dict);
	}

	if (title == FALSE)
		g_hash_table_insert(mp->track, g_strdup("Title"),
								g_strdup(""));

	mp->position = 0;
	g_timer_start(mp->timer);
	uid = get_uid(mp);

	avrcp_player_event(mp->player, AVRCP_EVENT_TRACK_CHANGED, &uid);
	avrcp_player_event(mp->player, AVRCP_EVENT_TRACK_REACHED_START, NULL);

	return TRUE;
}
Example #3
0
/* Helper for utils_get_value, reads a list. The "list" is a struct with the
 * list type and an array with the values directly in it.
 */
static GConfValue *
utils_get_value_helper_list (DBusMessageIter *iter)
{
  DBusMessageIter  struct_iter;
  DBusMessageIter  array_iter;
  GConfValue      *value;
  gint32           list_type;
  GSList          *list;
  GConfValue      *child_value;
  
  d(g_print ("Get value (list)\n"));

  value = gconf_value_new (GCONF_VALUE_LIST);

  dbus_message_iter_recurse (iter, &struct_iter);

  /* Get the list type. */
  dbus_message_iter_get_basic (&struct_iter, &list_type);
  gconf_value_set_list_type (value, list_type);

  /* Get the array. */
  dbus_message_iter_next (&struct_iter);
  dbus_message_iter_recurse (&struct_iter, &array_iter);

  /* And the values from the array. */
  list = NULL;
  switch (list_type)
    {
    case GCONF_VALUE_STRING:
      while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_STRING)
	{
	  const gchar *str;
	  
	  dbus_message_iter_get_basic (&array_iter, &str);

	  child_value = gconf_value_new (GCONF_VALUE_STRING);
	  gconf_value_set_string (child_value, str);
	  list = g_slist_prepend (list, child_value);

	  dbus_message_iter_next (&array_iter);
	}
      break;

    case GCONF_VALUE_INT:
      while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_INT32)
	{
	  gint32 i;
	  
	  dbus_message_iter_get_basic (&array_iter, &i);

	  child_value = gconf_value_new (GCONF_VALUE_INT);
	  gconf_value_set_int (child_value, i);
	  list = g_slist_prepend (list, child_value);

	  dbus_message_iter_next (&array_iter);
	}
      break;

    case GCONF_VALUE_FLOAT:
      while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_DOUBLE)
	{
	  gdouble d;
	  
	  dbus_message_iter_get_basic (&array_iter, &d);

	  child_value = gconf_value_new (GCONF_VALUE_FLOAT);
	  gconf_value_set_float (child_value, d);
	  list = g_slist_prepend (list, child_value);

	  dbus_message_iter_next (&array_iter);
	}
      break;

    case GCONF_VALUE_BOOL:
      while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_BOOLEAN)
	{
	  gboolean b;
	  
	  dbus_message_iter_get_basic (&array_iter, &b);

	  child_value = gconf_value_new (GCONF_VALUE_BOOL);
	  gconf_value_set_bool (child_value, b);
	  list = g_slist_prepend (list, child_value);

	  dbus_message_iter_next (&array_iter);
	}
      break;

    case GCONF_VALUE_SCHEMA:
      while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_STRUCT)
	{
	  child_value = utils_get_schema_value (&array_iter);
	  list = g_slist_prepend (list, child_value);

	  dbus_message_iter_next (&array_iter);
	}
      break;
      
    default:
      g_assert_not_reached ();
    }

  list = g_slist_reverse (list);
  gconf_value_set_list_nocopy (value, list);
  
  return value;
}
Example #4
0
int tool_cmd_scan(int argc, char *argv[])
{
	int ret = 0;
	int c;
	int timeout = DEFAULT_TIMEOUT_IN_SECONDS * 1000;
	const char *method_name;
	DBusConnection* connection = NULL;
	DBusMessage *message = NULL;
	DBusMessage *reply = NULL;
	DBusPendingCall *pending = NULL;
	DBusError error;
	int32_t scan_period = 0;
	uint32_t channel_mask = 0;
	dbus_bool_t joiner_flag = FALSE;
	dbus_bool_t enable_filtering = FALSE;
	uint16_t pan_id_filter = 0xffff;

	dbus_error_init(&error);

	sEnergyScan = false;
	sMleDiscoverScan = false;

	while (1) {
		static struct option long_options[] = {
			{"help", no_argument, 0, 'h'},
			{"timeout", required_argument, 0, 't'},
			{"channel", required_argument, 0, 'c'},
			{"energy", no_argument, 0, 'e'},
			{"discover", no_argument, 0, 'd'},
			{"joiner-only", no_argument, 0, 'j'},
			{"enable-filtering", no_argument, 0, 'f'},
			{"panid-filtering", required_argument, 0, 'p'},
			{0, 0, 0, 0}
		};

		int option_index = 0;
		c = getopt_long(argc, argv, "hc:t:edjfp:", long_options,
				&option_index);

		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_arg_list_help(scan_option_list, argv[0],
					    scan_cmd_syntax);
			ret = ERRORCODE_HELP;
			goto bail;

		case 't':
			timeout = strtol(optarg, NULL, 0);
			break;

		case 'c':
			channel_mask = strtomask_uint32(optarg);
			break;

		case 'e':
			sEnergyScan = true;
			break;

		case 'd':
			sMleDiscoverScan = true;
			break;

		case 'j':
			joiner_flag = TRUE;
			break;

		case 'f':
			enable_filtering = TRUE;
			break;

		case 'p':
			pan_id_filter = strtol(optarg, NULL, 0);
			break;
		}
	}

	if (optind < argc) {
		if (scan_period == 0) {
			scan_period = strtol(argv[optind], NULL, 0);
			optind++;
		}
	}

	if (optind < argc) {
			fprintf(stderr,
			        "%s: error: Unexpected extra argument: \"%s\"\n",
			argv[0], argv[optind]);
			ret = ERRORCODE_BADARG;
			goto bail;
		}

	if (gInterfaceName[0] == 0) {
		fprintf(stderr,
		        "%s: error: No WPAN interface set (use the `cd` command, or the `-I` argument for `wpanctl`).\n",
		        argv[0]);
		ret = ERRORCODE_BADARG;
		goto bail;
	}

	connection = dbus_bus_get(DBUS_BUS_STARTER, &error);

	if (!connection) {
		dbus_error_free(&error);
		dbus_error_init(&error);
		connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
	}

	require_string(connection != NULL, bail, error.message);

	dbus_bus_add_match(connection, gDBusObjectManagerMatchString, &error);

	require_string(error.name == NULL, bail, error.message);

	dbus_connection_add_filter(connection, &dbus_signal_handler, NULL, NULL);

	{
		char path[DBUS_MAXIMUM_NAME_LENGTH+1];
		char interface_dbus_name[DBUS_MAXIMUM_NAME_LENGTH+1];
		DBusMessageIter iter;
		ret = lookup_dbus_name_from_interface(interface_dbus_name, gInterfaceName);

		if (ret != 0) {
			print_error_diagnosis(ret);
			goto bail;
		}

		snprintf(
			path,
			sizeof(path),
			"%s/%s",
			WPANTUND_DBUS_PATH,
			gInterfaceName
		);

		if (sEnergyScan) {
			method_name = WPANTUND_IF_CMD_ENERGY_SCAN_START;
		} else {
			if (sMleDiscoverScan) {
				method_name = WPANTUND_IF_CMD_DISCOVER_SCAN_START;
			} else {
				method_name = WPANTUND_IF_CMD_NET_SCAN_START;
			}
		}

		message = dbus_message_new_method_call(
			interface_dbus_name,
			path,
			WPANTUND_DBUS_APIv1_INTERFACE,
			method_name
		);

		dbus_message_append_args(
			message,
			DBUS_TYPE_UINT32, &channel_mask,
			DBUS_TYPE_INVALID
		);

		if (sMleDiscoverScan) {
			dbus_message_append_args(
				message,
				DBUS_TYPE_BOOLEAN, &joiner_flag,
				DBUS_TYPE_BOOLEAN, &enable_filtering,
				DBUS_TYPE_UINT16, &pan_id_filter,
				DBUS_TYPE_INVALID
			);
		}

		print_scan_header();

		if (!sEnergyScan) {
			gScannedNetworkCount = 0;
		}

		if(!dbus_connection_send_with_reply(
		    connection,
		    message,
			&pending,
		    timeout
	    )) {
			fprintf(stderr, "%s: error: IPC failure\n", argv[0]);
			ret = ERRORCODE_UNKNOWN;
			goto bail;
		}

		while ((dbus_connection_get_dispatch_status(connection) == DBUS_DISPATCH_DATA_REMAINS)
			|| dbus_connection_has_messages_to_send(connection)
			|| !dbus_pending_call_get_completed(pending)
		) {
			dbus_connection_read_write_dispatch(connection, 5000 /*ms*/);
		}

		reply = dbus_pending_call_steal_reply(pending);

		require(reply != NULL, bail);


		dbus_message_iter_init(reply, &iter);

		if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32) {
			fprintf(stderr, "%s: error: Server returned a bad response ('%c')\n",
			        argv[0], dbus_message_iter_get_arg_type(&iter));
			ret = ERRORCODE_UNKNOWN;
			goto bail;
		}

		// Get return code
		dbus_message_iter_get_basic(&iter, &ret);

		if (ret) {
			fprintf(stderr, "%s failed with error %d. %s\n", argv[0], ret, wpantund_status_to_cstr(ret));
			print_error_diagnosis(ret);
			goto bail;
		}
	}


bail:

	if (reply) {
		dbus_message_unref(reply);
	}

	if (pending != NULL) {
		dbus_pending_call_unref(pending);
	}

	if (message) {
		dbus_message_unref(message);
	}

	if (connection) {
		dbus_bus_remove_match(connection, gDBusObjectManagerMatchString, NULL);
		dbus_connection_remove_filter(connection,&dbus_signal_handler,NULL);
		dbus_connection_unref(connection);
	}

	dbus_error_free(&error);

	return ret;
}
Example #5
0
/**
 * Call a method on a remote object
 */
void query(char* param)
{
	DBusMessage* msg;
	DBusMessageIter args;
	DBusConnection* conn;
	DBusError err;
	DBusPendingCall* pending;
	int ret;
	bool stat;
	dbus_uint32_t level;

	printf("Calling remote method with %s\n", param);

	// initialiset the errors
	dbus_error_init(&err);

	// connect to the system bus and check for errors
	conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
	if (dbus_error_is_set(&err)) {
		fprintf(stderr, "Connection Error (%s)\n", err.message);
		dbus_error_free(&err);
	}
	if (NULL == conn) {
		exit(1);
	}

	// request our name on the bus
	ret = dbus_bus_request_name(conn, "test.method.caller", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
	if (dbus_error_is_set(&err)) {
		fprintf(stderr, "Name Error (%s)\n", err.message);
		dbus_error_free(&err);
	}
	if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
		exit(1);
	}

	// create a new method call and check for errors
	msg = dbus_message_new_method_call("test.method.server",// target for the method call
					   "/test/method/Object", // object to call on
					   "test.method.Type",	  // interface to call on
					   "Method");	     // method name
	if (NULL == msg) {
		fprintf(stderr, "Message Null\n");
		exit(1);
	}

	// append arguments
	dbus_message_iter_init_append(msg, &args);
	if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &param)) {
		fprintf(stderr, "Out Of Memory!\n");
		exit(1);
	}

	// send message and get a handle for a reply
	if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
		fprintf(stderr, "Out Of Memory!\n");
		exit(1);
	}
	if (NULL == pending) {
		fprintf(stderr, "Pending Call Null\n");
		exit(1);
	}
	dbus_connection_flush(conn);

	printf("Request Sent\n");

	// free message
	dbus_message_unref(msg);

	// block until we recieve a reply
	dbus_pending_call_block(pending);

	// get the reply message
	msg = dbus_pending_call_steal_reply(pending);
	if (NULL == msg) {
		fprintf(stderr, "Reply Null\n");
		exit(1);
	}
	// free the pending message handle
	dbus_pending_call_unref(pending);

	// read the parameters
	if (!dbus_message_iter_init(msg, &args))
		fprintf(stderr, "Message has no arguments!\n");
	else if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&args))
		fprintf(stderr, "Argument is not boolean!\n");
	else
		dbus_message_iter_get_basic(&args, &stat);

	if (!dbus_message_iter_next(&args))
		fprintf(stderr, "Message has too few arguments!\n");
	else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args))
		fprintf(stderr, "Argument is not int!\n");
	else
		dbus_message_iter_get_basic(&args, &level);

	printf("Got Reply: %d, %d\n", stat, level);

	// free reply
	dbus_message_unref(msg);
}
Example #6
0
static void mediaplayer_register(const char *id) {
	DBusMessage* msg;
	DBusMessageIter args, element;
	DBusPendingCall* pending;
	char *name=NULL;
	static const char *prop="Identity";
	struct MEDIAPLAYER **mp;
	char player[256];
	
	for(mp=&mediaplayer; *mp; mp=&((*mp)->next)) {
		if(!strcmp(id, (*mp)->id))
			return;
	}
	sprintf(player, "%s.%s", mpris.interface, id);
	if(!(msg=dbus_message_new_method_call(player, mpris.object, dbus.interface_prop, "Get")))
		goto do_register;
	dbus_message_iter_init_append(msg, &args);
	if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mpris.interface)) {
		dbus_message_unref(msg);
		goto do_register;
	}
	if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &prop)) {
		dbus_message_unref(msg);
		goto do_register;
	}
	if(!dbus_connection_send_with_reply(dbus.session.connection, msg, &pending, -1)) {
		dbus_message_unref(msg);
		goto do_register;
	}
	if(!pending) {
		dbus_message_unref(msg);
		goto do_register;
	}
	dbus_connection_flush(dbus.session.connection);
	dbus_message_unref(msg);
	dbus_pending_call_block(pending);
	if (!(msg=dbus_pending_call_steal_reply(pending))) {
		dbus_pending_call_unref(pending);
		goto do_register;
	}
	dbus_pending_call_unref(pending);
	if(!dbus_message_iter_init(msg, &args)) {
		dbus_message_unref(msg);
		goto do_register;
	} else if(dbus_message_iter_get_arg_type(&args)!=DBUS_TYPE_VARIANT) {
		dbus_message_unref(msg);
		goto do_register;
	}
	
	dbus_message_iter_recurse(&args, &element);
	dbus_message_iter_get_basic(&element, &name);
	dbus_message_unref(msg);
	
	do_register:
	*mp=malloc(sizeof(struct MEDIAPLAYER));
	(*mp)->id=malloc(strlen(id)+1);
	if(name) {
		(*mp)->name=malloc(strlen(name)+1);
		strcpy((void *) (*mp)->name, name);
	} else
		(*mp)->name=NULL;
	(*mp)->next=NULL;
	strcpy((void *) (*mp)->id, id);
}
Example #7
0
int
pdc_intf_show_ipv6_maps(DBusConnection *connection,
							int hansitype, int insid,
							struct pdc_ipv6_map_conf *map_conf)
{
	DBusMessage *query = NULL;
	DBusMessage *reply = NULL;
	DBusError err = {0};
	DBusMessageIter  iter;
	DBusMessageIter  iter_array;
	DBusMessageIter  iter_struct;
	int iRet = 0;
	uint32_t num = 0;
	int i = 0;
	int eag_slotid = 0;
	int eag_hansitype = 0;
	int eag_hansiid = 0;

	if (NULL == map_conf){
		return EAG_ERR_INPUT_PARAM_ERR;
	}

	memset(map_conf, 0, sizeof(struct pdc_ipv6_map_conf));
	
	pdc_dbus_path_reinit(hansitype,insid);
	query = dbus_message_new_method_call(
									PDC_DBUS_NAME,
									PDC_DBUS_OBJPATH,
									PDC_DBUS_INTERFACE,
									PDC_DBUS_METHOD_SHOW_IPV6_MAPS);
	
	dbus_error_init(&err);

	reply = dbus_connection_send_with_reply_and_block (
						connection, query, -1, &err );
	
	dbus_message_unref(query);
	
	if (NULL == reply) {		
		if (dbus_error_is_set(&err)) {
			dbus_error_free(&err);
		}
		return EAG_ERR_DBUS_FAILED;
	}else{
		dbus_message_iter_init(reply,&iter);
		dbus_message_iter_get_basic(&iter, &iRet);
		
		if( EAG_RETURN_OK == iRet ){
			dbus_message_iter_next(&iter);
			dbus_message_iter_get_basic(&iter, &num);
			
			if( num > MAX_PDC_IPV6_MAP_NUM ){
				num = MAX_PDC_IPV6_MAP_NUM;
			}
			map_conf->num = num;
			if( num > 0 ){
				dbus_message_iter_next(&iter);	
				dbus_message_iter_recurse(&iter,&iter_array);			
			
				for( i=0; i<num; i++ ){					
					dbus_message_iter_recurse(&iter_array,&iter_struct);
					dbus_message_iter_get_basic(&iter_struct, 
												&(map_conf->map[i].userip[0]));
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct, 
												&(map_conf->map[i].userip[1]));
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct, 
												&(map_conf->map[i].userip[2]));
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct, 
												&(map_conf->map[i].userip[3]));
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct,
									&(map_conf->map[i].prefix_length));
					
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct,
									&eag_slotid);
					map_conf->map[i].eag_slotid = eag_slotid;					
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct,
									&eag_hansitype);
					map_conf->map[i].eag_hansitype = eag_hansitype;					
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct,
									&eag_hansiid);
					map_conf->map[i].eag_hansiid = eag_hansiid;					
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct,
									&(map_conf->map[i].eag_ip));
					dbus_message_iter_next(&iter_struct);
					dbus_message_iter_get_basic(&iter_struct,
									&(map_conf->map[i].eag_port));					
					dbus_message_iter_next(&iter_array);					
				}
			}
		}
	}
	
	dbus_message_unref(reply);
	
	return iRet;
	
}
static DBusHandlerResult
my_com_netsplit_Nih_Test_Peek_method (NihDBusObject * object,
                                      NihDBusMessage *message)
{
	DBusMessageIter iter;
	DBusMessage *   reply;
	uint32_t        address;

	nih_assert (object != NULL);
	nih_assert (message != NULL);

	/* Iterate the arguments to the message and demarshal into arguments
	 * for our own function call.
	 */
	dbus_message_iter_init (message->message, &iter);

	/* Demarshal a uint32_t from the message */
	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32) {
		reply = dbus_message_new_error (message->message, DBUS_ERROR_INVALID_ARGS,
		                                "Invalid arguments to Peek method");
		if (! reply)
			return DBUS_HANDLER_RESULT_NEED_MEMORY;

		if (! dbus_connection_send (message->connection, reply, NULL)) {
			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		}

		dbus_message_unref (reply);
		return DBUS_HANDLER_RESULT_HANDLED;
	}

	dbus_message_iter_get_basic (&iter, &address);

	dbus_message_iter_next (&iter);

	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
		reply = dbus_message_new_error (message->message, DBUS_ERROR_INVALID_ARGS,
		                                "Invalid arguments to Peek method");
		if (! reply)
			return DBUS_HANDLER_RESULT_NEED_MEMORY;

		if (! dbus_connection_send (message->connection, reply, NULL)) {
			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		}

		dbus_message_unref (reply);
		return DBUS_HANDLER_RESULT_HANDLED;
	}

	/* Call the handler function */
	nih_error_push_context ();
	if (my_test_peek (object->data, message, address) < 0) {
		NihError *err;

		err = nih_error_get ();
		if (err->number == ENOMEM) {
			nih_free (err);
			nih_error_pop_context ();

			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		} else if (err->number == NIH_DBUS_ERROR) {
			NihDBusError *dbus_err = (NihDBusError *)err;

			reply = NIH_MUST (dbus_message_new_error (message->message, dbus_err->name, err->message));
			nih_free (err);
			nih_error_pop_context ();

			NIH_MUST (dbus_connection_send (message->connection, reply, NULL));

			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_HANDLED;
		} else {
			reply = NIH_MUST (dbus_message_new_error (message->message, DBUS_ERROR_FAILED, err->message));
			nih_free (err);
			nih_error_pop_context ();

			NIH_MUST (dbus_connection_send (message->connection, reply, NULL));

			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_HANDLED;
		}
	}
	nih_error_pop_context ();

	return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
my_com_netsplit_Nih_Test_IsValidAddress_method (NihDBusObject * object,
                                                NihDBusMessage *message)
{
	DBusMessageIter iter;
	DBusMessage *   reply;
	uint32_t        address;
	int             is_valid;

	nih_assert (object != NULL);
	nih_assert (message != NULL);

	/* Iterate the arguments to the message and demarshal into arguments
	 * for our own function call.
	 */
	dbus_message_iter_init (message->message, &iter);

	/* Demarshal a uint32_t from the message */
	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32) {
		reply = dbus_message_new_error (message->message, DBUS_ERROR_INVALID_ARGS,
		                                "Invalid arguments to IsValidAddress method");
		if (! reply)
			return DBUS_HANDLER_RESULT_NEED_MEMORY;

		if (! dbus_connection_send (message->connection, reply, NULL)) {
			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		}

		dbus_message_unref (reply);
		return DBUS_HANDLER_RESULT_HANDLED;
	}

	dbus_message_iter_get_basic (&iter, &address);

	dbus_message_iter_next (&iter);

	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
		reply = dbus_message_new_error (message->message, DBUS_ERROR_INVALID_ARGS,
		                                "Invalid arguments to IsValidAddress method");
		if (! reply)
			return DBUS_HANDLER_RESULT_NEED_MEMORY;

		if (! dbus_connection_send (message->connection, reply, NULL)) {
			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		}

		dbus_message_unref (reply);
		return DBUS_HANDLER_RESULT_HANDLED;
	}

	/* Call the handler function */
	nih_error_push_context ();
	if (my_test_is_valid_address (object->data, message, address, &is_valid) < 0) {
		NihError *err;

		err = nih_error_get ();
		if (err->number == ENOMEM) {
			nih_free (err);
			nih_error_pop_context ();

			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		} else if (err->number == NIH_DBUS_ERROR) {
			NihDBusError *dbus_err = (NihDBusError *)err;

			reply = NIH_MUST (dbus_message_new_error (message->message, dbus_err->name, err->message));
			nih_free (err);
			nih_error_pop_context ();

			NIH_MUST (dbus_connection_send (message->connection, reply, NULL));

			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_HANDLED;
		} else {
			reply = NIH_MUST (dbus_message_new_error (message->message, DBUS_ERROR_FAILED, err->message));
			nih_free (err);
			nih_error_pop_context ();

			NIH_MUST (dbus_connection_send (message->connection, reply, NULL));

			dbus_message_unref (reply);
			return DBUS_HANDLER_RESULT_HANDLED;
		}
	}
	nih_error_pop_context ();

	/* If the sender doesn't care about a reply, don't bother wasting
	 * effort constructing and sending one.
	 */
	if (dbus_message_get_no_reply (message->message))
		return DBUS_HANDLER_RESULT_HANDLED;

	do {
		__label__ enomem;

		/* Construct the reply message. */
		reply = dbus_message_new_method_return (message->message);
		if (! reply)
			goto enomem;

		dbus_message_iter_init_append (reply, &iter);

		/* Marshal a int onto the message */
		if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &is_valid)) {
			dbus_message_unref (reply);
			reply = NULL;
			goto enomem;
		}
	enomem: __attribute__ ((unused));
	} while (! reply);

	/* Send the reply, appending it to the outgoing queue. */
	NIH_MUST (dbus_connection_send (message->connection, reply, NULL));

	dbus_message_unref (reply);

	return DBUS_HANDLER_RESULT_HANDLED;
}
Example #10
0
static void request_input_reply(DBusMessage *reply, void *user_data)
{
	struct request_input_reply *pptp_reply = user_data;
	const char *error = NULL;
	char *username = NULL, *password = NULL;
	char *key;
	DBusMessageIter iter, dict;

	DBG("provider %p", pptp_reply->provider);

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
		error = dbus_message_get_error_name(reply);
		goto done;
	}

	if (!vpn_agent_check_reply_has_dict(reply))
		goto done;

	dbus_message_iter_init(reply, &iter);
	dbus_message_iter_recurse(&iter, &dict);
	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry, value;
		const char *str;

		dbus_message_iter_recurse(&dict, &entry);
		if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
			break;

		dbus_message_iter_get_basic(&entry, &key);

		if (g_str_equal(key, "Username")) {
			dbus_message_iter_next(&entry);
			if (dbus_message_iter_get_arg_type(&entry)
							!= DBUS_TYPE_VARIANT)
				break;
			dbus_message_iter_recurse(&entry, &value);
			if (dbus_message_iter_get_arg_type(&value)
							!= DBUS_TYPE_STRING)
				break;
			dbus_message_iter_get_basic(&value, &str);
			username = g_strdup(str);
		}

		if (g_str_equal(key, "Password")) {
			dbus_message_iter_next(&entry);
			if (dbus_message_iter_get_arg_type(&entry)
							!= DBUS_TYPE_VARIANT)
				break;
			dbus_message_iter_recurse(&entry, &value);
			if (dbus_message_iter_get_arg_type(&value)
							!= DBUS_TYPE_STRING)
				break;
			dbus_message_iter_get_basic(&value, &str);
			password = g_strdup(str);
		}

		dbus_message_iter_next(&dict);
	}

done:
	pptp_reply->callback(pptp_reply->provider, username, password, error,
				pptp_reply->user_data);

	g_free(username);
	g_free(password);

	g_free(pptp_reply);
}
Example #11
0
CVariant CDBusUtil::ParseType(DBusMessageIter *itr)
{
  CVariant value;
  const char *    string  = NULL;
  dbus_int32_t    int32   = 0;
  dbus_uint32_t   uint32  = 0;
  dbus_int64_t    int64   = 0;
  dbus_uint64_t   uint64  = 0;
  dbus_bool_t     boolean = false;
  double          doublev = 0;

  int type = dbus_message_iter_get_arg_type(itr);
  switch (type)
  {
  case DBUS_TYPE_OBJECT_PATH:
  case DBUS_TYPE_STRING:
    dbus_message_iter_get_basic(itr, &string);
    value = string;
    break;
  case DBUS_TYPE_UINT32:
    dbus_message_iter_get_basic(itr, &uint32);
    value = (uint64_t)uint32;
    break;
  case DBUS_TYPE_BYTE:
  case DBUS_TYPE_INT32:
    dbus_message_iter_get_basic(itr, &int32);
    value = (int64_t)int32;
    break;
  case DBUS_TYPE_UINT64:
    dbus_message_iter_get_basic(itr, &uint64);
    value = (uint64_t)uint64;
    break;
  case DBUS_TYPE_INT64:
    dbus_message_iter_get_basic(itr, &int64);
    value = (int64_t)int64;
    break;
  case DBUS_TYPE_BOOLEAN:
    dbus_message_iter_get_basic(itr, &boolean);
    value = (bool)boolean;
    break;
  case DBUS_TYPE_DOUBLE:
    dbus_message_iter_get_basic(itr, &doublev);
    value = (double)doublev;
    break;
  case DBUS_TYPE_ARRAY:
    DBusMessageIter array;
    dbus_message_iter_recurse(itr, &array);

    value = CVariant::VariantTypeArray;

    do
    {
      CVariant item = ParseType(&array);
      if (!item.isNull())
        value.push_back(item);
    } while (dbus_message_iter_next(&array));
    break;
  }

  return value;
}
Example #12
0
static int pptp_notify(DBusMessage *msg, struct vpn_provider *provider)
{
	DBusMessageIter iter, dict;
	const char *reason, *key, *value;
	char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
	char *ifname = NULL, *nameservers = NULL;
	struct connman_ipaddress *ipaddress = NULL;

	dbus_message_iter_init(msg, &iter);

	dbus_message_iter_get_basic(&iter, &reason);
	dbus_message_iter_next(&iter);

	if (!provider) {
		connman_error("No provider found");
		return VPN_STATE_FAILURE;
	}

	if (strcmp(reason, "auth failed") == 0) {
		DBG("authentication failure");

		vpn_provider_set_string(provider, "PPTP.User", NULL);
		vpn_provider_set_string(provider, "PPTP.Password", NULL);

		return VPN_STATE_AUTH_FAILURE;
	}

	if (strcmp(reason, "connect"))
		return VPN_STATE_DISCONNECT;

	dbus_message_iter_recurse(&iter, &dict);

	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry;

		dbus_message_iter_recurse(&dict, &entry);
		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);
		dbus_message_iter_get_basic(&entry, &value);

		DBG("%s = %s", key, value);

		if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
			addressv4 = g_strdup(value);

		if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
			netmask = g_strdup(value);

		if (!strcmp(key, "INTERNAL_IP4_DNS"))
			nameservers = g_strdup(value);

		if (!strcmp(key, "INTERNAL_IFNAME"))
			ifname = g_strdup(value);

		dbus_message_iter_next(&dict);
	}

	if (vpn_set_ifname(provider, ifname) < 0) {
		g_free(ifname);
		g_free(addressv4);
		g_free(netmask);
		g_free(nameservers);
		return VPN_STATE_FAILURE;
	}

	if (addressv4)
		ipaddress = connman_ipaddress_alloc(AF_INET);

	g_free(ifname);

	if (!ipaddress) {
		connman_error("No IP address for provider");
		g_free(addressv4);
		g_free(netmask);
		g_free(nameservers);
		return VPN_STATE_FAILURE;
	}

	value = vpn_provider_get_string(provider, "HostIP");
	if (value) {
		vpn_provider_set_string(provider, "Gateway", value);
		gateway = g_strdup(value);
	}

	if (addressv4)
		connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
					gateway);

	vpn_provider_set_ipaddress(provider, ipaddress);
	vpn_provider_set_nameservers(provider, nameservers);

	g_free(addressv4);
	g_free(netmask);
	g_free(gateway);
	g_free(nameservers);
	connman_ipaddress_free(ipaddress);

	return VPN_STATE_CONNECT;
}
Example #13
0
static DBusMessage* dbus_read_servers_ex(DBusMessage *message)
{
  DBusMessageIter iter, array_iter, string_iter;
  DBusMessage *error = NULL;
  const char *addr_err;

  if (!dbus_message_iter_init(message, &iter))
    {
      return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
                                    "Failed to initialize dbus message iter");
    }

  /* check that the message contains an array of arrays */
  if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) ||
      (dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY))
    {
      return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
                                    "Expected array of string arrays");
     }
 
  mark_dbus();

  /* array_iter points to each "as" element in the outer array */
  dbus_message_iter_recurse(&iter, &array_iter);
  while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID)
    {
      const char *str = NULL;
      union  mysockaddr addr, source_addr;
      char interface[IF_NAMESIZE];
      char *str_addr;

      /* check the types of the struct and its elements */
      if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) ||
          (dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING))
        {
          error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
                                         "Expected inner array of strings");
          break;
        }

      /* string_iter points to each "s" element in the inner array */
      dbus_message_iter_recurse(&array_iter, &string_iter);
      if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING)
        {
          /* no IP address given */
          error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
                                         "Expected IP address");
          break;
        }

      dbus_message_iter_get_basic(&string_iter, &str);
      if (!str || !strlen (str))
        {
          error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
                                         "Empty IP address");
          break;
        }

      memset(&addr, 0, sizeof(addr));
      memset(&source_addr, 0, sizeof(source_addr));
      memset(&interface, 0, sizeof(interface));

      /* dup the string because it gets modified during parsing */
      str_addr = strdup(str);
      if (!str_addr)
        {
          error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
                                         "Out of memory parsing IP address");
          break;
        }

      /* parse the IP address */
      addr_err = parse_server(str_addr, &addr, &source_addr, &interface, NULL);
      free(str_addr);

      if (addr_err)
        {
          error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
                                                "Invalid IP address '%s': %s",
                                                str, addr_err);
          break;
        }

      /* jump past the address to the domain list (if any) */
      dbus_message_iter_next (&string_iter);

      /* parse domains and add each server/domain pair to the list */
      do {
        str = NULL;
       if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING)
         dbus_message_iter_get_basic(&string_iter, &str);
       dbus_message_iter_next (&string_iter);

       add_update_server(&addr, &source_addr, interface, str);
      } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING);

      /* jump to next element in outer array */
      dbus_message_iter_next(&array_iter);
    }

  cleanup_dbus();

  return error;
}
Example #14
0
static void dbus_read_servers(DBusMessage *message)
{
  DBusMessageIter iter;
  union  mysockaddr addr, source_addr;
  char *domain;
  
  dbus_message_iter_init(message, &iter);

  mark_dbus();

  while (1)
    {
      int skip = 0;

      if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32)
	{
	  u32 a;
	  
	  dbus_message_iter_get_basic(&iter, &a);
	  dbus_message_iter_next (&iter);
	  
#ifdef HAVE_SOCKADDR_SA_LEN
	  source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in);
#endif
	  addr.in.sin_addr.s_addr = ntohl(a);
	  source_addr.in.sin_family = addr.in.sin_family = AF_INET;
	  addr.in.sin_port = htons(NAMESERVER_PORT);
	  source_addr.in.sin_addr.s_addr = INADDR_ANY;
	  source_addr.in.sin_port = htons(daemon->query_port);
	}
      else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE)
	{
	  unsigned char p[sizeof(struct in6_addr)];
	  unsigned int i;

	  skip = 1;

	  for(i = 0; i < sizeof(struct in6_addr); i++)
	    {
	      dbus_message_iter_get_basic(&iter, &p[i]);
	      dbus_message_iter_next (&iter);
	      if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE)
		break;
	    }

#ifndef HAVE_IPV6
	  my_syslog(LOG_WARNING, _("attempt to set an IPv6 server address via DBus - no IPv6 support"));
#else
	  if (i == sizeof(struct in6_addr)-1)
	    {
	      memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr));
#ifdef HAVE_SOCKADDR_SA_LEN
              source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6);
#endif
              source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6;
              addr.in6.sin6_port = htons(NAMESERVER_PORT);
              source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0;
	      source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0;
              source_addr.in6.sin6_addr = in6addr_any;
              source_addr.in6.sin6_port = htons(daemon->query_port);
	      skip = 0;
	    }
#endif
	}
      else
	/* At the end */
	break;
      
      /* process each domain */
      do {
	if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
	  {
	    dbus_message_iter_get_basic(&iter, &domain);
	    dbus_message_iter_next (&iter);
	  }
	else
	  domain = NULL;
	
	if (!skip)
	  add_update_server(&addr, &source_addr, NULL, domain);
     
      } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING); 
    }
   
  /* unlink and free anything still marked. */
  cleanup_dbus();
}
Example #15
0
static struct TRACK mediaplayer_get_track(const char *id) {
	int current_type;
	DBusMessage* msg;
	DBusMessageIter args, array, dict, element, var, artist;
	DBusPendingCall* pending;
	static const char *prop="Metadata";
	char player[256];
	struct TRACK track={
		"Unknown",
		"Unknown",
		"Unknown",
	};
	
	sprintf(player, "%s%s", mpris.base, id);
	if(!(msg=dbus_message_new_method_call(player, mpris.object, dbus.interface_prop, "Get")))
		return track;
	dbus_message_iter_init_append(msg, &args);
	if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mpris.interface_player)) {
		dbus_message_unref(msg);
		return track;
	}
	if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &prop)) {
		dbus_message_unref(msg);
		return track;
	}
	if(!dbus_connection_send_with_reply(dbus.session.connection, msg, &pending, -1)) {
		dbus_message_unref(msg);
		return track;
	}
	if(!pending) {
		dbus_message_unref(msg);
		return track;
	}
	dbus_connection_flush(dbus.session.connection);
	dbus_message_unref(msg);
	dbus_pending_call_block(pending);
	if(!(msg=dbus_pending_call_steal_reply(pending))) {
		dbus_pending_call_unref(pending);
		return track;
	}
	dbus_pending_call_unref(pending);
	if(!dbus_message_iter_init(msg, &args)) {
		dbus_message_unref(msg);
		return track;
	}
	
	if(dbus_message_iter_get_arg_type(&args)!=DBUS_TYPE_VARIANT) {
		dbus_message_unref(msg);
		return track;
	}
	dbus_message_iter_recurse(&args, &array);
	if(dbus_message_iter_get_arg_type(&array)!=DBUS_TYPE_ARRAY) {
		dbus_message_unref(msg);
		return track;
	}
	
	for(dbus_message_iter_recurse(&array, &dict); (current_type=dbus_message_iter_get_arg_type(&dict))!=DBUS_TYPE_INVALID; dbus_message_iter_next(&dict)) {
		char *element_key, *element_value;
		int element_type;
		dbus_message_iter_recurse(&dict, &element);
		if(dbus_message_iter_get_arg_type(&element)!=DBUS_TYPE_STRING)
			continue;
		
		dbus_message_iter_get_basic(&element, &element_key);
		dbus_message_iter_next(&element);
		if(dbus_message_iter_get_arg_type(&element)!=DBUS_TYPE_VARIANT)
			continue;
		
		dbus_message_iter_recurse(&element, &var);
		element_type=dbus_message_iter_get_arg_type(&var);
		if(element_type==DBUS_TYPE_STRING) {
			dbus_message_iter_get_basic(&var, &element_value);
			if(!strcmp(element_key, "xesam:album"))
				snprintf(track.album, TRACK_ELEMENT_LENGTH, "%s", element_value);
			else if(!strcmp(element_key, "xesam:title"))
				snprintf(track.title, TRACK_ELEMENT_LENGTH, "%s", element_value);
		} else if(element_type==DBUS_TYPE_ARRAY) {
			/*just handle one artist for now*/
			dbus_message_iter_recurse(&var, &artist);
			if(dbus_message_iter_get_arg_type(&artist)!=DBUS_TYPE_STRING)
				continue;
			dbus_message_iter_get_basic(&artist, &element_value);
			if(!strcmp(element_key, "xesam:artist"))
				snprintf(track.artist, TRACK_ELEMENT_LENGTH, "%s", element_value);
		}
	}
	dbus_message_unref(msg);
	return track;
}
static void
do_iter (DBusMessageIter *iter)
{
  static struct timeval album_time={0,0}, artist_time={0,0}, title_time={0,0}, now_time={0,0};
  char *decoded;
  int pos, len, i;
	do
		{
			int type = dbus_message_iter_get_arg_type (iter);
			const char *str;

			if (type == DBUS_TYPE_INVALID)
				break;

			switch (type)
			{
				case DBUS_TYPE_STRING:
					dbus_message_iter_get_basic (iter, &str);
          if(varian_now){
            gettimeofday(&now_time,NULL);
            mylog("DBUS>> %s : '%s'\n",last_string,str);
            if((strcmp(ALBUM_STRING,last_string)==0) || (strcmp(ALBUM_STRING2,last_string)==0)){
              strncpy(album,str,MAX_STR_LEN);
              now_playing_changed=1;
              if(now_time.tv_sec-artist_time.tv_sec>DBUS_INTERVAL) artist[0]='\0';
              if(now_time.tv_sec-title_time.tv_sec >DBUS_INTERVAL) title[0]='\0';
              album_time.tv_sec=now_time.tv_sec;
              mylog("DBUS>> ALBUM='%s'\n",str);
            }
            else if((strcmp(TITLE_STRING,last_string)==0) || (strcmp(TITLE_STRING2,last_string)==0)){
              strncpy(title,str,MAX_STR_LEN);
              now_playing_changed=1;
              if(now_time.tv_sec-artist_time.tv_sec>DBUS_INTERVAL) artist[0]='\0';
              if(now_time.tv_sec-album_time.tv_sec >DBUS_INTERVAL) album[0]='\0';
              title_time.tv_sec=now_time.tv_sec;
              mylog("DBUS>> TITLE='%s'\n",str);
            }
            else if((strcmp(ARTIST_STRING,last_string)==0) || (strcmp(ARTIST_STRING2,last_string)==0)){
              strncpy(artist,str,MAX_STR_LEN);
              now_playing_changed=1;
              if(now_time.tv_sec-album_time.tv_sec>DBUS_INTERVAL) album[0]='\0';
              if(now_time.tv_sec-title_time.tv_sec>DBUS_INTERVAL) title[0]='\0';
              artist_time.tv_sec=now_time.tv_sec;
              mylog("DBUS>> ARTIST='%s'\n",str);
            }
            else if((now_playing_changed==0) &&
                    (strcmp(LOC_STRING,last_string)==0)){
              decoded=url_decode(str);
              len=strlen(decoded);
              pos=0;
              for(i=0;i<len;++i){
                if(decoded[i]=='/')
                  pos=i+1;
              }
              strncpy(title,decoded+pos,MAX_STR_LEN);
              free(decoded);
              now_playing_changed=1;
            }
          }
          strncpy(last_string,str,MAX_STR_LEN);
					break;
				case DBUS_TYPE_VARIANT:
				{
					DBusMessageIter subiter;
					dbus_message_iter_recurse (iter, &subiter);
//					printf ("variant:");
          varian_now=1;
					do_iter (&subiter);
					varian_now=0;
					break;
				}
				case DBUS_TYPE_ARRAY:
				{
					int current_type;
					DBusMessageIter subiter;

					dbus_message_iter_recurse (iter, &subiter);

//					printf("[");
					while ((current_type = dbus_message_iter_get_arg_type (&subiter))
						!= DBUS_TYPE_INVALID)
					{
						do_iter (&subiter);
						dbus_message_iter_next (&subiter);
//						if (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID)
//							printf (",");
					}
//					printf("]");
					break;
				}
				case DBUS_TYPE_DICT_ENTRY:
				{
					DBusMessageIter subiter;
					dbus_message_iter_recurse (iter, &subiter);
//					printf("{");
					do_iter (&subiter);
					dbus_message_iter_next (&subiter);
					do_iter (&subiter);
//					printf("}");
					break;
				}
				default:
					break;
				}

	} while (dbus_message_iter_next (iter));
}
Example #17
0
int indicator_music_init(Indicator *indicator) {
	DBusMessage* msg;
	DBusMessageIter args, element;
	DBusPendingCall* pending;
	char *player;
	int current_type;
	
	if(!dbus.session.connection)
		return -1;
	
	if(!(msg=dbus_message_new_method_call(dbus.interface, dbus.object, dbus.interface, "ListNames")))
		return -1;
	
	if(!dbus_connection_send_with_reply(dbus.session.connection, msg, &pending, -1)) {
		dbus_connection_unref(dbus.session.connection);
		return -1;
	}
	if(!pending) {
		dbus_message_unref(msg);
		dbus_connection_unref(dbus.session.connection);
		return -1;
	}
	dbus_connection_flush(dbus.session.connection);
	dbus_message_unref(msg);
	dbus_pending_call_block(pending);

	if(!(msg=dbus_pending_call_steal_reply(pending))) {
		dbus_connection_unref(dbus.session.connection);
		return -1;
	}
	dbus_pending_call_unref(pending);

	if(!(dbus_message_iter_init(msg, &args)&&dbus_message_iter_get_arg_type(&args)==DBUS_TYPE_ARRAY)) {
		dbus_message_unref(msg);
		dbus_connection_unref(dbus.session.connection);
		return -1;
	}
	
	for(dbus_message_iter_recurse(&args, &element); (current_type=dbus_message_iter_get_arg_type(&element))!=DBUS_TYPE_INVALID; dbus_message_iter_next(&element)) {
		if(current_type!=DBUS_TYPE_STRING)
			continue;
		dbus_message_iter_get_basic(&element, &player);
		if(!strncmp(player, mpris.base, strlen(mpris.base))) {
			mediaplayer_register(player+strlen(mpris.base));
		}
	}
	dbus_message_unref(msg);
	
	dbus_bus_add_match(dbus.session.connection, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", &dbus.session.error);
	dbus_connection_flush(dbus.session.connection);
	if(dbus_error_is_set(&dbus.session.error)) { 
		dbus_connection_unref(dbus.session.connection);
		mediaplayer_deregister_all();
		return -1;
	}
	
	/*init alsa*/
	snd_mixer_selem_id_alloca(&alsa.sid);
	snd_mixer_selem_id_set_index(alsa.sid, alsa.mix_index);
	snd_mixer_selem_id_set_name(alsa.sid, alsa.mix_name);
	
	if((snd_mixer_open(&alsa.handle, 0))<0)
		return -1;
	if((snd_mixer_attach(alsa.handle, alsa.card))<0) {
		snd_mixer_close(alsa.handle);
		return -1;
	}
	if((snd_mixer_selem_register(alsa.handle, NULL, NULL))<0) {
		snd_mixer_close(alsa.handle);
		return -1;
	}
	if(snd_mixer_load(alsa.handle)<0) {
		snd_mixer_close(alsa.handle);
		return -1;
	}
	if(!(alsa.elem=snd_mixer_find_selem(alsa.handle, alsa.sid))) {
		snd_mixer_close(alsa.handle);
		return -1;
	}
	
	snd_mixer_selem_get_playback_volume_range (alsa.elem, &alsa.minv, &alsa.maxv);
	return 0;
}
Example #18
0
static GVariant *
dconf_dbus_to_gv (DBusMessageIter  *iter,
                  GError          **error)
{
  gint arg_type;

  arg_type = dbus_message_iter_get_arg_type (iter);

  switch (dbus_message_iter_get_arg_type (iter))
    {
     case DBUS_TYPE_BOOLEAN:
      {
        dbus_bool_t value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_boolean (value);
      }

     case DBUS_TYPE_BYTE:
      {
        guchar value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_byte (value);
      }

     case DBUS_TYPE_INT16:
      {
        gint16 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_int16 (value);
      }

     case DBUS_TYPE_UINT16:
      {
        guint16 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_uint16 (value);
      }

     case DBUS_TYPE_INT32:
      {
        gint32 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_int32 (value);
      }

     case DBUS_TYPE_UINT32:
      {
        guint32 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_uint32 (value);
      }

     case DBUS_TYPE_INT64:
      {
        gint64 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_int64 (value);
      }

     case DBUS_TYPE_UINT64:
      {
        guint64 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_uint64 (value);
      }

     case DBUS_TYPE_DOUBLE:
      {
        gdouble value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_double (value);
      }

     case DBUS_TYPE_STRING:
      {
       const gchar *value;
       dbus_message_iter_get_basic (iter, &value);
       return g_variant_new_string (value);
      }

     case DBUS_TYPE_OBJECT_PATH:
      {
       const gchar *value;
       dbus_message_iter_get_basic (iter, &value);
       return g_variant_new_object_path (value);
      }

     case DBUS_TYPE_SIGNATURE:
      {
       const gchar *value;
       dbus_message_iter_get_basic (iter, &value);
       return g_variant_new_signature (value);
      }

     case DBUS_TYPE_VARIANT:
       {
        GVariantBuilder *builder;
        GVariantClass class;
        DBusMessageIter sub;
        char *type;
        GVariant *val;

        dbus_message_iter_recurse (iter, &sub);
        class = dbus_message_iter_get_arg_type (iter);
        type = dbus_message_iter_get_signature (&sub);
        builder = g_variant_builder_new (G_VARIANT_TYPE_VARIANT);
        dbus_free (type);

        while (dbus_message_iter_get_arg_type (&sub))
          {
            val = dconf_dbus_to_gv (&sub, error);
            if (val == NULL)
              {
                g_variant_builder_cancel (builder);
                goto fail;
              }
            g_variant_builder_add_value (builder, val);
            dbus_message_iter_next (&sub);
          }

        return g_variant_builder_end (builder);
       }

     case DBUS_TYPE_ARRAY:
     case DBUS_TYPE_STRUCT:
     case DBUS_TYPE_DICT_ENTRY:
      {
        GVariantBuilder *builder;
        GVariantClass class;
        DBusMessageIter sub;
        char *type;
        GVariant *val;

        dbus_message_iter_recurse (iter, &sub);
        class = dbus_message_iter_get_arg_type (iter);
        type = dbus_message_iter_get_signature (iter);
        builder = g_variant_builder_new (G_VARIANT_TYPE (type));
        dbus_free (type);

        while (dbus_message_iter_get_arg_type (&sub))
          {
            val = dconf_dbus_to_gv (&sub, error);
            if (val == NULL)
              {
                g_variant_builder_cancel (builder);
                goto fail;
              }
            g_variant_builder_add_value (builder, val);
            dbus_message_iter_next (&sub);
          }

        return g_variant_builder_end (builder);
      }

     default:
       g_set_error (error,
                    G_DBUS_ERROR,
                    G_DBUS_ERROR_CONVERSION_FAILED,
                    _("Error serializing D-Bus message to GVariant. Unsupported arg type `%c' (%d)"),
                    arg_type,
                    arg_type);
      goto fail;
    }

  g_assert_not_reached ();

 fail:
  return NULL;
}
static DBusMessage*
service_method_create_collection (GkdSecretService *self, DBusMessage *message)
{
	GckBuilder builder = GCK_BUILDER_INIT;
	DBusMessageIter iter, array;
	GckAttributes *attrs;
	GkdSecretCreate *create;
	DBusMessage *reply;
	const gchar *path;
	const gchar *alias;
	const char *caller;
	const gchar *coll;

	/* Parse the incoming message */
	if (!dbus_message_has_signature (message, "a{sv}s"))
		return NULL;
	if (!dbus_message_iter_init (message, &iter))
		g_return_val_if_reached (NULL);
	dbus_message_iter_recurse (&iter, &array);
	if (!gkd_secret_property_parse_all (&array, SECRET_COLLECTION_INTERFACE, &builder)) {
		gck_builder_clear (&builder);
		return dbus_message_new_error_printf (message, DBUS_ERROR_INVALID_ARGS,
		                                      "Invalid properties");
	}
	if (!dbus_message_iter_next (&iter))
		g_return_val_if_reached (NULL);
	dbus_message_iter_get_basic (&iter, &alias);

	/* Empty alias is no alias */
	if (alias) {
		if (!alias[0]) {
			alias = NULL;
		} else if (!g_str_equal (alias, "default")) {
			gck_builder_clear (&builder);
			return dbus_message_new_error (message, DBUS_ERROR_NOT_SUPPORTED,
			                               "Only the 'default' alias is supported");
		}
	}

	gck_builder_add_boolean (&builder, CKA_TOKEN, TRUE);
	attrs = gck_attributes_ref_sink (gck_builder_end (&builder));

	/* Create the prompt object, for the password */
	caller = dbus_message_get_sender (message);
	create = gkd_secret_create_new (self, caller, attrs, alias);
	gck_attributes_unref (attrs);

	path = gkd_secret_dispatch_get_object_path (GKD_SECRET_DISPATCH (create));
	gkd_secret_service_publish_dispatch (self, caller,
	                                     GKD_SECRET_DISPATCH (create));

	coll = "/";
	reply = dbus_message_new_method_return (message);
	dbus_message_append_args (reply,
	                          DBUS_TYPE_OBJECT_PATH, &coll,
	                          DBUS_TYPE_OBJECT_PATH, &path,
	                          DBUS_TYPE_INVALID);

	g_object_unref (create);
	return reply;
}
Example #20
0
static int get_all_properties_by_unit_path(DBusConnection *conn, const char *unit_path, int(*callback)(const char *name, const char *value, void *arg), void *cbarg)
{
	int ret = 1;
	DBusMessage *msg = NULL;
	DBusPendingCall *pending = NULL;

	msg = dbus_message_new_method_call(
		"org.freedesktop.systemd1",
		unit_path,
		"org.freedesktop.DBus.Properties",
		"GetAll"
	);
	if (msg == NULL) {
		dI("Failed to create dbus_message via dbus_message_new_method_call!\n");
		goto cleanup;
	}

	DBusMessageIter args, property_iter;

	const char *interface = "org.freedesktop.systemd1.Unit";

	dbus_message_iter_init_append(msg, &args);
	if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface)) {
		dI("Failed to append interface '%s' string parameter to dbus message!\n", interface);
		goto cleanup;
	}

	if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
		dI("Failed to send message via dbus!\n");
		goto cleanup;
	}
	if (pending == NULL) {
		dI("Invalid dbus pending call!\n");
		goto cleanup;
	}

	dbus_connection_flush(conn);
	dbus_message_unref(msg); msg = NULL;

	dbus_pending_call_block(pending);
	msg = dbus_pending_call_steal_reply(pending);
	if (msg == NULL) {
		dI("Failed to steal dbus pending call reply.\n");
		goto cleanup;
	}
	dbus_pending_call_unref(pending); pending = NULL;

	if (!dbus_message_iter_init(msg, &args)) {
		dI("Failed to initialize iterator over received dbus message.\n");
		goto cleanup;
	}

	if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY && dbus_message_iter_get_element_type(&args) != DBUS_TYPE_DICT_ENTRY) {
		dI("Expected array of dict_entry argument in reply. Instead received: %s.\n", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
		goto cleanup;
	}

	dbus_message_iter_recurse(&args, &property_iter);
	do {
		DBusMessageIter dict_entry, value_variant;
		dbus_message_iter_recurse(&property_iter, &dict_entry);

		if (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_STRING) {
			dI("Expected string as key in dict_entry. Instead received: %s.\n", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&dict_entry)));
			goto cleanup;
		}

		_DBusBasicValue value;
		dbus_message_iter_get_basic(&dict_entry, &value);
		char *property_name = oscap_strdup(value.str);

		if (dbus_message_iter_next(&dict_entry) == false) {
			dW("Expected another field in dict_entry.");
			oscap_free(property_name);
			goto cleanup;
		}

		if (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_VARIANT) {
			dI("Expected variant as value in dict_entry. Instead received: %s.\n", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&dict_entry)));
			oscap_free(property_name);
			goto cleanup;
		}

		dbus_message_iter_recurse(&dict_entry, &value_variant);

		int cbret = 0;
		const int arg_type = dbus_message_iter_get_arg_type(&value_variant);
		// DBUS_TYPE_ARRAY is a special case, we report each element as one value entry
		if (arg_type == DBUS_TYPE_ARRAY) {
			DBusMessageIter array;
			dbus_message_iter_recurse(&value_variant, &array);

			do {
				char *element = dbus_value_to_string(&array);
				if (element == NULL)
					continue;

				const int elementcbret = callback(property_name, element, cbarg);
				if (elementcbret > cbret)
					cbret = elementcbret;

				oscap_free(element);
			}
			while (dbus_message_iter_next(&array));
		}
		else {
			char *property_value = dbus_value_to_string(&value_variant);
			cbret = callback(property_name, property_value, cbarg);
			oscap_free(property_value);
		}

		oscap_free(property_name);
		if (cbret != 0) {
			goto cleanup;
		}
	}
	while (dbus_message_iter_next(&property_iter));

	dbus_message_unref(msg); msg = NULL;
	ret = 0;

cleanup:
	if (pending != NULL)
		dbus_pending_call_unref(pending);

	if (msg != NULL)
		dbus_message_unref(msg);

	return ret;
}
/**
 * wpas_dbus_global_add_interface - Request registration of a network interface
 * @message: Pointer to incoming dbus message
 * @global: %wpa_supplicant global data structure
 * Returns: The object path of the new interface object,
 *          or a dbus error message with more information
 *
 * Handler function for "addInterface" method call. Handles requests
 * by dbus clients to register a network interface that wpa_supplicant
 * will manage.
 */
DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
					     struct wpa_global *global)
{
	char *ifname = NULL;
	char *driver = NULL;
	char *driver_param = NULL;
	char *confname = NULL;
	char *bridge_ifname = NULL;
	DBusMessage *reply = NULL;
	DBusMessageIter iter;

	dbus_message_iter_init(message, &iter);

	/* First argument: interface name (DBUS_TYPE_STRING)
	 *    Required; must be non-zero length
	 */
	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		goto error;
	dbus_message_iter_get_basic(&iter, &ifname);
	if (!os_strlen(ifname))
		goto error;

	/* Second argument: dict of options */
	if (dbus_message_iter_next(&iter)) {
		DBusMessageIter iter_dict;
		struct wpa_dbus_dict_entry entry;

		if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
			goto error;
		while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
			if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
				goto error;
			if (!strcmp(entry.key, "driver") &&
			    (entry.type == DBUS_TYPE_STRING)) {
				driver = os_strdup(entry.str_value);
				wpa_dbus_dict_entry_clear(&entry);
				if (driver == NULL)
					goto error;
			} else if (!strcmp(entry.key, "driver-params") &&
				   (entry.type == DBUS_TYPE_STRING)) {
				driver_param = os_strdup(entry.str_value);
				wpa_dbus_dict_entry_clear(&entry);
				if (driver_param == NULL)
					goto error;
			} else if (!strcmp(entry.key, "config-file") &&
				   (entry.type == DBUS_TYPE_STRING)) {
				confname = os_strdup(entry.str_value);
				wpa_dbus_dict_entry_clear(&entry);
				if (confname == NULL)
					goto error;
			} else if (!strcmp(entry.key, "bridge-ifname") &&
				   (entry.type == DBUS_TYPE_STRING)) {
				bridge_ifname = os_strdup(entry.str_value);
				wpa_dbus_dict_entry_clear(&entry);
				if (bridge_ifname == NULL)
					goto error;
			} else {
				wpa_dbus_dict_entry_clear(&entry);
				goto error;
			}
		}
	}

	/*
	 * Try to get the wpa_supplicant record for this iface, return
	 * an error if we already control it.
	 */
	if (wpa_supplicant_get_iface(global, ifname) != NULL) {
		reply = dbus_message_new_error(message,
					       WPAS_ERROR_EXISTS_ERROR,
					       "wpa_supplicant already "
					       "controls this interface.");
	} else {
		struct wpa_supplicant *wpa_s;
		struct wpa_interface iface;
		os_memset(&iface, 0, sizeof(iface));
		iface.ifname = ifname;
		iface.driver = driver;
		iface.driver_param = driver_param;
		iface.confname = confname;
		iface.bridge_ifname = bridge_ifname;
		/* Otherwise, have wpa_supplicant attach to it. */
		if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
			const char *path = wpa_s->dbus_path;
			reply = dbus_message_new_method_return(message);
			dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
			                         &path, DBUS_TYPE_INVALID);
		} else {
			reply = dbus_message_new_error(message,
						       WPAS_ERROR_ADD_ERROR,
						       "wpa_supplicant "
						       "couldn't grab this "
						       "interface.");
		}
	}

out:
	os_free(driver);
	os_free(driver_param);
	os_free(confname);
	os_free(bridge_ifname);
	return reply;

error:
	reply = wpas_dbus_new_invalid_opts_error(message, NULL);
	goto out;
}
Example #22
0
/**
 * Listens for signals on the bus
 */
int sedbus_send_avc(DBusConnection* conn, char *avc) {

	DBusMessage* msg;
	DBusMessageIter args;
	DBusPendingCall* pending;
	char* reply = NULL;

	msg = dbus_message_new_method_call(BUSNAME, 
					   PATH, 
					   INTERFACE,
					   "avc"); // method name
	if (NULL == msg) { 
		fprintf(stderr, "Can't communicate with setroubleshootd\n");
		return -1;
	}
	// append arguments
	dbus_message_iter_init_append(msg, &args);
	if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &avc)) { 
		fprintf(stderr, "Out Of Memory!\n"); 
		return -1;
	}

	// send message and get a handle for a reply
	if ( ! dbus_connection_send_with_reply (conn, msg, &pending, -1)) { 
		// -1 is default timeout
		fprintf(stderr, "Out Of Memory!\n"); 
		return -1;
	}
	if (NULL == pending) { 
		fprintf(stderr, "Pending Call Null\n"); 
		return -1;
	}
	dbus_connection_flush(conn);
	
	// free message
	dbus_message_unref(msg);

	// block until we receive a reply
	dbus_pending_call_block(pending);
   
	// get the reply message
	msg = dbus_pending_call_steal_reply(pending);
	if (NULL == msg) {
		fprintf(stderr, "Reply Null\n"); 
		exit(1); 
	}
	// free the pending message handle
	dbus_pending_call_unref(pending);

	// read the parameters
	if (!dbus_message_iter_init(msg, &args))
		fprintf(stderr, "Message has no arguments!\n"); 
	else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) 
		fprintf(stderr, "Argument is not boolean!\n"); 
	else
		dbus_message_iter_get_basic(&args, &reply);

	printf("Send: %s\n", avc);
	printf("Got Reply: %s\n", reply);

	// free reply and close connection
	dbus_message_unref(msg);   

	return 0;
}
Example #23
0
/**
 * Listens for signals on the bus
 */
void receive()
{
	DBusMessage* msg;
	DBusMessageIter args;
	DBusConnection* conn;
	DBusError err;
	int ret;
	char* sigvalue;

	printf("Listening for signals\n");

	// initialise the errors
	dbus_error_init(&err);

	// connect to the bus and check for errors
	conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
	if (dbus_error_is_set(&err)) {
		fprintf(stderr, "Connection Error (%s)\n", err.message);
		dbus_error_free(&err);
	}
	if (NULL == conn) {
		exit(1);
	}

	// request our name on the bus and check for errors
	ret = dbus_bus_request_name(conn, "test.signal.sink", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
	if (dbus_error_is_set(&err)) {
		fprintf(stderr, "Name Error (%s)\n", err.message);
		dbus_error_free(&err);
	}
	if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
		exit(1);
	}

	// add a rule for which messages we want to see
	dbus_bus_add_match(conn, "type='signal',interface='test.signal.Type'", &err); // see signals from the given interface
	dbus_connection_flush(conn);
	if (dbus_error_is_set(&err)) {
		fprintf(stderr, "Match Error (%s)\n", err.message);
		exit(1);
	}
	printf("Match rule sent\n");

	// loop listening for signals being emmitted
	while (true) {

		// non blocking read of the next available message
		dbus_connection_read_write(conn, 0);
		msg = dbus_connection_pop_message(conn);

		// loop again if we haven't read a message
		if (NULL == msg) {
			sleep(1);
			continue;
		}

		// check if the message is a signal from the correct interface and with the correct name
		if (dbus_message_is_signal(msg, "test.signal.Type", "Test")) {

			// read the parameters
			if (!dbus_message_iter_init(msg, &args))
				fprintf(stderr, "Message Has No Parameters\n");
			else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
				fprintf(stderr, "Argument is not string!\n");
			else
				dbus_message_iter_get_basic(&args, &sigvalue);

			printf("Got Signal with value %s\n", sigvalue);
		}

		// free the message
		dbus_message_unref(msg);
	}
}
Example #24
0
static DBusHandlerResult
mafw_proxy_source_dispatch_message(DBusConnection *conn,
                                   DBusMessage *msg,
                                   MafwProxySource *self)
{
	MafwProxySourcePrivate *priv;
	gchar *domain_str;
	gint code;
	gchar *message;
	GError *error = NULL;

	g_assert(conn != NULL);
	g_assert(msg != NULL);

	priv = MAFW_PROXY_SOURCE_GET_PRIVATE(self);

	if (dbus_message_has_interface(msg, MAFW_EXTENSION_INTERFACE))
		return proxy_extension_dispatch(conn, msg, self);

	if (dbus_message_has_member(msg,
				    MAFW_PROXY_SOURCE_METHOD_BROWSE_RESULT)) {
		MafwProxySourceBrowseReq *new_req = NULL;
		guint browse_id;
		gint  remaining_count;
		guint index;
		gchar *object_id;
		GHashTable *metadata;
		DBusMessageIter imsg, iary, istr;

		g_return_val_if_fail(
			priv->browse_requests != NULL,
			DBUS_HANDLER_RESULT_NOT_YET_HANDLED);

		dbus_message_iter_init(msg, &imsg);
		dbus_message_iter_get_basic(&imsg, &browse_id);
		dbus_message_iter_next(&imsg);
		g_assert(dbus_message_iter_get_arg_type(&imsg) ==
				DBUS_TYPE_ARRAY);
		dbus_message_iter_recurse(&imsg, &iary);

		while (dbus_message_iter_get_arg_type(&iary) !=
						DBUS_TYPE_INVALID)
		{
			dbus_message_iter_recurse(&iary, &istr);
			g_assert(dbus_message_iter_get_arg_type(&istr)
				== DBUS_TYPE_INT32);
			dbus_message_iter_get_basic(&istr,
						&remaining_count);
			dbus_message_iter_next(&istr);
			dbus_message_iter_get_basic(&istr, &index);
			dbus_message_iter_next(&istr);
			dbus_message_iter_get_basic(&istr, &object_id);
			dbus_message_iter_next(&istr);
			mafw_dbus_message_parse_metadata(&istr,
							&metadata);
			dbus_message_iter_next(&istr);
			dbus_message_iter_get_basic(&istr, &domain_str);
			dbus_message_iter_next(&istr);
			dbus_message_iter_get_basic(&istr, &code);
			dbus_message_iter_next(&istr);
			dbus_message_iter_get_basic(&istr, &message);
			if (domain_str && domain_str[0])
				g_set_error(&error,
					g_quark_from_string(domain_str),
					    code, "%s", message);
			dbus_message_iter_next(&iary);

			if (!new_req)
			{
				new_req = g_hash_table_lookup(
					priv->browse_requests,
					GUINT_TO_POINTER(browse_id));
				if (new_req)
					new_req->emitting = TRUE;
			}
			if (new_req) {
				new_req->browse_cb(MAFW_SOURCE(self),
						   browse_id,
						   remaining_count,
						   index,
						   object_id[0] ?
							object_id :
							NULL,
						   metadata,
						   new_req->user_data,
						   error);
				if (remaining_count == 0 || new_req->stop_emitting)
				{
					g_hash_table_remove(
						priv->browse_requests,
						GUINT_TO_POINTER(browse_id));
					new_req = NULL;
					g_clear_error(&error);
					mafw_metadata_release(metadata);
					break;
				}
			}
			else
			{
				g_clear_error(&error);
				mafw_metadata_release(metadata);
				break;
			}

			g_clear_error(&error);
			mafw_metadata_release(metadata);
		}
		if (new_req) {
			new_req->emitting = FALSE;
		}
		return DBUS_HANDLER_RESULT_HANDLED;
	} else if (!dbus_message_has_path(msg,
                                          proxy_extension_return_path(self))) {
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
	} else if (mafw_dbus_is_signal(msg,
                                       MAFW_SOURCE_SIGNAL_METADATA_CHANGED)) {
		return handle_metadata_changed_signal(self, msg);
	} else if (mafw_dbus_is_signal(msg,
                                       MAFW_SOURCE_SIGNAL_CONTAINER_CHANGED)) {
		return handle_container_changed_signal(self, msg);
	} else if (mafw_dbus_is_signal(msg,
                                       MAFW_SOURCE_SIGNAL_UPDATING)) {
                return handle_updating_signal(self, msg);
        }
	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
Example #25
0
GList *
systemd_unit_listall(void)
{
    int lpc = 0;
    GList *units = NULL;
    DBusMessageIter args;
    DBusMessageIter unit;
    DBusMessageIter elem;
    DBusMessage *msg = NULL;
    DBusMessage *reply = NULL;
    const char *method = "ListUnits";
    DBusError error;

    if (systemd_init() == FALSE) {
        return NULL;
    }

/*
        "  <method name=\"ListUnits\">\n"                               \
        "   <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
        "  </method>\n"                                                 \
*/

    dbus_error_init(&error);
    msg = systemd_new_method(BUS_NAME".Manager", method);
    CRM_ASSERT(msg != NULL);

    reply = pcmk_dbus_send_recv(msg, systemd_proxy, &error);
    dbus_message_unref(msg);

    if(error.name) {
        crm_err("Call to %s failed: %s", method, error.name);
        return NULL;

    } else if (reply == NULL) {
        crm_err("Call to %s failed: Message has no reply", method);
        return NULL;

    } else if (!dbus_message_iter_init(reply, &args)) {
        crm_err("Call to %s failed: Message has no arguments", method);
        dbus_message_unref(reply);
        return NULL;
    }

    if(!pcmk_dbus_type_check(reply, &args, DBUS_TYPE_ARRAY, __FUNCTION__, __LINE__)) {
        crm_err("Call to %s failed: Message has invalid arguments", method);
        dbus_message_unref(reply);
        return NULL;
    }

    dbus_message_iter_recurse(&args, &unit);
    while (dbus_message_iter_get_arg_type (&unit) != DBUS_TYPE_INVALID) {
        DBusBasicValue value;

        if(!pcmk_dbus_type_check(reply, &unit, DBUS_TYPE_STRUCT, __FUNCTION__, __LINE__)) {
            continue;
        }

        dbus_message_iter_recurse(&unit, &elem);
        if(!pcmk_dbus_type_check(reply, &elem, DBUS_TYPE_STRING, __FUNCTION__, __LINE__)) {
            continue;
        }

        dbus_message_iter_get_basic(&elem, &value);
        crm_trace("Got: %s", value.str);
        if(value.str) {
            char *match = strstr(value.str, ".service");

            if (match) {
                lpc++;
                match[0] = 0;

                units = g_list_append(units, strdup(value.str));
            }
        }
        dbus_message_iter_next (&unit);
    }

    dbus_message_unref(reply);

    crm_trace("Found %d systemd services", lpc);
    return units;
}
Example #26
0
/* MafwSource::get_metadatas */
static void got_metadatas(DBusPendingCall *pendelum, RequestReplyInfo *info)
{
	GError *error;
	DBusMessage *msg;

	msg = dbus_pending_call_steal_reply(pendelum);
	if (!(error = mafw_dbus_is_error(msg, MAFW_SOURCE_ERROR))) {
		gchar *object_id;
		GHashTable *cur_metadata, *metadatas = NULL;
		DBusMessageIter imsg, iary, istr;
		GError *error = NULL;
		gchar *domain_str;
		gint code;
		gchar *message;

		dbus_message_iter_init(msg, &imsg);
		if (dbus_message_iter_get_arg_type(&imsg) == DBUS_TYPE_ARRAY)
		{
			dbus_message_iter_recurse(&imsg, &iary);
			metadatas =
                                g_hash_table_new_full(
                                        g_str_hash,
                                        g_str_equal,
                                        (GDestroyNotify)g_free,
                                        (GDestroyNotify)mafw_metadata_release);

			while (dbus_message_iter_get_arg_type(&iary) !=
						DBUS_TYPE_INVALID)
			{
				dbus_message_iter_recurse(&iary, &istr);
				dbus_message_iter_get_basic(&istr, &object_id);
				dbus_message_iter_next(&istr);
				mafw_dbus_message_parse_metadata(&istr,
							&cur_metadata);
				g_hash_table_insert(metadatas,
                                                    g_strdup(object_id),
                                                    cur_metadata);
				dbus_message_iter_next(&iary);
			}
			dbus_message_iter_next(&imsg);
		}
		dbus_message_iter_get_basic(&imsg, &domain_str);
		dbus_message_iter_next(&imsg);
		dbus_message_iter_get_basic(&imsg, &code);
		dbus_message_iter_next(&imsg);
		dbus_message_iter_get_basic(&imsg, &message);
		if (domain_str && domain_str[0])
				g_set_error(&error,
					g_quark_from_string(domain_str),
					    code, "%s", message);

		info->got_metadatas_cb(info->src,
				      metadatas,
				      info->cbdata, error);
		if (metadatas)
			g_hash_table_unref(metadatas);
	} else {
		info->got_metadatas_cb(info->src,
				      NULL,
				      info->cbdata, error);
		g_error_free(error);
	}
	dbus_message_unref(msg);
	dbus_pending_call_unref(pendelum);
}
void PulseAudioControl::update()
{
    openConnection();

    if (dbusConnection == NULL) {
        return;
    }

    DBusError error;
    dbus_error_init(&error);

    DBusMessage *reply = NULL;
    DBusMessage *msg = dbus_message_new_method_call(VOLUME_SERVICE, VOLUME_PATH, "org.freedesktop.DBus.Properties", "GetAll");
    if (msg != NULL) {
        dbus_message_append_args(msg, DBUS_TYPE_STRING, &VOLUME_INTERFACE, DBUS_TYPE_INVALID);

        reply = dbus_connection_send_with_reply_and_block(dbusConnection, msg, -1, &error);

        DBUS_ERR_CHECK (error);

        dbus_message_unref(msg);
    }

    int currentStep = -1, stepCount = -1;
    if (reply != NULL) {
        if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
            DBusMessageIter iter;
            dbus_message_iter_init(reply, &iter);
            // Recurse into the array [array of dicts]
            while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID) {
                DBusMessageIter dict_entry;
                dbus_message_iter_recurse(&iter, &dict_entry);

                // Recurse into the dict [ dict_entry (string, variant(int)) ]
                while (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_INVALID) {
                    DBusMessageIter in_dict;
                    // Recurse into the dict_entry [ string, variant(int) ]
                    dbus_message_iter_recurse(&dict_entry, &in_dict);
                    {
                        char *prop_name = NULL;
                        // Get the string value, "property name"
                        dbus_message_iter_get_basic(&in_dict, &prop_name);

                        dbus_message_iter_next(&in_dict);

                        DBusMessageIter variant;
                        // Recurse into the variant [ variant(int) ]
                        dbus_message_iter_recurse(&in_dict, &variant);

                        quint32 value;
                        // Get the variant value which is uint32
                        dbus_message_iter_get_basic(&variant, &value);

                        if (prop_name && strcmp(prop_name, "StepCount") == 0) {
                            stepCount = value;
                        } else if (prop_name && strcmp(prop_name, "CurrentStep") == 0) {
                            currentStep = value;
                        }
                    }

                    dbus_message_iter_next(&dict_entry);
                }
                dbus_message_iter_next(&iter);
            }
        }
        dbus_message_unref(reply);
    }

    if (currentStep != -1 && stepCount != -1) {
        setSteps(currentStep, stepCount);
    }
}
Example #28
0
static DBusHandlerResult locale_message_handler(
                DBusConnection *connection,
                DBusMessage *message,
                void *userdata) {

        DBusMessage *reply = NULL, *changed = NULL;
        DBusError error;
        int r;

        assert(connection);
        assert(message);

        dbus_error_init(&error);

        if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
                char **l = NULL, **i;
                dbus_bool_t interactive;
                DBusMessageIter iter;
                bool modified = false;
                bool passed[_PROP_MAX];
                int p;

                if (!dbus_message_iter_init(message, &iter))
                        return bus_send_error_reply(connection, message, NULL, -EINVAL);

                r = bus_parse_strv_iter(&iter, &l);
                if (r < 0) {
                        if (r == -ENOMEM)
                                goto oom;

                        return bus_send_error_reply(connection, message, NULL, r);
                }

                if (!dbus_message_iter_next(&iter) ||
                    dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)  {
                        strv_free(l);
                        return bus_send_error_reply(connection, message, NULL, -EINVAL);
                }

                dbus_message_iter_get_basic(&iter, &interactive);

                zero(passed);

                /* Check whether a variable changed and if so valid */
                STRV_FOREACH(i, l) {
                        bool valid = false;

                        for (p = 0; p < _PROP_MAX; p++) {
                                size_t k;

                                k = strlen(names[p]);
                                if (startswith(*i, names[p]) &&
                                    (*i)[k] == '=' &&
                                    string_is_safe((*i) + k + 1)) {
                                        valid = true;
                                        passed[p] = true;

                                        if (!streq_ptr(*i + k + 1, data[p]))
                                                modified = true;

                                        break;
                                }
                        }

                        if (!valid) {
                                strv_free(l);
                                return bus_send_error_reply(connection, message, NULL, -EINVAL);
                        }
                }
Example #29
0
static GConfSchema *
utils_get_schema (DBusMessageIter *main_iter)
{
  DBusMessageIter  struct_iter;
  gint32           type, list_type, car_type, cdr_type;
  const gchar     *locale, *short_desc, *long_desc, *owner;
  const gchar     *encoded;
  GConfSchema     *schema;
  GConfValue      *default_value;
  
  g_assert (dbus_message_iter_get_arg_type (main_iter) == DBUS_TYPE_STRUCT);
  
  dbus_message_iter_recurse (main_iter, &struct_iter);
  dbus_message_iter_get_basic (&struct_iter, &type);
  
  dbus_message_iter_next (&struct_iter);
  dbus_message_iter_get_basic (&struct_iter, &list_type);

  dbus_message_iter_next (&struct_iter);
  dbus_message_iter_get_basic (&struct_iter, &car_type);

  dbus_message_iter_next (&struct_iter);
  dbus_message_iter_get_basic (&struct_iter, &cdr_type);

  dbus_message_iter_next (&struct_iter);
  locale = utils_get_optional_string (&struct_iter);

  dbus_message_iter_next (&struct_iter);
  short_desc = utils_get_optional_string (&struct_iter);

  dbus_message_iter_next (&struct_iter);
  long_desc = utils_get_optional_string (&struct_iter);

  dbus_message_iter_next (&struct_iter);
  owner = utils_get_optional_string (&struct_iter);

  dbus_message_iter_next (&struct_iter);
  dbus_message_iter_get_basic (&struct_iter, &encoded);

  schema = gconf_schema_new ();
  
  gconf_schema_set_type (schema, type);
  gconf_schema_set_list_type (schema, list_type);
  gconf_schema_set_car_type (schema, car_type);
  gconf_schema_set_cdr_type (schema, cdr_type);

  if (locale)
    gconf_schema_set_locale (schema, locale);
  
  if (short_desc)
    gconf_schema_set_short_desc (schema, short_desc);
  
  if (long_desc)
    gconf_schema_set_long_desc (schema, long_desc);
  
  if (owner)
    gconf_schema_set_owner (schema, owner);
  
  if (*encoded != '\0')
    {
      default_value = gconf_value_decode (encoded);
      if (default_value)
	gconf_schema_set_default_value_nocopy (schema, default_value);
    }

  return schema;
}
Example #30
0
int __connman_session_create(DBusMessage *msg)
{
	const char *owner, *notify_path;
	char *session_path = NULL;
	DBusMessageIter iter, array;
	struct connman_session *session = NULL;
	struct creation_data *creation_data = NULL;
	bool user_allowed_bearers = false;
	bool user_connection_type = false;
	int err, i;
	char *str;

	owner = dbus_message_get_sender(msg);

	DBG("owner %s", owner);

	if (ecall_session && ecall_session->ecall) {
		/*
		 * If there is an emergency call already going on,
		 * ignore session creation attempt
		 */
		err = -EBUSY;
		goto err;
	}

	creation_data = g_try_new0(struct creation_data, 1);
	if (!creation_data) {
		err = -ENOMEM;
		goto err;
	}

	creation_data->pending = dbus_message_ref(msg);

	dbus_message_iter_init(msg, &iter);
	dbus_message_iter_recurse(&iter, &array);

	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry, value;
		const char *key, *val;

		dbus_message_iter_recurse(&array, &entry);
		dbus_message_iter_get_basic(&entry, &key);

		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &value);

		switch (dbus_message_iter_get_arg_type(&value)) {
		case DBUS_TYPE_ARRAY:
			if (g_str_equal(key, "AllowedBearers")) {
				err = parse_bearers(&value,
					&creation_data->allowed_bearers);
				if (err < 0)
					goto err;

				user_allowed_bearers = true;
			} else {
				err = -EINVAL;
				goto err;
			}
			break;
		case DBUS_TYPE_STRING:
			if (g_str_equal(key, "ConnectionType")) {
				dbus_message_iter_get_basic(&value, &val);
				creation_data->type =
					connman_session_parse_connection_type(val);

				user_connection_type = true;
			} else if (g_str_equal(key, "ContextIdentifier")) {
				dbus_message_iter_get_basic(&value, &val);
				creation_data->context_identifier = g_strdup(val);
			} else if (g_str_equal(key, "AllowedInterface")) {
				dbus_message_iter_get_basic(&value, &val);
				creation_data->allowed_interface = g_strdup(val);
			} else {
				err = -EINVAL;
				goto err;
			}
			break;
		case DBUS_TYPE_BOOLEAN:
			if (g_str_equal(key, "SourceIPRule")) {
				dbus_bool_t source_ip_rule;
				dbus_message_iter_get_basic(&value, &source_ip_rule);
				creation_data->source_ip_rule = source_ip_rule;
			} else {
				err = -EINVAL;
				goto err;
			}
			break;
		default:
			err = -EINVAL;
			goto err;
		}

		dbus_message_iter_next(&array);
	}

	/*
	 * If the user hasn't provided a configuration, we set
	 * the default configuration.
	 *
	 * For AllowedBearers this is '*', ...
	 */
	if (!user_allowed_bearers) {
		add_default_bearer_types(&creation_data->allowed_bearers);
		if (!creation_data->allowed_bearers) {
			err = -ENOMEM;
			goto err;
		}
	}

	/* ... and for ConnectionType it is 'any'. */
	if (!user_connection_type)
		creation_data->type = CONNMAN_SESSION_TYPE_ANY;

	dbus_message_iter_next(&iter);
	dbus_message_iter_get_basic(&iter, &notify_path);

	if (!notify_path) {
		err = -EINVAL;
		goto err;
	}

	str = g_strdup(owner);
	for (i = 0; str[i] != '\0'; i++)
		if (str[i] == ':' || str[i] == '.')
			str[i] = '_';
	session_path = g_strdup_printf("/sessions/%s%s", str, notify_path);
	g_free(str);

	if (!session_path) {
		err = -ENOMEM;
		goto err;
	}

	session = g_hash_table_lookup(session_hash, session_path);
	if (session) {
		g_free(session_path);
		session = NULL;
		err = -EEXIST;
		goto err;
	}

	session = g_try_new0(struct connman_session, 1);
	if (!session) {
		g_free(session_path);
		err = -ENOMEM;
		goto err;
	}

	creation_data->session = session;
	session->session_path = session_path;

	session->info = g_try_new0(struct session_info, 1);
	if (!session->info) {
		err = -ENOMEM;
		goto err;
	}

	session->info_last = g_try_new0(struct session_info, 1);
	if (!session->info_last) {
		err = -ENOMEM;
		goto err;
	}

	session->owner = g_strdup(owner);
	session->notify_path = g_strdup(notify_path);
	session->notify_watch =
		g_dbus_add_disconnect_watch(connection, session->owner,
					owner_disconnect, session, NULL);

	err = create_policy_config(session, session_policy_config_cb,
					creation_data);
	if (err < 0 && err != -EINPROGRESS)
		return err;

	return -EINPROGRESS;

err:
	connman_error("Failed to create session");

	free_session(session);

	cleanup_creation_data(creation_data);
	return err;
}