summaryrefslogtreecommitdiff
path: root/bsp/synwit/libraries/SWM341_CSL/SWM341_UsbHost_Lib/MTP/usbh_mtp_ptp.c
blob: 42fed1eb28df0b14491d1e991e6b9a658ea93922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/******************************************************************************************************************************************
* 文件名称: usbh_mtp.c
* 功能说明: This file includes the PTP operations layer.
* 技术支持: http://www.synwit.com.cn/e/tool/gbook/?bid=1
* 注意事项:
* 版本日期: V1.1.0      2020年11月3日
* 升级记录:
*
*
*******************************************************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include <string.h>
#include "SWM341.h"
#include "usbh_core.h"
#include "usbh_mtp.h"


/******************************************************************************************************************************************
* 函数名称: PTP_GetDevicePropValue()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PTP_GetDevicePropValue(USBH_Info_t *phost, uint32_t *offset, uint32_t total, PTP_PropertyValue_t *value, uint16_t datatype)
{
    uint8_t *data = USBH_MTP_Info.data_container.payload;
    uint16_t len;
    switch(datatype)
    {
    case PTP_DTC_INT8:
        value->i8 = *(int8_t *)(void *) & (data[*offset]);
        *offset += 1U;
        break;

    case PTP_DTC_UINT8:
        value->u8 = *(uint8_t *) & (data[*offset]);
        *offset += 1U;
        break;

    case PTP_DTC_INT16:
        value->i16 = *(int16_t *)(void *) & (data[*offset]);
        *offset += 2U;
        break;

    case PTP_DTC_UINT16:
        value->u16 = PTP_LE16(&(data[*offset]));
        *offset += 2U;
        break;

    case PTP_DTC_INT32:
        value->i32 = *(int32_t *)(void *)(&(data[*offset]));
        *offset += 4U;
        break;

    case PTP_DTC_UINT32:
        value->u32 = PTP_LE32(&(data[*offset]));
        *offset += 4U;
        break;

    case PTP_DTC_INT64:
        value->i64 = *(int64_t *)(void *)(&(data[*offset]));
        *offset += 8U;
        break;

    case PTP_DTC_UINT64:
        value->u64 = PTP_LE64(&(data[*offset]));
        *offset += 8U;
        break;

    case PTP_DTC_UINT128:
        *offset += 16U;
        break;

    case PTP_DTC_INT128:
        *offset += 16U;
        break;

    case PTP_DTC_STR:
        PTP_GetString((uint8_t *)(void *)value->str, (uint8_t *) & (data[*offset]), &len);
        *offset += (uint32_t)(len * 2U) + 1U;
        break;

    default:
        break;
    }
}


/******************************************************************************************************************************************
* 函数名称: PTP_GetString()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PTP_GetString(uint8_t *str, uint8_t *data, uint16_t *len)
{
    uint16_t strlength;
    uint16_t idx;

    *len = data[0];
    strlength = (uint16_t)(2U * (uint32_t)data[0]);
    data ++; /* Adjust the offset ignoring the String Len */

    for (idx = 0U; idx < strlength; idx += 2U)
    {
        /* Copy Only the string and ignore the UNICODE ID, hence add the src */
        *str = data[idx];
        str++;
    }
    *str = 0U; /* mark end of string */
}


/******************************************************************************************************************************************
* 函数名称: PTP_GetArray16()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
uint32_t PTP_GetArray16(uint16_t *array, uint8_t *data, uint32_t offset)
{
    uint32_t size, idx = 0U;

    size = PTP_LE32(&data[offset]);
    while(size > idx)
    {
        array[idx] = (uint16_t)data[offset + (sizeof(uint16_t) * (idx + 2U))];
        idx++;
    }
    return size;
}


/******************************************************************************************************************************************
* 函数名称: PTP_GetArray32()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
uint32_t PTP_GetArray32(uint32_t *array, uint8_t *data, uint32_t offset)
{
    uint32_t size, idx = 0U;

    size = PTP_LE32(&data[offset]);
    while(size > idx)
    {
        array[idx] = PTP_LE32(&data[offset + (sizeof(uint32_t) * (idx + 1U))]);
        idx++;
    }
    return size;
}


/******************************************************************************************************************************************
* 函数名称: PTP_DecodeDeviceInfo()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PTP_DecodeDeviceInfo(USBH_Info_t *phost, PTP_DeviceInfo_t *dev_info)
{
    uint8_t *data = USBH_MTP_Info.data_container.payload;
    uint32_t totallen;
    uint16_t len;

    dev_info->StandardVersion = PTP_LE16(&data[PTP_di_StandardVersion]);
    dev_info->VendorExtensionID = PTP_LE32(&data[PTP_di_VendorExtensionID]);
    dev_info->VendorExtensionVersion = PTP_LE16(&data[PTP_di_VendorExtensionVersion]);
    PTP_GetString(dev_info->VendorExtensionDesc, &data[PTP_di_VendorExtensionDesc], &len);

    totallen = (uint32_t)(len * 2U) + 1U;
    dev_info->FunctionalMode = PTP_LE16(&data[PTP_di_FunctionalMode + totallen]);
    dev_info->OperationsSupportedNbr = PTP_GetArray16((uint16_t *)(void *)&dev_info->OperationsSupported, data, PTP_di_OperationsSupported + totallen);

    totallen = totallen + (dev_info->OperationsSupportedNbr * sizeof(uint16_t)) + sizeof(uint32_t);
    dev_info->EventsSupportedNbr = PTP_GetArray16((uint16_t *)(void *)&dev_info->EventsSupported, data, PTP_di_OperationsSupported + totallen);

    totallen = totallen + (dev_info->EventsSupportedNbr * sizeof(uint16_t)) + sizeof(uint32_t);
    dev_info->DevicePropertiesSupportedNbr = PTP_GetArray16((uint16_t *)(void *)&dev_info->DevicePropertiesSupported, data, PTP_di_OperationsSupported + totallen);

    totallen = totallen + (dev_info->DevicePropertiesSupportedNbr * sizeof(uint16_t)) + sizeof(uint32_t);

    dev_info->CaptureFormatsNbr = PTP_GetArray16((uint16_t *)(void *)&dev_info->CaptureFormats, data, PTP_di_OperationsSupported + totallen);

    totallen = totallen + (dev_info->CaptureFormatsNbr * sizeof(uint16_t)) + sizeof(uint32_t);
    dev_info->ImageFormatsNbr =  PTP_GetArray16((uint16_t *)(void *)&dev_info->ImageFormats, data, PTP_di_OperationsSupported + totallen);

    totallen = totallen + (dev_info->ImageFormatsNbr * sizeof(uint16_t)) + sizeof(uint32_t);
    PTP_GetString(dev_info->Manufacturer, &data[PTP_di_OperationsSupported + totallen], &len);

    totallen += (uint32_t)(len * 2U) + 1U;
    PTP_GetString(dev_info->Model, &data[PTP_di_OperationsSupported + totallen], &len);

    totallen += (uint32_t)(len * 2U) + 1U;
    PTP_GetString(dev_info->DeviceVersion, &data[PTP_di_OperationsSupported + totallen], &len);

    totallen += (uint32_t)(len * 2U) + 1U;
    PTP_GetString(dev_info->SerialNumber, &data[PTP_di_OperationsSupported + totallen], &len);
}


/******************************************************************************************************************************************
* 函数名称: PTP_DecodeStorageInfo()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PTP_DecodeStorageInfo(USBH_Info_t *phost, PTP_StorageInfo_t *storage_info)
{
    uint8_t *data = USBH_MTP_Info.data_container.payload;
    uint16_t len;

    storage_info->StorageType = PTP_LE16(&data[PTP_si_StorageType]);
    storage_info->FilesystemType = PTP_LE16(&data[PTP_si_FilesystemType]);
    storage_info->AccessCapability = PTP_LE16(&data[PTP_si_AccessCapability]);
    storage_info->MaxCapability = PTP_LE64(&data[PTP_si_MaxCapability]);
    storage_info->FreeSpaceInBytes = PTP_LE64(&data[PTP_si_FreeSpaceInBytes]);
    storage_info->FreeSpaceInImages = PTP_LE32(&data[PTP_si_FreeSpaceInImages]);

    PTP_GetString(storage_info->StorageDescription, &data[PTP_si_StorageDescription], &len);
    PTP_GetString(storage_info->VolumeLabel, &data[PTP_si_StorageDescription + (len * 2U) + 1U], &len);
}


/******************************************************************************************************************************************
* 函数名称: PTP_DecodeObjectInfo()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PTP_DecodeObjectInfo(USBH_Info_t *phost, PTP_ObjectInfo_t *object_info)
{
    uint8_t *data = USBH_MTP_Info.data_container.payload;
    uint16_t filenamelen;

    object_info->StorageID = PTP_LE32(&data[PTP_oi_StorageID]);
    object_info->ObjectFormat = PTP_LE16(&data[PTP_oi_ObjectFormat]);
    object_info->ProtectionStatus = PTP_LE16(&data[PTP_oi_ProtectionStatus]);
    object_info->ObjectCompressedSize = PTP_LE64(&data[PTP_oi_ObjectCompressedSize]);

    /* For Samsung Galaxy */
    if((data[PTP_oi_filenamelen] == 0U) && (data[PTP_oi_filenamelen + 4U] != 0U))
    {
        data += 4;
    }
    object_info->ThumbFormat = PTP_LE16(&data[PTP_oi_ThumbFormat]);
    object_info->ThumbCompressedSize = PTP_LE32(&data[PTP_oi_ThumbCompressedSize]);
    object_info->ThumbPixWidth = PTP_LE32(&data[PTP_oi_ThumbPixWidth]);
    object_info->ThumbPixHeight = PTP_LE32(&data[PTP_oi_ThumbPixHeight]);
    object_info->ImagePixWidth = PTP_LE32(&data[PTP_oi_ImagePixWidth]);
    object_info->ImagePixHeight = PTP_LE32(&data[PTP_oi_ImagePixHeight]);
    object_info->ImageBitDepth = PTP_LE32(&data[PTP_oi_ImageBitDepth]);
    object_info->ParentObject = PTP_LE32(&data[PTP_oi_ParentObject]);
    object_info->AssociationType = PTP_LE16(&data[PTP_oi_AssociationType]);
    object_info->AssociationDesc = PTP_LE32(&data[PTP_oi_AssociationDesc]);
    object_info->SequenceNumber = PTP_LE32(&data[PTP_oi_SequenceNumber]);
    PTP_GetString(object_info->Filename, &data[PTP_oi_filenamelen], &filenamelen);
}


/******************************************************************************************************************************************
* 函数名称: PTP_DecodeObjectPropDesc()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PTP_DecodeObjectPropDesc(USBH_Info_t *phost, PTP_ObjectPropDesc_t *opd, uint32_t opdlen)
{
    uint8_t *data = USBH_MTP_Info.data_container.payload;
    uint32_t offset = 0U, i;

    opd->ObjectPropertyCode = PTP_LE16(&data[PTP_opd_ObjectPropertyCode]);
    opd->DataType = PTP_LE16(&data[PTP_opd_DataType]);
    opd->GetSet = *(uint8_t *)(&data[PTP_opd_GetSet]);

    offset = PTP_opd_FactoryDefaultValue;
    PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FactoryDefaultValue, opd->DataType);

    opd->GroupCode = PTP_LE32(&data[offset]);
    offset += sizeof(uint32_t);

    opd->FormFlag = *(uint8_t *)(&data[offset]);
    offset += sizeof(uint8_t);

    switch(opd->FormFlag)
    {
    case PTP_OPFF_Range:
        PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MinimumValue, opd->DataType);
        PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MaximumValue, opd->DataType);
        PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.StepSize, opd->DataType);
        break;

    case PTP_OPFF_Enumeration:
        opd->FORM.Enum.NumberOfValues = PTP_LE16(&data[offset]);
        offset += sizeof(uint16_t);

        for(i = 0U; i < opd->FORM.Enum.NumberOfValues ; i++)
        {
            PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Enum.SupportedValue[i], opd->DataType);
        }
        break;

    default:
        break;
    }
}


/******************************************************************************************************************************************
* 函数名称: PTP_DecodeObjectPropList()
* 功能说明:
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
uint32_t PTP_DecodeObjectPropList(USBH_Info_t *phost, MTP_Properties_t *props, uint32_t len)
{
    uint8_t *data = USBH_MTP_Info.data_container.payload;
    uint32_t prop_count;
    uint32_t offset = 0U, i;

    prop_count = PTP_LE32(data);
    if(prop_count == 0U)
    {
        return 0;
    }

    data += sizeof(uint32_t);
    len -= sizeof(uint32_t);

    for(i = 0U; i < prop_count; i++)
    {
        if(len <= 0U)
        {
            return 0;
        }

        props[i].ObjectHandle = PTP_LE32(data);
        data += sizeof(uint32_t);
        len -= sizeof(uint32_t);

        props[i].property = PTP_LE16(data);
        data += sizeof(uint16_t);
        len -= sizeof(uint16_t);

        props[i].datatype = PTP_LE16(data);
        data += sizeof(uint16_t);
        len -= sizeof(uint16_t);

        offset = 0U;

        PTP_GetDevicePropValue(phost, &offset, len, &props[i].propval, props[i].datatype);

        data += offset;
        len -= offset;
    }

    return prop_count;
}