PROCESS_THREAD(collect_common_process, ev, data) { static struct etimer period_timer, wait_timer; PROCESS_BEGIN(); collect_common_net_init(); /* Send a packet every 60-62 seconds. */ etimer_set(&period_timer, CLOCK_SECOND * PERIOD); while(1) { PROCESS_WAIT_EVENT(); if(ev == serial_line_event_message) { char *line; char query[10]; char reg[2]; line = (char *)data; if(strncmp(line, "collect", 7) == 0 || strncmp(line, "gw", 2) == 0) { collect_common_set_sink(); } else if(strncmp(line, "net", 3) == 0) { collect_common_net_print(); } else if(strncmp(line, "time ", 5) == 0) { unsigned long tmp; line += 6; while(*line == ' ') { line++; } tmp = strtolong(line); time_offset = clock_seconds() - tmp; } else if(strncmp(line, "mac ", 4) == 0) { line +=4; while(*line == ' ') { line++; } if(*line == '0') { NETSTACK_RDC.off(1); } else { NETSTACK_RDC.on(); } } else if(strncmp(line, "~K", 2) == 0 || strncmp(line, "killall", 7) == 0) { /* Ignore stop commands */ } else if(strncmp(line, "Query-",6) == 0){ /* To extract the region information of the requested query */ strcpy(query, line+7); char *pch = NULL; pch = strtok (query,"."); if(strncmp(pch, "x", 1) != 0) { b = atoi(pch); multicast_flag = 0; } else{ b = 0; multicast_flag = 1; #if SCENARIO goto BUILD; #endif } pch = strtok (NULL, "."); if(strncmp(pch, "x", 1) != 0) { f = atoi(pch); multicast_flag = 0; } else{ f = 0; multicast_flag = 1; #if SCENARIO goto LEVL; #endif } pch = strtok (NULL, "."); if(strncmp(pch, "x", 1) != 0) { r = atoi(pch); multicast_flag = 0; } else{ r = 0; multicast_flag = 1; #if SCENARIO goto ROM; #endif } pch = strtok (NULL, "."); if(strncmp(pch, "x", 1) != 0) { sid = atoi(pch); multicast_flag = 0; } else{ sid = 0; multicast_flag = 1; #if SCENARIO goto SENSR; #endif } goto SEND_QR; #if SCENARIO BUILD: for(b=1;b<=3;b++) for(f=1;f<=3;f++) for(r=1;r<=3;r++) for(sid=1;sid<=3;sid++) query_send_sink(b,f,r,sid); goto JUMP; LEVL: for(f=1;f<=3;f++) for(r=1;r<=3;r++) for(sid=1;sid<=3;sid++) query_send_sink(b,f,r,sid); goto JUMP; ROM: for(r=1;r<=3;r++) for(sid=1;sid<=3;sid++) query_send_sink(b,f,r,sid); goto JUMP; SENSR: for(sid=1;sid<=3;sid++) query_send_sink(b,f,r,sid); goto JUMP; #endif SEND_QR: query_send_sink(b,f,r,sid); JUMP:{} } else if(strncmp(line, "Region-",7) == 0){ //Region of the node strcpy(reg, line+8); if(strncmp(reg, "B", 1) == 0){ region = BUILDING; } else if(strncmp(reg, "L", 1) == 0){ region = LEVEL; } else if(strncmp(reg, "R", 1) == 0){ region = ROOM; } else if(strncmp(reg, "S", 1) == 0){ region = SENSOR; } } else { //printf("unhandled command: %s\n", line); } } if(ev == PROCESS_EVENT_TIMER) { if(data == &period_timer) { etimer_reset(&period_timer); etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT)); } else if(data == &wait_timer) { if(send_active) { /* Time to send the data */ /* Broadcast its own region rank */ if( region == ROOM ) broadcast_identity(ROOM); else if ( region == BUILDING ) broadcast_identity(BUILDING); else if ( region == LEVEL ) broadcast_identity(LEVEL); else if ( region == SENSOR ) broadcast_identity(SENSOR); } } } } PROCESS_END(); }
int main() { /* we will just use ordinary idle mode */ set_sleep_mode(SLEEP_MODE_IDLE); /* setup uart */ uart_init(); while(1) { /* setup sd card slot */ if(!sd_raw_init()) { #if DEBUG uart_puts_p(PSTR("MMC/SD initialization failed\n")); #endif continue; } /* open first partition */ struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif 0 ); if(!partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif -1 ); if(!partition) { #if DEBUG uart_puts_p(PSTR("opening partition failed\n")); #endif continue; } } /* open file system */ struct fat_fs_struct* fs = fat_open(partition); if(!fs) { #if DEBUG uart_puts_p(PSTR("opening filesystem failed\n")); #endif continue; } /* open root directory */ struct fat_dir_entry_struct directory; fat_get_dir_entry_of_path(fs, "/", &directory); struct fat_dir_struct* dd = fat_open_dir(fs, &directory); if(!dd) { #if DEBUG uart_puts_p(PSTR("opening root directory failed\n")); #endif continue; } /* print some card information as a boot message */ print_disk_info(fs); /* provide a simple shell */ char buffer[24]; while(1) { /* print prompt */ uart_putc('>'); uart_putc(' '); /* read command */ char* command = buffer; if(read_line(command, sizeof(buffer)) < 1) continue; /* execute command */ if(strcmp_P(command, PSTR("init")) == 0) { break; } else if(strncmp_P(command, PSTR("cd "), 3) == 0) { command += 3; if(command[0] == '\0') continue; /* change directory */ struct fat_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, command, &subdir_entry)) { struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry); if(dd_new) { fat_close_dir(dd); dd = dd_new; continue; } } uart_puts_p(PSTR("directory not found: ")); uart_puts(command); uart_putc('\n'); } else if(strcmp_P(command, PSTR("ls")) == 0) { /* print directory listing */ struct fat_dir_entry_struct dir_entry; while(fat_read_dir(dd, &dir_entry)) { uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4; uart_puts(dir_entry.long_name); uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' '); while(spaces--) uart_putc(' '); uart_putdw_dec(dir_entry.file_size); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("cat "), 4) == 0) { command += 4; if(command[0] == '\0') continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } /* print file contents */ uint8_t buffer[8]; uint32_t offset = 0; while(fat_read_file(fd, buffer, sizeof(buffer)) > 0) { uart_putdw_hex(offset); uart_putc(':'); for(uint8_t i = 0; i < 8; ++i) { uart_putc(' '); uart_putc_hex(buffer[i]); } uart_putc('\n'); offset += 8; } fat_close_file(fd); } else if(strcmp_P(command, PSTR("disk")) == 0) { if(!print_disk_info(fs)) uart_puts_p(PSTR("error reading disk info\n")); } #if FAT_WRITE_SUPPORT else if(strncmp_P(command, PSTR("rm "), 3) == 0) { command += 3; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, command, &file_entry)) { if(fat_delete_file(fs, &file_entry)) continue; } uart_puts_p(PSTR("error deleting file: ")); uart_puts(command); uart_putc('\n'); } else if(strncmp_P(command, PSTR("touch "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(!fat_create_file(dd, command, &file_entry)) { uart_puts_p(PSTR("error creating file: ")); uart_puts(command); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("write "), 6) == 0) { command += 6; if(command[0] == '\0') continue; char* offset_value = command; while(*offset_value != ' ' && *offset_value != '\0') ++offset_value; if(*offset_value == ' ') *offset_value++ = '\0'; else continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } int32_t offset = strtolong(offset_value); if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { uart_puts_p(PSTR("error seeking on ")); uart_puts(command); uart_putc('\n'); fat_close_file(fd); continue; } /* read text from the shell and write it to the file */ uint8_t data_len; while(1) { /* give a different prompt */ uart_putc('<'); uart_putc(' '); /* read one line of text */ data_len = read_line(buffer, sizeof(buffer)); if(!data_len) break; /* write text to file */ if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len) { uart_puts_p(PSTR("error writing to file\n")); break; } } fat_close_file(fd); } else if(strncmp_P(command, PSTR("mkdir "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct dir_entry; if(!fat_create_dir(dd, command, &dir_entry)) { uart_puts_p(PSTR("error creating directory: ")); uart_puts(command); uart_putc('\n'); } } #endif #if SD_RAW_WRITE_BUFFERING else if(strcmp_P(command, PSTR("sync")) == 0) { if(!sd_raw_sync()) uart_puts_p(PSTR("error syncing disk\n")); } #endif else if(strcmp_P(command, PSTR("sync")) == 0) { // vytvor adresar mereni_teploty // nekonecna smycka - pomoci RTCka kazdou minutu odmer teplotu } else { uart_puts_p(PSTR("unknown command: ")); uart_puts(command); uart_putc('\n'); } } /* close directory */ fat_close_dir(dd); /* close file system */ fat_close(fs); /* close partition */ partition_close(partition); } return 0; }
bool TAccount::loadAccount(const CString& pAccount, bool ignoreNickname) { // Just in case this account was loaded offline through RC. accountName = pAccount; bool loadedFromDefault = false; CFileSystem* accfs = server->getAccountsFileSystem(); std::vector<CString> fileData; CString accountText = server->getPluginManager().LoadAccount(pAccount); if (!accountText.isEmpty()) fileData = accountText.tokenize("\n"); else { // Find the account in the file system. CString accpath(accfs->findi(CString() << pAccount << ".txt")); if (accpath.length() == 0) { accpath = CString() << server->getServerPath() << "accounts/defaultaccount.txt"; CFileSystem::fixPathSeparators(&accpath); loadedFromDefault = true; } // Load file. fileData = CString::loadToken(accpath, "\n"); if (fileData.size() == 0 || fileData[0].trim() != "GRACC001") return false; } // Clear Lists for (int i = 0; i < 30; ++i) attrList[i].clear(); chestList.clear(); mFlagList.clear(); folderList.clear(); weaponList.clear(); // Parse File for (unsigned int i = 0; i < fileData.size(); i++) { // Trim Line fileData[i].trimI(); // Declare Variables; CString section, val; int sep; // Seperate Section & Value sep = fileData[i].find(' '); section = fileData[i].subString(0, sep); if (sep != -1) val = fileData[i].subString(sep + 1); if (section == "NAME") continue; else if (section == "NICK") { if (!ignoreNickname) nickName = val; } else if (section == "COMMUNITYNAME") communityName = val; else if (section == "LEVEL") levelName = val; else if (section == "X") { x = (float)strtofloat(val); x2 = (int)(x * 16); } else if (section == "Y") { y = (float)strtofloat(val); y2 = (int)(y * 16); } else if (section == "Z") { z = (float)strtofloat(val); z2 = (int)(z * 16); } else if (section == "MAXHP") maxPower = (int)strtoint(val); else if (section == "HP") power = (float)strtofloat(val); else if (section == "RUPEES") gralatc = strtoint(val); else if (section == "ANI") gani = val; else if (section == "ARROWS") arrowc = strtoint(val); else if (section == "BOMBS") bombc = strtoint(val); else if (section == "GLOVEP") glovePower = strtoint(val); else if (section == "SHIELDP") shieldPower = strtoint(val); else if (section == "SWORDP") swordPower = strtoint(val); else if (section == "BOWP") bowPower = strtoint(val); else if (section == "BOW") bowImage = val; else if (section == "HEAD") headImg = val; else if (section == "BODY") bodyImg = val; else if (section == "SWORD") swordImg = val; else if (section == "SHIELD") shieldImg = val; else if (section == "COLORS") { std::vector<CString> t = val.tokenize(","); for (int i = 0; i < (int)t.size() && i < 5; i++) colors[i] = (unsigned char)strtoint(t[i]); } else if (section == "SPRITE") sprite = strtoint(val); else if (section == "STATUS") status = strtoint(val); else if (section == "MP") mp = strtoint(val); else if (section == "AP") ap = strtoint(val); else if (section == "APCOUNTER") apCounter = strtoint(val); else if (section == "ONSECS") onlineTime = strtoint(val); else if (section == "IP") { if (accountIp == 0) accountIp = strtolong(val); } else if (section == "LANGUAGE") { language = val; if (language.isEmpty()) language = "English"; } else if (section == "KILLS") kills = strtoint(val); else if (section == "DEATHS") deaths = strtoint(val); else if (section == "RATING") rating = (float)strtofloat(val); else if (section == "DEVIATION") deviation = (float)strtofloat(val); else if (section == "OLDDEVIATION") oldDeviation = (float)strtofloat(val); else if (section == "LASTSPARTIME") lastSparTime = strtolong(val); else if (section == "FLAG") setFlag(val); else if (section == "ATTR1") attrList[0] = val; else if (section == "ATTR2") attrList[1] = val; else if (section == "ATTR3") attrList[2] = val; else if (section == "ATTR4") attrList[3] = val; else if (section == "ATTR5") attrList[4] = val; else if (section == "ATTR6") attrList[5] = val; else if (section == "ATTR7") attrList[6] = val; else if (section == "ATTR8") attrList[7] = val; else if (section == "ATTR9") attrList[8] = val; else if (section == "ATTR10") attrList[9] = val; else if (section == "ATTR11") attrList[10] = val; else if (section == "ATTR12") attrList[11] = val; else if (section == "ATTR13") attrList[12] = val; else if (section == "ATTR14") attrList[13] = val; else if (section == "ATTR15") attrList[14] = val; else if (section == "ATTR16") attrList[15] = val; else if (section == "ATTR17") attrList[16] = val; else if (section == "ATTR18") attrList[17] = val; else if (section == "ATTR19") attrList[18] = val; else if (section == "ATTR20") attrList[19] = val; else if (section == "ATTR21") attrList[20] = val; else if (section == "ATTR22") attrList[21] = val; else if (section == "ATTR23") attrList[22] = val; else if (section == "ATTR24") attrList[23] = val; else if (section == "ATTR25") attrList[24] = val; else if (section == "ATTR26") attrList[25] = val; else if (section == "ATTR27") attrList[26] = val; else if (section == "ATTR28") attrList[27] = val; else if (section == "ATTR29") attrList[28] = val; else if (section == "ATTR30") attrList[29] = val; else if (section == "WEAPON") weaponList.push_back(val); else if (section == "CHEST") chestList.push_back(val); else if (section == "BANNED") isBanned = (strtoint(val) == 0 ? false : true); else if (section == "BANREASON") banReason = val; else if (section == "BANLENGTH") banLength = val; else if (section == "COMMENTS") accountComments = val; else if (section == "EMAIL") email = val; else if (section == "LOCALRIGHTS") adminRights = strtoint(val); else if (section == "IPRANGE") adminIp = val; else if (section == "FOLDERRIGHT") folderList.push_back(val); else if (section == "LASTFOLDER") lastFolder = val; } // Comment out this line if you are actually going to use community names. communityName = accountName; // If we loaded from the default account, save our account now and add it to the file system. if (loadedFromDefault) { saveAccount(); accfs->addFile(CString() << "accounts/" << pAccount << ".txt"); } return true; }