106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include "plugin_command_builder.h"
1706f6ba60Sopenharmony_ci#include "common_types.pb.h"
1806f6ba60Sopenharmony_ci#include "logging.h"
1906f6ba60Sopenharmony_ci#include "plugin_service_types.pb.h"
2006f6ba60Sopenharmony_ci
2106f6ba60Sopenharmony_ciPluginCommandBuilder::PluginCommandBuilder()
2206f6ba60Sopenharmony_ci{
2306f6ba60Sopenharmony_ci    cmdIdAutoIncrease_ = 1;
2406f6ba60Sopenharmony_ci}
2506f6ba60Sopenharmony_ci
2606f6ba60Sopenharmony_ciPluginCommandBuilder::~PluginCommandBuilder() {}
2706f6ba60Sopenharmony_ci
2806f6ba60Sopenharmony_ciGetCommandResponsePtr PluginCommandBuilder::BuildCreateSessionCmd(const ProfilerPluginConfig& config,
2906f6ba60Sopenharmony_ci                                                                  uint32_t bufferSize)
3006f6ba60Sopenharmony_ci{
3106f6ba60Sopenharmony_ci    auto cmd = std::make_shared<GetCommandResponse>();
3206f6ba60Sopenharmony_ci    cmd->set_status(ResponseStatus::OK);
3306f6ba60Sopenharmony_ci    cmd->set_has_more(false);
3406f6ba60Sopenharmony_ci    cmd->set_command_id(cmdIdAutoIncrease_);
3506f6ba60Sopenharmony_ci    CreateSessionCmd* csc = cmd->mutable_create_session_cmd();
3606f6ba60Sopenharmony_ci    csc->add_buffer_sizes(bufferSize);
3706f6ba60Sopenharmony_ci    ProfilerPluginConfig* ppc = csc->add_plugin_configs();
3806f6ba60Sopenharmony_ci    *ppc = config;
3906f6ba60Sopenharmony_ci    commandHistory_[cmdIdAutoIncrease_] = cmd;
4006f6ba60Sopenharmony_ci    cmdIdAutoIncrease_++;
4106f6ba60Sopenharmony_ci    return cmd;
4206f6ba60Sopenharmony_ci}
4306f6ba60Sopenharmony_ci
4406f6ba60Sopenharmony_ciGetCommandResponsePtr PluginCommandBuilder::BuildDestroySessionCmd(uint32_t pluginId)
4506f6ba60Sopenharmony_ci{
4606f6ba60Sopenharmony_ci    auto cmd = std::make_shared<GetCommandResponse>();
4706f6ba60Sopenharmony_ci    cmd->set_status(ResponseStatus::OK);
4806f6ba60Sopenharmony_ci    cmd->set_has_more(false);
4906f6ba60Sopenharmony_ci    cmd->set_command_id(cmdIdAutoIncrease_);
5006f6ba60Sopenharmony_ci
5106f6ba60Sopenharmony_ci    DestroySessionCmd* dsc = cmd->mutable_destroy_session_cmd();
5206f6ba60Sopenharmony_ci    dsc->add_plugin_ids(pluginId);
5306f6ba60Sopenharmony_ci
5406f6ba60Sopenharmony_ci    commandHistory_[cmdIdAutoIncrease_] = cmd;
5506f6ba60Sopenharmony_ci    cmdIdAutoIncrease_++;
5606f6ba60Sopenharmony_ci    return cmd;
5706f6ba60Sopenharmony_ci}
5806f6ba60Sopenharmony_ci
5906f6ba60Sopenharmony_ciGetCommandResponsePtr PluginCommandBuilder::BuildStartSessionCmd(const ProfilerPluginConfig& config, uint32_t pluginId)
6006f6ba60Sopenharmony_ci{
6106f6ba60Sopenharmony_ci    auto cmd = std::make_shared<GetCommandResponse>();
6206f6ba60Sopenharmony_ci    cmd->set_status(ResponseStatus::OK);
6306f6ba60Sopenharmony_ci    cmd->set_has_more(false);
6406f6ba60Sopenharmony_ci    cmd->set_command_id(cmdIdAutoIncrease_);
6506f6ba60Sopenharmony_ci
6606f6ba60Sopenharmony_ci    StartSessionCmd* ssc = cmd->mutable_start_session_cmd();
6706f6ba60Sopenharmony_ci    ssc->add_plugin_ids(pluginId);
6806f6ba60Sopenharmony_ci    ProfilerPluginConfig* ppc = ssc->add_plugin_configs();
6906f6ba60Sopenharmony_ci    *ppc = config;
7006f6ba60Sopenharmony_ci
7106f6ba60Sopenharmony_ci    commandHistory_[cmdIdAutoIncrease_] = cmd;
7206f6ba60Sopenharmony_ci    cmdIdAutoIncrease_++;
7306f6ba60Sopenharmony_ci    return cmd;
7406f6ba60Sopenharmony_ci}
7506f6ba60Sopenharmony_ci
7606f6ba60Sopenharmony_ciGetCommandResponsePtr PluginCommandBuilder::BuildStopSessionCmd(uint32_t pluginId)
7706f6ba60Sopenharmony_ci{
7806f6ba60Sopenharmony_ci    auto cmd = std::make_shared<GetCommandResponse>();
7906f6ba60Sopenharmony_ci    cmd->set_status(ResponseStatus::OK);
8006f6ba60Sopenharmony_ci    cmd->set_has_more(false);
8106f6ba60Sopenharmony_ci    cmd->set_command_id(cmdIdAutoIncrease_);
8206f6ba60Sopenharmony_ci
8306f6ba60Sopenharmony_ci    StopSessionCmd* ssc = cmd->mutable_stop_session_cmd();
8406f6ba60Sopenharmony_ci    ssc->add_plugin_ids(pluginId);
8506f6ba60Sopenharmony_ci
8606f6ba60Sopenharmony_ci    commandHistory_[cmdIdAutoIncrease_] = cmd;
8706f6ba60Sopenharmony_ci    cmdIdAutoIncrease_++;
8806f6ba60Sopenharmony_ci    return cmd;
8906f6ba60Sopenharmony_ci}
9006f6ba60Sopenharmony_ci
9106f6ba60Sopenharmony_ciGetCommandResponsePtr PluginCommandBuilder::BuildRefreshSessionCmd(uint32_t pluginId)
9206f6ba60Sopenharmony_ci{
9306f6ba60Sopenharmony_ci    auto cmd = std::make_shared<GetCommandResponse>();
9406f6ba60Sopenharmony_ci    cmd->set_status(ResponseStatus::OK);
9506f6ba60Sopenharmony_ci    cmd->set_has_more(false);
9606f6ba60Sopenharmony_ci    cmd->set_command_id(cmdIdAutoIncrease_);
9706f6ba60Sopenharmony_ci
9806f6ba60Sopenharmony_ci    RefreshSessionCmd* rsc = cmd->mutable_refresh_session_cmd();
9906f6ba60Sopenharmony_ci    rsc->add_plugin_ids(pluginId);
10006f6ba60Sopenharmony_ci
10106f6ba60Sopenharmony_ci    commandHistory_[cmdIdAutoIncrease_] = cmd;
10206f6ba60Sopenharmony_ci    cmdIdAutoIncrease_++;
10306f6ba60Sopenharmony_ci    return cmd;
10406f6ba60Sopenharmony_ci}
10506f6ba60Sopenharmony_ci
10606f6ba60Sopenharmony_cibool PluginCommandBuilder::GetedCommandResponse(uint32_t cmdId)
10706f6ba60Sopenharmony_ci{
10806f6ba60Sopenharmony_ci    if (commandHistory_.find(cmdId) == commandHistory_.end()) {
10906f6ba60Sopenharmony_ci        return false;
11006f6ba60Sopenharmony_ci    }
11106f6ba60Sopenharmony_ci    commandHistory_.erase(cmdId);
11206f6ba60Sopenharmony_ci    return true;
11306f6ba60Sopenharmony_ci}