kernel – passing module parameters

With normal programs that allow you pass parameters on there command line, for example

printoutmyname genux

where the program is called printoutmyname and the first parameter is genux (it is normal that the first parameter [0] is actually the program name and the second parameter [1] is the parameter that is passed)

Well in Linux kernel (where here is a example on how to compile the kernel for a ubuntu based setup) you can pass parameters to the modules that are getting loaded. One module would be your graphics card, in my case a nvidia graphics card.

To find out what parameters can be passed to the graphics card you will have to find the kernel module file (filename.ko, where the ko is the kernel object file), so to search for the nvidia.ko in my case I did

locate nvidia.ko

and then changed to that directory and did a module information on it

cd /lib/modules/2.6.31-17-generic/updates/dkms/
ls
nvidia.ko  vboxdrv.ko  vboxnetadp.ko  vboxnetflt.ko

and doing a module information on it you call the modinfo on the kernel object file as

modinfo nvidia.ko
</pre
 
my output was
 
<pre lang="bash">
filename:       nvidia.ko
license:        NVIDIA
alias:          char-major-195-*
alias:          pci:v000010DEd*sv*sd*bc03sc02i00*
alias:          pci:v000010DEd*sv*sd*bc03sc00i00*
depends:
vermagic:       2.6.31-17-generic SMP mod_unload modversions
parm:           NVreg_EnableVia4x:int
parm:           NVreg_EnableALiAGP:int
parm:           NVreg_ReqAGPRate:int
parm:           NVreg_EnableAGPSBA:int
parm:           NVreg_EnableAGPFW:int
parm:           NVreg_Mobile:int
parm:           NVreg_ResmanDebugLevel:int
parm:           NVreg_RmLogonRC:int
parm:           NVreg_ModifyDeviceFiles:int
parm:           NVreg_DeviceFileUID:int
parm:           NVreg_DeviceFileGID:int
parm:           NVreg_DeviceFileMode:int
parm:           NVreg_RemapLimit:int
parm:           NVreg_UpdateMemoryTypes:int
parm:           NVreg_UseVBios:int
parm:           NVreg_RMEdgeIntrCheck:int
parm:           NVreg_UsePageAttributeTable:int
parm:           NVreg_EnableMSI:int
parm:           NVreg_MapRegistersEarly:int
parm:           NVreg_RmNvclkIdleGraphics:int
parm:           NVreg_RegistryDwords:charp
parm:           NVreg_NvAGP:int

where the parm: is a parameter to pass to the module on load (insmod, or loaded via the kernel at boot time which you can force to load via the /etc/modules file and the parameters can be placed in the /etc/modprobe.d directory).

for example to load the nvidia module with a parameter NVreg_NvAGP you would do something like

insmod nvidia.ko NVreg_NvAGP=1

and the passing value is 1 to the NVreg_NvAGP parameter

operator – comparisons

Comparisons between classes are different compared to local variables e.g. int(eger), float, doubles.. because the comparisons aspects of the standard variables have already been done e.g.

int a=2;
int b =3;
if (a==b)....

the comparison being the “==”, within your own classes if you want to test against a another object that is of the same class you will have to write a comparison function that will either return true or false, of course there are other comparisons <,>, <=, >= etc and also +,- which can be implemented if you wanted to.

The basics of the comparison operator is

bool operator == (const yourclassname &tester)

where yourclassname is what you have called your class, of course you can run comparisons with other classes and also standard variables like floats, but you will need to write a separate one for each. The operator is the keyword, and it returns a bool(ean) result which is either true/false, so once you have done a comparison within this function you just return either true or false and the code below will still compile

classA a;
classA b;
if (a==b) ..

below is code that you can compile to demonstrate operator keyword abit more.

#include <iostream>
 
using namespace std;
 
class classA
{
  public : 
    int x;
 
  // of course you cannot alter the testing class so const 
  // the tester is the right hand side of the boolean test e.g.
  // if (A == B ) . A = this class and B = tester
  bool operator == (const classA &tester)
  {
      if (x == tester.x) 
	return true; 
      else 
	return false;
  };
};
 
int main()
{
    classA a;
    classA b;
    a.x = 0;
    b.x = 0;
 
    if (a==b)
      cout << "the same" << endl;
    else
      cout << "not the same" << endl;
 
    b.x = 1;
 
    if (a==b)
      cout << "the same" << endl;
    else
      cout << "not the same" << endl;
 
    return 0;
}

and the output would be

the same
not the same

QT – hello world – signals and slots

QT framework is a great application stack that allows for cross development (between different OS’s) without having to re-do the code for e.g.Linux/Windows different setups. It is similar to Java and C Sharp (C#) in that way, but the difference with them is that they compile into a native object which can then run within a virtual machine where as the QT framework is more designed to be compiled within each setup, which should make it quicker as well because it will be in the native code and not running in a virtual machine.

The nice thing about QT is that it has its own SIGNAL and SLOTS, similar to C Sharp (C#) events process where you can link something happening to when something else has just happened (e.g. moved a value on a slider bar and a integer value alters as well).

I shall go into more detail with SIGNAL’s and SLOT’s in the next tutorial for QT development, but in essence they are defined within the class of the object itself and then you link them with a “connect” syntax. e.g.

    object::connect(object_to_send_signal, SIGNAL(signal_of_the_object_that_will_be_emit),
                        object_to_pick_up_signal, SLOT(function_to_call_once_signal_has_been_emitted));

so you will link/connect a objects that sends a signal to a slot that receives the signal.

Here is a basic GUI for a QT application that will have a button that says “QUIT” and also a signal that is emitted once that button class clicked() has happened, that links to the application (QApplication object) that has a “quit” function that stops the program from executing.

To start with we first include the basic header files and then create a QApplication (the gui as such) and then a QPushButton object with the default text being “Quit”.

    QApplication app(argc, argv);
    QPushButton *button = new QPushButton("Quit");

once this has happened we then create the connection between the clicking of the button to the application “quit”ing function.

    QObject::connect(button, SIGNAL(clicked()),
                        &app, SLOT(quit()));

we have only created the button but not placed it on the screen, so we use the show method within the button class and then run the application execution function to actually run the GUI program

    button->show();
    return app.exec();

if you save this file within a directory called quit and call this file quit.cpp

#include <QApplication>
#include <QPushButton>
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton *button = new QPushButton("Quit");
    QObject::connect(button, SIGNAL(clicked()),
                        &app, SLOT(quit()));
    button->show();
    return app.exec();
}

then change to that directory within your console/command line and then we need to build up the QT compiling project files.

qmake -project

this will make the quit.pro file within that same directory (it uses the same name as the directory name), and within that file is

######################################################################
# Automatically generated by qmake (2.01a) Wed Feb 24 11:49:39 2010
######################################################################
 
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
 
# Input
SOURCES += quit.cpp

which basically means that the application uses the source files of quit.cpp, you then need to create the Makefile (which is the actual compiling c++ file that g++ uses to create the application)

qmake

so the directory will be something similar to

Makefile  
quit.cpp
quit.pro

within Linux you can just now run the make command which will make the application “quit” but without windows you would need to have the nmake command instead, so run either make or nmake and within the directory now will be a quit application that you can run and the output would be a simple GUI with a button on it that says “Quit” and once you click that button the application will quit.

Equals – why ? and why not ==

Object oriented programming allows for objects to be created on the fly with the keyword “new”. The new will create a new object on the heap memory space and then give that memory location to the variable name, for example.

class NewClass 
{
   private int x;
} ....
 
NewClass a = new NewClass();
NewClass b = new NewClass();

What is happening in memory is something like below, the a variable has a small space and so does b, because they are just “pointers” to the actual memory locations of the class that was created.

Variable Value
a 0x0102
b 0x0110

where as the memory on the heap space would actually hold the NewClass details for example.

Memory location Value
0x0102 NewClass – overhead details
0x0104 NewClass – x value
…. ….
0x0110 NewClass – overhead details
0x0112 NewClass – x value

So the actual value of a and b are just values within memory, this could give reason why the “==” (equals) does not work e.g.

if (a==b)

even if both of them have the same internal values of the NewClass x value, they are still actual pointing to different locations (which makes sense otherwise you are comparing the same object which would be silly). There is a few functions to do the comparsions of objects, Equals, Compare for example and if you implement these functions within your class you can then “compare” the two objects instead of the memory locations that are held within the variables (a 0x0102 and b 0x0110)

To implement a Equal function it would be something similar to

	public boolean Equals(Object obj)
	{
		if (x == ((ClassA)obj).xValue())
			return true;
                return false;
	}

where the obj (Object) passed is the same type as the Class e.g. NewClass and return true if they are same (after casting the object obj value to a ClassA structure).

Here is a bigger example with code for two java files, if you save this class as normal for java to have it the same as the class name e.g. ClassA.java

public class ClassA {
	private int x;
 
	public ClassA() { x =0;}
	public ClassA(int value) { x = value;}
 
	public int xValue() { return x; }
	public void setX (int value) { x = value;}
 
	public boolean Equals(Object obj)
	{
		if (x == ((ClassA)obj).xValue())
			return true;
		return false;
	}
 
	public int Compare(Object obj)
	{
		ClassA objA = (ClassA)obj;
		if (x > objA.xValue())
			return 1;
		else if (x < objA.xValue())
			return -1;
		else
			return 0;
	}
}

this file also includes the Compare function method, so the returns have to be normal to java comparison e.g. 1 means > (greater than) , 0 = same, -1 < (less than), it is up to you to implement to make sure that this kinder of comparison adhears to how you want the class is greater than another class, could be a X value as above, or could that you call the first class number 1 and the second of that type of class 2.. it is up to you. and then save this as Test.java

public class Test {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ClassA a = new ClassA();
		a.setX(10);
		ClassA a2 = new ClassA();
		a2.setX(10);
		if (a==a2)
			System.out.println("Yep the same");
		else
			System.out.println("nope not the same");
 
		if (a.Equals(a2))
			System.out.println("Yep the same");
		else
			System.out.println("nope not the same");
		a2.setX(11);
		int comparsion = a.Compare(a2);
		if (comparsion == 0)
			System.out.println("Same");
		else if (comparsion == 1)
			System.out.println("greater");
		else	// only -1 left.
			System.out.println("less than");
 
	}
 
}

compile them you just need to javac Test.java and that will compile the ClassA.java file as well, then to run

java Test
 
nope not the same
Yep the same
less than

Events – fire that event

An event will be fired when something has happened and then you can link to that event and do something else if you wanted to. To start of with you have to link a event with a delegate function which will be the event handler.

To setup a delegate you can have the object and also any event arguments ( you can add in more if you want to, but these are the basics to include)

	// setup a delegate for the event to fire with.
	public delegate void ChangedEventHandler(object sender, EventArgs e);

here is the class that will actual fire the event, to start with I link the “event” to a virtual method as such called Changed (with the delegate from above ChangedEventHander).

	// this is the MyEvent class that will create the event fired.
	public class MyEvent 
	{
		private int x;
 
		// the event for this internal class
		public event ChangedEventHandler Changed;

here is the event firing function, this will call the Changed “virtual” method as such when ever you call this function.

		protected virtual void ThisHasChanged(EventArgs e)
		{
			// try and call the delegate ChangedEventHandler
			// make sure that there is something to call for the event to fire.
			if (Changed != null)
				Changed(this, e);
		}

and here is the get/set for the internal X variable and in the set I call the ThisHasChanged function above.

		// call the ThisHasChanged event when you set the internal value of X.
		public int xValue
		{
			get { return x;}
			set { x = value;ThisHasChanged(EventArgs.Empty); }
		}
	}

So that is the event firing, but you will need to have a listener that will listen to any events and call something that will deal with the fired event, so here is the listener class.

So the listener, is just like any other class, but you will need to have a internal reference to the class above so that you can link to the event fired, so here is the MyEvent internal class for the listener.

	// the listener for the event firing
	class EventListener
	{
		// MyEvent class
		private MyEvent ThisIsMyEvent;

and then the constructor for the EventListener, you pass in the MyEvent class that you want to “link” (the one that you will create within the running part of the program). and from within the MyEvent, there was a “virtual” method as such called “Changed”, well now we can create a link so that when that event “Changed” has been called from within the MyEvent Class I can link to a function within this class EventListener with using a new delegate ChangedEventHandler and the ValueHadChanged is within the EventListener class.

		// the class constructor and you need to pass the MyEvent class to link to the listener.
		public EventListener(MyEvent SetUpTheEventLink)
		{
			// Create a link to the MyEvent class passed in to the internal MyEvent Class
			ThisIsMyEvent = SetUpTheEventLink;
			// this is the link to the listern function.
			ThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange);
		}

here is the ValueHadChange, same setup as the delegate parameters since we are basically passing event details between them both and can pass in more details is you want to, but you really only want to have the object (the MyEvent class) and any event arguments e.g. errors etc. at present it just says , “Oh yeah.. I have altered”.

		// same style as the delegate e.g. object, eventargs, that is sent from the event fired.
		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 
		private void ValueHadChange(object sender, EventArgs e)
		{
			Console.WriteLine("Oh yeah.. I have been altered");
		}

since there was a Changed link attached to the above function, to clean up you can detach the link with using something similar to the constructor of this class, but instead of using += (to add) you just need to subtract from the Change virtual method -=

		// detach the listener function from the event MyEvent 
		public void Detach()
		{
			// take the event linked from the MyEvent class.
			ThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange);
			ThisIsMyEvent = null;
		}
	}

Here is the full code

using System;
 
// when alter a number in the internal part of the class.
namespace codingfriendsEvents
{
	// setup a delegate for the event to fire with.
	public delegate void ChangedEventHandler(object sender, EventArgs e);
 
	// this is the MyEvent class that will create the event fired.
	public class MyEvent 
	{
		// the event for this internal class
		public event ChangedEventHandler Changed;
 
		private int x;
 
		protected virtual void ThisHasChanged(EventArgs e)
		{
			// try and call the delegate ChangedEventHandler
			// make sure that there is something to call for the event to fire.
			if (Changed != null)
				Changed(this, e);
		}
 
		// call the ThisHasChanged event when you set the internal value of X.
		public int xValue
		{
			get { return x;}
			set { x = value;ThisHasChanged(EventArgs.Empty); }
		}
	}
 
	// the listener for the event firing
	class EventListener
	{
		// MyEvent class
		private MyEvent ThisIsMyEvent;
 
		// same style as the delegate e.g. object, eventargs, that is sent from the event fired.
		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 
		private void ValueHadChange(object sender, EventArgs e)
		{
			Console.WriteLine("Oh yeah.. I have been altered");
		}
 
		// the class constructor and you need to pass the MyEvent class to link to the listener.
		public EventListener(MyEvent SetUpTheEventLink)
		{
			// Create a link to the MyEvent class passed in to the internal MyEvent Class
			ThisIsMyEvent = SetUpTheEventLink;
			// this is the link to the listern function.
			ThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange);
		}
 
		// detach the listener function from the event MyEvent 
		public void Detach()
		{
			// take the event linked from the MyEvent class.
			ThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange);
			ThisIsMyEvent = null;
		}
	}
 
	class TestMyEvent
	{
		public static void Main()
		{
			// create a new MyEvent class
			MyEvent MyEventToCheck = new MyEvent();
 
			// link in with the EventLister , passing in the class of MyEvent
			EventListener TheListener = new EventListener(MyEventToCheck);
 
			// should fire of a event !! because I am changing the internal x value.
			MyEventToCheck.xValue = 10;
 
			// will not fire a event because I have not changed it.
			Console.WriteLine("X = " + MyEventToCheck.xValue);
 
			// will fire of another event.
			MyEventToCheck.xValue = 5;
 
			TheListener.Detach();
		}
	}
}

and here would be the output

Oh yeah.. I have been altered
X = 10
Oh yeah.. I have been altered

If you want to gain access to the object sender within the EventListener class to the function that was listening to the event fired, if you alter it as below, but also since because the MyEvent was also linked within the constructor you also have that link as well.

		// same style as the delegate e.g. object, eventargs, that is sent from the event fired.
		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 
		private void ValueHadChange(object sender, EventArgs e)
		{
			Console.WriteLine("Oh yeah.. I have been altered");
			Console.WriteLine("New value of X = " + ((MyEvent)sender).xValue);
			Console.WriteLine("Internal MyEvent class x = " + ThisIsMyEvent.xValue);
		}

then you can gain access to the MyEvent from within the object sender and the output would be

Oh yeah.. I have been altered
New value of X = 10
Internal MyEvent class x = 10
X = 10
Oh yeah.. I have been altered
New value of X = 5
Internal MyEvent class x = 5

enum – what are they

A enum type is basically a array on constant elements that you can assign to words a integer value.

To give a example, lets say that you have a different type of car

enum cars { VAUX, FORD, LAND };

which in turn mean that VAX = 0 and FORD = 1 and LAND = 2, arrays start from 0 of course.

So you can use the code as

enum cars car = VAUX;
if (car == VAUX) printf("Vauxhall car\n");

which would make more sense than saying

int car = 0;
if (car == 0) printf("Vauxhall car\n");

struct – what are they for

A struct (structure) is a defined type that the user can define at compile time, for example if you want to have a user defined type that holds details of a database connection you could hold the hostname, username, password of the database connection within a structure (struct) without having to define 3 different variables you only have one that you need to fill in.

For the above example of a database struct

struct database_connection
{
   char *username;
   char *hostname;
   char *password;
};

then within the code you could create a new structure of this type and also fill in the details as

database_connection mydatabase;
mydatabase.username = "usernamebob";
mydatabase.hostname = "localhost";
mydatabase.password = "userpassword";

and you just need to pass that struct to a function if you wanted to to do the connections e.g.

bool dummyconnection(database_connection conn)
{
   createdatabase( conn.hostname, conn.username, conn.password);
}

saves passing in three different parameters.