1#!/usr/bin/env python3
2#
3#  Copyright (c) 2019, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29from typing import Tuple, List
30
31from pktverify.consts import COAP_CODE_POST, COAP_CODE_ACK
32from pktverify.layers import Layer
33
34
35class CoapTlvParser(object):
36
37    @staticmethod
38    def parse(t, v: bytearray) -> List[Tuple[str, str]]:
39        assert isinstance(v, bytearray)
40        return []
41
42
43class CoapLayer(Layer):
44    """
45    Represents a COAP layer.
46    """
47
48    def __init__(self, packet, layer_name):
49        super().__init__(packet, layer_name)
50
51    @property
52    def is_post(self) -> bool:
53        """
54        Returns if the COAP layer is using code POST.
55        """
56        return self.code == COAP_CODE_POST
57
58    @property
59    def is_ack(self) -> bool:
60        """
61        Returns if the COAP layer is using code ACK.
62        """
63        return self.code == COAP_CODE_ACK
64
65    def __getattr__(self, name):
66        super_attr = super().__getattr__(name)
67        if name == 'tlv':
68            self._parse_coap_payload()
69
70        return super_attr
71
72    def _parse_coap_payload(self):
73        payload = self.payload
74
75        r = 0
76        while True:
77            t, tvs, r = self._parse_next_tlv(payload, r)
78            if t is None:
79                break
80
81            self._add_field('coap.tlv.type', hex(t))
82            for k, v in tvs:
83                assert isinstance(k, str), (t, k, v)
84                assert isinstance(v, str), (t, k, v)
85                self._add_field('coap.tlv.' + k, v)
86
87    @staticmethod
88    def _parse_next_tlv(payload, read_pos) -> tuple:
89        assert read_pos <= len(payload)
90        if read_pos == len(payload):
91            return None, None, read_pos
92
93        t = payload[read_pos]
94        len_ = payload[read_pos + 1]
95        assert (len(payload) - read_pos - 2 >= len_)
96        kvs = CoapTlvParser.parse(t, payload[read_pos + 2:read_pos + 2 + len_])
97        return t, kvs, read_pos + len_ + 2
98