Wiselib
wiselib.testing/util/glab_rest_connector/swap_service.h
Go to the documentation of this file.
00001 /* 
00002  * File:   ssd_data_container.h
00003  * Author: maxpagel
00004  *
00005  * Created on 27. Dezember 2010, 16:29
00006  */
00007 
00008 #ifndef _SSD_DATA_CONTAINER_H
00009 #define  _SSD_DATA_CONTAINER_H
00010 
00011 #include "external_interface/default_return_values.h"
00012 #include "util/wisebed_node_api/command_types.h"
00013 #include "util/delegates/delegate.hpp"
00014 #include "util/serialization/simple_types.h"
00015 #include "util/pstl/map_static_vector.h"
00016 #include "util/pstl/list_static.h"
00017 #include "util/pstl/static_string.h"
00018 #include "util/delegates/delegate.hpp"
00019 #include "config_testing.h"
00020 #include <stdint.h>
00021 #include "swap_request_message.h"
00022 #include "swap_response_message.h"
00023 #include "request_types.h"
00024 #include "util/pstl/pair.h"
00025 #include "util/wisebed_node_api/sensors/sensor_controller.h"
00026 
00027 namespace wiselib{
00028 
00029    template<typename OsModel_P,
00030             typename Uart_P,
00031             typename SensorControler_P,
00032             typename Debug_P = typename OsModel_P::Debug,
00033             typename Timer_P = typename OsModel_P::Timer>
00034    class SwapService{
00035       
00036       typedef OsModel_P OsModel;
00037       typedef Uart_P Uart;
00038       typedef Timer_P Timer;
00039       typedef Debug_P Debug;
00040       typedef SensorControler_P SensorControler;
00041       typedef SwapService<OsModel,Uart,SensorControler,Debug,Timer> self_type;
00042       typedef SwapRequestMessage<OsModel> request_t;
00043       typedef SwapResponseMessage<OsModel> response_t;
00044       typedef typename Uart::block_data_t block_data_t;
00045       typedef typename Uart::size_t size_t;
00046       typedef wiselib::vector_static<OsModel,StaticString,4> meta_data_list_t;
00047 //      typedef wiselib::MapStaticVector<OsModel,uint8_t,StaticString,10 >  sensor_meta_data_map_t;
00048       typedef wiselib::MapStaticVector<OsModel,uint8_t,meta_data_list_t,2 >  sensor_meta_data_map_t;
00049 //      typedef wiselib::MapStaticVector<OsModel,StaticString,long,3 >  subscriptions_map_t;
00050       
00051 //      typedef wiselib::MapStaticVector<OsModel, int, pair<StaticString, delegate0<char*> >, 10> sensor_map_t;
00052 //      typedef wiselib::SensorController<OsModel, sensor_map_t, StaticString> sensor_controller_t;
00053 
00054 
00055    public:
00056 
00057        SwapService()
00058         :uart_(0),
00059                 timer_(0),
00060                 debug_(0)
00061         {
00062 
00063         }
00064   // --------------------------------------------------------------------
00065 //       typename subscriptions_map_t::reference subscriptions()
00066 //       {
00067 //         return &subscriptions_;
00068 //       }
00069 // --------------------------------------------------------------------
00070        StaticString& node_meta_data()
00071        {
00072            return &node_Meta_data_;
00073        }
00074 // --------------------------------------------------------------------
00075        void set_node_meta_dat(const char* str)
00076        {
00077            node_Meta_data_ = str;
00078        }
00079 // --------------------------------------------------------------------
00080        typename sensor_meta_data_map_t::reference sensor_meta_data()
00081        {
00082            return &sensor_meta_data_;
00083        }
00084 // --------------------------------------------------------------------
00085       void addSensorMetaData(uint8_t id, StaticString data)
00086       {
00087                     if(sensor_meta_data_[id].size()<3)
00088                     {
00089                         #ifdef SWAP_SERVICE_DEBUG
00090                         debug_->debug("adding sendor meta data");
00091                         #endif
00092                         sensor_meta_data_[id].push_back(data);
00093                     }
00094       }
00095 // --------------------------------------------------------------------
00096 //       typename sensor_list_t::reference sensors()
00097 //       {
00098 //           return &sensors_;
00099 //       }
00100 // --------------------------------------------------------------------
00101        void timeout_push(void*)
00102        {
00103 
00104        }
00105 // --------------------------------------------------------------------
00106        template<class T, void (T::*TMethod)(self_type*)>
00107        int subscribe(int interval,T *obj_pnt)
00108        {
00109            timer_->template set_timer<self_type, &self_type::timer_elapsed>(interval, this, 0);
00110            return 0;
00111        }
00112 // --------------------------------------------------------------------
00113        void timer_elapsed(void* ){
00114 
00115        }
00116 // --------------------------------------------------------------------
00117        void init(Uart& uart,SensorControler& sensControl,Debug& debug, Timer& timer)
00118        {
00119            uart_ = &uart;
00120            sensor_controler_ = &sensControl;
00121            timer_ = &timer;
00122            debug_ = &debug;
00123            uart_->template reg_read_callback<self_type,
00124                          &self_type::message_received>( this );
00125        }
00126 // --------------------------------------------------------------------
00127        void message_received( size_t length, block_data_t *data )
00128        {
00129            #ifdef SWAP_SERVICE_DEBUG
00130            debug_->debug("swap service received message(%d), type(%d)\n",data[0],data[2]);
00131            #endif
00132            request_t *request = (request_t*) data;
00133            if(request->command_type() == SSD_REST_REQUEST)
00134            {               
00135                block_data_t temp;
00136                uart_->new_packet();
00137                temp = (block_data_t) SSD_REST_RESPONSE;
00138                write_escape_packet(1,&temp);
00139                temp = (block_data_t) request->request_id();
00140                write_escape_packet(1,&temp);
00141                
00142                switch ( request->request_type())
00143                {
00144                    case GET_SENSORS:
00145                    {
00146                        temp = (block_data_t) DefaultReturnValues<OsModel>::SUCCESS;
00147                        write_escape_packet(1,&temp);
00148                        StaticString sensors = sensor_controler_->sensors();
00149                        #ifdef SWAP_SERVICE_DEBUG
00150                        debug_->debug("sending sensorids string lkength = %d", sensors.length());
00151                        debug_->debug(sensors.c_str());
00152                        #endif
00153                        temp = (block_data_t) sensors.length();
00154                        write_escape_packet(1,&temp);
00155                        write_escape_packet(sensors);
00156                    }
00157                    break;
00158                    case GET_NODE_META_DATA:
00159                    {
00160                        #ifdef SWAP_SERVICE_DEBUG
00161                        debug_->debug("sending node meta data");
00162                        #endif
00163                        temp = (block_data_t) DefaultReturnValues<OsModel>::SUCCESS;
00164                        write_escape_packet(1,&temp);                       
00165                        temp = (block_data_t) node_Meta_data_.length();
00166                        write_escape_packet(1,&temp);
00167                        write_escape_packet(node_Meta_data_);
00168                    }
00169                    break;
00170                    case POST_SENSOR_META_DATA:
00171                    {
00172                        #ifdef SWAP_SERVICE_DEBUG
00173                        debug_->debug("received sensor meta data for sensor %d",*request->request_opts());
00174                        #endif
00175                         if(sensor_controler_->has_sensor(*request->request_opts()))
00176                        {                           
00177                            block_data_t* request_opts = request->request_opts();
00178                            request_opts++;
00179                            block_data_t* size = request_opts;
00180                            request_opts++;
00181                            StaticString meta_data = StaticString((char*)request_opts,(char)*size);
00182                            #ifdef SWAP_SERVICE_DEBUG
00183                            debug_->debug(meta_data.c_str());
00184                            #endif
00185                            meta_data.append("\n");
00186                            addSensorMetaData(*request->request_opts(),meta_data);
00187 
00188                            temp = (block_data_t) DefaultReturnValues<OsModel>::SUCCESS;
00189                            write_escape_packet(1,&temp);
00190                            temp = (block_data_t) 0;
00191                            write_escape_packet(1,&temp);
00192                        }else
00193                        {
00194                            #ifdef SWAP_SERVICE_DEBUG
00195                            debug_->debug("sensor not found");
00196                            #endif
00197                            temp = (block_data_t) DefaultReturnValues<OsModel>::ERR_UNSPEC;
00198                            write_escape_packet(1,&temp);
00199                        }
00200                    }
00201                    break;
00202                    case GET_SENSOR_META_DATA:
00203                    {
00204 
00205                        if(sensor_controler_->has_sensor(*request->request_opts()))
00206                        {
00207                            #ifdef SWAP_SERVICE_DEBUG
00208                            debug_->debug("sending sensor meta data from sensor %d",*request->request_opts());
00209                            #endif
00210                            temp = (block_data_t) DefaultReturnValues<OsModel>::SUCCESS;
00211                            write_escape_packet(1,&temp);
00212                            int size = 0;
00213                            for(typename meta_data_list_t::iterator it = sensor_meta_data_[*request->request_opts()].begin(),
00214                             end = sensor_meta_data_[*request->request_opts()].end();it!=end;++it)
00215                            {
00216                                size+=it->length();
00217                            }
00218                            temp = (block_data_t) size;
00219                            write_escape_packet(1,&temp);
00220                            for(typename meta_data_list_t::iterator it = sensor_meta_data_[*request->request_opts()].begin(),
00221                             end = sensor_meta_data_[*request->request_opts()].end();it!=end;++it)
00222                            {
00223                                StaticString& smd = (*it);
00224                                write_escape_packet(smd);
00225                                debug_->debug(smd.c_str());
00226                            }
00227 
00228                        }else
00229                        {
00230                            temp = (block_data_t) DefaultReturnValues<OsModel>::ERR_UNSPEC;
00231                            write_escape_packet(1,&temp);
00232                        }
00233 
00234 
00235                    }
00236                    break;
00237                    case GET_SENSOR_VALUE:
00238                    {
00239                        #ifdef SWAP_SERVICE_DEBUG
00240                         debug_->debug("requesting sensor with id %d",(uint8_t)*request->request_opts());
00241                            #endif
00242                        if(sensor_controler_->has_sensor((uint8_t)*request->request_opts()))
00243                        {
00244                            #ifdef SWAP_SERVICE_DEBUG
00245                            debug_->debug("sending senor value for sensor %d\n",*request->request_opts());
00246                            #endif
00247                            temp = (block_data_t) DefaultReturnValues<OsModel>::SUCCESS;
00248                            write_escape_packet(1,&temp);
00249                            char *value = sensor_controler_->value((uint8_t)*request->request_opts());
00250                            #ifdef SWAP_SERVICE_DEBUG
00251                            debug_->debug("value = %d %d %d\n",value[0],value[1],value[2]);
00252                            debug_->debug("length %d", 2+((size_t)value[1]));
00253                            #endif
00254 
00255                            temp = (block_data_t) (2+((size_t)value[1]));
00256                            
00257                            write_escape_packet(1,&temp);                           
00258 
00259                            write_escape_packet(2+((size_t)value[1]),(block_data_t*)value);
00260                        }
00261                        else
00262                        {
00263                            temp = (block_data_t) DefaultReturnValues<OsModel>::ERR_UNSPEC;
00264                            write_escape_packet(1,&temp);
00265                            temp = (block_data_t) 0;
00266                            write_escape_packet(1,&temp);
00267                        }
00268                    }
00269                    break;
00270                    case SUBSCRIBE:
00271                    {
00272                        temp = (block_data_t) DefaultReturnValues<OsModel>::ERR_NOTIMPL;
00273                        write_escape_packet(1,&temp);
00274                        temp = (block_data_t) 0;
00275                        write_escape_packet(1,&temp);
00276                    }
00277                    break;
00278                    case CANCEL_SUBSCRIPTION:
00279                    {
00280                        temp = (block_data_t) DefaultReturnValues<OsModel>::ERR_NOTIMPL;
00281                        write_escape_packet(1,&temp);
00282                        temp = (block_data_t) 0;
00283                        write_escape_packet(1,&temp);
00284 
00285                    }
00286                    break;
00287                    default:
00288                        temp = (block_data_t) DefaultReturnValues<OsModel>::ERR_NOTIMPL;
00289                        write_escape_packet(1,&temp);
00290                        temp = (block_data_t) 0;
00291                        write_escape_packet(1,&temp);
00292                    break;
00293                }
00294                uart_->end_packet();
00295                
00296            }
00297        }
00298 // --------------------------------------------------------------------
00299    private:
00300        enum { DLE = 0x10, STX = 0x02, ETX = 0x03 };
00301 
00302        void write_escape_packet( size_t len,block_data_t* buffer)
00303        {
00304             
00305 
00306             for (size_t i = 0; i < len; i++) {
00307                 //DLE characters must be sent twice.
00308                 if ((uint8_t) buffer[i] == DLE)
00309                 {
00310                     block_data_t temp = (block_data_t) DLE;
00311                     uart_->write(1,&temp);
00312                 }
00313 
00314                 uart_->write(1, &buffer[i]);
00315             }
00316         }
00317        void write_escape_packet(StaticString& buffer)
00318        {
00319            write_escape_packet(buffer.length(),(block_data_t*)(buffer.c_str()));
00320         }
00321        void write_excape_packet(const char* str)
00322        {
00323            StaticString stStr = str;
00324            write_escape_packet(stStr);
00325        }      
00326        
00327        sensor_meta_data_map_t sensor_meta_data_;
00328 //       subscriptions_map_t subscriptions_;
00329        StaticString node_Meta_data_;
00330        Uart* uart_;
00331        SensorControler* sensor_controler_;
00332        Timer* timer_;
00333        Debug* debug_;
00334 
00335 
00336    };
00337 
00338 }
00339 
00340 #endif   /* _SSD_DATA_CONTAINER_H */
00341 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines