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 Result<std::tuple<std::vector<uint8_t>, std::string, bool, uint32_t, uint32_t, 42 uint32_t, double>> 43 SendBroadcast(std::vector<uint8_t> data, std::string type, bool crc, 44 uint8_t bits, uint32_t bitrate, uint32_t timeout, double power); 45 46 /* 47 * Poll for NFC-A + ISO-DEP 48 */ 49 Result<uint16_t> Poll(); 50 51 Result<std::vector<uint8_t>> SendApdu(uint16_t receiver_id, 52 std::vector<uint8_t> apdu); 53 54 private: 55 CasimirController(SharedFD sock); 56 /* 57 * Select NFC-A, and returns sender id. 58 */ 59 Result<uint16_t> SelectNfcA(); 60 61 /* 62 * Select T4AT by choosing NFC-A listener using ISO-DEP protocol (Type-4A Tag 63 * platform). 64 */ 65 Result<void> SelectT4AT(uint16_t sender_id); 66 67 Result<void> Write(const RfPacketBuilder& rf_packet); 68 Result<std::shared_ptr<std::vector<uint8_t>>> ReadExact( 69 size_t size, std::chrono::microseconds timeout); 70 Result<std::shared_ptr<std::vector<uint8_t>>> ReadRfPacket( 71 std::chrono::microseconds timeout); 72 73 SharedFD sock_; 74 uint8_t power_level; 75 }; 76 77 } // namespace cuttlefish 78