~ubuntu-branches/ubuntu/wily/unity-control-center/wily-proposed

« back to all changes in this revision

Viewing changes to panels/bluetooth/gnome-bluetooth/wizard/pin.c

  • Committer: Package Import Robot
  • Author(s): CI Train Bot, Lars Uebernickel, Sebastien Bacher
  • Date: 2015-08-13 15:43:59 UTC
  • mfrom: (1.1.38)
  • Revision ID: package-import@ubuntu.com-20150813154359-1rnt5g1txidqiyxc
Tags: 15.04.0+15.10.20150813-0ubuntu1
[ Lars Uebernickel ]
* Some tweaks for the new bluez changes include libgnome-bluetooth lib
  in the deb refresh translatable sources list updated build-depends
* bluetooth: make compatible with bluez5

[ Sebastien Bacher ]
* Some tweaks for the new bluez changes include libgnome-bluetooth lib
  in the deb refresh translatable sources list updated build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2009  Bastien Nocera <hadess@hadess.net>
 
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 <stdlib.h>
 
29
#include <string.h>
 
30
#include <glib.h>
 
31
#include <bluetooth-enums.h>
 
32
#include <bluetooth-utils.h>
 
33
 
 
34
#include "pin.h"
 
35
 
 
36
#define PIN_CODE_DB "pin-code-database.xml"
 
37
#define MAX_DIGITS_PIN_PREFIX "max:"
 
38
 
 
39
#define TYPE_IS(x, r) {                         \
 
40
        if (g_str_equal(type, x)) return r;     \
 
41
}
 
42
 
 
43
static guint string_to_type(const char *type)
 
44
{
 
45
        TYPE_IS ("any", BLUETOOTH_TYPE_ANY);
 
46
        TYPE_IS ("mouse", BLUETOOTH_TYPE_MOUSE);
 
47
        TYPE_IS ("tablet", BLUETOOTH_TYPE_TABLET);
 
48
        TYPE_IS ("keyboard", BLUETOOTH_TYPE_KEYBOARD);
 
49
        TYPE_IS ("headset", BLUETOOTH_TYPE_HEADSET);
 
50
        TYPE_IS ("headphones", BLUETOOTH_TYPE_HEADPHONES);
 
51
        TYPE_IS ("audio", BLUETOOTH_TYPE_OTHER_AUDIO);
 
52
        TYPE_IS ("printer", BLUETOOTH_TYPE_PRINTER);
 
53
        TYPE_IS ("network", BLUETOOTH_TYPE_NETWORK);
 
54
 
 
55
        g_warning ("unhandled type '%s'", type);
 
56
        return BLUETOOTH_TYPE_ANY;
 
57
}
 
58
 
 
59
typedef struct {
 
60
        char *ret_pin;
 
61
        guint max_digits;
 
62
        guint type;
 
63
        const char *address;
 
64
        const char *name;
 
65
        gboolean confirm;
 
66
} PinParseData;
 
67
 
 
68
static void
 
69
pin_db_parse_start_tag (GMarkupParseContext *ctx,
 
70
                        const gchar         *element_name,
 
71
                        const gchar        **attr_names,
 
72
                        const gchar        **attr_values,
 
73
                        gpointer             data,
 
74
                        GError             **error)
 
75
{
 
76
        PinParseData *pdata = (PinParseData *) data;
 
77
 
 
78
        if (pdata->ret_pin != NULL || pdata->max_digits != 0)
 
79
                return;
 
80
        if (g_str_equal (element_name, "device") == FALSE)
 
81
                return;
 
82
 
 
83
        while (*attr_names && *attr_values) {
 
84
                if (g_str_equal (*attr_names, "type")) {
 
85
                        guint type;
 
86
 
 
87
                        type = string_to_type (*attr_values);
 
88
                        if (type != BLUETOOTH_TYPE_ANY && type != pdata->type)
 
89
                                return;
 
90
                } else if (g_str_equal (*attr_names, "oui")) {
 
91
                        if (g_str_has_prefix (pdata->address, *attr_values) == FALSE)
 
92
                                return;
 
93
                } else if (g_str_equal (*attr_names, "name")) {
 
94
                        if (*attr_values == NULL || pdata->name == NULL)
 
95
                                return;
 
96
                        if (strstr (pdata->name, *attr_values) == NULL)
 
97
                                return;
 
98
                        pdata->confirm = FALSE;
 
99
                } else if (g_str_equal (*attr_names, "pin")) {
 
100
                        if (g_str_has_prefix (*attr_values, MAX_DIGITS_PIN_PREFIX) != FALSE) {
 
101
                                pdata->max_digits = strtoul (*attr_values + strlen (MAX_DIGITS_PIN_PREFIX), NULL, 0);
 
102
                                g_assert (pdata->max_digits > 0 && pdata->max_digits < PIN_NUM_DIGITS);
 
103
                        } else {
 
104
                                pdata->ret_pin = g_strdup (*attr_values);
 
105
                        }
 
106
                        return;
 
107
                }
 
108
 
 
109
                ++attr_names;
 
110
                ++attr_values;
 
111
        }
 
112
}
 
113
 
 
114
char *
 
115
get_pincode_for_device (guint       type,
 
116
                        const char *address,
 
117
                        const char *name,
 
118
                        guint      *max_digits,
 
119
                        gboolean   *confirm)
 
120
{
 
121
        GMarkupParseContext *ctx;
 
122
        GMarkupParser parser = { pin_db_parse_start_tag, NULL, NULL, NULL, NULL };
 
123
        PinParseData data;
 
124
        char *buf;
 
125
        gsize buf_len;
 
126
        GError *err = NULL;
 
127
 
 
128
        g_return_val_if_fail (address != NULL, NULL);
 
129
 
 
130
        g_debug ("Getting pincode for device '%s' (type: %s address: %s)",
 
131
                 name ? name : "", address, bluetooth_type_to_string (type));
 
132
 
 
133
        /* Load the PIN database and split it in lines */
 
134
        if (!g_file_get_contents(PIN_CODE_DB, &buf, &buf_len, NULL)) {
 
135
                char *filename;
 
136
 
 
137
                filename = g_build_filename(PKGDATADIR, PIN_CODE_DB, NULL);
 
138
                if (!g_file_get_contents(filename, &buf, &buf_len, NULL)) {
 
139
                        g_warning("Could not load "PIN_CODE_DB);
 
140
                        g_free (filename);
 
141
                        return NULL;
 
142
                }
 
143
                g_free (filename);
 
144
        }
 
145
 
 
146
        data.ret_pin = NULL;
 
147
        data.max_digits = 0;
 
148
        data.type = type;
 
149
        data.address = address;
 
150
        data.name = name;
 
151
        data.confirm = TRUE;
 
152
 
 
153
        ctx = g_markup_parse_context_new (&parser, 0, &data, NULL);
 
154
 
 
155
        if (!g_markup_parse_context_parse (ctx, buf, buf_len, &err)) {
 
156
                g_warning ("Failed to parse '%s': %s\n", PIN_CODE_DB, err->message);
 
157
                g_error_free (err);
 
158
        }
 
159
 
 
160
        g_markup_parse_context_free (ctx);
 
161
        g_free (buf);
 
162
 
 
163
        if (max_digits != NULL)
 
164
                *max_digits = data.max_digits;
 
165
        if (confirm != NULL)
 
166
                *confirm = data.confirm;
 
167
 
 
168
        g_debug ("Got pin '%s' (max digits: %d) for device '%s' (type: %s address: %s)",
 
169
                 data.ret_pin, data.max_digits,
 
170
                 name ? name : "", bluetooth_type_to_string (type), address);
 
171
 
 
172
        return data.ret_pin;
 
173
}
 
174