start_manager.c 2.9 KB

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