1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris *
5230557Sjimharris * GPL LICENSE SUMMARY
6230557Sjimharris *
7230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * This program is free software; you can redistribute it and/or modify
10230557Sjimharris * it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris * published by the Free Software Foundation.
12230557Sjimharris *
13230557Sjimharris * This program is distributed in the hope that it will be useful, but
14230557Sjimharris * WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris * General Public License for more details.
17230557Sjimharris *
18230557Sjimharris * You should have received a copy of the GNU General Public License
19230557Sjimharris * along with this program; if not, write to the Free Software
20230557Sjimharris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris * The full GNU General Public License is included in this distribution
22230557Sjimharris * in the file called LICENSE.GPL.
23230557Sjimharris *
24230557Sjimharris * BSD LICENSE
25230557Sjimharris *
26230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris * All rights reserved.
28230557Sjimharris *
29230557Sjimharris * Redistribution and use in source and binary forms, with or without
30230557Sjimharris * modification, are permitted provided that the following conditions
31230557Sjimharris * are met:
32230557Sjimharris *
33230557Sjimharris *   * Redistributions of source code must retain the above copyright
34230557Sjimharris *     notice, this list of conditions and the following disclaimer.
35230557Sjimharris *   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris *     notice, this list of conditions and the following disclaimer in
37230557Sjimharris *     the documentation and/or other materials provided with the
38230557Sjimharris *     distribution.
39230557Sjimharris *
40230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris */
52230557Sjimharris
53230557Sjimharris#include <sys/cdefs.h>
54230557Sjimharris__FBSDID("$FreeBSD$");
55230557Sjimharris
56230557Sjimharris/**
57230557Sjimharris * @file
58230557Sjimharris *
59230557Sjimharris * @brief This file provides the public and protected implementations for the
60230557Sjimharris *        state machine logger.  The state machine logger will provide debug
61230557Sjimharris *        log information on a state machine for each state transition.
62230557Sjimharris */
63230557Sjimharris
64230557Sjimharris#include <dev/isci/scil/sci_base_state_machine_logger.h>
65230557Sjimharris
66230557Sjimharris#if defined(SCI_LOGGING)
67230557Sjimharris
68230557Sjimharris//******************************************************************************
69230557Sjimharris//* P R O T E C T E D    M E T H O D S
70230557Sjimharris//******************************************************************************
71230557Sjimharris
72230557Sjimharris/**
73230557Sjimharris * This is the function that is called when the state machine wants to notify
74230557Sjimharris * this observer that there has been a state change.
75230557Sjimharris *
76230557Sjimharris * @param[in] observer The state machine logger that is observing the state
77230557Sjimharris *       machine.
78230557Sjimharris * @param[in] subject The state machine that is being observed.
79230557Sjimharris */
80230557Sjimharrisstatic
81230557Sjimharrisvoid sci_base_state_machine_logger_update(
82230557Sjimharris   SCI_BASE_OBSERVER_T *observer,
83230557Sjimharris   SCI_BASE_SUBJECT_T  *subject
84230557Sjimharris)
85230557Sjimharris{
86230557Sjimharris   SCI_BASE_STATE_MACHINE_LOGGER_T *this_observer;
87230557Sjimharris   this_observer = (SCI_BASE_STATE_MACHINE_LOGGER_T *)observer;
88230557Sjimharris
89230557Sjimharris   this_observer->log_function(
90230557Sjimharris      sci_base_object_get_logger(this_observer->log_object),
91230557Sjimharris      this_observer->log_mask,
92230557Sjimharris      "%s 0x%08x %s has transitioned from %d to %d\n",
93230557Sjimharris      this_observer->log_object_name,
94230557Sjimharris      this_observer->log_object,
95230557Sjimharris      this_observer->log_state_machine_name,
96230557Sjimharris      this_observer->parent.subject_state,
97230557Sjimharris      sci_base_state_machine_get_state((SCI_BASE_STATE_MACHINE_T *)subject)
98230557Sjimharris   );
99230557Sjimharris
100230557Sjimharris   sci_base_state_machine_observer_default_update(
101230557Sjimharris      &this_observer->parent.parent, subject
102230557Sjimharris   );
103230557Sjimharris}
104230557Sjimharris
105230557Sjimharris//******************************************************************************
106230557Sjimharris//* P U B L I C   M E T H O D S
107230557Sjimharris//******************************************************************************
108230557Sjimharris
109230557Sjimharris/**
110230557Sjimharris * This function will construct the state machine logger and attach it to the
111230557Sjimharris * state machine that is to be observed.
112230557Sjimharris *
113230557Sjimharris * @param[in] this_observer This is the state machine logger object that is
114230557Sjimharris *       going to observe the subject state machine.
115230557Sjimharris * @param[in] the_object This is the object that contains the state machine
116230557Sjimharris *       being observed it is used to report the address of the object for
117230557Sjimharris *       which a state transition has occurred.
118230557Sjimharris * @param[in] the_log_function This is the logging function to be used when a
119230557Sjimharris *       state machine transition occurs.  Since this is a base object type it
120230557Sjimharris *       does not actually know if the logging function is for the core or
121230557Sjimharris *       framework.
122230557Sjimharris * @param[in] the_object_name This is the name of the object that contains the
123230557Sjimharris *       state machine being observed.
124230557Sjimharris * @param[in] the_state_machine_name This is the name that will be displayed
125230557Sjimharris *       in the log string for the state machine being observed.
126230557Sjimharris * @param[in] the_object_mask This is the log object mask used when calling
127230557Sjimharris *       the logging function.
128230557Sjimharris *
129230557Sjimharris * @return Nothing
130230557Sjimharris */
131230557Sjimharrisvoid sci_base_state_machine_logger_construct(
132230557Sjimharris   SCI_BASE_STATE_MACHINE_LOGGER_T             * this_observer,
133230557Sjimharris   SCI_BASE_OBJECT_T                           * the_object,
134230557Sjimharris   SCI_BASE_STATE_MACHINE_LOGGER_LOG_HANDLER_T   the_log_function,
135230557Sjimharris   char                                        * log_object_name,
136230557Sjimharris   char                                        * log_state_machine_name,
137230557Sjimharris   U32                                           log_object_mask
138230557Sjimharris)
139230557Sjimharris{
140230557Sjimharris   sci_base_state_machine_observer_construct(&this_observer->parent);
141230557Sjimharris
142230557Sjimharris   this_observer->log_object             = the_object;
143230557Sjimharris   this_observer->log_function           = the_log_function;
144230557Sjimharris   this_observer->log_object_name        = log_object_name;
145230557Sjimharris   this_observer->log_state_machine_name = log_state_machine_name;
146230557Sjimharris   this_observer->log_mask               = log_object_mask;
147230557Sjimharris
148230557Sjimharris   this_observer->parent.parent.update = sci_base_state_machine_logger_update;
149230557Sjimharris}
150230557Sjimharris
151230557Sjimharris/**
152230557Sjimharris * This is a helper function that will construct the state machine logger and
153230557Sjimharris * attach it to the state machine that is to be observed.
154230557Sjimharris *
155230557Sjimharris * @param[in] this_observer This is the state machine logger object that is
156230557Sjimharris *       going to observe the subject state machine.
157230557Sjimharris * @param[in] the_state_machine This is the state machine that is under
158230557Sjimharris *       observation.
159230557Sjimharris * @param[in] the_object This is the object that contains the state machine
160230557Sjimharris *       being observed it is used to report the address of the object for
161230557Sjimharris *       which a state transition has occurred.
162230557Sjimharris * @param[in] the_log_function This is the logging function to be used when a
163230557Sjimharris *       state machine transition occurs.  Since this is a base object type it
164230557Sjimharris *       does not actually know if the logging function is for the core or
165230557Sjimharris *       framework.
166230557Sjimharris * @param[in] the_object_name This is the name of the object that contains the
167230557Sjimharris *       state machine being observed.
168230557Sjimharris * @param[in] the_state_machine_name This is the name that will be displayed
169230557Sjimharris *       in the log string for the state machine being observed.
170230557Sjimharris * @param[in] the_object_mask This is the log object mask used when calling
171230557Sjimharris *       the logging function.
172230557Sjimharris *
173230557Sjimharris * @return Nothing
174230557Sjimharris */
175230557Sjimharrisvoid sci_base_state_machine_logger_initialize(
176230557Sjimharris   SCI_BASE_STATE_MACHINE_LOGGER_T             * this_observer,
177230557Sjimharris   SCI_BASE_STATE_MACHINE_T                    * the_state_machine,
178230557Sjimharris   SCI_BASE_OBJECT_T                           * the_object,
179230557Sjimharris   SCI_BASE_STATE_MACHINE_LOGGER_LOG_HANDLER_T   the_log_function,
180230557Sjimharris   char                                        * log_object_name,
181230557Sjimharris   char                                        * log_state_machine_name,
182230557Sjimharris   U32                                           log_object_mask
183230557Sjimharris)
184230557Sjimharris{
185230557Sjimharris   sci_base_state_machine_logger_construct(
186230557Sjimharris      this_observer, the_object,
187230557Sjimharris      the_log_function, log_object_name, log_state_machine_name, log_object_mask
188230557Sjimharris   );
189230557Sjimharris
190230557Sjimharris   sci_base_subject_attach_observer(
191230557Sjimharris      &the_state_machine->parent, &this_observer->parent.parent
192230557Sjimharris   );
193230557Sjimharris}
194230557Sjimharris
195230557Sjimharris/**
196230557Sjimharris * This is a helper function that will detach this observer from the state
197230557Sjimharris * machine that is being observerd.
198230557Sjimharris *
199230557Sjimharris * @param[in] this_observer This is the observer to detach from the state
200230557Sjimharris *       machine.
201230557Sjimharris * @parame[in] the_state_machine This is the state machine that is no longer
202230557Sjimharris *       going to be observed.
203230557Sjimharris *
204230557Sjimharris * @return Nothing
205230557Sjimharris */
206230557Sjimharrisvoid sci_base_state_machine_logger_deinitialize(
207230557Sjimharris   SCI_BASE_STATE_MACHINE_LOGGER_T * this_observer,
208230557Sjimharris   SCI_BASE_STATE_MACHINE_T        * the_state_machine
209230557Sjimharris)
210230557Sjimharris{
211230557Sjimharris   sci_base_subject_detach_observer(
212230557Sjimharris      &the_state_machine->parent, &this_observer->parent.parent
213230557Sjimharris   );
214230557Sjimharris}
215230557Sjimharris
216230557Sjimharris#endif // defined(SCI_LOGGING)
217230557Sjimharris
218