Saturday, June 27, 2009

Python: Simple Singleton for Mysql Access

I was trying to come up with a simple implementation of Singleton pattern in python, its a bit different from other language constructs. Here is the code implementing a single point database access using MySQLdb.

#! /usr/bin/python

import MySQLdb
import sys

class Conn(object):
__instance = None
__conn = None


def __init__(self):
try:
if Conn.__instance == None:

Conn.__instance = self
Conn.__conn = MySQLdb.connect (host="localhost", user="user", passwd="pass", db="dbname")

except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])

sys.exit(1)


def get_connection(self):
return Conn.__conn


if __name__ == "__main__":
conn = (Conn()).get_connection()

''' Do your thing '''
conn.close()


It's not perfect I guess, but right now it serves the purpose. You are welcome to put down any valuable advice into this.

Friday, June 26, 2009

Mysql: Dumping only stored procedures

I had to find a way to dump ONLY the stored procedures from a mysql database. Here is how I did it.
mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt "databasename" > stored_procedure.sql



PS. This is the shortest post that I ever made. :)

Thursday, June 25, 2009

Python: C++ style cin, cout in Python

Ready to bring some flavor of C++ into python? If you like cout, cin in C++ and also a python programmer, you'd definitely like this snippet.
import sys
class ostream:
def __init__(self, file):
self.file = file

def __lshift__(self, obj):
self.file.write(str(obj));
return self
cout = ostream(sys.stdout)
cerr = ostream(sys.stderr)
endl = '\n'

x, y = 'Printing', 'like C++'
cout << x << " " << y << endl
Pretty cool, huh. I found this and a lot of other cool stuffs from Peter Norvig's blog. Its such a nice blog, just subscribed to it.

Sunday, June 21, 2009

Computer Vision: Working with OpenCV and beyond

For my software Engineering project, I had chosen to work on something different other than database and information management system that we typically take in this course. I've had enough of it in my last term. So this time we three group members choose to work on Computer Vision System. At first we where quite confused because we knew nothing about this field and given our inexperience, it was really difficult. Even our project requirement was a bit hazy, so to speak :)

We tried to learn about the basic concepts of computer vision, image analysis and had to go through an extensive set of frameworks to evaluate which fits our needs. Initially our project requirement was to detect/recognize human facial structure but this was later retrofitted with object detection and feature extraction. Through the course of time we tried to study a lot of research papers (most of them seemed to me like Egyptian Hieroglyphics, :(( ...).

The first framework we came across was Torch3Vision. This framework is built on top of Torch3, which is gives the user a lot of image processing algorithms. Torch3Vision is extensively built for Facial detection. While it was really a good framework, it proved a bit sturdy to customize for our general need as the project requirement started to change.

We later started to study about Content Based Image Retrieval (CBIR) techniques, two of the framework we took into account was GIFT (also known as GNUIFT, to avoid name confusion with another open source tool) and Lire. Lire is written in java and uses Lucene api in the backend, while GIFT is written in C/C++. These implementations enable the user to search images by 'Query by Example', not what we exactly wanted to do. So yeah, another dead end what it seemed ...

The last one, and which of course we choose to work on was OpenCV. OpenCV is an open source library initially built and later sponsored by Intel. It is used extensively in various fields of computer vision, so its in active development and well documented. It also comes with a comprehensive e-book that can help you a lot in crunching the heavy duty libs for the first time.

We also took help from another project that uses OpenCV library to detect objects using 'Boosted Histograms' method, its called objectdet. Here I'm going to go through the basic steps of running OpenCV library in Linux as well as compiling objectdet in Linux. Though OpenCV comes with all major linux distros, I recommend direcetly downloading and compiling from the svn. They have a very extensive wiki, this
page has the detailed instructions for installing in all OS. One thing I'd like to mention here, is the CMake based build is more straightforward than Autotools as it sometimes (on rare occasions) breaks.

After you have OpenCV set up and running, you can try to install 'objectdet' on your system (I'm running it in a Ubuntu 9.04). It hasn't been on active development for a while and running the old code with new libs can surely have its moments(!!!).


sudo apt-get install build-essential libxml++-dev libboost-filesystem-dev
svn checkout http://objectdet.googlecode.com/svn/trunk/ objectdet-read-only
cd objectdet-read-only/objectdet
chmod a+X autogen.sh
./autogen.sh
mv Makefile.am makefile.am
sudo ln -s /usr/lib/libboost_filesystem.a /usr/lib//libboost_filesystem-gcc.a
sudo ln -s /usr/lib/libboost_filesystem.so /usr/lib//libboost_filesystem-gcc.so
./configure --with-opencv-headers=/usr/local/include/opencv/
cd src
make
sudo make install
cd ..

Run the demo

src/objectdet -i images/000537.png -c classifiers/cabmodel_interm_nst40_VOC06car01train5_trainval_orienthistmod.xml

A simple window with a car being detected should appear. Here the xml file contains the trained data for the 'car' object. How to create this trained xml? Well, that's the harder part, I'll try to write that in my next post when I get some time from my GSoC project.

If you are interested in working in Computer Vision and searching for a good set of library, apart from the above mentioned ones, also take a look at these.

1. Gandalf (yeah Gandalf, I really liked the name)
2. This site hosts a lot of computed vision projects
3. LTI-LIB


Wednesday, June 17, 2009

Python: Working in Unicode

For my ongoing Google Summer of Code project, I need to write a lot of scripts that run analysis on Bengali words. So far I had been doing away with shell scripts and a lot of php-cli scripts. But writing long object oriented code in php seems a bit cumbersome to me (that's my personal opinion, no offense to the die hard php-lovers :)), so I decided to move to python. Most of the scripts that my mentor provided me was also in python, so it made really good sense.

While working on unicode based characters in python, you'll often come across this type error message (this cost me a while to fix).


UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)


This will happen if you do not set your character encoding in your python file to UTF-8. First you need to make sure the first few lines of your programm looks like this.


#!/usr/bin/python
# coding=utf-8
# -*- encoding: utf-8 -*-


This enables you to write unicode characters in your source code. But this does not enable you to print them in console and you'll still keep getting the same error I previously mentioned.

To solve this you need to add the following code in your /usr/lib/python2.5/sitecustomize.py file (This might change depending your installed python version)


import sys;
sys.setdefaultencoding('utf-8')


I first tried to did this in the source code but it didn't work. I kept getting this error

AttributeError: 'module' object has no attribute 'setdefaultencoding'


I'm no python expert, but maybe python's default behavior is not to allow changing of the encoding in runtime just for safety (an increasing amount of system tools are written in python these days and they run all the time in Gnome and KDE). That'd make more sense.

Saturday, May 02, 2009

Fun with Posix Threads: Bank Teller Problem

A few weeks earlier we were given an interesting assignment on POSIX threads. The problem statement is as follows:

"You have to simulate a multiteller bank. The bank has two tellers. Customers enter the bank one at a time. Each teller has a separate queue. A new arriving customer joins the shortest queue, choosing the leftmost in case of ties. Each teller takes one customer from his queue and services the customer (ignore the service time). If the teller finds that his queue is empty then it goes to sleep. There will be three Apartments. Each apartment will produce customers for the bank. Both queues have length of 5 customers. So whenever Queue is full no customer is allowed to enter the bank. In that case the apartment goes to sleep."


The POSIX library has a lot of powerful tools for handling this kind of problems. With my very little experience, I decided to stick with just mutex and semaphores. But one thing came to my mind, since I'm an avid fan of java's threading model, I decided code it in the Java way!!! Yes create a C++ class that behaves like a Java thread class. You just override the run function just like in Java, and you've got a new thread.

Let's get into some details, shall we? This is the Thread.h file. Here you can find the Thread class,


#ifndef THREAD_H
#define THREAD_H

#include <cstdio>
#include <string>
#include <exception>
#include <cassert>
#include <cstdlib>

#include <pthread.h>
#include <semaphore.h>


class Thread{
protected:

pthread_t thread;
int res;
void *thread_result;

volatile bool stoprequested;
volatile bool running;

// this semaphore is just for a safety precation
sem_t safety_sem;

static void* __run(void *object){
void *exit_status;

printf("Calling the run function for thread.\n");
// this is the crutial part,
reinterpret_cast(object)->run();

pthread_exit(exit_status);
}

virtual void run(){
printf("If you are seeing this, it means have to run this programme again\n\
This happened because the overridden run fuction did not get the time to be instantiated as the thread got created too earlier\n");
}

public:
const char *id;

int start(){
// assert(running == false);
if(running == false){
running = true;
printf("Creating the thread with id %s\n", id);
usleep(200);

// we are using this semaphore so that this function waits upto the point when the dereived class gets instantiated
sem_wait(&safety_sem);

res = pthread_create(&thread, NULL, __run, this);
if(res != 0){
perror("Thread creation failed\n");
exit(EXIT_FAILURE);
}
}
return 0;
}

Thread(const char *id): running(false), stoprequested(false){
this->id = id;
sem_init(&safety_sem, 0, 0);
}


void stop(){
// assert(running == true);
if(running == true){
running = false;
stoprequested = true;
res = pthread_join(thread, &thread_result);
if(res != 0){
printf("Thread joining failed");
}
printf("%s: Thread is returning\n", id);
}
sem_destroy(&safety_sem);
}

~Thread(){
if(stoprequested == false){
stop();
}

}

bool isAlive(){
return running;
}
};


#endif // THREAD_H



Now we'll need a thread safe queue implementation.
Here is the code in SafeQueue.h


#ifndef SAFEQUEUE_H
#define SAFEQUEUE_H

#include <semaphore.h>
#include <pthread.h>
#include <queue>
#include <vector>

using namespace std;

class SafeQueue{

protected:
sem_t sem_full;
sem_t sem_empty;
pthread_mutex_t mutex_common;

queue q;
int max_queue_size;
char* name;

public:

SafeQueue(const int max_queue_size, char* name){
this->max_queue_size = max_queue_size;
this->name = name;

sem_init(&sem_full, 0, max_queue_size);
sem_init(&sem_empty, 0, 0);
pthread_mutex_init(&mutex_common, 0);


}

// this function is thread safe
void push(int i, const char* thread_id){

// if full, go to sleep
sem_wait(&sem_full);

pthread_mutex_lock(&mutex_common);
// cout << thread_id << ": inside lock" << endl;
q.push(i);
cout << thread_id << ": Pushing data: " << i << ", in " << name <<", queue size is: " << q.size() << endl;

pthread_mutex_unlock(&mutex_common);

// we release the empty semaphore
sem_post(&sem_empty);
}

// this function is thread safe
void pop(int *i, const char* thread_id){

// if empty go to sleep
sem_wait(&sem_empty);

pthread_mutex_lock(&mutex_common);

*i = q.front();
q.pop();
cout << thread_id << ": Popping data: " << *i << " from "<< name << " queue size is: " << q.size() << endl;

pthread_mutex_unlock(&mutex_common);
// we release the full semapore, because the queue is now one element short of being full
// so that other thread can now push into this queue
sem_post(&sem_full);
}

// helper function, needed when we want to use this objects mutex from outside
pthread_mutex_t* getMutex(){
return &mutex_common;
}

// this function is NOT thread safe, use only when needed in conjuncture with getMutex function
unsigned get_size(){
return q.size();
}

~SafeQueue(){
sem_destroy(&sem_empty);
sem_destroy(&sem_full);
pthread_mutex_destroy(&mutex_common);
}
};

#endif // SAFEQUEUE_H



I also needed to create a parallel queue implementation which, upon call gives me the pointer to the least populated queue. Code for ParallelQueue.h


#ifndef PARALLELQUEUE_H
#define PARALLELQUEUE_H

#include <semaphore.h>
#include <pthread.h>
#include <queue>
#include <vector>

#include "SafeQueue.h"

using namespace std;

class ParallelQueue{

protected:
pthread_mutex_t mutex;

vector vq;

SafeQueue* min_q;

public:
ParallelQueue(vector vq){
pthread_mutex_init(&mutex, 0);
this->vq = vq;
}

void push_min(int data, const char* thread_id){
unsigned i;
pthread_mutex_lock(&mutex);

// lock all the queue
min_q = &(* vq[0]);
for(i=0; i pthread_mutex_lock((vq[i])->getMutex());
// find the min queue
if((vq[i])->get_size() < min_q->get_size()){
min_q = vq[i];
}
pthread_mutex_unlock((vq[i])->getMutex());
}

// insert into the min queue
min_q->push(data, thread_id);

pthread_mutex_unlock(&mutex);
}

~ParallelQueue(){
pthread_mutex_destroy(&mutex);
}
};

#endif // PARALLELQUEUE_H



Finally the main.cpp


#include <iostream>
#include <queue>
#include <stdexcept>
#include <exception>
#include <algorithm>

#include "Thread.h"
#include "ParallelQueue.h"

//using namespace std;

#ifndef _REENTRANT
#define _REENTRANT
#endif

#define QUEUE_SIZE 5



// this class gives a unique customer id, each time its called, and its thread safe
class UniqueCustomer{
public:
int id;
pthread_mutex_t id_mutex;

UniqueCustomer(): id(0){
pthread_mutex_init(&id_mutex, 0);
}
~UniqueCustomer(){
pthread_mutex_destroy(&id_mutex);
}

void setNextCustomerId(int *id){
pthread_mutex_lock(&id_mutex);
*id = this->id;
this->id++;
pthread_mutex_unlock(&id_mutex);
}
};

// the global variable for getting a unique customer
UniqueCustomer uc;

class Producer: public Thread{
private:
SafeQueue* q;
ParallelQueue *pq;
int max_producer_delay;

public:
Producer(SafeQueue* q, const int max_producer_delay, const char* id): Thread(id){
this->q = q;
this->max_producer_delay = max_producer_delay;
// this is a must
sem_post(&safety_sem);

}

Producer(ParallelQueue* pq, const int max_producer_delay, const char *id): Thread(id){
this->pq = pq;
this->max_producer_delay = max_producer_delay;
sem_post(&safety_sem);

}

void run(){
int data;
printf("%s: Starting\n", id);
for(int i=0; i < 100; i++){
// cout << id << ": trying to push " << i << endl;
uc.setNextCustomerId(&data);
// q->push(data, id);
pq->push_min(data, id);
sleep(rand() % max_producer_delay + 1);
}
}

~Producer(){
}
};


class Consumer: public Thread{

private:
SafeQueue* q;
int max_consumer_delay;

public:
Consumer(SafeQueue *q, const int max_consumer_delay,const char* id): Thread(id){
// this is a must
this->max_consumer_delay = max_consumer_delay;
this->q = q;
sem_post(&safety_sem);
}

void run(){
int i;
printf("%s: Starting\n", id);
while(1){
q->pop(&i, id);
// cout << id << ": popped " << i << endl;
sleep(rand() % max_consumer_delay + 1);
}
}

~Consumer(){
}
};


int main (void)
{
try{
srand(time(NULL));

SafeQueue sq1 = SafeQueue(QUEUE_SIZE, (char *)"Queue 1");
SafeQueue sq2 = SafeQueue(QUEUE_SIZE, (char *)"Queue 2");

vector< SafeQueue* > vq;
vq.push_back(&sq1);
vq.push_back(&sq2);

ParallelQueue pq = ParallelQueue(vq);

Producer prod1 = Producer(&pq, 2, "Producer 1");
Producer prod2 = Producer(&pq, 3, "Producer 2");

Consumer cons1 = Consumer(&sq1, 4, "Consumer 1");
Consumer cons2 = Consumer(&sq2, 2, "Consumer 2");

cons1.start();
prod1.start();

cons2.start();
prod2.start();

}catch(exception &ex){
printf("%s\n", ex.what());
}

return 0;

}




You can see that the thread functions has become pretty simple, you just need to make sure that the data you are accessing belongs to a syncronized class and don't have to worry about anything else. I'm looking for a better solution though, as there are some quirks in the code(see the comments). Might take a look into Boost's thread lib too if I can get the time (which most probably won't happen :().

Wednesday, April 29, 2009

Update: CityCell Zoom in Ubuntu 9.04

So far so goood. The new Ubuntu boots in less than 15 minues, has more eye candy than anything before. But at what cost? They have stripped the new distro of wvdial. Yes, its true.

Not only that, the usbserial module is built into kernel. This might sound advantageous in the first glance, but what about when you have to pass the module parameters into kernel, every time you boot?

I have found a work around for this problem, hope it helps people like me who are dependent on EDGE/GPRS. Here I'm going to describe the extra things that needs to be done to get things working. Details are in  my previus post, hope you don't mind. :)

First you need to open /boot/grub/menu.lst and add some parameters to the kernel. Find the line which is similar to this:

kernel /boot/vmlinuz-2.6.29.1-macbook root=UUID=c660e7d4-95e3-42a0-8967-8de554d76fb4 ro quiet splash


and change it to this:

kernel /boot/vmlinuz-2.6.29.1-macbook root=UUID=c660e7d4-95e3-42a0-8967-8de554d76fb4 ro quiet splash usbserial.vendor=0x05c6 usbserial.product=0x3197


The vendor and product sting can be found using lsusb command. Reboot the system.

Now comes the stupid part. Jaunty come with no wvdial, so you need to download the files manually. The files are

libuniconf4.4_4.4.1-0.2ubuntu2_i386.deb
libwvstreams4.4-base_4.4.1-0.2ubuntu2_i386.deb
libwvstreams4.4-extras_4.4.1-0.2ubuntu2_i386.deb
libxplc0.3.13_0.3.13-1build1_i386.deb
wvdial_1.60.1+nmu2_i386.deb


Try looking them up in http://archive.ubuntu.com/ubuntu/pool/. Once you download them put those downloaded files in the same folder, and then do a $sudo dpkg -i *.deb.

The rest of the process is pretty straightforward. You can find those here

Saturday, April 18, 2009

Subversion 101

This post covers the fundamentals of subversion, the popular version control system. I'll show how to use subversion to version-control linux-2.6.29.1 kernel on your local server (I had to set this up for easy management of my kernel assignment). But before we begin, let's get familiar with some of the subversion terms.


You do a check-out when you download some project from the server to your local machine, this creates the working copy for your subsequent modification.


You do a check-in or commit when you upload your changes to the server.


Self explanatory, update updates your code with the latest changes from the server.


Doing an export will download the latest update from the server, but without subversion metadata, so you cannot upload the changes to the server from here. This mode is generally used to make the distribution tars.


import will bring the data imported under subversion's control system.


Now that we know the basic, let's do some command line stuff. I'm assuming you have subversion installed in your system by now. In Ubuntu you can easily do this by sudo apt-get install subversion svnadmin.


First step is to create the repository, typically its created in /var/svn/. So issue the command svnadmin create /var/svn.


subversion can be run under several protocols, like svn:// or http://. But right now we don't want to bother with that, we'll use simple file:// for local access.


First we need to create several folders in our repository. trunk is the folder where we'll put or main code. We'll use branches and tags when we think that we need to create several fork of the same project. So,



svn mkdir file:///var/svn/trunk
svn mkdir file:///var/svn/branches
svn mkdir file:///var/svn/tags


Now import your linux source code into svn,

svn import ~/src/linux-2.6.29 file:///var/svn/trunk/linux-2.6.29.1 --message "Importing linux kernel 2.6.29.1"


Go to the folder where you want to create the working copy.

svn checkout file:///var/svn/trunk/linux-2.6.29.1


Edit files as needed. Then check status by,

svn status


Update code from server

svn update


Commit your changes

svn commit --message "Added some feature"


Create a branch

svn cp file:///var/svn/trunk/linux-2.6.29.1 file://var/svn/branches/linux-2.6.29.1-vanilla -m "Creating a branch for the vanilla kernel"


If you don't like command line, use rapidsvn. It has a really nice GUI. Also popular IDE like Netbeans and Eclipse has build in subversion support.

I'll try to cover using subversion over http in my next post.

Friday, April 17, 2009

Linux Kernel: Speed Up Compilation

I have been having a hard time for the last few days in doing my "Linux Kernel Assignment". No, programming is not the hardest part. Waiting for the kernel to compile is the main problem. Its pathetically the moment when you become the sitting duck. :). So here are some of the things I came across to speed up the compilation.
  1. Make sure you have ccache installed and configured. As the name suggests, ccache dramatically speeds up subsequent compilations by caching the object files. In most new Fedora distros, its enabled by default. You can check it by doing $which gcc. If it says something like /usr/lib/ccache/gcc, you are all set up. If not, try google, :)
    You can see see its status by issuing command $ccache -s. Its good to set the cache amount a bigger than default, so $ccache -M 2G will set the cache to 2GB which is enough for everyone.
  2. Use gcc option -pipe whenever possible. Self explanatory, it uses pipes instead of temporary files.
  3. If you have dual core or quad core processor and a lot of RAM, you'll notice that while compiling, only a fraction of your processor and RAM gets used. This is because there is only one compile process running at a time. You can specify multiple make jobs by passing parameter like this $make -j 2. Here, make will run at most 2 jobs at a time, taking full advantage of your multi-core CPU and RAM. Some argue on setting higher value, but I do not recommend it, as it likely to have negative effect because of the large amount of context switched in the CPU. People say that this approach leads to race conditions but I haven't bumped into any of that yet.
  4. For Linux Kernel compilation, make sure you have CONFIG_DEBUG_KERNEL unset in .config file. If set, this builds your kernel with debugging symbol making your kernel a lot bigger and significantly longer to build. It will make kernel debugging impossible though, but when you are in a hurry, this really makes more sense. Also strip the .config file from any unnecessary drivers/modules you don't require, but this will require you to have a thorough knowledge about both your PC and running kernel.
  5. For those with high speed network (ethernet or more), give a try to distcc, that distributes the compilation among multiple machine. The configuration is quite straight forward. Check the references section for details. 
  6. Try tmpfs. It enables you to mount any folder in your file system in RAM, so theoretically speeding up the compilation. Try mounting your build folder with tmpfs like this $sudo mount -t tmpfs -o size=500M,mode=0777 tmpfs /usr/src/linux-build.
Using the above mentioned method, you can get a clean build of 2.6.29 kernel in less than 15 minutes. Let me know if you have any problems or come up with any better solutions. :)

References:

https://help.ubuntu.com/community/Kernel/Compile

http://www.linux.org/docs/ldp/howto/Modules/speedup.html

http://en.gentoo-wiki.com/wiki/Portage_TMPDIR_on_tmpfs

http://www.ibm.com/developerworks/linux/library/l-distcc.html

Friday, April 03, 2009

Ubuntu: Easy kernel compile for newbies

Building a fully optimised kernel just for your pc is one of most exiting things to do. But manual kernel compilation not an easy task and occasionally things go haywire all the time. Luckily Ubuntu comes with some nifty tools to make it easier than ever. Here is a short how-to for the starters. Remember, these are the basic steps. I've skipped most of the exiting parts to make it as simple as possible.
  • Download the latest Kernel from http://kernel.org/. For e.g. linux-2.6.29.tar.bz2
  • Create a folder named src in your home folder and put the downloaded file in it.
  • Untar the file
      $tar xvjf ~/src/linux-2.6.29.tar.bz2
      $cd ~/src/linux-2.6.29
    Run the Following commands to install the necessary packages to build the kernel
      $sudo apt-get install build-essential fakeroot initramfs-tools libncurses5 libncurses5-dev
  • Create a base config file, we'll reuse the current kernel's config
      $cp -vi /boot/config-`uname -r` .config
  • Run this command, a graphical window will appear. Here you can make change to the kernel configs. Right now we won't change anything. So just run it and save.
      $make menuconfig
  • Do a cleaning in the source tree
      $make-kpkg clean
  • The real stuff, build the kernel (this will take a really long time, so grab a cup of cofee). I'll call this version 'paladin'.
      $fakeroot make-kpkg --initrd --append-to-version=-paladin kernel-image kernel-headers
  • Install the packages
      $sudo dpkg -i ~/src/*.deb
  • Do a reboot and enjoy the new kernel
  • If anything goes wrong (kernel panic !!!), you might need to take a socond look at the configurations.



Saturday, March 28, 2009

CityCell Zoom in Ubuntu

One of my friends just asked me on how to connect to Internet in Ubuntu/Linux using Citycell Zoom. Here goes the procedure.

Connect your zoom device/phone to your PC/laptops USB port.

Issue this command in the shell:

$lsusb


You shall get a list of devices:


Bus 005 Device 004: ID 05ac:8300 Apple, Inc. Built-in iSight (no firmware loaded)
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 003: ID 05ac:8205 Apple, Inc. Bluetooth HCI MacBookPro
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 002: ID 05ac:8240 Apple, Inc. IR Receiver [build-in]
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 004: ID 15ca:00c3 Textech International Ltd. Mini Optical Mouse
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 004: ID 05c6:3197 Qualcomm, Inc. CDMA Wireless Modem/Phone
Bus 001 Device 003: ID 05ac:021a Apple, Inc.
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub


Note the line:

Bus 001 Device 004: ID 05c6:3197 Qualcomm, Inc. CDMA Wireless Modem/Phone


The line has following meaninng.


Vendor ID: 05c6
Product ID: 3197


Now execute following command in shell


$sudo modprobe usbserial vendor=0x05c6 product=0x3197


Now back to good old wvdial

sudo wvdialconf


It shall now find a modem in /dev/ttyUSB0

Now open /etc/wvdial.conf and make sure you have the following lines in it.


Phone=#777
Username = waps
Password = waps
Stupid Mode = 1


Now run $sudo wvdial from your shell and you have net. :)

Happy Linuxing ...

Wednesday, March 04, 2009

Mozilla Weave: Bringing Desktop and Web Applications closer

"As the Web continues to evolve and more of our lives move online, we believe that Web browsers like Firefox can and should do more to broker rich experiences while increasing user control over their data and personal information.

One important area for exploration is the blending of the desktop and the Web through deeper integration of the browser with online services."


Just got my account activated @ Mozilla Weave Project.

Tuesday, March 03, 2009

Mac: Google Chrome on OS X

Yeah, like all Mac users I'm angry too. There is no mac version of Google's Chrome browser. Google has yet to release it for OS X, but here is the good news. Chrome's opens source version known as Chromium's source tree is open. So anyone can download it and try it out for themselves. Its far from finished, but you can at least get your hands dirty with Chromium in OS X.

Here are some links you'll need to get started.

http://code.google.com/p/chromium/wiki/MacBuildInstructionshttp://dev.chromium.org/developers/how-tos/get-the-code

Or if you are lazy like me, download the already built dmg image from this link.

Monday, March 02, 2009

Safari: Checking iPhone Version of Your Site Right from Safari

I must say, I'm greatly impressed at the new public beta release of Apples Safari's version 4. Tons of new features proved that its living up to expectations. Some notable features that caught my eye were, firebug like web inspector, new intuitive tabbed browsing, and some cosmetic features like cover flow like history browsing and opera like top site marker (Check out the screen-shot below)




To enable the debug menu, you need to enable it from the preferences tab.
You'll see a new "Develop" menu has appeared. Now to the thing what this post is all about. From the Develop menu, set your user agent to "Mobile Safari".


Now just go to any site that has special pages for iPhone, my Gmail Looks like this.


And this is my how my Facebook profile looks in iPhone.

And you never had to use an iPhone to check it out.

Friday, January 16, 2009

Tips: Download iPhone SDK with wget

Since its final release, I've been trying to download iPhone's SDK from Apple's website. The whopping 1.6 GB download seems a pretty daunting task in my unstable line, and some of apple's web sites features made it pretty impossible in no time.

My download failed three times after 50-70% completion, each time either I have manually paused the download for some reason or my connection was having some problem. And apples server did not give me any resume feature in any of the download  managers. Downloading 1.6 GB with Firefox's own download manger seemed an option, but what if Firefox crashes?

At last, I thought of giving good ol' wget a try and it didn't fail me. If you are in a similar situation like me, try this.
  1. Downlaod 'Export Cookie' addon for firefox from here
  2. Log into iPhone Developer Site using your apple developer account, if you don't have one, just register, its free.
  3. Export your cookie using the addon to a file named 'cookies.txt'
  4. Run this from the command line

    wget --limit-rate=5k --tries=inf -server-response --continue --load-cookies cookies.txt http://adcdownload.apple.com/iphone/iphone_sdk_for_iphone_os_2.2__9m2621__final/iphone_sdk_for_iphone_os_2.2_9m2621_final.dmg

Now you can pause and resume the download at your will. I have used --limit-rate=5k so that I can also browse without much overhead.

Wednesday, January 14, 2009

Javascript: Getting Current Window Dimension

Javascript function to get current window height and width. Works with most of the browsers.




// function to get the current width of the window
function getWidth(){
var x = 0;
if (self.innerHeight){
x = self.innerWidth;
}else if (document.documentElement && document.documentElement.clientHeight){
x = document.documentElement.clientWidth;
}else if (document.body){
x = document.body.clientWidth;
}
return x;
}

// function to get the current height of the window
function getHeight(){
var y = 0;
if (self.innerHeight){
y = self.innerHeight;
}else if (document.documentElement && document.documentElement.clientHeight){
y = document.documentElement.clientHeight;
}else if (document.body){
y = document.body.clientHeight;
}
return y;
}

Back to Blogging

After a failed attempt last year to get back to blogging, I'm trying it again this year. I really wanted to get back, but got busy will...