Skip to content

Commit

Permalink
Merge pull request #401 from silicon-heaven/shv-api-3-part-1
Browse files Browse the repository at this point in the history
Shv api 3 part 1
  • Loading branch information
fvacek authored Feb 2, 2024
2 parents c32795e + 8f4da55 commit 3f07a5f
Show file tree
Hide file tree
Showing 58 changed files with 1,164 additions and 1,494 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Checks:
-hicpp-use-override,
-hicpp-vararg,
-misc-const-correctness,
-misc-include-cleaner,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-misc-unused-parameters,
Expand Down
4 changes: 3 additions & 1 deletion .github/actions/run-linter/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ runs:
run: |
BASE_REF="${{github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before}}"
git fetch --depth=1 origin "$BASE_REF"
readarray -t CHANGED_FILES < <(git diff --name-only "$BASE_REF" | grep 'cpp$')
# FIXME: The clang on CI chokes on lambda captures of structured binding variables. Remove the blacklisting of
# the files after the CI gets updated. (AFAIK clang-16).
readarray -t CHANGED_FILES < <(git diff --name-only "$BASE_REF" | grep 'cpp$' | grep -v -e "libshviotqt/tests/serialportsocket/test_serialportsocket.cpp" -e "libshvchainpack/tests/test_rpcvalue.cpp")
if [[ "${#CHANGED_FILES[@]}" -eq 0 ]]; then
echo "No changed cpp files."
exit 0
Expand Down
2 changes: 1 addition & 1 deletion examples/cli/shvdevice/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Application::Application(int &argc, char **argv, AppCliOptions* cli_opts)

auto root = new AppRootNode();
m_shvTree = new si::node::ShvNodeTree(root, this);
connect(m_shvTree->root(), &si::node::ShvRootNode::sendRpcMessage, m_rpcConnection, &si::rpc::ClientConnection::sendMessage);
connect(m_shvTree->root(), &si::node::ShvRootNode::sendRpcMessage, m_rpcConnection, &si::rpc::ClientConnection::sendRpcMessage);
//m_shvTree->mkdir("sys/rproc");

QTimer::singleShot(0, m_rpcConnection, &si::rpc::ClientConnection::open);
Expand Down
5 changes: 3 additions & 2 deletions libshvbroker/include/shv/broker/brokerapp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "shv/chainpack/rpcdriver.h"
#ifdef USE_SHV_PATHS_GRANTS_CACHE
#include <string>

Expand Down Expand Up @@ -68,7 +69,7 @@ class SHVBROKER_DECL_EXPORT BrokerApp : public QCoreApplication
void onClientLogin(int connection_id);
void onConnectedToMasterBrokerChanged(int connection_id, bool is_connected);

void onRpcDataReceived(int connection_id, shv::chainpack::Rpc::ProtocolType protocol_type, shv::chainpack::RpcValue::MetaData &&meta, std::string &&data);
void onRpcFrameReceived(int connection_id, shv::chainpack::RpcFrame &&frame);

rpc::MasterBrokerConnection* masterBrokerConnectionForClient(int client_id);

Expand Down Expand Up @@ -134,7 +135,7 @@ class SHVBROKER_DECL_EXPORT BrokerApp : public QCoreApplication
void onClientConnected(int client_id);

void sendNotifyToSubscribers(const std::string &shv_path, const std::string &method, const shv::chainpack::RpcValue &params);
bool sendNotifyToSubscribers(const shv::chainpack::RpcValue::MetaData &meta_data, const std::string &data);
bool sendNotifyToSubscribers(const shv::chainpack::RpcFrame &frame);

static std::string brokerClientDirPath(int client_id);
static std::string brokerClientAppPath(int client_id);
Expand Down
125 changes: 62 additions & 63 deletions libshvbroker/src/brokerapp.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions libshvbroker/src/clientshvnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ void ClientShvNode::removeConnection(rpc::ClientConnectionOnBroker *conn)
deleteLater();
}

void ClientShvNode::handleRawRpcRequest(shv::chainpack::RpcValue::MetaData &&meta, std::string &&data)
void ClientShvNode::handleRpcFrame(chainpack::RpcFrame &&frame)
{
rpc::ClientConnectionOnBroker *conn = connection();
if(conn)
conn->sendRawData(meta, std::move(data));
conn->sendRpcFrame(std::move(frame));
}

shv::chainpack::RpcValue ClientShvNode::hasChildren(const StringViewList &shv_path)
Expand Down
2 changes: 1 addition & 1 deletion libshvbroker/src/clientshvnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ClientShvNode : public shv::iotqt::node::ShvNode
void addConnection(rpc::ClientConnectionOnBroker *conn);
void removeConnection(rpc::ClientConnectionOnBroker *conn);

void handleRawRpcRequest(shv::chainpack::RpcValue::MetaData &&meta, std::string &&data) override;
void handleRpcFrame(chainpack::RpcFrame &&frame) override;
shv::chainpack::RpcValue hasChildren(const StringViewList &shv_path) override;
private:
QList<rpc::ClientConnectionOnBroker *> m_connections;
Expand Down
21 changes: 9 additions & 12 deletions libshvbroker/src/rpc/clientconnectiononbroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,20 @@ void ClientConnectionOnBroker::setIdleWatchDogTimeOut(int sec)
m_idleWatchDogTimer->start(sec * 1000);
}

void ClientConnectionOnBroker::sendMessage(const shv::chainpack::RpcMessage &rpc_msg)
void ClientConnectionOnBroker::sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg)
{
logRpcMsg() << SND_LOG_ARROW
<< "client id:" << connectionId()
<< "protocol_type:" << static_cast<int>(protocolType()) << shv::chainpack::Rpc::protocolTypeToString(protocolType())
<< rpc_msg.toPrettyString();
Super::sendMessage(rpc_msg);
chainpack::RpcDriver::sendRpcMessage(rpc_msg);
}

void ClientConnectionOnBroker::sendRawData(const shv::chainpack::RpcValue::MetaData &meta_data, std::string &&data)
void ClientConnectionOnBroker::sendRpcFrame(chainpack::RpcFrame &&frame)
{
logRpcMsg() << SND_LOG_ARROW
<< "client id:" << connectionId()
<< "protocol_type:" << static_cast<int>(protocolType()) << shv::chainpack::Rpc::protocolTypeToString(protocolType())
<< RpcDriver::dataToPrettyCpon(shv::chainpack::RpcMessage::protocolType(meta_data), meta_data, data);
Super::sendRawData(meta_data, std::move(data));
<< RpcDriver::frameToPrettyCpon(frame);
chainpack::RpcDriver::sendRpcFrame(std::move(frame));
}

ClientConnectionOnBroker::Subscription ClientConnectionOnBroker::createSubscription(const std::string &shv_path, const std::string &method)
Expand Down Expand Up @@ -269,20 +267,19 @@ string ClientConnectionOnBroker::toSubscribedPath(const CommonRpcClientHandle::S
return signal_path;
}

void ClientConnectionOnBroker::onRpcDataReceived(shv::chainpack::Rpc::ProtocolType protocol_type, shv::chainpack::RpcValue::MetaData &&md, string &&msg_data)
void ClientConnectionOnBroker::onRpcFrameReceived(chainpack::RpcFrame &&frame)
{
logRpcMsg() << RCV_LOG_ARROW
<< "client id:" << connectionId()
<< "protocol_type:" << static_cast<int>(protocol_type) << shv::chainpack::Rpc::protocolTypeToString(protocol_type)
<< RpcDriver::dataToPrettyCpon(protocol_type, md, msg_data, 0, msg_data.size());
<< RpcDriver::frameToPrettyCpon(frame);
try {
if(isLoginPhase()) {
Super::onRpcDataReceived(protocol_type, std::move(md), std::move(msg_data));
Super::onRpcFrameReceived(std::move(frame));
return;
}
if(m_idleWatchDogTimer)
m_idleWatchDogTimer->start();
BrokerApp::instance()->onRpcDataReceived(connectionId(), protocol_type, std::move(md), std::move(msg_data));
BrokerApp::instance()->onRpcFrameReceived(connectionId(), std::move(frame));
}
catch (std::exception &e) {
shvError() << e.what();
Expand Down
6 changes: 3 additions & 3 deletions libshvbroker/src/rpc/clientconnectiononbroker.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class ClientConnectionOnBroker : public shv::iotqt::rpc::ServerConnection, publi

void setIdleWatchDogTimeOut(int sec);

void sendMessage(const shv::chainpack::RpcMessage &rpc_msg) override;
void sendRawData(const shv::chainpack::RpcValue::MetaData &meta_data, std::string &&data) override;
void sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg) override;
void sendRpcFrame(shv::chainpack::RpcFrame &&frame) override;

Subscription createSubscription(const std::string &shv_path, const std::string &method) override;
std::string toSubscribedPath(const Subscription &subs, const std::string &signal_path) const override;
Expand All @@ -56,7 +56,7 @@ class ClientConnectionOnBroker : public shv::iotqt::rpc::ServerConnection, publi
void setLoginResult(const chainpack::UserLoginResult &result) override;
private:
void onSocketConnectedChanged(bool is_connected);
void onRpcDataReceived(shv::chainpack::Rpc::ProtocolType protocol_type, shv::chainpack::RpcValue::MetaData &&md, std::string &&msg_data) override;
void onRpcFrameReceived(chainpack::RpcFrame &&frame) override;
bool checkTunnelSecret(const std::string &s);
QVector<int> callerIdsToList(const shv::chainpack::RpcValue &caller_ids);

Expand Down
4 changes: 2 additions & 2 deletions libshvbroker/src/rpc/commonrpcclienthandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class CommonRpcClientHandle
virtual bool isSlaveBrokerConnection() const = 0;
virtual bool isMasterBrokerConnection() const = 0;

virtual void sendRawData(const shv::chainpack::RpcValue::MetaData &meta_data, std::string &&data) = 0;
virtual void sendMessage(const shv::chainpack::RpcMessage &rpc_msg) = 0;
virtual void sendRpcFrame(chainpack::RpcFrame &&frame) = 0;
virtual void sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg) = 0;
protected:
std::vector<Subscription> m_subscriptions;
};
Expand Down
26 changes: 12 additions & 14 deletions libshvbroker/src/rpc/masterbrokerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,17 @@ bool MasterBrokerConnection::isMasterBrokerConnection() const
return true;
}

void MasterBrokerConnection::sendRawData(const shv::chainpack::RpcValue::MetaData &meta_data, std::string &&data)
void MasterBrokerConnection::sendRpcFrame(chainpack::RpcFrame &&frame)
{
logRpcMsg() << SND_LOG_ARROW
<< "client id:" << connectionId()
<< "protocol_type:" << static_cast<int>(protocolType()) << shv::chainpack::Rpc::protocolTypeToString(protocolType())
<< RpcDriver::dataToPrettyCpon(shv::chainpack::RpcMessage::protocolType(meta_data), meta_data, data, 0);
Super::sendRawData(meta_data, std::move(data));
<< RpcDriver::frameToPrettyCpon(frame);
Super::sendRpcFrame(std::move(frame));
}

void MasterBrokerConnection::sendMessage(const shv::chainpack::RpcMessage &rpc_msg)
void MasterBrokerConnection::sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg)
{
Super::sendMessage(rpc_msg);
Super::sendRpcMessage(rpc_msg);
}

CommonRpcClientHandle::Subscription MasterBrokerConnection::createSubscription(const std::string &shv_path, const std::string &method)
Expand Down Expand Up @@ -145,27 +144,26 @@ const std::string& MasterBrokerConnection::exportedShvPath() const
return m_exportedShvPath;
}

void MasterBrokerConnection::onRpcDataReceived(shv::chainpack::Rpc::ProtocolType protocol_type, shv::chainpack::RpcValue::MetaData &&md, std::string &&msg_data)
void MasterBrokerConnection::onRpcFrameReceived(chainpack::RpcFrame &&frame)
{
logRpcMsg() << RpcDriver::RCV_LOG_ARROW
<< "client id:" << connectionId()
<< "protocol_type:" << static_cast<int>(protocol_type) << shv::chainpack::Rpc::protocolTypeToString(protocol_type)
<< RpcDriver::dataToPrettyCpon(protocol_type, md, msg_data, 0, msg_data.size());
<< RpcDriver::frameToPrettyCpon(frame);
try {
if(isLoginPhase()) {
Super::onRpcDataReceived(protocol_type, std::move(md), std::move(msg_data));
Super::onRpcFrameReceived(std::move(frame));
return;
}
if(cp::RpcMessage::isRequest(md)) {
if(cp::RpcMessage::isRequest(frame.meta)) {
}
else if(cp::RpcMessage::isResponse(md)) {
int rq_id = cp::RpcMessage::requestId(md).toInt();
else if(cp::RpcMessage::isResponse(frame.meta)) {
int rq_id = cp::RpcMessage::requestId(frame.meta).toInt();
if(rq_id == m_connectionState.pingRqId) {
m_connectionState.pingRqId = 0;
return;
}
}
BrokerApp::instance()->onRpcDataReceived(connectionId(), protocol_type, std::move(md), std::move(msg_data));
BrokerApp::instance()->onRpcFrameReceived(connectionId(), std::move(frame));
}
catch (std::exception &e) {
shvError() << e.what();
Expand Down
6 changes: 3 additions & 3 deletions libshvbroker/src/rpc/masterbrokerconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class MasterBrokerConnection : public shv::iotqt::rpc::DeviceConnection, public
bool isSlaveBrokerConnection() const override;
bool isMasterBrokerConnection() const override;

void sendRawData(const shv::chainpack::RpcValue::MetaData &meta_data, std::string &&data) override;
void sendMessage(const shv::chainpack::RpcMessage &rpc_msg) override;
void sendRpcFrame(shv::chainpack::RpcFrame &&frame) override;
void sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg) override;

Subscription createSubscription(const std::string &shv_path, const std::string &method) override;
std::string toSubscribedPath(const Subscription &subs, const std::string &signal_path) const override;
Expand All @@ -39,7 +39,7 @@ class MasterBrokerConnection : public shv::iotqt::rpc::DeviceConnection, public
void setOptions(const shv::chainpack::RpcValue &slave_broker_options);
shv::chainpack::RpcValue options();
protected:
void onRpcDataReceived(shv::chainpack::Rpc::ProtocolType protocol_type, shv::chainpack::RpcValue::MetaData &&md, std::string &&msg_data) override;
void onRpcFrameReceived(shv::chainpack::RpcFrame &&frame) override;
protected:
std::string m_exportedShvPath;
shv::chainpack::RpcValue m_options;
Expand Down
2 changes: 1 addition & 1 deletion libshvchainpack/include/shv/chainpack/chainpack.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "rpcvalue.h"
#include <shv/chainpack/cchainpack.h>
#include <shv/chainpack/shvchainpackglobal.h>

namespace shv {
namespace chainpack {
Expand Down
42 changes: 25 additions & 17 deletions libshvchainpack/include/shv/chainpack/crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@

#include <cstddef>
#include <cstdint>
#include <vector>
#include <array>

namespace shv::chainpack {

using crc32_t = uint32_t;

// CRC-32/ISO-HDLC
constexpr crc32_t CRC_POSIX_POLY_REV = 0xEDB88320L;

template<crc32_t POLY_REV>
template<crc32_t POLY>
consteval auto make_table() {
auto crc32_for_byte = [](uint8_t b)
{
constexpr auto MSB_MASK = static_cast<crc32_t>(0xFF) << ((sizeof(crc32_t) - 1) * 8);
auto r = static_cast<crc32_t>(b);
for(int j = 0; j < 8; ++j)
r = (r & 1? 0: POLY) ^ (r >> 1);
return r ^ MSB_MASK;
};
std::array<crc32_t, 256> table;
for(crc32_t i = 0; i < 0x100; ++i) {
table[i] = crc32_for_byte(static_cast<uint8_t>(i));
}
return table;
}

template<crc32_t POLY>
class Crc32
{
public:
constexpr Crc32() {
auto crc32_for_byte = [](uint8_t b)
{
constexpr auto MSB_MASK = static_cast<crc32_t>(0xFF) << ((sizeof(crc32_t) - 1) * 8);
auto r = static_cast<crc32_t>(b);
for(int j = 0; j < 8; ++j)
r = (r & 1? 0: POLY_REV) ^ (r >> 1);
return r ^ MSB_MASK;
};
for(crc32_t i = 0; i < 0x100; ++i) {
m_table[i] = crc32_for_byte(static_cast<uint8_t>(i));
}
}

void add(uint8_t b) {
const auto ix = static_cast<uint8_t>(m_remainder ^ static_cast<crc32_t>(b));
m_remainder = m_table[ix] ^ (m_remainder >> 8);
Expand All @@ -37,11 +42,14 @@ class Crc32
}
}
crc32_t remainder() const { return m_remainder; }
void setRemainder(crc32_t rem) { m_remainder = rem; }
void reset() { setRemainder(0); }
private:
crc32_t m_remainder = 0;
crc32_t m_table[256];
static constexpr auto m_table = make_table<POLY>();
};

using Crc32Posix = Crc32<CRC_POSIX_POLY_REV>;
using Crc32Shv3 = Crc32Posix;

}
2 changes: 1 addition & 1 deletion libshvchainpack/include/shv/chainpack/irpcconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SHVCHAINPACK_DECL_EXPORT IRpcConnection
virtual void close() = 0;
virtual void abort() = 0;

virtual void sendMessage(const shv::chainpack::RpcMessage &rpc_msg) = 0;
virtual void sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg) = 0;
virtual void onRpcMessageReceived(const shv::chainpack::RpcMessage &msg) = 0;

int nextRequestId();
Expand Down
4 changes: 1 addition & 3 deletions libshvchainpack/include/shv/chainpack/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

#include <shv/chainpack/shvchainpackglobal.h>

#include <string>

namespace shv {
namespace chainpack {

class SHVCHAINPACK_DECL_EXPORT Rpc
{
public:
enum class ProtocolType {Invalid = 0, ChainPack, Cpon, JsonRpc};
enum class ProtocolType {Invalid = 0, ChainPack};
static const char* protocolTypeToString(ProtocolType pv);

static constexpr auto OPT_IDLE_WD_TIMEOUT = "idleWatchDogTimeOut";
Expand Down
Loading

0 comments on commit 3f07a5f

Please sign in to comment.