mirror of
https://github.com/linux-msm/qmic.git
synced 2024-11-23 17:54:01 +08:00
accessor: Move accessor generators to one file
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
parent
a8c34af751
commit
06a2c5a19b
2
Makefile
2
Makefile
@ -4,7 +4,7 @@ CFLAGS := -Wall -g -O2
|
|||||||
LDFLAGS :=
|
LDFLAGS :=
|
||||||
prefix := /usr/local
|
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)
|
OBJS := $(SRCS:.c=.o)
|
||||||
|
|
||||||
$(OUT): $(OBJS)
|
$(OUT): $(OBJS)
|
||||||
|
@ -1,11 +1,104 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "list.h"
|
|
||||||
#include "qmic.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,
|
static void qmi_message_emit_message_type(FILE *fp,
|
||||||
const char *package,
|
const char *package,
|
||||||
const char *message)
|
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_member *qmm;
|
||||||
struct qmi_message *qm;
|
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_member *qmm;
|
||||||
struct qmi_message *qm;
|
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);
|
||||||
|
}
|
101
qmi_struct.c
101
qmi_struct.c
@ -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
57
qmic.c
@ -20,7 +20,7 @@ const char *sz_simple_types[] = {
|
|||||||
[TYPE_STRING] = "char *",
|
[TYPE_STRING] = "char *",
|
||||||
};
|
};
|
||||||
|
|
||||||
static void qmi_const_header(FILE *fp)
|
void qmi_const_header(FILE *fp)
|
||||||
{
|
{
|
||||||
struct qmi_const *qc;
|
struct qmi_const *qc;
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ static void qmi_const_header(FILE *fp)
|
|||||||
fprintf(fp, "\n");
|
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"
|
fprintf(fp, "#include <errno.h>\n"
|
||||||
"#include <string.h>\n"
|
"#include <string.h>\n"
|
||||||
@ -41,7 +41,7 @@ static void emit_source_header(FILE *fp, const char *package)
|
|||||||
package);
|
package);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void guard_header(FILE *fp, const char *package)
|
void guard_header(FILE *fp, const char *package)
|
||||||
{
|
{
|
||||||
char *upper;
|
char *upper;
|
||||||
char *p;
|
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, "#ifndef __QMI_%s_H__\n", upper);
|
||||||
fprintf(fp, "#define __QMI_%s_H__\n", upper);
|
fprintf(fp, "#define __QMI_%s_H__\n", upper);
|
||||||
fprintf(fp, "\n");
|
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");
|
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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char fname[256];
|
char fname[256];
|
||||||
FILE *hfp;
|
FILE *hfp;
|
||||||
FILE *sfp;
|
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();
|
qmi_parse();
|
||||||
|
|
||||||
@ -94,16 +103,8 @@ int main(int argc, char **argv)
|
|||||||
if (!hfp)
|
if (!hfp)
|
||||||
err(1, "failed to open %s", fname);
|
err(1, "failed to open %s", fname);
|
||||||
|
|
||||||
/* Source output */
|
accessor_emit_c(sfp, qmi_package);
|
||||||
emit_source_header(sfp, qmi_package);
|
accessor_emit_h(hfp, 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);
|
|
||||||
|
|
||||||
fclose(hfp);
|
fclose(hfp);
|
||||||
fclose(sfp);
|
fclose(sfp);
|
||||||
|
17
qmic.h
17
qmic.h
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
|
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
@ -71,13 +73,14 @@ extern struct list_head qmi_consts;
|
|||||||
extern struct list_head qmi_messages;
|
extern struct list_head qmi_messages;
|
||||||
extern struct list_head qmi_structs;
|
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 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
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user