summaryrefslogtreecommitdiff
path: root/profile.h
blob: a18a94f6196508b0c25ac6079db4a68276763427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef __TIZEN_PROFILE_H__
#define __TIZEN_PROFILE_H__

#include <stdlib.h>

#include <iniparser.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

#define MODEL_CONFIG_FILE "/etc/config/model-config.xml"
#define TYPE_FIELD "string"
#define FEATURE_TAG "platform"
#define MODEL_CONFIG_TAG "model-config"
#define CERTI_STACK_FILE "/var/lib/bluetooth/stack_test"
#define CERTI_PROFILE_FILE "/var/lib/bluetooth/profile_test"


typedef enum {
	TIZEN_PROFILE_UNKNOWN = 0,
	TIZEN_PROFILE_MOBILE = 0x1,
	TIZEN_PROFILE_WEARABLE = 0x2,
	TIZEN_PROFILE_TV = 0x4,
	TIZEN_PROFILE_IVI = 0x8,
	TIZEN_PROFILE_IOT = 0x10,
	TIZEN_PROFILE_COMMON = 0x20,
} tizen_profile_t;

typedef enum {
	TIZEN_MODEL_UNKNOWN = 0,
	TIZEN_MODEL_COMMON = 0x1,
	TIZEN_MODEL_TM1 = 0x2,
	TIZEN_MODEL_TM2 = 0x4,
	TIZEN_MODEL_TW1 = 0x8,
	TIZEN_MODEL_TW2 = 0x10,
	TIZEN_MODEL_TW3 = 0x20,
	TIZEN_MODEL_RPI3 = 0x40,
	TIZEN_MODEL_ROBOT = 0x80,
	TIZEN_MODEL_FHUB = 0x100,
} tizen_model_t;

typedef enum {
	TIZEN_CERTI_MODE_UNKNOWN = 0,
	TIZEN_CERTI_MODE_DISABLE = 0x1,
	TIZEN_CERTI_MODE_STACK = 0x2,
	TIZEN_CERTI_MODE_PROFILE = 0x4,
} tizen_certifcation_mode_t;

static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
static tizen_model_t model = TIZEN_MODEL_UNKNOWN;
static tizen_certifcation_mode_t certification_mode = TIZEN_CERTI_MODE_UNKNOWN;

static inline int __get_profile_from_model_config_xml(const char *field, char **value)
{
	char *node_name = NULL;
	char *node_value = NULL;
	xmlNode *cur_node = NULL;
	xmlNodePtr cur_ptr = NULL;
	xmlNodePtr model_ptr = NULL;
	xmlDocPtr xml_doc = NULL;

	xml_doc = xmlParseFile(MODEL_CONFIG_FILE);
	if (xml_doc == NULL)
		return -1;

	cur_ptr = xmlDocGetRootElement(xml_doc);
	if (cur_ptr == NULL) {
		xmlFreeDoc(xml_doc);
		return -1;
	}

	for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
		if (!xmlStrcmp(cur_ptr->name, (const xmlChar*)MODEL_CONFIG_TAG))
			break;
	}

	if (cur_ptr == NULL) {
		xmlFreeDoc(xml_doc);
		return -1;
	}

	cur_ptr = cur_ptr->xmlChildrenNode;
	for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
		if (!xmlStrcmp(cur_node->name, (const xmlChar*)FEATURE_TAG)) {
			model_ptr = cur_node;
			break;
		}
	}

	if (model_ptr == NULL) {
		xmlFreeDoc(xml_doc);
		return -1;
	}

	if (model_ptr) {
		cur_ptr = model_ptr->xmlChildrenNode;

		for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				node_name = (char *)xmlGetProp(cur_node, (const xmlChar*)"name");

				if (!strncmp(node_name, field, strlen(node_name))) {
					node_value = (char *)xmlNodeListGetString(xml_doc, cur_node->xmlChildrenNode, 1);
					if (node_value) {
						*value = strdup(node_value);
						xmlFree(node_name);
						xmlFree(node_value);
						break;
					}
				}
				xmlFree(node_name);
			}
		}
	}

	xmlFreeDoc(xml_doc);
	return 0;
}

static inline tizen_profile_t _get_tizen_profile(void)
{
	char *profile_name = NULL;

	if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
		return profile;

	if (__get_profile_from_model_config_xml("tizen.org/feature/profile",
									&profile_name) < 0) {
		profile = TIZEN_PROFILE_MOBILE;
		return profile;
	}

	if (profile_name == NULL) {
		profile = TIZEN_PROFILE_MOBILE;
		return profile;
	}

	switch (*profile_name) {
		case 'm':
		case 'M':
			profile = TIZEN_PROFILE_MOBILE;
			break;
		case 'w':
		case 'W':
			profile = TIZEN_PROFILE_WEARABLE;
			break;
		case 't':
		case 'T':
			profile = TIZEN_PROFILE_TV;
			break;
		case 'i':
		case 'I':
			if (!strncasecmp(profile_name, "ivi", 3))
				profile = TIZEN_PROFILE_IVI;
			else if (!strncasecmp(profile_name, "iot", 3))
				profile = TIZEN_PROFILE_IOT;
			else
				profile = TIZEN_PROFILE_COMMON;
			break;
		default: /* common or unknown ==> ALL ARE COMMON */
			profile = TIZEN_PROFILE_COMMON;
	}
	free(profile_name);

	return profile;
}

static inline tizen_model_t _get_tizen_model(void)
{
	char *model_name = NULL;

	if (__builtin_expect(model != TIZEN_MODEL_UNKNOWN, 1))
		return model;

	if (__get_profile_from_model_config_xml("tizen.org/system/model_name",
									&model_name) < 0) {
		model = TIZEN_MODEL_COMMON;
		return model;
	}

	if (model_name == NULL) {
		model = TIZEN_MODEL_COMMON;
		return model;
	}

	if (!strcasecmp(model_name, "TM1"))
		model = TIZEN_MODEL_TM1;
	else if (!strcasecmp(model_name, "TM2"))
		model = TIZEN_MODEL_TM2;
	else if (!strcasecmp(model_name, "TW1"))
		model = TIZEN_MODEL_TW1;
	else if (!strcasecmp(model_name, "TW2"))
		model = TIZEN_MODEL_TW2;
	else if (!strcasecmp(model_name, "TW3"))
		model = TIZEN_MODEL_TW3;
	else if (!strcasecmp(model_name, "rpi3"))
		model = TIZEN_MODEL_RPI3;
	else if (!strcasecmp(model_name, "gems") || !strncasecmp(model_name, "ServingBot", 10))
		model = TIZEN_MODEL_ROBOT;
	else if (!strncasecmp(model_name, "Family Hub", 10))
		model = TIZEN_MODEL_FHUB;
	else
		model = TIZEN_MODEL_COMMON;

	free(model_name);

	return model;
}

static inline tizen_certifcation_mode_t _get_tizen_certification_mode(void)
{
	if (__builtin_expect(certification_mode != TIZEN_CERTI_MODE_UNKNOWN, 1))
		return certification_mode;

	if (access(CERTI_STACK_FILE, F_OK) == 0)
		certification_mode |= TIZEN_CERTI_MODE_STACK;

	if (access(CERTI_PROFILE_FILE, F_OK) == 0)
		certification_mode |= TIZEN_CERTI_MODE_PROFILE;

	if (certification_mode == TIZEN_CERTI_MODE_UNKNOWN)
		certification_mode = TIZEN_CERTI_MODE_DISABLE;

	return certification_mode;
}

#define TIZEN_FEATURE_BLUEZ_BRCM_CHIP ((_get_tizen_profile()) == TIZEN_PROFILE_IVI)
#define TIZEN_FEATURE_BLUEZ_WEARABLE ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
#define TIZEN_FEATURE_BLUEZ_SMS_ONLY ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
#define TIZEN_FEATURE_BLUEZ_BRCM_QOS ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
#define TIZEN_FEATURE_BLUEZ_ROLE_CHANGE ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
#define TIZEN_FEATURE_BLUEZ_CONFIRM_ONLY ((_get_tizen_profile()) == TIZEN_PROFILE_WEARABLE)
#define TIZEN_FEATURE_BLUEZ_SPRD_QOS ((_get_tizen_model()) == TIZEN_MODEL_TM1)
#define TIZEN_FEATURE_BLUEZ_SPRD_PAGE_SCAN ((_get_tizen_model()) == TIZEN_MODEL_TM1)
#define TIZEN_FEATURE_BLUEZ_SPEAKER_REFERENCE ((_get_tizen_model()) == TIZEN_MODEL_RPI3 && (_get_tizen_profile()) == TIZEN_PROFILE_COMMON)
#define TIZEN_FEATURE_ROBOT_REFERENCE ((_get_tizen_model()) == TIZEN_MODEL_ROBOT)
#define TIZEN_FEATURE_BLUEZ_FHUB ((_get_tizen_model()) == TIZEN_MODEL_FHUB)
#define TIZEN_FEATURE_BLUEZ_STACK_CERTIFICATION (((_get_tizen_certification_mode()) & TIZEN_CERTI_MODE_STACK) != 0)
#define TIZEN_FEATURE_BLUEZ_PROFILE_CERTIFICATION (((_get_tizen_certification_mode()) & TIZEN_CERTI_MODE_PROFILE) != 0)

#endif /* __TIZEN_PROFILE_H__ */