Blame obexd/client/map-event.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  OBEX
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2013  BMW Car IT GmbH. All rights reserved.
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#include <glib.h>
Packit 34410b
#include <string.h>
Packit 34410b
#include <stdbool.h>
Packit 34410b
#include <inttypes.h>
Packit 34410b
Packit 34410b
#include "gdbus/gdbus.h"
Packit 34410b
Packit 34410b
#include "obexd/src/log.h"
Packit 34410b
#include "map-event.h"
Packit 34410b
Packit 34410b
#include "transfer.h"
Packit 34410b
#include "session.h"
Packit 34410b
Packit 34410b
static GSList *handlers;
Packit 34410b
Packit 34410b
struct mns_handler {
Packit 34410b
	int mas_id;
Packit 34410b
	struct obc_session *session;
Packit 34410b
	map_event_cb cb;
Packit 34410b
	void *user_data;
Packit 34410b
};
Packit 34410b
Packit 34410b
static struct mns_handler *find_handler(int mas_id, const char *device)
Packit 34410b
{
Packit 34410b
	GSList *list;
Packit 34410b
Packit 34410b
	for (list = handlers; list; list = list->next) {
Packit 34410b
		struct mns_handler *handler = list->data;
Packit 34410b
Packit 34410b
		if (mas_id != handler->mas_id)
Packit 34410b
			continue;
Packit 34410b
Packit 34410b
		if (g_str_equal(device,
Packit 34410b
				obc_session_get_destination(handler->session)))
Packit 34410b
			return handler;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return NULL;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool map_register_event_handler(struct obc_session *session,
Packit 34410b
					int mas_id, map_event_cb cb,
Packit 34410b
					void *user_data)
Packit 34410b
{
Packit 34410b
	struct mns_handler *handler;
Packit 34410b
Packit 34410b
	handler = find_handler(mas_id, obc_session_get_destination(session));
Packit 34410b
	if (handler != NULL)
Packit 34410b
		return FALSE;
Packit 34410b
Packit 34410b
	handler = g_new0(struct mns_handler, 1);
Packit 34410b
	handler->mas_id = mas_id;
Packit 34410b
	handler->session = session;
Packit 34410b
	handler->cb = cb;
Packit 34410b
	handler->user_data = user_data;
Packit 34410b
Packit 34410b
	handlers = g_slist_prepend(handlers, handler);
Packit 34410b
	DBG("Handler %p for %s:%d registered", handler,
Packit 34410b
			obc_session_get_destination(session), mas_id);
Packit 34410b
Packit 34410b
	return TRUE;
Packit 34410b
}
Packit 34410b
Packit 34410b
void map_unregister_event_handler(struct obc_session *session, int mas_id)
Packit 34410b
{
Packit 34410b
	struct mns_handler *handler;
Packit 34410b
Packit 34410b
	handler = find_handler(mas_id, obc_session_get_destination(session));
Packit 34410b
	if (handler == NULL)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	handlers = g_slist_remove(handlers, handler);
Packit 34410b
	DBG("Handler %p for %s:%d unregistered", handler,
Packit 34410b
			obc_session_get_destination(session), mas_id);
Packit 34410b
	g_free(handler);
Packit 34410b
}
Packit 34410b
Packit 34410b
void map_dispatch_event(int mas_id, const char *device,
Packit 34410b
						struct map_event *event)
Packit 34410b
{
Packit 34410b
	struct mns_handler *handler;
Packit 34410b
Packit 34410b
	handler = find_handler(mas_id, device);
Packit 34410b
	if (handler)
Packit 34410b
		handler->cb(event, handler->user_data);
Packit 34410b
}