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

phWifiData.cpp

Go to the documentation of this file.
00001 #include <phWifiData.h>
00002 #include <phbase.h>
00003 
00004 /* ------------------------------------------------------------------------ */
00005 struct ph_player_wifi_link_t
00006 {
00007     playerc_wifi_link_t link;
00008 };
00009 
00010 /* ------------------------------------------------------------------------ */
00011 phWifiData::phWifiData()
00012 {
00013     phFUNCTION("phWifiData::phWifiData")
00014     int locked = 0;
00015     int rwlocked = 0;
00016     
00017     phTHIS_LOOSE_LOCK(locked);
00018     phTHIS_WRITELOCK_LOOSE(rwlocked);
00019     
00020     this->setName("phWifiData");
00021     
00022     this->m_link_count      = 0;
00023     
00024     this->m_links           = NULL;
00025     this->m_links_size      = 0;
00026 
00027     this->m_link_data       = NULL;
00028     this->m_link_data_size  = 0;
00029 
00030     phTHIS_RWUNLOCK_LOOSE(rwlocked);
00031     phTHIS_LOOSE_UNLOCK(locked);
00032 }
00033 
00034 /* ------------------------------------------------------------------------ */
00035 phWifiData::~phWifiData()
00036 {
00037     phFUNCTION("phWifiData::~phWifiData")
00038     int locked = 0;
00039     int rwlocked = 0;
00040     
00041     phTHIS_LOOSE_LOCK(locked);
00042     phTHIS_WRITELOCK_LOOSE(rwlocked);
00043     
00044     rc = this->reset();
00045     phPRINT_RC(rc,NULL,"this->reset()");
00046     
00047     phTHIS_RWUNLOCK_LOOSE(rwlocked);
00048     phTHIS_LOOSE_UNLOCK(locked);
00049 }
00050 
00051 /* ------------------------------------------------------------------------ */
00052 int phWifiData::copy( phObject *copyto_object )
00053 {
00054     phFUNCTION("phWifiData::copy")
00055     
00056     if (this->isNamed(((phObject *)copyto_object)->getName()))
00057     {
00058         phWifiData *copyto_data = (phWifiData *)copyto_object;
00059         return copyto_data->copyData(*this);
00060     }
00061     /* Otherwise I'll assume they are incompatible */
00062     else
00063     {
00064         phPRINT_RC(-1,NULL, "Invalid object pairing in update."
00065                           "[ this:%s != target:%s ]",
00066                  this->getName(), ((phObject *)copyto_object)->getName());
00067         return phFAIL;
00068     }
00069 }
00070 
00071 /* ------------------------------------------------------------------------ */
00072 int phWifiData::swap( phObject *object )
00073 {
00074     phFUNCTION("phWifiData::swap")
00075 
00076     if (this->isNamed(((phObject *)object)->getName()))
00077     {
00078         phWifiData *data = (phWifiData *)object;
00079 
00080         return data->swapData(*this);
00081     }
00082     /* Otherwise I'll assume they are incompatible */
00083     else
00084     {
00085         phPRINT_RC(-1,NULL, "Invalid object pairing in update."
00086                           "[ this:%s != target:%s ]",
00087                  this->getName(), ((phObject *)object)->getName());
00088         return phFAIL;
00089     }    
00090 }
00091 
00092 /* ------------------------------------------------------------------------ */
00093 int phWifiData::copyData( phWifiData &copyfrom )
00094 {
00095     phFUNCTION("phWifiData::copyData")
00096     int rwlocked = 0;
00097     int other_rwlocked = 0;
00098     uint32_t i = 0;
00099    
00100     phTHIS_WRITELOCK(rwlocked);
00101     phREADLOCK(copyfrom,other_rwlocked);
00102 
00103     this->m_link_count = copyfrom.m_link_count;
00104 
00105     if (this->m_link_count > 0)
00106     {
00107         phDALLOC_RESIZE(this->m_links,
00108                         this->m_links_size,
00109                         this->m_link_count,
00110                         playerc_wifi_link_t);
00111 
00112         phDALLOC_RESIZE(this->m_link_data,
00113                         this->m_link_data_size,
00114                         this->m_link_count,
00115                         phWifiLinkData *);
00116 
00117         if (copyfrom.m_links != NULL)
00118         {
00119             for (i = 0; i < copyfrom.m_link_count; i++)
00120             {
00121                 this->m_links[i] = copyfrom.m_links[i];
00122                 
00123                 if (this->m_link_data[i] == NULL)
00124                 {
00125                     this->m_link_data[i] = new phWifiLinkData();
00126                     phCHECK_PTR(this->m_link_data[i],"new",
00127                     "new failed to allocate phWifiLinkData()");
00128                 }
00129                 
00130                 this->m_link_data[i]->set((uint8_t*)this->m_links[i].mac, 
00131                                           (uint8_t*)this->m_links[i].ip, 
00132                                           this->m_links[i].essid,  
00133                                           this->m_links[i].mode, 
00134                                           this->m_links[i].encrypt, 
00135                                           this->m_links[i].freq, 
00136                                           this->m_links[i].qual, 
00137                                           this->m_links[i].level, 
00138                                           this->m_links[i].noise,
00139                                           i  );
00140             }
00141         }
00142         rc = this->notify();
00143         phPRINT_RC(rc,NULL,"this->notify()");
00144     }
00145 
00146     phRWUNLOCK(copyfrom,other_rwlocked);
00147     phTHIS_RWUNLOCK(rwlocked);
00148 
00149     return phSUCCESS;
00150 error:
00151     phRWUNLOCK_ERROR(copyfrom,other_rwlocked);
00152     phTHIS_RWUNLOCK_ERROR(rwlocked);
00153     
00154     return phFAIL;    
00155 }
00156 
00157 /* ------------------------------------------------------------------------ */
00158 int phWifiData::swapData( phWifiData &data )
00159 {
00160     phFUNCTION("phWifiData::swapData")
00161     int rwlocked = 0;
00162     int other_rwlocked = 0;
00163 
00164     playerc_wifi_link_t *t_links        = NULL;
00165     uint32_t            t_link_count    = 0;
00166     phWifiLinkData      **t_link_data   = NULL;
00167     uint32_t            t_links_size    = 0; 
00168     uint32_t            t_link_data_size= 0; 
00169 
00170     phTHIS_WRITELOCK(rwlocked);
00171         
00172     if (this->isSwapEnabled())
00173     {
00174         phWRITELOCK(data,other_rwlocked);
00175     
00176         /* Data to temp */
00177         t_links             = data.m_links;
00178         t_link_count        = data.m_link_count;
00179         t_link_data         = data.m_link_data;
00180         t_links_size        = data.m_links_size;
00181         t_link_data_size    = data.m_link_data_size;
00182 
00183         /* this to data */
00184         data.m_links        = this->m_links;
00185         data.m_link_count   = this->m_link_count;
00186         data.m_link_data    = this->m_link_data;
00187         data.m_links_size     = this->m_links_size;
00188         data.m_link_data_size = this->m_link_data_size;
00189 
00190         /* temp to this */    
00191         this->m_links       = t_links;
00192         this->m_link_count  = t_link_count;
00193         this->m_link_data   = t_link_data;
00194         this->m_links_size    = t_links_size;
00195         this->m_link_data_size= t_link_data_size;
00196 
00197         rc = this->notify();
00198         phPRINT_RC(rc,NULL,"this->notify()");
00199 
00200         rc = data.notify();
00201         phPRINT_RC(rc,NULL,"data.notify()");
00202 
00203         phRWUNLOCK(data,other_rwlocked);
00204     }
00205     else
00206     {
00207         rc = this->copyData(data);
00208         phCHECK_RC(rc,NULL,"this->copyData(data)");
00209     }
00210     phTHIS_RWUNLOCK(rwlocked);
00211 
00212     return phSUCCESS;
00213 error:
00214     phRWUNLOCK_ERROR(data,other_rwlocked);
00215     phTHIS_RWUNLOCK_ERROR(rwlocked);
00216 
00217     return phFAIL;    
00218 }
00219 
00220 /* ------------------------------------------------------------------------ */
00221 int phWifiData::reset()
00222 {
00223     phFUNCTION("phWifiData::reset")
00224     int rwlocked = 0;
00225     uint32_t i = 0;
00226 
00227     phTHIS_WRITELOCK(rwlocked);
00228 
00229     for (i = 0; i < this->m_link_count; i++ )
00230     {
00231         phDelete(this->m_link_data[i]);
00232     }
00233     phFree(this->m_link_data);
00234     phFree(this->m_links);
00235 
00236     this->m_link_count      = 0;
00237 
00238     this->m_links_size      = 0;
00239     this->m_link_data_size  = 0;
00240 
00241     phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL);
00242 }
00243 
00244 
00245 /* ------------------------------------------------------------------------ */
00246 int phWifiData::set(uint32_t link_count,
00247                     playerc_wifi_link_t *links )
00248 {
00249     phFUNCTION("phWifiData::set")
00250     int rwlocked = 0;
00251     
00252     int             locked              = 0;
00253     int             data_locked         = 0;
00254     int             data_rwlocked       = 0;
00255 
00256     int             send_notify         = 0;
00257 
00258     uint8_t mac[32]     = {0};
00259     uint8_t ip[32]      = {0};
00260     char    essid[32]   = {0};
00261     int     mode        = -1;
00262     int     encrypt     = 0;
00263     double  freq        = 0.0;
00264     int     qual        = 0;
00265     int     level       = 0;
00266     int     noise       = 0;
00267 
00268     uint32_t     i          = 0;
00269     int         all_changed = 0;
00270     int         changed     = 0;
00271     int         current_id  = -1;
00272 
00273     playerc_wifi_link_t *pwifi_link = NULL;
00274 
00275     phTHIS_WRITELOCK(rwlocked);
00276     
00277     if (this->m_link_count != link_count)
00278     {
00279         send_notify = all_changed = 1;
00280     }
00281     
00282     if (all_changed)
00283     {
00284         this->m_link_count = link_count;
00285 
00286         phDALLOC_RESIZE(this->m_links,
00287                         this->m_links_size,
00288                         this->m_link_count,
00289                         playerc_wifi_link_t);
00290 
00291         phDALLOC_RESIZE(this->m_link_data,
00292                         this->m_link_data_size,
00293                         this->m_link_count,
00294                         phWifiLinkData *);
00295     }
00296 
00297     for (i = 0; i < this->m_link_count; i++ )
00298     {
00299         int change = 0;
00300         
00301         this->m_links[i] = links[i];
00302         
00303         if (this->m_link_data[i] == NULL)
00304         {
00305             this->m_link_data[i] = new phWifiLinkData();
00306             phCHECK_PTR(this->m_link_data[i],"new",
00307             "new failed to allocate phWifiLinkData()");
00308         }   
00309         
00310         current_id = i;
00311         
00312         phMUTEX_LOCK((*(this->m_link_data[i])),data_locked);
00313         phWRITELOCK((*(this->m_link_data[i])),data_rwlocked);
00314         
00315         rc = this->m_link_data[i]->get((uint8_t*)mac,
00316                                        (uint8_t*)ip,
00317                                        (char*)essid,
00318                                        &mode,&encrypt,&freq,
00319                                        &qual,&level,&noise,NULL);
00320         phPRINT_RC(rc,NULL,"this->m_link_data[%d]->get",i);
00321         
00322         pwifi_link = &(links[i]);
00323 
00324         if ((all_changed) ||
00325             (mode       !=  pwifi_link->mode)   ||
00326             (encrypt    !=  pwifi_link->encrypt)||
00327             (freq       !=  pwifi_link->freq)   ||
00328             (qual       !=  pwifi_link->qual)   ||
00329             (level      !=  pwifi_link->level)  ||
00330             (noise      !=  pwifi_link->noise)  ||
00331             (memcmp(mac,    pwifi_link->mac,     sizeof(uint8_t)*32) != 0) ||
00332             (memcmp(ip,     pwifi_link->ip,      sizeof(uint8_t)*32) != 0) ||
00333             (memcmp(essid,  pwifi_link->essid,   sizeof(char)*32) != 0))
00334         {
00335             send_notify = changed = 1;
00336         }
00337 
00338         if (changed)
00339         {
00340             rc = this->m_link_data[i]->set((uint8_t*)pwifi_link->mac, 
00341                                            (uint8_t*)pwifi_link->ip, 
00342                                            pwifi_link->essid,  
00343                                            pwifi_link->mode, 
00344                                            pwifi_link->encrypt, 
00345                                            pwifi_link->freq, 
00346                                            pwifi_link->qual, 
00347                                            pwifi_link->level, 
00348                                            pwifi_link->noise,
00349                                            i );
00350             phCHECK_RC(rc,NULL,"this->m_link_data->set()");
00351         }
00352 
00353         phRWUNLOCK((*(this->m_link_data[i])),data_rwlocked);
00354         phMUTEX_UNLOCK((*(this->m_link_data[i])),data_locked);
00355         current_id = -1;
00356     }
00357     
00358 
00359     if (send_notify)
00360     {
00361         rc = this->notify();
00362         phPRINT_RC(rc,NULL,"this->notify()");
00363     }
00364 
00365     phTHIS_RWUNLOCK(rwlocked);
00366     
00367     return phSUCCESS;
00368     
00369 error:
00370     if ((current_id < (int)this->m_link_count) && (current_id >= 0))
00371     {
00372         phRWUNLOCK_ERROR((*(this->m_link_data[current_id])),data_rwlocked);
00373         phMUTEX_ERROR_UNLOCK((*(this->m_link_data[current_id])),data_locked);
00374     }
00375     
00376     phTHIS_RWUNLOCK_ERROR(rwlocked);
00377 
00378     return phFAIL;
00379 }
00380 
00381 /* ------------------------------------------------------------------------ */
00382 int phWifiData::get(uint32_t *link_count,
00383                     playerc_wifi_link_t *links)
00384 {
00385     phFUNCTION("phWifiData::get")
00386     int rwlocked = 0;
00387 
00388     phTHIS_READLOCK(rwlocked);
00389 
00390     if (link_count != NULL)
00391         *link_count = this->m_link_count;
00392 
00393     if (links != NULL)
00394         memcpy(links,this->m_links,sizeof(playerc_wifi_link_t)*this->m_link_count);
00395         
00396 
00397     phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL);
00398 }
00399 
00400 /* ------------------------------------------------------------------------ */
00401 uint32_t phWifiData::getLinkCount(  )
00402 {
00403     phFUNCTION("phWifiData::getLinkCount")
00404     int rwlocked = 0;
00405     uint32_t retval = 0;
00406  
00407     phTHIS_READLOCK(rwlocked);
00408 
00409     retval = this->m_link_count;
00410 
00411     phTHIS_RWUNLOCK_RET(rwlocked,retval,0);
00412 }
00413 
00414 /* ------------------------------------------------------------------------ */
00415 phWifiLinkData *phWifiData::getLink( uint32_t link_id )
00416 {
00417     phFUNCTION("phWifiData::getSource")
00418     phWifiLinkData *retval  = NULL;
00419     int             locked  = 0;
00420     int             rwlocked= 0;
00421     
00422     phTHIS_READLOCK(rwlocked);
00423    
00424     if (this->m_link_count > link_id)
00425     {
00426         retval = this->m_link_data[link_id];
00427     }
00428 
00429     phTHIS_RWUNLOCK_RET(rwlocked,retval,NULL);
00430 }
00431 
00432 
00433 /* ------------------------------------------------------------------------ */
00434 phLiveObject *phWifiData::getSource( uint32_t link_id )
00435 {
00436     phFUNCTION("phWifiData::getSource")
00437     phLiveObject   *retval  = NULL;
00438     int             locked  = 0;
00439     int             rwlocked= 0;
00440     
00441     phTHIS_READLOCK(rwlocked);
00442    
00443     if (this->m_link_count > link_id)
00444     {
00445         retval = this->m_link_data[link_id];
00446     }
00447 
00448     phTHIS_RWUNLOCK(rwlocked);
00449 
00450     return retval;
00451 error:
00452     phTHIS_RWUNLOCK_ERROR(rwlocked);
00453     
00454     return NULL;
00455 }
00456 
00457 /* ------------------------------------------------------------------------ */
00458 void phWifiData::print_data( )
00459 {
00460     phFUNCTION("phWifiData::print_data")
00461     uint32_t    i           = 0;
00462     int         j           = 0;
00463     int         rwlocked    = 0;
00464 
00465     phTHIS_READLOCK_LOOSE(rwlocked);
00466     
00467     fprintf(stderr,"----------------------------------------------------\n");
00468     fprintf(stderr,"%s\n",this->getName());
00469     fprintf(stderr,"----------------------------------------------------\n");
00470     fprintf(stderr,"Link Count: %u\n",this->m_link_count);
00471     for (i = 0; i < this->m_link_count; i++ )
00472     {
00473         this->m_link_data[i]->print_data();
00474     }
00475     fprintf(stderr,"----------------------------------------------------\n");
00476     
00477     phTHIS_RWUNLOCK_LOOSE(rwlocked);
00478 }




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