• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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 <iostream>
18 #include <memory>
19 #include <string>
20 
21 #include <android-base/hex.h>
22 #include <gflags/gflags.h>
23 #include <grpcpp/ext/proto_server_reflection_plugin.h>
24 #include <grpcpp/grpcpp.h>
25 #include <grpcpp/health_check_service_interface.h>
26 
27 #include "casimir_control.grpc.pb.h"
28 #include "casimir_controller.h"
29 #include "utils.h"
30 
31 using casimircontrolserver::CasimirControlService;
32 using casimircontrolserver::SendApduReply;
33 using casimircontrolserver::SendApduRequest;
34 
35 using cuttlefish::CasimirController;
36 
37 using google::protobuf::Empty;
38 using grpc::Server;
39 using grpc::ServerBuilder;
40 using grpc::ServerContext;
41 using grpc::Status;
42 using grpc::StatusCode;
43 using std::string;
44 using std::vector;
45 
46 DEFINE_string(grpc_uds_path, "", "grpc_uds_path");
47 DEFINE_int32(casimir_rf_port, -1, "RF port to control Casimir");
48 
49 class CasimirControlServiceImpl final : public CasimirControlService::Service {
SendApdu(ServerContext * context,const SendApduRequest * request,SendApduReply * response)50   Status SendApdu(ServerContext* context, const SendApduRequest* request,
51                   SendApduReply* response) override {
52     // Step 0: Parse input
53     std::vector<std::shared_ptr<std::vector<uint8_t>>> apdu_bytes;
54     for (int i = 0; i < request->apdu_hex_strings_size(); i++) {
55       auto apdu_bytes_res =
56           cuttlefish::BytesArray(request->apdu_hex_strings(i));
57       if (!apdu_bytes_res.ok()) {
58         LOG(ERROR) << "Failed to parse input " << request->apdu_hex_strings(i)
59                    << ", " << apdu_bytes_res.error().FormatForEnv();
60         return Status(StatusCode::INVALID_ARGUMENT,
61                       "Failed to parse input. Must only contain [0-9a-fA-F]");
62       }
63       apdu_bytes.push_back(apdu_bytes_res.value());
64     }
65 
66     // Step 1: Initialize connection with casimir
67     CasimirController device;
68     auto init_res = device.Init(FLAGS_casimir_rf_port);
69     if (!init_res.ok()) {
70       LOG(ERROR) << "Failed to initialize connection to casimir: "
71                  << init_res.error().FormatForEnv();
72       return Status(StatusCode::FAILED_PRECONDITION,
73                     "Failed to connect with casimir");
74     }
75 
76     // Step 2: Poll
77     auto poll_res = device.Poll();
78     if (!poll_res.ok()) {
79       LOG(ERROR) << "Failed to poll(): " << poll_res.error().FormatForEnv();
80       return Status(StatusCode::FAILED_PRECONDITION,
81                     "Failed to poll and select NFC-A and ISO-DEP");
82     }
83     uint16_t id = poll_res.value();
84 
85     // Step 3: Send APDU bytes
86     response->clear_response_hex_strings();
87     for (int i = 0; i < apdu_bytes.size(); i++) {
88       auto send_res = device.SendApdu(id, apdu_bytes[i]);
89       if (!send_res.ok()) {
90         LOG(ERROR) << "Failed to send APDU bytes: "
91                    << send_res.error().FormatForEnv();
92         return Status(StatusCode::UNKNOWN, "Failed to send APDU bytes");
93       }
94       auto bytes = *(send_res.value());
95       auto resp = android::base::HexString(
96           reinterpret_cast<void*>(bytes.data()), bytes.size());
97       response->add_response_hex_strings(resp);
98     }
99 
100     // Returns OK although returned bytes is valids if ends with [0x90, 0x00].
101     return Status::OK;
102   }
103 };
104 
RunServer()105 void RunServer() {
106   std::string server_address("unix:" + FLAGS_grpc_uds_path);
107   CasimirControlServiceImpl service;
108 
109   grpc::EnableDefaultHealthCheckService(true);
110   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
111   ServerBuilder builder;
112   // Listen on the given address without any authentication mechanism.
113   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
114   // Register "service" as the instance through which we'll communicate with
115   // clients. In this case it corresponds to an *synchronous* service.
116   builder.RegisterService(&service);
117   // Finally assemble the server.
118   std::unique_ptr<Server> server(builder.BuildAndStart());
119   std::cout << "Server listening on " << server_address << std::endl;
120 
121   // Wait for the server to shutdown. Note that some other thread must be
122   // responsible for shutting down the server for this call to ever return.
123   server->Wait();
124 }
125 
main(int argc,char ** argv)126 int main(int argc, char** argv) {
127   ::gflags::ParseCommandLineFlags(&argc, &argv, true);
128   RunServer();
129 
130   return 0;
131 }
132