@@ -48,6 +48,10 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs, const Fs
4848// Load income messages to sig printer
4949 sigprt->LoadMessages (msgs);
5050
51+ // save pointer to output file descriptor struct to
52+ // enable using this information inside class member functions
53+ fdesc = &fsd;
54+
5155std::sort (sigprt->sigs_expr .begin (), sigprt->sigs_expr .end (),
5256 [](const CiExpr_t* a,const CiExpr_t* b) ->bool
5357 {
@@ -177,7 +181,59 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs, const Fs
177181 fwriter->Flush (fsd.core_h .fpath );
178182
179183// 3 step is to print main source file
184+ // include main header file
185+ fwriter->AppendLine (PrintF (" #include" " %s" " " , fsd.core_h .fname .c_str ()),3 );
186+
187+ // put diagmonitor ifdef selection for including @drv-fmon header
188+ // with FMon_* signatures to call from unpack function
189+ fwriter->AppendLine (PrintF (" #ifdef %s" , fsd.usemon_def .c_str ()));
190+
191+ fwriter->AppendText (
192+ " // This file must define:\n "
193+ " // base monitor struct\n "
194+ " // function signature for CRC calculation\n "
195+ " // function signature for getting system tick value (100 us step)\n " );
196+
197+ fwriter->AppendLine (PrintF (" #include" " %s-fmon.h" " " , fsd.drvname .c_str ()),2 );
198+
199+ fwriter->AppendLine (PrintF (" #endif // %s" , fsd.usemon_def .c_str ()),3 );
180200
201+ // for each message 3 functions must be defined - 1 unpack function,
202+ // 2: pack with raw signature
203+ // 3: pack with canstruct
204+ for (size_t num =0 ; num < sigprt->sigs_expr .size (); num++)
205+ {
206+ // write message typedef s and additional expressions
207+ MessageDescriptor_t& m = sigprt->sigs_expr [num]->msg ;
208+
209+ // first function
210+ fwriter->AppendLine (
211+ PrintF (" uint32_t Unpack_%s_%s(%s_t* _m, const uint8_t* _d, uint8_t dlc_)\n {" ,
212+ m.Name .c_str (), fsd.DrvName_orig .c_str (), m.Name .c_str ()));
213+
214+ WriteUnpackBody (sigprt->sigs_expr [num]);
215+
216+ fwriter->AppendLine (" }" ,2 );
217+
218+
219+ fwriter->AppendLine (PrintF (" #ifdef %s" , fsd.usesruct_def .c_str ()));
220+
221+ // second function
222+ fwriter->AppendLine (
223+ PrintF (" uint32_t Pack_%s_%s(const %s_t* _m, __CoderDbcCanFrame_t__* cframe);" ,
224+ m.Name .c_str (), fsd.DrvName_orig .c_str (), m.Name .c_str ()));
225+
226+ fwriter->AppendLine (" #else" );
227+
228+ // third function
229+ fwriter->AppendLine (
230+ PrintF (" uint32_t Pack_%s_%s(const %s_t* _m, uint8_t* _d, uint8_t* _len, uint8_t* _ide);" ,
231+ m.Name .c_str (), fsd.DrvName_orig .c_str (), m.Name .c_str ()));
232+
233+ fwriter->AppendLine (PrintF (" #endif // %s" , fsd.usesruct_def .c_str ()),2 );
234+ }
235+
236+ fwriter->Flush (fsd.core_c .fpath );
181237// 4 step is to pring fmon head file
182238
183239// 5 step is to print fmon source file
@@ -237,3 +293,36 @@ void CiMainGenerator::WriteSigStructField(const SignalDescriptor_t& sig, bool bi
237293 fwriter->AppendLine (" " ,2 );
238294}
239295
296+ void CiMainGenerator::WriteUnpackBody (const CiExpr_t* sgs)
297+ {
298+ for (size_t num =0 ; num < sgs->to_signals .size (); num++)
299+ {
300+ auto expr = sgs->to_signals [num];
301+
302+ fwriter->AppendLine (PrintF (" _m->%s = %s;" , sgs->msg .Signals [num].Name .c_str (), expr.c_str ()));
303+ }
304+
305+ fwriter->AppendLine (" " );
306+
307+ fwriter->AppendLine (PrintF (" #ifdef %s" , fdesc->usemon_def .c_str ()));
308+
309+ fwriter->AppendLine (" // check DLC correctness" );
310+ fwriter->AppendLine (PrintF (" _m->mon1.dlc_error = (dlc_ < %s_DLC);" , sgs->msg .Name .c_str ()));
311+
312+
313+ // TODO: put CRC and ROLLING COUNTER tests here
314+ // 1
315+ // 2
316+
317+
318+ fwriter->AppendLine (" _m->mon1.last_cycle = GetSysTick();" );
319+ fwriter->AppendLine (" _m->mon1.frame_cnt++;" ,2 );
320+
321+ auto Fmon_func =" FMon_" + sgs->msg .Name +" _" + fdesc->drvname ;
322+
323+ fwriter->AppendLine (PrintF (" %s(&_m->mon1);" , Fmon_func.c_str ()));
324+
325+ fwriter->AppendLine (PrintF (" #endif // %s" , fdesc->usemon_def .c_str ()),2 );
326+
327+ fwriter->AppendLine (PrintF (" return %s_CANID;" , sgs->msg .Name .c_str ()));
328+ }