Add board HAL
[platform/adaptation/samsung_exynos/device-manager-plugin-artik.git] / hw / board / board.c
1 /*
2  * libdevice-node
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20 #include <hw/board.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26
27 #define SERIAL_FILE_PATH "/sys/firmware/devicetree/base/serial-number"
28 #define LINE_LEN 64
29
30 static int get_device_serial(char **out)
31 {
32         FILE *fp;
33         char *line, *p;
34
35         fp = fopen(SERIAL_FILE_PATH, "r");
36         if (!fp)
37                 return -1;
38
39         line = malloc(LINE_LEN);
40         p = fgets(line, LINE_LEN, fp);
41         fclose(fp);
42         if (p == NULL) {
43                 free(line);
44                 return -1;
45         }
46
47         *out = p;
48         return 0;
49 }
50
51 static int board_open(struct hw_info *info,
52                 const char *id, struct hw_common **common)
53 {
54         struct hw_board *b;
55
56         if (!info || !common)
57                 return -EINVAL;
58
59         b = calloc(1, sizeof(*b));
60         if (!b)
61                 return -ENOMEM;
62
63         b->common.info = info;
64         b->get_device_serial = get_device_serial;
65
66         *common = &b->common;
67         return 0;
68 }
69
70 static int board_close(struct hw_common *common)
71 {
72         struct hw_board *b;
73
74         if (!common)
75                 return -EINVAL;
76
77         b = container_of(common, struct hw_board, common);
78         free(b);
79
80         return 0;
81 }
82
83 HARDWARE_MODULE_STRUCTURE = {
84         .magic = HARDWARE_INFO_TAG,
85         .hal_version = HARDWARE_INFO_VERSION,
86         .device_version = BOARD_HARDWARE_DEVICE_VERSION,
87         .id = BOARD_HARDWARE_DEVICE_ID,
88         .name = "device",
89         .open = board_open,
90         .close = board_close,
91 };