Friday 1 November 2013

Call a User Space Application from Kernel Space Module


Hello,

Calling a user space application from kernel space is called Upcalling, and the application which is being called is termed as User Mode Helper (I dont know where to put spaces in this word..:D). Don't be afraid, it very easy. So, here is how it goes:
The first statement is an array which takes your app's name, and the command line arguments. So if you want to provide any command line arguments, replace them with NULL. Second argument is the environment that the application needs to execute (just copy paste it !!!!!). And, the third statement actually calls the app. It's a function which take the application and it's environment as arguments.

Here is a sample source code.

EDIT : Here is a full term project based on this. :)

Hope it Helped. Thanks for Reading.
Hardik Madhu

Timer Interrupt in Linux Kernel


Hello,

So, all the stuff starts with this function,

Now, all the setup is to be done in this function. First in this function, a timer is initialized by:


Here exp_timer is a variable of struct timer_list structure. This structure all the necessary information about the timer. And we are going to use the following information in our application.
If you feel confused, dont worry. Here's the explanation. The first line contains a variable named jiffies. This variable is incremented by kernel every at a definite interval given HZ. Normally HZ is 100Hz, so jiffies is incremented every 10ms. So let's I want delay of 5mins. Then just give that value to variable delay, and give that time to the structure. Now, when the timer is expired, we want a function to be called. This so we need to provide the name of function to the structure. Here, the function being do_something.

Here is a link for sample program.

EDIT : Here is a full term project based on this. :)

Hope it helped. Thanks for reading.
Hardik Madhu

A C program in Linux to send an SMS using SIM900 GSM Modem


Hello,

This is a tutorial which will show you, how you can send a Text SMS to a mobile number using a GSM Modem. Here I used GSM Modem with SIM900 inside it. If you don't know what the hell this is... then follow the link for brief description SIM900 and here is the modem which I used SIM900 GMS Modem. Sending SMS is not big deal, but it can be tricky for beginners (like it was for me when I did this).


Modems like this one works on a command list called "AT Commands". You can find a AT command list very easily by Google-ing. I will describe only those commands which are necessary to send an SMS. But, other commands (like calling, or reading SMS) are very similar so after knowing this, you will be able to do anything you want with it.


The modem as shown in ebay link has an RS232 interface. So to connect it with my computer, I used an RS232 to USB converter. Now, let's start going through the implementation.


I am a Linux user. So, to send command to GSM Modem in C (considered as Writing to a Device in Linux), you need to open it with permission to Write in it. Done by:




Okay, several things for a Linux newbie : open is a system call which opens any file in C program to read from it or write in it. The string in double quotes is not gibberish :D. When you attach any device to a Linux system, to communicate with it, Linux creates a specific file. That file is located in /dev directory. In this case, the name of file created is ttyUSB0. Now, after specifying the file to be opened, it's time to specify permissions. O_RDWR indicates that the file should be opened with Read/Write permissions. This open function returns a variable called "File Descriptor", and that will be used throughout the program to access this file.


Now, to make sure that connections are all right and devices are powered on and other stuff like that, write simple "AT" to the device. Can be done like this:




This is another system call, which writes to the already opened device, which is described by fd. Here, it first writes "AT" and then gives a carriage return, denoted by '\r'. Carriage specifies end of the command.




Here it tries to read 5 bytes from the Modem. There is something called an "Echo Mode" in modem, in this mode whatever you send in it, it will send back (and also appropriate reply to command will also be sent by the Modem). If everything is all right, it will reply with "OK". If you don't get this output, go and check things like Buad Rate, Power Levels etc.


After getting an OK signal from Modem, first thing we need to do it is set it in text mode. This is done by command "AT+CMFG=1". Done by:




Now, specify the mobile number (to which you want to send SMS).



This command will give you a prompt to write the content of SMS. In this prompt, you need to write the content and then press CTRL+Z to terminate it. At this termination entered text will be sent as SMS to the specified number. This can be done by:




'\x1A' is a backslash character which is CTRL+Z, which terminates the prompt, and sends entered text. So, our SMS is now sent. 

Here is the source code.



EDIT : Here is a full term project based on this. :)

I hope it was helpful. Thanks for reading. Leave comments.
Hardik Madhu

Generate True and Unique Random Number in a C Program


Hello,
           
Have you ever tried to generate a random number in C Program?
           
And face the problem that it gives same iterative numbers every time you run the program?

Now, let's say you need to generate a password which is different every time. How would you do it?

For example, a program like this:
would generate output like:


But there is a problem here (or the solution), to generate a "truly" random number, we need to provide unique seed to that function.

Here the question is: "What can be so unique in our world, that it will never be repeated?" And the answer is "TIME". So, current system time is provided as seed to generate truly random numbers. Code snippet:


After adding this, here is the snapshot of this program:

So, here you generated a unique random number.

Thanks for reading. Leave Comments.

EDIT : Here is a full term project based on this. :)

Hardik Madhu

Friday 27 September 2013

Motion Detection and Speed Estimation using OpenCV

Hello Reader,

                Action Detection and Speed Estimation, this is what I wanted to do. I started making this bit in my Undergraduate final year, as a part of the curriculum. At that time I had no idea of either Computer Vision or anything related to that. I researched for quite a long time (on the Internet, of course). But all the methods that I found, were very complex and computationally very expensive. What I wanted was a very minimal system which can work "Real-Time" on very less computationally powerful devices. The device that I was targeting at that time was Raspberry Pi (a recent release at that time).

               So, finally after months of research (I had sufficient knowledge of the field, and) putting everything aside I came up with a very simple solution (and very idiotic). Estimating and tracking speed of the action was always my first goal. So keeping everything aside, I divided speed of action in two parts, "Fast" and "Not Fast".  In detecting these two things I took advantage of the capturing limit of the camera (640 * 480, 25fps). When there is a motion above some level, these kind of cameras are not able to capture and digitize it successfully.

               Considering this camera limit, lets go to the steps which I took:
  1. Capture new frame and convert it to gray-scale (almost a first step everywhere)
  2. Subtract previous frame from the current frame (or we can say, calculate absolute difference between previous image and current image) (yeah, this is what we do to detect motion)
  3. Convert the difference image (which is gray-scale right now) to binary image using appropriate threshold (this can simplify the computation here onwards)
  4. Find contours of the difference image (okay, this will help us know the boundaries of things in motion)
  5. Estimate speed based on number of detected contours (you must be thinking : "HUH??")
                Okay then, let's fill in the void and try to understand the last step. Consider these sequence of images, in which the hand is moving very fast:






                   In the first image the hand is steady, and just about to start moving. Number of contours is 19. In second image, hand has started moving at a very fast speed. So, the camera is not able to capture it and digitize it properly. So number of contours we get is 9. Same thing is going to happen in next three images, number of contours found is 12, 10 and 9 respectively. Now, in the last image, hand has finished one cycle and has gone upwards again. Here, number of contours that we are detecting is 19.

                   These kind of events are the main base of the algorithm which estimates the speed of motion. Algorithm works this way:
  1. Wait for detection of contours more than a predefined maximum contour threshold
  2. Now, scan consecutive frames for number of contours
  3. If number of contours is less than a minimum contour threshold, keep scanning
  4. While scanning if the number of contours exceed the predefined maximum contour threshold, denote this event as a fast motion
                   Here is the source code for this application.

                   I hope you liked and enjoyed the post.

                   I am still researching for ways to detect Human Action Recognition. So all suggestions are most most most welcome.

                   Please leave comments.



EDIT : Here is a full term project based on this. :)

Thank You For Reading,
Hardik Madhu