################################################################################
# Copyright (c) 2020-2024, National Research Foundation (SARAO)
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy
# of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
"""Common SPEAD-related constants and helper function."""
from typing import Any
import numpy as np
import spead2
#: SPEAD ID of ``digitiser_id`` item
DIGITISER_ID_ID = 0x3101
#: SPEAD ID of ``digitiser_status`` item
DIGITISER_STATUS_ID = 0x3102
#: SPEAD ID of ``feng_id`` item
FENG_ID_ID = 0x4101
#: SPEAD ID of ``feng_raw`` item
FENG_RAW_ID = 0x4300
#: SPEAD ID of ``frequency`` item (which is actually a channel number).
FREQUENCY_ID = 0x4103
#: SPEAD ID of ``adc_samples`` item (digitiser data)
ADC_SAMPLES_ID = 0x3300 # Digitiser data
#: SPEAD ID of ``xeng_raw`` item
XENG_RAW_ID = 0x1800
#: SPEAD ID of ``timestamp`` item (number of ADC samples since the sync epoch)
TIMESTAMP_ID = 0x1600
#: SPEAD ID of ``bf_raw`` item
BF_RAW_ID = 0x5000
#: SPEAD ID of ``beam_ants`` item
BEAM_ANTS_ID = 0x5004
#: Maximum packet size to send (only used for descriptors)
MAX_PACKET_SIZE = 8872
#: Bit position in digitiser_status SPEAD item for ADC saturation flag
DIGITISER_STATUS_SATURATION_FLAG_BIT = 1
#: First bit position in digitiser status SPEAD item for ADC saturation count
DIGITISER_STATUS_SATURATION_COUNT_SHIFT = 32
#: SPEAD flavour used for all send streams
FLAVOUR = spead2.Flavour(4, 64, 48, 0)
#: Default UDP port
DEFAULT_PORT = 7148
#: Format for immediate items
IMMEDIATE_FORMAT = [("u", FLAVOUR.heap_address_bits)]
#: dtype for items that need to be immediate yet passed by reference
IMMEDIATE_DTYPE = np.dtype(">u8")