1 //==========================================================================
5 // Testcase for the power management support.
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
44 // Contributors: bartv
47 // This testcase checks most of the functionality of the power management
48 // package. The main exception is the POWER_CONTROLLER_CPU macro and
49 // the power_controller_cpu variable: that controller may or may not be
50 // provided by one of the HAL packages, so attempting to manipulate that
51 // HAL could cause link-time conflicts.
53 // The testcase is likely to fail if some package supplies a policy
54 // module that is making concurrent calls, for example if that module
55 // installs its own callback. For now there is no way of detecting
56 // that scenario, but also for now policy modules are assumed to be
57 // provided by the application rather than by any eCos package.
59 //####DESCRIPTIONEND####
61 //==========================================================================
63 #include <pkgconf/system.h>
64 #include <pkgconf/power.h>
65 #include <cyg/infra/cyg_type.h>
66 #include <cyg/infra/cyg_ass.h>
67 #include <cyg/infra/testcase.h>
68 #include <cyg/hal/hal_arch.h>
69 #include <cyg/power/power.h>
72 // Additional #include's to support the use of multiple threads.
73 #include <cyg/kernel/kapi.h>
74 #include <cyg/kernel/sched.hxx>
77 // Additional prototypes. These are builtins, but in some configurations
78 // the relevant headers may not be present.
79 extern "C" int strcmp(const char*, const char*);
81 // ----------------------------------------------------------------------------
82 // Define the various tests in the system. This allows the power controllers
83 // and the policy callback to take appropriate action as required.
87 PowerTest_InitialState,
91 // ----------------------------------------------------------------------------
94 static unsigned char policy_thread_stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
95 static cyg_thread policy_thread;
96 static cyg_handle_t policy_thread_handle;
99 // ----------------------------------------------------------------------------
100 // Provide three power controllers at different priorities.
101 static void xyzzy_power_controller_fn(PowerController*, PowerMode, PowerModeChange);
102 static void wumpus_power_controller_fn(PowerController*, PowerMode, PowerModeChange);
103 static void erwin_power_controller_fn(PowerController*, PowerMode, PowerModeChange);
105 POWER_CONTROLLER(xyzzy_controller, PowerPri_Early, "xyzzy", &xyzzy_power_controller_fn);
106 POWER_CONTROLLER(wumpus_controller, PowerPri_Typical, "wumpus", &wumpus_power_controller_fn);
107 POWER_CONTROLLER(erwin_controller, PowerPri_Late, "erwin", &erwin_power_controller_fn);
109 // ----------------------------------------------------------------------------
112 // Check that all three power controllers are actually present in the array.
116 bool seen_xyzzy = false;
117 bool seen_wumpus = false;
118 bool seen_erwin = false;
119 PowerController* controller;
121 for (controller = &(__POWER__[0]); controller != &__POWER_END__; controller++) {
122 if (controller == &xyzzy_controller) {
123 CYG_ASSERTC(!seen_xyzzy);
125 } else if (controller == &wumpus_controller) {
126 CYG_ASSERTC(!seen_wumpus);
128 } else if (controller == &erwin_controller) {
129 CYG_ASSERTC(!seen_erwin);
134 if (seen_xyzzy && seen_wumpus && seen_erwin) {
135 CYG_TEST_PASS("All expected controllers found");
137 CYG_TEST_FAIL_FINISH("Failed to find all the controllers that should be present");
141 // Now check that the controllers appear in the expected order.
145 int xyzzy_index = -1;
146 int wumpus_index = -1;
147 int erwin_index = -1;
150 for (i = 0; &(__POWER__[i]) != &__POWER_END__; i++) {
151 if (&__POWER__[i] == &xyzzy_controller) {
152 CYG_ASSERTC(-1 == xyzzy_index);
154 } else if (&__POWER__[i] == &wumpus_controller) {
155 CYG_ASSERTC(-1 == wumpus_index);
157 } else if (&__POWER__[i] == &erwin_controller) {
158 CYG_ASSERTC(-1 == erwin_index);
162 CYG_ASSERTC(-1 != xyzzy_index);
163 CYG_ASSERTC(-1 != wumpus_index);
164 CYG_ASSERTC(-1 != erwin_index);
166 if ((xyzzy_index < wumpus_index) && (wumpus_index < erwin_index)) {
167 CYG_TEST_PASS("The power controllers are in the correct order");
169 CYG_TEST_FAIL_FINISH("The power controllers are not in the correct order");
173 // Check the initial state of the system. Power controllers other than the ones
174 // defined by this testcase are ignored, since conceivably they might well come
175 // up in a mode other than active.
177 test_initial_state(void)
180 // There have been no calls to change power mode, so the system should be active
181 if (PowerMode_Active != power_get_mode()) {
182 CYG_TEST_FAIL("Initial power mode not active");
185 if (PowerMode_Active != power_get_desired_mode()) {
186 CYG_TEST_FAIL("Initial desired power mode not active");
189 if ((void (*)(PowerController*, PowerMode, PowerMode, PowerMode, PowerMode)) 0 != power_get_policy_callback()) {
190 CYG_TEST_FAIL("A policy callback has already been installed");
194 if ((PowerMode_Active != power_get_controller_mode(&xyzzy_controller)) ||
195 (PowerMode_Active != power_get_controller_mode(&wumpus_controller)) ||
196 (PowerMode_Active != power_get_controller_mode(&erwin_controller))) {
197 CYG_TEST_FAIL("Not all power controllers initialized to active");
200 if ((PowerMode_Active != power_get_controller_desired_mode(&xyzzy_controller)) ||
201 (PowerMode_Active != power_get_controller_desired_mode(&wumpus_controller)) ||
202 (PowerMode_Active != power_get_controller_desired_mode(&erwin_controller))) {
203 CYG_TEST_FAIL("Not all power controllers initialized to desired active");
207 if (!power_get_controller_attached(&xyzzy_controller) ||
208 !power_get_controller_attached(&wumpus_controller) ||
209 !power_get_controller_attached(&erwin_controller)) {
210 CYG_TEST_FAIL("Some power controllers started off detached");
214 // The initial policy data is checked in a separate testcase
216 CYG_TEST_PASS("Initial power management state is as expected");
220 // If the configuration involves per-controller id's, check them.
221 #ifdef CYGIMP_POWER_PROVIDE_STRINGS
222 static int test_ids_data = 0; // avoid problems with over-optimising
227 // All of the power controllers should have a valid id, i.e. it
228 // should be possible to dereference the pointer. A SEGV here
229 // probably indicates that a HAL or device driver package has
230 // failed to supply a sensible string, which is a test failure
231 // albeit one in that package rather than the power management
233 PowerController* controller;
234 for (controller = &(__POWER__[0]); controller != &__POWER_END__; controller++) {
235 const char* id = power_get_controller_id(controller);
236 test_ids_data += id[0];
239 // In addition the id strings for the three known controllers are also known.
240 if ((0 == strcmp("xyzzy", power_get_controller_id(&xyzzy_controller))) &&
241 (0 == strcmp("wumpus", power_get_controller_id(&wumpus_controller))) &&
242 (0 == strcmp("erwin", power_get_controller_id(&erwin_controller)))) {
243 CYG_TEST_PASS("Controller id strings match up");
245 CYG_TEST_FAIL("Controller id strings fail to match");
252 CYG_TEST_NA("Unable to test controller ids - this functionaly has been configured out");
255 // ----------------------------------------------------------------------------
256 // The individual power controller functions. By default these just obey the
257 // requested mode change, but there are some special cases.
259 xyzzy_power_controller_fn(PowerController* controller, PowerMode desired_mode, PowerModeChange change)
261 switch (current_test) {
263 controller->mode = desired_mode;
269 wumpus_power_controller_fn(PowerController* controller, PowerMode desired_mode, PowerModeChange change)
271 switch (current_test) {
273 controller->mode = desired_mode;
279 erwin_power_controller_fn(PowerController* controller, PowerMode desired_mode, PowerModeChange change)
281 switch (current_test) {
283 controller->mode = desired_mode;
288 // ----------------------------------------------------------------------------
289 // The main policy callback. Like the controller functions by default this
290 // does nothing, but for specific tests it has to take action.
292 policy_callback(PowerController* controller, PowerMode old_mode, PowerMode current_mode, PowerMode old_desired_mode, PowerMode current_desired_mode)
296 // ----------------------------------------------------------------------------
299 // Detach all controllers except the ones supplied by this package. It would
300 // be unfortunate if say the cpu stopped running because a test case decided
301 // to set the global mode to off.
303 detach_all_controllers(void)
305 PowerController* controller;
306 for (controller = &(__POWER__[0]); controller != &__POWER_END__; controller++) {
307 if ((controller != &xyzzy_controller) && (controller != &wumpus_controller) && (controller != &erwin_controller)) {
308 power_set_controller_attached(controller, false);
313 // ----------------------------------------------------------------------------
314 // The main routine, which acts as the policy module. This either runs as
315 // a separate thread, if the kernel is present, or else it is called
316 // directly from cyg_start().
318 powertest_main(CYG_ADDRWORD data)
320 CYG_ASSERT(0 == data, "No data should be supplied");
324 current_test = PowerTest_Presence;
327 current_test = PowerTest_Order;
330 current_test = PowerTest_InitialState;
331 test_initial_state();
333 current_test = PowerTest_Ids;
336 detach_all_controllers();
338 CYG_TEST_FINISH("All tests have been run");
341 // ----------------------------------------------------------------------------
346 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
347 cyg_hal_invoke_constructors();
351 cyg_thread_create(CYG_SCHED_DEFAULT_INFO,
354 "Power policy thread",
356 CYGNUM_HAL_STACK_SIZE_TYPICAL,
357 &policy_thread_handle,
359 cyg_thread_resume(policy_thread_handle);
360 Cyg_Scheduler::start();
361 CYG_TEST_FAIL_FINISH("Not reached");