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.

MyHwLinuxGeneric.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "MyHwLinuxGeneric.h"
  20. #include <errno.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <syscall.h>
  25. #include <unistd.h>
  26. #include "SoftEeprom.h"
  27. #include "log.h"
  28. #include "config.h"
  29. static SoftEeprom eeprom;
  30. static FILE *randomFp = NULL;
  31. bool hwInit(void)
  32. {
  33. MY_SERIALDEVICE.begin(MY_BAUD_RATE);
  34. #ifdef MY_GATEWAY_SERIAL
  35. #ifdef MY_LINUX_SERIAL_GROUPNAME
  36. if (!MY_SERIALDEVICE.setGroupPerm(MY_LINUX_SERIAL_GROUPNAME)) {
  37. logError("Unable to change permission for serial port device.\n");
  38. exit(1);
  39. }
  40. #endif
  41. #endif
  42. if (eeprom.init(conf.eeprom_file, conf.eeprom_size) != 0) {
  43. exit(1);
  44. }
  45. return true;
  46. }
  47. void hwReadConfigBlock(void *buf, void *addr, size_t length)
  48. {
  49. eeprom.readBlock(buf, addr, length);
  50. }
  51. void hwWriteConfigBlock(void *buf, void *addr, size_t length)
  52. {
  53. eeprom.writeBlock(buf, addr, length);
  54. }
  55. uint8_t hwReadConfig(const int addr)
  56. {
  57. return eeprom.readByte(addr);
  58. }
  59. void hwWriteConfig(const int addr, uint8_t value)
  60. {
  61. eeprom.writeByte(addr, value);
  62. }
  63. void hwRandomNumberInit(void)
  64. {
  65. uint32_t seed=0;
  66. if (randomFp != NULL) {
  67. fclose(randomFp);
  68. }
  69. if (!(randomFp = fopen("/dev/urandom", "r"))) {
  70. logError("Cannot open '/dev/urandom'.\n");
  71. exit(2);
  72. }
  73. while (hwGetentropy(&seed, sizeof(seed)) != sizeof(seed));
  74. randomSeed(seed);
  75. }
  76. ssize_t hwGetentropy(void *__buffer, size_t __length)
  77. {
  78. return(fread(__buffer, 1, __length, randomFp));
  79. }
  80. uint32_t hwMillis(void)
  81. {
  82. return millis();
  83. }
  84. bool hwUniqueID(unique_id_t *uniqueID)
  85. {
  86. // not implemented yet
  87. (void)uniqueID;
  88. return false;
  89. }
  90. // Not supported!
  91. int8_t hwSleep(uint32_t ms)
  92. {
  93. (void)ms;
  94. return MY_SLEEP_NOT_POSSIBLE;
  95. }
  96. // Not supported!
  97. int8_t hwSleep(uint8_t interrupt, uint8_t mode, uint32_t ms)
  98. {
  99. (void)interrupt;
  100. (void)mode;
  101. (void)ms;
  102. return MY_SLEEP_NOT_POSSIBLE;
  103. }
  104. // Not supported!
  105. int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2,
  106. uint32_t ms)
  107. {
  108. (void)interrupt1;
  109. (void)mode1;
  110. (void)interrupt2;
  111. (void)mode2;
  112. (void)ms;
  113. return MY_SLEEP_NOT_POSSIBLE;
  114. }
  115. uint16_t hwCPUVoltage(void)
  116. {
  117. // TODO: Not supported!
  118. return FUNCTION_NOT_SUPPORTED;
  119. }
  120. uint16_t hwCPUFrequency(void)
  121. {
  122. // TODO: Not supported!
  123. return FUNCTION_NOT_SUPPORTED;
  124. }
  125. int8_t hwCPUTemperature(void)
  126. {
  127. return -127; // not implemented yet
  128. }
  129. uint16_t hwFreeMem(void)
  130. {
  131. // TODO: Not supported!
  132. return FUNCTION_NOT_SUPPORTED;
  133. }
  134. void hwDigitalWrite(uint8_t pin, uint8_t value)
  135. {
  136. digitalWrite(pin, value);
  137. }
  138. int hwDigitalRead(uint8_t pin)
  139. {
  140. return digitalRead(pin);
  141. }
  142. void hwPinMode(uint8_t pin, uint8_t mode)
  143. {
  144. pinMode(pin, mode);
  145. }
  146. void hwDebugPrint(const char *fmt, ...)
  147. {
  148. #ifndef MY_DISABLED_SERIAL
  149. #ifdef MY_DEBUGDEVICE
  150. char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
  151. #ifdef MY_GATEWAY_SERIAL
  152. // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
  153. snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%" PRIu8 ";0;%" PRIu8 ";%" PRIu32 " "),
  154. C_INTERNAL, I_LOG_MESSAGE, hwMillis());
  155. MY_DEBUGDEVICE.print(fmtBuffer);
  156. #else
  157. // prepend timestamp
  158. MY_DEBUGDEVICE.print(hwMillis());
  159. MY_DEBUGDEVICE.print(" ");
  160. #endif
  161. va_list args;
  162. va_start (args, fmt );
  163. vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
  164. #ifdef MY_GATEWAY_SERIAL
  165. // Truncate message if this is gateway node
  166. fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
  167. fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
  168. #endif
  169. va_end (args);
  170. MY_DEBUGDEVICE.print(fmtBuffer);
  171. MY_DEBUGDEVICE.flush();
  172. #else
  173. va_list args;
  174. va_start(args, fmt);
  175. vlogDebug(fmt, args);
  176. va_end(args);
  177. #endif
  178. #else
  179. (void)fmt;
  180. #endif
  181. }