Subversion Repositories HomeAutomation

Rev

Rev 1627 | Rev 1657 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #ifndef CONTROL_NODE_H
  22. #define CONTROL_NODE_H
  23.  
  24. #include <string>
  25. #include <map>
  26.  
  27. #include <boost/shared_ptr.hpp>
  28. #include <boost/signals2.hpp>
  29.  
  30. #include <time.h>
  31.  
  32. #include "common/common.h"
  33. #include "logging/Logger.h"
  34.  
  35. #include "Code.h"
  36. #include <boost/concept_check.hpp>
  37.  
  38. namespace atom {
  39. namespace control {
  40.  
  41. class Node
  42. {
  43. public:
  44.     typedef boost::shared_ptr<Node> Pointer;
  45.     typedef unsigned int Id;
  46.    
  47.     typedef enum
  48.     {
  49.         STATE_INVALID,
  50.         STATE_NO_CHANGE,
  51.         STATE_NORM_OFFLINE,
  52.         STATE_NORM_ONLINE,
  53.         STATE_NORM_LIST,
  54.         STATE_NORM_INITIALIZED,
  55.         STATE_BPGM_OFFLINE,
  56.         STATE_BPGM_START,
  57.         STATE_BPGM_DATA,
  58.         STATE_BPGM_END,
  59.         STATE_BPGM_COPY,
  60.         STATE_APGM_OFFLINE,
  61.         STATE_APGM_START,
  62.         STATE_APGM_DATA,
  63.         STATE_APGM_END,
  64.     } State;
  65.  
  66.     typedef enum
  67.     {
  68.         EVENT_BIOS_START,
  69.         EVENT_APP_START,
  70.         EVENT_HEARTBEAT,
  71.         EVENT_PGM_ACK,
  72.         EVENT_PGM_NACK,
  73.         EVENT_LIST_DONE,
  74.         EVENT_PROGRAM_BIOS,
  75.         EVENT_PROGRAM_APP,
  76.         EVENT_RESET
  77.     } Event;
  78.    
  79.     typedef boost::signals2::signal<void(Id, State, State)> SignalOnNewState;
  80.    
  81.    
  82.     Node(Id id);
  83.     virtual ~Node();
  84.    
  85.     static void SetupStateMachine();
  86.    
  87.     void ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state);
  88.    
  89.     Id GetId();
  90.     State GetState();
  91.  
  92.     void Trigger(Event event, common::StringMap variables);
  93.    
  94.     bool CheckTimeout();
  95.     void ResetTimeout();
  96.    
  97.     void ProgramApplication(Code::Pointer code);
  98.     void ProgramBios(Code::Pointer code);
  99.    
  100. private:
  101.     typedef std::map<Event, State> Transition;
  102.     typedef std::map<State, Transition> TransitionList;
  103.    
  104.     Id id_;
  105.     State state_;
  106.     time_t last_active_;
  107.     SignalOnNewState signal_on_new_state_;
  108.     logging::Logger LOG;
  109.     Code::Pointer code_;
  110.     unsigned int start_offset_;
  111.     unsigned int current_offset_;
  112.     unsigned int expected_ack_data_;
  113.     time_t program_start_time_;
  114.    
  115.    
  116.     static TransitionList transitions_;
  117.     static std::map<State, std::string> state_names_;
  118.     static std::map<Event, std::string> event_names_;
  119.    
  120.     static void AddTransition(State current_state, Event event, State target_state);
  121.    
  122.     State GetTransitionTarget(Event event);
  123.     void SendReset();
  124.     void SendListRequest();
  125. };
  126.  
  127. }; // namespace control
  128. }; // namespace atom
  129.  
  130. #endif // CONTROL_NODE_H
  131.