Sunday, November 27, 2011

Java Source Code Hiding

I was thinking of a way to hide complex algorithms in source code and it turns out that Java Compiler can compile source code with unicode code character encoding. So for instance a typical HelloWorld class like this
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
 
Can be written as

\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0063\u006C\u0061\u0073\u0073\u0020\u0048\u0065\u006C\u006C\u006F\u0057\u006F\u0072\u006C\u0064\u0020\u007B\u0009\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006F\u0069\u0064\u0020\u006D\u0061\u0069\u006E\u0028\u0053\u0074\u0072\u0069\u006E\u0067\u005B\u005D\u0020\u0061\u0072\u0067\u0073\u0029\u0020\u007B\u0009\u0009\u0053\u0079\u0073\u0074\u0065\u006D\u002E\u006F\u0075\u0074\u002E\u0070\u0072\u0069\u006E\u0074\u006C\u006E\u0028\u0022\u0048\u0065\u006C\u006C\u006F\u002C\u0020\u0057\u006F\u0072\u006C\u0064\u0021\u0022\u0029\u003B\u0009\u007D\u007D

And it would compile just fine using javac and will run and print Hello, World!. I wrote a small application which converts any java source file to Unicode encoding so that you can hide your source within your computer from people who have access.

import java.io.*;
/*
 * @author Shazin Sadakath
 */
public class JavaUnicodeEncoding {
    public static void main(String[] args) throws Exception {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(args[0]));
            bw = new BufferedWriter(new FileWriter(args[1]));
            String line = null;
            while((line = br.readLine()) != null) {
                for(char c:line.toCharArray()) {
                    bw.write(String.format("\\u%04X",(int) c));
                }
                bw.flush();
            }
        } catch(ArrayIndexOutOfBoundsException e) {
            usage();
        } catch(Exception e) {
            System.err.println(e.getMessage());
        } finally {
            if(br != null) {
                br.close();
            }
            
            if(bw != null) {
                bw.close();
            }
        }
    }
    
    private static void usage() {
        System.out.println("java JavaUnicodeEncoding <input class filename /> <output class filename/>");
    }
}
You can even use the two interchangeably also
public class HelloWorld {
    public static void main(String[] args) {
        \u0053\u0079\u0073\u0074\u0065\u006D\u002E\u006F\u0075\u0074\u002E\u0070\u0072\u0069\u006E\u0074\u006C\u006E\u0028\u0022\u0048\u0065\u006C\u006C\u006F\u002C\u0020\u0057\u006F\u0072\u006C\u0064\u0021\u0022\u0029\u003B
    }
}

Thursday, November 24, 2011

DOS Batch File to Periodically Copy Files to a Location

During a Testing I had to do at my workplace I had to copy files to an input location at a random time so that there would be a constant supply of input files. I did some googling and found out several commands and integrated those together to create a batch file which can do that. The waiting is done using a ping command with a timeout. The timeout is taken from the system environment variable RANDOM so that it pauses for a random amount of time before copying files. After copying files I do a clean up on output files so that the storage is not spiked.

:again

SET COPYCMD=/Y

ping 123.45.67.89 -n 1 -w %RANDOM% > nul

xcopy <Source Folder with Files>\* <Destination Folder with Files> /s 

del /q <Folder Where Files to be Deleted Available>\*

goto again


COPYCMD Environment Variable is used to Enable overwriting of existing files without prompting.

Saturday, November 19, 2011

Arduino Google Voice Activated Servo Motor Controlling using Android

I was having some free time and wanted to put learn Android development. I found a Great Android Application called SL4A (Scripting Layer for Android) which is an intermediate Android app which sits between the Android OS native methods and popular scripting languages such as Shell Script, Python, Perl etc.

It opens up many possibilities to develop applications in Android really quickly with interpreted languages like Python. I always wanted to try out Door unlocking with an Android Google Voice API. For this system I wanted to connect to Wifi network and given the Voice command open or close a Door lock. So I came up with the following Circuit using Arduino;

Things required to build the Prototype.

  1. One Arduino (Any model)
  2. A Breadboard
  3. A Servo Motor
  4. Two LEDs (Green and Red)
  5. Two 1K resistors for LEDs
  6. Jumper Cables
Using the above mentioned equipment I put up the following prototype. 


The Green LED is driven using Pin 2, The Red LED is driven using Pin 3 and the Servo motor is attached to Pin 9 (Analog Output with PWM).

Android phone I have is a Samsung Galaxy ACE with Android OS 2.3.3 Gingerbread. I installed SL4A and  Python for Android to enable Python Scripting in Android.

#=======================================
#=          Shazin Sadakath            =
#=======================================

import android
import socket
# Android API Object SL4A Specific
droid = android.Android();

# Hostname or IP
hostname = "192.168.1.65";
# Port
port = 10000;
data = "";
# Creating a UDP Socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
# Connecting to Host (Usually not required since it is UDP)
s.connect((hostname,port));
print "Connected!";

exit = False;
while not exit:
  # Using Android API for Google Voice and Recognizing command
  data = droid.recognizeSpeech().result;
  # Command is open or close proceding it to the Server else Error message
  if data == "open" or data == "close":
    # Sending command
    s.sendto(data,(hostname,port));
    # Recieving an ACK since UDP is unreliable 
    data = s.recvfrom(1024);    
    print "Ack ", data[0];
    # If ACK is success ending the program
    if data[0] == "success":
      exit = True;
  else:
    print "Invalid Command";
   

The Middle Man in this application is a Java Thread based UDP Server which listens for UDP packets from a connected Android app and Does the Serial communication to the Arduino Microcontroller.

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Shazin Sadakath
 */
public class SpeechDoor implements Runnable {
    private DatagramSocket socket;

    public SpeechDoor() {
        try {
            ArduinoBridge.init("COM27");
            socket = new DatagramSocket(10000);
            new Thread(this).start();
        } catch (SocketException ex) {
            Logger.getLogger(SpeechDoor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void run() {
        byte[] data;
        boolean success;
        DatagramPacket recv, ack;
        while (true) {
            try {
                data = new byte[1024];
                success = false;
                recv = new DatagramPacket(data, data.length);
                System.out.printf("Waiting on Socket : %s:%d\n", socket.getLocalAddress().toString(), socket.getPort());
                socket.receive(recv);
                System.out.printf("Packet Recieved From : %s:%d\n", recv.getAddress().toString(), recv.getPort());
                String value = new String(data, recv.getOffset(), recv.getLength());
                System.out.printf("Value : %s\n", value);
                if("open".equals(value)) {
                    ArduinoBridge.writeToArduino((byte) 1);
                    success = true;
                } else if("close".equals(value)) {
                    ArduinoBridge.writeToArduino((byte) 2);
                    success = true;
                }
                if(success) {
                    data = "success".getBytes();                    
                } else {
                    data = "fail".getBytes();
                }
                ack = new DatagramPacket(data, data.length, recv.getAddress(), recv.getPort());
                socket.send(ack);
                success = false;
                Thread.sleep(1000);
            } catch (Exception ex) {
                Logger.getLogger(SpeechDoor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) {
        new SpeechDoor();
    }
}


Arduino Serial Communication is done using the RXTX Library for Serial Communication.


import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.OutputStream;
import java.util.Enumeration;

/**
 *
 * @author Shazin Sadakath
 */
public class ArduinoBridge {

    private static SerialPort port = null;
    private static CommPortIdentifier cpi = null;

    public static void init(String p) {
        Enumeration enums = CommPortIdentifier.getPortIdentifiers();

        while (enums.hasMoreElements()) {
            cpi = (CommPortIdentifier) enums.nextElement();
            if (p.equals(cpi.getName())) {
                break;
            }
        }

        if (cpi != null) {
            try {
                port = (SerialPort) cpi.open("ArduinoJavaBridge", 1000);
                if (port != null) {
                    port.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                }

                System.out.println("Ready!");
                //new Thread(new ArduinoBridge()).start();

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


        }
    }

    public static void writeToArduino(byte value) {
        try {
            
            OutputStream os = null;

            os = port.getOutputStream();
            
            os.write((byte) 0xff);
            os.write((byte) (value));
            os.flush();        

            if (os != null) {
                os.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   
}


Finally the Arduino Sketch which is Loaded in to the Microcontroller


/* 
 * Shazin Sadakath
*/

#include <Servo.h>
#define MAX 150
#define MIN 0

int greenLedPin = 2;
int redLedPin = 3;
int servoPin = 9;
int command = 0;
boolean open = false;

Servo doorLock;

void setup() {
 Serial.begin(9600);
 doorLock.attach(9);
 pinMode(greenLedPin, OUTPUT);
 pinMode(redLedPin, OUTPUT);
 digitalWrite(greenLedPin, LOW);
 digitalWrite(redLedPin, HIGH);
}

void loop() {
 if(Serial.available() >= 2) {
  if(Serial.read() == 0xff) {
    command = Serial.read();
      if(command == 1) {
        if(!open) {
         doorLock.write(MAX); 
         digitalWrite(redLedPin, LOW);
         digitalWrite(greenLedPin, HIGH);
         open = true;
        }
      } else if(command == 2) {
        if(open) {
         doorLock.write(MIN);
         digitalWrite(greenLedPin, LOW);
         digitalWrite(redLedPin, HIGH);
         open = false;
      } 
     }     
  }  
 }  
 delay(10);
}

The video of the System is below. The accuracy completely depends on the way you pronounce the words "open" and "close", Surrounding noise and Google Voice API. In this video as you can see the The "open" word is not recognized first time. But the close word is.



Constructive Criticism is always welcome!

Trackbacks/Pings
  1. An Exercise in Servo Voice Control with Android - Hackaday 
  2. A Voice Activated Servo - SL4A Tutorials  

Saturday, November 12, 2011

Arduino Interrupt based LED with Toggle Button

I have been quit for sometime in my blog due to some work I had at work place. Got the time to work on Arduino Interrupts today and managed to put a small sketch on Arduino based Interrupts. Interrupts are a really powerful concept in hardware as well as in software. Specially in hardware, Interrupt eliminates polling saving precious processing cycles.

An interrupt is a Sporadic event which occurs asynchronously. For instance while reading a socket for data we can either Read that socket Periodically to see whether there is any data to be read or we can attach an ISR (Interrupt Service Routine) for that socket. Whenever there is data available in the socket buffer the ISR will be called asynchronously and the main program will be stopped executing. When the ISR is finished executing the main program will execute from where it stopped. This is called Context Switching.

Arduino UNO has two pins for External Interrupt handing INT0 (attached to pin 2) and INT1 (attached to pin 3). What I have tried to accomplish is without periodically reading pin 2 for a High value using Processing Cycles. Whenever there is FALLING (High to Low) in pin 2 a predefined ISR to be called which basically checks the state and does the opposite.

The circuit sketch looks as the following 



The Arduino Sketch for the program is as below. I have used INT0 with pin 2 for interrupt signal. As you can see the loop method doesn't do any polling on the value of pin 2. And I have used pin 10 for LED Output.

int pin = 10;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  digitalWrite(2, HIGH);
  attachInterrupt(0, toggle, FALLING); // Attaching the ISR to INT0
}

void loop()
{
  // Does Nothing
}

// Interrupt Service Routine
void toggle()
{
  if(state == LOW) {
    state = HIGH;
  } else {
    state = LOW; 
  }
  digitalWrite(pin, state);
}