Mar 28 2008

When it comes to VxWorks…..

I did some experiments with VxWorks last week… using it’s Wind River workbench. I have managed to run the helloworld application in the vxsim….. who knows the futre…. So just another learning experience….

My first impression about the Wind River workbench is that it is a highly customised Eclipse… You will get the feel of eclipse from the very begining… So far so good…

Now into the helloworld application…. It will print Hello World 1000000 times, well if you want to … of course with a simple delay.

hello.c in VxWorks..

#include <vxworks.h>
#include <taskLib.h>
#include <sysLib.h>
#include <stdio.h>
int main(){
   
    taskExit(0);
}

int fun1(void)
{
    int i;
    for (i=0;i<1000000;i++)
    {
    taskDelay(sysClkRateGet());
    printf(“Hello World!\n”);
   
    }
    return 0;
}

 Now we can run this VxWorks 6.6 Real Time Process in the emulator..

Hmm.. It just works….


Mar 27 2008

Program to run multiple threads in a process to have different Effective UIDs (uid).

Most of the times, multi threaded programs poses multiple threats to the programmer. There was one question whether the multiple threads in a program can run with different effective uids or gids( run as a different unprivileged user)..  My solution to this program (though not so  brilliant….. ) is by using seteuid(), pthread_mutex_lock() and pthread_mutex_unlock().

resuid.c

/***************************************************************/
/*      Program to run different threads in different uids     */
/*    Compile it as : cc resuid.c -lpthread -o resuid        */
/***************************************************************/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex;

void *
fun1(void *arg)
{
    uid_t fun1uid,fun1uid_orig;
    int retfun1;
   
    (void) pthread_mutex_lock(&mutex);
    fun1uid_orig= geteuid();
    retfun1= seteuid(500);
   
    if(retfun1){
    printf(“fun1 seteuid failed \n”);
    }

    fun1uid= geteuid();
    printf(“In fun1 thread \n”);
    printf(“Fun1 uid is %d\n”,fun1uid);
    seteuid(fun1uid_orig);

    (void) pthread_mutex_unlock(&mutex);
}

void *
fun2(void *arg)
{
    uid_t fun2uid, fun2uid_orig;
    int retfun2;
    (void) pthread_mutex_lock(&mutex);
    fun2uid_orig = geteuid();
    retfun2= seteuid(502);

    if(retfun2){
    printf(“fun2 seteuid failed \n”);
    }

    fun2uid= geteuid();
    printf(“In fun2 thread \n”);
    printf(“Fun2 uid is %d\n”,fun2uid);
    seteuid(fun2uid_orig);

    (void)pthread_mutex_unlock(&mutex);
}

int main(void)
{
    pthread_t t1,t2;
    uid_t myuid;
    int retmain;

    (void)pthread_mutex_init(&mutex, NULL);
    (void)pthread_create(&t1,NULL, fun1,NULL);
    (void)pthread_create(&t2,NULL, fun2,NULL);
    (void)pthread_join(t1,NULL);
    (void)pthread_join(t2,NULL);
    (void)pthread_mutex_destroy(&mutex);

    printf(“In the main function\n”);
    myuid= geteuid();
    printf(“UID of main is %d\n”,myuid);
    return 0;
}
 


Mar 19 2008

Get Bluetooth RSSI values using Python

The Received Signal Strength Indicator (RSSI ) is a measurement of the power present in a received bluetooth signal. The end-user can observe the RSSI value when measuring the signal strength of received bluetooth signal.

Some interesting Projects like BlueProximity (http://www.gnomefiles.org/app.php/BlueProximity) uses RSSI value at it’s core. 
This software helps you add a little more security to your desktop by detecting your mobile phone(bluetooth enabled) , and keeping track of its distance. If you move away from your computer and the distance is above a certain level (no measurement in meters is possible) for a given time, it automatically locks your desktop.

But even in that project, they doen’t measure RSSI using Python. Instead, that project depends on the hcitool to measure RSSI . The developer of Blueproximity (Lars) posted the same issue in Ubuntu Forum (http://ubuntuforums.org/showthread.php?t=528701). That’s how I decided to spend some time for it Embarassed

My first investigation was what the Perl guys do to measure RSSI. I got the answer from http://perl.jonallen.info/pub/Main/BluetoothProximityDetection/xscreensaver.pl . 
  Yes, They depend on C to measure the RSSI using inline C code in Perl. So I decided to move in the same
direction.

As far as I know, Swig is the best method to integrate C with Python (http://linuxgazette.net/issue49/pramode.html).  So , I decided to move in that direction. I have modified the C code in hcitool by Maxim Krasnyansky <maxk@qualcomm.com> and Integrated it with Python.

enerating the swig wrapper for bluessid.c

swig -python -module bluessid bluessid.c

gcc -I /usr/include/python2.4 -c bluessid.c bluessid_wrap.c

ld -shared -o _bluessid.so bluessid.o bluessid_wrap.o -lbluetooth

This will create the _bluessid.so in the present working directory.
Invoke python

import bluessid
help(bluessid)

The completed source code of Pyrssi is available at  

http://pysportslive.googlecode.com/svn/trunk/pyrssi/

Warning: Due to the non availabity of Bluetooth module, I haven’t tested this code. It is in Beta stage . So please dont complain if it cause an explosion in your PC or fried your bluetooth adapter Innocent 


Mar 12 2008

Creating your own themes for Sony Ericsson K300i in Linux

First of all, Get some funny tux pictures from http://tux.crystalxp.net/en.cat.16.3.12.html.

Now get the tux.thm(or any other theme) from http://www.esato.com/logos/colour/k300themes.php?p=42

Ok , let’s check the theme file.

file tux.thm
tux.thm: POSIX tar archive
Oh.. It’s just a tar file ..Now Let’s untar it:

tar xvf tux.thm

background.png highlightSmall.png softkeys.png tabUnselected.png
desktop.png popupHighlight.png standby.png Theme.xml
doubleTitle.png popup.png status.png titleSmall.png
highlight.png popupTitle.png tabSelected.png

These files has a pre-defined sizes . It will be better to keep the size of
each image files as it will look good in the mobile.

Let’s peek into the Theme.xml file and see it’s structure.

<?xml version=”1.0″?>

<Sony_Ericsson_theme version=”3.0″>

<Author_firstname value=”Maxin”/>

<Author_url value=”153EE6″/>

<Background Color=”0xbbf7fb”/>

<Background_image Source=”background.png”/>

<Desktop Color=”0x153ee6″/>

<Desktop_image Source=”desktop.png”/>

<Desktop_title_text Color=”0xbbf7fb”/>

<Highlight_text Color=”0xbbf7fb”/>

<Highlight Color=”0x153ee6″/>

<Highlight_image Source=”highlight.png”/>

<Highlight_small_image Source=”highlightSmall.png”/>

<Popup_text Color=”0xbbf7fb”/>

<Popup Color=”0x153ee6″/>

<Popup_frame Color=”0xbbf7fb”/>

<Popup_highlight_text Color=”0×000040″/>

<Popup_highlight Color=”0xbbf7fb”/>

<Popup_highlight_image Source=”popupHighlight.png”/>

<Popup_image Source=”popup.png”/>

<Popup_scrollbar_background Color=”0x3cacff”/>

<Popup_scrollbar_slider Color=”0xbbf7fb”/>

<Popup_title_text Color=”0×000040″/>

<Popup_title Color=”0xbbf7fb”/>

<Popup_title_image Source=”popupTitle.png”/>

<Scrollbar_background Color=”0xbbf7fb”/>

<Scrollbar_slider Color=”0x0018b1″/>

<Softkeys_text Color=”0xbbf7fb”/>

<Softkeys Color=”0x153ee6″/>

<Softkeys_image Source=”softkeys.png”/>

<Standby_image Source=”standby.png”/>

<Standby_operatorname_text Color=”0xffffff”/>

<Standby_operatorname_outline Color=”0×000040″/>

<Standby_softkey_image Source=”softkeys.png”/>

<Standby_time Color=”0xffffff”/>

<Standby_time_outline Color=”0×000040″/>

<Standby_statusbar_image Source=”status.png”/>

<Tab_text Color=”0xbbf7fb”/>

<Tab Color=”0x153ee6″/>

<Tab_image Source=”doubleTitle.png”/>

<Tab_selected_image Source=”tabSelected.png”/>

<Tab_unselected_image Source=”tabUnselected.png”/>

<Text Color=”0×000040″/>

<Title_image Source=”doubleTitle.png”/>

<Title_small_image Source=”titleSmall.png”/>

<Title_text Color=”0xbbf7fb”/>

<Title Color=”0x153ee6″/>

<Wapbrowser_underline Color=”0x0000ff”/>

<Wapbrowser_tableborder Color=”0x0000ff”/>

</Sony_Ericsson_theme>

If you need to rename some of your files, then never forget to make change in this XML file too.. Otherwise, just replace the required files and don’t touch the XML file.. that’s safe if you are new to XML format :)

Now open the png image in GIMP and resize the image as that of the equivalent image in the theme.

Here the desktop.png file should be of size 128×110. You can resize your image in gimp by..

Image -> Scale Image ->
Then give Width as 128 and Height as 110

or just use the convert file if you want to write a shell script for the theme creation

convert -resize 128×110 source.png destination.png

This will take care of your resizing requirements :)

After that save that image as desktop.png .. Go on modifying these files till
you are satisfied.

Now create your theme by issuing this command.

tar cvf tux.thm *

Now let’s transfer the theme to K300i using the USB cable connected to our Gnu/Linux Machine.

obexftp –tty /dev/ttyUSB0 -p ./tux.thm Themes/tux.thm
Connecting…done
Sending “./tux.thm”…|done
Sending “Themes/tux.thm”… failed: Themes/tux.thm
(Don’t care the failed message.. )

Disconnecting…done

Save the theme in your mobile and apply it.. Now you have the cool tux theme in your mobile.


Mar 4 2008

Sending Linus.mp3 to K300i from Linux – The obexftp way

One of the most difficult things that I faced when connecting my Sony Ericsson K300i to the Gnu/Linux box was the difficulty in sending and receiving data from Linux to Mobile and vice versa. Today, I have managed to overcome that difficulty too.. Now my mobile is completely useful… (Atleast from the Gnu/Linux side :) )

First I need the obex installation in my system (Fedora core 6)
For that
yum install openobex

After the installation, connect the mobile phone using usb cable. Now dmesg shows that we have a new device called ‘/dev/ttyUSB0′.

Here are the following steps:

obexftp –tty /dev/ttyUSB0 -c . -l

This gives the following output..

Connecting…done
Sending “.”… failed: .
Receiving “(null)”… <?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE folder-listing SYSTEM “obex-folder-listing.dtd”>
<!–
XML Coder, May 24 2005, 21:06:32, (C) 2001 Sony Ericsson Mobile Communications AB
–>
<folder-listing version=”1.0″><folder name=”Pictures”/>
<folder name=”Sounds”/>
<folder name=”Themes”/>
<folder name=”Videos”/>
<folder name=”Other”/>
</folder-listing>
done
Disconnecting…done

Then download the Linus torvald’s voice

wget http://www.paul.sladen.org/pronunciation/torvalds-says-linux.mp3

Now push the mp3 to the mobile phone

obexftp –tty /dev/ttyUSB0 -p ./torvalds-says-linux.mp3 Sounds/linux.mp3

After about 5 seconds, your phone will prompt you to save the mp3 to phone memory. Now play the torvalds-says-linux.mp3
 
Now you have the voice of Linus in your mobile….. Cheers.. Wink


Mar 3 2008

You got a call.. with Python OSD and Sony Ericsson K300i

Consider the situation where you have put your mobile in silent mode and working on your PC. Most of the times,
you will not be able to attend the calls simply because you won’t care the buzzing of the mobile phone… 

This Python program will Show an On Screen Display telling ” You have a call” to divert our attention to the mobile phone. Only thing is that you should connect your Mobile with your PC using the USB cable 
provided with your mobile ..  or if you are a bit adventurous, try to connect your phone using bluetooth using the
bluetooth dongle.. ( /dev/rfcomm0 instead of /dev/ttyUSB0)..

For Bluetooth ….

###################
hcitool scan
Scanning …
11:22:33:44:55:66

Then modify the /etc/bluetooth/rfcomm.conf file
#
# RFCOMM configuration file.
#

rfcomm0 {
# Automatically bind the device at startup
bind no;

# Bluetooth address of the device
device 11:22:33:44:55:66;

# RFCOMM channel for the connection
channel 1;

# Description of the connection
comment “Example Bluetooth device”;
}

Then give this command

rfcomm bind /dev/rfcomm0 

after running the below given python program, release the device

rfcomm release /dev/rfcomm0
###################

Then try this python program

######################################################
# mobilecall.py #
# Python program which displays the information when #
# somebody calls your mobile (here my K300i) which is #
# plugged to my pc using the usb cable. #
# ################################################### #
# Technical Details: #
# The usb device is shown as ttyUSB0 in the Linux PC #
# The serial module can be used to read and write from#
# that device. PyOSD is used to diplay the information#
# on the screen. #
# If you want, you can use the pygame module to play #
# music in your PC to inform you about the call .. #
# or dbus module to mute other music players #
# #
# Technically , this is the meanest way(or simplest) #
# to achieve this using python #
######################################################

import serial
import time
import pyosd
#import pygame

def mobilecall():
try:
# /dev/rfcomm0 in the case you are using bluetooth connectivity :)
s= serial.Serial(‘/dev/ttyUSB0′,9600)
# This will wait till some string is pushed from phone.
# Highest probable event is ‘RING’ indication from phone
# when somebody calls you.
data = s.readline()

# PyOSD initialization and other settings..
displayer = pyosd.osd()
displayer.set_colour(‘red’)
displayer.set_pos(0)
displayer.set_timeout(10)
displayer.display(‘You have a call . Please pick up your mobile’)

# Uncomment this if you want to play music on receiving a call
#pygame.mixer.init()
#indi_music = pygame.mixer.Sound(‘/usr/share/sounds/KDE_Startup.wav’)
#pygame.mixer.Sound.play(indi_music)

 time.sleep(10)
except:
   print ‘Failed to open Port’

# Pythonic way to invoke the function
if __name__== ‘__main__’:
   mobilecall()

###############################################################

And here goes the output:


Get Adobe Flash playerPlugin by wpburn.com wordpress themes