Andrew Boyson / motorhome

Dependencies:   net lpc1768 crypto clock web fram log

Committer:
andrewboyson
Date:
Thu Jan 14 16:05:55 2021 +0000
Revision:
0:b843d647695c
Child:
4:f38b5bb8adac
Creation (mostly a copy of heating at the moment)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:b843d647695c 1 #include <stdint.h>
andrewboyson 0:b843d647695c 2 #include <string.h>
andrewboyson 0:b843d647695c 3 #include <stdbool.h>
andrewboyson 0:b843d647695c 4
andrewboyson 0:b843d647695c 5 #include "tftp.h"
andrewboyson 0:b843d647695c 6 #include "dnslabel.h"
andrewboyson 0:b843d647695c 7 #include "fram.h"
andrewboyson 0:b843d647695c 8 #include "clk.h"
andrewboyson 0:b843d647695c 9 #include "clktime.h"
andrewboyson 0:b843d647695c 10 #include "mstimer.h"
andrewboyson 0:b843d647695c 11 #include "clktm.h"
andrewboyson 0:b843d647695c 12 #include "ds18b20.h"
andrewboyson 0:b843d647695c 13 #include "radiator.h"
andrewboyson 0:b843d647695c 14 #include "boiler.h"
andrewboyson 0:b843d647695c 15 #include "hot-water.h"
andrewboyson 0:b843d647695c 16 #include "log.h"
andrewboyson 0:b843d647695c 17 #include "net.h"
andrewboyson 0:b843d647695c 18
andrewboyson 0:b843d647695c 19 bool ValuesTrace = false;
andrewboyson 0:b843d647695c 20
andrewboyson 0:b843d647695c 21 static char serverName[DNS_MAX_LABEL_LENGTH+1]; static int iServerName;
andrewboyson 0:b843d647695c 22 static char fileName[DNS_MAX_LABEL_LENGTH+1]; static int iFileName;
andrewboyson 0:b843d647695c 23 static int32_t writeSize; static int iWriteSize;
andrewboyson 0:b843d647695c 24 static int32_t readInterval; static int iReadInterval;
andrewboyson 0:b843d647695c 25 static int64_t startTime; static int iStartTime;
andrewboyson 0:b843d647695c 26 static int32_t count; static int iCount;
andrewboyson 0:b843d647695c 27 static int iData;
andrewboyson 0:b843d647695c 28
andrewboyson 0:b843d647695c 29 char* ValuesGetServerName ( ) { return serverName; }
andrewboyson 0:b843d647695c 30 char* ValuesGetFileName ( ) { return fileName; }
andrewboyson 0:b843d647695c 31 int ValuesGetWriteSize ( ) { return (int) writeSize; }
andrewboyson 0:b843d647695c 32 int ValuesGetReadInterval ( ) { return (int) readInterval; }
andrewboyson 0:b843d647695c 33 void ValuesGetStartTm (struct tm* ptm) { ClkTimeToTmUtc(startTime, ptm); }
andrewboyson 0:b843d647695c 34 int64_t ValuesGetStartTime ( ) { return startTime >> CLK_TIME_ONE_SECOND_SHIFT;}
andrewboyson 0:b843d647695c 35 int ValuesGetCount ( ) { return (int) count; }
andrewboyson 0:b843d647695c 36
andrewboyson 0:b843d647695c 37 void ValuesSetServerName (char* value) { DnsLabelCopy(serverName, value); FramWrite(iServerName , DNS_MAX_LABEL_LENGTH, serverName ); }
andrewboyson 0:b843d647695c 38 void ValuesSetFileName (char* value) { DnsLabelCopy( fileName, value); FramWrite(iFileName , DNS_MAX_LABEL_LENGTH, fileName ); }
andrewboyson 0:b843d647695c 39 void ValuesSetWriteSize (int value) { writeSize = value ; FramWrite(iWriteSize , 4, &writeSize ); }
andrewboyson 0:b843d647695c 40 void ValuesSetReadInterval (int value) { readInterval = value ; FramWrite(iReadInterval, 4, &readInterval ); }
andrewboyson 0:b843d647695c 41 static void setStartTime (int64_t value) { startTime = value ; FramWrite(iStartTime , 8, &startTime ); }
andrewboyson 0:b843d647695c 42 static void setCount (int value) { count = value ; FramWrite(iCount , 4, &count ); }
andrewboyson 0:b843d647695c 43
andrewboyson 0:b843d647695c 44 static int readValuesFromFram()
andrewboyson 0:b843d647695c 45 {
andrewboyson 0:b843d647695c 46 int address;
andrewboyson 0:b843d647695c 47 int32_t def4;
andrewboyson 0:b843d647695c 48
andrewboyson 0:b843d647695c 49 address = FramLoad( DNS_MAX_LABEL_LENGTH+1, serverName , NULL ); if (address < 0) return -1; iServerName = address;
andrewboyson 0:b843d647695c 50 address = FramLoad( DNS_MAX_LABEL_LENGTH+1, fileName , NULL ); if (address < 0) return -1; iFileName = address;
andrewboyson 0:b843d647695c 51 def4 = 100; address = FramLoad( 4, &writeSize , &def4); if (address < 0) return -1; iWriteSize = address;
andrewboyson 0:b843d647695c 52 def4 = 15; address = FramLoad( 4, &readInterval, &def4); if (address < 0) return -1; iReadInterval = address;
andrewboyson 0:b843d647695c 53 address = FramLoad( 8, &startTime , NULL ); if (address < 0) return -1; iStartTime = address;
andrewboyson 0:b843d647695c 54 address = FramLoad( 4, &count , NULL ); if (address < 0) return -1; iCount = address;
andrewboyson 0:b843d647695c 55 address = FramAllocate( 1 ); if (address < 0) return -1; iData = address;
andrewboyson 0:b843d647695c 56 return 0;
andrewboyson 0:b843d647695c 57 }
andrewboyson 0:b843d647695c 58
andrewboyson 0:b843d647695c 59
andrewboyson 0:b843d647695c 60 static uint32_t readStartMs;
andrewboyson 0:b843d647695c 61
andrewboyson 0:b843d647695c 62 static int writeIndex;
andrewboyson 0:b843d647695c 63
andrewboyson 0:b843d647695c 64 static int nextByteOfWriteStream()
andrewboyson 0:b843d647695c 65 {
andrewboyson 0:b843d647695c 66 int byteAfterData = count * 8;
andrewboyson 0:b843d647695c 67 if (writeIndex >= byteAfterData || writeIndex + iData >= FRAM_SIZE)
andrewboyson 0:b843d647695c 68 {
andrewboyson 0:b843d647695c 69 setCount(0);
andrewboyson 0:b843d647695c 70 return -1;
andrewboyson 0:b843d647695c 71 }
andrewboyson 0:b843d647695c 72 char c;
andrewboyson 0:b843d647695c 73 FramRead(writeIndex + iData, 1, &c);
andrewboyson 0:b843d647695c 74 writeIndex++;
andrewboyson 0:b843d647695c 75 return c;
andrewboyson 0:b843d647695c 76 }
andrewboyson 0:b843d647695c 77
andrewboyson 0:b843d647695c 78 static void readValues()
andrewboyson 0:b843d647695c 79 {
andrewboyson 0:b843d647695c 80 uint64_t record = 0;
andrewboyson 0:b843d647695c 81 uint16_t value;
andrewboyson 0:b843d647695c 82 value = RadiatorGetHallDS18B20Value();
andrewboyson 0:b843d647695c 83 value &= 0x0FFF;
andrewboyson 0:b843d647695c 84 record |= value; //0000 0000 0000 0AAA
andrewboyson 0:b843d647695c 85 record <<= 12; //0000 0000 00AA A000
andrewboyson 0:b843d647695c 86 value = BoilerGetTankDS18B20Value();
andrewboyson 0:b843d647695c 87 value &= 0x0FFF;
andrewboyson 0:b843d647695c 88 record |= value; //0000 0000 00AA ABBB
andrewboyson 0:b843d647695c 89 record <<= 12; //0000 000A AABB B000
andrewboyson 0:b843d647695c 90 value = BoilerGetOutputDS18B20Value();
andrewboyson 0:b843d647695c 91 value &= 0x0FFF;
andrewboyson 0:b843d647695c 92 record |= value; //0000 000A AABB BCCC
andrewboyson 0:b843d647695c 93 record <<= 12; //0000 AAAB BBCC C000
andrewboyson 0:b843d647695c 94 value = BoilerGetReturnDS18B20Value();
andrewboyson 0:b843d647695c 95 value &= 0x0FFF;
andrewboyson 0:b843d647695c 96 record |= value; //0000 AAAB BBCC CDDD
andrewboyson 0:b843d647695c 97 record <<= 12; //0AAA BBBC CCDD D000
andrewboyson 0:b843d647695c 98 value = HotWaterGetDS18B20Value();
andrewboyson 0:b843d647695c 99 value &= 0x0FFF;
andrewboyson 0:b843d647695c 100 record |= value; //0AAA BBBC CCDD DEEE
andrewboyson 0:b843d647695c 101 record <<= 4; //AAAB BBCC CDDD EEE0
andrewboyson 0:b843d647695c 102
andrewboyson 0:b843d647695c 103 record |= RadiatorPump << 0;
andrewboyson 0:b843d647695c 104 record |= BoilerCall << 1;
andrewboyson 0:b843d647695c 105 record |= BoilerPump << 2; //AAAB BBCC CDDD EEEF
andrewboyson 0:b843d647695c 106
andrewboyson 0:b843d647695c 107 if (count == 0) setStartTime(ClkNowTai());
andrewboyson 0:b843d647695c 108
andrewboyson 0:b843d647695c 109 FramWrite(iData + 8 * count, 8, &record);
andrewboyson 0:b843d647695c 110 setCount(count + 1);
andrewboyson 0:b843d647695c 111 }
andrewboyson 0:b843d647695c 112
andrewboyson 0:b843d647695c 113 static void writeValues()
andrewboyson 0:b843d647695c 114 {
andrewboyson 0:b843d647695c 115 if (!serverName[0] ) return; //Do nothing if have no server name
andrewboyson 0:b843d647695c 116 if ( !fileName[0] ) return; //Do nothing if have no file name
andrewboyson 0:b843d647695c 117 if (TftpWriteStatus) return; //Do nothing if the TFTP client is busy
andrewboyson 0:b843d647695c 118
andrewboyson 0:b843d647695c 119 strcpy(TftpServerName, serverName);
andrewboyson 0:b843d647695c 120 struct tm tm;
andrewboyson 0:b843d647695c 121 ClkTimeToTmUtc(startTime, &tm);
andrewboyson 0:b843d647695c 122 int len = strftime(TftpFileName, DNS_MAX_LABEL_LENGTH+1, fileName, &tm);
andrewboyson 0:b843d647695c 123 if (len == 0)
andrewboyson 0:b843d647695c 124 {
andrewboyson 0:b843d647695c 125 LogTimeF("Values - cannot make filename from template '%s'\r\n", count, fileName);
andrewboyson 0:b843d647695c 126 return;
andrewboyson 0:b843d647695c 127 }
andrewboyson 0:b843d647695c 128
andrewboyson 0:b843d647695c 129 if (ValuesTrace)
andrewboyson 0:b843d647695c 130 {
andrewboyson 0:b843d647695c 131 if (NetTraceNewLine) Log("\r\n");
andrewboyson 0:b843d647695c 132 LogTimeF("Values - requesting backup of %d values to %s\r\n", count, TftpFileName);
andrewboyson 0:b843d647695c 133 }
andrewboyson 0:b843d647695c 134
andrewboyson 0:b843d647695c 135 writeIndex = 0;
andrewboyson 0:b843d647695c 136 TftpWriteStatus = TFTP_WRITE_STATUS_REQUEST; //This is reset by TFTP when finished
andrewboyson 0:b843d647695c 137 }
andrewboyson 0:b843d647695c 138
andrewboyson 0:b843d647695c 139 void ValuesMain()
andrewboyson 0:b843d647695c 140 {
andrewboyson 0:b843d647695c 141 if (!readInterval) readStartMs = MsTimerCount;
andrewboyson 0:b843d647695c 142 if (writeSize && count < writeSize && readInterval)
andrewboyson 0:b843d647695c 143 {
andrewboyson 0:b843d647695c 144 if (MsTimerRelative(readStartMs, readInterval * 1000))
andrewboyson 0:b843d647695c 145 {
andrewboyson 0:b843d647695c 146 readValues(); //Only read values if they are going to be backed up
andrewboyson 0:b843d647695c 147 readStartMs = MsTimerCount;
andrewboyson 0:b843d647695c 148 }
andrewboyson 0:b843d647695c 149 }
andrewboyson 0:b843d647695c 150 else
andrewboyson 0:b843d647695c 151 {
andrewboyson 0:b843d647695c 152 readStartMs = MsTimerCount;
andrewboyson 0:b843d647695c 153 }
andrewboyson 0:b843d647695c 154 if (writeSize && count >= writeSize) writeValues(); //Backup the values once the backup size is reached
andrewboyson 0:b843d647695c 155 }
andrewboyson 0:b843d647695c 156
andrewboyson 0:b843d647695c 157 int ValuesInit()
andrewboyson 0:b843d647695c 158 {
andrewboyson 0:b843d647695c 159 if (readValuesFromFram()) return -1;
andrewboyson 0:b843d647695c 160 readStartMs = MsTimerCount;
andrewboyson 0:b843d647695c 161 TftpGetNextByteFunction = nextByteOfWriteStream;
andrewboyson 0:b843d647695c 162 if (count > 0) writeValues(); //Backup the values if there are any
andrewboyson 0:b843d647695c 163 return 0;
andrewboyson 0:b843d647695c 164 }