summaryrefslogtreecommitdiff
path: root/obexd/client/map-event.c
blob: bbe351954dac0f17375a245b8fcc083eff78639a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 *
 *  OBEX
 *
 *  Copyright (C) 2013  BMW Car IT GmbH. All rights reserved.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <string.h>
#include <stdbool.h>

#include "log.h"
#include "map-event.h"

#include <gdbus/gdbus.h>
#include "transfer.h"
#include "session.h"

static GSList *handlers;

struct mns_handler {
	int mas_id;
	struct obc_session *session;
	map_event_cb cb;
	void *user_data;
};

static struct mns_handler *find_handler(int mas_id, const char *device)
{
	GSList *list;

	for (list = handlers; list; list = list->next) {
		struct mns_handler *handler = list->data;

		if (mas_id != handler->mas_id)
			continue;

		if (g_str_equal(device,
				obc_session_get_destination(handler->session)))
			return handler;
	}

	return NULL;
}

bool map_register_event_handler(struct obc_session *session,
					int mas_id, map_event_cb cb,
					void *user_data)
{
	struct mns_handler *handler;

	handler = find_handler(mas_id, obc_session_get_destination(session));
	if (handler != NULL)
		return FALSE;

	handler = g_new0(struct mns_handler, 1);
	handler->mas_id = mas_id;
	handler->session = session;
	handler->cb = cb;
	handler->user_data = user_data;

	handlers = g_slist_prepend(handlers, handler);
	DBG("Handler %p for %s:%d registered", handler,
			obc_session_get_destination(session), mas_id);

	return TRUE;
}

void map_unregister_event_handler(struct obc_session *session, int mas_id)
{
	struct mns_handler *handler;

	handler = find_handler(mas_id, obc_session_get_destination(session));
	if (handler == NULL)
		return;

	handlers = g_slist_remove(handlers, handler);
	DBG("Handler %p for %s:%d unregistered", handler,
			obc_session_get_destination(session), mas_id);
	g_free(handler);
}

void map_dispatch_event(int mas_id, const char *device,
						struct map_event *event)
{
	struct mns_handler *handler;

	handler = find_handler(mas_id, device);
	if (handler)
		handler->cb(event, handler->user_data);
}