MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/communication/mavlink_message_handler.hxx
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file mavlink_message_handler.hxx
00034  *
00035  * \author MAV'RIC Team
00036  * \author Julien Lecoeur
00037  *
00038  * \brief This module handles of all incoming MAVLink message by calling the
00039  * appropriate functions
00040  *
00041  ******************************************************************************/
00042 
00043 template<typename T>
00044 bool Mavlink_message_handler::add_msg_callback(uint8_t                          message_id,
00045                                                uint8_t                          sysid_filter,
00046                                                mav_component_t                  compid_filter,
00047                                                typename msg_function<T>::type_t function,
00048                                                T*                               module_struct)
00049 {
00050     bool add_callback_success = true;
00051 
00052     if (function == NULL || module_struct == NULL )
00053     {
00054         print_util_dbg_print("[MESSAGE HANDLER] Error: null pointer.\r\n");
00055 
00056         add_callback_success &= false;
00057     }
00058     else
00059     {
00060         if (msg_callback_count_ <  msg_callback_max_count())
00061         {
00062             msg_callback_t* new_callback = &msg_callback_list()[msg_callback_count_];
00063 
00064             new_callback->message_id    = message_id;
00065             new_callback->sysid_filter  = sysid_filter;
00066             new_callback->compid_filter = compid_filter;
00067             new_callback->function      = reinterpret_cast<msg_function<void>::type_t>(function);      // we do dangerous casting here, but it is safe because
00068             new_callback->module_struct = reinterpret_cast<void*>(module_struct);                      // the types of telemetry_function and telemetry_argument are compatible
00069 
00070             sort_latest_msg_callback();
00071 
00072             msg_callback_count_ += 1;
00073 
00074             add_callback_success &= true;
00075         }
00076         else
00077         {
00078             print_util_dbg_print("[MESSAGE HANDLER] Error: Cannot add more msg callback\r\n");
00079 
00080             add_callback_success &= false;
00081         }
00082     }
00083 
00084     return add_callback_success;
00085 }
00086 
00087 
00088 template<typename T>
00089 bool Mavlink_message_handler::add_cmd_callback( uint16_t                            command_id,
00090                                                 uint8_t                             sysid_filter,
00091                                                 mav_component_t                     compid_filter,
00092                                                 mav_component_t                     compid_target,
00093                                                 typename cmd_function<T>::type_t    function,
00094                                                 T*                                  module_struct)
00095 {
00096     bool add_callback_success = true;
00097 
00098     if (function == NULL || module_struct == NULL )
00099     {
00100         print_util_dbg_print("[MESSAGE HANDLER] Error: null pointer.\r\n");
00101 
00102         add_callback_success &= false;
00103     }
00104     else
00105     {
00106         if (cmd_callback_count_ <  cmd_callback_max_count())
00107         {
00108             cmd_callback_t* new_callback = &cmd_callback_list()[cmd_callback_count_];
00109 
00110             new_callback->command_id    = command_id;
00111             new_callback->sysid_filter  = sysid_filter;
00112             new_callback->compid_filter = compid_filter;
00113             new_callback->compid_target = compid_target;
00114             new_callback->function      = reinterpret_cast<cmd_function<void>::type_t>(function);      // we do dangerous casting here, but it is safe because
00115             new_callback->module_struct = reinterpret_cast<void*>(module_struct);                      // the types of telemetry_function and telemetry_argument are compatible
00116 
00117             sort_latest_cmd_callback();
00118 
00119             cmd_callback_count_ += 1;
00120 
00121             add_callback_success &= true;
00122         }
00123         else
00124         {
00125             print_util_dbg_print("[MESSAGE HANDLER] Error: Cannot add more msg callback\r\n");
00126 
00127             add_callback_success &= false;
00128         }
00129     }
00130 
00131     return add_callback_success;
00132 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines