Debian Codegraph @

ports/stm32/usbhost/Class/MTP/Src/usbh_mtp_ptp.c

raw
1 /**
2 ******************************************************************************
3 * @file usbh_mtp_ptp.c
4 * @author MCD Application Team
5 * @version V3.0.0
6 * @date 18-February-2014
7 * @brief This file includes the PTP operations layer
8 ******************************************************************************
9 * @attention
10 *
11 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
12 *
13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14 * You may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at:
16 *
17 * http://www.st.com/software_license_agreement_liberty_v2
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 ******************************************************************************
26 */
27
28 /* Includes ------------------------------------------------------------------*/
29 #include "usbh_mtp_ptp.h"
30 #include "usbh_mtp.h"
31 /** @addtogroup USBH_LIB
32 * @{
33 */
34
35 /** @addtogroup USBH_CLASS
36 * @{
37 */
38
39 /** @addtogroup USBH_MTP_CLASS
40 * @{
41 */
42
43 /** @defgroup USBH_MTP_PTP
44 * @brief This file includes the mass storage related functions
45 * @{
46 */
47
48
49 /** @defgroup USBH_MTP_PTP_Private_TypesDefinitions
50 * @{
51 */
52 /**
53 * @}
54 */
55
56 /** @defgroup USBH_MTP_PTP_Private_Defines
57 * @{
58 */
59 /**
60 * @}
61 */
62
63 /** @defgroup USBH_MTP_PTP_Private_Macros
64 * @{
65 */
66 /**
67 * @}
68 */
69
70
71 /** @defgroup USBH_MTP_PTP_Private_Variables
72 * @{
73 */
74
75 /**
76 * @}
77 */
78
79
80 /** @defgroup USBH_MTP_PTP_Private_FunctionPrototypes
81 * @{
82 */
83 static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info);
84 static void PTP_GetStorageIDs (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *stor_ids);
85 static void PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *stor_info);
86 static void PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, PTP_ObjectPropDescTypeDef *opd, uint32_t opdlen);
87 static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info);
88 static void PTP_GetDevicePropValue(USBH_HandleTypeDef *phost,
89 uint32_t *offset,
90 uint32_t total,
91 PTP_PropertyValueTypedef* value,
92 uint16_t datatype);
93
94 static uint32_t PTP_GetObjectPropList (USBH_HandleTypeDef *phost,
95 MTP_PropertiesTypedef *props,
96 uint32_t len);
97
98
99 static void PTP_BufferFullCallback(USBH_HandleTypeDef *phost);
100 static void PTP_GetString(uint8_t *str, uint8_t* data, uint16_t *len);
101 static uint32_t PTP_GetArray16 (uint16_t *array, uint8_t *data, uint32_t offset);
102 static uint32_t PTP_GetArray32 (uint32_t *array, uint8_t *data, uint32_t offset);
103 /**
104 * @}
105 */
106
107
108 /** @defgroup USBH_MTP_PTP_Exported_Variables
109 * @{
110 */
111 /**
112 * @}
113 */
114
115
116 /** @defgroup USBH_MTP_PTP_Private_Functions
117 * @{
118 */
119 /**
120 * @brief USBH_PTP_Init
121 * The function Initializes the BOT protocol.
122 * @param phost: Host handle
123 * @retval USBH Status
124 */
125 USBH_StatusTypeDef USBH_PTP_Init(USBH_HandleTypeDef *phost)
126 {
127 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
128
129 /* Set state to idle to be ready for operations */
130 MTP_Handle->ptp.state = PTP_IDLE;
131 MTP_Handle->ptp.req_state = PTP_REQ_SEND;
132
133 return USBH_OK;
134 }
135
136 /**
137 * @brief USBH_PTP_Process
138 * The function handle the BOT protocol.
139 * @param phost: Host handle
140 * @param lun: Logical Unit Number
141 * @retval USBH Status
142 */
143 USBH_StatusTypeDef USBH_PTP_Process (USBH_HandleTypeDef *phost)
144 {
145 USBH_StatusTypeDef status = USBH_BUSY;
146 USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE;
147 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
148 PTP_ContainerTypedef ptp_container;
149 uint32_t len;
150
151 switch (MTP_Handle->ptp.state)
152 {
153 case PTP_IDLE:
154 /*Do Nothing */
155 break;
156
157 case PTP_OP_REQUEST_STATE:
158 USBH_BulkSendData (phost,
159 (uint8_t*)&(MTP_Handle->ptp.op_container),
160 MTP_Handle->ptp.op_container.length,
161 MTP_Handle->DataOutPipe,
162 1);
163 MTP_Handle->ptp.state = PTP_OP_REQUEST_WAIT_STATE;
164 break;
165
166 case PTP_OP_REQUEST_WAIT_STATE:
167 URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataOutPipe);
168
169 if(URB_Status == USBH_URB_DONE)
170 {
171 if(MTP_Handle->ptp.flags == PTP_DP_NODATA)
172 {
173 MTP_Handle->ptp.state = PTP_RESPONSE_STATE;
174 }
175 else if(MTP_Handle->ptp.flags == PTP_DP_SENDDATA)
176 {
177 MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_STATE;
178 }
179 else if(MTP_Handle->ptp.flags == PTP_DP_GETDATA)
180 {
181 MTP_Handle->ptp.state = PTP_DATA_IN_PHASE_STATE;
182 }
183 #if (USBH_USE_OS == 1)
184 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
185 #endif
186 }
187 else if(URB_Status == USBH_URB_NOTREADY)
188 {
189 /* Re-send Request */
190 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
191 #if (USBH_USE_OS == 1)
192 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
193 #endif
194 }
195 else if(URB_Status == USBH_URB_STALL)
196 {
197 MTP_Handle->ptp.state = PTP_ERROR;
198 #if (USBH_USE_OS == 1)
199 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
200 #endif
201 }
202 break;
203
204 case PTP_DATA_OUT_PHASE_STATE:
205
206 USBH_BulkSendData (phost,
207 MTP_Handle->ptp.data_ptr,
208 MTP_Handle->DataOutEpSize ,
209 MTP_Handle->DataOutPipe,
210 1);
211
212
213 MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_WAIT_STATE;
214 break;
215
216 case PTP_DATA_OUT_PHASE_WAIT_STATE:
217 URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataOutPipe);
218
219 if(URB_Status == USBH_URB_DONE)
220 {
221 /* Adjudt Data pointer and data length */
222 if(MTP_Handle->ptp.data_length > MTP_Handle->DataOutEpSize)
223 {
224 MTP_Handle->ptp.data_ptr += MTP_Handle->DataOutEpSize;
225 MTP_Handle->ptp.data_length -= MTP_Handle->DataOutEpSize;
226 MTP_Handle->ptp.data_packet += MTP_Handle->DataOutEpSize;
227
228 if(MTP_Handle->ptp.data_packet >= PTP_USB_BULK_PAYLOAD_LEN_READ)
229 {
230 PTP_BufferFullCallback (phost);
231 MTP_Handle->ptp.data_packet = 0;
232 MTP_Handle->ptp.iteration++;
233 }
234
235 }
236 else
237 {
238 MTP_Handle->ptp.data_length = 0;
239 }
240
241 /* More Data To be Sent */
242 if(MTP_Handle->ptp.data_length > 0)
243 {
244 USBH_BulkSendData (phost,
245 MTP_Handle->ptp.data_ptr,
246 MTP_Handle->DataOutEpSize ,
247 MTP_Handle->DataOutPipe,
248 1);
249 }
250 else
251 {
252 /* If value was 0, and successful transfer, then change the state */
253 MTP_Handle->ptp.state = PTP_RESPONSE_STATE;
254 }
255 #if (USBH_USE_OS == 1)
256 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
257 #endif
258 }
259
260 else if(URB_Status == USBH_URB_NOTREADY)
261 {
262 /* Re-send same data */
263 MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_STATE;
264 #if (USBH_USE_OS == 1)
265 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
266 #endif
267 }
268
269 else if(URB_Status == USBH_URB_STALL)
270 {
271 MTP_Handle->ptp.state = PTP_ERROR;
272 #if (USBH_USE_OS == 1)
273 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
274 #endif
275 }
276 break;
277
278 case PTP_DATA_IN_PHASE_STATE:
279 /* Send first packet */
280 USBH_BulkReceiveData (phost,
281 MTP_Handle->ptp.data_ptr,
282 MTP_Handle->DataInEpSize,
283 MTP_Handle->DataInPipe);
284
285 MTP_Handle->ptp.state = PTP_DATA_IN_PHASE_WAIT_STATE;
286 break;
287
288 case PTP_DATA_IN_PHASE_WAIT_STATE:
289 URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataInPipe);
290
291 if(URB_Status == USBH_URB_DONE)
292 {
293 len = USBH_LL_GetLastXferSize (phost, MTP_Handle->DataInPipe);
294
295 if( MTP_Handle->ptp.data_packet_counter++ == 0)
296 {
297 /* This is the first packet; so retrieve exact data length from payload */
298 MTP_Handle->ptp.data_length = *(uint32_t*)(MTP_Handle->ptp.data_ptr);
299 MTP_Handle->ptp.iteration = 0;
300 }
301
302 if((len >= MTP_Handle->DataInEpSize) && (MTP_Handle->ptp.data_length > 0))
303 {
304 MTP_Handle->ptp.data_ptr += len;
305 MTP_Handle->ptp.data_length -= len;
306 MTP_Handle->ptp.data_packet += len;
307
308 if(MTP_Handle->ptp.data_packet >= PTP_USB_BULK_PAYLOAD_LEN_READ)
309 {
310 PTP_BufferFullCallback (phost);
311 MTP_Handle->ptp.data_packet = 0;
312 MTP_Handle->ptp.iteration++;
313 }
314
315 /* Continue receiving data*/
316 USBH_BulkReceiveData (phost,
317 MTP_Handle->ptp.data_ptr,
318 MTP_Handle->DataInEpSize,
319 MTP_Handle->DataInPipe);
320 }
321 else
322 {
323 MTP_Handle->ptp.data_length -= len;
324 MTP_Handle->ptp.state = PTP_RESPONSE_STATE;
325 #if (USBH_USE_OS == 1)
326 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
327 #endif
328 }
329 }
330 else if(URB_Status == USBH_URB_STALL)
331 {
332 MTP_Handle->ptp.state = PTP_ERROR;
333 #if (USBH_USE_OS == 1)
334 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
335 #endif
336 }
337 break;
338
339 case PTP_RESPONSE_STATE:
340
341 USBH_BulkReceiveData (phost,
342 (uint8_t*)&(MTP_Handle->ptp.resp_container),
343 PTP_USB_BULK_REQ_RESP_MAX_LEN ,
344 MTP_Handle->DataInPipe);
345
346 MTP_Handle->ptp.state = PTP_RESPONSE_WAIT_STATE;
347 break;
348
349 case PTP_RESPONSE_WAIT_STATE:
350 URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataInPipe);
351
352 if(URB_Status == USBH_URB_DONE)
353 {
354 USBH_PTP_GetResponse (phost, &ptp_container);
355
356 if(ptp_container.Code == PTP_RC_OK)
357 {
358 status = USBH_OK;
359 }
360 else
361 {
362 status = USBH_FAIL;
363 }
364 MTP_Handle->ptp.req_state = PTP_REQ_SEND;
365 }
366 else if(URB_Status == USBH_URB_STALL)
367 {
368 MTP_Handle->ptp.state = PTP_ERROR;
369 #if (USBH_USE_OS == 1)
370 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
371 #endif
372 }
373 break;
374
375 case PTP_ERROR:
376 MTP_Handle->ptp.req_state = PTP_REQ_SEND;
377 break;
378
379 default:
380 break;
381 }
382 return status;
383 }
384
385 /**
386 * @brief USBH_PTP_OpenSession
387 * Open a new session
388 * @param phost: Host handle
389 * @retval USBH Status
390 */
391 USBH_StatusTypeDef USBH_PTP_SendRequest (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *req)
392 {
393 USBH_StatusTypeDef status = USBH_OK;
394 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
395
396 /* Clear PTP Data container*/
397 USBH_memset(&(MTP_Handle->ptp.op_container), 0, sizeof(PTP_OpContainerTypedef));
398
399 /* build appropriate USB container */
400 MTP_Handle->ptp.op_container.length = PTP_USB_BULK_REQ_LEN- (sizeof(uint32_t)*(5-req->Nparam));
401 MTP_Handle->ptp.op_container.type = PTP_USB_CONTAINER_COMMAND;
402 MTP_Handle->ptp.op_container.code = req->Code;
403 MTP_Handle->ptp.op_container.trans_id = req->Transaction_ID;
404 MTP_Handle->ptp.op_container.param1 = req->Param1;
405 MTP_Handle->ptp.op_container.param2 = req->Param2;
406 MTP_Handle->ptp.op_container.param3 = req->Param3;
407 MTP_Handle->ptp.op_container.param4 = req->Param4;
408 MTP_Handle->ptp.op_container.param5 = req->Param5;
409
410 return status;
411 }
412
413 /**
414 * @brief USBH_PTP_OpenSession
415 * Open a new session
416 * @param phost: Host handle
417 * @retval USBH Status
418 */
419 USBH_StatusTypeDef USBH_PTP_GetResponse (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *resp)
420 {
421 USBH_StatusTypeDef status = USBH_OK;
422 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
423
424 /* build an appropriate PTPContainer */
425 resp->Code = MTP_Handle->ptp.resp_container.code;
426 resp->SessionID = MTP_Handle->ptp.session_id;
427 resp->Transaction_ID = MTP_Handle->ptp.resp_container.trans_id;
428 resp->Param1 = MTP_Handle->ptp.resp_container.param1;
429 resp->Param2 = MTP_Handle->ptp.resp_container.param2;
430 resp->Param3 = MTP_Handle->ptp.resp_container.param3;
431 resp->Param4 = MTP_Handle->ptp.resp_container.param4;
432 resp->Param5 = MTP_Handle->ptp.resp_container.param5;
433
434 return status;
435 }
436
437 /**
438 * @brief The function informs user that data buffer is full
439 * @param phost: host handle
440 * @retval None
441 */
442 void PTP_BufferFullCallback(USBH_HandleTypeDef *phost)
443 {
444 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
445
446 switch (MTP_Handle->ptp.data_container.code)
447 {
448 case PTP_OC_GetDeviceInfo:
449 PTP_DecodeDeviceInfo (phost, &(MTP_Handle->info.devinfo));
450 break;
451
452 case PTP_OC_GetPartialObject:
453 case PTP_OC_GetObject:
454
455 /* first packet is in the PTP data payload buffer */
456 if(MTP_Handle->ptp.iteration == 0)
457 {
458 /* copy it to object */
459 USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, PTP_USB_BULK_PAYLOAD_LEN_READ);
460
461 /* next packet should be directly copied to object */
462 MTP_Handle->ptp.data_ptr = (MTP_Handle->ptp.object_ptr + PTP_USB_BULK_PAYLOAD_LEN_READ);
463 }
464 break;
465
466 case PTP_OC_SendObject:
467 /* first packet is in the PTP data payload buffer */
468 if(MTP_Handle->ptp.iteration == 0)
469 {
470 /* next packet should be directly copied to object */
471 MTP_Handle->ptp.data_ptr = (MTP_Handle->ptp.object_ptr + PTP_USB_BULK_PAYLOAD_LEN_READ);
472 }
473 break;
474
475 default:
476 break;
477
478
479 }
480 }
481
482 /**
483 * @brief PTP_GetDeviceInfo
484 * Gets device info dataset and fills deviceinfo structure.
485 * @param phost: Host handle
486 * @param dev_info: Device info structure
487 * @retval None
488 */
489 static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info)
490 {
491 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
492 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
493 uint32_t totallen;
494 uint16_t len;
495
496 /* Max device info is PTP_USB_BULK_HS_MAX_PACKET_LEN_READ */
497 USBH_DbgLog (" MTP device info size exceeds internal buffer size.\
498 only available data are decoded.");
499
500 if(MTP_Handle->ptp.iteration == 0)
501 {
502 dev_info->StandardVersion = LE16(&data[PTP_di_StandardVersion]);
503 dev_info->VendorExtensionID = LE32(&data[PTP_di_VendorExtensionID]);
504 dev_info->VendorExtensionVersion = LE16(&data[PTP_di_VendorExtensionVersion]);
505 PTP_GetString(dev_info->VendorExtensionDesc, &data[PTP_di_VendorExtensionDesc], &len);
506
507 totallen=len*2+1;
508 dev_info->FunctionalMode = LE16(&data[PTP_di_FunctionalMode+totallen]);
509 dev_info->OperationsSupported_len = PTP_GetArray16 ((uint16_t *)&dev_info->OperationsSupported,
510 data,
511 PTP_di_OperationsSupported+totallen);
512
513 totallen=totallen+dev_info->OperationsSupported_len*sizeof(uint16_t)+sizeof(uint32_t);
514 dev_info->EventsSupported_len = PTP_GetArray16 ((uint16_t *)&dev_info->EventsSupported,
515 data,
516 PTP_di_OperationsSupported+totallen);
517
518 totallen=totallen+dev_info->EventsSupported_len*sizeof(uint16_t)+sizeof(uint32_t);
519 dev_info->DevicePropertiesSupported_len = PTP_GetArray16 ((uint16_t *)&dev_info->DevicePropertiesSupported,
520 data,
521 PTP_di_OperationsSupported+totallen);
522
523 totallen=totallen+dev_info->DevicePropertiesSupported_len*sizeof(uint16_t)+sizeof(uint32_t);
524
525 dev_info->CaptureFormats_len = PTP_GetArray16 ((uint16_t *)&dev_info->CaptureFormats,
526 data,
527 PTP_di_OperationsSupported+totallen);
528
529 totallen=totallen+dev_info->CaptureFormats_len*sizeof(uint16_t)+sizeof(uint32_t);
530 dev_info->ImageFormats_len = PTP_GetArray16 ((uint16_t *)&dev_info->ImageFormats,
531 data,
532 PTP_di_OperationsSupported+totallen);
533
534 totallen=totallen+dev_info->ImageFormats_len*sizeof(uint16_t)+sizeof(uint32_t);
535 PTP_GetString(dev_info->Manufacturer, &data[PTP_di_OperationsSupported+totallen], &len);
536
537 totallen+=len*2+1;
538 PTP_GetString(dev_info->Model, &data[PTP_di_OperationsSupported+totallen], &len);
539
540 totallen+=len*2+1;
541 PTP_GetString(dev_info->DeviceVersion, &data[PTP_di_OperationsSupported+totallen], &len);
542
543 totallen+=len*2+1;
544 PTP_GetString(dev_info->SerialNumber, &data[PTP_di_OperationsSupported+totallen], &len);
545 }
546 }
547
548 /**
549 * @brief PTP_GetStorageIDs
550 * Gets Storage Ids and fills stor_ids structure.
551 * @param phost: Host handle
552 * @param stor_ids: Storage IDsstructure
553 * @retval None
554 */
555 static void PTP_GetStorageIDs (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *stor_ids)
556 {
557 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
558 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
559
560 stor_ids->n = PTP_GetArray32 (stor_ids->Storage, data, 0);
561 }
562
563
564 /**
565 * @brief PTP_GetStorageInfo
566 * Gets Storage Info and fills stor_info structure.
567 * @param phost: Host handle
568 * @param stor_ids: Storage IDsstructure
569 * @retval None
570 */
571 static void PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *stor_info)
572 {
573 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
574 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
575
576 uint16_t len;
577
578 stor_info->StorageType=LE16(&data[PTP_si_StorageType]);
579 stor_info->FilesystemType=LE16(&data[PTP_si_FilesystemType]);
580 stor_info->AccessCapability=LE16(&data[PTP_si_AccessCapability]);
581 stor_info->MaxCapability=LE64(&data[PTP_si_MaxCapability]);
582 stor_info->FreeSpaceInBytes=LE64(&data[PTP_si_FreeSpaceInBytes]);
583 stor_info->FreeSpaceInImages=LE32(&data[PTP_si_FreeSpaceInImages]);
584
585 PTP_GetString(stor_info->StorageDescription, &data[PTP_si_StorageDescription], &len);
586 PTP_GetString(stor_info->VolumeLabel, &data[PTP_si_StorageDescription+len*2+1], &len);
587 }
588
589 /**
590 * @brief PTP_GetObjectInfo
591 * Gets objectInfo and fills object_info structure.
592 * @param phost: Host handle
593 * @param object_info: object info structure
594 * @retval None
595 */
596 static void PTP_GetObjectInfo (USBH_HandleTypeDef *phost, PTP_ObjectInfoTypedef *object_info)
597 {
598 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
599 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
600 uint16_t filenamelen;
601
602 object_info->StorageID=LE32(&data[PTP_oi_StorageID]);
603 object_info->ObjectFormat=LE16(&data[PTP_oi_ObjectFormat]);
604 object_info->ProtectionStatus=LE16(&data[PTP_oi_ProtectionStatus]);
605 object_info->ObjectCompressedSize=LE32(&data[PTP_oi_ObjectCompressedSize]);
606
607 /* For Samsung Galaxy */
608 if ((data[PTP_oi_filenamelen] == 0) && (data[PTP_oi_filenamelen+4] != 0))
609 {
610 data += 4;
611 }
612 object_info->ThumbFormat=LE16(&data[PTP_oi_ThumbFormat]);
613 object_info->ThumbCompressedSize=LE32(&data[PTP_oi_ThumbCompressedSize]);
614 object_info->ThumbPixWidth=LE32(&data[PTP_oi_ThumbPixWidth]);
615 object_info->ThumbPixHeight=LE32(&data[PTP_oi_ThumbPixHeight]);
616 object_info->ImagePixWidth=LE32(&data[PTP_oi_ImagePixWidth]);
617 object_info->ImagePixHeight=LE32(&data[PTP_oi_ImagePixHeight]);
618 object_info->ImageBitDepth=LE32(&data[PTP_oi_ImageBitDepth]);
619 object_info->ParentObject=LE32(&data[PTP_oi_ParentObject]);
620 object_info->AssociationType=LE16(&data[PTP_oi_AssociationType]);
621 object_info->AssociationDesc=LE32(&data[PTP_oi_AssociationDesc]);
622 object_info->SequenceNumber=LE32(&data[PTP_oi_SequenceNumber]);
623 PTP_GetString(object_info->Filename, &data[PTP_oi_filenamelen], &filenamelen);
624 }
625
626
627 /**
628 * @brief PTP_GetObjectPropDesc
629 * Gets objectInfo and fills object_info structure.
630 * @param phost: Host handle
631 * @param opd: object prop descriptor structure
632 * @retval None
633 */
634 static void PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, PTP_ObjectPropDescTypeDef *opd, uint32_t opdlen)
635 {
636 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
637 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
638 uint32_t offset = 0, i;
639
640 opd->ObjectPropertyCode=LE16(&data[PTP_opd_ObjectPropertyCode]);
641 opd->DataType=LE16(&data[PTP_opd_DataType]);
642 opd->GetSet=*(uint8_t *)(&data[PTP_opd_GetSet]);
643
644 offset = PTP_opd_FactoryDefaultValue;
645 PTP_GetDevicePropValue (phost, &offset, opdlen, &opd->FactoryDefaultValue, opd->DataType);
646
647 opd->GroupCode=LE32(&data[offset]);
648 offset+=sizeof(uint32_t);
649
650 opd->FormFlag=*(uint8_t *)(&data[offset]);
651 offset+=sizeof(uint8_t);
652
653 switch (opd->FormFlag)
654 {
655 case PTP_OPFF_Range:
656 PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MinimumValue, opd->DataType);
657 PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MaximumValue, opd->DataType);
658 PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.StepSize, opd->DataType);
659 break;
660
661 case PTP_OPFF_Enumeration:
662
663 opd->FORM.Enum.NumberOfValues = LE16(&data[offset]);
664 offset+=sizeof(uint16_t);
665
666 for (i=0 ; i < opd->FORM.Enum.NumberOfValues ; i++)
667 {
668 PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Enum.SupportedValue[i], opd->DataType);
669 }
670 break;
671 default:
672 break;
673 }
674 }
675
676 /**
677 * @brief PTP_GetDevicePropValue
678 * Gets objectInfo and fills object_info structure.
679 * @param phost: Host handle
680 * @param opd: object prop descriptor structure
681 * @retval None
682 */
683 static void PTP_GetDevicePropValue(USBH_HandleTypeDef *phost,
684 uint32_t *offset,
685 uint32_t total,
686 PTP_PropertyValueTypedef* value,
687 uint16_t datatype)
688 {
689 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
690 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
691 uint16_t len;
692 switch (datatype)
693 {
694 case PTP_DTC_INT8:
695 value->i8 = *(uint8_t *)&(data[*offset]);
696 *offset += 1;
697 break;
698 case PTP_DTC_UINT8:
699 value->u8 = *(uint8_t *)&(data[*offset]);
700 *offset += 1;
701 break;
702 case PTP_DTC_INT16:
703
704 value->i16 = LE16(&(data[*offset]));
705 *offset += 2;
706 break;
707 case PTP_DTC_UINT16:
708 value->u16 = LE16(&(data[*offset]));
709 *offset += 2;
710 break;
711 case PTP_DTC_INT32:
712 value->i32 = LE32(&(data[*offset]));
713 *offset += 4;
714 break;
715 case PTP_DTC_UINT32:
716 value->u32 = LE32(&(data[*offset]));
717 *offset += 4;
718 break;
719 case PTP_DTC_INT64:
720 value->i64 = LE64(&(data[*offset]));
721 *offset += 8;
722 break;
723 case PTP_DTC_UINT64:
724 value->u64 = LE64(&(data[*offset]));
725 *offset += 8;
726 break;
727
728 case PTP_DTC_UINT128:
729 *offset += 16;
730 break;
731 case PTP_DTC_INT128:
732 *offset += 16;
733 break;
734
735 case PTP_DTC_STR:
736
737 PTP_GetString((uint8_t *)value->str, (uint8_t *)&(data[*offset]), &len);
738 *offset += len*2+1;
739 break;
740 default:
741 break;
742 }
743 }
744
745
746 /**
747 * @brief PTP_GetDevicePropValue
748 * Gets objectInfo and fills object_info structure.
749 * @param phost: Host handle
750 * @param opd: object prop descriptor structure
751 * @retval None
752 */
753 static uint32_t PTP_GetObjectPropList (USBH_HandleTypeDef *phost,
754 MTP_PropertiesTypedef *props,
755 uint32_t len)
756 {
757 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
758 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
759 uint32_t prop_count;
760 uint32_t offset = 0, i;
761
762 prop_count = LE32(data);
763
764
765 if (prop_count == 0)
766 {
767 return 0;
768 }
769
770 data += sizeof(uint32_t);
771 len -= sizeof(uint32_t);
772
773 for (i = 0; i < prop_count; i++)
774 {
775 if (len <= 0)
776 {
777 return 0;
778 }
779
780 props[i].ObjectHandle = LE32(data);
781 data += sizeof(uint32_t);
782 len -= sizeof(uint32_t);
783
784 props[i].property = LE16(data);
785 data += sizeof(uint16_t);
786 len -= sizeof(uint16_t);
787
788 props[i].datatype = LE16(data);
789 data += sizeof(uint16_t);
790 len -= sizeof(uint16_t);
791
792 offset = 0;
793
794 PTP_GetDevicePropValue(phost, &offset, len, &props[i].propval, props[i].datatype);
795
796 data += offset;
797 len -= offset;
798 }
799
800 return prop_count;
801 }
802
803 /**
804 * @brief PTP_GetString
805 * Gets SCII String from.
806 * @param str: ascii string
807 * @param data: Device info structure
808 * @retval None
809 */
810 static void PTP_GetString (uint8_t *str, uint8_t* data, uint16_t *len)
811 {
812 uint16_t strlength;
813 uint16_t idx;
814
815 *len = data[0];
816 strlength = 2 * data[0];
817 data ++; /* Adjust the offset ignoring the String Len */
818
819 for (idx = 0; idx < strlength; idx+=2 )
820 {
821 /* Copy Only the string and ignore the UNICODE ID, hence add the src */
822 *str = data[idx];
823 str++;
824 }
825 *str = 0; /* mark end of string */
826 }
827
828 /**
829 * @brief PTP_GetString
830 * Gets SCII String from.
831 * @param str: ascii string
832 * @param data: Device info structure
833 * @retval None
834 */
835
836 static uint32_t PTP_GetArray16 (uint16_t *array, uint8_t *data, uint32_t offset)
837 {
838 uint32_t size, idx = 0;
839
840 size=LE32(&data[offset]);
841 while (size > idx)
842 {
843 array[idx] = LE16(&data[offset+(sizeof(uint16_t)*(idx+2))]);
844 idx++;
845 }
846 return size;
847 }
848
849 /**
850 * @brief PTP_GetString
851 * Gets SCII String from.
852 * @param str: ascii string
853 * @param data: Device info structure
854 * @retval None
855 */
856
857 static uint32_t PTP_GetArray32 (uint32_t *array, uint8_t *data, uint32_t offset)
858 {
859 uint32_t size, idx = 0;
860
861 size=LE32(&data[offset]);
862 while (size > idx)
863 {
864 array[idx] = LE32(&data[offset+(sizeof(uint32_t)*(idx+1))]);
865 idx++;
866 }
867 return size;
868 }
869
870 /*******************************************************************************
871
872 PTP Requests
873
874 *******************************************************************************/
875 /**
876 * @brief USBH_PTP_OpenSession
877 * Open a new session
878 * @param phost: Host handle
879 * @param session: Session ID (MUST BE > 0)
880 * @retval USBH Status
881 */
882 USBH_StatusTypeDef USBH_PTP_OpenSession (USBH_HandleTypeDef *phost, uint32_t session)
883 {
884 USBH_StatusTypeDef status = USBH_BUSY;
885 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
886 PTP_ContainerTypedef ptp_container;
887
888 switch(MTP_Handle->ptp.req_state)
889 {
890 case PTP_REQ_SEND:
891
892 /* Init session params */
893 MTP_Handle->ptp.transaction_id = 0x00000000;
894 MTP_Handle->ptp.session_id = session;
895 MTP_Handle->ptp.flags = PTP_DP_NODATA;
896
897 /* Fill operation request params */
898 ptp_container.Code = PTP_OC_OpenSession;
899 ptp_container.SessionID = session;
900 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
901 ptp_container.Param1 = session;
902 ptp_container.Nparam = 1;
903
904 /* convert request packet inti USB raw packet*/
905 USBH_PTP_SendRequest (phost, &ptp_container);
906
907 /* Setup State machine and start transfer */
908 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
909 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
910 status = USBH_BUSY;
911 break;
912
913 case PTP_REQ_WAIT:
914 status = USBH_PTP_Process(phost);
915 break;
916
917 default:
918 break;
919 }
920 return status;
921 }
922
923 /**
924 * @brief USBH_PTP_GetDevicePropDesc
925 * Gets object partially
926 * @param phost: Host handle
927 * @param dev_info: Device info structure
928 * @retval USBH Status
929 */
930 USBH_StatusTypeDef USBH_PTP_GetDevicePropDesc (USBH_HandleTypeDef *phost,
931 uint16_t propcode,
932 PTP_DevicePropDescTypdef* devicepropertydesc)
933 {
934 USBH_StatusTypeDef status = USBH_BUSY;
935 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
936 PTP_ContainerTypedef ptp_container;
937 uint8_t *data = MTP_Handle->ptp.data_container.payload.data;
938 switch(MTP_Handle->ptp.req_state)
939 {
940 case PTP_REQ_SEND:
941
942 /* Set operation request type */
943 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
944 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
945 MTP_Handle->ptp.data_length = 0;
946 MTP_Handle->ptp.data_packet_counter = 0;
947 MTP_Handle->ptp.data_packet = 0;
948
949 /* Fill operation request params */
950 ptp_container.Code = PTP_OC_GetDevicePropDesc;
951 ptp_container.SessionID = MTP_Handle->ptp.session_id;
952 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
953 ptp_container.Param1 = propcode;
954 ptp_container.Nparam = 1;
955
956 /* convert request packet into USB raw packet*/
957 USBH_PTP_SendRequest (phost, &ptp_container);
958
959 /* Setup State machine and start transfer */
960 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
961 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
962 status = USBH_BUSY;
963 break;
964
965 case PTP_REQ_WAIT:
966 status = USBH_PTP_Process(phost);
967
968 if(status == USBH_OK)
969 {
970 devicepropertydesc->DevicePropertyCode = LE16(&data[PTP_dpd_DevicePropertyCode]);
971 devicepropertydesc->DataType = LE16(&data[PTP_dpd_DataType]);
972 devicepropertydesc->GetSet = *(uint8_t *)(&data[PTP_dpd_GetSet]);
973 devicepropertydesc->FormFlag = PTP_DPFF_None;
974 }
975 break;
976
977 default:
978 break;
979 }
980 return status;
981 }
982 /**
983 * @brief USBH_PTP_GetDeviceInfo
984 * Gets device info dataset and fills deviceinfo structure.
985 * @param phost: Host handle
986 * @param dev_info: Device info structure
987 * @retval USBH Status
988 */
989 USBH_StatusTypeDef USBH_PTP_GetDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info)
990 {
991 USBH_StatusTypeDef status = USBH_BUSY;
992 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
993 PTP_ContainerTypedef ptp_container;
994
995 switch(MTP_Handle->ptp.req_state)
996 {
997 case PTP_REQ_SEND:
998
999 /* Set operation request type */
1000 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1001 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1002 MTP_Handle->ptp.data_length = 0;
1003 MTP_Handle->ptp.data_packet_counter = 0;
1004 MTP_Handle->ptp.data_packet = 0;
1005
1006 /* Fill operation request params */
1007 ptp_container.Code = PTP_OC_GetDeviceInfo;
1008 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1009 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1010 ptp_container.Nparam = 0;
1011
1012 /* convert request packet inti USB raw packet*/
1013 USBH_PTP_SendRequest (phost, &ptp_container);
1014
1015 /* Setup State machine and start transfer */
1016 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1017 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1018 status = USBH_BUSY;
1019 break;
1020
1021 case PTP_REQ_WAIT:
1022 status = USBH_PTP_Process(phost);
1023
1024 if(status == USBH_OK)
1025 {
1026 PTP_DecodeDeviceInfo (phost, dev_info);
1027 }
1028 break;
1029
1030 default:
1031 break;
1032 }
1033 return status;
1034 }
1035
1036 /**
1037 * @brief USBH_PTP_GetStorageIds
1038 * Gets device info dataset and fills deviceinfo structure.
1039 * @param phost: Host handle
1040 * @param dev_info: Device info structure
1041 * @retval USBH Status
1042 */
1043 USBH_StatusTypeDef USBH_PTP_GetStorageIds (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *storage_ids)
1044 {
1045 USBH_StatusTypeDef status = USBH_BUSY;
1046 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1047 PTP_ContainerTypedef ptp_container;
1048
1049 switch(MTP_Handle->ptp.req_state)
1050 {
1051 case PTP_REQ_SEND:
1052
1053 /* Set operation request type */
1054 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1055 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1056 MTP_Handle->ptp.data_length = 0;
1057 MTP_Handle->ptp.data_packet_counter = 0;
1058 MTP_Handle->ptp.data_packet = 0;
1059
1060 /* Fill operation request params */
1061 ptp_container.Code = PTP_OC_GetStorageIDs;
1062 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1063 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1064 ptp_container.Nparam = 0;
1065
1066 /* convert request packet inti USB raw packet*/
1067 USBH_PTP_SendRequest (phost, &ptp_container);
1068
1069 /* Setup State machine and start transfer */
1070 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1071 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1072 status = USBH_BUSY;
1073 break;
1074
1075 case PTP_REQ_WAIT:
1076 status = USBH_PTP_Process(phost);
1077
1078 if(status == USBH_OK)
1079 {
1080 PTP_GetStorageIDs (phost, storage_ids);
1081 }
1082 break;
1083
1084 default:
1085 break;
1086 }
1087 return status;
1088 }
1089
1090 /**
1091 * @brief USBH_PTP_GetDeviceInfo
1092 * Gets device info dataset and fills deviceinfo structure.
1093 * @param phost: Host handle
1094 * @param dev_info: Device info structure
1095 * @retval USBH Status
1096 */
1097 USBH_StatusTypeDef USBH_PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *storage_info)
1098 {
1099 USBH_StatusTypeDef status = USBH_BUSY;
1100 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1101 PTP_ContainerTypedef ptp_container;
1102
1103 switch(MTP_Handle->ptp.req_state)
1104 {
1105 case PTP_REQ_SEND:
1106
1107 /* Set operation request type */
1108 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1109 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1110 MTP_Handle->ptp.data_length = 0;
1111 MTP_Handle->ptp.data_packet_counter = 0;
1112 MTP_Handle->ptp.data_packet = 0;
1113
1114 /* Fill operation request params */
1115 ptp_container.Code = PTP_OC_GetStorageInfo;
1116 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1117 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1118 ptp_container.Param1 = storage_id;
1119 ptp_container.Nparam = 1;
1120
1121 /* convert request packet inti USB raw packet*/
1122 USBH_PTP_SendRequest (phost, &ptp_container);
1123
1124 /* Setup State machine and start transfer */
1125 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1126 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1127 status = USBH_BUSY;
1128 break;
1129
1130 case PTP_REQ_WAIT:
1131 status = USBH_PTP_Process(phost);
1132
1133 if(status == USBH_OK)
1134 {
1135 PTP_GetStorageInfo (phost, storage_id, storage_info);
1136 }
1137 break;
1138
1139 default:
1140 break;
1141 }
1142 return status;
1143 }
1144
1145 /**
1146 * @brief USBH_PTP_GetNumObjects
1147 * Gets device info dataset and fills deviceinfo structure.
1148 * @param phost: Host handle
1149 * @param dev_info: Device info structure
1150 * @retval USBH Status
1151 */
1152 USBH_StatusTypeDef USBH_PTP_GetNumObjects (USBH_HandleTypeDef *phost,
1153 uint32_t storage_id,
1154 uint32_t objectformatcode,
1155 uint32_t associationOH,
1156 uint32_t* numobs)
1157 {
1158 USBH_StatusTypeDef status = USBH_BUSY;
1159 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1160 PTP_ContainerTypedef ptp_container;
1161
1162 switch(MTP_Handle->ptp.req_state)
1163 {
1164 case PTP_REQ_SEND:
1165
1166 /* Set operation request type */
1167 MTP_Handle->ptp.flags = PTP_DP_NODATA;
1168
1169 /* Fill operation request params */
1170 ptp_container.Code = PTP_OC_GetNumObjects;
1171 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1172 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1173 ptp_container.Param1 = storage_id;
1174 ptp_container.Param2 = objectformatcode;
1175 ptp_container.Param3 = associationOH;
1176 ptp_container.Nparam = 3;
1177
1178 /* convert request packet into USB raw packet*/
1179 USBH_PTP_SendRequest (phost, &ptp_container);
1180
1181 /* Setup State machine and start transfer */
1182 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1183 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1184 status = USBH_BUSY;
1185 break;
1186
1187 case PTP_REQ_WAIT:
1188 status = USBH_PTP_Process(phost);
1189
1190 if(status == USBH_OK)
1191 {
1192 *numobs = MTP_Handle->ptp.resp_container.param1;
1193 }
1194 break;
1195
1196 default:
1197 break;
1198 }
1199 return status;
1200 }
1201
1202 /**
1203 * @brief USBH_PTP_GetObjectHandles
1204 * Gets device info dataset and fills deviceinfo structure.
1205 * @param phost: Host handle
1206 * @param dev_info: Device info structure
1207 * @retval USBH Status
1208 */
1209 USBH_StatusTypeDef USBH_PTP_GetObjectHandles (USBH_HandleTypeDef *phost,
1210 uint32_t storage_id,
1211 uint32_t objectformatcode,
1212 uint32_t associationOH,
1213 PTP_ObjectHandlesTypedef* objecthandles)
1214 {
1215 USBH_StatusTypeDef status = USBH_BUSY;
1216 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1217 PTP_ContainerTypedef ptp_container;
1218
1219 switch(MTP_Handle->ptp.req_state)
1220 {
1221 case PTP_REQ_SEND:
1222
1223 /* Set operation request type */
1224 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1225 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1226 MTP_Handle->ptp.data_length = 0;
1227 MTP_Handle->ptp.data_packet_counter = 0;
1228 MTP_Handle->ptp.data_packet = 0;
1229
1230 /* Fill operation request params */
1231 ptp_container.Code = PTP_OC_GetObjectHandles;
1232 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1233 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1234 ptp_container.Param1 = storage_id;
1235 ptp_container.Param2 = objectformatcode;
1236 ptp_container.Param3 = associationOH;
1237 ptp_container.Nparam = 3;
1238
1239 /* convert request packet into USB raw packet*/
1240 USBH_PTP_SendRequest (phost, &ptp_container);
1241
1242 /* Setup State machine and start transfer */
1243 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1244 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1245 status = USBH_BUSY;
1246 break;
1247
1248 case PTP_REQ_WAIT:
1249 status = USBH_PTP_Process(phost);
1250
1251 if(status == USBH_OK)
1252 {
1253 objecthandles->n = PTP_GetArray32 (objecthandles->Handler,
1254 MTP_Handle->ptp.data_container.payload.data,
1255 0);
1256 }
1257 break;
1258
1259 default:
1260 break;
1261 }
1262 return status;
1263 }
1264
1265 /**
1266 * @brief USBH_PTP_GetObjectInfo
1267 * Gets objert info
1268 * @param phost: Host handle
1269 * @param dev_info: Device info structure
1270 * @retval USBH Status
1271 */
1272 USBH_StatusTypeDef USBH_PTP_GetObjectInfo (USBH_HandleTypeDef *phost,
1273 uint32_t handle,
1274 PTP_ObjectInfoTypedef* object_info)
1275 {
1276 USBH_StatusTypeDef status = USBH_BUSY;
1277 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1278 PTP_ContainerTypedef ptp_container;
1279
1280 switch(MTP_Handle->ptp.req_state)
1281 {
1282 case PTP_REQ_SEND:
1283
1284 /* Set operation request type */
1285 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1286 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1287 MTP_Handle->ptp.data_length = 0;
1288 MTP_Handle->ptp.data_packet_counter = 0;
1289 MTP_Handle->ptp.data_packet = 0;
1290
1291 /* Fill operation request params */
1292 ptp_container.Code = PTP_OC_GetObjectInfo;
1293 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1294 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1295 ptp_container.Param1 = handle;
1296 ptp_container.Nparam = 1;
1297
1298 /* convert request packet into USB raw packet*/
1299 USBH_PTP_SendRequest (phost, &ptp_container);
1300
1301 /* Setup State machine and start transfer */
1302 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1303 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1304 status = USBH_BUSY;
1305 break;
1306
1307 case PTP_REQ_WAIT:
1308 status = USBH_PTP_Process(phost);
1309
1310 if(status == USBH_OK)
1311 {
1312 PTP_GetObjectInfo (phost, object_info);
1313 }
1314 break;
1315
1316 default:
1317 break;
1318 }
1319 return status;
1320 }
1321
1322 /**
1323 * @brief USBH_PTP_DeleteObject
1324 * Delete an object.
1325 * @param phost: Host handle
1326 * @param handle : Object Handle
1327 * @retval USBH Status
1328 */
1329 USBH_StatusTypeDef USBH_PTP_DeleteObject (USBH_HandleTypeDef *phost,
1330 uint32_t handle,
1331 uint32_t objectformatcode)
1332 {
1333 USBH_StatusTypeDef status = USBH_BUSY;
1334 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1335 PTP_ContainerTypedef ptp_container;
1336
1337 switch(MTP_Handle->ptp.req_state)
1338 {
1339 case PTP_REQ_SEND:
1340
1341 /* Set operation request type */
1342 MTP_Handle->ptp.flags = PTP_DP_NODATA;
1343
1344 /* Fill operation request params */
1345 ptp_container.Code = PTP_OC_DeleteObject;
1346 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1347 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1348 ptp_container.Param1 = handle;
1349 ptp_container.Param2 = objectformatcode;
1350 ptp_container.Nparam = 2;
1351
1352 /* convert request packet into USB raw packet*/
1353 USBH_PTP_SendRequest (phost, &ptp_container);
1354
1355 /* Setup State machine and start transfer */
1356 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1357 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1358 status = USBH_BUSY;
1359 break;
1360
1361 case PTP_REQ_WAIT:
1362 status = USBH_PTP_Process(phost);
1363 break;
1364
1365 default:
1366 break;
1367 }
1368 return status;
1369 }
1370
1371 /**
1372 * @brief USBH_PTP_GetObject
1373 * Gets object
1374 * @param phost: Host handle
1375 * @param dev_info: Device info structure
1376 * @retval USBH Status
1377 */
1378 USBH_StatusTypeDef USBH_PTP_GetObject (USBH_HandleTypeDef *phost,
1379 uint32_t handle,
1380 uint8_t *object)
1381 {
1382 USBH_StatusTypeDef status = USBH_BUSY;
1383 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1384 PTP_ContainerTypedef ptp_container;
1385
1386 switch(MTP_Handle->ptp.req_state)
1387 {
1388 case PTP_REQ_SEND:
1389
1390 /* Set operation request type */
1391 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1392 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1393 MTP_Handle->ptp.data_length = 0;
1394 MTP_Handle->ptp.data_packet_counter = 0;
1395 MTP_Handle->ptp.data_packet = 0;
1396
1397 /* set object control params */
1398 MTP_Handle->ptp.object_ptr = object;
1399
1400 /* Fill operation request params */
1401 ptp_container.Code = PTP_OC_GetObject;
1402 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1403 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1404 ptp_container.Param1 = handle;
1405 ptp_container.Nparam = 1;
1406
1407
1408 /* convert request packet into USB raw packet*/
1409 USBH_PTP_SendRequest (phost, &ptp_container);
1410
1411 /* Setup State machine and start transfer */
1412 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1413 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1414 status = USBH_BUSY;
1415 break;
1416
1417 case PTP_REQ_WAIT:
1418 status = USBH_PTP_Process(phost);
1419
1420 if(status == USBH_OK)
1421 {
1422 /* first packet is in the PTP data payload buffer */
1423 if(MTP_Handle->ptp.iteration == 0)
1424 {
1425 /* copy it to object */
1426 USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, PTP_USB_BULK_PAYLOAD_LEN_READ);
1427 }
1428 }
1429 break;
1430
1431 default:
1432 break;
1433 }
1434 return status;
1435 }
1436
1437 /**
1438 * @brief USBH_PTP_GetPartialObject
1439 * Gets object partially
1440 * @param phost: Host handle
1441 * @param dev_info: Device info structure
1442 * @retval USBH Status
1443 */
1444 USBH_StatusTypeDef USBH_PTP_GetPartialObject(USBH_HandleTypeDef *phost,
1445 uint32_t handle,
1446 uint32_t offset,
1447 uint32_t maxbytes,
1448 uint8_t *object,
1449 uint32_t *len)
1450 {
1451 USBH_StatusTypeDef status = USBH_BUSY;
1452 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1453 PTP_ContainerTypedef ptp_container;
1454
1455 switch(MTP_Handle->ptp.req_state)
1456 {
1457 case PTP_REQ_SEND:
1458
1459 /* Set operation request type */
1460 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1461 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1462 MTP_Handle->ptp.data_length = 0;
1463 MTP_Handle->ptp.data_packet_counter = 0;
1464 MTP_Handle->ptp.data_packet = 0;
1465
1466 /* set object control params */
1467 MTP_Handle->ptp.object_ptr = object;
1468
1469 /* Fill operation request params */
1470 ptp_container.Code = PTP_OC_GetPartialObject;
1471 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1472 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1473 ptp_container.Param1 = handle;
1474 ptp_container.Param2 = offset;
1475 ptp_container.Param3 = maxbytes;
1476 ptp_container.Nparam = 3;
1477
1478 /* convert request packet into USB raw packet*/
1479 USBH_PTP_SendRequest (phost, &ptp_container);
1480
1481 /* Setup State machine and start transfer */
1482 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1483 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1484 status = USBH_BUSY;
1485 break;
1486
1487 case PTP_REQ_WAIT:
1488 status = USBH_PTP_Process(phost);
1489
1490 if(status == USBH_OK)
1491 {
1492 *len = MTP_Handle->ptp.resp_container.param1;
1493 /* first packet is in the PTP data payload buffer */
1494 if(MTP_Handle->ptp.iteration == 0)
1495 {
1496 /* copy it to object */
1497 USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, *len);
1498 }
1499 }
1500 break;
1501
1502 default:
1503 break;
1504 }
1505 return status;
1506 }
1507
1508 /**
1509 * @brief USBH_PTP_GetObjectPropsSupported
1510 * Gets object partially
1511 * @param phost: Host handle
1512 * @param dev_info: Device info structure
1513 * @retval USBH Status
1514 */
1515 USBH_StatusTypeDef USBH_PTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost,
1516 uint16_t ofc,
1517 uint32_t *propnum,
1518 uint16_t *props)
1519 {
1520 USBH_StatusTypeDef status = USBH_BUSY;
1521 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1522 PTP_ContainerTypedef ptp_container;
1523
1524 switch(MTP_Handle->ptp.req_state)
1525 {
1526 case PTP_REQ_SEND:
1527
1528 /* Set operation request type */
1529 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1530 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1531 MTP_Handle->ptp.data_length = 0;
1532 MTP_Handle->ptp.data_packet_counter = 0;
1533 MTP_Handle->ptp.data_packet = 0;
1534
1535 /* Fill operation request params */
1536 ptp_container.Code = PTP_OC_GetObjectPropsSupported;
1537 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1538 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1539 ptp_container.Param1 = ofc;
1540 ptp_container.Nparam = 1;
1541
1542 /* convert request packet into USB raw packet*/
1543 USBH_PTP_SendRequest (phost, &ptp_container);
1544
1545 /* Setup State machine and start transfer */
1546 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1547 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1548 status = USBH_BUSY;
1549 break;
1550
1551 case PTP_REQ_WAIT:
1552 status = USBH_PTP_Process(phost);
1553
1554 if(status == USBH_OK)
1555 {
1556 *propnum = PTP_GetArray16 (props, MTP_Handle->ptp.data_container.payload.data, 0);
1557 }
1558 break;
1559
1560 default:
1561 break;
1562 }
1563 return status;
1564 }
1565
1566 /**
1567 * @brief USBH_PTP_GetObjectPropDesc
1568 * Gets object partially
1569 * @param phost: Host handle
1570 * @param dev_info: Device info structure
1571 * @retval USBH Status
1572 */
1573 USBH_StatusTypeDef USBH_PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost,
1574 uint16_t opc,
1575 uint16_t ofc,
1576 PTP_ObjectPropDescTypeDef *opd)
1577 {
1578 USBH_StatusTypeDef status = USBH_BUSY;
1579 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1580 PTP_ContainerTypedef ptp_container;
1581
1582 switch(MTP_Handle->ptp.req_state)
1583 {
1584 case PTP_REQ_SEND:
1585
1586 /* Set operation request type */
1587 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1588 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1589 MTP_Handle->ptp.data_length = 0;
1590 MTP_Handle->ptp.data_packet_counter = 0;
1591 MTP_Handle->ptp.data_packet = 0;
1592
1593 /* Fill operation request params */
1594 ptp_container.Code = PTP_OC_GetObjectPropDesc;
1595 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1596 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1597 ptp_container.Param1 = opc;
1598 ptp_container.Param2 = ofc;
1599 ptp_container.Nparam = 2;
1600
1601 /* convert request packet into USB raw packet*/
1602 USBH_PTP_SendRequest (phost, &ptp_container);
1603
1604 /* Setup State machine and start transfer */
1605 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1606 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1607 status = USBH_BUSY;
1608 break;
1609
1610 case PTP_REQ_WAIT:
1611 status = USBH_PTP_Process(phost);
1612
1613 if(status == USBH_OK)
1614 {
1615 PTP_GetObjectPropDesc(phost, opd, MTP_Handle->ptp.data_length);
1616 }
1617 break;
1618
1619 default:
1620 break;
1621 }
1622 return status;
1623 }
1624
1625 /**
1626 * @brief USBH_PTP_GetObjectPropList
1627 * Gets object partially
1628 * @param phost: Host handle
1629 * @param dev_info: Device info structure
1630 * @retval USBH Status
1631 */
1632 USBH_StatusTypeDef USBH_PTP_GetObjectPropList (USBH_HandleTypeDef *phost,
1633 uint32_t handle,
1634 MTP_PropertiesTypedef *pprops,
1635 uint32_t *nrofprops)
1636 {
1637 USBH_StatusTypeDef status = USBH_BUSY;
1638 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1639 PTP_ContainerTypedef ptp_container;
1640
1641 switch(MTP_Handle->ptp.req_state)
1642 {
1643 case PTP_REQ_SEND:
1644
1645 /* Set operation request type */
1646 MTP_Handle->ptp.flags = PTP_DP_GETDATA;
1647 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1648 MTP_Handle->ptp.data_length = 0;
1649 MTP_Handle->ptp.data_packet_counter = 0;
1650 MTP_Handle->ptp.data_packet = 0;
1651
1652 /* copy first packet of the object into data container */
1653 USBH_memcpy(MTP_Handle->ptp.data_container.payload.data, MTP_Handle->ptp.object_ptr, PTP_USB_BULK_PAYLOAD_LEN_READ);
1654
1655 /* Fill operation request params */
1656 ptp_container.Code = PTP_OC_GetObjPropList;
1657 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1658 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1659 ptp_container.Param1 = handle;
1660 ptp_container.Param2 = 0x00000000U; /* 0x00000000U should be "all formats" */
1661 ptp_container.Param3 = 0xFFFFFFFFU; /* 0xFFFFFFFFU should be "all properties" */
1662 ptp_container.Param4 = 0x00000000U;
1663 ptp_container.Param5 = 0xFFFFFFFFU; /* Return full tree below the Param1 handle */
1664 ptp_container.Nparam = 5;
1665
1666 /* convert request packet into USB raw packet*/
1667 USBH_PTP_SendRequest (phost, &ptp_container);
1668
1669 /* Setup State machine and start transfer */
1670 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1671 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1672 status = USBH_BUSY;
1673 break;
1674
1675 case PTP_REQ_WAIT:
1676 status = USBH_PTP_Process(phost);
1677
1678 if(status == USBH_OK)
1679 {
1680 PTP_GetObjectPropList (phost, pprops, MTP_Handle->ptp.data_length);
1681 }
1682 break;
1683
1684 default:
1685 break;
1686 }
1687 return status;
1688 }
1689
1690 /**
1691 * @brief USBH_PTP_SendObject
1692 * Send an object
1693 * @param phost: Host handle
1694 * @param dev_info: Device info structure
1695 * @retval USBH Status
1696 */
1697 USBH_StatusTypeDef USBH_PTP_SendObject (USBH_HandleTypeDef *phost,
1698 uint32_t handle,
1699 uint8_t *object,
1700 uint32_t size)
1701 {
1702 USBH_StatusTypeDef status = USBH_BUSY;
1703 MTP_HandleTypeDef *MTP_Handle = phost->pActiveClass->pData;
1704 PTP_ContainerTypedef ptp_container;
1705
1706 switch(MTP_Handle->ptp.req_state)
1707 {
1708 case PTP_REQ_SEND:
1709
1710 /* Set operation request type */
1711 MTP_Handle->ptp.flags = PTP_DP_SENDDATA;
1712 MTP_Handle->ptp.data_ptr = (uint8_t *)&(MTP_Handle->ptp.data_container);
1713 MTP_Handle->ptp.data_packet_counter = 0;
1714 MTP_Handle->ptp.data_packet = 0;
1715 MTP_Handle->ptp.iteration = 0;
1716
1717 /* set object control params */
1718 MTP_Handle->ptp.object_ptr = object;
1719 MTP_Handle->ptp.data_length = size;
1720
1721 /* Fill operation request params */
1722 ptp_container.Code = PTP_OC_SendObject;
1723 ptp_container.SessionID = MTP_Handle->ptp.session_id;
1724 ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++;
1725 ptp_container.Nparam = 0;
1726
1727
1728 /* convert request packet into USB raw packet*/
1729 USBH_PTP_SendRequest (phost, &ptp_container);
1730
1731 /* Setup State machine and start transfer */
1732 MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE;
1733 MTP_Handle->ptp.req_state = PTP_REQ_WAIT;
1734 status = USBH_BUSY;
1735 break;
1736
1737 case PTP_REQ_WAIT:
1738 status = USBH_PTP_Process(phost);
1739 break;
1740
1741 default:
1742 break;
1743 }
1744 return status;
1745 }
1746 /**
1747 * @}
1748 */
1749
1750 /**
1751 * @}
1752 */
1753
1754 /**
1755 * @}
1756 */
1757
1758 /**
1759 * @}
1760 */
1761
1762 /**
1763 * @}
1764 */
1765
1766 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1767
1768
1769