Skip to content

Commit

Permalink
[NETWORK_SWITCH] Merge pull request #264 from pioneers/network_switch…
Browse files Browse the repository at this point in the history
…_update

[NETWORK_SWITCH] Made much more robust for final day of final comp
  • Loading branch information
benliao1 authored Apr 30, 2023
2 parents d956812 + 891bdbc commit 4b24cee
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ lowcar/Device
# ignore the entire net_handler/pbc_gen folder
net_handler/pbc_gen

# ignore network_switch/exit_status.txt
network_switch/exit_status.txt

# Log files
*.log

Expand Down
4 changes: 2 additions & 2 deletions lowcar/devices/KoalaBear/KoalaBear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ uint16_t torque_read_data;
//******************************* KOALABEAR CONSTANTS AND PARAMS ****************************//

// ----------------------> CHANGE THIS TO IMPLEMENT MAX SPEED FOR KARTS <----------------------
#define MAX_DUTY_CYCLE 0.75 // maximum duty cycle to cap how fast the motors can go: range (0, 1]
#define MAX_DUTY_CYCLE 1.0 // maximum duty cycle to cap how fast the motors can go: range (0, 1]
// --------------------------------------------------------------------------------------------

// --------------------> CHANGE THIS TO IMPLEMENT ACCELERATION FOR KARTS <---------------------
#define ACCEL 0.7 // acceleration of motors, in duty cycle units per second squared
#define ACCEL 2.0 // acceleration of motors, in duty cycle units per second squared
// --------------------------------------------------------------------------------------------

// default values for PID controllers; PID control is explained in the Wiki!
Expand Down
69 changes: 59 additions & 10 deletions network_switch/network_switch.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "../logger/logger.h"
#include "../runtime_util/runtime_util.h"
#include "../shm_wrapper/shm_wrapper.h"


#define SLEEP_DELAY 1
#define INITIAL_SWITCH 2

// Retrieves router name and password from the file name: router_txt
void get_router_name_password(char* router_name, char* router_password, char* router_txt) {
Expand All @@ -22,16 +25,32 @@ int main() {
shm_init();
chdir("../network_switch");
log_printf(INFO, "NETWORK_SWITCH initialized.");
bool prev_switch = false;
int prev_switch = 2;

char bash_exe[] = "/usr/bin/bash";
char script_name[] = "network_switch.sh";

char local_router[32];
char local_password[32];
char *local_args[5];
char team_router[] = "teamrouter.txt";
get_router_name_password(local_router, local_password, team_router);
local_args[0] = bash_exe;
local_args[1] = script_name;
local_args[2] = local_router;
local_args[3] = local_password;
local_args[4] = NULL;

char pie_router[32];
char pie_password[32];
char *pie_args[5];
char pie_router_file[] = "pierouter.txt";
get_router_name_password(pie_router, pie_password, pie_router_file);
pie_args[0] = bash_exe;
pie_args[1] = script_name;
pie_args[2] = pie_router;
pie_args[3] = pie_password;
pie_args[4] = NULL;

while (1) {
dev_id_t dev_ids[MAX_DEVICES];
Expand All @@ -58,19 +77,49 @@ int main() {
device_t* device = get_device(dev_ids[idx].type);
param_val_t param_data[MAX_PARAMS];
device_read(idx, NET_HANDLER, DATA, get_readable_param_bitmap(device->type), param_data);
bool curr_switch = param_data[device->num_params - 1].p_b; // network switch value is the last parameter
if (curr_switch != prev_switch) {
if (!curr_switch) { // switch to pioneers if curr_switch is false
snprintf(total_command, sizeof(total_command), "./network_switch.sh %s %s", pie_router, pie_password);
system(total_command);
bool curr_switch_bool = param_data[device->num_params - 1].p_b; // network switch value is the last parameter
int curr_switch;
if (curr_switch_bool) {
curr_switch = 1;
} else if (!curr_switch_bool) {
curr_switch = 0;
}
while (curr_switch != prev_switch) {
int status;
if (curr_switch == 0) { // switch to pioneers if curr_switch is false
pid_t pid;
if ((pid = fork()) < 0) {
log_printf(ERROR, "network_switch: Failed to fork");
sleep(SLEEP_DELAY);
} else if (pid == 0) {
execv(bash_exe, pie_args); // call the bash script with pioneers router arguments
} else {
waitpid(pid, &status, 0);
}
} else { // switch to student's router if curr_switch is true
snprintf(total_command, sizeof(total_command), "./network_switch.sh %s %s", local_router, local_password);
system(total_command);
pid_t pid;
if ((pid = fork()) < 0) {
log_printf(ERROR, "network_switch: Failed to fork");
sleep(SLEEP_DELAY);
} else if (pid == 0) {
execv(bash_exe, local_args); // call the bash script with local router arguments
} else {
waitpid(pid, &status, 0);
}
}
char buf[5];
FILE* exit_status = fopen("exit_status.txt", "r");
fgets(buf, 5, exit_status); // retrieve the output of the bash script in the exit_status.txt
if (strcmp(buf, "1") == 0) { // if output is 1, we successfully connected. Set previous switch = current switch
log_printf(WARN, "SUCCESSFULLY CONNECTED TO NETWORK");
prev_switch = curr_switch;
} else { // if output is not 1, we loop again to call the bash script again
log_printf(WARN, "FAILED TO CONNECT TO NETWORK");
}
prev_switch = curr_switch;
fclose(exit_status);
}
}
}
sleep(SLEEP_DELAY);
}
}
}
60 changes: 55 additions & 5 deletions network_switch/network_switch.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
#!/bin/bash

# move into folder with all the network names the potato connected to previously
cd /etc/NetworkManager/system-connections
sudo rm * # remove all network names
sudo nmcli d wifi connect $1 password $2 # connect to wifi determined by network switch
# script takes two arguments, the first being the name of the network to connect to,
# the second being the password to that network

# keep trying to connect until wifi is connected, check by looping through output of nmcli d
# script returns 1 if successfully connected, 0 if unsuccessful

# forget all networks by removing all network configuration files in nmcli folders
sudo rm /etc/NetworkManager/system-connections/*

# initiate rescan of networks; this command only starts the scan, it doesn't wait for scan to complete
sudo nmcli dev wifi rescan

# wait 3 seconds, look for the network name in the output of nmcli d wifi list
# repeat a maximum of 3 times (arbitrarily chosen nmumbers)
pass=0
for i in {1..3}; do
# if passed, break early
if [[ $pass == 1 ]]; then
break;
fi

sleep 3
printf "Try number $i ...\n"

while read line; do
if [[ $line == *"$1"* ]]; then
pass=1
break
fi
done <<< "$(nmcli d wifi list)"
done

printf "Going to connect!\n"

# connect to the network; again do not proceed before action is complete
sudo nmcli d wifi connect $1 password $2

# we use the command nmcli -t -f CONNECTION,STATE device to get the state of NetworkManager
# we look for a line that is exactly <network_name>:connected
# for example, if successfully connect to pioneers, the output will contain:
# pioneers:connected
pass=0
while read line; do
if [[ $line == "$1:connected" ]]; then
pass=1
break
fi
done <<< "$(nmcli -t -f CONNECTION,STATE device)"

# write "1" or "0" to exit_status.txt to give the C program the result of this script
if [[ $pass == 1 ]]; then
printf "1" > exit_status.txt
else
printf "0" > exit_status.txt
fi

exit $pass

0 comments on commit 4b24cee

Please sign in to comment.