ansible4ozw 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. data/lib/ansible.rb +12 -9
  2. data/lib/ansible/ansible_device.rb +1 -1
  3. data/lib/ansible/ansible_value.rb +1 -1
  4. data/lib/ansible/config.rb +8 -5
  5. data/lib/ansible/devices.rb +1 -0
  6. data/lib/ansible/knx.rb +0 -0
  7. data/lib/ansible/knx/dpt/tests/test_dpt10.rb~ +45 -0
  8. data/lib/ansible/knx/dpt/tests/test_dpt9.rb~ +55 -0
  9. data/lib/ansible/transceiver.rb +0 -3
  10. data/lib/ansible/version.rb +3 -0
  11. data/lib/ansible/zwave.rb +1 -0
  12. data/lib/ansible/{openzwave → zwave/openzwave}/ozw_constants.rb +1 -2
  13. data/lib/ansible/{openzwave → zwave/openzwave}/ozw_headers.rb +2 -6
  14. data/lib/ansible/{openzwave → zwave/openzwave}/ozw_remote_manager.rb +0 -3
  15. data/lib/ansible/{openzwave → zwave/openzwave}/ozw_types.rb +0 -0
  16. data/lib/ansible/zwave/openzwave/src/Driver.h +695 -0
  17. data/lib/ansible/zwave/openzwave/src/Notification.h +165 -0
  18. data/lib/ansible/zwave/openzwave/src/command_classes/Alarm.cpp +142 -0
  19. data/lib/ansible/zwave/openzwave/src/command_classes/Alarm.h +69 -0
  20. data/lib/ansible/zwave/openzwave/src/command_classes/ApplicationStatus.cpp +93 -0
  21. data/lib/ansible/zwave/openzwave/src/command_classes/ApplicationStatus.h +57 -0
  22. data/lib/ansible/zwave/openzwave/src/command_classes/Association.cpp +385 -0
  23. data/lib/ansible/zwave/openzwave/src/command_classes/Association.h +73 -0
  24. data/lib/ansible/zwave/openzwave/src/command_classes/AssociationCommandConfiguration.cpp +277 -0
  25. data/lib/ansible/zwave/openzwave/src/command_classes/AssociationCommandConfiguration.h +70 -0
  26. data/lib/ansible/zwave/openzwave/src/command_classes/Basic.cpp +212 -0
  27. data/lib/ansible/zwave/openzwave/src/command_classes/Basic.h +72 -0
  28. data/lib/ansible/zwave/openzwave/src/command_classes/BasicWindowCovering.cpp +118 -0
  29. data/lib/ansible/zwave/openzwave/src/command_classes/BasicWindowCovering.h +64 -0
  30. data/lib/ansible/zwave/openzwave/src/command_classes/Battery.cpp +141 -0
  31. data/lib/ansible/zwave/openzwave/src/command_classes/Battery.h +65 -0
  32. data/lib/ansible/zwave/openzwave/src/command_classes/ClimateControlSchedule.cpp +415 -0
  33. data/lib/ansible/zwave/openzwave/src/command_classes/ClimateControlSchedule.h +71 -0
  34. data/lib/ansible/zwave/openzwave/src/command_classes/Clock.cpp +227 -0
  35. data/lib/ansible/zwave/openzwave/src/command_classes/Clock.h +67 -0
  36. data/lib/ansible/zwave/openzwave/src/command_classes/CommandClass.cpp +546 -0
  37. data/lib/ansible/zwave/openzwave/src/command_classes/CommandClass.h +145 -0
  38. data/lib/ansible/zwave/openzwave/src/command_classes/CommandClasses.cpp +291 -0
  39. data/lib/ansible/zwave/openzwave/src/command_classes/CommandClasses.h +79 -0
  40. data/lib/ansible/zwave/openzwave/src/command_classes/Configuration.cpp +258 -0
  41. data/lib/ansible/zwave/openzwave/src/command_classes/Configuration.h +67 -0
  42. data/lib/ansible/zwave/openzwave/src/command_classes/ControllerReplication.cpp +142 -0
  43. data/lib/ansible/zwave/openzwave/src/command_classes/ControllerReplication.h +63 -0
  44. data/lib/ansible/zwave/openzwave/src/command_classes/EnergyProduction.cpp +163 -0
  45. data/lib/ansible/zwave/openzwave/src/command_classes/EnergyProduction.h +65 -0
  46. data/lib/ansible/zwave/openzwave/src/command_classes/Hail.cpp +68 -0
  47. data/lib/ansible/zwave/openzwave/src/command_classes/Hail.h +58 -0
  48. data/lib/ansible/zwave/openzwave/src/command_classes/Indicator.cpp +159 -0
  49. data/lib/ansible/zwave/openzwave/src/command_classes/Indicator.h +66 -0
  50. data/lib/ansible/zwave/openzwave/src/command_classes/Language.cpp +159 -0
  51. data/lib/ansible/zwave/openzwave/src/command_classes/Language.h +65 -0
  52. data/lib/ansible/zwave/openzwave/src/command_classes/Lock.cpp +159 -0
  53. data/lib/ansible/zwave/openzwave/src/command_classes/Lock.h +66 -0
  54. data/lib/ansible/zwave/openzwave/src/command_classes/ManufacturerSpecific.cpp +439 -0
  55. data/lib/ansible/zwave/openzwave/src/command_classes/ManufacturerSpecific.h +116 -0
  56. data/lib/ansible/zwave/openzwave/src/command_classes/Meter.cpp +541 -0
  57. data/lib/ansible/zwave/openzwave/src/command_classes/Meter.h +73 -0
  58. data/lib/ansible/zwave/openzwave/src/command_classes/MeterPulse.cpp +137 -0
  59. data/lib/ansible/zwave/openzwave/src/command_classes/MeterPulse.h +65 -0
  60. data/lib/ansible/zwave/openzwave/src/command_classes/MultiCmd.cpp +77 -0
  61. data/lib/ansible/zwave/openzwave/src/command_classes/MultiCmd.h +63 -0
  62. data/lib/ansible/zwave/openzwave/src/command_classes/MultiInstance.cpp +568 -0
  63. data/lib/ansible/zwave/openzwave/src/command_classes/MultiInstance.h +96 -0
  64. data/lib/ansible/zwave/openzwave/src/command_classes/MultiInstanceAssociation.cpp +61 -0
  65. data/lib/ansible/zwave/openzwave/src/command_classes/MultiInstanceAssociation.h +59 -0
  66. data/lib/ansible/zwave/openzwave/src/command_classes/NodeNaming.cpp +525 -0
  67. data/lib/ansible/zwave/openzwave/src/command_classes/NodeNaming.h +66 -0
  68. data/lib/ansible/zwave/openzwave/src/command_classes/Powerlevel.cpp +157 -0
  69. data/lib/ansible/zwave/openzwave/src/command_classes/Powerlevel.h +82 -0
  70. data/lib/ansible/zwave/openzwave/src/command_classes/Proprietary.cpp +62 -0
  71. data/lib/ansible/zwave/openzwave/src/command_classes/Proprietary.h +58 -0
  72. data/lib/ansible/zwave/openzwave/src/command_classes/Protection.cpp +177 -0
  73. data/lib/ansible/zwave/openzwave/src/command_classes/Protection.h +73 -0
  74. data/lib/ansible/zwave/openzwave/src/command_classes/SensorAlarm.cpp +208 -0
  75. data/lib/ansible/zwave/openzwave/src/command_classes/SensorAlarm.h +77 -0
  76. data/lib/ansible/zwave/openzwave/src/command_classes/SensorBinary.cpp +134 -0
  77. data/lib/ansible/zwave/openzwave/src/command_classes/SensorBinary.h +65 -0
  78. data/lib/ansible/zwave/openzwave/src/command_classes/SensorMultilevel.cpp +237 -0
  79. data/lib/ansible/zwave/openzwave/src/command_classes/SensorMultilevel.h +71 -0
  80. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchAll.cpp +219 -0
  81. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchAll.h +69 -0
  82. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchBinary.cpp +157 -0
  83. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchBinary.h +66 -0
  84. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchMultilevel.cpp +559 -0
  85. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchMultilevel.h +83 -0
  86. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchToggleBinary.cpp +151 -0
  87. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchToggleBinary.h +66 -0
  88. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchToggleMultilevel.cpp +195 -0
  89. data/lib/ansible/zwave/openzwave/src/command_classes/SwitchToggleMultilevel.h +75 -0
  90. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatFanMode.cpp +332 -0
  91. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatFanMode.h +77 -0
  92. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatFanState.cpp +149 -0
  93. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatFanState.h +71 -0
  94. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatMode.cpp +373 -0
  95. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatMode.h +76 -0
  96. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatOperatingState.cpp +147 -0
  97. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatOperatingState.h +71 -0
  98. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatSetpoint.cpp +317 -0
  99. data/lib/ansible/zwave/openzwave/src/command_classes/ThermostatSetpoint.h +73 -0
  100. data/lib/ansible/zwave/openzwave/src/command_classes/Version.cpp +255 -0
  101. data/lib/ansible/zwave/openzwave/src/command_classes/Version.h +71 -0
  102. data/lib/ansible/zwave/openzwave/src/command_classes/WakeUp.cpp +391 -0
  103. data/lib/ansible/zwave/openzwave/src/command_classes/WakeUp.h +85 -0
  104. data/lib/ansible/zwave/openzwave/src/platform/Controller.cpp +68 -0
  105. data/lib/ansible/zwave/openzwave/src/platform/Controller.h +106 -0
  106. data/lib/ansible/zwave/openzwave/src/platform/Event.cpp +102 -0
  107. data/lib/ansible/zwave/openzwave/src/platform/Event.h +91 -0
  108. data/lib/ansible/zwave/openzwave/src/platform/HidController.cpp +539 -0
  109. data/lib/ansible/zwave/openzwave/src/platform/HidController.h +147 -0
  110. data/lib/ansible/zwave/openzwave/src/platform/Log.cpp +310 -0
  111. data/lib/ansible/zwave/openzwave/src/platform/Log.h +184 -0
  112. data/lib/ansible/zwave/openzwave/src/platform/Mutex.cpp +96 -0
  113. data/lib/ansible/zwave/openzwave/src/platform/Mutex.h +88 -0
  114. data/lib/ansible/zwave/openzwave/src/platform/Ref.h +91 -0
  115. data/lib/ansible/zwave/openzwave/src/platform/SerialController.cpp +180 -0
  116. data/lib/ansible/zwave/openzwave/src/platform/SerialController.h +138 -0
  117. data/lib/ansible/zwave/openzwave/src/platform/Stream.cpp +229 -0
  118. data/lib/ansible/zwave/openzwave/src/platform/Stream.h +135 -0
  119. data/lib/ansible/zwave/openzwave/src/platform/Thread.cpp +118 -0
  120. data/lib/ansible/zwave/openzwave/src/platform/Thread.h +101 -0
  121. data/lib/ansible/zwave/openzwave/src/platform/TimeStamp.cpp +92 -0
  122. data/lib/ansible/zwave/openzwave/src/platform/TimeStamp.h +85 -0
  123. data/lib/ansible/zwave/openzwave/src/platform/Wait.cpp +176 -0
  124. data/lib/ansible/zwave/openzwave/src/platform/Wait.h +113 -0
  125. data/lib/ansible/zwave/openzwave/src/platform/unix/EventImpl.cpp +262 -0
  126. data/lib/ansible/zwave/openzwave/src/platform/unix/EventImpl.h +62 -0
  127. data/lib/ansible/zwave/openzwave/src/platform/unix/LogImpl.cpp +304 -0
  128. data/lib/ansible/zwave/openzwave/src/platform/unix/LogImpl.h +70 -0
  129. data/lib/ansible/zwave/openzwave/src/platform/unix/MutexImpl.cpp +136 -0
  130. data/lib/ansible/zwave/openzwave/src/platform/unix/MutexImpl.h +56 -0
  131. data/lib/ansible/zwave/openzwave/src/platform/unix/SerialControllerImpl.cpp +368 -0
  132. data/lib/ansible/zwave/openzwave/src/platform/unix/SerialControllerImpl.h +74 -0
  133. data/lib/ansible/zwave/openzwave/src/platform/unix/ThreadImpl.cpp +181 -0
  134. data/lib/ansible/zwave/openzwave/src/platform/unix/ThreadImpl.h +67 -0
  135. data/lib/ansible/zwave/openzwave/src/platform/unix/TimeStampImpl.cpp +121 -0
  136. data/lib/ansible/zwave/openzwave/src/platform/unix/TimeStampImpl.h +84 -0
  137. data/lib/ansible/zwave/openzwave/src/platform/unix/WaitImpl.cpp +160 -0
  138. data/lib/ansible/zwave/openzwave/src/platform/unix/WaitImpl.h +74 -0
  139. data/lib/ansible/zwave/openzwave/src/platform/windows/EventImpl.cpp +102 -0
  140. data/lib/ansible/zwave/openzwave/src/platform/windows/EventImpl.h +60 -0
  141. data/lib/ansible/zwave/openzwave/src/platform/windows/LogImpl.cpp +302 -0
  142. data/lib/ansible/zwave/openzwave/src/platform/windows/LogImpl.h +71 -0
  143. data/lib/ansible/zwave/openzwave/src/platform/windows/MutexImpl.cpp +114 -0
  144. data/lib/ansible/zwave/openzwave/src/platform/windows/MutexImpl.h +58 -0
  145. data/lib/ansible/zwave/openzwave/src/platform/windows/SerialControllerImpl.cpp +410 -0
  146. data/lib/ansible/zwave/openzwave/src/platform/windows/SerialControllerImpl.h +66 -0
  147. data/lib/ansible/zwave/openzwave/src/platform/windows/ThreadImpl.cpp +158 -0
  148. data/lib/ansible/zwave/openzwave/src/platform/windows/ThreadImpl.h +71 -0
  149. data/lib/ansible/zwave/openzwave/src/platform/windows/TimeStampImpl.cpp +95 -0
  150. data/lib/ansible/zwave/openzwave/src/platform/windows/TimeStampImpl.h +83 -0
  151. data/lib/ansible/zwave/openzwave/src/platform/windows/WaitImpl.cpp +134 -0
  152. data/lib/ansible/zwave/openzwave/src/platform/windows/WaitImpl.h +73 -0
  153. data/lib/ansible/zwave/openzwave/src/value_classes/ValueID.h +283 -0
  154. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_bool.rb +0 -0
  155. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_button.rb +0 -0
  156. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_byte.rb +0 -0
  157. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_decimal.rb +0 -0
  158. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_int.rb +0 -0
  159. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_list.rb +0 -0
  160. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_short.rb +0 -0
  161. data/lib/ansible/zwave/{types → openzwave/types}/valuetype_string.rb +0 -0
  162. data/lib/ansible/zwave/zwave_transceiver.rb +2 -8
  163. data/lib/ansible/zwave/zwave_value.rb +6 -10
  164. metadata +159 -17
  165. data/lib/ansible/zwave/zwave_node.rb +0 -5
@@ -0,0 +1,71 @@
1
+ //-----------------------------------------------------------------------------
2
+ //
3
+ // LogImpl.h
4
+ //
5
+ // Windows implementation of message and error logging
6
+ //
7
+ // Copyright (c) 2010 Mal Lansell <mal@lansell.org>
8
+ // All rights reserved.
9
+ //
10
+ // SOFTWARE NOTICE AND LICENSE
11
+ //
12
+ // This file is part of OpenZWave.
13
+ //
14
+ // OpenZWave is free software: you can redistribute it and/or modify
15
+ // it under the terms of the GNU Lesser General Public License as published
16
+ // by the Free Software Foundation, either version 3 of the License,
17
+ // or (at your option) any later version.
18
+ //
19
+ // OpenZWave is distributed in the hope that it will be useful,
20
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ // GNU Lesser General Public License for more details.
23
+ //
24
+ // You should have received a copy of the GNU Lesser General Public License
25
+ // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
26
+ //
27
+ //-----------------------------------------------------------------------------
28
+ #ifndef _LogImpl_H
29
+ #define _LogImpl_H
30
+
31
+ #include "Defs.h"
32
+ #include <string>
33
+ #include "Log.h"
34
+ #include "Windows.h"
35
+
36
+ namespace OpenZWave
37
+ {
38
+ /** \brief Windows-specific implementation of the Log class.
39
+ */
40
+ class LogImpl : public i_LogImpl
41
+ {
42
+ private:
43
+ friend class Log;
44
+
45
+ LogImpl( string const& _filename, bool const _bAppendLog, bool const _bConsoleOutput, LogLevel const _saveLevel, LogLevel const _queueLevel, LogLevel const _dumpTrigger );
46
+ ~LogImpl();
47
+
48
+ void Write( LogLevel _level, uint8 const _nodeId, char const* _format, va_list _args );
49
+ void Queue( char const* _buffer );
50
+ void QueueDump();
51
+ void QueueClear();
52
+ void SetLoggingState( LogLevel _saveLevel, LogLevel _queueLevel, LogLevel _dumpTrigger );
53
+ void SetLogFileName( string _filename );
54
+
55
+ string GetTimeStampString();
56
+ string GetNodeString( uint8 const _nodeId );
57
+ string GetThreadId();
58
+
59
+ string m_filename; /**< filename specified by user (default is ozw_log.txt) */
60
+ bool m_bConsoleOutput; /**< if true, send log output to console as well as to the file */
61
+ bool m_bAppendLog; /**< if true, the log file should be appended to any with the same name */
62
+ list<string> m_logQueue; /**< list of queued log messages */
63
+ LogLevel m_saveLevel;
64
+ LogLevel m_queueLevel;
65
+ LogLevel m_dumpTrigger;
66
+ };
67
+
68
+ } // namespace OpenZWave
69
+
70
+ #endif //_LogImpl_H
71
+
@@ -0,0 +1,114 @@
1
+ //-----------------------------------------------------------------------------
2
+ //
3
+ // MutexImpl.cpp
4
+ //
5
+ // Windows Implementation of the cross-platform mutex
6
+ //
7
+ // Copyright (c) 2010 Mal Lansell <mal@lansell.org>
8
+ // All rights reserved.
9
+ //
10
+ // SOFTWARE NOTICE AND LICENSE
11
+ //
12
+ // This file is part of OpenZWave.
13
+ //
14
+ // OpenZWave is free software: you can redistribute it and/or modify
15
+ // it under the terms of the GNU Lesser General Public License as published
16
+ // by the Free Software Foundation, either version 3 of the License,
17
+ // or (at your option) any later version.
18
+ //
19
+ // OpenZWave is distributed in the hope that it will be useful,
20
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ // GNU Lesser General Public License for more details.
23
+ //
24
+ // You should have received a copy of the GNU Lesser General Public License
25
+ // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
26
+ //
27
+ //-----------------------------------------------------------------------------
28
+ #include "Defs.h"
29
+ #include "MutexImpl.h"
30
+
31
+
32
+ using namespace OpenZWave;
33
+
34
+ //-----------------------------------------------------------------------------
35
+ // <MutexImpl::MutexImpl>
36
+ // Constructor
37
+ //-----------------------------------------------------------------------------
38
+ MutexImpl::MutexImpl
39
+ (
40
+ ):
41
+ m_lockCount( 0 )
42
+ {
43
+ InitializeCriticalSection( &m_criticalSection );
44
+ }
45
+
46
+ //-----------------------------------------------------------------------------
47
+ // <MutexImpl::~MutexImpl>
48
+ // Destructor
49
+ //-----------------------------------------------------------------------------
50
+ MutexImpl::~MutexImpl
51
+ (
52
+ )
53
+ {
54
+ DeleteCriticalSection( &m_criticalSection );
55
+ }
56
+
57
+ //-----------------------------------------------------------------------------
58
+ // <MutexImpl::Lock>
59
+ // Lock the mutex
60
+ //-----------------------------------------------------------------------------
61
+ bool MutexImpl::Lock
62
+ (
63
+ bool const _bWait // = true;
64
+ )
65
+ {
66
+ if( _bWait )
67
+ {
68
+ // We will wait for the lock
69
+ EnterCriticalSection( &m_criticalSection );
70
+ ++m_lockCount;
71
+ return true;
72
+ }
73
+
74
+ // Returns immediately, even if the lock was not available.
75
+ if( TryEnterCriticalSection( &m_criticalSection ) )
76
+ {
77
+ ++m_lockCount;
78
+ return true;
79
+ }
80
+
81
+ return false;
82
+ }
83
+
84
+ //-----------------------------------------------------------------------------
85
+ // <MutexImpl::Unlock>
86
+ // Release our lock on the mutex
87
+ //-----------------------------------------------------------------------------
88
+ void MutexImpl::Unlock
89
+ (
90
+ )
91
+ {
92
+ if( !m_lockCount )
93
+ {
94
+ // No locks - we have a mismatched lock/release pair
95
+ assert(0);
96
+ }
97
+ else
98
+ {
99
+ --m_lockCount;
100
+ LeaveCriticalSection( &m_criticalSection );
101
+ }
102
+ }
103
+
104
+ //-----------------------------------------------------------------------------
105
+ // <MutexImpl::IsSignalled>
106
+ // Test whether the mutex is free
107
+ //-----------------------------------------------------------------------------
108
+ bool MutexImpl::IsSignalled
109
+ (
110
+ )
111
+ {
112
+ return( 0 == m_lockCount );
113
+ }
114
+
@@ -0,0 +1,58 @@
1
+ //-----------------------------------------------------------------------------
2
+ //
3
+ // MutexImpl.h
4
+ //
5
+ // Windows Implementation of the cross-platform mutex
6
+ //
7
+ // Copyright (c) 2010 Mal Lansell <mal@lansell.org>
8
+ // All rights reserved.
9
+ //
10
+ // SOFTWARE NOTICE AND LICENSE
11
+ //
12
+ // This file is part of OpenZWave.
13
+ //
14
+ // OpenZWave is free software: you can redistribute it and/or modify
15
+ // it under the terms of the GNU Lesser General Public License as published
16
+ // by the Free Software Foundation, either version 3 of the License,
17
+ // or (at your option) any later version.
18
+ //
19
+ // OpenZWave is distributed in the hope that it will be useful,
20
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ // GNU Lesser General Public License for more details.
23
+ //
24
+ // You should have received a copy of the GNU Lesser General Public License
25
+ // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
26
+ //
27
+ //-----------------------------------------------------------------------------
28
+ #ifndef _MutexImpl_H
29
+ #define _MutexImpl_H
30
+
31
+ #include <windows.h>
32
+
33
+
34
+ namespace OpenZWave
35
+ {
36
+ /** \brief Windows-specific implementation of the Mutex class.
37
+ */
38
+ class MutexImpl
39
+ {
40
+ private:
41
+ friend class Mutex;
42
+
43
+ MutexImpl();
44
+ ~MutexImpl();
45
+
46
+ bool Lock( bool const _bWait = true );
47
+ void Unlock();
48
+
49
+ bool IsSignalled();
50
+
51
+ CRITICAL_SECTION m_criticalSection;
52
+ uint32 m_lockCount; // Keep track of the locks (there can be more than one if they occur on the same thread.
53
+ };
54
+
55
+ } // namespace OpenZWave
56
+
57
+ #endif //_MutexIF_H
58
+
@@ -0,0 +1,410 @@
1
+ //-----------------------------------------------------------------------------
2
+ //
3
+ // SerialControllerImpl.cpp
4
+ //
5
+ // Windows Implementation of the cross-platform serial port
6
+ //
7
+ // Copyright (c) 2010 Jason Frazier <frazierjason@gmail.com>
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
+ #include "Defs.h"
29
+ #include "SerialControllerImpl.h"
30
+
31
+ #include "Log.h"
32
+
33
+ using namespace OpenZWave;
34
+
35
+ DWORD WINAPI SerialReadThreadEntryPoint( void* _context );
36
+
37
+ //-----------------------------------------------------------------------------
38
+ // <SerialControllerImpl::SerialControllerImpl>
39
+ // Constructor
40
+ //-----------------------------------------------------------------------------
41
+ SerialControllerImpl::SerialControllerImpl
42
+ (
43
+ SerialController* _owner
44
+ ):
45
+ m_owner( _owner )
46
+ {
47
+ }
48
+
49
+ //-----------------------------------------------------------------------------
50
+ // <SerialControllerImpl::~SerialControllerImpl>
51
+ // Destructor
52
+ //-----------------------------------------------------------------------------
53
+ SerialControllerImpl::~SerialControllerImpl
54
+ (
55
+ )
56
+ {
57
+ CloseHandle( m_hSerialController );
58
+ }
59
+
60
+ //-----------------------------------------------------------------------------
61
+ // <SerialControllerImpl::Open>
62
+ // Open the serial port
63
+ //-----------------------------------------------------------------------------
64
+ bool SerialControllerImpl::Open
65
+ (
66
+ )
67
+ {
68
+ // Try to init the serial port
69
+ if( !Init( 1 ) )
70
+ {
71
+ // Failed. We bail to allow the app a chance to take over, rather than retry
72
+ // automatically. Automatic retries only occur after a successful init.
73
+ return false;
74
+ }
75
+
76
+ // Create an event to trigger exiting the read thread
77
+ m_hExit = ::CreateEvent( NULL, TRUE, FALSE, NULL );
78
+
79
+ // Start the read thread
80
+ m_hThread = ::CreateThread( NULL, 0, SerialReadThreadEntryPoint, this, CREATE_SUSPENDED, NULL );
81
+ ::ResumeThread( m_hThread );
82
+
83
+ return true;
84
+ }
85
+
86
+ //-----------------------------------------------------------------------------
87
+ // <SerialControllerImpl::Close>
88
+ // Close the serial port
89
+ //-----------------------------------------------------------------------------
90
+ void SerialControllerImpl::Close
91
+ (
92
+ )
93
+ {
94
+ ::SetEvent( m_hExit );
95
+ ::WaitForSingleObject( m_hThread, INFINITE );
96
+
97
+ CloseHandle( m_hThread );
98
+ m_hThread = INVALID_HANDLE_VALUE;
99
+
100
+ CloseHandle( m_hExit );
101
+ m_hExit = INVALID_HANDLE_VALUE;
102
+
103
+ CloseHandle( m_hSerialController );
104
+ m_hSerialController = INVALID_HANDLE_VALUE;
105
+ }
106
+
107
+ //-----------------------------------------------------------------------------
108
+ // <SerialReadThreadEntryPoint>
109
+ // Entry point of the thread for receiving data from the serial port
110
+ //-----------------------------------------------------------------------------
111
+ DWORD WINAPI SerialReadThreadEntryPoint
112
+ (
113
+ void* _context
114
+ )
115
+ {
116
+ SerialControllerImpl* impl = (SerialControllerImpl*)_context;
117
+ if( impl )
118
+ {
119
+ impl->ReadThreadProc();
120
+ }
121
+
122
+ return 0;
123
+ }
124
+
125
+ //-----------------------------------------------------------------------------
126
+ // <SerialControllerImpl::ReadThreadProc>
127
+ // Handle receiving data
128
+ //-----------------------------------------------------------------------------
129
+ void SerialControllerImpl::ReadThreadProc
130
+ (
131
+ )
132
+ {
133
+ uint32 attempts = 0;
134
+ while( true )
135
+ {
136
+ // Init must have been called successfully during Open, so we
137
+ // don't do it again until the end of the loop
138
+ if( INVALID_HANDLE_VALUE != m_hSerialController )
139
+ {
140
+ // Enter read loop. Call will only return if
141
+ // an exit is requested or an error occurs
142
+ Read();
143
+
144
+ // Reset the attempts, so we get a rapid retry for temporary errors
145
+ attempts = 0;
146
+ }
147
+
148
+ if( attempts < 25 )
149
+ {
150
+ // Retry every 5 seconds for the first two minutes...
151
+ if( WAIT_OBJECT_0 == ::WaitForSingleObject( m_hExit, 5000 ) )
152
+ {
153
+ // Exit signalled.
154
+ break;
155
+ }
156
+ }
157
+ else
158
+ {
159
+ // ...retry every 30 seconds after that
160
+ if( WAIT_OBJECT_0 == ::WaitForSingleObject( m_hExit, 30000 ) )
161
+ {
162
+ // Exit signalled.
163
+ break;
164
+ }
165
+ }
166
+
167
+ Init( ++attempts );
168
+ }
169
+ }
170
+
171
+ //-----------------------------------------------------------------------------
172
+ // <SerialControllerImpl::Init>
173
+ // Initialize the serial port
174
+ //-----------------------------------------------------------------------------
175
+ bool SerialControllerImpl::Init
176
+ (
177
+ uint32 const _attempts
178
+ )
179
+ {
180
+ Log::Write( LogLevel_Info, " Trying to open serial port %s (Attempt %d)", m_owner->m_serialControllerName.c_str(), _attempts );
181
+
182
+ m_hSerialController = CreateFile( m_owner->m_serialControllerName.c_str(),
183
+ GENERIC_READ | GENERIC_WRITE,
184
+ 0,
185
+ NULL,
186
+ OPEN_EXISTING,
187
+ FILE_FLAG_OVERLAPPED,
188
+ NULL );
189
+
190
+ if( INVALID_HANDLE_VALUE == m_hSerialController )
191
+ {
192
+ //Error
193
+ Log::Write( LogLevel_Error, "ERROR: Cannot open serial port %s. Error code %d\n", m_owner->m_serialControllerName.c_str(), GetLastError() );
194
+ goto SerialOpenFailure;
195
+ }
196
+
197
+ // Configure the serial device parameters
198
+ // Build on the current configuration
199
+ DCB dcb;
200
+ if( !GetCommState( m_hSerialController, &dcb ) )
201
+ {
202
+ //Error. Clean up and exit
203
+ Log::Write( LogLevel_Error, "ERROR: Failed to read serial port state" );
204
+ goto SerialOpenFailure;
205
+ }
206
+
207
+ // Fill in the Device Control Block
208
+ dcb.BaudRate = (DWORD)m_owner->m_baud;
209
+ dcb.ByteSize = 8;
210
+ dcb.Parity = (BYTE)m_owner->m_parity;
211
+ dcb.StopBits = (BYTE)m_owner->m_stopBits;
212
+
213
+ if( !SetCommState( m_hSerialController, &dcb) )
214
+ {
215
+ //Error. Clean up and exit
216
+ Log::Write( LogLevel_Error, "ERROR: Failed to set serial port state" );
217
+ goto SerialOpenFailure;
218
+ }
219
+
220
+ // Set the timeouts for the serial port
221
+ COMMTIMEOUTS commTimeouts;
222
+ commTimeouts.ReadIntervalTimeout = MAXDWORD;
223
+ commTimeouts.ReadTotalTimeoutConstant = 0;
224
+ commTimeouts.ReadTotalTimeoutMultiplier = 0;
225
+ commTimeouts.WriteTotalTimeoutConstant = 0;
226
+ commTimeouts.WriteTotalTimeoutMultiplier = 0;
227
+ if( !SetCommTimeouts( m_hSerialController, &commTimeouts ) )
228
+ {
229
+ // Error. Clean up and exit
230
+ Log::Write( LogLevel_Error, "ERROR: Failed to set serial port timeouts" );
231
+ goto SerialOpenFailure;
232
+ }
233
+
234
+ // Set the serial port to signal when data is received
235
+ if( !SetCommMask( m_hSerialController, EV_RXCHAR ) )
236
+ {
237
+ //Error. Clean up and exit
238
+ Log::Write( LogLevel_Info, "ERROR: Failed to set serial port mask" );
239
+ goto SerialOpenFailure;
240
+ }
241
+
242
+ // Clear any residual data from the serial port
243
+ PurgeComm( m_hSerialController, PURGE_RXABORT|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_TXCLEAR );
244
+
245
+ // Open successful
246
+ Log::Write( LogLevel_Info, " Serial port %s opened (attempt %d)", m_owner->m_serialControllerName.c_str(), _attempts );
247
+ return true;
248
+
249
+ SerialOpenFailure:
250
+ Log::Write( LogLevel_Info, "ERROR: Failed to open serial port %s (attempt %d)", m_owner->m_serialControllerName.c_str(), _attempts );
251
+ CloseHandle( m_hSerialController );
252
+ m_hSerialController = INVALID_HANDLE_VALUE;
253
+ return false;
254
+ }
255
+
256
+ //-----------------------------------------------------------------------------
257
+ // <SerialControllerImpl::Read>
258
+ // Read data from the serial port
259
+ //-----------------------------------------------------------------------------
260
+ void SerialControllerImpl::Read
261
+ (
262
+ )
263
+ {
264
+ uint8 buffer[256];
265
+
266
+ OVERLAPPED overlapped;
267
+ memset( &overlapped, 0, sizeof(overlapped) );
268
+ overlapped.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
269
+
270
+ while( true )
271
+ {
272
+ // Try to read all available data from the serial port
273
+ DWORD bytesRead = 0;
274
+ do
275
+ {
276
+ if( ::ReadFile( m_hSerialController, buffer, 256, NULL, &overlapped ) )
277
+ {
278
+ // Read completed
279
+ GetOverlappedResult( m_hSerialController, &overlapped, &bytesRead, TRUE );
280
+
281
+ // Copy to the stream buffer
282
+ if( bytesRead > 0 )
283
+ m_owner->Put( buffer, bytesRead );
284
+ }
285
+ else
286
+ {
287
+ //Wait for the read to complete
288
+ if( ERROR_IO_PENDING == GetLastError() )
289
+ {
290
+ // Wait for the read to complete or the
291
+ // signal that this thread should exit.
292
+ HANDLE handles[2];
293
+ handles[0] = overlapped.hEvent;
294
+ handles[1] = m_hExit;
295
+ DWORD res = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
296
+
297
+ if( (WAIT_OBJECT_0+1) == res )
298
+ {
299
+ // Exit signalled. Cancel the read.
300
+ goto exitRead;
301
+ }
302
+
303
+ if( WAIT_TIMEOUT == res )
304
+ {
305
+ // Timed out - should never happen
306
+ goto exitRead;
307
+ }
308
+
309
+ // Read completed
310
+ GetOverlappedResult( m_hSerialController, &overlapped, &bytesRead, TRUE );
311
+
312
+ // Copy to the stream buffer
313
+ if( bytesRead > 0 )
314
+ m_owner->Put( buffer, bytesRead );
315
+ }
316
+ else
317
+ {
318
+ // An error has occurred
319
+ goto exitRead;
320
+ }
321
+ }
322
+ }
323
+ while( bytesRead > 0 );
324
+
325
+ // Clear the event
326
+ ResetEvent( overlapped.hEvent );
327
+
328
+ // Nothing available to read, so wait for the next rx char event
329
+ DWORD dwEvtMask;
330
+ if( !WaitCommEvent( m_hSerialController, &dwEvtMask, &overlapped ) )
331
+ {
332
+ if( ERROR_IO_PENDING == GetLastError() )
333
+ {
334
+ // Wait for either some data to arrive or
335
+ // the signal that this thread should exit.
336
+ HANDLE handles[2];
337
+ handles[0] = overlapped.hEvent;
338
+ handles[1] = m_hExit;
339
+
340
+ DWORD res = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
341
+
342
+ if( (WAIT_OBJECT_0+1) == res )
343
+ {
344
+ // Exit signalled. Prevent WaitCommEvent from corrupting the
345
+ // stack by forcing it to exit.
346
+ goto exitRead;
347
+ }
348
+
349
+ if( WAIT_TIMEOUT == res )
350
+ {
351
+ // Timed out - should never happen
352
+ goto exitRead;
353
+ }
354
+
355
+ GetOverlappedResult( m_hSerialController, &overlapped, &bytesRead, TRUE );
356
+ }
357
+ }
358
+
359
+ // Clear the event
360
+ ResetEvent( overlapped.hEvent );
361
+
362
+ // Loop back to the top to read the waiting data
363
+ }
364
+
365
+ exitRead:
366
+ // Exit event has been signalled, or an error has occurred
367
+ SetCommMask( m_hSerialController, 0 );
368
+ CancelIo( m_hSerialController );
369
+ CloseHandle( overlapped.hEvent );
370
+ }
371
+
372
+ //-----------------------------------------------------------------------------
373
+ // <SerialControllerImpl::Write>
374
+ // Send data to the serial port
375
+ //-----------------------------------------------------------------------------
376
+ uint32 SerialControllerImpl::Write
377
+ (
378
+ uint8* _buffer,
379
+ uint32 _length
380
+ )
381
+ {
382
+ if( INVALID_HANDLE_VALUE == m_hSerialController )
383
+ {
384
+ //Error
385
+ Log::Write( LogLevel_Error, "ERROR: Serial port must be opened before writing\n" );
386
+ return 0;
387
+ }
388
+
389
+ // Write the data
390
+ OVERLAPPED overlapped;
391
+ memset( &overlapped, 0, sizeof(overlapped) );
392
+ overlapped.hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
393
+
394
+ DWORD bytesWritten;
395
+ if( !::WriteFile( m_hSerialController, _buffer, _length, &bytesWritten, &overlapped ) )
396
+ {
397
+ //Wait for the write to complete
398
+ if( ERROR_IO_PENDING == GetLastError() )
399
+ {
400
+ GetOverlappedResult( m_hSerialController, &overlapped, &bytesWritten, TRUE );
401
+ }
402
+ else
403
+ {
404
+ Log::Write( LogLevel_Error, "ERROR: Serial port write (0x%.8x)", GetLastError() );
405
+ }
406
+ }
407
+
408
+ CloseHandle( overlapped.hEvent );
409
+ return (uint32)bytesWritten;
410
+ }