Debian Codegraph @

lib/chip-info.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]>
3
4 #include <assert.h>
5 #include <gpiod.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "internal.h"
10
11 struct gpiod_chip_info {
12 size_t num_lines;
13 char name[GPIO_MAX_NAME_SIZE + 1];
14 char label[GPIO_MAX_NAME_SIZE + 1];
15 };
16
17 GPIOD_API void gpiod_chip_info_free(struct gpiod_chip_info *info)
18 {
19 free(info);
20 }
21
22 GPIOD_API const char *gpiod_chip_info_get_name(struct gpiod_chip_info *info)
23 {
24 assert(info);
25
26 return info->name;
27 }
28
29 GPIOD_API const char *gpiod_chip_info_get_label(struct gpiod_chip_info *info)
30 {
31 assert(info);
32
33 return info->label;
34 }
35
36 GPIOD_API size_t gpiod_chip_info_get_num_lines(struct gpiod_chip_info *info)
37 {
38 assert(info);
39
40 return info->num_lines;
41 }
42
43 struct gpiod_chip_info *
44 gpiod_chip_info_from_uapi(struct gpiochip_info *uapi_info)
45 {
46 struct gpiod_chip_info *info;
47
48 info = malloc(sizeof(*info));
49 if (!info)
50 return NULL;
51
52 memset(info, 0, sizeof(*info));
53
54 info->num_lines = uapi_info->lines;
55
56 /*
57 * GPIO device must have a name - don't bother checking this field. In
58 * the worst case (would have to be a weird kernel bug) it'll be empty.
59 */
60 strncpy(info->name, uapi_info->name, GPIO_MAX_NAME_SIZE);
61 info->name[GPIO_MAX_NAME_SIZE] = '\0';
62
63 /*
64 * The kernel sets the label of a GPIO device to "unknown" if it
65 * hasn't been defined in DT, board file etc. On the off-chance that
66 * we got an empty string, do the same.
67 */
68 if (uapi_info->label[0] == '\0')
69 strncpy(info->label, "unknown", GPIO_MAX_NAME_SIZE);
70 else
71 strncpy(info->label, uapi_info->label, GPIO_MAX_NAME_SIZE);
72 info->label[GPIO_MAX_NAME_SIZE] = '\0';
73
74 return info;
75 }