xref: /openwifi/user_space/wgd.sh (revision 2349a78387b7736b14bd0166346f157167603958)
1#!/bin/bash
2
3# Author: Xianjun Jiao
4# SPDX-FileCopyrightText: 2022 UGent
5# SPDX-License-Identifier: AGPL-3.0-or-later
6
7print_usage () {
8  echo "usage:"
9  echo "  Script for load (or download+load) different driver and FPGA img without rebooting"
10  echo "  no  argument: Load .ko driver files and FPGA img (if system_top.bit.bin exist) in current dir with test_mode=0."
11  echo "  1st argument: If it is a NUMBER, it will be assigned to test_mode. Then load everything from current dir."
12  echo "  1st argument: If it is a string called \"remote\", it will download driver/FPGA and load everything."
13  echo "  - 2nd argument (if exist) is the target directory name for downloading and reloading"
14  echo "  - 3rd argument (if exist) is the value for test_mode"
15  echo "  1st argument: neither NUMBER nor \"remote\" nor a .tar.gz file, it is regarded as a directory and load everything from it."
16  echo "  - 2nd argument (if exist) is the value for test_mode"
17  echo "  1st argument: a .tar.gz file, it will be unpacked then load from that unpacked directory"
18  echo "  - 2nd argument (if exist) is the value for test_mode"
19  echo " "
20}
21
22checkModule () {
23  MODULE_input="$1"
24  if lsmod | grep "$MODULE_input" &> /dev/null ; then
25    echo "$MODULE_input is loaded!"
26    return 0
27  else
28    echo "$MODULE_input is not loaded!"
29    return 1
30  fi
31}
32
33download_module () {
34  MODULE_input="$1"
35  TARGET_DIR_input="$2"
36  mkdir -p $TARGET_DIR_input
37  if [ "$MODULE_input" == "fpga" ]; then
38    wget -O $TARGET_DIR_input/system_top.bit.bin ftp://192.168.10.1/user_space/system_top.bit.bin
39  else
40    if [ "$MODULE_input" == "sdr" ]; then
41      wget -O $TARGET_DIR_input/$MODULE_input.ko ftp://192.168.10.1/driver/$MODULE_input.ko
42    else
43      wget -O $TARGET_DIR_input/$MODULE_input.ko ftp://192.168.10.1/driver/$MODULE_input/$MODULE_input.ko
44    fi
45  fi
46  sync
47}
48
49insert_check_module () {
50  TARGET_DIR_input="$1"
51  MODULE_input="$2"
52  rmmod $MODULE_input
53  if [[ -n $3 ]]; then
54    (set -x; insmod $TARGET_DIR_input/$MODULE_input.ko test_mode=$3)
55  else
56    (set -x; insmod $TARGET_DIR_input/$MODULE_input.ko)
57  fi
58
59  checkModule $MODULE_input
60  if [ $? -eq 1 ]; then
61    exit 1
62  fi
63}
64
65print_usage
66
67insmod ad9361_drv.ko
68insmod xilinx_dma.ko
69# modprobe ad9361_drv
70# modprobe xilinx_dma
71modprobe mac80211
72lsmod
73
74TARGET_DIR=./
75DOWNLOAD_FLAG=0
76test_mode=0
77
78if [[ -n $1 ]]; then
79  re='^[0-9]+$'
80  if ! [[ $1 =~ $re ]] ; then # not a number
81    if [ "$1" == "remote" ]; then
82      DOWNLOAD_FLAG=1
83      if [[ -n $2 ]]; then
84        TARGET_DIR=$2
85      fi
86      if [[ -n $3 ]]; then
87        test_mode=$3
88      fi
89    else
90      if [[ "$1" == *".tar.gz"* ]]; then
91	set -x
92        tar_gz_filename=$1
93        TARGET_DIR=${tar_gz_filename%".tar.gz"}
94        mkdir -p $TARGET_DIR
95        rm -rf $TARGET_DIR/*
96        tar -zxvf $1 -C $TARGET_DIR
97        find $TARGET_DIR/ -name \*.ko -exec cp {} $TARGET_DIR/ \;
98        find $TARGET_DIR/ -name \*.bit.bin -exec cp {} $TARGET_DIR/ \;
99	set +x
100      else
101        TARGET_DIR=$1
102      fi
103      if [[ -n $2 ]]; then
104        test_mode=$2
105      fi
106    fi
107  else # is a number
108    test_mode=$1
109  fi
110fi
111
112echo TARGET_DIR $TARGET_DIR
113echo DOWNLOAD_FLAG $DOWNLOAD_FLAG
114echo test_mode $test_mode
115
116#if ((($test_mode & 0x2) != 0)); then
117  tx_offset_tuning_enable=0
118#else
119#  tx_offset_tuning_enable=1
120#fi
121
122echo tx_offset_tuning_enable $tx_offset_tuning_enable
123
124if [ -d "$TARGET_DIR" ]; then
125  echo "\$TARGET_DIR is found!"
126else
127  if [ $DOWNLOAD_FLAG -eq 0 ]; then
128    echo "\$TARGET_DIR is not correct. Please check!"
129    exit 1
130  fi
131fi
132
133echo " "
134
135killall hostapd
136service dhcpcd stop #dhcp client. it will get secondary ip for sdr0 which causes trouble
137killall dhcpd
138killall wpa_supplicant
139#service network-manager stop
140ifconfig sdr0 down
141
142rmmod sdr
143
144if [ $DOWNLOAD_FLAG -eq 1 ]; then
145  download_module fpga $TARGET_DIR
146fi
147
148if [ -f "$TARGET_DIR/system_top.bit.bin" ]; then
149  ./load_fpga_img.sh $TARGET_DIR/system_top.bit.bin
150else
151  echo $TARGET_DIR/system_top.bit.bin not found. Skip reloading FPGA.
152  # ./load_fpga_img.sh fjdo349ujtrueugjhj
153fi
154
155./rf_init_11n.sh
156
157MODULE_ALL="tx_intf rx_intf openofdm_tx openofdm_rx xpu sdr"
158for MODULE in $MODULE_ALL
159do
160  if [ $DOWNLOAD_FLAG -eq 1 ]; then
161      download_module $MODULE $TARGET_DIR
162  fi
163  if [ "$MODULE" == "sdr" ]; then
164    insert_check_module $TARGET_DIR $MODULE $test_mode
165  else
166    insert_check_module $TARGET_DIR $MODULE
167  fi
168done
169
170# [ -e /tmp/check_calib_inf.pid ] && kill -0 $(</tmp/check_calib_inf.pid)
171# ./check_calib_inf.sh
172
173echo the end
174# dmesg
175# lsmod
176