---
title: Picture Transfer Protocol
date: 2024-06-12T06:51:12.000Z
updated: 2026-01-08T14:51:00.650Z
url: https://baku89.com/Picture_Transfer_Protocol
---

# Picture Transfer Protocol

[Tethr](/Tethr) が基盤とするカメラのテザーシステムのための通信プロトコル。

## Tethrに新しいカメラをサポートさせたい

-

### WireSharkでのリバースエンジニアリング方法

- **Apple SiliconでUSB Trafficを覗き見る**

  - Wiresharkをインストール

  - SIPを無効にする

  - Wiresharkに表示されているXHCから始まるポートそれぞれに、`sudo ifconfig XHC0 up` を実行する

  - `XHR#`のうちのいずれかがUSBカメラ

  - 送受信パケットのうち

    - Infoが以下のものをチェック。(submitted) がつく行を無視する

      - URB\_BULK out (completed):PC → カメラ
      - URB\_BULK in (completed): カメラ → PC

    - Leftover Capture Dataから読んでいく

    - ![](https://wp.baku89.com/wp-content/uploads/2026/07/scrapbox-66695a1c90fa56001c760069.png)

## PTPのコミュニケーションの流れ

3つのデータ送受信タイプが存在する

**SendCommand**: カメラに短いコマンドを送信する

- `transferOutCommand (PTPType.Command)`: OpCodeを送信
- `waitBulkIn (PTPType.Response)`: カメラからのレスポンス

**SendData**: カメラに長いデータを送信する

- `transferOutCommand (PTPType.Command)`: OpCodeを送信
- `transferOutData (PTPType.Data)`: 実データを送信
- `waitBulkIn (PTPType.Response)`: カメラからのレスポンス

**ReceiveData**: カメラからデータを取得する

- `transferOutCommand`: 問い合わせたいOpCodeを送る

- _成功した場合_

  - `waitBulkIn`: カメラから本データを受信
  - `waitBulkIn`: 最後のレスポンス

- _カメラが何らかの理由で実データを用意できない場合_
  - `waitBulkIn (PTPType=Response)`: 失敗コードを代わりに送る

## パケットの構造

**PTPType.Command**（PC→カメラに）

```
0c 00 00 00 // Length: 0x0000000c = 12 bytes (Leftover Capture Dataのバイト数と一致)
01 00       // PTPType: 0x0001 = Command
01 10       // OpCode: 0x1001 = GetDeviceInfo
12 00 00 00 // Transaction ID: 0x12 = 18
```

**PTPType.Data**（PC→カメラ、カメラ→PCの両方）

```
21 01 00 00 // Length
02 00       // PTPType: 0x0002 = Data
01 10       // OpCode: 0x1001 = GetDeviceInfo
12 00 00 00 // Transaction ID: 0x12 = 18
...         // 実データ
```

**PTPType.Response**（カメラ→PCに）

```
0c 00 00 00 // Length
03 00       // PTPType: 0x0003 = Response
01 20       // PTPResponse: 0x2001 = OK
12 00 00 00 // Transaction ID: 0x12 = 18
```
