Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phPlayerPower.cpp

Go to the documentation of this file.
00001 #include <playerc.h>
00002 
00003 #include <phPlayerPower.h>
00004 #include <phPlayer.h>
00005 #include <phbase.h>
00006 
00007 /* ------------------------------------------------------------------------ */
00008 struct ph_player_power_t
00009 {
00010     playerc_power_t *m_player_power;
00011 };
00012 
00013 /* ------------------------------------------------------------------------ */
00014 phPlayerPower::phPlayerPower( )
00015 {
00016     phFUNCTION("phPlayerPower::phPlayerPower")
00017     int locked = 0;
00018 
00019     phTHIS_LOOSE_LOCK(locked);
00020     
00021     this->setName("phPlayerPower");
00022 
00023     this->m_power = (struct ph_player_power_t *)phCalloc(1,
00024                         sizeof(struct ph_player_power_t));
00025     phPRINT_PTR(this->m_power,"phCalloc","phCalloc failed");
00026 
00027     this->m_source = this->m_data = new phPowerData();
00028     phPRINT_PTR(this->m_source,"new", "new failed");
00029 
00030     phTHIS_LOOSE_UNLOCK(locked);
00031 }
00032 
00033 /* ------------------------------------------------------------------------ */
00034 phPlayerPower::phPlayerPower( int index, phPlayerClient *client )
00035 {
00036     phFUNCTION("phPlayerPower::phPlayerPower")
00037     int locked = 0;
00038 
00039     phTHIS_LOOSE_LOCK(locked);
00040     
00041     phPlayerPower();
00042 
00043     rc = this->set(index,client);
00044     phPRINT_RC(rc,NULL,"this->set(%d,%p)\n",index,client);
00045 
00046     phTHIS_LOOSE_UNLOCK(locked);
00047 }
00048 
00049 /* ------------------------------------------------------------------------ */
00050 phPlayerPower::~phPlayerPower( )
00051 {
00052     phFUNCTION("phPlayerPositon::~phPlayerPower")
00053     int locked = 0;
00054 
00055     phTHIS_LOOSE_LOCK(locked);
00056     
00057     if (this->m_power != NULL)
00058     {
00059         if (this->m_is_subscribed)
00060         {
00061             rc = this->unsubscribe();
00062             phPRINT_RC(rc,NULL,"this->unsubscribe()");
00063         }
00064    
00065         if (this->m_power->m_player_power != NULL)
00066         {
00067             playerc_power_destroy(this->m_power->m_player_power);
00068             this->m_power->m_player_power = NULL;
00069         }
00070 
00071         phFree(this->m_power);
00072     }
00073     
00074     this->m_power   = NULL;
00075     phDelete(this->m_data);
00076     
00077     phTHIS_LOOSE_UNLOCK(locked);
00078 }
00079 
00080 /* ------------------------------------------------------------------------ */
00081 int phPlayerPower::set( int index, phPlayerClient *client )
00082 {
00083     phFUNCTION("phPlayerPower::set")
00084     int locked = 0;
00085 
00086     if (this->m_power == NULL) return phFAIL;
00087     
00088     phTHIS_LOCK(locked);
00089 
00090     if (index >= 0)
00091         this->m_index = index;
00092         
00093     if (client != NULL)
00094         this->m_client = client;
00095 
00096     if (this->m_is_subscribed)
00097     {
00098         rc = this->unsubscribe();
00099         phPRINT_RC(rc,NULL,"this->unsubscribe()");
00100     }
00101 
00102     if (this->m_power->m_player_power != NULL)
00103     {
00104         playerc_power_destroy(this->m_power->m_player_power);
00105         this->m_power->m_player_power = NULL;
00106         this->m_is_subscribed = 0;
00107     }
00108 
00109     if ((this->m_power->m_player_power == NULL) &&
00110         (this->m_client != NULL))
00111     {
00112         this->m_power->m_player_power = 
00113                 playerc_power_create((playerc_client_t*)this->m_client->getClientPtr(),
00114                                         index);
00115         phPRINT_PTR(this->m_power->m_player_power,
00116                     "playerc_power_create","playerc_power_create");
00117         if (this->m_power->m_player_power != NULL)
00118         {
00119             this->m_is_subscribed = 0;
00120         }
00121     }
00122 
00123     if (this->m_power->m_player_power != NULL)
00124     {
00125         rc = this->m_data->reset();
00126         phPRINT_RC(rc,NULL,"this->m_data->reset()");
00127     }
00128     
00129     phTHIS_UNLOCK(locked);
00130     
00131     return phSUCCESS;
00132 error:
00133     phTHIS_ERROR_UNLOCK(locked);
00134 
00135     return phFAIL;
00136 }
00137 
00138 /* ------------------------------------------------------------------------ */
00139 int phPlayerPower::subscribe( int access, int index, phPlayerClient *client )
00140 {
00141     phFUNCTION("phPlayerPower::subscribe")
00142     int locked = 0;
00143     int retrc = phSUCCESS;
00144 
00145     phTHIS_LOCK(locked);
00146     
00147     rc = this->set(index,client);
00148     phPRINT_RC(rc,NULL,"this->set(client:%p)",client);
00149 
00150     retrc = this->subscribe( access );
00151 
00152     phTHIS_UNLOCK(locked);
00153 
00154     return retrc;
00155 error:
00156     phTHIS_ERROR_UNLOCK(locked);
00157 
00158     return phFAIL;
00159 }
00160 
00161 /* ------------------------------------------------------------------------ */
00162 int phPlayerPower::subscribe( int phaccess )
00163 {
00164     phFUNCTION("phPlayerPower::subscribe")
00165     int locked = 0;
00166     int access = 0;
00167     int tries = 0;
00168     int max_tries = 35;
00169 
00170     if (this->m_power == NULL) return phFAIL;
00171 
00172     phCHECK_PTR(this->m_client,NULL,"No client to subscribe to; call 'set'");
00173 
00174     phTHIS_LOCK(locked);
00175 
00176     if (this->m_is_subscribed)
00177     {
00178         this->unsubscribe();
00179     }
00180   
00181     if ((this->m_power->m_player_power != NULL) && 
00182         (!this->m_is_subscribed))
00183     {
00184         switch(phaccess)
00185         {
00186             case phMODE_READ:
00187                 access = PLAYER_READ_MODE;
00188                 break;
00189             case phMODE_WRITE:
00190                 access = PLAYER_WRITE_MODE;
00191                 break;
00192             case phMODE_ALL:
00193                 access = PLAYER_ALL_MODE;
00194                 break;
00195             case phMODE_CLOSE:
00196                 access = PLAYER_CLOSE_MODE;
00197                 break;
00198             case phMODE_ERROR:
00199                 access = PLAYER_ERROR_MODE;
00200                 break;
00201             default:
00202                 access = PLAYER_ALL_MODE;
00203                 break;
00204         }
00205         
00206         do 
00207         {
00208             rc = playerc_power_subscribe(this->m_power->m_player_power,access);
00209             tries++;
00210         } while ((rc != phSUCCESS) && (tries <= max_tries));
00211         PLAYER_PRINT_RC(rc,"playerc_power_subscribe","playerc_power_subscribe");
00212 
00213         if (rc == 0)
00214             this->m_is_subscribed = 1;
00215         else
00216             this->m_is_subscribed = 0;
00217 
00218         rc = this->m_data->reset();
00219         phPRINT_RC(rc,NULL,"this->m_data->reset()");
00220     
00221         if ((this->m_client != NULL) && (this->m_is_subscribed))
00222         {
00223             rc = this->m_client->subscribeDevice(this);
00224             phPRINT_RC(rc,NULL,"this->m_client->subsribeDevice");
00225         }
00226     }
00227     
00228     phTHIS_UNLOCK(locked);
00229 
00230     return phSUCCESS;
00231 error:
00232     phTHIS_ERROR_UNLOCK(locked);
00233 
00234     return phFAIL;
00235 }
00236 
00237 /* ------------------------------------------------------------------------ */
00238 int phPlayerPower::unsubscribe()
00239 {
00240     phFUNCTION("phPlayerPower::unsubscribe")
00241     int locked = 0;
00242 
00243     if (this->m_power == NULL) return phFAIL;
00244 
00245     phTHIS_LOCK(locked);
00246 
00247     if ((this->m_client != NULL) && (this->m_is_subscribed))
00248     {
00249         rc = this->m_client->unsubscribeDevice(this);
00250         phPRINT_RC(rc,NULL,"this->m_client->unsubsribeDevice");
00251     }
00252     
00253     if (this->m_power->m_player_power != NULL)
00254     {
00255         rc = playerc_power_unsubscribe(this->m_power->m_player_power);
00256         PLAYER_PRINT_RC(rc,"playerc_power_unsubscribe","playerc_power_unsubscribe");
00257             
00258         this->m_is_subscribed = 0;
00259         
00260         rc = this->m_data->reset();
00261         phPRINT_RC(rc,NULL,"this->m_data->reset()");
00262     }
00263 
00264     phTHIS_UNLOCK(locked);
00265 
00266     return phSUCCESS;
00267 error:
00268     phTHIS_ERROR_UNLOCK(locked);
00269 
00270     return phFAIL;
00271 }
00272 
00273 /* ------------------------------------------------------------------------ */
00274 int phPlayerPower::getData( phPowerData &data )
00275 {
00276     phFUNCTION("phPlayerPower::getData")
00277     int locked = 0;
00278     playerc_power_t *ppower;
00279     
00280     if (this->m_power == NULL) return phFAIL;
00281 
00282     phTHIS_LOCK(locked);
00283 
00284     if (this->m_power->m_player_power != NULL)
00285     {
00286         ppower = this->m_power->m_player_power;
00287         rc = data.set(  ppower->charge );
00288         phCHECK_RC(rc,NULL,"data.set()");
00289     }
00290     
00291     phTHIS_UNLOCK(locked);
00292 
00293     return phSUCCESS;
00294     
00295 error:
00296     phTHIS_ERROR_UNLOCK(locked);
00297 
00298     return phFAIL;
00299 }
00300 
00301 /* ------------------------------------------------------------------------ */
00302 int phPlayerPower::updateData()
00303 {
00304     phFUNCTION("phPlayerPower::updateData")
00305     int             locked              = 0;
00306     int             data_locked         = 0;
00307     int             data_rwlocked       = 0;
00308     playerc_power_t *ppower = NULL;
00309     
00310     double  charge  = 0;
00311     
00312     int     i       = 0;
00313     int     changed = 0;
00314 
00315     if (this->m_power == NULL) return phFAIL;
00316 
00317     phTHIS_LOCK(locked);
00318 
00319     /*phPROGRESS("\n");*/
00320     if (this->m_power->m_player_power != NULL)
00321     {
00322         phMUTEX_LOCK((*(this->m_data)),data_locked);
00323         phWRITELOCK((*(this->m_data)),data_rwlocked);
00324         
00325         charge = this->m_data->getCharge();
00326 
00327         ppower = this->m_power->m_player_power;
00328 
00329         if (charge != ppower->charge)   changed = 1;
00330 
00331         if (changed)
00332         {
00333             rc = this->m_data->set( ppower->charge );
00334             phCHECK_RC(rc,NULL,"this->m_data->set()");
00335         }
00336 
00337         phRWUNLOCK((*(this->m_data)),data_rwlocked);
00338         phMUTEX_UNLOCK((*(this->m_data)),data_locked);
00339     }
00340     
00341     phTHIS_UNLOCK(locked);
00342 
00343     return phSUCCESS;
00344     
00345 error:
00346     phRWUNLOCK_ERROR((*(this->m_data)),data_rwlocked);
00347     phMUTEX_ERROR_UNLOCK((*(this->m_data)),data_locked);
00348 
00349     phTHIS_ERROR_UNLOCK(locked);
00350 
00351     return phFAIL;
00352 }
00353 
00354 /* ------------------------------------------------------------------------ */
00355 void *phPlayerPower::getId()
00356 {
00357     void *retrc = NULL;
00358     if (this->m_power != NULL)
00359     {
00360         retrc = this->m_power->m_player_power->info.id;
00361     }
00362     return retrc;
00363 }
00364 
00365 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:07 2007 for phission by  doxygen 1.4.4