1/*
2 $License:
3    Copyright (C) 2011-2012 InvenSense Corporation, All Rights Reserved.
4    See included License.txt for License information.
5 $
6 */
7
8/*******************************************************************************
9 *
10 * $Id:$
11 *
12 ******************************************************************************/
13/**
14 *   @defgroup  Start_Manager start_manager
15 *   @brief     Motion Library - Start Manager
16 *              Start Manager
17 *
18 *   @{
19 *       @file start_manager.c
20 *       @brief This handles all the callbacks when inv_start_mpl() is called.
21 */
22
23
24#include <string.h>
25#include "log.h"
26#include "start_manager.h"
27
28typedef inv_error_t (*inv_start_cb_func)();
29struct inv_start_cb_t {
30    int num_cb;
31    inv_start_cb_func start_cb[INV_MAX_START_CB];
32};
33
34static struct inv_start_cb_t inv_start_cb;
35
36/** Initilize the start manager. Typically called by inv_start_mpl();
37* @return Returns INV_SUCCESS if successful or an error code if not.
38*/
39inv_error_t inv_init_start_manager(void)
40{
41    memset(&inv_start_cb, 0, sizeof(inv_start_cb));
42    return INV_SUCCESS;
43}
44
45/** Removes a callback from start notification
46* @param[in] start_cb function to remove from start notification
47* @return Returns INV_SUCCESS if successful or an error code if not.
48*/
49inv_error_t inv_unregister_mpl_start_notification(inv_error_t (*start_cb)(void))
50{
51    int kk;
52
53    for (kk=0; kk<inv_start_cb.num_cb; ++kk) {
54        if (inv_start_cb.start_cb[kk] == start_cb) {
55            // Found the match
56            if (kk != (inv_start_cb.num_cb-1)) {
57                memmove(&inv_start_cb.start_cb[kk],
58                    &inv_start_cb.start_cb[kk+1],
59                    (inv_start_cb.num_cb-kk-1)*sizeof(inv_start_cb_func));
60            }
61            inv_start_cb.num_cb--;
62            return INV_SUCCESS;
63        }
64    }
65    return INV_ERROR_INVALID_PARAMETER;
66}
67
68/** Register a callback to receive when inv_start_mpl() is called.
69* @param[in] start_cb Function callback that will be called when inv_start_mpl() is
70*            called.
71* @return Returns INV_SUCCESS if successful or an error code if not.
72*/
73inv_error_t inv_register_mpl_start_notification(inv_error_t (*start_cb)(void))
74{
75    if (inv_start_cb.num_cb >= INV_MAX_START_CB)
76        return INV_ERROR_INVALID_PARAMETER;
77
78    inv_start_cb.start_cb[inv_start_cb.num_cb] = start_cb;
79    inv_start_cb.num_cb++;
80    return INV_SUCCESS;
81}
82
83/** Callback all the functions that want to be notified when inv_start_mpl() was
84* called.
85* @return Returns INV_SUCCESS if successful or an error code if not.
86*/
87inv_error_t inv_execute_mpl_start_notification(void)
88{
89    inv_error_t result,first_error;
90    int kk;
91
92    first_error = INV_SUCCESS;
93
94    for (kk = 0; kk < inv_start_cb.num_cb; ++kk) {
95        result = inv_start_cb.start_cb[kk]();
96        if (result && (first_error == INV_SUCCESS)) {
97            first_error = result;
98        }
99    }
100    return first_error;
101}
102
103/**
104 * @}
105 */
106