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   Result<void> Init(int casimir_rf_port);
34 
35   /*
36    * Poll for NFC-A + ISO-DEP
37    */
38   Result<uint16_t> Poll();
39 
40   Result<std::shared_ptr<std::vector<uint8_t>>> SendApdu(
41       uint16_t receiver_id, const std::shared_ptr<std::vector<uint8_t>>& apdu);
42 
43  private:
44   /*
45    * Select NFC-A, and returns sender id.
46    */
47   Result<uint16_t> SelectNfcA();
48 
49   /*
50    * Select T4AT by choosing NFC-A listener using ISO-DEP protocol (Type-4A Tag
51    * platform).
52    */
53   Result<void> SelectT4AT(uint16_t sender_id);
54 
55   Result<void> Write(const RfPacketBuilder& rf_packet);
56   Result<std::shared_ptr<std::vector<uint8_t>>> ReadExact(
57       size_t size, std::chrono::milliseconds timeout);
58   Result<std::shared_ptr<std::vector<uint8_t>>> ReadRfPacket(
59       std::chrono::milliseconds timeout);
60 
61  private:
62   SharedFD sock_;
63 };
64 
65 }  // namespace cuttlefish