Subversion Repositories HomeAutomation

Rev

Rev 1788 | 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. #include <boost/algorithm/string.hpp>
  30.  
  31. #include <time.h>
  32.  
  33. #include "common/common.h"
  34. #include "logging/Logger.h"
  35.  
  36. #include "Code.h"
  37. #include <boost/concept_check.hpp>
  38.  
  39. namespace atom {
  40. namespace control {
  41.  
  42. class Node
  43. {
  44. public:
  45.     typedef boost::shared_ptr<Node> Pointer;
  46.     typedef std::string Id;
  47.    
  48.     typedef struct
  49.     {
  50.         bool valid_;
  51.         unsigned int bios_version_;
  52.         std::string device_type_;
  53.         bool has_application_;
  54.         time_t last_active_;
  55.     } Information;
  56.    
  57.     typedef enum
  58.     {
  59.         STATE_INVALID,
  60.         STATE_NO_CHANGE,
  61.         STATE_NORM_OFFLINE,
  62.         STATE_NORM_ONLINE,
  63.         STATE_NORM_LIST,
  64.         STATE_NORM_INITIALIZED,
  65.         STATE_BPGM_OFFLINE,
  66.         STATE_BPGM_START,
  67.         STATE_BPGM_DATA,
  68.         STATE_BPGM_END,
  69.         STATE_BPGM_COPY,
  70.         STATE_APGM_OFFLINE,
  71.         STATE_APGM_START,
  72.         STATE_APGM_DATA,
  73.         STATE_APGM_END,
  74.     } State;
  75.  
  76.     typedef enum
  77.     {
  78.         EVENT_BIOS_START,
  79.         EVENT_APP_START,
  80.         EVENT_HEARTBEAT,
  81.         EVENT_PGM_ACK,
  82.         EVENT_PGM_NACK,
  83.         EVENT_LIST_DONE,
  84.         EVENT_PROGRAM_BIOS,
  85.         EVENT_PROGRAM_APP,
  86.         EVENT_RESET
  87.     } Event;
  88.    
  89.     typedef boost::signals2::signal<void(Id, State, State)> SignalOnNewState;
  90.    
  91.     Node(Id id);
  92.     virtual ~Node();
  93.    
  94.     static void SetupStateMachine();
  95.    
  96.     void ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state);
  97.    
  98.     Id GetId();
  99.     Information GetInformation();
  100.     unsigned int GetBiosVersion();
  101.     bool HasApplication();
  102.     State GetState();
  103.  
  104.     void Trigger(Event event, common::StringMap variables);
  105.    
  106.     bool CheckTimeout();
  107.     void ResetTimeout();
  108.    
  109.     void ProgramApplication(Code::Pointer code);
  110.     void ProgramBios(Code::Pointer code);
  111.     void Reset();
  112.    
  113.     unsigned int GetProgramProgress();
  114.    
  115. private:
  116.     typedef std::map<Event, State> Transition;
  117.     typedef std::map<State, Transition> TransitionList;
  118.    
  119.     Id id_;
  120.     Information information_;    
  121.     State state_;
  122.    
  123.     Code::Pointer code_;
  124.     unsigned int start_offset_;
  125.     unsigned int current_offset_;
  126.     unsigned int expected_ack_data_;
  127.     time_t program_start_time_;
  128.  
  129.     logging::Logger LOG;
  130.    
  131.     static TransitionList transitions_;
  132.     static std::map<State, std::string> state_names_;
  133.     static std::map<Event, std::string> event_names_;
  134.    
  135.     SignalOnNewState signal_on_new_state_;
  136.    
  137.     static void AddTransition(State current_state, Event event, State target_state);
  138.    
  139.     State GetTransitionTarget(Event event);
  140.     void SendReset();
  141.     void SendListRequest();
  142. };
  143.  
  144. }; // namespace control
  145. }; // namespace atom
  146.  
  147. #endif // CONTROL_NODE_H
  148.