This commit is contained in:
刘可亮
2024-09-30 17:06:01 +08:00
parent 2ce4d6bb89
commit 0ef85b55da
9363 changed files with 843557 additions and 428518 deletions

54
packages/third-party/protobuf-c/Kconfig vendored Normal file
View File

@@ -0,0 +1,54 @@
# Kconfig file for package protobuf-c
menuconfig LPKG_USING_PROTOBUF_C
bool "protobuf-c: a C implementation of the Google Protocol Buffers data serialization format on RT-Thread"
default n
if LPKG_USING_PROTOBUF_C
config LPKG_PROTOBUF_C_PATH
string
default "/packages/iot/protobuf-c"
config PROTOBUF_C_USING_EXAMPLE
bool "protobuf-c examples"
help
protobuf-c examples
default n
if PROTOBUF_C_USING_EXAMPLE
config PROTOBUF_C_USING_ENCODE_DECODE_EXAMPLE
bool "protobuf-c example: encode and decode in memory"
help
protobuf-c example: encode and decode in memory
default n
config PROTOBUF_C_USING_ENCODE_DECODE_FILE_EXAMPLE
select RT_USING_DFS if RT_VER_NUM < 0x40100
select RT_USING_POSIX_FS if RT_VER_NUM >= 0x40100
bool "protobuf-c example: encode to file and decode from file"
help
protobuf-c example: encode to file and decode from file
default n
endif
choice
prompt "Version"
default LPKG_USING_PROTOBUF_C_LATEST_VERSION
help
Select the package version
config LPKG_USING_PROTOBUF_C_V100
bool "v1.0.0"
config LPKG_USING_PROTOBUF_C_LATEST_VERSION
bool "latest"
endchoice
config LPKG_PROTOBUF_C_VER
string
default "v1.0.0" if LPKG_USING_PROTOBUF_C_V100
default "latest" if LPKG_USING_PROTOBUF_C_LATEST_VERSION
config LPKG_PROTOBUF_C_VER_NUM
hex
default 0x10000 if LPKG_USING_PROTOBUF_C_V100
default 0x99999 if LPKG_USING_PROTOBUF_C_LATEST_VERSION
endif

32
packages/third-party/protobuf-c/LICENSE vendored Normal file
View File

@@ -0,0 +1,32 @@
Copyright (c) 2008-2016, Dave Benson and the protobuf-c authors.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The code generated by the protoc-gen-c code generator and by the
protoc-c compiler is owned by the owner of the input files used when
generating it. This code is not standalone and requires a support
library to be linked with it. This support library is covered by the
above license.

View File

@@ -0,0 +1,39 @@
# protobuf-c
## 1、介绍
Google 的 prototol buffer 一种轻便高效的数据存储格式,可以很方便地把结构化的数据用二进制的格式保存起来,很适合高效地数据存储和传输。
## 2、项目说明
| 名称 | 说明 |
| -------- | ---------- |
| doc | 文档目录 |
| examples | 例子目录 |
| src | 源代码目录 |
## 3、如何使用 protobuf-c
使用 protobuf-c 需要在 RT-Thread 的包管理器中选择它,具体路径如下:
```
RT-Thread online packages
IoT - internet of things --->
[*] protobuf-c: a C implementation of the Google Protocol Buffers data serialization format on RT-Thread
```
然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。
## 4、注意事项
- 如果使用了 encode_decode_file 例程,需要打开文件系统支持
## 5、感谢
- protobuf-c: [https://github.com/protobuf-c/protobuf-c](https://github.com/protobuf-c/protobuf-c)
## 6、联系方式
- 维护Wu Han
- 主页http://wuhanstudio.cc
- 联系https://github.com/wuhanstudio/protobuf-c/issues

View File

@@ -0,0 +1,27 @@
from building import *
import rtconfig
# get current directory
cwd = GetCurrentDir()
# The set of source files associated with this SConscript file.
src = Glob('src/*.c')
if GetDepend('PROTOBUF_C_USING_EXAMPLE'):
src += Glob('examples/amessage.pb-c.c')
if GetDepend('PROTOBUF_C_USING_ENCODE_DECODE_EXAMPLE'):
src += Glob('examples/amessage_encode_decode.c')
if GetDepend('PROTOBUF_C_USING_ENCODE_DECODE_FILE_EXAMPLE'):
src += Glob('examples/amessage_encode_to_file.c')
src += Glob('examples/amessage_decode_from_file.c')
path = [cwd + '/src']
path += [cwd + '/examples']
LOCAL_CCFLAGS = ''
group = DefineGroup('protobuf-c', src, depend = ['LPKG_USING_PROTOBUF_C'], CPPPATH = path, LOCAL_CCFLAGS = LOCAL_CCFLAGS)
Return('group')

View File

@@ -0,0 +1,103 @@
/* Generated by the protocol buffer compiler. DO NOT EDIT! */
/* Generated from: amessage.proto */
/* Do not generate deprecated warnings for self */
#ifndef PROTOBUF_C__NO_DEPRECATED
#define PROTOBUF_C__NO_DEPRECATED
#endif
#include "amessage.pb-c.h"
void amessage__init
(AMessage *message)
{
static AMessage init_value = AMESSAGE__INIT;
*message = init_value;
}
size_t amessage__get_packed_size
(const AMessage *message)
{
assert(message->base.descriptor == &amessage__descriptor);
return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
}
size_t amessage__pack
(const AMessage *message,
uint8_t *out)
{
assert(message->base.descriptor == &amessage__descriptor);
return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
}
size_t amessage__pack_to_buffer
(const AMessage *message,
ProtobufCBuffer *buffer)
{
assert(message->base.descriptor == &amessage__descriptor);
return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
}
AMessage *
amessage__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data)
{
return (AMessage *)
protobuf_c_message_unpack (&amessage__descriptor,
allocator, len, data);
}
void amessage__free_unpacked
(AMessage *message,
ProtobufCAllocator *allocator)
{
assert(message->base.descriptor == &amessage__descriptor);
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
}
static const ProtobufCFieldDescriptor amessage__field_descriptors[2] =
{
{
"a",
1,
PROTOBUF_C_LABEL_OPTIONAL,
PROTOBUF_C_TYPE_INT32,
offsetof(AMessage, has_a),
offsetof(AMessage, a),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"b",
2,
PROTOBUF_C_LABEL_OPTIONAL,
PROTOBUF_C_TYPE_INT32,
offsetof(AMessage, has_b),
offsetof(AMessage, b),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
};
static const unsigned amessage__field_indices_by_name[] = {
0, /* field[0] = a */
1, /* field[1] = b */
};
static const ProtobufCIntRange amessage__number_ranges[1 + 1] =
{
{ 1, 0 },
{ 0, 2 }
};
const ProtobufCMessageDescriptor amessage__descriptor =
{
PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
"AMessage",
"AMessage",
"AMessage",
"",
sizeof(AMessage),
2,
amessage__field_descriptors,
amessage__field_indices_by_name,
1, amessage__number_ranges,
(ProtobufCMessageInit) amessage__init,
NULL,NULL,NULL /* reserved[123] */
};

View File

@@ -0,0 +1,74 @@
/* Generated by the protocol buffer compiler. DO NOT EDIT! */
/* Generated from: amessage.proto */
#ifndef PROTOBUF_C_amessage_2eproto__INCLUDED
#define PROTOBUF_C_amessage_2eproto__INCLUDED
#include "protobuf-c.h"
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 _AMessage AMessage;
/* --- enums --- */
/* --- messages --- */
struct _AMessage
{
ProtobufCMessage base;
protobuf_c_boolean has_a;
int32_t a;
protobuf_c_boolean has_b;
int32_t b;
};
#define AMESSAGE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&amessage__descriptor) \
, 0,0, 0,0 }
/* AMessage methods */
void amessage__init
(AMessage *message);
size_t amessage__get_packed_size
(const AMessage *message);
size_t amessage__pack
(const AMessage *message,
uint8_t *out);
size_t amessage__pack_to_buffer
(const AMessage *message,
ProtobufCBuffer *buffer);
AMessage *
amessage__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void amessage__free_unpacked
(AMessage *message,
ProtobufCAllocator *allocator);
/* --- per-message closures --- */
typedef void (*AMessage_Closure)
(const AMessage *message,
void *closure_data);
/* --- services --- */
/* --- descriptors --- */
extern const ProtobufCMessageDescriptor amessage__descriptor;
PROTOBUF_C__END_DECLS
#endif /* PROTOBUF_C_amessage_2eproto__INCLUDED */

View File

@@ -0,0 +1,6 @@
syntax = "proto3";
message AMessage {
int32 a=1;
int32 b=2;
}

View File

@@ -0,0 +1,59 @@
#include <rtthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <dfs_posix.h>
#include "amessage.pb-c.h"
static void protobuf_decode_from_file(int argc, char *argv[])
{
rt_kprintf("---- Reading ---\n");
// Get file size first
int ret;
struct stat buf;
ret = stat("/data/amessage.onnx", &buf);
if(ret == 0)
{
rt_kprintf("amessage.onnx file size = %d\n", buf.st_size);
}
else
{
rt_kprintf("amessage.onnx file not fonud\n");
return;
}
// Read protobuf file
size_t msg_len = buf.st_size;
uint8_t buffer[128];
int fd = open("/data/amessage.onnx", O_RDONLY);
if (fd>= 0)
{
int size = read(fd, buffer, msg_len);
close(fd);
rt_kprintf("Read from file amessage.onnx \n");
if (size < 0)
{
return ;
}
}
{
rt_kprintf("---- Decoding ---\n");
AMessage* decode_msg;
decode_msg = amessage__unpack(NULL, msg_len, buffer);
if (decode_msg == NULL)
{
rt_kprintf("Error unpacking incoming message\n");
return;
}
rt_kprintf("Received: a=%d b=%d \n",decode_msg->a, decode_msg->b); // required field
amessage__free_unpacked(decode_msg, NULL);
}
return;
}
MSH_CMD_EXPORT(protobuf_decode_from_file, protobuf-c decode from file);

View File

@@ -0,0 +1,54 @@
#include <rtthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "amessage.pb-c.h"
static void protobuf_encode_decode(int argc, char *argv[])
{
void *buf; // Buffer to store serialized data
unsigned msg_len; // Length of serialized data
if (argc != 3)
{
rt_kprintf("Usage: %s a b\n", argv[0]);
return ;
}
// Encode message to buffer
{
rt_kprintf("---- Encoding ---\n");
AMessage encode_msg = AMESSAGE__INIT;
encode_msg.has_a = 1;
encode_msg.a = atoi(argv[1]);
encode_msg.has_b = 1;
encode_msg.b = atoi(argv[2]);
msg_len = amessage__get_packed_size(&encode_msg);
buf = malloc(msg_len);
rt_kprintf("Encoding %d serialized bytes\n", msg_len);
amessage__pack(&encode_msg, buf);
}
// Unpack the message from buffer.
{
rt_kprintf("---- Decoding ---\n");
AMessage* decode_msg;
decode_msg = amessage__unpack(NULL, msg_len, buf);
if (decode_msg == NULL)
{
rt_kprintf("Error unpacking incoming message\n");
return;
}
rt_kprintf("Received: a=%d b=%d \n",decode_msg->a, decode_msg->b); // required field
amessage__free_unpacked(decode_msg, NULL);
}
free(buf);
return;
}
MSH_CMD_EXPORT(protobuf_encode_decode, protobuf-c encode decode test);

View File

@@ -0,0 +1,49 @@
#include <rtthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <dfs_posix.h>
#include "amessage.pb-c.h"
static void protobuf_encode_to_file(int argc, char *argv[])
{
void *buffer; // Buffer to store serialized data
unsigned msg_len; // Length of serialized data
if (argc != 3)
{
rt_kprintf("Usage: %s a b\n", argv[0]);
return ;
}
// Encode message to buffer
{
rt_kprintf("---- Encoding ---\n");
AMessage encode_msg = AMESSAGE__INIT; // AMessage
encode_msg.has_a = 1;
encode_msg.a = atoi(argv[1]);
encode_msg.has_b = 1;
encode_msg.b = atoi(argv[2]);
msg_len = amessage__get_packed_size(&encode_msg);
buffer = malloc(msg_len);
rt_kprintf("Encoding %d serialized bytes\n", msg_len);
amessage__pack(&encode_msg, buffer);
rt_kprintf("---- Saving ---\n");
int fd = open("/data/amessage.onnx", O_WRONLY | O_CREAT | O_TRUNC);
if (fd>= 0)
{
write(fd, buffer, msg_len);
close(fd);
rt_kprintf("Written to file amessage.onnx.\n");
}
}
free(buffer);
return;
}
MSH_CMD_EXPORT(protobuf_encode_to_file, protobuf-c encode to file test);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff