botsense/bsModules/bsI2C/bsI2CServer.c
static int bsModI2CReqRead(BsVConnHnd_T  hndVConn,
                           BsTid_T       uTid,
                           BsMsgId_T     uMsgIdReq,
                           byte_t        bufReq[],
                           size_t        uReqLen)
{
  static BsI2CMsgId_T  uMsgIdRsp = BsI2CMsgIdRspRead;

  BsModCtlBlk_T    *pCtlBlk;                      // resource control block
  BsI2CReqRead_T    msgReq;                       // request message 
  BsI2CRspRead_T    msgRsp;                       // response message 
  byte_t            bufRsp[BSPROXY_MSG_MAX_LEN];  // req/rsp buffer
  i2c_addr_t        addr;                         // \h_i2c device address
  int               n;                            // number of bytes/return code

  //
  // Parameter checks
  //
  if( !BSMOD_IS_VCONN_HANDLE(hndVConn) )
  {
    BSMOD_SEND_ERROR_RSP(BsModCallbacks, hndVConn, uTid, BS_ECODE_BAD_VCONN_HND,
          "Module vconn handle out-of-range.");
    return -BS_ECODE_BAD_VCONN_HND; \
  }

  // retrieve the resource control block
  pCtlBlk = (BsModCtlBlk_T *)BSMOD_RSRC(BsModRsrcTbl, hndVConn);

  if( pCtlBlk == NULL )
  {
    BSMOD_SEND_ERROR_RSP(BsModCallbacks, hndVConn, uTid, BS_ECODE_NO_VCONN,
        "No resources for virtual connection found.");
    return -BS_ECODE_NO_VCONN;
  }

  //
  // Unpack client request.
  //
  n = BsI2CUnpackReqRead(bufReq, uReqLen, &msgReq, pCtlBlk->m_bTrace);

  // check unpacking return code
  if( n < 0 )
  {
    BSMOD_SEND_NMERROR_RSP(BsModCallbacks, hndVConn, uTid, n,
        "MsgId=%u", uMsgIdReq);
    return -BS_ECODE_BAD_MSG;
  }

  addr = (i2c_addr_t)msgReq.m_addr;

  //
  // Execute client request.
  //
  n = i2c_read(&pCtlBlk->m_hndI2C, addr, msgRsp.m_readbuf.u.m_buf,
                        (uint_t)msgReq.m_readlen);

  // check operation return code
  if( n < 0 )
  {
    BSMOD_SEND_SYSERROR_RSP(BsModCallbacks, hndVConn, uTid,
        "i2c_read(fd=%d, addr=0x%02x, ...).", pCtlBlk->m_hndI2C.fd, addr);
    return -BS_ECODE_SYS;
  }

  LOGDIAG3("VConn=%d: %d=i2c_read(fd=%d, addr=0x%02x, buf=%p, count=%zu).",
      hndVConn, n, pCtlBlk->m_hndI2C.fd, addr, msgRsp.m_readbuf.u.m_buf,
      (size_t)msgReq.m_readlen);

  //
  // Pack server response.
  //
  msgRsp.m_readbuf.m_count = (byte_t)n;
    
  // pack
  n = BsI2CPackRspRead(&msgRsp, BSPROXY_BUF_BODY(bufRsp), pCtlBlk->m_bTrace);

  // check packing return code
  if( n < 0 )
  {
    BSMOD_SEND_NMERROR_RSP(BsModCallbacks, hndVConn, uTid, n,
        "MsgId=%u", uMsgIdRsp);
    return -BS_ECODE_BAD_MSG;
  }

  //
  // Send response.
  //
  BsModCallbacks->m_cbSendRsp(hndVConn, uTid, uMsgIdRsp, bufRsp, (size_t)n);

  return BS_OK;
}
/*!
 * \brief Service a \h_i2c read request.
 *
 * \note A module-specific request service must send a response.
 *
 * \param hndVConn  Virtual connection handle.
 * \param uTid      Request-Response transaction id.
 * \param uMsgIdReq Request message id.
 * \param bufReq    Packed request message buffer.
 * \param uReqLen   Size of request in buffer (number of bytes).
 *
 * \copydoc doc_return_std
 */
\brief Service a \h_i2c read request.
\note A module-specific request service must send a response.

\param hndVConn  Virtual connection handle.
\param uTid      Request-Response transaction id.
\param uMsgIdReq Request message id.
\param bufReq    Packed request message buffer.
\param uReqLen   Size of request in buffer (number of bytes).


\ brief Service a \ h_i2c read request . \ note A module - specific request service must send a response . \ param hndVConn Virtual connection handle . \ param uTid Request - Response transaction id . \ param uMsgIdReq Request message id . \ param bufReq Packed request message buffer . \ param uReqLen Size of request in buffer ( number of bytes ) .
static int bsModI2CReqRead(BsVConnHnd_T  hndVConn,
                           BsTid_T       uTid,
                           BsMsgId_T     uMsgIdReq,
                           byte_t        bufReq[],
                           size_t        uReqLen)
{
  static BsI2CMsgId_T  uMsgIdRsp = BsI2CMsgIdRspRead;
  BsModCtlBlk_T    *pCtlBlk;                      
  BsI2CReqRead_T    msgReq;                       
  BsI2CRspRead_T    msgRsp;                       
  byte_t            bufRsp[BSPROXY_MSG_MAX_LEN];  
  i2c_addr_t        addr;                         
  int               n;                            
   Parameter checks
  if( !BSMOD_IS_VCONN_HANDLE(hndVConn) )
  {
    BSMOD_SEND_ERROR_RSP(BsModCallbacks, hndVConn, uTid, BS_ECODE_BAD_VCONN_HND,
          "Module vconn handle out-of-range.");
    return -BS_ECODE_BAD_VCONN_HND; \
  }
   retrieve the resource control block
  pCtlBlk = (BsModCtlBlk_T *)BSMOD_RSRC(BsModRsrcTbl, hndVConn);
  if( pCtlBlk == NULL )
  {
    BSMOD_SEND_ERROR_RSP(BsModCallbacks, hndVConn, uTid, BS_ECODE_NO_VCONN,
        "No resources for virtual connection found.");
    return -BS_ECODE_NO_VCONN;
  }
   Unpack client request.
  n = BsI2CUnpackReqRead(bufReq, uReqLen, &msgReq, pCtlBlk->m_bTrace);
   check unpacking return code
  if( n < 0 )
  {
    BSMOD_SEND_NMERROR_RSP(BsModCallbacks, hndVConn, uTid, n,
        "MsgId=%u", uMsgIdReq);
    return -BS_ECODE_BAD_MSG;
  }
  addr = (i2c_addr_t)msgReq.m_addr;
   Execute client request.
  n = i2c_read(&pCtlBlk->m_hndI2C, addr, msgRsp.m_readbuf.u.m_buf,
                        (uint_t)msgReq.m_readlen);
   check operation return code
  if( n < 0 )
  {
    BSMOD_SEND_SYSERROR_RSP(BsModCallbacks, hndVConn, uTid,
        "i2c_read(fd=%d, addr=0x%02x, ...).", pCtlBlk->m_hndI2C.fd, addr);
    return -BS_ECODE_SYS;
  }
  LOGDIAG3("VConn=%d: %d=i2c_read(fd=%d, addr=0x%02x, buf=%p, count=%zu).",
      hndVConn, n, pCtlBlk->m_hndI2C.fd, addr, msgRsp.m_readbuf.u.m_buf,
      (size_t)msgReq.m_readlen);
   Pack server response.
  msgRsp.m_readbuf.m_count = (byte_t)n;
   pack
  n = BsI2CPackRspRead(&msgRsp, BSPROXY_BUF_BODY(bufRsp), pCtlBlk->m_bTrace);
   check packing return code
  if( n < 0 )
  {
    BSMOD_SEND_NMERROR_RSP(BsModCallbacks, hndVConn, uTid, n,
        "MsgId=%u", uMsgIdRsp);
    return -BS_ECODE_BAD_MSG;
  }
   Send response.
  BsModCallbacks->m_cbSendRsp(hndVConn, uTid, uMsgIdRsp, bufRsp, (size_t)n);
  return BS_OK;
}
static int bsModI2CReqRead ( BsVConnHnd_T hndVConn , BsTid_T uTid , BsMsgId_T uMsgIdReq , byte_t bufReq [ ] , size_t uReqLen ) { static BsI2CMsgId_T uMsgIdRsp = BsI2CMsgIdRspRead ; BsModCtlBlk_T * pCtlBlk ; BsI2CReqRead_T msgReq ; BsI2CRspRead_T msgRsp ; byte_t bufRsp [ BSPROXY_MSG_MAX_LEN ] ; i2c_addr_t addr ; int n ; if ( ! BSMOD_IS_VCONN_HANDLE ( hndVConn ) ) { BSMOD_SEND_ERROR_RSP ( BsModCallbacks , hndVConn , uTid , BS_ECODE_BAD_VCONN_HND , " " ) ; return - BS_ECODE_BAD_VCONN_HND ; } pCtlBlk = ( BsModCtlBlk_T * ) BSMOD_RSRC ( BsModRsrcTbl , hndVConn ) ; if ( pCtlBlk == NULL ) { BSMOD_SEND_ERROR_RSP ( BsModCallbacks , hndVConn , uTid , BS_ECODE_NO_VCONN , " " ) ; return - BS_ECODE_NO_VCONN ; } n = BsI2CUnpackReqRead ( bufReq , uReqLen , & msgReq , pCtlBlk -> m_bTrace ) ; if ( n < 0 ) { BSMOD_SEND_NMERROR_RSP ( BsModCallbacks , hndVConn , uTid , n , " " , uMsgIdReq ) ; return - BS_ECODE_BAD_MSG ; } addr = ( i2c_addr_t ) msgReq . m_addr ; n = i2c_read ( & pCtlBlk -> m_hndI2C , addr , msgRsp . m_readbuf . u . m_buf , ( uint_t ) msgReq . m_readlen ) ; if ( n < 0 ) { BSMOD_SEND_SYSERROR_RSP ( BsModCallbacks , hndVConn , uTid , " " , pCtlBlk -> m_hndI2C . fd , addr ) ; return - BS_ECODE_SYS ; } LOGDIAG3 ( " " , hndVConn , n , pCtlBlk -> m_hndI2C . fd , addr , msgRsp . m_readbuf . u . m_buf , ( size_t ) msgReq . m_readlen ) ; msgRsp . m_readbuf . m_count = ( byte_t ) n ; n = BsI2CPackRspRead ( & msgRsp , BSPROXY_BUF_BODY ( bufRsp ) , pCtlBlk -> m_bTrace ) ; if ( n < 0 ) { BSMOD_SEND_NMERROR_RSP ( BsModCallbacks , hndVConn , uTid , n , " " , uMsgIdRsp ) ; return - BS_ECODE_BAD_MSG ; } BsModCallbacks -> m_cbSendRsp ( hndVConn , uTid , uMsgIdRsp , bufRsp , ( size_t ) n ) ; return BS_OK ; }
\brief Service a \h_i2c read request.
\ brief Service a \ h_i2c read request .
// resource control block // request message  // response message  // req/rsp buffer // \h_i2c device address // number of bytes/return code // // Parameter checks // // retrieve the resource control block // // Unpack client request. // // check unpacking return code // // Execute client request. // // check operation return code // // Pack server response. // // pack // check packing return code // // Send response. //