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 #define LOG_TAG "MtpDeviceInfo"
18 
19 #include "MtpDebug.h"
20 #include "MtpDataPacket.h"
21 #include "MtpDeviceInfo.h"
22 #include "MtpStringBuffer.h"
23 
24 namespace android {
25 
MtpDeviceInfo()26 MtpDeviceInfo::MtpDeviceInfo()
27     :   mStandardVersion(0),
28         mVendorExtensionID(0),
29         mVendorExtensionVersion(0),
30         mVendorExtensionDesc(NULL),
31         mFunctionalMode(0),
32         mOperations(NULL),
33         mEvents(NULL),
34         mDeviceProperties(NULL),
35         mCaptureFormats(NULL),
36         mPlaybackFormats(NULL),
37         mManufacturer(NULL),
38         mModel(NULL),
39         mVersion(NULL),
40         mSerial(NULL)
41 {
42 }
43 
~MtpDeviceInfo()44 MtpDeviceInfo::~MtpDeviceInfo() {
45     if (mVendorExtensionDesc)
46         free(mVendorExtensionDesc);
47     delete mOperations;
48     delete mEvents;
49     delete mDeviceProperties;
50     delete mCaptureFormats;
51     delete mPlaybackFormats;
52     if (mManufacturer)
53         free(mManufacturer);
54     if (mModel)
55         free(mModel);
56     if (mVersion)
57         free(mVersion);
58     if (mSerial)
59         free(mSerial);
60 }
61 
read(MtpDataPacket & packet)62 bool MtpDeviceInfo::read(MtpDataPacket& packet) {
63     MtpStringBuffer string;
64 
65     // read the device info
66     if (!packet.getUInt16(mStandardVersion)) return false;
67     if (!packet.getUInt32(mVendorExtensionID)) return false;
68     if (!packet.getUInt16(mVendorExtensionVersion)) return false;
69 
70     if (!packet.getString(string)) return false;
71     mVendorExtensionDesc = strdup((const char *)string);
72 
73     if (!packet.getUInt16(mFunctionalMode)) return false;
74     mOperations = packet.getAUInt16();
75     if (!mOperations) return false;
76     mEvents = packet.getAUInt16();
77     if (!mEvents) return false;
78     mDeviceProperties = packet.getAUInt16();
79     if (!mDeviceProperties) return false;
80     mCaptureFormats = packet.getAUInt16();
81     if (!mCaptureFormats) return false;
82     mPlaybackFormats = packet.getAUInt16();
83     if (!mCaptureFormats) return false;
84 
85     if (!packet.getString(string)) return false;
86     mManufacturer = strdup((const char *)string);
87     if (!packet.getString(string)) return false;
88     mModel = strdup((const char *)string);
89     if (!packet.getString(string)) return false;
90     mVersion = strdup((const char *)string);
91     if (!packet.getString(string)) return false;
92     mSerial = strdup((const char *)string);
93 
94     return true;
95 }
96 
print()97 void MtpDeviceInfo::print() {
98     ALOGV("Device Info:\n\tmStandardVersion: %d\n\tmVendorExtensionID: %d\n\tmVendorExtensionVersiony: %d\n",
99             mStandardVersion, mVendorExtensionID, mVendorExtensionVersion);
100     ALOGV("\tmVendorExtensionDesc: %s\n\tmFunctionalMode: %d\n\tmManufacturer: %s\n\tmModel: %s\n\tmVersion: %s\n\tmSerial: %s\n",
101             mVendorExtensionDesc, mFunctionalMode, mManufacturer, mModel, mVersion, mSerial);
102 }
103 
104 }  // namespace android
105