feature move gen to grammar

This commit is contained in:
tqcq 2023-12-03 08:04:44 +08:00
parent 73846e885a
commit 41663e1c2f
19 changed files with 430 additions and 83 deletions

View File

@ -0,0 +1,31 @@
name: build-crpc
on:
- push
jobs:
build:
runs-on: ubuntu-20.04
strategy:
matrix:
build_type:
- Debug
- Release
toolchain:
- hisiv500.toolchain.cmake
- himix200.toolchain.cmake
- mips64el-linux-gnuabi64.toolchain.cmake
- mipsel-openwrt-linux-musl.toolchain.cmake
- mipsel-openwrt-linux.toolchain.cmake
- host.toolchain.cmake
steps:
- name: Install Dependencies
run: |
apt-get update -y
apt-get install -y \
cmake make g++ libssl-dev pkg-config autoconf libtool libantlr4-runtime-dev
- uses: actions/checkout@v4
- name: Generate Lexer and Parser
run: |
wget https://www.antlr.org/download/antlr-4.13.1-complete.jar -o antlr.jar
cd ${{ github.workspace }}/src/grammar
java -jar antlr.jar -Dlanguage=Cpp -o ./ ProtoLexer.g4
java -jar antlr.jar -Dlanguage=Cpp -o ./ ProtoParser.g4

6
.gitignore vendored
View File

@ -1,4 +1,8 @@
.idea/
cmake-build-*
build/
*gen*/
*gen*/
src/grammar/*.cpp
src/grammar/*.h
src/grammar/*.tokens
src/grammar/*.interp

View File

@ -24,14 +24,12 @@ add_executable(crpc
)
target_link_libraries(crpc PRIVATE antlr4_static)
target_include_directories(crpc PRIVATE /opt/dev/vcpkg/installed/arm64-osx/include/antlr4-runtime)
target_include_directories(crpc PRIVATE src/gen/ src/)
target_include_directories(crpc PRIVATE src/grammar/ src/)
FILE(GLOB_RECURSE TEST_SRC_LIST ${CMAKE_CURRENT_BINARY_DIR}/gen/*.c)
FILE(GLOB_RECURSE TEST_SRC_LIST ${CMAKE_CURRENT_BINARY_DIR}/grammar/*.c)
add_executable(test ${TEST_SRC_LIST}
test/test.c
${CMAKE_CURRENT_SOURCE_DIR}/3party/tpl/tpl.c
)
target_include_directories(test PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/gen/
${CMAKE_CURRENT_SOURCE_DIR}/3party/tpl
)

View File

@ -76,6 +76,13 @@ public:
int
main(int argc, char *argv[])
{
if (argc < 3) {
std::cout << "usage: " << argv[0] << " <proto file> <output dir>" << std::endl;
return 1;
}
std::string proto_file = argv[1];
std::string output_dir = argv[2];
// show current working directory
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
@ -86,7 +93,7 @@ main(int argc, char *argv[])
}
std::ifstream ifs;
ifs.open(argv[1]);
ifs.open(proto_file);
antlr4::ANTLRInputStream input(ifs);
ProtoLexer lexer(&input);
@ -98,18 +105,18 @@ 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;
}
// for (const auto& service : listener.services) {
// std::cout << service->ToString() << std::endl;
// }
tqcq::CMessagePlugin message_plugin("./gen/");
tqcq::CMessagePlugin message_plugin(output_dir);
message_plugin.Generate(listener.message_map);
tqcq::CServiceServerPlugin service_server_plugin("./gen/");
tqcq::CServiceServerPlugin service_server_plugin(output_dir);
service_server_plugin.Generate(listener.services);
// tqcq::CServiceClientPlugin service_client_plugin("./gen/");

View File

@ -21,6 +21,7 @@ CLOSE_BRACE: '}';
OPEN_PAREN: '(';
CLOSE_PAREN: ')';
SLASH: '/';
COMMENT: '//' .*? '\n' -> skip;
NUMBER: [0-9]+;
ID: [a-zA-Z_][a-zA-Z_0-9]*;

View File

@ -1,14 +1,14 @@
parser grammar ProtoParser;
options{ tokenVocab='gen/ProtoLexer'; language=Cpp; }
options{ tokenVocab='ProtoLexer'; language=Cpp; }
program: syntaxStatement baseStatement* EOF;
commentStatement : SLASH SLASH .*? NEWLINE;
syntaxStatement: SYNTAX_TYPE '=' '"' (VERSION2 | VERSION3) '"' ';';
baseStatement: messageStatement | serviceStatement | ';';
baseStatement: messageStatement | serviceStatement | ';' ;
messageStatement: MESSAGE_TYPE ID '{' (itemStatement | ';')* '}';
serviceStatement: SERVICE_TYPE ID '{' (rpcStatement | ';')* '}';
itemStatement: (INT32 | STRING) ID '=' NUMBER ';';
rpcStatement: RPC_TYPE ID '(' ID ')' 'returns' '(' ID ')' '{' '}';

View File

@ -53,7 +53,7 @@ CMessagePlugin::GenerateHeader(tqcq::Message::Ptr message)
ss << GenerateIncludeGuardEnd(message);
std::ofstream ofs(generate_path() + "/" + GenerateHeaderFileName(message->id()));
ofs << ss.str();
std::cout << ss.str() << std::endl;
// std::cout << ss.str() << std::endl;
}
void
@ -120,6 +120,9 @@ CMessagePlugin::GenerateStructItem(Message::Ptr message)
for (auto &item : message->items()) {
ss << CItem::Create(item)->GenerateStructDeclareCodeBlock(message->id()) << std::endl;
}
if (message->items().empty()) {
ss << " char:8; " << std::endl;
}
return ss.str();
}

View File

@ -10,4 +10,88 @@ namespace tqcq {
CServiceClientPlugin::CServiceClientPlugin(std::string generate_path, std::string generate_prefix)
: CServicePlugin(std::move(generate_path), std::move(generate_prefix))
{}
std::string
CServiceClientPlugin::GenerateHeaderFileName(std::string service_id)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateSourceFileName(std::string service_id)
{
return std::string();
}
void
CServiceClientPlugin::GenerateHeader(Service::Ptr service)
{
CServicePlugin::GenerateHeader(service);
}
void
CServiceClientPlugin::GenerateSource(Service::Ptr service)
{
CServicePlugin::GenerateSource(service);
}
std::string
CServiceClientPlugin::GenerateHeaderIncludeFiles(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateHeaderStructStart(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateHeaderStructItems(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateHeaderStructEnd(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateHeaderStructAPIStart(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateHeaderStructAPIItems(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateHeaderStructAPIEnd(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateHeaderFunctionDeclaration(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateSourceIncludeFiles(Service::Ptr service)
{
return std::string();
}
std::string
CServiceClientPlugin::GenerateSourceFunctionDefinition(Service::Ptr service)
{
return std::string();
}
}// namespace tqcq

View File

@ -14,6 +14,20 @@ public:
CServiceClientPlugin(std::string generate_path, std::string generate_prefix = "CService");
virtual ~CServiceClientPlugin() = default;
protected:
std::string GenerateHeaderFileName(std::string service_id) override;
std::string GenerateSourceFileName(std::string service_id) override;
void GenerateHeader(Service::Ptr service) override;
void GenerateSource(Service::Ptr service) override;
std::string GenerateHeaderIncludeFiles(Service::Ptr service) override;
std::string GenerateHeaderStructStart(Service::Ptr service) override;
std::string GenerateHeaderStructItems(Service::Ptr service) override;
std::string GenerateHeaderStructEnd(Service::Ptr service) override;
std::string GenerateHeaderStructAPIStart(Service::Ptr service) override;
std::string GenerateHeaderStructAPIItems(Service::Ptr service) override;
std::string GenerateHeaderStructAPIEnd(Service::Ptr service) override;
std::string GenerateHeaderFunctionDeclaration(Service::Ptr service) override;
std::string GenerateSourceIncludeFiles(Service::Ptr service) override;
std::string GenerateSourceFunctionDefinition(Service::Ptr service) override;
};
}// namespace tqcq

View File

@ -32,16 +32,16 @@ CServicePlugin::GenerateHeader(Service::Ptr service)
ss << GenerateHeaderIncludeFiles(service);
ss << std::endl;
ss << GenerateHeaderStructStart(service);
ss << GenerateHeaderStructItems(service);
ss << GenerateHeaderStructEnd(service);
ss << std::endl;
ss << GenerateHeaderStructAPIStart(service);
ss << GenerateHeaderStructAPIItems(service);
ss << GenerateHeaderStructAPIEnd(service);
ss << std::endl;
ss << GenerateHeaderStructStart(service);
ss << GenerateHeaderStructItems(service);
ss << GenerateHeaderStructEnd(service);
ss << std::endl;
ss << GenerateHeaderFunctionDeclaration(service);
ss << std::endl;
@ -53,5 +53,31 @@ CServicePlugin::GenerateHeader(Service::Ptr service)
void
CServicePlugin::GenerateSource(Service::Ptr service)
{
std::stringstream ss;
ss << GenerateSourceIncludeFiles(service);
ss << std::endl;
ss << GenerateSourceFunctionDefinition(service);
std::ofstream ofs(generate_path() + "/" + GenerateSourceFileName(service->id()));
ofs << ss.str();
}
std::string
CServicePlugin::GenerateHeaderGuardStart(Service::Ptr service)
{
std::stringstream ss;
auto guard_macro = GenerateGuardMacro(service->id(), generate_prefix());
ss << "#ifndef " << guard_macro << std::endl;
ss << "#define " << guard_macro << std::endl;
return ss.str();
}
std::string
CServicePlugin::GenerateHeaderGuardEnd(Service::Ptr service)
{
std::stringstream ss;
auto guard_macro = GenerateGuardMacro(service->id(), generate_prefix());
ss << "#endif // " << guard_macro << std::endl;
return ss.str();
}
}// namespace tqcq

View File

@ -23,9 +23,9 @@ protected:
virtual void GenerateHeader(Service::Ptr service) override;
virtual void GenerateSource(Service::Ptr service) override;
virtual std::string GenerateHeaderGuardStart(Service::Ptr service) = 0;
virtual std::string GenerateHeaderGuardStart(Service::Ptr service);
virtual std::string GenerateHeaderIncludeFiles(Service::Ptr service) = 0;
virtual std::string GenerateHeaderGuardEnd(Service::Ptr service) = 0;
virtual std::string GenerateHeaderGuardEnd(Service::Ptr service);
virtual std::string GenerateHeaderStructStart(Service::Ptr service) = 0;
virtual std::string GenerateHeaderStructItems(Service::Ptr service) = 0;
@ -36,6 +36,9 @@ protected:
virtual std::string GenerateHeaderStructAPIEnd(Service::Ptr service) = 0;
virtual std::string GenerateHeaderFunctionDeclaration(Service::Ptr service) = 0;
virtual std::string GenerateSourceIncludeFiles(Service::Ptr service) = 0;
virtual std::string GenerateSourceFunctionDefinition(Service::Ptr service) = 0;
};
}// namespace tqcq

View File

@ -12,17 +12,6 @@ CServiceServerPlugin::CServiceServerPlugin(std::string generate_path, std::strin
: CServicePlugin(std::move(generate_path), std::move(generate_prefix))
{}
std::string
CServiceServerPlugin::GenerateHeaderGuardStart(Service::Ptr service)
{
std::stringstream ss;
auto guard_macro = GenerateGuardMacro(service->id(), generate_prefix());
ss << "#ifndef " << guard_macro << std::endl;
ss << "#define " << guard_macro << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateHeaderIncludeFiles(Service::Ptr service)
{
@ -30,16 +19,13 @@ CServiceServerPlugin::GenerateHeaderIncludeFiles(Service::Ptr service)
ss << "#include <stdint.h>" << std::endl;
ss << "#include <string.h>" << std::endl;
ss << "#include <stdlib.h>" << std::endl;
ss << "#include <crpc/service.h>" << std::endl;
std::set<std::string> message_set;
for (const auto& rpc : service->rpcs()) {
for (const auto &rpc : service->rpcs()) {
auto crpc = CRPCServer::Create(rpc);
message_set.insert(crpc->request_id());
message_set.insert(crpc->response_id());
}
for (const auto& message_id : message_set) {
ss << "#include \"" << message_id << ".h\"" << std::endl;
}
for (const auto &message_id : message_set) { ss << "#include \"" << message_id << ".h\"" << std::endl; }
ss << std::endl;
ss << "#ifdef __cplusplus" << std::endl;
@ -48,23 +34,12 @@ CServiceServerPlugin::GenerateHeaderIncludeFiles(Service::Ptr service)
return ss.str();
}
std::string
CServiceServerPlugin::GenerateHeaderGuardEnd(Service::Ptr service)
{
std::stringstream ss;
auto guard_macro = GenerateGuardMacro(service->id(), generate_prefix());
ss << "#ifdef __cplusplus" << std::endl;
ss << "}" << std::endl;
ss << "#endif" << std::endl;
ss << "#endif // " << guard_macro << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateHeaderStructAPIStart(Service::Ptr service)
{
std::stringstream ss;
ss << "typedef struct " << "{" << std::endl;
ss << "typedef struct "
<< "{" << std::endl;
return ss.str();
}
@ -72,7 +47,7 @@ std::string
CServiceServerPlugin::GenerateHeaderStructAPIItems(Service::Ptr service)
{
std::stringstream ss;
for (const auto& rpc : service->rpcs()) {
for (const auto &rpc : service->rpcs()) {
auto crpc = CRPCServer::Create(rpc);
ss << crpc->GenerateStructDeclareCodeBlock(service->id());
}
@ -83,14 +58,8 @@ std::string
CServiceServerPlugin::GenerateHeaderStructAPIEnd(Service::Ptr service)
{
std::stringstream ss;
ss << "} " << service->id() << "_API" << ";" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateHeaderFunctionDeclaration(Service::Ptr service)
{
std::stringstream ss;
ss << "} " << service->id() << "_API"
<< ";" << std::endl;
return ss.str();
}
@ -113,19 +82,148 @@ CServiceServerPlugin::GenerateSourceFileName(std::string service_id)
std::string
CServiceServerPlugin::GenerateHeaderStructStart(Service::Ptr service)
{
return std::string();
std::stringstream ss;
ss << "typedef struct "
<< "{" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateHeaderStructItems(Service::Ptr service)
{
return std::string();
std::stringstream ss;
ss << " void* impl;" << std::endl;
ss << " DeviceService_API api;" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateHeaderStructEnd(Service::Ptr service)
{
return std::string();
std::stringstream ss;
ss << "} " << service->id() << "_Server"
<< ";" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateHeaderFunctionDeclaration(Service::Ptr service)
{
std::stringstream ss;
ss << "int32_t " << service->id() << "_Server_Init(" << service->id() << "_Server* server, " << service->id()
<< "_API* api);" << std::endl;
ss << "int32_t " << service->id() << "_Server_Bind(" << service->id() << "_Server* server, const char* format, ...);"
<< std::endl;
ss << "int32_t " << service->id() << "_Server_Listen(" << service->id() << "_Server* server);" << std::endl;
ss << "void " << service->id() << "_Server_Destroy(" << service->id() << "_Server* server);" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateSourceIncludeFiles(Service::Ptr service)
{
std::stringstream ss;
ss << "#include <assert.h>" << std::endl;
ss << "#include <stdarg.h>" << std::endl;
ss << "#include <stdio.h>" << std::endl;
ss << "#include <crpc/service.h>" << std::endl;
ss << "#include \"" << GenerateHeaderFileName(service->id()) << "\"" << std::endl;
std::set<std::string> message_set;
for (const auto &rpc : service->rpcs()) {
auto crpc = CRPCServer::Create(rpc);
message_set.insert(crpc->request_id());
message_set.insert(crpc->response_id());
}
for (const auto &message_id : message_set) { ss << "#include \"" << message_id << ".h\"" << std::endl; }
return ss.str();
}
std::string
CServiceServerPlugin::GenerateSourceFunctionDefinition(Service::Ptr service)
{
std::stringstream ss;
for (const auto &rpc : service->rpcs()) {
auto crpc = CRPCServer::Create(rpc);
ss << crpc->GenerateStructAPIWrapper(service->id());
ss << std::endl;
}
ss << std::endl;
ss << GenerateSourceInitFunctionDefinition(service) << std::endl;
ss << GenerateSourceBindFunctionDefinition(service) << std::endl;
ss << GenerateSourceListenFunctionDefinition(service) << std::endl;
ss << GenerateSourceDestroyFunctionDefinition(service) << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateSourceInitFunctionDefinition(Service::Ptr service)
{
std::stringstream ss;
ss << "int32_t " << service->id() << "_Server_Init(" << service->id() << "_Server* server, " << service->id()
<< "_API* api)" << std::endl;
ss << "{" << std::endl;
ss << " assert(server != NULL);" << std::endl;
ss << " server->impl = Service_New();" << std::endl;
ss << " if (api) {" << std::endl;
ss << " memcpy(&server->api, api, sizeof(" << service->id() << "_API));" << std::endl;
ss << " } else {" << std::endl;
ss << " memset(&server->api, 0, sizeof(" << service->id() << "_API));" << std::endl;
ss << " }" << std::endl;
ss << " if (server->impl == NULL) { return -1; }" << std::endl;
ss << " return 0;" << std::endl;
ss << "}" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateSourceBindFunctionDefinition(Service::Ptr service)
{
std::stringstream ss;
ss << "int32_t " << service->id() << "_Server_Bind(" << service->id() << "_Server* server, const char* format, ...)"
<< std::endl;
ss << "{" << std::endl;
ss << " assert(server != NULL);" << std::endl;
ss << " assert(server->impl != NULL);" << std::endl;
ss << std::endl;
ss << " char address_str[1024];" << std::endl;
ss << " va_list args;" << std::endl;
ss << " va_start(args, format);" << std::endl;
ss << " vsnprintf(address_str, sizeof(address_str), format, args);" << std::endl;
ss << " va_end(args);" << std::endl;
ss << std::endl;
ss << " return Service_Bind(server->impl, address_str);" << std::endl;
ss << "}" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateSourceListenFunctionDefinition(Service::Ptr service)
{
std::stringstream ss;
ss << "int32_t " << service->id() << "_Server_Listen(" << service->id() << "_Server* server)" << std::endl;
ss << "{" << std::endl;
ss << " assert(server != NULL);" << std::endl;
ss << " assert(server->impl != NULL);" << std::endl;
ss << std::endl;
ss << " return Service_Listen(server->impl);" << std::endl;
ss << "}" << std::endl;
return ss.str();
}
std::string
CServiceServerPlugin::GenerateSourceDestroyFunctionDefinition(Service::Ptr service)
{
std::stringstream ss;
ss << "void " << service->id() << "_Server_Destroy(" << service->id() << "_Server* server)" << std::endl;
ss << "{" << std::endl;
ss << " if (server == NULL) { return; }" << std::endl;
ss << " if (server->impl != NULL) { Service_Free(server->impl); }" << std::endl;
ss << "}" << std::endl;
return ss.str();
}
}// namespace tqcq

View File

@ -16,19 +16,26 @@ public:
protected:
std::string GenerateHeaderFileName(std::string service_id) override;
std::string GenerateSourceFileName(std::string service_id) override;
std::string GenerateHeaderGuardStart(Service::Ptr service) override;
std::string GenerateHeaderIncludeFiles(Service::Ptr service) override;
std::string GenerateHeaderGuardEnd(Service::Ptr service) override;
std::string GenerateHeaderStructStart(Service::Ptr service) override;
std::string GenerateHeaderStructItems(Service::Ptr service) override;
std::string GenerateHeaderStructEnd(Service::Ptr service) override;
std::string GenerateHeaderIncludeFiles(Service::Ptr service) override;
std::string GenerateHeaderStructAPIStart(Service::Ptr service) override;
std::string GenerateHeaderStructAPIItems(Service::Ptr service) override;
std::string GenerateHeaderStructAPIEnd(Service::Ptr service) override;
std::string GenerateHeaderStructStart(Service::Ptr service) override;
std::string GenerateHeaderStructItems(Service::Ptr service) override;
std::string GenerateHeaderStructEnd(Service::Ptr service) override;
std::string GenerateHeaderFunctionDeclaration(Service::Ptr service) override;
std::string GenerateSourceIncludeFiles(Service::Ptr service) override;
std::string GenerateSourceFunctionDefinition(Service::Ptr service) override;
std::string GenerateSourceInitFunctionDefinition(Service::Ptr service);
std::string GenerateSourceBindFunctionDefinition(Service::Ptr service);
std::string GenerateSourceListenFunctionDefinition(Service::Ptr service);
std::string GenerateSourceDestroyFunctionDefinition(Service::Ptr service);
};
}// namespace tqcq

View File

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

View File

@ -10,6 +10,9 @@
namespace tqcq {
class CRPCClient : public CRPC {
public:
CRPCClient() = default;
~CRPCClient() override = default;
};
}// namespace tqcq

View File

@ -24,7 +24,48 @@ std::string
CRPCServer::GenerateStructDeclareCodeBlock(std::string service_id) const
{
std::stringstream ss;
ss << " int32_t (*" << id() << ")(" << request_id() << " *request, " << response_id() << " **response);\n";
ss << " int32_t (*" << id() << ")(const " << request_id() << " *request, " << response_id() << " *response);\n";
return ss.str();
}
std::string
CRPCServer::GenerateStructAPIWrapper(std::string service_id) const
{
std::stringstream ss;
ss << "int32_t " << id() << "_Wrapper(" << service_id << "_Server *server, char *msg, int32_t msg_len)" << std::endl;
ss << "{" << std::endl;
ss << " assert(server != NULL);" << std::endl;
ss << " assert(msg != NULL || msg_len == 0);" << std::endl;
ss << " if (server->api." << id() << " == NULL) { return 0; }" << std::endl;
ss << std::endl;
ss << " int32_t status = 0;" << std::endl;
ss << " " << request_id() << " request;" << std::endl;
ss << " " << response_id() << " response;" << std::endl;
ss << std::endl;
ss << " do {" << std::endl;
ss << " " << request_id() << "_Init(&request);" << std::endl;
ss << " status = " << request_id() << "_Deserialize(&request, msg, msg_len);" << std::endl;
ss << " if (status != 0) {" << std::endl;
ss << " " << request_id() << "_Destroy(&request);" << std::endl;
ss << " break;" << std::endl;
ss << " }" << std::endl;
ss << std::endl;
ss << " " << response_id() << "_Init(&response);" << std::endl;
ss << " " << response_id() << "_set_code(&response, 200);" << std::endl;
ss << " " << response_id() << "_set_msg_string(&response, \"OK\");" << std::endl;
ss << " status = server->api." << id() << "(&request, &response);" << std::endl;
ss << " if (status != 0) {" << std::endl;
ss << " printf(\"" << id() << " failed: %d\\n\", status);" << std::endl;
ss << " " << response_id() << "_set_code(&response, 500);" << std::endl;
ss << " " << response_id() << "_set_msg_string(&response, \"Internal Server Error\");" << std::endl;
ss << " }" << std::endl;
ss << std::endl;
ss << " } while (0);" << std::endl;
ss << " " << response_id() << "_Destroy(&response);" << std::endl;
ss << " " << request_id() << "_Destroy(&request);" << std::endl;
ss << " return status;" << std::endl;
ss << "}" << std::endl;
return ss.str();
}

View File

@ -17,6 +17,7 @@ public:
CRPCServer(RPC::Ptr rpc);
~CRPCServer() override = default;
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;
};

View File

@ -10,10 +10,11 @@ namespace tqcq {
static void EnsureDirectoryExist(std::string path)
{
std::string::size_type pos = 0;
while (true) {
bool is_end = false;
while (!is_end) {
pos = path.find('/', pos);
if (pos == std::string::npos) {
break;
is_end = true;
}
std::string dir = path.substr(0, pos);
int status = mkdir(dir.c_str(), 0755);

View File

@ -1,6 +1,10 @@
syntax = "proto3";
message Empty {
message EmptyRequest {
}
message EmptyResponse {
int32 code = 1;
string msg = 2;
}
@ -54,21 +58,35 @@ message GenerateSignatureByExternalPrivateKeyRequest {
}
message VerifySignatureRequest {
int32 code = 1;
string msg = 2;
// int32 code = 1;
// string msg = 2;
string source_data = 3;
string ecc_public_key = 4;
string ecc_signature = 5;
}
message SMSymmetricRequest {
int32 operator_mode = 1; // alg mode
string iv = 2;
string key = 3;
string in_data = 4;
}
message SMSymmetricResponse {
int32 code = 1;
string msg = 2;
string out_data = 3;
}
service DeviceService {
rpc Init(Empty) returns (StatusResponse) {}
rpc Status(Empty) returns (StatusResponse) {}
rpc GetHWCode(Empty) returns (SingleDataResponse) {}
rpc Close(Empty) returns (StatusResponse) {}
rpc ClearApp(Empty) returns (Empty) {}
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 UnlockApplicationPIN(ApplicationPINRequest) returns (StatusResponse) {}
rpc ChangeApplicationPIN(ApplicationPINRequest) returns (StatusResponse) {}
@ -79,4 +97,10 @@ service DeviceService {
rpc GenerateSignatureByInternalPrivateKey(GenerateSignatureByInternalPrivateKeyRequest) returns (SingleDataResponse) {}
rpc GenerateSignatureByExternalPrivateKey(GenerateSignatureByExternalPrivateKeyRequest) returns (SingleDataResponse) {}
rpc VerifySignature(VerifySignatureRequest) returns (StatusResponse) {}
rpc SM1Encrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
rpc SM1Decrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
rpc SM4Encrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
rpc SM4Decrypt(SMSymmetricRequest) returns (SMSymmetricResponse) {}
}