PRUint16 FCGXStream::RecvUint16() { // Try fast path where both bytes are already in the buffer. if (mRecvBytesLeftInBlock >= 2 && mRecvIn >= mRecvOut + 2) { g_recv.LogContents(mRecvBuf + mRecvOut, 2); PRUint16 val = (mRecvBuf[mRecvOut] << 8) | mRecvBuf[mRecvOut + 1]; mRecvOut += 2; mRecvBytesLeftInBlock -= 2; return val; } // Slower, general case. PRUint8 val = RecvByte(); return (val << 8) | RecvByte(); }
/** * @brief Description "模拟I2C读RTC8025函数" * @param pdata 读取的数据缓冲区指针 * @param addr 读取的器件内存地址 * @param count 读取的器件数据大小 * @retval bool RTC8025读函数是否成功 */ bool RTC8025_Read(uint8_t *pData, uint16_t addr, uint16_t count) { uint8_t addr_h = 0, addr_l = 0; uint16_t i; uAddress = AI2C_RX8025_ADDRESS; addr_l = ((addr % 16) << 4); addr_h = uAddress + addr_h; if (count == 0) { return true; } //! START condition sent by Master if (!Start()) return false; //! Slave address + write specification SendByte(addr_h); //! Confirmation response from Master if (!WaitAck()) { Stop(); return false; } // theAI2C.SendByte(addr_h); // theAI2C.WaitAck(); SendByte(addr_l); WaitAck(); //! RESTART condition sent by Master Start(); //! Indicates next byte will be read SendByte((addr_h) | 0x01); WaitAck(); //! Data is read from the specified start address for (i = 0; i < count - 1; i ++) { RecvByte((pData + i)); Ack(false); } RecvByte(pData + count - 1); //! Master does not respond Ack(true);//接收结束,发送非应答信号 //! STOP condition sent by Master Stop(); return true; }
/** * @brief Description "模拟I2C读函数" * @param pdata 读取的数据缓冲区指针 * @param addr 读取的器件内存地址 * @param count 读取的器件数据大小 * @retval bool I2C读函数是否成功 */ bool AI2C_Read(uint8_t *pData, uint16_t addr, uint16_t count) { uint8_t device_addr = 0; uint8_t addr_msb = 0, addr_lsb = 0; uint16_t i; uAddress = AI2C_FRAM_ADDRESS; // CAI2C theAI2C(0xA0); /* 从机ID + 器件选择 + 读写选择 */ device_addr = uAddress + 0x0 + 0; addr_msb = ((addr / 256)&0X3F); addr_lsb = (addr % 256); if (count == 0) { return true; } if (!Start()) return false; SendByte(device_addr); if (!WaitAck()) { Stop(); return false; } SendByte(addr_msb); WaitAck(); SendByte(addr_lsb); WaitAck(); Start(); SendByte(device_addr|0X01); WaitAck(); for (i = 0; i < count - 1; i ++) { RecvByte((pData + i)); Ack(false); } RecvByte(pData + count - 1); Ack(true);//接收结束,发送非应答信号 Stop(); return true; }
NS_IMETHODIMP FCGXStream::NextRequest(PRInt32 *_retval) { //TODO : return a different error code indicating protocol error char error[256]; if (!RecvNextBlock(PR_FALSE)) { g_recv.LogProtocolError(mRecvBuf + mRecvOut, mRecvIn - mRecvOut); return NS_ERROR_ABORT; } if (mRecvCurBlockType != bt_BeginRequest) { LogFatal("Expected BeginRequest not received."); g_recv.LogProtocolError(mRecvBuf + mRecvOut, mRecvIn - mRecvOut); CloseConnection(true); return NS_ERROR_ABORT; } // Verify protocol version. PRUint16 version = RecvUint16(); *_retval = RecvByte(); PRBool bCanHandleRequest = (*_retval == RT_Handler || *_retval == RT_Filter); PRBool bCanHandleProtocol = (version == JAXER_PROTOCOL_VERSION || version == 3); if (!bCanHandleProtocol) { sprintf(error, "Connector protocol version (%d) does not match jaxer's (%d). You need to update the web connector or Jaxer.", version, JAXER_PROTOCOL_VERSION); LogFatal(error); } if (!bCanHandleRequest) { sprintf(error, "Jaxer does not understand the request type (%d). You need to update the web connector or Jaxer.", *_retval); LogFatal(error); } mPostDataAfterEndRequest = PR_FALSE; /* * Send back a msg indicating whether we are ok to handle the request */ SendBlockType(bt_BeginRequest); //SendUint16(JAXER_PROTOCOL_VERSION); // protocol version SendUint16(bCanHandleProtocol? version : JAXER_PROTOCOL_VERSION); // protocol version SendByte((bCanHandleRequest && bCanHandleProtocol) ? 1 : 0); //whether we can handle the protocol if (!bCanHandleRequest || !bCanHandleProtocol) { //optionally, we can provide error code (INT32, A short text msg) if (!bCanHandleProtocol) { SendUint16(bre_ProtocolVersionMustBeTheSame); }else { SendUint16(bre_CannotHandleRequestType); } SendUint16(strlen(error)); SendBytes(error, strlen(error)); } nsresult rc = Flush(!bCanHandleRequest || !bCanHandleProtocol); if ( rc != NS_OK) return rc; return (mFD != INVALID_SOCKET && (bCanHandleRequest && bCanHandleProtocol))? NS_OK : NS_ERROR_ABORT; }