Configuration.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * TI Voxel Lib component.
  3. *
  4. * Copyright (c) 2014 Texas Instruments Inc.
  5. */
  6. #ifndef VOXEL_CONFIGURATION_H
  7. #define VOXEL_CONFIGURATION_H
  8. #include "Common.h"
  9. #include "Serializable.h"
  10. #include "DataPacket.h"
  11. #include "HardwareSerializer.h"
  12. #define FILE_PREFIX "file:"
  13. #define CALIB_DISABLE "calib_disable"
  14. namespace Voxel
  15. {
  16. /**
  17. * \ingroup Util
  18. */
  19. struct SDKVersion
  20. {
  21. uint8_t major, minor, patch, abi, conf;
  22. };
  23. class VOXEL_EXPORT Configuration
  24. {
  25. protected:
  26. struct _Path
  27. {
  28. String installPath;
  29. String environmentVariable;
  30. };
  31. String _sdkPath;
  32. String _sdkVersion;
  33. Vector<String> _pathKeys;
  34. Vector<String> _pathValues;
  35. static const Map<String, _Path> _pathTypes;
  36. bool _getPaths(const String &type, Vector<String> &paths);
  37. bool _get(const String &type, String &name);
  38. static bool _addPath(const String &type, const String &path);
  39. public:
  40. static SDKVersion getSDKVersion();
  41. Configuration();
  42. inline bool getFirmwarePaths(Vector<String> &paths) { return _getPaths("fw", paths); }
  43. inline bool getConfPaths(Vector<String> &paths) { return _getPaths("conf", paths); }
  44. inline bool getLibPaths(Vector<String> &paths) { return _getPaths("lib", paths); }
  45. inline static bool addFirmwarePath(const String &path) { return _addPath("fw", path); }
  46. inline static bool addConfPath(const String &path) { return _addPath("conf", path); }
  47. inline static bool addLibPath(const String &path) { return _addPath("lib", path); }
  48. static bool addPathToEnvironmentVariable(const String &environmentVariable, const String &path, bool prepend = true);
  49. inline static bool getEnvironmentVariable(const String &environmentVariable, String &value)
  50. {
  51. char *p = getenv(environmentVariable.c_str());
  52. if(p) { value = p; return true; }
  53. return false;
  54. }
  55. static bool setEnvironmentVariable(const String &environmentVariable, const String &value);
  56. inline static bool setSDKPath(const String &path) { return setEnvironmentVariable("VOXEL_SDK_PATH", path); }
  57. inline static bool getSDKPath(String &path) { return getEnvironmentVariable("VOXEL_SDK_PATH", path); }
  58. bool getLocalPath(const String &type, String &path);
  59. inline bool getLocalFirmwarePath(String &path) { return getLocalPath("fw", path); }
  60. inline bool getLocalConfPath(String &path) { return getLocalPath("conf", path); }
  61. inline bool getLocalLibPath(String &path) { return getLocalPath("lib", path); }
  62. bool getLocalFile(const String &type, String &fileName);
  63. inline bool getLocalFirmwareFile(String &fileName) { return getLocalFile("fw", fileName); }
  64. inline bool getLocalConfFile(String &fileName) { return getLocalFile("conf", fileName); }
  65. inline bool getLocalLibFile(String &fileName) { return getLocalFile("lib", fileName); }
  66. /// Updates "name" to full path
  67. inline bool getConfFile(String &fileName) { return _get("conf", fileName); }
  68. inline bool getFirmwareFile(String &fileName) { return _get("fw", fileName); }
  69. inline bool geLibFile(String &fileName) { return _get("lib", fileName); }
  70. };
  71. class ConfigurationFile;
  72. class VOXEL_EXPORT ConfigSet
  73. {
  74. protected:
  75. bool _get(const String &name, String &value) const;
  76. bool _set(const String &name, const String &value);
  77. public:
  78. Map<String, String> params;
  79. Vector<String> paramNames;
  80. bool remove(const String &name);
  81. bool isEmpty() { return params.size() == 0; }
  82. bool isPresent(const String &name) const;
  83. String get(const String &name) const;
  84. int getInteger(const String &name) const;
  85. float getFloat(const String &name) const;
  86. bool getBoolean(const String &name) const;
  87. bool set(const String &name, const String &value);
  88. bool setInteger(const String &name, int value);
  89. bool setFloat(const String &name, float value);
  90. bool setBoolean(const String &name, bool value);
  91. friend class ConfigurationFile;
  92. };
  93. struct ConfigDataPacket: public DataPacket
  94. {
  95. enum PacketType
  96. {
  97. PACKET_CONFIG = 0,
  98. PACKET_2D_DATA_FILE = 1
  99. };
  100. ConfigDataPacket(): DataPacket() {}
  101. };
  102. class MainConfigurationFile;
  103. class VOXEL_EXPORT ConfigurationFile
  104. {
  105. protected:
  106. bool _get(const String &section, const String &name, String &value) const;
  107. bool _set(const String &section, const String &name, const String &value);
  108. String _fileName;
  109. Map<String, Vector<ByteType>> _dataFiles;
  110. MainConfigurationFile *_mainConfigurationFile;
  111. int _id, _parentID;
  112. String _profileName;
  113. mutable Mutex _mutex;
  114. bool _serializeAllDataFiles(OutputStream &out);
  115. bool _saveAllDataFiles(const String &prefix);
  116. template <typename T>
  117. bool _getData(const String &fileName, Vector<T> &data);
  118. template <typename T>
  119. bool _setData(const String &fileName, const Vector<T> &data);
  120. bool _copyFromParentIfNotPresent(ConfigurationFile *other, bool recurse = true);
  121. public:
  122. typedef Map<String, ConfigSet> ConfigSetMap;
  123. enum Location
  124. {
  125. IN_HOST = 0,
  126. IN_CAMERA = 1
  127. };
  128. protected:
  129. Location _location;
  130. public:
  131. ConfigSetMap configs;
  132. inline Location getLocation() const { return _location; }
  133. inline int getID() const { return _id; }
  134. inline int getParentID() const { return _parentID; }
  135. inline const String &getProfileName() const { return _profileName; }
  136. virtual bool isPresent(const String &section, const String &name, bool includeParent = true) const;
  137. virtual String get(const String &section, const String &name) const;
  138. inline void setProfileName(const String &name) { _profileName = name; set("global", "name", name); }
  139. inline void setID(const int id) { _id = id; setInteger("global", "id", id); }
  140. inline void setParentID(const int id) { _parentID = id; setInteger("global", "parent", id); }
  141. template <typename T>
  142. bool getFile(const String &section, const String &name, String &fileName, Vector<T> &data);
  143. int getInteger(const String &section, const String &name) const;
  144. float getFloat(const String &section, const String &name) const;
  145. bool getBoolean(const String &section, const String &name) const;
  146. virtual bool set(const String &section, const String &name, const String &value);
  147. template <typename T>
  148. bool setFile(const String &section, const String &name, const String &fileName, const Vector<T> &data);
  149. bool setInteger(const String &section, const String &name, int value);
  150. bool setFloat(const String &section, const String &name, float value);
  151. bool setBoolean(const String &section, const String &name, bool value);
  152. bool remove(const String &section, const String &name);
  153. bool removeSection(const String &section);
  154. virtual bool getConfigSet(const String &section, const ConfigSet *&configSet, bool includeParent = true) const;
  155. virtual bool read(const String &configFile);
  156. virtual bool read(InputStream &in);
  157. virtual bool write(const String &configFile = "");
  158. virtual bool write(OutputStream &out);
  159. bool isValidCameraProfile();
  160. virtual bool removeFile();
  161. inline void clear() { Lock<Mutex> _(_mutex); configs.clear(); }
  162. inline const String &getConfigFileName() { return _fileName; }
  163. ConfigurationFile(): ConfigurationFile(0) {}
  164. ConfigurationFile(MainConfigurationFile *mainConfigurationFile):
  165. _mainConfigurationFile(mainConfigurationFile), _id(-1), _parentID(-1), _location(IN_HOST) {}
  166. ConfigurationFile(const ConfigurationFile &other)
  167. {
  168. operator =(other);
  169. }
  170. inline ConfigurationFile &operator =(const ConfigurationFile &other)
  171. {
  172. configs = other.configs;
  173. _fileName = other._fileName;
  174. _mainConfigurationFile = other._mainConfigurationFile;
  175. _id = other._id;
  176. _profileName = other._profileName;
  177. _parentID = other._parentID;
  178. _location = other._location;
  179. _dataFiles = other._dataFiles;
  180. return *this;
  181. }
  182. inline bool mergeParentConfiguration()
  183. {
  184. return _copyFromParentIfNotPresent(this);
  185. }
  186. inline ConfigurationFile &copy(const ConfigurationFile &other)
  187. {
  188. return operator =(other);
  189. }
  190. virtual ~ConfigurationFile() {}
  191. friend class MainConfigurationFile;
  192. };
  193. template <typename T>
  194. bool ConfigurationFile::getFile(const String &section, const String &name, String &fileName, Vector<T> &data)
  195. {
  196. String v = get(section, name);
  197. if(v.compare(0, sizeof(FILE_PREFIX) - 1, FILE_PREFIX) != 0 || v.size() <= sizeof(FILE_PREFIX) - 1)
  198. {
  199. logger(LOG_ERROR) << "ConfigurationFile: section = " << section << ", name = " << name << ", is not a file type." << std::endl;
  200. return false;
  201. }
  202. fileName = v.substr(sizeof(FILE_PREFIX) - 1);
  203. return _getData(fileName, data);
  204. }
  205. template <typename T>
  206. bool ConfigurationFile::setFile(const String &section, const String &name, const String &fileName, const Vector<T> &data)
  207. {
  208. return _setData(fileName, data) && set(section, name, "file:" + fileName);
  209. }
  210. template <typename T>
  211. bool ConfigurationFile::_setData(const String &fileName, const Vector<T> &data)
  212. {
  213. Configuration c;
  214. String f = fileName;
  215. if(!c.getLocalConfFile(f))
  216. {
  217. logger(LOG_ERROR) << "ConfigurationFile: Failed to locate file '" << fileName << "'" << std::endl;
  218. return false;
  219. }
  220. OutputFileStream fs(f, std::ios::out | std::ios::binary);
  221. if(!fs.good())
  222. {
  223. logger(LOG_ERROR) << "ConfigurationFile: Could not open file '" << fileName << "'" << std::endl;
  224. return false;
  225. }
  226. fs.write((const char *)data.data(), data.size()*sizeof(T));
  227. fs.close();
  228. _dataFiles[fileName].resize(data.size()*sizeof(T));
  229. memcpy(_dataFiles[fileName].data(), data.data(), data.size()*sizeof(T));
  230. return true;
  231. }
  232. struct CalibrationInformation
  233. {
  234. String name;
  235. int id;
  236. Vector<String> definingParameters;
  237. Vector<String> calibrationParameters;
  238. };
  239. class VOXEL_EXPORT MainConfigurationFile: public ConfigurationFile
  240. {
  241. protected:
  242. Map<int, ConfigurationFile> _cameraProfiles;
  243. Map<int, String> _cameraProfileNames;
  244. int _defaultCameraProfileID, _defaultCameraProfileIDInHardware;
  245. int _currentCameraProfileID;
  246. ConfigurationFile *_currentCameraProfile;
  247. bool _updateCameraProfileList();
  248. String _mainConfigName, _hardwareID;
  249. int _getNewCameraProfileID(bool inHost = true);
  250. bool _saveHardwareImage(Version &version, TimeStampType &timestamp, SerializedObject &so);
  251. HardwareSerializerPtr _hardwareSerializer;
  252. int _quantizationFactor;
  253. Map<String, CalibrationInformation> _calibrationInformation;
  254. bool _removeCameraProfile(const int id, bool updateHardware = true);
  255. bool _saveCameraProfileToHardware(int &id, Vector<int> &newIDsAdded, Vector<ConfigurationFile> &oldIDsModified, bool saveParents = false, bool updateHardware = true, bool setAsDefault = false, const String &namePrefix = "");
  256. bool _rollbackCameraProfiles(const Vector<int> &newIDsAdded, const Vector<ConfigurationFile> &oldIDsModified);
  257. public:
  258. MainConfigurationFile(const String &name, const String &hardwareID, int quantizationFactor = 4, HardwareSerializerPtr hardwareSerializer = nullptr):
  259. _currentCameraProfile(nullptr), _defaultCameraProfileID(-1), _defaultCameraProfileIDInHardware(-1), _mainConfigName(name), _hardwareSerializer(hardwareSerializer),
  260. _quantizationFactor(quantizationFactor)
  261. {
  262. if(!hardwareSerializer)
  263. _hardwareSerializer = HardwareSerializerPtr(new HardwareSerializer());
  264. }
  265. void setSerializationQuantizationFactor(int quantizationFactor) { _quantizationFactor = quantizationFactor; }
  266. virtual bool read(const String &configFile);
  267. bool readFromHardware();
  268. bool writeToHardware();
  269. inline bool hasHardwareConfigurationSupport() { return _hardwareSerializer && *_hardwareSerializer; }
  270. inline void setHardwareConfigSerializer(const HardwareSerializerPtr &hardwareSerializer) { _hardwareSerializer = hardwareSerializer; }
  271. virtual String get(const String &section, const String &name) const;
  272. virtual bool isPresent(const String &section, const String &name, bool includeParent = true) const;
  273. int addCameraProfile(const String &profileName, const int parentID = -1);
  274. bool setCurrentCameraProfile(const int id);
  275. bool removeAllHardwareCameraProfiles();
  276. bool removeCameraProfile(const int id);
  277. bool saveCameraProfileToHardware(int &id, bool saveParents = false, bool setAsDefault = false, const String &namePrefix = "");
  278. ConfigurationFile *getDefaultCameraProfile();
  279. ConfigurationFile *getCameraProfile(const int id);
  280. template <typename T>
  281. bool getFile(const String &section, const String &name, String &fileName, Vector<T> &data);
  282. inline int getDefaultCameraProfileID()
  283. {
  284. if(_defaultCameraProfileIDInHardware >= 0)
  285. return _defaultCameraProfileIDInHardware;
  286. else
  287. return _defaultCameraProfileID;
  288. }
  289. inline int getDefaultCameraProfileIDInHost()
  290. {
  291. return _defaultCameraProfileID;
  292. }
  293. inline int getDefaultCameraProfileIDInCamera()
  294. {
  295. return _defaultCameraProfileIDInHardware;
  296. }
  297. /// This only removes the default ID from hardware and not the profile itself
  298. bool removeDefaultCameraProfileIDInCamera();
  299. bool setDefaultCameraProfile(const int id);
  300. inline void setHardwareID(const String &hwID) { _hardwareID = hwID; }
  301. inline int getCurrentProfileID() { return _currentCameraProfileID; }
  302. bool getCameraProfileName(const int id, String &cameraProfileName);
  303. inline const Map<int, String> &getCameraProfileNames() { return _cameraProfileNames; }
  304. inline const Map<String, CalibrationInformation> &getCalibrationInformation() const { return _calibrationInformation; }
  305. virtual ~MainConfigurationFile() {}
  306. friend class ConfigurationFile;
  307. friend class DepthCamera;
  308. };
  309. template <typename T>
  310. bool ConfigurationFile::_getData(const String &fileName, Vector<T> &data)
  311. {
  312. bool loadFromFile = false;
  313. if(_dataFiles.find(fileName) == _dataFiles.end())
  314. {
  315. if(_mainConfigurationFile && _parentID >= 0)
  316. {
  317. ConfigurationFile *parentConfig = _mainConfigurationFile->getCameraProfile(_parentID);
  318. // TODO This does not handle circular references between profiles
  319. if(parentConfig && parentConfig->_getData<T>(fileName, data))
  320. return true;
  321. else
  322. loadFromFile = true;
  323. }
  324. else
  325. loadFromFile = true;
  326. }
  327. if(loadFromFile)
  328. {
  329. Configuration c;
  330. String f = fileName;
  331. if(!c.getConfFile(f))
  332. {
  333. logger(LOG_ERROR) << "ConfigurationFile: Could not locate file '" << fileName << "'" << std::endl;
  334. return false;
  335. }
  336. InputFileStream fs(f, std::ios::in | std::ios::binary | std::ios::ate);
  337. if(!fs.good())
  338. {
  339. logger(LOG_ERROR) << "ConfigurationFile: Could not open file '" << fileName << "'" << std::endl;
  340. return false;
  341. }
  342. int size = fs.tellg();
  343. if(size == 0)
  344. {
  345. logger(LOG_ERROR) << "ConfigurationFile: Null config data '" << f << "'" << std::endl;
  346. return false;
  347. }
  348. Vector<ByteType> &d = _dataFiles[fileName];
  349. d.resize(size);
  350. fs.seekg(std::ios::beg);
  351. fs.clear();
  352. fs.read((char *)d.data(), size);
  353. }
  354. Vector<ByteType> &d = _dataFiles[fileName];
  355. data.resize((d.size() + sizeof(T)/2)/sizeof(T));
  356. memcpy(data.data(), d.data(), d.size());
  357. return true;
  358. }
  359. template <typename T>
  360. bool MainConfigurationFile::getFile(const String &section, const String &name, String &fileName, Vector<T> &data)
  361. {
  362. if(!_currentCameraProfile || !_currentCameraProfile->getFile(section, name, fileName, data))
  363. return ConfigurationFile::getFile(section, name, fileName, data);
  364. else
  365. return true;
  366. }
  367. }
  368. #endif // CONFIGURATION_H