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.

1953 lines
46 KiB

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