Set the permission of CCC descriptior properly
[platform/upstream/bluez.git] / profile.h
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  */
19
20 #ifndef __TIZEN_PROFILE_H__
21 #define __TIZEN_PROFILE_H__
22
23 #include <stdlib.h>
24
25 #include <iniparser.h>
26 #include <libxml/xmlmemory.h>
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29
30 #define MODEL_CONFIG_FILE "/etc/config/model-config.xml"
31 #define TYPE_FIELD "string"
32 #define FEATURE_TAG "platform"
33 #define MODEL_CONFIG_TAG "model-config"
34
35 typedef enum {
36         TIZEN_PROFILE_UNKNOWN = 0,
37         TIZEN_PROFILE_MOBILE = 0x1,
38         TIZEN_PROFILE_WEARABLE = 0x2,
39         TIZEN_PROFILE_TV = 0x4,
40         TIZEN_PROFILE_IVI = 0x8,
41         TIZEN_PROFILE_IOT = 0x10,
42         TIZEN_PROFILE_COMMON = 0x20,
43 } tizen_profile_t;
44
45 typedef enum {
46         TIZEN_MODEL_UNKNOWN = 0,
47         TIZEN_MODEL_COMMON = 0x1,
48         TIZEN_MODEL_TM1 = 0x2,
49         TIZEN_MODEL_TM2 = 0x4,
50         TIZEN_MODEL_TW1 = 0x8,
51         TIZEN_MODEL_TW2 = 0x10,
52         TIZEN_MODEL_TW3 = 0x20,
53         TIZEN_MODEL_RPI3 = 0x40,
54 } tizen_model_t;
55
56 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
57 static tizen_model_t model = TIZEN_MODEL_UNKNOWN;
58
59 static inline int __get_profile_from_model_config_xml(const char *field, char **value)
60 {
61         char *node_name = NULL;
62         char *node_value = NULL;
63         xmlNode *cur_node = NULL;
64         xmlNodePtr cur_ptr = NULL;
65         xmlNodePtr model_ptr = NULL;
66         xmlDocPtr xml_doc = NULL;
67
68         xml_doc = xmlParseFile(MODEL_CONFIG_FILE);
69         if (xml_doc == NULL)
70                 return -1;
71
72         cur_ptr = xmlDocGetRootElement(xml_doc);
73         if (cur_ptr == NULL) {
74                 xmlFreeDoc(xml_doc);
75                 return -1;
76         }
77
78         for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
79                 if (!xmlStrcmp(cur_ptr->name, (const xmlChar*)MODEL_CONFIG_TAG))
80                         break;
81         }
82
83         if (cur_ptr == NULL) {
84                 xmlFreeDoc(xml_doc);
85                 return -1;
86         }
87
88         cur_ptr = cur_ptr->xmlChildrenNode;
89         for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
90                 if (!xmlStrcmp(cur_node->name, (const xmlChar*)FEATURE_TAG)) {
91                         model_ptr = cur_node;
92                         break;
93                 }
94         }
95
96         if (model_ptr == NULL) {
97                 xmlFreeDoc(xml_doc);
98                 return -1;
99         }
100
101         if (model_ptr) {
102                 cur_ptr = model_ptr->xmlChildrenNode;
103
104                 for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
105                         if (cur_node->type == XML_ELEMENT_NODE) {
106                                 node_name = (char *)xmlGetProp(cur_node, (const xmlChar*)"name");
107
108                                 if (!strncmp(node_name, field, strlen(node_name))) {
109                                         node_value = (char *)xmlNodeListGetString(xml_doc, cur_node->xmlChildrenNode, 1);
110                                         if (node_value) {
111                                                 *value = strdup(node_value);
112                                                 free(node_name);
113                                                 free(node_value);
114                                                 break;
115                                         }
116                                 }
117                                 free(node_name);
118                         }
119                 }
120         }
121
122         xmlFreeDoc(xml_doc);
123         return 0;
124 }
125
126 static inline tizen_profile_t _get_tizen_profile(void)
127 {
128         char *profile_name = NULL;
129
130         if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
131                 return profile;
132
133         if (__get_profile_from_model_config_xml("tizen.org/feature/profile",
134                                                                         &profile_name) < 0) {
135                 profile = TIZEN_PROFILE_MOBILE;
136                 return profile;
137         }
138
139         if (profile_name == NULL) {
140                 profile = TIZEN_PROFILE_MOBILE;
141                 return profile;
142         }
143
144         switch (*profile_name) {
145                 case 'm':
146                 case 'M':
147                         profile = TIZEN_PROFILE_MOBILE;
148                         break;
149                 case 'w':
150                 case 'W':
151                         profile = TIZEN_PROFILE_WEARABLE;
152                         break;
153                 case 't':
154                 case 'T':
155                         profile = TIZEN_PROFILE_TV;
156                         break;
157                 case 'i':
158                 case 'I':
159                         if (!strncasecmp(profile_name, "ivi", 3))
160                                 profile = TIZEN_PROFILE_IVI;
161                         else if (!strncasecmp(profile_name, "iot", 3))
162                                 profile = TIZEN_PROFILE_IOT;
163                         else
164                                 profile = TIZEN_PROFILE_COMMON;
165                         break;
166                 default: /* common or unknown ==> ALL ARE COMMON */
167                         profile = TIZEN_PROFILE_COMMON;
168         }
169         free(profile_name);
170
171         return profile;
172 }
173
174 static inline tizen_model_t _get_tizen_model(void)
175 {
176         char *model_name = NULL;
177
178         if (__builtin_expect(model != TIZEN_MODEL_UNKNOWN, 1))
179                 return model;
180
181         if (__get_profile_from_model_config_xml("tizen.org/system/model_name",
182                                                                         &model_name) < 0) {
183                 model = TIZEN_MODEL_COMMON;
184                 return model;
185         }
186
187         if (model_name == NULL) {
188                 model = TIZEN_MODEL_COMMON;
189                 return model;
190         }
191
192         if (!strcasecmp(model_name, "TM1"))
193                 model = TIZEN_MODEL_TM1;
194         else if (!strcasecmp(model_name, "TM2"))
195                 model = TIZEN_MODEL_TM2;
196         else if (!strcasecmp(model_name, "TW1"))
197                 model = TIZEN_MODEL_TW1;
198         else if (!strcasecmp(model_name, "TW2"))
199                 model = TIZEN_MODEL_TW2;
200         else if (!strcasecmp(model_name, "TW3"))
201                 model = TIZEN_MODEL_TW3;
202         else if (!strcasecmp(model_name, "rpi3"))
203                 model = TIZEN_MODEL_RPI3;
204         else
205                 model = TIZEN_MODEL_COMMON;
206
207         free(model_name);
208
209         return model;
210 }
211
212 #define TIZEN_FEATURE_BLUEZ_BRCM_CHIP ((_get_tizen_profile()) == TIZEN_PROFILE_IVI)
213 #define TIZEN_FEATURE_BLUEZ_SMS_ONLY ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
214 #define TIZEN_FEATURE_BLUEZ_BRCM_QOS ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
215 #define TIZEN_FEATURE_BLUEZ_ROLE_CHANGE ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
216 #define TIZEN_FEATURE_BLUEZ_CONFIRM_ONLY ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
217 #define TIZEN_FEATURE_BLUEZ_SPRD_QOS ((_get_tizen_model()) == TIZEN_MODEL_TM1)
218 #define TIZEN_FEATURE_BLUEZ_SPRD_PAGE_SCAN ((_get_tizen_model()) == TIZEN_MODEL_TM1)
219 #define TIZEN_FEATURE_BLUEZ_SPEAKER_REFERENCE ((_get_tizen_model()) == TIZEN_MODEL_RPI3 && (_get_tizen_profile()) == TIZEN_PROFILE_COMMON)
220
221 #endif /* __TIZEN_PROFILE_H__ */
222