/*
 * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All rights reserved.
 *
 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 */

module OSMUX_Types {

import from General_Types all;

external function enc_OSMUX_PDU ( in OSMUX_PDU msg ) return octetstring
        with { extension "prototype(convert) encode(RAW)" };
external function dec_OSMUX_PDU ( in octetstring msg ) return OSMUX_PDU
        with { extension "prototype(convert) decode(RAW)" };

type INT1 OsmuxCID	(0 .. 255);

type enumerated OsmuxFT {
  OSMUX_FT_LAPD,
  OSMUX_FT_AMR,
  OSMUX_FT_DUMMY
};

type record Osmux_AMR_header {
  BIT1                 marker,
  INT2b                ft,
  INT3b                ctr,
  BIT1                 amr_f,
  BIT1                 amr_q,
  INT1                 seq,
  OsmuxCID             cid,
  INT4b                amr_ft,
  INT4b                amr_cmr
} with {
  variant "FIELDORDER(msb)"
}

type record PDU_Osmux_AMR {
  Osmux_AMR_header     header,
  octetstring          data
} with {
  variant "FIELDORDER(msb)"
};

type record PDU_Osmux_DUMMY {
  Osmux_AMR_header     header,
  octetstring          data
} with {
  variant "FIELDORDER(msb)"
};

type record Osmux_session_par {
  integer               id optional,
  charstring            local_address optional,
  integer               local_port optional,
  charstring            dest_address optional,
  integer               dest_port optional
}

type record ASP_Osmux_Open_session {
  Osmux_session_par       session_id
}

type record ASP_Osmux_Open_session_result {
  Osmux_session_par       session_id
}

type record ASP_Osmux_Close_session {
  Osmux_session_par       session_id
}

type union OSMUX_PDU {
  PDU_Osmux_AMR               osmux_amr,
  PDU_Osmux_DUMMY             osmux_dummy
} with {
  variant "TAG (
    osmux_amr, header.ft = 1;
    osmux_dummy, header.ft = 2;
  )"
};


/* AMR voice frame type identifiers
 * See also 3GPP TS 26.101, Table 1a: Interpretation of Frame Type, Mode
 * Indication and Mode Request fields */
type enumerated AMRFT {
  AMR_FT_0,	/* 4.75 */
  AMR_FT_1,	/* 5.15 */
  AMR_FT_2,	/* 5.90 */
  AMR_FT_3,	/* 6.70 */
  AMR_FT_4,	/* 7.40 */
  AMR_FT_5,	/* 7.95 */
  AMR_FT_6,	/* 10.2 */
  AMR_FT_7,	/* 12.2 */
  AMR_FT_SID	/* SID */
};

/* AMR voice frame length (in bytes, rounded),
 * See also RFC 3267, chapter 3.6 */
const integer c_AMRFT_len[9] := {12, 13, 15, 17, 19, 20, 26, 31, 5};

function f_amrft_payload_len(INT4b amr_ft) return integer {
        return c_AMRFT_len[amr_ft];
}

} with { encode "RAW"}
