From 91635761dc910b214b910e16abd8125278450a67 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 12 Mar 2021 14:37:14 +0900 Subject: [PATCH] Use Parcel API instead of custom Parcel Change-Id: I802c7aaee4fbeb476a27745e7e303ab801e52e78 Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 3 ++- aul/socket/client.cc | 5 +++-- aul/socket/packet.cc | 33 ++++++++++++++++------------ aul/socket/packet.hh | 10 ++++----- aul/socket/parcel.cc | 49 ----------------------------------------- aul/socket/parcel.hh | 61 ---------------------------------------------------- packaging/aul.spec | 1 + 7 files changed, 30 insertions(+), 132 deletions(-) delete mode 100644 aul/socket/parcel.cc delete mode 100644 aul/socket/parcel.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 9513203..284c975 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ PKG_CHECK_MODULES(INIPARSER_DEPS REQUIRED iniparser) PKG_CHECK_MODULES(LIBSMACK_DEPS REQUIRED libsmack) PKG_CHECK_MODULES(LIBTZPLATFORM_CONFIG_DEPS REQUIRED libtzplatform-config) PKG_CHECK_MODULES(LIBXML_DEPS REQUIRED libxml-2.0) +PKG_CHECK_MODULES(PARCEL_DEPS REQUIRED parcel) PKG_CHECK_MODULES(PKGMGR_INFO_DEPS REQUIRED pkgmgr-info) PKG_CHECK_MODULES(PKGMGR_INSTALLER_DEPS REQUIRED pkgmgr-installer) PKG_CHECK_MODULES(SQLITE3_DEPS REQUIRED sqlite3) @@ -52,7 +53,6 @@ PKG_CHECK_MODULES(UUID_DEPS REQUIRED uuid) PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf) PKG_CHECK_MODULES(XDGMIME_DEPS REQUIRED xdgmime) - ## Target sources AUX_SOURCE_DIRECTORY(src SRCS) AUX_SOURCE_DIRECTORY(aul AUL_SRCS) @@ -100,6 +100,7 @@ APPLY_PKG_CONFIG(${TARGET_AUL} PUBLIC LIBSMACK_DEPS LIBTZPLATFORM_CONFIG_DEPS LIBXML_DEPS + PARCEL_DEPS PKGMGR_INFO_DEPS STORAGE_DEPS TTRACE_DEPS diff --git a/aul/socket/client.cc b/aul/socket/client.cc index b5cef4c..879dc8d 100644 --- a/aul/socket/client.cc +++ b/aul/socket/client.cc @@ -19,7 +19,6 @@ #include "aul/common/exception.hh" #include "aul/common/log_private.hh" #include "aul/socket/client.hh" -#include "aul/socket/parcel.hh" namespace aul { @@ -49,7 +48,9 @@ Client::Client(std::string path, int timeout_msec) : Socket(std::move(path)) { int Client::Send(const Packet& packet) { _W("cmd(%d)", packet.GetCmd()); - auto raw = const_cast(packet).GetRaw(); + tizen_base::Parcel parcel; + parcel.WriteParcelable(packet); + auto raw = parcel.GetRaw(); return Socket::Send(reinterpret_cast(&raw[0]), raw.size()); } diff --git a/aul/socket/packet.cc b/aul/socket/packet.cc index d312441..4577669 100644 --- a/aul/socket/packet.cc +++ b/aul/socket/packet.cc @@ -17,7 +17,6 @@ #include #include "aul/socket/packet.hh" -#include "aul/socket/parcel.hh" namespace aul { @@ -36,12 +35,8 @@ Packet::Packet(int cmd, int opt, tizen_base::Bundle data) } Packet::Packet(const unsigned char* buf, unsigned int size) { - Parcel parcel(buf, size); - cmd_ = parcel.ReadInt32(); - int len = parcel.ReadInt32(); - opt_ = parcel.ReadInt32(); - auto* p = reinterpret_cast(&data_[0]); - parcel.Read(p, len); + tizen_base::Parcel parcel(buf, size); + parcel.ReadParcelable(this); } Packet::~Packet() = default; @@ -76,15 +71,25 @@ tizen_base::Bundle Packet::DataToBundle() { return tizen_base::Bundle(b, false, true); } -const std::vector& Packet::GetRaw() { - if (parcel_.GetRaw().size() == 0) { - parcel_.WriteInt32(cmd_); - parcel_.WriteInt32(data_.size()); - parcel_.WriteInt32(opt_); +void Packet::WriteToParcel(tizen_base::Parcel* parcel) const { + parcel->WriteInt32(cmd_); + parcel->WriteInt32(data_.size()); + parcel->WriteInt32(opt_); + if (data_.size() > 0) { + auto* p = reinterpret_cast(&data_[0]); + parcel->Write(p, data_.size()); + } +} + +void Packet::ReadFromParcel(tizen_base::Parcel* parcel) { + parcel->ReadInt32(&cmd_); + int size = 0; + parcel->ReadInt32(&size); + parcel->ReadInt32(&opt_); + if (size > 0) { auto* p = reinterpret_cast(&data_[0]); - parcel_.Write(p, data_.size()); + parcel->Read(p, size); } - return parcel_.GetRaw(); } } // namespace aul diff --git a/aul/socket/packet.hh b/aul/socket/packet.hh index 0cc3245..7b61e0d 100644 --- a/aul/socket/packet.hh +++ b/aul/socket/packet.hh @@ -18,14 +18,14 @@ #define AUL_SOCKET_PACKET_HH_ #include +#include +#include #include -#include "aul/socket/parcel.hh" - namespace aul { -class Packet { +class Packet : public tizen_base::Parcelable { public: Packet(); Packet(int cmd, int opt, std::vector data); @@ -43,13 +43,13 @@ class Packet { tizen_base::Bundle DataToBundle(); - const std::vector& GetRaw(); + void WriteToParcel(tizen_base::Parcel* parcel) const override; + void ReadFromParcel(tizen_base::Parcel* parcel) override; private: int cmd_; int opt_; std::vector data_; - Parcel parcel_; }; } // namespace aul diff --git a/aul/socket/parcel.cc b/aul/socket/parcel.cc deleted file mode 100644 index a860339..0000000 --- a/aul/socket/parcel.cc +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "aul/socket/parcel.hh" - -namespace aul { - -Parcel::Parcel(const unsigned char* buf, unsigned int size) - : data_(buf, buf + size) { -} - -void Parcel::Write(const unsigned char* buf, unsigned int size) { - std::copy(buf, buf + size, std::back_inserter(data_)); -} - -void Parcel::Read(unsigned char* buf, unsigned int size) { - if (reader_ + size > data_.size()) - return; - - std::copy(&data_[reader_], &data_[reader_] + size, buf); - reader_ += size; -} - -void Parcel::WriteInt32(int i) { - Write(i); -} - -int Parcel::ReadInt32() { - return Read(); -} - -const std::vector& Parcel::GetRaw() { - return data_; -} - -} // namespace aul diff --git a/aul/socket/parcel.hh b/aul/socket/parcel.hh deleted file mode 100644 index 3358353..0000000 --- a/aul/socket/parcel.hh +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef AUL_SOCKET_PARCEL_HH_ -#define AUL_SOCKET_PARCEL_HH_ - -#include - -namespace aul { - -class Parcel { - public: - Parcel() = default; - Parcel(const unsigned char* buf, unsigned int size); - - void Write(const unsigned char* buf, unsigned int size); - void Read(unsigned char* buf, unsigned int size); - void WriteInt32(int i); - int ReadInt32(); - const std::vector& GetRaw(); - - private: - template - void Write(T d) { - unsigned char* p = reinterpret_cast(&d); - std::copy(p, p + sizeof(T), std::back_inserter(data_)); - } - - template - T Read() { - T d = 0; - unsigned char* p = reinterpret_cast(&d); - if (reader_ + sizeof(T) > data_.size()) - return d; - - std::copy(&data_[reader_], &data_[reader_] + sizeof(T), p); - reader_ += sizeof(T); - return d; - } - - private: - std::vector data_; - unsigned int reader_ = 0; -}; - -} // namespace aul - -#endif // AUL_SOCKET_PARCEL_HH_ diff --git a/packaging/aul.spec b/packaging/aul.spec index 03762c0..55e3f90 100644 --- a/packaging/aul.spec +++ b/packaging/aul.spec @@ -35,6 +35,7 @@ BuildRequires: pkgconfig(libxml-2.0) BuildRequires: pkgconfig(uuid) BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(gmock) +BuildRequires: pkgconfig(parcel) %if 0%{?gcov:1} BuildRequires: lcov -- 2.7.4