#pragma once

#include <stdbool.h>
#include <stdint.h>

#include <osmocom/core/linuxlist.h>
#include <osmocom/gsm/gsm48.h>
#include <osmocom/sigtran/sccp_sap.h>

#include <openbsc/common.h>

struct sgsn_pdp_ctx;
struct msgb;
struct gsm_auth_tuple;
struct osmo_sccp_addr;
struct osmo_ss7_instance;

struct RANAP_RAB_SetupOrModifiedItemIEs_s;
struct RANAP_GlobalRNC_ID;
struct RANAP_Cause;

/* Debugging switches from asn1c and osmo-iuh */
extern int asn_debug;
extern int asn1_xer_print;

struct ue_conn_ctx {
	struct llist_head list;
	/* TODO: It's not needed to store the full SCCP address for each
	 * UE.  Rather than that, a pointer to the RNC should be far
	 * sufficient */
	struct osmo_sccp_addr sccp_addr;
	uint32_t conn_id;
	int integrity_active;
	struct gprs_ra_id ra_id;
	enum nsap_addr_enc rab_assign_addr_enc;
};

enum iu_event_type {
	IU_EVENT_RAB_ASSIGN,
	IU_EVENT_SECURITY_MODE_COMPLETE,
	IU_EVENT_IU_RELEASE, /* An actual Iu Release message was received */
	IU_EVENT_LINK_INVALIDATED, /* A SUA link was lost or closed down */
};

extern const struct value_string iu_event_type_names[];
static inline const char *iu_event_type_str(enum iu_event_type e)
{
	return get_value_string(iu_event_type_names, e);
}

/* Implementations of iu_recv_cb_t shall find the ue_conn_ctx in msg->dst. */
typedef int (* iu_recv_cb_t )(struct msgb *msg, struct gprs_ra_id *ra_id,
			      uint16_t *sai);

typedef int (* iu_event_cb_t )(struct ue_conn_ctx *ue_ctx,
			       enum iu_event_type type, void *data);

typedef int (* iu_rab_ass_resp_cb_t )(struct ue_conn_ctx *ue_ctx, uint8_t rab_id,
		struct RANAP_RAB_SetupOrModifiedItemIEs_s *setup_ies);

int iu_init(void *ctx, struct osmo_sccp_instance *sccp,
	    iu_recv_cb_t iu_recv_cb, iu_event_cb_t iu_event_cb);

int iu_tx(struct msgb *msg, uint8_t sapi);

int iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac);
int iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac);

int iu_rab_act(struct ue_conn_ctx *ue_ctx, struct msgb *msg);
int iu_rab_deact(struct ue_conn_ctx *ue_ctx, uint8_t rab_id);
int iu_tx_sec_mode_cmd(struct ue_conn_ctx *uectx, struct gsm_auth_tuple *tp,
		       int send_ck, int new_key);
int iu_tx_common_id(struct ue_conn_ctx *ue_ctx, const char *imsi);
int iu_tx_release(struct ue_conn_ctx *ctx, const struct RANAP_Cause *cause);

void iu_vty_init(int iu_parent_node, enum nsap_addr_enc *rab_assign_addr_enc);
int iu_vty_config_write(struct vty *vty, const char *indent);

struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sccp_addr *addr, uint32_t conn_id);
