NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Battery_logs.c
1/************************************************************************************
2* Copyright (C) 2022 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Yugank Goyal *
22* *
23* ----------------------------------------------------------------------------------*/
24#define _BATTERY_MODEL_CODE_
25#include "main.h"
26#include "BatteryModel.h"
27#include "NetSim_utility.h"
28
29static FILE* fpBatterylog = NULL;
30
31UINT DEVICE_ID_LIST[] = { 0 };//When { 0 } log is written for all ZigBee Devices. Specific ZigBee Device ID's can be defined
32//for example {1,2,3} log will be written only for ZigBee Device ID's 1, 2 and 3 as transmitter/receiver
33
34static bool validate_log(NETSIM_ID nDevID)
35{
36 int count = sizeof(DEVICE_ID_LIST) / sizeof(int);
37 if (count == 1 && DEVICE_ID_LIST[0] == 0) return true;
38 else
39 {
40 for (int i = 0; i < count; i++)
41 {
42 if (DEVICE_ID_LIST[i] == DEVICE_CONFIGID(nDevID))
43 return true;
44 }
45 }
46 return false;
47}
48
49void init_battery_log(ptrBATTERY battery)
50{
51 if (get_protocol_log_status("ENERGY_LOG"))
52 {
53 char s[BUFSIZ];
54 sprintf(s, "%s\\%s", pszIOLogPath, "Energy_log.csv");
55 fpBatterylog = fopen(s, "w");
56 if (!fpBatterylog)
57 {
58 fnSystemError("Unable to open %s file", s);
59 perror(s);
60 }
61 else
62 {
63 fprintf(fpBatterylog, "%s,%s,%s,%s,",
64 "Current Time(ms)", "Mode Switch Time(ms)", "Previous Mode", "New Mode");
65
66 fprintf(fpBatterylog, "%s,%s,%s,%s,",
67 "Device Name", "Device Id", "Voltage(V)", "Mode Current(mA)");
68
69 fprintf(fpBatterylog, "%s,%s,",
70 "Initial Energy(mJ)", "Remaining Energy(mJ)");
71
72 for (int i = 1;i < battery->modeCount;i++)
73 {
74 fprintf(fpBatterylog, "%s,Running Total %s,",
75 battery->mode[i].heading, battery->mode[i].heading);
76 }
77
78 fprintf(fpBatterylog, "%s,%s,%s,%s,",
79 "Harvested Energy(mJ)", "Running Total Harvested Energy(mJ)", "Consumed Energy(mJ)", "Running Total Consumed Energy(mJ)");
80
81 fprintf(fpBatterylog, "\n");
82 if (nDbgFlag) fflush(fpBatterylog);
83 }
84 }
85}
86
87void Battery_RadioMeasurements_Log(ptrBATTERY battery, double harvestedEnergy, double ConsumedEnergy, int prev_mode, int curr_mode, double mode_change_time, ptrBATTERYMODE pm)
88{
89 if (!fpBatterylog)
90 init_battery_log(battery);
91 if (fpBatterylog == NULL || (!validate_log(battery->deviceId))) { return; }
92
93 char old_mode[BUFSIZ],new_mode[BUFSIZ];
94 switch (prev_mode)
95 {
96 case 1:sprintf(old_mode, "RX_ON_IDLE");
97 break;
98 case 2:sprintf(old_mode, "RX_ON_BUSY");
99 break;
100 case 3:sprintf(old_mode, "TRX_ON_BUSY");
101 break;
102 case 4:sprintf(old_mode, "SLEEP");
103 break;
104 default:
105 sprintf(old_mode, "RX_OFF");
106 }
107
108 switch (curr_mode)
109 {
110 case 1:sprintf(new_mode, "RX_ON_IDLE");
111 break;
112 case 2:sprintf(new_mode, "RX_ON_BUSY");
113 break;
114 case 3:sprintf(new_mode, "TRX_ON_BUSY");
115 break;
116 case 4:sprintf(new_mode, "SLEEP");
117 break;
118 default:
119 sprintf(new_mode, "RX_OFF");
120 }
121
122 fprintf(fpBatterylog, "%lf,%lf,%s,%s,%s,",
123 ldEventTime / MILLISECOND, mode_change_time / MILLISECOND, old_mode, new_mode, DEVICE_NAME(battery->deviceId));
124
125 fprintf(fpBatterylog, "%d,%lf,%lf,",
126 DEVICE_CONFIGID(battery->deviceId), battery->voltage, pm->current);
127
128 fprintf(fpBatterylog, "%lf,%lf,",
129 battery->initialEnergy,
130 battery->remainingEnergy);
131
132 for (int i = 1;i < battery->modeCount;i++)
133 {
134 if(prev_mode == battery->mode[i].mode)
135 {
136 fprintf(fpBatterylog, "%lf,", ConsumedEnergy);
137 switch (prev_mode)
138 {
139 case 1:fprintf(fpBatterylog, "%lf,", battery->SumIdleEnergy);
140 break;
141 case 2:fprintf(fpBatterylog, "%lf,", battery->SumReceiveEnergy);
142 break;
143 case 3:fprintf(fpBatterylog, "%lf,", battery->SumTransmitEnergy);
144 break;
145 case 4:fprintf(fpBatterylog, "%lf,", battery->SumSleepEnergy);
146 break;
147 default:fprintf(fpBatterylog, "%lf,", 0.0);
148 break;
149 }
150 }
151 else {
152 fprintf(fpBatterylog, "%lf,", 0.0);
153 switch (battery->mode[i].mode)
154 {
155 case 1:fprintf(fpBatterylog, "%lf,", battery->SumIdleEnergy);
156 break;
157 case 2:fprintf(fpBatterylog, "%lf,", battery->SumReceiveEnergy);
158 break;
159 case 3:fprintf(fpBatterylog, "%lf,", battery->SumTransmitEnergy);
160 break;
161 case 4:fprintf(fpBatterylog, "%lf,", battery->SumSleepEnergy);
162 break;
163 default:fprintf(fpBatterylog, "%lf,", 0.0);
164 break;
165 }
166 }
167 }
168
169 fprintf(fpBatterylog, "%lf,%lf,%lf,%lf,",
170 harvestedEnergy, battery->SumHarvestedEnergy,
171 ConsumedEnergy, battery->SumConsumedEnergy);
172
173 fprintf(fpBatterylog, "\n");
174 if (nDbgFlag) fflush(fpBatterylog);
175}
176
177void close_battery_log()
178{
179 if (fpBatterylog)
180 fclose(fpBatterylog);
181}
182
183bool get_protocol_log_status(char* logname)
184{
185 FILE* fp;
186 char str[BUFSIZ];
187 sprintf(str, "%s/%s", pszIOPath, "ProtocolLogsConfig.txt");
188 fp = fopen(str, "r");
189 if (!fp)
190 return false;
191
192 char data[BUFSIZ];
193 while (fgets(data, BUFSIZ, fp))
194 {
195 lskip(data);
196 sprintf(str, "%s=true", logname);
197 if (!_strnicmp(data, str, strlen(str)))
198 {
199 fclose(fp);
200 return true;
201 }
202 }
203 fclose(fp);
204
205 return false;
206}