obexd-test package is needed for testing
[profile/ivi/obexd.git] / src / service.c
1 /*
2  *
3  *  OBEX Server
4  *
5  *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33
34 #include <glib.h>
35
36 #include "obex.h"
37 #include "service.h"
38 #include "log.h"
39
40 static GSList *drivers = NULL;
41
42 struct obex_service_driver *obex_service_driver_find(GSList *drivers,
43                         const uint8_t *target, unsigned int target_size,
44                         const uint8_t *who, unsigned int who_size)
45 {
46         GSList *l;
47
48         for (l = drivers; l; l = l->next) {
49                 struct obex_service_driver *driver = l->data;
50
51                 /* who is optional, so only check for it if not NULL */
52                 if (who != NULL && memncmp0(who, who_size, driver->who,
53                                                         driver->who_size))
54                         continue;
55
56                 if (memncmp0(target, target_size, driver->target,
57                                                 driver->target_size) == 0)
58                         return driver;
59         }
60
61         return NULL;
62 }
63
64 GSList *obex_service_driver_list(uint16_t services)
65 {
66         GSList *l;
67         GSList *list = NULL;
68
69         if (services == 0)
70                 return drivers;
71
72         for (l = drivers; l && services; l = l->next) {
73                 struct obex_service_driver *driver = l->data;
74
75                 if (driver->service & services) {
76                         list = g_slist_append(list, driver);
77                         services &= ~driver->service;
78                 }
79         }
80
81         return list;
82 }
83
84 int obex_service_driver_register(struct obex_service_driver *driver)
85 {
86         if (!driver) {
87                 error("Invalid driver");
88                 return -EINVAL;
89         }
90
91         if (obex_service_driver_list(driver->service)) {
92                 error("Permission denied: service %s already registered",
93                         driver->name);
94                 return -EPERM;
95         }
96
97         DBG("driver %p service %s registered", driver, driver->name);
98
99         /* Drivers that support who has priority */
100         if (driver->who)
101                 drivers = g_slist_prepend(drivers, driver);
102         else
103                 drivers = g_slist_append(drivers, driver);
104
105         return 0;
106 }
107
108 void obex_service_driver_unregister(struct obex_service_driver *driver)
109 {
110         if (!g_slist_find(drivers, driver)) {
111                 error("Unable to unregister: No such driver %p", driver);
112                 return;
113         }
114
115         DBG("driver %p service %s unregistered", driver, driver->name);
116
117         drivers = g_slist_remove(drivers, driver);
118 }