Andrew Boyson / motorhome

Dependencies:   net lpc1768 crypto clock web fram log

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers values.c Source File

values.c

00001 #include <stdint.h>
00002 #include <string.h>
00003 #include <stdbool.h>
00004 
00005 #include "tftp.h"
00006 #include "dnslabel.h"
00007 #include "clk.h"
00008 #include "clktime.h"
00009 #include "mstimer.h"
00010 #include "clktm.h"
00011 #include "comms.h"
00012 #include "log.h"
00013 #include "net.h"
00014 #include "settings.h"
00015 
00016 bool ValuesTrace = false;
00017 
00018 static char    serverName[DNS_MAX_LABEL_LENGTH+1];
00019 static char    fileName  [DNS_MAX_LABEL_LENGTH+1];
00020 static int32_t writeSize;
00021 static int32_t readInterval;
00022 static int64_t startTime;
00023 static int32_t count;
00024 
00025 struct record_t
00026 {
00027     uint32_t countedCapacity;
00028     int32_t  currentMa;
00029     int16_t  temperatureTenths;
00030     int16_t  voltageMv;
00031 };
00032 
00033 char*   ValuesGetServerName   (              ) { return         serverName;                    }
00034 char*   ValuesGetFileName     (              ) { return         fileName;                      }
00035 int     ValuesGetWriteSize    (              ) { return (int)   writeSize;                     }
00036 int     ValuesGetReadInterval (              ) { return (int)   readInterval;                  }
00037 void    ValuesGetStartTm      (struct tm* ptm) { ClkTimeToTmUtc(startTime, ptm);               }
00038 int64_t ValuesGetStartTime    (              ) { return startTime >> CLK_TIME_ONE_SECOND_SHIFT;}
00039 int     ValuesGetCount        (              ) { return (int)   count;                         }
00040 
00041 void ValuesSetServerName    (char*   value) { DnsLabelCopy(serverName, value); SetValuesServerName  ( value); }
00042 void ValuesSetFileName      (char*   value) { DnsLabelCopy(  fileName, value); SetValuesFileName    ( value); }
00043 void ValuesSetWriteSize     (int     value)
00044 {
00045     int maxCount = GetValuesMaxCount(sizeof(struct record_t));
00046     if (value > maxCount) value = maxCount;
00047     writeSize = value;
00048     SetValuesWriteSize(&value);
00049 }
00050 void ValuesSetReadInterval  (int     value) { readInterval  =          value ; SetValuesReadInterval(&value); }
00051 static void setStartTime    (int64_t value) { startTime     =          value ; SetValuesStartTime   (&value); }
00052 static void setCount        (int     value) { count         =          value ; SetValuesCount       (&value); }
00053 
00054 static int readValuesFromFram()
00055 {
00056     int address;
00057     int32_t def4;
00058     GetValuesServerName  (   serverName);
00059     GetValuesFileName    (     fileName);
00060     GetValuesWriteSize   (   &writeSize);
00061     GetValuesReadInterval(&readInterval);
00062     GetValuesStartTime   (   &startTime);
00063     GetValuesCount       (       &count);
00064     return 0;
00065 }
00066 
00067 
00068 static uint32_t readStartMs;
00069 
00070 static int writeIndex;
00071 
00072 static int nextByteOfWriteStream()
00073 {
00074     int byteAfterData = count * sizeof(struct record_t);
00075     if (writeIndex >= byteAfterData || writeIndex >= GetValuesMaxIndex())
00076     {
00077         setCount(0);
00078         return -1;
00079     }
00080     char c;
00081     GetValuesData(writeIndex, &c);
00082     writeIndex++;
00083     return c;
00084 }
00085 
00086 static void readValues()
00087 {    
00088     struct record_t record;
00089     record.countedCapacity   = BatteryGetCountedCapacity  ();
00090     record.currentMa         = BatteryGetCurrentMa        ();
00091     record.temperatureTenths = BatteryGetTemperatureTenths();
00092     record.voltageMv         = BatteryGetVoltageMv        ();
00093     
00094     if (count <= 0)
00095     {
00096         count = 0;
00097         setStartTime(ClkNowTai());
00098     }
00099     
00100     SetValuesData(sizeof(struct record_t), count, &record);
00101     setCount(count + 1);
00102 }
00103 
00104 static void writeValues()
00105 {
00106     if (!serverName[0] ) return; //Do nothing if have no server name
00107     if (  !fileName[0] ) return; //Do nothing if have no file name
00108     if (TftpWriteStatus) return; //Do nothing if the TFTP client is busy
00109 
00110     strcpy(TftpServerName, serverName);
00111     struct tm tm;
00112     ClkTimeToTmUtc(startTime, &tm);
00113     int len = strftime(TftpFileName, DNS_MAX_LABEL_LENGTH+1, fileName, &tm);
00114     if (len == 0)
00115     {
00116         LogTimeF("Values - cannot make filename from template '%s'\r\n", count, fileName);
00117         return;
00118     }
00119 
00120     if (ValuesTrace)
00121     {
00122         if (NetTraceNewLine) Log("\r\n");
00123         LogTimeF("Values - requesting backup of %d values to %s\r\n", count, TftpFileName);
00124     }
00125 
00126     writeIndex = 0;
00127     TftpWriteStatus = TFTP_WRITE_STATUS_REQUEST; //This is reset by TFTP when finished
00128 }
00129 
00130 void ValuesMain()
00131 {
00132     if (!readInterval) readStartMs = MsTimerCount;
00133     if (writeSize && count < writeSize && readInterval)
00134     {
00135         if (MsTimerRelative(readStartMs, readInterval * 1000))
00136         {
00137             readValues(); //Only read values if they are going to be backed up
00138             readStartMs = MsTimerCount;
00139         }
00140     }
00141     else
00142     {
00143         readStartMs = MsTimerCount;
00144     }
00145     if (writeSize && (count >= writeSize || count >= GetValuesMaxCount(sizeof(struct record_t)))) writeValues(); //Backup the values once the backup size is reached
00146 }
00147 
00148 int ValuesInit()
00149 {
00150     if (readValuesFromFram()) return -1;
00151     readStartMs = MsTimerCount;
00152     TftpGetNextByteFunction = nextByteOfWriteStream;
00153     if (count > 0) writeValues();     //Backup the values if there are any
00154     return 0;
00155 }