Thursday, December 15, 2011

ICS: Getting NFC Tags to Work

Since December I'm a happy owner of a Galaxy Nexus :)
NFC seemed like an interesting thing, so I started to integrate NFC support into PAW.

First thing I did was to order some Mifare Classic 1K tags from tagsfordroid.com.
They arrived fairly quickly and delivery was free of charge.
NFC Tags
To try things out I installed a lot of NFC reader and writer apps. To my dismay none of them seemed to work.
They could read the tags but could not write them. Having no idea how NFC worked, I was fairly disappointed :(

Searching the web I found out that this seems to be a bug in ICS:
http://code.google.com/p/android/issues/detail?id=22258

Googles sais that there will be a fix provided, but when, no one can tell.
So I tried to get it working myself...

If you would like to test the things below, install the latest PAW version (0.80) from the Android Market.

Disclaimer:
This solution is only working for Mifare Classic 1K tags!
If you try as described you do it at your own risk!
It's not my fault if your tags do not work afterwards!


Read the updates at the end of the post first!
This solution is no longer necessary and will most likely damage your tag(s), so do not use it!

The Problem
The problem (as reported in the bug report) seems to be that the tech reported by the tag do not contain
android.nfc.tech.Ndef which is necessary to write NDEF messages.

You can try this with the following code that you can insert into the BenShell Console of the PAW web application:

import de.fun2code.android.pawserver.AndroidInterface;
import android.nfc.NfcAdapter;

intent = AndroidInterface.getNfcIntent(5); // 5 secs timeout
if(intent != null) {
  tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  print(tag);
}
else {
  print("No Tag found!");
}

If the result is TAG: Tech [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA] writing NDEF messages will not work!


The Possible Fix
As mentioned above I have no expirience with NFC, so I tried to find a solution that worked without having to know NFC or the Mifare tags in detail.

I searched the web and found some Mifare Classic 1K dumps at the nfc-tools site.
After downloading a dump I tried to write it to the tag. Interesting enough it seemd to work :)

After writing the dump, the above script reported:
TAG: Tech [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.Ndef]

So here are the step by step instructions:
  1. Download the dump file from the nfc-tools site: mfc_1k__url_nfc-tools.mfd
  2. Save the dump file to the /sdcard of your android device.
  3. Start PAW and open the BeanShell console.
  4. Copy/paste the following script into the console:
    /*
    Mifare Classic - Write dump
    */
    import de.fun2code.android.pawserver.AndroidInterface;
    import android.nfc.NfcAdapter;
    import android.nfc.NdefRecord;
    import android.nfc.NdefMessage;
    import android.nfc.tech.*;
    import android.os.Bundle;
    
    
    intent = AndroidInterface.getNfcIntent(5); // 5 secs timeout
    print(intent);
    if(intent != null) {
      tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
      print(tag);
      
      if(tag != null) {
          nfc = MifareClassic.get(tag);
          nfc.connect();
        print("Is connected: " + nfc.isConnected());
    
      
         fis = new FileInputStream("/sdcard/mfc_1k__url_nfc-tools.mfd");
         
         b = new byte[16];
    
         errors = 0;
        
        sectors = nfc.getSectorCount();
        blocksPerSector = nfc.getBlockCountInSector(0);
        
        for(sector=0; sector<sectors; sector++) {
          print("Sector " + sector);
          
          for(block=0; block < blocksPerSector; block++) {
            logicBlock = nfc.sectorToBlock(sector) + block;
            $$.print("  Block: " + block + " ... ");
    
            fis.read(b);
    
            if(sector == 0 && block == 0) {
               print("Manufacturer block ... skipped");
               continue;
            }
    
            try {
              nfc.authenticateSectorWithKeyA(sector, MifareClassic.KEY_DEFAULT);
              nfc.writeBlock(logicBlock, b);
              print("ok");
            }
            catch(e) {
                print("error");
                errors++;
            }
          }
        }
        
        fis.close();
        nfc.close();
          print("--------------------------------------------------------------------------");
          print("Errors: " + errors);
          print("Dump written!");
       }
       else {
          print("Tag not supported!");
       }
    
    }
    else {
      print("No Tag found!");
    }
    
    
  5. Start the script and as soon as the PAW app is in foreground, place the tag at the back of the phone and leaf it there until the script is finished.
After that the tag is hopefully working.

Good luck and happy hacking :)

Update:
Ndef.getMaxSize() returns only 92 bytes that can be written to a NDEF message. I don't think that's normal :(

You can check this yourself with the following code:
import de.fun2code.android.pawserver.AndroidInterface;
import android.nfc.NfcAdapter;
import android.nfc.tech.Ndef;
 
intent = AndroidInterface.getNfcIntent(5); // 5 secs timeout
if(intent != null) {
  tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  print("Max NDEF message size: " + Ndef.get(tag).getMaxSize() + " bytes");
}
else {
  print("No Tag found!");
} 

Update 2:
The latest NFC TagWriter by NXP is able to format unformatted tags and fixes the problem.
Here is the Android Market link: NFC TagWriter by NXP

Thursday, December 1, 2011

PirateBox on Android

A while ago psundegroud created the thread [TUT]Roll Your Own PirateBox! PirateBoxMobile. At that time my only rooted device was an old Samsung Galaxy I7500 running Android 1.6. We managed to get this working. The two main issues were that WiFi Tether was needed, which didn't run on all Android devices and that only Ad-Hoc mode was available. The missing Infrastructure mode prevented most Androids from seeing the PirateBox hotspot.

Note: This post is somewhat outdated. The current state of the PirateBox can be found here: PirateBox Reloaded

Meanwhile I have a rooted Notion Ink Adam running AdamComb v0.3. So I tried to get PirateBox running by using the built in tether app. Built in tethering uses Infrastructure mode, which solves above mentioned problem.

On my Adam this is really working fine and I hope that it will also run on other Android units.
I'll divide the post in two parts. The first part describes the technical details, so if someone has problems in getting this to work this might be helpful for further testing.

Along with setting up the scripts I have also written a plugin for PAW which should make setup really easy.
If you are not interested in the technical details you can right jump to the Installing the Plugin section.

Techical Details

Setting up the PirateBox during boot is done by PAW startup scripts.

The main tasks of these scripts are:
  • Killing dnsmasq and restarting it with changed parameters, so that DNS requests always respond with the IP of the Android device.
  • Configuring iptables so that requests to port 80 are redirected to the port PAW is listening on, because PAW can not operate on privileged ports.
  • Change the PAW configuration to use the PirateBox Handler, which ensures that all unknown request are forwarded to the base URL (piratebox.org/).

The startup scripts are located in the subfolders 0 and 1 of the directory /sdcard/paw/etc/init. These folders represent PAW runlevels. Runlevel 0 is before the server starts and after server shutdown and 1 is after server start and before the server shuts down. Scripts beginning with S_ will be called on startup and scripts starting with K_ will be run at shutdown.

So here is a short summary what these scripts are doing.

0/S_PirateBox.bsh:
  • Checks if tethering is running. If not none of the steps below will be performed.
  • Kills the "original" dnsmasq process and starts a new one.
  • Executes iptables commands to get port redirection working.
  • Replaces the original PAW configuration with the PirateBox configuration.

1/S_SpirateBox.bsh:
  • Checks if tethering is running. If not none of the steps below will be performed.
  • Restores the original PAW configuration.
  • Displays a PirateBox notification.
  • Sets the max upload limit to 200MB.

1/K_S_SpirateBox.bsh:
  • Clears the PirateBox notification.
  • Kills dnsmasq process.
  • Stops tetherig and restarts WiFi.

0/K_S_SpirateBox.bsh:
  • Removes iptables rules for port redirection.

If you run into problems have a look at these scripts and try to run the commands inside the scripts individually.


Installing the Plugin

PAW Plug-in Menu
Installing the plugin is easy. I've put it on the PAW plugin page, so you'll find it within the PAW web application or you can direct download it from the PAW Plugin Page.
After download extract the ZIP file to the /sdcard/paw/html/app/plugins directory of your Android device.
Now after re-entering the PAW web application, the PirateBox plugin should be visible in the Plugins menu.

If the plugin setup screen shows a warning in red, the installation is likely to fail, because some prerequisites are missing.
If no warnings are displayed, press the Install button. You can also try to setup a PirateBox Access Point (AP) automatically. I'm not sure, if this is working on all devices. If it's not working, please create an AP manually.
To uninstall the plugin, press the Uninstall button.

PirateBox Plugin

It is important to note, that PirateBox will only be started if tethering is active before PAW starts up.
Otherwise the normal PAW configuration will be applied. So you can choose between these two configurations.

I hope that's not just working on my Adam but on many other devices as well.

Screenshost
Here are some screenshots from my setup...

PirateBox Startup

Connected to PirateBox

PirateBox on Nexus One
PirateBox in Chromium

Video

Here is a video that shows the PirateBox in action...



Update

Since version 0.3 of the plugin, PirateBox is also working on rooted Galaxy Nexus devices running Ice Cream Sandwich.
Here is a video...


Thursday, November 24, 2011

Running PAW on Port 80

PAW is only running on ports above 1023, because restricted ports are not permitted for normal apps.
For root users there is a solution by using iptables via the Superuser app.

I'm not sure if the below mentioned solution is working on all rooted devices.
Here is my configuration: Notion Ink Adam running AdamComb v0.3

If this works for you can easily be tested by copying the below code into the BeanShell Console of the PAW web application:

import de.fun2code.android.pawserver.util.*;

execRootShell(String[] commands) {
  shellCmd = "su -c sh";

  sh = Runtime.getRuntime().exec(shellCmd);
  os = sh.getOutputStream();

  for(cmd : commands) {
    os.write((cmd + "\n").getBytes());
  }

  os.write(("exit\n").getBytes());
  os.flush();
  sh.waitFor();  
}

/* ---- Config -------- */
ip = Utils.getLocalIpAddress();
redirectFrom = 80;
redirectTo = 8080;
action = "ADD"; // Can be ADD or DELETE
/* -------------------- */

action = "-" + action.substring(0, 1);

String[] iptablesCmds = new String[] {
  "iptables -t nat " + action + " OUTPUT -d 127.0.0.1 -p tcp --dport " + redirectFrom + " -j REDIRECT --to-ports " + redirectTo,
  "iptables -t nat " + action + " OUTPUT -d " + ip + " -p tcp --dport " + redirectFrom + " -j REDIRECT --to-ports " + redirectTo,
  "iptables -t nat " + action + " PREROUTING -d " + ip + " -p tcp --dport " + redirectFrom + " -j REDIRECT --to-ports " + redirectTo
};

execRootShell(iptablesCmds);

After executing the code, PAW should respond to requests on port 80.
If not, this solution seems not to be working on your configuration.

To forward port 8080 to port 80 every time PAW starts up, create a file called S_forward.bsh inside the /sdcard/paw/etc/init/0 directory.
In order to stop the forwarding when the PAW service shuts down, create an additinal file called K_forward.bsh and copy the same code.
You only have to change the line action = "ADD"; to action = "DELETE"; to stop the forwarding.

Hope that's not just working on the Adam...

PHP Plug-in Update

My cross compiled version of the PHP CGI was not working so well and I didn't manage to fix it.

Fortunately Klaas Biker informed me that Iulian Virtejanu has cross compiled a PHP CGI version that seems to work significantly better.
Here is Iulian's blog entry: PHP and Lighttpd for Android

Iulian gave me the permission to use it in PAW, so I have updated the PHP Plug-in to version 0.3.

There is one known issue:

system(...) does not work.
It is because system('pwd') actually invokes a hardcoded /bin/sh -c 'pwd' - and /bin/sh is not available on Android.
Iulian has submitted a bug report to PHP: https://bugs.php.net/bug.php?id=60081

If you find more bugs, please let me know. I will forward them.

Monday, October 31, 2011

Building PHP from Scratch

This post by Rahul Amaram describes how to build PHP for Android from scratch and how it can later be used within PAW.
I've added links to the needed files at the end of the post.

Thanks to Rahul for putting this together!

Here is Rahul's documentation...



It is suggested to use a VM for compilation so that you can take snapshots at regular intervals. Also as per google recommendation, it is suggested to use a VM with at least 8 GB RAM/swap and 12 GB of free hard disk.

Install Ubuntu 10.04 64-bit (select username as "joschi") on a VM.

After booting ubuntu, login as joschi.

Download and extract android-php

$ cd
$ wget -c "http://paw-android.fun2code.de/download/android-php.zip"
$ sudo apt-get install unzip
$ unzip android-php.zip

First initialize build environment. For donut, java 5 is needed. Also compiling with gcc/g++ 4.4 was throwing errors during compilation. Therefore we use gcc/g++ 4.3 for compilation.

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu hardy main multiverse"
$ sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu hardy-updates main multiverse"
$ sudo apt-get update
$ sudo apt-get install sun-java5-jdk


$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
zip curl zlib1g-dev libc6-dev lib32ncurses5-dev ia32-libs \
x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev \
libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown \
libxml2-utils


$ sudo apt-get install g++-4.3-multilib

Download Android source
This will be about 3 GB.

$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
$ chmod a+x ~/bin/repo


$ mkdir ~/android-donut-src
$ cd ~/android-donut-src
$ repo init -u https://android.googlesource.com/platform/manifest -b android-1.6_r2
$ repo sync

It is a good idea to take snapshot of the VM here.

Build Android

$ cd /usr/bin
$ sudo ln -sf cpp-4.3 cpp
$ sudo ln -sf g++-4.3 g++
$ sudo ln -sf gcc-4.3 gcc
$ sudo ln -sf gcov-4.3 gcov
$ sudo ln -sf cpp-4.3 x86_64-linux-gnu-cpp
$ sudo ln -sf g++-4.3 x86_64-linux-gnu-g++
$ sudo ln -sf gcc-4.3 x86_64-linux-gnu-gcc


$ cd ~/android-donut-src
$ source build/envsetup.sh
$ lunch generic-eng
$ make -j4

It is a good idea to take another snapshot of the VM here.

Next build android-php. The patch is applied in order to avoid the error "undefined reference to `__sync_fetch_and_add_4'" during compilation.

$ cd ~/android-php
$ rm -rf ~/android-donut-src/bionic/libc/include/
$ unzip -d ~/android-donut-src/bionic/libc/ bionic_libc_include.zip
$ patch php-5.3.6/ext/standard/php_crypt_r.c < ~/sync_fetch_and_add.patch
$ ./build_php_5.3.sh

Finally copy php-5.3.6/sapi/cgi/php-cgi to /mnt/sdcard/paw/html/app/plugins/php_plugin/bin/ on your android phone (be sure to backup the existing php-cgi file), and install PHP using the PAW Web App.

References:



Files:
android-php.zip
sync_fetch_and_add.patch

Wednesday, October 19, 2011

Setting up a Cloud Print Printer using the Cloud-X API

The Cloud-X Java API makes it easy to set up a Google Cloud Print aware printer. In the latest API version Cloud Print push notifications are supported.


The below sample code shows the registration of a new printer (if not already present) and how to listen to push notifications.
For the sake of simplicity this is straight forward code, so there might be cleaner ways to do is ;)

Here is the sample code with comments:

/*
  * Process print job
  */
 public static void processJobs(CloudPrintConnection gcp, Printer printer) {
  try {
   List<Job> jobs = gcp.fetch(printer);
   if (jobs != null) {
    for (Job job : jobs) {
     System.out.println("Processing job " + job.getId() + " ("
       + job.getTitle() + ")");
     gcp.deletJob(job);
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  try {
   String printProxy = "testprinter-" + NetworkUtil.getMacAddress();

   /*
    * Our printer
    */
   final Printer printer = new Printer();
   printer.setProxy(printProxy);
   printer.setName("Test Printer");
   printer.setStatus("Online");

   final CloudPrintConnection gcp = new CloudPrintConnection();
   gcp.connect(username, password, "cloudprint",
     "Cloud Print Test Client", null);

   /*
    * Register printer if not already present
    */
   List<Printer> printers = gcp.list(printProxy);
   if (printers.size() == 0) {
    System.out.println("Registering Printer with Proxy "
      + printProxy);
    gcp.register(printer, null);
   } else {
    printer.setId(printers.get(0).getId());
   }

   /*
    * Refetch all printer info
    */
   gcp.printer(printer);

   System.out.println("Printer Name: " + printer.getName());
   System.out.println("Printer ID: " + printer.getId());
   System.out.println("---------------------------------------------");

   processJobs(gcp, printer);

   /*
    * Wait for push notifications
    */
   PushReceiver pr = new PushReceiver("Cloud-X API - Push Receiver");
   pr.addListener(new PushListener() {

    @Override
    public void onReceive(String printerId) {
     if (printerId.equals(printer.getId())) {
      processJobs(gcp, printer);
     }
    }

    @Override
    public void onConnect() {
      System.out.println("Connected to Google Talk!");
    }

    @Override
    public void onDisconnect() {
      // Do nothing
    }
   });

   pr.connectPlain(username, password);

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

Thursday, October 13, 2011

Cloud-X - Sharing Files through the Cloud

We just released the first alpha version of Cloud-X.
Cloud-X uses Google Cloud Print to share files between different users and devices. Along with the PC and Android app the Java API to access Google Cloud Print is also released.

This is a very early version, so there might be bugs.
The Android app and an online and stand alone PC version can be found at the following site:
http://cloud-x.fun2code.de

Comments welcome!

Update

The website is no longer online.
API with included sources can be downloaded from http://fun2code.de/downloads/GoogleCloudPrint.jar.