summaryrefslogtreecommitdiffstats
path: root/libsensors/mlsdk/mllite/mlstates.c
blob: e44f10040c5a7ebe3bea950fa60e059b35f7caf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 $License:
   Copyright 2011 InvenSense, Inc.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
  $
 */
/*******************************************************************************
 *
 * $Id: mlstates.c 5629 2011-06-11 03:13:08Z mcaramello $
 *
 *******************************************************************************/

/**
 *  @defgroup MLSTATES
 *  @brief  Basic state machine definition and support for the Motion Library.
 *
 *  @{
 *      @file mlstates.c
 *      @brief The Motion Library state machine definition.
 */

#define ML_C

/* ------------------ */
/* - Include Files. - */
/* ------------------ */

#include <stdio.h>
#include <string.h>

#include "mlstates.h"
#include "mltypes.h"
#include "mlinclude.h"
#include "ml.h"
#include "mlos.h"

#include <log.h>
#undef MPL_LOG_TAG
#define MPL_LOG_TAG "MPL-mlstates"

#define _stateDebug(x)          //{x}

#define MAX_STATE_CHANGE_PROCESSES (8)

struct state_callback_obj {
    int_fast8_t numStateChangeCallbacks;
    HANDLE mutex;
    state_change_callback_t stateChangeCallbacks[MAX_STATE_CHANGE_PROCESSES];
};

static struct state_callback_obj sStateChangeCallbacks = { 0, 0, { NULL } };

/* --------------- */
/* -  Functions. - */
/* --------------- */

static inv_error_t inv_init_state_callbacks(void)
{
    memset(&sStateChangeCallbacks, 0, sizeof(sStateChangeCallbacks));
    return inv_create_mutex(&sStateChangeCallbacks.mutex);
}

static inv_error_t MLStateCloseCallbacks(void)
{
    inv_error_t result;
    result = inv_destroy_mutex(sStateChangeCallbacks.mutex);
    memset(&sStateChangeCallbacks, 0, sizeof(sStateChangeCallbacks));
    return result;
}

/**
 *  @internal
 *  @brief  return a string containing the label assigned to the given state.
 *  @param  state   The state of which the label has to be returned.
 *  @return A string containing the state label.
**/
char *inv_state_name(unsigned char state)
{
    switch (state) {
    case INV_STATE_SERIAL_CLOSED:
        return INV_STATE_NAME(INV_STATE_SERIAL_CLOSED);
        break;
    case INV_STATE_SERIAL_OPENED:
        return INV_STATE_NAME(INV_STATE_SERIAL_OPENED);
        break;
    case INV_STATE_DMP_OPENED:
        return INV_STATE_NAME(INV_STATE_DMP_OPENED);
        break;
    case INV_STATE_DMP_STARTED:
        return INV_STATE_NAME(INV_STATE_DMP_STARTED);
        break;
    default:
        return NULL;
    }
}

/**
 *  @internal
 *  @brief  Perform a transition from the current state to newState.
 *          Check for the correctness of the transition.
 *          Print out an error message if the transition is illegal .
 *          This routine is also called if a certain normally constant parameters
 *          are changed such as the FIFO Rate.
 *  @param  newState    state we are transitioning to.
 *  @return
**/
inv_error_t inv_state_transition(unsigned char newState)
{
    inv_error_t result = INV_SUCCESS;

    if (newState == INV_STATE_SERIAL_CLOSED) {
        // Always allow transition to closed
    } else if (newState == INV_STATE_SERIAL_OPENED) {
        inv_init_state_callbacks(); // Always allow first transition to start over
    } else if (((newState == INV_STATE_DMP_OPENED) &&
                ((inv_params_obj.state == INV_STATE_SERIAL_OPENED) ||
                 (inv_params_obj.state == INV_STATE_DMP_STARTED)))
               ||
               ((newState == INV_STATE_DMP_STARTED) &&
                (inv_params_obj.state == INV_STATE_DMP_OPENED))) {
        // Valid transitions but no special action required
    } else {
        // All other combinations are illegal
        MPL_LOGE("Error : illegal state transition from %s to %s\n",
                 inv_state_name(inv_params_obj.state),
                 inv_state_name(newState));
        result = INV_ERROR_SM_TRANSITION;
    }

    if (result == INV_SUCCESS) {
        _stateDebug(MPL_LOGV
                    ("ML State transition from %s to %s\n",
                     inv_state_name(inv_params_obj.state),
                     inv_state_name(newState)));
        result = inv_run_state_callbacks(newState);
        if (INV_SUCCESS == result && newState == INV_STATE_SERIAL_CLOSED) {
            MLStateCloseCallbacks();
        }
        inv_params_obj.state = newState;
    }
    return result;
}

/**
 *  @internal
 *  @brief  To be moved in mlstates.c
**/
unsigned char inv_get_state(void)
{
    return (inv_params_obj.state);
}

/**
 * @internal
 * @brief   This registers a function to be called each time the state
 *          changes. It may also be called when the FIFO Rate is changed.
 *          It will be called at the start of a state change before the
 *          state change has taken place. See Also inv_unregister_state_callback()
 *          The FIFO does not have to be on for this callback.
 * @param func Function to be called when a DMP interrupt occurs.
 * @return INV_SUCCESS or non-zero error code.
 */

inv_error_t inv_register_state_callback(state_change_callback_t callback)
{
    INVENSENSE_FUNC_START;
    int kk;
    inv_error_t result;

    result = inv_lock_mutex(sStateChangeCallbacks.mutex);
    if (INV_SUCCESS != result) {
        return result;
    }
    // Make sure we have not filled up our number of allowable callbacks
    if (sStateChangeCallbacks.numStateChangeCallbacks <
        MAX_STATE_CHANGE_PROCESSES) {
        // Make sure we haven't registered this function already
        for (kk = 0; kk < sStateChangeCallbacks.numStateChangeCallbacks; ++kk) {
            if (sStateChangeCallbacks.stateChangeCallbacks[kk] == callback) {
                result = INV_ERROR_INVALID_PARAMETER;
                break;
            }
        }

        if (INV_SUCCESS == result) {
            // Add new callback
            sStateChangeCallbacks.stateChangeCallbacks[sStateChangeCallbacks.
                                                       numStateChangeCallbacks]
                = callback;
            sStateChangeCallbacks.numStateChangeCallbacks++;
        }
    } else {
        result = INV_ERROR_MEMORY_EXAUSTED;
    }

    inv_unlock_mutex(sStateChangeCallbacks.mutex);
    return result;
}

/**
 * @internal
 * @brief   This unregisters a function to be called each time the state
 *          changes. See Also inv_register_state_callback()
 *          The FIFO does not have to be on for this callback.
 * @return INV_SUCCESS or non-zero error code.
 */
inv_error_t inv_unregister_state_callback(state_change_callback_t callback)
{
    INVENSENSE_FUNC_START;
    int kk, jj;
    inv_error_t result;

    result = inv_lock_mutex(sStateChangeCallbacks.mutex);
    if (INV_SUCCESS != result) {
        return result;
    }
    // Make sure we haven't registered this function already
    result = INV_ERROR_INVALID_PARAMETER;
    for (kk = 0; kk < sStateChangeCallbacks.numStateChangeCallbacks; ++kk) {
        if (sStateChangeCallbacks.stateChangeCallbacks[kk] == callback) {
            for (jj = kk + 1;
                 jj < sStateChangeCallbacks.numStateChangeCallbacks; ++jj) {
                sStateChangeCallbacks.stateChangeCallbacks[jj - 1] =
                    sStateChangeCallbacks.stateChangeCallbacks[jj];
            }
            sStateChangeCallbacks.numStateChangeCallbacks--;
            result = INV_SUCCESS;
            break;
        }
    }

    inv_unlock_mutex(sStateChangeCallbacks.mutex);
    return result;
}

inv_error_t inv_run_state_callbacks(unsigned char newState)
{
    int kk;
    inv_error_t result;

    result = inv_lock_mutex(sStateChangeCallbacks.mutex);
    if (INV_SUCCESS != result) {
        MPL_LOGE("MLOsLockMutex returned %d\n", result);
        return result;
    }

    for (kk = 0; kk < sStateChangeCallbacks.numStateChangeCallbacks; ++kk) {
        if (sStateChangeCallbacks.stateChangeCallbacks[kk]) {
            result = sStateChangeCallbacks.stateChangeCallbacks[kk] (newState);
            if (INV_SUCCESS != result) {
                break;          // Can't return, must release mutex
            }
        }
    }

    inv_unlock_mutex(sStateChangeCallbacks.mutex);
    return result;
}