~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to serial/storage.c

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-10-07 12:10:29 UTC
  • Revision ID: james.westby@ubuntu.com-20081007121029-4gup4fmmh2vfo5nh
Tags: upstream-4.12
ImportĀ upstreamĀ versionĀ 4.12

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) 2004-2008  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 <errno.h>
 
29
#include <stdlib.h>
 
30
#include <termios.h>
 
31
#include <unistd.h>
 
32
#include <sys/stat.h>
 
33
#include <sys/param.h>
 
34
 
 
35
#include <bluetooth/bluetooth.h>
 
36
#include <bluetooth/sdp.h>
 
37
#include <bluetooth/sdp_lib.h>
 
38
 
 
39
#include <glib.h>
 
40
 
 
41
#include "logging.h"
 
42
#include "textfile.h"
 
43
 
 
44
#include "storage.h"
 
45
 
 
46
int port_delete(bdaddr_t *src, bdaddr_t *dst, int16_t id)
 
47
{
 
48
        char filename[PATH_MAX + 1];
 
49
        char src_addr[18], dst_addr[18];
 
50
        char key[32];
 
51
 
 
52
        ba2str(src, src_addr);
 
53
        ba2str(dst, dst_addr);
 
54
 
 
55
        create_name(filename, PATH_MAX, STORAGEDIR, src_addr, "serial");
 
56
        snprintf(key, sizeof(key), "%s#%hd", dst_addr, id);
 
57
 
 
58
        return textfile_del(filename, key);
 
59
}
 
60
 
 
61
int port_store(bdaddr_t *src, bdaddr_t *dst, int16_t id,
 
62
                        uint8_t ch, const char *svcname)
 
63
{
 
64
        char filename[PATH_MAX + 1];
 
65
        char src_addr[18], dst_addr[18];
 
66
        char key[32];
 
67
        char *value;
 
68
        int size, err;
 
69
 
 
70
        if (!svcname)
 
71
                svcname = "Bluetooth RFCOMM port";
 
72
 
 
73
        ba2str(src, src_addr);
 
74
        ba2str(dst, dst_addr);
 
75
 
 
76
        create_name(filename, PATH_MAX, STORAGEDIR, src_addr, "serial");
 
77
        create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
78
 
 
79
        size = strlen(svcname) + 5;
 
80
        value = g_malloc0(size);
 
81
 
 
82
        snprintf(key, 32, "%s#%hd", dst_addr, id);
 
83
        snprintf(value, size, "%d:%s", ch, svcname);
 
84
 
 
85
        err = textfile_put(filename, key, value);
 
86
        g_free(value);
 
87
 
 
88
        return err;
 
89
}
 
90
 
 
91
int proxy_delete(bdaddr_t *src, const char *tty)
 
92
{
 
93
        char filename[PATH_MAX + 1], src_addr[18];
 
94
 
 
95
        ba2str(src, src_addr);
 
96
 
 
97
        create_name(filename, PATH_MAX, STORAGEDIR, src_addr, "proxy");
 
98
 
 
99
        return textfile_del(filename, tty);
 
100
}
 
101
 
 
102
int proxy_store(bdaddr_t *src, const char *uuid, const char *tty,
 
103
                const char *name, uint8_t ch, int opts, struct termios *ti)
 
104
{
 
105
        char filename[PATH_MAX + 1], key[32], src_addr[18], *value;
 
106
        int i, pos, size, err;
 
107
        uint8_t *pti;
 
108
 
 
109
        ba2str(src, src_addr);
 
110
 
 
111
        create_name(filename, PATH_MAX, STORAGEDIR, src_addr, "proxy");
 
112
        create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
113
 
 
114
        if (!name)
 
115
                name = "Port Proxy Entity";
 
116
 
 
117
        size = MAX_LEN_UUID_STR + 16 + strlen(name) + sizeof(struct termios) * 2;
 
118
        value = g_malloc0(size);
 
119
 
 
120
        snprintf(key, 32, "%s", tty);
 
121
 
 
122
        /* tty uuid 00 0x0000 name:termios */
 
123
        pos = snprintf(value, size, "%s %d 0x%04X %s:", uuid, ch, opts, name);
 
124
 
 
125
        if (!ti)
 
126
                goto done;
 
127
 
 
128
        for (i = 0, pti = (uint8_t *) ti; i < sizeof(struct termios); i++, pti++)
 
129
                sprintf(value + pos + (i * 2), "%2.2X", *pti);
 
130
 
 
131
done:
 
132
        err = textfile_put(filename, key, value);
 
133
        g_free(value);
 
134
 
 
135
        return err;
 
136
}
 
137
 
 
138
int read_device_name(bdaddr_t *src, bdaddr_t *dst, char **name)
 
139
{
 
140
        char filename[PATH_MAX + 1], *str;
 
141
        char src_addr[18], dst_addr[18];
 
142
        int len;
 
143
 
 
144
        ba2str(src, src_addr);
 
145
        ba2str(dst, dst_addr);
 
146
 
 
147
        create_name(filename, PATH_MAX, STORAGEDIR, src_addr, "names");
 
148
 
 
149
        str = textfile_get(filename, dst_addr);
 
150
        if (!str)
 
151
                return -ENOENT;
 
152
 
 
153
        len = strlen(str);
 
154
 
 
155
        /* Max remote device name */
 
156
        if (len < 248) {
 
157
                *name = str;
 
158
                return 0;
 
159
        }
 
160
 
 
161
        *name = g_try_malloc0(248);
 
162
        if (!*name)
 
163
                return -ENOMEM;
 
164
 
 
165
        snprintf(*name, 248, "%s", str);
 
166
 
 
167
        free(str);
 
168
 
 
169
        return 0;
 
170
}