summaryrefslogtreecommitdiff
path: root/io-module/mts_capab.c
blob: e8e4a273f3b739412e628abe3f689e5ca32af30c (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
/*
 * capab.c
 *
 *  Created on: Dec 1, 2016
 *      Author: jklug
 *
 *  capabilities from EE-PROM, enumerated in /sys/devices/platform/mts-io/capability
 *  an associative array, written the hard way in C.
 */

/* This array is not in any order. */
struct capab_map_s {
	unsigned int bitvalue;
	const char *name;
};

/*
 * Should match mts_eeprom.h capabilities
 */
static struct capab_map_s capabilities_map[] = {
		{ CAPA_GPS,			"gps"},
		{ CAPA_DIN,			"din"},
		{ CAPA_DOUT,		"dout"},
		{ CAPA_ADC,			"adc"},
		{ CAPA_BLUETOOTH, 	"bluetooth"},
		{ CAPA_WIFI, 		"wifi"},
		{ CAPA_LORA, 		"lora"},
};
static const unsigned capabilities_count = sizeof(capabilities_map)/sizeof(capabilities_map[0]);

static ssize_t capab_show_value(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
	int i;
	const int count = sizeof(capabilities_map)/sizeof(capabilities_map[0]);

	for(i=0;i<count;i++) {
		if (! strcmp(capabilities_map[i].name, attr->attr.name)) {
			return snprintf(buf, 32, "%u\n", DEVICE_CAPA(id_eeprom.capa, capabilities_map[i].bitvalue) != 0);
		}
	}

	return (ssize_t)-1;
}

static bool capab_add_attributes(struct attribute** attrs) {
	struct kobj_attribute* kobj_attr;
	int i;
	int count = capabilities_count;

	// We must keep the number of attributes in sync
	BUG_ON(CAPA_COUNT != count);
	for(i=0;i<count;i++) {
		kobj_attr = create_attribute(capabilities_map[i].name, MTS_ATTR_MODE_RO);
		if (! kobj_attr) {
			log_error("failed to create attribute [%s]", capabilities_map[i].name);
			return false;
		}
		kobj_attr->show = capab_show_value;
		attrs[i] = &kobj_attr->attr;
	}
	return true;
}