File: chip-info.c

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (73 lines) | stat: -rw-r--r-- 1,609 bytes parent folder | download | duplicates (3)
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
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include <assert.h>
#include <gpiod.h>
#include <stdlib.h>
#include <string.h>

#include "internal.h"

struct gpiod_chip_info {
	size_t num_lines;
	char name[GPIO_MAX_NAME_SIZE + 1];
	char label[GPIO_MAX_NAME_SIZE + 1];
};

GPIOD_API void gpiod_chip_info_free(struct gpiod_chip_info *info)
{
	free(info);
}

GPIOD_API const char *gpiod_chip_info_get_name(struct gpiod_chip_info *info)
{
	assert(info);

	return info->name;
}

GPIOD_API const char *gpiod_chip_info_get_label(struct gpiod_chip_info *info)
{
	assert(info);

	return info->label;
}

GPIOD_API size_t gpiod_chip_info_get_num_lines(struct gpiod_chip_info *info)
{
	assert(info);

	return info->num_lines;
}

struct gpiod_chip_info *
gpiod_chip_info_from_uapi(struct gpiochip_info *uapi_info)
{
	struct gpiod_chip_info *info;

	info = malloc(sizeof(*info));
	if (!info)
		return NULL;

	memset(info, 0, sizeof(*info));

	info->num_lines = uapi_info->lines;

	/*
	 * GPIO device must have a name - don't bother checking this field. In
	 * the worst case (would have to be a weird kernel bug) it'll be empty.
	 */
	strncpy(info->name, uapi_info->name, sizeof(info->name));

	/*
	 * The kernel sets the label of a GPIO device to "unknown" if it
	 * hasn't been defined in DT, board file etc. On the off-chance that
	 * we got an empty string, do the same.
	 */
	if (uapi_info->label[0] == '\0')
		strncpy(info->label, "unknown", sizeof(info->label));
	else
		strncpy(info->label, uapi_info->label, sizeof(info->label));

	return info;
}