|
Nokia N97 SDK Example Applications Guide |
00001 /** @file simulateactiveobject.c 00002 * Contained the implementation of simulating asynchronous event handling 00003 * like Symbian Active Object. 00004 */ 00005 00006 /* 00007 * ============================================================================== 00008 * Name : simulateactiveobject.c 00009 * Part of : Open C/Example code 00010 * Description : This is used to simulate the asynchronous event handling 00011 * similar to Symbian Active Object. 00012 * Version : 1.0 00013 * 00014 * Copyright (c) 2007 Nokia Corporation. 00015 * This material, including documentation and any related 00016 * computer programs, is protected by copyright controlled by 00017 * Nokia Corporation. 00018 * ============================================================================== 00019 */ 00020 00021 // system include 00022 #include <unistd.h> 00023 #include <stdio.h> 00024 #include <stdlib.h> 00025 #include <sys/fcntl.h> 00026 #include <string.h> 00027 #include <pthread.h> 00028 #include <errno.h> 00029 #include <sys/time.h> 00030 #include <sys/select.h> 00031 #include <errno.h> 00032 #include <sys/msg.h> 00033 #include <assert.h> 00034 // GCCE specific header file 00035 //#include <staticlibinit_gcce.h> 00036 00037 typedef unsigned char bool; 00038 00039 #define true 1 00040 #define false 0 00041 // Event handler thread message queue key 00042 #define EVENT_HANDLER_MSG_QUEUE_KEY 1000 00043 #define EVENT_MSG_LEN 1 00044 00045 typedef enum 00046 { 00047 E_TERMINATE = 0x01, 00048 E_SOUND_EVENT, 00049 E_KEY_EVENT 00050 }TAsyncEvent; 00051 00052 // Asynchronous callback 00053 typedef void (*AsyncCallBackFuncPtr)(void* aArg); 00054 00055 // structure used to pass the parameters while registering 00056 typedef struct 00057 { 00058 int iEventId; 00059 AsyncCallBackFuncPtr iAsyncFuncPtr; 00060 void* iArg; 00061 }SAsyncParameter; 00062 00063 // Async paramer link list node 00064 typedef struct SAsyncContainerNode 00065 { 00066 SAsyncParameter *iCallbackParameter; 00067 struct SAsyncContainerNode *iNext; 00068 }SAsyncContainerNode; 00069 00070 // Containter to store the callback information 00071 typedef struct 00072 { 00073 int iRegisterCount; 00074 SAsyncContainerNode *iAsyncContainerNode; 00075 }SAsyncContainer; 00076 00077 // message structure 00078 00079 typedef struct 00080 { 00081 int mtype; 00082 char mtext[EVENT_MSG_LEN]; 00083 }SEventMsg; 00084 00085 /** 00086 * global variable to store the the callback information 00087 */ 00088 static SAsyncContainer gAsyncContainer; 00089 00090 /** 00091 * Flag to indicate that main thread is terminating 00092 */ 00093 bool exitMainThreadFlag; 00094 00095 // Forward declaration 00096 int CreateMsgQueue(int aMsgKey); 00097 void SendNotification(int msg_id, int msg_no); 00098 00099 // Debug message 00100 //#ifdef _DEBUG 00101 #if 1 // we want to see debug-traces also in device 00102 /** 00103 * Define this macro to enable file based logging. 00104 */ 00105 00106 /** 00107 * Use to get execution trace 00108 * @param aMessagePtr pointer to the message 00109 * @param ... Variable parameters like printf 00110 * 00111 */ 00112 inline int Trace(char* aMessagePtr, ...) 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 } 00121 #define DEBUG Trace 00122 #else 00123 // On target 00124 inline int Trace(char* aMessagePtr, ...) 00125 { 00126 return 0; 00127 } 00128 #define DEBUG 0 & Trace 00129 #endif 00130 00131 00132 /** 00133 * Asynchronous callback because of sound message 00134 * @param aArg pointer to the registered argument 00135 */ 00136 00137 void AsyncSoundCallback(void * aArg) 00138 { 00139 (void) aArg; 00140 DEBUG("sound async handler.\n"); 00141 } 00142 00143 /** 00144 * Asynchronous callback because of key message 00145 * @param aArg pointer to the registered argument 00146 */ 00147 00148 void AsyncKeyCallback(void * aArg) 00149 { 00150 (void) aArg; 00151 DEBUG("key async handler.\n"); 00152 } 00153 00154 /** 00155 * this method is used to register the callback 00156 * @param aAsyncParameter Asynchronous parameters 00157 * @return 0 if successfull else -1 00158 */ 00159 00160 int RegisterCallback(SAsyncParameter* aAsyncParameter) 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 } 00214 00215 /** 00216 * this method is used to deregister the callback 00217 * @param aEventId event number 00218 * @return 0 if successful else -1 00219 */ 00220 00221 int DeregisterCallback(int aEventId) 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 } 00267 00268 00269 /** 00270 * thread entry point for the async message handling loop 00271 * @param args Pointer to the argument passed to the thread 00272 * @return NULL 00273 */ 00274 00275 void* AsyncEventHandleThreadEntry(void * args) 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 } 00342 00343 00344 /** 00345 * Used to creat async event handler thread 00346 * @return 0 for success else -1 00347 */ 00348 00349 int CreateAsyncEventHandlerThread() 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 } 00363 00364 /** 00365 * thread entry point for Key handling thread. 00366 * @param args Pointer to the argument passed to the thread 00367 * @return NULL 00368 */ 00369 00370 void* KeyhandlingThreadEntry(void * args) 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 } 00401 00402 /** 00403 * Used to creat key handler thread 00404 * @return 0 for success else -1 00405 */ 00406 00407 int CreateKeyhandlingThread() 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 } 00421 00422 /** 00423 * thread entry point for sound handling thread. 00424 * @param args Pointer to the argument passed to the thread 00425 * @return NULL 00426 */ 00427 00428 void* SoundhandlingThreadEntry(void * args) 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 } 00459 00460 /** 00461 * Used to creat sound handler thread 00462 * @return 0 for success else -1 00463 */ 00464 00465 int CreateSoundhandlingThread() 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 } 00479 00480 /** 00481 * Helper function to send notification using message queue send method 00482 * @param msg_id Message Queue identifier 00483 * @param msg_no Message number 00484 */ 00485 00486 void SendNotification(int msg_id, int msg_no) 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 } 00504 00505 /** 00506 * Create message queue 00507 * @param msg_key Key used to create message queue 00508 * @return message identifier else successful else -1 00509 */ 00510 00511 int CreateMsgQueue(int msg_key) 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 } 00525 00526 /** 00527 * application entry point 00528 */ 00529 00530 int main(void) 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 } 00574 00575 //End of file
|
© Nokia 2009 |