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.

1962 lines
46 KiB

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