24#define _BATTERY_MODEL_CODE_
26#include "BatteryModel.h"
27#include "NetSim_utility.h"
29static FILE* fpBatterylog = NULL;
31UINT DEVICE_ID_LIST[] = { 0 };
34static bool validate_log(NETSIM_ID nDevID)
36 int count =
sizeof(DEVICE_ID_LIST) /
sizeof(
int);
37 if (count == 1 && DEVICE_ID_LIST[0] == 0)
return true;
40 for (
int i = 0; i < count; i++)
42 if (DEVICE_ID_LIST[i] == DEVICE_CONFIGID(nDevID))
49void init_battery_log(ptrBATTERY battery)
51 if (get_protocol_log_status(
"ENERGY_LOG"))
54 sprintf(s,
"%s\\%s", pszIOLogPath,
"Energy_log.csv");
55 fpBatterylog = fopen(s,
"w");
58 fnSystemError(
"Unable to open %s file", s);
63 fprintf(fpBatterylog,
"%s,%s,%s,%s,",
64 "Current Time(ms)",
"Mode Switch Time(ms)",
"Previous Mode",
"New Mode");
66 fprintf(fpBatterylog,
"%s,%s,%s,%s,",
67 "Device Name",
"Device Id",
"Voltage(V)",
"Mode Current(mA)");
69 fprintf(fpBatterylog,
"%s,%s,",
70 "Initial Energy(mJ)",
"Remaining Energy(mJ)");
72 for (
int i = 1;i < battery->modeCount;i++)
74 fprintf(fpBatterylog,
"%s,Running Total %s,",
75 battery->mode[i].heading, battery->mode[i].heading);
78 fprintf(fpBatterylog,
"%s,%s,%s,%s,",
79 "Harvested Energy(mJ)",
"Running Total Harvested Energy(mJ)",
"Consumed Energy(mJ)",
"Running Total Consumed Energy(mJ)");
81 fprintf(fpBatterylog,
"\n");
82 if (nDbgFlag) fflush(fpBatterylog);
87void Battery_RadioMeasurements_Log(ptrBATTERY battery,
double harvestedEnergy,
double ConsumedEnergy,
int prev_mode,
int curr_mode,
double mode_change_time, ptrBATTERYMODE pm)
90 init_battery_log(battery);
91 if (fpBatterylog == NULL || (!validate_log(battery->deviceId))) {
return; }
93 char old_mode[BUFSIZ],new_mode[BUFSIZ];
96 case 1:sprintf(old_mode,
"RX_ON_IDLE");
98 case 2:sprintf(old_mode,
"RX_ON_BUSY");
100 case 3:sprintf(old_mode,
"TRX_ON_BUSY");
102 case 4:sprintf(old_mode,
"SLEEP");
105 sprintf(old_mode,
"RX_OFF");
110 case 1:sprintf(new_mode,
"RX_ON_IDLE");
112 case 2:sprintf(new_mode,
"RX_ON_BUSY");
114 case 3:sprintf(new_mode,
"TRX_ON_BUSY");
116 case 4:sprintf(new_mode,
"SLEEP");
119 sprintf(new_mode,
"RX_OFF");
122 fprintf(fpBatterylog,
"%lf,%lf,%s,%s,%s,",
123 ldEventTime / MILLISECOND, mode_change_time / MILLISECOND, old_mode, new_mode, DEVICE_NAME(battery->deviceId));
125 fprintf(fpBatterylog,
"%d,%lf,%lf,",
126 DEVICE_CONFIGID(battery->deviceId), battery->voltage, pm->current);
128 fprintf(fpBatterylog,
"%lf,%lf,",
129 battery->initialEnergy,
130 battery->remainingEnergy);
132 for (
int i = 1;i < battery->modeCount;i++)
134 if(prev_mode == battery->mode[i].mode)
136 fprintf(fpBatterylog,
"%lf,", ConsumedEnergy);
139 case 1:fprintf(fpBatterylog,
"%lf,", battery->SumIdleEnergy);
141 case 2:fprintf(fpBatterylog,
"%lf,", battery->SumReceiveEnergy);
143 case 3:fprintf(fpBatterylog,
"%lf,", battery->SumTransmitEnergy);
145 case 4:fprintf(fpBatterylog,
"%lf,", battery->SumSleepEnergy);
147 default:fprintf(fpBatterylog,
"%lf,", 0.0);
152 fprintf(fpBatterylog,
"%lf,", 0.0);
153 switch (battery->mode[i].mode)
155 case 1:fprintf(fpBatterylog,
"%lf,", battery->SumIdleEnergy);
157 case 2:fprintf(fpBatterylog,
"%lf,", battery->SumReceiveEnergy);
159 case 3:fprintf(fpBatterylog,
"%lf,", battery->SumTransmitEnergy);
161 case 4:fprintf(fpBatterylog,
"%lf,", battery->SumSleepEnergy);
163 default:fprintf(fpBatterylog,
"%lf,", 0.0);
169 fprintf(fpBatterylog,
"%lf,%lf,%lf,%lf,",
170 harvestedEnergy, battery->SumHarvestedEnergy,
171 ConsumedEnergy, battery->SumConsumedEnergy);
173 fprintf(fpBatterylog,
"\n");
174 if (nDbgFlag) fflush(fpBatterylog);
177void close_battery_log()
180 fclose(fpBatterylog);
183bool get_protocol_log_status(
char* logname)
187 sprintf(str,
"%s/%s", pszIOPath,
"ProtocolLogsConfig.txt");
188 fp = fopen(str,
"r");
193 while (fgets(data, BUFSIZ, fp))
196 sprintf(str,
"%s=true", logname);
197 if (!_strnicmp(data, str, strlen(str)))