00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "l1394_session.h"
00019 #include "l1394_card.h"
00020 #include "l1394_nodefactory.h"
00021 #include "libraw1394/raw1394.h"
00022 #include "libraw1394/csr.h"
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <unistd.h>
00026 #include <dirent.h>
00027
00028 namespace L1394{
00029 using namespace internal;
00030
00031 Session::Session() {
00032 MutexGuard mg (global_mutex);
00033 message = SMessage::getInstance();
00034 reset_handle_array = NULL;
00035 default_handle_array = NULL;
00036 thread_array = NULL;
00037 card_array = NULL;
00038 card_count = 0;
00039 session_is_running = false;
00040
00041 scanIsoDevices();
00042
00043
00044
00045 allocate();
00046 }
00047
00048
00049 Session::~Session() {
00050 deallocate();
00051 }
00052
00053 void Session::deallocate() {
00054 session_is_running = false;
00055 message->debugStream() << "Session > delete Session " << endl;
00056 for (int i = 0; i < card_count; i++) {
00057
00058
00059 delete card_array[i];
00060 raw1394_destroy_handle(reset_handle_array[i]);
00061 raw1394_destroy_handle(default_handle_array[i]);
00062
00063
00064
00065
00066 }
00067 delete NodeContainer::getNodeContainer();
00068 delete NodeFactory::getNodeFactory();
00069 message->debugStream() << "Session < delete Session " << endl ;
00070
00071 delete [] card_array;
00072 delete [] thread_array;
00073 delete [] reset_handle_array;
00074 delete [] default_handle_array;
00075 card_array = 0;
00076 reset_handle_array = 0;
00077 default_handle_array = 0;
00078 thread_array = 0;
00079 }
00080 int Session::busResetHandler(raw1394handle_t h, unsigned int generation) {
00081 MutexGuard mg(global_mutex);
00082 if (!Session::session_is_running)
00083 return 0;
00084
00085 for (int i = 0; i < SSession::getInstance()->card_count; i++)
00086 if (h == SSession::getInstance()->reset_handle_array[i]) {
00087 SSession::getInstance()->message->messageStream() << "Session > Bus Reset on card " << i << endl;
00088
00089 SSession::getInstance()->card_array[i]->makeNodeList(h);
00090 SSession::getInstance()->card_array[i]->getEventObject()->call(BUSRESET);
00091
00092 return L1394_SUCCESS;
00093 }
00094
00095 SSession::getInstance()->message->errorStream() << "Session > no busresethandle found with address" << h << " in reset_handle_array " << endl;
00096
00097 return L1394_FAILED;
00098 }
00099
00100
00101 void Session::initCard(int card_number)
00102 {
00103 NodeFactory* factory = NodeFactory::getNodeFactory();
00104
00105 if (card_array[card_number] == NULL)
00106 {
00107
00108 reset_handle_array[card_number] = raw1394_new_handle();
00109 default_handle_array[card_number] = raw1394_new_handle();
00110 if (raw1394_set_port(default_handle_array[card_number], card_number) < 0)
00111 {
00112 message->errorStream() << "Session > couldn't set card number for default handler" << endl;
00113 return;
00114 }
00115 }
00116
00117 if (raw1394_set_port(reset_handle_array[card_number], card_number) < 0)
00118 {
00119 message->errorStream() << "Session > couldn't set card number for reset handler" << endl;
00120 return;
00121 }
00122
00123
00124 int node;
00125
00126
00127 node = raw1394_get_local_id(default_handle_array[card_number]) & 0x3f;
00128 card_array[card_number] = factory->createCard(card_number, node, default_handle_array[card_number]);
00129
00130 message->debugStream()<< "Session > starting thread on card" << card_number << endl;
00131
00132 raw1394_set_bus_reset_handler(reset_handle_array[card_number], busResetHandler);
00133 thread_array[card_number].start( startBusReset, (void*)(reset_handle_array[card_number]));
00134
00135 }
00136
00137 Node* Session::findNode(int node_type) const
00138 {
00139 Node *node;
00140 node = NULL;
00141 int i = 0;
00142 while(i < card_count && node==NULL) {
00143 node = getCard(i)->findNode(node_type);
00144 i++;
00145 }
00146
00147 if (node == NULL) {
00148 message->messageStream() << "Session > No such device" << endl;
00149 }
00150 return node;
00151 }
00152
00153
00154 void *Session::startBusReset(void* my_handle) {
00155 global_mutex.lock();
00156 SSession::getInstance()->message->debugStream() << "Session > starting BusReset Handling " << endl;
00157 global_mutex.unlock();
00158 while(SSession::getInstance()->session_is_running)
00159 raw1394_loop_iterate(reinterpret_cast<raw1394handle_t>(my_handle));
00160
00161 return 0;
00162 }
00163
00164
00165
00166 Node* Session::getNode(u_int64_t guid) const {
00167 return internal::NodeContainer::getNodeContainer()->getNode(guid);
00168 }
00169
00170 const Card* Session::getCard(unsigned int card_id) const {
00171 if (card_id < 16) {
00172 return card_array[card_id];
00173 }
00174 return 0;
00175 }
00176
00177 list<const Card*> Session::getCardList() const {
00178 list<const Card*> card_list;
00179 for (int i = 0; i< 16; i++) {
00180 if (card_array[i] != 0)
00181 card_list.insert(card_list.end(), card_array[i]);
00182 }
00183 return card_list;
00184 }
00185
00186 list<Node*> Session::getAllNodes(int type) const {
00187 list<Node*> node_list;
00188 for (int i = 0; i < card_count; i++) {
00189 for (unsigned int j = 0; j < getCard(i)->getNodeCount(); j++) {
00190 if (getCard(i)->getExplicitNode(j)->getNodeType() == type)
00191 node_list.insert(node_list.begin(),getCard(i)->getExplicitNode(j));
00192 }
00193 }
00194 return node_list;
00195 }
00196
00197
00198 bool Session::nodeExist(u_int64_t guid) const {
00199 for (int i = 0; i < card_count; i++)
00200 if (getCard(i)->nodeExist(guid))
00201 return true;
00202
00203 return false;
00204 }
00205
00206 Camera* Session::findCamera(const string& name) const
00207 {
00208 list<Camera*> camera_list = getAllCameras();
00209 if ( camera_list.size() <= 0 )
00210 return 0;
00211 if ( name == "Camera" )
00212 return *camera_list.begin();
00213
00214 for (list<Camera*>::const_iterator it = camera_list.begin(); it != camera_list.end(); it++)
00215 {
00216 if ( (*it)->getName() == name)
00217 return *it;
00218 }
00219 return 0;
00220 }
00221
00222 list<Camera*> Session::getAllCameras() const
00223 {
00224 list<Camera*> camera_list;
00225
00226 list<DccCamera*> dcc_list = getAllDccCameras();
00227 list<AvcCamera*> avc_list = getAllAvcCameras();
00228
00229 for (list<DccCamera*>::const_iterator it = dcc_list.begin(); it != dcc_list.end(); it++)
00230 camera_list.insert(camera_list.end(), *it);
00231
00232 for (list<AvcCamera*>::const_iterator it = avc_list.begin(); it != avc_list.end(); it++)
00233 camera_list.insert(camera_list.end(), *it);
00234
00235 return camera_list;
00236 }
00237
00238 Vcr* Session::findVcr(const string& name) const {
00239 list<Vcr*> vcr_list = getAllVcrs();
00240
00241 if (vcr_list.size() <= 0)
00242 return 0;
00243
00244 if (name == "Vcr")
00245 return *vcr_list.begin();
00246
00247 for (list<Vcr*>::const_iterator it = vcr_list.begin(); it != vcr_list.end(); it++)
00248 {
00249 if ( (*it)->getName() == name)
00250 return *it;
00251 }
00252
00253 return NULL;
00254 }
00255
00256 list<Vcr*> Session::getAllVcrs() const {
00257 list<Node*> node_list = getAllNodes(_FcpNode);
00258 list<Vcr*> vcr_list;
00259
00260 for (list<Node*>::const_iterator it = node_list.begin(); it != node_list.end(); it++)
00261 {
00262 const list<Device*>& tmp_list = (*it)->getDeviceList();
00263 for (list<Device*>::const_iterator d_it = tmp_list.begin(); d_it != tmp_list.end(); d_it++)
00264 {
00265 if ( ((*d_it)->getDeviceType() == _Vcr ) && (dynamic_cast<Vcr*>(*d_it) != 0) )
00266 vcr_list.insert(vcr_list.end(), dynamic_cast<Vcr*>(*d_it));
00267 }
00268 }
00269 return vcr_list;
00270 }
00271
00272 list<AvcVcr*> Session::getAllAvcVcrs() const {
00273 list<AvcVcr*> avcvcr_list;
00274 list<Vcr*> vcr_list = getAllVcrs();
00275 for (list<Vcr*>::iterator it = vcr_list.begin(); it != vcr_list.end(); it++)
00276 {
00277 if ((dynamic_cast<AvcVcr*>(*it)) != 0)
00278 avcvcr_list.insert(avcvcr_list.begin(),dynamic_cast<AvcVcr*>(*it));
00279 }
00280 return avcvcr_list;
00281 }
00282
00283 list<DccCamera*> Session::getAllDccCameras() const
00284 {
00285 list<Node*> node_list = getAllNodes(_DccNode);
00286 list<DccCamera*> camera_list;
00287 for (list<Node*>::iterator it = node_list.begin(); it != node_list.end(); it++)
00288 {
00289 if (dynamic_cast<DccCamera*>(*it) != 0)
00290 camera_list.insert(camera_list.begin(), dynamic_cast<DccCamera*>(*it));
00291 }
00292 return camera_list;
00293 }
00294
00295 DccCamera* Session::findDccCamera(const string& name) const {
00296 list<DccCamera*> camera_list = getAllDccCameras();
00297 if (camera_list.size() <= 0)
00298 return 0;
00299 if ( (name == "Camera") || (name == "DccCamera") )
00300 return *camera_list.begin();
00301
00302 for (list<DccCamera*>::const_iterator it = camera_list.begin(); it != camera_list.end(); it++)
00303 if ( (*it)->getName() == name)
00304 return *it;
00305 return 0;
00306 }
00307
00308 list<AvcCamera*> Session::getAllAvcCameras() const {
00309 list<Node*> node_list = getAllNodes(_FcpNode);
00310 list<AvcCamera*> camera_list;
00311
00312 for (list<Node*>::const_iterator it = node_list.begin(); it != node_list.end(); it++)
00313 {
00314 const list<Device*>& device_list = (*it)->getDeviceList();
00315 for (list<Device*>::const_iterator d_it = device_list.begin(); d_it != device_list.end(); d_it++)
00316 {
00317 if ( ((*d_it)->getDeviceType() == _Camera) && (dynamic_cast<AvcCamera*>(*d_it) != 0 ) )
00318 camera_list.insert(camera_list.begin(), dynamic_cast<AvcCamera*>(*d_it));
00319 }
00320 }
00321 return camera_list;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 void Session::addEventHandle(EventHandle* handle)
00337 {
00338 for (int i = 0; i< card_count; i++)
00339 {
00340 if (card_array[i] != NULL)
00341 card_array[i]->getEventObject()->addEventHandle(handle);
00342 else
00343 message->errorStream()<< "Session > could not add event handle" << endl;
00344 }
00345 }
00346
00347 void Session::removeEventHandle(EventHandle* handle)
00348 {
00349 for (int i = 0; i< card_count; i++)
00350 {
00351 if (card_array[i] != NULL)
00352 card_array[i]->getEventObject()->removeEventHandle(handle);
00353 else
00354 message->errorStream() << "Session > could not remove event handle" << endl;
00355 }
00356 }
00357
00358 void Session::allocate()
00359 {
00360 session_default_handle = raw1394_new_handle();
00361 if (!session_default_handle) {
00362
00363 return;
00364 }
00365
00366 card_count = raw1394_get_port_info(session_default_handle, pinf, 16);
00367
00368 message->messageStream() << card_count << " card(s) found " << endl;
00369
00370 if (card_count <= 0) {
00371 message->warningStream() << "Session > No 1394 Card available. "<< endl;
00372 }
00373
00374 session_is_running = true;
00375
00376 card_array = new Card*[16];
00377 default_handle_array = new raw1394handle_t[16];
00378 reset_handle_array = new raw1394handle_t[16];
00379 thread_array = new Thread[16];
00380
00381 for (int i = 0; i < 16; i++) {
00382 card_array[i] = NULL;
00383 default_handle_array[i] = NULL;
00384 reset_handle_array[i] = NULL;
00385 }
00386
00387
00388 for (int i = 0; i < card_count; i++)
00389 initCard(i);
00390
00391 for (int i = 0; i < card_count; i++)
00392 card_array[i]->makeNodeList(default_handle_array[i]);
00393 }
00394
00395 Device* Session::findDevice(const string& name) const {
00396 list<Device*> device_list = getAllDevices();
00397
00398 if (device_list.size() <= 0)
00399 return 0;
00400
00401 if (name == "Device")
00402 return *device_list.begin();
00403
00404 for (list<Device*>::iterator it = device_list.begin(); it != device_list.end(); it++)
00405 {
00406 if ( (*it)->getName() == name )
00407 return *it;
00408 }
00409
00410 return 0;
00411 }
00412
00413 Device* Session::findDevice(u_int64_t guid) {
00414 list<Device*> device_list = getAllDevices();
00415
00416 for (list<Device*>::iterator it = device_list.begin(); it != device_list.end(); ++it)
00417 {
00418 if ( (*it)->getParentNode()->getGuid() == guid )
00419 return *it;
00420 }
00421
00422 return 0;
00423 }
00424
00425 Camera* Session::findCamera(u_int64_t guid) {
00426 return dynamic_cast<Camera*>(findDevice(guid));
00427 }
00428
00429 Vcr* Session::findVcr(u_int64_t guid) {
00430 return dynamic_cast<Vcr*>(findDevice(guid));
00431 }
00432
00433 void Session::scanIsoDevices() {
00434 struct stat buffer;
00435 bool is_newdevfs = false;
00436 if (stat("/dev/video1394", &buffer) != 0) {
00437
00438 } else {
00439 if (S_ISDIR(buffer.st_mode))
00440 is_newdevfs = true;
00441 }
00442
00443
00444 if (is_newdevfs) {
00445 struct dirent **namelist;
00446 int count = scandir("/dev/video1394", &namelist, 0, alphasort);
00447 string tmp;
00448 if (count < 0) {
00449
00450 } else {
00451 for (int i = 2; i< count; ++i) {
00452 __iso_devices.push_back(std::string("/dev/video1394/") + namelist[i]->d_name);
00453 }
00454 }
00455 free(namelist);
00456 }
00457
00458 else {
00459 if (stat("/dev/video1394", &buffer) != 0) {
00460 if (stat("/dev/video1394_0", &buffer) != 0) {
00461
00462 return;
00463 } else
00464 __iso_devices.push_back("/dev/video1394_0");
00465 } else
00466 __iso_devices.push_back("/dev/video1394");
00467
00468 if (stat("/dev/video1394_1", &buffer) != 0)
00469 return;
00470 else
00471 __iso_devices.push_back("/dev/video1394_1");
00472
00473 if (stat("/dev/video1394_2", &buffer) != 0)
00474 return;
00475 else
00476 __iso_devices.push_back("/dev/video1394_2");
00477
00478 if (stat("/dev/video1394_3", &buffer) != 0)
00479 return;
00480 else
00481 __iso_devices.push_back("/dev/video1394_3");
00482
00483 if (stat("/dev/video1394_4", &buffer) != 0)
00484 return;
00485 else
00486 __iso_devices.push_back("/dev/video1394_4");
00487
00488 if (stat("/dev/video1394_5", &buffer) != 0)
00489 return;
00490 else
00491 __iso_devices.push_back("/dev/video1394_5");
00492 }
00493 }
00494
00495 list<Device*> Session::getAllDevices() const {
00496 list<Device*> device_list;
00497 for (int i = 0; i< card_count; i++) {
00498 for (unsigned int j = 0; j < card_array[i]->getNodeCount(); j++) {
00499 const list<Device*>& tmp_list = card_array[i]->getExplicitNode(j)->getDeviceList();
00500 for (list<Device*>::const_iterator it = tmp_list.begin(); it != tmp_list.end(); it++)
00501 device_list.insert(device_list.end(), *it);
00502 }
00503 }
00504 return device_list;
00505 }
00506
00507 bool Session::session_is_running = true;
00508 ThreadMutex Session::global_mutex;
00509 }