Beaglebone Configuration

#############
Problem cross ref
#############
NOHUP not working: see dropbear config
configure: error: cannot run C compiled programs
[solution:set LD_LIBRARY_PATH before configure]
configure: error: can't find -l<somelib>
[solution: 
- make sure that environment is correct (e.g. source src/scripts/gwconf b) 
- see that lib is installed (/usr/lib, /usr/local/lib (should have .a and .la).
- in bitbake image recipe (), may need to include <pkg>-dev and <pkg>-staticdev variants to get .a and .la
]

#############
14 nov 2013 procedure update
#############
2GB base SD card image
- removed data partition  (would just replace anyway)
- added gwsetup script and an xfoce distro to /home/root
- made new SD card distro 
	projects/foce/xfoce/distros/xfoce_0.25-mmc2GB-base-14nov13.img

- TODO: put these into the oebb build

Disk setup procedure
sudo dd if=~/host/projects/foce/xfoce/distros/xfoce_0.25-mmc2GB-base-14nov13.img of=/dev/sdd bs=1M

#############
users and groups
#############
adduser ops
addgroup ops

moduser -aG sudo ops
moduser -aG users ops
moduser -aG lock ops
moduser -aG uucp ops
moduser -aG dialout ops
moduser -aG operator ops
moduser -aG tty ops
OR
add ops to groups (/etc/group)
tty:x:5:xuser,ops
uucp:x:10:ops
sudo:x:27:ops
operator:x:37:ops
users:x:100:ops
lock:x:999:ops
dialout:x:20:ops

#############
/etc/hostname
#############
as root 
hostname <hostname>
echo <hostname> > /etc/hostname
reboot

#############
/etc/sodoers
visudo
uncomment sudo group line (allows sudo group members)

#############
/mnt/data
#############
sudo mkdir /mnt/xfoce
sudo mount /dev/mmcblk0p3 /mnt/data
# add to /etc/fstab
/dev/mmcblk0p3   /mnt/xfoce   auto   defaults  0  0

sudo chown ops /mnt/xfoce
sudo group users /mnt/xfoce

#############
ops .profile
#############

if [ -f ~/.xfenv ]
then    
. ~/.xfenv
fi

#############
ops .bashrc
#############
#!/bin/bash
[]]
# environment for xFOCE build
export LDFLAGS="-L/usr/lib -L/usr/local/lib" 
export LIBS="-lpthread -lrt -lzmq -lm -lmodbus -lzmq -lczmq -luuid -lcrypto"
export CPP_FLAGS="-I/usr/include/modbus"
export CFLAGS="-I/usr/include/modbus"

#############
ops .xfenv
#############

#!/bin/bash

XF_HOME=/mnt/data/build/xfoce-0.21

if [ ! -z ${1} ]; then
        XF_HOME=${1}
fi

export XF_TBIN=$XF_HOME/src/gateway/test
export XF_GBIN=$XF_HOME/src/gateway/apps
export XF_BBIN=$XF_HOME/src/bridge/apps
export XF_SBIN=$XF_HOME/src/scripts
export XF_PATH=/home/ops:$XF_TBIN:$XF_GBIN:$XF_BBIN:$XF_SBIN
echo $PATH | grep $XF_PATH
test=$?
if [ "$test" -ne 0 ]; then
         export PATH=${PATH}:$XF_PATH
fi

if [ ! -e ~/xfoce ]
then
        ln -s ${XF_HOME} ~/xfoce
fi

echo XF_HOME $XF_HOME

#############
xfoce/src/scripts/gwconf
#############
Note: source gwconf before ./configure, make

#!/bin/bash

if [ $# -lt 1 ]
then
echo
echo "usage: . gwconf [ub]"
echo
return
fi

if [ "$1" == "u" ]
then
# ubuntu 
export LDFLAGS="-L/usr/lib/i386-linux-gnu -L/usr/local/lib" 
export LIBS="-lpthread -lrt -lzmq -lczmq -luuid -lm -lmodbus -lcrypto" 
export CPPFLAGS="-I/usr/local/include/modbus -I/usr/include"
export CFLAGS="-O0 -g" 
export CXXFLAGS="-O0 -g" 
#export CFLAGS="-O0 -g -D_ZMALLOC_DEBUG" 
#export CXXFLAGS="-O0 -g -D_ZMALLOC_DEBUG" 
fi
if [ "$1" == "b" ]
then
# beaglebone
export LDFLAGS="-L/usr/lib -L/usr/local/lib" 
export LIBS="-lpthread -lrt -lzmq -lczmq -luuid -lm -lmodbus  -lcrypto"
export CPP_FLAGS="-I/usr/include/modbus -I/usr/local/include"
export CFLAGS="-I/usr/include/modbus -I/usr/local/include"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib
fi
#./configure LDFLAGS="-L/usr/lib -L/usr/local/lib" LIBS="-lpthread -lrt -lzmq -lczmq -luuid -lm -lmodbus" CPP_FLAGS="-I/usr/include/modbus" CFLAGS="-I/usr/include/modbus"

#############
xfoce
#############
sudo mkdir -p /mnt/data/swfoce/conf
sudo mkdir -p /mnt/data/swfoce/arch
sudo mkdir -p /mnt/data/swfoce/logs

install swfoce/gateway.conf

#############
socat  
##############
socat may used to create virtual serial ports for serial-ethernet converters like Digi and XPort. socat connects a socket on the converter to a file on the local file system.

example
socat pty,link=/dev/pty0,user=ops,min=0,time=0,nonblock=1,clocal=0,ignoreeof tcp4:xfoce-sp1:2001

To ensure that the ports will available automatically, we can install socat calls as services that start automatically at boot time and restart if the connections break. To do this, we use systemd, the linux service configuration architecture (which replaces SystemV init).

Since we need several instances of a similar service, we'll use the systemd unit template feature. This enables us to create one service unit (file), and use it to make services by simply passing in different configuration files. 

Simple service unit files have names like foo.service (they must use the .service suffix); unit templates are specially named, using the "@" character after the service name, e.g., foo@.service. Unit template instances (the actual services we start with the template) have names like foo@bar.service. 

We need only create the unit template that describes how the service should be run, and the configuration files that describe instance-specific arguments (for example, socatd host, port and terminal settings). It is convenient to name the  instance configuration files with the instance name (e.g. socatd-pty0) so that they may be accessed canonically by the template. We will also create a wrapper script to call socat. 

So, the necessary pieces are:
- unit service template file (socatd@.service)
- unit instance configuration file(s) (e.g. socatd-pty0)
- socatd wrapper script (socatd.sh)

All of these files are in xfoce/src/scripts/.

To install as systemd service, 
- put socatd@.service (unit template) in /etc/systemd/system/
- put socatd-pty0 in /etc/sysconfig
- make sure that socatd.sh has 755 permissions, and make sure that the path matches in socat@.service.
- install the template
	sudo systemctl daemon-reload
	sudo systemctl enable socat@.service

The template is configured to start the service at boot time, and restart a service instance if it fails. If something goes wrong and you need to disable the service, use
	sudo systemctl disable socat@.service
to keep the service from running or loading at boot time

The following manual operations may be useful:
- start a service instance using
	sudo systemctl start socat@pty0.service
- stop a service instance using
	sudo systemctl stop socat@pty0.service
- get service instance status using
	sudo systemctl status socat@pty0.service

Service output is sent to the system log (journal). Use journalctl for debugging.

#############
socat: xfoce/src/scripts/socatd@.service
##############
/etc/systemd/system/socatd@.service

systemd service unit template definition
sudo systemctl enable socatd@.service
sudo systemctl start socatd@pty0.service
sudo systemctl stop socatd@pty0.service

#############
socat: xfoce/src/scripts/socatd-pty0
##############
/etc/sysconfig/socatd-pty0
systemd configuation for socpty-pty0 instance of socpty service template
Defines command line options for service instance (e.g. socpty@pty0.service)
Each template instance should have it's own

#############
socat: xfoce/src/scripts/socatd.sh
##############
systemd start/stop script for socatd instances
called by socatd@.service
may also be called from command line

#############
dropbear SSH configuration.  
##############
By default, the SSH daemon (dropbear) on the BeagleBones are configured to kill all user procesess as soon as the user logs off.  This makes it difficult to leave processes running in the background for debugging.  To change this, edit the dropbear@.service file in /lib/systemd and add the "KillMode=process" line:

root@beaglebone:/lib/systemd/system# cat dropbear\@.service
[Unit]
Description=SSH Per-Connection Server
Requires=dropbearkey.service
After=syslog.target dropbearkey.service

[Service]
ExecStart=-/usr/sbin/dropbear -i -r /etc/dropbear/dropbear_rsa_host_key -p 22
ExecReload=/bin/kill -HUP $MAINPID
StandardInput=socket
KillMode=process

You can either reboot the system for these changes to take effect, or reload the systemctl config and restart the dropbear service.