1 //-----------------------------------------------------------------------------
2 //
3 //	ManufacturerSpecificDB.h
4 //
5 //	Interface for Handling Device Configuration Files.
6 //
7 //	Copyright (c) 2016 Justin Hammond <justin@dynam.ac>
8 //
9 //	SOFTWARE NOTICE AND LICENSE
10 //
11 //	This file is part of OpenZWave.
12 //
13 //	OpenZWave is free software: you can redistribute it and/or modify
14 //	it under the terms of the GNU Lesser General Public License as published
15 //	by the Free Software Foundation, either version 3 of the License,
16 //	or (at your option) any later version.
17 //
18 //	OpenZWave is distributed in the hope that it will be useful,
19 //	but WITHOUT ANY WARRANTY; without even the implied warranty of
20 //	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 //	GNU Lesser General Public License for more details.
22 //
23 //	You should have received a copy of the GNU Lesser General Public License
24 //	along with OpenZWave.  If not, see <http://www.gnu.org/licenses/>.
25 //
26 //-----------------------------------------------------------------------------
27 
28 #ifndef _ManufacturerSpecificDB_H
29 #define _ManufacturerSpecificDB_H
30 
31 #include <string>
32 #include <map>
33 #include <list>
34 
35 #include "Node.h"
36 #include "platform/Ref.h"
37 #include "Defs.h"
38 
39 namespace OpenZWave
40 {
41 	class Driver;
42 	namespace Internal
43 	{
44 		namespace Platform
45 		{
46 			class Mutex;
47 		}
48 
49 		class ProductDescriptor
50 		{
51 			public:
ProductDescriptor(uint16 _manufacturerId,uint16 _productType,uint16 _productId,string const & _productName,string const & _manufacturerName,string const & _configPath)52 				ProductDescriptor(uint16 _manufacturerId, uint16 _productType, uint16 _productId, string const& _productName, string const& _manufacturerName, string const& _configPath) :
53 						m_manufacturerId(_manufacturerId), m_productType(_productType), m_productId(_productId), m_productName(_productName), m_manufacturerName(_manufacturerName), m_configPath(_configPath), m_configrevision(0)
54 				{
55 				}
~ProductDescriptor()56 				~ProductDescriptor()
57 				{
58 
59 				}
GetKey()60 				int64 GetKey() const
61 				{
62 					return (GetKey(m_manufacturerId, m_productType, m_productId));
63 				}
64 
GetKey(uint16 _manufacturerId,uint16 _productType,uint16 _productId)65 				static int64 GetKey(uint16 _manufacturerId, uint16 _productType, uint16 _productId)
66 				{
67 					int64 key = (((int64) _manufacturerId) << 32) | (((int64) _productType) << 16) | (int64) _productId;
68 					return key;
69 				}
70 
GetManufacturerId()71 				uint16 GetManufacturerId() const
72 				{
73 					return m_manufacturerId;
74 				}
GetManufacturerName()75 				string GetManufacturerName() const
76 				{
77 					return m_manufacturerName;
78 				}
GetProductType()79 				uint16 GetProductType() const
80 				{
81 					return m_productType;
82 				}
GetProductId()83 				uint16 GetProductId() const
84 				{
85 					return m_productId;
86 				}
GetProductName()87 				string GetProductName() const
88 				{
89 					return m_productName;
90 				}
GetConfigPath()91 				string GetConfigPath() const
92 				{
93 					return m_configPath;
94 				}
SetConfigRevision(uint32 revision)95 				void SetConfigRevision(uint32 revision)
96 				{
97 					m_configrevision = revision;
98 				}
GetConfigRevision()99 				uint32 GetConfigRevision() const
100 				{
101 					return m_configrevision;
102 				}
103 			private:
104 				uint16 m_manufacturerId;
105 				uint16 m_productType;
106 				uint16 m_productId;
107 				string m_productName;
108 				string m_manufacturerName;
109 				string m_configPath;
110 				uint32 m_configrevision;
111 		};
112 
113 		/** \brief The _ManufacturerSpecificDB class handles the Config File Database
114 		 * that we use to configure devices.
115 		 */
116 		class OPENZWAVE_EXPORT ManufacturerSpecificDB
117 		{
118 			public:
119 				static ManufacturerSpecificDB *Create();
Get()120 				static ManufacturerSpecificDB *Get()
121 				{
122 					return s_instance;
123 				}
124 				static void Destroy();
125 
126 				bool LoadProductXML();
127 				void UnloadProductXML();
getRevision()128 				uint32 getRevision()
129 				{
130 					return m_revision;
131 				}
getLatestRevision()132 				uint32 getLatestRevision()
133 				{
134 					return m_latestRevision;
135 				}
136 				;
setLatestRevision(uint32 rev)137 				void setLatestRevision(uint32 rev)
138 				{
139 					m_latestRevision = rev;
140 				}
141 				;
142 				void checkConfigFiles(Driver *);
143 				void configDownloaded(Driver *, string file, uint8 node, bool success = true);
144 				void mfsConfigDownloaded(Driver *, string file, bool success = true);
145 				bool isReady();
146 				bool updateConfigFile(Driver *, Node *);
147 				bool updateMFSConfigFile(Driver *);
148 				void checkInitialized();
149 
150 			private:
151 				void LoadConfigFileRevision(ProductDescriptor *product);
152 				ManufacturerSpecificDB();
153 				~ManufacturerSpecificDB();
154 
155 				Internal::Platform::Mutex* m_MfsMutex; /**< Mutex to ensure its accessed by a single thread at a time */
156 
157 				static ManufacturerSpecificDB *s_instance;
158 			public:
159 				std::shared_ptr<ProductDescriptor> getProduct(uint16 _manufacturerId, uint16 _productType, uint16 _productId);
160 
161 			private:
162 				static map<uint16, string> s_manufacturerMap;
163 				static map<int64, std::shared_ptr<ProductDescriptor> > s_productMap;
164 				static bool s_bXmlLoaded;
165 
166 				list<string> m_downloading;
167 				uint32 m_revision;
168 				uint32 m_latestRevision;
169 				bool m_initializing;
170 
171 		};
172 
173 	} // namespace Internal
174 } // namespace OpenZWave
175 
176 #endif
177