#!/bin/sh
#------------------------------------------------------------------
#
# generate_platforms - parsing and file generation
#
# Avoid runtime platform/board/SKU/PID check like "if (Salt-Spring)"
# as much as possible.  Instead, create files here for things you
# need that are diffent between boards
#
# Copyright (c) 2018 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------
#set -x

. /usr/bin/parse_cmdline.sh
. /usr/bin/platformfunc.sh
. /usr/bin/modulefunc.sh
. /usr/cisco/bin/gpio.sh

DST_DIR=/var/platform

BOARD_AP9117I_PWR=248
BOARD_AP9117I_UPOE_PWR=227
BOARD_AP9130_PWR=254            # Base/default 30w without USB
BOARD_AP9130_UPOE_PWR=243       # upoe+ 4.5w(39) =33.3w
BOARD_AP9140_PWR=254
BOARD_AP9140_UPOE_PWR=231       # Base/default without USB and EM

USB4DOT5W_PWR=39                # 4.5 W
BOARD_AP9140_EM_OFFSET_PWR=25
DART_FILEPATH="/meraki_gpio/dart_connector"
WCPSTATE_FILEPATH="/tmp/wcpstate"
usb_power_level="`plat_get_usb_admin_state`"

# $1 - Error message
error_action () {
    echo "WARNING: $1"
    echo "WARNING: $1" > ${DST_DIR}/error_log
}

# $1 - wiredX
# $2 - ethX
# $3 - physical port
create_port_mappings () {
    echo ${2} > ${DST_DIR}/wired${1}_port
    echo wired${1} > ${DST_DIR}/eth${2}
    if [ ! -z $3 ]; then
        plat_set_port_id $1 $3
    fi
    if [ ! -f ${DST_DIR}/wired${1} ]; then
        echo eth${2} > ${DST_DIR}/wired${1}
    fi
}

# $1 - Board id from bootloader
gen_board_info() {
    local val=$1
    local board_id=$((val >> 1))
    local antenna=$((val & 1))
    echo $board_id > ${DST_DIR}/board_id
    case $board_id in
        0|1)
            echo HG > ${DST_DIR}/board_name
            # Ethernet stuff
            echo 1 > ${DST_DIR}/max_num_if
            create_port_mappings 0 0 6
            # Radio or wifi stuff
            echo 2 > ${DST_DIR}/max_radios
            echo 5G > ${DST_DIR}/radio1
            echo 2G > ${DST_DIR}/radio0
            # BLE stuff
            echo 2640R2 > ${DST_DIR}/ble_chip
            # TBD: GPIO stuff

            # No cleanair/sensord for HG
            rm -f /usr/sbin/sensord /opt/cisco/bin/cleanaird

            # USB module power
            plat_set_usb_small_max_power $USB4DOT5W_PWR
            # TODO: USB enable by default for now.
            plat_set_usb_power_level $usb_power_level

            # POE stuff (in Deciwatts)
            plat_set_default_poe_max $BOARD_AP9117I_PWR
            plat_set_default_upoe_power_max $BOARD_AP9117I_UPOE_PWR

            echo internal > ${DST_DIR}/antenna
            ;;
        7)
            echo Axel > ${DST_DIR}/board_name
            # Ethernet stuff
            echo 1 > ${DST_DIR}/max_num_if
            create_port_mappings 0 0 6
            # Radio or wifi stuff
            echo 3 > ${DST_DIR}/max_radios
            echo 5G > ${DST_DIR}/radio2
            echo 5G > ${DST_DIR}/radio1
            echo 2G > ${DST_DIR}/radio0

            # RHL stuff
            touch ${DST_DIR}/has_rhl
            echo 19 > ${DST_DIR}/rhb_reset_gpio

            # BLE stuff
            echo 2652R1 > ${DST_DIR}/ble_chip
            # TBD: GPIO stuff
            
            # USB module power 4.5w
            plat_set_usb_small_max_power $USB4DOT5W_PWR
            # TODO: USB enable by default for now.
            plat_set_usb_power_level $usb_power_level

            # POE stuff (in Deciwatts)
            plat_set_default_poe_max $BOARD_AP9130_PWR
            plat_set_default_upoe_power_max $BOARD_AP9130_UPOE_PWR

            if [ $antenna -eq 0 ]; then
                echo external > ${DST_DIR}/antenna
                setup_gpio "59" "in"  "dart_connector"
                setup_gpio "57" "in"  "dart_legacy_connector"
                touch ${DST_DIR}/has_dart
                touch ${DST_DIR}/dart_antenna_only
                ln -s ${DART_FILEPATH} ${WCPSTATE_FILEPATH}
                touch ${DST_DIR}/is_sia_capable
            else
                echo internal > ${DST_DIR}/antenna
                # no self-identify antenna on Axel-I
                rm -f /lib/modules/marlin_device.ko
                rm -f /lib/modules/marlin_driver.ko
            fi
            ;;
        5)
            echo Baffin > ${DST_DIR}/board_name
            # Ethernet stuff
            echo 2 > ${DST_DIR}/max_num_if
            create_port_mappings 0 0 6
            create_port_mappings 1 1 5
            # Radio or wifi stuff
            echo 3 > ${DST_DIR}/max_radios
            echo 5G > ${DST_DIR}/radio2
            echo 5G > ${DST_DIR}/radio1
            echo 2G > ${DST_DIR}/radio0

            # HALO/AoA
            echo 3 > ${DST_DIR}/halo_radio_id

            # RHL stuff
            touch ${DST_DIR}/has_rhl
            echo 19 > ${DST_DIR}/rhb_reset_gpio

            # BLE stuff
            echo 2652R1 > ${DST_DIR}/ble_chip
            # TBD: GPIO stuff

            # POE stuff (in Deciwatts)
            plat_set_default_poe_max $BOARD_AP9140_PWR
            plat_set_default_upoe_power_max $BOARD_AP9140_UPOE_PWR

            # Expansion module power
            plat_set_em_small_max_power $BOARD_AP9140_EM_OFFSET_PWR

            # USB module power
            plat_set_usb_small_max_power $USB4DOT5W_PWR
            # TODO: USB enable by default for now.
            #       Assume small 4.5W max-power USB module for now.
            #       Will add support for large 9W USB module later
            plat_set_usb_power_level $usb_power_level

            # File for LAG support
            touch ${DST_DIR}/lag_support

            # Protos still have no ACT2 programmed.
            # Remove when all boards have ACT2 programmed
            echo "WARNING: ACT2 check ignored!!! MUST BE REMOVED IN PRODUCTION"
            touch ${DST_DIR}/proto_noact2_prog
            if [ $antenna -eq 0 ]; then
                echo internal > ${DST_DIR}/antenna
            else
                echo external > ${DST_DIR}/antenna
            fi
            # no self-identify antenna on Baffin
            rm -f /lib/modules/marlin_device.ko
            rm -f /lib/modules/marlin_driver.ko

            # support Antenna ID capability
            touch ${DST_DIR}/antenna_id_capability
            ;;
        *)
            echo UNKNOWN > ${DST_DIR}/board_name
            error_action "Unknown board id"
    esac

    touch ${DST_DIR}/has_usb
}

mkdir -p $DST_DIR
rm -f $DST_DIR/*

val="`cmdline_get_board_id`"
if [ ! -z "$val" ]; then
    gen_board_info $val
else
    # default to Haida Gwaii board type
    gen_board_info 0
fi

val="`cmdline_get_detected_expanssion_module`"
if [ ! -z "$val" ]; then
    echo $val > ${DST_DIR}/detected_em
fi

val="`cmdline_get_em0_interface`"
if [ ! -z "$val" ]; then
    echo "eth${val}" > ${DST_DIR}/em0
fi

val="`cmdline_get_poe_max`"
if [ ! -z "$val" ]; then
    # Possible overide
    plat_set_default_poe_max $val
fi
