botsense/bsModules/bsI2C/bsI2CServer.c
int bsModOpen(BsVConnHnd_T  hndVConn,
              bool_t        bTrace,
              const char   *sDevUri,
              byte_t        argbuf[],
              size_t        uArgSize)
{
  BsModCtlBlk_T        *pCtlBlk;
  int                   rc;

  // Check if handle is in valid range.
  BSMOD_TRY_VCONN_HND_RANGE(hndVConn);

  // Check if resources are available.
  if( BSMOD_RSRC_INUSE_COUNT(BsModRsrcTbl) >= BSMOD_MAX_HANDLES )
  {
    BSMOD_LOG_ERROR(hndVConn, BS_ECODE_NO_RSRC,
        "InUseCount=%u: No more resources available.",
        BSMOD_RSRC_INUSE_COUNT(BsModRsrcTbl));
    return -BS_ECODE_NO_RSRC;
  }
  
  // Check if the handle in this module's resource table is not already in-use.
  else if( BSMOD_RSRC_IS_INUSE(BsModRsrcTbl, hndVConn) )
  {
    BSMOD_LOG_ERROR(hndVConn, BS_ECODE_BUSY, 
        "Module virtual connection handle already in use");
    return -BS_ECODE_BUSY;
  }

  //
  // Search for an existing opened device control block. If this is a first 
  // reference to the device, then create a shared control block and open
  // the actual device.
  //
  if( (pCtlBlk = bsModCtlBlkFind(sDevUri)) == NULL )
  {
    // allocate a new control block and initialize
    pCtlBlk = bsModCtlBlkNew(sDevUri, bTrace);

    // open the device
    rc = i2c_open(&pCtlBlk->m_hndI2C, sDevUri);

    if( rc < 0 )
    {
      BSMOD_LOG_SYSERROR(hndVConn, "i2c_open(%p, %s).",
                                  &pCtlBlk->m_hndI2C, sDevUri);
      bsModCtlBlkDelete(pCtlBlk);
      return -BS_ECODE_SYS;
    }
  }

  // bump device reference count
  pCtlBlk->m_uRefCnt++;

  // add (a copy) to this module's resource table
  BsModCallbacks->m_cbModRsrcAdd(BsModRsrcTbl, hndVConn, pCtlBlk);

  LOGDIAG2("VConn=%d: %d=i2c_open(%p, dev=%s).",
      hndVConn, pCtlBlk->m_hndI2C.fd, &pCtlBlk->m_hndI2C, sDevUri);

  return pCtlBlk->m_hndI2C.fd;
}
/*!
 * \brief Open an \h_i2c device and associate with the given handle.
 *
 * Subsequent calls to the module use the given handle to associate the 
 * specific module-device instance.
 *
 * The argument buffer contains packed message arguements specific to the 
 * device and module. For this \h_i2c module, there are now additional
 * arguments.
 *
 * \note Each call to this exported function will only open the \h_i2c device
 * if it is the first call. All virtual connections to the same device share
 * the same opened file resource descriptor.
 *
 * \note
 * It is the responsibile of the client to ensure consistency if more than
 * one virtual connection is established.
 *
 * \param hndVConn      Virtual connection handle.
 * \param bTrace        Do [not] enable message tracing on this handle.
 * \param sDevUri       Device URI.
 * \param argbuf        Packed specific open configuration arguments submessage.
 * \param uArgSize      Size of packed argumets in buffer (number of bytes).
 *
 * \return
 * On success, returns a unique resource descriptor \h_ge 0 which is typically
 * an opened file descriptor or socket descriptor, but can be module defined.\n
 * \copydoc doc_return_ecode
 */
\brief Open an \h_i2c device and associate with the given handle.
Subsequent calls to the module use the given handle to associate the
specific module-device instance.

The argument buffer contains packed message arguements specific to the
device and module. For this \h_i2c module, there are now additional
arguments.

\note Each call to this exported function will only open the \h_i2c device
if it is the first call. All virtual connections to the same device share
the same opened file resource descriptor.

\note
It is the responsibile of the client to ensure consistency if more than
one virtual connection is established.

\param hndVConn      Virtual connection handle.
\param bTrace        Do [not] enable message tracing on this handle.
\param sDevUri       Device URI.
\param argbuf        Packed specific open configuration arguments submessage.
\param uArgSize      Size of packed argumets in buffer (number of bytes).

\return
On success, returns a unique resource descriptor \h_ge 0 which is typically
an opened file descriptor or socket descriptor, but can be module defined.\n
\copydoc doc_return_ecode
\ brief Open an \ h_i2c device and associate with the given handle . Subsequent calls to the module use the given handle to associate the specific module - device instance . The argument buffer contains packed message arguements specific to the device and module . For this \ h_i2c module there are now additional arguments . \ note Each call to this exported function will only open the \ h_i2c device if it is the first call . All virtual connections to the same device share the same opened file resource descriptor . \ note It is the responsibile of the client to ensure consistency if more than one virtual connection is established . \ param hndVConn Virtual connection handle . \ param bTrace Do [ not ] enable message tracing on this handle . \ param sDevUri Device URI . \ param argbuf Packed specific open configuration arguments submessage . \ param uArgSize Size of packed argumets in buffer ( number of bytes ) . \ return On success returns a unique resource descriptor \ h_ge 0 which is typically an opened file descriptor or socket descriptor but can be module defined . \ n \ copydoc doc_return_ecode
int bsModOpen(BsVConnHnd_T  hndVConn,
              bool_t        bTrace,
              const char   *sDevUri,
              byte_t        argbuf[],
              size_t        uArgSize)
{
  BsModCtlBlk_T        *pCtlBlk;
  int                   rc;
  BSMOD_TRY_VCONN_HND_RANGE(hndVConn);
  if( BSMOD_RSRC_INUSE_COUNT(BsModRsrcTbl) >= BSMOD_MAX_HANDLES )
  {
    BSMOD_LOG_ERROR(hndVConn, BS_ECODE_NO_RSRC,
        "InUseCount=%u: No more resources available.",
        BSMOD_RSRC_INUSE_COUNT(BsModRsrcTbl));
    return -BS_ECODE_NO_RSRC;
  }
  else if( BSMOD_RSRC_IS_INUSE(BsModRsrcTbl, hndVConn) )
  {
    BSMOD_LOG_ERROR(hndVConn, BS_ECODE_BUSY, 
        "Module virtual connection handle already in use");
    return -BS_ECODE_BUSY;
  }
   Search for an existing opened device control block. If this is a first 
   reference to the device, then create a shared control block and open
   the actual device.
  if( (pCtlBlk = bsModCtlBlkFind(sDevUri)) == NULL )
  {
     allocate a new control block and initialize
    pCtlBlk = bsModCtlBlkNew(sDevUri, bTrace);
     open the device
    rc = i2c_open(&pCtlBlk->m_hndI2C, sDevUri);
    if( rc < 0 )
    {
      BSMOD_LOG_SYSERROR(hndVConn, "i2c_open(%p, %s).",
                                  &pCtlBlk->m_hndI2C, sDevUri);
      bsModCtlBlkDelete(pCtlBlk);
      return -BS_ECODE_SYS;
    }
  }
   bump device reference count
  pCtlBlk->m_uRefCnt++;
   add (a copy) to this module's resource table
  BsModCallbacks->m_cbModRsrcAdd(BsModRsrcTbl, hndVConn, pCtlBlk);
  LOGDIAG2("VConn=%d: %d=i2c_open(%p, dev=%s).",
      hndVConn, pCtlBlk->m_hndI2C.fd, &pCtlBlk->m_hndI2C, sDevUri);
  return pCtlBlk->m_hndI2C.fd;
}
int bsModOpen ( BsVConnHnd_T hndVConn , bool_t bTrace , const char * sDevUri , byte_t argbuf [ ] , size_t uArgSize ) { BsModCtlBlk_T * pCtlBlk ; int rc ; BSMOD_TRY_VCONN_HND_RANGE ( hndVConn ) ; if ( BSMOD_RSRC_INUSE_COUNT ( BsModRsrcTbl ) >= BSMOD_MAX_HANDLES ) { BSMOD_LOG_ERROR ( hndVConn , BS_ECODE_NO_RSRC , " " , BSMOD_RSRC_INUSE_COUNT ( BsModRsrcTbl ) ) ; return - BS_ECODE_NO_RSRC ; } else if ( BSMOD_RSRC_IS_INUSE ( BsModRsrcTbl , hndVConn ) ) { BSMOD_LOG_ERROR ( hndVConn , BS_ECODE_BUSY , " " ) ; return - BS_ECODE_BUSY ; } if ( ( pCtlBlk = bsModCtlBlkFind ( sDevUri ) ) == NULL ) { pCtlBlk = bsModCtlBlkNew ( sDevUri , bTrace ) ; rc = i2c_open ( & pCtlBlk -> m_hndI2C , sDevUri ) ; if ( rc < 0 ) { BSMOD_LOG_SYSERROR ( hndVConn , " " , & pCtlBlk -> m_hndI2C , sDevUri ) ; bsModCtlBlkDelete ( pCtlBlk ) ; return - BS_ECODE_SYS ; } } pCtlBlk -> m_uRefCnt ++ ; BsModCallbacks -> m_cbModRsrcAdd ( BsModRsrcTbl , hndVConn , pCtlBlk ) ; LOGDIAG2 ( " " , hndVConn , pCtlBlk -> m_hndI2C . fd , & pCtlBlk -> m_hndI2C , sDevUri ) ; return pCtlBlk -> m_hndI2C . fd ; }
\brief Open an \h_i2c device and associate with the given handle.
\ brief Open an \ h_i2c device and associate with the given handle .
// Check if handle is in valid range. // Check if resources are available. // Check if the handle in this module's resource table is not already in-use. // // Search for an existing opened device control block. If this is a first  // reference to the device, then create a shared control block and open // the actual device. // // allocate a new control block and initialize // open the device // bump device reference count // add (a copy) to this module's resource table