Nokia N97 SDK
Example Applications Guide

simulateactiveobject.c File Reference


Detailed Description

Contained the implementation of simulating asynchronous event handling like Symbian Active Object.

Definition in file simulateactiveobject.c.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/msg.h>
#include <assert.h>

Go to the source code of this file.


Classes

struct   SAsyncParameter
struct   SAsyncContainerNode
struct   SAsyncContainer
struct   SEventMsg

Defines

#define  true   1
#define  false   0
#define  EVENT_HANDLER_MSG_QUEUE_KEY   1000
#define  EVENT_MSG_LEN   1
#define  DEBUG   Trace

Typedefs

typedef unsigned char  bool
typedef void(*  AsyncCallBackFuncPtr )(void *aArg)
typedef SAsyncContainerNode  SAsyncContainerNode

Enumerations

enum   TAsyncEvent { E_TERMINATE = 0x01, E_SOUND_EVENT, E_KEY_EVENT }

Functions

int  CreateMsgQueue (int aMsgKey)
void  SendNotification (int msg_id, int msg_no)
int  Trace (char *aMessagePtr,...)
void  AsyncSoundCallback (void *aArg)
void  AsyncKeyCallback (void *aArg)
int  RegisterCallback (SAsyncParameter *aAsyncParameter)
int  DeregisterCallback (int aEventId)
void *  AsyncEventHandleThreadEntry (void *args)
int  CreateAsyncEventHandlerThread ()
void *  KeyhandlingThreadEntry (void *args)
int  CreateKeyhandlingThread ()
void *  SoundhandlingThreadEntry (void *args)
int  CreateSoundhandlingThread ()
int  main (void)

Variables

static SAsyncContainer  gAsyncContainer
bool  exitMainThreadFlag


Function Documentation

int CreateMsgQueue (  int  msg_key  ) 
 

Create message queue

Parameters:
msg_key  Key used to create message queue
Returns:
message identifier else successful else -1

Definition at line 511 of file simulateactiveobject.c.

References DEBUG.

Referenced by AsyncEventHandleThreadEntry(), KeyhandlingThreadEntry(), main(), and SoundhandlingThreadEntry().

00512 {
00513     int msg_id = -1;
00514     if ((msg_id = msgget(msg_key, IPC_CREAT)) == -1)
00515     {
00516         DEBUG("Message Q create failed. Errno - %d\n", errno);
00517     }
00518     else
00519     {
00520         DEBUG("Message Q created\n");
00521     }
00522 
00523     return msg_id;
00524 }

void SendNotification (  int  msg_id,
int  msg_no
) 
 

Helper function to send notification using message queue send method

Parameters:
msg_id  Message Queue identifier
msg_no  Message number

Definition at line 486 of file simulateactiveobject.c.

References DEBUG, EVENT_MSG_LEN, and SEventMsg::mtype.

Referenced by KeyhandlingThreadEntry(), and SoundhandlingThreadEntry().

00487 {
00488     int len = EVENT_MSG_LEN + sizeof(int);
00489     SEventMsg msg_buf;
00490     DEBUG("Sending message id - %d\n", msg_no);
00491 
00492     /*
00493     * Create a message queue with the given key
00494     */
00495     msg_buf.mtype = msg_no; /* message identifier */
00496     /*
00497     * Put the message in the queue
00498     */
00499     if (msgsnd(msg_id, (struct msgbuf *)&msg_buf, len, 0) == -1)
00500     {
00501         DEBUG("Message Q send failed. Errno - %d\n", errno);
00502     }
00503 }

int Trace (  char *  aMessagePtr,
  ...
)  [inline]
 

Use to get execution trace

Parameters:
aMessagePtr  pointer to the message
...  Variable parameters like printf

Definition at line 112 of file simulateactiveobject.c.

00113 {
00114     va_list marker;
00115     int printChar;
00116     va_start(marker, aMessagePtr);
00117     printChar = vprintf(aMessagePtr, marker);
00118     va_end(marker);
00119     return printChar;
00120 }

void AsyncSoundCallback (  void *  aArg  ) 
 

Asynchronous callback because of sound message

Parameters:
aArg  pointer to the registered argument

Definition at line 137 of file simulateactiveobject.c.

References DEBUG.

Referenced by SoundhandlingThreadEntry().

00138 {
00139     (void) aArg;
00140     DEBUG("sound async handler.\n");
00141 }

void AsyncKeyCallback (  void *  aArg  ) 
 

Asynchronous callback because of key message

Parameters:
aArg  pointer to the registered argument

Definition at line 148 of file simulateactiveobject.c.

References DEBUG.

Referenced by KeyhandlingThreadEntry().

00149 {
00150     (void) aArg;
00151     DEBUG("key async handler.\n");
00152 }

int RegisterCallback (  SAsyncParameter *  aAsyncParameter  ) 
 

this method is used to register the callback

Parameters:
aAsyncParameter  Asynchronous parameters
Returns:
0 if successfull else -1

Definition at line 160 of file simulateactiveobject.c.

References DEBUG, gAsyncContainer, SAsyncContainer::iAsyncContainerNode, SAsyncContainerNode::iCallbackParameter, SAsyncParameter::iEventId, and SAsyncContainer::iRegisterCount.

Referenced by KeyhandlingThreadEntry(), and SoundhandlingThreadEntry().

00161 {
00162     bool registerFlag = true;
00163     SAsyncContainerNode* asyncContainerNode = gAsyncContainer.iAsyncContainerNode;
00164     SAsyncContainerNode* asyncBackContainerNode = asyncContainerNode;
00165     SAsyncParameter* asyncParameter = NULL;
00166     // Check whether the message is already registered or not
00167     int count = 0;
00168     for(count = 0; count < gAsyncContainer.iRegisterCount; count++)
00169     {
00170         asyncBackContainerNode = asyncContainerNode;
00171         if(asyncContainerNode == NULL)
00172         {
00173             break;
00174         }
00175         else
00176         {
00177             asyncParameter = asyncContainerNode->iCallbackParameter;
00178             if(asyncParameter != NULL)
00179             {
00180                 if(asyncParameter->iEventId == aAsyncParameter->iEventId)
00181                 {
00182                     // already registered
00183                     registerFlag = false;
00184                     errno = EINVAL;
00185                     DEBUG("Event is already registered. Errno - %d\n", errno);
00186                 }
00187             }
00188             else
00189             {
00190                 registerFlag = false;
00191                 errno = EINVAL;
00192                 DEBUG("Async Container node is NULL. Errno - %d\n", errno);
00193             }
00194         } // end of else
00195         asyncContainerNode = asyncContainerNode->iNext;
00196     } // end of for
00197     if(registerFlag)
00198     {
00199         // okay for registration
00200         asyncContainerNode = (SAsyncContainerNode*)malloc(sizeof(SAsyncContainerNode));
00201         asyncContainerNode->iCallbackParameter = (SAsyncParameter*)malloc(sizeof(SAsyncParameter));
00202         asyncContainerNode->iCallbackParameter->iEventId = aAsyncParameter->iEventId;
00203         asyncContainerNode->iCallbackParameter->iAsyncFuncPtr = aAsyncParameter->iAsyncFuncPtr;
00204         asyncContainerNode->iCallbackParameter->iArg = aAsyncParameter->iArg;
00205         asyncContainerNode->iNext = NULL;
00206         if(!gAsyncContainer.iRegisterCount)
00207             gAsyncContainer.iAsyncContainerNode = asyncContainerNode;
00208         else
00209             asyncBackContainerNode->iNext = asyncContainerNode;
00210         gAsyncContainer.iRegisterCount++;
00211     }
00212     return registerFlag ? 0 : -1;
00213 }

int DeregisterCallback (  int  aEventId  ) 
 

this method is used to deregister the callback

Parameters:
aEventId  event number
Returns:
0 if successful else -1

Definition at line 221 of file simulateactiveobject.c.

References DEBUG, gAsyncContainer, SAsyncContainer::iAsyncContainerNode, SAsyncContainerNode::iCallbackParameter, SAsyncParameter::iEventId, SAsyncContainerNode::iNext, and SAsyncContainer::iRegisterCount.

00222 {
00223     int retVal = -1;
00224     SAsyncContainerNode* asyncContainerNode = gAsyncContainer.iAsyncContainerNode;
00225     SAsyncContainerNode* backAsyncContainerNode = asyncContainerNode;
00226     SAsyncParameter* asyncParameter = NULL;
00227     // Check whether the message is already registered or not
00228     int count = 0;
00229     for(count = 0; count < gAsyncContainer.iRegisterCount; count++)
00230     {
00231         if(asyncContainerNode == NULL)
00232         {
00233             break;
00234         }
00235         else
00236         {
00237             asyncParameter = asyncContainerNode->iCallbackParameter;
00238             if(asyncParameter != NULL)
00239             {
00240                 if(asyncParameter->iEventId == aEventId)
00241                 {
00242                     // event is registered
00243                     // delete the container node.
00244                     if(gAsyncContainer.iAsyncContainerNode == asyncContainerNode)
00245                         gAsyncContainer.iAsyncContainerNode = asyncContainerNode->iNext;
00246                     else
00247                         backAsyncContainerNode->iNext = asyncContainerNode->iNext;
00248                     free(asyncContainerNode->iCallbackParameter);
00249                     free(asyncContainerNode);
00250                     retVal = 0;
00251                     gAsyncContainer.iRegisterCount --;
00252                     assert(gAsyncContainer.iRegisterCount >= 0);
00253                     break;
00254                 }
00255             }
00256             else
00257             {
00258                 errno = EINVAL;
00259                 DEBUG("Async Container node is NULL. Errno - %d\n", errno);
00260             }
00261         } // end of else
00262         backAsyncContainerNode = asyncContainerNode;
00263         asyncContainerNode = asyncContainerNode->iNext;
00264     } // end of for
00265     return retVal;
00266 }

void* AsyncEventHandleThreadEntry (  void *  args  ) 
 

thread entry point for the async message handling loop

Parameters:
args  Pointer to the argument passed to the thread
Returns:
NULL

Definition at line 275 of file simulateactiveobject.c.

References CreateMsgQueue(), DEBUG, EVENT_HANDLER_MSG_QUEUE_KEY, EVENT_MSG_LEN, exitMainThreadFlag, gAsyncContainer, SAsyncParameter::iArg, SAsyncContainer::iAsyncContainerNode, SAsyncParameter::iAsyncFuncPtr, SAsyncContainerNode::iCallbackParameter, SAsyncParameter::iEventId, and SAsyncContainer::iRegisterCount.

Referenced by CreateAsyncEventHandlerThread().

00276 {
00277     int msqId = -1;
00278     int msgLen = EVENT_MSG_LEN + sizeof(int);//sizeof(SEventMsg);
00279     int msgType = 0; /* Any type of message */
00280     SEventMsg msgBuf;
00281     (void) args;
00282     /*
00283     * Get the message queue id for the given key
00284     */
00285     msqId = CreateMsgQueue(EVENT_HANDLER_MSG_QUEUE_KEY);
00286     if (msqId == -1)
00287     {
00288         DEBUG("Unable to create the message queue. Errno - %d\n", errno);
00289         return NULL;
00290     }
00291     /*
00292     * Get the message from the queue
00293     */
00294     while(!exitMainThreadFlag)
00295     {
00296         if (msgrcv(msqId, (struct msgbuf *)&msgBuf, msgLen, msgType, 0) == -1)
00297         {
00298             DEBUG("Message Q recv failed. Errno - %d\n", errno);
00299             break;
00300         }
00301         else
00302         {
00303             // received some message
00304             // Search the container for the event handler
00305             SAsyncContainerNode* asyncContainerNode = gAsyncContainer.iAsyncContainerNode;
00306             int count = 0;
00307             if(msgBuf.mtype == E_TERMINATE)
00308                 break;
00309             DEBUG("Received message type - %d\n", msgBuf.mtype);
00310             for(count = 0; asyncContainerNode && count < gAsyncContainer.iRegisterCount; count++)
00311             {
00312                 if(asyncContainerNode->iCallbackParameter->iEventId == msgBuf.mtype)
00313                 {
00314                     // found the container node
00315                     if(asyncContainerNode->iCallbackParameter != NULL)
00316                     {
00317                         // call the async method
00318                         DEBUG("Found aync callback at index - %d\n", count);
00319                         asyncContainerNode->iCallbackParameter->iAsyncFuncPtr
00320                             (asyncContainerNode->iCallbackParameter->iArg);
00321                     }
00322                     else
00323                     {
00324                         DEBUG("Callback parameter is non NULL., Errno - %d\n", errno);
00325                     }
00326                 } // end of if
00327                 asyncContainerNode = asyncContainerNode->iNext;
00328             } // end of for
00329         } // end of else
00330     } // end of while
00331 
00332     /*
00333     * Remove the message queue
00334     */
00335     if (msgctl(msqId, IPC_RMID, NULL) == -1)
00336     {
00337         DEBUG("Failed to delte Message Q. Errno - %d\n", errno);
00338     }
00339     DEBUG("Async thread finished.\n");
00340     return NULL;
00341 }

int CreateAsyncEventHandlerThread (   ) 
 

Used to creat async event handler thread

Returns:
0 for success else -1

Definition at line 349 of file simulateactiveobject.c.

References AsyncEventHandleThreadEntry(), and DEBUG.

00350 {
00351     unsigned int threadId;
00352     int err = pthread_create(&threadId,(pthread_attr_t *)NULL,AsyncEventHandleThreadEntry,NULL);
00353     if(err == 0)
00354     {
00355         DEBUG("Async Thread is created successfully\n");
00356     }
00357     else
00358     {
00359         DEBUG("Unable to creat async thread. Errno - %d\n", errno);
00360     }
00361     return threadId;
00362 }

void* KeyhandlingThreadEntry (  void *  args  ) 
 

thread entry point for Key handling thread.

Parameters:
args  Pointer to the argument passed to the thread
Returns:
NULL

Definition at line 370 of file simulateactiveobject.c.

References AsyncKeyCallback(), CreateMsgQueue(), DEBUG, EVENT_HANDLER_MSG_QUEUE_KEY, exitMainThreadFlag, SAsyncParameter::iArg, SAsyncParameter::iAsyncFuncPtr, SAsyncParameter::iEventId, RegisterCallback(), and SendNotification().

Referenced by CreateKeyhandlingThread().

00371 {
00372     int msg_id = CreateMsgQueue(EVENT_HANDLER_MSG_QUEUE_KEY);
00373     (void) args;
00374     if(msg_id == -1)
00375     {
00376         DEBUG("Unable to create the message queue. Errno - %d\n", errno);
00377     }
00378     else
00379     {
00380         SAsyncParameter asyncParameter;
00381         asyncParameter.iEventId = E_KEY_EVENT;
00382         asyncParameter.iAsyncFuncPtr = AsyncKeyCallback;
00383         asyncParameter.iArg = NULL;
00384         if(RegisterCallback(&asyncParameter) != -1)
00385         {
00386             while(!exitMainThreadFlag)
00387             {
00388                 // do something and then send notification for key event.
00389                 // async handler will call the callback method
00390                 DEBUG("Within key thread.\n");
00391                 SendNotification(msg_id, E_KEY_EVENT);
00392                 // just to schedule others.
00393                 sleep(1);
00394             }
00395         }
00396     }
00397     DeregisterCallback(E_KEY_EVENT);
00398     DEBUG("key thread finished.\n");
00399     return NULL;
00400 }

int CreateKeyhandlingThread (   ) 
 

Used to creat key handler thread

Returns:
0 for success else -1

Definition at line 407 of file simulateactiveobject.c.

References DEBUG, and KeyhandlingThreadEntry().

00408 {
00409     unsigned int threadId;
00410     int err = pthread_create(&threadId,(pthread_attr_t *)NULL,KeyhandlingThreadEntry,NULL);
00411     if(err == 0)
00412     {
00413         DEBUG("Thread is created successfully.\n");
00414     }
00415     else
00416     {
00417         DEBUG("Unable to creat key thread. Errno - %d\n", errno);
00418     }
00419     return threadId;
00420 }

void* SoundhandlingThreadEntry (  void *  args  ) 
 

thread entry point for sound handling thread.

Parameters:
args  Pointer to the argument passed to the thread
Returns:
NULL

Definition at line 428 of file simulateactiveobject.c.

References AsyncSoundCallback(), CreateMsgQueue(), DEBUG, EVENT_HANDLER_MSG_QUEUE_KEY, exitMainThreadFlag, SAsyncParameter::iArg, SAsyncParameter::iAsyncFuncPtr, SAsyncParameter::iEventId, RegisterCallback(), and SendNotification().

Referenced by CreateSoundhandlingThread().

00429 {
00430     int msg_id = CreateMsgQueue(EVENT_HANDLER_MSG_QUEUE_KEY);
00431     (void) args;
00432     if(msg_id == -1)
00433     {
00434         DEBUG("Unable to create the message queue. Errno - %d\n", errno);
00435     }
00436     else
00437     {
00438         SAsyncParameter asyncParameter;
00439         asyncParameter.iEventId = E_SOUND_EVENT;
00440         asyncParameter.iAsyncFuncPtr = AsyncSoundCallback;
00441         asyncParameter.iArg = NULL;
00442         if(RegisterCallback(&asyncParameter) != -1)
00443         {
00444             while(!exitMainThreadFlag)
00445             {
00446                 // do something and then send notification for key event.
00447                 // async handler will call the callback method
00448                 DEBUG("within sound thread.\n");
00449                 SendNotification(msg_id, E_SOUND_EVENT);
00450                 // just to schedule others.
00451                 sleep(1);
00452             }
00453         }
00454     }
00455     DeregisterCallback(E_SOUND_EVENT);
00456     DEBUG("sound thread finished.\n");
00457     return NULL;
00458 }

int CreateSoundhandlingThread (   ) 
 

Used to creat sound handler thread

Returns:
0 for success else -1

Definition at line 465 of file simulateactiveobject.c.

References DEBUG, and SoundhandlingThreadEntry().

00466 {
00467     unsigned int threadId;
00468     int err = pthread_create(&threadId,(pthread_attr_t *)NULL,SoundhandlingThreadEntry,NULL);
00469     if(err == 0)
00470     {
00471         DEBUG("Thread is created successfully.\n");
00472     }
00473     else
00474     {
00475         DEBUG("Unable to creat sound thread. Errno - %d\n", errno);
00476     }
00477     return threadId;
00478 }

int main (  void   ) 
 

application entry point

Definition at line 530 of file simulateactiveobject.c.

References CreateMsgQueue(), DEBUG, and EVENT_HANDLER_MSG_QUEUE_KEY.

00531 {
00532     int msg_id;
00533     pthread_t keyThreadId;
00534     pthread_t soundThreadId;
00535     pthread_t asyncThreadId;
00536     int counter = 10;
00537 
00538     printf("You are in async applicaiton main thread.\n");
00539     msg_id = CreateMsgQueue(EVENT_HANDLER_MSG_QUEUE_KEY);
00540     if(msg_id == -1)
00541     {
00542         DEBUG("Unable to create message queue\n. Press any key to quit.\n");
00543         getchar();
00544         return 0;
00545     }
00546 
00547     asyncThreadId = CreateAsyncEventHandlerThread();
00548     keyThreadId = CreateKeyhandlingThread();
00549     soundThreadId = CreateSoundhandlingThread();
00550     exitMainThreadFlag = false;
00551     // do whatever you want to ...
00552     while(counter--) // this can be run on infinite loop or some conditional loop
00553     {
00554         DEBUG("within the main thread.\n");
00555         // do something. sleep to handle context switch
00556         // put some termination condition
00557         sleep(1);
00558     }
00559     // terminating the application
00560     exitMainThreadFlag = true;
00561     // wait for the child to complete it's own task.
00562     pthread_join(keyThreadId, NULL);
00563     pthread_join(soundThreadId, NULL);
00564     // send notification to asynch thread to terminate the handling loop
00565     SendNotification(msg_id, E_TERMINATE);
00566     // wait for async handler thread to terminate
00567     pthread_join(asyncThreadId, NULL);
00568     DEBUG("Closing the application.\n");
00569     printf("Press enter to exit.");
00570     fflush(stdin);
00571     getchar(); // wait for user input before terminate
00572     return 0;
00573 }


Variable Documentation

SAsyncContainer gAsyncContainer [static]
 

global variable to store the the callback information

Definition at line 88 of file simulateactiveobject.c.

Referenced by AsyncEventHandleThreadEntry(), DeregisterCallback(), and RegisterCallback().

bool exitMainThreadFlag
 

Flag to indicate that main thread is terminating

Definition at line 93 of file simulateactiveobject.c.

Referenced by AsyncEventHandleThreadEntry(), KeyhandlingThreadEntry(), and SoundhandlingThreadEntry().


© Nokia 2009

Back to top