blob: d1db7c2983efe24e39e05fa787d48ec1d920d8ce [file] [log] [blame]
Gilad Arnold1ebd8132012-03-05 10:19:29 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_HANDLER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_HANDLER_H__
7
8#include <libudev.h>
9
10#include <string>
11
Gilad Arnold4d740eb2012-05-15 08:48:13 -070012#include "update_engine/file_descriptor.h"
13#include "update_engine/udev_interface.h"
Gilad Arnold1ebd8132012-03-05 10:19:29 -080014
15namespace chromeos_update_engine {
16
Gilad Arnold4d740eb2012-05-15 08:48:13 -070017// An abstract GPIO handler interface. Serves as basic for both concrete and
18// mock implementations.
Gilad Arnold1ebd8132012-03-05 10:19:29 -080019class GpioHandler {
20 public:
Gilad Arnold4d740eb2012-05-15 08:48:13 -070021 GpioHandler() {}
22 virtual ~GpioHandler() {}; // ensure virtual destruction
23
24 // Returns true iff GPIOs have been used to signal an automated test case.
25 // This call may trigger a (deferred) GPIO discovery step prior to engaging in
26 // the signaling protocol; if discovery did not reveal GPIO devices, or the
27 // protocol has terminated prematurely, it will conservatively default to
28 // false.
29 virtual bool IsTestModeSignaled() = 0;
Gilad Arnold1ebd8132012-03-05 10:19:29 -080030
31 private:
Gilad Arnold4d740eb2012-05-15 08:48:13 -070032 DISALLOW_COPY_AND_ASSIGN(GpioHandler);
33};
Gilad Arnold1ebd8132012-03-05 10:19:29 -080034
Gilad Arnold4d740eb2012-05-15 08:48:13 -070035
36// Concrete implementation of GPIO signal handling. Currently, it only utilizes
37// the two Chromebook-specific GPIOs (aka 'dut_flaga' and 'dut_flagb') in
38// deciding whether a lab test mode has been signaled. Internal logic includes
39// detection, setup and reading from / writing to GPIOs. Detection is done via
40// libudev calls. This class should be instantiated at most once to avoid race
41// conditions in communicating over GPIO signals; instantiating a second
42// object will actually cause a runtime error.
43class StandardGpioHandler : public GpioHandler {
44 public:
Gilad Arnold6eccc532012-05-17 15:44:22 -070045 // This constructor accepts a udev interface |udev_iface| and a reusable file
46 // descriptor |fd|. The value of |is_defer_discovery| determines whether GPIO
47 // discovery should be attempted right away (false) or done lazily, when
48 // necessitated by other calls (true). If |is_cache_test_mode| is true,
49 // checking for test mode signal is done only once and further queries return
50 // the cached result.
51 StandardGpioHandler(UdevInterface* udev_iface, FileDescriptor* fd,
52 bool is_defer_discovery, bool is_cache_test_mode);
Gilad Arnold4d740eb2012-05-15 08:48:13 -070053
54 // Free all resources, allow to reinstantiate.
55 virtual ~StandardGpioHandler();
56
57 // Returns true iff GPIOs have been used to signal an automated test case. The
58 // check is performed at most once and the result is cached and returned on
59 // subsequent calls, unless |is_force| is true. This call may trigger a
60 // delayed GPIO discovery prior to engaging in the signaling protocol; if the
61 // delay period has not elapsed, it will conservatively default to false.
62 virtual bool IsTestModeSignaled();
63
64 private:
65 // GPIO identifiers, currently includes only the two dutflags.
66 enum GpioId {
67 kGpioIdDutflaga = 0,
68 kGpioIdDutflagb,
69 kGpioIdMax // marker, do not remove!
Gilad Arnold1ebd8132012-03-05 10:19:29 -080070 };
71
Gilad Arnold4d740eb2012-05-15 08:48:13 -070072 // GPIO direction specifier.
73 enum GpioDir {
74 kGpioDirIn = 0,
75 kGpioDirOut,
76 kGpioDirMax // marker, do not remove!
77 };
Gilad Arnold1ebd8132012-03-05 10:19:29 -080078
Gilad Arnold4d740eb2012-05-15 08:48:13 -070079 // GPIO value.
80 enum GpioVal {
81 kGpioValUp = 0,
82 kGpioValDown,
83 kGpioValMax // marker, do not remove!
84 };
85
86 // GPIO definition data.
87 struct GpioDef {
88 const char* name; // referential name of this GPIO
89 const char* udev_property; // udev property containing the device id
90 };
91
92 // GPIO runtime control structure.
93 struct Gpio {
94 std::string descriptor; // unique GPIO descriptor
95 std::string dev_path; // sysfs device name
96 };
97
Gilad Arnold6eccc532012-05-17 15:44:22 -070098 // The number of seconds we wait before flipping the output signal (aka,
99 // producing the "challenge" signal). Assuming a 1 second sampling frequency
100 // on the servo side, a two second wait should be enough.
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700101 static const int kServoOutputResponseWaitInSecs = 2;
102
Gilad Arnold6eccc532012-05-17 15:44:22 -0700103 // The total number of seconds we wait for a servo response from the point we
104 // flip the output signal. Assuming a 1 second sampling frequency on the servo
105 // side, a two second wait should suffice. We add one more second for grace
106 // (servod / hardware processing delays, etc).
107 static const int kServoInputResponseTimeoutInSecs = 3;
108
109 // The number of times per second we check for a servo response. Five seems
110 // like a reasonable value.
111 static const int kServoInputNumChecksPerSec = 5;
112
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700113 // GPIO value/direction conversion tables.
114 static const char* gpio_dirs_[kGpioDirMax];
115 static const char* gpio_vals_[kGpioValMax];
116
117 // GPIO definitions.
118 static const GpioDef gpio_defs_[kGpioIdMax];
119
120 // Udev device enumeration helper classes. First here is an interface
121 // definition, which provides callbacks for enumeration setup and processing.
122 class UdevEnumHelper {
123 public:
124 UdevEnumHelper(StandardGpioHandler* gpio_handler)
125 : gpio_handler_(gpio_handler) {}
126
127 // Setup the enumeration filters.
128 virtual bool SetupEnumFilters(udev_enumerate* udev_enum) = 0;
129
130 // Processes an enumerated device. Returns true upon success, false
131 // otherwise.
132 virtual bool ProcessDev(udev_device* dev) = 0;
133
134 // Finalize the enumeration.
135 virtual bool Finalize() = 0;
136
137 protected:
138 StandardGpioHandler* gpio_handler_;
139
140 private:
141 DISALLOW_COPY_AND_ASSIGN(UdevEnumHelper);
142 };
143
144 // Specialized udev enumerate helper for extracting GPIO descriptors from the
145 // GPIO chip device.
146 class GpioChipUdevEnumHelper : public UdevEnumHelper {
147 public:
148 GpioChipUdevEnumHelper(StandardGpioHandler* gpio_handler)
149 : UdevEnumHelper(gpio_handler), num_gpio_chips_(0) {}
150 virtual bool SetupEnumFilters(udev_enumerate* udev_enum);
151 virtual bool ProcessDev(udev_device* dev);
152 virtual bool Finalize();
153
154 private:
155 // Records the number of times a GPIO chip has been enumerated (should not
156 // exceed 1).
157 int num_gpio_chips_;
158
159 DISALLOW_COPY_AND_ASSIGN(GpioChipUdevEnumHelper);
160 };
161
162 // Specialized udev enumerate helper for extracting a sysfs device path from a
163 // GPIO device.
164 class GpioUdevEnumHelper : public UdevEnumHelper {
165 public:
166 GpioUdevEnumHelper(StandardGpioHandler* gpio_handler, GpioId id)
167 : UdevEnumHelper(gpio_handler), num_gpios_(0), id_(id) {}
168 virtual bool SetupEnumFilters(udev_enumerate* udev_enum);
169 virtual bool ProcessDev(udev_device* dev);
170 virtual bool Finalize();
171
172 private:
173 // Records the number of times a GPIO has been enumerated with a given
174 // descriptor (should not exceed 1).
175 int num_gpios_;
176
177 // The enumerated GPIO identifier.
178 GpioId id_;
179
180 DISALLOW_COPY_AND_ASSIGN(GpioUdevEnumHelper);
181 };
182
Gilad Arnold6eccc532012-05-17 15:44:22 -0700183 // Helper class for resetting a GPIO direction.
184 class GpioDirResetter {
185 public:
186 GpioDirResetter(StandardGpioHandler* handler, GpioId id, GpioDir dir);
187 ~GpioDirResetter();
188
189 bool do_reset() const {
190 return do_reset_;
191 }
192 bool set_do_reset(bool do_reset) {
193 return (do_reset_ = do_reset);
194 }
195
196 private:
197 // Determines whether or not the GPIO direction should be reset to the
198 // initial value.
199 bool do_reset_;
200
201 // The GPIO handler to use for changing the GPIO direction.
202 StandardGpioHandler* handler_;
203
204 // The GPIO identifier and initial direction.
205 GpioId id_;
206 GpioDir dir_;
207 };
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700208
209 // An initialization helper performing udev enumeration. |enum_helper|
210 // implements an enumeration initialization and processing methods. Returns
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800211 // true upon success, false otherwise.
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700212 bool InitUdevEnum(struct udev* udev, UdevEnumHelper* enum_helper);
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800213
Gilad Arnold6eccc532012-05-17 15:44:22 -0700214 // Resets the object's flags which determine the status of test mode
215 // signaling.
216 void ResetTestModeSignalingFlags();
217
218 // Attempt GPIO discovery, at most once. Returns true if discovery process was
Gilad Arnold95931b82013-01-09 10:37:17 -0800219 // successfully completed, false otherwise.
Gilad Arnold6eccc532012-05-17 15:44:22 -0700220 bool DiscoverGpios();
221
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700222 // Assigns a copy of the device name of GPIO |id| to |dev_path_p|. Assumes
223 // initialization. Returns true upon success, false otherwise.
224 bool GetGpioDevName(GpioId id, std::string* dev_path_p);
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800225
Gilad Arnold6eccc532012-05-17 15:44:22 -0700226 // Open a sysfs file device |dev_name| of GPIO |id|, for either reading or
227 // writing depending on |is_write|. Uses the internal file descriptor for
228 // this purpose, which can be reused as long as it is closed between
229 // successive opens. Returns true upon success, false otherwise (optionally,
230 // with errno set accordingly).
231 bool OpenGpioFd(GpioId id, const char* dev_name, bool is_write);
232
233 // Writes a value to device |dev_name| of GPIO |id|. The index |output| is
234 // used to index the corresponding string to be written from the list
235 // |entries| of length |num_entries|. Returns true upon success, false
236 // otherwise.
237 bool SetGpio(GpioId id, const char* dev_name, const char* entries[],
238 const int num_entries, int output);
239
240 // Reads a value from device |dev_name| of GPIO |id|. The list |entries| of
241 // length |num_entries| is used to convert the read string into an index,
242 // which is written to |input_p|. The call will fail if the value being read
243 // is not listed in |entries|. Returns true upon success, false otherwise.
244 bool GetGpio(GpioId id, const char* dev_name, const char* entries[],
245 const int num_entries, int* input_p);
246
247 // Sets GPIO |id| to to operate in a given |direction|. Assumes
248 // initialization. Returns true on success, false otherwise.
249 bool SetGpioDirection(GpioId id, GpioDir direction);
250
251 // Assigns the current direction of GPIO |id| into |direction_p|. Assumes
252 // initialization. Returns true on success, false otherwise.
253 bool GetGpioDirection(GpioId id, GpioDir* direction_p);
254
255 // Sets the value of GPIO |id| to |value|. Assumues initialization. The GPIO
256 // direction should be set to 'out' prior to this call. If
257 // |is_check_direction| is true, it'll ensure that the direction is indeed
258 // 'out' prior to attempting the write. Returns true on success, false
259 // otherwise.
260 bool SetGpioValue(GpioId id, GpioVal value, bool is_check_direction);
261
262 // Reads the value of a GPIO |id| and stores it in |value_p|. Assumes
263 // initialization. The GPIO direction should be set to 'in' prior to this
264 // call. If |is_check_direction| is true, it'll ensure that the direction is
265 // indeed 'in' prior to attempting the read. Returns true upon success, false
266 // otherwise.
267 bool GetGpioValue(GpioId id, GpioVal *value_p, bool is_check_direction);
268
269 // Invokes the actual GPIO handshake protocol to determine whether test mode
270 // was signaled. Returns true iff the handshake has terminated gracefully
271 // without encountering any errors; note that a true value does *not* mean
272 // that a test mode signal has been detected. The spec for this protocol:
273 // https://docs.google.com/a/google.com/document/d/1DB-35ptck1wT1TYrgS5AC5Y3ALfHok-iPA7kLBw2XCI/edit
274 bool DoTestModeSignalingProtocol();
275
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700276 // Dynamic counter for the number of instances this class has. Used to enforce
277 // that no more than one instance created. Thread-unsafe.
278 static unsigned num_instances_;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800279
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700280 // GPIO control structures.
281 Gpio gpios_[kGpioIdMax];
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800282
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700283 // Udev interface.
284 UdevInterface* const udev_iface_;
285
Gilad Arnold6eccc532012-05-17 15:44:22 -0700286 // A file abstraction for handling GPIO devices.
287 FileDescriptor* const fd_;
288
289 // Determines whether test mode signal should be checked at most once and
290 // cached, or reestablished on each query.
291 const bool is_cache_test_mode_;
292
Gilad Arnold95931b82013-01-09 10:37:17 -0800293 // Indicates whether GPIO discovery was performed, and whether it's been
294 // successful.
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700295 bool is_discovery_attempted_;
Gilad Arnold95931b82013-01-09 10:37:17 -0800296 bool is_discovery_successful_;
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700297
Gilad Arnold6eccc532012-05-17 15:44:22 -0700298 // Persistent state of the test mode check.
299 bool is_first_check_;
300 bool is_handshake_completed_;
301 bool is_test_mode_;
302
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700303 DISALLOW_COPY_AND_ASSIGN(StandardGpioHandler);
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800304};
305
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800306
307// A "no-op" GPIO handler, initialized to return either test or normal mode
308// signal. This is useful for disabling the GPIO functionality in production
309// code.
310class NoopGpioHandler : public GpioHandler {
311 public:
312 // This constructor accepts a single argument, which is the value to be
313 // returned by repeated calls to IsTestModeSignaled().
314 NoopGpioHandler(bool is_test_mode) : is_test_mode_(is_test_mode) {}
315
316 // Returns the constant Boolean value handed to the constructor.
317 virtual bool IsTestModeSignaled();
318
319 private:
320 // Stores the constant value to return on subsequent test mode checks.
321 bool is_test_mode_;
322
323 DISALLOW_COPY_AND_ASSIGN(NoopGpioHandler);
324};
325
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800326} // namespace chromeos_update_engine
327
328#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_HANDLER_H__