void wildcard_string_getall(char *wildcard, void (*getall)(char *string)) { int i; for (i = 0; i < len; i++) { if (strstr(strings[i], wildcard)) getall(strings[i]); } }
bool handleInput(std::string& aLine) { try { if (isFirst) doinitinit(); bool result = true; if (!aLine.empty()) result = getall(aLine); return result; // Unexpected input } catch (std::string message) { throw message; } catch (...) { throw std::string("Handle Input is broke"); } }
static int test_main(int argc, char **argv) { /* static const char *extra[] = { "clkfreq", "pa0b0", "pa0b1", "pa0b2", "pa0itssit", "pa0maxpwr", "sdram_config", "sdram_init", "sdram_ncdl", "vlan0ports", NULL }; const char **x; */ char buffer[NVRAM_SPACE]; char *k, *v, *e; const defaults_t *rest; struct nvram_convert *conv; printf("Unknown keys:\n"); getall(buffer); k = buffer; while (*k) { e = k + strlen(k) + 1; if ((v = strchr(k, '=')) != NULL) { *v = 0; for (rest = defaults; rest->key; ++rest) { if (strcmp(k, rest->key) == 0) break; } if (rest->key == NULL) { for (conv = nvram_converts; conv->name; ++conv) { if ((strcmp(k, conv->name) == 0) || (strcmp(k, conv->wl0_name) == 0)) break; } if (conv->name == NULL) { printf("%s=%s\n", k, v + 1); /* for (x = extra; *x; ++x) { if (strcmp(k, *x) == 0) break; } if (*x == NULL) { printf("nvram unset \"%s\"\n", k); } */ } } } else { printf("WARNING: '%s' doesn't have a '=' delimiter\n", k); } k = e; } return 0; }
static int backup_main(int argc, char **argv) { backup_t data; unsigned int size; char *p; char s[512]; char tmp[128]; int r; getall(data.buffer); data.sig = V1; data.hwid = check_hw_type(); p = data.buffer; while (*p != 0) p += strlen(p) + 1; size = (sizeof(data) - sizeof(data.buffer)) + (p - data.buffer) + 1; strcpy(tmp, "/tmp/nvramXXXXXX"); mktemp(tmp); if (f_write(tmp, &data, size, 0, 0) != size) { printf("Error saving file.\n"); return 1; } sprintf(s, "gzip < %s > %s", tmp, argv[1]); r = system(s); unlink(tmp); if (r != 0) { unlink(argv[1]); printf("Error compressing file.\n"); return 1; } printf("Saved.\n"); return 0; }
static int show_main(int argc, char **argv) { char *p, *q; char buffer[NVRAM_SPACE]; int n; int count; int show = 1; int stat = 1; int sort = 1; for (n = 1; n < argc; ++n) { if (strcmp(argv[n], "--nostat") == 0) stat = 0; else if (strcmp(argv[n], "--nosort") == 0) sort = 0; else help(); } if (sort) { system("nvram show --nostat --nosort | sort"); // smallest and easiest way :) show = 0; } getall(buffer); count = 0; for (p = buffer; *p; p += strlen(p) + 1) { q = p; while (*q) { if (!isprint(*q)) *q = ' '; ++q; } if (show) puts(p); ++count; } if (stat) { n = sizeof(struct nvram_header) + (p - buffer); printf("---\n%d entries, %d bytes used, %d bytes free.\n", count, n, NVRAM_SPACE - n); } return 0; }
int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256], command[256], arg[256], *occur; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); printf(MAKE_RED_DARK"Start FTP Program\n"RESET_COLOR); printf(MAKE_YELLOW"1.Go 2. Exit\n"RESET_COLOR); printf(MAKE_GREEN_DARK"Enter UR Choice :: "RESET_COLOR); bzero(buffer,256); fgets(buffer,255,stdin); while(strcmp(buffer,"exit\n")) //Keep getting command prompt till exit { system("clear"); printf(MAKE_GREEN_DARK"\nSN Command Name Syntax"RESET_COLOR); printf("\n--------------------------------------------------"); printf("\n1. get get <fileName>"); printf("\n2. get get *.<fileExtension>"); printf("\n3. mget mget <fileName> <fileName> "); printf("\n4. put put <fileName>"); printf("\n5. put put *.<fileExtension>"); printf("\n6. mput mput <fileName> <fileName> "); printf("\n7. lcd lcd <folderName> "); printf("\n8. cd cd <folderName> "); printf("\n9. lls lls "); printf("\n10. ls ls "); printf("\n11. pwd pwd "); printf("\n12. lpwd lpwd "); printf("\n13. exit exit\n\n\n"); if(!strcmp(buffer,"\n")) ; else if(!strcmp(buffer, "go\n")) ; else if(!strcmp(buffer, "pwd\n")) { n = write(sockfd,buffer,255); if (n < 0) error(MAKE_RED_DARK"ERROR writing to socket"RESET_COLOR); //printf("%d\n",sockfd); serPwd(sockfd); } else if(!strcmp(buffer, "lpwd\n")) { char lPwd[256]; printf(MAKE_GREEN_DARK"Present Working Directory-Client \n%s\n"RESET_COLOR, getcwd(lPwd, sizeof(lPwd))); } else if(!strcmp(buffer,"lcd\n")) cd(NULL); /*local cd*/ else if(!strcmp(buffer,"cd\n")) { n = write(sockfd,buffer,255); if(n<0) error(MAKE_RED_DARK"ERROR writing to socket"RESET_COLOR); ccd(sockfd); } else if(!strcmp(buffer,"lls\n")) ls(); /*local ls*/ else if(!strcmp(buffer,"ls\n")) { n = write(sockfd,buffer,255); if (n < 0) error(MAKE_RED_DARK"ERROR writing to socket"RESET_COLOR); // printf("%d\n",sockfd); lls(sockfd); } else { bzero(command,256); bzero(arg,256); occur=strchr(buffer,' '); if(occur==NULL) { printf(MAKE_RED_DARK"\nEnter Command-->> "RESET_COLOR); bzero(buffer,256); fgets(buffer,255,stdin); continue; } strcpy(command,buffer); occur=strchr(command,' '); if(occur==NULL) { printf(MAKE_RED_DARK"\nEnter Command-->> "RESET_COLOR); bzero(buffer,256); fgets(buffer,255,stdin); continue; } *occur='\0'; strcpy(arg,occur+1); occur=strchr(arg,'\n'); *occur='\0'; if(!strcmp(command,"get")) { if((arg[0]=='*') && (arg[1]=='.')) { bzero(buffer,256); strcpy(buffer,"getall "); strcat(buffer,arg); strcat(buffer,"\n"); n = write(sockfd,buffer,255); if (n < 0) error(MAKE_RED_DARK"ERROR writing to socket"RESET_COLOR); getall(arg,sockfd); } else { n = write(sockfd,buffer,255); if (n < 0) error(MAKE_RED_DARK"ERROR writing to socket"RESET_COLOR); getfile(arg,sockfd); } } else if(!strcmp(command,"put")) { if((arg[0]=='*') && (arg[1]=='.')) putall(arg,sockfd); else { n = write(sockfd,buffer,255); if (n < 0) error(MAKE_RED_DARK"ERROR writing to socket"RESET_COLOR); putfile(arg,sockfd); } } else if(!strcmp(command,"mget")) mgetfile(arg,sockfd); else if(!strcmp(command,"mput")) mputfile(arg,sockfd); else if(!strcmp(command,"lcd")) cd(arg); else if(!strcmp(command,"cd")) { n = write(sockfd,buffer,255); if (n < 0) error(MAKE_RED_DARK"ERROR writing to socket"RESET_COLOR); ccd(sockfd); } else printf("Enter Command-->> "); } printf("Enter Command-->> "); bzero(buffer,256); fgets(buffer,255,stdin); } //End of while if(!strcmp(buffer,"exit\n")) { n = write(sockfd,buffer,255); if (n < 0) error("ERROR writing to socket"); bzero(buffer,256); read(sockfd,buffer,18); printf("%s\n-----Exit-----\n",buffer); } return 0; }
elemptr symmodel::get_model_widx( const vector<string>& parsed, const vector<size_t>& idx, const vector<elemptr>& trace ) { //I start with it already parsed. //If parsed.size() == 0, I simply return this (with an index?) //The empty string indicates "this" model? No, when I parse it, I need to sep it, so an empty, will return in a zero-parse, of size zero, or nothing? //Either way, would result in same, so return ;) if( parsed.size() == 0 || parsed[0].compare("") == 0 ) { fprintf(stdout, "FOUND MODEL! [%s]\n", buildpath().c_str() ); elemptr t = elemptr( shared_from_this(), idx ); return t; } else { fprintf(stdout, "Model [%s], attempting to find model name [%s] widx (note, trace size is [%lu])\n", buildpath().c_str(), CAT(parsed, "/").c_str(), trace.size()); } //This is the next model I will go into string submodel = parsed[0]; vector<string> remainder( parsed.begin()+1, parsed.end() ); //same as remainder, remainder.erase(0); //REV: This is where I should iterate up the tree! This is the issue. vector<size_t> mlocs = find_model( submodel ); vector<size_t> hlocs = find_hole( submodel ); //At this point, we are finding the hole etc. normally. if( mlocs.size() >= 1 ) { if( mlocs.size() > 1 ) { fprintf(stderr, "WTF found more than one in getmodelwidx\n"); exit(1); } size_t mloc = mlocs[0]; //add model to trace? I guess? It is a submodel, so it is not necessary I guess? But it helps it find submodels I guess? Could this cause a problem? fprintf(stdout, "Model [%s], going through submodel [%s] to find [%s]\n", buildpath().c_str(), models[mloc]->localname.c_str(), CAT(remainder, "/").c_str() ); std::shared_ptr<symmodel> nextmodel = models[mloc]; //Don't add to trace because if same model, parent will cause infinite loop in combin with trace. //However //Problem is if I go through a hole, and the hole is the same model, that is the main problem vector<elemptr> newtrace = trace; //size_t idx_in_submodel = idx; //no change, b/c submodel. vector<size_t> idx_in_submodel = idx; //no change, b/c submodel. //newtrace.push_back( elemptr( shared_from_this(), idx ) ); return nextmodel->get_model_widx( remainder, idx_in_submodel, newtrace ); } else if( hlocs.size() >= 1 ) { if( hlocs.size() > 1) { fprintf(stderr, "WTF more than one HOLE found in getmodelwidx\n"); exit(1); } size_t hloc = hlocs[0]; fprintf(stdout, "Model [%s], going through hole [%s] to find [%s]\n", buildpath().c_str(), holes[hloc].name.c_str(), CAT(remainder, "/").c_str()); if( holes[ hloc ].members.size() != 1 ) { fprintf(stderr, "ERROR in get_model_widx, getting [%s] from HOLE, but hole [%s] has size [%lu], but it should be 1\n", submodel.c_str(), holes[hloc].name.c_str(), holes[hloc].members.size() ); exit(1); } //REV: so in the case it does not exist yet, we have a problem? std::shared_ptr<symmodel> nextmodel = holes[hloc].members[0]; if( check_same_toplevel_model( nextmodel ) ) { //Dont add to trace because its same model so infinite loop with going to parent.x vector<elemptr> newtrace = trace; //size_t idx_in_submodel = idx; //no change, b/c submodel. vector<size_t> idx_in_submodel = idx; //no change, b/c submodel. //newtrace.push_back( elemptr( shared_from_this(), idx ) ); return nextmodel->get_model_widx( remainder, idx_in_submodel, newtrace ); } else //not same toplevel model { //I NEED TO GO THROUGH A CORRESPONDENCE fprintf(stdout, "REV: about to go through a corresp to get a non-same model thing through a hole...\n"); //std::shared_ptr<corresp> mycorresp; auto mycorresp = getcorresp( nextmodel ); if( !mycorresp ) { fprintf(stderr, "REV: getcorresp in get_model_widx, failed, no such corresp exists between [%s] and [%s]\n", buildpath().c_str(), nextmodel->buildpath().c_str()); exit(1); } //REV; SANITY, if corresp not allocated yet, just return 0. //size_t idx_in_submodel = 0; vector<size_t> idx_in_submodel(1,0); //REV; Don't check this here, check this in the corresp struct? I.e. return dummy data if it is not existing yet (or exit?) if(mycorresp->isinit()) { fprintf(stdout, "Corresp is INIT!!!! Will attempt a GETALL...\n"); //REV: TODO HERE, just return it directly, with new IDX_IN_SUBMODEL ;0 //REV: this is it!!! This is where I vector<size_t> sanity = mycorresp->getall( idx ); fprintf(stdout, "Attempted to GETALL from the corresp!\n"); /*if( sanity.size() != 1 ) { fprintf(stderr, "SANITY check for corresp during access failed! Expected corresp for idx [%lu] of model [%s] to have only 1 corresponding element in model [%s], but it had [%lu]\n", idx, buildpath().c_str(), nextmodel->buildpath().c_str(), sanity.size() ); exit(1); } size_t idx_in_submodel = sanity[0]; //no change, b/c submodel. */ idx_in_submodel = sanity; } vector<elemptr> newtrace = trace; newtrace.push_back( elemptr( shared_from_this(), idx ) ); fprintf(stdout, "About to get next model with idx...\n"); auto toret = nextmodel->get_model_widx( remainder, idx_in_submodel, newtrace ); fprintf(stdout, "FINISHED About to get next model with idx...\n"); return toret; } } //end if not found in HOLES (or submodels) else { fprintf(stdout, "Model [%s], walking up to parent [%s] to find [%s]\n", buildpath().c_str(), parent->localname.c_str(), CAT(parsed, "/").c_str()); //Else, try to bubble up to ROOT. if( parent && (parent->parent) ) { std::shared_ptr<symmodel> nextmodel = parent; vector<elemptr> newtrace = trace; //size_t idx_in_submodel = idx; //no change, b/c submodel. vector<size_t> idx_in_submodel = idx; //no change, b/c submodel. //newtrace.push_back( elemptr( shared_from_this(), idx ) ); return nextmodel->get_model_widx( parsed, idx_in_submodel, newtrace ); } else if( parent && !(parent->parent) ) { //Couldn't find it! Return empty elemptr...bad. elemptr ep; return ep; } else { fprintf(stderr, "REV; this should never happen weird, Neither parent nor parent->parent? In searching for model with idx. Exit\n"); if( parent ) { fprintf( stderr, "Parent of me [%s] exists and is [%s]\n", buildpath().c_str(), parent->buildpath().c_str() ); } else { fprintf( stderr, "Parent does not exist... (note current model is [%s])!\n", buildpath().c_str() ); } exit(1); } } //couldn't find in "else" (i.e. not in this model, so try bubbling up parents) if(trace.size() == 0) { fprintf(stderr, "Trace size zero. This should never happen (should have been caught above)\n"); exit(1); } //REV: Did I mess something up? First it should check through all guys directly to see if it is same model? I.e. if target model matches b/c we can use that idx. fprintf(stdout, "Couldn't find model [%s] in previous model trace [%s], so moving to next! (trace size is [%lu])\n", CAT(parsed,"/").c_str(), buildpath().c_str(), trace.size() ); //Move back model and try again? vector<elemptr> newtrace = trace; //size_t idx_in_submodel = newtrace[ newtrace.size() - 1].idx; //end of trace. vector<size_t> idx_in_submodel = newtrace[ newtrace.size() - 1].idx; //end of trace. std::shared_ptr<symmodel> nextmodel = newtrace[ newtrace.size() - 1].model; newtrace.pop_back(); fprintf(stdout, "Will now try to run with new trace size [%lu]\n", newtrace.size() ); return nextmodel->get_model_widx( parsed, idx_in_submodel, newtrace ); } //end get_model_widx
int main(int argc, char *argv[]) { uint8_t *tab_rp_bits; uint16_t *tab_rp_registers; uint16_t *rd_position_registers; uint16_t *tab_rp_registers_bad; modbus_t *ctx; /*********************************************************************** * feedback is used to store the return value of every called function * rc is used to store the return value of the modbus command * resend is used to define the resend times if the command is failed * i is used for the loop parameter * insert_bit is used to indicate the calibarate function if there is value to set * num is used to store the number of data blocks in database * use_backend is used to indicate the modbus mode is rtu * next_option is used to take the command options * pre_step, curr_step are used to indicate the previous step number and current position step number * pre_length and value indicate the latest posiotion in database and current position * SLEN is a kind of struct used to store the database blocks ************************************************************************/ int feedback,i; int insert_bit, nb_points,num =0; int next_option; long curr_step; long pystep = -1; double value; double pdepth,pspacing,pdwell,pinterval; double profiled,stopped; double depth,dwell,spacing,interval; double last_position; int profilebit =0,feedback1,feedback2; int modbus=0; int motor_stop = 0; char * command_arg = ""; char * return_value; double in_position = 0; SLEN *examp; ARF *config, *profile,*off_set; modbus_mapping_t *mb_mapping; int ShmID; int *ShmPTR; int stop_indicator = 0; key_t MyKey; MyKey = ftok(".", 's'); ShmID = shmget(MyKey, sizeof(int), IPC_CREAT | 0666); ShmPTR = (int *) shmat(ShmID, NULL, 0); tab_rp_registers = (uint16_t *) malloc(4 * sizeof(uint16_t)); rd_position_registers = (uint16_t *) malloc(2 * sizeof(uint16_t)); tab_rp_registers_bad = (uint16_t *) malloc(2 * sizeof(uint16_t)); config = (ARF*)malloc( 10 * sizeof(ARF) ); if ( config == NULL ) { printf("Error: Out of Memory, use ./master reset to reset memory\n"); exit(1); } const char *const short_options = "hd::u::l:p::cD::w::s::i::gSmt::"; const struct option long_options[] = { { "help", 0,NULL, 'h'}, { "down", 2,NULL, 'd'}, { "up", 2,NULL, 'u'}, { "length", 1,NULL, 'l'}, { "position", 2,NULL, 'p'}, { "count", 0,NULL, 'c'}, { "Depth", 2,NULL, 'D'}, { "well", 2,NULL, 'w'}, { "spacing", 2,NULL, 's'}, { "interval", 2,NULL, 'i'}, { "go", 0,NULL, 'g'}, { "System", 0,NULL, 'S'}, { "motor", 0,NULL, 'm'}, { "time", 2,NULL, 't'}, { NULL, 0, NULL, 0 }, }; if (argc < 2) { print_comusage (stderr, 1); return_value = json_option("status:",-1); return return_value; } program_name = argv[0]; /*Get the first argument that passed through main function but does not contain*/ command_name = argv[1]; if(argc > 2) { command_arg = argv[2]; } /******************************************************************************************* * The next three command_name are used to control the motor through modbus (need modbus) * ********************************************************************************************/ if ( strcmp(command_name, "go") == 0 ) { double curr_position; char *recd = (char*)malloc(10*sizeof(char)); double offset; int re_send = 0; *ShmPTR = 0; modbus = 0; next_option = getopt_long (argc, argv, short_options, long_options, NULL); if (next_option == -1) print_comusage (stderr, 1); while (next_option != -1) { switch (next_option) { case 'h': print_comusage(stdout, 0); case 'd': godown: enable(0); initbus(1); /* sleep(1); ctx = modbusconnection(ctx); modbus = 1; feedback = godown(ctx); if((feedback == -1)&&(re_send <1)) { enable(0); initbus(0); re_send++; goto godown; } return_value = json_option("status",feedback); printf("%s\n",return_value); */ feedback = process_go_down(1); if((feedback == 0) && (re_send < 1)) { printf("get false recycle power\n"); enable(0); initbus(0); re_send++; goto godown; } return_value = json_option("status",feedback); printf("%s\n",return_value); break; case 'u': goup: enable(0); initbus(1); /* sleep(1); ctx = modbusconnection(ctx); modbus = 1; feedback = goup(ctx); if((feedback == -1)&&(re_send <1)) { enable(0); initbus(0); re_send++; goto goup; } */ feedback = process_go_up(1); if((feedback == 0) && (re_send < 1)) { printf("Get false recycle power\n"); enable(0); initbus(0); re_send++; goto goup; } return_value = json_option("status",feedback); printf("%s\n",return_value); break; case 'p': enable(0); initbus(1); sleep(1); ctx = modbusconnection(ctx); modbus = 1; in_position = atof(optarg); off_set = (ARF*)malloc(15*sizeof(ARF)); off_set = getconfig(&num,off_set); offset = off_set[10].value; in_position = in_position - offset; //printf("inposition is %f offset is %f\n",in_position,offset); free(off_set); //system("/home/sampler/kingkong.sh"); gotoposition: if (in_position <= 0) { if( process_read_home_switch(1) == 0) { feedback = process_go_home(1); } else feedback = 1; } else { pystep = process_read_step(1); curr_position = process_position(pystep); if ( !(( (in_position -0.1) <= curr_position ) && ( curr_position <= (in_position + 0.1) )) ) { feedback = process_go_position(1,in_position); return_value = json_option("status",feedback); } } if((feedback == 0)&&(re_send <1)) { printf("get false recycle power"); enable(0); initbus(0); enable(0); initbus(1); re_send++; goto gotoposition; } break; case '?': print_comusage (stderr, 1); default: abort (); } next_option = getopt_long (argc, argv, short_options, long_options, NULL); } //If the go command is failed, then exit the current process and power off the controller if(feedback == 0) { printf("1\n"); //*ShmPTR = 1; enable(0); initbus(0); return_value = json_option("status",-1); return return_value; } do{ usleep(5000); stop_indicator = process_check(1); }while(stop_indicator != 0); //printf("stop\n"); sleep(1); pystep = process_read_step(1); curr_position = process_position(pystep); //printf("cur position is %f\n",curr_position); if(curr_position != -1) { login("Go to command set last position"); curr_position = curr_position + offset; setconfig(9,curr_position); // In order to avoid "Read status command shutdown the power by accident enable(0); initbus(0); return_value = json_option("status",1); } else return_value = json_option("status",-1); } if ( strcmp(command_name, "stop") == 0 ) { if(check_power() == 1) { /*sleep(1); ctx = modbusconnection(ctx); modbus = 1; feedback = stop(ctx); if(feedback == -1)stop(ctx); */ process_stop(1); } } if ( strcmp(command_name, "position") == 0 ) { login("Check position command"); int resend = 0; double temp_position,offset; char *rec = (char*)malloc(10*sizeof(char)); stop_indicator = *ShmPTR; uint16_t * position_registers = (uint16_t *) malloc(2 * sizeof(uint16_t)); off_set = (ARF*)malloc(15*sizeof(ARF)); off_set = getconfig(&num,off_set); offset = off_set[10].value; checkposition: if (check_power()== 1) { ctx = modbusconnection(ctx); modbus = 1; temp_position = checkposition(ctx,position_registers); sprintf(rec,"The position read is %f",temp_position); login(rec); if(temp_position != -1) { login("Check position set last position"); //This sentence is used to show the position with offset temp_position = temp_position + offset; feedback = setconfig(9,temp_position); } else { if(resend < 2) { resend++; goto checkposition; } else return -100; } } else { config = getconfig(&num,config); temp_position = config[8].value; } return_value = json_float_option("status",temp_position); printf("%s\n",return_value); } /*********************************************************************** * 0: motor is stopped * * 1: motor is going down * * 2: motor is going up * * 3: motor is ramp up * * 4: motor is ramp down * * (need modbus) * * * ************************************************************************/ if(strcmp(command_name, "status") == 0) { stop_indicator = *ShmPTR; if (check_power()== 1) { sleep(1); ctx = modbusconnection(ctx); modbus = 1; next_option = getopt_long (argc, argv, short_options, long_options, NULL); if(next_option == -1) print_comusage (stderr, 1); while(next_option != -1) { switch (next_option) { case 'h': print_comusage(stdout, 0); case 'S': feedback = checksystemstatus(ctx,tab_rp_registers); return_value = json_option("status",feedback); break; case 'm': feedback = checkmotorstatus(ctx,tab_rp_registers); return_value = json_option("status",feedback); break; case '?': print_comusage (stderr, 1); default: abort (); } next_option = getopt_long (argc, argv, short_options, long_options, NULL); } if(feedback == -1) { return_value = json_option("status",0); } } else { return_value = json_option("status",0); //login("Check status from database"); } printf("%s\n",return_value); } /**************************************************************************************** * The next three command_name are used to control the database through sqlite3 * *****************************************************************************************/ if ( strcmp(command_name, "factory_default") == 0 ) { feedback1 = reset(0); feedback2 = dbinit(0); if ( (feedback1 == 1) && (feedback2 == 1)) { return_value = json_float_option("status",1); printf("%s\n",return_value); } else { return_value = json_float_option("status",-1); printf("%s\n",return_value); } } if ( strcmp(command_name, "reset") == 0 ) { feedback = reset(0); if(feedback == 1) { feedback = expected_time_reset(); } return_value = json_float_option("status",feedback); printf("%s\n",return_value); } if ( strcmp(command_name, "init") == 0 ) { feedback = -1; if ( strcmp(command_arg, "all") == 0 ) { feedback = dbinit(0); if(feedback == 1) { feedback = expected_time_init(); } } if ( strcmp(command_arg, "calibrate" ) == 0 ) { setconfig(6,0); feedback = dbinit(1); } if ( strcmp(command_arg, "configure" ) == 0 ) { feedback = dbinit(2); } if ( feedback == -1 ) { return_value = json_float_option("status",-1); print_comusage (stderr, 1); } else return_value = json_float_option("status",feedback); printf("%s\n",return_value); } if ( strcmp(command_name,"get") == 0 ) { examp = getall(&num,examp); return_value = json_array_option(num,examp); free(examp); printf("%s",return_value); } if ( strcmp(command_name,"set_offset") == 0 ) { double offset; next_option = getopt_long (argc, argv, short_options, long_options, NULL); if ( next_option == -1 ) print_comusage (stderr, 1); while( next_option != -1 ) { switch (next_option) { case 'h': print_comusage(stdout, 0); case 'l': if(optarg!=0)offset = strtod(optarg,NULL); insert_bit = 1; break; case '?': print_comusage (stderr, 1); default: abort (); } next_option = getopt_long (argc, argv, short_options, long_options, NULL); } feedback = setconfig(11,offset); return_value = json_option("status",feedback); printf("%s",return_value); } if ( strcmp(command_name,"get_offset") == 0 ) { double offset; off_set = (ARF*)malloc(15*sizeof(ARF)); off_set = getconfig(&num,off_set); offset = off_set[10].value; return_value = json_float_option("status",offset); printf("%s",return_value); free(off_set); } /************************************************************************** * The next three command_name are used to calibrate (need modbus) * ***************************************************************************/ if ( strcmp(command_name, "calibrate") == 0 ) { double calibrate; enable(0); initbus(1); sleep(1); ctx = modbusconnection(ctx); modbus = 1; next_option = getopt_long (argc, argv, short_options, long_options, NULL); if ( next_option == -1 ) print_comusage (stderr, 1); config = getconfig(&num,config); calibrate = config[5].value; if ( calibrate == 0 ) { reset(1); setconfig(6,1.0); set(num,0,0); } while( next_option != -1 ) { switch (next_option) { case 'h': print_comusage(stdout, 0); case 'l': if(optarg!=0)value = atof(optarg); insert_bit = 1; break; case 'c': getall(&num,examp); return_value = json_option("status",num); printf("%s\n",return_value); break; case '?': print_comusage (stderr, 1); default: abort (); } next_option = getopt_long (argc, argv, short_options, long_options, NULL); } if ( insert_bit == 1 ) { curr_step = checksteps(ctx,rd_position_registers); if ( curr_step < 0 ) curr_step =0;//do not need feedback = checkvalue(curr_step,value); if ( feedback == 1 ) { feedback = set(num,curr_step,value); return_value = json_option("status",feedback); } else { return_value = json_option("status",-1); } } /*if ( checkmotorstatus(ctx,tab_rp_registers) == 0 ) { enable(0); initbus(0); }*/ printf("%s\n",return_value); } /*********************************************************************** * The following functions are used for profile * * * ************************************************************************/ if ( strcmp(command_name, "profile") == 0 ) { next_option = getopt_long (argc, argv, short_options, long_options, NULL); if ( next_option == -1 ) print_comusage (stderr, 1); while ( next_option != -1 ) { switch (next_option) { case 'h': print_comusage(stdout, 0); case 'd': if(optarg!=0)depth = atof(optarg); profilebit = 1; break; case 's': if(optarg!=0)spacing = atof(optarg); profilebit = 1; break; case 'w': if(optarg!=0)dwell = atof(optarg); profilebit = 1; break; case 'i': //if(optarg!=0)interval = atof(optarg); if(optarg!=0)interval = strtod(optarg,NULL); profilebit = 1; break; case '?': print_comusage (stderr, 1); default: abort (); } next_option = getopt_long (argc, argv, short_options, long_options, NULL); } if ( profilebit == 1 ) { feedback = set_profile(depth,spacing,dwell,interval); } //Want to get the expected profile time and save it in database profile_time_check(interval); return_value = json_float_option("status",feedback); printf("%s\n",return_value); } if ( strcmp(command_name, "profileget" ) == 0) { profile = getconfig(&num,config); return_value = json_profile_option(num-2,profile); free(profile); printf("%s",return_value); } if ( strcmp(command_name, "profile_check" ) == 0) { int *expected_profile_time; long remain_profile_time; config = getconfig(&num,config); pinterval = config[3].value; if(pinterval == 0) { printf("-999\n"); return -999; } expected_profile_time = (int*)malloc(10*sizeof(int)); if(expected_profile_time == NULL){printf("error\n");exit(1);} expected_time_get(expected_profile_time); remain_profile_time = auto_run(0,expected_profile_time); if(remain_profile_time <=0 )remain_profile_time = 0; printf("%d\n",remain_profile_time); free(expected_profile_time); return remain_profile_time; } if ( strcmp(command_name, "profile_reset") == 0 ) { //feedback = dbinit(2); //reading_hourly(); system("ps aux | grep -e 'master profilego' | grep -v grep | awk '{print $2}' | xargs -i kill {}"); feedback = set_profile(0,0,0,0); return_value = json_float_option("status",feedback); printf("%s\n",return_value); } if ( strcmp(command_name, "profilego") == 0 ) { double stayposition, curr_position,tmp,cal_position; double sdl_read,offset; long wait_time,motor_status; int year; time_t fail_time; int stop_check = -1; int count,fini_count,re_try1 = 0,re_power1 =0,re_try = 0,re_send=0; int i=1,sample = 0,profile_times,sample_indicator; int * expected_tm, *curr_time,inter_val; setconfig(10,0); profile: /* The following eight lines are used to get profile arguments from database */ config = getconfig(&num,config); pdepth = config[0].value; pspacing = config[1].value; pdwell = config[2].value; pinterval = config[3].value; profiled = config[4].value; sample = config[9].value; offset = config[10].value; profile_times = 1+(pdepth - offset)/pspacing; // Caculate the profile times inter_val = (int)pinterval; if(pinterval == 0){schedule_reading();goto profile;} if(profiled == 0 ) { config = getconfig(&num,config); pdepth = config[0].value; pspacing = config[1].value; pdwell = config[2].value; pinterval = config[3].value; profiled = config[4].value; sample = config[9].value; /* The following part are used to get the expected profile time and compare with current time */ expected_tm = (int*)malloc(10*sizeof(int)); curr_time = (int*)malloc(10*sizeof(int)); if(curr_time == NULL){printf("error\n");exit(1);} if(expected_tm == NULL){printf("error\n");exit(1);} do{ config = getconfig(&num,config); sample = config[9].value; expected_time_get(expected_tm); wait_time= auto_run(0,expected_tm); curr_time = check_time(curr_time); sample_indicator = curr_time[3]%inter_val; printf("Wait for next profile\n"); //because the board will boot up 3 minutes after clock time if(wait_time < -600) { profile_time_check(pinterval); goto profile; } sleep(1); }while(wait_time>0); free(expected_tm); four_minute_delay: sleep(1); curr_time = check_time(curr_time); if((curr_time[4]>=4) &&(curr_time[4]<=10))goto start_profile; if(curr_time[4]<4)goto four_minute_delay; if(curr_time[4]>10)goto profile; } start_profile: enable(0); initbus(1); sleep(1); ctx = modbusconnection(ctx); if(ctx == NULL)goto profile; modbus = 1; sleep(9); if ( profiled == 0 ) { if (process_syncclock() == 0) { login("Failed in connectting with SDL"); return; } if (process_expression(3,1,profile_times,0) == 0) { login("Failed in send profile to SDL"); return; } login("Start Profiling"); if( process_read_home_switch(1) == 0) { gotoposition(ctx, 0,rd_position_registers); //Do not need to check if it is home because we want it home anyway do{ usleep(5000); stop_check = process_check(1); }while(stop_check != 0); if(process_pass_through_check() == 0)initbus(0); setconfig(5,1); //Set the profile flag sleep(pdwell); } else { setconfig(5,1); sleep(pdwell); } } enable(0); initbus(1); sleep(1); ctx = modbusconnection(ctx); /* This following part is used to determine where is destination */ curr_position = checkposition(ctx,tab_rp_registers) + offset; cal_position = i*pspacing + offset; if ( cal_position < pdepth ) { stayposition = cal_position; } else stayposition = pdepth; i++; stayposition = stayposition - offset; gotoposition(ctx,stayposition,tab_rp_registers); position1: sleep(1); ctx = modbusconnection(ctx); curr_position = checkposition(ctx,tab_rp_registers) + offset; if(curr_position == -1) { if(re_power1 < 3) { if(re_try1 < 2) { sleep(3); re_try1++; goto position1; } else { re_try1 = 0; enable(0); initbus(0); sleep(3); initbus(1); sleep(1); re_power1++; goto position1; } } else { enable(0); initbus(0); enable(1); re_power1 = 0; return -1; } } if (!((stayposition -0.1) <= curr_position ) && (curr_position <= (stayposition + 0.1)))goto position1; wait_for_stop(ctx, tab_rp_registers); //Here check position in order to determine if it is destination now curr_position = checkposition(ctx,tab_rp_registers)+offset; setconfig(9,curr_position); printf("Go to sleep for dwell time\n"); if(process_pass_through_check() == 0)initbus(0); // Add this part to check if it is passing command sleep(pdwell); if (((pdepth -0.1) <= curr_position) && (curr_position <= (pdepth + 0.1))) { setconfig(5,0); //Set profile flag here to avoid reprofile if something wrong happens during following profile_time_check(pinterval); // Set next profile time enable(0); initbus(1); ctx = modbusconnection(ctx); gotoposition(ctx,0,rd_position_registers); // After finish profile, go home wait_for_stop(ctx, tab_rp_registers); sleep(1); enable(0); if(process_pass_through_check() == 0) initbus(0); //Check is pass through enable(1); setconfig(9,offset); //Save the position 0 sleep(40); goto profile; } goto profile; } /*********************************************************************** * The next three command_name are used * * to control the date and time * ************************************************************************/ if ( strcmp(command_name, "checktime") == 0 ) { /* char *sdate; if ( (sdate = malloc( 80 * sizeof(char) ) )== NULL)return NULL; sdate = current_time(sdate); return_value = json_option_string("status",sdate); printf("%s\n",return_value); free(sdate); */ //long pystep = process_read_step(1); //process_position(pystep); //process_expression(3,1,3,1); //process_pass_through_check(); //process_syncclock(); process_expression(3,1,3,0); process_read_home_switch(1); } if ( strcmp(command_name, "settime") == 0 ) { if ( argc < 4 ) { print_comusage (stderr, 1); } char *date = argv[2]; char *time = argv[3]; int *buf = (int*)malloc(6*sizeof(int)); parse(buf,date,time); int i,m_buf[6]; for(i=0;i<=5;i++) { m_buf[i]=*(buf+i); } feedback = set_time(&m_buf); return_value = json_option("status:",feedback); printf("%s\n",return_value); login("Set local time"); login(return_value); sleep(5); } if ( strcmp(command_name, "voltage") == 0 ) { double voltage; voltage = voltage_read(); return_value = json_float_option("status",voltage); printf("%s\n",return_value); } if ( strcmp(command_name, "temp") == 0 ) { double temp; temp = temp_read(); return_value = json_float_option("status",temp); printf("%s\n",return_value); } if(strcmp(command_name, "enable_power") == 0) { enable(0); initbus(1); return_value = json_option("status:",1); } if(strcmp(command_name, "disable_power") == 0) { enable(0); initbus(0); return_value = json_option("status:",1); } if ( strcmp(command_name, "backup") == 0 ) { feedback = system("cp /home/sampler/lr.sl3 /home/sampler/lr_default.sl3"); if(feedback == -1) { return_value = json_float_option("status",-1); } else return_value = json_float_option("status",1); printf("%s\n",return_value); } if ( strcmp(command_name, "restore") == 0 ) { feedback = system("cp /home/sampler/lr_default.sl3 /home/sampler/lr.sl3"); if(feedback == -1) { return_value = json_float_option("status",-1); } else return_value = json_float_option("status",1); printf("%s\n",return_value); } if ( strcmp(command_name, "debug") == 0 ) { long return_steps; char *debug_result; enable(0); initbus(1); sleep(1); ctx = modbusconnection(ctx); modbus = 1; uint16_t *debug_position_registers = (uint16_t*)malloc(2*sizeof(uint16_t)); debug_result = (char*)malloc(50*sizeof(char)); return_steps = checksteps(ctx,debug_position_registers); sprintf(debug_result,"%x,%x,%x\n",debug_position_registers[0],debug_position_registers[1],return_steps); json_option_string("status",debug_result); printf("%s\n",debug_result); initbus(0); } if ( strcmp(command_name, "power_check") == 0 ) { int power_status = check_power(); printf("Power is %d\n",power_status); } if ( strcmp(command_name, "sdl") == 0 ) { modbus_t * sdl; sdl = sdl_connection(sdl); if(sdl != NULL) { double vol = sdltest(sdl); return_value = json_float_option("status",vol); printf("%s\n",return_value); setsdl(30000,vol); login("Read SDL"); login(return_value); } else setsdl(30000,-100000); modbus_close(sdl); modbus_free(sdl); } if ( strcmp(command_name, "sdl_reset") == 0 ) { resetsdl(); } if ( strcmp(command_name, "sdl_get") == 0 ) { int num_records; SLEN * sdl_records; sdl_records = (SLEN*)malloc(100*sizeof(SLEN)); sdl_records = getsdl(&num_records, sdl_records); return_value = json_sdl_option(num_records,sdl_records); printf("%s\n",return_value); free(sdl_records); } if ( strcmp(command_name, "sdl_uploadtime") == 0 ) { modbus_t * sdl; sdl = sdl_connection(sdl); if(sdl == NULL) { setsdl(30000,-100000); } else sdl_setuploadtime(sdl,12,5,21,12,50,0); } if ( strcmp(command_name, "sdl_settime") == 0 ) { modbus_t * sdl; sdl = sdl_connection(sdl); if(sdl == NULL) { setsdl(30000,-100000); } else sdl_rtc_time(sdl,12,5,25,7,58,0); } if ( strcmp(command_name, "sdl_readsize") == 0 ) { modbus_t * sdl; sdl = sdl_connection(sdl); if(sdl == NULL) { setsdl(30000,-100000); } else sdl_readbuffsize(sdl); } if ( strcmp(command_name, "sdl_readsensor") == 0 ) { modbus_t * sdl; sdl = sdl_connection(sdl); if(sdl == NULL) { setsdl(30000,-100000); } else sdl_read_sensor(sdl,1,1); } if ( strcmp(command_name, "sdl_upload") == 0 ) { //sdl_read_log_data(16); int number; modbus_t * sdl; sdl = sdl_connection(sdl); if(sdl == NULL) { setsdl(30000,-100000); } else { profile_save_data(sdl); } modbus_close(sdl); } if ( strcmp(command_name, "sdl_sample") == 0 ) { int number; modbus_t * sdl; sdl = sdl_connection(sdl); if(sdl == NULL) { setsdl(30000,-100000); } else { sdl_read_sensor(sdl,1,1); sleep(60); sample_save_data(sdl); //sdl_start_profile(sdl,2); } modbus_close(sdl); } if ( strcmp(command_name, "shutdown") == 0 ) { feedback = system("/sbin/shutdown now"); } if ( strcmp(command_name, "maxstep") == 0 ) { enable(0); initbus(1); sleep(1); ctx = modbusconnection(ctx); modbus = 1; feedback = set_max_step(ctx,rd_position_registers); return_value = json_option("status",feedback); initbus(0); } if(strcmp(command_name, "slave") == 0) { slave(); } if(strcmp(command_name, "motor_status") == 0) { if (check_power()== 1) { sleep(1); ctx = modbusconnection(ctx); modbus = 1; feedback = checkmotorstatus(ctx,tab_rp_registers); if(feedback == -1) { printf("0\n"); return 0; } else { printf("%d\n",feedback); return feedback; } } else { printf("0\n"); return 0; } } close: /* Free the memory */ free(config); free(tab_rp_registers); free(rd_position_registers); //modbus_mapping_free(mb_mapping); /* Close the connection */ if (modbus == 1) { modbus_close(ctx); } if (motor_stop == 1) { printf("stop setting\n"); setconfig(9,last_position); } return return_value; }
void File::getall ( void * data, ::DWORD size ) { getall(static_cast<char*>(data), size); }
void wait(CentralIOToDo todo) { getall(todo); ev_loop(ev_default_loop(0), EVLOOP_ONESHOT); }
void poll(CentralIOToDo todo) { getall(todo); ev_loop(ev_default_loop(0), EVLOOP_NONBLOCK); }
static int restore_main(int argc, char **argv) { char *name; int test; int force; int commit; backup_t data; unsigned int size; char s[512]; char tmp[128]; unsigned long hw; char current[NVRAM_SPACE]; char *b, *bk, *bv; char *c, *ck, *cv; int nset; int nunset; int nsame; int cmp; int i; test = 0; force = 0; commit = 1; name = NULL; for (i = 1; i < argc; ++i) { if (argv[i][0] == '-') { if (strcmp(argv[i], "--test") == 0) { test = 1; } else if (strcmp(argv[i], "--force") == 0) { force = 1; } else if (strcmp(argv[i], "--forceall") == 0) { force = 2; } else if (strcmp(argv[i], "--nocommit") == 0) { commit = 0; } else { help(); } } else { name = argv[i]; } } if (!name) help(); strcpy(tmp, "/tmp/nvramXXXXXX"); mktemp(tmp); sprintf(s, "gzip -d < %s > %s", name, tmp); if (system(s) != 0) { unlink(tmp); printf("Error decompressing file.\n"); return 1; } size = f_size(tmp); if ((size <= (sizeof(data) - sizeof(data.buffer))) || (size > sizeof(data)) || (f_read(tmp, &data, sizeof(data)) != size)) { unlink(tmp); printf("Invalid data size or read error.\n"); return 1; } unlink(tmp); if (data.sig != V1) { printf("Invalid signature: %08lX / %08lX\n", data.sig, V1); return 1; } hw = check_hw_type(); if ((data.hwid != hw) && (!force)) { printf("Invalid hardware type: %08lX / %08lX\n", data.hwid, hw); return 1; } // 1 - check data size -= sizeof(data) - sizeof(data.buffer); if ((data.buffer[size - 1] != 0) || (data.buffer[size - 2] != 0)) { CORRUPT: printf("Corrupted data area.\n"); return 1; } b = data.buffer; while (*b) { bk = b; b += strlen(b) + 1; if ((bv = strchr(bk, '=')) == NULL) { goto CORRUPT; } *bv = 0; if (strcmp(bk, "et0macaddr") == 0) { if (!nvram_match(bk, bv + 1)) { if (!force) { printf("Cannot restore on a different router.\n"); return 1; } } } *bv = '='; } if (((b - data.buffer) + 1) != size) { printf("Extra data found at the end.\n"); return 1; } // 2 - set if (!test) { if (!wait_action_idle(10)) { printf("System busy.\n"); return 1; } set_action(ACT_SW_RESTORE); led(LED_DIAG, 1); } nset = nunset = nsame = 0; b = data.buffer; while (*b) { bk = b; b += strlen(b) + 1; bv = strchr(bk, '='); *bv++ = 0; if ((force != 1) || (in_defaults(bk))) { if (!nvram_match(bk, bv)) { if (test) printf("nvram set \"%s=%s\"\n", bk, bv); else nvram_set(bk, bv); ++nset; } else { ++nsame; } } *(bv - 1) = '='; } // 3 - unset getall(current); c = current; while (*c) { ck = c; c += strlen(c) + 1; if ((cv = strchr(ck, '=')) == NULL) { printf("Invalid data in NVRAM: %s.", ck); continue; } *cv++ = 0; if ((force != 1) || (in_defaults(ck))) { cmp = 1; b = data.buffer; while (*b) { bk = b; b += strlen(b) + 1; bv = strchr(bk, '='); *bv++ = 0; cmp = strcmp(bk, ck); *(bv - 1) = '='; if (cmp == 0) break; } if (cmp != 0) { ++nunset; if (test) printf("nvram unset \"%s\"\n", ck); else nvram_unset(ck); } } } if ((nset == 0) && (nunset == 0)) commit = 0; printf("\nPerformed %d set and %d unset operations. %d required no changes.\n%s\n", nset, nunset, nsame, commit ? "Committing..." : "Not commiting."); fflush(stdout); if (!test) { set_action(ACT_IDLE); if (commit) nvram_commit(); } return 0; }
static int export_main(int argc, char **argv) { char *p; char buffer[NVRAM_SPACE]; int eq; int mode; int all, n, skip; char *bk, *bv, *v; // C, set, quote static const char *start[4] = { "\"", "nvram set ", "{ \"", "" }; static const char *stop[4] = { "\"", "\"", "\" },", "" }; getall(buffer); p = buffer; all = 1; for (n = 1; n < argc; ++n) { if (strcmp(argv[n], "--nodefaults") == 0) { if (argc < 3) help(); all = 0; if (n == 1) ++argv; break; } } if (strcmp(argv[1], "--dump") == 0) { if (!all) help(); for (p = buffer; *p; p += strlen(p) + 1) { puts(p); } return 0; } if (strcmp(argv[1], "--dump0") == 0) { if (!all) help(); for (p = buffer; *p; p += strlen(p) + 1) { } fwrite(buffer, p - buffer, 1, stdout); return 0; } if (strcmp(argv[1], "--c") == 0) mode = X_C; else if (strcmp(argv[1], "--set") == 0) mode = X_SET; else if (strcmp(argv[1], "--tab") == 0) mode = X_TAB; else if (strcmp(argv[1], "--quote") == 0) mode = X_QUOTE; else help(); while (*p) { eq = 0; if (!all) { skip = 0; bk = p; p += strlen(p) + 1; bv = strchr(bk, '='); *bv++ = 0; if ((v = (char *)get_default_value(bk)) != NULL) { skip = (strcmp(bv, v) == 0); } *(bv - 1) = '='; if (skip) continue; else p = bk; } printf("%s", start[mode]); do { switch (*p) { case 9: if (mode == X_SET) putchar(*p); else printf("\\t"); break; case 13: if (mode == X_SET) putchar(*p); else printf("\\r"); break; case 10: if (mode == X_SET) putchar(*p); else printf("\\n"); break; case '"': case '\\': printf("\\%c", *p); break; case '$': case '`': if (mode != X_SET) putchar(*p); else printf("\\$"); break; case '=': if ((eq == 0) && (mode > X_QUOTE)) { printf((mode == X_C) ? "\", \"" : ((mode == X_SET) ? "=\"" : "\t")); eq = 1; break; } eq = 1; default: if (!isprint(*p)) printf("\\x%02x", *p); else putchar(*p); break; } ++p; } while (*p); printf("%s\n", stop[mode]); ++p; } return 0; }