1 //-----------------------------------------------------------------------------
2 //
3 //	Defs.h
4 //
5 //	Basic types and preprocessor directives
6 //
7 //	Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
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 _Defs_H
29 #define _Defs_H
30 #include <assert.h>
31 #include <stdio.h>
32 #include <string>
33 #include <stdint.h>
34 
35 
36 
37 // Compilation export flags
38 #if (defined _WINDOWS || defined WIN32 || defined _MSC_VER) && (!defined MINGW && !defined __MINGW32__ && !defined __MINGW64__)
39 #	if defined OPENZWAVE_MAKEDLL	// Create the dynamic library.
40 #		define OPENZWAVE_EXPORT    __declspec(dllexport)
41 #	elif defined OPENZWAVE_USEDLL	// Use the dynamic library
42 #		define OPENZWAVE_EXPORT    __declspec(dllimport)
43 #	else							// Create/Use the static library
44 #		define OPENZWAVE_EXPORT
45 #	endif
46 // Disable export warnings
47 #	define OPENZWAVE_EXPORT_WARNINGS_OFF	__pragma( warning(push) )\
48 											__pragma( warning(disable: 4251) ) \
49 											__pragma( warning(disable: 4275) )
50 #	define OPENZWAVE_EXPORT_WARNINGS_ON		__pragma( warning(pop) )
51 #else
52 #	define OPENZWAVE_EXPORT
53 #	define OPENZWAVE_EXPORT_WARNINGS_OFF
54 #	define OPENZWAVE_EXPORT_WARNINGS_ON
55 #endif
56 
57 #ifdef __GNUC__
58 #define DEPRECATED __attribute__((deprecated))
59 #elif defined(_MSC_VER)
60 #define DEPRECATED __declspec(deprecated)
61 #else
62 #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
63 #define DEPRECATED
64 #endif
65 
66 
67 #ifdef NULL
68 #undef NULL
69 #endif
70 #define NULL 0
71 
72 // Basic types
73 typedef signed char			int8;
74 typedef unsigned char		uint8;
75 
76 typedef signed short		int16;
77 typedef unsigned short		uint16;
78 
79 typedef signed int			int32;
80 typedef unsigned int		uint32;
81 
82 #ifdef _MSC_VER
83 typedef signed __int64		int64;
84 typedef unsigned __int64	uint64;
85 #endif
86 
87 #ifdef __GNUC__
88 typedef signed long long	int64;
89 typedef unsigned long long  uint64;
90 #endif
91 
92 typedef float				float32;
93 typedef double				float64;
94 
95 typedef struct ozwversion {
96 	uint32_t _v; /* major << 16  | minor */
97 } ozwversion;
98 
99 /**
100  * \brief version_major - return the major version of the given struct
101  * \param v: the version number to obtain the major number from
102  * \return the Major Version Number
103  */
version_major(struct ozwversion v)104 static inline uint16_t version_major(struct ozwversion v) {
105 	return (v._v & 0xFFFF0000) >> 16;
106 }
107 
108 /**
109  * \brief version_minor - return the minor version of the given struct
110  * \param v: the version number to obtain the minor number from
111  * \return the Minor Version Number
112  */
version_minor(const struct ozwversion & v)113 static inline uint16_t version_minor(const struct ozwversion &v) {
114 	return v._v & 0xFFFF;
115 }
116 
117 /**
118  * \brief version - create a new version number
119  * \param major: major version number
120  * \param minor: minor version number
121  * \return the Version Number Struct
122  */
version(uint16_t major,uint16_t minor)123 static inline struct ozwversion version(uint16_t major, uint16_t minor)
124 {
125 	struct ozwversion v;
126 	v._v = (uint32_t)(major << 16) | (uint32_t)minor;
127 	return v;
128 }
129 
130 /**
131  * \brief version_cmp - compare two versions
132  * \param a: the first version number
133  * \param b: the second version number
134  * \return a number greater, equal, or less than 0 if a is greater, equal or
135  * less than b, respectively
136  *
137  * Example:
138  *	struct version a = version(1, 0);
139  *	struct version b = version(1, 3);
140  *	if (version_cmp(a, b) < 0)
141  *		printf("b is smaller than b\n");
142  */
version_cmp(struct ozwversion a,struct ozwversion b)143 static inline int version_cmp(struct ozwversion a, struct ozwversion b)
144 {
145 	return  (a._v == b._v) ? 0 : (a._v > b._v) ? 1 : - 1;
146 }
147 
148 #include "OZWException.h"
149 #define OPENZWAVE_DISABLE_EXCEPTIONS
150 #if defined(_MSC_VER)
151 #  define __MYFUNCTION__ __FUNCTION__
152 #else
153 #  define __MYFUNCTION__ __FILE__
154 #endif
155 // Exceptions : define OPENZWAVE_DISABLE_EXCEPTIONS in compiler flags to enable exceptions instead of exit function
156 #ifndef OPENZWAVE_DISABLE_EXCEPTIONS
157 
158 #  define OZW_FATAL_ERROR(exitCode, msg)   	Log::Write( LogLevel_Error,"Exception: %s:%d - %d - %s", std::string(__MYFUNCTION__).substr(std::string(__MYFUNCTION__).find_last_of("/\\") + 1).c_str(), __LINE__, exitCode, msg); \
159 											throw OZWException(__MYFUNCTION__, __LINE__, exitCode, msg)
160 #  define OZW_ERROR(exitCode, msg) 			Log::Write( LogLevel_Warning,"Exception: %s:%d - %d - %s", std::string(__MYFUNCTION__).substr(std::string(__MYFUNCTION__).find_last_of("/\\") + 1).c_str(), __LINE__, exitCode, msg); \
161 											throw OZWException(__MYFUNCTION__, __LINE__, exitCode, msg)
162 
163 #else
164 
165 #  define OZW_FATAL_ERROR(exitCode, msg)   	Log::Write( LogLevel_Error,"Exception: %s:%d - %d - %s", std::string(__MYFUNCTION__).substr(std::string(__MYFUNCTION__).find_last_of("/\\") + 1).c_str(), __LINE__, exitCode, msg); \
166 											std::cerr << "Error: "<< std::string(__MYFUNCTION__).substr(std::string(__MYFUNCTION__).find_last_of("/\\") + 1) << ":" << __LINE__ << " - " << msg << std::endl; exit(exitCode)
167 #  define OZW_ERROR(exitCode, msg)			Log::Write( LogLevel_Warning,"Exception: %s:%d - %d - %s", std::string(__MYFUNCTION__).substr(std::string(__MYFUNCTION__).find_last_of("/\\") + 1).c_str(), __LINE__, exitCode, msg);
168 
169 #endif
170 
171 // Declare the OpenZWave namespace
172 namespace std {}
173 namespace OpenZWave
174 {
175 	// Include the STL namespace
176 	using namespace std;
177 }
178 
179 // Modifications for Microsoft compilers
180 #ifdef _MSC_VER
181 
182 // Fix for namespace-related compiler bug
183 namespace OpenZWave
184 {
185 }
186 
187 // Rename safe versions of sprintf etc
188 #define snprintf sprintf_s
189 #define strcasecmp _stricmp
190 #define sscanf sscanf_s
191 #define strncpy strncpy_s
192 
193 #endif
194 
195 // Modifications for MiNGW32 compiler
196 #ifdef MINGW
197 
198 // Replace "safe" versions of sprintf
199 #define sprintf_s snprintf
200 
201 // seems some MINGW versions don't have a errno_t
202 #ifndef errno_t
203 #define errno_t int
204 #endif
205 
206 #define fopen_s fopen
207 
208 
209 #endif
210 
211 //#define MAX_TRIES		3	// Retry sends up to 3 times
212 #define MAX_TRIES		1	// set this to one, as I believe now that a ACK failure is indication that the device is offline, hence additional attempts will not work.
213 #define MAX_MAX_TRIES		7	// Don't exceed this retry limit
214 #define ACK_TIMEOUT	1000		// How long to wait for an ACK
215 #define BYTE_TIMEOUT	150
216 //#define RETRY_TIMEOUT	40000		// Retry send after 40 seconds
217 #define RETRY_TIMEOUT	10000		// Retry send after 10 seconds (we might need to keep this below 10 for Security CC to function correctly)
218 
219 #define SOF												0x01
220 #define ACK												0x06
221 #define NAK												0x15
222 #define CAN												0x18
223 
224 #define NUM_NODE_BITFIELD_BYTES							29		// 29 bytes = 232 bits, one for each possible node in the network.
225 
226 #define REQUEST											0x00
227 #define RESPONSE										0x01
228 
229 #define ZW_CLOCK_SET									0x30
230 
231 #define TRANSMIT_OPTION_ACK		 						0x01
232 #define TRANSMIT_OPTION_LOW_POWER		   				0x02
233 #define TRANSMIT_OPTION_AUTO_ROUTE  					0x04
234 #define TRANSMIT_OPTION_NO_ROUTE 						0x10
235 #define TRANSMIT_OPTION_EXPLORE							0x20
236 
237 #define TRANSMIT_COMPLETE_OK	  						0x00
238 #define TRANSMIT_COMPLETE_NO_ACK	  					0x01
239 #define TRANSMIT_COMPLETE_FAIL							0x02
240 #define TRANSMIT_COMPLETE_NOT_IDLE						0x03
241 #define TRANSMIT_COMPLETE_NOROUTE 						0x04
242 
243 #define RECEIVE_STATUS_ROUTED_BUSY						0x01
244 #define RECEIVE_STATUS_TYPE_BROAD	 					0x04
245 
246 #define FUNC_ID_SERIAL_API_GET_INIT_DATA				0x02
247 #define FUNC_ID_SERIAL_API_APPL_NODE_INFORMATION		0x03
248 #define FUNC_ID_APPLICATION_COMMAND_HANDLER				0x04
249 #define FUNC_ID_ZW_GET_CONTROLLER_CAPABILITIES			0x05
250 #define FUNC_ID_SERIAL_API_SET_TIMEOUTS 				0x06
251 #define FUNC_ID_SERIAL_API_GET_CAPABILITIES				0x07
252 #define FUNC_ID_SERIAL_API_SOFT_RESET					0x08
253 
254 #define FUNC_ID_ZW_SEND_NODE_INFORMATION				0x12
255 #define FUNC_ID_ZW_SEND_DATA							0x13
256 #define FUNC_ID_ZW_GET_VERSION							0x15
257 #define FUNC_ID_ZW_R_F_POWER_LEVEL_SET					0x17
258 #define FUNC_ID_ZW_GET_RANDOM							0x1c
259 #define FUNC_ID_ZW_MEMORY_GET_ID						0x20
260 #define FUNC_ID_MEMORY_GET_BYTE							0x21
261 #define FUNC_ID_ZW_READ_MEMORY							0x23
262 
263 #define FUNC_ID_ZW_SET_LEARN_NODE_STATE					0x40	// Not implemented
264 #define FUNC_ID_ZW_GET_NODE_PROTOCOL_INFO				0x41	// Get protocol info (baud rate, listening, etc.) for a given node
265 #define FUNC_ID_ZW_SET_DEFAULT							0x42	// Reset controller and node info to default (original) values
266 #define FUNC_ID_ZW_NEW_CONTROLLER						0x43	// Not implemented
267 #define FUNC_ID_ZW_REPLICATION_COMMAND_COMPLETE			0x44	// Replication send data complete
268 #define FUNC_ID_ZW_REPLICATION_SEND_DATA				0x45	// Replication send data
269 #define FUNC_ID_ZW_ASSIGN_RETURN_ROUTE					0x46	// Assign a return route from the specified node to the controller
270 #define FUNC_ID_ZW_DELETE_RETURN_ROUTE					0x47	// Delete all return routes from the specified node
271 #define FUNC_ID_ZW_REQUEST_NODE_NEIGHBOR_UPDATE			0x48	// Ask the specified node to update its neighbors (then read them from the controller)
272 #define FUNC_ID_ZW_APPLICATION_UPDATE					0x49	// Get a list of supported (and controller) command classes
273 #define FUNC_ID_ZW_ADD_NODE_TO_NETWORK					0x4a	// Control the addnode (or addcontroller) process...start, stop, etc.
274 #define FUNC_ID_ZW_REMOVE_NODE_FROM_NETWORK				0x4b	// Control the removenode (or removecontroller) process...start, stop, etc.
275 #define FUNC_ID_ZW_CREATE_NEW_PRIMARY					0x4c	// Control the createnewprimary process...start, stop, etc.
276 #define FUNC_ID_ZW_CONTROLLER_CHANGE					0x4d	// Control the transferprimary process...start, stop, etc.
277 #define FUNC_ID_ZW_SET_LEARN_MODE						0x50	// Put a controller into learn mode for replication/ receipt of configuration info
278 #define FUNC_ID_ZW_ASSIGN_SUC_RETURN_ROUTE				0x51	// Assign a return route to the SUC
279 #define FUNC_ID_ZW_ENABLE_SUC							0x52	// Make a controller a Static Update Controller
280 #define FUNC_ID_ZW_REQUEST_NETWORK_UPDATE				0x53	// Network update for a SUC(?)
281 #define FUNC_ID_ZW_SET_SUC_NODE_ID						0x54	// Identify a Static Update Controller node id
282 #define FUNC_ID_ZW_DELETE_SUC_RETURN_ROUTE				0x55	// Remove return routes to the SUC
283 #define FUNC_ID_ZW_GET_SUC_NODE_ID						0x56	// Try to retrieve a Static Update Controller node id (zero if no SUC present)
284 #define FUNC_ID_ZW_REQUEST_NODE_NEIGHBOR_UPDATE_OPTIONS	0x5a	// Allow options for request node neighbor update
285 #define FUNC_ID_ZW_EXPLORE_REQUEST_INCLUSION			0x5e	// supports NWI
286 #define FUNC_ID_ZW_REQUEST_NODE_INFO					0x60	// Get info (supported command classes) for the specified node
287 #define FUNC_ID_ZW_REMOVE_FAILED_NODE_ID				0x61	// Mark a specified node id as failed
288 #define FUNC_ID_ZW_IS_FAILED_NODE_ID					0x62	// Check to see if a specified node has failed
289 #define FUNC_ID_ZW_REPLACE_FAILED_NODE					0x63	// Remove a failed node from the controller's list (?)
290 #define FUNC_ID_ZW_GET_ROUTING_INFO						0x80	// Get a specified node's neighbor information from the controller
291 #define FUNC_ID_SERIAL_API_SLAVE_NODE_INFO				0xA0	// Set application virtual slave node information
292 #define FUNC_ID_APPLICATION_SLAVE_COMMAND_HANDLER		0xA1	// Slave command handler
293 #define FUNC_ID_ZW_SEND_SLAVE_NODE_INFO					0xA2	// Send a slave node information frame
294 #define FUNC_ID_ZW_SEND_SLAVE_DATA						0xA3	// Send data from slave
295 #define FUNC_ID_ZW_SET_SLAVE_LEARN_MODE					0xA4	// Enter slave learn mode
296 #define FUNC_ID_ZW_GET_VIRTUAL_NODES					0xA5	// Return all virtual nodes
297 #define FUNC_ID_ZW_IS_VIRTUAL_NODE						0xA6	// Virtual node test
298 #define FUNC_ID_ZW_SET_PROMISCUOUS_MODE					0xD0	// Set controller into promiscuous mode to listen to all frames
299 #define FUNC_ID_PROMISCUOUS_APPLICATION_COMMAND_HANDLER	0xD1
300 
301 #define ADD_NODE_ANY									0x01
302 #define ADD_NODE_CONTROLLER								0x02
303 #define ADD_NODE_SLAVE									0x03
304 #define ADD_NODE_EXISTING								0x04
305 #define ADD_NODE_STOP									0x05
306 #define ADD_NODE_STOP_FAILED							0x06
307 
308 #define ADD_NODE_STATUS_LEARN_READY						0x01
309 #define ADD_NODE_STATUS_NODE_FOUND						0x02
310 #define ADD_NODE_STATUS_ADDING_SLAVE	 				0x03
311 #define ADD_NODE_STATUS_ADDING_CONTROLLER				0x04
312 #define ADD_NODE_STATUS_PROTOCOL_DONE					0x05
313 #define ADD_NODE_STATUS_DONE							0x06
314 #define ADD_NODE_STATUS_FAILED							0x07
315 
316 #define REMOVE_NODE_ANY									0x01
317 #define REMOVE_NODE_CONTROLLER							0x02
318 #define REMOVE_NODE_SLAVE								0x03
319 #define REMOVE_NODE_STOP								0x05
320 
321 #define REMOVE_NODE_STATUS_LEARN_READY					0x01
322 #define REMOVE_NODE_STATUS_NODE_FOUND					0x02
323 #define REMOVE_NODE_STATUS_REMOVING_SLAVE				0x03
324 #define REMOVE_NODE_STATUS_REMOVING_CONTROLLER			0x04
325 #define REMOVE_NODE_STATUS_DONE							0x06
326 #define REMOVE_NODE_STATUS_FAILED						0x07
327 
328 #define CREATE_PRIMARY_START							0x02
329 #define CREATE_PRIMARY_STOP								0x05
330 #define CREATE_PRIMARY_STOP_FAILED						0x06
331 
332 #define CONTROLLER_CHANGE_START							0x02
333 #define CONTROLLER_CHANGE_STOP							0x05
334 #define CONTROLLER_CHANGE_STOP_FAILED					0x06
335 
336 #define LEARN_MODE_STARTED								0x01
337 #define LEARN_MODE_DONE									0x06
338 #define LEARN_MODE_FAILED								0x07
339 #define LEARN_MODE_DELETED								0x80
340 
341 #define REQUEST_NEIGHBOR_UPDATE_STARTED					0x21
342 #define REQUEST_NEIGHBOR_UPDATE_DONE					0x22
343 #define REQUEST_NEIGHBOR_UPDATE_FAILED					0x23
344 
345 #define FAILED_NODE_OK									0x00
346 #define FAILED_NODE_REMOVED								0x01
347 #define FAILED_NODE_NOT_REMOVED							0x02
348 
349 #define FAILED_NODE_REPLACE_WAITING						0x03
350 #define FAILED_NODE_REPLACE_DONE						0x04
351 #define FAILED_NODE_REPLACE_FAILED						0x05
352 
353 #define FAILED_NODE_REMOVE_STARTED						0x00
354 #define FAILED_NODE_NOT_PRIMARY_CONTROLLER				0x02
355 #define FAILED_NODE_NO_CALLBACK_FUNCTION				0x04
356 #define FAILED_NODE_NOT_FOUND							0x08
357 #define FAILED_NODE_REMOVE_PROCESS_BUSY					0x10
358 #define FAILED_NODE_REMOVE_FAIL							0x20
359 
360 #define SUC_UPDATE_DONE									0x00
361 #define SUC_UPDATE_ABORT								0x01
362 #define SUC_UPDATE_WAIT									0x02
363 #define SUC_UPDATE_DISABLED								0x03
364 #define SUC_UPDATE_OVERFLOW								0x04
365 
366 #define SUC_FUNC_BASIC_SUC								0x00
367 #define SUC_FUNC_NODEID_SERVER							0x01
368 
369 #define UPDATE_STATE_NODE_INFO_RECEIVED					0x84
370 #define UPDATE_STATE_NODE_INFO_REQ_DONE					0x82
371 #define UPDATE_STATE_NODE_INFO_REQ_FAILED				0x81
372 #define UPDATE_STATE_ROUTING_PENDING					0x80
373 #define UPDATE_STATE_NEW_ID_ASSIGNED					0x40
374 #define UPDATE_STATE_DELETE_DONE						0x20
375 #define UPDATE_STATE_SUC_ID								0x10
376 
377 #define APPLICATION_NODEINFO_LISTENING					0x01
378 #define APPLICATION_NODEINFO_OPTIONAL_FUNCTIONALITY		0x02
379 
380 #define SLAVE_ASSIGN_COMPLETE							0x00
381 #define SLAVE_ASSIGN_NODEID_DONE						0x01	// Node ID has been assigned
382 #define SLAVE_ASSIGN_RANGE_INFO_UPDATE					0x02	// Node is doing neighbor discovery
383 
384 #define SLAVE_LEARN_MODE_DISABLE						0x00	// disable add/remove virtual slave nodes
385 #define SLAVE_LEARN_MODE_ENABLE							0x01	// enable ability to include/exclude virtual slave nodes
386 #define SLAVE_LEARN_MODE_ADD							0x02	// add node directly but only if primary/inclusion controller
387 #define SLAVE_LEARN_MODE_REMOVE							0x03	// remove node directly but only if primary/inclusion controller
388 
389 #define OPTION_HIGH_POWER								0x80
390 #define OPTION_NWI										0x40	// NWI Inclusion
391 //Device request related
392 #define BASIC_SET										0x01
393 #define BASIC_REPORT									0x03
394 
395 #define COMMAND_CLASS_BASIC								0x20
396 #define	COMMAND_CLASS_CONTROLLER_REPLICATION			0x21
397 #define COMMAND_CLASS_APPLICATION_STATUS 				0x22
398 #define COMMAND_CLASS_HAIL								0x82
399 
400 #endif // _Defs_H
401