/*!
 * \internal
 * \brief Session supplement callback on an incoming INVITE request
 *
 * If we are receiving an initial INVITE, then we will set the session's identity
 * based on the INVITE or configured endpoint values. If we are receiving a reinvite,
 * then we will potentially queue a connected line update via the \ref update_incoming_connected_line
 * function
 *
 * \param session The session that has received an INVITE
 * \param rdata The incoming INVITE
 */
static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
{
	if (!session->channel) {
		/*
		 * Since we have no channel this must be the initial inbound
		 * INVITE.  Set the session ID directly because the channel
		 * has not been created yet.
		 */
		if (session->endpoint->id.trust_inbound
			&& (!set_id_from_pai(rdata, &session->id)
				|| !set_id_from_rpid(rdata, &session->id))) {
			ast_free(session->id.tag);
			session->id.tag = ast_strdup(session->endpoint->id.self.tag);
			return 0;
		}
		ast_party_id_copy(&session->id, &session->endpoint->id.self);
		if (!session->endpoint->id.self.number.valid) {
			set_id_from_from(rdata, &session->id);
		}
	} else {
		/*
		 * ReINVITE or UPDATE.  Check for changes to the ID and queue
		 * a connected line update if necessary.
		 */
		update_incoming_connected_line(session, rdata);
	}
	return 0;
}
/*!
 * \internal
 * \brief Session supplement callback on an incoming INVITE request
 *
 * If we are receiving an initial INVITE, then we will set the session's identity
 * based on the INVITE or configured endpoint values. If we are receiving a reinvite,
 * then we will potentially queue a connected line update via the \ref update_incoming_connected_line
 * function
 *
 * \param session The session that has received an INVITE
 * \param rdata The incoming INVITE
 */
static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
{
	if (session->inv_session->state < PJSIP_INV_STATE_CONFIRMED) {
		/*
		 * Initial inbound INVITE.  Set the session ID directly
		 * because the channel has not been created yet.
		 */
		if (session->endpoint->id.trust_inbound
			&& (!set_id_from_pai(rdata, &session->id)
				|| !set_id_from_rpid(rdata, &session->id))) {
			ast_free(session->id.tag);
			session->id.tag = ast_strdup(session->endpoint->id.self.tag);
			return 0;
		}
		ast_party_id_copy(&session->id, &session->endpoint->id.self);
		if (!session->endpoint->id.self.number.valid) {
			set_id_from_from(rdata, &session->id);
		}
	} else if (session->channel) {
		/* Reinvite. Check for changes to the ID and queue a connected line
		 * update if necessary
		 */
		update_incoming_connected_line(session, rdata);
	}
	return 0;
}
/*!
 * \internal
 * \brief Make updates to connected line information based on an incoming request.
 *
 * This will get identity information from an incoming request. Once the identification is
 * retrieved, we will check if the new information warrants a connected line update and queue
 * a connected line update if so.
 *
 * \param session The session on which we received an incoming request
 * \param rdata The incoming request
 */
static void update_incoming_connected_line(struct ast_sip_session *session, pjsip_rx_data *rdata)
{
	struct ast_party_id id;

	if (!session->endpoint->id.trust_inbound) {
		return;
	}

	ast_party_id_init(&id);
	if (!set_id_from_pai(rdata, &id) || !set_id_from_rpid(rdata, &id)) {
		if (should_queue_connected_line_update(session, &id)) {
			queue_connected_line_update(session, &id);
		}
	}
	ast_party_id_free(&id);
}