function dump_capi_data_mapping_info_code(file, chart)

global gChartInfo %#ok<GVMIS>

if ~gChartInfo.hasTestPoint
return;
end

% Dump data type info, dimensions info, dimensions array, fixed point
% info, fixed point slope and bias vector and the test point struct
% All the information is calculated in the MATLAB class Stateflow.Coder.TestPoints
%
% The general structure is as follows :
% The following maps Dimensions Info, Fixed point info and Data type
% info are the primary sources of information. The test point struct
% describes the data being test pointed and carries an index into each
% of the above maps. These maps might carry further indices. For example
% the Dimesnions map carries an index into the dimensions array (which
% is a flat list of dimensions). This index couple with numDims can be
% used to determine the dimensions of a testpoint. Similarly the fixed
% point map also contains indices into the slope and bias map for the
% slope and bias values.

fixPtValArrName = 'fixPtSlopeBiasVals';
tpObj = Stateflow.Coder.TestPoints(chart, fixPtValArrName);
dump_capi_data_type_map_struct(file, tpObj.uniqDataTypeInfo, tpObj.nonBusTpPresent);

if(tpObj.fixPtDataPresent)
% If test pointed data has fix point types, generate slope and bias
% map
dump_capi_fixed_point_value_array(file, tpObj.uniqFixPtSlopeBias, fixPtValArrName);
end
checksumVector = double((sf('get',chart,'chart.rtwChecksum')));
dump_capi_sample_time_map_struct(file);
dump_capi_dimension_map_struct(file, tpObj.uniqDimsInfo, tpObj.nonBusTpPresent);
dump_capi_test_point_signals_struct(file, tpObj.tpStruct, tpObj.nonBusTpPresent);
dump_capi_fixed_point_map_struct(file, tpObj.uniqFixPtInfo, tpObj.nonBusTpPresent);
dump_capi_dimension_array(file, tpObj.flatDimsList, tpObj.nonBusTpPresent);
if tpObj.busTpPresent
dump_sig_hier_logging_info(file, tpObj);
dump_sig_hier_child_idx(file, tpObj);
dump_bus_logging_info(file, tpObj);
end

dump_capi_data_mapping_static_info_struct(file, tpObj, checksumVector);

dump_capi_init_data_mapping_info_fcn(file, chart, tpObj);
return;

function dump_capi_string_map(file, map)

fprintf(file,'%s',' ');
if(~isempty(map))
fprintf(file,'%s,\n',map{1:end-1});
fprintf(file,'%s',map{end});
end

function dump_capi_string_map_v2(file, map)

fprintf(file,'%s',' ');
if(~isempty(map))
fprintf(file,'%s, ',map{1:end-1});
fprintf(file,'%s',map{end});
end

function dump_capi_data_type_map_struct(file, map, nonBusTpPresent)

fprintf(file,'\n');
if ~nonBusTpPresent
fprintf(file,'static const rtwCAPI_DataTypeMap dataTypeMap[] = { {"", "", 0, 0, 0, 0, 0, 0} };\n');
else
fprintf(file,'static const rtwCAPI_DataTypeMap dataTypeMap[] = {\n');
fprintf(file,' /* cName, mwName, numElements, elemMapIndex, dataSize, slDataId, isComplex, isPointer */\n');
dump_capi_string_map(file, map);
fprintf(file,'};\n');
end

return;

function dump_capi_fixed_point_value_array(file, fixPtValueMap, fixPtValArrName)

numEntries = length(fixPtValueMap);
fprintf(file,'\n');
fprintf(file,'static real_T %s[%.17g] = {\n',fixPtValArrName,numEntries);
strMap = cell(numEntries, 1);
for i = 1:numEntries
strMap{i} = sprintf('%g', fixPtValueMap(i));
end
dump_capi_string_map(file, strMap);
fprintf(file,'};\n');

return;

function dump_capi_fixed_point_map_struct(file, map, nonBusTpPresent)

fprintf(file,'\n');
if ~nonBusTpPresent
fprintf(file,'static const rtwCAPI_FixPtMap fixedPointMap[] = { {NULL, NULL, rtwCAPI_FIX_UNIFORM_SCALING, 0, 0, 0 } };\n');
else
fprintf(file,'static const rtwCAPI_FixPtMap fixedPointMap[] = {\n');
fprintf(file,' /* *fracSlope, *bias, scaleType, wordLength, exponent, isSigned */\n');
dump_capi_string_map(file, map);
fprintf(file,'};\n');
end

return;

function dump_capi_dimension_map_struct(file, strMap, nonBusTpPresent)

fprintf(file,'\n');
if ~nonBusTpPresent
fprintf(file,'static const rtwCAPI_DimensionMap dimensionMap[] = { {rtwCAPI_SCALAR, 0, 0, 0} };\n');
else
fprintf(file,'static const rtwCAPI_DimensionMap dimensionMap[] = {\n');
fprintf(file,' /* dataOrientation, dimArrayIndex, numDims*/\n');
dump_capi_string_map(file, strMap);
fprintf(file,'};\n');
end

return;

function dump_capi_dimension_array(file, strMap, nonBusTpPresent)

fprintf(file,'\n');
if ~nonBusTpPresent
fprintf(file,'static const uint_T dimensionArray[] = { 0 };\n');
else
fprintf(file,'static const uint_T dimensionArray[] = {\n');
dump_capi_string_map_v2(file, strMap);
fprintf(file,'};\n');
end
return;

% A dummy sample time map to satisfy !NULL assertion in floating scope code
function dump_capi_sample_time_map_struct(file)

fprintf(file,'\n');
fprintf(file,'static real_T sfCAPIsampleTimeZero = 0.0;\n');
fprintf(file,'static const rtwCAPI_SampleTimeMap sampleTimeMap[] = {\n');
fprintf(file,' /* *period, *offset, taskId, mode */\n');
fprintf(file,' {&sfCAPIsampleTimeZero, &sfCAPIsampleTimeZero, 0, 0}\n');
fprintf(file,'};\n');

return;

function dump_capi_test_point_signals_struct(file, strMap, nonBusTpPresent)
fprintf(file,'\n');
if ~nonBusTpPresent
fprintf(file,'static const rtwCAPI_Signals testPointSignals[] = { {0, 0, "", "", 0, 0, 0, 0, 0 } };\n');
else
fprintf(file,'static const rtwCAPI_Signals testPointSignals[] = {\n');
fprintf(file,' /* addrMapIndex, sysNum, SFRelativePath, dataName, portNumber, dataTypeIndex, dimIndex, fixPtIdx, sTimeIndex */\n');
dump_capi_string_map(file, strMap);
fprintf(file,'};\n');
end

return;

function dump_capi_data_mapping_static_info_struct(file, tpObj, checksumVector)
numTestPoints = tpObj.numTps - tpObj.numBusTps;

fprintf(file,'\n');
fprintf(file,'static rtwCAPI_ModelMappingStaticInfo testPointMappingStaticInfo = {\n');
fprintf(file,' /* block signal monitoring */\n');
fprintf(file,' {\n');
fprintf(file,' testPointSignals, /* Block signals Array */\n');
fprintf(file,' %.17g, /* Num Block IO signals */\n',numTestPoints);
fprintf(file,' NULL, /* Root Inputs Array */\n');
fprintf(file,' 0, /* Num root inputs */\n');
fprintf(file,' NULL, /* Root Outputs Array */\n');
fprintf(file,' 0 /* Num root outputs */\n');
fprintf(file,' },\n');
fprintf(file,'\n');
fprintf(file,' /* parameter tuning */\n');
fprintf(file,' {\n');
fprintf(file,' NULL, /* Block parameters Array */\n');
fprintf(file,' 0, /* Num block parameters */\n');
fprintf(file,' NULL, /* Variable parameters Array */\n');
fprintf(file,' 0 /* Num variable parameters */\n');
fprintf(file,' },\n');
fprintf(file,'\n');
fprintf(file,' /* block states */\n');
fprintf(file,' {\n');
fprintf(file,' NULL, /* Block States array */\n');
fprintf(file,' 0 /* Num Block States */\n');
fprintf(file,' },\n');
fprintf(file,'\n');
fprintf(file,' /* Static maps */\n');
fprintf(file,' {\n');
fprintf(file,' dataTypeMap, /* Data Type Map */\n');
fprintf(file,' dimensionMap, /* Data Dimension Map */\n');
fprintf(file,' fixedPointMap, /* Fixed Point Map */\n');
fprintf(file,' NULL, /* Structure Element map */\n');
fprintf(file,' sampleTimeMap, /* Sample Times Map */\n');
fprintf(file,' dimensionArray /* Dimension Array */ \n');
fprintf(file,' },\n');
fprintf(file,'\n');
fprintf(file,' /* Target type */\n');
fprintf(file,' "float",\n');
fprintf(file,' {\n');
fprintf(file,' %.17gU,\n',checksumVector(1));
fprintf(file,' %.17gU,\n',checksumVector(2));
fprintf(file,' %.17gU,\n',checksumVector(3));
fprintf(file,' %.17gU\n',checksumVector(4));
fprintf(file,' }\n');
if tpObj.busTpPresent
fprintf(file,' , &mmiStaticInfoLogging, 0\n');
end
fprintf(file,'};\n');

return;

function dump_capi_init_data_mapping_info_fcn(file, chart, tpObj)

global gTargetInfo gChartInfo %#ok<GVMIS>
tpInfoAccessFcns = sf('Cg', 'get_testpoint_accessfcn_names', chart);

fprintf(file,'\n');
fprintf(file,'static void init_test_point_mapping_info(SimStruct *S) {\n');
fprintf(file,' rtwCAPI_ModelMappingInfo *testPointMappingInfo;\n');
fprintf(file,' void **testPointAddrMap;\n');
if gTargetInfo.codingMultiInstance
fprintf(file,' %s *chartInstance = (%s *)sf_get_chart_instance_ptr(S);\n',gChartInfo.chartInstanceTypedef,gChartInfo.chartInstanceTypedef);
fprintf(file,' %s(chartInstance);\n',tpInfoAccessFcns.initAddrMapFcn);
fprintf(file,' testPointMappingInfo = %s(chartInstance);\n',tpInfoAccessFcns.mappingInfoAccessFcn);
fprintf(file,' testPointAddrMap = %s(chartInstance);\n',tpInfoAccessFcns.addrMapAccessFcn);
else
fprintf(file,'\n');
fprintf(file,' %s();\n',tpInfoAccessFcns.initAddrMapFcn);
fprintf(file,' testPointMappingInfo = %s();\n',tpInfoAccessFcns.mappingInfoAccessFcn);
fprintf(file,' testPointAddrMap = %s();\n',tpInfoAccessFcns.addrMapAccessFcn);
end
fprintf(file,'\n');
fprintf(file,' rtwCAPI_SetStaticMap(*testPointMappingInfo, &testPointMappingStaticInfo);\n');
if tpObj.busTpPresent
fprintf(file,' rtwCAPI_SetLoggingStaticMap(*testPointMappingInfo, &mmiStaticInfoLogging);\n');
else
fprintf(file,' rtwCAPI_SetLoggingStaticMap(*testPointMappingInfo, NULL);\n');
end
fprintf(file,' rtwCAPI_SetInstanceLoggingInfo(*testPointMappingInfo, NULL);\n');
fprintf(file,' rtwCAPI_SetPath(*testPointMappingInfo, "");\n');
fprintf(file,' rtwCAPI_SetFullPath(*testPointMappingInfo, NULL);\n');
fprintf(file,' rtwCAPI_SetDataAddressMap(*testPointMappingInfo, testPointAddrMap);\n');
fprintf(file,' rtwCAPI_SetChildMMIArray(*testPointMappingInfo, NULL);\n');
fprintf(file,' rtwCAPI_SetChildMMIArrayLen(*testPointMappingInfo, 0);\n');
fprintf(file,'\n');
fprintf(file,' ssSetModelMappingInfoPtr(S, testPointMappingInfo);\n');

fprintf(file,'}\n');

return;

function dump_sig_hier_logging_info(file, tpObj)
fprintf(file,' static const rtwCAPI_SignalHierLoggingInfo rtSigHierLoggingInfo[] = {\n');
for i=1:length(tpObj.heirLoggingInfoTable)
tBus = tpObj.heirLoggingInfoTable{i};

if i == length(tpObj.heirLoggingInfoTable)
fprintf(file,' { "%s", %.17g, %.17g }\n',tBus{1},tBus{2},tBus{3});
else
fprintf(file,' { "%s", %.17g, %.17g },\n',tBus{1},tBus{2},tBus{3});
end

end
fprintf(file,' };\n');

function dump_sig_hier_child_idx(file, tpObj)
fprintf(file,' static const uint_T rtSigHierLoggingChildIdxs[] = {\n');
for i=1:length(tpObj.heirLoggingChildTable)
if i == length(tpObj.heirLoggingChildTable)
fprintf(file,' %.17g\n',tpObj.heirLoggingChildTable(i));
else
fprintf(file,' %.17g,\n',tpObj.heirLoggingChildTable(i));
end

end
fprintf(file,' };\n');

function dump_bus_logging_info(file, tpObj)
dump_rt_context_systems(file);
dump_logging_metainfo(file);
dump_model_mapping_static_info(file, tpObj);

function dump_rt_context_systems(file)
fprintf(file,' static int_T rtContextSystems[3] = { 0, 0, 0 };\n');

function dump_logging_metainfo(file)
fprintf(file,' static rtwCAPI_LoggingMetaInfo loggingMetaInfo[] = {\n');
fprintf(file,' { 0, 0, "", 0 }\n');
fprintf(file,' };\n');

function dump_model_mapping_static_info(file, tpObj)
fprintf(file,' static rtwCAPI_ModelMapLoggingStaticInfo mmiStaticInfoLogging = {\n');
fprintf(file,' 3, rtContextSystems, loggingMetaInfo, 0, NULL, { %.17g, rtSigHierLoggingInfo,\n',length(tpObj.heirLoggingInfoTable));
fprintf(file,' rtSigHierLoggingChildIdxs }, 0, (NULL)\n');
fprintf(file,' };\n');