Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MidiIoWrapper" |
| 19 | #include <utils/Log.h> |
| 20 | #include <utils/RefBase.h> |
| 21 | |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
| 24 | |
Pawin Vongmasa | 255735a | 2017-07-19 11:24:56 -0700 | [diff] [blame] | 25 | #include <media/MidiIoWrapper.h> |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 26 | |
| 27 | static int readAt(void *handle, void *buffer, int pos, int size) { |
| 28 | return ((android::MidiIoWrapper*)handle)->readAt(buffer, pos, size); |
| 29 | } |
| 30 | static int size(void *handle) { |
| 31 | return ((android::MidiIoWrapper*)handle)->size(); |
| 32 | } |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | MidiIoWrapper::MidiIoWrapper(const char *path) { |
| 37 | ALOGV("MidiIoWrapper(%s)", path); |
| 38 | mFd = open(path, O_RDONLY | O_LARGEFILE); |
| 39 | mBase = 0; |
| 40 | mLength = lseek(mFd, 0, SEEK_END); |
Dongwon Kang | 04ce77f | 2018-02-13 00:27:24 -0800 | [diff] [blame^] | 41 | mDataSource = nullptr; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) { |
| 45 | ALOGV("MidiIoWrapper(fd=%d)", fd); |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 46 | mFd = fd < 0 ? -1 : dup(fd); |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 47 | mBase = offset; |
| 48 | mLength = size; |
Dongwon Kang | 04ce77f | 2018-02-13 00:27:24 -0800 | [diff] [blame^] | 49 | mDataSource = nullptr; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 52 | MidiIoWrapper::MidiIoWrapper(DataSourceBase *source) { |
Marco Nelissen | 13b97d6 | 2015-01-29 11:16:17 -0800 | [diff] [blame] | 53 | ALOGV("MidiIoWrapper(DataSource)"); |
| 54 | mFd = -1; |
Marco Nelissen | 0e8928b | 2015-01-08 13:40:53 -0800 | [diff] [blame] | 55 | mDataSource = source; |
| 56 | off64_t l; |
| 57 | if (mDataSource->getSize(&l) == OK) { |
| 58 | mLength = l; |
| 59 | } else { |
| 60 | mLength = 0; |
| 61 | } |
| 62 | } |
| 63 | |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 64 | MidiIoWrapper::~MidiIoWrapper() { |
| 65 | ALOGV("~MidiIoWrapper"); |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 66 | if (mFd >= 0) { |
| 67 | close(mFd); |
| 68 | } |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int MidiIoWrapper::readAt(void *buffer, int offset, int size) { |
| 72 | ALOGV("readAt(%p, %d, %d)", buffer, offset, size); |
Marco Nelissen | 0e8928b | 2015-01-08 13:40:53 -0800 | [diff] [blame] | 73 | |
| 74 | if (mDataSource != NULL) { |
| 75 | return mDataSource->readAt(offset, buffer, size); |
| 76 | } |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 77 | if (mFd < 0) { |
| 78 | errno = EBADF; |
| 79 | return -1; // as per failed read. |
| 80 | } |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 81 | lseek(mFd, mBase + offset, SEEK_SET); |
| 82 | if (offset + size > mLength) { |
| 83 | size = mLength - offset; |
| 84 | } |
| 85 | return read(mFd, buffer, size); |
| 86 | } |
| 87 | |
| 88 | int MidiIoWrapper::size() { |
Marco Nelissen | 13b97d6 | 2015-01-29 11:16:17 -0800 | [diff] [blame] | 89 | ALOGV("size() = %d", int(mLength)); |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 90 | return mLength; |
| 91 | } |
| 92 | |
| 93 | EAS_FILE_LOCATOR MidiIoWrapper::getLocator() { |
| 94 | mEasFile.handle = this; |
| 95 | mEasFile.readAt = ::readAt; |
| 96 | mEasFile.size = ::size; |
| 97 | return &mEasFile; |
| 98 | } |
| 99 | |
| 100 | } // namespace android |