tizen 2.3.1 release
[framework/connectivity/bluez.git] / android / cutils / properties.h
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2013  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library 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 GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30
31 #define PROPERTY_VALUE_MAX 32
32 #define PROPERTY_KEY_MAX 32
33
34 #define BLUETOOTH_MODE_PROPERTY_NAME "persist.sys.bluetooth.mode"
35 #define BLUETOOTH_MODE_PROPERTY_HANDSFREE "persist.sys.bluetooth.handsfree"
36
37 static inline int property_get(const char *key, char *value,
38                                                 const char *default_value)
39 {
40         const char *prop = NULL;
41
42         if (!strcmp(key, BLUETOOTH_MODE_PROPERTY_NAME))
43                 prop = getenv("BLUETOOTH_MODE");
44
45         if (!strcmp(key, BLUETOOTH_MODE_PROPERTY_HANDSFREE))
46                 prop = getenv("BLUETOOTH_HANDSFREE_MODE");
47
48         if (!prop)
49                 prop = default_value;
50
51         if (prop) {
52                 strncpy(value, prop, PROPERTY_VALUE_MAX);
53
54                 value[PROPERTY_VALUE_MAX - 1] = '\0';
55
56                 return strlen(value);
57         }
58
59         return 0;
60 }
61
62 /* property_set: returns 0 on success, < 0 on failure
63 */
64 static inline int property_set(const char *key, const char *value)
65 {
66         static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
67
68         struct sockaddr_un addr;
69         char msg[256];
70         int fd, len;
71
72         fd = socket(PF_LOCAL, SOCK_DGRAM, 0);
73         if (fd < 0)
74                 return -1;
75
76         memset(&addr, 0, sizeof(addr));
77         addr.sun_family = AF_UNIX;
78         memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
79
80         if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
81                 close(fd);
82                 return 0;
83         }
84
85         len = snprintf(msg, sizeof(msg), "%s=%s", key, value);
86
87         if (send(fd, msg, len + 1, 0) < 0) {
88                 close(fd);
89                 return -1;
90         }
91
92         close(fd);
93
94         return 0;
95 }