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 #pragma once
18 
19 #include <vector>
20 
21 #include "common/libs/fs/shared_buf.h"
22 #include "common/libs/fs/shared_fd.h"
23 #include "common/libs/utils/result.h"
24 
25 #include "rf_packets.h"
26 
27 namespace cuttlefish {
28 
29 using namespace casimir::rf;
30 
31 class CasimirController {
32  public:
33   static Result<CasimirController> ConnectToTcpPort(int rf_port);
34   static Result<CasimirController> ConnectToUnixSocket(const std::string& rf);
35 
36   Result<void> Mute();
37   Result<void> Unmute();
38 
39   Result<void> SetPowerLevel(uint32_t power_level);
40 
41   /*
42    * Poll for NFC-A + ISO-DEP
43    */
44   Result<uint16_t> Poll();
45 
46   Result<std::vector<uint8_t>> SendApdu(uint16_t receiver_id,
47                                         std::vector<uint8_t> apdu);
48 
49  private:
50   CasimirController(SharedFD sock);
51   /*
52    * Select NFC-A, and returns sender id.
53    */
54   Result<uint16_t> SelectNfcA();
55 
56   /*
57    * Select T4AT by choosing NFC-A listener using ISO-DEP protocol (Type-4A Tag
58    * platform).
59    */
60   Result<void> SelectT4AT(uint16_t sender_id);
61 
62   Result<void> Write(const RfPacketBuilder& rf_packet);
63   Result<std::shared_ptr<std::vector<uint8_t>>> ReadExact(
64       size_t size, std::chrono::milliseconds timeout);
65   Result<std::shared_ptr<std::vector<uint8_t>>> ReadRfPacket(
66       std::chrono::milliseconds timeout);
67 
68   SharedFD sock_;
69   uint8_t power_level;
70 };
71 
72 }  // namespace cuttlefish
73