#!/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=254
BOARD_AP9117I_UPOE_PWR=289

DART_FILEPATH="/meraki_gpio/dart_connector"
WCPSTATE_FILEPATH="/tmp/wcpstate"

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

# $1 - wiredX
# $2 - ethX
create_port_mappings () {
    echo ${2} > ${DST_DIR}/wired${1}_port
    echo wired${1} > ${DST_DIR}/eth${2}
    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
            # 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

            # POE stuff (in Deciwatts)
            # Tweaked for resulting PSE=32.6W on 4-wires uPoE
            plat_set_upoe_required
            plat_set_upoe_power_max $BOARD_AP9117I_UPOE_PWR
            # Fallback: Tweaked for resulting PSE=30W on 2-wire PoE+
            plat_set_poe_max $BOARD_AP9117I_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
            # 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
            
            # POE stuff (in Deciwatts)
            # Tweaked for resulting PSE=32.6W on 4-wires uPoE
            plat_set_upoe_required
            plat_set_upoe_power_max $BOARD_AP9117I_UPOE_PWR
            # Fallback: Tweaked for resulting PSE=30W on 2-wire PoE+
            plat_set_poe_max $BOARD_AP9117I_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
            create_port_mappings 1 1
            # 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

            # POE stuff (in Deciwatts)
            # Tweaked for resulting PSE=32.6W on 4-wires uPoE
            plat_set_upoe_required
            plat_set_upoe_power_max $BOARD_AP9117I_UPOE_PWR
            # Fallback: Tweaked for resulting PSE=30W on 2-wire PoE+
            plat_set_poe_max $BOARD_AP9117I_PWR
            # 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
            ;;
        *)
            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_poe_max $val
fi
