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.

2019 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
1 day 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
1 day 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)->validate())
  434. {
  435. // recreate Resource
  436. LOG_ERROR("HW Resource Invalid!");
  437. m_pDBC->setRessourceActive((*idx)->getDbId(), false);
  438. #ifndef DEBUG
  439. char tB[256];
  440. sprintf(tB, "Info: DBID[%d] R-Type: %d Provider: %d ", (*idx)->getDbId(), (*idx)->getHardwareType(), (*idx)->getProviderType());
  441. LOG(tB);
  442. #endif
  443. if (! (*idx)->create())
  444. {
  445. LOG_ERROR("HW Resource Creation Failure!");
  446. }
  447. else
  448. {
  449. m_pDBC->setRessourceActive((*idx)->getDbId());
  450. #ifndef DEBUG
  451. LOG("Re-Created OK");
  452. #endif
  453. };
  454. #ifdef DEBUG_EXTRA
  455. }
  456. else
  457. {
  458. LOG("Resource OK!");
  459. #endif
  460. };
  461. };
  462. #ifdef DEBUG_EXTRA
  463. }
  464. else
  465. {
  466. LOG("No Resources!");
  467. #endif
  468. };
  469. m_mVectorProtector.unlock();
  470. // Mem Validator
  471. #ifdef MEMORY_CONSUMPTION_LIMIT
  472. if (m_nMemoryLimitHeapSize)
  473. {
  474. size_t memStat = getCurrentRSS();
  475. #ifdef DEBUG_EXTRA
  476. char MEM_MSG[128];
  477. sprintf(MEM_MSG, "MEM: %" PRIu64 " TARG: %" PRIu64, memStat, m_nMemoryLimitHeapSize);
  478. LOG(MEM_MSG);
  479. #endif
  480. // https://www.ebay.com/itm/274974088159?
  481. RetVal = (memStat < m_nMemoryLimitHeapSize);
  482. if (! RetVal)
  483. {
  484. LOG_ERROR("Memory Consumption too BIG!");
  485. };
  486. };
  487. if (m_bCacheCleanCounter == 0)
  488. {
  489. LOG_ERROR("System Cache Fas Too Big Too Many Times!");
  490. RetVal = false;
  491. };
  492. #endif
  493. return RetVal;
  494. }
  495. void hardwareManager::getMAC(CSTR if_name, CSTR buf)
  496. {
  497. char tB[128];
  498. FILE * fd = NULL;
  499. if (buf)
  500. {
  501. sprintf(tB, "/sys/class/net/%s/address", if_name);
  502. #ifdef DEBUG_EXTRA
  503. LOG(tB);
  504. #endif
  505. fd = fopen(tB, "r");
  506. if (fd)
  507. {
  508. fread(buf, 1, INET6_ADDRSTRLEN, fd);
  509. fclose(fd);
  510. buf = trim(buf);
  511. }
  512. else
  513. {
  514. memset(buf, 0, INET6_ADDRSTRLEN);
  515. };
  516. #ifdef DEBUG_EXTRA
  517. LOG(buf);
  518. #endif
  519. };
  520. }
  521. void hardwareManager::getGW(CSTR if_name, CSTR buf)
  522. {
  523. memset(buf, 0, INET6_ADDRSTRLEN);
  524. #ifndef NO_EXEC_EXTERNALS
  525. FILE * fd = popen("route -n", "r");
  526. char tB[1024];
  527. // char expBuf[1024];
  528. if (fd)
  529. {
  530. while (! feof(fd))
  531. {
  532. fgets((CSTR) tB, 1024, fd);
  533. trim((CSTR) tB);
  534. if (strlen(tB))
  535. {
  536. if (strstr(tB, if_name) != NULL)
  537. {
  538. // strncpy( expBuf, tB, 1024 );
  539. CArray * CA = explode(' ', tB);
  540. if (CA)
  541. {
  542. // for (int i=0; i< CA->size(); i++ ) {
  543. // char tB[128];
  544. // trim( CA->at(i) );
  545. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  546. // LOG( tB );
  547. // };
  548. trim(CA->at(1));
  549. if (strstr(CA->at(1), "0.0.0.0") == NULL)
  550. {
  551. strncpy(buf, CA->at(1), INET6_ADDRSTRLEN);
  552. };
  553. delete(CA);
  554. };
  555. };
  556. };
  557. };
  558. fclose(fd);
  559. };
  560. #endif
  561. if (strlen(buf) == 0)
  562. {
  563. strncpy(buf, "0.0.0.0", INET6_ADDRSTRLEN);
  564. }
  565. }
  566. void hardwareManager::getTTyDesc(CSTR dev_name, CSTR buf)
  567. {
  568. memset(buf, 0, DBC_RESOURCE_DESC_SIZE);
  569. #ifndef NO_EXEC_EXTERNALS
  570. FILE * fd = popen("dmesg | grep tty", "r");
  571. char tB[1024];
  572. // char expBuf[1024];
  573. if (fd)
  574. {
  575. while (! feof(fd))
  576. {
  577. fgets((CSTR) tB, 1024, fd);
  578. trim((CSTR) tB);
  579. if (strlen(tB))
  580. {
  581. if (strstr(tB, dev_name) != NULL)
  582. {
  583. CArray * CA = explode(':', tB);
  584. if (CA)
  585. {
  586. if (CA->size())
  587. {
  588. trim(CA->at(CA->size() - 1));
  589. strncpy(buf, CA->at(CA->size() - 1), CTEXT);
  590. buf[CTEXT - 1] = (char) 0;
  591. };
  592. delete(CA);
  593. };
  594. };
  595. };
  596. };
  597. fclose(fd);
  598. };
  599. #endif
  600. if (strlen(buf) == 0)
  601. {
  602. strncpy(buf, "--Unknown--", DBC_RESOURCE_DESC_SIZE);
  603. buf[DBC_RESOURCE_DESC_SIZE - 1] = (char) 0;
  604. };
  605. }
  606. PARSE_RESULT_NET_INTERFACES * hardwareManager::parse_neconfig(CSTR if_name)
  607. {
  608. PARSE_RESULT_NET_INTERFACES * tVal = NULL;
  609. FILE * fd;
  610. bool cConfig = false;
  611. fd = fopen("/etc/network/interfaces", "r");
  612. if (fd)
  613. {
  614. char tB[1024];
  615. char expBuf[1024];
  616. tVal = (PARSE_RESULT_NET_INTERFACES *) malloc(sizeof(PARSE_RESULT_NET_INTERFACES));
  617. if (tVal)
  618. {
  619. memset(tVal, 0, sizeof(PARSE_RESULT_NET_INTERFACES));
  620. while (! feof(fd))
  621. {
  622. fgets((CSTR) tB, 1024, fd);
  623. trim((CSTR) tB);
  624. if (strlen(tB))
  625. {
  626. strncpy(expBuf, tB, 1024);
  627. CArray * CA = explode(' ', expBuf);
  628. if (CA)
  629. {
  630. for (CArray::iterator idx = CA->begin(); idx < CA->end(); idx++)
  631. {
  632. if ((*idx)[0] == '#')
  633. {
  634. break;
  635. }
  636. if (strstr(*idx, "auto") != NULL)
  637. {
  638. if (strstr(tB, if_name) != NULL)
  639. {
  640. tVal->enabled = true;
  641. }
  642. }
  643. if (strstr(*idx, "allow-hotplug") != NULL)
  644. {
  645. if (strstr(tB, if_name) != NULL)
  646. {
  647. tVal->hot_plug = true;
  648. }
  649. }
  650. if (strstr(*idx, "iface") != NULL)
  651. {
  652. if (strstr(tB, if_name) != NULL)
  653. {
  654. if (strstr(tB, "static") != NULL)
  655. {
  656. tVal->if_static = true;
  657. }
  658. cConfig = true;
  659. }
  660. else
  661. {
  662. cConfig = false;
  663. }
  664. }
  665. if (strstr(*idx, "address") != NULL)
  666. {
  667. if (cConfig)
  668. {
  669. idx++;
  670. if (strstr(*idx, "/") != NULL)
  671. {
  672. int8_t sh = strstr(*idx, "/") - (*idx);
  673. strncpy(tVal->address, *idx, sh);
  674. tVal->address[sh] = (char) 0;
  675. int8_t bm = atoi((*idx) + sh + 1);
  676. union
  677. {
  678. uint32_t mm;
  679. uint8_t nm[4];
  680. } nmu = {0};
  681. for (sh = 0; sh < bm; sh++)
  682. {
  683. nmu.mm |= (1 << sh);
  684. }
  685. sprintf(tVal->netmask, "%d.%d.%d.%d", nmu.nm[0], nmu.nm[1], nmu.nm[2], nmu.nm[3]);
  686. }
  687. else
  688. {
  689. strncpy(tVal->address, *idx, INET6_ADDRSTRLEN);
  690. };
  691. };
  692. }
  693. if (strstr(*idx, "netmask") != NULL)
  694. {
  695. if (cConfig)
  696. {
  697. LOG("netMSK");
  698. idx++;
  699. LOG(*idx);
  700. strncpy(tVal->netmask, *idx, INET6_ADDRSTRLEN);
  701. };
  702. }
  703. // if(strstr((*idx), "bridge_ports")!= NULL ) cMode = 6;
  704. // if(strstr((*idx), "bridge_stp")!= NULL ) cMode = 7;
  705. // if(strstr((*idx), "bridge_waitport")!= NULL ) cMode = 8;
  706. // if(strstr((*idx), "bridge_fd")!= NULL ) cMode = 9;
  707. };
  708. delete(CA);
  709. };
  710. };
  711. };
  712. };
  713. fclose(fd);
  714. };
  715. #ifdef DEBUG_EXTRA
  716. if (tVal)
  717. {
  718. if (tVal->enabled)
  719. {
  720. LOG("NIF Enabled");
  721. }
  722. else
  723. {
  724. LOG("NIF Disabled");
  725. }
  726. if (tVal->hot_plug)
  727. {
  728. LOG("NIF Hot Plugable");
  729. }
  730. else
  731. {
  732. LOG("NIF not Hot Plugable");
  733. }
  734. if (tVal->if_static)
  735. {
  736. LOG("Address is static");
  737. }
  738. else
  739. {
  740. LOG("Address is Dynamic");
  741. }
  742. if (strlen(tVal->address))
  743. {
  744. LOG(tVal->address);
  745. }
  746. if (strlen(tVal->netmask))
  747. {
  748. LOG(tVal->netmask);
  749. }
  750. };
  751. #endif
  752. return tVal;
  753. }
  754. PARSE_RESULT_WIFI_SETTINGS * hardwareManager::parse_wifi(CSTR if_name)
  755. {
  756. PARSE_RESULT_WIFI_SETTINGS * tVal = NULL;
  757. FILE * fd;
  758. bool cConfig = false;
  759. bool is_wifi = false;
  760. tVal = (PARSE_RESULT_WIFI_SETTINGS *) malloc(sizeof(PARSE_RESULT_WIFI_SETTINGS));
  761. if (tVal)
  762. {
  763. memset(tVal, 0, sizeof(PARSE_RESULT_WIFI_SETTINGS));
  764. fd = fopen("/etc/dnsmasq.conf", "r");
  765. if (fd)
  766. {
  767. char tB[1024];
  768. char expBuf[1024];
  769. while (! feof(fd))
  770. {
  771. fgets((CSTR) tB, 1024, fd);
  772. trim((CSTR) tB);
  773. if (strlen(tB))
  774. {
  775. strncpy(expBuf, tB, 1024);
  776. CArray * CA = explode('=', expBuf);
  777. if (CA)
  778. {
  779. if (CA->size())
  780. {
  781. if (CA->at(0)[0] == '#')
  782. {
  783. continue;
  784. }
  785. if (strstr(CA->at(0), "interface") != NULL)
  786. {
  787. if (strstr(tB, if_name) != NULL)
  788. {
  789. cConfig = true;
  790. tVal->dhcp_enabled = true;
  791. };
  792. };
  793. if (cConfig)
  794. {
  795. if (strstr(CA->at(0), "dhcp-range") != NULL)
  796. {
  797. char tT[1024];
  798. strncpy(tT, CA->at(1), 1024);
  799. CArray * CB = explode(',', tT);
  800. if (CB)
  801. {
  802. if (CB->size() > 3)
  803. {
  804. strncpy(tVal->range_start, CB->at(0), INET6_ADDRSTRLEN);
  805. strncpy(tVal->range_stop, CB->at(1), INET6_ADDRSTRLEN);
  806. strncpy(tVal->netmask, CB->at(2), INET6_ADDRSTRLEN);
  807. strncpy(tVal->lease_period, CB->at(3), INET6_ADDRSTRLEN);
  808. };
  809. delete(CB);
  810. };
  811. };
  812. };
  813. };
  814. delete(CA);
  815. };
  816. };
  817. };
  818. fclose(fd);
  819. };
  820. #ifndef NO_EXEC_EXTERNALS
  821. // Check WiFi
  822. if (strstr(if_name, "w") != NULL)
  823. {
  824. char tName[128];
  825. is_wifi = true;
  826. sprintf(tName, "iwconfig %s", if_name);
  827. fd = popen(tName, "r");
  828. if (fd)
  829. {
  830. char tB[1024];
  831. while (! feof(fd))
  832. {
  833. fgets((CSTR) tB, 1024, fd);
  834. trim(tB);
  835. if (strstr(tB, "Mode") != NULL)
  836. {
  837. if (strstr(tB, "Master") != NULL)
  838. {
  839. tVal->access_point = true;
  840. }
  841. }
  842. };
  843. fclose(fd);
  844. };
  845. };
  846. #endif
  847. if (tVal->access_point)
  848. {
  849. fd = fopen("/etc/hostapd/hostapd.conf", "r");
  850. if (fd)
  851. {
  852. char tB[1024];
  853. char expBuf[1024];
  854. while (! feof(fd))
  855. {
  856. fgets((CSTR) tB, 1024, fd);
  857. trim(tB);
  858. if (strlen(tB))
  859. {
  860. if (tB[0] == '#')
  861. {
  862. continue;
  863. }
  864. strncpy(expBuf, tB, 1024);
  865. CArray * CA = explode('=', expBuf);
  866. if (CA)
  867. {
  868. if (CA->size())
  869. {
  870. if (strstr(CA->at(0), "ssid") != NULL)
  871. {
  872. if (strstr(CA->at(0), "ignore_broadcast_ssid") == NULL)
  873. {
  874. strncpy(tVal->SSID, CA->at(1), 64);
  875. }
  876. else
  877. {
  878. tVal->hiden_ssid = (bool) atoi(CA->at(1));
  879. }
  880. }
  881. if (strstr(CA->at(0), "wpa_passphrase") != NULL)
  882. {
  883. strncpy(tVal->password, CA->at(1), 64);
  884. }
  885. if (strstr(CA->at(0), "country_code") != NULL)
  886. {
  887. strncpy(tVal->country_code, CA->at(1), 3);
  888. }
  889. if (strstr(CA->at(0), "channel") != NULL)
  890. {
  891. tVal->channel = atoi(CA->at(1));
  892. }
  893. };
  894. delete(CA);
  895. };
  896. };
  897. };
  898. fclose(fd);
  899. };
  900. }
  901. else
  902. {
  903. fd = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r");
  904. if (fd)
  905. {
  906. fclose(fd);
  907. };
  908. };
  909. fd = fopen("/etc/rc.local", "r");
  910. if (fd)
  911. {
  912. char tB[1024];
  913. char expBuf[1024];
  914. while (! feof(fd))
  915. {
  916. fgets((CSTR) tB, 1024, fd);
  917. trim((CSTR) tB);
  918. if (strlen(tB))
  919. {
  920. strncpy(expBuf, tB, 1024);
  921. CArray * CA = explode(' ', expBuf);
  922. if (CA)
  923. {
  924. if (CA->size())
  925. {
  926. if (CA->at(0)[0] == '#')
  927. {
  928. continue;
  929. }
  930. if (strstr(CA->at(0), "iptables-restore") != NULL)
  931. {
  932. char zB[1024];
  933. FILE * afd = fopen(CA->at(2), "r");
  934. if (afd)
  935. {
  936. while (! feof(afd))
  937. {
  938. fgets((CSTR) zB, 1024, afd);
  939. if (strstr(zB, "POSTROUTING") != NULL)
  940. {
  941. if (strstr(zB, "MASQUERADE") != NULL)
  942. {
  943. if (strstr(zB, if_name) == NULL)
  944. {
  945. CArray * CB = explode(' ', zB);
  946. if (CB)
  947. {
  948. if (CB->size() > 3)
  949. {
  950. strncpy(tVal->router_outbond, CB->at(3), INET6_ADDRSTRLEN);
  951. };
  952. delete(CB);
  953. };
  954. tVal->router_enabled = true;
  955. };
  956. }
  957. }
  958. };
  959. fclose(afd);
  960. }
  961. else
  962. {
  963. LOG("Error Opening Firewall Rules!");
  964. }
  965. };
  966. };
  967. delete(CA);
  968. };
  969. };
  970. };
  971. fclose(fd);
  972. }
  973. else
  974. {
  975. LOG_ERROR("Can not open rc.local!");
  976. }
  977. #ifdef DEBUG
  978. char zz[512];
  979. if (is_wifi)
  980. {
  981. if (tVal->access_point)
  982. {
  983. LOG("WiFi is in AP Mode");
  984. sprintf(zz, "\tSSID:%s PWD:%s", tVal->SSID, tVal->password);
  985. LOG(zz);
  986. if (tVal->hiden_ssid)
  987. {
  988. LOG("SSID is hidden");
  989. }
  990. }
  991. else
  992. {
  993. LOG("WiFi is in Client Mode");
  994. }
  995. };
  996. if (tVal->dhcp_enabled)
  997. {
  998. LOG("DHCP Service - Enabled");
  999. sprintf(zz, "\tRange: %s - %s Lease Time: %s", tVal->range_start, tVal->range_stop, tVal->lease_period);
  1000. LOG(zz);
  1001. }
  1002. else
  1003. {
  1004. LOG("DHCP Service - Disabled");
  1005. };
  1006. if (tVal->router_enabled)
  1007. {
  1008. sprintf(zz, "Routing enabled to [%s] NIF", tVal->router_outbond);
  1009. LOG(zz);
  1010. }
  1011. else
  1012. {
  1013. LOG("Routing Disabled");
  1014. };
  1015. #endif
  1016. };
  1017. return tVal;
  1018. }
  1019. void hardwareManager::enumerateNIFS(void)
  1020. {
  1021. struct ifaddrs * ifaddr = NULL;
  1022. char tBuffer[128];
  1023. // if( m_pKernel == NULL ) return;
  1024. if (m_pDBC == NULL)
  1025. {
  1026. return;
  1027. }
  1028. if (-1 == getifaddrs(&ifaddr))
  1029. {
  1030. LOG_ERROR("No NET INTERFACES?!");
  1031. }
  1032. else
  1033. {
  1034. #ifdef DEBUG
  1035. LOG("Enumerating Network Interfaces");
  1036. #endif
  1037. struct ifaddrs * ifa;
  1038. for (ifa = ifaddr; ifa; ifa = ifa->ifa_next)
  1039. {
  1040. char addr[INET6_ADDRSTRLEN];
  1041. char mask[INET6_ADDRSTRLEN];
  1042. char gw[INET6_ADDRSTRLEN];
  1043. char bc[INET6_ADDRSTRLEN];
  1044. char mac[INET6_ADDRSTRLEN];
  1045. char device[INET6_ADDRSTRLEN];
  1046. uint32_t nif_id = 0;
  1047. uint32_t nif_aux_id = 0;
  1048. uint32_t router_id = 0;
  1049. NIF_TYPE nif_type = NIF_LOOPBACK;
  1050. NETWORK_TYPE net_type = NETWORK_IPV4;
  1051. RESOURCE_TYPE res_type = HARDWARE_NIF_LOOPBACK;
  1052. NIF_MODE nif_mode = ADDRESS_DISABLED;
  1053. #ifdef DEBUG_EXTRA
  1054. LOG("*** Network Interface ***");
  1055. #endif
  1056. strncpy(device, ifa->ifa_name, INET6_ADDRSTRLEN);
  1057. strncpy(mac, "--no-mac--", INET6_ADDRSTRLEN);
  1058. #ifdef DEBUG_EXTRA
  1059. LOG(device);
  1060. #endif
  1061. // Filter Loopback if
  1062. if (strstr(device, "lo") != NULL)
  1063. {
  1064. nif_type = NIF_LOOPBACK;
  1065. res_type = HARDWARE_NIF_LOOPBACK;
  1066. // Not Implemented
  1067. #ifdef DEBUG_EXTRA
  1068. LOG("Loopback Device -> SKIP");
  1069. #endif
  1070. continue;
  1071. };
  1072. // Filter vpn TUN if
  1073. if (strstr(device, "tun") != NULL)
  1074. {
  1075. nif_type = NIF_VPN_TUNEL;
  1076. res_type = HARDWARE_NIF_TUNEL;
  1077. // Not Implemented Yet
  1078. #ifdef DEBUG_EXTRA
  1079. LOG("Tunel Device -> SKIP");
  1080. #endif
  1081. continue;
  1082. };
  1083. if (strstr(device, "e") != NULL)
  1084. {
  1085. nif_type = NIF_ETHERNET;
  1086. res_type = HARDWARE_NIF_ETHERNET;
  1087. };
  1088. if (strstr(device, "wl") != NULL)
  1089. {
  1090. nif_type = NIF_WIFI;
  1091. res_type = HARDWARE_NIF_WIFI;
  1092. };
  1093. if (strstr(device, "ww") != NULL)
  1094. {
  1095. nif_type = NIF_WAN;
  1096. res_type = HARDWARE_NIF_WAN;
  1097. };
  1098. if (ifa->ifa_addr != NULL)
  1099. {
  1100. if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6)
  1101. {
  1102. #ifdef DEBUG_EXTRA
  1103. LOG("No INET ADDRESSES -> SKIP");
  1104. #endif
  1105. continue;
  1106. }
  1107. }
  1108. else
  1109. {
  1110. #ifdef DEBUG_EXTRA
  1111. LOG("ifa->ifa_addr is NULL -> SKIP");
  1112. #endif
  1113. continue;
  1114. };
  1115. #ifdef DEBUG_EXTRA
  1116. LOG("Getting Info....");
  1117. #endif
  1118. getMAC(device, mac);
  1119. getGW(device, gw);
  1120. #ifdef DEBUG_EXTRA
  1121. LOG(mac);
  1122. LOG(gw);
  1123. #endif
  1124. PARSE_RESULT_NET_INTERFACES * tt = NULL;
  1125. PARSE_RESULT_WIFI_SETTINGS * tw = NULL;
  1126. #ifdef DEBUG_EXTRA
  1127. LOG("Parse NetConfig Info....");
  1128. #endif
  1129. tt = parse_neconfig(device);
  1130. if (tt)
  1131. {
  1132. if (tt->enabled)
  1133. {
  1134. if (tt->if_static)
  1135. {
  1136. nif_mode = ADDRESS_STATIC;
  1137. }
  1138. else
  1139. {
  1140. nif_mode = ADDRESS_DYNAMIC;
  1141. };
  1142. }
  1143. else
  1144. {
  1145. nif_mode = ADDRESS_DISABLED;
  1146. };
  1147. }
  1148. else
  1149. {
  1150. LOG_ERROR("Error Parsing System NIF Settings!");
  1151. continue;
  1152. };
  1153. #ifdef DEBUG_EXTRA
  1154. LOG("Parse Routing/DHCP/AP Info....");
  1155. #endif
  1156. tw = parse_wifi(device);
  1157. if (tw)
  1158. {
  1159. #ifdef DEBUG_EXTRA
  1160. LOG("Routing/AP Info OK!");
  1161. #endif
  1162. if (nif_mode == ADDRESS_STATIC)
  1163. {
  1164. if (tw->dhcp_enabled)
  1165. {
  1166. #ifdef DEBUG_EXTRA
  1167. LOG("Address is Router");
  1168. #endif
  1169. nif_mode = ADDRESS_STATIC_RAP;
  1170. };
  1171. };
  1172. nif_id = m_pDBC->addLocalNIF(device, mac, nif_type, nif_mode);
  1173. #ifdef DEBUG_EXTRA
  1174. sprintf(tBuffer, "NIF(%s) added as [%d]", device, nif_id);
  1175. LOG(tBuffer);
  1176. #endif
  1177. if (nif_type == NIF_WIFI)
  1178. {
  1179. if (nif_id == 0)
  1180. {
  1181. LOG_ERROR("Error Adding NIF in DB!");
  1182. continue;
  1183. };
  1184. WIFI_MODE w_mode;
  1185. if (! tt->enabled)
  1186. {
  1187. w_mode = WIFI_DISABLED;
  1188. }
  1189. else
  1190. {
  1191. if (tt->if_static)
  1192. {
  1193. if (tw->access_point)
  1194. {
  1195. w_mode = WIFI_IS_AP;
  1196. uint32_t outbond_id = 0;
  1197. if (tw->router_enabled)
  1198. {
  1199. outbond_id = m_pDBC->getNIF_IDbyDevice(tw->router_outbond);
  1200. };
  1201. if (tw->dhcp_enabled)
  1202. {
  1203. #ifdef DEBUG_EXTRA
  1204. LOG("ADD Router With DHCP");
  1205. #endif
  1206. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period);
  1207. }
  1208. else
  1209. {
  1210. #ifdef DEBUG_EXTRA
  1211. LOG("ADD Router ONLY");
  1212. #endif
  1213. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id);
  1214. };
  1215. #ifdef DEBUG_EXTRA
  1216. sprintf(tBuffer, "Router added as [%d]", router_id);
  1217. LOG(tBuffer);
  1218. #endif
  1219. }
  1220. else
  1221. {
  1222. w_mode = WIFI_CONNECTED_TO_AP;
  1223. };
  1224. };
  1225. };
  1226. #ifdef DEBUG_EXTRA
  1227. LOG("ADD WiFi Record");
  1228. #endif
  1229. nif_aux_id = m_pDBC->addLocalWiFi(nif_id, w_mode, tw->SSID, tw->password, ENCRIPTION_WPA_PSK, router_id);
  1230. };
  1231. if (nif_type == NIF_ETHERNET || nif_type == NIF_ETHERNET_VIRTUAL)
  1232. {
  1233. if (tt->if_static)
  1234. {
  1235. uint32_t outbond_id = 0;
  1236. nif_mode = ADDRESS_STATIC;
  1237. if (tw->router_enabled)
  1238. {
  1239. nif_mode = ADDRESS_STATIC_RAP;
  1240. outbond_id = m_pDBC->getNIF_IDbyDevice(tw->router_outbond);
  1241. };
  1242. if (tw->dhcp_enabled)
  1243. {
  1244. #ifdef DEBUG_EXTRA
  1245. LOG("ADD Router With DHCP");
  1246. #endif
  1247. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period);
  1248. }
  1249. else
  1250. {
  1251. #ifdef DEBUG_EXTRA
  1252. LOG("ADD Router ONLY");
  1253. #endif
  1254. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id);
  1255. };
  1256. #ifdef DEBUG_EXTRA
  1257. sprintf(tBuffer, "Router added as [%d]", router_id);
  1258. LOG(tBuffer);
  1259. #endif
  1260. }
  1261. else
  1262. {
  1263. nif_mode = ADDRESS_DYNAMIC;
  1264. };
  1265. };
  1266. free(tt);
  1267. free(tw);
  1268. }
  1269. else
  1270. {
  1271. LOG_ERROR("Error Parsing System Router/AP Settings!");
  1272. free(tt);
  1273. continue;
  1274. };
  1275. if (nif_aux_id != 0)
  1276. {
  1277. nif_id = m_pDBC->addLocalNIF(device, mac, nif_type, nif_mode, nif_aux_id);
  1278. #ifdef DEBUG_EXTRA
  1279. sprintf(tBuffer, "2 NIF(%s) Updated as [%d]", device, nif_id);
  1280. LOG(tBuffer);
  1281. #endif
  1282. if (nif_id == 0)
  1283. {
  1284. LOG_ERROR("Error Adding NIF in DB!");
  1285. continue;
  1286. };
  1287. #ifdef DEBUG_EXTRA
  1288. LOG("NIF added!");
  1289. }
  1290. else
  1291. {
  1292. LOG("NIF Already added (regular Mode)!");
  1293. #endif
  1294. };
  1295. m_pDBC->addResource(/*m_nServerNodeID,*/ res_type, device, nif_id);
  1296. if (ifa->ifa_addr->sa_family == AF_INET)
  1297. {
  1298. net_type = NETWORK_IPV4;
  1299. // create IPv4 string
  1300. struct sockaddr_in * in = (struct sockaddr_in *) ifa->ifa_addr;
  1301. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  1302. 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]);
  1303. 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]);
  1304. }
  1305. else
  1306. { // AF_INET6
  1307. net_type = NETWORK_IPV6;
  1308. // create IPv6 string
  1309. struct sockaddr_in6 * in6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  1310. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  1311. 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],
  1312. (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],
  1313. (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],
  1314. (uint8_t) ifa->ifa_netmask->sa_data[15], (uint8_t) ifa->ifa_netmask->sa_data[16], (uint8_t) ifa->ifa_netmask->sa_data[17]);
  1315. if (ifa->ifa_ifu.ifu_broadaddr != NULL)
  1316. {
  1317. 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],
  1318. (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],
  1319. (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],
  1320. (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],
  1321. (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[16], (uint8_t) ifa->ifa_ifu.ifu_broadaddr->sa_data[17]);
  1322. }
  1323. else
  1324. {
  1325. //sprintf(bc, "");
  1326. bc[0]=(char)0;
  1327. };
  1328. };
  1329. m_pDBC->addLocalNetwork(nif_id, net_type, addr, mask, gw, bc);
  1330. #ifdef DEBUG
  1331. sprintf(tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr);
  1332. LOG(tBuffer);
  1333. #endif
  1334. nif_id = 0;
  1335. nif_aux_id = 0;
  1336. router_id = 0;
  1337. nif_type = NIF_LOOPBACK;
  1338. net_type = NETWORK_IPV4;
  1339. res_type = HARDWARE_NIF_LOOPBACK;
  1340. nif_mode = ADDRESS_DISABLED;
  1341. };
  1342. freeifaddrs(ifaddr);
  1343. };
  1344. }
  1345. void hardwareManager::enumerateHardware(void)
  1346. {
  1347. char tBuffer[CTEXT];
  1348. DIR * dFD = NULL;
  1349. struct dirent * pDR = NULL;
  1350. // ToDo: Check Ability to work without Kernel and DBC
  1351. // if( m_pKernel == NULL ) return;
  1352. if (m_pDBC == NULL)
  1353. {
  1354. return;
  1355. }
  1356. #ifdef DEBUG
  1357. LOG("HW Enumeration Started!");
  1358. #endif
  1359. // Moved to a constructor!!
  1360. // m_nServerNodeID = m_pKernel->getServerNodeId();
  1361. // m_nServerID = m_pKernel->getServerId();
  1362. // m_nSiteID = m_pKernel->getSiteId();
  1363. /* Add Network Interfaces */
  1364. // if( !m_pDBC->cleanResources( true ) ){
  1365. // LOG_ERROR("Cannot DeActivate Old Ressources!");
  1366. // return false;
  1367. // };
  1368. if (! m_pDBC->wipeServerResources())
  1369. {
  1370. LOG_ERROR("Cannot Wipe Old Ressources!");
  1371. return;
  1372. };
  1373. enumerateNIFS();
  1374. /* Check HID Devices */
  1375. struct hid_device_info * pDevs;
  1376. uint8_t DCBCount;
  1377. // Enumerate USB devices with the given IDs
  1378. pDevs = hid_enumerate(0x16c0, 0x05df);
  1379. if (pDevs != NULL)
  1380. {
  1381. struct hid_device_info * pCDev = pDevs;
  1382. // Count the number of returned devices
  1383. for (DCBCount = 0; pCDev != NULL; DCBCount++)
  1384. {
  1385. pCDev = pCDev->next;
  1386. }
  1387. // Allocate a buffer for the relays
  1388. if (DCBCount > 0)
  1389. {
  1390. DC_BOARD tDCB;
  1391. // Fill the relay structs
  1392. pCDev = pDevs;
  1393. for (int idx = 0; idx < DCBCount; idx++)
  1394. {
  1395. // Save the path to this device
  1396. memset(tDCB.path, 0, MAX_PATH_LEN);
  1397. strncpy(tDCB.path, pCDev->path, MAX_PATH_LEN);
  1398. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  1399. tDCB.relay_count = atoi((const char *) &pCDev->product_string[8]);
  1400. // Open it to get more details
  1401. hid_device * handle;
  1402. handle = hid_open_path(tDCB.path);
  1403. if (handle)
  1404. {
  1405. unsigned char buf[9];
  1406. // Get the features of the device
  1407. buf[0] = 0x01;
  1408. if (hid_get_feature_report(handle, buf, sizeof(buf)) == -1)
  1409. {
  1410. LOG_ERROR("hid_get_feature_report");
  1411. pCDev = pCDev->next;
  1412. hid_close(handle);
  1413. continue;
  1414. };
  1415. // Set the serial number (0x0 for termination)
  1416. memset(tDCB.serial, 0x0, HID_DCR_SERIIAL_LENGTH + 1);
  1417. memcpy(tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH);
  1418. // Byte 7 in the response contains the target_state of the relays
  1419. tDCB.state = buf[7];
  1420. hid_close(handle);
  1421. #ifdef DEBUG
  1422. // Output the device enumeration details if verbose is on
  1423. LOG("HID DCB Found");
  1424. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  1425. LOG(tBuffer);
  1426. sprintf(tBuffer, " Path: %s", pCDev->path);
  1427. LOG(tBuffer);
  1428. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number);
  1429. LOG(tBuffer);
  1430. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  1431. LOG(tBuffer);
  1432. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  1433. LOG(tBuffer);
  1434. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  1435. LOG(tBuffer);
  1436. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  1437. LOG(tBuffer);
  1438. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  1439. LOG(tBuffer);
  1440. // If debug are on, output individual relay details
  1441. for (int k = 0; k < tDCB.relay_count; k++)
  1442. {
  1443. if (tDCB.state & 1 << k)
  1444. {
  1445. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  1446. }
  1447. else
  1448. {
  1449. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  1450. };
  1451. LOG(tBuffer);
  1452. };
  1453. #endif
  1454. m_pDBC->addLocalDCB(m_nSiteID, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state);
  1455. }
  1456. else
  1457. {
  1458. LOG_ERROR("unable to open HID device");
  1459. };
  1460. pCDev = pCDev->next;
  1461. };
  1462. };
  1463. hid_free_enumeration(pDevs);
  1464. };
  1465. /* Add devices from DEV */
  1466. dFD = opendir("/dev");
  1467. if (dFD != NULL)
  1468. {
  1469. while ((pDR = readdir(dFD)) != NULL)
  1470. {
  1471. if (strcasestr(pDR->d_name, "video") == pDR->d_name)
  1472. {
  1473. createResourceVIDEO(pDR->d_name);
  1474. continue;
  1475. };
  1476. if (strcasestr(pDR->d_name, "ttyS") == pDR->d_name)
  1477. {
  1478. createResourceUART(pDR->d_name);
  1479. continue;
  1480. };
  1481. // RaspberyPI Hardware
  1482. if (strcasestr(pDR->d_name, "ttyAMA") == pDR->d_name)
  1483. {
  1484. createResourceUART(pDR->d_name);
  1485. continue;
  1486. };
  1487. if (strcasestr(pDR->d_name, "ttyUSB") == pDR->d_name)
  1488. {
  1489. createResourceUART(pDR->d_name);
  1490. continue;
  1491. };
  1492. };
  1493. closedir(dFD);
  1494. };
  1495. // m_bHardwareEnumerated = true;
  1496. m_pDBC->updateSystemInfo(&m_sSYS_INFO);
  1497. }
  1498. uint32_t hardwareManager::createResourceUART(CSTR devName)
  1499. {
  1500. uint32_t RetVal = 0;
  1501. char tBuffer[CTEXT];
  1502. /* Enumerate BuiltIn Serial */
  1503. #ifdef DEBUG
  1504. sprintf(tBuffer, "Serial Device: %s", devName);
  1505. LOG(tBuffer);
  1506. #endif
  1507. char dev_name[CTEXT];
  1508. int fd = -1;
  1509. struct serial_struct ss;
  1510. sprintf(dev_name, "/dev/%s", devName);
  1511. fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
  1512. if (-1 == fd)
  1513. {
  1514. sprintf(tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1515. LOG_ERROR(tBuffer);
  1516. }
  1517. else
  1518. {
  1519. if (0 && (-1 == xioctl(fd, TIOCGSERIAL, &ss)))
  1520. {
  1521. LOG_ERROR("TIOCGSERIAL");
  1522. }
  1523. else
  1524. {
  1525. #ifdef DEBUG
  1526. 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);
  1527. LOG(tBuffer);
  1528. #endif
  1529. memset(tBuffer, 0, CTEXT);
  1530. getTTyDesc(devName, tBuffer);
  1531. if (tBuffer[0])
  1532. {
  1533. LOG(tBuffer);
  1534. uint32_t tID = m_pDBC->addLocalUART(/*m_nServerNodeID,*/ 1, 19200, 8, 'N', 1.0f, dev_name);
  1535. if (tID)
  1536. {
  1537. m_pDBC->addResource(/*m_nServerNodeID,*/ HARDWARE_UART, tBuffer, tID);
  1538. };
  1539. }
  1540. else
  1541. {
  1542. LOG_ERROR("Device Not Initialized at Boot Time!");
  1543. };
  1544. };
  1545. close(fd);
  1546. };
  1547. return RetVal;
  1548. }
  1549. uint32_t hardwareManager::createResourceVIDEO(CSTR devName)
  1550. {
  1551. uint32_t RetVal = 0;
  1552. char tBuffer[512];
  1553. /* Enumerate Video Capture */
  1554. #ifdef DEBUG_EXTRA
  1555. sprintf(tBuffer, "Video Source: %s", devName);
  1556. LOG(tBuffer);
  1557. #endif
  1558. struct v4l2_capability cap;
  1559. // struct v4l2_cropcap cropcap;
  1560. // struct v4l2_crop crop;
  1561. struct v4l2_format fmt;
  1562. struct v4l2_frmivalenum sprm;
  1563. char dev_name[CTEXT];
  1564. int fd = -1;
  1565. struct stat st;
  1566. sprintf(dev_name, "/dev/%s", devName);
  1567. if (-1 == stat(dev_name, &st))
  1568. {
  1569. sprintf(tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1570. LOG_ERROR(tBuffer);
  1571. return 0;
  1572. };
  1573. if (! S_ISCHR(st.st_mode))
  1574. {
  1575. sprintf(tBuffer, "%s is no devicen", dev_name);
  1576. LOG_ERROR(tBuffer);
  1577. return 0;
  1578. };
  1579. #ifdef DEBUG
  1580. LOG(dev_name);
  1581. #endif
  1582. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  1583. if (-1 == fd)
  1584. {
  1585. sprintf(tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1586. LOG_ERROR(tBuffer);
  1587. }
  1588. else
  1589. {
  1590. if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap))
  1591. {
  1592. if (EINVAL == errno)
  1593. {
  1594. sprintf(tBuffer, "%s is no V4L2 device", dev_name);
  1595. LOG_ERROR(tBuffer);
  1596. }
  1597. else
  1598. {
  1599. LOG_ERROR("VIDIOC_QUERYCAP");
  1600. };
  1601. close(fd);
  1602. return 0;
  1603. };
  1604. if (! (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
  1605. {
  1606. #ifdef DEBUG
  1607. sprintf(tBuffer, "%s is no video capture device", dev_name);
  1608. LOG_ERROR(tBuffer);
  1609. #endif
  1610. close(fd);
  1611. return 0;
  1612. };
  1613. CLEAR(fmt);
  1614. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1615. /* Preserve original settings as set by v4l2-ctl for example */
  1616. if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
  1617. {
  1618. LOG_ERROR("VIDIOC_G_FMT");
  1619. }
  1620. else
  1621. {
  1622. CLEAR(sprm);
  1623. if (-1 == xioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &sprm) && 0)
  1624. {
  1625. LOG_ERROR("VIDIOC_ENUM_FRAMEINTERVALS");
  1626. }
  1627. else
  1628. {
  1629. uint16_t fps = 15;
  1630. switch (sprm.type)
  1631. {
  1632. case V4L2_FRMIVAL_TYPE_DISCRETE : LOG("1DISCRETE"); break;
  1633. case V4L2_FRMIVAL_TYPE_CONTINUOUS: LOG("2DISCRETE"); break;
  1634. case V4L2_FRMIVAL_TYPE_STEPWISE : LOG("3DISCRETE"); break;
  1635. };
  1636. #ifdef DEBUG
  1637. sprintf(tBuffer, "Grabber{%s}{%s}[%dx%d@%d]", cap.card, cap.driver, fmt.fmt.pix.width, fmt.fmt.pix.height, fps);
  1638. LOG(tBuffer);
  1639. #endif
  1640. uint32_t mid = m_pDBC->addLocalCam((CSTR) cap.card, (CSTR) cap.driver, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height, fps);
  1641. // uint64_t mid = m_pDBC->addLocalMediaSource( m_nSiteID, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  1642. // m_pDBC->addNodeSocket( m_nServerNodeID, mid, NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  1643. RetVal = m_pDBC->addResource(HARDWARE_ANALOG_VIDEO_IN, dev_name, mid);
  1644. };
  1645. };
  1646. close(fd);
  1647. };
  1648. return RetVal;
  1649. }
  1650. uint32_t hardwareManager::createResourceUDCB(CSTR devName)
  1651. {
  1652. uint32_t RetVal = 0;
  1653. if ( devName ) RetVal = 1;
  1654. RetVal = 0;
  1655. // todo: must do something
  1656. return RetVal;
  1657. }
  1658. bool hardwareManager::assignResource(hardwareResource * ptr, bool protected_mode)
  1659. {
  1660. LOG("ASSIGN HWR");
  1661. if (ptr == NULL)
  1662. {
  1663. return false;
  1664. }
  1665. if (protected_mode)
  1666. {
  1667. m_mVectorProtector.lock();
  1668. }
  1669. LOG("LOCKED");
  1670. if (std::find(m_vResources.begin(), m_vResources.end(), ptr) == m_vResources.end())
  1671. {
  1672. m_vResources.push_back(ptr);
  1673. LOG("ADDED");
  1674. }
  1675. else
  1676. {
  1677. LOG("ALREADY ASSIGNED"); // validated twice per round otherwise
  1678. };
  1679. if (protected_mode)
  1680. {
  1681. m_mVectorProtector.unlock();
  1682. }
  1683. return true;
  1684. }
  1685. bool hardwareManager::resignResource(hardwareResource * ptr, bool protected_mode)
  1686. {
  1687. LOG("RESIGN HWR");
  1688. if (ptr == NULL)
  1689. {
  1690. return false;
  1691. }
  1692. if (protected_mode)
  1693. {
  1694. m_mVectorProtector.lock();
  1695. }
  1696. LOG("LOCKED");
  1697. if (m_vResources.size())
  1698. {
  1699. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  1700. {
  1701. if ((*idx) == ptr)
  1702. {
  1703. LOG("FOUND");
  1704. m_vResources.erase(idx);
  1705. LOG("ERASED");
  1706. break;
  1707. };
  1708. };
  1709. }
  1710. else
  1711. {
  1712. LOG("NO RES!");
  1713. };
  1714. LOG("UNLOCKED");
  1715. if (protected_mode)
  1716. {
  1717. m_mVectorProtector.unlock();
  1718. }
  1719. return true;
  1720. }
  1721. hardwareResource * hardwareManager::requestResource(HARDWARE_PROVIDERS type, uint32_t id)
  1722. {
  1723. hardwareResource * RetVal = NULL;
  1724. m_mVectorProtector.lock();
  1725. if (m_vResources.size())
  1726. {
  1727. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  1728. {
  1729. #ifdef DEBUG_EXTRA
  1730. char tB[256];
  1731. sprintf(tB, "Check Resources RR[%d] RI[%d] ?? CR[%d] CI[%d]", type, id, (*idx)->getProviderType(), (*idx)->getDbId());
  1732. LOG(tB);
  1733. #endif
  1734. if (((*idx)->getProviderType() == type) && ((*idx)->getDbId() == id))
  1735. {
  1736. if ( (*idx)->activate() ) {
  1737. RetVal = (*idx);
  1738. };
  1739. break;
  1740. };
  1741. };
  1742. }
  1743. else
  1744. {
  1745. LOG("No Resources!!");
  1746. };
  1747. m_mVectorProtector.unlock();
  1748. return RetVal;
  1749. }
  1750. DBC_INT_LIST * hardwareManager::requestResourcesByType(HARDWARE_PROVIDERS type)
  1751. {
  1752. DBC_INT_LIST * RetVal = NULL;
  1753. #ifdef DEBUG
  1754. char tBuffer[128];
  1755. sprintf(tBuffer, "Requested Resource type: %d", type);
  1756. LOG(tBuffer);
  1757. #endif
  1758. m_mVectorProtector.lock();
  1759. if (m_vResources.size())
  1760. {
  1761. #ifdef DEBUG
  1762. LOG("Have Some Resources!");
  1763. #endif
  1764. RetVal = new DBC_INT_LIST();
  1765. if (RetVal)
  1766. {
  1767. #ifdef DEBUG
  1768. LOG("RetVal Created!");
  1769. #endif
  1770. for (VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++)
  1771. {
  1772. #ifdef DEBUG
  1773. sprintf(tBuffer, "Have Resource type: %d", (*idx)->getHardwareType());
  1774. LOG(tBuffer);
  1775. #endif
  1776. if ((*idx)->getProviderType() == type)
  1777. {
  1778. #ifdef DEBUG
  1779. LOG("Matched!");
  1780. #endif
  1781. RetVal->push_back((*idx)->getDbId());
  1782. };
  1783. };
  1784. }
  1785. else
  1786. {
  1787. LOG_ERROR("Cannot Create DBC_INT_LIST");
  1788. };
  1789. };
  1790. m_mVectorProtector.unlock();
  1791. return RetVal;
  1792. }
  1793. #include <signal.h>
  1794. #include <sys/types.h>
  1795. #ifdef USE_LMSENSORS
  1796. #include <sensors/sensors.h>
  1797. #include <sensors/error.h>
  1798. #endif
  1799. bool hardwareManager::updateSysInfo(DBC_SYS_LOAD_INFO * ptr)
  1800. {
  1801. if (ptr == NULL)
  1802. {
  1803. return false;
  1804. }
  1805. struct sysinfo SI;
  1806. struct statvfs DI;
  1807. ptr->CPU_CORES = get_nprocs();
  1808. if (sysinfo(&SI) == 0)
  1809. {
  1810. float f_load = 100.f / (float) (ptr->CPU_CORES * (1 << SI_LOAD_SHIFT));
  1811. ptr->cpu1 = SI.loads[0] * f_load;
  1812. ptr->cpu5 = SI.loads[1] * f_load;
  1813. ptr->cpu15 = SI.loads[2] * f_load;
  1814. ptr->T_MEM = SI.totalram * SI.mem_unit;
  1815. // "freeram" is obvious, and "bufferram" is memory temporarily used by the kernel to cache some files
  1816. ptr->C_MEM = (SI.totalram - SI.freeram - SI.bufferram) * SI.mem_unit;
  1817. ptr->mem = 100.0f * (float) ptr->C_MEM / (float) ptr->T_MEM;
  1818. #ifdef DEBUG_EXTRA
  1819. char tB[128];
  1820. sprintf(tB, "Total Mem: %" PRIu64 " Free: %" PRIu64 " Perc: %.2f", SI.totalram, SI.freeram, ptr->mem);
  1821. LOG(tB);
  1822. #endif
  1823. #ifdef MEMORY_CONSUMPTION_LIMIT
  1824. if (ptr->mem > 80.0f)
  1825. {
  1826. LOG_ERROR("Try to free some caches!");
  1827. /*
  1828. To free pagecache:
  1829. # echo 1 > /proc/sys/vm/drop_caches
  1830. To free dentries and inodes:
  1831. # echo 2 > /proc/sys/vm/drop_caches
  1832. To free pagecache, dentries and inodes:
  1833. # echo 3 > /proc/sys/vm/drop_caches
  1834. */
  1835. // system( "echo 1 > /proc/sys/vm/drop_caches" );
  1836. if (m_bCacheCleanCounter)
  1837. {
  1838. m_bCacheCleanCounter--;
  1839. }
  1840. else
  1841. {
  1842. // KillSellf
  1843. // kill( getpid(), 9 );
  1844. };
  1845. };
  1846. #endif
  1847. }
  1848. else
  1849. {
  1850. LOG_ERROR("Error while getting System Info!");
  1851. };
  1852. // const uint32_t MB = 1 << 10;//(1024 * 1024);// * 1024;
  1853. if( statvfs("/", &DI) == 0 ){
  1854. ptr->T_DISK = (DI.f_blocks * (unsigned long long) DI.f_frsize) >> 10; // / MB;
  1855. // cDISK = ( DI.f_bavail * (unsigned long long) DI.f_bsize ) >> 10;// / MB;
  1856. ptr->C_DISK = ptr->T_DISK - ((DI.f_bfree * (unsigned long long) DI.f_bsize) >> 10);
  1857. ptr->disk = 100.0f * (float) ptr->C_DISK / (float) ptr->T_DISK;
  1858. } else {
  1859. LOG_ERROR("Error while getting Disk Info!");
  1860. };
  1861. // Temperature reading
  1862. ptr->temperature = -1;
  1863. if (fileExist("/sys/class/thermal")){
  1864. char tB[128];
  1865. // Check first 8 zones to find CPU one
  1866. for (uint8_t i = 0; i < 8; i++){
  1867. sprintf(tB, "/sys/class/thermal/thermal_zone%d", i);
  1868. if (fileExist(tB)){
  1869. sprintf(tB, "/sys/class/thermal/thermal_zone%d/type", i);
  1870. if (fileExist(tB)){
  1871. FILE * fd = fopen(tB, "r");
  1872. readLine(fd, tB, 128);
  1873. fclose(fd);
  1874. if (strcasestr(tB, "cpu") != NULL || strcasestr(tB, "x86_pkg") != NULL || strcasestr(tB, "acpitz") != NULL){
  1875. sprintf(tB, "/sys/class/thermal/thermal_zone%d/temp", i);
  1876. FILE * fd = fopen(tB, "r");
  1877. readLine(fd, tB, 128);
  1878. fclose(fd);
  1879. ptr->temperature = ((float) atoll(tB)) / 1000.0f;
  1880. break;
  1881. };
  1882. } else {
  1883. continue;
  1884. };
  1885. } else {
  1886. break;
  1887. };
  1888. };
  1889. };
  1890. #ifdef USE_LMSENSORS
  1891. if ( ptr->temperature < 0 ) {
  1892. //still have problem to read temp
  1893. // last chance -> use lm-sensors
  1894. if( sensors_init(nullptr) == 0 ) {
  1895. int chip_nr = 0;
  1896. const sensors_chip_name* chip;
  1897. // 2. Iterate through all detected sensor chips
  1898. while ((chip = sensors_get_detected_chips(nullptr, &chip_nr)) != nullptr) {
  1899. //char chip_name_buf[256];
  1900. //sensors_snprintf_chip_name(chip_name_buf, sizeof(chip_name_buf), chip);
  1901. //LOG( chip_name_buf );
  1902. //LOG( chip->prefix );
  1903. int feat_nr = 0;
  1904. const sensors_feature* feature;
  1905. // 3. Iterate through all features of the current chip
  1906. while ((feature = sensors_get_features(chip, &feat_nr)) != nullptr) {
  1907. // Filter strictly for Temperature Features
  1908. if (feature->type == SENSORS_FEATURE_TEMP) {
  1909. // Get the user-friendly label for this sensor (e.g., "Core 0", "CPU Temperature")
  1910. //char* label = sensors_get_label(chip, feature);
  1911. //LOG((label ? label : "Unknown"));
  1912. //if( label ) free( label ); // Must free the label returned by libsensors
  1913. // 4. Find the main subfeature containing the actual reading (input value)
  1914. const sensors_subfeature* subfeature = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
  1915. if (subfeature != nullptr) {
  1916. double val = 0.0;
  1917. // 5. Inquiry the actual temperature reading
  1918. if ( sensors_get_value(chip, subfeature->number, &val) == 0 ) {
  1919. //char tB[128];
  1920. //sprintf( tB, "T: %0.3f �C", val );
  1921. //LOG( tB );
  1922. if ( val > 0.0 ) {
  1923. ptr->temperature = ( float ) val;
  1924. break;
  1925. };
  1926. } else {
  1927. LOG_ERROR("[Error reading value]");
  1928. };
  1929. } else {
  1930. LOG_ERROR("[No input channel found]");
  1931. };
  1932. };
  1933. };
  1934. if ( ptr->temperature > 0.0f ) {
  1935. break;
  1936. };
  1937. };
  1938. // 6. Clean up resources allocated by the library
  1939. sensors_cleanup();
  1940. };
  1941. };
  1942. #endif //USE_LMSENSORS
  1943. return m_pDBC->updateServerUtilisation(ptr);
  1944. }
  1945. void hardwareManager::clearAllResources(void) {}
  1946. #undef CLEAR
  1947. #undef LOG
  1948. #undef LOG_ERROR