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,56 @@
1
+ //----------------------------------------------------------------------------
2
+ //
3
+ // MutexImpl.h
4
+ //
5
+ // POSIX implementation of the cross-platform mutex
6
+ //
7
+ // Copyright (c) 2010, Greg Satz <satz@iranger.com>
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 <stdio.h>
32
+ #include <pthread.h>
33
+
34
+ namespace OpenZWave
35
+ {
36
+ class MutexImpl
37
+ {
38
+ private:
39
+ friend class Mutex;
40
+
41
+ MutexImpl();
42
+ ~MutexImpl();
43
+
44
+ bool Lock( bool const _bWait = true );
45
+ void Unlock();
46
+
47
+ bool IsSignalled();
48
+
49
+ uint32 m_lockCount; // Keep track of the locks (there can be more than one if they occur on the same thread.
50
+ pthread_mutex_t m_criticalSection;
51
+ };
52
+
53
+ } // namespace OpenZWave
54
+
55
+ #endif //_MutexIF_H
56
+
@@ -0,0 +1,368 @@
1
+ //-----------------------------------------------------------------------------
2
+ //
3
+ // SerialControllerImpl.cpp
4
+ //
5
+ // POSIX implementation of a cross-platform serial port
6
+ //
7
+ // Copyright (c) 2010, Greg Satz <satz@iranger.com>
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 <unistd.h>
29
+ #include "Defs.h"
30
+ #include "Thread.h"
31
+ #include "Event.h"
32
+ #include "SerialControllerImpl.h"
33
+ #include "Log.h"
34
+
35
+ #ifdef __linux__
36
+ #include <libudev.h>
37
+ #endif
38
+
39
+ using namespace OpenZWave;
40
+
41
+ //-----------------------------------------------------------------------------
42
+ // <SerialControllerImpl::SerialControllerImpl>
43
+ // Constructor
44
+ //-----------------------------------------------------------------------------
45
+ SerialControllerImpl::SerialControllerImpl
46
+ (
47
+ SerialController* _owner
48
+ ):
49
+ m_owner( _owner ),
50
+ m_hSerialController( -1 )
51
+ {
52
+ }
53
+
54
+ //-----------------------------------------------------------------------------
55
+ // <SerialControllerImpl::~SerialControllerImpl>
56
+ // Destructor
57
+ //-----------------------------------------------------------------------------
58
+ SerialControllerImpl::~SerialControllerImpl
59
+ (
60
+ )
61
+ {
62
+ flock(m_hSerialController, LOCK_UN);
63
+ if(m_hSerialController >= 0)
64
+ close( m_hSerialController );
65
+ }
66
+
67
+ //-----------------------------------------------------------------------------
68
+ // <SerialControllerImpl::Open>
69
+ // Open the serial port
70
+ //-----------------------------------------------------------------------------
71
+ bool SerialControllerImpl::Open
72
+ (
73
+ )
74
+ {
75
+ // Try to init the serial port
76
+ if( !Init( 1 ) )
77
+ {
78
+ // Failed. We bail to allow the app a chance to take over, rather than retry
79
+ // automatically. Automatic retries only occur after a successful init.
80
+ return false;
81
+ }
82
+
83
+ // Start the read thread
84
+ m_pThread = new Thread( "SerialController" );
85
+ m_pThread->Start( SerialReadThreadEntryPoint, this );
86
+
87
+ return true;
88
+ }
89
+
90
+ //-----------------------------------------------------------------------------
91
+ // <SerialControllerImpl::Close>
92
+ // Close the serial port
93
+ //-----------------------------------------------------------------------------
94
+ void SerialControllerImpl::Close
95
+ (
96
+ )
97
+ {
98
+ if( m_pThread )
99
+ {
100
+ m_pThread->Stop();
101
+ m_pThread->Release();
102
+ m_pThread = NULL;
103
+ }
104
+ close( m_hSerialController );
105
+ m_hSerialController = -1;
106
+ }
107
+
108
+ //-----------------------------------------------------------------------------
109
+ // <SerialReadThreadEntryPoint>
110
+ // Entry point of the thread for receiving data from the serial port
111
+ //-----------------------------------------------------------------------------
112
+ void SerialControllerImpl::SerialReadThreadEntryPoint
113
+ (
114
+ Event* _exitEvent,
115
+ void* _context
116
+ )
117
+ {
118
+ SerialControllerImpl* impl = (SerialControllerImpl*)_context;
119
+ if( impl )
120
+ {
121
+ impl->ReadThreadProc( _exitEvent );
122
+ }
123
+ }
124
+
125
+ //-----------------------------------------------------------------------------
126
+ // <SerialControllerImpl::ReadThreadProc>
127
+ // Handle receiving data
128
+ //-----------------------------------------------------------------------------
129
+ void SerialControllerImpl::ReadThreadProc
130
+ (
131
+ Event* _exitEvent
132
+ )
133
+ {
134
+ uint32 attempts = 0;
135
+ while( true )
136
+ {
137
+ // Init must have been called successfully during Open, so we
138
+ // don't do it again until the end of the loop
139
+ if( -1 != m_hSerialController )
140
+ {
141
+ // Enter read loop. Call will only return if
142
+ // an exit is requested or an error occurs
143
+ Read();
144
+
145
+ // Reset the attempts, so we get a rapid retry for temporary errors
146
+ attempts = 0;
147
+ }
148
+
149
+ if( attempts < 25 )
150
+ {
151
+ // Retry every 5 seconds for the first two minutes...
152
+ if( Wait::Single( _exitEvent, 5000 ) >= 0 )
153
+ {
154
+ // Exit signalled.
155
+ break;
156
+ }
157
+ }
158
+ else
159
+ {
160
+ // ...retry every 30 seconds after that
161
+ if( Wait::Single( _exitEvent, 30000 ) >= 0 )
162
+ {
163
+ // Exit signalled.
164
+ break;
165
+ }
166
+ }
167
+
168
+ Init( ++attempts );
169
+ }
170
+ }
171
+
172
+ //-----------------------------------------------------------------------------
173
+ // <SerialControllerImpl::Init>
174
+ // Initialize the serial port
175
+ //-----------------------------------------------------------------------------
176
+ bool SerialControllerImpl::Init
177
+ (
178
+ uint32 const _attempts
179
+ )
180
+ {
181
+
182
+ string device = m_owner->m_serialControllerName;
183
+
184
+ Log::Write( LogLevel_Info, "Trying to open serial port %s (attempt %d)", device.c_str(), _attempts );
185
+
186
+ m_hSerialController = open( device.c_str(), O_RDWR | O_NOCTTY, 0 );
187
+
188
+ if( -1 == m_hSerialController )
189
+ {
190
+ //Error
191
+ Log::Write( LogLevel_Error, "ERROR: Cannot open serial port %s. Error code %d", device.c_str(), errno );
192
+ goto SerialOpenFailure;
193
+ }
194
+
195
+ if( flock( m_hSerialController, LOCK_EX | LOCK_NB) == -1 )
196
+ {
197
+ Log::Write( LogLevel_Error, "ERROR: Cannot get exclusive lock for serial port %s. Error code %d", device.c_str(), errno );
198
+ }
199
+
200
+ int bits;
201
+ bits = 0;
202
+ ioctl( m_hSerialController, TIOCMSET, &bits );
203
+
204
+ // Configure the serial device parameters
205
+ // Build on the current configuration
206
+ struct termios tios;
207
+
208
+ bzero( &tios, sizeof(tios) );
209
+ tcgetattr( m_hSerialController, &tios );
210
+ switch( m_owner->m_parity )
211
+ {
212
+ case SerialController::Parity_None:
213
+ tios.c_iflag = IGNPAR;
214
+ break;
215
+ case SerialController::Parity_Odd:
216
+ tios.c_iflag = INPCK;
217
+ tios.c_cflag = PARENB | PARODD;
218
+ break;
219
+ default:
220
+ Log::Write( LogLevel_Error, "ERROR: Parity not supported" );
221
+ goto SerialOpenFailure;
222
+ }
223
+ switch( m_owner->m_stopBits )
224
+ {
225
+ case SerialController::StopBits_One:
226
+ break; // default
227
+ case SerialController::StopBits_Two:
228
+ tios.c_cflag |= CSTOPB;
229
+ break;
230
+ default:
231
+ Log::Write( LogLevel_Error, "ERROR: Stopbits not supported" );
232
+ goto SerialOpenFailure;
233
+ }
234
+ tios.c_iflag |= IGNBRK;
235
+ tios.c_cflag |= CS8 | CREAD | CLOCAL;
236
+ tios.c_oflag = 0;
237
+ tios.c_lflag = 0;
238
+ for( int i = 0; i < NCCS; i++ )
239
+ tios.c_cc[i] = 0;
240
+ tios.c_cc[VMIN] = 0;
241
+ tios.c_cc[VTIME] = 1;
242
+ switch( m_owner->m_baud )
243
+ {
244
+ case 300:
245
+ cfsetspeed( &tios, B300 );
246
+ break;
247
+ case 1200:
248
+ cfsetspeed( &tios, B1200 );
249
+ break;
250
+ case 2400:
251
+ cfsetspeed( &tios, B2400 );
252
+ break;
253
+ case 4800:
254
+ cfsetspeed( &tios, B4800 );
255
+ break;
256
+ case 9600:
257
+ cfsetspeed( &tios, B9600 );
258
+ break;
259
+ case 19200:
260
+ cfsetspeed( &tios, B19200 );
261
+ break;
262
+ case 38400:
263
+ cfsetspeed( &tios, B38400 );
264
+ break;
265
+ case 57600:
266
+ cfsetspeed( &tios, B57600 );
267
+ break;
268
+ #ifdef DARWIN
269
+ case 76800:
270
+ cfsetspeed( &tios, B76800 );
271
+ break;
272
+ #endif
273
+ case 115200:
274
+ cfsetspeed( &tios, B115200 );
275
+ break;
276
+ case 230400:
277
+ cfsetspeed( &tios, B230400 );
278
+ break;
279
+ default:
280
+ Log::Write( LogLevel_Error, "Baud rate not supported" );
281
+ goto SerialOpenFailure;
282
+ }
283
+ if ( tcsetattr( m_hSerialController, TCSANOW, &tios ) == -1 )
284
+ {
285
+ // Error. Clean up and exit
286
+ Log::Write( LogLevel_Error, "ERROR: Failed to set serial port parameters" );
287
+ goto SerialOpenFailure;
288
+ }
289
+
290
+ tcflush( m_hSerialController, TCIOFLUSH );
291
+
292
+ // Open successful
293
+ Log::Write( LogLevel_Info, "Serial port %s opened (attempt %d)", device.c_str(), _attempts );
294
+ return true;
295
+
296
+ SerialOpenFailure:
297
+ Log::Write( LogLevel_Error, "ERROR: Failed to open serial port %s", device.c_str() );
298
+ if(m_hSerialController >= 0)
299
+ {
300
+ close( m_hSerialController );
301
+ m_hSerialController = -1;
302
+ }
303
+ return false;
304
+ }
305
+
306
+ //-----------------------------------------------------------------------------
307
+ // <SerialControllerImpl::Read>
308
+ // Read data from the serial port
309
+ //-----------------------------------------------------------------------------
310
+ void SerialControllerImpl::Read
311
+ (
312
+ )
313
+ {
314
+ uint8 buffer[256];
315
+
316
+ while( 1 )
317
+ {
318
+ int32 bytesRead;
319
+ int err;
320
+
321
+ do
322
+ {
323
+ bytesRead = read( m_hSerialController, buffer, sizeof(buffer) );
324
+ if( bytesRead > 0 )
325
+ m_owner->Put( buffer, bytesRead );
326
+ } while( bytesRead > 0 );
327
+
328
+ do
329
+ {
330
+ struct timeval *whenp;
331
+ fd_set rds, eds;
332
+ int oldstate;
333
+
334
+ FD_ZERO( &rds );
335
+ FD_SET( m_hSerialController, &rds );
336
+ FD_ZERO( &eds );
337
+ FD_SET( m_hSerialController, &eds );
338
+ whenp = NULL;
339
+
340
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
341
+ err = select( m_hSerialController + 1, &rds, NULL, &eds, whenp );
342
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
343
+ } while( err <= 0 );
344
+ }
345
+ }
346
+
347
+ //-----------------------------------------------------------------------------
348
+ // <SerialControllerImpl::Write>
349
+ // Send data to the serial port
350
+ //-----------------------------------------------------------------------------
351
+ uint32 SerialControllerImpl::Write
352
+ (
353
+ uint8* _buffer,
354
+ uint32 _length
355
+ )
356
+ {
357
+ if( -1 == m_hSerialController )
358
+ {
359
+ //Error
360
+ Log::Write( LogLevel_Error, "ERROR: Serial port must be opened before writing" );
361
+ return 0;
362
+ }
363
+
364
+ // Write the data
365
+ uint32 bytesWritten;
366
+ bytesWritten = write( m_hSerialController, _buffer, _length);
367
+ return bytesWritten;
368
+ }
@@ -0,0 +1,74 @@
1
+ //
2
+ // SerialControllerImpl.h
3
+ //
4
+ // POSIX implementation of a cross-platform serial port
5
+ //
6
+ // Copyright (c) 2010, Greg Satz <satz@iranger.com>
7
+ // All rights reserved.
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 _SerialControllerImpl_H
29
+ #define _SerialControllerImpl_H
30
+
31
+ #include <strings.h>
32
+ #include <termios.h>
33
+ #include <errno.h>
34
+ #include <fcntl.h>
35
+ #include <time.h>
36
+ #include <sys/file.h>
37
+ #include <sys/ioctl.h>
38
+ #include <arpa/inet.h>
39
+
40
+ #include "Defs.h"
41
+ #include "SerialController.h"
42
+
43
+ namespace OpenZWave
44
+ {
45
+ class SerialControllerImpl
46
+ {
47
+ public:
48
+ void ReadThreadProc( Event* _exitEvent );
49
+
50
+ private:
51
+ friend class SerialController;
52
+
53
+ SerialControllerImpl( SerialController* _owner );
54
+ ~SerialControllerImpl();
55
+
56
+ bool Open();
57
+ void Close();
58
+
59
+ uint32 Write( uint8* _buffer, uint32 _length );
60
+
61
+ bool Init( uint32 const _attempts );
62
+ void Read();
63
+
64
+ SerialController* m_owner;
65
+ int m_hSerialController;
66
+ Thread* m_pThread;
67
+
68
+ static void SerialReadThreadEntryPoint( Event* _exitEvent, void* _content );
69
+ };
70
+
71
+ } // namespace OpenZWave
72
+
73
+ #endif //_SerialControllerImpl_H
74
+