Wiselib
wiselib.testing/external_interface/feuerwhere/feuerwhere_cc1100_radio.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef CONNECTOR_FEUERWHERE_CC1100_RADIOMODEL_H
00020 #define CONNECTOR_FEUERWHERE_CC1100_RADIOMODEL_H
00021 
00022 #include "external_interface/feuerwhere/feuerwhere_types.h"
00023 #include "util/delegates/delegate.hpp"
00024 #include <string.h>
00025 #include <stdarg.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 //#include <iostream>
00029 //#include <sstream>
00030 
00031 
00032 extern "C" {
00033 #include "board.h"
00034 #include "cc1100.h"
00035 //#include "cc1100-interface.h" already included in cc1100.h
00036 }
00037 
00038 enum SpecialNodeIds {
00039   WISELIB_PROTOCOL = 2,
00040   WISELIB_PRIORITY = 3
00041 };
00042 
00043 namespace wiselib
00044 {
00045 
00046    typedef delegate3<void, uint8_t, uint8_t, char*> feuerwhere_radio_delegate_t;
00047    typedef feuerwhere_radio_delegate_t radio_delegate_t;
00048    // -----------------------------------------------------------------------
00049    void init_feuerwhere_cc1100_radio( void );
00050    int feuerwhere_radio_add_receiver( feuerwhere_radio_delegate_t& delegate );
00051    void feuerwhere_radio_del_receiver( int idx );
00052    void feuerwhere_notify_receivers( struct abc_conn *c );
00053    // -----------------------------------------------------------------------
00054    // -----------------------------------------------------------------------
00055    // -----------------------------------------------------------------------
00061    template<typename OsModel_P>
00062    class FeuerwhereRadioModel
00063    {
00064    public:
00065       typedef OsModel_P OsModel;
00066 
00067       typedef FeuerwhereRadioModel<OsModel> self_type;
00068       typedef self_type* self_pointer_t;
00069 
00070       typedef uint8_t node_id_t;
00071       typedef char  block_data_t;
00072       typedef uint8_t  size_t;
00073       typedef uint8_t  message_id_t;
00074       //static void protocol_handler(void* msg, int msg_size, packet_info_t* packet_info);
00075       // --------------------------------------------------------------------
00076       enum ErrorCodes
00077       {
00078    /*
00079          SUCCESS = OsModel::SUCCESS,
00080        ERROR_MNF=CMD_ERROR,
00081          ERR_UNSPEC = OsModel::ERR_UNSPEC,
00082          ERR_NOTIMPL = OsModel::ERR_NOTIMPL
00083    */
00084 
00085          SUCCESS = OsModel::SUCCESS,
00086          ERR_UNSPEC = OsModel::ERR_UNSPEC,
00087          ERR_NOTIMPL = OsModel::ERR_NOTIMPL
00088 
00089       };
00090       // --------------------------------------------------------------------
00092       enum SpecialNodeIds {
00093          BROADCAST_ADDRESS = 0x00, 
00094          NULL_NODE_ID      = -1    
00095       };
00096       // --------------------------------------------------------------------
00097       enum Restrictions {
00098          // max is 58
00099          MAX_MESSAGE_LENGTH = 57 
00100       };
00101       // --------------------------------------------------------------------
00102       int send( node_id_t id, size_t len, block_data_t *data )
00103       {
00104         printf("------- sending to %u: %s (%u chars)\n", id, data, len);
00105         //int res = cc1100_send_csmaca(id, WISELIB_PROTOCOL, PRIORITY_DATA, (char*)data, len);
00106         int res = cc1100_send_csmaca(id, WISELIB_PROTOCOL, PRIORITY_DATA, data, len);
00107 
00108         if (res > 0) {
00109            printf("needed %u retransmissions\r\n", rflags.RTC);
00110            return SUCCESS;
00111         } else {
00112            if (res == RADIO_CS_TIMEOUT) {
00113               printf("CS timeout reached, air was never free!\r\n");
00114            }
00115            return ERR_UNSPEC;
00116         }
00117       }
00118       // --------------------------------------------------------------------
00119       int enable_radio()
00120       {
00121         //return ERR_NOTIMPL;
00122         return SUCCESS;
00123       }
00124       // --------------------------------------------------------------------
00125       int disable_radio()
00126       {
00127          return SUCCESS;
00128       }
00129       // --------------------------------------------------------------------
00130       node_id_t id()
00131       {
00132     //radio_address_t cc1100_get_address(void)
00133     //{
00134    // return radio_address;
00135     //}
00136 //TODO DONE 
00137 //       return radio_cc1100->get_address();
00138          //return ERR_NOTIMPL;
00139        // printf ("The node id is %d\n", cc1100_get_address());
00140         return cc1100_get_address();
00141       }
00142       // --------------------------------------------------------------------
00143       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00144       int reg_recv_callback( T *obj_pnt );
00145       // --------------------------------------------------------------------
00146       int unreg_recv_callback( int idx )
00147       {
00148          feuerwhere_radio_del_receiver( idx );
00149          return SUCCESS;
00150       }
00151    };
00152    // -----------------------------------------------------------------------
00153    // -----------------------------------------------------------------------
00154    // -----------------------------------------------------------------------
00155    template<typename OsModel_P>
00156    template<class T,
00157             void (T::*TMethod)(
00158                       typename FeuerwhereRadioModel<OsModel_P>::node_id_t,
00159                       typename FeuerwhereRadioModel<OsModel_P>::size_t,
00160                       typename FeuerwhereRadioModel<OsModel_P>::block_data_t*)>
00161    int
00162    FeuerwhereRadioModel<OsModel_P>::
00163    reg_recv_callback( T *obj_pnt )
00164    {
00165       feuerwhere_radio_delegate_t delegate =
00166            feuerwhere_radio_delegate_t::from_method<T, TMethod>( obj_pnt );
00167       return feuerwhere_radio_add_receiver( delegate );
00168    }
00169 }
00170 
00171 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines