board: support switch partition
[platform/hal/api/device.git] / src / board.c
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <hal/hal-common.h>
19
20 #include "hal-board-interface.h"
21 #include "common.h"
22
23 static hal_backend_board_funcs *hal_board_funcs = NULL;
24 /*
25 -1 : failed to initialize
26 0  : not initialized
27 1  : succeeded to initialize
28 */
29 static int hal_initialized = 0;
30
31 int hal_device_board_get_backend(void)
32 {
33         int ret;
34
35         if (hal_board_funcs)
36                 return 0;
37
38         ret = hal_common_get_backend(HAL_MODULE_DEVICE_BOARD, (void **)&hal_board_funcs);
39         if (ret < 0) {
40                  _E("Failed to get board backend");
41                 hal_initialized = -1;
42                 return -EINVAL;
43         }
44
45         hal_initialized = 1;
46         return 0;
47 }
48
49 int hal_device_board_put_backend(void)
50 {
51         if (!hal_board_funcs)
52                 return 0;
53
54         hal_common_put_backend(HAL_MODULE_DEVICE_BOARD, (void *)hal_board_funcs);
55         hal_board_funcs = NULL;
56         hal_initialized = 0;
57
58         return 0;
59 }
60
61 int hal_device_board_get_device_serial_number(char *buffer, int len)
62 {
63         int ret ;
64
65         if (!hal_board_funcs && !hal_initialized) {
66                 if ((ret = hal_device_board_get_backend()) < 0)
67                         return ret;
68         }
69
70         if (!hal_board_funcs ||
71             !hal_board_funcs->get_device_serial_number)
72                 return -ENODEV;
73
74         return hal_board_funcs->get_device_serial_number(buffer, len);
75 }
76
77 int hal_device_board_get_device_revision(int *revision)
78 {
79         int ret ;
80
81         if (!hal_board_funcs && !hal_initialized) {
82                 if ((ret = hal_device_board_get_backend()) < 0)
83                         return ret;
84         }
85
86         if (!hal_board_funcs ||
87             !hal_board_funcs->get_device_revision)
88                 return -ENODEV;
89
90         return hal_board_funcs->get_device_revision(revision);
91 }
92
93 int hal_device_board_switch_partition(int argc, char *argv[])
94 {
95         int ret;
96
97         if (!hal_board_funcs && !hal_initialized) {
98                 if ((ret = hal_device_board_get_backend()) < 0)
99                         return ret;
100         }
101
102         if (!hal_board_funcs ||
103             !hal_board_funcs->switch_partition)
104                 return -ENODEV;
105
106         return hal_board_funcs->switch_partition(argc, argv);
107 }