Esempio n. 1
0
/*
 * Starts a oneshot timer with a timeout in seconds.
 */
void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
{
    osi_alarm_t *alarm = NULL;

    assert(p_tle != NULL);

    // Get the alarm for the timer list entry.
    osi_mutex_lock(&btu_oneshot_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
    if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
        alarm = osi_alarm_new("btu_oneshot", btu_oneshot_alarm_cb, (void *)p_tle, 0);
        hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm);
    }
    osi_mutex_unlock(&btu_oneshot_alarm_lock);

    alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
    if (alarm == NULL) {
        LOG_ERROR("%s Unable to create alarm", __func__);
        return;
    }
    osi_alarm_cancel(alarm);

    p_tle->event = type;
    p_tle->in_use = TRUE;
    // NOTE: This value is in seconds but stored in a ticks field.
    p_tle->ticks = timeout_sec;
    osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
}
Esempio n. 2
0
File: hal.c Progetto: morrey/bt_bcm
static bool set_wake_alarm(uint64_t delay_millis, bool should_wake, alarm_cb cb, void *data) {
  static hash_map_t *timers;

  if (!timers) {
    timers = hash_map_new(TIMER_BUCKET_COUNT, hash_function_pointer, NULL, NULL, NULL);
  }

  timer_t *timer = hash_map_get(timers, cb);
  if (!timer) {
    timer = malloc(sizeof(timer_t));
    hash_map_set(timers, cb, timer);

    struct sigevent sigevent;
    memset(&sigevent, 0, sizeof(sigevent));
    sigevent.sigev_notify = SIGEV_THREAD;
    sigevent.sigev_notify_function = (void (*)(union sigval))cb;
    sigevent.sigev_value.sival_ptr = data;
    timer_create(CLOCK_MONOTONIC, &sigevent, timer);
  }

  struct itimerspec new_value;
  new_value.it_value.tv_sec = delay_millis / 1000;
  new_value.it_value.tv_nsec = (delay_millis % 1000) * 1000 * 1000;
  new_value.it_interval.tv_sec = 0;
  new_value.it_interval.tv_nsec = 0;
  if(!timer){
      return false;
  } else {
       timer_settime(*timer, 0, &new_value, NULL);
       return true;
  }

}
Esempio n. 3
0
void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks)
{
    osi_alarm_t *alarm = NULL;

    assert(p_tle != NULL);

    // Get the alarm for the timer list entry.
    osi_mutex_lock(&btu_l2cap_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
    if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
        alarm = osi_alarm_new("btu_l2cap", btu_l2cap_alarm_cb, (void *)p_tle, 0);
        hash_map_set(btu_l2cap_alarm_hash_map, p_tle, (void *)alarm);
    }
    osi_mutex_unlock(&btu_l2cap_alarm_lock);

    alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
    if (alarm == NULL) {
        LOG_ERROR("%s Unable to create alarm", __func__);
        return;
    }
    osi_alarm_cancel(alarm);

    p_tle->event = type;
    p_tle->ticks = timeout_ticks;
    p_tle->in_use = TRUE;
    // The quick timer ticks are 100ms long.
    osi_alarm_set(alarm, (period_ms_t)(timeout_ticks * 100));
}
Esempio n. 4
0
classic_peer_t *classic_peer_by_address(bt_bdaddr_t *address) {
  assert(initialized);
  assert(address != NULL);

  classic_peer_t *peer = hash_map_get(peers_by_address, address);

  if (!peer) {
    pthread_mutex_lock(&bag_of_peers_lock);

    // Make sure it didn't get added in the meantime
    peer = hash_map_get(peers_by_address, address);
    if (peer)
      goto done;

    // Splice in a new peer struct on behalf of the caller.
    peer = osi_calloc(sizeof(classic_peer_t));
    peer->address = *address;
    hash_map_set(peers_by_address, &peer->address, peer);

    pthread_mutex_unlock(&bag_of_peers_lock);
  }

done:
  return peer;
}
Esempio n. 5
0
int insertions (hash_map_t *map, int size)
{
  int idx = 0, r = 0;
  char *key = NULL, *value = NULL, *old_value = NULL;
  int ret = -1;

  /* insert @size random entries
   * key - random number in the range 0 - 100
   * value - 1000000 % key
   */
  for (idx = 0; idx < 100; idx++) {
    r = hh_random ();

    ret = asprintf (&key, "%d", r);
    if (ret < 0)
      printf ("ERROR: asprintf failed\n");

    ret = asprintf (&value, "%04d", ((HH_LIMIT * HH_LIMIT + 1) % r));
    if (ret < 0)
      printf ("ERROR: asprintf failed\n");
      
    old_value = hash_map_set (map, key, value);

    if (old_value) {
      free (old_value);
      old_value = NULL;
    }

    free (key);
  }
}
Esempio n. 6
0
void data_dispatcher_register(data_dispatcher_t *dispatcher, data_dispatcher_type_t type, fixed_queue_t *queue) {
  assert(dispatcher != NULL);

  hash_map_erase(dispatcher->dispatch_table, (void *)type);
  if (queue)
    hash_map_set(dispatcher->dispatch_table, (void *)type, queue);
}
Esempio n. 7
0
/* Store script->live mapping for outbound TCP timestamp value. */
static void set_outbound_ts_val_mapping(
	struct socket *socket, u32 script_timestamp, u32 live_timestamp)
{
	DEBUGP("set_outbound_ts_val_mapping\n");
	DEBUGP("ts_val_mapping %u -> %u\n",
	       ntohl(script_timestamp), ntohl(live_timestamp));
	hash_map_set(socket->ts_val_map,
			     script_timestamp, live_timestamp);
}
Esempio n. 8
0
static void background_connection_add(bt_bdaddr_t *address) {
  assert(address);
  background_connections_lazy_init();
  background_connection_t *connection = hash_map_get(background_connections, address);
  if (!connection) {
    connection = osi_calloc(sizeof(background_connection_t));
    connection->address = *address;
    hash_map_set(background_connections, address, connection);
  }
}
Esempio n. 9
0
static void set_module_state(const module_t *module, module_state_t state) {
  pthread_mutex_lock(&metadata_lock);

  module_state_t *state_ptr = hash_map_get(metadata, module);
  if (!state_ptr) {
    state_ptr = osi_malloc(sizeof(module_state_t));
    hash_map_set(metadata, module, state_ptr);
  }

  pthread_mutex_unlock(&metadata_lock);

  *state_ptr = state;
}
char *test_reset_value() {
  spec_describe("Setting and getting value");

  HashMap *hash_map = HashMap_create(10);
  String *key = String_create("key");
  String *value_1 = String_create("value");
  String *value_2 = String_create("another value");

  hash_map_set(hash_map, key, value_1);
  hash_map_set(hash_map, key, value_2);

  assert_equal(hash_map_get(hash_map, key), value_2, "value same");
  int index = hash_map_index_for_key(hash_map, key);
  assert_ints_equal(list_length((List *)hash_map_list_at_index(hash_map, index)), 1, "no duplicates for key in list");

  string_free(key);
  string_free(value_1);
  string_free(value_2);
  hash_map_free(hash_map);

  return NULL;
}
Esempio n. 11
0
hash_map_t *hash_map_utils_new_from_string_params(const char *params) {
  assert(params != NULL);

  hash_map_t *map = hash_map_new(BUCKETS_NUM, hash_function_string, osi_free,
                                 osi_free, string_equals);
  if (!map)
    return NULL;

  char *str = osi_strdup(params);
  if (!str)
    return NULL;

  LOG_VERBOSE(LOG_TAG, "%s: source string: '%s'", __func__, str);

  // Parse |str| and add extracted key-and-value pair(s) in |map|.
  int items = 0;
  char *tmpstr;
  char *kvpair = strtok_r(str, ";", &tmpstr);
  while (kvpair && *kvpair) {
    char *eq = strchr(kvpair, '=');

    if (eq == kvpair)
      goto next_pair;

    char *key;
    char *value;
    if (eq) {
      key = osi_strndup(kvpair, eq - kvpair);

      // The increment of |eq| moves |eq| to the beginning of the value.
      ++eq;
      value = (*eq != '\0') ? osi_strdup(eq) : osi_strdup("");
    } else {
      key = osi_strdup(kvpair);
      value = osi_strdup("");
    }

    hash_map_set(map, key, value);

    items++;
next_pair:
    kvpair = strtok_r(NULL, ";", &tmpstr);
  }

  if (!items)
    LOG_VERBOSE(LOG_TAG, "%s: no items found in string\n", __func__);

  osi_free(str);
  return map;
}
char *test_set_value() {
  spec_describe("Setting and getting value");

  HashMap *hash_map = HashMap_create(10);
  String *key = String_create("key");
  String *value = String_create("value");

  hash_map_set(hash_map, key, value);

  assert_equal(hash_map_get(hash_map, key), value, "value same");

  string_free(key);
  string_free(value);
  hash_map_free(hash_map);

  return NULL;
}
Esempio n. 13
0
void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks) {
  assert(p_tle != NULL);

  // Get the alarm for the timer list entry.
  pthread_mutex_lock(&btu_l2cap_alarm_lock);
  if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
    hash_map_set(btu_l2cap_alarm_hash_map, p_tle, alarm_new());
  }
  pthread_mutex_unlock(&btu_l2cap_alarm_lock);

  alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
    return;
  }
  alarm_cancel(alarm);

  p_tle->event = type;
  p_tle->ticks = timeout_ticks;
  p_tle->in_use = TRUE;
  // The quick timer ticks are 100ms long.
  alarm_set(alarm, (period_ms_t)(timeout_ticks * 100), btu_l2cap_alarm_cb, (void *)p_tle);
}
Esempio n. 14
0
void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
  assert(p_tle != NULL);

  // Get the alarm for the timer list entry.
  pthread_mutex_lock(&btu_general_alarm_lock);
  if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
    hash_map_set(btu_general_alarm_hash_map, p_tle, alarm_new());
  }
  pthread_mutex_unlock(&btu_general_alarm_lock);

  alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
    return;
  }
  alarm_cancel(alarm);

  p_tle->event = type;
  // NOTE: This value is in seconds but stored in a ticks field.
  p_tle->ticks = timeout_sec;
  p_tle->in_use = TRUE;
  alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_general_alarm_cb, (void *)p_tle);
}
Esempio n. 15
0
static void reassemble_and_dispatch(BT_HDR *packet)
{
    LOG_DEBUG("reassemble_and_dispatch\n");

    if ((packet->event & MSG_EVT_MASK) == MSG_HC_TO_STACK_HCI_ACL) {
        uint8_t *stream = packet->data + packet->offset;
        uint16_t handle;
        uint16_t l2cap_length;
        uint16_t acl_length;

        STREAM_TO_UINT16(handle, stream);
        STREAM_TO_UINT16(acl_length, stream);
        STREAM_TO_UINT16(l2cap_length, stream);

        assert(acl_length == packet->len - HCI_ACL_PREAMBLE_SIZE);

        uint8_t boundary_flag = GET_BOUNDARY_FLAG(handle);
        handle = handle & HANDLE_MASK;

        BT_HDR *partial_packet = (BT_HDR *)hash_map_get(partial_packets, (void *)(uintptr_t)handle);

        if (boundary_flag == START_PACKET_BOUNDARY) {
            if (partial_packet) {
                LOG_WARN("%s found unfinished packet for handle with start packet. Dropping old.\n", __func__);
                hash_map_erase(partial_packets, (void *)(uintptr_t)handle);
                buffer_allocator->free(partial_packet);
            }

            uint16_t full_length = l2cap_length + L2CAP_HEADER_SIZE + HCI_ACL_PREAMBLE_SIZE;
            if (full_length <= packet->len) {
                if (full_length < packet->len) {
                    LOG_WARN("%s found l2cap full length %d less than the hci length %d.\n", __func__, l2cap_length, packet->len);
                }

                callbacks->reassembled(packet);
                return;
            }

            partial_packet = (BT_HDR *)buffer_allocator->alloc(full_length + sizeof(BT_HDR));
            partial_packet->event = packet->event;
            partial_packet->len = full_length;
            partial_packet->offset = packet->len;

            memcpy(partial_packet->data, packet->data + packet->offset, packet->len);

            // Update the ACL data size to indicate the full expected length
            stream = partial_packet->data;
            STREAM_SKIP_UINT16(stream); // skip the handle
            UINT16_TO_STREAM(stream, full_length - HCI_ACL_PREAMBLE_SIZE);

            hash_map_set(partial_packets, (void *)(uintptr_t)handle, partial_packet);
            // Free the old packet buffer, since we don't need it anymore
            buffer_allocator->free(packet);
        } else {
            if (!partial_packet) {
                LOG_ERROR("%s got continuation for unknown packet. Dropping it.\n", __func__);
                buffer_allocator->free(packet);
                return;
            }

            packet->offset += HCI_ACL_PREAMBLE_SIZE; // skip ACL preamble
            packet->len -= HCI_ACL_PREAMBLE_SIZE;
            uint16_t projected_offset = partial_packet->offset + packet->len;
            if (projected_offset > partial_packet->len) { // len stores the expected length
                LOG_ERROR("%s got packet which would exceed expected length of %d. Truncating.\n", __func__, partial_packet->len);
                packet->len = partial_packet->len - partial_packet->offset;
                projected_offset = partial_packet->len;
            }

            memcpy(
                partial_packet->data + partial_packet->offset,
                packet->data + packet->offset,
                packet->len
            );

            // Free the old packet buffer, since we don't need it anymore
            buffer_allocator->free(packet);
            partial_packet->offset = projected_offset;

            if (partial_packet->offset == partial_packet->len) {
                hash_map_erase(partial_packets, (void *)(uintptr_t)handle);
                partial_packet->offset = 0;
                callbacks->reassembled(partial_packet);
            }
        }
    } else {
        callbacks->reassembled(packet);
    }
}
Esempio n. 16
0
list* search_a_star(void* state,
                    void* state_world,
                    search_is_goal state_goal_func,
                    search_gen_successors state_gen_func,
                    search_link_parent state_link_func,
                    search_goal_backtrace state_back_func,
                    search_trans_cost state_trans_func,
                    search_heuristic state_heur_func,
                    search_set_f_cost state_f_cost_set_func,
                    hash_func state_hash_alg,
                    generic_comp state_comp_func,
                    generic_cpy state_copy_func,
                    generic_op state_free_func,
                    heap_comp state_heap_func) {
    int* g_cost_ptr, *f_cost_ptr, f_cost, tmp_f, g_cost, found;
    void* current_state, *successor_state, *heap_memory_location;
    list* states_overflow, *successor_list, *path;
    hash_table* states_closed_set, *states_open_set;
    hash_map* states_g_cost, *states_f_cost, *states_heap_index;
    heap* states_heap;

    states_overflow = list_create(NULL,
                                  NULL,
                                  state_free_func);

    states_closed_set = hash_table_create(89,
                                          .75,
                                          state_hash_alg,
                                          state_comp_func,
                                          state_copy_func,
                                          state_free_func);

    states_open_set = hash_table_create(89,
                                        .75,
                                        state_hash_alg,
                                        state_comp_func,
                                        state_copy_func,
                                        state_free_func);

    states_g_cost = hash_map_create(89,
                                    .75,
                                    state_hash_alg,
                                    state_comp_func,
                                    NULL,
                                    NULL,
                                    NULL,
                                    state_free_func,
                                    (generic_op)free);

    states_f_cost = hash_map_create(89,
                                    .75,
                                    state_hash_alg,
                                    state_comp_func,
                                    NULL,
                                    NULL,
                                    NULL,
                                    state_free_func,
                                    (generic_op)free);

    states_heap_index = hash_map_create(89,
                                        .75,
                                        state_hash_alg,
                                        state_comp_func,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL);

    states_heap = heap_create(89,
                              state_heap_func,
                              state_comp_func,
                              state_copy_func,
                              state_free_func);
    current_state = state;
    f_cost = state_heur_func(current_state, NULL);
    state_f_cost_set_func(current_state, f_cost);
    g_cost = 0;
    g_cost_ptr = malloc(sizeof(int));
    *g_cost_ptr = g_cost;
    f_cost_ptr = malloc(sizeof(int));
    *f_cost_ptr = f_cost;
    hash_map_insert(states_g_cost, current_state, g_cost_ptr, 0);
    heap_memory_location = heap_add(states_heap, state_copy_func(current_state));
    hash_table_insert(states_open_set, state_copy_func(current_state), 0);
    hash_map_insert(states_f_cost, state_copy_func(current_state), f_cost_ptr, 0);
    hash_map_insert(states_heap_index, current_state, heap_memory_location, 1);
    path = NULL;
    found = 0;
    while(!heap_is_empty(states_heap) && !found) {
        current_state = state_copy_func(heap_peek(states_heap));
        heap_remove(states_heap);
        hash_table_remove(states_open_set, current_state);
        hash_map_remove(states_heap_index, current_state);
        if(state_goal_func(current_state, state_world)) {
            path = state_back_func(current_state);
            found = 1;
        } else {
            if(!hash_table_insert(states_closed_set, current_state, 0)) {
                list_push_front(states_overflow, current_state);
            }
            successor_list = state_gen_func(current_state, state_world);
            while(!list_is_empty(successor_list)) {
                successor_state = list_front(successor_list);
                g_cost = *(int*)hash_map_get(states_g_cost, current_state) +
                    state_trans_func(current_state, successor_state, state_world);
                f_cost = g_cost + state_heur_func(successor_state, state_world);
                tmp_f = hash_map_contains_key(states_f_cost, successor_state) ?
                    *(int*)hash_map_get(states_f_cost, successor_state) : UINT_MAX;
                if(hash_table_contains(states_closed_set, successor_state) && f_cost > tmp_f) {
                    list_remove_front(successor_list);
                    continue;
                }
                if(!hash_table_contains(states_open_set, successor_state) || f_cost < tmp_f) {
                    state_f_cost_set_func(successor_state, f_cost);
                    state_link_func(successor_state, current_state);
                    g_cost_ptr = malloc(sizeof(int));
                    f_cost_ptr = malloc(sizeof(int));
                    *g_cost_ptr = g_cost;
                    *f_cost_ptr = f_cost;
                    if(!hash_table_contains(states_open_set, successor_state)) {
                        hash_table_insert(states_open_set, successor_state, 0);
                        heap_memory_location = heap_add(states_heap, state_copy_func(successor_state));
                        hash_map_insert(states_heap_index, successor_state,  heap_memory_location, 1);
                    } else {
                        heap_memory_location = hash_map_get(states_heap_index, successor_state);
                        heap_up_mod_data(states_heap, heap_memory_location,  successor_state);
                    }
                    if(!hash_map_set(states_g_cost, successor_state, g_cost_ptr)) {
                        hash_map_insert(states_g_cost, state_copy_func(successor_state), g_cost_ptr, 0);
                    }
                    if(!hash_map_set(states_f_cost, successor_state, f_cost_ptr)) {
                        hash_map_insert(states_f_cost, state_copy_func(successor_state), f_cost_ptr, 0);
                    }
                    list_pop(successor_list);
                } else {
                    list_remove_front(successor_list);
                }
            }
            list_kill(successor_list);
        }
    }
    heap_kill(states_heap);
    list_kill(states_overflow);
    hash_map_kill(states_g_cost);
    hash_map_kill(states_f_cost);
    hash_table_kill(states_open_set);
    hash_table_kill(states_closed_set);
    hash_map_dissolve(states_heap_index);
    return path;
}