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