int main(int argc, char* argv[]) { /************** VARIABLE DECLARATION */ KeyStruct keyPadState; AlarmStruct state; EdgeStruct sense; char code[CODE_DIM+1]; DEFAULT_CODE(code); bool isEquals; /************* INIT ***************/ keypad_init(&keyPadState); timer_start(); sense_init(&sense); alarm_init(&state); lcd_init(); buzzer_init(); /************** INFINITE LOOP *****/ HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("SALVE PRF FOGGIA"); HD44780_GotoXY(0,1); HD44780_PutStr("Alarm System 1.0"); keypad_flush(&keyPadState); while(1){ state.eventsArray[MAGN1] = magn_one_read(&sense); state.eventsArray[MAGN2] = magn_two_read(&sense); state.eventsArray[MOVE1] = move_one_read(&sense); state.eventsArray[MOVE2] = move_two_read(&sense); state.eventsArray[MOVE3] = move_three_read(&sense); state.eventsArray[MOVE4] = move_four_read(&sense); state.eventsArray[KEYPAD] = get_code(&keyPadState); /******** Update State ***********/ isEquals = true; if(state.eventsArray[KEYPAD]){ pin_sound(); int i=0; HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("PIN:"); HD44780_GotoXY(0,1); alarm_off(); while(keyPadState.code[i] != '\0' && code[i] != '\0'){ HD44780_PutChar(keyPadState.code[i]); (isEquals & (keyPadState.code[i] == code[i])) ? (isEquals = true) : (isEquals = false); i++; } if(isEquals){ state.isActive = !state.isActive; keypad_flush(&keyPadState); if(state.isActive){ HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("ALLARME"); HD44780_GotoXY(0,1); HD44780_PutStr("ATTIVATO"); }else{ HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("ALLARME"); HD44780_GotoXY(0,1); HD44780_PutStr("DISATTIVATO"); } } if(!isEquals && keyPadState.index == CODE_DIM){ HD44780_GotoXY(0,1); HD44780_PutStr("ERRATO"); keypad_flush(&keyPadState); } } /******** Generate Outputs *******/ if(state.eventsArray[MAGN1] && state.isActive){ state.isRinging = true; HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("VIOLAZIONE"); HD44780_GotoXY(0,1); HD44780_PutStr("PORTA 1"); } if(state.eventsArray[MAGN2] && state.isActive){ state.isRinging = true; HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("VIOLAZIONE"); HD44780_GotoXY(0,1); HD44780_PutStr("PORTA 2"); } if(state.eventsArray[MOVE1] && state.isActive){ state.isRinging = true; HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("VIOLAZIONE"); HD44780_GotoXY(0,1); HD44780_PutStr("STANZA 1"); } if(state.eventsArray[MOVE2] && state.isActive){ state.isRinging = true; HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("VIOLAZIONE"); HD44780_GotoXY(0,1); HD44780_PutStr("STANZA 2"); } if(state.eventsArray[MOVE3] && state.isActive){ state.isRinging = true; HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("VIOLAZIONE"); HD44780_GotoXY(0,1); HD44780_PutStr("STANZA 3"); } if(state.eventsArray[MOVE4] && state.isActive){ state.isRinging = true; HD44780_ClrScr(); HD44780_GotoXY(0,0); HD44780_PutStr("VIOLAZIONE"); HD44780_GotoXY(0,1); HD44780_PutStr("STANZA 4"); } if(!state.isActive){ alarm_off(); state.isRinging = false; } if(state.isRinging) alarm_on(); if(keyPadState.prevChar == ENTER_CHAR) change_pin(code, &keyPadState); timer_sleep(1); } }
/* * Upload files from the user. * Returns: * 0 - All seems well * 1 - No transfer protocol selected. * 2 - Transfer failed */ int upload(up_list **upload_list) { char *temp; struct dirent *dp; DIR *dirp; struct stat statfile; struct timeval starttime, endtime; struct timezone tz; unsigned int Size = 0; int err, Count = 0, rc = 0, want1k = FALSE, wantg = FALSE; up_list *tmp, *ta; /* * If user has no default protocol, make sure he has one. */ if (!ForceProtocol()) { return 1; } temp = calloc(PATH_MAX, sizeof(char)); /* Please start your upload now */ snprintf(temp, PATH_MAX, "%s, %s", sProtAdvice, (char *) Language(283)); pout(CFG.HiliteF, CFG.HiliteB, temp); Enter(2); Syslog('+', "Upload using %s", sProtName); snprintf(temp, PATH_MAX, "%s/%s/upl", CFG.bbs_usersdir, exitinfo.Name); if (chdir(temp)) { WriteError("$Can't chdir to %s", temp); free(temp); return 1; } sleep(2); if (uProtInternal) { if ((strncasecmp(sProtName, "zmodem", 6) == 0) || (strncasecmp(sProtName, "ymodem", 6) == 0) || (strncasecmp(sProtName, "xmodem", 6) == 0)) { if (strncasecmp(sProtName, "zmodem", 6) == 0) { zmodem_requested = TRUE; protocol = ZM_ZMODEM; } else { zmodem_requested = FALSE; } if (strncasecmp(sProtName, "ymodem", 6) == 0) protocol = ZM_YMODEM; if (strncasecmp(sProtName, "xmodem", 6) == 0) protocol = ZM_XMODEM; if (strstr(sProtName, "-1K") && (protocol != ZM_ZMODEM)) { Syslog('x', "%s: want 1K blocks", protname()); want1k = TRUE; } if ((strstr(sProtName, "-G")) && (protocol == ZM_YMODEM)) { Syslog('x', "%s: want Ymodem-G", protname()); wantg = TRUE; } rc = zmrcvfiles(want1k, wantg); Syslog('b', "Begin dir processing"); if ((dirp = opendir(".")) == NULL) { WriteError("$Upload: can't open ./upl"); Home(); rc = 1; } else { while ((dp = readdir(dirp)) != NULL) { if (*(dp->d_name) != '.') { if (rc == 0) { stat(dp->d_name, &statfile); Syslog('b', "Uploaded \"%s\", %ld bytes", dp->d_name, statfile.st_size); snprintf(temp, PATH_MAX, "%s/%s/upl/%s", CFG.bbs_usersdir, exitinfo.Name, dp->d_name); chmod(temp, 0660); /* * Add uploaded file to the list */ tmp = (up_list *)malloc(sizeof(up_list)); tmp->next = NULL; tmp->filename = xstrcpy(temp); tmp->size = Size; if (*upload_list == NULL) { *upload_list = tmp; } else { for (ta = *upload_list; ta; ta = ta->next) { if (ta->next == NULL) { ta->next = (up_list *)tmp; break; } } } } else { Syslog('+', "Remove failed %s result %d", dp->d_name, unlink(dp->d_name)); } } } closedir(dirp); } } else { Syslog('!', "Internal protocol %s not supported", sProtName); free(temp); return 1; } } else { /* * External protocol */ gettimeofday(&starttime, &tz); Altime(7200); alarm_set(7190); err = execute_str(sProtUp, (char *)"", NULL, NULL, NULL, NULL); if (rawport() != 0) { WriteError("Unable to set raw mode"); } if (err) { WriteError("$Upload error %d, prot: %s", err, sProtUp); rc = 2; } Altime(0); alarm_off(); alarm_on(); gettimeofday(&endtime, &tz); /* * With external protocols we don't know anything about what we got. * Just check the files in the users upload directory. */ if ((dirp = opendir(".")) == NULL) { WriteError("$Upload: can't open ./upl"); Home(); rc = 1; } else { while ((dp = readdir(dirp)) != NULL) { if (*(dp->d_name) != '.') { stat(dp->d_name, &statfile); Syslog('+', "Uploaded \"%s\", %ld bytes", dp->d_name, statfile.st_size); Count++; Size += statfile.st_size; snprintf(temp, PATH_MAX, "%s/%s/upl/%s", CFG.bbs_usersdir, exitinfo.Name, dp->d_name); chmod(temp, 0660); /* * Add uploaded file to the list */ tmp = (up_list *)malloc(sizeof(up_list)); tmp->next = NULL; tmp->filename = xstrcpy(temp); tmp->size = Size; if (*upload_list == NULL) { *upload_list = tmp; } else { for (ta = *upload_list; ta; ta = ta->next) { if (ta->next == NULL) { ta->next = (up_list *)tmp; break; } } } } } closedir(dirp); Syslog('+', "Upload %s in %d file(s)", transfertime(starttime, endtime, (unsigned int)Size, FALSE), Count); } } free(temp); Syslog('b', "Done, return rc=%d", rc); return rc; }
void main(void) { init_DigIO(); init_timer(); init_adc(); //// init_SWTimer(); // Enable peripherial interrupts and start processing PEIE = 1; // Enable peripheral interrupt GIE = 1; // Enable global interrupt Start_Timer(TIMER_3,10); unsigned char tempcounter; //Switch Off alarm and heater; alarm_on(); heater_off(); while (1) { if (GetTimer_State(TIMER_3) ==TIMER_EXPIRED) { Start_Timer(TIMER_3,10); // Call every 10ms cyclic timer if(Read_Adc_E1() == TRUE)//true { WaterSensed_State = Read_Adc_E0(); /* if(WaterSensed_State == E0_E1_SHORT) { alarm_on(); heater_on(); } else { alarm_off(); heater_off(); } */ //------------------------------------------------------------------------------------- if ((WaterSensed_PrevState == E0_E1_SHORT ) && ( WaterSensed_State == E0_E1_OPEN )) { Start_Timer(TIMER_0,5000); // For 5 Seconds pressuresw_Flag = FALSE; } else if (GetTimer_State(TIMER_0) ==TIMER_EXPIRED) { alarm_on(); heater_off(); Stop_Timer(TIMER_0); } //------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------- if ((WaterSensed_PrevState == E0_E1_OPEN ) && ( WaterSensed_State == E0_E1_SHORT )) { Start_Timer(TIMER_1,5000);// For 5 Seconds } else if (GetTimer_State(TIMER_1) ==TIMER_EXPIRED) { alarm_off(); pressuresw_Flag = TRUE; Stop_Timer(TIMER_1); } WaterSensed_PrevState =WaterSensed_State; //------------------------------------------------------------------------------------- if (E0_E1_UNKNOWN == WaterSensed_State) //ADC E0 is inbetween 1 and 2 Volts which is error case; { alarm_on(); heater_off(); } // Delay 2 sec //------------Pressure switch case----true--------------------------------------------------------------------- if (pressuresw_Flag) { Switch_State = read_pressure_switch(); if ((Switch_PrevState == FALSE )&& (Switch_State == TRUE)) { Start_Timer(TIMER_2,2000); } if ((GetTimer_State(TIMER_2) ==TIMER_EXPIRED) && (Switch_State == TRUE)) { heater_on(); alarm_off(); } if(Switch_State == FALSE) { Stop_Timer(TIMER_2); heater_off(); } } //---------------------------------------------------------------------------------------- Switch_PrevState=Switch_State; } else { // E1 voltage is other than 1.25 alarm_on(); heater_off(); } // While ends } } }
/* * Download files to the user. * Returns: * 0 - All seems well * 1 - No transfer protocol selected * 2 - No files to download. */ int download(down_list *download_list) { down_list *tmpf; int err, maxrc = 0, Count = 0; char *temp, *symTo, *symFrom; unsigned int Size = 0; struct dirent *dp; DIR *dirp; struct timeval starttime, endtime; struct timezone tz; Syslog('b', "download()"); for (tmpf = download_list; tmpf; tmpf = tmpf->next) { Syslog('b', "%s,%s,%ld,%ld,%ld,%s,%s,%s", tmpf->local, tmpf->remote, tmpf->cps, tmpf->area, tmpf->size, tmpf->kfs ?"KFS":"KEEP", tmpf->sent ?"SENT":"N/A", tmpf->failed ?"FAILED":"N/A"); } /* * If user has no default protocol, make sure he has one. */ if (!ForceProtocol()) { return 1; } symTo = calloc(PATH_MAX, sizeof(char)); symFrom = calloc(PATH_MAX, sizeof(char)); temp = calloc(PATH_MAX, sizeof(char)); /* * Build symlinks into the users tag directory. */ chdir("./tag"); for (tmpf = download_list; tmpf; tmpf = tmpf->next) { if (!tmpf->sent && !tmpf->failed) { snprintf(symFrom, PATH_MAX, "%s/%s/tag/%s", CFG.bbs_usersdir, exitinfo.Name, tmpf->remote); Syslog('b', "test \"%s\" \"%s\"", symFrom, tmpf->local); if (strcmp(symFrom, tmpf->local)) { Syslog('b', "different, need a symlink"); unlink(tmpf->remote); snprintf(symFrom, PATH_MAX, "%s", tmpf->remote); snprintf(symTo, PATH_MAX, "%s", tmpf->local); if (symlink(symTo, symFrom)) { WriteError("$Can't create symlink %s %s %d", symTo, symFrom, errno); tmpf->failed = TRUE; } else { Syslog('b', "Created symlink %s -> %s", symFrom, symTo); } tmpf->kfs = FALSE; } else { Syslog('b', "the same, file is in tag directory"); } /* * Check if file or symlink is really there. */ snprintf(symFrom, PATH_MAX, "%s", tmpf->remote); if ((access(symFrom, F_OK)) != 0) { WriteError("File or symlink %s check failed, unmarking download", symFrom); tmpf->failed = TRUE; } else { Count++; Size += tmpf->size; } } } Home(); if (!Count) { /* * Nothing to download */ free(temp); free(symTo); free(symFrom); return 2; } clear(); /* File(s) : */ pout(YELLOW, BLACK, (char *) Language(349)); snprintf(temp, PATH_MAX, "%d", Count); PUTSTR(temp); Enter(1); /* Size : */ pout( CYAN, BLACK, (char *) Language(350)); snprintf(temp, PATH_MAX, "%u", Size); PUTSTR(temp); Enter(1); /* Protocol : */ pout( CYAN, BLACK, (char *) Language(351)); snprintf(temp, PATH_MAX, "%s", sProtName); PUTSTR(temp); Enter(1); Syslog('+', "Download files start, protocol: %s", sProtName); PUTSTR(sProtAdvice); Enter(2); /* * Wait a while before download */ sleep(2); if (uProtInternal) { snprintf(temp, PATH_MAX, "%s/%s/tag", CFG.bbs_usersdir, exitinfo.Name); chdir(temp); if (strncasecmp(sProtName, "zmodem-8k", 9) == 0) { maxrc = zmsndfiles(download_list, TRUE); Home(); } else if (strncasecmp(sProtName, "zmodem", 6) == 0) { maxrc = zmsndfiles(download_list, FALSE); Home(); } else if ((strncasecmp(sProtName, "xmodem", 6) == 0) || (strncasecmp(sProtName, "ymodem", 6) == 0)) { if (strncasecmp(sProtName, "xmodem", 6) == 0) protocol = ZM_XMODEM; else protocol = ZM_YMODEM; if (strstr(sProtName, "1K") || strstr(sProtName, "1k")) maxrc = ymsndfiles(download_list, TRUE); else maxrc = ymsndfiles(download_list, FALSE); Home(); } else { Syslog('!', "Warning internal protocol %s not supported", sProtName); maxrc = 1; } } else { gettimeofday(&starttime, &tz); /* * Transfer the files. Set the Client/Server time at the maximum * time the user has plus 10 minutes. The overall timer 10 seconds * less. Not a nice but working solution. */ alarm_set(((exitinfo.iTimeLeft + 10) * 60) - 10); Altime((exitinfo.iTimeLeft + 10) * 60); snprintf(temp, PATH_MAX, "%s/%s/tag", CFG.bbs_usersdir, exitinfo.Name); if ((dirp = opendir(temp)) == NULL) { WriteError("$Download: Can't open dir: %s", temp); free(temp); } else { chdir(temp); free(temp); temp = NULL; while ((dp = readdir(dirp)) != NULL ) { if (*(dp->d_name) != '.') { if (temp != NULL) { temp = xstrcat(temp, (char *)" "); temp = xstrcat(temp, dp->d_name); } else { temp = xstrcpy(dp->d_name); } } } if (temp != NULL) { if ((err = execute_str(sProtDn, temp, NULL, NULL, NULL, NULL))) { WriteError("$Download error %d, prot: %s", err, sProtDn); } /* * Restore rawport */ rawport(); free(temp); } else { WriteError("No filebatch created"); } closedir(dirp); } purgeline(200); /* Wait a while, some Wintendo programs ignore input for a few seconds */ Altime(0); alarm_off(); alarm_on(); Home(); gettimeofday(&endtime, &tz); /* * Checking the successfull sent files, they are missing from * the ./tag directory. Failed files are still there. */ Count = Size = 0; for (tmpf = download_list; tmpf && (maxrc < 2); tmpf = tmpf->next) { if (!tmpf->sent && !tmpf->failed) { snprintf(symTo, PATH_MAX, "./tag/%s", tmpf->remote); /* * If symlink is gone the file is sent. */ if ((access(symTo, R_OK)) != 0) { Syslog('+', "File %s from area %d sent ok", tmpf->remote, tmpf->area); tmpf->sent = TRUE; Size += tmpf->size; Count++; } else { Syslog('+', "Failed to sent %s from area %d", Tag.LFile, Tag.Area); } } } /* * Work out transfer rate in seconds by dividing the * Size of the File by the amount of time it took to download * the file. */ Syslog('+', "Download %s in %d file(s)", transfertime(starttime, endtime, (unsigned int)Size, TRUE), Count); } free(symTo); free(symFrom); for (tmpf = download_list; tmpf; tmpf = tmpf->next) { Syslog('b', "%s,%s,%ld,%ld,%ld,%s,%s,%s", tmpf->local, tmpf->remote, tmpf->cps, tmpf->area, tmpf->size, tmpf->kfs ?"KFS":"KEEP", tmpf->sent ?"SENT":"N/A", tmpf->failed ?"FAILED":"N/A"); } Syslog('b', "download() rc=%d", maxrc); return maxrc; }
// 0: Up // 1: Down // 2: Mode // 3: Top void handle_buttons() { if (!multiPress) { int8_t delta = 0; if (longPress) { // User is holding down a button if (clock_ticked) { // Quickly move up/down if (buttons[0].current) { // Up delta = 1; } else if (buttons[1].current) { // Down delta = -1; } if (mode == mode_clock) { delta *= 3; } else { delta *= 2; } } } else { // Not long press if (pressed(&buttons[0])) { // Up delta = 1; } else if (pressed(&buttons[1])) { // Down delta = -1; } else if (pressed(&buttons[2])) { // Mode handle_mode_switch(); if (mode >= mode_nap) { mode = mode_clock; } else { mode++; } } else if (pressed(&buttons[3])) { // Top if (status == status_alarm) { nap_enabled = false; alarm_off(); } else { if (nap_enabled) { nap_enabled = false; status = status_error; status_ticks = clock_ticks; } else { nap_enabled = true; nap_time = clock_ticks + (uint32_t)nap_duration * (uint32_t)60 * (uint32_t)TICKS_PER_SECOND; status = status_success; status_ticks = clock_ticks; } } } } if (delta != 0) { last_mode_switch_ticks = 0; if (mode == mode_clock) { cli(); clock_set = true; clock.minutes += delta; if (clock.minutes > 59) { clock.minutes = 0; clock.hours++; if (clock.hours > 23) { clock.hours = 0; } } else if (clock.minutes <0) { clock.minutes = 59; clock.hours--; if (clock.hours < 0) { clock.hours = 23; } } sei(); } else if (mode == mode_alarm) { dirty = true; alarm_time += delta; if (alarm_time >= 60 * 24) { alarm_time = 0; } else if (alarm_time < 0) { alarm_time = 60 * 24 - 1; } } else if (mode == mode_nap) { dirty = true; nap_duration += delta; if (nap_duration >= 60 * 24) { nap_duration = 0; } else if (nap_duration < 0) { nap_duration = 60 * 24 - 1; } } else if (mode == mode_adjustment) { dirty = true; adjustment += delta; } else if (mode == mode_seconds) { cli(); clock.subseconds = 0; clock.seconds = 0; sei(); } } } else { // multipress if (buttons[2].current) { if (pressed(&buttons[0])) { handle_mode_switch(); mode = mode_seconds; } else if (pressed(&buttons[1])) { handle_mode_switch(); mode = mode_adjustment; } } } }