38 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 64 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 71 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 84 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 92 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 100 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 107 #define _________________________________________INTERFACE_SECTION__________________________________________________________________________ 128 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
130 oC_IntMan_EnterCriticalSection();
134 if(ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied ))
136 RegistrationsList = oC_List_New( &
Allocator, AllocationFlags_Default );
137 ModuleBusy = oC_Mutex_New( oC_Mutex_Type_Recursive, &
Allocator, AllocationFlags_Default );
140 ErrorCondition( RegistrationsList !=
NULL, oC_ErrorCode_AllocationError )
141 && ErrorCondition( ModuleBusy !=
NULL, oC_ErrorCode_AllocationError )
145 errorCode = oC_ErrorCode_None;
149 bool listDeleted = RegistrationsList ==
NULL || oC_List_Delete( RegistrationsList, AllocationFlags_Default );
150 bool mutexDeleted = ModuleBusy ==
NULL || oC_Mutex_Delete( &ModuleBusy, AllocationFlags_Default );
152 oC_SaveIfFalse(
"PortMan::TurnOn list - " , listDeleted , oC_ErrorCode_ReleaseError );
153 oC_SaveIfFalse(
"PortMan::TurnOn mutex - " , mutexDeleted , oC_ErrorCode_ReleaseError );
158 oC_IntMan_ExitCriticalSection();
182 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
184 oC_IntMan_EnterCriticalSection();
188 if(ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied ))
190 bool allModulesDeleted =
true;
194 foreach(RegistrationsList, registration)
199 bool listDeleted = oC_List_Delete( RegistrationsList, AllocationFlags_Default );
200 bool mutexDeleted = oC_Mutex_Delete( &ModuleBusy, AllocationFlags_Default );
202 if(ErrorCondition( listDeleted && mutexDeleted && allModulesDeleted, oC_ErrorCode_ReleaseError ))
204 errorCode = oC_ErrorCode_None;
209 oC_IntMan_ExitCriticalSection();
259 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
264 ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
265 && ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
266 && ErrorCondition( isaddresscorrect(Config) , oC_ErrorCode_WrongAddress )
267 && ErrorCondition( Config->
MaximumPortNumber > 0 , oC_ErrorCode_MaximumValueNotCorrect )
272 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
274 && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleIsBusy )
279 if(ErrorCondition( registration , oC_ErrorCode_AllocationError ))
281 bool registrationAdded = oC_List_PushBack(RegistrationsList,registration,&
Allocator);
283 if(ErrorCondition(registrationAdded, oC_ErrorCode_CannotAddObjectToList))
285 errorCode = oC_ErrorCode_None;
293 oC_Mutex_Give(ModuleBusy);
330 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
336 ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
337 && ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
338 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
340 && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleIsBusy )
343 bool removedFromList = oC_List_RemoveAll(RegistrationsList,registration);
347 ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList )
348 && ErrorCondition( deleted , oC_ErrorCode_ReleaseError )
351 errorCode = oC_ErrorCode_None;
354 oC_Mutex_Give(ModuleBusy);
391 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
393 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
398 ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
399 && ErrorCondition( isram(Port) , oC_ErrorCode_AddressNotInRam )
400 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
403 && ErrorCondition(
WaitForPortRelease(registration, Port, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_PortBusy )
404 && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleIsBusy )
409 if(ErrorCondition( reservation !=
NULL , oC_ErrorCode_AllocationError ))
411 bool pushed = oC_List_PushBack(registration->PortList, reservation, &
Allocator);
413 if(ErrorCondition( pushed , oC_ErrorCode_CannotAddObjectToList ))
415 errorCode = oC_ErrorCode_None;
419 oC_SaveIfFalse(
"PortMan::ReservePort - ",
PortReservation_Delete(reservation), oC_ErrorCode_ReleaseError);
423 oC_Mutex_Give(ModuleBusy);
459 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
462 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
467 ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
468 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
470 && ErrorCondition( (reservation =
GetPortReservation(registration,Port)) !=
NULL , oC_ErrorCode_PortNotReserved )
471 && ErrorCondition( Port <= registration->Config.
MaximumPortNumber , oC_ErrorCode_PortNotCorrect )
472 && ErrorCondition( reservation->
Process == getcurprocess() , oC_ErrorCode_PortReservedByDifferentProcess )
473 && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleIsBusy )
476 bool removedFromList = oC_List_RemoveAll(registration->PortList,reservation);
480 ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList )
481 && ErrorCondition( deleted , oC_ErrorCode_ReleaseError )
485 errorCode = oC_ErrorCode_None;
487 oC_Mutex_Give(ModuleBusy);
524 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
526 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
532 ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
533 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
534 && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
536 && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleIsBusy )
539 errorCode = oC_ErrorCode_None;
543 reservation->
Process = getcurprocess();
548 oC_Mutex_Give(ModuleBusy);
569 bool reserved =
false;
571 if(oC_SaveIfFalse(
"PortMan::IsPortReserved ",
oC_Module_IsTurnedOn(oC_Module_PortMan), oC_ErrorCode_ModuleNotStartedYet))
576 oC_SaveIfFalse(
"PortMan::IsPortReserved ", Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
577 && oC_SaveIfFalse(
"PortMan::IsPortReserved ", (registration =
GetModuleRegistration(Module)) !=
NULL , oC_ErrorCode_ModuleNotRegistered )
578 && oC_SaveIfFalse(
"PortMan::IsPortReserved ", Port <= registration->Config.
MaximumPortNumber , oC_ErrorCode_PortNotCorrect )
579 && oC_SaveIfFalse(
"PortMan::IsPortReserved ", Port > 0 , oC_ErrorCode_PortNotCorrect )
604 bool reserved =
false;
606 if(oC_SaveIfFalse(
"PortMan::IsPortReservedBy ",
oC_Module_IsTurnedOn(oC_Module_PortMan), oC_ErrorCode_ModuleNotStartedYet))
611 oC_SaveIfFalse(
"PortMan::IsPortReservedBy ", Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
612 && oC_SaveIfFalse(
"PortMan::IsPortReservedBy ", (registration =
GetModuleRegistration(Module)) !=
NULL , oC_ErrorCode_ModuleNotRegistered )
613 && oC_SaveIfFalse(
"PortMan::IsPortReservedBy ", Port <= registration->Config.
MaximumPortNumber , oC_ErrorCode_PortNotCorrect )
614 && oC_SaveIfFalse(
"PortMan::IsPortReservedBy ", Port > 0 , oC_ErrorCode_PortNotCorrect )
619 if(oC_SaveIfFalse(
"PortMan::IsPortReservedBy ", reservation , oC_ErrorCode_PortNotReserved))
621 reserved = reservation->
Process == Process;
629 #undef _________________________________________INTERFACE_SECTION__________________________________________________________________________ 636 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 647 if(registration !=
NULL)
650 registration->PortList = oC_List_New( &
Allocator, AllocationFlags_Default );
654 registration->
Module = Module;
661 bool listDeleted = registration->PortList ==
NULL || oC_List_Delete (registration->PortList , AllocationFlags_Default);
662 bool registrationDeleted = kfree( registration, AllocationFlags_Default );
664 oC_SaveIfFalse(
"PortMan::ModuleRegistration_New ", eventDeleted && listDeleted && registrationDeleted, oC_ErrorCode_ReleaseError);
680 bool eventDeleted = oC_Event_Delete(&Registration->
PortReleasedEvent, AllocationFlags_Default);
681 bool listDeleted = oC_List_Delete(Registration->PortList, AllocationFlags_Default);
682 bool registrationDeleted = kfree( Registration, AllocationFlags_Default );
684 return eventDeleted && listDeleted && registrationDeleted;
696 foreach( RegistrationsList, registration )
698 if(registration->Module == Module)
700 foundRegistration = registration;
705 return foundRegistration;
716 foreach(Registration->PortList, reservation)
718 if(Port == reservation->Port)
720 foundReservation = reservation;
725 return foundReservation;
737 foreach(Registration->PortList, reservation)
739 if(Process == reservation->Process)
741 foundReservation = reservation;
746 return foundReservation;
787 bool foundFreePort =
false;
788 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
789 oC_Event_State_t state = 0;
791 while(foundFreePort ==
false)
793 if(oC_Mutex_Take(ModuleBusy,oC_KTime_CalculateTimeout(endTimestamp)))
797 oC_Mutex_Give(ModuleBusy);
802 foundFreePort =
true;
808 oC_SaveError(
"WaitForFreePortRelease ", oC_ErrorCode_ModuleBusy);
813 oC_SaveError(
"WaitForFreePortRelease ", oC_ErrorCode_NoFreeSlots);
818 return foundFreePort;
828 bool portReady =
false;
829 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
837 while(portReady ==
false)
839 if(oC_SaveIfFalse(
"PortMan::WaitForPortRelease ", oC_Mutex_Take( ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp) ), oC_ErrorCode_ModuleBusy))
842 oC_Mutex_Give(ModuleBusy);
848 if(portReady ==
false)
852 oC_SaveError(
"PortMan::WaitForPortRelease ", oC_ErrorCode_PortBusy);
871 if(reservation !=
NULL)
873 reservation->
Port = Port;
874 reservation->
Process = getcurprocess();
887 return kfree(Reservation,AllocationFlags_Default);
891 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ static ModuleRegistration_t * GetModuleRegistration(oC_Module_t Module)
looking for a registration of the given module
oC_Module_t Module
Module ID.
static PortReservation_t * GetPortReservation(ModuleRegistration_t *Registration, oC_PortMan_Port_t Port)
looking for a port reservation in the given module registration
uint32_t oC_PortMan_Port_t
stores the port number
oC_Process_t Process
Process that has reserved the port.
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
static ModuleRegistration_t * ModuleRegistration_New(oC_Module_t Module, const oC_PortMan_Config_t *Config)
allocates memory for new module registration
identifier for allocations
oC_Event_t PortReleasedEvent
Port Released Event.
oC_PortMan_Port_t Port
Reserved port.
oC_ErrorCode_t oC_PortMan_RegisterModule(oC_Module_t Module, const oC_PortMan_Config_t *Config, oC_Time_t Timeout)
registers module in the port manager
static PortReservation_t * GetNextProcessReservation(ModuleRegistration_t *Registration, oC_Process_t Process)
looking for the next reservation of the port that is related with the given process ...
oC_ErrorCode_t oC_PortMan_TurnOff(void)
turns off the module
static PortReservation_t * PortReservation_New(oC_PortMan_Port_t Port)
allocates memory for a port reservation
Module for managing port reservations.
static bool ModuleRegistration_Delete(ModuleRegistration_t *Registration)
releases memory for the given module registration
oC_ErrorCode_t oC_PortMan_TurnOn(void)
turns on the module
oC_PortMan_Port_t FirstDynamicPortNumber
The first port, that can be dynamically reserved (when the port to reserve is 0). ...
oC_ErrorCode_t oC_PortMan_ReleaseAllPortsOf(oC_Module_t Module, oC_Process_t Process, oC_Time_t Timeout)
releases all ports reserved by a given process
oC_PortMan_Config_t Config
Configuration of the module.
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
The file with interface for event module.
oC_PortMan_Port_t LastDynamicPortNumber
Last port, that can be dynamically reserved (when the port to reserve is 0).
oC_PortMan_Port_t MaximumPortNumber
Maximum number of correct port.
The file with list library.
The file with interface for interrupt manager.
static oC_PortMan_Port_t GetFreePort(ModuleRegistration_t *Registration)
returns first free dynamic port
The file with interface for mutex managing.
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
bool oC_PortMan_IsPortReservedBy(oC_Module_t Module, oC_PortMan_Port_t Port, oC_Process_t Process)
checks if the given port is reserved by the given process
bool oC_PortMan_IsPortReserved(oC_Module_t Module, oC_PortMan_Port_t Port)
returns true if port is reserved
stores module registration
oC_ErrorCode_t oC_PortMan_UnregisterModule(oC_Module_t Module, oC_Time_t Timeout)
unregisters module in the port manager
stores configuration of the module
static bool WaitForPortRelease(ModuleRegistration_t *Registration, oC_PortMan_Port_t *Port, oC_Time_t Timeout)
waits for a port release
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
static const oC_Allocator_t Allocator
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
oC_ErrorCode_t oC_PortMan_ReservePort(oC_Module_t Module, oC_PortMan_Port_t *Port, oC_Time_t Timeout)
reserves a port of the given module
static bool PortReservation_Delete(PortReservation_t *Reservation)
releases memory of port reservation
static bool WaitForFreePortRelease(ModuleRegistration_t *Registration, oC_PortMan_Port_t *Port, oC_Time_t Timeout)
waits for release of free port
oC_ErrorCode_t oC_PortMan_ReleasePort(oC_Module_t Module, oC_PortMan_Port_t Port, oC_Time_t Timeout)
releases port
#define NULL
pointer to a zero
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off