Generic hardware access library
/home/cschwick/hal/generic/src/common/DOMToPCIMapConverter.cc
Go to the documentation of this file.
00001 #include "hal/DOMToPCIMapConverter.hh"
00002 #include <sstream>
00003 #include <iomanip>
00004 
00005 using namespace XERCES_CPP_NAMESPACE;
00006 
00007 void HAL::DOMToPCIMapConverter::convert( DOMDocument* doc, 
00008                                          std::list<HAL::AddressTableItem*>& itemPointerList )
00009   throw ( HAL::XMLProcessingException ) {
00010 
00011   DOMNodeList* l = doc->getElementsByTagName(XMLString::transcode("CARD_TYPE"));        
00012   if (l->getLength() != 1) {
00013     std::string text = "There is not exactly one CARD_TYPE node in the DOM\n     (HAL::DOMToPCIMapConverter::convert)";
00014     throw( HAL::XMLProcessingException( text, __FILE__, __LINE__, __FUNCTION__ ) );
00015   }
00016   DOMNodeList* itemList = doc->getElementsByTagName(XMLString::transcode("PCI_ADDRESS"));
00017   DOMNode* item;
00018   for (uint32_t i=0; i<itemList->getLength(); i++) {
00019     item = itemList->item(i);
00020     processNode(item, itemPointerList);
00021 
00022   }
00023 }
00024 
00025 void HAL::DOMToPCIMapConverter::processNode(DOMNode* item, 
00026                                             std::list<HAL::AddressTableItem*>& itemPointerList) 
00027   throw (HAL::XMLProcessingException) {
00028   uint32_t address, mask, bar;
00029   std::string description(""), access, key, space;
00030 
00031   // Frank wants the keys as attributes::
00032   DOMNamedNodeMap* attributes = item->getAttributes();
00033   const XMLCh* valPtr = attributes->getNamedItem(XMLString::transcode("ITEM_NAME"))->getNodeValue();
00034   char* xmlChar = XMLString::transcode(valPtr);
00035   key = std::string(xmlChar );
00036   XMLString::release( &xmlChar );
00037 
00038   // all other column as subelements:
00039   findNamedChild( item, "ADDRESS", address);
00040   findNamedChild( item, "MASK", mask);
00041   findNamedChild( item, "BAR", bar);
00042 
00043   findNamedChild( item, "SPACE", space);
00044   findNamedChild( item, "DESCRIPTION", description);
00045   findNamedChild( item, "READ_OR_WRITE", access);
00046   
00047   AddressSpace pciSpace;
00048   if ( strcasecmp( space.c_str(), "CONFIGURATION") == 0 ) {
00049     pciSpace = AddressSpace(CONFIGURATION);
00050   } else if ( strcasecmp( space.c_str(), "MEMORY") == 0 ) {
00051     pciSpace = AddressSpace(MEMORY);
00052   } else {
00053     std::string text="The pci space " + space +
00054       "is not implemented. \n    (HAL::DOMToPCIMapConverter::processNode)";
00055     throw (HAL::XMLProcessingException( text, __FILE__, __LINE__, __FUNCTION__ ));
00056   }
00057   
00058   bool isReadable, isWritable;
00059   if (access == "r") {
00060     isReadable = true;
00061     isWritable = false;
00062   } else if ( access == "w" ) {
00063     isReadable = false;
00064     isWritable = true;
00065   } else if ( access == "rw" ) {
00066     isReadable = true;
00067     isWritable = true;
00068   } else {
00069       std::string text = access + " is not an allowed access modifier\n     (HAL::DOMToPCIMapConverter::processNode)";
00070       throw( HAL::XMLProcessingException( text, __FILE__, __LINE__, __FUNCTION__ ) );
00071   }
00072   // load the list :
00073   HAL::PCIHardwareAddress* addressPointer = new HAL::PCIHardwareAddress(address, pciSpace, bar );
00074   HAL::AddressTableItem* itemPointer = 
00075     new HAL::AddressTableItem(key, description, *addressPointer, 
00076                          mask, isWritable, isReadable);
00077   itemPointerList.push_back(itemPointer);
00078 }
00079 
00080 void HAL::DOMToPCIMapConverter::getString( const DOMNode* node, std::string& text ) {
00081   text = "";
00082   if ( node->hasChildNodes() ) {
00083     const XMLCh *xmlChar =  node->getFirstChild()->getNodeValue();
00084     char *textChar = XMLString::transcode(xmlChar);
00085     text = std::string(textChar);
00086     XMLString::release( &textChar );
00087   }
00088 }
00089 
00090 void HAL::DOMToPCIMapConverter::getUnsignedLong( const DOMNode* node, uint32_t& number ) {
00091   number = 0;
00092   if ( node->hasChildNodes() ) {
00093     const XMLCh* xmlChar = node->getFirstChild()->getNodeValue();
00094     char *textChar = XMLString::transcode(xmlChar);
00095     std::istringstream text(textChar);
00096     XMLString::release( &textChar );
00097     std::string str;
00098     text >> std::ws >> str;
00099     std::istringstream textCopy(str);
00100     if ( str[0] == '0' && ( str[1] == 'x' || str[1] == 'X' )) {
00101       textCopy.get();
00102       textCopy.get();
00103       textCopy >> std::hex >> number;
00104     } else {
00105       textCopy >> number;
00106     }
00107   }
00108 }
00109   
00110 bool HAL::DOMToPCIMapConverter::findNamedChild( const DOMNode* node, std::string nodeName, uint32_t& value) {
00111   bool result = false;
00112   DOMNodeList* children = node->getChildNodes();
00113   for ( uint32_t i=0; ( i<children->getLength()) && (result == false); i++ ) {
00114     XMLCh* compXmlCh = XMLString::transcode(nodeName.c_str());
00115     if ( XMLString::equals( children->item(i)->getNodeName(), compXmlCh ) ) {
00116       result = true;
00117       getUnsignedLong( children->item(i), value );
00118     }
00119     XMLString::release( &compXmlCh );
00120   }
00121   return result;
00122 }
00123   
00124 
00125 bool HAL::DOMToPCIMapConverter::findNamedChild( const DOMNode* node, std::string nodeName, std::string& value) {
00126   bool result = false;
00127   DOMNodeList* children = node->getChildNodes();
00128   for ( uint32_t i=0; ( i<children->getLength()) && (result == false); i++ ) {
00129     XMLCh* compXmlCh = XMLString::transcode(nodeName.c_str());
00130     if ( XMLString::equals( children->item(i)->getNodeName(),compXmlCh ) ) {
00131       result = true;
00132       getString( children->item(i), value );
00133     }
00134     XMLString::release( &compXmlCh );
00135   }
00136   return result;
00137 }