1 /* 2 * Copyright (C) 2020 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 #ifndef _MTP_MOCK_HANDLE_H 18 #define _MTP_MOCK_HANDLE_H 19 20 #include <vector> 21 22 typedef std::vector<uint8_t> packet_t; 23 24 namespace android { 25 class MtpMockHandle : public IMtpHandle { 26 private: 27 size_t mPacketNumber; 28 size_t mPacketOffset; 29 std::vector<packet_t> mPackets; 30 31 public: MtpMockHandle()32 MtpMockHandle() : mPacketNumber(0), mPacketOffset(0) {} 33 add_packet(packet_t pkt)34 void add_packet(packet_t pkt) { mPackets.push_back(pkt); } 35 36 // Return number of bytes read/written, or -1 and errno is set read(void * data,size_t len)37 int read(void *data, size_t len) { 38 if (mPacketNumber >= mPackets.size()) { 39 return 0; 40 } else { 41 int readAmt = 0; 42 packet_t pkt = mPackets[mPacketNumber]; 43 44 ALOGD("%s: sz %zu, pkt %zu+%zu/%zu\n", __func__, len, mPacketNumber, mPacketOffset, 45 pkt.size()); 46 47 // packet is bigger than what the caller can handle, 48 if (pkt.size() > len) { 49 memcpy(data, pkt.data() + mPacketOffset, len); 50 51 mPacketOffset += len; 52 readAmt = len; 53 // packet is equal or smaller than the caller buffer 54 } else { 55 memcpy(data, pkt.data() + mPacketOffset, pkt.size()); 56 57 mPacketNumber++; 58 mPacketOffset = 0; 59 readAmt = pkt.size(); 60 } 61 62 return readAmt; 63 } 64 } write(const void * data,size_t len)65 int write(const void *data, size_t len) { 66 ALOGD("MockHandle %s: len=%zu\n", __func__, len); 67 // fake the write 68 return len; 69 } 70 71 // Return 0 if send/receive is successful, or -1 and errno is set receiveFile(mtp_file_range mfr,bool zero_packet)72 int receiveFile(mtp_file_range mfr, bool zero_packet) { 73 ALOGD("MockHandle %s\n", __func__); 74 return 0; 75 } sendFile(mtp_file_range mfr)76 int sendFile(mtp_file_range mfr) { 77 ALOGD("MockHandle %s\n", __func__); 78 return 0; 79 } sendEvent(mtp_event me)80 int sendEvent(mtp_event me) { 81 ALOGD("MockHandle %s: len=%zu\n", __func__, me.length); 82 return 0; 83 } 84 85 // Return 0 if operation is successful, or -1 else start(bool ptp)86 int start(bool ptp) { return 0; } 87 close()88 void close() {} 89 ~MtpMockHandle()90 virtual ~MtpMockHandle() {} 91 }; 92 }; // namespace android 93 94 #endif // _MTP_MOCK_HANDLE_H 95