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

phPlayerWifi.cpp

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




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