Wiselib
|
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_CONTIKI_UARTMODEL_H 00020 #define CONNECTOR_CONTIKI_UARTMODEL_H 00021 00022 #include "external_interface/contiki/contiki_types.h" 00023 #include "util/delegates/delegate.hpp" 00024 #include <stdio.h> 00025 #include <string.h> 00026 extern "C" { 00027 #include "contiki.h" 00028 } 00029 00030 namespace wiselib 00031 { 00032 typedef delegate2<void, uint8_t, uint8_t*> contiki_uart_delegate_t; 00033 typedef contiki_uart_delegate_t uart_delegate_t; 00034 // ----------------------------------------------------------------------- 00035 typedef void (*contiki_uart_fp)( uint8_t, uint8_t* ); 00036 extern contiki_uart_fp contiki_internal_uart_callback; 00037 extern unsigned char base64Table[64]; 00038 // ----------------------------------------------------------------------- 00039 void init_contiki_uart( void ); 00040 int contiki_uart_add_receiver( contiki_uart_delegate_t& delegate ); 00041 void contiki_uart_del_receiver( int idx ); 00042 void contiki_uart_notify_receivers( uint8_t, uint8_t* ); 00043 void encodeBase64( unsigned char b, unsigned char *result ); 00044 // ----------------------------------------------------------------------- 00045 // ----------------------------------------------------------------------- 00046 // ----------------------------------------------------------------------- 00052 template<typename OsModel_P> 00053 class ContikiUartModel 00054 { 00055 public: 00056 typedef OsModel_P OsModel; 00057 00058 typedef ContikiUartModel<OsModel> self_type; 00059 typedef self_type* self_pointer_t; 00060 00061 typedef uint8_t block_data_t; 00062 typedef uint8_t size_t; 00063 // -------------------------------------------------------------------- 00064 enum ErrorCodes 00065 { 00066 SUCCESS = OsModel::SUCCESS, 00067 ERR_UNSPEC = OsModel::ERR_UNSPEC 00068 }; 00069 // -------------------------------------------------------------------- 00070 void init() 00071 { 00072 init_contiki_uart(); 00073 } 00074 // -------------------------------------------------------------------- 00075 int enable_serial_comm() 00076 { 00077 contiki_internal_uart_callback = contiki_uart_notify_receivers; 00078 return SUCCESS; 00079 } 00080 // -------------------------------------------------------------------- 00081 int disable_serial_comm() 00082 { 00083 contiki_internal_uart_callback = 0; 00084 return SUCCESS; 00085 } 00086 // -------------------------------------------------------------------- 00087 int write( size_t len, block_data_t *buf ) 00088 { 00089 unsigned char x[2]; 00090 for ( int i = 0; i < len; i++ ) 00091 { 00092 encodeBase64( buf[i], x ); 00093 printf( "%c%c", x[0], x[1] ); 00094 } 00095 printf( "\n" ); 00096 00097 return SUCCESS; 00098 } 00099 // -------------------------------------------------------------------- 00100 template<class T, void (T::*TMethod)(size_t, block_data_t*)> 00101 inline int reg_read_callback( T *obj_pnt ); 00102 // -------------------------------------------------------------------- 00103 int unreg_recv_callback( int idx ) 00104 { 00105 contiki_uart_del_receiver( idx ); 00106 return SUCCESS; 00107 } 00108 }; 00109 // -------------------------------------------------------------------- 00110 // ----------------------------------------------------------------------- 00111 // ----------------------------------------------------------------------- 00112 template<typename OsModel_P> 00113 template<class T, 00114 void (T::*TMethod)( typename ContikiUartModel<OsModel_P>::size_t, 00115 typename ContikiUartModel<OsModel_P>::block_data_t*)> 00116 int 00117 ContikiUartModel<OsModel_P>:: 00118 reg_read_callback( T *obj_pnt ) 00119 { 00120 contiki_uart_delegate_t delegate = 00121 contiki_uart_delegate_t::from_method<T, TMethod>( obj_pnt ); 00122 return contiki_uart_add_receiver( delegate ); 00123 } 00124 } 00125 00126 #endif