void CGXDLMSAutoConnect::GetAttributeIndexToRead(std::vector<int>& attributes) { //LN is static and read only once. if (CGXDLMSObject::IsLogicalNameEmpty(m_LN)) { attributes.push_back(1); } //Mode if (CanRead(2)) { attributes.push_back(2); } //Repetitions if (CanRead(3)) { attributes.push_back(3); } //RepetitionDelay if (CanRead(4)) { attributes.push_back(4); } //CallingWindow if (CanRead(5)) { attributes.push_back(5); } //Destinations if (CanRead(6)) { attributes.push_back(6); } }
int CanConfig(unsigned char channel) { unsigned char temp; CanReset(channel); // reset CAN controller //------ Write Control Register (CR) CanWrite(channel, CR, 0x01); // Enter Reset Mode without setting RIE // and enter reset mode (Set Reset Request bit) //------ Write Bus Timing Register 0 & 1 (BTR0 & BTR1) // BTR can be accessed (read/write) if the reset mode is active CanWrite(channel, BTR0, CAN[channel].btr0); // Write Bus Timing Register 0 (BTR0) CanWrite(channel, BTR1, CAN[channel].btr1); // Write Bus Timing Register 1 (BTR1) temp = CanRead(channel, BTR0); if(temp != CAN[channel].btr0) // Read BTR0 and confirm it return(ERR_CONFIG); // fail to configure temp = CanRead(channel, BTR1); if(temp != CAN[channel].btr1) // Read BTR1 and confirm it return(ERR_CONFIG); // fail to configure //------ Write Acceptance Code Register (ACR) and // Acceptance Mask Register (AMR) CanWrite(channel, ACR, CAN[channel].acc_code); // Write ACR CanWrite(channel, AMR, CAN[channel].acc_mask); // Write AMR //------ Write Output Control Register (OCR) // Set Normal Output Mode & Push-pull dirver CanWrite(channel, OCR, 0xfa); return(ERR_OK); // successful }
void CGXDLMSActivityCalendar::GetAttributeIndexToRead(std::vector<int>& attributes) { //LN is static and read only once. if (CGXDLMSObject::IsLogicalNameEmpty(m_LN)) { attributes.push_back(1); } //CalendarNameActive if (CanRead(2)) { attributes.push_back(2); } //SeasonProfileActive if (CanRead(3)) { attributes.push_back(3); } //WeekProfileTableActive if (CanRead(4)) { attributes.push_back(4); } //DayProfileTableActive if (CanRead(5)) { attributes.push_back(5); } //CalendarNamePassive if (CanRead(6)) { attributes.push_back(6); } //SeasonProfilePassive if (CanRead(7)) { attributes.push_back(7); } //WeekProfileTablePassive if (CanRead(8)) { attributes.push_back(8); } //DayProfileTablePassive if (CanRead(9)) { attributes.push_back(9); } //Time. if (CanRead(10)) { attributes.push_back(10); } }
wxInputStream& wxInputStream::Read(void *buf, size_t size) { char *p = (char *)buf; m_lastcount = 0; size_t read = GetWBack(buf, size); for ( ;; ) { size -= read; m_lastcount += read; p += read; if ( !size ) { // we read the requested amount of data break; } if ( p != buf && !CanRead() ) { // we have already read something and we would block in OnSysRead() // now: don't do it but return immediately break; } read = OnSysRead(p, size); if ( !read ) { // no more data available break; } } return *this; }
size_t BufferStream::ReadDataTo(MemoryData& outData, DataReadingMode mode/*=DataReadingMode::AlwaysCopy*/)const { RETURN_ZERO_IF_FALSE(CanRead()); FlushOnReadWrite(StreamDataOperation::Read); size_t outPos = 0; size_t outSize = outData.Size(); //read left buffer data size_t bufferLeftLength = mBufferLength - mBuffer.Position(); if (bufferLeftLength != 0) { size_t readSize = Math::Min(bufferLeftLength, outSize); MemoryData tempData = MemoryData::FromStatic(outData.MutableData() + outPos, readSize); readSize = mBuffer.ReadDataTo(tempData, DataReadingMode::AlwaysCopy); outPos += readSize; outSize -= readSize; } //directly read to out data block per block size_t blockSize = mBuffer.Length(); size_t blockCount = outSize / blockSize; FOR_EACH_SIZE(i, blockCount) { MemoryData tempData = MemoryData::FromStatic(outData.MutableData() + outPos, blockSize); size_t readSize = mSourceStream->ReadDataTo(tempData); outPos += readSize; outSize -= readSize; if (readSize != blockSize) //last block { return outPos; } }
bool EzSockets::accept(EzSockets& socket) { if (!blocking && !CanRead()) return false; #if defined(HAVE_INET_NTOP) char buf[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &addr.sin_addr, buf, INET_ADDRSTRLEN); address = buf; #elif defined(HAVE_INET_NTOA) address = inet_ntoa(addr.sin_addr); #endif int length = sizeof(socket); socket.sock = ::accept(sock,(struct sockaddr*) &socket.addr, (socklen_t*) &length); lastCode = socket.sock; if (socket.sock <= 0) return false; socket.state = skCONNECTED; return true; }
size_t IStream::ReadToStream(size_t size, IStream& dest, size_t bufferSize/*=1024*/)const { RETURN_ZERO_IF_FALSE(CanRead() && dest.CanWrite()); if (dest.IsPtrAvailable())//could directly write { dest.ReserveLeftSize(size); byte* buffer = dest.MutablePtr(); MemoryData destBuffer = MemoryData::FromStatic(buffer, size); return ReadDataTo(destBuffer, DataReadingMode::AlwaysCopy); } else { //should use temp buffer size_t count = 0; size_t realBufferSize = Math::Min(LeftLength(), bufferSize, size); MemoryData tempBuffer = MemoryData::Alloc(realBufferSize); do { size_t readSize = Math::Min(size, realBufferSize); tempBuffer.ForceSetSize(readSize); readSize = ReadDataTo(tempBuffer, DataReadingMode::AlwaysCopy); BREAK_IF_ZERO(readSize); tempBuffer.ForceSetSize(readSize); count += dest.WriteData(tempBuffer); tempBuffer.ForceSetSize(realBufferSize); size -= readSize; } while (size > 0); return count; } }
// 接收数据 int RecvUnknowLen(int SocketFd, void *ptr, size_t nbytes, int nSecond, int nMiniSec) { ssize_t n = -1; if (nbytes > 0) { while ((n = recv(SocketFd, (char *)ptr, nbytes, 0)) < 0) { if (EWOULDBLOCK == errno) { if (nSecond || nMiniSec) { if (CanRead(SocketFd, nSecond, nMiniSec) <= 0) { break; } nSecond = 0;//nMiniSec只等一次 nMiniSec = 0; } else { break; } } else { //连接需要关闭 n = 0; break; } } } return n; }
void CGXDLMSAutoAnswer::GetAttributeIndexToRead(std::vector<int>& attributes) { //LN is static and read only once. if (CGXDLMSObject::IsLogicalNameEmpty(m_LN)) { attributes.push_back(1); } //Mode is static and read only once. if (!IsRead(2)) { attributes.push_back(2); } //ListeningWindow is static and read only once. if (!IsRead(3)) { attributes.push_back(3); } //Status is not static. if (CanRead(4)) { attributes.push_back(4); } //NumberOfCalls is static and read only once. if (!IsRead(5)) { attributes.push_back(5); } //NumberOfRingsInListeningWindow is static and read only once. if (!IsRead(6)) { attributes.push_back(6); } }
// CAN 메세지를 보내는 함수 int CanSendMsg(unsigned char channel, unsigned short id, unsigned char *data, unsigned char dlc, unsigned char rtr) { unsigned char v, i; //RtWprintf(L"\n>>> ID:%x Tx_Data:%x Tx_DLC:%x", id, data[0], dlc); v = id >> 3; CanWrite(channel, TXID1, v); // write Identifier (ID.10 .. ID.3) v = id & 0x07; // write Identifier (ID.2 .. ID.0) to b7..b5 v <<= 5; if(rtr==1) v |= 0x10; // check Remote Transmit Request (RTR) bit v += dlc; // add Data Length Code (DLC) CanWrite(channel, TXID2, v); // Write Identifier unsigned char 2 for(i=0; i<dlc; i++) CanWrite(channel, TXDATA+i, *(data+i)); // write Data unsigned char // Write TR of Command Register (CMR) CanWrite(channel, CMR, 0x01); // Set Transmission Request (TR): message will be transmitted while(1) { v = CanRead(channel, SR); // Read Status Register (SR) if(v & 0x40) // If Error Status(ES) bit is set return(ERR_SEND); // fail to send if(v & 0x08) // when Transmission Complete Status (TCS) bit is set return(ERR_OK); // return (=successful) } }
void CBPacket::Read(void* buf, long size) { if (size < 1 || !CanRead(size)) return ; memcpy(buf, m_buf + m_ptr, size); m_ptr += size; }
// Read a boolean value bool BufferedSocket::ReadBool(bool &val) { if (!CanRead(1)) { wxLogDebug(_T("ReadBool: End of buffer reached!")); val = false; m_BadRead = true; return false; } wxDataInputStream dis(*m_ReceiveBufferHandler); dis.BigEndianOrdered(BigEndian); wxInt8 Value = 0; Value = dis.Read8(); if (Value < 0 || Value > 1) { wxLogDebug(_T("ReadBool: Value is not 0 or 1, possibly corrupted packet")); val = false; m_BadRead = true; return false; } val = Value ? true : false; return true; }
// CAN Reset 함수 void CanReset(unsigned char channel) { unsigned char temp; temp = CanRead(channel, 0x0100); CanWrite(channel, 0x0100, temp); Sleep(100); // wait 100ms }
ezSockets * ezSockets::Accept() { if (!bBlocking && !CanRead()) return NULL; sockaddr_in local_addr; int length = sizeof(local_addr); int localsock = ::accept(sock,(struct sockaddr*) &local_addr, (socklen_t*) &length); if ( localsock == SOCKET_ERROR ) return NULL; ezSockets * sNew = new ezSockets; #if !( defined (_WIN32) || defined( _XBOX ) ) char buf[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &local_addr.sin_addr, buf, INET_ADDRSTRLEN); sNew->address = buf; #else sNew->address = inet_ntoa(local_addr.sin_addr); #endif lastCode = localsock; sNew->bBlocking = bBlocking; sNew->lastCode = 0; sNew->mode = mode; sNew->pDataInHead = NULL; sNew->sock = localsock; sNew->state = skCONNECTED; return sNew; }
bool ezSockets::Accept(ezSockets& socket) { if (!bBlocking && !CanRead()) return false; int length = sizeof(socket); socket.sock = ::accept(sock,(struct sockaddr*) &socket.addr, (socklen_t*) &length); #if !( defined (_WIN32) || defined( _XBOX ) ) char buf[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &socket.addr.sin_addr, buf, INET_ADDRSTRLEN); address = buf; #else address = inet_ntoa(socket.addr.sin_addr); #endif lastCode = socket.sock; if ( socket.sock == SOCKET_ERROR ) return false; socket.state = skCONNECTED; return true; }
/* * return value * = 0 recv failed * > 0 bytes recv * = -1 net dead */ int CG_NetSocket::Recv(char *buf, int len) { if (CanRead()==false) return 0; int ret; /* in linux be careful of SIGPIPE */ ret = recv(m_socket,buf,len,0); if (ret==0) { /* remote closed */ return -1; } if (ret==SOCKET_ERROR) { int err=GETERROR; if (err!=WSAEWOULDBLOCK) { return -1; } } return ret; }
size_t IStream::ReadDataToString(WHeapString& outString, int readCount/*=0*/, bool withNullTermitated /*= false*/)const { RETURN_ZERO_IF_FALSE(CanRead()); uintp readSize = 0; if (readCount == 0) { readSize = Math::Min(outString.LeftLength()*sizeof(wchar_t), LeftLength()); } else if (readCount < 0) { readSize = LeftLength(); outString.ReserveLeftLength(readSize/sizeof(wchar_t)); } else { readSize = Math::Min((size_t)readCount, LeftLength()); outString.ReserveLeftLength(readSize/sizeof(wchar_t)); } MemoryData outData = MemoryData::FromStatic((const byte*)outString.LeftPtr(), readSize*sizeof(wchar_t)); size_t count = ReadDataTo(outData, DataReadingMode::AlwaysCopy); if (withNullTermitated) { count -= sizeof(wchar_t); } outString.ForceAppendLength(count / sizeof(wchar_t)); return count; }
/***************************************************************************** * FUNCTION - ReadMpcTime * DESCRIPTION: IFlashReader implementation ****************************************************************************/ void FlashBlock::ReadMpcTime(MpcTime* pMpcTime) { if (CanRead(9)) { // read data part if (ReadBool(false)) { pMpcTime->SetDate(YEAR, ReadU8((U8)pMpcTime->GetDate(YEAR))+2000); pMpcTime->SetDate(MONTH, ReadU8((U8)pMpcTime->GetDate(MONTH))); pMpcTime->SetDate(DAY, ReadU8((U8)pMpcTime->GetDate(DAY))); pMpcTime->SetDate(DAY_OF_WEEK, ReadU8((U8)pMpcTime->GetDate(DAY_OF_WEEK))); } else { pMpcTime->SetDateInValid(); mReadPos += 4; mReadAvailable -= 4; } // read time part if (ReadBool(false)) { pMpcTime->SetTime(HOURS, ReadU8((U8)pMpcTime->GetTime(HOURS))); pMpcTime->SetTime(MINUTES, ReadU8((U8)pMpcTime->GetTime(MINUTES))); pMpcTime->SetTime(SECONDS, ReadU8((U8)pMpcTime->GetTime(SECONDS))); } else { pMpcTime->SetTimeInValid(); mReadPos += 3; mReadAvailable -= 3; } } }
// Read a null terminated string bool BufferedSocket::ReadString(wxString &str) { if (!CanRead(1)) { wxLogDebug(_T("ReadString: End of buffer reached!")); str = wxT(""); m_BadRead = true; return false; } wxDataInputStream dis(*m_ReceiveBufferHandler); dis.BigEndianOrdered(BigEndian); wxString in_str; // ooh, a priming read! wxChar ch = (wxChar)dis.Read8(); bool IsRead = CanRead(1); while (ch != '\0' && IsRead) { in_str << ch; ch = (wxChar)dis.Read8(); IsRead = CanRead(1); } if (!IsRead) { wxLogDebug(_T("ReadString: End of buffer reached!")); str = wxT(""); m_BadRead = true; return false; } str = in_str; return true; }
float SDLFile::ReadFloat() { ASSERT(IsOpen()); ASSERT(CanRead()); float buffer; SDL_RWread(m_fp, &buffer, sizeof(float), 1); return buffer; }
int32_t FileStream::ReadByte() { if (CanRead()) { return fgetc(_file); } return -1; }
bool CGXDLMSObject::IsRead(int index) { if (!CanRead(index)) { return true; } return m_ReadTimes.find(index) != m_ReadTimes.end(); }
int64_t SDLFile::ReadLong() { ASSERT(IsOpen()); ASSERT(CanRead()); int64_t buffer; SDL_RWread(m_fp, &buffer, sizeof(int64_t), 1); return buffer; }
uint8_t SDLFile::ReadUnsignedChar() { ASSERT(IsOpen()); ASSERT(CanRead()); uint8_t buffer; SDL_RWread(m_fp, &buffer, sizeof(uint8_t), 1); return buffer; }
float DiskFile::ReadFloat() { ASSERT(IsOpen()); ASSERT(CanRead()); float buffer; fread(&buffer, sizeof(float), 1, m_fp); return buffer; }
int16_t DiskFile::ReadShort() { ASSERT(IsOpen()); ASSERT(CanRead()); int16_t buffer; fread(&buffer, sizeof(int16_t), 1, m_fp); return buffer; }
uint8_t DiskFile::ReadUnsignedChar() { ASSERT(IsOpen()); ASSERT(CanRead()); uint8_t buffer; fread(&buffer, sizeof(uint8_t), 1, m_fp); return buffer; }
double DiskFile::ReadDouble() { ASSERT(IsOpen()); ASSERT(CanRead()); double buffer; fread(&buffer, sizeof(double), 1, m_fp); return buffer; }
size_t FileStream::Read(void *buffer, size_t size) { if (CanRead()) { return fread(buffer, sizeof(uint8_t), size, _file); } return 0; }
double SDLFile::ReadDouble() { ASSERT(IsOpen()); ASSERT(CanRead()); double buffer; SDL_RWread(m_fp, &buffer, sizeof(double), 1); return buffer; }