Skip to content

Commit 55902b7

Browse files
committed
Export device_parser struct and functions
Export the functions and struct to enable devices to parse yaml aswell via the parse_options op to be stored in the device control_options device property. Signed-off-by: Neil Armstrong <[email protected]>
1 parent fbdc677 commit 55902b7

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

device.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ struct cdb_assist;
88
struct fastboot;
99
struct fastboot_ops;
1010
struct device;
11+
struct device_parser;
1112

1213
struct control_ops {
14+
void *(*parse_options)(struct device_parser *dp);
1315
void *(*open)(struct device *dev);
1416
void (*close)(struct device *dev);
1517
int (*power)(struct device *dev, bool on);
@@ -28,6 +30,7 @@ struct console_ops {
2830
struct device {
2931
char *board;
3032
char *control_dev;
33+
void *control_options;
3134
char *console_dev;
3235
char *name;
3336
char *serial;

device_parser.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <yaml.h>
3434

3535
#include "device.h"
36+
#include "device_parser.h"
3637

3738
#define TOKEN_LENGTH 16384
3839

@@ -49,12 +50,13 @@ static void nextsym(struct device_parser *dp)
4950
}
5051
}
5152

52-
static int accept(struct device_parser *dp, int type, char *scalar)
53+
int device_parser_accept(struct device_parser *dp, int type,
54+
char *scalar, size_t scalar_len)
5355
{
5456
if (dp->event.type == type) {
55-
if (scalar) {
56-
strncpy(scalar, (char *)dp->event.data.scalar.value, TOKEN_LENGTH - 1);
57-
scalar[TOKEN_LENGTH - 1] = '\0';
57+
if (scalar && scalar_len > 0) {
58+
strncpy(scalar, (char *)dp->event.data.scalar.value, scalar_len - 1);
59+
scalar[scalar_len - 1] = '\0';
5860
}
5961

6062
yaml_event_delete(&dp->event);
@@ -65,9 +67,10 @@ static int accept(struct device_parser *dp, int type, char *scalar)
6567
}
6668
}
6769

68-
static bool expect(struct device_parser *dp, int type, char *scalar)
70+
bool device_parser_expect(struct device_parser *dp, int type,
71+
char *scalar, size_t scalar_len)
6972
{
70-
if (accept(dp, type, scalar)) {
73+
if (device_parser_accept(dp, type, scalar, scalar_len)) {
7174
return true;
7275
}
7376

@@ -103,30 +106,30 @@ static void parse_board(struct device_parser *dp)
103106

104107
dev = calloc(1, sizeof(*dev));
105108

106-
while (accept(dp, YAML_SCALAR_EVENT, key)) {
109+
while (device_parser_accept(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) {
107110
if (!strcmp(key, "users")) {
108111
dev->users = calloc(1, sizeof(*dev->users));
109112
list_init(dev->users);
110113

111-
if (accept(dp, YAML_SCALAR_EVENT, value))
114+
if (device_parser_accept(dp, YAML_SCALAR_EVENT, value, 0))
112115
continue;
113116

114-
expect(dp, YAML_SEQUENCE_START_EVENT, NULL);
117+
device_parser_expect(dp, YAML_SEQUENCE_START_EVENT, NULL, 0);
115118

116-
while (accept(dp, YAML_SCALAR_EVENT, key)) {
119+
while (device_parser_accept(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) {
117120
struct device_user *user = calloc(1, sizeof(*user));
118121

119122
user->username = strdup(key);
120123

121124
list_add(dev->users, &user->node);
122125
}
123126

124-
expect(dp, YAML_SEQUENCE_END_EVENT, NULL);
127+
device_parser_expect(dp, YAML_SEQUENCE_END_EVENT, NULL, 0);
125128

126129
continue;
127130
}
128131

129-
expect(dp, YAML_SCALAR_EVENT, value);
132+
device_parser_expect(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH);
130133

131134
if (!strcmp(key, "board")) {
132135
dev->board = strdup(value);
@@ -212,25 +215,25 @@ int device_parser(const char *path)
212215

213216
nextsym(&dp);
214217

215-
expect(&dp, YAML_STREAM_START_EVENT, NULL);
218+
device_parser_expect(&dp, YAML_STREAM_START_EVENT, NULL, 0);
216219

217-
expect(&dp, YAML_DOCUMENT_START_EVENT, NULL);
218-
expect(&dp, YAML_MAPPING_START_EVENT, NULL);
220+
device_parser_expect(&dp, YAML_DOCUMENT_START_EVENT, NULL, 0);
221+
device_parser_expect(&dp, YAML_MAPPING_START_EVENT, NULL, 0);
219222

220-
if (accept(&dp, YAML_SCALAR_EVENT, key)) {
221-
expect(&dp, YAML_SEQUENCE_START_EVENT, NULL);
223+
if (device_parser_accept(&dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) {
224+
device_parser_expect(&dp, YAML_SEQUENCE_START_EVENT, NULL, 0);
222225

223-
while (accept(&dp, YAML_MAPPING_START_EVENT, NULL)) {
226+
while (device_parser_accept(&dp, YAML_MAPPING_START_EVENT, NULL, 0)) {
224227
parse_board(&dp);
225-
expect(&dp, YAML_MAPPING_END_EVENT, NULL);
228+
device_parser_expect(&dp, YAML_MAPPING_END_EVENT, NULL, 0);
226229
}
227230

228-
expect(&dp, YAML_SEQUENCE_END_EVENT, NULL);
231+
device_parser_expect(&dp, YAML_SEQUENCE_END_EVENT, NULL, 0);
229232
}
230233

231-
expect(&dp, YAML_MAPPING_END_EVENT, NULL);
232-
expect(&dp, YAML_DOCUMENT_END_EVENT, NULL);
233-
expect(&dp, YAML_STREAM_END_EVENT, NULL);
234+
device_parser_expect(&dp, YAML_MAPPING_END_EVENT, NULL, 0);
235+
device_parser_expect(&dp, YAML_DOCUMENT_END_EVENT, NULL, 0);
236+
device_parser_expect(&dp, YAML_STREAM_END_EVENT, NULL, 0);
234237

235238
yaml_event_delete(&dp.event);
236239
yaml_parser_delete(&dp.parser);

device_parser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#ifndef __DEVICE_PARSER_H__
22
#define __DEVICE_PARSER_H__
33

4+
struct device_parser;
5+
6+
int device_parser_accept(struct device_parser *dp, int type,
7+
char *scalar, size_t scalar_len);
8+
bool device_parser_expect(struct device_parser *dp, int type,
9+
char *scalar, size_t scalar_len);
10+
411
int device_parser(const char *path);
512

613
#endif

0 commit comments

Comments
 (0)