5c0c61f1a5ceff63839c9305bbaae9035a70e941
[matches/MCTX3420.git] / server / run.sh
1 #!/bin/bash
2 # Use this to quickly test run the server in valgrind
3 #spawn-fcgi -p9005 -n ./valgrind.sh
4 # Use this to run the server normally
5 #./stream &
6
7 if [[ "$(uname -m)" != *arm*  ]]; then
8         echo Not running on the BBB
9         # Use this to quickly test run the server in valgrind
10         spawn-fcgi -p9005 -n ./valgrind.sh
11         # Use this to run the server normally
12         #./stream &
13         exit 0
14 fi
15
16 # Check running as root
17 if [ "$(whoami)" != "root" ]; then
18         (echo "Run $0 as root.") 1>&2
19         exit 1
20 fi
21
22 # Check existence of program
23 if [ ! -e "server" ]; then
24         (echo "Rebuild server.") 1>&2;
25         exit 1
26 fi
27
28 # Identify cape-manager slots
29 slot=$(echo /sys/devices/bone_capemgr.*/slots | awk '{print $1}')
30 pwm=/sys/class/pwm/
31
32 # Load PWM module
33 if [ $(cat $slot | grep am33xx_pwm -c) -gt 0 ]; then 
34         echo am33xx_pwm already loaded, not loading again; 
35 else 
36         echo Enabling PWM driver am33xx_pwm
37         echo am33xx_pwm > $slot
38 fi;
39
40 # Safe pins that won't interfere with one another
41 #ports=(P9_22 P9_42 P9_16 P8_13 P9_28);
42 #portnumbers=(0 2 4 6 7);
43 #Full correspondence from pwm0-pwm7
44 ports=(P9_22 P9_21 P9_42 P9_14 P9_16 P8_19 P8_13 P9_28);
45 portnumbers=(0 1 2 3 4 5 6 7);
46 # Enable PWM pins
47 # They must be exported at this stage, before the device tree
48 # overlay for that pin is enabled.
49 for ((c=0; c < ${#ports[*]}; c++)); do
50         if [ ! -d $pwm/pwm${portnumbers[$c]} ]; then
51                 echo Exporting PWM ${portnumbers[$c]} \(${ports[$c]}\)
52                 echo ${portnumbers[$c]} > $pwm/export
53         else
54                 echo PWM ${portnumbers[$c]} already enabled
55         fi;
56                 
57         if [ $(cat $slot | grep ${ports[$c]} -c) -gt 0 ]; then
58                 echo PWM pin ${ports[$c]} already enabled, not enabling again
59         else
60                 (echo bone_pwm_${ports[$c]} > $slot) 1>&2 >> /dev/null
61         fi;
62 done;
63
64 # Load ADCs
65 if [ $(cat $slot | grep BB-ADC -c) -gt 0 ]; then 
66         echo BB-ADC already loaded, not loading again; 
67 else 
68         echo Enabling BB-ADC
69         (echo BB-ADC > $slot) 1>&2 >> /dev/null
70 fi;
71
72 # Find adc_device_path
73 # NOTE: This has to be passed as a parameter, because it is not always the same. For some unfathomable reason. Hooray.
74 #adc_device_path=$(dirname $(find /sys -name *AIN0))
75
76
77 # Run the program with parameters
78 # TODO: Can tell spawn-fcgi to run the program as an unprivelaged user?
79 # But first will have to work out how to set PWM/GPIO as unprivelaged user
80 fails=0
81 while [ $fails -lt 10 ]; do
82         spawn-fcgi -p9005 -n -- ./server #-a "$adc_device_path"
83         if [ "$?" == "0" ]; then
84                 exit 0
85         fi
86         fails=$(( $fails + 1 ))
87         (echo "Restarting server after Fatal Error #$fails") 1>&2
88         
89 done
90 (echo "Server had too many Fatal Errors ($fails)") 1>&2
91 exit $fails\0\0\0\0\0\0

UCC git Repository :: git.ucc.asn.au