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 "cras_device_blacklist.h"
7 #include "iniparser_wrapper.h"
8 #include "utlist.h"
9
10 /* Allocate 63 chars + 1 for null where declared. */
11 static const unsigned int MAX_INI_NAME_LEN = 63;
12 static const unsigned int MAX_KEY_LEN = 63;
13
14 struct cras_device_blacklist {
15 dictionary *ini;
16 };
17
18 /*
19 * Exported Interface
20 */
21
cras_device_blacklist_create(const char * config_path)22 struct cras_device_blacklist *cras_device_blacklist_create(
23 const char *config_path)
24 {
25 struct cras_device_blacklist *blacklist;
26 char ini_name[MAX_INI_NAME_LEN + 1];
27
28 blacklist = calloc(1, sizeof(*blacklist));
29 if (!blacklist)
30 return NULL;
31
32 snprintf(ini_name, MAX_INI_NAME_LEN, "%s/%s",
33 config_path, "device_blacklist");
34 ini_name[MAX_INI_NAME_LEN] = '\0';
35 blacklist->ini = iniparser_load_wrapper(ini_name);
36
37 return blacklist;
38 }
39
cras_device_blacklist_destroy(struct cras_device_blacklist * blacklist)40 void cras_device_blacklist_destroy(struct cras_device_blacklist *blacklist)
41 {
42 if (blacklist && blacklist->ini)
43 iniparser_freedict(blacklist->ini);
44 free(blacklist);
45 }
46
cras_device_blacklist_check(struct cras_device_blacklist * blacklist,unsigned vendor_id,unsigned product_id,unsigned desc_checksum,unsigned device_index)47 int cras_device_blacklist_check(struct cras_device_blacklist *blacklist,
48 unsigned vendor_id,
49 unsigned product_id,
50 unsigned desc_checksum,
51 unsigned device_index)
52 {
53 char ini_key[MAX_KEY_LEN + 1];
54
55 if (!blacklist)
56 return 0;
57
58 snprintf(ini_key, MAX_KEY_LEN, "USB_Outputs:%04x_%04x_%08x_%u",
59 vendor_id, product_id, desc_checksum, device_index);
60 ini_key[MAX_KEY_LEN] = 0;
61 return iniparser_getboolean(blacklist->ini, ini_key, 0);
62 }
63