1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include "TracingServerImpl.h"
18 #include "perfetto_trace.pb.h"
19
20 #include <android-base/logging.h>
21
22 #include <grpc++/grpc++.h>
23 #include <memory>
24 #include <string>
25
26 namespace android::tools::automotive::tracing {
27
getServerCredentials()28 static std::shared_ptr<::grpc::ServerCredentials> getServerCredentials() {
29 return ::grpc::InsecureServerCredentials();
30 }
31
TracingServerImpl(const std::string & addr)32 TracingServerImpl::TracingServerImpl(const std::string& addr) : mServiceAddr(addr) {}
33
Start()34 void TracingServerImpl::Start() {
35 LOG(INFO) << "mGrpcServer " << mGrpcServer << " starting at " << mServiceAddr << std::endl;
36 if (mGrpcServer) {
37 LOG(INFO) << __func__ << " GRPC Server is running.";
38 return;
39 }
40
41 ::grpc::ServerBuilder builder;
42 builder.AddListeningPort(mServiceAddr, getServerCredentials());
43 builder.RegisterService(this);
44 mGrpcServer = builder.BuildAndStart();
45
46 if (!mGrpcServer) {
47 LOG(ERROR) << __func__ << ": failed to create the GRPC server, "
48 << "please make sure the configuration and permissions are correct.";
49 return;
50 }
51 LOG(INFO) << __func__ << " start server";
52 mGrpcServer->Wait();
53 }
54
StartTracing(::grpc::ServerContext * context,const tracing_vm_proto::StartTracingRequest * request,tracing_vm_proto::RequestStatus * request_status)55 grpc::Status TracingServerImpl::StartTracing(::grpc::ServerContext* context,
56 const tracing_vm_proto::StartTracingRequest* request,
57 tracing_vm_proto::RequestStatus* request_status) {
58 LOG(INFO) << "Received StartTracing rquest";
59 request_status->mutable_session_id()->set_session_id(mSessionId.fetch_add(1));
60 request_status->set_is_ok(true);
61 return ::grpc::Status::OK;
62 }
63
StopTracing(::grpc::ServerContext * context,const tracing_vm_proto::TracingSessionIdentifier * session_id,tracing_vm_proto::RequestStatus * request_status)64 grpc::Status TracingServerImpl::StopTracing(
65 ::grpc::ServerContext* context,
66 const tracing_vm_proto::TracingSessionIdentifier* session_id,
67 tracing_vm_proto::RequestStatus* request_status) {
68 LOG(INFO) << "Received StopTracing request";
69 request_status->set_is_ok(true);
70 return ::grpc::Status::OK;
71 }
72
GetTracingFile(::grpc::ServerContext * context,const tracing_vm_proto::TracingSessionIdentifier * session_id,::grpc::ServerWriter<tracing_vm_proto::TracingFileBuffer> * stream)73 grpc::Status TracingServerImpl::GetTracingFile(
74 ::grpc::ServerContext* context,
75 const tracing_vm_proto::TracingSessionIdentifier* session_id,
76 ::grpc::ServerWriter<tracing_vm_proto::TracingFileBuffer>* stream) {
77 LOG(INFO) << "Received GetTracingFile request";
78 // TODO: b/222110159 maintain sessions information and validate session id.
79 if (session_id->session_id() == 0) {
80 LOG(ERROR) << "Received invalid session_id 0.";
81 return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "");
82 }
83
84 tracing_vm_proto::TracingFileBuffer buffer;
85 std::string data = "Test data";
86 buffer.set_buffer(data);
87 stream->Write(buffer);
88 return ::grpc::Status::OK;
89 }
90
91 } // namespace android::tools::automotive::tracing
92