Thursday, April 30, 2015

C language from BaseBand Processor Eng perspective Part-7 Externs

It's so much difficult to debug code when it spans across multiple files.

In case of baseband , it can be 1k files.  And to figure out the error you have to build baseband binary , check in real time emulating scenario and keeps on fixing.

Therefore sometimes it is good to have concept clear.  One other C keyword used extensively is EXTERN.

You will almost see this being defined in almost every C file of baseband processor code.


1) Extern on variables.

int main(){

extern int a;
a=10;
printf("%d",a);

}

int main(){

extern int a=10;
a=20;
printf("%d",a);

}

Run the above two codes , debug more on your own with extern inside function and you will know

>>  Using EXTERN inside function is useless. It doesn't serves any purpose.

Further  , when we define       extern int a;     // it doesn't allocates memory to a;
when we define                       extern int a=10;   // memory is allocated,  but compilation error will be thrown, but same will work if defining it for global variable.


Now defining it for global variable,

extern int a;

int main(){

a=10;
printf("%d",a);

}

extern int a=10;

int main(){

a=10;
printf("%d",a);

}

What we see is that if we DECLARE variable using EXTERN  no memory is allocated, but variable is declared.
so no COMPILATION ERROR,  but LINKER error is thrown.

Why LINK ERROR , since at compilation time due to extern COMPILATION thinks this varibale will be DEFINED some where else and mark it as unresolved symbol.  At Linkage time, compiler is not able to find any DEFINATION and hence it will give "symbol unresolved" error.

Just try to check if below code throws any error

extern int a;
int a=10;
int main(){

a=20;
printf("%d",a);
}

and also this one  and  notice the error thrown if any

extern int a=2;
int a=3;
int main(){

a=5;
}

Here error saying "redefination of int a" will come, which is compile time error.
Also to remove that error if we remove int a=3 will work.

But best practice will be to not have value initialized to variable if we are using EXTERN.




Tuesday, April 28, 2015

C language from BaseBand Processor Eng perspective Part-6 Function Pointers

Function Pointers

In modem code, depending on the parameters different function needs to be called.

Ex

For SMS , CALL , USSD , INTERNET , LOCATION  different function needs to be called.

We can't have one to one mapping from up to down for these functionality s.

So when at UI your press any one of the options.  There will a common function whose work will be to post or call respective functions.

We use function pointers mostly in those cases.


Ex , Check the below two codes

typedef enum{
call =0,
sms,
ussd,
location
}request_type;



switch(request_type)

case (call) :
   call_fucntion();
case(sms):
   sms_function();
case(ussd)
  ussd_function();


The other way is

typedef void (*func_dummy)(void);

func_dummy functions[4];

functions[call] = call_function;
fucntions[sms] = sms_function;

Now rather than having switch statement with lot of cases, we can have simply functions pointer , in which index will be request.

so  functions(request_type)  will call the correct functions. These also helps in debugging, since rather than checking
1) enums
2) switch statement, also break after each case
3) whether correct function is mapped or not
4) having user readable names of cases an function names



In case of function pointers we have to worry only about 3 part.


There are mostly two ways of defining  function pointer

typedef void(*func_dummy)(void);

  func_dummy functions[8];

typedef void(func_dummy)(void)

   func_dummy* functions[8]; // Note we have use pointer here

Since
  func_dummy functions[8];  // This will throw error saying function array can't be declared.

Not sure why , but I think this is due to  function is always accessible by address.

Ex :

int a;

here  a  and &a  are different, one is value and other is  address of a

but in case of function

void hello()
{
}

hello  or &hello    both are same ,  implicitly hello is same as &hello.

So when you declare array of functions, they is no way of assigning values to it . So we have to always define  array of function pointers.

.


Wednesday, April 22, 2015

C language from BaseBand Processor Eng perspective Part-5 Enums

Enums seems to be quite straightforward in the way we use in most of our codes.

Many a times we even don't need them.

But in modem code we extensively use them, let me take a scenario

The voice call you make seems to very easy from UI or what option comes ar screen. You simply swaps the number and call is made.

But Call itself has some types example

Voice Call,
Data Call ,
Emergency call,
Fax Call  etc

Now whenever call request will come , we have to check call type.

For that we can't simply write the below code

#define VOICE_CALL 1
#define DATA_CALL 2
#define EMER_CALL 3
#define FAX_CALL 4

the disadvantage been if tomorrow a new call type is added and we have to add it in between we have to change all the #defines.

So  we use enums for that

typedef enum{
VOICE_CALL=1,
DATA_CALL,
EMER_CALL,
FAX_CALL
}call_type;

Now we all know how to go on using this code .

ex

if( firstcall.call_type == VOICE_CALL)  do this ......

1) Now while working some example , I found that below is not allowed in C but allowed in CPP



typedef enum{
VOICE_CALL=1,
DATA_CALL,
EMER_CALL,
FAX_CALL
}call_type;


typedef enum{
VOICE_CALL=12,
DATA_CALL,
EMER_CALL,
FAX_CALL
}call_type_more;

In C and CPP  , we can't have same constant define in two different enums with same scope .

In above code both enums are global and hence will give error saying all the variables inside them are redefined.

But in case we define one enum inside main function and one global, no COMPILE ERROR, but again CPP we can access them using below

call_type_more :: VOICE_CALL.

but no such scope resolution operator in C.



2) Also call_type  here is a data type , which will take only values inside in it.

ex

call_type var1 = 2;  // this will throw error
call_type var2 = VOICE_CALL ; // this only will work

but while comparing  we can compare  enum type to integers.

if( 2== var2 ) do this....

Also we can compare two different enums.

if(var1 == var2)








Sunday, April 19, 2015

Java Learning : if u know C : Part 6 : Classes Very Basic

Here we will cover very basic of Class , will not see much into its modifiers. This is to just get Java programming started.

Below is basic structure using which you can write your code.

public class classname{

// varibales declaration
            // for class variables use  static modifier
            // for object variables do not use static modifier

// method (functions) can be defined

 public static int main(String args[]) {

 }


}

Points to note :  classname should be same as filename with which you are saving the file

In one file you can have multiple classes but only one should be PUBLIC.

main method will always be PUBLIC and STATIC.

The other beauty of Java is you have to define and access classes. You can't do anything without them.

Ex the below program is simple and I was writing it to tell that in ONE FILE , you can have only one PUBLIC class, but it was not working .

public class global1{

 static int global=2;

public static void main(String args[])
{

 System.out.printtln(global);
System.out.println(global12.adder(global));
}

}


class global12{
public int adder(int a)
return a+2;
}
}

// It will throw error non-static method can't be access from static method.

So we cannot simply learn syntax and start coding like C .

CLASSES needs to be define. THE below CODE works fine

public class global1{

 static int global=2;

public static void main(String args[])
{

   global1 my = new global1();
   global12  ur = new global12();

 System.out.printtln(my.global);
System.out.println(ur.adder(global));
}

}


class global12{
public int adder(int a)
return a+2;
}
}

Wednesday, April 15, 2015

C language from BaseBand Processor Eng perspective Part-4 Unions

Most of us know why unions are used in C and how to use them.

As seen in my baseband processor code, Unions are used when we have to communicate between two modules and both of modules doesn't know which exact SIGNAL is to be communicated.

So union in that case act as wrapper.

Suppose you need to give to your friend some fruit, but he don't know which fruit it will be. He only knows you will give him just a single fruit.

So in that case FRUIT is like union, in which we define struct having fruit names (like bananana, apple).

And once your friend unwraps it, he will know what to do with it. Below is simple program for this

***********************************************************************

#include


typedef enum{
RED,
BLUE,
BLACK
}color;

typedef enum{
BANANA,
APPLE
}name;

typedef struct{
int numbers;
int price;
}banana;

typedef struct{
int price;
color col;
}apple;

typedef union{
banana b;
apple a;
}fruit;

typedef struct
{
fruit f;
name fruit_type;
}fruit_wrapper;

void send(fruit_wrapper);

int main()
{
apple d;
fruit f;
fruit_wrapper g;

d.price = 10;
d.col= RED;


f.a = d;

g.f = f;
g.fruit_type = APPLE;
send(g);
}

void send (fruit_wrapper f)
{

switch(f.fruit_type)
{
case (BANANA):
printf("fruit is banana");
break;
case (APPLE):
printf("fruit is apple");
break;
}
}


*****************************************************************************888

Also there is no need to have packed union, since it will have no effect. It will simply take biggest struct size.

Also during working, we found sometimes Compliers behave in diffrent way.
Ex : the below code resulted in crash in Visual Studio , but in gcc gave garbage value

typedef struct{
int a;
}che;

int main()
{
che var1;
printf("%d",che.var1);
}

// Since we are using struct variable without initializing, so to avoid compiler specific error , have variables initialize default values.


Union allocates the memory for the biggest structure. If you have assign value to any one structure , you can access the value from other structure also. the value will be what is stored in those bytes/bits.

Ex  ( Below code is for VS )

#include

#pragma pack(1)

typedef struct{
short  a;
int b;
}var1;

typedef struct{
int a;
short b;
}var2;

typedef union{
var1 t1;
var 2 t2;
}var3;

int main()
{
var 3 ki;
ki.t1.a=2;
ki.t1.b=1;

printf("%d",ki.t2.a);
printf("%d",ki.t2.b);
}

The answer wud be
65538  and 0

Explanation

Since both structures have one short and one int and also they are also packed, so  6 bytes will be reserved.

the byte allocation happens like below
the first member of structure occupies the LSB of total memory.

So for above example
ki.t1  has  short and then int
so
1 1 1 1 (int bytes)  1 1 (short bytes)  since value is  2 for short and 1 for int

0000 0000 0000 0000 0000 0000 0000 0001 (for int)   <-->   0000 0000 0000 0010 ( this for short).

Now when we access ki.t2 , first int is there and then short ..so compiler divides memory as

0000 0000 0000 0000 (for short) <--> 0000 0000 0000 0001 0000 0000 0000 0010 ( for int).

Now convert into decimal 0 for short and 65538 for int

Monday, April 13, 2015

C language from BaseBand Processor Eng perspective Part-3 PACKED AND UNPACKED STRUCTURES

The other thing which we see in modem codes is use of data structures. Now unlike in most C codes which we write in colleges. We do not use char , int , long so much as byte, word, UNIT8 , UNIT 16.

Simply put we don't have luxury of wasting Modem memory by using ints. Most of modem data type is in hexadecimal. So we define bytes, unsigned int , char , short .

Also the data structures are __packed type , so that there is no wastage of bits.

What the __packed data structure do, Its removes the padding and use padded bits for other type.

Example

typedef struct ex{

    byte a;
    short b;
    byte c;
} ;

The size of above structure is 4 bytes on a machine with address size of 1 byte (8 bits in memory) .
For address size of 2 bytes ( 16 bits ). The above data structure will take 6 bytes.  that is wastage of 2 bytes , since each byte will occuoy only 8 bits , the rest 8 bits will be padded.


But if we define the above structure as belows

typedef  __packed stuct ex2{

 byte a;
 short b;
 byte c;
};

It will occupy 4 bytes in 16 bit machine also.

Also after discussing with my colleagues, what we have concluded is for FOR UNPACKED , how much size will be taken depends on machine and compiler implementation.

Only PACKED type gives correct answer.

Address size of maximum modem processor as of now is 32-bit  (ARM9).

Also the best way to typedef a structure is

typedef struct {
int a;
int b;
}struct_type;


This will work in both C and C++ and will avoid error if you compile your code sometime in C and sometime in C++.

The below syntax works in C++ , but not in C

typedef struct struct_type{
int a;
int b;
};



C language from BaseBand Processor Eng perspective Part-2 DEFINE vs TYPEDEF

Okay ...one important thing in modem code which you will find in most of the places will be

#define   and typedef .

#define is preprocessor. It is used to replace a text/word with a some formula or constant value.
It is helpful when we need to replace a given formula or constant in lot of place in the code. Also we can use this in place of loops. It make code easier to read.

typedef if used to define a new datatype.

Now the below code will behave same.
-----------------------------------------------------------------------------------

#define WORD int
typedef int dword;

void main(){ WORD a =10; dword b=10; cout << a ; cout << b; }

// Both will print 10
--------------------------------------------------------------------------------------

But you can clearly see difference for below code

--------------------------------------------------------------------------------------

#define WORD int *
typedef int * dword;

void main(){ WORD t,u ; dword a,b; }

// Here a,t,b  will be pointers   and  u will be int .
// U  will not be pointer , Since WORD will simply be replaced by  int * a,b .

The difference is much more visible when we define functions pointers.

typedef void(*func_point)(void);
// Now we can use func_point to define new functions whose output and input are nothing.

func_point function;

// Here function is function pointer and can be assigned function whose input/output is no parameters.

but we can't do same using #define

#define FUNC_POINT void(*func_point)(void).



So remember even though sometimes both can do other work, better to

reserve #define  for loops, constants and formulas.

and typedef  to define new type which can be data type or functions pointer type.

C language from BaseBand Processor Eng perspective Part-1

Well ...I have as of now worked for 3 years in IT industry, working as Baseband /Modem engineer.

Looking back at my work till now, it was limited mostly to C language.

Now preparing for new job and reflecting on the technologies, I was just thinking what actually is needed from a language when a person is working on it, more from modem eng perspective.


Now what is modem.  It is simply a processor/ chip in your mobile phones, whose functionality is to provide you with CALL , SMS , Supplementary Service , data /internet to your phones.

Now the modem protocols are defined by 3gpp body. It has already defined functions of each module and messages exchanged between different modules.

Now what we have to understand that modem codes run on a single processor ,( as I have seen till now) . Also it doesn't have any complex data structures  like tress, graphs etc. Most of times it will be array / linked list.

What we will find extensively in modem codes are function pointers, structures , enums and typedef.

Each module will register a callback functions to other modules to wait for a event. Also it will allocate memory for its structures. Checking of parameters received and typecasting them will be present. Then the new message data structure will be sent to other module.

Other thing which we have to learn and see is that in modem codes a lot of message posting / mailbox will be seen. Also mobile phones have Application Processor which will exchange messages with the baseband processor. That will need IPC ( Inter Processor Communication).

Lets see do you find anything good in next some lectures.

Sunday, April 5, 2015

Java Learning , if you know C : Input / Output : Part 6

In most of my programs in which I have to take input from console . I have used Scanner object .

The below program tells how to use it.

public class inputoutput{

public static void main(String args[]){

   System.out.println("enter any number");

 Scanner input = new Scanner(System.In);

 int x = input.nextInt();

}
}

now Scanner class provides you with lot of methods to read int, short, strings and other things .

A basic google will solve your problems.

I have not till yet tried to make any java program which reads from file. once done will blog it.


Regarding the Output .

System.out.println():  will work smoothly .

To type some explanation and then you variable

System.out.println("this is your answer" + var_name);   +  is something like ','  of C

To print multiple variables, see sample program


public class io {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a= 12;
int b=34;
String c = "leo";

System.out.println("my name and number is " + c + a +" " + b);
}

}



Unlinke C, there is typefdef format symbol needed to be put. In case you need to output Hexa value of int , there will be some method surely, I have not still explored that .


Java Learning : Point of reflection : Part-5

So if you have followed the Part 1-4 , we have idea 

how to 
1) define data type in Java
2) Global variable
3) Operators
4) Array declaration


Now to start basic programming, which doesn't require Object Oriented concepts, we will cover in next two Parts 

1) Input and Output.
2) How to define classes and tools to use.

Note : They things cover are not whole, and a lot of help will be needed from google to cover all aspects. 

These writings are done as I myself is learning Java. So most important thing which I learn is to code more , rather than just readings from google.

If you know C , and next time want to code any program try to do in Java rather than C , this way some basic queries will get resolved.

Java Learning , if you know C : Part-4 , Loops , Operators

Learning Java

Q > What about the extra bitwise >>> operator in Java.

To understand more about bit operators and also the diff in C and Java for them. We have to first see how they are stored in here.

Unlike C, Java doesn't have unsigned int,char or short. Everything is signed.  So first let see if there is any difference in C when bitwise operation is performed on signed /unsigned variables.

> With unsigned number, it is very easy. the number will be stored as binary representation and then output will be printed as number to bit.
  In case negative number is assigned to unsigned integer, then while storing 2's complement will be stored but only conversion back it will be treated as positive number, the MSB will
  not be treated as SIGN flag
 
> With signed number, the MSB will be treated as SIGN flag while conversion. If the number is negative it will be stored as 2's complement irrespective of signed/unsigned
Only at the time of conversion back to integer/short, it will be checked if the number was signed or unsigned. In case of signed, the MSB of bit representation will be treated as sign flag.
If it is 1, then 2's complement will be taken for number for all bits and - sign will be appended at the beginning.

********************************************************* C PROGRAM *********************************************************

1#include
#include

void bits_rep( char);
void bits_repp(unsigned char);

int main()
{
// exmaples for unsigned char
// unsigned char  a =-255; //signed char  a =-255;
// unsigned char  a =255; //signed char  a =255;
  //unsigned char  a =-1;  //signed char  a =-1;
  //unsigned char  a =256; //signed char  a =256;



int yes;int i;

do
{
printf(" enter number \n ");scanf("%d",&i);
//switch(i)
//{
//case 1:
//bits_repp(a);
printf(" signed %d  ",(char)i);
//break;
//case 2:
//bits_rep(a);
printf(" unsigned %d\n ",(unsigned char)i);
//break;
//}
printf(" Want to continue \n ");
scanf("%d",&yes);
}while(yes);

return 0;
}

// Other Bitwise operands for C  are , ~ ( act as complement, make 1 to 0 and 0 to 1), << and >> (LHS and RHS) a<<2 a="" or="">>4 , a<<2 2="" a="" bit="" by="" hand="" left="" makes="" p="" places="" shift="" side="">// ^  is XOR operator

// negative number as stored as two complement

void bits_rep( char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i));

}

void bits_repp( unsigned char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i));

}
********************************************************* C PROGRAM *********************************************************

>  Now we will see if there is any difference due to signed or unsigned in C on bitwise >> or << operators

There is no difference in unsigned or signed operator for << operator, but >> operation changes for signed and unsigned.

For unsigned the right bits get filled with 0, for signed if MSB is 1, then bits will be filled with 1.

********************************************************* C PROGRAM *********************************************************


#include
#include

void bits_rep( char);
void bits_repp(unsigned char);

int main()
{
// exmaples for unsigned char
// unsigned char  a =-255; //signed char  a =-255;
// unsigned char  a =255; //signed char  a =255;
  //unsigned char  a =-1;  //signed char  a =-1;
  //unsigned char  a =256; //signed char  a =256;



int yes;int i;

do
{
printf(" enter number \n ");scanf("%d",&i);

//switch(i)
//{
//case 1:
//bits_repp(a);
printf(" unsigned %d  ",(unsigned char)i);
//break;
//case 2:
//bits_rep(a);
printf(" signed %d\n ",( char)i);
//break;

printf(" unsigned << 2 %x  signed << 2 %d\n ",((unsigned char)i << 2) & 0xFF , (char)i << 2 );//bits_repp(i<<2 p="">
printf(" \n unsigned >> 2 %x  signed >> 2 %d\n ",((unsigned char)i >>  2) & 0xFF, (char)i >> 2 );//bits_rep(i>>2);
//}
printf("\n Want to continue \n ");
scanf("%d",&yes);
}while(yes);

return 0;
}

// Other Bitwise operands for C  are , ~ ( act as complement, make 1 to 0 and 0 to 1), << and >> (LHS and RHS) a<<2 a="" or="">>4 , a<<2 2="" a="" bit="" by="" hand="" left="" makes="" p="" places="" shift="" side="">// ^  is XOR operator

// negative number as stored as two complement

void bits_rep( char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i)&1);

}

void bits_repp( unsigned char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i)&1);

}
********************************************************* C PROGRAM *********************************************************

Well , what about the loops and other operators.

Hmmmm ...they work in same way as in C . So no explanation about them.

Java learning, if you know C : Part-3, Array

Learning Java

2. Difference in array declaration C and Java.

 > Unlike C, Java doesn't have pointers. So we cannot define dynamic array in Java. For it we have to use a different data type/object like
 
   Below code tells about the difference in C and Java
 
  ********************************************************* JAVA PROGRAM *********************************************************

public class twodarray{

public static void main(String args[])
{
int[][] arra;
arra = new int[3][4];

int[][] array = new int[3][]; // One way to define array when number of column elements might change. But can't define dynamic array like C
array[0] = new int[3];

int aRray[][] = new int[2][5];

}
}

********************************************************* JAVA PROGRAM *********************************************************

********************************************************* C PROGRAM *********************************************************
#include
#include

int main()
{
int twoD[2][3];

/* Below one throws error
int twod[][3];
int twoDd[2][];
int Twod[][];
Below one throws error */

int *twod[2];
int **TWOD;

twod[0] = (int *) malloc(sizeof(int)*3);
twod[1] = (int *) malloc(sizeof(int)*4);


TWOD = (int **) malloc(sizeof(int*)*4);

TWOD[0]=(int*) malloc (sizeof(int) *5);

return 0;
}



********************************************************* C PROGRAM *********************************************************

Learning Java , if you know C : Part-2 , Static Keyword

Learning Java

2. Difference in static variable in C and Java.

 > In c we have two types of global variables : static global and non-static global. The difference is in the scope of these, static global variable can only be used inside the C file
   in which they are defined.
   In Java and also in C++, static global variable means CLASS VARIABLE, they are shared by all the instances.
   whereas non-static global variable are INSTANCE VARIABLE, they cannot be shared among different instances.
 
  ********************************************************* JAVA PROGRAM *********************************************************

  public class global1{

static int global=2;
public static void main(String args[])
{
System.out.println(global);
global++;

}
 }


public class global2{

public static void main(String args[])
{
System.out.println(global1.global);
global1.global++;

}
 }

   ********************************************************* JAVA PROGRAM *********************************************************

 The global variable when has static qualifier in front of it becomes CLASS variable, every object defined of that class will have a single copy.
 whereas non-static are OBJECT variables, every class will have it's own copy

Learning Java, if you know C : Part-1 , Data Type

The only thing I like about blogs is you can look back and see how under-developed your mind was.

Again after getting bore of all things possible, i am here. This time I will be putting a series of article.

More for my sake than yours. Before we get down to main thing for which we are here, I will just say watching HOUSE OF CARDS, has taken a toll on me . I was want to get into politics.

Other point , EVERNOTE is cool.I mostly write my most of the articles there, but since my company doesn't allow me to access it ( WE ARE STILL NOT FREE). I have to migrate my articles to BLOGGER.  In starting things will be simply copy-paste.


There will be no coherent structure by which we will learn , it will simply be chaotic.


DATA TYPES

Learning Java

1. What happens when a data-type is assigned value greater than it can hold

 > Both in C and Java, there will be no error thrown, but the value shown will not be correct. The value will be represented in bits and then only those bits whose are defined
for the data-type will be used to display the value.

 > NEW THING LEARNED : there is no signed type in Java, in C only int,char,long,short are UNSIGNED, float and double not.

 ********************************************************* JAVA PROGRAM *********************************************************
 public class intmaxsize{

public static void main(String[] args)
{
short a=(short)65535;
System.out.println(Short.BYTES);
System.out.println(" " + a);
}
}

// Java doesn't have concept of unsigned datatype like in C/C++.
// Also like C/C++ , there is no error thrown on excedding the max size, the binary representation of bits allocated to the number is displayed
 ********************************************************* JAVA PROGRAM *********************************************************

  ********************************************************* C PROGRAM *********************************************************

 #include
#include

using namespace std;


int main()
{
unsigned short a=65536;
cout << sizeof(a) <<" ";
cout << a;
return 0;
}

// short can store upto 65535 only, but even if we store value greater than that no error will be thrown at run time or compile time, only wrong value will be stored
 ********************************************************* C PROGRAM *********************************************************