Force add a primary plane to lease
[src/drm-lease-manager.git] / drm-lease-manager / lease-config.c
1 /* Copyright 2022 IGEL Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "lease-config.h"
17 #include "log.h"
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <toml.h>
23
24 #define CONFIG_ERROR(x, ...) ERROR_LOG("%s: " x, filename, ##__VA_ARGS__)
25
26 static bool populate_connector_planes(struct connector_config *config,
27                                       toml_array_t *planes)
28 {
29         config->nplanes = toml_array_nelem(planes);
30         config->planes = calloc(config->nplanes, sizeof(uint32_t));
31         if (!config->planes) {
32                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
33                 return false;
34         }
35
36         for (int j = 0; j < config->nplanes; j++) {
37                 toml_datum_t plane = toml_int_at(planes, j);
38                 if (!plane.ok) {
39                         return false;
40                 }
41                 config->planes[j] = plane.u.i;
42         }
43         return true;
44 }
45
46 static bool populate_connector_config(struct lease_config *config,
47                                       toml_table_t *global_table,
48                                       toml_array_t *conns)
49 {
50         int nconnectors = toml_array_nelem(conns);
51         config->connectors = calloc(nconnectors, sizeof(*config->connectors));
52         if (!config->connectors) {
53                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
54                 return false;
55         }
56
57         config->nconnectors = nconnectors;
58
59         for (int i = 0; i < config->nconnectors; i++) {
60                 struct connector_config *conn_config = &config->connectors[i];
61                 toml_datum_t conn = toml_string_at(conns, i);
62                 if (!conn.ok) {
63                         ERROR_LOG("Invalid connector in lease %s: idx:%d\n",
64                                   config->lease_name, i);
65                         return false;
66                 }
67                 conn_config->name = conn.u.s;
68
69                 toml_table_t *conn_config_data =
70                     toml_table_in(global_table, conn.u.s);
71                 if (!conn_config_data)
72                         continue;
73
74                 toml_datum_t optional =
75                     toml_bool_in(conn_config_data, "optional");
76                 if (optional.ok)
77                         config->connectors[i].optional = optional.u.b;
78
79                 toml_array_t *planes =
80                     toml_array_in(conn_config_data, "planes");
81                 if (planes != NULL) {
82                         bool ret =
83                             populate_connector_planes(conn_config, planes);
84                         if (ret == false) {
85                                 ERROR_LOG(
86                                     "Invalid plane id for connector: %s\n",
87                                     conn_config->name);
88                                 return false;
89                         } else {
90                                 config->have_plane_setting = true;
91                         }
92                 }
93         }
94         return true;
95 }
96
97 int parse_config(char *filename, struct lease_config **parsed_config)
98 {
99         struct lease_config *config = NULL;
100         int nconfigs, i, ret = 0;
101         char parse_error[160];
102
103         FILE *fp = fopen(filename, "r");
104         if (!fp)
105                 return 0;
106
107         toml_table_t *t_config =
108             toml_parse_file(fp, parse_error, sizeof parse_error);
109         if (!t_config) {
110                 CONFIG_ERROR("configuration file parse error: %s\n",
111                              parse_error);
112                 fclose(fp);
113                 return 0;
114         }
115
116         toml_array_t *leases = toml_array_in(t_config, "lease");
117         if (!leases) {
118                 CONFIG_ERROR(
119                     "Invalid config - cannot find any 'lease' configs");
120                 goto err;
121         }
122         nconfigs = toml_array_nelem(leases);
123         config = calloc(nconfigs, sizeof *config);
124
125         if (!config) {
126                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
127                 goto err;
128         }
129         config->have_plane_setting = false;
130
131         for (i = 0; i < toml_array_nelem(leases); i++) {
132                 toml_table_t *lease = toml_table_at(leases, i);
133
134                 toml_datum_t name = toml_string_in(lease, "name");
135                 if (!name.ok) {
136                         CONFIG_ERROR("Invalid lease name in entry #%d\n", i);
137                         goto err_free_config;
138                 }
139
140                 config[i].lease_name = name.u.s;
141
142                 toml_array_t *conns = toml_array_in(lease, "connectors");
143                 if (conns &&
144                     !populate_connector_config(&config[i], t_config, conns)) {
145                         CONFIG_ERROR("Error configuring lease: %s\n",
146                                      config[i].lease_name);
147                         goto err_free_config;
148                 }
149         }
150
151         *parsed_config = config;
152         ret = nconfigs;
153 err:
154         toml_free(t_config);
155         fclose(fp);
156         return ret;
157 err_free_config:
158         release_config(i, config);
159         goto err;
160 }
161
162 void release_config(int num_leases, struct lease_config *config)
163 {
164         for (int i = 0; i < num_leases; i++) {
165                 struct lease_config *c = &config[i];
166                 free(c->lease_name);
167                 for (int j = 0; j < c->nconnectors; j++) {
168                         free(c->connectors[j].name);
169                         free(c->connectors[j].planes);
170                 }
171                 free(c->connectors);
172         }
173         free(config);
174 }