36 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 41 oC_Stream_Type_t Type;
48 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 55 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 59 oC_Stream_t oC_Stream_New(
Allocator_t Allocator , AllocationFlags_t Flags , oC_Stream_Type_t Type ,
const char * Name , oC_Driver_t Driver ,
const void * Config )
64 ((Type & oC_Stream_Type_Input) || (Type & oC_Stream_Type_Output)) &&
65 isaddresscorrect(Config) &&
66 isaddresscorrect(Name)
69 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
70 void * context =
NULL;
72 errorCode = oC_Driver_Configure(Driver,Config,&context);
74 if(oC_ErrorOccur(errorCode))
76 oC_SaveError(Driver->FileName , errorCode);
80 stream = kmalloc(
sizeof(
struct Stream_t) , Allocator , Flags);
86 stream->Driver = Driver;
87 stream->Config = Config;
88 stream->DriverContext = context;
93 oC_SaveError(Driver->FileName,oC_ErrorCode_AllocationError);
105 bool deleted =
false;
107 if(oC_Stream_IsCorrect(*Stream))
109 (*Stream)->ObjectControl = 0;
111 deleted = kfree(*Stream,AllocationFlags_CanWaitForever);
135 bool oC_Stream_IsType(
oC_Stream_t Stream , oC_Stream_Type_t Type )
137 return oC_Stream_IsCorrect(Stream) && ((Stream->Type & Type) == Type);
142 oC_ErrorCode_t oC_Stream_Write(
oC_Stream_t Stream ,
const char * Buffer , uint32_t * Size , oC_IoFlags_t IoFlags )
144 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
146 if(oC_Stream_IsCorrect(Stream) ==
false && (IoFlags & oC_IoFlags_WriteToStdError))
148 Stream = oC_StreamMan_GetStdErrorStream();
152 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_ObjectNotCorrect ) &&
153 oC_AssignErrorCodeIfFalse(&errorCode , isram(Size) , oC_ErrorCode_OutputAddressNotInRAM ) &&
154 oC_AssignErrorCodeIfFalse(&errorCode , *Size > 0 , oC_ErrorCode_SizeNotCorrect ) &&
155 oC_AssignErrorCodeIfFalse(&errorCode , Stream->Type & oC_Stream_Type_Output , oC_ErrorCode_StreamTypeNotCorrect )
158 oC_Time_t timeout = oC_IoFlags_GetTimeout(IoFlags);
159 uint32_t bytesToSend = *Size;
160 uint32_t sentBytes = 0;
162 while(sentBytes < (*Size))
164 errorCode = oC_Driver_Write(Stream->Driver,Stream->DriverContext,&Buffer[sentBytes],&bytesToSend,timeout);
166 sentBytes += bytesToSend;
167 bytesToSend = (*Size) - sentBytes;
169 if(oC_ErrorOccur(errorCode) &&
170 errorCode != oC_ErrorCode_Timeout &&
171 errorCode != oC_ErrorCode_NoAllBytesRead &&
172 errorCode != oC_ErrorCode_NotAllSent
179 errorCode = oC_ErrorCode_None;
209 oC_ErrorCode_t oC_Stream_Read(
oC_Stream_t Stream ,
char * outBuffer , uint32_t * Size , oC_IoFlags_t IoFlags )
211 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
214 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_ObjectNotCorrect ) &&
215 oC_AssignErrorCodeIfFalse(&errorCode , Stream->Type & oC_Stream_Type_Input , oC_ErrorCode_StreamTypeNotCorrect ) &&
216 oC_AssignErrorCodeIfFalse(&errorCode , isram(Size) , oC_ErrorCode_OutputAddressNotInRAM ) &&
217 oC_AssignErrorCodeIfFalse(&errorCode , *Size > 0 , oC_ErrorCode_SizeNotCorrect )
220 oC_Time_t timeout = oC_IoFlags_GetTimeout(IoFlags);
221 uint32_t bytesToRead = *Size;
222 uint32_t readBytes = 0;
224 while(readBytes < (*Size))
226 errorCode = oC_Driver_Read(Stream->Driver,Stream->DriverContext,&outBuffer[readBytes],&bytesToRead,timeout);
228 readBytes += bytesToRead;
229 bytesToRead = (*Size) - readBytes;
231 if(oC_ErrorOccur(errorCode) &&
232 errorCode != oC_ErrorCode_Timeout &&
233 errorCode != oC_ErrorCode_NoAllBytesRead &&
234 errorCode != oC_ErrorCode_NoneElementReceived
239 else if(errorCode != oC_ErrorCode_Timeout)
241 errorCode = oC_ErrorCode_None;
244 if(readBytes == 0 &&
oC_Bits_IsAtLeastOneBitSetU32(IoFlags,oC_IoFlags_WaitForSomeElements | oC_IoFlags_WaitForAllElements | oC_IoFlags_NoTimeout))
266 oC_ErrorCode_t oC_Stream_ClearReadBuffer(
oC_Stream_t Stream )
268 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
271 ErrorCondition( oC_Stream_IsCorrect(Stream) , oC_ErrorCode_ObjectNotCorrect )
272 && ErrorCondition( Stream->Type & oC_Stream_Type_Input , oC_ErrorCode_StreamTypeNotCorrect )
273 && ErrorCondition( isaddresscorrect(Stream->Driver) , oC_ErrorCode_WrongAddress )
276 errorCode = oC_Driver_HandleIoctl(Stream->Driver, Stream->DriverContext, oC_IoCtl_SpecialCommand_ClearRxFifo,
NULL);
284 const char * oC_Stream_GetName(
oC_Stream_t Stream )
286 const char * name =
"unknown";
288 if(oC_Stream_IsCorrect(Stream))
298 oC_Driver_t oC_Stream_GetDriver(
oC_Stream_t Stream )
300 oC_Driver_t driver =
NULL;
302 if(oC_Stream_IsCorrect(Stream))
304 driver = Stream->Driver;
311 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ static bool oC_Bits_AreBitsSetU32(uint32_t BitMask, uint32_t BitsToCheck)
checks if all bits in field are set
The file with interface for stream.
identifier for allocations
static bool oC_Bits_IsAtLeastOneBitSetU32(uint32_t BitMask, uint32_t BitsToCheck)
checks if at least one of bits in field is set
The file with helper macros for managing objects.
The file with interface for stream manager.
uint32_t oC_ObjectControl_t
stores object control value
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
static const oC_Allocator_t Allocator
#define NULL
pointer to a zero