40 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 47 oC_DefaultString_t Name;
48 oC_Service_MainFunction_t MainFunction;
49 oC_Service_StartFunction_t StartFunction;
50 oC_Service_StopFunction_t StopFunction;
51 oC_MemorySize_t MainThreadStackSize;
52 oC_Process_Priority_t Priority;
53 oC_MemorySize_t HeapMapSize;
56 oC_Module_RequiredArray_t RequiredModules;
59 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 66 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 71 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 80 #define _________________________________________GLOBALS_SECTION____________________________________________________________________________ 86 #undef _________________________________________GLOBALS_SECTION____________________________________________________________________________ 95 #define _________________________________________INTERFACE_SECTION__________________________________________________________________________ 114 oC_Stream_t stream = oC_StreamMan_GetStdErrorStream();
117 oC_SaveIfFalse(
"Service::New: Registration - " , isaddresscorrect(Registration) , oC_ErrorCode_WrongAddress )
118 && oC_SaveIfFalse(
"Service::New: Name - " , isaddresscorrect(Registration->Name) , oC_ErrorCode_WrongAddress )
119 && oC_SaveIfFalse(
"Service::New: Name - " , strlen(Registration->Name) > 0 , oC_ErrorCode_StringIsEmpty )
120 && oC_SaveIfFalse(
"Service::New: StartFunction - " , isaddresscorrect(Registration->StartFunction) , oC_ErrorCode_WrongAddress )
121 && oC_SaveIfFalse(
"Service::New: StopFunction - " , isaddresscorrect(Registration->StopFunction) , oC_ErrorCode_WrongAddress )
122 && oC_SaveIfFalse(
"Service::New: MainFunction - " , isaddresscorrect(Registration->MainFunction) , oC_ErrorCode_WrongAddress )
126 service = kmalloc(
sizeof(
struct Service_t), &Allocator, AllocationFlags_ZeroFill );
128 if(oC_SaveIfFalse(
"Service::New: Main object - ", service !=
NULL, oC_ErrorCode_AllocationError))
131 strncpy(service->Name, Registration->Name,
sizeof(service->Name));
133 user = user ==
NULL ? getrootuser() : user;
135 service->Process = oC_Process_New( Registration->Priority, service->Name, user, Registration->HeapMapSize,
NULL, stream, stream);
136 service->MainThread = oC_Thread_New ( Registration->Priority, Registration->MainThreadStackSize, service->Process, service->Name, (oC_Thread_Function_t)
ServiceThread, service );
137 service->MainFunction = Registration->MainFunction;
138 service->StartFunction = Registration->StartFunction;
139 service->StopFunction = Registration->StopFunction;
140 service->MainThreadStackSize = Registration->MainThreadStackSize;
141 service->Priority = Registration->Priority;
142 service->HeapMapSize = Registration->HeapMapSize;
143 service->Context =
NULL;
144 service->Started =
false;
146 memcpy(service->RequiredModules,Registration->RequiredModules,
sizeof(service->RequiredModules));
149 oC_SaveIfFalse(
"Service::New: Process - ", oC_Process_IsCorrect(service->Process) , oC_ErrorCode_AllocationError )
150 && oC_SaveIfFalse(
"Service::New: Thread - " , oC_Thread_IsCorrect(service->MainThread) , oC_ErrorCode_AllocationError )
155 bool allocationLimitSet = oC_Process_SetAllocationLimit(service->Process, Registration->AllocationLimit);
156 oC_SaveIfFalse(
"Service::New: Cannot set allocation limit ", allocationLimitSet, oC_ErrorCode_InternalDataAreDamaged);
177 bool deleted =
false;
180 oC_SaveIfFalse(
"Service::Delete ", isram(Service) , oC_ErrorCode_AddressNotInRam )
181 && oC_SaveIfFalse(
"Service::Delete ",
oC_Service_IsCorrect(*Service) , oC_ErrorCode_ObjectNotCorrect )
184 oC_IntMan_EnterCriticalSection();
188 bool memoryReleased = kfree(service, AllocationFlags_Default);
189 bool processKilled = oC_Process_Kill( process );
190 bool threadDeleted = oC_Thread_Delete(&thread);
191 bool processDeleted = oC_Process_Delete(&process);
196 oC_SaveIfFalse(
"Service::Delete: Main object - ", memoryReleased, oC_ErrorCode_ReleaseError )
197 && oC_SaveIfFalse(
"Service::Delete: Process - " , processKilled , oC_ErrorCode_CannotKillProcess )
198 && oC_SaveIfFalse(
"Service::Delete: Thread - " , threadDeleted , oC_ErrorCode_ReleaseError )
199 && oC_SaveIfFalse(
"Service::Delete: Process - " , processDeleted, oC_ErrorCode_ReleaseError )
205 oC_IntMan_ExitCriticalSection();
218 return isram(Service) &&
oC_CheckObjectControl(Service, oC_ObjectId_Service, Service->ObjectControl );
234 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
235 oC_Stream_t stream = oC_StreamMan_GetStdErrorStream();
239 && ErrorCondition(
oC_Service_IsActive(Service) ==
false , oC_ErrorCode_ServiceAlreadyStarted )
243 if( oC_Process_IsCorrect(Service->Process) ==
false || oC_Thread_IsCorrect(Service->MainThread) ==
false )
245 Service->Process = oC_Process_New( Service->Priority, Service->Name, getcuruser(), Service->HeapMapSize,
NULL, stream, stream);
246 Service->MainThread = oC_Thread_New ( Service->Priority, Service->MainThreadStackSize, Service->Process, Service->Name, (oC_Thread_Function_t)
ServiceThread, Service );
249 ErrorCondition( oC_Process_IsCorrect(Service->Process) , oC_ErrorCode_AllocationError )
250 && ErrorCondition( oC_Thread_IsCorrect(Service->MainThread) , oC_ErrorCode_AllocationError )
251 && ErrorCode ( oC_ProcessMan_AddProcess(Service->Process) )
252 && ErrorCondition( oC_Thread_Run( Service->MainThread ) , oC_ErrorCode_CannotRunThread )
255 errorCode = oC_ErrorCode_None;
275 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
282 oC_IntMan_EnterCriticalSection();
285 Service->Started ==
false 287 ErrorCondition( isaddresscorrect(Service->StopFunction), oC_ErrorCode_WrongAddress )
288 && ErrorCode ( Service->StopFunction( &Service->Context ) )
292 if( ErrorCode( oC_Process_Kill(Service->Process) ) )
294 Service->Process =
NULL;
295 Service->MainThread =
NULL;
296 errorCode = oC_ErrorCode_None;
300 oC_IntMan_ExitCriticalSection();
323 const char * name =
"incorrect service";
327 name = Service->Name;
344 process = Service->Process;
350 #undef _________________________________________INTERFACE_SECTION__________________________________________________________________________ 357 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 367 bool allEnabled =
true;
369 oC_ARRAY_FOREACH_IN_ARRAY(Service->RequiredModules,moduleReference)
371 if((*moduleReference) > oC_Module_None && (*moduleReference) < oC_Module_NumberOfModules)
375 kdebuglog( oC_LogType_Info,
"Cannot start service %s - module ID = %d is not enabled", Service->Name, *moduleReference );
392 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
395 oC_SaveIfFalse(
"ServiceThread: Service is not correct - ",
oC_Service_IsCorrect(Service) , oC_ErrorCode_ObjectNotCorrect )
396 && oC_SaveIfFalse(
"ServiceThread: StartFunction - " , isaddresscorrect(Service->StartFunction) , oC_ErrorCode_WrongAddress )
397 && oC_SaveIfFalse(
"ServiceThread: MainFunction - " , isaddresscorrect(Service->MainFunction) , oC_ErrorCode_WrongAddress )
398 && oC_SaveIfFalse(
"ServiceThread: StopFunction - " , isaddresscorrect(Service->StopFunction) , oC_ErrorCode_WrongAddress )
401 if(oC_SaveIfErrorOccur(Service->Name, errorCode = Service->StartFunction( &Service->Context )))
403 kdebuglog( oC_LogType_Track ,
"The service '%s' has been started", Service->Name );
404 oC_SaveIfErrorOccur(Service->Name, errorCode = Service->MainFunction ( Service->Context ));
405 kdebuglog( oC_ErrorOccur(errorCode) ? oC_LogType_Error : oC_LogType_Track,
"The service '%s' has finished with status '%s'", Service->Name,
oC_GetErrorString(errorCode));
406 oC_SaveIfErrorOccur(Service->Name, errorCode = Service->StopFunction ( &Service->Context ));
407 kdebuglog( oC_ErrorOccur(errorCode) ? oC_LogType_Error : oC_LogType_Track,
"The service '%s' has been stopped with status: '%s'", Service->Name,
oC_GetErrorString(errorCode));
411 kdebuglog( oC_LogType_Error,
"Cannot start service '%s' because of error '%s'", Service->Name ,
oC_GetErrorString(errorCode));
416 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
identifier for allocations
The file with interface for process manager.
oC_Service_t oC_Service_New(const oC_Service_Registration_t *Registration)
allocates memory for a new service
bool oC_Service_IsCorrect(oC_Service_t Service)
returns true if the service object is correct
oC_ErrorCode_t oC_Service_Stop(oC_Service_t Service)
stops the service
const char * oC_Service_GetName(oC_Service_t Service)
returns name of the service
The file with helper macros for managing objects.
static bool AreRequiredModulesEnabled(oC_Service_t Service)
checks if all required modules are enabled
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
The file with interface for stream manager.
uint32_t oC_ObjectControl_t
stores object control value
bool oC_Service_Delete(oC_Service_t *Service)
releases memory allocated for a service
The file with interface for interrupt manager.
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
bool oC_Service_IsActive(oC_Service_t Service)
returns true if the service is correct and active
The file with interface for process mechanism.
The file with interface for string library.
interface for handling services
const char * oC_GetErrorString(oC_ErrorCode_t ErrorCode)
oC_ErrorCode_t oC_Service_Start(oC_Service_t Service)
starts the service
oC_Process_t oC_Service_GetProcess(oC_Service_t Service)
returns process associated with the service
#define NULL
pointer to a zero
static void ServiceThread(oC_Service_t Service)
main thread for services