temp_t randomIntervalTest(ActuatorPwm* act, ActuatorDigital * target, temp_t duty, int delayMax) { act->setValue(duty); ticks_millis_t lowToHighTime = ticks.millis(); ticks_millis_t highToLowTime = ticks.millis(); ticks_millis_t totalHighTime = 0; ticks_millis_t totalLowTime = 0; output << format("\n\n*** Results running 100 periods and random 0-%d ms update intervals," "with duty cycle %u and period %u ***\n") % delayMax % duty % act->getPeriod(); #if PRINT_TOGGLE_TIMES output << "\n\nl->h timestamp h->l timestamp high time low time\n"; #endif // run for 100 periods for (int i = 0; i < 100; i++) { do { highToLowTime = random_delay(delayMax); act->update(); } while (target->isActive()); ticks_millis_t highTime = highToLowTime - lowToHighTime; if (i > 0) { // skip first cycle in totals, it can be incomplete totalHighTime += highTime; } #if PRINT_TOGGLE_TIMES output << format("_/ %10u \t \\_ %10u \t") % lowToHighTime % highToLowTime; output << format(" %10u ms \t") % highTime; #endif do { lowToHighTime = random_delay(delayMax); act->update(); } while (!target->isActive()); ticks_millis_t lowTime = lowToHighTime - highToLowTime; if (i > 0) { // skip first cycle in totals, it can have old duty cycle totalLowTime += lowTime; #if PRINT_TOGGLE_TIMES output << format("%10u ms \n") % lowTime; #endif } else{ #if PRINT_TOGGLE_TIMES output << format("%10u ms (ignored)\n") % lowTime; #endif } } double avgDuty = double(totalHighTime) / (totalHighTime + totalLowTime); temp_t avgDutyTemp = temp_t(round(avgDuty * double(act->max()))); output << "total high time: " << totalHighTime << "\n" << "total low time: " << totalLowTime << "\n" << "avg duty: " << avgDutyTemp << "/" << act->max() << "\n"; return avgDutyTemp; }
blink_random() //rrrrr { int last_on,on,off,i; for (i=0;i< 10;i++) { on = rand() % 16; printf("on = %d\n",on); digitalWrite(Pins[on], HIGH); random_delay(rand() % 1000000); digitalWrite(Pins[on], LOW); random_delay(rand() % 1000000); } }
static int run_enable_sequence() { /*disable the master enable */ enable_enable(0); dmp_event_control(); enable_gyro(1); enable_accel(1); if (has_compass) enable_compass(1); enable_quaternion(1); enable_accel_output(1); write_dmp_event(0); write_dmp_output_rate(10); enable_motion(0); /*enable the master enable */ enable_enable(1); if (enable_random_delay) random_delay(); else { printf("sleep %ds\n", enable_delay); sleep(enable_delay); } return 0; }
bool maybe_random_delay(cb_t *cb, void (cb_t::*method)(arg1_t), arg1_t arg) { if (randint(2) == 0) { return true; } else { random_delay(cb, method, arg); return false; } }
void random_delay(cb_t *cb, void (cb_t::*method)()) { rassert(cb); no_arg_caller_t<cb_t> *c = new no_arg_caller_t<cb_t>; c->cb = cb; c->method = method; random_delay(&no_arg_caller_t<cb_t>::on_timer, c); }
void random_delay(cb_t *cb, void (cb_t::*method)(arg1_t), arg1_t arg) { rassert(cb); one_arg_caller_t<cb_t, arg1_t> *c = new one_arg_caller_t<cb_t, arg1_t>; c->cb = cb; c->method = method; c->arg = arg; random_delay(&one_arg_caller_t<cb_t, arg1_t>::on_timer, c); }
bool read_all_rows_as_vector( auth::user_context_t const &user_context, signal_t *interruptor, std::vector<ql::datum_t> *rows_out, UNUSED admin_err_t *error_out) { random_delay(interruptor); on_thread_t thread_switcher(home_thread()); user_context.require_admin_user(); rows_out->clear(); for (auto const &item : data) { rows_out->push_back(item.second); } return true; }
bool write_row( auth::user_context_t const &user_context, ql::datum_t primary_key, UNUSED bool pkey_was_autogenerated, ql::datum_t *new_value_inout, signal_t *interruptor, UNUSED admin_err_t *error_out) { random_delay(interruptor); on_thread_t thread_switcher(home_thread()); user_context.require_admin_user(); if (new_value_inout->has()) { /* Not all datums can be serialized into an actual table (r.minval, r.maxval and large arrays in particular). To make the in-memory test table behave as closely to an actual table as possible, we attempt to serialize the datum, check for errors, and then discard the serialization result. */ { write_message_t wm; ql::serialization_result_t res = ql::datum_serialize( &wm, *new_value_inout, ql::check_datum_serialization_errors_t::YES); if (res & ql::serialization_result_t::ARRAY_TOO_BIG) { rfail_typed_target(new_value_inout, "Array too large for disk " "writes (limit 100,000 elements)."); } else if (res & ql::serialization_result_t::EXTREMA_PRESENT) { rfail_typed_target(new_value_inout, "`r.minval` and `r.maxval` " "cannot be written to disk."); } r_sanity_check(!ql::bad(res)); } ql::datum_t primary_key_2 = new_value_inout->get_field("id", ql::NOTHROW); guarantee(primary_key_2.has()); guarantee(primary_key == primary_key_2); data[primary_key.print_primary()] = *new_value_inout; } else { data.erase(primary_key.print_primary()); } notify_row(primary_key); return true; }
bool read_row( auth::user_context_t const &user_context, ql::datum_t primary_key, signal_t *interruptor, ql::datum_t *row_out, UNUSED admin_err_t *error_out) { random_delay(interruptor); on_thread_t thread_switcher(home_thread()); user_context.require_admin_user(); auto it = data.find(primary_key.print_primary()); if (it != data.end()) { *row_out = it->second; } else { *row_out = ql::datum_t(); } return true; }
static int run_disable_sequence() { enable_enable(0); enable_gyro(0); enable_accel(1); enable_quaternion(0); enable_accel_output(0); write_dmp_event(1); enable_motion(enable_motion_on); enable_enable(1); if (enable_random_delay) random_delay(); else { printf("sleep %ds\n", disable_delay); sleep(disable_delay); } return 0; }
static int run_enable_sequence() { bool g, a, out; counter++; g = rand()%2; a = rand()%2; if (!g && !a) a = true; //g = true; //a = true; /*disable the master enable */ enable_enable(0); if(g) { enable_gyro(1); if (rand()%2) { out = rand()%2; enable_quaternion(out); enable_gyro_output(!out); } else { enable_quaternion(1); enable_gyro_output(1); } // enable_quaternion(0); // enable_gyro_output(0); } else { enable_gyro(0); enable_gyro_output(0); enable_quaternion(0); } if(a) { enable_accel(1); enable_accel_output(1); } else { enable_accel(0); enable_accel_output(0); } if (has_compass) { if(rand()%2) enable_compass(1); else enable_compass(0); enable_compass(counter%2); //enable_compass(0); } if (has_pressure) { if(rand()%2) enable_pressure(1); else enable_pressure(0); enable_pressure(counter%3); //enable_pressure(0); } enable_step_detector(1); enable_step_indicator(1); //enable_step_detector(0); //enable_step_indicator(0); write_dmp_event(0); enable_motion(0); if (accel_engine_is_on) dmp_event_control(1); else dmp_event_control(0); first_flag = 1; /*enable the master enable */ enable_enable(1); //write_sysfs_string_and_verify("wake_unlock", "/sys/power/", "hack"); if (enable_random_delay) random_delay(); else { printf("sleep %ds\n", enable_delay); sleep(enable_delay); } return 0; }
static #endif int real_main(int argc, char *argv[]) { int ret=1; int option=0; int daemon=1; int forking=1; int strip=0; int randomise=0; struct lock *lock=NULL; struct conf **confs=NULL; int forceoverwrite=0; enum action act=ACTION_LIST; const char *backup=NULL; const char *backup2=NULL; char *restoreprefix=NULL; char *stripfrompath=NULL; const char *regex=NULL; const char *browsefile=NULL; char *browsedir=NULL; const char *conffile=get_conf_path(); const char *orig_client=NULL; const char *logfile=NULL; // The orig_client is the original client that the normal client // would like to restore from. #ifndef HAVE_WIN32 int generate_ca_only=0; #endif int vss_restore=1; int test_confs=0; enum burp_mode mode; log_init(argv[0]); #ifndef HAVE_WIN32 if(!strcmp(prog, "bedup")) return run_bedup(argc, argv); if(!strcmp(prog, "bsigs")) return run_bsigs(argc, argv); #endif while((option=getopt(argc, argv, "a:b:c:C:d:fFghil:nq:Qr:s:tvxjz:?"))!=-1) { switch(option) { case 'a': if(parse_action(&act, optarg)) goto end; break; case 'b': // The diff command may have two backups // specified. if(!backup2 && backup) backup2=optarg; if(!backup) backup=optarg; break; case 'c': conffile=optarg; break; case 'C': orig_client=optarg; break; case 'd': restoreprefix=optarg; // for restores browsedir=optarg; // for lists break; case 'f': forceoverwrite=1; break; case 'F': daemon=0; break; case 'g': #ifndef HAVE_WIN32 generate_ca_only=1; #endif break; case 'i': cmd_print_all(); ret=0; goto end; case 'l': logfile=optarg; break; case 'n': forking=0; break; case 'q': randomise=atoi(optarg); break; case 'Q': log_force_quiet(); break; case 'r': regex=optarg; break; case 's': strip=atoi(optarg); break; case 'v': printf("%s-%s\n", progname(), VERSION); ret=0; goto end; case 'x': vss_restore=0; break; case 't': test_confs=1; break; case 'z': browsefile=optarg; break; case 'h': case '?': default: usage(); goto end; } } if(optind<argc) { usage(); goto end; } if(act==ACTION_MONITOR) { // Try to output everything in JSON. log_set_json(1); #ifndef HAVE_WIN32 // Need to do this so that processes reading stdout get the // result of the printfs of logp straight away. setlinebuf(stdout); #endif } if(!(confs=confs_alloc())) goto end; if(reload(confs, conffile, 1)) goto end; // Dry run to test config file syntax. if(test_confs) { ret=run_test_confs(confs, orig_client); goto end; } if(!backup) switch(act) { case ACTION_DELETE: logp("No backup specified for deletion.\n"); goto end; case ACTION_RESTORE: case ACTION_VERIFY: case ACTION_DIFF: case ACTION_DIFF_LONG: logp("No backup specified. Using the most recent.\n"); backup="0"; default: break; } if(!backup2) switch(act) { case ACTION_DIFF: case ACTION_DIFF_LONG: logp("No second backup specified. Using file system scan.\n"); backup2="n"; // For 'next'. default: break; } // The logfile option is only used for the status client stuff. if(logfile && (act!=ACTION_STATUS && act!=ACTION_STATUS_SNAPSHOT)) logp("-l <logfile> option obsoleted\n"); if(orig_client && *orig_client && set_string(confs[OPT_ORIG_CLIENT], orig_client)) goto end; // The random delay needs to happen before the lock is got, otherwise // you would never be able to use burp by hand. if(randomise) set_int(confs[OPT_RANDOMISE], randomise); mode=get_e_burp_mode(confs[OPT_BURP_MODE]); if(mode==BURP_MODE_CLIENT && (act==ACTION_BACKUP_TIMED || act==ACTION_TIMER_CHECK)) random_delay(confs); if(mode==BURP_MODE_SERVER && act==ACTION_CHAMP_CHOOSER) { // These server modes need to run without getting the lock. } else if(mode==BURP_MODE_CLIENT && (act==ACTION_LIST || act==ACTION_LIST_LONG || act==ACTION_DIFF || act==ACTION_DIFF_LONG || act==ACTION_STATUS || act==ACTION_STATUS_SNAPSHOT || act==ACTION_MONITOR)) { // These client modes need to run without getting the lock. } else { const char *lockfile=confs_get_lockfile(confs); if(!(lock=lock_alloc_and_init(lockfile))) goto end; lock_get(lock); switch(lock->status) { case GET_LOCK_GOT: break; case GET_LOCK_NOT_GOT: logp("Could not get lockfile.\n"); logp("Another process is probably running,\n"); goto end; case GET_LOCK_ERROR: default: logp("Could not get lockfile.\n"); logp("Maybe you do not have permissions to write to %s.\n", lockfile); goto end; } } set_int(confs[OPT_OVERWRITE], forceoverwrite); set_int(confs[OPT_STRIP], strip); set_int(confs[OPT_FORK], forking); set_int(confs[OPT_DAEMON], daemon); strip_trailing_slashes(&restoreprefix); strip_trailing_slashes(&browsedir); if(replace_conf_str(confs[OPT_BACKUP], backup) || replace_conf_str(confs[OPT_BACKUP2], backup2) || replace_conf_str(confs[OPT_RESTOREPREFIX], restoreprefix) || replace_conf_str(confs[OPT_STRIP_FROM_PATH], stripfrompath) || replace_conf_str(confs[OPT_REGEX], regex) || replace_conf_str(confs[OPT_BROWSEFILE], browsefile) || replace_conf_str(confs[OPT_BROWSEDIR], browsedir) || replace_conf_str(confs[OPT_MONITOR_LOGFILE], logfile)) goto end; base64_init(); hexmap_init(); if(mode==BURP_MODE_SERVER) { #ifdef HAVE_WIN32 logp("Sorry, server mode is not implemented for Windows.\n"); #else ret=server_modes(act, conffile, lock, generate_ca_only, confs); #endif } else { ret=client(confs, act, vss_restore); } end: lock_release(lock); lock_free(&lock); confs_free(&confs); return ret; }
int main(int argc, char **argv) { char mac_addr[ETH_ALEN]; char elan_name[32 + 1]; char preferred_les[ATM_ESA_LEN]; /* LANE2 */ char foreId[255]; /* Max size for a TLV */ char atm2textbuff[100]; char esibuff[20]; int esi_set = 0; int listen_addr_set = 0; int atm_set=0; int proxy_flag = 0; int lane_version = 0; /* LANE2 */ int max_frame_size = MTU_UNSPEC; int lecs_method = LECS_WELLKNOWN; int poll_ret = 0, itf = 0, phys_itf = 0, selector = 0; int daemon_flag = 0; pid_t pid; struct sockaddr_atmsvc manual_atm_addr; struct sockaddr_atmsvc listen_addr; char pidbuf[PATH_MAX + 1]; int fd; int retval; memset(elan_name, '\0', sizeof(elan_name)); memset(foreId, '\0', sizeof(foreId)); memset(preferred_les, 0, ATM_ESA_LEN); memset(&manual_atm_addr, 0, sizeof(struct sockaddr_atmsvc)); memset(&listen_addr, 0, sizeof(struct sockaddr_atmsvc)); listen_addr.sas_family = AF_ATMSVC; set_application("zeppelin"); /* for debug msgs */ while(poll_ret != -1) { poll_ret = getopt(argc, argv, "bc:e:n:s:m:l:i:I:q:12pf:t:F:"); switch(poll_ret) { case 'b': daemon_flag = 1; break; case 'c': if (atm_set) { usage(argv[0]); exit(-1); } if (text2atm(optarg, (struct sockaddr *)&manual_atm_addr, sizeof(struct sockaddr_atmsvc), T2A_NAME) < 0) { diag(COMPONENT, DIAG_ERROR, "Invalid LECS address"); usage(argv[0]); exit(-1); } atm2text(atm2textbuff, sizeof(atm2textbuff), (struct sockaddr *)&manual_atm_addr, 0); diag(COMPONENT, DIAG_INFO, "LECS address: %s", atm2textbuff); lecs_method = LECS_MANUAL; atm_set=1; break; case 'e': if(esi_convert(optarg, mac_addr)<0) { diag(COMPONENT, DIAG_ERROR, "Invalid ESI format"); usage(argv[0]); exit(-1); } mac2text(esibuff, mac_addr); diag(COMPONENT, DIAG_DEBUG, "LEC ESI:%s", esibuff); esi_set=1; break; case 'n': if (strlen(optarg) > 32) { diag(COMPONENT, DIAG_ERROR, "ELAN name too long"); exit(-1); } strcpy(elan_name, optarg); diag(COMPONENT, DIAG_INFO, "Vlan name :'%s'", elan_name); break; case 's': if (atm_set) { usage(argv[0]); exit(-1); } if (text2atm(optarg, (struct sockaddr *)&manual_atm_addr, sizeof(struct sockaddr_atmsvc), T2A_NAME) < 0) { diag(COMPONENT, DIAG_ERROR, "Invalid LES address"); usage(argv[0]); exit(-1); } atm2text(atm2textbuff, sizeof(atm2textbuff), (struct sockaddr *)&manual_atm_addr, 0); diag(COMPONENT, DIAG_INFO, "LES address: %s", atm2textbuff); lecs_method = LECS_NONE; atm_set=1; break; case 'm': set_verbosity(NULL, DIAG_DEBUG); break; case 'l': if (isdigit(optarg[0]) && strlen(optarg) < 4 && sscanf(optarg, "%d", &selector) && selector >=0 && selector <= 0xff) { listen_addr.sas_addr.prv[ATM_ESA_LEN - 1] = (char) selector; diag(COMPONENT, DIAG_INFO, "Selector byte set " "to %d", selector); } else { if (text2atm(optarg, (struct sockaddr *)&listen_addr, sizeof(struct sockaddr_atmsvc), T2A_NAME) < 0) { diag(COMPONENT, DIAG_ERROR, "Invalid ATM listen address"); usage(argv[0]); exit(-1); } listen_addr_set = 1; } break; case 'i': if (sscanf(optarg, "%d", &itf) <= 0 || itf >= MAX_LEC_ITF) { diag(COMPONENT, DIAG_ERROR, "Invalid interface number"); usage(argv[0]); exit(-1); } diag(COMPONENT, DIAG_INFO, "Interface number set to %d", itf); break; case 'I': if (sscanf(optarg, "%d", &phys_itf) <= 0 || phys_itf < 0) { diag(COMPONENT, DIAG_ERROR, "Invalid physical interface number"); usage(argv[0]); exit(-1); } diag(COMPONENT, DIAG_INFO, "Physical interface number set to %d", phys_itf); break; case 'q': #if 0 if (text2qos(optarg,NULL,0) < 0) { diag(COMPONENT, DIAG_ERROR, "Invalid QOS specification"); usage(argv[0]); exit(-1); } qos_spec = optarg; #endif diag(COMPONENT, DIAG_INFO, "-q is deprecated, ignoring it"); break; case '1': lane_version = 1; break; case '2': lane_version = 2; break; case 'p': proxy_flag = 1; break; case 'f': if (strlen(optarg) > 255) { diag(COMPONENT, DIAG_ERROR, "foreId too long"); exit(-1); } memcpy (foreId, optarg, strlen(optarg)); foreId[strlen(optarg)] = '\0'; diag(COMPONENT, DIAG_INFO, "foreId :'%s'", foreId); break; case 't': /* ERIC */ if( !strncmp( optarg, "1516", 4 )) max_frame_size = MTU_1516; else if( !strncmp( optarg, "1580", 4 )) max_frame_size = MTU_1580; else if( !strncmp( optarg, "4544", 4 )) max_frame_size = MTU_4544; else if( !strncmp( optarg, "9234", 4 )) max_frame_size = MTU_9234; else if( !strncmp( optarg, "18190", 5 )) max_frame_size = MTU_18190; break; case 'F': set_logfile(optarg); diag(COMPONENT, DIAG_DEBUG, "logfile set to %s", optarg); break; case -1: break; default: usage(argv[0]); exit(-1); } } if (argc != optind) { usage(argv[0]); exit(1); } if (lane_version == 1 && max_frame_size == MTU_1580) { diag(COMPONENT, DIAG_ERROR, "MTU 1580 not defined with LANEv1"); exit(-1); } /* Reserve signals */ signal(SIGHUP, sig_reset); signal(SIGPIPE, SIG_IGN); if (!esi_set) { if(addr_getesi(mac_addr, phys_itf) < 0) { diag(COMPONENT, DIAG_ERROR, "Can't get ESI from kernel!"); return -1; } mac2text(esibuff, mac_addr); diag(COMPONENT, DIAG_DEBUG, "LEC ESI:%s", esibuff); if (itf != 0) mac_addr[0] = 0x2 | ((itf - 1) << 2); } if ((itf = kernel_init(mac_addr, itf)) < 0 ) { diag(COMPONENT, DIAG_FATAL, "Kernel interface creation failed, exiting..."); return -1; } if (daemon_flag == 1) { daemon_flag = 0; pid = fork(); if (pid < 0) { diag(COMPONENT, DIAG_FATAL, "fork failed, exiting..."); return -1; } if (pid) { /* parent */ return 0; } else { /* child */ if (setsid() < 0) { diag(COMPONENT, DIAG_FATAL, "setsid failed, exiting..."); return -1; } } } sprintf(pidbuf, "/var/run/lec%d.pid", itf); fd = open(pidbuf, O_CREAT | O_WRONLY, 0600); if (fd < 0) { diag(COMPONENT, DIAG_FATAL, "open(%s, ..) failed, %s", pidbuf, strerror(errno)); return -1; } sprintf(pidbuf, "%d\n", getpid()); write(fd, pidbuf, strlen(pidbuf)); close(fd); /* Loop here until the Sun gets cold */ while (1) { if (!listen_addr_set) { char sel = listen_addr.sas_addr.prv[ATM_ESA_LEN - 1]; if (get_listenaddr(listen_addr.sas_addr.prv, phys_itf) < 0) { diag(COMPONENT, DIAG_FATAL, "Could not figure out my ATM address"); exit(-1); } listen_addr.sas_addr.prv[ATM_ESA_LEN - 1] = sel; } atm2text(atm2textbuff, sizeof(atm2textbuff), (struct sockaddr *)&listen_addr, A2T_NAME | A2T_PRETTY | A2T_LOCAL); diag(COMPONENT, DIAG_INFO, "Our ATM address: %s", atm2textbuff); diag(COMPONENT, DIAG_DEBUG, "initializing lec parameters"); init_lec_params(mac_addr, elan_name, listen_addr.sas_addr.prv, itf, foreId, max_frame_size, proxy_flag, lane_version); if (lecs_method != LECS_MANUAL && lecs_method != LECS_NONE) { diag(COMPONENT, DIAG_DEBUG, "trying to get LECS address from ILMI"); /* Not sure why this memset is necessary */ memset(&manual_atm_addr, 0, sizeof(struct sockaddr_atmsvc)); retval = get_lecsaddr(phys_itf, &manual_atm_addr); if (retval <= 0) { diag(COMPONENT, DIAG_DEBUG, "get_lecsaddr failed; not enough " "memory allocated for all addresses " "or no LECS address registered"); } else { diag(COMPONENT, DIAG_DEBUG, "obtained LECS address from ILMI"); lecs_method = LECS_FROM_ILMI; } } diag(COMPONENT, DIAG_DEBUG, "About to connect LECS"); if (lec_configure(lecs_method, &manual_atm_addr, &listen_addr) < 0) { close_connections(); random_delay(); continue; } diag(COMPONENT, DIAG_DEBUG, "About to connect LES"); if (les_connect(lecs_method, &manual_atm_addr, &listen_addr) < 0) { close_connections(); random_delay(); continue; } diag(COMPONENT, DIAG_DEBUG, "About to connect BUS"); if (bus_connect() < 0) { close_connections(); random_delay(); continue; } diag(COMPONENT, DIAG_DEBUG, "About to create data direct listen socket"); if (create_data_listen() < 0) { close_connections(); random_delay(); continue; } diag(COMPONENT, DIAG_DEBUG, "About to tell kernel our LEC_ID %d", lec_params.c14_lec_id); if (set_lec_id(lec_params.c14_lec_id) < 0) { close_connections(); continue; } diag(COMPONENT, DIAG_DEBUG, "About to tell kernel LEC parameters"); if (config_kernel() < 0) { close_connections(); continue; } diag(COMPONENT, DIAG_DEBUG, "Joined ELAN '%s' successfully", lec_params.c5_elan_name); main_loop(); diag(COMPONENT, DIAG_INFO, "Resetting..."); close_connections(); random_delay(); reset = 0; } return 0; /* not reached */ }