Choco OS  V.0.16.9.0
Join to the chocolate world
oc_portman.c
Go to the documentation of this file.
1 
27 #include <oc_portman.h>
28 #include <oc_list.h>
29 #include <oc_intman.h>
30 #include <oc_mutex.h>
31 #include <oc_event.h>
32 
38 #define _________________________________________TYPES_SECTION______________________________________________________________________________
39 
40 //==========================================================================================================================================
44 //==========================================================================================================================================
45 typedef struct
46 {
50 
51 //==========================================================================================================================================
55 //==========================================================================================================================================
56 typedef struct
57 {
58  oC_Module_t Module;
61  oC_List(PortReservation_t*) PortList;
63 
64 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
65 
71 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
72 
73 static ModuleRegistration_t* ModuleRegistration_New ( oC_Module_t Module , const oC_PortMan_Config_t * Config );
74 static bool ModuleRegistration_Delete ( ModuleRegistration_t * Registration );
75 static ModuleRegistration_t* GetModuleRegistration ( oC_Module_t Module );
78 static oC_PortMan_Port_t GetFreePort ( ModuleRegistration_t * Registration );
79 static bool WaitForFreePortRelease ( ModuleRegistration_t * Registration , oC_PortMan_Port_t * Port , oC_Time_t Timeout );
80 static bool WaitForPortRelease ( ModuleRegistration_t * Registration , oC_PortMan_Port_t * Port , oC_Time_t Timeout );
82 static bool PortReservation_Delete ( PortReservation_t * Reservation );
83 
84 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
85 
86 
92 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
93 
94 static oC_Mutex_t ModuleBusy = NULL;
95 static oC_List(ModuleRegistration_t*) RegistrationsList = NULL;
96 static oC_Allocator_t Allocator = {
97  .Name = "PortMan"
98 };
99 
100 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
101 
107 #define _________________________________________INTERFACE_SECTION__________________________________________________________________________
108 
109 //==========================================================================================================================================
125 //==========================================================================================================================================
126 oC_ErrorCode_t oC_PortMan_TurnOn( void )
127 {
128  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
129 
130  oC_IntMan_EnterCriticalSection();
131 
132  if(oC_Module_TurnOffVerification(&errorCode, oC_Module_PortMan))
133  {
134  if(ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied ))
135  {
136  RegistrationsList = oC_List_New( &Allocator, AllocationFlags_Default );
137  ModuleBusy = oC_Mutex_New( oC_Mutex_Type_Recursive, &Allocator, AllocationFlags_Default );
138 
139  if(
140  ErrorCondition( RegistrationsList != NULL, oC_ErrorCode_AllocationError )
141  && ErrorCondition( ModuleBusy != NULL, oC_ErrorCode_AllocationError )
142  )
143  {
144  oC_Module_TurnOn(oC_Module_PortMan);
145  errorCode = oC_ErrorCode_None;
146  }
147  else
148  {
149  bool listDeleted = RegistrationsList == NULL || oC_List_Delete( RegistrationsList, AllocationFlags_Default );
150  bool mutexDeleted = ModuleBusy == NULL || oC_Mutex_Delete( &ModuleBusy, AllocationFlags_Default );
151 
152  oC_SaveIfFalse( "PortMan::TurnOn list - " , listDeleted , oC_ErrorCode_ReleaseError );
153  oC_SaveIfFalse( "PortMan::TurnOn mutex - " , mutexDeleted , oC_ErrorCode_ReleaseError );
154  }
155  }
156  }
157 
158  oC_IntMan_ExitCriticalSection();
159 
160  return errorCode;
161 }
162 
163 //==========================================================================================================================================
179 //==========================================================================================================================================
180 oC_ErrorCode_t oC_PortMan_TurnOff( void )
181 {
182  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
183 
184  oC_IntMan_EnterCriticalSection();
185 
186  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_PortMan))
187  {
188  if(ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied ))
189  {
190  bool allModulesDeleted = true;
191 
192  oC_Module_TurnOff(oC_Module_PortMan);
193 
194  foreach(RegistrationsList, registration)
195  {
196  allModulesDeleted = ModuleRegistration_Delete(registration) && allModulesDeleted;
197  }
198 
199  bool listDeleted = oC_List_Delete( RegistrationsList, AllocationFlags_Default );
200  bool mutexDeleted = oC_Mutex_Delete( &ModuleBusy, AllocationFlags_Default );
201 
202  if(ErrorCondition( listDeleted && mutexDeleted && allModulesDeleted, oC_ErrorCode_ReleaseError ))
203  {
204  errorCode = oC_ErrorCode_None;
205  }
206  }
207  }
208 
209  oC_IntMan_ExitCriticalSection();
210 
211  return errorCode;
212 }
213 
214 //==========================================================================================================================================
256 //==========================================================================================================================================
257 oC_ErrorCode_t oC_PortMan_RegisterModule( oC_Module_t Module , const oC_PortMan_Config_t * Config , oC_Time_t Timeout )
258 {
259  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
260 
261  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_PortMan))
262  {
263  if(
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 )
268  && ErrorCondition( Config->FirstDynamicPortNumber > 0 , oC_ErrorCode_PortNotCorrect )
269  && ErrorCondition( Config->FirstDynamicPortNumber <= Config->MaximumPortNumber , oC_ErrorCode_PortNotCorrect )
270  && ErrorCondition( Config->LastDynamicPortNumber > Config->FirstDynamicPortNumber , oC_ErrorCode_PortNotCorrect )
271  && ErrorCondition( Config->LastDynamicPortNumber <= Config->MaximumPortNumber , oC_ErrorCode_PortNotCorrect )
272  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
273  && ErrorCondition( GetModuleRegistration(Module) == NULL , oC_ErrorCode_ModuleAlreadyRegistered )
274  && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleIsBusy )
275  )
276  {
277  ModuleRegistration_t * registration = ModuleRegistration_New( Module, Config );
278 
279  if(ErrorCondition( registration , oC_ErrorCode_AllocationError ))
280  {
281  bool registrationAdded = oC_List_PushBack(RegistrationsList,registration,&Allocator);
282 
283  if(ErrorCondition(registrationAdded, oC_ErrorCode_CannotAddObjectToList))
284  {
285  errorCode = oC_ErrorCode_None;
286  }
287  else
288  {
289  oC_SaveIfFalse("PortMan::RegisterModule ", ModuleRegistration_Delete(registration), oC_ErrorCode_ReleaseError);
290  }
291  }
292 
293  oC_Mutex_Give(ModuleBusy);
294  }
295  }
296 
297  return errorCode;
298 }
299 
300 //==========================================================================================================================================
327 //==========================================================================================================================================
328 oC_ErrorCode_t oC_PortMan_UnregisterModule( oC_Module_t Module , oC_Time_t Timeout )
329 {
330  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
331  ModuleRegistration_t* registration = NULL;
332 
333  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_PortMan))
334  {
335  if(
336  ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
337  && ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
338  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
339  && ErrorCondition( (registration = GetModuleRegistration(Module)) != NULL , oC_ErrorCode_ModuleNotRegistered )
340  && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleIsBusy )
341  )
342  {
343  bool removedFromList = oC_List_RemoveAll(RegistrationsList,registration);
344  bool deleted = ModuleRegistration_Delete(registration);
345 
346  if(
347  ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList )
348  && ErrorCondition( deleted , oC_ErrorCode_ReleaseError )
349  )
350  {
351  errorCode = oC_ErrorCode_None;
352  }
353 
354  oC_Mutex_Give(ModuleBusy);
355  }
356  }
357 
358  return errorCode;
359 }
360 
361 //==========================================================================================================================================
388 //==========================================================================================================================================
389 oC_ErrorCode_t oC_PortMan_ReservePort( oC_Module_t Module , oC_PortMan_Port_t * Port , oC_Time_t Timeout )
390 {
391  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
392  ModuleRegistration_t* registration = NULL;
393  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
394 
395  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_PortMan))
396  {
397  if(
398  ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
399  && ErrorCondition( isram(Port) , oC_ErrorCode_AddressNotInRam )
400  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
401  && ErrorCondition( (registration = GetModuleRegistration(Module)) != NULL , oC_ErrorCode_ModuleNotRegistered )
402  && ErrorCondition( (*Port) <= registration->Config.MaximumPortNumber , oC_ErrorCode_PortNotCorrect )
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 )
405  )
406  {
407  PortReservation_t * reservation = PortReservation_New(*Port);
408 
409  if(ErrorCondition( reservation != NULL , oC_ErrorCode_AllocationError ))
410  {
411  bool pushed = oC_List_PushBack(registration->PortList, reservation, &Allocator);
412 
413  if(ErrorCondition( pushed , oC_ErrorCode_CannotAddObjectToList ))
414  {
415  errorCode = oC_ErrorCode_None;
416  }
417  else
418  {
419  oC_SaveIfFalse("PortMan::ReservePort - ", PortReservation_Delete(reservation), oC_ErrorCode_ReleaseError);
420  }
421  }
422 
423  oC_Mutex_Give(ModuleBusy);
424  }
425  }
426 
427  return errorCode;
428 }
429 
430 //==========================================================================================================================================
456 //==========================================================================================================================================
457 oC_ErrorCode_t oC_PortMan_ReleasePort( oC_Module_t Module , oC_PortMan_Port_t Port , oC_Time_t Timeout )
458 {
459  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
460  ModuleRegistration_t* registration = NULL;
461  PortReservation_t * reservation = NULL;
462  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
463 
464  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_PortMan))
465  {
466  if(
467  ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
468  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
469  && ErrorCondition( (registration = GetModuleRegistration(Module)) != NULL , oC_ErrorCode_ModuleNotRegistered )
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 )
474  )
475  {
476  bool removedFromList = oC_List_RemoveAll(registration->PortList,reservation);
477  bool deleted = PortReservation_Delete(reservation);
478 
479  if(
480  ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList )
481  && ErrorCondition( deleted , oC_ErrorCode_ReleaseError )
482  )
483  {
484  oC_Event_SetState(registration->PortReleasedEvent,Port);
485  errorCode = oC_ErrorCode_None;
486  }
487  oC_Mutex_Give(ModuleBusy);
488  }
489  }
490 
491  return errorCode;
492 }
493 
494 //==========================================================================================================================================
521 //==========================================================================================================================================
522 oC_ErrorCode_t oC_PortMan_ReleaseAllPortsOf( oC_Module_t Module , oC_Process_t Process , oC_Time_t Timeout )
523 {
524  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
525  ModuleRegistration_t* registration = NULL;
526  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
527  PortReservation_t * reservation = NULL;
528 
529  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_PortMan))
530  {
531  if(
532  ErrorCondition( Module < oC_Module_NumberOfModules , oC_ErrorCode_ModuleNotCorrect )
533  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
534  && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
535  && ErrorCondition( (registration = GetModuleRegistration(Module)) != NULL , oC_ErrorCode_ModuleNotRegistered )
536  && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleIsBusy )
537  )
538  {
539  errorCode = oC_ErrorCode_None;
540 
541  while((reservation = GetNextProcessReservation(registration,Process)) != NULL )
542  {
543  reservation->Process = getcurprocess();
544 
545  ErrorCode( oC_PortMan_ReleasePort( Module, reservation->Port, oC_KTime_CalculateTimeout(endTimestamp)) );
546  }
547 
548  oC_Mutex_Give(ModuleBusy);
549  }
550  }
551 
552  return errorCode;
553 }
554 
555 //==========================================================================================================================================
566 //==========================================================================================================================================
567 bool oC_PortMan_IsPortReserved( oC_Module_t Module , oC_PortMan_Port_t Port )
568 {
569  bool reserved = false;
570 
571  if(oC_SaveIfFalse("PortMan::IsPortReserved ", oC_Module_IsTurnedOn(oC_Module_PortMan), oC_ErrorCode_ModuleNotStartedYet))
572  {
573  ModuleRegistration_t* registration = NULL;
574 
575  if(
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 )
580  )
581  {
582  reserved = GetPortReservation(registration,Port) != NULL;
583  }
584  }
585 
586  return reserved;
587 }
588 
589 //==========================================================================================================================================
601 //==========================================================================================================================================
602 bool oC_PortMan_IsPortReservedBy( oC_Module_t Module , oC_PortMan_Port_t Port , oC_Process_t Process )
603 {
604  bool reserved = false;
605 
606  if(oC_SaveIfFalse("PortMan::IsPortReservedBy ", oC_Module_IsTurnedOn(oC_Module_PortMan), oC_ErrorCode_ModuleNotStartedYet))
607  {
608  ModuleRegistration_t* registration = NULL;
609 
610  if(
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 )
615  )
616  {
617  PortReservation_t * reservation = GetPortReservation(registration,Port);
618 
619  if(oC_SaveIfFalse("PortMan::IsPortReservedBy ", reservation , oC_ErrorCode_PortNotReserved))
620  {
621  reserved = reservation->Process == Process;
622  }
623  }
624  }
625 
626  return reserved;
627 }
628 
629 #undef _________________________________________INTERFACE_SECTION__________________________________________________________________________
630 
636 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
637 
638 //==========================================================================================================================================
642 //==========================================================================================================================================
643 static ModuleRegistration_t* ModuleRegistration_New( oC_Module_t Module , const oC_PortMan_Config_t * Config )
644 {
645  ModuleRegistration_t * registration = kmalloc( sizeof(ModuleRegistration_t), &Allocator, AllocationFlags_ZeroFill );
646 
647  if(registration != NULL)
648  {
649  registration->PortReleasedEvent = oC_Event_New(oC_Event_State_Inactive, &Allocator, AllocationFlags_Default);
650  registration->PortList = oC_List_New( &Allocator, AllocationFlags_Default );
651 
652  if(registration->PortReleasedEvent != NULL && registration->PortList != NULL)
653  {
654  registration->Module = Module;
655 
656  memcpy(&registration->Config, Config, sizeof(oC_PortMan_Config_t));
657  }
658  else
659  {
660  bool eventDeleted = registration->PortReleasedEvent == NULL || oC_Event_Delete(&registration->PortReleasedEvent, AllocationFlags_Default);
661  bool listDeleted = registration->PortList == NULL || oC_List_Delete (registration->PortList , AllocationFlags_Default);
662  bool registrationDeleted = kfree( registration, AllocationFlags_Default );
663 
664  oC_SaveIfFalse("PortMan::ModuleRegistration_New ", eventDeleted && listDeleted && registrationDeleted, oC_ErrorCode_ReleaseError);
665 
666  registration = NULL;
667  }
668  }
669 
670  return registration;
671 }
672 
673 //==========================================================================================================================================
677 //==========================================================================================================================================
678 static bool ModuleRegistration_Delete( ModuleRegistration_t * Registration )
679 {
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 );
683 
684  return eventDeleted && listDeleted && registrationDeleted;
685 }
686 
687 //==========================================================================================================================================
691 //==========================================================================================================================================
692 static ModuleRegistration_t* GetModuleRegistration( oC_Module_t Module )
693 {
694  ModuleRegistration_t * foundRegistration = NULL;
695 
696  foreach( RegistrationsList, registration )
697  {
698  if(registration->Module == Module)
699  {
700  foundRegistration = registration;
701  break;
702  }
703  }
704 
705  return foundRegistration;
706 }
707 //==========================================================================================================================================
711 //==========================================================================================================================================
713 {
714  PortReservation_t * foundReservation = NULL;
715 
716  foreach(Registration->PortList, reservation)
717  {
718  if(Port == reservation->Port)
719  {
720  foundReservation = reservation;
721  break;
722  }
723  }
724 
725  return foundReservation;
726 }
727 
728 //==========================================================================================================================================
732 //==========================================================================================================================================
734 {
735  PortReservation_t * foundReservation = NULL;
736 
737  foreach(Registration->PortList, reservation)
738  {
739  if(Process == reservation->Process)
740  {
741  foundReservation = reservation;
742  break;
743  }
744  }
745 
746  return foundReservation;
747 }
748 
749 //==========================================================================================================================================
753 //==========================================================================================================================================
755 {
756  oC_PortMan_Port_t freePort = 0;
757 
758  for( oC_PortMan_Port_t port = Registration->Config.FirstDynamicPortNumber; port < Registration->Config.LastDynamicPortNumber; port++ )
759  {
760  if(GetPortReservation(Registration,port) == NULL)
761  {
762  freePort = port;
763  break;
764  }
765  }
766 
767  /* If we still don't have a free port, we can try also the last dynamic port number (it is not in the loop to protect
768  * against infinit loops ) */
769  if(freePort == 0)
770  {
771  if(GetPortReservation(Registration,Registration->Config.LastDynamicPortNumber) == NULL)
772  {
773  freePort = Registration->Config.LastDynamicPortNumber;
774  }
775  }
776 
777  return freePort;
778 }
779 
780 //==========================================================================================================================================
784 //==========================================================================================================================================
785 static bool WaitForFreePortRelease( ModuleRegistration_t * Registration , oC_PortMan_Port_t * Port , oC_Time_t Timeout )
786 {
787  bool foundFreePort = false;
788  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
789  oC_Event_State_t state = 0;
790 
791  while(foundFreePort == false)
792  {
793  if(oC_Mutex_Take(ModuleBusy,oC_KTime_CalculateTimeout(endTimestamp)))
794  {
795  *Port = GetFreePort(Registration);
796 
797  oC_Mutex_Give(ModuleBusy);
798  oC_Event_ReadState(Registration->PortReleasedEvent,&state);
799 
800  if(*Port != 0)
801  {
802  foundFreePort = true;
803  break;
804  }
805  }
806  else
807  {
808  oC_SaveError("WaitForFreePortRelease ", oC_ErrorCode_ModuleBusy);
809  break;
810  }
811  if(!oC_Event_WaitForState(Registration->PortReleasedEvent, state, oC_Event_StateMask_DifferentThan, oC_KTime_CalculateTimeout(endTimestamp)))
812  {
813  oC_SaveError("WaitForFreePortRelease ", oC_ErrorCode_NoFreeSlots);
814  break;
815  }
816  }
817 
818  return foundFreePort;
819 }
820 
821 //==========================================================================================================================================
825 //==========================================================================================================================================
826 static bool WaitForPortRelease( ModuleRegistration_t * Registration , oC_PortMan_Port_t * Port , oC_Time_t Timeout )
827 {
828  bool portReady = false;
829  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
830 
831  if(*Port == 0)
832  {
833  portReady = WaitForFreePortRelease(Registration, Port, oC_KTime_CalculateTimeout(endTimestamp));
834  }
835  else
836  {
837  while(portReady == false)
838  {
839  if(oC_SaveIfFalse("PortMan::WaitForPortRelease ", oC_Mutex_Take( ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp) ), oC_ErrorCode_ModuleBusy))
840  {
841  portReady = GetPortReservation(Registration, *Port) == NULL;
842  oC_Mutex_Give(ModuleBusy);
843  }
844  else
845  {
846  break;
847  }
848  if(portReady == false)
849  {
850  if(!oC_Event_WaitForState(Registration->PortReleasedEvent, *Port, oC_Event_StateMask_Full, oC_KTime_CalculateTimeout(endTimestamp)))
851  {
852  oC_SaveError("PortMan::WaitForPortRelease ", oC_ErrorCode_PortBusy);
853  break;
854  }
855  }
856  }
857  }
858 
859  return portReady;
860 }
861 
862 //==========================================================================================================================================
866 //==========================================================================================================================================
868 {
869  PortReservation_t * reservation = kmalloc( sizeof(PortReservation_t), &Allocator, AllocationFlags_Default );
870 
871  if(reservation != NULL)
872  {
873  reservation->Port = Port;
874  reservation->Process = getcurprocess();
875  }
876 
877  return reservation;
878 }
879 
880 //==========================================================================================================================================
884 //==========================================================================================================================================
885 static bool PortReservation_Delete( PortReservation_t * Reservation )
886 {
887  return kfree(Reservation,AllocationFlags_Default);
888 }
889 
890 
891 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
892 
static ModuleRegistration_t * GetModuleRegistration(oC_Module_t Module)
looking for a registration of the given module
Definition: oc_portman.c:692
oC_Module_t Module
Module ID.
Definition: oc_portman.c:58
static PortReservation_t * GetPortReservation(ModuleRegistration_t *Registration, oC_PortMan_Port_t Port)
looking for a port reservation in the given module registration
Definition: oc_portman.c:712
uint32_t oC_PortMan_Port_t
stores the port number
Definition: oc_portman.h:54
oC_Process_t Process
Process that has reserved the port.
Definition: oc_portman.c:48
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
Definition: oc_event.c:245
const char * Name
Definition: oc_stdlib.h:161
static ModuleRegistration_t * ModuleRegistration_New(oC_Module_t Module, const oC_PortMan_Config_t *Config)
allocates memory for new module registration
Definition: oc_portman.c:643
identifier for allocations
Definition: oc_stdlib.h:159
oC_Event_t PortReleasedEvent
Port Released Event.
Definition: oc_portman.c:59
oC_PortMan_Port_t Port
Reserved port.
Definition: oc_portman.c:47
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
Definition: oc_portman.c:257
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 ...
Definition: oc_portman.c:733
oC_ErrorCode_t oC_PortMan_TurnOff(void)
turns off the module
Definition: oc_portman.c:180
static PortReservation_t * PortReservation_New(oC_PortMan_Port_t Port)
allocates memory for a port reservation
Definition: oc_portman.c:867
Module for managing port reservations.
static bool ModuleRegistration_Delete(ModuleRegistration_t *Registration)
releases memory for the given module registration
Definition: oc_portman.c:678
oC_ErrorCode_t oC_PortMan_TurnOn(void)
turns on the module
Definition: oc_portman.c:126
oC_PortMan_Port_t FirstDynamicPortNumber
The first port, that can be dynamically reserved (when the port to reserve is 0). ...
Definition: oc_portman.h:66
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
Definition: oc_portman.c:522
oC_PortMan_Config_t Config
Configuration of the module.
Definition: oc_portman.c:60
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
Definition: oc_module.h:121
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).
Definition: oc_portman.h:67
oC_PortMan_Port_t MaximumPortNumber
Maximum number of correct port.
Definition: oc_portman.h:65
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
Definition: oc_portman.c:754
The file with interface for mutex managing.
stores port reservation
Definition: oc_portman.c:45
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
Definition: oc_module.h:170
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
Definition: oc_portman.c:602
bool oC_PortMan_IsPortReserved(oC_Module_t Module, oC_PortMan_Port_t Port)
returns true if port is reserved
Definition: oc_portman.c:567
stores module registration
Definition: oc_portman.c:56
oC_ErrorCode_t oC_PortMan_UnregisterModule(oC_Module_t Module, oC_Time_t Timeout)
unregisters module in the port manager
Definition: oc_portman.c:328
stores configuration of the module
Definition: oc_portman.h:63
static bool WaitForPortRelease(ModuleRegistration_t *Registration, oC_PortMan_Port_t *Port, oC_Time_t Timeout)
waits for a port release
Definition: oc_portman.c:826
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
Definition: oc_module.h:155
static const oC_Allocator_t Allocator
Definition: oc_eth.c:152
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
Definition: oc_module.h:138
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
Definition: oc_portman.c:389
static bool PortReservation_Delete(PortReservation_t *Reservation)
releases memory of port reservation
Definition: oc_portman.c:885
static bool WaitForFreePortRelease(ModuleRegistration_t *Registration, oC_PortMan_Port_t *Port, oC_Time_t Timeout)
waits for release of free port
Definition: oc_portman.c:785
oC_ErrorCode_t oC_PortMan_ReleasePort(oC_Module_t Module, oC_PortMan_Port_t Port, oC_Time_t Timeout)
releases port
Definition: oc_portman.c:457
#define NULL
pointer to a zero
Definition: oc_null.h:37
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off
Definition: oc_module.h:185