CoAP(Constrained Application Protocol)

CoAP(Constrained Application Protocol)は、インターネット技術の標準化を推進する任意団体のIETF(The Internet Engineering Task Force)で標準化策定が進む機器間通信向けのプロトコル。

CoAPは、ヘッダサイズが4バイトであり、代表的な通信プロトコルであるHTTPの140バイトより軽量であるため、HTTPと比較して約6割の通信量削減の効果が期待されている。

RFC7252 The Constrained Application Protocol (CoAP)

簡易HTTPなCoAP(Constrained Application Protocol)プロトコル

M2M アーキテクチャと技術課題 - 電子情報通信学会

                       +----------------------+
                        |      Application     |
                        +----------------------+
                        +----------------------+  \
                        |  Requests/Responses  |  |
                        |----------------------|  | CoAP
                        |       Messages       |  |
                        +----------------------+  /
                        +----------------------+
                        |          UDP         |
                        +----------------------+

                    Figure 1: Abstract Layering of CoAP
CoAPは、リクエスト/レスポンス及びメッセージのプロトコルを定義しています。
                        Client              Server
                           |                                |
                           |   CON [0x7d34]   |
                           +----------------->|
                           |                                |
                           |   ACK [0x7d34]   |
                           |<-----------------+
                           |                                |

                  Figure 2: Reliable Message Transmission
CONとACKだけの転送。0x7d34はメッセージID。
                       Client              Server
                           |                                |
                           |   NON [0x01a0]   |
                           +----------------->|
                           |                           |

                 Figure 3: Unreliable Message Transmission
NONだから返信が不要
       Client                     Server       Client         Server
           |                                 |             |                                  |
           |   CON [0xbc90]     |             |   CON [0xbc91]      |
           | GET /temperature |             | GET /temperature |
           |   (Token 0x71)      |             |   (Token 0x72)       |
             +----------------->|               +----------------->|
           |                                |             |                                   |
           |   ACK [0xbc90]    |             |   ACK [0xbc91]       |
           |   2.05 Content     |             |  4.04 Not Found      |
           |   (Token 0x71)    |             |   (Token 0x72)         |
           |     "22.5 C"           |             |   "Not found"            |
             |<-----------------+                    |<-----------------+
           |                               |                 |                  |

           Figure 4: Two GET Requests with Piggybacked Responses
 温度センサーの交信例。CONのあと、HTTPのGETメソッドが使われ、ACKのあと、2.05や4.04などのステータスコードとコンテンツが帰ってくる。
                     Client              Server
                           |                  |
                           |   CON [0x7a10]   |
                           | GET /temperature |
                           |   (Token 0x73)   |
                           +----------------->|
                           |                  |
                           |   ACK [0x7a10]   |
                           |<-----------------+
                           |                  |
                           ... Time Passes  ...
                           |                  |
                           |   CON [0x23bb]   |
                           |   2.05 Content   |
                           |   (Token 0x73)   |
                           |     "22.5 C"     |
                           |<-----------------+
                           |                  |
                           |   ACK [0x23bb]   |
                           +----------------->|
                           |                  |

             Figure 5: A GET Request with a Separate Response
           非同期通信のイメージ。Token がリクエストの識別子。
                     Client              Server
                           |                  |
                           |   NON [0x7a11]   |
                           | GET /temperature |
                           |   (Token 0x74)   |
                           +----------------->|
                           |                  |
                           |   NON [0x23bc]   |
                           |   2.05 Content   |
                           |   (Token 0x74)   |
                           |     "22.5 C"     |
                           |<-----------------+
                           |                  |

       Figure 6: A Request and a Response Carried in Non-confirmable
                                 Messages
NONベースの通信。
    0                               1                                 2                               3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Ver| T |  TKL  |      Code     |          Message ID           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Token (if any, TKL bytes) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Options (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |1 1 1 1 1 1 1 1|    Payload (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                         Figure 7: Message Format


                     +----------+--- --+--- --+--- --+--- --+
                     |               | CON | NON | ACK | RST |
                     +----------+-----+-----+-----+-----+
                   | Request   | X     | X     | -     | -      |
                   | Response | X    | X     | X     | -      |
                   | Empty      | *     | -     | X     | X      |
                     +----------+--- --+--- --+-- ---+--- --+

                      Table 1: Usage of Message Types

                  +-------------------+---------------+
                   | name                 | default value |
                   +-------------------+---------------+
                   | ACK_TIMEOUT       | 2 seconds     |
                   | ACK_RANDOM_FACTOR | 1.5           |
                   | MAX_RETRANSMIT    | 4             |
                   | NSTART            | 1             |
                   | DEFAULT_LEISURE   | 5 seconds     |
                   | PROBING_RATE      | 1 byte/second |
                   +-------------------+---------------+

                     Table 2: CoAP Protocol Parameters

                      +------+--------+-----------+
                       | Code | Name   | Reference |
                       +------+--------+-----------+
                       | 0.01 | GET    | [RFC7252] |
                       | 0.02 | POST   | [RFC7252] |
                       | 0.03 | PUT    | [RFC7252] |
                       | 0.04 | DELETE | [RFC7252] |
                       +------+--------+-----------+

                        Table 5: CoAP Method Codes


           +------+------------------------------+-----------+
            | Code | Description                  | Reference |
            +------+------------------------------+-----------+
            | 2.01 | Created                      | [RFC7252] |
            | 2.02 | Deleted                      | [RFC7252] |
            | 2.03 | Valid                        | [RFC7252] |
            | 2.04 | Changed                      | [RFC7252] |
            | 2.05 | Content                      | [RFC7252] |
            | 4.00 | Bad Request                  | [RFC7252] |
            | 4.01 | Unauthorized                 | [RFC7252] |
            | 4.02 | Bad Option                   | [RFC7252] |
            | 4.03 | Forbidden                    | [RFC7252] |
            | 4.04 | Not Found                    | [RFC7252] |
            | 4.05 | Method Not Allowed           | [RFC7252] |
            | 4.06 | Not Acceptable               | [RFC7252] |
            | 4.12 | Precondition Failed          | [RFC7252] |
            | 4.13 | Request Entity Too Large     | [RFC7252] |
            | 4.15 | Unsupported Content-Format   | [RFC7252] |
            | 5.00 | Internal Server Error        | [RFC7252] |
            | 5.01 | Not Implemented              | [RFC7252] |
            | 5.02 | Bad Gateway                  | [RFC7252] |
            | 5.03 | Service Unavailable          | [RFC7252] |
            | 5.04 | Gateway Timeout              | [RFC7252] |
            | 5.05 | Proxying Not Supported       | [RFC7252] |
            +------+------------------------------+-----------+

                       Table 6: CoAP Response Codes

                +--------+------------------+-----------+
                 | Number | Name             | Reference |
                 +--------+------------------+-----------+
                 |      0 | (Reserved)       | [RFC7252] |
                 |      1 | If-Match         | [RFC7252] |
                 |      3 | Uri-Host         | [RFC7252] |
                 |      4 | ETag             | [RFC7252] |
                 |      5 | If-None-Match    | [RFC7252] |
                 |      7 | Uri-Port         | [RFC7252] |
                 |      8 | Location-Path    | [RFC7252] |
                 |     11 | Uri-Path         | [RFC7252] |
                 |     12 | Content-Format   | [RFC7252] |
                 |     14 | Max-Age          | [RFC7252] |
                 |     15 | Uri-Query        | [RFC7252] |
                 |     17 | Accept           | [RFC7252] |
                 |     20 | Location-Query   | [RFC7252] |
                 |     35 | Proxy-Uri        | [RFC7252] |
                 |     39 | Proxy-Scheme     | [RFC7252] |
                 |     60 | Size1            | [RFC7252] |
                 |    128 | (Reserved)       | [RFC7252] |
                 |    132 | (Reserved)       | [RFC7252] |
                 |    136 | (Reserved)       | [RFC7252] |
                 |    140 | (Reserved)       | [RFC7252] |
                 +--------+------------------+-----------+

                       Table 7: CoAP Option Numbers

   +--------------------------+----------+----+------------------------+
   | Media type               | Encoding | ID | Reference              |
   +--------------------------+----------+----+------------------------+
   | text/plain;              | -        |  0 | [RFC2046] [RFC3676]    |
   | charset=utf-8            |          |    | [RFC5147]              |
   | application/link-format  | -        | 40 | [RFC6690]              |
   | application/xml          | -        | 41 | [RFC3023]              |
   | application/octet-stream | -        | 42 | [RFC2045] [RFC2046]    |
   | application/exi          | -        | 47 | [REC-exi-20140211]     |
   | application/json         | -        | 50 | [RFC7159]              |
   +--------------------------+----------+----+------------------------+

                       Table 9: CoAP Content-Formats