Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / obexd / 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 static struct obex_service_driver *find_driver(uint16_t service)
85 {
86         GSList *l;
87
88         for (l = drivers; l; l = l->next) {
89                 struct obex_service_driver *driver = l->data;
90
91                 if (driver->service == service)
92                         return driver;
93         }
94
95         return NULL;
96 }
97
98 int obex_service_driver_register(struct obex_service_driver *driver)
99 {
100         if (!driver) {
101                 error("Invalid driver");
102                 return -EINVAL;
103         }
104
105         if (find_driver(driver->service)) {
106                 error("Permission denied: service %s already registered",
107                         driver->name);
108                 return -EPERM;
109         }
110
111         DBG("driver %p service %s registered", driver, driver->name);
112
113         /* Drivers that support who has priority */
114         if (driver->who)
115                 drivers = g_slist_prepend(drivers, driver);
116         else
117                 drivers = g_slist_append(drivers, driver);
118
119         return 0;
120 }
121
122 void obex_service_driver_unregister(struct obex_service_driver *driver)
123 {
124         if (!g_slist_find(drivers, driver)) {
125                 error("Unable to unregister: No such driver %p", driver);
126                 return;
127         }
128
129         DBG("driver %p service %s unregistered", driver, driver->name);
130
131         drivers = g_slist_remove(drivers, driver);
132 }