Socket coding in Android

So I’ve recently been playing about with Android for ‘Not So Super Secret Startup Idea’ and needed some code to communicate data such as Phone Calls/Text messages and other things from my phone to another device using TCP/IP. This is a bit harder on Android then it is on a PC as Android is bassed on apps having something called Activities and Services.
An Activity is the part of the Application which handles user input and can be destroyed at any time by the OS. This makes it pretty much useless for running anything which will communicate with the outside world without user intervention.
The other part part of an Android app is called a service which can be used to perform tasks in the background, process events from Activities, etc.
So what I currently have is an Android app which has a Service to handle TCP/IP communications from a telnet client on a PC. The service will also send data such as GPS location, phone calls and text messages (currently work in progress) over the socket to the PC.
One problem with the default TCP/IP functions is that they block (do not return until the right amount of data is received). I choose to use threads to handle blocking I/O and post Intents (a form of IPC) to the main activity to update it’s UI.
There is however one problem with using Threads in that when the Service is stopped there is no clean way to Kill a thread. This can be gotten around by closing the socket and setting a variable inside the threads main loop to tell it that it needs to shutdown cleanly.
So here’s some quick and dirty code which output’s GPS location and the phone no. of any calls received to a socket:

GPSTest

Going Underground

IMG_4463
A few months ago I saw that the London Transport measeum where offering tours of Awdych tube station so I managed to book me and S a couple of tickets before they sold out.
Awdych is one of the many abandoned tube stations on the London Underground network and which you don’t often get the chance to visit.
The tour started at the booking hall where we were told about the stations history and ultimate downfall. We then descended the 120 stairs down to the bottom of the lift shaft. We then looked around the lift shafts before proceeding to the platform level and the first of the platforms at the station.

IMG_4464

IMG_4465

We also saw the second platform which was used as a storage area for actifacts during the war, had a look at the tunnels beyond the station (to protect against trains missing the station from derailing). We then finally saw some of the unfineshed tunnels from the lift to the platform.

More pictures are on Siobhán’s blog.

RS485 networking

A couple of years ago I built the first version of my tube time display which shows the time of the next train at my local Underground Station.  This was connected to a Raspberry PI via a USB connection.  I found the Raspberry PI not the most reliable piece of hardware in the world and it was also a bit of an over kill for a simple display so I’ve been working on a new version of my display that uses RS485 to communicate with a computer which can be a long way away from it.

The original tube time display thing.
The original tube time display thing.

For this I decided on a token bus type network where each device sends out a token to the next device telling it that it can use the communication channel. This allows me to attach other AVR/Arduino based devices to the same piece of cable (I’m using CAT5 ethernet cable). I also have one device on the network acting as a superviser which will create tokens for a device if the token is given to a device that doesn’t exist.

So far I’ve managed to send text between two AVR32U4 boards over the bus so it’s going quite well so far. I think things can be further optimised so the bus is faster but it’s certainly good enough for my tube time display.

IMG_4440

Terraria

2014-05-24_00001I recently bought Terraria on Steam when it was on offer.  It’s best described as a 2D Minecraft alike where you have to survive on a hostile world.  I’m still very much learning how to play it and have got as far as creating a small house for me and my adviser.  We can now survive the night without be killed by zombies or flying eye things.

I’ll post more updates as I learn more and expand my home!

Anatomy of a bicycle chain

image

I changed the chain on my bicycle awhile ago and decided to keep the old one and recycle it.  I haven’t decided what or if I’m going to us it for yet but was curious about the wear on its components.  So I used a chain too to remove 3 of the links so I could completely disassemble one of the links.
Once I disassembled it I could see that the main component that show wear is the connecting pins.  They have quit obvious grooves which is probably the main thing the cause train stretch.  Surprisingly I couldn’t see any obvious wear on the two bearings.  I also saw some wear on the silver coloured connecting links.
It was also quite interesting how the rear sprocket had worn.  It looks like some of the teeth have actually bent out of shape.

image

Day off to visit the NPL (Plus picture of me!).

image.php
Wearing stompy goth boots and leggings for my visit to the NPL!

So I had a day off yesterday to visit the National Physics Laboratory for there open house event.  Me and S saw lots of interesting exhibits including seeing a house fly under an electron microscope which was amazing.  You could see the compound eye in amazing detail.  I also got the chance to use a ultrasound machine to plastic stick men in gel which was a lot harder then it looks and lot of fun.

Unfortunately my stompy Goth boots got the better of me and my feet were aching after several hours so me and S retreated to a nearby pub for food and then got the train home.

 

Space engineers

I recently bought Space Engineers on steam when it was discounted. It’s pretty much minecraft in space the object is to build ships and spacestations. Like Minecraft it has a survival mode and creative mode. In survival mode you have to mine asteroids for minerals which you can smelt down and assemble into components needed to build things and run nuclear reactors to generate power.
It’s currently early Alpha but still fun. I like the fact you build your space ships and platforms out of blocks then fly them around. You can also enable space ships which fly through the game area which you can either salvage for parts or just take control of yourself.

Skirting the issue.

Every so often I decide that I actually want to be a bit feminine for a change and you know wear make up and stuff. Funnily enough this has usually been around the time of BiCon which is the annual convention for bisexual people and allies to get together and hang out.
This year however I have a party coming up and I’m thinking it would be nice to dress up for it for a change rather than just wearing my usual jeans and stuff. I think part of if is hanging around K and remembering when I used to dress up really gothy myself.
I’m currently spending some time trying to find myself the right knee length skirt (yes I’m very fussy about what I wear being exactly right) and thinking about wearing it with some funky tights (I used to have a thing for funky tights). I think I may also be taking S to Camden market to buy a new pair of New Rocks. I used to have the ones I really like but sadly the zip went on them so they didn’t end up moving with me to London.
I’ve also been practising a bit with eye make-up as I used to be good at it but am so hideously out of practice I’m having to relearn how to do it.

GRE tunneling for fun and profit.

I recently subscribed to Netflix and being in the UK I found that they have loads more available in the US to watch.  To get around this in a way that would also allow me to stream programmes to my chromecast is actually quite complicated.  As I have small Linux box on my network to provide IPv6 via a tunnel I thought I would allow this to also allow access via a GRE tunnel to a VPS running in the US.

The first thing you need to do is setup the machine in the US so it can do NAT just like your home router can do with the following:
#!/bin/sh
echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -F FORWARD

iptables -A FORWARD -m state –state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 192.168.0.0/16 -j ACCEPT
iptables -A FORWARD -j REJECT
iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o eth0 -j MASQUERADE
iptables -A INPUT -i tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A INPUT -i tap+ -j ACCEPT
iptables -A FORWARD -i tap+ -j ACCEPT

iptables -A INPUT -i us-gre -j ACCEPT
iptables -A FORWARD -i us-gre -j ACCEPT
iptables -A INPUT -i us-gre -j ACCEPT
iptables -A FORWARD -i us-gre -j ACCEPT

Your then need to setup a GRE tunnel between your VM running in another country and your home network.  One thing to be aware of is that GRE tunnels use a specific IP protocol number rather than TCP or UDP.  This means that your need to either activate an option which enables this or setup the machine doing the routing on your home network as the DMZ host.

I used the following to setup a GRE tunnel on the VPS which will forward all data to my home network over the tunnel (my home network has addresses in the 192.168.x.x range).


ip tunnel del us-gre
ip tunnel add us-gre mode gre remote local ttl 255

ip link set us-gre up
ip addr add 192.168.X.1/24 dev us-gre

echo add routes

ip route add 192.168.0.0/16 via 192.168.X.10 dev us-gre
#ip route add 192.168.0.0/16 dev us-gre

Once you have everything setup on the VPS VM then your need to do the same on your home network with the following:


# VPN hosts.
ip rule add from 192.168.0.x table vpn

# Add default routes for vpn table.
ip route add default via 10.9.0.1 dev tun0 table vpn

ip route flush cache

ip tunnel del us-gre
ip tunnel add us-gre mode gre remote local ttl 255

ip link set us-gre up
ip addr add 192.168.8.10/24 dev us-gre

echo add default route

ip route add 0.0.0.0/1 via 192.168.8.1 dev us-gre table vpn

You will also need to run the following to add a table so you can have different routing destinations for different hosts which route via this machine.

echo 200 vpn >> /etc/iproute2/rt_tables

In order to make a machine use the tunnel your need to adjust your DHCP settings so the machine you want uses the machine with the tunnel as it’s default route. Once this is done your use:

ip rule add from ip route flush cache

table vpn on the machine with the tunnel on it. This creates a rule which makes the requested machine use a different routing table to all other traffic.

Minediary: Pyramid Scheme

2014-02-27_22.51.45
So Me and S have been busy for the last few days creating a minecart railway network. We decided to create a minecart line going all the way down to the village on the south of the map with a spur line going to my pyramid in the swamps.
It’s going well so far. S’s line is quite close to the village now after a day of creating bridge, tunnels and clearing obstacles. My next job will be to create a automated junction with buttons to select the appropriate destination. Then I will start laying track to the pyramid. I think it will take awhile as I don’t play as much and I also want to build a farm and a new house on an island I built a branch line to.