2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "pkg_request.hh"
18 #include "pkgmgrinfo_debug.h"
20 namespace pkgmgr_server {
22 PkgRequest::PkgRequest(int fd)
23 : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1) {
24 socket_ = std::unique_ptr<pkgmgr_common::socket::DataSocket>(
25 new (std::nothrow) pkgmgr_common::socket::DataSocket(fd));
26 if (socket_ == nullptr)
27 LOGE("Out of memory");
30 PkgRequest::~PkgRequest() {
35 unsigned char* PkgRequest::GetData() {
39 int PkgRequest::GetSize() {
43 int PkgRequest::GetFd() {
44 return socket_->GetFd();
47 pid_t PkgRequest::GetSenderPID() {
48 return socket_->GetPID();
51 uid_t PkgRequest::GetSenderUID() {
52 return socket_->GetUID();
55 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
59 bool PkgRequest::ReceiveData() {
60 int ret = socket_->ReceiveData(&request_type_, sizeof(request_type_));
61 if (ret < 0 || request_type_ == pkgmgr_common::REQ_TYPE_NONE) {
62 LOGE("Failed to ReceiveData");
65 ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
67 LOGE("Failed to ReceiveData");
68 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
71 if (data_size_ <= 0) {
73 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
77 data_ = new (std::nothrow) unsigned char[data_size_];
78 if (data_ == nullptr) {
79 LOGE("Out of memory");
80 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
83 ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
85 LOGE("Failed to ReceiveData");
88 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
95 bool PkgRequest::SendData(unsigned char* data, int size) {
96 if (socket_->SendData(&size, sizeof(size)) < 0)
99 return (socket_->SendData(data, size) == 0);
102 } // namespace pkgmgr_server