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.

2023 lines
49 KiB

​
Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234): deviceETH2UART - In TCP mode, link health is the TCP connection itself (socket, I/O thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on mobile networks often don't answer ping, and the working connection was closed every second ("No Ping to device!"). - Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an echo reply from the gateway with our id and sequence counts. A signal (EINTR) no longer ends the wait. The old single recvfrom() with SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART. - TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in select() continues waiting. - create_SOL() closes the previous socket and I/O thread first. It overwrote the socket (the connection leaked) and init() failed while the old thread ran, so each reconnect left an open TCP connection at the gateway and the node stayed invalid although "Connected". If init() fails, the new socket is closed. - create()/validate()/reconnect() are serialised: they run on the node factory thread and on the hardware manager thread. - Registered with the hardware manager once. Re-creation from the hardware manager locked its non-recursive list mutex again (deadlock). - Reconnect back-off 2 s -> 30 s. - recv() == 0 on TCP means the gateway closed the connection (a stale errno was checked before). - Destructor resigns from the hardware manager before closing. deviceNodeFactory - deleteNode() deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. - Nodes are deleted after the validation loop (erasing inside it skipped the next node). deviceNode::validate - Compares the configuration only. "online" changes whenever another object of the same node id is created or deleted. hardwareManager::assignResource - Ignores a resource that is already assigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
8 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234): deviceETH2UART - In TCP mode, link health is the TCP connection itself (socket, I/O thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on mobile networks often don't answer ping, and the working connection was closed every second ("No Ping to device!"). - Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an echo reply from the gateway with our id and sequence counts. A signal (EINTR) no longer ends the wait. The old single recvfrom() with SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART. - TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in select() continues waiting. - create_SOL() closes the previous socket and I/O thread first. It overwrote the socket (the connection leaked) and init() failed while the old thread ran, so each reconnect left an open TCP connection at the gateway and the node stayed invalid although "Connected". If init() fails, the new socket is closed. - create()/validate()/reconnect() are serialised: they run on the node factory thread and on the hardware manager thread. - Registered with the hardware manager once. Re-creation from the hardware manager locked its non-recursive list mutex again (deadlock). - Reconnect back-off 2 s -> 30 s. - recv() == 0 on TCP means the gateway closed the connection (a stale errno was checked before). - Destructor resigns from the hardware manager before closing. deviceNodeFactory - deleteNode() deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. - Nodes are deleted after the validation loop (erasing inside it skipped the next node). deviceNode::validate - Compares the configuration only. "online" changes whenever another object of the same node id is created or deleted. hardwareManager::assignResource - Ignores a resource that is already assigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
2 months ago
​
​
​
2 months ago
​
2 months ago
2 months ago
2 months ago
​
​
2 months ago
​
​
​
​
​
​
2 months ago
2 months ago
​
2 months ago
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "hardwareManager.h"
  2. #include <algorithm>
  3. #include <sys/statvfs.h>
  4. #include <sys/sysinfo.h>
  5. #include <sys/types.h>
  6. #include <time.h>
  7. #ifdef USE_MYSQL
  8. #include "MySQL_dataBinder.h"
  9. #else
  10. #include "SQLite_dataBinder.h"
  11. #endif
  12. // #include "rgooseKernel.h"
  13. // #include "rgooseDBConnector.h"
  14. #include "../alg/generic/heapDebug.h"
  15. #include "../hardware/hid/hid_dc.h"
  16. #include "hardwareResourceNetworkProvider.h"
  17. #include "hardwareResourceSerialProvider.h"
  18. #include "hardwareResourceVideoProvider.h"
  19. #include <hidapi/hidapi.h>
  20. #include <ifaddrs.h>
  21. #include <linux/serial.h>
  22. #include <linux/videodev2.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/resource.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. // #define NO_EXEC_EXTERNALS
  28. #define LOG(X) m_pLog->Log("HWM", this, X)
  29. #ifdef DEBUG
  30. #define LOG_ERROR(X) \
  31. { \
  32. char _tBfrErrMSG2w34[1024]; \
  33. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  34. m_pLog->Log("HWM", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  35. }
  36. #else
  37. #define LOG_ERROR(X) m_pLog->Log("HWM", this, _LOG_ERROR, X);
  38. #endif
  39. hardwareManager * hardwareManager::_instance = NULL;
  40. /**
  41. * Returns the single instance of the object.
  42. */
  43. hardwareManager * hardwareManager::getInstance(bool cold_start)
  44. {
  45. // check if the instance has been created yet
  46. if (0 == _instance)
  47. {
  48. // if not, then create it
  49. _instance = new hardwareManager(cold_start);
  50. };
  51. // return the single instance
  52. return _instance;
  53. }
  54. hardwareManager::hardwareManager(bool cold_start)
  55. {
  56. m_pLog = basicLogCollection::getInstance();
  57. if ( cold_start ) {
  58. //m_bHardwareEnumerated = false;
  59. m_nServerNodeID = 0;
  60. };
  61. m_nServerNodeID = 0;
  62. m_nServerID = 0;
  63. m_nSiteID = 0;
  64. #ifdef MEMORY_CONSUMPTION_LIMIT
  65. m_bCacheCleanCounter = 3;
  66. #endif
  67. memset(&m_sSYS_INFO, 0, sizeof(DBC_SYS_INFO));
  68. strncpy(m_sSYS_INFO.SW_INFO, _BUILD_STRING_, 128);
  69. config_setting_t * m_pSettings = basicSettingsProvider::getInstance()->getSubNode(NULL, "HARDWARE");
  70. if (m_pSettings)
  71. {
  72. #ifdef MEMORY_CONSUMPTION_LIMIT
  73. config_setting_t * pSetting;
  74. long long int tVal;
  75. const char * tBuf;
  76. char tBuffer[128];
  77. if (! config_setting_lookup_int64(m_pSettings, "MEMORY_LIMIT", &tVal))
  78. {
  79. tVal = m_nMemoryLimitHeapSize = MEMORY_CONSUMPTION_LIMIT;
  80. pSetting = config_setting_add(m_pSettings, "MEMORY_LIMIT", CONFIG_TYPE_INT64);
  81. config_setting_set_int64(pSetting, tVal);
  82. basicSettingsProvider::getInstance()->compromise();
  83. LOG("MEMORY LIMIT NOT DEFINED - Using 2Gb Default!");
  84. }
  85. else
  86. {
  87. m_nMemoryLimitHeapSize = tVal;
  88. sprintf(tBuffer, "MEMORY LIMIT Read From Settings: %u bytes", m_nMemoryLimitHeapSize);
  89. LOG(tBuffer);
  90. };
  91. #endif
  92. basicSettingsProvider::getInstance()->validate();
  93. }
  94. else
  95. {
  96. #ifdef MEMORY_CONSUMPTION_LIMIT
  97. m_nMemoryLimitHeapSize = MEMORY_CONSUMPTION_LIMIT;
  98. #endif
  99. };
  100. #ifndef NO_EXEC_EXTERNALS
  101. FILE * fd = popen("/bin/uname -s -r -v -m", "r");
  102. if (fd != NULL)
  103. {
  104. fgets(m_sSYS_INFO.OS_INFO, 127, fd);
  105. m_sSYS_INFO.OS_INFO[127] = (char) 0;
  106. trim(m_sSYS_INFO.OS_INFO);
  107. memset(m_sSYS_INFO.HW_INFO, 0, 128);
  108. fclose(fd);
  109. fd = NULL;
  110. };
  111. #else
  112. FILE * fd = NULL;
  113. #endif
  114. fd = fopen("/proc/cpuinfo", "r");
  115. if (fd != NULL)
  116. {
  117. char tBB[1024];
  118. bool bARM = false;
  119. while (! feof(fd))
  120. {
  121. fgets(tBB, 1024, fd);
  122. trim(tBB);
  123. if (strcasestr(tBB, "model") != NULL)
  124. {
  125. if (strcasestr(tBB, "model name") != NULL)
  126. {
  127. if (! bARM)
  128. {
  129. if (strcasestr(tBB, "ARM") != NULL)
  130. {
  131. bARM = true;
  132. }
  133. else
  134. {
  135. CArray * tA = explode(':', tBB);
  136. if (tA)
  137. {
  138. strncpy(m_sSYS_INFO.HW_INFO, tA->at(1), 127);
  139. delete tA;
  140. };
  141. break;
  142. };
  143. };
  144. }
  145. else
  146. {
  147. if (bARM)
  148. {
  149. CArray * tA = explode(':', tBB);
  150. if (tA)
  151. {
  152. strncpy(m_sSYS_INFO.HW_INFO, tA->at(1), 127);
  153. delete tA;
  154. };
  155. break;
  156. };
  157. };
  158. };
  159. };
  160. m_sSYS_INFO.HW_INFO[127] = (char) 0;
  161. trim(m_sSYS_INFO.HW_INFO);
  162. fclose(fd);
  163. fd = NULL;
  164. };
  165. #ifdef USE_MYSQL
  166. m_pDBC = mysqlDBC::getInstance();
  167. #else
  168. m_pDBC = sqliteDBC::getInstance();
  169. #endif
  170. if (m_pDBC == NULL)
  171. {
  172. LOG_ERROR("Failure to get DBC Instance -> Forced HW Enumeration!");
  173. enumerateHardware();
  174. }
  175. else
  176. {
  177. char tB[128];
  178. struct rlimit rl;// will be initialized by getting info = {0};
  179. getrlimit( RLIMIT_STACK, &rl );
  180. sprintf(tB, "Stack Size: %lu", rl.rlim_cur); // RLIM_INFINITY
  181. LOG(tB);
  182. sprintf(tB, "MAX Stack Size: %lu", rl.rlim_max);
  183. LOG(tB);
  184. // enumerateHardware( !cold_start );
  185. enumerateNIFS();
  186. // #ifdef DEBUG
  187. LOG("Load Ressources...");
  188. // #endif
  189. loadResources();
  190. // #ifdef DEBUG
  191. LOG("Load Resources Done");
  192. // #endif
  193. };
  194. }
  195. hardwareManager::~hardwareManager()
  196. {
  197. #ifdef DEBUG
  198. LOG("Destructor Begin");
  199. #endif
  200. unloadResources();
  201. #ifdef DEBUG
  202. LOG("Destructor End");
  203. #endif
  204. _instance = NULL;
  205. }
  206. void hardwareManager::unloadResources(void)
  207. {
  208. #ifdef DEBUG
  209. LOG("Unload Resources ...");
  210. #endif
  211. // m_mVectorProtector.lock();
  212. if (m_vResources.size())
  213. {
  214. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  215. {
  216. switch ((*idx)->getHardwareType())
  217. {
  218. case HARDWARE_UART:
  219. case HARDWARE_IP_UART : delete((hardwareResourceSerialProvider *) (*idx)); break;
  220. case HARDWARE_NIF_LOOPBACK :
  221. case HARDWARE_NIF_ETHERNET :
  222. case HARDWARE_NIF_WIFI :
  223. case HARDWARE_NIF_VIRTUAL :
  224. case HARDWARE_NIF_TUNEL : delete((hardwareResourceNetwork *) (*idx)); break;
  225. case HARDWARE_ANALOG_VIDEO_IN :
  226. case HARDWARE_DIGITAL_VIDEO_IN: delete((hardwareResourceVideoProvider *) (*idx)); break;
  227. default: break;
  228. };
  229. };
  230. m_vResources.clear();
  231. };
  232. // m_mVectorProtector.unlock();
  233. #ifdef DEBUG
  234. LOG("Unload Resources Done");
  235. #endif
  236. }
  237. void hardwareManager::loadResources(void)
  238. {
  239. /** @todo Valgrind is showing several memory leaks from this function.
  240. *
  241. * ~~~~
  242. ==00:00:04:04.207 26021== 513 (480 direct, 33 indirect) bytes in 1 blocks are definitely lost in loss record 3,863 of 3,972
  243. ==00:00:04:04.207 26021== at 0x4838DEF: operator new(unsigned long) (vg_replace_malloc.c:342)
  244. ==00:00:04:04.207 26021== by 0x16D0EF: hardwareManager::loadResources() (hardwareManager.cpp:318)
  245. ==00:00:04:04.207 26021== by 0x16C9CE: hardwareManager::hardwareManager(bool) (hardwareManager.cpp:174)
  246. ==00:00:04:04.207 26021== by 0x16C470: hardwareManager::getInstance(bool) (hardwareManager.cpp:55)
  247. ==00:00:04:04.207 26021== by 0x1D6E89: mojoKernel::create() (mojoKernel.cpp:264)
  248. ==00:00:04:04.207 26021== by 0x1D5C60: main (mojoBackend.cpp:476)
  249. ==00:00:04:04.207 26021== by 0x779CD79: __libc_start_main (libc-start.c:308)
  250. ==00:00:04:04.207 26021== by 0x124CF9: _start (in /home/stephane/src/argus/Projects/la_mojo/Debug86/mojo_d)
  251. ==00:00:04:04.207 26021== by 0x1FFF000497: ???
  252. ==00:00:04:04.207 26021== by 0x1B: ???
  253. * ~~~~
  254. *
  255. * And:
  256. *
  257. * ~~~~
  258. ==00:00:04:04.198 26021== 240 bytes in 3 blocks are definitely lost in loss record 3,796 of 3,972
  259. ==00:00:04:04.198 26021== at 0x483877F: malloc (vg_replace_malloc.c:307)
  260. ==00:00:04:04.198 26021== by 0x233EAA: sqliteDBC::getResourceData(unsigned int) (SQLite_dataBinder.cpp:2897)
  261. ==00:00:04:04.198 26021== by 0x16CD43: hardwareManager::loadResources() (hardwareManager.cpp:260)
  262. ==00:00:04:04.198 26021== by 0x16C9CE: hardwareManager::hardwareManager(bool) (hardwareManager.cpp:174)
  263. ==00:00:04:04.198 26021== by 0x16C470: hardwareManager::getInstance(bool) (hardwareManager.cpp:55)
  264. ==00:00:04:04.198 26021== by 0x1D6E89: mojoKernel::create() (mojoKernel.cpp:264)
  265. ==00:00:04:04.198 26021== by 0x1D5C60: main (mojoBackend.cpp:476)
  266. ==00:00:04:04.198 26021== by 0x779CD79: __libc_start_main (libc-start.c:308)
  267. ==00:00:04:04.198 26021== by 0x124CF9: _start (in /home/stephane/src/argus/Projects/la_mojo/Debug86/mojo_d)
  268. ==00:00:04:04.198 26021== by 0x1FFF000497: ???
  269. ==00:00:04:04.198 26021== by 0x1B: ???
  270. * ~~~~
  271. */
  272. if (m_pDBC == NULL)
  273. {
  274. return;
  275. }
  276. DBC_INT_LIST * tVal;
  277. tVal = m_pDBC->listResources(); // load whole site resources
  278. // tVal = m_pDBC->listResources( m_nServerNodeID );//Load Only Server Node resources
  279. if (tVal != NULL)
  280. {
  281. if (tVal->size())
  282. {
  283. for (DBC_INT_LIST::iterator index = tVal->begin(); index < tVal->end(); index++)
  284. {
  285. DBC_RESOURCE_DATA * tData = m_pDBC->getResourceData(*index);
  286. if (tData != NULL)
  287. {
  288. hardwareResource * tPtr = NULL;
  289. switch (tData->type)
  290. {
  291. case HARDWARE_UART:
  292. {
  293. #ifdef DEBUG
  294. LOG("Construct HW UART");
  295. #endif
  296. hardwareResourceSerialProvider * tRes = new hardwareResourceSerialProvider(tData->id);
  297. if (tRes != NULL)
  298. {
  299. if (tRes->create())
  300. {
  301. tPtr = (hardwareResource *) tRes;
  302. }
  303. else
  304. {
  305. LOG_ERROR("Error Creating HW UART!");
  306. delete(tRes);
  307. };
  308. };
  309. break;
  310. };
  311. case HARDWARE_NIF_LOOPBACK:
  312. case HARDWARE_NIF_ETHERNET:
  313. case HARDWARE_NIF_WIFI:
  314. case HARDWARE_NIF_VIRTUAL:
  315. case HARDWARE_NIF_TUNEL:
  316. case HARDWARE_NIF_WAN:
  317. {
  318. #ifdef DEBUG
  319. LOG("Construct NetIF");
  320. #endif
  321. hardwareResourceNetwork * tRes = new hardwareResourceNetwork(tData->id);
  322. if (tRes != NULL)
  323. {
  324. if (tRes->create())
  325. {
  326. tPtr = (hardwareResource *) tRes;
  327. }
  328. else
  329. {
  330. LOG_ERROR("Error Creating Network Ressource!");
  331. delete(tRes);
  332. };
  333. };
  334. break;
  335. };
  336. case HARDWARE_IP_UART:
  337. {
  338. #ifdef DEBUG
  339. LOG("Construct IP UART");
  340. #endif
  341. hardwareResourceSerialProvider * tRes = new hardwareResourceSerialProvider(tData->id);
  342. if (tRes != NULL)
  343. {
  344. if (tRes->create())
  345. {
  346. tPtr = (hardwareResource *) tRes;
  347. }
  348. else
  349. {
  350. LOG_ERROR("Error Creating IP UART!");
  351. delete(tRes);
  352. };
  353. };
  354. break;
  355. };
  356. case HARDWARE_ANALOG_VIDEO_IN:
  357. case HARDWARE_DIGITAL_VIDEO_IN:
  358. {
  359. #ifdef DEBUG
  360. LOG("Construct VIDEO IN");
  361. #endif
  362. hardwareResourceVideoProvider * tRes = new hardwareResourceVideoProvider(tData->id);
  363. if (tRes != NULL)
  364. {
  365. if (tRes->create())
  366. {
  367. tPtr = (hardwareResource *) tRes;
  368. }
  369. else
  370. {
  371. LOG_ERROR("Error Creating Video IN!");
  372. delete(tRes);
  373. };
  374. };
  375. break;
  376. };
  377. #ifdef DEBUG
  378. default:
  379. {
  380. char tB[128];
  381. sprintf(tB, "Unknown Ressource[%d] Type[%d]!", *index, tData->type);
  382. LOG_ERROR(tB);
  383. break;
  384. };
  385. #endif
  386. };
  387. if (tPtr != NULL)
  388. {
  389. #ifdef DEBUG
  390. LOG("Resource Sucessfully Created!");
  391. #endif
  392. m_mVectorProtector.lock();
  393. m_vResources.push_back(tPtr);
  394. m_mVectorProtector.unlock();
  395. m_pDBC->setRessourceActive(*index);
  396. }
  397. else
  398. {
  399. m_pDBC->setRessourceActive(*index, false);
  400. };
  401. free(tData);
  402. }
  403. else
  404. {
  405. LOG_ERROR("Can not fetch Resource Data from Database!");
  406. };
  407. };
  408. #ifdef DEBUG
  409. }
  410. else
  411. {
  412. LOG("Fetched Empty List!");
  413. #endif
  414. };
  415. tVal->clear();
  416. delete(tVal);
  417. #ifdef DEBUG
  418. }
  419. else
  420. {
  421. LOG("Fetched NULL!");
  422. #endif
  423. };
  424. }
  425. bool hardwareManager::validate(void)
  426. {
  427. bool RetVal = true;
  428. m_mVectorProtector.lock();
  429. if (m_vResources.size())
  430. {
  431. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  432. {
  433. if ((*idx)->selfValidating())
  434. {
  435. continue; // its node validates / reconnects it (node factory thread)
  436. };
  437. if (! (*idx)->validate())
  438. {
  439. // recreate Resource
  440. LOG_ERROR("HW Resource Invalid!");
  441. m_pDBC->setRessourceActive((*idx)->getDbId(), false);
  442. #ifndef DEBUG
  443. char tB[256];
  444. sprintf(tB, "Info: DBID[%d] R-Type: %d Provider: %d ", (*idx)->getDbId(), (*idx)->getHardwareType(), (*idx)->getProviderType());
  445. LOG(tB);
  446. #endif
  447. if (! (*idx)->create())
  448. {
  449. LOG_ERROR("HW Resource Creation Failure!");
  450. }
  451. else
  452. {
  453. m_pDBC->setRessourceActive((*idx)->getDbId());
  454. #ifndef DEBUG
  455. LOG("Re-Created OK");
  456. #endif
  457. };
  458. #ifdef DEBUG_EXTRA
  459. }
  460. else
  461. {
  462. LOG("Resource OK!");
  463. #endif
  464. };
  465. };
  466. #ifdef DEBUG_EXTRA
  467. }
  468. else
  469. {
  470. LOG("No Resources!");
  471. #endif
  472. };
  473. m_mVectorProtector.unlock();
  474. // Mem Validator
  475. #ifdef MEMORY_CONSUMPTION_LIMIT
  476. if (m_nMemoryLimitHeapSize)
  477. {
  478. size_t memStat = getCurrentRSS();
  479. #ifdef DEBUG_EXTRA
  480. char MEM_MSG[128];
  481. sprintf(MEM_MSG, "MEM: %" PRIu64 " TARG: %" PRIu64, memStat, m_nMemoryLimitHeapSize);
  482. LOG(MEM_MSG);
  483. #endif
  484. // https://www.ebay.com/itm/274974088159?
  485. RetVal = (memStat < m_nMemoryLimitHeapSize);
  486. if (! RetVal)
  487. {
  488. LOG_ERROR("Memory Consumption too BIG!");
  489. };
  490. };
  491. if (m_bCacheCleanCounter == 0)
  492. {
  493. LOG_ERROR("System Cache Fas Too Big Too Many Times!");
  494. RetVal = false;
  495. };
  496. #endif
  497. return RetVal;
  498. }
  499. void hardwareManager::getMAC(CSTR if_name, CSTR buf)
  500. {
  501. char tB[128];
  502. FILE * fd = NULL;
  503. if (buf)
  504. {
  505. sprintf(tB, "/sys/class/net/%s/address", if_name);
  506. #ifdef DEBUG_EXTRA
  507. LOG(tB);
  508. #endif
  509. fd = fopen(tB, "r");
  510. if (fd)
  511. {
  512. fread(buf, 1, INET6_ADDRSTRLEN, fd);
  513. fclose(fd);
  514. buf = trim(buf);
  515. }
  516. else
  517. {
  518. memset(buf, 0, INET6_ADDRSTRLEN);
  519. };
  520. #ifdef DEBUG_EXTRA
  521. LOG(buf);
  522. #endif
  523. };
  524. }
  525. void hardwareManager::getGW(CSTR if_name, CSTR buf)
  526. {
  527. memset(buf, 0, INET6_ADDRSTRLEN);
  528. #ifndef NO_EXEC_EXTERNALS
  529. FILE * fd = popen("route -n", "r");
  530. char tB[1024];
  531. // char expBuf[1024];
  532. if (fd)
  533. {
  534. while (! feof(fd))
  535. {
  536. fgets((CSTR) tB, 1024, fd);
  537. trim((CSTR) tB);
  538. if (strlen(tB))
  539. {
  540. if (strstr(tB, if_name) != NULL)
  541. {
  542. // strncpy( expBuf, tB, 1024 );
  543. CArray * CA = explode(' ', tB);
  544. if (CA)
  545. {
  546. // for (int i=0; i< CA->size(); i++ ) {
  547. // char tB[128];
  548. // trim( CA->at(i) );
  549. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  550. // LOG( tB );
  551. // };
  552. trim(CA->at(1));
  553. if (strstr(CA->at(1), "0.0.0.0") == NULL)
  554. {
  555. strncpy(buf, CA->at(1), INET6_ADDRSTRLEN);
  556. };
  557. delete(CA);
  558. };
  559. };
  560. };
  561. };
  562. fclose(fd);
  563. };
  564. #endif
  565. if (strlen(buf) == 0)
  566. {
  567. strncpy(buf, "0.0.0.0", INET6_ADDRSTRLEN);
  568. }
  569. }
  570. void hardwareManager::getTTyDesc(CSTR dev_name, CSTR buf)
  571. {
  572. memset(buf, 0, DBC_RESOURCE_DESC_SIZE);
  573. #ifndef NO_EXEC_EXTERNALS
  574. FILE * fd = popen("dmesg | grep tty", "r");
  575. char tB[1024];
  576. // char expBuf[1024];
  577. if (fd)
  578. {
  579. while (! feof(fd))
  580. {
  581. fgets((CSTR) tB, 1024, fd);
  582. trim((CSTR) tB);
  583. if (strlen(tB))
  584. {
  585. if (strstr(tB, dev_name) != NULL)
  586. {
  587. CArray * CA = explode(':', tB);
  588. if (CA)
  589. {
  590. if (CA->size())
  591. {
  592. trim(CA->at(CA->size() - 1));
  593. strncpy(buf, CA->at(CA->size() - 1), CTEXT);
  594. buf[CTEXT - 1] = (char) 0;
  595. };
  596. delete(CA);
  597. };
  598. };
  599. };
  600. };
  601. fclose(fd);
  602. };
  603. #endif
  604. if (strlen(buf) == 0)
  605. {
  606. strncpy(buf, "--Unknown--", DBC_RESOURCE_DESC_SIZE);
  607. buf[DBC_RESOURCE_DESC_SIZE - 1] = (char) 0;
  608. };
  609. }
  610. PARSE_RESULT_NET_INTERFACES * hardwareManager::parse_neconfig(CSTR if_name)
  611. {
  612. PARSE_RESULT_NET_INTERFACES * tVal = NULL;
  613. FILE * fd;
  614. bool cConfig = false;
  615. fd = fopen("/etc/network/interfaces", "r");
  616. if (fd)
  617. {
  618. char tB[1024];
  619. char expBuf[1024];
  620. tVal = (PARSE_RESULT_NET_INTERFACES *) malloc(sizeof(PARSE_RESULT_NET_INTERFACES));
  621. if (tVal)
  622. {
  623. memset(tVal, 0, sizeof(PARSE_RESULT_NET_INTERFACES));
  624. while (! feof(fd))
  625. {
  626. fgets((CSTR) tB, 1024, fd);
  627. trim((CSTR) tB);
  628. if (strlen(tB))
  629. {
  630. strncpy(expBuf, tB, 1024);
  631. CArray * CA = explode(' ', expBuf);
  632. if (CA)
  633. {
  634. for (CArray::iterator idx = CA->begin(); idx < CA->end(); idx++)
  635. {
  636. if ((*idx)[0] == '#')
  637. {
  638. break;
  639. }
  640. if (strstr(*idx, "auto") != NULL)
  641. {
  642. if (strstr(tB, if_name) != NULL)
  643. {
  644. tVal->enabled = true;
  645. }
  646. }
  647. if (strstr(*idx, "allow-hotplug") != NULL)
  648. {
  649. if (strstr(tB, if_name) != NULL)
  650. {
  651. tVal->hot_plug = true;
  652. }
  653. }
  654. if (strstr(*idx, "iface") != NULL)
  655. {
  656. if (strstr(tB, if_name) != NULL)
  657. {
  658. if (strstr(tB, "static") != NULL)
  659. {
  660. tVal->if_static = true;
  661. }
  662. cConfig = true;
  663. }
  664. else
  665. {
  666. cConfig = false;
  667. }
  668. }
  669. if (strstr(*idx, "address") != NULL)
  670. {
  671. if (cConfig)
  672. {
  673. idx++;
  674. if (strstr(*idx, "/") != NULL)
  675. {
  676. int8_t sh = strstr(*idx, "/") - (*idx);
  677. strncpy(tVal->address, *idx, sh);
  678. tVal->address[sh] = (char) 0;
  679. int8_t bm = atoi((*idx) + sh + 1);
  680. union
  681. {
  682. uint32_t mm;
  683. uint8_t nm[4];
  684. } nmu = {0};
  685. for (sh = 0; sh < bm; sh++)
  686. {
  687. nmu.mm |= (1 << sh);
  688. }
  689. sprintf(tVal->netmask, "%d.%d.%d.%d", nmu.nm[0], nmu.nm[1], nmu.nm[2], nmu.nm[3]);
  690. }
  691. else
  692. {
  693. strncpy(tVal->address, *idx, INET6_ADDRSTRLEN);
  694. };
  695. };
  696. }
  697. if (strstr(*idx, "netmask") != NULL)
  698. {
  699. if (cConfig)
  700. {
  701. LOG("netMSK");
  702. idx++;
  703. LOG(*idx);
  704. strncpy(tVal->netmask, *idx, INET6_ADDRSTRLEN);
  705. };
  706. }
  707. // if(strstr((*idx), "bridge_ports")!= NULL ) cMode = 6;
  708. // if(strstr((*idx), "bridge_stp")!= NULL ) cMode = 7;
  709. // if(strstr((*idx), "bridge_waitport")!= NULL ) cMode = 8;
  710. // if(strstr((*idx), "bridge_fd")!= NULL ) cMode = 9;
  711. };
  712. delete(CA);
  713. };
  714. };
  715. };
  716. };
  717. fclose(fd);
  718. };
  719. #ifdef DEBUG_EXTRA
  720. if (tVal)
  721. {
  722. if (tVal->enabled)
  723. {
  724. LOG("NIF Enabled");
  725. }
  726. else
  727. {
  728. LOG("NIF Disabled");
  729. }
  730. if (tVal->hot_plug)
  731. {
  732. LOG("NIF Hot Plugable");
  733. }
  734. else
  735. {
  736. LOG("NIF not Hot Plugable");
  737. }
  738. if (tVal->if_static)
  739. {
  740. LOG("Address is static");
  741. }
  742. else
  743. {
  744. LOG("Address is Dynamic");
  745. }
  746. if (strlen(tVal->address))
  747. {
  748. LOG(tVal->address);
  749. }
  750. if (strlen(tVal->netmask))
  751. {
  752. LOG(tVal->netmask);
  753. }
  754. };
  755. #endif
  756. return tVal;
  757. }
  758. PARSE_RESULT_WIFI_SETTINGS * hardwareManager::parse_wifi(CSTR if_name)
  759. {
  760. PARSE_RESULT_WIFI_SETTINGS * tVal = NULL;
  761. FILE * fd;
  762. bool cConfig = false;
  763. bool is_wifi = false;
  764. tVal = (PARSE_RESULT_WIFI_SETTINGS *) malloc(sizeof(PARSE_RESULT_WIFI_SETTINGS));
  765. if (tVal)
  766. {
  767. memset(tVal, 0, sizeof(PARSE_RESULT_WIFI_SETTINGS));
  768. fd = fopen("/etc/dnsmasq.conf", "r");
  769. if (fd)
  770. {
  771. char tB[1024];
  772. char expBuf[1024];
  773. while (! feof(fd))
  774. {
  775. fgets((CSTR) tB, 1024, fd);
  776. trim((CSTR) tB);
  777. if (strlen(tB))
  778. {
  779. strncpy(expBuf, tB, 1024);
  780. CArray * CA = explode('=', expBuf);
  781. if (CA)
  782. {
  783. if (CA->size())
  784. {
  785. if (CA->at(0)[0] == '#')
  786. {
  787. continue;
  788. }
  789. if (strstr(CA->at(0), "interface") != NULL)
  790. {
  791. if (strstr(tB, if_name) != NULL)
  792. {
  793. cConfig = true;
  794. tVal->dhcp_enabled = true;
  795. };
  796. };
  797. if (cConfig)
  798. {
  799. if (strstr(CA->at(0), "dhcp-range") != NULL)
  800. {
  801. char tT[1024];
  802. strncpy(tT, CA->at(1), 1024);
  803. CArray * CB = explode(',', tT);
  804. if (CB)
  805. {
  806. if (CB->size() > 3)
  807. {
  808. strncpy(tVal->range_start, CB->at(0), INET6_ADDRSTRLEN);
  809. strncpy(tVal->range_stop, CB->at(1), INET6_ADDRSTRLEN);
  810. strncpy(tVal->netmask, CB->at(2), INET6_ADDRSTRLEN);
  811. strncpy(tVal->lease_period, CB->at(3), INET6_ADDRSTRLEN);
  812. };
  813. delete(CB);
  814. };
  815. };
  816. };
  817. };
  818. delete(CA);
  819. };
  820. };
  821. };
  822. fclose(fd);
  823. };
  824. #ifndef NO_EXEC_EXTERNALS
  825. // Check WiFi
  826. if (strstr(if_name, "w") != NULL)
  827. {
  828. char tName[128];
  829. is_wifi = true;
  830. sprintf(tName, "iwconfig %s", if_name);
  831. fd = popen(tName, "r");
  832. if (fd)
  833. {
  834. char tB[1024];
  835. while (! feof(fd))
  836. {
  837. fgets((CSTR) tB, 1024, fd);
  838. trim(tB);
  839. if (strstr(tB, "Mode") != NULL)
  840. {
  841. if (strstr(tB, "Master") != NULL)
  842. {
  843. tVal->access_point = true;
  844. }
  845. }
  846. };
  847. fclose(fd);
  848. };
  849. };
  850. #endif
  851. if (tVal->access_point)
  852. {
  853. fd = fopen("/etc/hostapd/hostapd.conf", "r");
  854. if (fd)
  855. {
  856. char tB[1024];
  857. char expBuf[1024];
  858. while (! feof(fd))
  859. {
  860. fgets((CSTR) tB, 1024, fd);
  861. trim(tB);
  862. if (strlen(tB))
  863. {
  864. if (tB[0] == '#')
  865. {
  866. continue;
  867. }
  868. strncpy(expBuf, tB, 1024);
  869. CArray * CA = explode('=', expBuf);
  870. if (CA)
  871. {
  872. if (CA->size())
  873. {
  874. if (strstr(CA->at(0), "ssid") != NULL)
  875. {
  876. if (strstr(CA->at(0), "ignore_broadcast_ssid") == NULL)
  877. {
  878. strncpy(tVal->SSID, CA->at(1), 64);
  879. }
  880. else
  881. {
  882. tVal->hiden_ssid = (bool) atoi(CA->at(1));
  883. }
  884. }
  885. if (strstr(CA->at(0), "wpa_passphrase") != NULL)
  886. {
  887. strncpy(tVal->password, CA->at(1), 64);
  888. }
  889. if (strstr(CA->at(0), "country_code") != NULL)
  890. {
  891. strncpy(tVal->country_code, CA->at(1), 3);
  892. }
  893. if (strstr(CA->at(0), "channel") != NULL)
  894. {
  895. tVal->channel = atoi(CA->at(1));
  896. }
  897. };
  898. delete(CA);
  899. };
  900. };
  901. };
  902. fclose(fd);
  903. };
  904. }
  905. else
  906. {
  907. fd = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r");
  908. if (fd)
  909. {
  910. fclose(fd);
  911. };
  912. };
  913. fd = fopen("/etc/rc.local", "r");
  914. if (fd)
  915. {
  916. char tB[1024];
  917. char expBuf[1024];
  918. while (! feof(fd))
  919. {
  920. fgets((CSTR) tB, 1024, fd);
  921. trim((CSTR) tB);
  922. if (strlen(tB))
  923. {
  924. strncpy(expBuf, tB, 1024);
  925. CArray * CA = explode(' ', expBuf);
  926. if (CA)
  927. {
  928. if (CA->size())
  929. {
  930. if (CA->at(0)[0] == '#')
  931. {
  932. continue;
  933. }
  934. if (strstr(CA->at(0), "iptables-restore") != NULL)
  935. {
  936. char zB[1024];
  937. FILE * afd = fopen(CA->at(2), "r");
  938. if (afd)
  939. {
  940. while (! feof(afd))
  941. {
  942. fgets((CSTR) zB, 1024, afd);
  943. if (strstr(zB, "POSTROUTING") != NULL)
  944. {
  945. if (strstr(zB, "MASQUERADE") != NULL)
  946. {
  947. if (strstr(zB, if_name) == NULL)
  948. {
  949. CArray * CB = explode(' ', zB);
  950. if (CB)
  951. {
  952. if (CB->size() > 3)
  953. {
  954. strncpy(tVal->router_outbond, CB->at(3), INET6_ADDRSTRLEN);
  955. };
  956. delete(CB);
  957. };
  958. tVal->router_enabled = true;
  959. };
  960. }
  961. }
  962. };
  963. fclose(afd);
  964. }
  965. else
  966. {
  967. LOG("Error Opening Firewall Rules!");
  968. }
  969. };
  970. };
  971. delete(CA);
  972. };
  973. };
  974. };
  975. fclose(fd);
  976. }
  977. else
  978. {
  979. LOG_ERROR("Can not open rc.local!");
  980. }
  981. #ifdef DEBUG
  982. char zz[512];
  983. if (is_wifi)
  984. {
  985. if (tVal->access_point)
  986. {
  987. LOG("WiFi is in AP Mode");
  988. sprintf(zz, "\tSSID:%s PWD:%s", tVal->SSID, tVal->password);
  989. LOG(zz);
  990. if (tVal->hiden_ssid)
  991. {
  992. LOG("SSID is hidden");
  993. }
  994. }
  995. else
  996. {
  997. LOG("WiFi is in Client Mode");
  998. }
  999. };
  1000. if (tVal->dhcp_enabled)
  1001. {
  1002. LOG("DHCP Service - Enabled");
  1003. sprintf(zz, "\tRange: %s - %s Lease Time: %s", tVal->range_start, tVal->range_stop, tVal->lease_period);
  1004. LOG(zz);
  1005. }
  1006. else
  1007. {
  1008. LOG("DHCP Service - Disabled");
  1009. };
  1010. if (tVal->router_enabled)
  1011. {
  1012. sprintf(zz, "Routing enabled to [%s] NIF", tVal->router_outbond);
  1013. LOG(zz);
  1014. }
  1015. else
  1016. {
  1017. LOG("Routing Disabled");
  1018. };
  1019. #endif
  1020. };
  1021. return tVal;
  1022. }
  1023. void hardwareManager::enumerateNIFS(void)
  1024. {
  1025. struct ifaddrs * ifaddr = NULL;
  1026. char tBuffer[128];
  1027. // if( m_pKernel == NULL ) return;
  1028. if (m_pDBC == NULL)
  1029. {
  1030. return;
  1031. }
  1032. if (-1 == getifaddrs(&ifaddr))
  1033. {
  1034. LOG_ERROR("No NET INTERFACES?!");
  1035. }
  1036. else
  1037. {
  1038. #ifdef DEBUG
  1039. LOG("Enumerating Network Interfaces");
  1040. #endif
  1041. struct ifaddrs * ifa;
  1042. for (ifa = ifaddr; ifa; ifa = ifa->ifa_next)
  1043. {
  1044. char addr[INET6_ADDRSTRLEN];
  1045. char mask[INET6_ADDRSTRLEN];
  1046. char gw[INET6_ADDRSTRLEN];
  1047. char bc[INET6_ADDRSTRLEN];
  1048. char mac[INET6_ADDRSTRLEN];
  1049. char device[INET6_ADDRSTRLEN];
  1050. uint32_t nif_id = 0;
  1051. uint32_t nif_aux_id = 0;
  1052. uint32_t router_id = 0;
  1053. NIF_TYPE nif_type = NIF_LOOPBACK;
  1054. NETWORK_TYPE net_type = NETWORK_IPV4;
  1055. RESOURCE_TYPE res_type = HARDWARE_NIF_LOOPBACK;
  1056. NIF_MODE nif_mode = ADDRESS_DISABLED;
  1057. #ifdef DEBUG_EXTRA
  1058. LOG("*** Network Interface ***");
  1059. #endif
  1060. strncpy(device, ifa->ifa_name, INET6_ADDRSTRLEN);
  1061. strncpy(mac, "--no-mac--", INET6_ADDRSTRLEN);
  1062. #ifdef DEBUG_EXTRA
  1063. LOG(device);
  1064. #endif
  1065. // Filter Loopback if
  1066. if (strstr(device, "lo") != NULL)
  1067. {
  1068. nif_type = NIF_LOOPBACK;
  1069. res_type = HARDWARE_NIF_LOOPBACK;
  1070. // Not Implemented
  1071. #ifdef DEBUG_EXTRA
  1072. LOG("Loopback Device -> SKIP");
  1073. #endif
  1074. continue;
  1075. };
  1076. // Filter vpn TUN if
  1077. if (strstr(device, "tun") != NULL)
  1078. {
  1079. nif_type = NIF_VPN_TUNEL;
  1080. res_type = HARDWARE_NIF_TUNEL;
  1081. // Not Implemented Yet
  1082. #ifdef DEBUG_EXTRA
  1083. LOG("Tunel Device -> SKIP");
  1084. #endif
  1085. continue;
  1086. };
  1087. if (strstr(device, "e") != NULL)
  1088. {
  1089. nif_type = NIF_ETHERNET;
  1090. res_type = HARDWARE_NIF_ETHERNET;
  1091. };
  1092. if (strstr(device, "wl") != NULL)
  1093. {
  1094. nif_type = NIF_WIFI;
  1095. res_type = HARDWARE_NIF_WIFI;
  1096. };
  1097. if (strstr(device, "ww") != NULL)
  1098. {
  1099. nif_type = NIF_WAN;
  1100. res_type = HARDWARE_NIF_WAN;
  1101. };
  1102. if (ifa->ifa_addr != NULL)
  1103. {
  1104. if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6)
  1105. {
  1106. #ifdef DEBUG_EXTRA
  1107. LOG("No INET ADDRESSES -> SKIP");
  1108. #endif
  1109. continue;
  1110. }
  1111. }
  1112. else
  1113. {
  1114. #ifdef DEBUG_EXTRA
  1115. LOG("ifa->ifa_addr is NULL -> SKIP");
  1116. #endif
  1117. continue;
  1118. };
  1119. #ifdef DEBUG_EXTRA
  1120. LOG("Getting Info....");
  1121. #endif
  1122. getMAC(device, mac);
  1123. getGW(device, gw);
  1124. #ifdef DEBUG_EXTRA
  1125. LOG(mac);
  1126. LOG(gw);
  1127. #endif
  1128. PARSE_RESULT_NET_INTERFACES * tt = NULL;
  1129. PARSE_RESULT_WIFI_SETTINGS * tw = NULL;
  1130. #ifdef DEBUG_EXTRA
  1131. LOG("Parse NetConfig Info....");
  1132. #endif
  1133. tt = parse_neconfig(device);
  1134. if (tt)
  1135. {
  1136. if (tt->enabled)
  1137. {
  1138. if (tt->if_static)
  1139. {
  1140. nif_mode = ADDRESS_STATIC;
  1141. }
  1142. else
  1143. {
  1144. nif_mode = ADDRESS_DYNAMIC;
  1145. };
  1146. }
  1147. else
  1148. {
  1149. nif_mode = ADDRESS_DISABLED;
  1150. };
  1151. }
  1152. else
  1153. {
  1154. LOG_ERROR("Error Parsing System NIF Settings!");
  1155. continue;
  1156. };
  1157. #ifdef DEBUG_EXTRA
  1158. LOG("Parse Routing/DHCP/AP Info....");
  1159. #endif
  1160. tw = parse_wifi(device);
  1161. if (tw)
  1162. {
  1163. #ifdef DEBUG_EXTRA
  1164. LOG("Routing/AP Info OK!");
  1165. #endif
  1166. if (nif_mode == ADDRESS_STATIC)
  1167. {
  1168. if (tw->dhcp_enabled)
  1169. {
  1170. #ifdef DEBUG_EXTRA
  1171. LOG("Address is Router");
  1172. #endif
  1173. nif_mode = ADDRESS_STATIC_RAP;
  1174. };
  1175. };
  1176. nif_id = m_pDBC->addLocalNIF(device, mac, nif_type, nif_mode);
  1177. #ifdef DEBUG_EXTRA
  1178. sprintf(tBuffer, "NIF(%s) added as [%d]", device, nif_id);
  1179. LOG(tBuffer);
  1180. #endif
  1181. if (nif_type == NIF_WIFI)
  1182. {
  1183. if (nif_id == 0)
  1184. {
  1185. LOG_ERROR("Error Adding NIF in DB!");
  1186. continue;
  1187. };
  1188. WIFI_MODE w_mode;
  1189. if (! tt->enabled)
  1190. {
  1191. w_mode = WIFI_DISABLED;
  1192. }
  1193. else
  1194. {
  1195. if (tt->if_static)
  1196. {
  1197. if (tw->access_point)
  1198. {
  1199. w_mode = WIFI_IS_AP;
  1200. uint32_t outbond_id = 0;
  1201. if (tw->router_enabled)
  1202. {
  1203. outbond_id = m_pDBC->getNIF_IDbyDevice(tw->router_outbond);
  1204. };
  1205. if (tw->dhcp_enabled)
  1206. {
  1207. #ifdef DEBUG_EXTRA
  1208. LOG("ADD Router With DHCP");
  1209. #endif
  1210. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period);
  1211. }
  1212. else
  1213. {
  1214. #ifdef DEBUG_EXTRA
  1215. LOG("ADD Router ONLY");
  1216. #endif
  1217. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id);
  1218. };
  1219. #ifdef DEBUG_EXTRA
  1220. sprintf(tBuffer, "Router added as [%d]", router_id);
  1221. LOG(tBuffer);
  1222. #endif
  1223. }
  1224. else
  1225. {
  1226. w_mode = WIFI_CONNECTED_TO_AP;
  1227. };
  1228. };
  1229. };
  1230. #ifdef DEBUG_EXTRA
  1231. LOG("ADD WiFi Record");
  1232. #endif
  1233. nif_aux_id = m_pDBC->addLocalWiFi(nif_id, w_mode, tw->SSID, tw->password, ENCRIPTION_WPA_PSK, router_id);
  1234. };
  1235. if (nif_type == NIF_ETHERNET || nif_type == NIF_ETHERNET_VIRTUAL)
  1236. {
  1237. if (tt->if_static)
  1238. {
  1239. uint32_t outbond_id = 0;
  1240. nif_mode = ADDRESS_STATIC;
  1241. if (tw->router_enabled)
  1242. {
  1243. nif_mode = ADDRESS_STATIC_RAP;
  1244. outbond_id = m_pDBC->getNIF_IDbyDevice(tw->router_outbond);
  1245. };
  1246. if (tw->dhcp_enabled)
  1247. {
  1248. #ifdef DEBUG_EXTRA
  1249. LOG("ADD Router With DHCP");
  1250. #endif
  1251. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period);
  1252. }
  1253. else
  1254. {
  1255. #ifdef DEBUG_EXTRA
  1256. LOG("ADD Router ONLY");
  1257. #endif
  1258. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id);
  1259. };
  1260. #ifdef DEBUG_EXTRA
  1261. sprintf(tBuffer, "Router added as [%d]", router_id);
  1262. LOG(tBuffer);
  1263. #endif
  1264. }
  1265. else
  1266. {
  1267. nif_mode = ADDRESS_DYNAMIC;
  1268. };
  1269. };
  1270. free(tt);
  1271. free(tw);
  1272. }
  1273. else
  1274. {
  1275. LOG_ERROR("Error Parsing System Router/AP Settings!");
  1276. free(tt);
  1277. continue;
  1278. };
  1279. if (nif_aux_id != 0)
  1280. {
  1281. nif_id = m_pDBC->addLocalNIF(device, mac, nif_type, nif_mode, nif_aux_id);
  1282. #ifdef DEBUG_EXTRA
  1283. sprintf(tBuffer, "2 NIF(%s) Updated as [%d]", device, nif_id);
  1284. LOG(tBuffer);
  1285. #endif
  1286. if (nif_id == 0)
  1287. {
  1288. LOG_ERROR("Error Adding NIF in DB!");
  1289. continue;
  1290. };
  1291. #ifdef DEBUG_EXTRA
  1292. LOG("NIF added!");
  1293. }
  1294. else
  1295. {
  1296. LOG("NIF Already added (regular Mode)!");
  1297. #endif
  1298. };
  1299. m_pDBC->addResource(/*m_nServerNodeID,*/ res_type, device, nif_id);
  1300. if (ifa->ifa_addr->sa_family == AF_INET)
  1301. {
  1302. net_type = NETWORK_IPV4;
  1303. // create IPv4 string
  1304. struct sockaddr_in * in = (struct sockaddr_in *) ifa->ifa_addr;
  1305. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  1306. sprintf(mask, "%d.%d.%d.%d", (uint8_t) ifa->ifa_netmask->sa_data[2], (uint8_t) ifa->ifa_netmask->sa_data[3], (uint8_t) ifa->ifa_netmask->sa_data[4], (uint8_t) ifa->ifa_netmask->sa_data[5]);
  1307. sprintf(bc, "%d.%d.%d.%d", (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[2], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[3], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[4], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[5]);
  1308. }
  1309. else
  1310. { // AF_INET6
  1311. net_type = NETWORK_IPV6;
  1312. // create IPv6 string
  1313. struct sockaddr_in6 * in6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  1314. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  1315. sprintf(mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", (uint8_t) ifa->ifa_netmask->sa_data[2], (uint8_t) ifa->ifa_netmask->sa_data[3], (uint8_t) ifa->ifa_netmask->sa_data[4],
  1316. (uint8_t) ifa->ifa_netmask->sa_data[5], (uint8_t) ifa->ifa_netmask->sa_data[6], (uint8_t) ifa->ifa_netmask->sa_data[7], (uint8_t) ifa->ifa_netmask->sa_data[8], (uint8_t) ifa->ifa_netmask->sa_data[9],
  1317. (uint8_t) ifa->ifa_netmask->sa_data[10], (uint8_t) ifa->ifa_netmask->sa_data[11], (uint8_t) ifa->ifa_netmask->sa_data[12], (uint8_t) ifa->ifa_netmask->sa_data[13], (uint8_t) ifa->ifa_netmask->sa_data[14],
  1318. (uint8_t) ifa->ifa_netmask->sa_data[15], (uint8_t) ifa->ifa_netmask->sa_data[16], (uint8_t) ifa->ifa_netmask->sa_data[17]);
  1319. if (ifa->ifa_ifu.ifu_broadaddr != NULL)
  1320. {
  1321. sprintf(bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[2], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  1322. (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[4], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[5], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[6], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
  1323. (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[8], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[9], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[10], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
  1324. (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[12], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[13], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[14], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
  1325. (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[16], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[17]);
  1326. }
  1327. else
  1328. {
  1329. //sprintf(bc, "");
  1330. bc[0]=(char)0;
  1331. };
  1332. };
  1333. m_pDBC->addLocalNetwork(nif_id, net_type, addr, mask, gw, bc);
  1334. #ifdef DEBUG
  1335. sprintf(tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr);
  1336. LOG(tBuffer);
  1337. #endif
  1338. nif_id = 0;
  1339. nif_aux_id = 0;
  1340. router_id = 0;
  1341. nif_type = NIF_LOOPBACK;
  1342. net_type = NETWORK_IPV4;
  1343. res_type = HARDWARE_NIF_LOOPBACK;
  1344. nif_mode = ADDRESS_DISABLED;
  1345. };
  1346. freeifaddrs(ifaddr);
  1347. };
  1348. }
  1349. void hardwareManager::enumerateHardware(void)
  1350. {
  1351. char tBuffer[CTEXT];
  1352. DIR * dFD = NULL;
  1353. struct dirent * pDR = NULL;
  1354. // ToDo: Check Ability to work without Kernel and DBC
  1355. // if( m_pKernel == NULL ) return;
  1356. if (m_pDBC == NULL)
  1357. {
  1358. return;
  1359. }
  1360. #ifdef DEBUG
  1361. LOG("HW Enumeration Started!");
  1362. #endif
  1363. // Moved to a constructor!!
  1364. // m_nServerNodeID = m_pKernel->getServerNodeId();
  1365. // m_nServerID = m_pKernel->getServerId();
  1366. // m_nSiteID = m_pKernel->getSiteId();
  1367. /* Add Network Interfaces */
  1368. // if( !m_pDBC->cleanResources( true ) ){
  1369. // LOG_ERROR("Cannot DeActivate Old Ressources!");
  1370. // return false;
  1371. // };
  1372. if (! m_pDBC->wipeServerResources())
  1373. {
  1374. LOG_ERROR("Cannot Wipe Old Ressources!");
  1375. return;
  1376. };
  1377. enumerateNIFS();
  1378. /* Check HID Devices */
  1379. struct hid_device_info * pDevs;
  1380. uint8_t DCBCount;
  1381. // Enumerate USB devices with the given IDs
  1382. pDevs = hid_enumerate(0x16c0, 0x05df);
  1383. if (pDevs != NULL)
  1384. {
  1385. struct hid_device_info * pCDev = pDevs;
  1386. // Count the number of returned devices
  1387. for (DCBCount = 0; pCDev != NULL; DCBCount++)
  1388. {
  1389. pCDev = pCDev->next;
  1390. }
  1391. // Allocate a buffer for the relays
  1392. if (DCBCount > 0)
  1393. {
  1394. DC_BOARD tDCB;
  1395. // Fill the relay structs
  1396. pCDev = pDevs;
  1397. for (int idx = 0; idx < DCBCount; idx++)
  1398. {
  1399. // Save the path to this device
  1400. memset(tDCB.path, 0, MAX_PATH_LEN);
  1401. strncpy(tDCB.path, pCDev->path, MAX_PATH_LEN);
  1402. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  1403. tDCB.relay_count = atoi((const char *) &pCDev->product_string[8]);
  1404. // Open it to get more details
  1405. hid_device * handle;
  1406. handle = hid_open_path(tDCB.path);
  1407. if (handle)
  1408. {
  1409. unsigned char buf[9];
  1410. // Get the features of the device
  1411. buf[0] = 0x01;
  1412. if (hid_get_feature_report(handle, buf, sizeof(buf)) == -1)
  1413. {
  1414. LOG_ERROR("hid_get_feature_report");
  1415. pCDev = pCDev->next;
  1416. hid_close(handle);
  1417. continue;
  1418. };
  1419. // Set the serial number (0x0 for termination)
  1420. memset(tDCB.serial, 0x0, HID_DCR_SERIIAL_LENGTH + 1);
  1421. memcpy(tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH);
  1422. // Byte 7 in the response contains the target_state of the relays
  1423. tDCB.state = buf[7];
  1424. hid_close(handle);
  1425. #ifdef DEBUG
  1426. // Output the device enumeration details if verbose is on
  1427. LOG("HID DCB Found");
  1428. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  1429. LOG(tBuffer);
  1430. sprintf(tBuffer, " Path: %s", pCDev->path);
  1431. LOG(tBuffer);
  1432. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number);
  1433. LOG(tBuffer);
  1434. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  1435. LOG(tBuffer);
  1436. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  1437. LOG(tBuffer);
  1438. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  1439. LOG(tBuffer);
  1440. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  1441. LOG(tBuffer);
  1442. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  1443. LOG(tBuffer);
  1444. // If debug are on, output individual relay details
  1445. for (int k = 0; k < tDCB.relay_count; k++)
  1446. {
  1447. if (tDCB.state & 1 << k)
  1448. {
  1449. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  1450. }
  1451. else
  1452. {
  1453. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  1454. };
  1455. LOG(tBuffer);
  1456. };
  1457. #endif
  1458. m_pDBC->addLocalDCB(m_nSiteID, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state);
  1459. }
  1460. else
  1461. {
  1462. LOG_ERROR("unable to open HID device");
  1463. };
  1464. pCDev = pCDev->next;
  1465. };
  1466. };
  1467. hid_free_enumeration(pDevs);
  1468. };
  1469. /* Add devices from DEV */
  1470. dFD = opendir("/dev");
  1471. if (dFD != NULL)
  1472. {
  1473. while ((pDR = readdir(dFD)) != NULL)
  1474. {
  1475. if (strcasestr(pDR->d_name, "video") == pDR->d_name)
  1476. {
  1477. createResourceVIDEO(pDR->d_name);
  1478. continue;
  1479. };
  1480. if (strcasestr(pDR->d_name, "ttyS") == pDR->d_name)
  1481. {
  1482. createResourceUART(pDR->d_name);
  1483. continue;
  1484. };
  1485. // RaspberyPI Hardware
  1486. if (strcasestr(pDR->d_name, "ttyAMA") == pDR->d_name)
  1487. {
  1488. createResourceUART(pDR->d_name);
  1489. continue;
  1490. };
  1491. if (strcasestr(pDR->d_name, "ttyUSB") == pDR->d_name)
  1492. {
  1493. createResourceUART(pDR->d_name);
  1494. continue;
  1495. };
  1496. };
  1497. closedir(dFD);
  1498. };
  1499. // m_bHardwareEnumerated = true;
  1500. m_pDBC->updateSystemInfo(&m_sSYS_INFO);
  1501. }
  1502. uint32_t hardwareManager::createResourceUART(CSTR devName)
  1503. {
  1504. uint32_t RetVal = 0;
  1505. char tBuffer[CTEXT];
  1506. /* Enumerate BuiltIn Serial */
  1507. #ifdef DEBUG
  1508. sprintf(tBuffer, "Serial Device: %s", devName);
  1509. LOG(tBuffer);
  1510. #endif
  1511. char dev_name[CTEXT];
  1512. int fd = -1;
  1513. struct serial_struct ss;
  1514. sprintf(dev_name, "/dev/%s", devName);
  1515. fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
  1516. if (-1 == fd)
  1517. {
  1518. sprintf(tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1519. LOG_ERROR(tBuffer);
  1520. }
  1521. else
  1522. {
  1523. if (0 && (-1 == xioctl(fd, TIOCGSERIAL, &ss)))
  1524. {
  1525. LOG_ERROR("TIOCGSERIAL");
  1526. }
  1527. else
  1528. {
  1529. #ifdef DEBUG
  1530. sprintf(tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]", ss.port, ss.irq, ss.baud_base, ss.line, ss.type, ss.flags);
  1531. LOG(tBuffer);
  1532. #endif
  1533. memset(tBuffer, 0, CTEXT);
  1534. getTTyDesc(devName, tBuffer);
  1535. if (tBuffer[0])
  1536. {
  1537. LOG(tBuffer);
  1538. uint32_t tID = m_pDBC->addLocalUART(/*m_nServerNodeID,*/ 1, 19200, 8, 'N', 1.0f, dev_name);
  1539. if (tID)
  1540. {
  1541. m_pDBC->addResource(/*m_nServerNodeID,*/ HARDWARE_UART, tBuffer, tID);
  1542. };
  1543. }
  1544. else
  1545. {
  1546. LOG_ERROR("Device Not Initialized at Boot Time!");
  1547. };
  1548. };
  1549. close(fd);
  1550. };
  1551. return RetVal;
  1552. }
  1553. uint32_t hardwareManager::createResourceVIDEO(CSTR devName)
  1554. {
  1555. uint32_t RetVal = 0;
  1556. char tBuffer[512];
  1557. /* Enumerate Video Capture */
  1558. #ifdef DEBUG_EXTRA
  1559. sprintf(tBuffer, "Video Source: %s", devName);
  1560. LOG(tBuffer);
  1561. #endif
  1562. struct v4l2_capability cap;
  1563. // struct v4l2_cropcap cropcap;
  1564. // struct v4l2_crop crop;
  1565. struct v4l2_format fmt;
  1566. struct v4l2_frmivalenum sprm;
  1567. char dev_name[CTEXT];
  1568. int fd = -1;
  1569. struct stat st;
  1570. sprintf(dev_name, "/dev/%s", devName);
  1571. if (-1 == stat(dev_name, &st))
  1572. {
  1573. sprintf(tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1574. LOG_ERROR(tBuffer);
  1575. return 0;
  1576. };
  1577. if (! S_ISCHR(st.st_mode))
  1578. {
  1579. sprintf(tBuffer, "%s is no devicen", dev_name);
  1580. LOG_ERROR(tBuffer);
  1581. return 0;
  1582. };
  1583. #ifdef DEBUG
  1584. LOG(dev_name);
  1585. #endif
  1586. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  1587. if (-1 == fd)
  1588. {
  1589. sprintf(tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1590. LOG_ERROR(tBuffer);
  1591. }
  1592. else
  1593. {
  1594. if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap))
  1595. {
  1596. if (EINVAL == errno)
  1597. {
  1598. sprintf(tBuffer, "%s is no V4L2 device", dev_name);
  1599. LOG_ERROR(tBuffer);
  1600. }
  1601. else
  1602. {
  1603. LOG_ERROR("VIDIOC_QUERYCAP");
  1604. };
  1605. close(fd);
  1606. return 0;
  1607. };
  1608. if (! (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
  1609. {
  1610. #ifdef DEBUG
  1611. sprintf(tBuffer, "%s is no video capture device", dev_name);
  1612. LOG_ERROR(tBuffer);
  1613. #endif
  1614. close(fd);
  1615. return 0;
  1616. };
  1617. CLEAR(fmt);
  1618. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1619. /* Preserve original settings as set by v4l2-ctl for example */
  1620. if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
  1621. {
  1622. LOG_ERROR("VIDIOC_G_FMT");
  1623. }
  1624. else
  1625. {
  1626. CLEAR(sprm);
  1627. if (-1 == xioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &sprm) && 0)
  1628. {
  1629. LOG_ERROR("VIDIOC_ENUM_FRAMEINTERVALS");
  1630. }
  1631. else
  1632. {
  1633. uint16_t fps = 15;
  1634. switch (sprm.type)
  1635. {
  1636. case V4L2_FRMIVAL_TYPE_DISCRETE : LOG("1DISCRETE"); break;
  1637. case V4L2_FRMIVAL_TYPE_CONTINUOUS: LOG("2DISCRETE"); break;
  1638. case V4L2_FRMIVAL_TYPE_STEPWISE : LOG("3DISCRETE"); break;
  1639. };
  1640. #ifdef DEBUG
  1641. sprintf(tBuffer, "Grabber{%s}{%s}[%dx%d@%d]", cap.card, cap.driver, fmt.fmt.pix.width, fmt.fmt.pix.height, fps);
  1642. LOG(tBuffer);
  1643. #endif
  1644. uint32_t mid = m_pDBC->addLocalCam((CSTR) cap.card, (CSTR) cap.driver, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height, fps);
  1645. // uint64_t mid = m_pDBC->addLocalMediaSource( m_nSiteID, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  1646. // m_pDBC->addNodeSocket( m_nServerNodeID, mid, NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  1647. RetVal = m_pDBC->addResource(HARDWARE_ANALOG_VIDEO_IN, dev_name, mid);
  1648. };
  1649. };
  1650. close(fd);
  1651. };
  1652. return RetVal;
  1653. }
  1654. uint32_t hardwareManager::createResourceUDCB(CSTR devName)
  1655. {
  1656. uint32_t RetVal = 0;
  1657. if ( devName ) RetVal = 1;
  1658. RetVal = 0;
  1659. // todo: must do something
  1660. return RetVal;
  1661. }
  1662. bool hardwareManager::assignResource(hardwareResource * ptr, bool protected_mode)
  1663. {
  1664. LOG("ASSIGN HWR");
  1665. if (ptr == NULL)
  1666. {
  1667. return false;
  1668. }
  1669. if (protected_mode)
  1670. {
  1671. m_mVectorProtector.lock();
  1672. }
  1673. LOG("LOCKED");
  1674. if (std::find(m_vResources.begin(), m_vResources.end(), ptr) == m_vResources.end())
  1675. {
  1676. m_vResources.push_back(ptr);
  1677. LOG("ADDED");
  1678. }
  1679. else
  1680. {
  1681. LOG("ALREADY ASSIGNED"); // validated twice per round otherwise
  1682. };
  1683. if (protected_mode)
  1684. {
  1685. m_mVectorProtector.unlock();
  1686. }
  1687. return true;
  1688. }
  1689. bool hardwareManager::resignResource(hardwareResource * ptr, bool protected_mode)
  1690. {
  1691. LOG("RESIGN HWR");
  1692. if (ptr == NULL)
  1693. {
  1694. return false;
  1695. }
  1696. if (protected_mode)
  1697. {
  1698. m_mVectorProtector.lock();
  1699. }
  1700. LOG("LOCKED");
  1701. if (m_vResources.size())
  1702. {
  1703. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  1704. {
  1705. if ((*idx) == ptr)
  1706. {
  1707. LOG("FOUND");
  1708. m_vResources.erase(idx);
  1709. LOG("ERASED");
  1710. break;
  1711. };
  1712. };
  1713. }
  1714. else
  1715. {
  1716. LOG("NO RES!");
  1717. };
  1718. LOG("UNLOCKED");
  1719. if (protected_mode)
  1720. {
  1721. m_mVectorProtector.unlock();
  1722. }
  1723. return true;
  1724. }
  1725. hardwareResource * hardwareManager::requestResource(HARDWARE_PROVIDERS type, uint32_t id)
  1726. {
  1727. hardwareResource * RetVal = NULL;
  1728. m_mVectorProtector.lock();
  1729. if (m_vResources.size())
  1730. {
  1731. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  1732. {
  1733. #ifdef DEBUG_EXTRA
  1734. char tB[256];
  1735. sprintf(tB, "Check Resources RR[%d] RI[%d] ?? CR[%d] CI[%d]", type, id, (*idx)->getProviderType(), (*idx)->getDbId());
  1736. LOG(tB);
  1737. #endif
  1738. if (((*idx)->getProviderType() == type) && ((*idx)->getDbId() == id))
  1739. {
  1740. if ( (*idx)->activate() ) {
  1741. RetVal = (*idx);
  1742. };
  1743. break;
  1744. };
  1745. };
  1746. }
  1747. else
  1748. {
  1749. LOG("No Resources!!");
  1750. };
  1751. m_mVectorProtector.unlock();
  1752. return RetVal;
  1753. }
  1754. DBC_INT_LIST * hardwareManager::requestResourcesByType(HARDWARE_PROVIDERS type)
  1755. {
  1756. DBC_INT_LIST * RetVal = NULL;
  1757. #ifdef DEBUG
  1758. char tBuffer[128];
  1759. sprintf(tBuffer, "Requested Resource type: %d", type);
  1760. LOG(tBuffer);
  1761. #endif
  1762. m_mVectorProtector.lock();
  1763. if (m_vResources.size())
  1764. {
  1765. #ifdef DEBUG
  1766. LOG("Have Some Resources!");
  1767. #endif
  1768. RetVal = new DBC_INT_LIST();
  1769. if (RetVal)
  1770. {
  1771. #ifdef DEBUG
  1772. LOG("RetVal Created!");
  1773. #endif
  1774. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  1775. {
  1776. #ifdef DEBUG
  1777. sprintf(tBuffer, "Have Resource type: %d", (*idx)->getHardwareType());
  1778. LOG(tBuffer);
  1779. #endif
  1780. if ((*idx)->getProviderType() == type)
  1781. {
  1782. #ifdef DEBUG
  1783. LOG("Matched!");
  1784. #endif
  1785. RetVal->push_back((*idx)->getDbId());
  1786. };
  1787. };
  1788. }
  1789. else
  1790. {
  1791. LOG_ERROR("Cannot Create DBC_INT_LIST");
  1792. };
  1793. };
  1794. m_mVectorProtector.unlock();
  1795. return RetVal;
  1796. }
  1797. #include <signal.h>
  1798. #include <sys/types.h>
  1799. #ifdef USE_LMSENSORS
  1800. #include <sensors/sensors.h>
  1801. #include <sensors/error.h>
  1802. #endif
  1803. bool hardwareManager::updateSysInfo(DBC_SYS_LOAD_INFO * ptr)
  1804. {
  1805. if (ptr == NULL)
  1806. {
  1807. return false;
  1808. }
  1809. struct sysinfo SI;
  1810. struct statvfs DI;
  1811. ptr->CPU_CORES = get_nprocs();
  1812. if (sysinfo(&SI) == 0)
  1813. {
  1814. float f_load = 100.f / (float) (ptr->CPU_CORES * (1 << SI_LOAD_SHIFT));
  1815. ptr->cpu1 = SI.loads[0] * f_load;
  1816. ptr->cpu5 = SI.loads[1] * f_load;
  1817. ptr->cpu15 = SI.loads[2] * f_load;
  1818. ptr->T_MEM = SI.totalram * SI.mem_unit;
  1819. // "freeram" is obvious, and "bufferram" is memory temporarily used by the kernel to cache some files
  1820. ptr->C_MEM = (SI.totalram - SI.freeram - SI.bufferram) * SI.mem_unit;
  1821. ptr->mem = 100.0f * (float) ptr->C_MEM / (float) ptr->T_MEM;
  1822. #ifdef DEBUG_EXTRA
  1823. char tB[128];
  1824. sprintf(tB, "Total Mem: %" PRIu64 " Free: %" PRIu64 " Perc: %.2f", SI.totalram, SI.freeram, ptr->mem);
  1825. LOG(tB);
  1826. #endif
  1827. #ifdef MEMORY_CONSUMPTION_LIMIT
  1828. if (ptr->mem > 80.0f)
  1829. {
  1830. LOG_ERROR("Try to free some caches!");
  1831. /*
  1832. To free pagecache:
  1833. # echo 1 > /proc/sys/vm/drop_caches
  1834. To free dentries and inodes:
  1835. # echo 2 > /proc/sys/vm/drop_caches
  1836. To free pagecache, dentries and inodes:
  1837. # echo 3 > /proc/sys/vm/drop_caches
  1838. */
  1839. // system( "echo 1 > /proc/sys/vm/drop_caches" );
  1840. if (m_bCacheCleanCounter)
  1841. {
  1842. m_bCacheCleanCounter--;
  1843. }
  1844. else
  1845. {
  1846. // KillSellf
  1847. // kill( getpid(), 9 );
  1848. };
  1849. };
  1850. #endif
  1851. }
  1852. else
  1853. {
  1854. LOG_ERROR("Error while getting System Info!");
  1855. };
  1856. // const uint32_t MB = 1 << 10;//(1024 * 1024);// * 1024;
  1857. if( statvfs("/", &DI) == 0 ){
  1858. ptr->T_DISK = (DI.f_blocks * (unsigned long long) DI.f_frsize) >> 10; // / MB;
  1859. // cDISK = ( DI.f_bavail * (unsigned long long) DI.f_bsize ) >> 10;// / MB;
  1860. ptr->C_DISK = ptr->T_DISK - ((DI.f_bfree * (unsigned long long) DI.f_bsize) >> 10);
  1861. ptr->disk = 100.0f * (float) ptr->C_DISK / (float) ptr->T_DISK;
  1862. } else {
  1863. LOG_ERROR("Error while getting Disk Info!");
  1864. };
  1865. // Temperature reading
  1866. ptr->temperature = -1;
  1867. if (fileExist("/sys/class/thermal")){
  1868. char tB[128];
  1869. // Check first 8 zones to find CPU one
  1870. for (uint8_t i = 0; i < 8; i++){
  1871. sprintf(tB, "/sys/class/thermal/thermal_zone%d", i);
  1872. if (fileExist(tB)){
  1873. sprintf(tB, "/sys/class/thermal/thermal_zone%d/type", i);
  1874. if (fileExist(tB)){
  1875. FILE * fd = fopen(tB, "r");
  1876. readLine(fd, tB, 128);
  1877. fclose(fd);
  1878. if (strcasestr(tB, "cpu") != NULL || strcasestr(tB, "x86_pkg") != NULL || strcasestr(tB, "acpitz") != NULL){
  1879. sprintf(tB, "/sys/class/thermal/thermal_zone%d/temp", i);
  1880. FILE * fd = fopen(tB, "r");
  1881. readLine(fd, tB, 128);
  1882. fclose(fd);
  1883. ptr->temperature = ((float) atoll(tB)) / 1000.0f;
  1884. break;
  1885. };
  1886. } else {
  1887. continue;
  1888. };
  1889. } else {
  1890. break;
  1891. };
  1892. };
  1893. };
  1894. #ifdef USE_LMSENSORS
  1895. if ( ptr->temperature < 0 ) {
  1896. //still have problem to read temp
  1897. // last chance -> use lm-sensors
  1898. if( sensors_init(nullptr) == 0 ) {
  1899. int chip_nr = 0;
  1900. const sensors_chip_name* chip;
  1901. // 2. Iterate through all detected sensor chips
  1902. while ((chip = sensors_get_detected_chips(nullptr, &chip_nr)) != nullptr) {
  1903. //char chip_name_buf[256];
  1904. //sensors_snprintf_chip_name(chip_name_buf, sizeof(chip_name_buf), chip);
  1905. //LOG( chip_name_buf );
  1906. //LOG( chip->prefix );
  1907. int feat_nr = 0;
  1908. const sensors_feature* feature;
  1909. // 3. Iterate through all features of the current chip
  1910. while ((feature = sensors_get_features(chip, &feat_nr)) != nullptr) {
  1911. // Filter strictly for Temperature Features
  1912. if (feature->type == SENSORS_FEATURE_TEMP) {
  1913. // Get the user-friendly label for this sensor (e.g., "Core 0", "CPU Temperature")
  1914. //char* label = sensors_get_label(chip, feature);
  1915. //LOG((label ? label : "Unknown"));
  1916. //if( label ) free( label ); // Must free the label returned by libsensors
  1917. // 4. Find the main subfeature containing the actual reading (input value)
  1918. const sensors_subfeature* subfeature = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
  1919. if (subfeature != nullptr) {
  1920. double val = 0.0;
  1921. // 5. Inquiry the actual temperature reading
  1922. if ( sensors_get_value(chip, subfeature->number, &val) == 0 ) {
  1923. //char tB[128];
  1924. //sprintf( tB, "T: %0.3f �C", val );
  1925. //LOG( tB );
  1926. if ( val > 0.0 ) {
  1927. ptr->temperature = ( float ) val;
  1928. break;
  1929. };
  1930. } else {
  1931. LOG_ERROR("[Error reading value]");
  1932. };
  1933. } else {
  1934. LOG_ERROR("[No input channel found]");
  1935. };
  1936. };
  1937. };
  1938. if ( ptr->temperature > 0.0f ) {
  1939. break;
  1940. };
  1941. };
  1942. // 6. Clean up resources allocated by the library
  1943. sensors_cleanup();
  1944. };
  1945. };
  1946. #endif //USE_LMSENSORS
  1947. return m_pDBC->updateServerUtilisation(ptr);
  1948. }
  1949. void hardwareManager::clearAllResources(void) {}
  1950. #undef CLEAR
  1951. #undef LOG
  1952. #undef LOG_ERROR