mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-22 10:55:56 +08:00
更新硬件SDK
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2022 OpenLuat & AirM2M
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "common_api.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_mem.h"
|
||||
#include "luat_debug.h"
|
||||
|
||||
luat_rtos_task_handle task_handle;
|
||||
|
||||
#include "user.pb.h"
|
||||
#include "pb_encode.h"
|
||||
#include "pb_decode.h"
|
||||
|
||||
#define DATA_BUFFER_SIZE 1024
|
||||
|
||||
static int pack_user_data(uint8_t *buffer)
|
||||
{
|
||||
//Initialize the UserInformation structure;
|
||||
UserInformation userInfo = UserInformation_init_zero;
|
||||
|
||||
// Initialize buffer
|
||||
memset(buffer, 0, DATA_BUFFER_SIZE);
|
||||
|
||||
strcpy(userInfo.name, "LuatOS-Hello");
|
||||
userInfo.age = 18;
|
||||
strcpy(userInfo.phone, "0110-12345678");
|
||||
userInfo.stat = UserStatus_IDLE;
|
||||
strcpy(userInfo.email, "luatcsdk@123.com");
|
||||
|
||||
// encode userInfo data
|
||||
pb_ostream_t enc_stream;
|
||||
enc_stream = pb_ostream_from_buffer(buffer, DATA_BUFFER_SIZE);
|
||||
if (!pb_encode(&enc_stream, UserInformation_fields, &userInfo))
|
||||
{
|
||||
//encode error happened
|
||||
LUAT_DEBUG_PRINT("pb encode error in [%s]\n",PB_GET_ERROR(&enc_stream));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return enc_stream.bytes_written;
|
||||
}
|
||||
|
||||
static int unpack_user_data(const uint8_t *buffer, size_t len)
|
||||
{
|
||||
UserInformation userInfo;
|
||||
|
||||
// decode userInfo data
|
||||
pb_istream_t dec_stream;
|
||||
dec_stream = pb_istream_from_buffer(buffer, len);
|
||||
if (!pb_decode(&dec_stream, UserInformation_fields, &userInfo))
|
||||
{
|
||||
LUAT_DEBUG_PRINT("pb decode error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
LUAT_DEBUG_PRINT("Unpack: name: %s | age: %d | phone: %s | email: %s\n", userInfo.name, userInfo.age, userInfo.phone, userInfo.email);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void demo_init_protobuf()
|
||||
{
|
||||
LUAT_DEBUG_PRINT("==================protobuf is running==================");
|
||||
uint8_t buffer[DATA_BUFFER_SIZE];
|
||||
|
||||
int lenght = pack_user_data(buffer);
|
||||
if(lenght<0){
|
||||
LUAT_DEBUG_PRINT("main: pack_user_data is fail!!!\n");
|
||||
return -1;
|
||||
}
|
||||
LUAT_DEBUG_PRINT("User data len: %d\n",lenght);
|
||||
unpack_user_data(buffer, lenght);
|
||||
|
||||
}
|
||||
|
||||
static void task(void *param)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
demo_init_protobuf();
|
||||
luat_rtos_task_sleep(1000);
|
||||
LUAT_DEBUG_PRINT("==================protobuf is done==================");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void task_demoE_init(void)
|
||||
{
|
||||
luat_rtos_task_create(&task_handle, 2*1024, 50, "task", task, NULL, 0);
|
||||
}
|
||||
|
||||
//启动task_demoE_init,启动位置任务1级
|
||||
INIT_TASK_EXPORT(task_demoE_init, "1");
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "user.pb.h"
|
||||
|
||||
/* @@protoc_insertion_point(includes) */
|
||||
#if PB_PROTO_HEADER_VERSION != 30
|
||||
#error Regenerate this file with the current version of nanopb generator.
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
const pb_field_t UserInformation_fields[6] = {
|
||||
PB_FIELD( 1, STRING , SINGULAR, STATIC , FIRST, UserInformation, name, name, 0),
|
||||
PB_FIELD( 2, UINT32 , SINGULAR, STATIC , OTHER, UserInformation, age, name, 0),
|
||||
PB_FIELD( 3, STRING , SINGULAR, STATIC , OTHER, UserInformation, phone, age, 0),
|
||||
PB_FIELD( 4, UENUM , SINGULAR, STATIC , OTHER, UserInformation, stat, phone, 0),
|
||||
PB_FIELD( 5, STRING , SINGULAR, STATIC , OTHER, UserInformation, email, stat, 0),
|
||||
PB_LAST_FIELD
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <pb.h>
|
||||
|
||||
#if PB_PROTO_HEADER_VERSION != 30
|
||||
#error Regenerate this file with the current version of nanopb generator.
|
||||
#endif
|
||||
|
||||
|
||||
/* Enum definitions */
|
||||
typedef enum _UserStatus {
|
||||
UserStatus_UNKNOWN = 0,
|
||||
UserStatus_IDLE = 1,
|
||||
UserStatus_BUSY = 2
|
||||
} UserStatus;
|
||||
#define _UserStatus_MIN UserStatus_UNKNOWN
|
||||
#define _UserStatus_MAX UserStatus_BUSY
|
||||
#define _UserStatus_ARRAYSIZE ((UserStatus)(UserStatus_BUSY+1))
|
||||
|
||||
/* Struct definitions */
|
||||
typedef struct _UserInformation {
|
||||
char name[20];
|
||||
uint32_t age;
|
||||
char phone[16];
|
||||
UserStatus stat;
|
||||
char email[30];
|
||||
/* @@protoc_insertion_point(struct:UserInformation) */
|
||||
} UserInformation;
|
||||
|
||||
/* Default values for struct fields */
|
||||
|
||||
/* Initializer values for message structs */
|
||||
#define UserInformation_init_default {"", 0, "", _UserStatus_MIN, ""}
|
||||
#define UserInformation_init_zero {"", 0, "", _UserStatus_MIN, ""}
|
||||
|
||||
/* Field tags (for use in manual encoding/decoding) */
|
||||
#define UserInformation_name_tag 1
|
||||
#define UserInformation_age_tag 2
|
||||
#define UserInformation_phone_tag 3
|
||||
#define UserInformation_stat_tag 4
|
||||
#define UserInformation_email_tag 5
|
||||
|
||||
/* Struct field encoding specification for nanopb */
|
||||
extern const pb_field_t UserInformation_fields[6];
|
||||
|
||||
/* Maximum encoded size of messages (where known) */
|
||||
#define UserInformation_size 80
|
||||
|
||||
/* Message IDs (where set with "msgid" option) */
|
||||
#ifdef PB_MSGID
|
||||
|
||||
#define USERINFORMATION_MESSAGES \
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,16 @@
|
||||
syntax = "proto3";
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
enum UserStatus {
|
||||
UNKNOWN = 0;
|
||||
IDLE = 1;
|
||||
BUSY = 2;
|
||||
}
|
||||
|
||||
message UserInformation {
|
||||
string name = 1;
|
||||
uint32 age = 2;
|
||||
string phone = 3;
|
||||
UserStatus stat = 4;
|
||||
string email = 5;
|
||||
}
|
||||
Reference in New Issue
Block a user