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

« back to all changes in this revision

Viewing changes to serial/storage.c

ImportĀ upstreamĀ versionĀ 4.81

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-2009  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
 
        unsigned int i;
107
 
        int pos, size, err;
108
 
        uint8_t *pti;
109
 
 
110
 
        ba2str(src, src_addr);
111
 
 
112
 
        create_name(filename, PATH_MAX, STORAGEDIR, src_addr, "proxy");
113
 
        create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
114
 
 
115
 
        if (!name)
116
 
                name = "Port Proxy Entity";
117
 
 
118
 
        size = MAX_LEN_UUID_STR + 16 + strlen(name) + sizeof(struct termios) * 2;
119
 
        value = g_malloc0(size);
120
 
 
121
 
        snprintf(key, 32, "%s", tty);
122
 
 
123
 
        /* tty uuid 00 0x0000 name:termios */
124
 
        pos = snprintf(value, size, "%s %d 0x%04X %s:", uuid, ch, opts, name);
125
 
 
126
 
        if (!ti)
127
 
                goto done;
128
 
 
129
 
        for (i = 0, pti = (uint8_t *) ti; i < sizeof(struct termios); i++, pti++)
130
 
                sprintf(value + pos + (i * 2), "%2.2X", *pti);
131
 
 
132
 
done:
133
 
        err = textfile_put(filename, key, value);
134
 
        g_free(value);
135
 
 
136
 
        return err;
137
 
}
138