Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <errno.h> |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <dirent.h> |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 26 | |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
| 28 | |
Mike Lockwood | 9f67924 | 2010-09-23 22:32:05 -0400 | [diff] [blame] | 29 | #define LOG_TAG "MtpServer" |
| 30 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 31 | #include "MtpDebug.h" |
Mike Lockwood | c5c7853 | 2010-07-09 10:45:22 -0400 | [diff] [blame] | 32 | #include "MtpDatabase.h" |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 33 | #include "MtpProperty.h" |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 34 | #include "MtpServer.h" |
| 35 | #include "MtpStorage.h" |
| 36 | #include "MtpStringBuffer.h" |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 37 | |
Mike Lockwood | 9c7fdf5 | 2010-07-15 13:36:52 -0400 | [diff] [blame] | 38 | #include <linux/usb/f_mtp.h> |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 39 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 42 | static const MtpOperationCode kSupportedOperationCodes[] = { |
| 43 | MTP_OPERATION_GET_DEVICE_INFO, |
| 44 | MTP_OPERATION_OPEN_SESSION, |
| 45 | MTP_OPERATION_CLOSE_SESSION, |
| 46 | MTP_OPERATION_GET_STORAGE_IDS, |
| 47 | MTP_OPERATION_GET_STORAGE_INFO, |
| 48 | MTP_OPERATION_GET_NUM_OBJECTS, |
| 49 | MTP_OPERATION_GET_OBJECT_HANDLES, |
| 50 | MTP_OPERATION_GET_OBJECT_INFO, |
| 51 | MTP_OPERATION_GET_OBJECT, |
| 52 | // MTP_OPERATION_GET_THUMB, |
| 53 | MTP_OPERATION_DELETE_OBJECT, |
| 54 | MTP_OPERATION_SEND_OBJECT_INFO, |
| 55 | MTP_OPERATION_SEND_OBJECT, |
| 56 | // MTP_OPERATION_INITIATE_CAPTURE, |
| 57 | // MTP_OPERATION_FORMAT_STORE, |
| 58 | // MTP_OPERATION_RESET_DEVICE, |
| 59 | // MTP_OPERATION_SELF_TEST, |
| 60 | // MTP_OPERATION_SET_OBJECT_PROTECTION, |
| 61 | // MTP_OPERATION_POWER_DOWN, |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame] | 62 | MTP_OPERATION_GET_DEVICE_PROP_DESC, |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 63 | MTP_OPERATION_GET_DEVICE_PROP_VALUE, |
| 64 | MTP_OPERATION_SET_DEVICE_PROP_VALUE, |
| 65 | MTP_OPERATION_RESET_DEVICE_PROP_VALUE, |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 66 | // MTP_OPERATION_TERMINATE_OPEN_CAPTURE, |
| 67 | // MTP_OPERATION_MOVE_OBJECT, |
| 68 | // MTP_OPERATION_COPY_OBJECT, |
| 69 | // MTP_OPERATION_GET_PARTIAL_OBJECT, |
| 70 | // MTP_OPERATION_INITIATE_OPEN_CAPTURE, |
| 71 | MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED, |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 72 | MTP_OPERATION_GET_OBJECT_PROP_DESC, |
Mike Lockwood | d3bfecb | 2010-09-23 23:04:28 -0400 | [diff] [blame] | 73 | MTP_OPERATION_GET_OBJECT_PROP_VALUE, |
| 74 | MTP_OPERATION_SET_OBJECT_PROP_VALUE, |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 75 | MTP_OPERATION_GET_OBJECT_PROP_LIST, |
| 76 | // MTP_OPERATION_SET_OBJECT_PROP_LIST, |
| 77 | // MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC, |
| 78 | // MTP_OPERATION_SEND_OBJECT_PROP_LIST, |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 79 | MTP_OPERATION_GET_OBJECT_REFERENCES, |
| 80 | MTP_OPERATION_SET_OBJECT_REFERENCES, |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 81 | // MTP_OPERATION_SKIP, |
| 82 | }; |
| 83 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 84 | static const MtpEventCode kSupportedEventCodes[] = { |
| 85 | MTP_EVENT_OBJECT_ADDED, |
| 86 | MTP_EVENT_OBJECT_REMOVED, |
| 87 | }; |
| 88 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 89 | MtpServer::MtpServer(int fd, MtpDatabase* database, |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 90 | int fileGroup, int filePerm, int directoryPerm) |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 91 | : mFD(fd), |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 92 | mDatabase(database), |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 93 | mFileGroup(fileGroup), |
| 94 | mFilePermission(filePerm), |
| 95 | mDirectoryPermission(directoryPerm), |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 96 | mSessionID(0), |
| 97 | mSessionOpen(false), |
| 98 | mSendObjectHandle(kInvalidObjectHandle), |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 99 | mSendObjectFormat(0), |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 100 | mSendObjectFileSize(0) |
| 101 | { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | MtpServer::~MtpServer() { |
| 105 | } |
| 106 | |
| 107 | void MtpServer::addStorage(const char* filePath) { |
| 108 | int index = mStorages.size() + 1; |
| 109 | index |= index << 16; // set high and low part to our index |
| 110 | MtpStorage* storage = new MtpStorage(index, filePath, mDatabase); |
| 111 | addStorage(storage); |
| 112 | } |
| 113 | |
| 114 | MtpStorage* MtpServer::getStorage(MtpStorageID id) { |
| 115 | for (int i = 0; i < mStorages.size(); i++) { |
| 116 | MtpStorage* storage = mStorages[i]; |
| 117 | if (storage->getStorageID() == id) |
| 118 | return storage; |
| 119 | } |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 123 | void MtpServer::run() { |
| 124 | int fd = mFD; |
| 125 | |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 126 | LOGV("MtpServer::run fd: %d\n", fd); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 127 | |
| 128 | while (1) { |
| 129 | int ret = mRequest.read(fd); |
| 130 | if (ret < 0) { |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 131 | LOGE("request read returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 132 | if (errno == ECANCELED) { |
| 133 | // return to top of loop and wait for next command |
| 134 | continue; |
| 135 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | MtpOperationCode operation = mRequest.getOperationCode(); |
| 139 | MtpTransactionID transaction = mRequest.getTransactionID(); |
| 140 | |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 141 | LOGV("operation: %s", MtpDebug::getOperationCodeName(operation)); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 142 | mRequest.dump(); |
| 143 | |
| 144 | // FIXME need to generalize this |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 145 | bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 146 | || operation == MTP_OPERATION_SET_OBJECT_REFERENCES |
| 147 | || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE |
| 148 | || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 149 | if (dataIn) { |
| 150 | int ret = mData.read(fd); |
| 151 | if (ret < 0) { |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 152 | LOGE("data read returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 153 | if (errno == ECANCELED) { |
| 154 | // return to top of loop and wait for next command |
| 155 | continue; |
| 156 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 157 | break; |
| 158 | } |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 159 | LOGV("received data:"); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 160 | mData.dump(); |
| 161 | } else { |
| 162 | mData.reset(); |
| 163 | } |
| 164 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 165 | if (handleRequest()) { |
| 166 | if (!dataIn && mData.hasData()) { |
| 167 | mData.setOperationCode(operation); |
| 168 | mData.setTransactionID(transaction); |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 169 | LOGV("sending data:"); |
Mike Lockwood | db77431 | 2010-10-11 17:31:44 -0400 | [diff] [blame] | 170 | mData.dump(); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 171 | ret = mData.write(fd); |
| 172 | if (ret < 0) { |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 173 | LOGE("request write returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 174 | if (errno == ECANCELED) { |
| 175 | // return to top of loop and wait for next command |
| 176 | continue; |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 181 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 182 | mResponse.setTransactionID(transaction); |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 183 | LOGV("sending response %04X", mResponse.getResponseCode()); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 184 | ret = mResponse.write(fd); |
Mike Lockwood | db77431 | 2010-10-11 17:31:44 -0400 | [diff] [blame] | 185 | mResponse.dump(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 186 | if (ret < 0) { |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 187 | LOGE("request write returned %d, errno: %d", ret, errno); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 188 | if (errno == ECANCELED) { |
| 189 | // return to top of loop and wait for next command |
| 190 | continue; |
| 191 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 192 | break; |
| 193 | } |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 194 | } else { |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 195 | LOGV("skipping response\n"); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 196 | } |
| 197 | } |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 198 | |
| 199 | if (mSessionOpen) |
| 200 | mDatabase->sessionEnded(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 203 | void MtpServer::sendObjectAdded(MtpObjectHandle handle) { |
Mike Lockwood | dc453d4 | 2010-07-19 14:29:58 -0400 | [diff] [blame] | 204 | if (mSessionOpen) { |
| 205 | LOGD("sendObjectAdded %d\n", handle); |
| 206 | mEvent.setEventCode(MTP_EVENT_OBJECT_ADDED); |
| 207 | mEvent.setTransactionID(mRequest.getTransactionID()); |
| 208 | mEvent.setParameter(1, handle); |
| 209 | int ret = mEvent.write(mFD); |
| 210 | LOGD("mEvent.write returned %d\n", ret); |
| 211 | } |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void MtpServer::sendObjectRemoved(MtpObjectHandle handle) { |
Mike Lockwood | dc453d4 | 2010-07-19 14:29:58 -0400 | [diff] [blame] | 215 | if (mSessionOpen) { |
| 216 | LOGD("sendObjectRemoved %d\n", handle); |
| 217 | mEvent.setEventCode(MTP_EVENT_OBJECT_REMOVED); |
| 218 | mEvent.setTransactionID(mRequest.getTransactionID()); |
| 219 | mEvent.setParameter(1, handle); |
| 220 | int ret = mEvent.write(mFD); |
| 221 | LOGD("mEvent.write returned %d\n", ret); |
| 222 | } |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 225 | bool MtpServer::handleRequest() { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 226 | MtpOperationCode operation = mRequest.getOperationCode(); |
| 227 | MtpResponseCode response; |
| 228 | |
| 229 | mResponse.reset(); |
| 230 | |
| 231 | if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) { |
| 232 | // FIXME - need to delete mSendObjectHandle from the database |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 233 | LOGE("expected SendObject after SendObjectInfo"); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 234 | mSendObjectHandle = kInvalidObjectHandle; |
| 235 | } |
| 236 | |
| 237 | switch (operation) { |
| 238 | case MTP_OPERATION_GET_DEVICE_INFO: |
| 239 | response = doGetDeviceInfo(); |
| 240 | break; |
| 241 | case MTP_OPERATION_OPEN_SESSION: |
| 242 | response = doOpenSession(); |
| 243 | break; |
| 244 | case MTP_OPERATION_CLOSE_SESSION: |
| 245 | response = doCloseSession(); |
| 246 | break; |
| 247 | case MTP_OPERATION_GET_STORAGE_IDS: |
| 248 | response = doGetStorageIDs(); |
| 249 | break; |
| 250 | case MTP_OPERATION_GET_STORAGE_INFO: |
| 251 | response = doGetStorageInfo(); |
| 252 | break; |
| 253 | case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED: |
| 254 | response = doGetObjectPropsSupported(); |
| 255 | break; |
| 256 | case MTP_OPERATION_GET_OBJECT_HANDLES: |
| 257 | response = doGetObjectHandles(); |
| 258 | break; |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 259 | case MTP_OPERATION_GET_NUM_OBJECTS: |
| 260 | response = doGetNumObjects(); |
| 261 | break; |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 262 | case MTP_OPERATION_GET_OBJECT_REFERENCES: |
| 263 | response = doGetObjectReferences(); |
| 264 | break; |
| 265 | case MTP_OPERATION_SET_OBJECT_REFERENCES: |
| 266 | response = doSetObjectReferences(); |
| 267 | break; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 268 | case MTP_OPERATION_GET_OBJECT_PROP_VALUE: |
| 269 | response = doGetObjectPropValue(); |
| 270 | break; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 271 | case MTP_OPERATION_SET_OBJECT_PROP_VALUE: |
| 272 | response = doSetObjectPropValue(); |
| 273 | break; |
| 274 | case MTP_OPERATION_GET_DEVICE_PROP_VALUE: |
| 275 | response = doGetDevicePropValue(); |
| 276 | break; |
| 277 | case MTP_OPERATION_SET_DEVICE_PROP_VALUE: |
| 278 | response = doSetDevicePropValue(); |
| 279 | break; |
| 280 | case MTP_OPERATION_RESET_DEVICE_PROP_VALUE: |
| 281 | response = doResetDevicePropValue(); |
| 282 | break; |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 283 | case MTP_OPERATION_GET_OBJECT_PROP_LIST: |
| 284 | response = doGetObjectPropList(); |
| 285 | break; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 286 | case MTP_OPERATION_GET_OBJECT_INFO: |
| 287 | response = doGetObjectInfo(); |
| 288 | break; |
| 289 | case MTP_OPERATION_GET_OBJECT: |
| 290 | response = doGetObject(); |
| 291 | break; |
| 292 | case MTP_OPERATION_SEND_OBJECT_INFO: |
| 293 | response = doSendObjectInfo(); |
| 294 | break; |
| 295 | case MTP_OPERATION_SEND_OBJECT: |
| 296 | response = doSendObject(); |
| 297 | break; |
| 298 | case MTP_OPERATION_DELETE_OBJECT: |
| 299 | response = doDeleteObject(); |
| 300 | break; |
| 301 | case MTP_OPERATION_GET_OBJECT_PROP_DESC: |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 302 | response = doGetObjectPropDesc(); |
| 303 | break; |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame] | 304 | case MTP_OPERATION_GET_DEVICE_PROP_DESC: |
| 305 | response = doGetDevicePropDesc(); |
| 306 | break; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 307 | default: |
Mike Lockwood | 9f67924 | 2010-09-23 22:32:05 -0400 | [diff] [blame] | 308 | LOGE("got unsupported command %s", MtpDebug::getOperationCodeName(operation)); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 309 | response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED; |
| 310 | break; |
| 311 | } |
| 312 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 313 | if (response == MTP_RESPONSE_TRANSACTION_CANCELLED) |
| 314 | return false; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 315 | mResponse.setResponseCode(response); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 316 | return true; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | MtpResponseCode MtpServer::doGetDeviceInfo() { |
| 320 | MtpStringBuffer string; |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 321 | char prop_value[PROPERTY_VALUE_MAX]; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 322 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 323 | MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats(); |
| 324 | MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats(); |
| 325 | MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties(); |
| 326 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 327 | // fill in device info |
| 328 | mData.putUInt16(MTP_STANDARD_VERSION); |
| 329 | mData.putUInt32(6); // MTP Vendor Extension ID |
| 330 | mData.putUInt16(MTP_STANDARD_VERSION); |
| 331 | string.set("microsoft.com: 1.0;"); |
| 332 | mData.putString(string); // MTP Extensions |
| 333 | mData.putUInt16(0); //Functional Mode |
| 334 | mData.putAUInt16(kSupportedOperationCodes, |
| 335 | sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 336 | mData.putAUInt16(kSupportedEventCodes, |
| 337 | sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 338 | mData.putAUInt16(deviceProperties); // Device Properties Supported |
| 339 | mData.putAUInt16(captureFormats); // Capture Formats |
| 340 | mData.putAUInt16(playbackFormats); // Playback Formats |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 341 | // FIXME |
| 342 | string.set("Google, Inc."); |
| 343 | mData.putString(string); // Manufacturer |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 344 | |
| 345 | property_get("ro.product.model", prop_value, "MTP Device"); |
| 346 | string.set(prop_value); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 347 | mData.putString(string); // Model |
| 348 | string.set("1.0"); |
| 349 | mData.putString(string); // Device Version |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 350 | |
| 351 | property_get("ro.serialno", prop_value, "????????"); |
| 352 | string.set(prop_value); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 353 | mData.putString(string); // Serial Number |
| 354 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 355 | delete playbackFormats; |
| 356 | delete captureFormats; |
| 357 | delete deviceProperties; |
| 358 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 359 | return MTP_RESPONSE_OK; |
| 360 | } |
| 361 | |
| 362 | MtpResponseCode MtpServer::doOpenSession() { |
| 363 | if (mSessionOpen) { |
| 364 | mResponse.setParameter(1, mSessionID); |
| 365 | return MTP_RESPONSE_SESSION_ALREADY_OPEN; |
| 366 | } |
| 367 | mSessionID = mRequest.getParameter(1); |
| 368 | mSessionOpen = true; |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 369 | |
| 370 | mDatabase->sessionStarted(); |
| 371 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 372 | return MTP_RESPONSE_OK; |
| 373 | } |
| 374 | |
| 375 | MtpResponseCode MtpServer::doCloseSession() { |
| 376 | if (!mSessionOpen) |
| 377 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 378 | mSessionID = 0; |
| 379 | mSessionOpen = false; |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 380 | mDatabase->sessionEnded(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 381 | return MTP_RESPONSE_OK; |
| 382 | } |
| 383 | |
| 384 | MtpResponseCode MtpServer::doGetStorageIDs() { |
| 385 | if (!mSessionOpen) |
| 386 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 387 | |
| 388 | int count = mStorages.size(); |
| 389 | mData.putUInt32(count); |
| 390 | for (int i = 0; i < count; i++) |
| 391 | mData.putUInt32(mStorages[i]->getStorageID()); |
| 392 | |
| 393 | return MTP_RESPONSE_OK; |
| 394 | } |
| 395 | |
| 396 | MtpResponseCode MtpServer::doGetStorageInfo() { |
| 397 | MtpStringBuffer string; |
| 398 | |
| 399 | if (!mSessionOpen) |
| 400 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 401 | MtpStorageID id = mRequest.getParameter(1); |
| 402 | MtpStorage* storage = getStorage(id); |
| 403 | if (!storage) |
| 404 | return MTP_RESPONSE_INVALID_STORAGE_ID; |
| 405 | |
| 406 | mData.putUInt16(storage->getType()); |
| 407 | mData.putUInt16(storage->getFileSystemType()); |
| 408 | mData.putUInt16(storage->getAccessCapability()); |
| 409 | mData.putUInt64(storage->getMaxCapacity()); |
| 410 | mData.putUInt64(storage->getFreeSpace()); |
| 411 | mData.putUInt32(1024*1024*1024); // Free Space in Objects |
| 412 | string.set(storage->getDescription()); |
| 413 | mData.putString(string); |
| 414 | mData.putEmptyString(); // Volume Identifier |
| 415 | |
| 416 | return MTP_RESPONSE_OK; |
| 417 | } |
| 418 | |
| 419 | MtpResponseCode MtpServer::doGetObjectPropsSupported() { |
| 420 | if (!mSessionOpen) |
| 421 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 422 | MtpObjectFormat format = mRequest.getParameter(1); |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 423 | MtpDevicePropertyList* properties = mDatabase->getSupportedObjectProperties(format); |
| 424 | mData.putAUInt16(properties); |
Mike Lockwood | 33ea5a4 | 2010-08-10 15:11:32 -0400 | [diff] [blame] | 425 | delete properties; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 426 | return MTP_RESPONSE_OK; |
| 427 | } |
| 428 | |
| 429 | MtpResponseCode MtpServer::doGetObjectHandles() { |
| 430 | if (!mSessionOpen) |
| 431 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 432 | MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage |
Mike Lockwood | 3743365 | 2010-05-19 15:12:14 -0400 | [diff] [blame] | 433 | MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 434 | MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent |
| 435 | // 0x00000000 for all objects? |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 436 | if (parent == 0xFFFFFFFF) |
| 437 | parent = 0; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 438 | |
| 439 | MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent); |
| 440 | mData.putAUInt32(handles); |
| 441 | delete handles; |
| 442 | return MTP_RESPONSE_OK; |
| 443 | } |
| 444 | |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 445 | MtpResponseCode MtpServer::doGetNumObjects() { |
| 446 | if (!mSessionOpen) |
| 447 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 448 | MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage |
| 449 | MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats |
| 450 | MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent |
| 451 | // 0x00000000 for all objects? |
| 452 | if (parent == 0xFFFFFFFF) |
| 453 | parent = 0; |
| 454 | |
| 455 | int count = mDatabase->getNumObjects(storageID, format, parent); |
| 456 | if (count >= 0) { |
| 457 | mResponse.setParameter(1, count); |
| 458 | return MTP_RESPONSE_OK; |
| 459 | } else { |
| 460 | mResponse.setParameter(1, 0); |
| 461 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
| 462 | } |
| 463 | } |
| 464 | |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 465 | MtpResponseCode MtpServer::doGetObjectReferences() { |
| 466 | if (!mSessionOpen) |
| 467 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 468 | MtpStorageID handle = mRequest.getParameter(1); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 469 | |
| 470 | // FIXME - check for invalid object handle |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 471 | MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 472 | if (handles) { |
| 473 | mData.putAUInt32(handles); |
| 474 | delete handles; |
| 475 | } else { |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 476 | mData.putEmptyArray(); |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 477 | } |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 478 | return MTP_RESPONSE_OK; |
| 479 | } |
| 480 | |
| 481 | MtpResponseCode MtpServer::doSetObjectReferences() { |
| 482 | if (!mSessionOpen) |
| 483 | return MTP_RESPONSE_SESSION_NOT_OPEN; |
| 484 | MtpStorageID handle = mRequest.getParameter(1); |
| 485 | MtpObjectHandleList* references = mData.getAUInt32(); |
| 486 | MtpResponseCode result = mDatabase->setObjectReferences(handle, references); |
| 487 | delete references; |
| 488 | return result; |
| 489 | } |
| 490 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 491 | MtpResponseCode MtpServer::doGetObjectPropValue() { |
| 492 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 493 | MtpObjectProperty property = mRequest.getParameter(2); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 494 | LOGD("GetObjectPropValue %d %s\n", handle, |
| 495 | MtpDebug::getObjectPropCodeName(property)); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 496 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 497 | return mDatabase->getObjectPropertyValue(handle, property, mData); |
| 498 | } |
| 499 | |
| 500 | MtpResponseCode MtpServer::doSetObjectPropValue() { |
| 501 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 502 | MtpObjectProperty property = mRequest.getParameter(2); |
| 503 | LOGD("SetObjectPropValue %d %s\n", handle, |
| 504 | MtpDebug::getObjectPropCodeName(property)); |
| 505 | |
| 506 | return mDatabase->setObjectPropertyValue(handle, property, mData); |
| 507 | } |
| 508 | |
| 509 | MtpResponseCode MtpServer::doGetDevicePropValue() { |
| 510 | MtpDeviceProperty property = mRequest.getParameter(1); |
| 511 | LOGD("GetDevicePropValue %s\n", |
| 512 | MtpDebug::getDevicePropCodeName(property)); |
| 513 | |
| 514 | return mDatabase->getDevicePropertyValue(property, mData); |
| 515 | } |
| 516 | |
| 517 | MtpResponseCode MtpServer::doSetDevicePropValue() { |
| 518 | MtpDeviceProperty property = mRequest.getParameter(1); |
| 519 | LOGD("SetDevicePropValue %s\n", |
| 520 | MtpDebug::getDevicePropCodeName(property)); |
| 521 | |
| 522 | return mDatabase->setDevicePropertyValue(property, mData); |
| 523 | } |
| 524 | |
| 525 | MtpResponseCode MtpServer::doResetDevicePropValue() { |
| 526 | MtpDeviceProperty property = mRequest.getParameter(1); |
| 527 | LOGD("ResetDevicePropValue %s\n", |
| 528 | MtpDebug::getDevicePropCodeName(property)); |
| 529 | |
| 530 | return mDatabase->resetDeviceProperty(property); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 533 | MtpResponseCode MtpServer::doGetObjectPropList() { |
| 534 | |
| 535 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 536 | MtpObjectFormat format = mRequest.getParameter(2); |
| 537 | MtpDeviceProperty property = mRequest.getParameter(3); |
| 538 | int groupCode = mRequest.getParameter(4); |
| 539 | int depth = mRequest.getParameter(4); |
| 540 | LOGD("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n", |
| 541 | handle, MtpDebug::getFormatCodeName(format), |
| 542 | MtpDebug::getObjectPropCodeName(property), groupCode, depth); |
| 543 | |
| 544 | return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData); |
| 545 | } |
| 546 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 547 | MtpResponseCode MtpServer::doGetObjectInfo() { |
| 548 | MtpObjectHandle handle = mRequest.getParameter(1); |
| 549 | return mDatabase->getObjectInfo(handle, mData); |
| 550 | } |
| 551 | |
| 552 | MtpResponseCode MtpServer::doGetObject() { |
| 553 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 554 | MtpString pathBuf; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 555 | int64_t fileLength; |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 556 | int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength); |
| 557 | if (result != MTP_RESPONSE_OK) |
| 558 | return result; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 559 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 560 | const char* filePath = (const char *)pathBuf; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 561 | mtp_file_range mfr; |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 562 | mfr.fd = open(filePath, O_RDONLY); |
| 563 | if (mfr.fd < 0) { |
| 564 | return MTP_RESPONSE_GENERAL_ERROR; |
| 565 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 566 | mfr.offset = 0; |
| 567 | mfr.length = fileLength; |
| 568 | |
| 569 | // send data header |
| 570 | mData.setOperationCode(mRequest.getOperationCode()); |
| 571 | mData.setTransactionID(mRequest.getTransactionID()); |
Mike Lockwood | db77431 | 2010-10-11 17:31:44 -0400 | [diff] [blame] | 572 | mData.writeDataHeader(mFD, fileLength + MTP_CONTAINER_HEADER_SIZE); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 573 | |
| 574 | // then transfer the file |
| 575 | int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr); |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 576 | close(mfr.fd); |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 577 | if (ret < 0) { |
| 578 | if (errno == ECANCELED) |
| 579 | return MTP_RESPONSE_TRANSACTION_CANCELLED; |
| 580 | else |
| 581 | return MTP_RESPONSE_GENERAL_ERROR; |
| 582 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 583 | return MTP_RESPONSE_OK; |
| 584 | } |
| 585 | |
| 586 | MtpResponseCode MtpServer::doSendObjectInfo() { |
| 587 | MtpString path; |
| 588 | MtpStorageID storageID = mRequest.getParameter(1); |
| 589 | MtpStorage* storage = getStorage(storageID); |
| 590 | MtpObjectHandle parent = mRequest.getParameter(2); |
| 591 | if (!storage) |
| 592 | return MTP_RESPONSE_INVALID_STORAGE_ID; |
| 593 | |
| 594 | // special case the root |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 595 | if (parent == MTP_PARENT_ROOT) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 596 | path = storage->getPath(); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 597 | parent = 0; |
| 598 | } else { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 599 | int64_t dummy; |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 600 | int result = mDatabase->getObjectFilePath(parent, path, dummy); |
| 601 | if (result != MTP_RESPONSE_OK) |
| 602 | return result; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // read only the fields we need |
| 606 | mData.getUInt32(); // storage ID |
| 607 | MtpObjectFormat format = mData.getUInt16(); |
| 608 | mData.getUInt16(); // protection status |
| 609 | mSendObjectFileSize = mData.getUInt32(); |
| 610 | mData.getUInt16(); // thumb format |
| 611 | mData.getUInt32(); // thumb compressed size |
| 612 | mData.getUInt32(); // thumb pix width |
| 613 | mData.getUInt32(); // thumb pix height |
| 614 | mData.getUInt32(); // image pix width |
| 615 | mData.getUInt32(); // image pix height |
| 616 | mData.getUInt32(); // image bit depth |
| 617 | mData.getUInt32(); // parent |
| 618 | uint16_t associationType = mData.getUInt16(); |
| 619 | uint32_t associationDesc = mData.getUInt32(); // association desc |
| 620 | mData.getUInt32(); // sequence number |
| 621 | MtpStringBuffer name, created, modified; |
| 622 | mData.getString(name); // file name |
| 623 | mData.getString(created); // date created |
| 624 | mData.getString(modified); // date modified |
| 625 | // keywords follow |
| 626 | |
Mike Lockwood | 0b58c19 | 2010-11-17 15:42:09 -0500 | [diff] [blame] | 627 | LOGD("name: %s format: %04X\n", (const char *)name, format); |
Mike Lockwood | d078267 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 628 | time_t modifiedTime; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 629 | if (!parseDateTime(modified, modifiedTime)) |
| 630 | modifiedTime = 0; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 631 | |
| 632 | if (path[path.size() - 1] != '/') |
| 633 | path += "/"; |
| 634 | path += (const char *)name; |
| 635 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 636 | MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, |
| 637 | format, parent, storageID, mSendObjectFileSize, modifiedTime); |
Mike Lockwood | d078267 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 638 | if (handle == kInvalidObjectHandle) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 639 | return MTP_RESPONSE_GENERAL_ERROR; |
Mike Lockwood | d078267 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 640 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 641 | |
| 642 | if (format == MTP_FORMAT_ASSOCIATION) { |
| 643 | mode_t mask = umask(0); |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 644 | int ret = mkdir((const char *)path, mDirectoryPermission); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 645 | umask(mask); |
| 646 | if (ret && ret != -EEXIST) |
| 647 | return MTP_RESPONSE_GENERAL_ERROR; |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 648 | chown((const char *)path, getuid(), mFileGroup); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 649 | } else { |
| 650 | mSendObjectFilePath = path; |
| 651 | // save the handle for the SendObject call, which should follow |
| 652 | mSendObjectHandle = handle; |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 653 | mSendObjectFormat = format; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | mResponse.setParameter(1, storageID); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 657 | mResponse.setParameter(2, parent); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 658 | mResponse.setParameter(3, handle); |
| 659 | |
| 660 | return MTP_RESPONSE_OK; |
| 661 | } |
| 662 | |
| 663 | MtpResponseCode MtpServer::doSendObject() { |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 664 | MtpResponseCode result = MTP_RESPONSE_OK; |
| 665 | mode_t mask; |
| 666 | int ret; |
Mike Lockwood | 413577d | 2010-11-16 17:38:43 -0500 | [diff] [blame^] | 667 | uint64_t actualSize = -1; |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 668 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 669 | if (mSendObjectHandle == kInvalidObjectHandle) { |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 670 | LOGE("Expected SendObjectInfo before SendObject"); |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 671 | result = MTP_RESPONSE_NO_VALID_OBJECT_INFO; |
| 672 | goto done; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // read the header |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 676 | ret = mData.readDataHeader(mFD); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 677 | // FIXME - check for errors here. |
| 678 | |
| 679 | // reset so we don't attempt to send this back |
| 680 | mData.reset(); |
| 681 | |
| 682 | mtp_file_range mfr; |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 683 | mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC); |
| 684 | if (mfr.fd < 0) { |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 685 | result = MTP_RESPONSE_GENERAL_ERROR; |
| 686 | goto done; |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 687 | } |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 688 | fchown(mfr.fd, getuid(), mFileGroup); |
| 689 | // set permissions |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 690 | mask = umask(0); |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 691 | fchmod(mfr.fd, mFilePermission); |
| 692 | umask(mask); |
| 693 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 694 | mfr.offset = 0; |
| 695 | mfr.length = mSendObjectFileSize; |
| 696 | |
Mike Lockwood | 0b58c19 | 2010-11-17 15:42:09 -0500 | [diff] [blame] | 697 | LOGD("receiving %s\n", (const char *)mSendObjectFilePath); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 698 | // transfer the file |
| 699 | ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr); |
Mike Lockwood | 42dbfa5 | 2010-06-22 15:03:53 -0400 | [diff] [blame] | 700 | close(mfr.fd); |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 701 | |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 702 | LOGV("MTP_RECEIVE_FILE returned %d", ret); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 703 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 704 | if (ret < 0) { |
| 705 | unlink(mSendObjectFilePath); |
| 706 | if (errno == ECANCELED) |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 707 | result = MTP_RESPONSE_TRANSACTION_CANCELLED; |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 708 | else |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 709 | result = MTP_RESPONSE_GENERAL_ERROR; |
Mike Lockwood | 413577d | 2010-11-16 17:38:43 -0500 | [diff] [blame^] | 710 | } else if (mSendObjectFileSize == 0xFFFFFFFF) { |
| 711 | // actual size is likely > 4 gig so stat the file to compute actual length |
| 712 | struct stat s; |
| 713 | if (lstat(mSendObjectFilePath, &s) == 0) { |
| 714 | actualSize = s.st_size; |
| 715 | LOGD("actualSize: %lld\n", actualSize); |
| 716 | } |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 717 | } |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 718 | |
| 719 | done: |
| 720 | mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat, |
Mike Lockwood | 413577d | 2010-11-16 17:38:43 -0500 | [diff] [blame^] | 721 | actualSize, result == MTP_RESPONSE_OK); |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 722 | mSendObjectHandle = kInvalidObjectHandle; |
| 723 | mSendObjectFormat = 0; |
| 724 | return result; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 725 | } |
| 726 | |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 727 | static void deleteRecursive(const char* path) { |
| 728 | char pathbuf[PATH_MAX]; |
| 729 | int pathLength = strlen(path); |
| 730 | if (pathLength >= sizeof(pathbuf) - 1) { |
| 731 | LOGE("path too long: %s\n", path); |
| 732 | } |
| 733 | strcpy(pathbuf, path); |
| 734 | if (pathbuf[pathLength - 1] != '/') { |
| 735 | pathbuf[pathLength++] = '/'; |
| 736 | } |
| 737 | char* fileSpot = pathbuf + pathLength; |
| 738 | int pathRemaining = sizeof(pathbuf) - pathLength - 1; |
| 739 | |
| 740 | DIR* dir = opendir(path); |
| 741 | if (!dir) { |
| 742 | LOGE("opendir %s failed: %s", path, strerror(errno)); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | struct dirent* entry; |
| 747 | while ((entry = readdir(dir))) { |
| 748 | const char* name = entry->d_name; |
| 749 | |
| 750 | // ignore "." and ".." |
| 751 | if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { |
| 752 | continue; |
| 753 | } |
| 754 | |
| 755 | int nameLength = strlen(name); |
| 756 | if (nameLength > pathRemaining) { |
| 757 | LOGE("path %s/%s too long\n", path, name); |
| 758 | continue; |
| 759 | } |
| 760 | strcpy(fileSpot, name); |
| 761 | |
| 762 | int type = entry->d_type; |
| 763 | if (entry->d_type == DT_DIR) { |
| 764 | deleteRecursive(pathbuf); |
| 765 | rmdir(pathbuf); |
| 766 | } else { |
| 767 | unlink(pathbuf); |
| 768 | } |
| 769 | } |
Mike Lockwood | a108b2f | 2010-11-11 11:22:32 -0500 | [diff] [blame] | 770 | closedir(dir); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void deletePath(const char* path) { |
| 774 | struct stat statbuf; |
| 775 | if (stat(path, &statbuf) == 0) { |
| 776 | if (S_ISDIR(statbuf.st_mode)) { |
| 777 | deleteRecursive(path); |
| 778 | rmdir(path); |
| 779 | } else { |
| 780 | unlink(path); |
| 781 | } |
| 782 | } else { |
| 783 | LOGE("deletePath stat failed for %s: %s", path, strerror(errno)); |
| 784 | } |
| 785 | } |
| 786 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 787 | MtpResponseCode MtpServer::doDeleteObject() { |
| 788 | MtpObjectHandle handle = mRequest.getParameter(1); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 789 | MtpObjectFormat format = mRequest.getParameter(2); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 790 | // FIXME - support deleting all objects if handle is 0xFFFFFFFF |
| 791 | // FIXME - implement deleting objects by format |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 792 | |
| 793 | MtpString filePath; |
| 794 | int64_t fileLength; |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 795 | int result = mDatabase->getObjectFilePath(handle, filePath, fileLength); |
| 796 | if (result == MTP_RESPONSE_OK) { |
| 797 | LOGV("deleting %s", (const char *)filePath); |
Mike Lockwood | ccb6e96 | 2010-09-13 17:15:58 -0400 | [diff] [blame] | 798 | deletePath((const char *)filePath); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 799 | return mDatabase->deleteFile(handle); |
| 800 | } else { |
| 801 | return result; |
| 802 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | MtpResponseCode MtpServer::doGetObjectPropDesc() { |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 806 | MtpObjectProperty propCode = mRequest.getParameter(1); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 807 | MtpObjectFormat format = mRequest.getParameter(2); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 808 | LOGD("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode), |
| 809 | MtpDebug::getFormatCodeName(format)); |
| 810 | MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format); |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 811 | if (!property) |
| 812 | return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED; |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 813 | property->write(mData); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 814 | delete property; |
| 815 | return MTP_RESPONSE_OK; |
| 816 | } |
| 817 | |
| 818 | MtpResponseCode MtpServer::doGetDevicePropDesc() { |
| 819 | MtpDeviceProperty propCode = mRequest.getParameter(1); |
| 820 | LOGD("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode)); |
| 821 | MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode); |
| 822 | if (!property) |
| 823 | return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED; |
| 824 | property->write(mData); |
| 825 | delete property; |
Mike Lockwood | 767c5e4 | 2010-06-30 17:00:35 -0400 | [diff] [blame] | 826 | return MTP_RESPONSE_OK; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 827 | } |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 828 | |
| 829 | } // namespace android |