accessor: Move accessor generators to one file

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Bjorn Andersson 2018-01-30 16:54:13 -08:00
parent a8c34af751
commit 06a2c5a19b
5 changed files with 172 additions and 142 deletions

View File

@ -4,7 +4,7 @@ CFLAGS := -Wall -g -O2
LDFLAGS :=
prefix := /usr/local
SRCS := parser.c qmic.c qmi_message.c qmi_struct.c
SRCS := accessor.c parser.c qmic.c
OBJS := $(SRCS:.c=.o)
$(OUT): $(OBJS)

View File

@ -1,11 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "list.h"
#include "qmic.h"
static void qmi_struct_header(FILE *fp, const char *package)
{
struct qmi_struct_member *qsm;
struct qmi_struct *qs;
list_for_each_entry(qs, &qmi_structs, node) {
fprintf(fp, "struct %s_%s {\n",
package, qs->name);
list_for_each_entry(qsm, &qs->members, node) {
fprintf(fp, "\t%s %s;\n",
sz_simple_types[qsm->type], qsm->name);
}
fprintf(fp, "};\n"
"\n");
}
}
static void qmi_struct_emit_prototype(FILE *fp,
const char *package,
const char *message,
const char *member,
unsigned array_size,
struct qmi_struct *qs)
{
if (array_size) {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count);\n",
package, message, member, qs->name);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count);\n\n",
package, message, member, qs->name);
} else {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val);\n",
package, message, member, qs->name);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s);\n\n",
package, message, member, qs->name);
}
}
static void qmi_struct_emit_accessors(FILE *fp,
const char *package,
const char *message,
const char *member,
int member_id,
unsigned array_size,
struct qmi_struct *qs)
{
if (array_size) {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count)\n"
"{\n"
" return qmi_tlv_set_array((struct qmi_tlv*)%2$s, %5$d, %6$d, val, count, sizeof(struct %1$s_%4$s));\n"
"}\n\n",
package, message, member, qs->name, member_id, array_size);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count)\n"
"{\n"
" size_t size;\n"
" size_t len;\n"
" void *ptr;\n"
"\n"
" ptr = qmi_tlv_get_array((struct qmi_tlv*)%2$s, %5$d, %6$d, &len, &size);\n"
" if (!ptr)\n"
" return NULL;\n"
"\n"
" if (size != sizeof(struct %1$s_%4$s))\n"
" return NULL;\n"
"\n"
" *count = len;\n"
" return ptr;\n"
"}\n\n",
package, message, member, qs->name, member_id, array_size);
} else {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val)\n"
"{\n"
" return qmi_tlv_set((struct qmi_tlv*)%2$s, %5$d, val, sizeof(struct %1$s_%4$s));\n"
"}\n\n",
package, message, member, qs->name, member_id);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s)\n"
"{\n"
" size_t len;\n"
" void *ptr;\n"
"\n"
" ptr = qmi_tlv_get((struct qmi_tlv*)%2$s, %5$d, &len);\n"
" if (!ptr)\n"
" return NULL;\n"
"\n"
" if (len != sizeof(struct %1$s_%4$s))\n"
" return NULL;\n"
"\n"
" return ptr;\n"
"}\n\n",
package, message, member, qs->name, member_id);
}
}
static void qmi_message_emit_message_type(FILE *fp,
const char *package,
const char *message)
@ -188,7 +281,7 @@ static void qmi_message_emit_string_accessors(FILE *fp,
}
void qmi_message_source(FILE *fp, const char *package)
static void qmi_message_source(FILE *fp, const char *package)
{
struct qmi_message_member *qmm;
struct qmi_message *qm;
@ -215,7 +308,7 @@ void qmi_message_source(FILE *fp, const char *package)
}
}
void qmi_message_header(FILE *fp, const char *package)
static void qmi_message_header(FILE *fp, const char *package)
{
struct qmi_message_member *qmm;
struct qmi_message *qm;
@ -246,3 +339,37 @@ void qmi_message_header(FILE *fp, const char *package)
}
}
}
static void emit_header_file_header(FILE *fp)
{
fprintf(fp, "#include <stdint.h>\n"
"#include <stdlib.h>\n\n");
fprintf(fp, "struct qmi_tlv;\n"
"\n"
"struct qmi_tlv *qmi_tlv_init(unsigned txn, unsigned msg_id, unsigned type);\n"
"struct qmi_tlv *qmi_tlv_decode(void *buf, size_t len, unsigned *txn, unsigned type);\n"
"void *qmi_tlv_encode(struct qmi_tlv *tlv, size_t *len);\n"
"void qmi_tlv_free(struct qmi_tlv *tlv);\n"
"\n"
"void *qmi_tlv_get(struct qmi_tlv *tlv, unsigned id, size_t *len);\n"
"void *qmi_tlv_get_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, size_t *len, size_t *size);\n"
"int qmi_tlv_set(struct qmi_tlv *tlv, unsigned id, void *buf, size_t len);\n"
"int qmi_tlv_set_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, void *buf, size_t len, size_t size);\n"
"\n");
}
void accessor_emit_c(FILE *fp, const char *package)
{
emit_source_includes(fp, package);
qmi_message_source(fp, package);
}
void accessor_emit_h(FILE *fp, const char *package)
{
guard_header(fp, qmi_package);
emit_header_file_header(fp);
qmi_const_header(fp);
qmi_struct_header(fp, qmi_package);
qmi_message_header(fp, qmi_package);
guard_footer(fp);
}

View File

@ -1,101 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "qmic.h"
void qmi_struct_header(FILE *fp, const char *package)
{
struct qmi_struct_member *qsm;
struct qmi_struct *qs;
list_for_each_entry(qs, &qmi_structs, node) {
fprintf(fp, "struct %s_%s {\n",
package, qs->name);
list_for_each_entry(qsm, &qs->members, node) {
fprintf(fp, "\t%s %s;\n",
sz_simple_types[qsm->type], qsm->name);
}
fprintf(fp, "};\n"
"\n");
}
}
void qmi_struct_emit_prototype(FILE *fp,
const char *package,
const char *message,
const char *member,
unsigned array_size,
struct qmi_struct *qs)
{
if (array_size) {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count);\n",
package, message, member, qs->name);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count);\n\n",
package, message, member, qs->name);
} else {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val);\n",
package, message, member, qs->name);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s);\n\n",
package, message, member, qs->name);
}
}
void qmi_struct_emit_accessors(FILE *fp,
const char *package,
const char *message,
const char *member,
int member_id,
unsigned array_size,
struct qmi_struct *qs)
{
if (array_size) {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count)\n"
"{\n"
" return qmi_tlv_set_array((struct qmi_tlv*)%2$s, %5$d, %6$d, val, count, sizeof(struct %1$s_%4$s));\n"
"}\n\n",
package, message, member, qs->name, member_id, array_size);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count)\n"
"{\n"
" size_t size;\n"
" size_t len;\n"
" void *ptr;\n"
"\n"
" ptr = qmi_tlv_get_array((struct qmi_tlv*)%2$s, %5$d, %6$d, &len, &size);\n"
" if (!ptr)\n"
" return NULL;\n"
"\n"
" if (size != sizeof(struct %1$s_%4$s))\n"
" return NULL;\n"
"\n"
" *count = len;\n"
" return ptr;\n"
"}\n\n",
package, message, member, qs->name, member_id, array_size);
} else {
fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val)\n"
"{\n"
" return qmi_tlv_set((struct qmi_tlv*)%2$s, %5$d, val, sizeof(struct %1$s_%4$s));\n"
"}\n\n",
package, message, member, qs->name, member_id);
fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s)\n"
"{\n"
" size_t len;\n"
" void *ptr;\n"
"\n"
" ptr = qmi_tlv_get((struct qmi_tlv*)%2$s, %5$d, &len);\n"
" if (!ptr)\n"
" return NULL;\n"
"\n"
" if (len != sizeof(struct %1$s_%4$s))\n"
" return NULL;\n"
"\n"
" return ptr;\n"
"}\n\n",
package, message, member, qs->name, member_id);
}
}

57
qmic.c
View File

@ -20,7 +20,7 @@ const char *sz_simple_types[] = {
[TYPE_STRING] = "char *",
};
static void qmi_const_header(FILE *fp)
void qmi_const_header(FILE *fp)
{
struct qmi_const *qc;
@ -33,7 +33,7 @@ static void qmi_const_header(FILE *fp)
fprintf(fp, "\n");
}
static void emit_source_header(FILE *fp, const char *package)
void emit_source_includes(FILE *fp, const char *package)
{
fprintf(fp, "#include <errno.h>\n"
"#include <string.h>\n"
@ -41,7 +41,7 @@ static void emit_source_header(FILE *fp, const char *package)
package);
}
static void guard_header(FILE *fp, const char *package)
void guard_header(FILE *fp, const char *package)
{
char *upper;
char *p;
@ -55,32 +55,41 @@ static void guard_header(FILE *fp, const char *package)
fprintf(fp, "#ifndef __QMI_%s_H__\n", upper);
fprintf(fp, "#define __QMI_%s_H__\n", upper);
fprintf(fp, "\n");
fprintf(fp, "#include <stdint.h>\n"
"#include <stdlib.h>\n\n");
fprintf(fp, "struct qmi_tlv;\n"
"\n"
"struct qmi_tlv *qmi_tlv_init(unsigned txn, unsigned msg_id, unsigned type);\n"
"struct qmi_tlv *qmi_tlv_decode(void *buf, size_t len, unsigned *txn, unsigned type);\n"
"void *qmi_tlv_encode(struct qmi_tlv *tlv, size_t *len);\n"
"void qmi_tlv_free(struct qmi_tlv *tlv);\n"
"\n"
"void *qmi_tlv_get(struct qmi_tlv *tlv, unsigned id, size_t *len);\n"
"void *qmi_tlv_get_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, size_t *len, size_t *size);\n"
"int qmi_tlv_set(struct qmi_tlv *tlv, unsigned id, void *buf, size_t len);\n"
"int qmi_tlv_set_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, void *buf, size_t len, size_t size);\n"
"\n");
}
static void guard_footer(FILE *fp)
void guard_footer(FILE *fp)
{
fprintf(fp, "#endif\n");
}
static void usage(void)
{
extern const char *__progname;
fprintf(stderr, "Usage: %s -a\n", __progname);
exit(1);
}
int main(int argc, char **argv)
{
char fname[256];
FILE *hfp;
FILE *sfp;
int method = 0;
int opt;
while ((opt = getopt(argc, argv, "a")) != -1) {
switch (opt) {
case 'a':
method = 1;
break;
default:
usage();
}
}
if (!method)
usage();
qmi_parse();
@ -94,16 +103,8 @@ int main(int argc, char **argv)
if (!hfp)
err(1, "failed to open %s", fname);
/* Source output */
emit_source_header(sfp, qmi_package);
qmi_message_source(sfp, qmi_package);
/* Header output */
guard_header(hfp, qmi_package);
qmi_const_header(hfp);
qmi_struct_header(hfp, qmi_package);
qmi_message_header(hfp, qmi_package);
guard_footer(hfp);
accessor_emit_c(sfp, qmi_package);
accessor_emit_h(hfp, qmi_package);
fclose(hfp);
fclose(sfp);

17
qmic.h
View File

@ -3,6 +3,8 @@
#include <stdbool.h>
#include "list.h"
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
enum {
@ -71,13 +73,14 @@ extern struct list_head qmi_consts;
extern struct list_head qmi_messages;
extern struct list_head qmi_structs;
void qmi_message_source(FILE *fp, const char *package);
void qmi_message_header(FILE *fp, const char *package);
void qmi_struct_header(FILE *fp, const char *package);
void qmi_struct_emit_prototype(FILE *fp, const char *package, const char *message, const char *member, unsigned array_size, struct qmi_struct *qs);
void qmi_struct_emit_accessors(FILE *fp, const char *package, const char *message, const char *member, int member_id, unsigned array_size, struct qmi_struct *qs);
void qmi_parse(void);
void emit_source_includes(FILE *fp, const char *package);
void guard_header(FILE *fp, const char *package);
void guard_footer(FILE *fp);
void qmi_const_header(FILE *fp);
void accessor_emit_c(FILE *fp, const char *package);
void accessor_emit_h(FILE *fp, const char *package);
#endif