Wiselib
wiselib.testing/external_interface/iphone/iphone_radio.h
Go to the documentation of this file.
00001 #ifndef CONNECTOR_IPHONE_RADIOMODEL_H
00002 #define CONNECTOR_IPHONE_RADIOMODEL_H
00003 
00004 #include "external_interface/iphone/iphone_system.h"
00005 #include "external_interface/iphone/iphone_os_model.h"
00006 #include "util/delegates/delegate.hpp"
00007 #include "util/iphone_node_api/iphone_link_message.h"
00008 #include "util/base_classes/radio_base.h"
00009 
00010 #include "util/serialization/simple_types.h"
00011 #include "util/pstl/vector_static.h"
00012 
00013 #include "iPhoneRadioController.h"
00014 
00015 namespace wiselib
00016 {
00021    template<typename OsModel_P, bool IPhoneLinkRadio = false>
00022    class iPhoneRadioModel
00023       : public RadioBase<OsModel_P, uint16_t, __darwin_size_t, uint8_t>
00024    {
00025    public:
00026       typedef OsModel_P OsModel;
00027       typedef iPhoneRadioModel<OsModel> self_type;
00028       typedef self_type* self_pointer_t;
00029 
00030       typedef IPhoneLinkMessage<OsModel, typename OsModel::Radio> Message;
00031       
00032       typedef uint16_t node_id_t;
00033       typedef uint8_t  block_data_t;
00034       typedef __darwin_size_t  size_t;
00035       typedef uint8_t  message_id_t;
00036    
00037       typedef delegate3<void, node_id_t, size_t, block_data_t*> radio_delegate_t;
00038       // --------------------------------------------------------------------
00039       enum ErrorCodes
00040       {
00041          SUCCESS = OsModel::SUCCESS,
00042          ERR_UNSPEC = OsModel::ERR_UNSPEC
00043       };
00044       // --------------------------------------------------------------------
00045       enum SpecialNodeIds {
00046          BROADCAST_ADDRESS = 0xffff, 
00047          NULL_NODE_ID      = 0       
00048       };
00049       // --------------------------------------------------------------------
00050       enum Restrictions {
00051          // todo
00052          MAX_MESSAGE_LENGTH = 512    
00053       };
00054       // --------------------------------------------------------------------
00055       iPhoneRadioModel(iPhoneSystem& system)
00056          : system_(system)
00057       {
00058          iPhoneRadio_ = [[iPhoneRadioController alloc] init];
00059       }
00060       // --------------------------------------------------------------------
00061       virtual ~iPhoneRadioModel()
00062       {
00063          [iPhoneRadio_ release];
00064          iPhoneRadio_ = nil;
00065       }
00066       
00067       // --------------------------------------------------------------------
00068       void set_testbed( NSString* url, UInt16 port )
00069       {
00070          [iPhoneRadio_ setUrl:url andPort:port];
00071       }
00072       // --------------------------------------------------------------------
00073       int enable_radio()
00074       {
00075 #ifdef IPHONE_RADIO_DEBUG
00076       NSLog(@"IPHONE_RADIO_DEBUG: Radio enable");
00077 #endif
00078          if([iPhoneRadio_ enable] == SUCCESS) {
00079             radio_delegate_t d = radio_delegate_t::template from_method<self_type, &self_type::receive_message>(this);
00080             [iPhoneRadio_ registerReceiver: d];
00081             
00082             return SUCCESS;
00083          }
00084          return ERROR_UNSPEC;
00085       }
00086       // --------------------------------------------------------------------
00087       int disable_radio()
00088       {
00089 #ifdef IPHONE_RADIO_DEBUG
00090       NSLog(@"IPHONE_RADIO_DEBUG: Radio disable");
00091 #endif
00092          return [iPhoneRadio_ disable];
00093       }
00094       // --------------------------------------------------------------------
00095       node_id_t id()
00096       {    
00097          return [iPhoneRadio_ getRadioId];
00098       }
00099       // --------------------------------------------------------------------
00100       int send( node_id_t to, size_t len, block_data_t *data )
00101       {
00102 #ifdef IPHONE_RADIO_DEBUG
00103       NSLog(@"IPHONE_RADIO_DEBUG: Send %d with lenght %d", to, len);
00104 #endif
00105 
00106          if(!IPhoneLinkRadio) {
00107             return [iPhoneRadio_ sendTo:to withData:data withLenght:len];
00108          
00109          } else {
00110             Message message;
00111             message.set_command_type( IPHONE_LINK_MESSAGE );
00112             message.set_destination( to );
00113             message.set_source( this->id() );
00114             message.set_payload( len, data );
00115          
00116             [iPhoneRadio_ sendTo:to withData:(uint8_t*)(&message) withLenght:message.buffer_size()];
00117             return [iPhoneRadio_ sendTo:to withData:(uint8_t*)(&message) withLenght:message.buffer_size()];
00118          }
00119             
00120       }
00121       // --------------------------------------------------------------------
00122       void receive_message( node_id_t from, size_t len, block_data_t *buf )
00123       {
00124             
00125                   
00126          if(!IPhoneLinkRadio) {
00127             this->notify_receivers( from, len, buf );
00128          
00129          } else {
00130             switch (*buf) {
00131                case IPHONE_LINK_MESSAGE: 
00132                {
00133 
00134                   Message *msg = (Message*)buf;
00135                   
00136                   #ifdef IPHONE_RADIO_DEBUG
00137                      NSLog( @"IPHONE_RADIO_DEBUG: RECEIVED IPhoneLinkMessage from %d to %d with size %d",
00138                         (uint32_t)msg->source(), (uint32_t)msg->destination(), msg->payload_length() );
00139                   #endif
00140                   
00141                   this->notify_receivers( msg->source() , msg->payload_length(), msg->payload() );
00142                } break;
00143                
00144                default:
00145                {
00146                   #ifdef IPHONE_RADIO_DEBUG
00147                      NSLog( @"IPHONE_RADIO_DEBUG: Received message is not a IPhoneLinkMessage" );
00148                   #endif
00149                } break;
00150             }
00151          }
00152          
00153       }
00154       
00155    private:
00156       iPhoneSystem& system_;
00157       iPhoneRadioController* iPhoneRadio_;
00158    };
00159 }
00160 
00161 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines