xref: /openwifi/user_space/wgd.sh (revision a7346801b61f871223d4bb5a6f4f47e920df7b4a)
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
67TARGET_DIR=./
68DOWNLOAD_FLAG=0
69test_mode=0
70
71if [[ -n $1 ]]; then
72  re='^[0-9]+$'
73  if ! [[ $1 =~ $re ]] ; then # not a number
74    if [ "$1" == "remote" ]; then
75      DOWNLOAD_FLAG=1
76      if [[ -n $2 ]]; then
77        TARGET_DIR=$2
78      fi
79      if [[ -n $3 ]]; then
80        test_mode=$3
81      fi
82    else
83      if [[ "$1" == *".tar.gz"* ]]; then
84	set -x
85        tar_gz_filename=$1
86        TARGET_DIR=${tar_gz_filename%".tar.gz"}
87        mkdir -p $TARGET_DIR
88        rm -rf $TARGET_DIR/*
89        tar -zxvf $1 -C $TARGET_DIR
90        find $TARGET_DIR/ -name \*.ko -exec cp {} $TARGET_DIR/ \;
91        find $TARGET_DIR/ -name \*.bit.bin -exec cp {} $TARGET_DIR/ \;
92	set +x
93      else
94        TARGET_DIR=$1
95      fi
96      if [[ -n $2 ]]; then
97        test_mode=$2
98      fi
99    fi
100  else # is a number
101    test_mode=$1
102  fi
103fi
104
105echo TARGET_DIR $TARGET_DIR
106echo DOWNLOAD_FLAG $DOWNLOAD_FLAG
107echo test_mode $test_mode
108
109#if ((($test_mode & 0x2) != 0)); then
110  tx_offset_tuning_enable=0
111#else
112#  tx_offset_tuning_enable=1
113#fi
114
115echo tx_offset_tuning_enable $tx_offset_tuning_enable
116
117if [ -d "$TARGET_DIR" ]; then
118  echo "\$TARGET_DIR is found!"
119else
120  if [ $DOWNLOAD_FLAG -eq 0 ]; then
121    echo "\$TARGET_DIR is not correct. Please check!"
122    exit 1
123  fi
124fi
125
126echo " "
127
128killall hostapd
129service dhcpcd stop #dhcp client. it will get secondary ip for sdr0 which causes trouble
130killall dhcpd
131killall wpa_supplicant
132#service network-manager stop
133ifconfig sdr0 down
134
135rmmod sdr
136# insert_check_module ./ ad9361_drv
137
138if [ $DOWNLOAD_FLAG -eq 1 ]; then
139  download_module fpga $TARGET_DIR
140fi
141
142if [ -f "$TARGET_DIR/system_top.bit.bin" ]; then
143  ./load_fpga_img.sh $TARGET_DIR/system_top.bit.bin
144else
145  echo $TARGET_DIR/system_top.bit.bin not found. Skip reloading FPGA.
146  ./load_fpga_img.sh fjdo349ujtrueugjhj
147fi
148
149./rf_init_11n.sh
150# insert_check_module ./ xilinx_dma
151
152depmod
153modprobe mac80211
154lsmod
155
156MODULE_ALL="tx_intf rx_intf openofdm_tx openofdm_rx xpu sdr"
157for MODULE in $MODULE_ALL
158do
159  if [ $DOWNLOAD_FLAG -eq 1 ]; then
160      download_module $MODULE $TARGET_DIR
161  fi
162  if [ "$MODULE" == "sdr" ]; then
163    insert_check_module $TARGET_DIR $MODULE $test_mode
164  else
165    insert_check_module $TARGET_DIR $MODULE
166  fi
167done
168
169# [ -e /tmp/check_calib_inf.pid ] && kill -0 $(</tmp/check_calib_inf.pid)
170# ./check_calib_inf.sh
171
172echo the end
173# dmesg
174# lsmod
175