summaryrefslogtreecommitdiff
path: root/recipes-bsp/multitech/get-eeprom-device-config/get-eeprom-device-config.sh
blob: ab47d310b7f31b2aeaa7b2578782445343f04e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
# Radio ID loop count
TRY_COUNT=30

CONFIG_PATH=/var/run/config

#system capabilities
CAPABILITY_ADC="false"
CAPABILITY_BLUETOOTH="false"
CAPABILITY_DIN="false"
CAPABILITY_DOUT="false"
#CAPABILITY_EXTERNAL_SERIAL_PORT="false"
#CAPABILITY_FLEXIBLE_SERIAL_PORT="false"
CAPABILITY_GPS="false"
CAPABILITY_WIFI="false"
CAPABILITY_CELL="false"
CAPABILITY_LORA="false"
CAPABILITY_GPIO="false"
CAPABILITY_SERIAL="false"
CAPABILITY_RS232="false"
CAPABILITY_RS422="false"
CAPABILITY_RS485="false"
CAPABILITY_NODE_RED="false"
CAPABILITY_LORA_NETWORK_SERVER="false"


# accessory cards constans
TEMP_AC_PATH="/var/volatile/tmp"
TEMP_AC_JSON="$TEMP_AC_PATH/ac_data.json"
# Number of accessory card places - size of array
AC_CARD_COUNT=4

function usage {
	echo 'get-eeprom-device-info [-t TRY_COUNT]'
        echo '  Where TRY_COUNT is the number of half'
        echo '  second waits for the cellular radio'
        echo '  to be ready for identification'
        exit 1
}

# Reads accessory cards data
# WARNING: Make sure function called after CAPABILITIY_* variables is set with initial values.
ReadAcData() {
  # Doing the same as mts-io-sysfs, but with one step, without unnesesary parsing.
  MTS_IO_DIR="/sys/devices/platform/mts-io"
  AC_PRFIX="ap"
  TEMP_AC_FILE_PREFIX="/card"
  TEMP_AC_PREFIX="$TEMP_AC_PATH$TEMP_AC_FILE_PREFIX"
  # Fill property data for each accessory card
  rm -f $TEMP_AC_PREFIX*
  for f in `find $MTS_IO_DIR/$AC_PRFIX* -follow -maxdepth 1 -type f -follow -maxdepth 1 -type f 2>/dev/null`;
  do
    FILENAME=${f##*$MTS_IO_DIR/$AC_PRFIX}
    case $FILENAME in
      modalias )
        ;;
      power )
        ;;
      subsystem* )
        ;;
      uevent )
        ;;
      * )
        AC_NUMBER=$(echo "$FILENAME" | cut -d "/" -f1)
        AC_PROP=$(echo "$FILENAME" | cut -d "/" -f2)
        # Do camel casing
        # Because {sed -r '{s/-([a-z])/\U\1/g;}'} did not work for Not GNU sed 4.0 use awk + tr
        # Removing next 2 lines gives about 50% function speed-up
        AC_PROP=$( echo $AC_PROP | awk -F"-" '{for(j=1;j<=NF;j++){$j=toupper(substr($j,1,1))substr($j,2)}}1' | sed -r '{s/ //g;}' )
        AC_PROP=`echo ${AC_PROP:0:1} | tr '[A-Z]' '[a-z]'`${AC_PROP:1}
        #Output to temp file
        AC_FILE="$TEMP_AC_PREFIX$AC_NUMBER"
        printf "\"$AC_PROP\" : \"%s\",\n" `cat $f` >> $AC_FILE
        ;;
    esac
  done
  # Compose accessory cards data to array
  echo "\"accessoryCards\" : [ " > $TEMP_AC_JSON
  for ((I=1; I<=$AC_CARD_COUNT; ++I)) ;
  do
    AC_FILE="$TEMP_AC_PREFIX$I"
    if [ -f "$AC_FILE" ];then
      sed -i '$s/,$//' $AC_FILE # remove last comma
      echo "{" >> $TEMP_AC_JSON
      cat $AC_FILE >> $TEMP_AC_JSON
      echo "}," >> $TEMP_AC_JSON

      # Map product-id with capabilities.
      if $(grep -q '"productId" : "MTAC-MFSER-DTE"' $AC_FILE) || $(grep -q '"productId" : "MTAC-MFSER-DCE"' $AC_FILE); then
        CAPABILITY_SERIAL="true"
        CAPABILITY_RS232="true"
        CAPABILITY_RS422="true"
        CAPABILITY_RS485="true"
      elif $(grep -q '"productId" : "MTAC-GPIOB"' $AC_FILE) || $(grep -q '"productId" : "MTAC-GPIOI"' $AC_FILE); then
        CAPABILITY_GPIO="true"
        CAPABILITY_ADC="true"
        CAPABILITY_DIN="true"
        CAPABILITY_DOUT="true"
      elif $(grep -q '"productId" : "MTAC-LORA-.*"' $AC_FILE); then
        CAPABILITY_LORA="true"
      fi
    else
      echo "null," >> $TEMP_AC_JSON
    fi
  done
  sed -i '$s/,$//' $TEMP_AC_JSON # remove last comma
  echo "]," >> $TEMP_AC_JSON
}

#check if LORA is present onboard a device.
#Lora capability from an MTAC card is determined by ReadAcData()
isNativeLoraPresent() {
  LORA_ID_PREFIX="MTCAP-LORA-"
  LORA_ID=$(mts-io-sysfs show lora/product-id 2> /dev/null)
  if [[ $LORA_ID == $LORA_ID_PREFIX* ]]; then
    return 0
  else
    return 1
  fi
}

TEMP=$(getopt -o t: -n 'get-eeprom-device-info' -- "$@")
eval set -- "$TEMP"
# extract options and their arguments into variables.
while true ; do
    case "$1" in
        -t)
            case "$2" in
                "") usage ;;
                *) TRY_COUNT=$2 ; shift 2 ;;
            esac ;;
        --) shift ; break ;;
        *) echo "usage" ;;
    esac
done

# Read device information from eeprom device

mts-id-eeprom --in-file /sys/bus/i2c/devices/i2c-0/0-0056/eeprom > ${CONFIG_PATH}/device_info

DEVICE_CAPA=${CONFIG_PATH}/device_capa
DEVICE_INFO_JSON=${CONFIG_PATH}/device_info.json

# Create json data files
echo "{" > $DEVICE_CAPA
echo "{" > ${CONFIG_PATH}/device_info.json


# Read each line and parse the device data
while read line
do

if [[ $line =~ ^vendor-id:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/vendor_id
    echo \"vendorId\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^product-id:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/device_type
    echo \"productId\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^device-id:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/device_id
    echo \"deviceId\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^hw-version:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/hw_version
    echo \"hardwareVersion\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^mac-addr:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/mac_addr
    echo \"macAddress\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^mac-bluetooth:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/mac_bluetooth
    echo \"macBluetooth\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^mac-wifi:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/mac_wifi
    echo \"macWifi\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^uuid:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/uuid
    echo \"uuid\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ ^imei:\ \"(.*)\" ]]; then
    echo ${BASH_REMATCH[1]} > ${CONFIG_PATH}/modem_imei
    echo \"imei\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ capa-gps:\ (.*) ]]; then
    CAPABILITY_GPS=$(echo ${BASH_REMATCH[1]})
elif [[ $line =~ capa-din:\ (.*) ]]; then
    CAPABILITY_DIN=$(echo ${BASH_REMATCH[1]})
elif [[ $line =~ capa-dout:\ (.*) ]]; then
    CAPABILITY_DOUT=$(echo ${BASH_REMATCH[1]})
elif [[ $line =~ capa-adc:\ (.*) ]]; then
    CAPABILITY_ADC=$(echo ${BASH_REMATCH[1]})
elif [[ $line =~ capa-wifi:\ (.*) ]]; then
    CAPABILITY_WIFI=$(echo ${BASH_REMATCH[1]})
elif [[ $line =~ capa-bluetooth:\ (.*) ]]; then
    CAPABILITY_BLUETOOTH=$(echo ${BASH_REMATCH[1]})
elif [[ $line =~ capa:\ \"(.*)\" ]]; then
    echo \"mask\": \"${BASH_REMATCH[1]}\", >> $DEVICE_CAPA
fi

done <${CONFIG_PATH}/device_info

HASRADIO=$(cat /sys/devices/platform/mts-io/has-radio)                               
if ((HASRADIO == 1)) ; then
  echo "Finding cellular module..."
  for ((i=0; i < TRY_COUNT; i++))
    do
      # Check cellular module
      echo "Try #"$i
      if [[ -L "/dev/modem_at0" && -L "/dev/modem_at1" ]]; then
        CAPABILITY_CELL="true"
        break
      fi
      if ((TRY_COUNT > 1)) ; then
        sleep 0.5
      fi
    done
fi

if isNativeLoraPresent; then
  CAPABILITY_LORA="true"
fi

#
# Check if NODE-RED and Lora Network Server packages are installed
#
OPKG_FILE=/tmp/opkg_inst.tmp
opkg list-installed > $OPKG_FILE

cat $OPKG_FILE | grep node-red > /dev/null
[ $? -eq 0 ] && CAPABILITY_NODE_RED="true"

cat $OPKG_FILE | grep lora-network-server > /dev/null
[ $? -eq 0 ] && CAPABILITY_LORA_NETWORK_SERVER="true"

rm -f $OPKG_FILE


# Read firmware info from /etc/issue
cat /etc/issue |
(
while read line
do

if [[ $line =~ Version:\ (.*) ]]; then
    echo \"firmware\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
elif [[ $line =~ Date:\ (.*) ]]; then
    echo \"firmwareDate\": \"${BASH_REMATCH[1]}\", >> $DEVICE_INFO_JSON
fi

done
)
# Add accessory cards
echo "Reading accessory cards data"
ReadAcData
echo "Adding accessory cards data"
cat $TEMP_AC_JSON >> $DEVICE_INFO_JSON

# Complete the json files
echo \"adc\": $CAPABILITY_ADC, >> $DEVICE_CAPA
echo \"bluetooth\": $CAPABILITY_BLUETOOTH, >> $DEVICE_CAPA
echo \"din\": $CAPABILITY_DIN, >> $DEVICE_CAPA
echo \"dout\": $CAPABILITY_DOUT, >> $DEVICE_CAPA
echo \"gps\": $CAPABILITY_GPS, >> $DEVICE_CAPA
echo \"wifi\": $CAPABILITY_WIFI, >> $DEVICE_CAPA
echo \"cell\": $CAPABILITY_CELL, >> $DEVICE_CAPA
echo \"lora\": $CAPABILITY_LORA, >> $DEVICE_CAPA
echo \"gpio\": $CAPABILITY_GPIO, >> $DEVICE_CAPA
echo \"serial\": $CAPABILITY_SERIAL, >> $DEVICE_CAPA
echo \"rs232\": $CAPABILITY_RS232, >> $DEVICE_CAPA
echo \"rs422\": $CAPABILITY_RS422, >> $DEVICE_CAPA
echo \"rs485\": $CAPABILITY_RS485, >> $DEVICE_CAPA
echo \"nodeRed\": $CAPABILITY_NODE_RED, >> $DEVICE_CAPA
echo \"loraNetworkServer\": $CAPABILITY_LORA_NETWORK_SERVER >> $DEVICE_CAPA
echo "}" >> $DEVICE_CAPA
echo \"capabilities\": >> $DEVICE_INFO_JSON
cat $DEVICE_CAPA >> $DEVICE_INFO_JSON
echo "}" >> $DEVICE_INFO_JSON

# Clean up temp files
rm $DEVICE_CAPA
rm ${CONFIG_PATH}/device_info

#echo device_info.json ----------
#cat /var/run/config/device_info.json
#echo ---------------------------