home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / src / IrDA / IrPort.cc < prev    next >
C/C++ Source or Header  |  2000-06-06  |  3KB  |  152 lines

  1. //
  2. //  $Id: IrPort.cc,v 1.4 2000/06/06 09:30:57 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include <irlib.h>
  7. #include "IrPort.h"
  8. #include "IrConnection.h"
  9. #include "IrError.h"
  10. #include "Util/Error.h"
  11. #include "Util/Assert.h"
  12.  
  13.  
  14. namespace IrDA
  15. {
  16.     //==============================================================================
  17.     //  Constants
  18.     //==============================================================================
  19.  
  20.     const Word  NULL_REF_NUM = 0xFFFF;
  21.  
  22.  
  23.     //==============================================================================
  24.     //  Port class
  25.     //==============================================================================
  26.  
  27.     Port::Port():
  28.         _opened(false),
  29.         _refNum(NULL_REF_NUM),
  30.         _connection(new Connection)
  31.     {}
  32.  
  33.     Port::~Port()
  34.     {
  35.         close();
  36.         delete _connection;
  37.     }
  38.  
  39.     // operations
  40.  
  41.     bool Port::open(Callback* callback)
  42.     {
  43.         assert(!_opened);
  44.  
  45.         if(!initIrLib())
  46.             return false;
  47.  
  48.         if (IrOpen(_refNum, irOpenOptSpeed115200) != 0)
  49.         {
  50.             IrError::openIrLibraryError();
  51.             return false;
  52.         }
  53.  
  54.         // connection could be NULL if allocation in constructor has been failed
  55.         if (_connection == NULL || !_connection->init(_refNum, callback))
  56.         {
  57.             IrClose(_refNum);
  58.             return false;
  59.         }
  60.  
  61.         _opened = true;
  62.         return true;
  63.     }
  64.  
  65.     void Port::close()
  66.     {
  67.         if (isOpened())
  68.         {
  69.             _connection->shutdown();
  70.  
  71.             if (!IrClose(_refNum) == 0)
  72.                 IrError::closeIrLibraryError();
  73.  
  74.            _opened = false;
  75.         }
  76.     }
  77.  
  78.     bool Port::connect()
  79.     {
  80.         assert(isOpened());
  81.         return _connection->connect();
  82.     }
  83.  
  84.     void Port::disconnect()
  85.     {
  86.         assert(isOpened());
  87.         _connection->disconnect();
  88.     }
  89.  
  90.     bool Port::sendData(const Byte* data, int size)
  91.     {
  92.         assert(isOpened());
  93.  
  94.         if (_connection->ready())
  95.             return _connection->sendData(data, size);
  96.  
  97.         return false;
  98.     }
  99.  
  100.     bool Port::blockingSendData(const Byte* data, int size)
  101.     {
  102.         assert(isOpened());
  103.  
  104.         if (_connection->ready())
  105.             return _connection->blockingSendData(data, size);
  106.  
  107.         return false;
  108.     }
  109.  
  110.     // attributes
  111.  
  112.     bool Port::isConnected() const
  113.     {
  114.         return isOpened() && _connection->ready();
  115.     }
  116.  
  117.     int Port::getMaxTxSize() const
  118.     {
  119.         assert(isOpened());
  120.  
  121.         if (_connection->ready())
  122.             return _connection->getMaxTxSize();
  123.     }
  124.  
  125.     bool Port::dataSendReady() const
  126.     {
  127.         assert(isOpened());
  128.  
  129.         if (_connection->ready())
  130.             return _connection->dataSendReady();
  131.     }
  132.  
  133.     // implementation
  134.  
  135.     bool Port::initIrLib()
  136.     {
  137.         if (_refNum == NULL_REF_NUM)
  138.         {
  139.             if (SysLibFind(irLibName, &_refNum) != 0)
  140.             {
  141.                 Util::Error::findLibraryError(irLibName);
  142.  
  143.                 _refNum = NULL_REF_NUM;
  144.                 return false;
  145.             }
  146.         }
  147.  
  148.         return true;
  149.     }
  150. }
  151. // namespace IrDA
  152.