Wiselib
wiselib.stable/external_interface/lorien/lorien_os_component.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 #include "external_interface/lorien/lorien_os.h"
00020 #include "external_interface/lorien/lorien_timer.h"
00021 #include "external_interface/lorien/lorien_radio.h"
00022 
00023 //NOTE: This is written in raw Lorien Component Assembly, which is bad (function names and record attributes are not stable)
00024 
00025 
00026 void application_main( Component& );
00027 
00028 namespace wiselib
00029 {
00030 //    LorienOsModel lorien_os;
00031    
00032    
00033    // -----------------------------------------------------------------------
00034    /* TODO: Find a better solution for the following:
00035     * This forces the msp430-g++ to create contructor and destructor in the
00036     * ctor/dtor-section. When this is done, you can call tr = tree_routing_t()
00037     * locally in a method. Otherwise, when this global dummy is not created
00038     * (and thus no ctor/dtor), tr = tree_routing_t() fails.
00039     */
00040    //tree_routing_t dummy;
00041    // -----------------------------------------------------------------------
00042    static unsigned int send( Component *comp, unsigned char *data, size_t len, unsigned int port )
00043    {
00044       //((LXState*)comp->state)->routing.send( 0, len, data );
00045       return OK;
00046    }
00047    // -----------------------------------------------------------------------
00048    static unsigned int sendTo( Component *comp, unsigned char *data, size_t len, unsigned int id, unsigned int port )
00049    {
00050       //((LXState*)comp->state)->routing.send( id, len, data );
00051       return OK;
00052    }
00053    // -----------------------------------------------------------------------
00054    static int reg_recv_callback( Component *comp, Component *r, unsigned int port )
00055    {
00056       //?!
00057       //oc_regReceptacle(comp, (char*)"IReceiver_Int", sizeof(IReceiver_Int));
00058       //comp -> kernel -> bind(comp, r, (char*)"IReceiver_Int", NULL);
00059 
00060       // TODO: When Wiselib receives message, call r. See contiki_radio of how this works.
00061       // TODO: This method can only be called *once*. Quick: Introduce state and return error
00062       //       when registered second time. Long-Term: See sontiki_radio
00063 
00064       return OK;
00065    }
00066    // -----------------------------------------------------------------------
00067    static int unreg_recv_callback( Component *comp, Component *r, unsigned int port )
00068    {
00069       //?!
00070       //comp -> kernel -> unbind(comp -> kernel -> getBinding(comp, (char*)"IReceiver_Int"));
00071       //oc_delReceptacle(comp, (char*)"IReceiver_Int");
00072 
00073       return OK;
00074    }
00075    // -----------------------------------------------------------------------
00076    static unsigned int get_id( Component *comp)
00077    {
00078       return ((IMXRadio*) ((LXState*) comp -> state) -> radio -> userRecp) -> getID(((LXState*) comp -> state) -> radio -> ifHostComponent);
00079    }
00080    // -----------------------------------------------------------------------
00081    static int set_id( Component *comp, int id )
00082    {
00083       //NI
00084 
00085       return OK;
00086    }
00087    // -----------------------------------------------------------------------
00088    static const char* get_id_type( Component *comp)
00089    {
00090       return "int";
00091    }
00092    // -----------------------------------------------------------------------
00093    static int enable( Component *comp)
00094    {
00095       //((LXState*) comp -> state) -> routing.enable();
00096       return OK;
00097    }
00098    // -----------------------------------------------------------------------
00099    static int disable( Component *comp)
00100    {
00101       //((LXState*) comp -> state) -> routing.disable();
00102       return OK;
00103    }
00104    // -----------------------------------------------------------------------
00105    // -----------------------------------------------------------------------
00106    // -----------------------------------------------------------------------
00107    static int timer_elapse(Component *comp, void *tid, void *ptr_o )
00108    {
00109       LorienTimerModel<LorienOsModel>::timer_elapsed(ptr_o);
00110       return OK;
00111    }
00112    // -----------------------------------------------------------------------
00113    static int osa_recv( Component *comp, unsigned char *data, size_t len, unsigned int from_id, unsigned int port )
00114    {
00115       LorienRadioModel<LorienOsModel>::received(data, len, from_id);
00116       return OK;
00117    }
00118    // -----------------------------------------------------------------------
00119    static int start(Component *comp)
00120    {
00121       //register with the radio
00122       ((IMXRadio*) ((LXState*) comp -> state) -> radio -> userRecp) -> registerCallback(((LXState*) comp -> state) -> radio -> ifHostComponent, comp, WISELIB_PORT);
00123       
00124       application_main( *comp );
00125 
00126       return OK;
00127    }
00128    // -----------------------------------------------------------------------
00129    static int stop(Component *comp)
00130    {
00131       //unregister with the radio
00132       ((IMXRadio*) ((LXState*) comp -> state) -> radio -> userRecp) -> unregisterCallback(((LXState*) comp -> state) -> radio -> ifHostComponent, comp, WISELIB_PORT);
00133 
00134       return OK;
00135    }
00136    // -----------------------------------------------------------------------
00137    // -----------------------------------------------------------------------
00138    // -----------------------------------------------------------------------
00139    extern "C" int construct( Component *comp )
00140    {
00141       int nerror = OK;
00142       /* set up my interfaces */
00143       IMXRadio *ir = NULL;
00144       IHWControl *ihc = NULL;
00145       ILife *il = NULL;
00146       ITimerCallback *itc = NULL;
00147       IMXRadioCallback *irc = NULL;
00148       
00149       LXState *st = (LXState*) malloc(sizeof(LXState));
00150       
00151       if (st == NULL)
00152          return OUT_OF_MEMORY;
00153       
00154       oc_regState(comp, st);
00155       
00156       if ((nerror = oc_regInterfaces(comp, 5, "IMXRadio", NULL, &ir, sizeof(IMXRadio),
00157                                                 "IHWControl", NULL, &ihc, sizeof(IHWControl),
00158                                                 "ILife", NULL, &il, sizeof(ILife),
00159                                                 "ITimerCallback", NULL, &itc, sizeof(ITimerCallback),
00160                                                 "IMXRadioCallback", NULL, &irc, sizeof(IMXRadioCallback))) != OK)
00161                                                 return nerror;
00162 
00163       ir->send = send;
00164       ir->sendTo = sendTo;
00165       ir->registerCallback = reg_recv_callback;
00166       ir->unregisterCallback = unreg_recv_callback;
00167       ir->getID = get_id;
00168 
00169       irc -> recv = osa_recv;
00170 
00171       itc -> timeout = timer_elapse;
00172 
00173       ihc -> enable = enable;
00174       ihc -> disable = disable;
00175       
00176       il -> start = start;
00177       il -> stop = stop;
00178 
00179       if ((nerror = oc_regReceptacles(comp, 3,
00180                         "IMXRadio", NULL, &st -> radio, sizeof(IMXRadio),
00181                         "ITimer", NULL, &st -> timer, sizeof(ITimer),
00182                         "IPort", NULL, &st -> port, sizeof(IPort))) != OK)
00183       {
00184          return nerror;
00185       }
00186 
00187       return OK;
00188    }
00189    // -----------------------------------------------------------------------
00190    extern "C" int destruct( Component *comp )
00191    {
00192       return OK;
00193    }
00194 
00195 }
00196 //---------------------------------------------------------------------------
00197 //---------------------------------------------------------------------------
00198 //---------------------------------------------------------------------------
00199 void* operator new ( size_t size )
00200 {
00201    return malloc( size );
00202 }
00203 //---------------------------------------------------------------------------
00204 void operator delete ( void *pointer )
00205 {
00206    free( pointer );
00207 }
00208 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines