summaryrefslogtreecommitdiff
path: root/trunks/trunks_binder_service.cc
blob: 028aa3c744a4c478fc8d425f90645439607e6ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Copyright (C) 2016 The Android Open Source Project
//
// 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 "trunks/trunks_binder_service.h"

#include <sysexits.h>

#include <base/bind.h>
#include <binderwrapper/binder_wrapper.h>

#include "trunks/binder_interface.h"
#include "trunks/command_transceiver.h"
#include "trunks/error_codes.h"
#include "trunks/interface.pb.h"

namespace {

// If |command| is a valid command protobuf, provides the |command_data| and
// returns true. Otherwise, returns false.
bool ParseCommandProto(const std::vector<int8_t>& command,
                       std::string* command_data) {
  trunks::SendCommandRequest request_proto;
  if (!request_proto.ParseFromArray(command.data(), command.size()) ||
      !request_proto.has_command() || request_proto.command().empty()) {
    return false;
  }
  *command_data = request_proto.command();
  return true;
}

void CreateResponseProto(const std::string& data,
                         std::vector<int8_t>* response) {
  trunks::SendCommandResponse response_proto;
  response_proto.set_response(data);
  response->resize(response_proto.ByteSize());
  CHECK(response_proto.SerializeToArray(response->data(), response->size()))
      << "TrunksBinderService: Failed to serialize protobuf.";
}

}  // namespace

namespace trunks {

int TrunksBinderService::OnInit() {
  android::BinderWrapper::Create();
  if (!watcher_.Init()) {
    LOG(ERROR) << "TrunksBinderService: BinderWatcher::Init failed.";
    return EX_UNAVAILABLE;
  }
  binder_ = new BinderServiceInternal(this);
  if (!android::BinderWrapper::Get()->RegisterService(
          kTrunksServiceName, android::IInterface::asBinder(binder_))) {
    LOG(ERROR) << "TrunksBinderService: RegisterService failed.";
    return EX_UNAVAILABLE;
  }
  LOG(INFO) << "Trunks: Binder service registered.";
  return brillo::Daemon::OnInit();
}

TrunksBinderService::BinderServiceInternal::BinderServiceInternal(
    TrunksBinderService* service)
    : service_(service) {}

android::binder::Status TrunksBinderService::BinderServiceInternal::SendCommand(
    const std::vector<int8_t>& command,
    const android::sp<android::trunks::ITrunksClient>& client) {
  auto callback =
      base::Bind(&TrunksBinderService::BinderServiceInternal::OnResponse,
                 GetWeakPtr(), client);
  std::string command_data;
  if (!ParseCommandProto(command, &command_data)) {
    LOG(ERROR) << "TrunksBinderService: Bad command data.";
    callback.Run(CreateErrorResponse(SAPI_RC_BAD_PARAMETER));
    return android::binder::Status::ok();
  }
  service_->transceiver_->SendCommand(command_data, callback);
  return android::binder::Status::ok();
}

void TrunksBinderService::BinderServiceInternal::OnResponse(
    const android::sp<android::trunks::ITrunksClient>& client,
    const std::string& response) {
  std::vector<int8_t> binder_response;
  CreateResponseProto(response, &binder_response);
  android::binder::Status status = client->OnCommandResponse(binder_response);
  if (!status.isOk()) {
    LOG(ERROR) << "TrunksBinderService: Failed to send response to client: "
               << status.toString8();
  }
}

android::binder::Status
TrunksBinderService::BinderServiceInternal::SendCommandAndWait(
    const std::vector<int8_t>& command,
    std::vector<int8_t>* response) {
  std::string command_data;
  if (!ParseCommandProto(command, &command_data)) {
    LOG(ERROR) << "TrunksBinderService: Bad command data.";
    CreateResponseProto(CreateErrorResponse(SAPI_RC_BAD_PARAMETER), response);
    return android::binder::Status::ok();
  }
  CreateResponseProto(service_->transceiver_->SendCommandAndWait(command_data),
                      response);
  return android::binder::Status::ok();
}

}  // namespace trunks