#!/bin/sh

# Startup script for the LPC1114 I/O Processor

# Copyright (C)2013-2025, Philip Munts, President, Munts AM Corp.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# Work-around for ldconfig race condition in dpkg
ldconfig

###############################################################################

# Import configuration settings

. /usr/local/etc/expansion_lpc1114.config

###############################################################################

# Configure GPIO (for reset, interrupt, etc.)

chown root:gpio /dev/gpiochip0
chmod 660 /dev/gpiochip0

###############################################################################

# Configure serial port device (only used for updating the LPC1114 firmware)

if [ -n "${LPC1114_SERIAL:=/dev/nonexistentSERIAL}" -a -c ${LPC1114_SERIAL} ]; then
  chown root:gpio ${LPC1114_SERIAL}
  chmod 660 ${LPC1114_SERIAL}
fi

###############################################################################

# Configure I2C bus device

if [ -n "${LPC1114_I2C:=/dev/nonexistentI2C}" -a -c ${LPC1114_I2C} ]; then
  chown root:gpio ${LPC1114_I2C}
  chmod 660 ${LPC1114_I2C}
fi

###############################################################################

# Configure SPI device

if [ -n "${LPC1114_SPI:=/dev/nonexistentSPI}" -a -c ${LPC1114_SPI} ]; then
  chown root:gpio ${LPC1114_SPI}
  chmod 660 ${LPC1114_SPI}
fi

###############################################################################

# Reset the LPC1114 microcontroller, if possible

if [ -n "${LPC1114_RESET}" ]; then
  /usr/local/libexec/expansion_lpc1114_reset
fi

###############################################################################

# Define a function for adding iptables rules -- Parameters are protocol and
# name

InsertFilterRule()
{
  if [ "${OSNAME}" = "MuntsOS" ]; then
    iptables -A INPUT -p $1 -m conntrack --ctstate NEW --dport $2 -j ACCEPT
  fi
}

###############################################################################

# Start the SPI agent HTTP server (if available)

if [ "$LPC1114_HTTP" = "yes" -a -x /usr/local/libexec/spi_agent_http_server ]; then
  /usr/local/libexec/spi_agent_http_server
  InsertFilterRule tcp spiagent-http
fi

# Start the SPI agent LabView LINX server (if available)

if [ "$LPC1114_LINX" = "yes" -a -x /usr/local/libexec/spi_agent_linx_server ]; then
  /usr/local/libexec/spi_agent_linx_server
  InsertFilterRule tcp spiagent-linx
fi

# Start the SPI agent ONC/RPC server (if available)

if [ "$LPC1114_ONCRPC" = "yes" -a -x /usr/local/libexec/spi_agent_oncrpc_server ]; then
  nc -z localhost 111
  if [ $? -ne 0 ]; then
    rpcbind
    InsertFilterRule tcp sunrpc
    InsertFilterRule udp sunrpc
  fi

  /usr/local/libexec/spi_agent_oncrpc_server

  InsertFilterRule tcp `rpcinfo -p localhost | grep tcp | awk '/ 10000 / { print $4 }'`
  InsertFilterRule udp `rpcinfo -p localhost | grep udp | awk '/ 10000 / { print $4 }'`
fi

# Start the SPI agent Remote I/O server (if available)

if [ "$LPC1114_REMOTEIO" = "yes" -a -x /usr/local/libexec/spi_agent_remoteio_server ]; then
  /usr/local/libexec/spi_agent_remoteio_server
fi

# Start the SPI agent XML-RPC server (if available)

if [ "$LPC1114_XMLRPC" = "yes" -a -x /usr/local/libexec/spi_agent_xmlrpc_server ]; then
  /usr/local/libexec/spi_agent_xmlrpc_server
  InsertFilterRule tcp spiagent-xmlrpc
fi

# Start the SPI agent ZeroMQ server (if available)

if [ "$LPC1114_ZEROMQ" = "yes" -a -x /usr/local/libexec/spi_agent_zeromq_server ]; then
  /usr/local/libexec/spi_agent_zeromq_server
  InsertFilterRule tcp spiagent-zeromq
fi
