From 177ac0ca8565e2f1f25a1b6dbf3f0db009815d14 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Thu, 6 Jul 2023 21:24:53 +0800 Subject: [PATCH] ses 1st commit --- Makefile | 14 ++ main.c | 77 +++++++ scf_def.h | 71 ++++++ scf_eda.pb-c.c | 609 +++++++++++++++++++++++++++++++++++++++++++++++++ scf_eda.pb-c.h | 227 ++++++++++++++++++ scf_eda.proto | 43 ++++ scf_eda_pb.c | 407 +++++++++++++++++++++++++++++++++ scf_eda_pb.h | 68 ++++++ 8 files changed, 1516 insertions(+) create mode 100644 Makefile create mode 100644 main.c create mode 100644 scf_def.h create mode 100644 scf_eda.pb-c.c create mode 100644 scf_eda.pb-c.h create mode 100644 scf_eda.proto create mode 100644 scf_eda_pb.c create mode 100644 scf_eda_pb.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4300770 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CFILES += main.c +CFILES += scf_eda.pb-c.c +CFILES += scf_eda_pb.c + +CFLAGS += -g +CFLAGS += -I./ + +LDFLAGS += -lprotobuf-c + +all: + gcc $(CFLAGS) $(CFILES) $(LDFLAGS) + +clean: + rm *.o diff --git a/main.c b/main.c new file mode 100644 index 0000000..351b568 --- /dev/null +++ b/main.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include"scf_eda_pb.h" +#include"scf_def.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + scf_loge("./a.out 1.pb\n"); + return -EINVAL; + } + + FILE* fp = fopen(argv[1], "rb"); + if (!fp) { + scf_loge("fopen '%s' error\n", argv[1]); + return -EINVAL; + } + + fseek(fp, 0, SEEK_END); + + long len = ftell(fp); + if (len < 0) { + scf_loge("pb len: %ld\n", len); + return -EINVAL; + } + + uint8_t* pb = malloc(len); + if (!pb) { + scf_loge("malloc error\n"); + return -ENOMEM; + } + + fseek(fp, 0, SEEK_SET); + + if (fread(pb, 1, len, fp) != len) { + scf_loge("fread error\n"); + return -ENOMEM; + } + + fclose(fp); + fp = NULL; + + ScfEboard* p = scf_eboard__unpack(NULL, len, pb); + + size_t i; + size_t j; + size_t k; + size_t l; + + for (i = 0; i < p->n_functions; i++) { + ScfEfunction* pf = p->functions[i]; + + printf("f: %s\n", pf->name); + + for (l = 0; l < pf->n_components; l++) { + ScfEcomponent* pc = pf->components[l]; + + printf("i: %ld, pc: %p, id: %ld, cid: %ld, n_pins: %ld\n", i, pc, pc->id, pc->type, pc->n_pins); + + for (j = 0; j < pc->n_pins; j++) { + ScfEpin* pp = pc->pins[j]; + + printf("j: %ld, pp: %p, n_tos: %ld\n", j, pp, pp->n_tos); + + for (k = 0; k + 1 < pp->n_tos; k += 2) { + printf("k: %ld, cid: %ld, pid: %ld\n", k, pp->tos[k], pp->tos[k + 1]); + } + } + printf("\n"); + } + printf("\n\n"); + } + + scf_eboard__free_unpacked(p, NULL); + return 0; +} diff --git a/scf_def.h b/scf_def.h new file mode 100644 index 0000000..707a2a8 --- /dev/null +++ b/scf_def.h @@ -0,0 +1,71 @@ +#ifndef SCF_DEF_H +#define SCF_DEF_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if 1 +#include +static inline int64_t gettime() +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + return tv.tv_sec * 1000000LL + tv.tv_usec; +} +#endif + +// sign-extend low 'src_bits' of src to 64 bits +static inline uint64_t scf_sign_extend(uint64_t src, int src_bits) +{ + uint64_t sign = src >> (src_bits - 1) & 0x1; + uint64_t mask = (~sign + 1) << (src_bits - 1); + + src |= mask; + return src; +} + +// zero-extend low 'src_bits' of src to 64 bits +static inline uint64_t scf_zero_extend(uint64_t src, int src_bits) +{ + uint64_t mask = (1ULL << src_bits) - 1; + + src &= mask; + return src; +} + +#define SCF_XCHG(x, y) \ + do {\ + typeof(x) tmp = x; \ + x = y; \ + y = tmp; \ + } while (0) + +#ifdef SCF_DEBUG +#define scf_logd(fmt, ...) printf("%s(), %d, "fmt, __func__, __LINE__, ##__VA_ARGS__) +#else +#define scf_logd(fmt, ...) +#endif + +#define scf_logi(fmt, ...) printf("%s(), %d, info: "fmt, __func__, __LINE__, ##__VA_ARGS__) + +#define scf_loge(fmt, ...) printf("%s(), %d, \033[31m error:\033[0m "fmt, __func__, __LINE__, ##__VA_ARGS__) +#define scf_logw(fmt, ...) printf("%s(), %d, \033[33m warning:\033[0m "fmt, __func__, __LINE__, ##__VA_ARGS__) + +#define SCF_CHECK_ERROR(cond, ret, fmt, ...) \ + do { \ + if (cond) { \ + printf("%s(), %d, \033[31m error:\033[0m "fmt, __func__, __LINE__, ##__VA_ARGS__); \ + return ret; \ + } \ + } while (0) + +#endif + diff --git a/scf_eda.pb-c.c b/scf_eda.pb-c.c new file mode 100644 index 0000000..190f5b8 --- /dev/null +++ b/scf_eda.pb-c.c @@ -0,0 +1,609 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: scf_eda.proto */ + +/* Do not generate deprecated warnings for self */ +#ifndef PROTOBUF_C__NO_DEPRECATED +#define PROTOBUF_C__NO_DEPRECATED +#endif + +#include "scf_eda.pb-c.h" +void scf_eline__init + (ScfEline *message) +{ + static ScfEline init_value = SCF_ELINE__INIT; + *message = init_value; +} +size_t scf_eline__get_packed_size + (const ScfEline *message) +{ + assert(message->base.descriptor == &scf_eline__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t scf_eline__pack + (const ScfEline *message, + uint8_t *out) +{ + assert(message->base.descriptor == &scf_eline__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t scf_eline__pack_to_buffer + (const ScfEline *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &scf_eline__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +ScfEline * + scf_eline__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (ScfEline *) + protobuf_c_message_unpack (&scf_eline__descriptor, + allocator, len, data); +} +void scf_eline__free_unpacked + (ScfEline *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &scf_eline__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void scf_epin__init + (ScfEpin *message) +{ + static ScfEpin init_value = SCF_EPIN__INIT; + *message = init_value; +} +size_t scf_epin__get_packed_size + (const ScfEpin *message) +{ + assert(message->base.descriptor == &scf_epin__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t scf_epin__pack + (const ScfEpin *message, + uint8_t *out) +{ + assert(message->base.descriptor == &scf_epin__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t scf_epin__pack_to_buffer + (const ScfEpin *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &scf_epin__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +ScfEpin * + scf_epin__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (ScfEpin *) + protobuf_c_message_unpack (&scf_epin__descriptor, + allocator, len, data); +} +void scf_epin__free_unpacked + (ScfEpin *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &scf_epin__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void scf_ecomponent__init + (ScfEcomponent *message) +{ + static ScfEcomponent init_value = SCF_ECOMPONENT__INIT; + *message = init_value; +} +size_t scf_ecomponent__get_packed_size + (const ScfEcomponent *message) +{ + assert(message->base.descriptor == &scf_ecomponent__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t scf_ecomponent__pack + (const ScfEcomponent *message, + uint8_t *out) +{ + assert(message->base.descriptor == &scf_ecomponent__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t scf_ecomponent__pack_to_buffer + (const ScfEcomponent *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &scf_ecomponent__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +ScfEcomponent * + scf_ecomponent__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (ScfEcomponent *) + protobuf_c_message_unpack (&scf_ecomponent__descriptor, + allocator, len, data); +} +void scf_ecomponent__free_unpacked + (ScfEcomponent *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &scf_ecomponent__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void scf_efunction__init + (ScfEfunction *message) +{ + static ScfEfunction init_value = SCF_EFUNCTION__INIT; + *message = init_value; +} +size_t scf_efunction__get_packed_size + (const ScfEfunction *message) +{ + assert(message->base.descriptor == &scf_efunction__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t scf_efunction__pack + (const ScfEfunction *message, + uint8_t *out) +{ + assert(message->base.descriptor == &scf_efunction__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t scf_efunction__pack_to_buffer + (const ScfEfunction *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &scf_efunction__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +ScfEfunction * + scf_efunction__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (ScfEfunction *) + protobuf_c_message_unpack (&scf_efunction__descriptor, + allocator, len, data); +} +void scf_efunction__free_unpacked + (ScfEfunction *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &scf_efunction__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void scf_eboard__init + (ScfEboard *message) +{ + static ScfEboard init_value = SCF_EBOARD__INIT; + *message = init_value; +} +size_t scf_eboard__get_packed_size + (const ScfEboard *message) +{ + assert(message->base.descriptor == &scf_eboard__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t scf_eboard__pack + (const ScfEboard *message, + uint8_t *out) +{ + assert(message->base.descriptor == &scf_eboard__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t scf_eboard__pack_to_buffer + (const ScfEboard *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &scf_eboard__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +ScfEboard * + scf_eboard__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (ScfEboard *) + protobuf_c_message_unpack (&scf_eboard__descriptor, + allocator, len, data); +} +void scf_eboard__free_unpacked + (ScfEboard *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &scf_eboard__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +static const ProtobufCFieldDescriptor scf_eline__field_descriptors[4] = +{ + { + "x0", + 1, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEline, x0), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "y0", + 2, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEline, y0), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "x1", + 3, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEline, x1), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "y1", + 4, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEline, y1), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned scf_eline__field_indices_by_name[] = { + 0, /* field[0] = x0 */ + 2, /* field[2] = x1 */ + 1, /* field[1] = y0 */ + 3, /* field[3] = y1 */ +}; +static const ProtobufCIntRange scf_eline__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 4 } +}; +const ProtobufCMessageDescriptor scf_eline__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "scf_eline", + "ScfEline", + "ScfEline", + "", + sizeof(ScfEline), + 4, + scf_eline__field_descriptors, + scf_eline__field_indices_by_name, + 1, scf_eline__number_ranges, + (ProtobufCMessageInit) scf_eline__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor scf_epin__field_descriptors[6] = +{ + { + "tos", + 1, + PROTOBUF_C_LABEL_REPEATED, + PROTOBUF_C_TYPE_UINT64, + offsetof(ScfEpin, n_tos), + offsetof(ScfEpin, tos), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "id", + 2, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT64, + 0, /* quantifier_offset */ + offsetof(ScfEpin, id), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "flags", + 3, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT64, + 0, /* quantifier_offset */ + offsetof(ScfEpin, flags), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "x", + 4, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEpin, x), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "y", + 5, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEpin, y), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "lines", + 6, + PROTOBUF_C_LABEL_REPEATED, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(ScfEpin, n_lines), + offsetof(ScfEpin, lines), + &scf_eline__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned scf_epin__field_indices_by_name[] = { + 2, /* field[2] = flags */ + 1, /* field[1] = id */ + 5, /* field[5] = lines */ + 0, /* field[0] = tos */ + 3, /* field[3] = x */ + 4, /* field[4] = y */ +}; +static const ProtobufCIntRange scf_epin__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 6 } +}; +const ProtobufCMessageDescriptor scf_epin__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "scf_epin", + "ScfEpin", + "ScfEpin", + "", + sizeof(ScfEpin), + 6, + scf_epin__field_descriptors, + scf_epin__field_indices_by_name, + 1, scf_epin__number_ranges, + (ProtobufCMessageInit) scf_epin__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor scf_ecomponent__field_descriptors[7] = +{ + { + "id", + 1, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT64, + 0, /* quantifier_offset */ + offsetof(ScfEcomponent, id), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "type", + 2, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT64, + 0, /* quantifier_offset */ + offsetof(ScfEcomponent, type), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "pins", + 3, + PROTOBUF_C_LABEL_REPEATED, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(ScfEcomponent, n_pins), + offsetof(ScfEcomponent, pins), + &scf_epin__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "x", + 4, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEcomponent, x), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "y", + 5, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEcomponent, y), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "w", + 6, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEcomponent, w), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "h", + 7, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(ScfEcomponent, h), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned scf_ecomponent__field_indices_by_name[] = { + 6, /* field[6] = h */ + 0, /* field[0] = id */ + 2, /* field[2] = pins */ + 1, /* field[1] = type */ + 5, /* field[5] = w */ + 3, /* field[3] = x */ + 4, /* field[4] = y */ +}; +static const ProtobufCIntRange scf_ecomponent__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 7 } +}; +const ProtobufCMessageDescriptor scf_ecomponent__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "scf_ecomponent", + "ScfEcomponent", + "ScfEcomponent", + "", + sizeof(ScfEcomponent), + 7, + scf_ecomponent__field_descriptors, + scf_ecomponent__field_indices_by_name, + 1, scf_ecomponent__number_ranges, + (ProtobufCMessageInit) scf_ecomponent__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor scf_efunction__field_descriptors[2] = +{ + { + "name", + 1, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_STRING, + 0, /* quantifier_offset */ + offsetof(ScfEfunction, name), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "components", + 2, + PROTOBUF_C_LABEL_REPEATED, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(ScfEfunction, n_components), + offsetof(ScfEfunction, components), + &scf_ecomponent__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned scf_efunction__field_indices_by_name[] = { + 1, /* field[1] = components */ + 0, /* field[0] = name */ +}; +static const ProtobufCIntRange scf_efunction__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 2 } +}; +const ProtobufCMessageDescriptor scf_efunction__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "scf_efunction", + "ScfEfunction", + "ScfEfunction", + "", + sizeof(ScfEfunction), + 2, + scf_efunction__field_descriptors, + scf_efunction__field_indices_by_name, + 1, scf_efunction__number_ranges, + (ProtobufCMessageInit) scf_efunction__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor scf_eboard__field_descriptors[1] = +{ + { + "functions", + 1, + PROTOBUF_C_LABEL_REPEATED, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(ScfEboard, n_functions), + offsetof(ScfEboard, functions), + &scf_efunction__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned scf_eboard__field_indices_by_name[] = { + 0, /* field[0] = functions */ +}; +static const ProtobufCIntRange scf_eboard__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 1 } +}; +const ProtobufCMessageDescriptor scf_eboard__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "scf_eboard", + "ScfEboard", + "ScfEboard", + "", + sizeof(ScfEboard), + 1, + scf_eboard__field_descriptors, + scf_eboard__field_indices_by_name, + 1, scf_eboard__number_ranges, + (ProtobufCMessageInit) scf_eboard__init, + NULL,NULL,NULL /* reserved[123] */ +}; diff --git a/scf_eda.pb-c.h b/scf_eda.pb-c.h new file mode 100644 index 0000000..53b065e --- /dev/null +++ b/scf_eda.pb-c.h @@ -0,0 +1,227 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: scf_eda.proto */ + +#ifndef PROTOBUF_C_scf_5feda_2eproto__INCLUDED +#define PROTOBUF_C_scf_5feda_2eproto__INCLUDED + +#include + +PROTOBUF_C__BEGIN_DECLS + +#if PROTOBUF_C_VERSION_NUMBER < 1000000 +# error This file was generated by a newer version of protoc-c which is incompatible with your libprotobuf-c headers. Please update your headers. +#elif 1002001 < PROTOBUF_C_MIN_COMPILER_VERSION +# error This file was generated by an older version of protoc-c which is incompatible with your libprotobuf-c headers. Please regenerate this file with a newer version of protoc-c. +#endif + + +typedef struct _ScfEline ScfEline; +typedef struct _ScfEpin ScfEpin; +typedef struct _ScfEcomponent ScfEcomponent; +typedef struct _ScfEfunction ScfEfunction; +typedef struct _ScfEboard ScfEboard; + + +/* --- enums --- */ + + +/* --- messages --- */ + +struct _ScfEline +{ + ProtobufCMessage base; + uint32_t x0; + uint32_t y0; + uint32_t x1; + uint32_t y1; +}; +#define SCF_ELINE__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&scf_eline__descriptor) \ + , 0, 0, 0, 0 } + + +struct _ScfEpin +{ + ProtobufCMessage base; + size_t n_tos; + uint64_t *tos; + uint64_t id; + uint64_t flags; + uint32_t x; + uint32_t y; + size_t n_lines; + ScfEline **lines; +}; +#define SCF_EPIN__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&scf_epin__descriptor) \ + , 0,NULL, 0, 0, 0, 0, 0,NULL } + + +struct _ScfEcomponent +{ + ProtobufCMessage base; + uint64_t id; + uint64_t type; + size_t n_pins; + ScfEpin **pins; + uint32_t x; + uint32_t y; + uint32_t w; + uint32_t h; +}; +#define SCF_ECOMPONENT__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&scf_ecomponent__descriptor) \ + , 0, 0, 0,NULL, 0, 0, 0, 0 } + + +struct _ScfEfunction +{ + ProtobufCMessage base; + char *name; + size_t n_components; + ScfEcomponent **components; +}; +#define SCF_EFUNCTION__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&scf_efunction__descriptor) \ + , NULL, 0,NULL } + + +struct _ScfEboard +{ + ProtobufCMessage base; + size_t n_functions; + ScfEfunction **functions; +}; +#define SCF_EBOARD__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&scf_eboard__descriptor) \ + , 0,NULL } + + +/* ScfEline methods */ +void scf_eline__init + (ScfEline *message); +size_t scf_eline__get_packed_size + (const ScfEline *message); +size_t scf_eline__pack + (const ScfEline *message, + uint8_t *out); +size_t scf_eline__pack_to_buffer + (const ScfEline *message, + ProtobufCBuffer *buffer); +ScfEline * + scf_eline__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void scf_eline__free_unpacked + (ScfEline *message, + ProtobufCAllocator *allocator); +/* ScfEpin methods */ +void scf_epin__init + (ScfEpin *message); +size_t scf_epin__get_packed_size + (const ScfEpin *message); +size_t scf_epin__pack + (const ScfEpin *message, + uint8_t *out); +size_t scf_epin__pack_to_buffer + (const ScfEpin *message, + ProtobufCBuffer *buffer); +ScfEpin * + scf_epin__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void scf_epin__free_unpacked + (ScfEpin *message, + ProtobufCAllocator *allocator); +/* ScfEcomponent methods */ +void scf_ecomponent__init + (ScfEcomponent *message); +size_t scf_ecomponent__get_packed_size + (const ScfEcomponent *message); +size_t scf_ecomponent__pack + (const ScfEcomponent *message, + uint8_t *out); +size_t scf_ecomponent__pack_to_buffer + (const ScfEcomponent *message, + ProtobufCBuffer *buffer); +ScfEcomponent * + scf_ecomponent__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void scf_ecomponent__free_unpacked + (ScfEcomponent *message, + ProtobufCAllocator *allocator); +/* ScfEfunction methods */ +void scf_efunction__init + (ScfEfunction *message); +size_t scf_efunction__get_packed_size + (const ScfEfunction *message); +size_t scf_efunction__pack + (const ScfEfunction *message, + uint8_t *out); +size_t scf_efunction__pack_to_buffer + (const ScfEfunction *message, + ProtobufCBuffer *buffer); +ScfEfunction * + scf_efunction__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void scf_efunction__free_unpacked + (ScfEfunction *message, + ProtobufCAllocator *allocator); +/* ScfEboard methods */ +void scf_eboard__init + (ScfEboard *message); +size_t scf_eboard__get_packed_size + (const ScfEboard *message); +size_t scf_eboard__pack + (const ScfEboard *message, + uint8_t *out); +size_t scf_eboard__pack_to_buffer + (const ScfEboard *message, + ProtobufCBuffer *buffer); +ScfEboard * + scf_eboard__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void scf_eboard__free_unpacked + (ScfEboard *message, + ProtobufCAllocator *allocator); +/* --- per-message closures --- */ + +typedef void (*ScfEline_Closure) + (const ScfEline *message, + void *closure_data); +typedef void (*ScfEpin_Closure) + (const ScfEpin *message, + void *closure_data); +typedef void (*ScfEcomponent_Closure) + (const ScfEcomponent *message, + void *closure_data); +typedef void (*ScfEfunction_Closure) + (const ScfEfunction *message, + void *closure_data); +typedef void (*ScfEboard_Closure) + (const ScfEboard *message, + void *closure_data); + +/* --- services --- */ + + +/* --- descriptors --- */ + +extern const ProtobufCMessageDescriptor scf_eline__descriptor; +extern const ProtobufCMessageDescriptor scf_epin__descriptor; +extern const ProtobufCMessageDescriptor scf_ecomponent__descriptor; +extern const ProtobufCMessageDescriptor scf_efunction__descriptor; +extern const ProtobufCMessageDescriptor scf_eboard__descriptor; + +PROTOBUF_C__END_DECLS + + +#endif /* PROTOBUF_C_scf_5feda_2eproto__INCLUDED */ diff --git a/scf_eda.proto b/scf_eda.proto new file mode 100644 index 0000000..78e95fb --- /dev/null +++ b/scf_eda.proto @@ -0,0 +1,43 @@ +syntax="proto2"; + +message scf_eline +{ + required uint32 x0 = 1; + required uint32 y0 = 2; + required uint32 x1 = 3; + required uint32 y1 = 4; +} + +message scf_epin +{ + repeated uint64 tos = 1; + required uint64 id = 2; + required uint64 flags = 3; + + required uint32 x = 4; + required uint32 y = 5; + repeated scf_eline lines = 6; +} + +message scf_ecomponent +{ + required uint64 id = 1; + required uint64 type = 2; + repeated scf_epin pins = 3; + + required uint32 x = 4; + required uint32 y = 5; + required uint32 w = 6; + required uint32 h = 7; +} + +message scf_efunction +{ + required string name = 1; + repeated scf_ecomponent components = 2; +} + +message scf_eboard +{ + repeated scf_efunction functions = 1; +} diff --git a/scf_eda_pb.c b/scf_eda_pb.c new file mode 100644 index 0000000..2791ce3 --- /dev/null +++ b/scf_eda_pb.c @@ -0,0 +1,407 @@ +#include "scf_eda_pb.h" +#include "scf_def.h" + +static int component_pins[SCF_EDA_Components_NB] = +{ + 0, // None + SCF_EDA_Battery_NB, // SCF_EDA_Battery + + 2, // SCF_EDA_Resistor + 2, // SCF_EDA_Capacitor + 2, // SCF_EDA_Inductor + + SCF_EDA_Diode_NB, + SCF_EDA_Transistor_NB, +}; + +ScfEline* scf_eline__alloc() +{ + ScfEline* l = malloc(sizeof(ScfEline)); + if (!l) + return NULL; + + scf_eline__init(l); + return l; +} + +void scf_eline__free(ScfEline* l) +{ + if (l) + free(l); +} + +ScfEpin* scf_epin__alloc() +{ + ScfEpin* pin = malloc(sizeof(ScfEpin)); + if (!pin) + return NULL; + + scf_epin__init(pin); + return pin; +} + +int scf_epin__add_component(ScfEpin* pin, uint64_t cid, uint64_t pid) +{ + if (!pin) + return -EINVAL; + + for (size_t i = 0; i + 1 < pin->n_tos; i += 2) { + + if (pin->tos[i] == cid && pin->tos[i + 1] == pid) + return 0; + } + + uint64_t* p = realloc(pin->tos, sizeof(uint64_t) * (pin->n_tos + 2)); + if (!p) + return -ENOMEM; + + pin->tos = p; + pin->tos[pin->n_tos++] = cid; + pin->tos[pin->n_tos++] = pid; + return 0; +} + +int scf_epin__del_component(ScfEpin* pin, uint64_t cid, uint64_t pid) +{ + if (!pin) + return -EINVAL; + + uint64_t* p; + size_t i; + size_t j; + + for (i = 0; i + 1 < pin->n_tos; i += 2) { + + if (pin->tos[i] == cid && pin->tos[i + 1] == pid) { + + for (j = i + 2; j < pin->n_tos; j++) + pin->tos[j - 2] = pin->tos[j]; + + pin->n_tos -= 2; + + p = realloc(pin->tos, sizeof(uint64_t) * pin->n_tos); + if (p) + pin->tos = p; + return 0; + } + } + + return -EINVAL; +} + +int scf_epin__add_line(ScfEpin* pin, ScfEline* l) +{ + if (!pin || !l) + return -EINVAL; + + for (size_t i = 0; i < pin->n_lines; i++) { + + if (pin->lines[i]->x0 == l->x0 + && pin->lines[i]->y0 == l->y0 + && pin->lines[i]->x1 == l->x1 + && pin->lines[i]->y1 == l->y1) + return 0; + } + + void* p = realloc(pin->lines, sizeof(ScfEline*) * (pin->n_lines + 1)); + if (!p) + return -ENOMEM; + + pin->lines = p; + pin->lines[pin->n_lines++] = l; + return 0; +} + +int scf_epin__del_line(ScfEpin* pin, ScfEline* l) +{ + if (!pin || !l) + return -EINVAL; + + void* p; + size_t i; + size_t j; + size_t n = pin->n_lines; + + for (i = 0; i < pin->n_lines; ) { + + if (pin->lines[i]->x0 == l->x0 + && pin->lines[i]->y0 == l->y0 + && pin->lines[i]->x1 == l->x1 + && pin->lines[i]->y1 == l->y1) { + + for (j = i + 1; j < pin->n_lines; j++) + pin->lines[j - 1] = pin->lines[j]; + + pin->n_lines--; + } else + i++; + } + + if (pin->n_lines < n) { + p = realloc(pin->lines, sizeof(ScfEline*) * pin->n_lines); + if (p) + pin->lines = p; + } + + return 0; +} + +void scf_epin__free(ScfEpin* pin) +{ + if (pin) { + scf_logd("pin: %p\n", pin); + + if (pin->lines) { + size_t i; + + for (i = 0; i < pin->n_lines; i++) + scf_eline__free(pin->lines[i]); + + free(pin->lines); + } + + if (pin->tos) + free(pin->tos); + free(pin); + } +} + +ScfEcomponent* scf_ecomponent__alloc(uint64_t type) +{ + if (type >= SCF_EDA_Components_NB) + return NULL; + + ScfEcomponent* c = malloc(sizeof(ScfEcomponent)); + if (!c) + return NULL; + + scf_ecomponent__init(c); + + c->type = type; + + int i; + for (i = 0; i < component_pins[type]; i++) { + + ScfEpin* pin = scf_epin__alloc(); + if (!pin) { + scf_ecomponent__free(c); + return NULL; + } + + pin->id = i; + + if (scf_ecomponent__add_pin(c, pin) < 0) { + scf_ecomponent__free(c); + scf_epin__free(pin); + return NULL; + } + } + + return c; +} + +int scf_ecomponent__add_pin(ScfEcomponent* c, ScfEpin* pin) +{ + if (!c || !pin) + return -EINVAL; + + void* p = realloc(c->pins, sizeof(ScfEpin*) * (c->n_pins + 1)); + if (!p) + return -ENOMEM; + + c->pins = p; + c->pins[c->n_pins++] = pin; + return 0; +} + +int scf_ecomponent__del_pin(ScfEcomponent* c, ScfEpin* pin) +{ + if (!c) + return -EINVAL; + + size_t i; + size_t j; + void* p; + + for (i = 0; i < c->n_pins; i++) { + + if (c->pins[i] == pin) { + + for (j = i + 1; j < c->n_pins; j++) + c->pins[j - 1] = c->pins[j]; + + c->n_pins--; + + p = realloc(c->pins, sizeof(ScfEpin*) * c->n_pins); + if (p) + c->pins = p; + return 0; + } + } + + return -EINVAL; +} + +void scf_ecomponent__free(ScfEcomponent* c) +{ + if (c) { + scf_logd("c: %ld\n", c->id); + + if (c->pins) { + size_t i; + + for (i = 0; i < c->n_pins; i++) + scf_epin__free(c->pins[i]); + + free(c->pins); + } + + free(c); + } +} + +ScfEfunction* scf_efunction__alloc(const char* name) +{ + ScfEfunction* f = malloc(sizeof(ScfEfunction)); + if (!f) + return NULL; + + scf_efunction__init(f); + + f->name = strdup(name); + if (!f->name) { + free(f); + return NULL; + } + + return f; +} + +int scf_efunction__add_component(ScfEfunction* f, ScfEcomponent* c) +{ + if (!f || !c) + return -EINVAL; + + void* p = realloc(f->components, sizeof(ScfEcomponent*) * (f->n_components + 1)); + if (!p) + return -ENOMEM; + + f->components = p; + f->components[f->n_components++] = c; + return 0; +} + +int scf_efunction__del_component(ScfEfunction* f, ScfEcomponent* c) +{ + if (!f) + return -EINVAL; + + size_t i; + size_t j; + void* p; + + for (i = 0; i < f->n_components; i++) { + + if (f->components[i] == c) { + + for (j = i + 1; j < f->n_components; j++) + f->components[j - 1] = f->components[j]; + + f->n_components--; + + p = realloc(f->components, sizeof(ScfEfunction*) * f->n_components); + if (p) + f->components = p; + return 0; + } + } + + return -EINVAL; +} + +void scf_efunction__free(ScfEfunction* f) +{ + if (f) { + scf_logd("f: %s\n", f->name); + + if (f->components) { + size_t i; + + for (i = 0; i < f->n_components; i++) + scf_ecomponent__free(f->components[i]); + + free(f->components); + } + + free(f->name); + free(f); + } +} + +ScfEboard* scf_eboard__alloc() +{ + ScfEboard* b = malloc(sizeof(ScfEboard)); + if (!b) + return NULL; + + scf_eboard__init(b); + return b; +} + +int scf_eboard__add_function(ScfEboard* b, ScfEfunction* f) +{ + if (!b || !f) + return -EINVAL; + + void* p = realloc(b->functions, sizeof(ScfEfunction*) * (b->n_functions + 1)); + if (!p) + return -ENOMEM; + + b->functions = p; + b->functions[b->n_functions++] = f; + return 0; +} + +int scf_eboard__del_function(ScfEboard* b, ScfEfunction* f) +{ + if (!b) + return -EINVAL; + + size_t i; + size_t j; + void* p; + + for (i = 0; i < b->n_functions; i++) { + + if (b->functions[i] == f) { + + for (j = i + 1; j < b->n_functions; j++) + b->functions[j - 1] = b->functions[j]; + + b->n_functions--; + + p = realloc(b->functions, sizeof(ScfEfunction*) * b->n_functions); + if (p) + b->functions = p; + return 0; + } + } + + return -EINVAL; +} + +void scf_eboard__free(ScfEboard* b) +{ + if (b) { + if (b->functions) { + size_t i; + + for (i = 0; i < b->n_functions; i++) + scf_efunction__free(b->functions[i]); + + free(b->functions); + } + + free(b); + } +} diff --git a/scf_eda_pb.h b/scf_eda_pb.h new file mode 100644 index 0000000..739e566 --- /dev/null +++ b/scf_eda_pb.h @@ -0,0 +1,68 @@ +#ifndef SCF_EDA_PB_H +#define SCF_EDA_PB_H + +#include "scf_eda.pb-c.h" + +enum { + SCF_EDA_None, + SCF_EDA_Battery, + + SCF_EDA_Resistor, + SCF_EDA_Capacitor, + SCF_EDA_Inductor, + + SCF_EDA_Diode, + SCF_EDA_Transistor, + + SCF_EDA_Components_NB, +}; + +#define SCF_EDA_PIN_NONE 0 +#define SCF_EDA_PIN_IN 1 +#define SCF_EDA_PIN_OUT 2 + +enum { + SCF_EDA_Battery_NEG, + SCF_EDA_Battery_POS, + SCF_EDA_Battery_NB, +}; + +enum { + SCF_EDA_Diode_NEG, + SCF_EDA_Diode_POS, + SCF_EDA_Diode_NB, +}; + +enum { + SCF_EDA_Transistor_B, + SCF_EDA_Transistor_C, + SCF_EDA_Transistor_E, + SCF_EDA_Transistor_NB, +}; + +ScfEline* scf_eline__alloc(); +void scf_eline__free (ScfEline* l); + +ScfEpin* scf_epin__alloc(); +int scf_epin__add_component(ScfEpin* pin, uint64_t cid, uint64_t pid); +int scf_epin__del_component(ScfEpin* pin, uint64_t cid, uint64_t pid); +int scf_epin__add_line (ScfEpin* pin, ScfEline* l); +int scf_epin__del_line (ScfEpin* pin, ScfEline* l); +void scf_epin__free (ScfEpin* pin); + +ScfEcomponent* scf_ecomponent__alloc(uint64_t type); +int scf_ecomponent__add_pin(ScfEcomponent* c, ScfEpin* pin); +int scf_ecomponent__del_pin(ScfEcomponent* c, ScfEpin* pin); +void scf_ecomponent__free (ScfEcomponent* c); + +ScfEfunction* scf_efunction__alloc (const char* name); +int scf_efunction__add_component(ScfEfunction* f, ScfEcomponent* c); +int scf_efunction__del_component(ScfEfunction* f, ScfEcomponent* c); +void scf_efunction__free (ScfEfunction* f); + +ScfEboard* scf_eboard__alloc(); +int scf_eboard__add_function(ScfEboard* b, ScfEfunction* f); +int scf_eboard__del_function(ScfEboard* b, ScfEfunction* f); +void scf_eboard__free (ScfEboard* b); + +#endif -- 2.25.1