Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SoftEeprom.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. */
  19. #include <stdlib.h>
  20. #include <iostream>
  21. #include <fstream>
  22. #include <sys/stat.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include "log.h"
  26. #include "SoftEeprom.h"
  27. SoftEeprom::SoftEeprom() : _length(0), _fileName(NULL), _values(NULL)
  28. {
  29. }
  30. SoftEeprom::SoftEeprom(const SoftEeprom& other)
  31. {
  32. _fileName = strdup(other._fileName);
  33. _length = other._length;
  34. _values = new uint8_t[_length];
  35. for (size_t i = 0; i < _length; ++i) {
  36. _values[i] = other._values[i];
  37. }
  38. }
  39. SoftEeprom::~SoftEeprom()
  40. {
  41. destroy();
  42. }
  43. int SoftEeprom::init(const char *fileName, size_t length)
  44. {
  45. struct stat fileInfo;
  46. destroy();
  47. _fileName = strdup(fileName);
  48. if (_fileName == NULL) {
  49. logError("Error: %s\n", strerror(errno));
  50. return -1;
  51. }
  52. _length = length;
  53. _values = new uint8_t[_length];
  54. if (stat(_fileName, &fileInfo) != 0) {
  55. //File does not exist. Create it.
  56. logInfo("EEPROM file %s does not exist, creating new file.\n", _fileName);
  57. std::ofstream myFile(_fileName, std::ios::out | std::ios::binary);
  58. if (!myFile) {
  59. logError("Unable to create config file %s.\n", _fileName);
  60. return -1;
  61. }
  62. // Fill the eeprom with 1s
  63. for (size_t i = 0; i < _length; ++i) {
  64. _values[i] = 0xFF;
  65. }
  66. myFile.write((const char*)_values, _length);
  67. myFile.close();
  68. } else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) {
  69. logError("EEPROM file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n",
  70. _fileName, _length);
  71. destroy();
  72. return -1;
  73. } else {
  74. //Read config into local memory.
  75. std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
  76. if (!myFile) {
  77. logError("Unable to open EEPROM file %s for reading.\n", _fileName);
  78. return -1;
  79. }
  80. myFile.read((char*)_values, _length);
  81. myFile.close();
  82. }
  83. return 0;
  84. }
  85. void SoftEeprom::destroy()
  86. {
  87. if (_values) {
  88. delete[] _values;
  89. }
  90. if (_fileName) {
  91. free(_fileName);
  92. }
  93. _length = 0;
  94. }
  95. void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
  96. {
  97. unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
  98. if (!length) {
  99. logError("EEPROM being read without being initialized!\n");
  100. return;
  101. }
  102. if (offs + length <= _length) {
  103. memcpy(buf, _values+offs, length);
  104. }
  105. }
  106. void SoftEeprom::writeBlock(void* buf, void* addr, size_t length)
  107. {
  108. unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
  109. if (!length) {
  110. logError("EEPROM being written without being initialized!\n");
  111. return;
  112. }
  113. if (offs + length <= _length) {
  114. if (memcmp(_values+offs, buf, length) == 0) {
  115. return;
  116. }
  117. memcpy(_values+offs, buf, length);
  118. std::ofstream myFile(_fileName, std::ios::out | std::ios::in | std::ios::binary);
  119. if (!myFile) {
  120. logError("Unable to write config to file %s.\n", _fileName);
  121. return;
  122. }
  123. myFile.seekp(offs);
  124. myFile.write((const char*)buf, length);
  125. myFile.close();
  126. }
  127. }
  128. uint8_t SoftEeprom::readByte(int addr)
  129. {
  130. uint8_t value = 0xFF;
  131. readBlock(&value, reinterpret_cast<void*>(addr), 1);
  132. return value;
  133. }
  134. void SoftEeprom::writeByte(int addr, uint8_t value)
  135. {
  136. uint8_t curr = readByte(addr);
  137. if (curr != value) {
  138. writeBlock(&value, reinterpret_cast<void*>(addr), 1);
  139. }
  140. }
  141. SoftEeprom& SoftEeprom::operator=(const SoftEeprom& other)
  142. {
  143. if (this != &other) {
  144. delete[] _values;
  145. free(_fileName);
  146. _fileName = strdup(other._fileName);
  147. _length = other._length;
  148. _values = new uint8_t[_length];
  149. for (size_t i = 0; i < _length; ++i) {
  150. _values[i] = other._values[i];
  151. }
  152. }
  153. return *this;
  154. }