Example #1
0
int main(void) {
  //  uint8_t i;
  uint8_t mcustate;

  // turn boost off
  TCCR0B = 0;
  BOOST_DDR |= _BV(BOOST);
  BOOST_PORT &= ~_BV(BOOST); // pull boost fet low

  // check if we were reset
  mcustate = MCUSR;
  MCUSR = 0;

  wdt_disable();
  // now turn it back on... 2 second time out
  //WDTCSR |= _BV(WDP0) | _BV(WDP1) | _BV(WDP2);
  //WDTCSR = _BV(WDE);
  wdt_enable(WDTO_2S);
  kickthedog();

  // we lost power at some point so lets alert the user
  // that the time may be wrong (the clock still works)
  timeunknown = 1;

  // have we read the time & date from eeprom?
  restored = 0;

  // setup uart
  uart_init(BRRL_192);
  //DEBUGP("VFD Clock");
  DEBUGP("!");

  //DEBUGP("turning on anacomp");
  // set up analog comparator
  ACSR = _BV(ACBG) | _BV(ACIE); // use bandgap, intr. on toggle!
  // settle!
  if (ACSR & _BV(ACO)) {
    // hmm we should not interrupt here
    ACSR |= _BV(ACI);

    // even in low power mode, we run the clock 
    DEBUGP("clock init");
    clock_init();  

  } else {
    // we aren't in low power mode so init stuff

    // init io's
    initbuttons();
    
    VFDSWITCH_PORT &= ~_BV(VFDSWITCH);
    
    DEBUGP("turning on buttons");
    // set up button interrupts
    DEBUGP("turning on alarmsw");
    // set off an interrupt if alarm is set or unset
    EICRA = _BV(ISC00);
    EIMSK = _BV(INT0);
  
    displaymode = SHOW_TIME;
    DEBUGP("vfd init");
    vfd_init();
   
    dimmer_init();
 
    DEBUGP("boost init");
    brightness_level = eeprom_read_byte((uint8_t *)EE_BRIGHT);
    boost_init(brightness_level);
    sei();

    region = eeprom_read_byte((uint8_t *)EE_REGION);
    
    DEBUGP("speaker init");
    speaker_init();

    beep(4000, 1);

    DEBUGP("clock init");
    clock_init();  

    DEBUGP("alarm init");
    setalarmstate();
  }
  DEBUGP("done");
  while (1) {
    //_delay_ms(100);
    kickthedog();
    //uart_putc_hex(ACSR);
    if (ACSR & _BV(ACO)) {
      // DEBUGP("SLEEPYTIME");
      gotosleep();
      continue;
    }
    //DEBUGP(".");
    if (just_pressed & 0x1) {
      just_pressed = 0;
      switch(displaymode) {
      case (SHOW_TIME):
	displaymode = SET_ALARM;
	display_str("set alarm");
	set_alarm();
	break;
      case (SET_ALARM):
	displaymode = SET_TIME;
	display_str("set time");
	set_time();
	timeunknown = 0;
	break;
      case (SET_TIME):
	displaymode = SET_DATE;
	display_str("set date");
	set_date();
	break;
      case (SET_DATE):
	displaymode = SET_BRIGHTNESS;
	display_str("set brit");
	set_brightness();
	break;
      case (SET_BRIGHTNESS):
	displaymode = SET_DIMMER;
	display_str("set dimr");
	set_dimmer();
	break;
      case (SET_DIMMER):
	displaymode = SET_VOLUME;
	display_str("set vol ");
	set_volume();
	break;
      case (SET_VOLUME):
	displaymode = SET_REGION;
	display_str("set regn");
	set_region();
	break;
	/*
      case (SET_REGION):
	displaymode = SET_SNOOZE;
	display_str("set snoz");
	set_snooze();
	break;
	*/
      default:
	displaymode = SHOW_TIME;
      }
    } else if ((just_pressed & 0x2) || (just_pressed & 0x4)) {
      just_pressed = 0;
      displaymode = NONE;
      display_date(DAY);

      kickthedog();
      delayms(1500);
      kickthedog();

      displaymode = SHOW_TIME;     
    } 
  }
}
int process_cmd(char *cmd,int sockfd){

 char * msg;
 commands cmd_pkt;
 char *cmds;
 cmds =strdup(cmd);
 cmd_pkt = getdlmtdcmdstr(cmd);

 if(cmd_pkt.no_of_args == -1)
  return 0;
 //printf("%s:%s\n",cmd_pkt.cmnd,"");
 /*call method to validate the nodes*/
 if(strcasecmp(cmd_pkt.arglist[0],"UPDATE")==0){
    msg ="Invalid Arguments.Usage - update <server-ID1> <server-ID2> <Link Cost>";
   if(cmd_pkt.no_of_args != 4 ){
     //printf("%s:%s\n",cmd_pkt.cmnd,msg);
     cse4589_print_and_log("%s:%s\n",cmds,msg);
     //printf("\n[PA3]$:Invalid Arguments.Usage - update <server-ID1> <server-ID2> <Link Cost>");
     return -1;
   }
   
   if(validate_nodes(cmd_pkt.arglist,0) == -1){
     msg ="Invalid Arguments.server-ID1 - Node Id server-ID2:Must be neighbor node Id";
     cse4589_print_and_log("%s:%s\n",cmds,msg);
   
   return -1;
   }
   else{
    update_rout_info(cmd_pkt.arglist); 
    compute_bellman_ford();
  }
   //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
   cse4589_print_and_log("%s:SUCCESS\n",cmds);
   
 }/*call method to send the update packet */ 
   else if(strcasecmp(cmd_pkt.arglist[0],"STEP")==0){
    if(cmd_pkt.no_of_args > 1 ){
     msg ="Invalid Arguments.Usage - step"; 
     cse4589_print_and_log("%s:%s\n",cmds,msg);
     //printf("\n[PA3]$:Invalid Arguments.Usage - step");
     return -1;
   }
   send_update_packet_nnodes(sockfd);
   //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
   cse4589_print_and_log("%s:SUCCESS\n",cmds); 

 }
 else if(strcasecmp(cmd_pkt.arglist[0],"PACKETS")==0){
  if(cmd_pkt.no_of_args > 1 ){
   msg ="Invalid Arguments.Usage - packets";
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   //printf("\n[PA3]$:Invalid Arguments.Usage - packets");
   return -1;
 }
  //display the packets received
  //printf("\n The number of packets sent : %d",packet_count);
  cse4589_print_and_log("%s:SUCCESS\n",cmds);
  cse4589_print_and_log("%d\n",packet_count);
  //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
  packet_count = 0 ;

}else if(strcasecmp(cmd_pkt.arglist[0],"DISPLAY")==0){
  if(cmd_pkt.no_of_args > 1 ){
    
   msg ="Invalid Arguments.Usage - display";
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   //printf("\n[PA3]$:Invalid Arguments.Usage - display");
   return -1;
 }
  cse4589_print_and_log("%s:SUCCESS\n",cmds);
  dsplay_rt_table();
}else if(strcasecmp(cmd_pkt.arglist[0],"DISABLE")==0){
  if(cmd_pkt.no_of_args != 2  ){
   msg ="Invalid Arguments.Usage - disable <server-id>"; 
   //printf("\n[PA3]$:Invalid Arguments.Usage - disable <server-ID>");
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   return -1;
 } 
  int iret = 0;
  iret = disable_link(cmd_pkt.arglist); 
    //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
  msg ="Invalid Arguments.Usage - Server Id has to be a neighbor node id"; 
  if(iret == 1)
  cse4589_print_and_log("%s:SUCCESS\n",cmds);
  else
  cse4589_print_and_log("%s:%s\n",cmds,msg);

}else if(strcasecmp(cmd_pkt.arglist[0],"CRASH")==0){
 if(cmd_pkt.no_of_args > 1 ){
   msg ="Invalid Arguments.Usage - crash";
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   //printf("\n[PA3]$:Invalid Arguments.Usage - crash");
   return -1;
 }
  gotosleep();
 
}else if(strcasecmp(cmd_pkt.arglist[0],"DUMP")==0){
  if(cmd_pkt.no_of_args > 1 ){
   msg ="Invalid Arguments.Usage - dump"; 
   //printf("\n[PA3]$:Invalid Arguments.Usage - dump");
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   return -1;
 }
 build_rtpkt_dump(1);
 //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
 cse4589_print_and_log("%s:SUCCESS\n",cmds);
}else if(strcasecmp(cmd_pkt.arglist[0],"academic_integrity")== 0)
{
  //printf("\n I have read and understood the course academic integrity policy located at http://www.cse.buffalo.edu/faculty/dimitrio/courses/cse4589_f14/index.html#integrity");
  cse4589_print_and_log("I have read and understood the course academic integrity policy located at http://www.cse.buffalo.edu/faculty/dimitrio/courses/cse4589_f14/index.html#integrity");
}else{
 printf("\n[PA3]$:Command not found");
 fflush(stdout);//cse4589_print_and_log("Command not found\n");
 return -1;
}
free(cmds);
return 1;
}