home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 March / VPR0303A.ISO / AIBO / AiboBiff2 / AiboBiff / AiboBiff.cc next >
C/C++ Source or Header  |  2002-12-19  |  5KB  |  181 lines

  1. //-----------------------------------------------
  2. // AiboBiff ver.2
  3. // 2002/12/19  Kazuto Sato
  4. //-----------------------------------------------
  5. #include <string.h>
  6. #include <OPENR/OSyslog.h>
  7. #include <OPENR/OPENRAPI.h>
  8. #include <OPENR/core_macro.h>
  9. #include <OPENR/OTime.h>
  10. #include "AiboBiff.h"
  11. #include "entry.h"
  12.  
  13. AiboBiff::AiboBiff () : biffState(BIFF_IDLE),
  14.     moNetTestState(MNTS_IDLE), commandQueue()
  15. {
  16. }
  17.  
  18. OStatus
  19. AiboBiff::DoInit(const OSystemEvent& event)
  20. {
  21.     OSYSDEBUG(("AiboBiff::DoInit()\n"));
  22.     NEW_ALL_SUBJECT_AND_OBSERVER;
  23.     REGISTER_ALL_ENTRY;
  24.     SET_ALL_READY_AND_NOTIFY_ENTRY;
  25.     return oSUCCESS;
  26. }
  27.  
  28. OStatus
  29. AiboBiff::DoStart(const OSystemEvent& event)
  30. {
  31.     OSYSDEBUG(("AiboBiff::DoStart()\n"));
  32.     
  33.     if (subject[sbjCommand]->IsReady() == true) {
  34.         Execute(SLEEP2SLEEP_NULL);
  35.         moNetTestState = MNTS_WAITING_RESULT;
  36.     } else {
  37.         moNetTestState = MNTS_START;
  38.     }
  39.     
  40.     ENABLE_ALL_SUBJECT;
  41.     ASSERT_READY_TO_ALL_OBSERVER;
  42.     
  43.     return oSUCCESS;
  44. }
  45.  
  46. OStatus
  47. AiboBiff::DoStop(const OSystemEvent& event)
  48. {
  49.     OSYSDEBUG(("AiboBiff::DoStop()\n"));
  50.     
  51.     biffState = BIFF_IDLE;
  52.     moNetTestState = MNTS_IDLE;
  53.     
  54.     DISABLE_ALL_SUBJECT;
  55.     DEASSERT_READY_TO_ALL_OBSERVER;
  56.     return oSUCCESS;
  57. }
  58.  
  59. OStatus
  60. AiboBiff::DoDestroy(const OSystemEvent& event)
  61. {
  62.     DELETE_ALL_SUBJECT_AND_OBSERVER;
  63.     return oSUCCESS;
  64. }
  65.  
  66. //-------------------------------------------------
  67. // ・サ・オ。シ、ォ、鯏ホ、鬢サ、ア、ソ、鮟エヨ、チ・ァ・テ・ッ
  68. //-------------------------------------------------
  69. void AiboBiff::NotifySensor(const ONotifyEvent& event)
  70. {
  71.     static time_t timeLast = 0;
  72.     
  73.     if(biffState == BIFF_READY) {
  74.         OTime ot;
  75.         OPENR::GetTime(&ot);
  76.         time_t t = ot.GetClock();
  77.         if(timeLast == 0 ||
  78.                     t - timeLast >= 5*60) {
  79.             char str[20];
  80.             strcpy(str, "let's go!");
  81.             // MailChecker、ヒ・ウ・゙・ノチョ
  82.             subject[sbjMailCheck]->SetData(str, sizeof(str));
  83.             subject[sbjMailCheck]->NotifyObservers();
  84.             
  85.             timeLast = t;
  86.         }
  87.     }
  88.     observer[event.ObsIndex()]->AssertReady();
  89. }
  90.  
  91. //-----------------------------------------------
  92. // MailChecker、隍ス猜K
  93. //-----------------------------------------------
  94. void AiboBiff::ReadyMailCheck(const OReadyEvent& event)
  95. {
  96.     OSYSDEBUG(("AiboBiff::ReadyMailCheck()\n"));
  97. }
  98.  
  99. //-----------------------------------------------
  100. // MailChecker、隍ソキテ螂癸シ・、ホ、ェテホ、鬢サ
  101. //-----------------------------------------------
  102. void AiboBiff::NotifyNewMail(const ONotifyEvent& event)
  103. {
  104.     OSYSDEBUG(("AiboBiff::NotifyNewMail()\n"));
  105.  
  106.     char cmd[20];
  107.     strcpy(cmd, "2 200 0");
  108.     
  109.     // MoNet、ヒ・ウ・゙・ノチョ
  110.     OSYSPRINT(("You have new mail!\n"));
  111.     ParseCommandLine(cmd);
  112.     Execute(commandQueue.front());
  113.     commandQueue.pop_front();
  114.     
  115.     observer[event.ObsIndex()]->AssertReady();
  116. }
  117.  
  118. //-------------------------------------------------
  119. // ーハイシ。「MoNetTest、隍・ウ・ヤ。シ、キ、ニイツ、
  120. //-------------------------------------------------
  121. void
  122. AiboBiff::ReadyCommand(const OReadyEvent& event)
  123. {
  124.     if (moNetTestState == MNTS_START) {
  125.         MoNetCommand cmd(SLEEP2SLEEP_NULL);
  126.         subject[sbjCommand]->SetData(&cmd, sizeof(cmd));
  127.         subject[sbjCommand]->NotifyObservers();
  128.         moNetTestState = MNTS_WAITING_RESULT;
  129.     }
  130. }
  131.  
  132. void
  133. AiboBiff::NotifyResult(const ONotifyEvent& event)
  134. {
  135.     if (moNetTestState == MNTS_IDLE) {
  136.  
  137.         ; // do nothing
  138.  
  139.     } else if (moNetTestState == MNTS_START) {
  140.  
  141.         OSYSLOG1((osyslogERROR,
  142.                   "MoNetTest::NotifyResult() : moNetTestState ERROR\n"));
  143.         
  144.         observer[event.ObsIndex()]->AssertReady();
  145.  
  146.     } else if (moNetTestState == MNTS_WAITING_RESULT) {
  147.  
  148.         if(biffState == BIFF_IDLE) biffState = BIFF_READY;
  149.         
  150.         MoNetResult* result = (MoNetResult*)event.Data(0);
  151.         OSYSPRINT(("MONET RESULT : commandID %d status %d posture %d\n",
  152.                    result->commandID, result->status, result->posture));
  153.         
  154.         if (commandQueue.size() > 0) {
  155.             Execute(commandQueue.front());
  156.             commandQueue.pop_front();
  157.         }
  158.         observer[event.ObsIndex()]->AssertReady();
  159.     }
  160. }
  161.  
  162. void
  163. AiboBiff::Execute(MoNetCommandID cmdID)
  164. {
  165.     MoNetCommand cmd(cmdID);
  166.     subject[sbjCommand]->SetData(&cmd, sizeof(cmd));
  167.     subject[sbjCommand]->NotifyObservers();
  168. }
  169.  
  170. void
  171. AiboBiff::ParseCommandLine(char* cmdline)
  172. {
  173.     char* ptr= strtok(cmdline, " \t");
  174.     commandQueue.push_back(atoi(ptr));
  175.     
  176.     while ((ptr = strtok(0, " \t")) != 0) {
  177.         commandQueue.push_back(atoi(ptr));
  178.     }
  179. }
  180.  
  181.