@@ -664,24 +664,62 @@ function generateFmi2FunctionsC(id: string, nVars: number, nStates: number, resu
664664 lines . push (
665665 "fmi2Status fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, size_t nCategories, const fmi2String categories[]) { (void)c; (void)loggingOn; (void)nCategories; (void)categories; return fmi2OK; }" ,
666666 ) ;
667+ // ── FMU State Save/Restore ──
668+ lines . push ( "/* --- FMU State Management --- */" ) ;
669+ lines . push ( "fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate* state) {" ) ;
670+ lines . push ( " FMUInstance* inst = (FMUInstance*)c;" ) ;
671+ lines . push ( " FMUInstance* copy = (FMUInstance*)malloc(sizeof(FMUInstance));" ) ;
672+ lines . push ( " if (!copy) return fmi2Error;" ) ;
673+ lines . push ( " memcpy(copy, inst, sizeof(FMUInstance));" ) ;
674+ lines . push ( " *state = (fmi2FMUstate)copy;" ) ;
675+ lines . push ( " return fmi2OK;" ) ;
676+ lines . push ( "}" ) ;
677+ lines . push ( "" ) ;
678+ lines . push ( "fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate state) {" ) ;
679+ lines . push ( " if (!state) return fmi2Error;" ) ;
680+ lines . push ( " FMUInstance* inst = (FMUInstance*)c;" ) ;
681+ lines . push ( " /* Preserve the callback pointers — they belong to the master, not the snapshot */" ) ;
682+ lines . push ( " fmi2CallbackFunctions savedCb = inst->callbacks;" ) ;
683+ lines . push ( " fmi2String savedName = inst->instanceName;" ) ;
684+ lines . push ( " memcpy(inst, (FMUInstance*)state, sizeof(FMUInstance));" ) ;
685+ lines . push ( " inst->callbacks = savedCb;" ) ;
686+ lines . push ( " inst->instanceName = savedName;" ) ;
687+ lines . push ( " return fmi2OK;" ) ;
688+ lines . push ( "}" ) ;
689+ lines . push ( "" ) ;
690+ lines . push ( "fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate* state) {" ) ;
691+ lines . push ( " (void)c;" ) ;
692+ lines . push ( " if (!state || !*state) return fmi2Error;" ) ;
693+ lines . push ( " free(*state);" ) ;
694+ lines . push ( " *state = NULL;" ) ;
695+ lines . push ( " return fmi2OK;" ) ;
696+ lines . push ( "}" ) ;
697+ lines . push ( "" ) ;
698+ lines . push ( "fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate state, size_t* size) {" ) ;
699+ lines . push ( " (void)c; (void)state;" ) ;
700+ lines . push ( " if (!size) return fmi2Error;" ) ;
701+ lines . push ( " *size = sizeof(FMUInstance);" ) ;
702+ lines . push ( " return fmi2OK;" ) ;
703+ lines . push ( "}" ) ;
704+ lines . push ( "" ) ;
705+ lines . push ( "fmi2Status fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate state, fmi2Byte buf[], size_t size) {" ) ;
706+ lines . push ( " (void)c;" ) ;
707+ lines . push ( " if (!state || !buf || size < sizeof(FMUInstance)) return fmi2Error;" ) ;
708+ lines . push ( " memcpy(buf, (FMUInstance*)state, sizeof(FMUInstance));" ) ;
709+ lines . push ( " return fmi2OK;" ) ;
710+ lines . push ( "}" ) ;
711+ lines . push ( "" ) ;
667712 lines . push (
668- "fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate* state) { (void)c; (void)state; return fmi2Error; }" ,
669- ) ;
670- lines . push (
671- "fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate state) { (void)c; (void)state; return fmi2Error; }" ,
672- ) ;
673- lines . push (
674- "fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate* state) { (void)c; (void)state; return fmi2Error; }" ,
675- ) ;
676- lines . push (
677- "fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate state, size_t* size) { (void)c; (void)state; (void)size; return fmi2Error; }" ,
678- ) ;
679- lines . push (
680- "fmi2Status fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate state, fmi2Byte buf[], size_t size) { (void)c; (void)state; (void)buf; (void)size; return fmi2Error; }" ,
681- ) ;
682- lines . push (
683- "fmi2Status fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte buf[], size_t size, fmi2FMUstate* state) { (void)c; (void)buf; (void)size; (void)state; return fmi2Error; }" ,
713+ "fmi2Status fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte buf[], size_t size, fmi2FMUstate* state) {" ,
684714 ) ;
715+ lines . push ( " (void)c;" ) ;
716+ lines . push ( " if (!buf || !state || size < sizeof(FMUInstance)) return fmi2Error;" ) ;
717+ lines . push ( " FMUInstance* copy = (FMUInstance*)malloc(sizeof(FMUInstance));" ) ;
718+ lines . push ( " if (!copy) return fmi2Error;" ) ;
719+ lines . push ( " memcpy(copy, buf, sizeof(FMUInstance));" ) ;
720+ lines . push ( " *state = (fmi2FMUstate)copy;" ) ;
721+ lines . push ( " return fmi2OK;" ) ;
722+ lines . push ( "}" ) ;
685723 lines . push (
686724 "fmi2Status fmi2GetDirectionalDerivative(fmi2Component c, const fmi2ValueReference unknown[], size_t nUnknown, const fmi2ValueReference known[], size_t nKnown, const fmi2Real dvKnown[], fmi2Real dvUnknown[]) { (void)c; (void)unknown; (void)nUnknown; (void)known; (void)nKnown; (void)dvKnown; (void)dvUnknown; return fmi2Error; }" ,
687725 ) ;
0 commit comments