1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*------------------------------------------------------------------------------------------------*/
/* UNICENS V2.1.0-3491 */
/* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG. */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/* You may also obtain this software under a propriety license from Microchip. */
/* Please contact Microchip for further information. */
/*------------------------------------------------------------------------------------------------*/
/*!
* \file
* \brief Implementation of Job classes
*
* \cond UCS_INTERNAL_DOC
* \addtogroup G_MGR
* @{
*/
/*------------------------------------------------------------------------------------------------*/
/* Includes */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_jobs.h"
#include "ucs_misc.h"
/*#include "ucs_scheduler.h"
#include "ucs_trace.h"*/
/*------------------------------------------------------------------------------------------------*/
/* Internal constants */
/*------------------------------------------------------------------------------------------------*/
static const uint8_t JBS_SRV_PRIO = 246U; /* parasoft-suppress MISRA2004-8_7 "Value shall be part of the module, not part of a function." */
/*------------------------------------------------------------------------------------------------*/
/* Internal prototypes */
/*------------------------------------------------------------------------------------------------*/
static void Jbs_Service(void *self);
static void Jbq_OnJobResult(void *self, void *data_ptr);
static bool Jbs_ForEachJbq(void *d_ptr, void *ud_ptr);
static bool Jbq_CheckState(CJobQ *self, CJob *job_ptr);
/*------------------------------------------------------------------------------------------------*/
/* CJobService Methods */
/*------------------------------------------------------------------------------------------------*/
void Jbs_Ctor(CJobService *self, CBase *base_ptr)
{
MISC_MEM_SET(self, 0, sizeof(*self));
self->base_ptr = base_ptr;
Dl_Ctor(&self->list, base_ptr->ucs_user_ptr);
Srv_Ctor(&self->service, JBS_SRV_PRIO, self, &Jbs_Service);
(void)Scd_AddService(&self->base_ptr->scd, &self->service);
}
void Jbs_RegisterJobQ(CJobService *self, CDlNode *job_q_node)
{
Dl_InsertTail(&self->list, job_q_node);
}
void Jbs_TriggerEvent(CJobService *self, Srv_Event_t id)
{
Srv_SetEvent(&self->service, id);
}
static bool Jbs_ForEachJbq(void *d_ptr, void *ud_ptr)
{
Srv_Event_t *event_ptr = (Srv_Event_t*)ud_ptr;
CJobQ *jobQ_ptr = (CJobQ*)d_ptr;
if ((*event_ptr & Jbq_GetEventId(jobQ_ptr)) != 0U)
{
Jbq_Service(jobQ_ptr);
}
return false; /* continue loop for all jobQs */
}
static void Jbs_Service(void *self)
{
CJobService *self_ = (CJobService *)self;
Srv_Event_t event_mask;
Srv_GetEvent(&self_->service, &event_mask); /* save and reset current events */
Srv_ClearEvent(&self_->service, event_mask);
Dl_Foreach(&self_->list, &Jbs_ForEachJbq, &event_mask); /* service jobQ with the corresponding event */
}
/*------------------------------------------------------------------------------------------------*/
/* CJobQ Methods */
/*------------------------------------------------------------------------------------------------*/
void Jbq_Ctor(CJobQ *self, CJobService *job_service_ptr, Srv_Event_t event_id, CJob *job_list[])
{
MISC_MEM_SET(self, 0, sizeof(*self));
self->job_service_ptr = job_service_ptr;
self->event_id = event_id;
self->job_list = job_list;
self->index = 0U;
self->state = JOB_S_STOPPED;
self->result = JOB_R_NA;
Dln_Ctor(&self->node, self);
Ssub_Ctor(&self->q_subject, 0U /*inst id*/);
Sobs_Ctor(&self->result_obs, self, &Jbq_OnJobResult);
Jbs_RegisterJobQ(self->job_service_ptr, &self->node);
}
Srv_Event_t Jbq_GetEventId(CJobQ *self)
{
return self->event_id;
}
void Jbq_Start(CJobQ *self, CSingleObserver *result_obs_ptr)
{
if (self->state != JOB_S_STARTED)
{
if (self->job_list[self->index] != NULL)
{
TR_INFO((0U, "[JBQ]", "Jbq_Start(): Starting job queue. Id: 0x%04X", 1U, self->event_id));
self->index = 0U;
self->state = JOB_S_STARTED;
self->result = JOB_R_NA;
(void)Ssub_AddObserver(&self->q_subject, result_obs_ptr); /* register observer for finished queue */
Job_Start(self->job_list[self->index], &self->result_obs); /* pass own observer for finished job */
}
else
{
TR_ERROR((0U, "[JBQ]", "Jbq_Start(): Invalid job list. Id: 0x%04X", 1U, self->event_id));
}
}
else
{
TR_ERROR((0U, "[JBQ]", "Jbq_Start(): JobQ already started. Id: 0x%04X", 1U, self->event_id));
}
}
void Jbq_Stop(CJobQ *self)
{
if (self->state == JOB_S_STARTED)
{
if (self->job_list[self->index] != NULL)
{
self->index = 0U;
self->state = JOB_S_STOPPED;
self->result = JOB_R_NA;
(void)Ssub_RemoveObserver(&self->q_subject);
Job_Stop(self->job_list[self->index]);
}
}
}
static void Jbq_OnJobResult(void *self, void *data_ptr)
{
CJobQ *self_ = (CJobQ *)self;
Job_Result_t *result_ptr = (Job_Result_t *)data_ptr;
if (self_->state == JOB_S_STARTED)
{
TR_INFO((0U, "[JBQ]", "Jbq_OnJobResult(): Receiving job result. event_id=0x%04X, result=0x%02X", 2U, self_->event_id, *result_ptr));
Jbs_TriggerEvent(self_->job_service_ptr, self_->event_id);
}
else
{
TR_INFO((0U, "[JBQ]", "Jbq_OnJobResult(): Receiving job result for stopped job. Id: 0x%04X", 1U, self_->event_id));
}
MISC_UNUSED(result_ptr);
}
static bool Jbq_CheckState(CJobQ *self, CJob *job_ptr)
{
bool ret = false;
if (self->state == JOB_S_STARTED)
{
if (job_ptr != NULL)
{
if ((Job_GetState(job_ptr) == JOB_S_FINISHED) && (Job_GetResult(job_ptr) != JOB_R_NA))
{
ret = true; /* job attributes are correct -> process */
}
}
else
{
TR_ERROR((0U, "[JBQ]", "Jbq_Service(): Invalid job list. Id: 0x%04X", 1U, self->event_id));
}
}
else
{
TR_ERROR((0U, "[JBQ]", "Jbq_Service(): JobQ not started. Id: 0x%04X", 1U, self->event_id));
}
return ret;
}
void Jbq_Service(CJobQ *self)
{
CJob *curr_job_ptr = self->job_list[self->index];
CJob *next_job_ptr = self->job_list[self->index + 1U];
if (Jbq_CheckState(self, curr_job_ptr))
{
if (curr_job_ptr != NULL)
{
Job_Result_t tmp_res = Job_GetResult(curr_job_ptr);
if ((next_job_ptr != NULL) && (tmp_res == JOB_R_SUCCESS)) /* job successfully and next job available */
{
self->index += 1U;
Job_Start(next_job_ptr, &self->result_obs);
}
else /* current job not successful or last job */
{
self->result = tmp_res; /* copy status from last job and finish */
self->state = JOB_S_FINISHED;
Ssub_Notify(&self->q_subject, &tmp_res, true/*auto-remove*/);
}
}
}
}
/*------------------------------------------------------------------------------------------------*/
/* CJob Methods */
/*------------------------------------------------------------------------------------------------*/
void Job_Ctor(CJob *self, Job_StartCb_t start_fptr, void *inst_ptr)
{
MISC_MEM_SET(self, 0, sizeof(*self));
self->start_fptr = start_fptr;
self->inst_ptr = inst_ptr;
self->state = JOB_S_STOPPED;
self->result = JOB_R_NA;
Ssub_Ctor(&self->subject, 0U /*ucs instance*/);
}
void Job_Start(CJob *self, CSingleObserver *result_obs_ptr)
{
if (self->state != JOB_S_STARTED)
{
TR_ASSERT(0U, "[JOB]", (self->start_fptr != NULL));
(void)Ssub_AddObserver(&self->subject, result_obs_ptr);
self->state = JOB_S_STARTED;
self->result = JOB_R_NA;
TR_INFO((0U, "[JOB]", "Job_Start(): starting job", 0U));
self->start_fptr(self->inst_ptr);
}
else
{
TR_INFO((0U, "[JOB]", "Job_Start(): ambiguous state during job start", 0U));
}
}
void Job_Stop(CJob *self)
{
self->state = JOB_S_STOPPED;
self->result = JOB_R_NA;
Ssub_RemoveObserver(&self->subject);
TR_INFO((0U, "[JOB]", "Job_Stop()", 0U));
}
void Job_SetResult(CJob *self, Job_Result_t result)
{
TR_INFO((0U, "[JOB]", "Job_SetResult(): result=%d", 1U, result));
if (self->state == JOB_S_STARTED)
{
self->state = JOB_S_FINISHED;
self->result = result;
Ssub_Notify(&self->subject, &result, true/*auto-remove*/);
MISC_UNUSED(self);
MISC_UNUSED(result);
}
else
{
TR_ERROR((0U, "[JOB]", "Job_SetResult(): called in ambiguous state=%d", 1U, self->state));
}
}
Job_State_t Job_GetState(CJob *self)
{
return self->state;
}
Job_Result_t Job_GetResult(CJob *self)
{
return self->result;
}
#if 0
/*------------------------------------------------------------------------------------------------*/
/* Initialization Methods */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of Manager class
* \param self The instance
* \param base_ptr Reference to base component
* \param inic_ptr Reference to INIC component
* \param net_ptr Reference to net component
* \param packet_bw Desired packet bandwidth
*/
void Mgr_Ctor(CManager *self, CBase *base_ptr, CInic *inic_ptr, CNetworkManagement *net_ptr, uint16_t packet_bw)
{
MISC_MEM_SET(self, 0, sizeof(*self));
self->base_ptr = base_ptr;
self->inic_ptr = inic_ptr;
self->net_ptr = net_ptr;
self->packet_bw = packet_bw;
Srv_Ctor(&self->service, MGR_SRV_PRIO, self, &Mgr_Service); /* register service */
(void)Scd_AddService(&self->base_ptr->scd, &self->service);
Mobs_Ctor(&self->event_observer, self, EH_E_INIT_SUCCEEDED, &Mgr_OnInitComplete);
Eh_AddObsrvInternalEvent(&self->base_ptr->eh, &self->event_observer);
Sobs_Ctor(&self->startup_obs, self, &Mgr_OnNwStartupResult);
Sobs_Ctor(&self->shutdown_obs, self, &Mgr_OnNwShutdownResult);
Mobs_Ctor(&self->nwstatus_mobs, self, MGR_NWSTATUS_MASK, &Mgr_OnNwStatus);
Fsm_Ctor(&self->fsm, self, &(mgr_state_tbl[0][0]), (uint8_t)MGR_EV_MAX_NUM_EVENTS, MGR_EV_NIL/*init.event*/);
}
#endif
/*!
* @}
* \endcond
*/
/*------------------------------------------------------------------------------------------------*/
/* End of file */
/*------------------------------------------------------------------------------------------------*/
|