int get_param_double(char *value, double *res) { if (strcmp(value, "random")) { *res = strtod(value, NULL); } else { *res = get_random_double(); } return 0; }
int get_param_double_range(char *value, double *res, double min, double max) { if (strcmp(value, "random")) { *res = strtod(value, NULL); } else { *res = get_random_double() * (max - min) + min; } return 0; }
/* ************************************************** */ void rx(call_t *c, packet_t *packet) { struct nodedata *nodedata = get_node_private_data(c); array_t *up = get_entity_bindings_up(c); int i = up->size; /* radio sleep */ if (nodedata->sleep) { packet_dealloc(packet); return; } /* half-duplex */ if (nodedata->tx_busy != -1) { packet_dealloc(packet); return; } /* handle carrier sense */ if (nodedata->rx_busy == packet->id) { nodedata->rx_busy = -1; nodedata->rxdBm = MIN_DBM; /* log rx */ PRINT_REPLAY("radio-rx1 %"PRId64" %d\n", get_time(), c->node); /* consume energy */ battery_consume_rx(c, packet->duration); } else { packet_dealloc(packet); return; } /* check wether the reception has killed us */ if (!is_node_alive(c->node)) { packet_dealloc(packet); return; } /* drop packet depending on the FER */ if (get_random_double() < packet->PER) { packet_dealloc(packet); return; } /* forward to upper layers */ while (i--) { call_t c_up = {up->elts[i], c->node, c->entity}; packet_t *packet_up; if (i > 0) { packet_up = packet_clone(packet); } else { packet_up = packet; } RX(&c_up, packet_up); } return; }
/* ************************************************** */ double compute_fading(struct entitydata *entitydata) { // struct entitydata *entitydata = get_entity_private_data(c); double sum = 0; int i; for(i=0; i < entitydata->m; i++){ sum = sum - log(get_random_double()); } return 10 * log10(sum/entitydata->m); }
/* ************************************************** */ double propagation(call_t *c, packet_t *packet, nodeid_t src, nodeid_t dst, double rxdBm) { struct entitydata *entitydata = get_entity_private_data(c); double success = *(entitydata->success + (src * entitydata->node_cnt) + dst); if (success == 1) { return rxdBm; } else if (success == 0) { return MIN_DBM; } else if (get_random_double() <= success) { return rxdBm; } else { return MIN_DBM; } }
/* ************************************************** */ int bootstrap(call_t *c) { struct nodedata *nodedata = get_node_private_data(c); call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity}; /* get mac header overhead */ nodedata->overhead = GET_HEADER_SIZE(&c0); /* hello packet */ if (nodedata->period > 0) { uint64_t start = get_time() + nodedata->start + get_random_double() * nodedata->period; scheduler_add_callback(start, c, advert_callback, NULL); } return 0; }
mychild* child_create(QofBook *book) { mychild *g; g_return_val_if_fail(book, NULL); g = g_object_new(GNC_TYPE_MYCHILD, NULL); qof_instance_init_data (&g->inst, CHILD_MODULE_NAME, book); g->date = *get_random_timespec(); g->discount = get_random_double(); g->active = get_random_boolean(); g->version = get_random_int_in_range(1, 10000); g->minor = get_random_int_in_range(100001, 99999999); g->flag = get_random_character(); g->Name = get_random_string(); g->Amount = get_random_gnc_numeric(); qof_event_gen(&g->inst, QOF_EVENT_CREATE, NULL); return g; }
int bootstrap(call_t *c) { struct nodedata *nodedata = get_node_private_data(c); entityid_t *down = get_entity_links_down(c); call_t c0 = {down[0], c->node, c->entity}; uint64_t schedule = get_time() + nodedata->h_start + get_random_double() * nodedata->h_period; /* get overhead */ if ((get_entity_type(&c0) != MODELTYPE_ROUTING) && (get_entity_type(&c0) != MODELTYPE_MAC)) { nodedata->overhead = 0; } else { nodedata->overhead = GET_HEADER_SIZE(&c0); } /* scheduler first hello */ if (nodedata->h_nbr == -1 || nodedata->h_nbr > 0) { scheduler_add_callback(schedule, c, hello_callback, NULL); } return 0; }
static void kaa_demo_add_log_record(void *context) { static size_t log_number = LOGS_TO_SEND_COUNT; kaa_logging_power_report_t *log_record = kaa_logging_power_report_create(); if (!log_record) { printf("Failed to create log record, error code %d\n", KAA_ERR_NOMEM); return; } log_record->timestamp = time(NULL) * 1000; // expected in millis log_record->samples = kaa_list_create(); size_t zone_id = 0; size_t panel_id = 0; for (zone_id = 0; zone_id < ZONE_COUNT; ++zone_id) { for (panel_id = 0; panel_id < PANEL_COUNT; ++panel_id) { kaa_logging_power_sample_t *sample = kaa_logging_power_sample_create(); sample->zone_id = zone_id; sample->panel_id = panel_id; sample->power = get_random_double(MAX_PANEL_POWER); kaa_list_push_back(log_record->samples, sample); } } kaa_error_t error_code = kaa_logging_add_record(kaa_client_get_context((kaa_client_t *)context)->log_collector, log_record); if (error_code) { printf("Failed to add log record, error code %d\n", error_code); } log_record->destroy(log_record); if (!--log_number) { kaa_client_stop(kaa_client); } }
/* ************************************************** */ double normal (double avg, double deviation) { return (avg + deviation * cos(2*M_PI*get_random_double()) * sqrt(-2.0 * log(get_random_double()))); }
/* ************************************************** */ int get_param_time(char *value, uint64_t *time) { uint64_t t1, t2; char *endptr; /* Random? */ if (strcmp(value, "random") == 0) { *time = get_random_time(); return 0; } /* skip space */ while (isspace(*value)) value++; /* decode integer */ t1 = strtoll(value, &endptr, 10); if (value == endptr) goto missing; value = endptr; /* skip space */ while (isspace(*value)) value++; /* decode unit */ if (!strncmp(value, "s", 1)) { t1 = t1 * 1000000000; value += 1; } else if (!strncmp(value, "ms", 2)) { t1 = t1 * 1000000; value += 2; } else if (!strncmp(value, "us", 2)) { t1 = t1 * 1000; value += 2; } else if (!strncmp(value, "ns", 2)) { value += 2; } /* skip space */ while (isspace(*value)) value++; /* finished? */ if (*value == '\0') { *time = t1; return 0; } /* range string */ if (strncmp(value, "..", 2)) goto garbage; value += 2; /* skip space */ while (isspace(*value)) value++; /* decode integer */ t2 = strtoll(value, &endptr, 10); if (value == endptr) { missing: fprintf(stderr, "get_param_time(): missing time value ('%s')\n", value); return -1; } value = endptr; /* skip space */ while (isspace(*value)) value++; /* decode unit */ if (!strncmp(value, "s", 1)) { t2 = t2 * 1000000000; value += 1; }else if (!strncmp(value, "ms", 2)) { t2 = t2 * 1000000; value += 2; } else if (!strncmp(value, "us", 2)) { t2 = t2 * 1000; value += 2; } else if (!strncmp(value, "ns", 2)) { value += 2; } /* skip space */ while (isspace(*value)) value++; /* garbage? */ if (*value != '\0') { garbage: fprintf(stderr, "get_param_time(): garbage at end of time\n"); return -1; } /* Generate random value */ if (t1 > t2) { fprintf(stderr, "get_param_time(): time range must be min..max\n"); return -1; } *time = t1 + (t2 - t1) * get_random_double(); return 0; }
int state_machine(call_t *c, void *args) { struct nodedata *nodedata = get_node_private_data(c); call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity}; uint64_t timeout; uint64_t backoff; packet_t *packet; if (nodedata->clock != get_time()) { return 0; } switch (nodedata->state) { case STATE_IDLE: if (nodedata->txbuf == NULL) { nodedata->txbuf = (packet_t *) das_pop_FIFO(nodedata->packets); if (nodedata->txbuf == NULL) { return 0; } } if (nodedata->MaxCSMABackoffs != 0) { nodedata->state = STATE_BACKOFF; nodedata->BE = nodedata->MinBE; nodedata->NB = 0; backoff = get_random_double() * (pow(2, nodedata->BE) - 1) * aUnitBackoffPeriod; nodedata->clock = get_time() + backoff; scheduler_add_callback(nodedata->clock, c, state_machine, NULL); return 0; } else { nodedata->state = STATE_TX; nodedata->clock = get_time(); scheduler_add_callback(nodedata->clock, c, state_machine, NULL); return 0; } case STATE_BACKOFF: if (check_channel_busy(c)) { if ((++nodedata->BE) > nodedata->MaxBE) { nodedata->BE = nodedata->MaxBE; } if (++nodedata->NB >= nodedata->MaxCSMABackoffs) { packet_dealloc(nodedata->txbuf); nodedata->txbuf = NULL; nodedata->state = STATE_IDLE; nodedata->clock = get_time(); state_machine(c,NULL); return 0; } backoff = get_random_double() * (pow(2, nodedata->BE) - 1) * aUnitBackoffPeriod; nodedata->clock = get_time() + backoff; scheduler_add_callback(nodedata->clock, c, state_machine, NULL); return 0; } nodedata->state = STATE_TX; nodedata->clock = get_time(); state_machine(c,NULL); return 0; case STATE_TX: packet = nodedata->txbuf; nodedata->txbuf = NULL; timeout = packet->size * 8 * radio_get_Tb(&c0) + macMinSIFSPeriod; TX(&c0, packet); nodedata->state = STATE_TXING; nodedata->clock = get_time() + timeout; scheduler_add_callback(nodedata->clock, c, state_machine, NULL); return 0; case STATE_TXING: nodedata->state = STATE_IDLE; nodedata->clock = get_time(); state_machine(c,NULL); return 0; default: break; } return 0; }
bool rng::get_random_bool(double probability) { assert(probability >= 0.0 && probability <= 1.0); return get_random_double() < probability; }
/* ************************************************** */ double compute_fading() { // return 5.0 * log10(-2.0 * VARIANCE * log(get_random_double())); return 10*log10(-1.55 * VARIANCE * log(get_random_double())); }