/* packet-hdmi.c
 * Routines for HDMI dissection
 * Copyright 2014 Martin Kaiser <martin@kaiser.cx>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* this dissector handles I2C messages on the HDMI Display Data Channel (DDC)
 *
 * EDID (Extended Display Identification Data) messages are dissected here,
 * HDCP messages are passed on to the HDCP dissector
 */

#include "config.h"

#include <glib.h>
#include <epan/packet.h>
#include <epan/ptvcursor.h>
#include <epan/expert.h>
#include <epan/wmem/wmem.h>
#include "packet-hdmi.h"

void proto_register_hdmi(void);
void proto_reg_handoff_hdmi(void);

static int proto_hdmi  = -1;

static dissector_handle_t hdcp_handle;

static gint ett_hdmi = -1;
static gint ett_hdmi_edid = -1;

static int hf_hdmi_addr = -1;
static int hf_hdmi_edid_offset = -1;
static int hf_hdmi_edid_hdr = -1;
static int hf_hdmi_edid_manf_id = -1;
static int hf_hdmi_edid_manf_prod_code = -1;
static int hf_hdmi_edid_manf_serial = -1;
static int hf_hdmi_edid_manf_week = -1;
static int hf_hdmi_edid_mod_year = -1;
static int hf_hdmi_edid_manf_year = -1;


/* also called Source and Sink in the HDMI spec */
#define ADDR_TRX "Transmitter"
#define ADDR_RCV "Receiver"

/* we use 8bit I2C addresses, including the direction bit */
#define ADDR8_HDCP_WRITE 0x74  /* transmitter->receiver */
#define ADDR8_HDCP_READ  0x75  /* r->t */
#define ADDR8_EDID_WRITE 0xA0  /* t->r */
#define ADDR8_EDID_READ  0xA1  /* r->t */

#define HDCP_ADDR8(x)   (x==ADDR8_HDCP_WRITE || x==ADDR8_HDCP_READ)

static const value_string hdmi_addr[] = {
    { ADDR8_HDCP_WRITE, "transmitter writes HDCP data for receiver" },
    { ADDR8_HDCP_READ,  "transmitter reads HDCP data from receiver" },

    { ADDR8_EDID_WRITE, "EDID request" },
    { ADDR8_EDID_READ,  "EDID read" },
    { 0, NULL }
};

#define EDID_HDR_VALUE G_GUINT64_CONSTANT(0x00ffffffffffff00)

/* grab 5 bits, from bit n to n+4, from a big-endian number x
   map those bits to a capital letter such that A==1, B==2, ... */
#define CAPITAL_LETTER(x, n) ('A'-1 + (((x) & (0x1F<<n)) >> n))


gboolean
sub_check_hdmi(packet_info *pinfo _U_)
{
    /* by looking at the i2c_phdr only, we can't decide if this packet is HDMI
       this function is called when the user explicitly selected HDMI
       in the preferences
       therefore, we always return TRUE and hand the data to the (new
       style) dissector who sees the 8bit address and the packet content */

   return TRUE;
}


/* dissect EDID data from the receiver
   return the offset after the dissected data */
static gint
dissect_hdmi_edid(tvbuff_t *tvb, gint offset, packet_info *pinfo, proto_tree *tree)
{
    proto_item *ti, *yi;
    proto_tree *edid_tree;
    guint64     edid_hdr;
    guint16     manf_id;
    gchar       manf_id_str[4]; /* 3 letters + 0-termination */
    guint8      week, year;
    int         year_hf;
    guint8      edid_ver, edid_rev;


    ti = proto_tree_add_text(tree, tvb,
            offset, tvb_reported_length_remaining(tvb, offset),
            "Extended Display Identification Data (EDID)");
    edid_tree = proto_item_add_subtree(ti, ett_hdmi_edid);

    edid_hdr = tvb_get_ntoh64(tvb, offset);
    if (edid_hdr != EDID_HDR_VALUE)
        return offset; /* XXX handle fragmented EDID messages */

    col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "EDID");

    proto_tree_add_item(edid_tree, hf_hdmi_edid_hdr,
            tvb, offset, 8, ENC_LITTLE_ENDIAN);
    offset += 8;

    /* read as big endian for easier splitting */
    manf_id = tvb_get_ntohs(tvb, offset);
    /* XXX check that MSB is 0 */
    manf_id_str[0] = CAPITAL_LETTER(manf_id, 10);
    manf_id_str[1] = CAPITAL_LETTER(manf_id,  5);
    manf_id_str[2] = CAPITAL_LETTER(manf_id,  0);
    manf_id_str[3] = 0;
    proto_tree_add_string(edid_tree, hf_hdmi_edid_manf_id,
            tvb, offset, 2, manf_id_str);
    offset += 2;

    proto_tree_add_item(edid_tree, hf_hdmi_edid_manf_prod_code,
            tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    proto_tree_add_item(edid_tree, hf_hdmi_edid_manf_serial,
            tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    week = tvb_get_guint8(tvb, offset);
    proto_tree_add_item(edid_tree, hf_hdmi_edid_manf_week,
            tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    year_hf = week==255 ? hf_hdmi_edid_mod_year : hf_hdmi_edid_manf_year;
    year = tvb_get_guint8(tvb, offset);
    yi = proto_tree_add_item(edid_tree, year_hf,
            tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_item_append_text(yi, " (year %d)", 1990+year);
    offset++;

    edid_ver = tvb_get_guint8(tvb, offset);
    edid_rev = tvb_get_guint8(tvb, offset+1);

    /* XXX make this filterable */
    proto_tree_add_text(edid_tree, tvb, offset, 2,
            "EDID Version %d.%d", edid_ver, edid_rev);

    /* XXX dissect the parts following the EDID header */

    return tvb_reported_length(tvb);
}


static int
dissect_hdmi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    guint8      addr;
    gint        offset=0;
    proto_item *pi;
    proto_tree *hdmi_tree;

    /* the I2C address in the first byte is always handled by the HDMI
       dissector, even if the packet contains HDCP data */
    addr = tvb_get_guint8(tvb, 0);
    if (!try_val_to_str(addr, hdmi_addr))
        return 0; /* no HDMI packet */

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "HDMI");
    col_clear(pinfo->cinfo, COL_INFO);

    pi = proto_tree_add_protocol_format(tree, proto_hdmi,
            tvb, 0, tvb_reported_length(tvb), "HDMI");
    hdmi_tree = proto_item_add_subtree(pi, ett_hdmi);

    if (addr&0x01) {
        SET_ADDRESS(&pinfo->src, AT_STRINGZ, (int)strlen(ADDR_RCV)+1, ADDR_RCV);
        SET_ADDRESS(&pinfo->dst, AT_STRINGZ, (int)strlen(ADDR_TRX)+1, ADDR_TRX);
        pinfo->p2p_dir = P2P_DIR_RECV;
    }
    else {
        SET_ADDRESS(&pinfo->src, AT_STRINGZ, (int)strlen(ADDR_TRX)+1, ADDR_TRX);
        SET_ADDRESS(&pinfo->dst, AT_STRINGZ, (int)strlen(ADDR_RCV)+1, ADDR_RCV);
        pinfo->p2p_dir = P2P_DIR_SENT;
    }

    /* there's no explicit statement in the spec saying that the protocol is
        big or little endian
       there's three cases: one byte values, symmetrical values or values
        that are explicitly marked as little endian
       for the sake of simplicity, we use little endian everywhere */
    proto_tree_add_item(hdmi_tree, hf_hdmi_addr, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    if (HDCP_ADDR8(addr)) {
        gint      hdcp_len;
        tvbuff_t *hdcp_tvb;

        hdcp_len = tvb_reported_length_remaining(tvb, offset);
        hdcp_tvb = tvb_new_subset(tvb, offset, hdcp_len, hdcp_len);

        return call_dissector(hdcp_handle, hdcp_tvb, pinfo, hdmi_tree);
    }

    if (addr==ADDR8_EDID_WRITE) {
        col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "EDID request");
        proto_tree_add_item(hdmi_tree, hf_hdmi_edid_offset,
            tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;
        return offset;
    }

    return dissect_hdmi_edid(tvb, offset, pinfo, hdmi_tree);
}


void
proto_register_hdmi(void)
{
    static hf_register_info hf[] = {
        { &hf_hdmi_addr,
            { "8bit I2C address", "hdmi.addr", FT_UINT8, BASE_HEX,
                VALS(hdmi_addr), 0, NULL, HFILL } },
        { &hf_hdmi_edid_offset,
            { "Offset", "hdmi.edid.offset",
                FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
        { &hf_hdmi_edid_hdr,
            { "EDID header", "hdmi.edid.hdr",
                FT_UINT64, BASE_HEX, NULL, 0, NULL, HFILL } },
        { &hf_hdmi_edid_manf_id,
            { "Manufacturer ID", "hdmi.edid.manf_id",
                FT_STRING, STR_ASCII, NULL, 0, NULL, HFILL } },
        { &hf_hdmi_edid_manf_prod_code,
            { "Manufacturer product code", "hdmi.edid.manf_prod_code",
                FT_UINT16, BASE_HEX, NULL, 0, NULL, HFILL } },
        { &hf_hdmi_edid_manf_serial,
            { "Serial number", "hdmi.edid.serial_num",
                FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL } },
        { &hf_hdmi_edid_manf_week,
            { "Week of manufacture", "hdmi.edid.manf_week",
                FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL } },
        { &hf_hdmi_edid_mod_year,
            { "Model year", "hdmi.edid.model_year",
                FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL } },
        { &hf_hdmi_edid_manf_year,
            { "Year of manufacture", "hdmi.edid.manf_year",
                FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL } }
    };

    static gint *ett[] = {
        &ett_hdmi,
        &ett_hdmi_edid
    };

    proto_hdmi = proto_register_protocol(
            "High-Definition Multimedia Interface", "HDMI", "hdmi");

    proto_register_field_array(proto_hdmi, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    new_register_dissector("hdmi", dissect_hdmi, proto_hdmi);
}


void
proto_reg_handoff_hdmi(void)
{
    hdcp_handle = find_dissector("hdcp");
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */
