1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23  * The full GNU General Public License is included in this distribution
24  * in the file called LICENSE.GPL.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  *   * Redistributions of source code must retain the above copyright
36  *     notice, this list of conditions and the following disclaimer.
37  *   * Redistributions in binary form must reproduce the above copyright
38  *     notice, this list of conditions and the following disclaimer in
39  *     the documentation and/or other materials provided with the
40  *     distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /**
59  * @file
60  *
61  * @brief This file provides the public and protected implementations for the
62  *        state machine logger.  The state machine logger will provide debug
63  *        log information on a state machine for each state transition.
64  */
65 
66 #include <dev/isci/scil/sci_base_state_machine_logger.h>
67 
68 #if defined(SCI_LOGGING)
69 
70 //******************************************************************************
71 //* P R O T E C T E D    M E T H O D S
72 //******************************************************************************
73 
74 /**
75  * This is the function that is called when the state machine wants to notify
76  * this observer that there has been a state change.
77  *
78  * @param[in] observer The state machine logger that is observing the state
79  *       machine.
80  * @param[in] subject The state machine that is being observed.
81  */
82 static
83 void sci_base_state_machine_logger_update(
84    SCI_BASE_OBSERVER_T *observer,
85    SCI_BASE_SUBJECT_T  *subject
86 )
87 {
88    SCI_BASE_STATE_MACHINE_LOGGER_T *this_observer;
89    this_observer = (SCI_BASE_STATE_MACHINE_LOGGER_T *)observer;
90 
91    this_observer->log_function(
92       sci_base_object_get_logger(this_observer->log_object),
93       this_observer->log_mask,
94       "%s 0x%08x %s has transitioned from %d to %d\n",
95       this_observer->log_object_name,
96       this_observer->log_object,
97       this_observer->log_state_machine_name,
98       this_observer->parent.subject_state,
99       sci_base_state_machine_get_state((SCI_BASE_STATE_MACHINE_T *)subject)
100    );
101 
102    sci_base_state_machine_observer_default_update(
103       &this_observer->parent.parent, subject
104    );
105 }
106 
107 //******************************************************************************
108 //* P U B L I C   M E T H O D S
109 //******************************************************************************
110 
111 /**
112  * This function will construct the state machine logger and attach it to the
113  * state machine that is to be observed.
114  *
115  * @param[in] this_observer This is the state machine logger object that is
116  *       going to observe the subject state machine.
117  * @param[in] the_object This is the object that contains the state machine
118  *       being observed it is used to report the address of the object for
119  *       which a state transition has occurred.
120  * @param[in] the_log_function This is the logging function to be used when a
121  *       state machine transition occurs.  Since this is a base object type it
122  *       does not actually know if the logging function is for the core or
123  *       framework.
124  * @param[in] the_object_name This is the name of the object that contains the
125  *       state machine being observed.
126  * @param[in] the_state_machine_name This is the name that will be displayed
127  *       in the log string for the state machine being observed.
128  * @param[in] the_object_mask This is the log object mask used when calling
129  *       the logging function.
130  *
131  * @return Nothing
132  */
133 void sci_base_state_machine_logger_construct(
134    SCI_BASE_STATE_MACHINE_LOGGER_T             * this_observer,
135    SCI_BASE_OBJECT_T                           * the_object,
136    SCI_BASE_STATE_MACHINE_LOGGER_LOG_HANDLER_T   the_log_function,
137    char                                        * log_object_name,
138    char                                        * log_state_machine_name,
139    U32                                           log_object_mask
140 )
141 {
142    sci_base_state_machine_observer_construct(&this_observer->parent);
143 
144    this_observer->log_object             = the_object;
145    this_observer->log_function           = the_log_function;
146    this_observer->log_object_name        = log_object_name;
147    this_observer->log_state_machine_name = log_state_machine_name;
148    this_observer->log_mask               = log_object_mask;
149 
150    this_observer->parent.parent.update = sci_base_state_machine_logger_update;
151 }
152 
153 /**
154  * This is a helper function that will construct the state machine logger and
155  * attach it to the state machine that is to be observed.
156  *
157  * @param[in] this_observer This is the state machine logger object that is
158  *       going to observe the subject state machine.
159  * @param[in] the_state_machine This is the state machine that is under
160  *       observation.
161  * @param[in] the_object This is the object that contains the state machine
162  *       being observed it is used to report the address of the object for
163  *       which a state transition has occurred.
164  * @param[in] the_log_function This is the logging function to be used when a
165  *       state machine transition occurs.  Since this is a base object type it
166  *       does not actually know if the logging function is for the core or
167  *       framework.
168  * @param[in] the_object_name This is the name of the object that contains the
169  *       state machine being observed.
170  * @param[in] the_state_machine_name This is the name that will be displayed
171  *       in the log string for the state machine being observed.
172  * @param[in] the_object_mask This is the log object mask used when calling
173  *       the logging function.
174  *
175  * @return Nothing
176  */
177 void sci_base_state_machine_logger_initialize(
178    SCI_BASE_STATE_MACHINE_LOGGER_T             * this_observer,
179    SCI_BASE_STATE_MACHINE_T                    * the_state_machine,
180    SCI_BASE_OBJECT_T                           * the_object,
181    SCI_BASE_STATE_MACHINE_LOGGER_LOG_HANDLER_T   the_log_function,
182    char                                        * log_object_name,
183    char                                        * log_state_machine_name,
184    U32                                           log_object_mask
185 )
186 {
187    sci_base_state_machine_logger_construct(
188       this_observer, the_object,
189       the_log_function, log_object_name, log_state_machine_name, log_object_mask
190    );
191 
192    sci_base_subject_attach_observer(
193       &the_state_machine->parent, &this_observer->parent.parent
194    );
195 }
196 
197 /**
198  * This is a helper function that will detach this observer from the state
199  * machine that is being observerd.
200  *
201  * @param[in] this_observer This is the observer to detach from the state
202  *       machine.
203  * @parame[in] the_state_machine This is the state machine that is no longer
204  *       going to be observed.
205  *
206  * @return Nothing
207  */
208 void sci_base_state_machine_logger_deinitialize(
209    SCI_BASE_STATE_MACHINE_LOGGER_T * this_observer,
210    SCI_BASE_STATE_MACHINE_T        * the_state_machine
211 )
212 {
213    sci_base_subject_detach_observer(
214       &the_state_machine->parent, &this_observer->parent.parent
215    );
216 }
217 
218 #endif // defined(SCI_LOGGING)
219 
220