1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <iniparser.h>
7
8 #include "cras_device_blacklist.h"
9 #include "utlist.h"
10
11 /* Allocate 63 chars + 1 for null where declared. */
12 static const unsigned int MAX_INI_NAME_LEN = 63;
13 static const unsigned int MAX_KEY_LEN = 63;
14
15 struct cras_device_blacklist {
16 dictionary *ini;
17 };
18
19 /*
20 * Exported Interface
21 */
22
cras_device_blacklist_create(const char * config_path)23 struct cras_device_blacklist *cras_device_blacklist_create(
24 const char *config_path)
25 {
26 struct cras_device_blacklist *blacklist;
27 char ini_name[MAX_INI_NAME_LEN + 1];
28
29 blacklist = calloc(1, sizeof(*blacklist));
30 if (!blacklist)
31 return NULL;
32
33 snprintf(ini_name, MAX_INI_NAME_LEN, "%s/%s",
34 config_path, "device_blacklist");
35 ini_name[MAX_INI_NAME_LEN] = '\0';
36 blacklist->ini = iniparser_load(ini_name);
37
38 return blacklist;
39 }
40
cras_device_blacklist_destroy(struct cras_device_blacklist * blacklist)41 void cras_device_blacklist_destroy(struct cras_device_blacklist *blacklist)
42 {
43 if (blacklist && blacklist->ini)
44 iniparser_freedict(blacklist->ini);
45 free(blacklist);
46 }
47
cras_device_blacklist_check(struct cras_device_blacklist * blacklist,unsigned vendor_id,unsigned product_id,unsigned desc_checksum,unsigned device_index)48 int cras_device_blacklist_check(struct cras_device_blacklist *blacklist,
49 unsigned vendor_id,
50 unsigned product_id,
51 unsigned desc_checksum,
52 unsigned device_index)
53 {
54 char ini_key[MAX_KEY_LEN + 1];
55
56 if (!blacklist)
57 return 0;
58
59 snprintf(ini_key, MAX_KEY_LEN, "USB_Outputs:%04x_%04x_%08x_%u",
60 vendor_id, product_id, desc_checksum, device_index);
61 ini_key[MAX_KEY_LEN] = 0;
62 return iniparser_getboolean(blacklist->ini, ini_key, 0);
63 }
64