Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Openwrt + Huawei E3372 + balance of velcom.by with bash and c
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <wchar.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 print_data_group(const char *input) {
wchar_t data_group = 0;
uint8_t data;
for (uint8_t index = 0; index < 4; index++) {
if (get_data(*(input + index), &data) != 0) {
return 1;
}
data_group = data_group << 4 | data;
}
putwchar(data_group);
return 0;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fputs("required argument: hex\n", stderr);
return 1;
}
char *hex = argv[1];
setlocale(LC_CTYPE, "");
for (size_t index = 0; index + 4 <= strlen(hex); index += 4) {
if (print_data_group(hex + index) != 0) {
fprintf(stderr, "invalid hex: %s\n", hex);
return 2;
}
}
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 hex_to_utf16be.c -o hex_to_utf16be
$(TOOLCHAIN_DIR)/mips-openwrt-linux-strip hex_to_utf16be
#!/bin/sh
# Huawei E3372 + velcom.by
device="/dev/ttyUSB2"
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 ./hex_to_utf16be
cp /dev/null "$tmp_file"
@andrew-aladev

This comment has been minimized.

Copy link
Owner Author

@andrew-aladev andrew-aladev commented Jul 23, 2019

./ussd.sh 20

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