Compare commits

...

4 Commits

Author SHA1 Message Date
tqcq
fae8944991 feature add assert
Some checks failed
build-crpc / build (Debug, host.toolchain.cmake) (push) Failing after 13m5s
build-crpc / build (Release, host.toolchain.cmake) (push) Failing after 13m9s
2023-12-04 16:23:02 +08:00
tqcq
b2b52861bf fix add type
Some checks failed
build-crpc / build (Debug, host.toolchain.cmake) (push) Failing after 5m20s
build-crpc / build (Release, host.toolchain.cmake) (push) Failing after 7m47s
2023-12-04 14:47:57 +08:00
tqcq
67979897a6 feature update proto 2023-12-04 13:28:23 +08:00
tqcq
055f32c163 feature small fix
Some checks failed
build-crpc / build (Debug, host.toolchain.cmake) (push) Failing after 4m7s
build-crpc / build (Release, host.toolchain.cmake) (push) Failing after 4m21s
2023-12-04 12:27:56 +08:00
17 changed files with 287 additions and 16 deletions

View File

@ -34,6 +34,8 @@ add_executable(crpc
src/plugins/c/rpcs/crpc.cpp
src/plugins/c/c_service_plugin.cpp
${antlr4cpp_src_files_tqcq}
src/plugins/c/items/c_item_message.cpp
src/plugins/c/items/c_item_uint_32.cpp
)
target_include_directories(crpc PRIVATE src/grammar/ src/)
target_include_directories(crpc PRIVATE

View File

@ -35,17 +35,30 @@ public:
message_stack.pop();
}
void exitItemStatement(ProtoParser::ItemStatementContext *context) override
void exitMessageBaseItemStatement(ProtoParser::MessageBaseItemStatementContext *context) override
{
int idx = std::stoi(context->NUMBER()->getText());
tqcq::Item::Ptr item = nullptr;
tqcq::Item::Type item_type = tqcq::Item::Type::kInt32;
if (context->INT32()) {
item = std::make_shared<tqcq::Item>(tqcq::Item::Type::kInt32, context->ID()->getText(), idx);
item_type = tqcq::Item::Type::kInt32;
} else if (context->STRING()){
item = std::make_shared<tqcq::Item>(tqcq::Item::Type::kString, context->ID()->getText(), idx);
item_type = tqcq::Item::Type::kString;
} else if (context->UINT32()) {
item_type = tqcq::Item::Type::kUInt32;
} else {
assert(false);
}
item = std::make_shared<tqcq::Item>(item_type, context->ID()->getText(), idx);
items.push_back(std::move(item));
}
void exitMessageMessageItemStatement(ProtoParser::MessageMessageItemStatementContext *context) override
{
int idx = std::stoi(context->NUMBER()->getText());
auto message_type = context->ID(0)->getText();
auto message_name = context->ID(1)->getText();
auto item = std::make_shared<tqcq::Item>(tqcq::Item::Type::kMessage, message_name, idx);
items.push_back(std::move(item));
}
@ -106,9 +119,9 @@ main(int argc, char *argv[])
Listener listener;
Listener listener2;
antlr4::tree::ParseTreeWalker::DEFAULT.walk(&listener, parser.program());
// for (const auto& item : listener.message_map) {
// std::cout << item.second->ToString() << std::endl;
// }
for (const auto& item : listener.message_map) {
std::cout << item.second->ToString() << std::endl;
}
// for (const auto& service : listener.services) {
// std::cout << service->ToString() << std::endl;

View File

@ -1,6 +1,7 @@
lexer grammar ProtoLexer;
INT32: 'int32';
UINT32: 'uint32';
STRING: 'string';
VERSION2: 'proto2';

View File

@ -6,9 +6,15 @@ program: syntaxStatement baseStatement* EOF;
syntaxStatement: SYNTAX_TYPE '=' '"' (VERSION2 | VERSION3) '"' ';';
baseStatement: messageStatement | serviceStatement | ';' ;
messageStatement: MESSAGE_TYPE ID '{' (itemStatement | ';')* '}';
messageStatement:
MESSAGE_TYPE ID '{' (
messageBaseItemStatement
| ';'
| messageMessageItemStatement
)* '}';
serviceStatement: SERVICE_TYPE ID '{' (rpcStatement | ';')* '}';
itemStatement: (INT32 | STRING) ID '=' NUMBER ';';
messageBaseItemStatement: (INT32 | UINT32 | STRING) ID '=' NUMBER ';';
messageMessageItemStatement: ID ID '=' NUMBER ';';
rpcStatement: RPC_TYPE ID '(' ID ')' 'returns' '(' ID ')' '{' '}';

View File

@ -15,6 +15,7 @@ public:
enum class Type {
kMessage,
kInt32,
kUInt32,
kString
};
static std::string ToString(Type type);

View File

@ -430,4 +430,16 @@ CServiceClientPlugin::GenerateHeaderGuardStart(Service::Ptr service)
return ss.str();
}
std::string
CServiceClientPlugin::GenerateHeaderExamples(Service::Ptr service)
{
return "";
}
std::string
CServiceClientPlugin::GenerateSourceExamples(Service::Ptr service)
{
return "";
}
}// namespace tqcq

View File

@ -39,6 +39,9 @@ protected:
std::string GenerateSourceSendFunctionDefinition(Service::Ptr service);
std::string GenerateSourceDispatchFunctionDefinition(Service::Ptr service);
std::string GenerateHeaderExamples(Service::Ptr service);
std::string GenerateSourceExamples(Service::Ptr service);
};
}// namespace tqcq

View File

@ -38,6 +38,15 @@ std::string
CServiceServerPlugin::GenerateHeaderStructAPIStart(Service::Ptr service)
{
std::stringstream ss;
ss << "/** api examples" << std::endl;
for (const auto& rpc : service->rpcs()) {
auto crpc = CRPCServer::Create(rpc);
ss << crpc->GenerateAPITypedef(service->id());
}
ss << " * api examples" << std::endl;
ss << " */ " << std::endl;
ss << std::endl;
ss << "typedef struct "
<< "{" << std::endl;
return ss.str();

View File

@ -5,6 +5,7 @@
#include "c_item.h"
#include "assert.h"
#include "c_item_int_32.h"
#include "c_item_uint_32.h"
#include "c_item_string.h"
namespace tqcq {
@ -15,6 +16,8 @@ CItem::Create(Item::Ptr item)
switch (item->type()) {
case Item::Type::kInt32:
return std::make_shared<CItemInt32>(item);
case Item::Type::kUInt32:
return std::make_shared<CItemUInt32>(item);
case Item::Type::kString:
return std::make_shared<CItemString>(item);
default:

View File

@ -0,0 +1,7 @@
//
// Created by tqcq on 2023/12/4.
//
#include "c_item_message.h"
namespace tqcq {}// namespace tqcq

View File

@ -0,0 +1,14 @@
//
// Created by tqcq on 2023/12/4.
//
#ifndef CRPC_SRC_PLUGINS_C_ITEMS_C_ITEM_MESSAGE_H_
#define CRPC_SRC_PLUGINS_C_ITEMS_C_ITEM_MESSAGE_H_
namespace tqcq {
class CItemMessage {};
}// namespace tqcq
#endif//CRPC_SRC_PLUGINS_C_ITEMS_C_ITEM_MESSAGE_H_

View File

@ -41,9 +41,9 @@ std::string
CItemString::GenerateSetterDeclaration(std::string message_id) const
{
std::stringstream ss;
ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const char* " << id() << ");";
ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const void* " << id() << ");";
ss << std::endl;
ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const char* " << id() << ", int32_t " << id() << "_len);";
ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const void* " << id() << ", int32_t " << id() << "_len);";
ss << std::endl;
return ss.str();
}
@ -53,7 +53,8 @@ CItemString::GenerateSetterDefinition(std::string message_id) const
{
std::stringstream ss;
// set_xxx_data
ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const char* " << id() << ", int32_t " << id() << "_len) {" << std::endl;
ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const void* " << id() << ", int32_t " << id() << "_len) {" << std::endl;
ss << " assert(" << id() << " || " << id() << "_len == 0);" << std::endl;
ss << " char* new_" << id() << " = (char*)malloc(" << id() << "_len + 1);" << std::endl;
ss << " if (new_" << id() << " == NULL) { return; }" << std::endl;
ss << " memcpy(new_" << id() << ", " << id() << ", " << id() << "_len);" << std::endl;
@ -67,7 +68,7 @@ CItemString::GenerateSetterDefinition(std::string message_id) const
ss << std::endl;
// set_xxx_string
ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const char* " << id() << ") {" << std::endl;
ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const void* " << id() << ") {" << std::endl;
ss << " int32_t " << id() << "_len = strlen(" << id() << ");" << std::endl;
ss << " " << message_id << "_set_" << id() << "_data(message, " << id() << ", " << id() << "_len);" << std::endl;
ss << "}" << std::endl;
@ -80,8 +81,18 @@ std::string
CItemString::GenerateGetterDefinition(std::string message_id) const
{
std::stringstream ss;
ss << "const char* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message) {" << std::endl;
ss << " return message->" << id() << ";" << std::endl;
ss << "void* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message) {" << std::endl;
ss << " return (void*)message->" << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
ss << "const char* " << message_id << "_get_" << id() << "_string(const " << message_id << "* message) {" << std::endl;
ss << " return (const char*)message->" << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
ss << "int32_t " << message_id << "_get_" << id() << "_len(const " << message_id << "* message) {" << std::endl;
ss << " return message->" << id() << "_len;" << std::endl;
ss << "}" << std::endl;
return ss.str();
@ -91,9 +102,11 @@ std::string
CItemString::GenerateGetterDeclaration(std::string message_id) const
{
std::stringstream ss;
ss << "const char* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message);";
ss << "void* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message);";
ss << std::endl;
ss << "const char* " << message_id << "_get_" << id() << "_len(const " << message_id << "* message);";
ss << "const char* " << message_id << "_get_" << id() << "_string(const " << message_id << "* message);";
ss << std::endl;
ss << "int32_t " << message_id << "_get_" << id() << "_len(const " << message_id << "* message);";
return ss.str();
}

View File

@ -0,0 +1,78 @@
//
// Created by tqcq on 2023/12/2.
//
#include "c_item_uint_32.h"
#include <sstream>
namespace tqcq {
CItemUInt32::CItemUInt32(Item::Ptr item) : CItem(item) {}
std::string
CItemUInt32::GenerateStructDeclareCodeBlock(std::string message_id) const
{
return " uint32_t " + id() + ";";
}
std::string
CItemUInt32::GenerateInitCodeBlock(std::string message_id) const
{
std::stringstream ss;
ss << " " << "message->" << id() << " = 0;";
return ss.str();
}
std::string
CItemUInt32::GenerateDestroyCodeBlock(std::string message_id) const
{
return " // do nothing for " + id() + " destroy";
}
std::string
CItemUInt32::GenerateSetterDeclaration(std::string message_id) const
{
return "void " + message_id + "_set_" + id() + "(" + message_id + "* message, uint32_t " + id() + ");";
}
std::string
CItemUInt32::GenerateSetterDefinition(std::string message_id) const
{
std::stringstream ss;
ss << "void " << message_id << "_set_" << id() << "(" << message_id << "* message, uint32_t " << id() << ") {" << std::endl;
ss << " message->" << id() << " = " << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
return ss.str();
}
std::string
CItemUInt32::GenerateGetterDefinition(std::string message_id) const
{
std::stringstream ss;
ss << "uint32_t " << message_id << "_get_" << id() << "(const " << message_id << "* message) {" << std::endl;
ss << " return message->" << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
return ss.str();
}
std::string
CItemUInt32::GenerateGetterDeclaration(std::string message_id) const
{
std::stringstream ss;
ss << "uint32_t " << message_id << "_get_" << id() << "(const " << message_id << "* message);";
return ss.str();
}
std::string
CItemUInt32::GenerateSerializeCodeBlock(std::string message_id) const
{
return " // implement me for " + message_id + "_" + id() + " serialize";
}
std::string
CItemUInt32::GenerateDeserializeCodeBlock(std::string message_id) const
{
return " // implement me for " + id() + " deserialize";
}
}// namespace tqcq

View File

@ -0,0 +1,35 @@
//
// Created by tqcq on 2023/12/2.
//
#ifndef CRPC_SRC_PLUGINS_C_GENERATORS_C_ITEM_UINT_32_H_
#define CRPC_SRC_PLUGINS_C_GENERATORS_C_ITEM_UINT_32_H_
#include "c_item.h"
namespace tqcq {
class CItemUInt32 : public CItem {
public:
using Ptr = std::shared_ptr<CItemUInt32>;
CItemUInt32(Item::Ptr item);
~CItemUInt32() override = default;
std::string GenerateStructDeclareCodeBlock(std::string message_id) const override;
std::string GenerateInitCodeBlock(std::string message_id) const override;
std::string GenerateDestroyCodeBlock(std::string message_id) const override;
std::string GenerateSetterDeclaration(std::string message_id) const override;
std::string GenerateSetterDefinition(std::string message_id) const override;
std::string GenerateGetterDefinition(std::string message_id) const override;
std::string GenerateGetterDeclaration(std::string message_id) const override;
std::string GenerateSerializeCodeBlock(std::string message_id) const override;
std::string GenerateDeserializeCodeBlock(std::string message_id) const override;
};
}// namespace tqcq
#endif//CRPC_SRC_PLUGINS_C_GENERATORS_C_ITEM_UINT_32_H_

View File

@ -110,4 +110,13 @@ CRPCServer::GenerateStructAPIWrapper(std::string service_id) const
return ss.str();
}
std::string
CRPCServer::GenerateAPITypedef(std::string service_id) const
{
std::stringstream ss;
// ss << "typedef int32_t (*" << service_id << "_API_"<< id() << "_Type)(const " << request_id() << " *request, " << response_id() << " *response);\n";
ss << "int32_t YOUR_PREFIX_" << id() << "_API_Impl" << "(const " << request_id() << " *request, " << response_id() << " *response);\n";;
return ss.str();
}
}// namespace tqcq

View File

@ -20,6 +20,7 @@ public:
std::string GenerateStructAPIWrapper(std::string service_id) const override;
std::string GenerateStructDeclareCodeBlock(std::string service_id) const override;
std::string GenerateStructAPIDeclareCodeBlock(std::string service_id) const override;
std::string GenerateAPITypedef(std::string service_id) const;
};
}// namespace tqcq

View File

@ -2,6 +2,7 @@ syntax = "proto3";
message EmptyRequest {
}
message EmptyResponse {
int32 code = 1;
string msg = 2;
@ -80,13 +81,63 @@ message SMSymmetricResponse {
string out_data = 3;
}
message SM2_3_Request {
string ecc_pubkey = 1;
string ecc_prikey = 2;
string in_data = 3;
}
message SM2_3_Response {
int32 code = 1;
string msg = 2;
string out_data = 3;
}
message ExportPublicKeyRequest {
int32 key_pair_type = 1;
}
message ImportECCKeyPairRequest {
int32 key_pair_type = 1;
string private_key = 2;
string public_key = 3;
}
message ImportCertificateRequest {
int32 cer_type = 1;
string in_data = 2;
}
message ExportCertificateRequest {
int32 cer_type = 1;
}
message ImportFileRequest {
string filename = 1;
string in_data = 2;
}
message ExportFileRequest {
string filename = 1;
}
message ExportResponse {
int32 code = 1;
string msg = 2;
string out_data = 3;
}
service DeviceService {
rpc GetUseCount(EmptyRequest) returns (StatusResponse) {}
rpc Init(EmptyRequest) returns (StatusResponse) {}
rpc Status(EmptyRequest) returns (StatusResponse) {}
rpc GetHWCode(EmptyRequest) returns (SingleDataResponse) {}
rpc Close(EmptyRequest) returns (StatusResponse) {}
rpc ClearApp(EmptyRequest) returns (EmptyResponse) {}
rpc GenerateECCKeyPair(EmptyRequest) returns (StatusResponse) {}
rpc UnlockApplicationPIN(ApplicationPINRequest) returns (StatusResponse) {}
rpc ChangeApplicationPIN(ApplicationPINRequest) returns (StatusResponse) {}
@ -99,8 +150,21 @@ service DeviceService {
rpc GenerateSignatureByExternalPrivateKey(GenerateSignatureByExternalPrivateKeyRequest) returns (SingleDataResponse) {}
rpc VerifySignature(VerifySignatureRequest) returns (StatusResponse) {}
rpc SM2_3_Encrypt_ExternalPubkey(SM2_3_Request) returns (SM2_3_Response) {}
rpc SM2_3_Decrypt_ExternalPrikey(SM2_3_Request) returns (SM2_3_Response) {}
rpc SM2_3_Decrypt_InternalPrikey(SM2_3_Request) returns (SM2_3_Response) {}
rpc SM2_3_Decrypt_InternalCryptPrikey(SM2_3_Request) returns (SM2_3_Response) {}
rpc SM1Encrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
rpc SM1Decrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
rpc SM4Encrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
rpc SM4Decrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
rpc ExportPublicKey(ExportPublicKeyRequest) returns (ExportResponse) {}
rpc ImportEccKeyPair(ImportECCKeyPairRequest) returns (StatusResponse) {}
rpc ImportCertificate(ImportCertificateRequest) returns (StatusResponse) {}
rpc ExportCertificate(ExportCertificateRequest) returns (ExportResponse) {}
rpc ImportFile(ImportFileRequest) returns (StatusResponse) {}
rpc ExportFile(ExportFileRequest) returns (ExportResponse) {}
}