Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandoislas committed May 1, 2017
2 parents 9726244 + 679de46 commit 1b08652
Show file tree
Hide file tree
Showing 42 changed files with 847 additions and 476 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
__pycache__/
*.pyc
.idea
.idea/
.pypy/
build/
dist/
*.egg-info/
setup.cfg
temp/
.drc-sim/
.drc-sim/
region_dump.json
*.bin
.cache/
15 changes: 11 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
language: python
python:
- "2.7"
- "pypy"
- "3.4"
- "3.5"
- "3.6"
dist: trusty
sudo: required
install:
- pip install .
script: True
- sudo apt-get -qq update
- sudo ./install.sh local
- python setup.py install
before_script:
- export PYTHONPATH=$(pwd)
script: pytest
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ RUN apt-get update \
RUN apt-get update \
&& apt-get install -y \
wpasupplicant-drc \
python2.7 \
python2.7-dev \
python-pip \
python3 \
python3-dev \
python3-pip \
libffi-dev \
zlib1g-dev \
libjpeg-dev \
Expand All @@ -20,15 +20,15 @@ RUN apt-get update \
libswscale-dev \
rfkill \
isc-dhcp-client \
ifmetric \
python-tk
ifmetric

ADD drc*.py /root/
ADD setup.py /root/
ADD src/ /root/src/
ADD resources/ /root/resources/
ADD MANIFEST.in /root/
RUN cd /root/ && python setup.py install && rm -rf /root/*
RUN cd /root/ && python3 setup.py install

ENV TERM xterm
ENTRYPOINT ["drc-sim-backend.py"]
ENTRYPOINT ["drc-sim-backend.py", "--cli"]
CMD ["-h"]
139 changes: 99 additions & 40 deletions drc-info.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import codecs
import json
import select
import socket
import sys
import time
from threading import Thread

from src.server.data import constants
from src.server.data.struct import input, command

sock_cmd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_cmd.bind(("192.168.1.10", constants.PORT_WII_CMD))
Expand All @@ -16,61 +20,116 @@
sock_aud = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_aud.bind(("192.168.1.10", constants.PORT_WII_AUD))

json_dump = {}

def print_packet(s, name, do_print=True):
data = s.recv(2048)
if do_print:
print "%s: %s" % (name, data.encode('hex'))

def print_packet(sock, name):
data = sock.recv(2048)
print("%s: %s" % (name, codecs.encode(data, "hex").decode()))


def print_packet_cmd(sock):
data = sock.recv(2048)
print("cmd: %s" % codecs.encode(data, "hex").decode())
header = command.header.parse(data)
if header.packet_type != 2: # Only accept response packets
return
size = 8
if header.cmd_id == 1:
data_string = codecs.encode(data[size + command.header_cmd1.sizeof():], "hex").decode()
print("cmd 1: %s" % data_string)
json_dump["1"] = data_string
elif header.cmd_id == 0:
data_string = codecs.encode(data[size + command.header_cmd0.sizeof():], "hex").decode()
print("cmd 0 %d %d: %s" % (header.id_primary, header.id_secondary, data_string))
if "0" not in json_dump:
json_dump["0"] = {}
if str(header.id_primary) not in json_dump["0"]:
json_dump["0"][str(header.id_primary)] = {}
json_dump["0"][str(header.id_primary)][str(header.id_secondary)] = data_string


def send_cmd(data):
sock_cmd.sendto(data, ("192.168.1.11", constants.PORT_WII_CMD + 100))


def send_command_from_string(command_string, sid):
send_data = command.header.parse(codecs.decode(command_string, "hex"))
send_data.seq_id = sid
if send_data.cmd_id == 1:
send_data.mic_enabled = 0 # floods logs with audio data if enabled
send_data = command.header.build(send_data)
send_cmd(send_data)
sid += 1
sid %= 65535
time.sleep(1)
return sid


def cmd_request():
sid = 0
while True:
data = {
0: {0: {0: "000000000c00%s087e0115880040000000000000",
10: "000000000d00%s007e0101780040000a0000000100"},
4: {4: "000000000c00%s007e0109780040040400000000"},
5: {6: "000000000c00%s007e0101a80040050600000000",
12: "000000001100%s007e0102f80040050c000000050e0300870f",
24: "000000001600%s007e0101c8004005180000000a54313936333030303030"}
0: {0: {0: "000000000c0005087e0115880040000000000000", # TODO construct these
10: "000000000d0005007e0101780040000a0000000100"},
4: {4: "000000000c0005007e0109780040040400000000",
10: "000000000d0005117e012fc80040040a0000000100",
11: "000000000c0005017e0107180040040b00000000"},
5: {6: "000000000c0005007e0101a80040050600000000",
12: "00000000110005007e0102f80040050c000000050e0300870f",
16: "0000010030000580010000000000000000000000803e0000000100029e0000000000000070000000404003002d0000"
"018000400000000000", # FIXME 0 5 16 is a CMD 1 broadcast - missing actual request
24: "00000000160005007e0101c8004005180000000a54313936333030303030"}
},
1: {0: {0: "000001003000%s1a010000000000000000000000803e000000010002000000000000000070000000404003002d00000"
"10000000000000000" # Just CMD 1 - keys 0 0 are there so it fits nicely with the for loop
}
}
1: "000001003000051a010000000000000000000000803e000000010002000000000000000070000000404003002d00000"
"10000000000000000"
}
for command in data.keys():
for primary_id in data[command].keys():
for secondary_id in data[command][primary_id].keys():
h = hex(sid).replace("0x", "")
if len(h) == 1:
h = "0" + h
send_data = bytes((data[command][primary_id][secondary_id] % h).decode("hex"))
print "Sending command %d %d %d" % (command, primary_id, secondary_id)
send_cmd(send_data)
sid += 1
time.sleep(1)
for cmd in data.keys():
if isinstance(data[cmd], str):
print("Sending command %d" % cmd)
sid = send_command_from_string(data[cmd], sid)
else:
for primary_id in data[cmd].keys():
for secondary_id in data[cmd][primary_id].keys():
print("Sending command %d %d %d" % (cmd, primary_id, secondary_id))
sid = send_command_from_string(data[cmd][primary_id][secondary_id], sid)


def print_hid(sock):
data = sock.recv(2048)
input_parsed = input.input_data.parse(data)
print(input_parsed)


if __name__ == '__main__':
Thread(target=cmd_request).start()
hid = len(sys.argv) > 1 and sys.argv[1] == "--hid"

if not hid:
send_thread = Thread(target=cmd_request)
send_thread.daemon = True
send_thread.start()

while True:
rlist, wlist, xlist = select.select((sock_cmd, sock_msg, sock_hid, sock_vid, sock_aud), (), (), 1)
if rlist:
for s in rlist:
if s == sock_hid:
print_packet(s, "hid", False)
pass
elif s == sock_aud:
print_packet(s, "aud")
elif s == sock_vid:
print_packet(s, "vid")
elif s == sock_cmd:
print_packet(s, "cmd")
elif s == sock_msg:
print_packet(s, "msg")
try:
rlist, wlist, xlist = select.select((sock_cmd, sock_msg, sock_hid, sock_vid, sock_aud), (), (), 1)
if rlist:
for s in rlist:
if s == sock_hid and hid:
print_hid(s)
if hid:
continue
if s == sock_aud:
print_packet(s, "aud")
elif s == sock_vid:
print_packet(s, "vid")
elif s == sock_cmd:
print_packet_cmd(s)
elif s == sock_msg:
print_packet(s, "msg")
except KeyboardInterrupt:
if not hid:
dump = open("region_dump.json", "w")
dump.write(json.dumps(json_dump, indent=4))
dump.close()
print("Wrote dump to region_dump.json")
sys.exit(0)
22 changes: 19 additions & 3 deletions drc-sim-backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from src.server.util.logging.logger_wpa import LoggerWpa
from src.server.data.args import Args
from src.server.ui.cli.cli_main import CliMain
from src.server.ui.gui.gui_main import GuiMain
from src.server.util.logging.logger import Logger
from src.server.util.logging.logger_backend import LoggerBackend
from src.server.util.logging.logger_cli import LoggerCli
Expand All @@ -12,6 +11,10 @@


def init_loggers():
"""
Initialize loggers with a specified log level if they have the argument.
:return: None
"""
loggers = (Logger, LoggerBackend, LoggerGui, LoggerCli, LoggerWpa)
for logger in loggers:
if Args.args.debug:
Expand All @@ -27,26 +30,35 @@ def init_loggers():


def start():
"""
Main loop. It can be GUI or CLI based on args. Dies if an error makes it way here or main loop stops.
:return: None
"""
ui = None
try:
if Args.args.cli:
Logger.info("Enabling CLI")
ui = CliMain()
else:
Logger.info("Enabling GUI")
from src.server.ui.gui.gui_main import GuiMain
ui = GuiMain()
ui.start()
except KeyboardInterrupt:
if ui:
ui.stop()
except Exception, e:
except Exception as e:
if ui:
ui.stop()
Logger.throw(e)
Logger.exception(e)
Logger.info("Exiting")


def log_level():
"""
Log at every level to display the levels that are enabled.
:return: None
"""
# Logger info
Logger.debug("Debug logging enabled")
Logger.extra("Extra debug logging enabled")
Expand All @@ -57,6 +69,10 @@ def log_level():


def main():
"""
Main entry point. Parses arguments, loads configuration files, initialized loggers and starts the main loop.
:return: None
"""
Args.parse_args()
ConfigServer.load()
ConfigServer.save()
Expand Down
38 changes: 27 additions & 11 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ check_os() {
if command -v apt-get &> /dev/null; then
echo "Command apt-get found."
# Backend dependencies
dependencies=("python2.7" "python2.7-dev" "python-pip" "libffi-dev" "zlib1g-dev" "libjpeg-dev"
dependencies=("python3" "python3-dev" "python3-pip" "libffi-dev" "zlib1g-dev" "libjpeg-dev"
"net-tools" "wireless-tools" "sysvinit-utils" "psmisc" "libavcodec-dev" "libswscale-dev" "rfkill"
"isc-dhcp-client" "ifmetric" "python-tk" "gksu")
"isc-dhcp-client" "ifmetric" "python3-tk" "gksu")
# Wpa supplicant compile dependencies
dependencies+=("git" "libssl-dev" "libnl-genl-3-dev" "gcc" "make")
else
Expand Down Expand Up @@ -131,18 +131,29 @@ compile_wpa() {

# Installs drc-sim in a virtualenv
install_drc_sim() {
# Get repo
get_git ${REPO_DRC_SIM} "drc"
# Paths
drc_dir="${INSTALL_DIR}drc/"
cur_dir="${PWD}"
venv_dir="${INSTALL_DIR}venv_drc/"
# Get source
if [[ "${branch_drc_sim}" != "local" ]]; then
# Get repo
get_git ${REPO_DRC_SIM} "drc"
else
# Copy local
if [[ ! -d "${INSTALL_DIR}" ]]; then
mkdir "${INSTALL_DIR}" &> /dev/null || return 1
fi
rm -rf ${drc_dir} &> /dev/null || return 1
mkdir ${drc_dir} &> /dev/null || return 1
cp -R "${PWD}/." "${drc_dir%/*}" &> /dev/null || return 1
fi
# Install virtualenv
echo "Installing virtualenv"
python -m pip install virtualenv &> /dev/null || return 1
python3 -m pip install virtualenv &> /dev/null || return 1
# Create venv
echo "Creating virtualenv"
python -m virtualenv "${venv_dir}" &> /dev/null || return 1
python3 -m virtualenv -p python3 "${venv_dir}" &> /dev/null || return 1
# Activate venv
echo "Activating virtualenv"
source "${venv_dir}bin/activate" || return 1
Expand All @@ -152,12 +163,16 @@ install_drc_sim() {
# Set the directory
cd "${drc_dir}" &> /dev/null || return 1
# Branch to checkout
echo "Using branch \"${branch_drc_sim}\" for drc-sim install"
git checkout ${branch_drc_sim} &> /dev/null || return 1
if [[ "${branch_drc_sim}" != "local" ]]; then
echo "Using branch \"${branch_drc_sim}\" for drc-sim install"
git checkout ${branch_drc_sim} &> /dev/null || return 1
else
echo "Using current directory as install source"
fi
# Install
echo "Installing drc-sim"
echo "Downloading Python packages. This may take a while."
python setup.py install &> /dev/null || return 1
python3 setup.py install &> /dev/null || return 1
cd "${cur_dir}" &> /dev/null || return 1
}

Expand Down Expand Up @@ -216,14 +231,15 @@ check_args() {
echo " Defaults to install."
echo " Arguments:"
echo " -h, help : help menu"
echo " branch : branch to use for drc-sim (master or develop) master is used by default"
echo " branch : branch to use for drc-sim (master, develop, local) master is used by default"
echo " uninstall : uninstall DRC Sim"
exit 1
# Uninstall
elif [[ "${1}" == "uninstall" ]]; then
uninstall
# Install branch
elif [[ "${branch_drc_sim}" != "develop" ]] && [[ "${branch_drc_sim}" != "master" ]]; then
elif [[ "${branch_drc_sim}" != "develop" ]] && [[ "${branch_drc_sim}" != "master" ]] &&
[[ "${branch_drc_sim}" != "local" ]]; then
echo "Invalid branch \"${1}\""
check_args "help"
fi
Expand Down
Loading

0 comments on commit 1b08652

Please sign in to comment.