Skip to content

Instantly share code, notes, and snippets.

@andrew-aladev
Last active August 7, 2024 19:09
Show Gist options
  • Select an option

  • Save andrew-aladev/1d160bca305238b35d15 to your computer and use it in GitHub Desktop.

Select an option

Save andrew-aladev/1d160bca305238b35d15 to your computer and use it in GitHub Desktop.
Openwrt + Huawei E367 + balance of mts.by with bash and c
#include <stdint.h>
#include <stdio.h>
#include <string.h>
uint8_t get_data(char input, uint8_t *output) {
if (input - '0' >= 0 && '9' - input >= 0) {
*output = input - '0';
} else if (input - 'a' >= 0 && 'f' - input >= 0) {
*output = input - 'a' + 10;
} else if (input - 'A' >= 0 && 'F' - input >= 0) {
*output = input - 'A' + 10;
} else {
return 1;
}
return 0;
}
uint8_t get_data_pair(const char *input, uint8_t *output) {
uint8_t data;
if (get_data(*input, &data) != 0) {
return 1;
}
*output = data << 4;
if (get_data(*(input + 1), &data) != 0) {
return 2;
}
*output = *output | data;
return 0;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fputs("required argument: hex\n", stderr);
return 1;
}
char *hex = argv[1];
uint16_t data = 0;
uint8_t data_length = 0;
while (*hex != '\0') {
uint8_t new_data;
if (get_data_pair(hex, &new_data) != 0) {
fprintf(stderr, "invalid hex: bad pair %.2s\n", hex);
return 2;
}
hex += 2;
data = new_data << data_length | data;
data_length += 8;
while (data_length >= 7) {
putchar(data & 0x7f);
data = data >> 7;
data_length -= 7;
}
}
return 0;
}
# system type : Atheros AR9330 rev 1
# machine : GL.Inet GL-AR150
# cpu model : MIPS 24Kc V7.4
STAGING_DIR = openwrt-sdk-18.06.4-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/staging_dir
export STAGING_DIR
TOOLCHAIN_DIR = $(STAGING_DIR)/toolchain-mips_24kc_gcc-7.3.0_musl/bin
all:
$(TOOLCHAIN_DIR)/mips-openwrt-linux-gcc -Wall -Os -march=mips32r2 -mtune=24kc 7bit_hex_to_char.c -o 7bit_hex_to_char
$(TOOLCHAIN_DIR)/mips-openwrt-linux-strip 7bit_hex_to_char
#!/bin/sh
# Huawei E367 + mts.by
device="/dev/ttyUSB1"
command="AT+CUSD=1,AA180C3602,15\r"
tmp_file="/tmp/ussd_response.txt"
if ! echo "$1" | egrep -q "^[0-9]+$" ; then
>&2 echo "sleep time is required (first argument)"
exit 1
fi
sleep_time=$1
cp /dev/null "$tmp_file"
cat "$device" > "$tmp_file" &
bg_pid=$!
fault () {
kill $bg_pid
cp /dev/null "$tmp_file"
exit 2
}
trap "fault" SIGINT
quit () {
kill $bg_pid
cp /dev/null "$tmp_file"
}
trap "quit" EXIT
# at least 1 ussd response
ussd_responses=""
while [ -z "$ussd_responses" ]; do
echo -e "$command" > "$device"
sleep $sleep_time
# response will be like '+CUSD: 1,"C23..",15'
ussd_responses=$(cat /tmp/ussd_response.txt | sed -nr "\
s/\
\+CUSD:\
[[:space:]]*?[01]\
[[:space:]]*?,\
[[:space:]]*?\"\
([0-9a-f]+?)\"\
.*$\
/\1/gip\
")
done
echo -e "$ussd_responses" | xargs -n1 ./7bit_hex_to_char
cp /dev/null "$tmp_file"
@andrew-aladev
Copy link
Author

./ussd.sh 20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment