static bool encode_read_by_grp_type_rsp(struct gatt_db *db, struct queue *q, struct bt_att *att, uint16_t mtu, uint8_t *pdu, uint16_t *len) { int iter = 0; uint16_t start_handle, end_handle; struct iovec value; uint8_t data_val_len; *len = 0; while (queue_peek_head(q)) { struct gatt_db_attribute *attrib = queue_pop_head(q); value.iov_base = NULL; value.iov_len = 0; /* * This should never be deferred to the read callback for * primary/secondary service declarations. */ if (!gatt_db_attribute_read(attrib, 0, BT_ATT_OP_READ_BY_GRP_TYPE_REQ, att, attribute_read_cb, &value) || !value.iov_len) return false; /* * Use the first attribute to determine the length of each * attribute data unit. Stop the list when a different attribute * value is seen. */ if (iter == 0) { data_val_len = MIN(MIN((unsigned)mtu - 6, 251), value.iov_len); pdu[0] = data_val_len + 4; iter++; } else if (value.iov_len != data_val_len) break; /* Stop if this unit would surpass the MTU */ if (iter + data_val_len + 4 > mtu - 1) break; gatt_db_attribute_get_service_handles(attrib, &start_handle, &end_handle); put_le16(start_handle, pdu + iter); put_le16(end_handle, pdu + iter + 2); memcpy(pdu + iter + 4, value.iov_base, data_val_len); iter += data_val_len + 4; } *len = iter; return true; }
static void remove_default_agent(struct agent *agent) { if (queue_peek_head(default_agents) != agent) { queue_remove(default_agents, agent); return; } queue_remove(default_agents, agent); agent = queue_peek_head(default_agents); if (agent) DBG("Default agent set to %s %s", agent->owner, agent->path); else DBG("Default agent cleared"); adapter_foreach(set_io_cap, agent); }
static void hf_call_prefix_handler(struct hfp_hf *hfp, const char *data) { struct event_handler *handler; const char *separators = ";:\0"; struct hfp_context context; enum hfp_result result; enum hfp_error cme_err; char lookup_prefix[18]; uint8_t pref_len = 0; const char *prefix; int i; context.offset = 0; context.data = data; hf_skip_whitespace(&context); if (strlen(data + context.offset) < 2) return; prefix = data + context.offset; pref_len = strcspn(prefix, separators); if (pref_len > 17 || pref_len < 2) return; for (i = 0; i < pref_len; i++) lookup_prefix[i] = toupper(prefix[i]); lookup_prefix[pref_len] = '\0'; context.offset += pref_len + 1; if (is_response(lookup_prefix, &result, &cme_err, &context)) { struct cmd_response *cmd; cmd = queue_peek_head(hfp->cmd_queue); if (!cmd) return; cmd->resp_cb(result, cme_err, cmd->user_data); queue_remove(hfp->cmd_queue, cmd); free(cmd); hf_wakeup_writer(hfp); return; } handler = queue_find(hfp->event_handlers, match_handler_event_prefix, lookup_prefix); if (!handler) return; handler->callback(&context, handler->user_data); }
static bool encode_find_info_rsp(struct gatt_db *db, struct queue *q, uint16_t mtu, uint8_t *pdu, uint16_t *len) { uint16_t handle; struct gatt_db_attribute *attr; const bt_uuid_t *type; int uuid_len, cur_uuid_len; int iter = 0; *len = 0; while (queue_peek_head(q)) { attr = queue_pop_head(q); handle = gatt_db_attribute_get_handle(attr); type = gatt_db_attribute_get_type(attr); if (!handle || !type) return false; cur_uuid_len = bt_uuid_len(type); if (iter == 0) { switch (cur_uuid_len) { case 2: uuid_len = 2; pdu[0] = 0x01; break; case 4: case 16: uuid_len = 16; pdu[0] = 0x02; break; default: return false; } iter++; } else if (cur_uuid_len != uuid_len) break; if (iter + uuid_len + 2 > mtu - 1) break; put_le16(handle, pdu + iter); bt_uuid_to_le(type, pdu + iter + 2); iter += uuid_len + 2; } *len = iter; return true; }
static void map_client_get_instances_action(void) { struct test_data *data = tester_get_data(); struct step *current_data_step = queue_peek_head(data->steps); bt_bdaddr_t *bd_addr = current_data_step->set_data; struct step *step = g_new0(struct step, 1); step->action_status = data->if_map_client->get_remote_mas_instances(bd_addr); schedule_action_verification(step); }
void test_queue_peek_head(void) { Queue *queue; /* Check peeking into an empty queue */ queue = queue_new(); assert(queue_peek_head(queue) == NULL); queue_free(queue); /* Pop off all the values from the queue, making sure that peek * has the correct value beforehand */ queue = generate_queue(); while (!queue_is_empty(queue)) { assert(queue_peek_head(queue) == &variable4); assert(queue_pop_head(queue) == &variable4); assert(queue_peek_head(queue) == &variable3); assert(queue_pop_head(queue) == &variable3); assert(queue_peek_head(queue) == &variable2); assert(queue_pop_head(queue) == &variable2); assert(queue_peek_head(queue) == &variable1); assert(queue_pop_head(queue) == &variable1); } assert(queue_peek_head(queue) == NULL); queue_free(queue); }
struct agent *agent_get(const char *owner) { struct agent *agent; if (owner) { agent = g_hash_table_lookup(agent_list, owner); if (agent) return agent_ref(agent); } if (!queue_isempty(default_agents)) return agent_ref(queue_peek_head(default_agents)); return NULL; }
static bool add_default_agent(struct agent *agent) { if (queue_peek_head(default_agents) == agent) return true; queue_remove(default_agents, agent); if (!queue_push_head(default_agents, agent)) return false; DBG("Default agent set to %s %s", agent->owner, agent->path); adapter_foreach(set_io_cap, agent); return true; }
/** * Build a simple submission string for only one item */ static gushort build_querystring_single(gchar **qs) { gchar *sig, *tmp; queue_node *song = queue_peek_head(); tmp = g_strdup_printf("album%sapi_key" API_KEY "artist%sduration%d" "methodtrack.scrobblesk%stimestamp%" G_GINT64_FORMAT "track%s" "tracknumber%d" API_SECRET, song->album, song->artist, song->length, as_conn.session_id, song->date, song->title, song->track); sig = g_compute_checksum_for_string(G_CHECKSUM_MD5, tmp, -1); g_free(tmp); *qs = g_strdup_printf("api_key=" API_KEY "&method=track.scrobble&sk=%s" "&album=%s&artist=%s&duration=%d×tamp=%" G_GINT64_FORMAT "&track=%s" "&tracknumber=%d&api_sig=%s", as_conn.session_id, song->album, song->artist, song->length, song->date, song->title, song->track, sig); g_free(sig); return 1; }
/** * Build a more complex string using array notation for up to 10 songs */ static gushort build_querystring_multi(gchar **qs) { gchar *sig, *tmp; GString *nqs; GString *albums, *artists, *lengths, *timestamps, *titles; GString *tracks; gushort num = 0; queue_node *song = queue_peek_head(); nqs = g_string_new("api_key=" API_KEY "&method=track.scrobble&sk="); g_string_append(nqs, as_conn.session_id); albums = g_string_new(""); artists = g_string_new(""); lengths = g_string_new(""); timestamps = g_string_new(""); titles = g_string_new(""); tracks = g_string_new(""); while (song && num < 10) { gchar *album, *artist, *title; g_string_append_printf(albums, "album[%d]%s", num, song->album); g_string_append_printf(artists, "artist[%d]%s", num, song->artist); g_string_append_printf(lengths, "duration[%d]%d", num, song->length); g_string_append_printf(timestamps, "timestamp[%d]%" G_GINT64_FORMAT, num, song->date); g_string_append_printf(titles, "track[%d]%s", num, song->title); g_string_append_printf(tracks, "trackNumber[%d]%d", num, song->track); album = curl_easy_escape(as_conn.handle, song->album, 0); artist = curl_easy_escape(as_conn.handle, song->artist, 0); title = curl_easy_escape(as_conn.handle, song->title, 0); g_string_append_printf(nqs, "&album[%1$d]=%2$s" "&artist[%1$d]=%3$s&duration[%1$d]=%4$d" "×tamp[%1$d]=%5$" G_GINT64_FORMAT "&track[%1$d]=%6$s" "&trackNumber[%1$d]=%7$d", num, album, artist, song->length, song->date, title, song->track); curl_free(album); curl_free(artist); curl_free(title); num++; song = queue_peek_nth(num); } tmp = g_strdup_printf("%sapi_key" API_KEY "%s%smethodtrack.scrobble" "sk%s%s%s%s" API_SECRET, albums->str, artists->str, lengths->str, as_conn.session_id, timestamps->str, tracks->str, titles->str); sig = g_compute_checksum_for_string(G_CHECKSUM_MD5, tmp, -1); g_free(tmp); g_string_free(albums, TRUE); g_string_free(artists, TRUE); g_string_free(lengths, TRUE); g_string_free(timestamps, TRUE); g_string_free(titles, TRUE); g_string_free(tracks, TRUE); g_string_append_printf(nqs, "&api_sig=%s", sig); g_free(sig); *qs = g_string_free(nqs, FALSE); return num; }
int newest_entry_moving_average(Moving_Average *self) { return *((int *)queue_peek_head(self->data)); }
static void end_element( void *ud, const xmlChar *xname) { char *name = (char*)xname; parse_data_t *pd = (parse_data_t*)ud; int id; union { int id; void * pid; } start_id; int ii; // Check to see if the first element found has been closed // If so, ignore any junk following it. if (pd->closed_top) return; for (ii = 0; ii < TAG_MAP_SZ; ii++) { if (strcmp(name, tag_map[ii].tag) == 0) { id = tag_map[ii].id; break; } } if (ii == TAG_MAP_SZ) { hb_error("Unrecognized start tag (%s)", name); return; } start_id.pid = queue_pop_head(pd->tag_stack); if (start_id.id != id) hb_error("start tag != end tag: (%s %d) %d", name, id, id); hb_value_t *gval = NULL; hb_value_t *current = queue_peek_head(pd->stack); hb_value_type_t gtype = 0; const char *value; if (pd->value != NULL) value = pd->value; else value = ""; switch (id) { case P_PLIST: { // Ignore } break; case P_KEY: { if (pd->key) free(pd->key); pd->key = strdup(value); return; } break; case P_DICT: { queue_pop_head(pd->stack); } break; case P_ARRAY: { queue_pop_head(pd->stack); } break; case P_INTEGER: { uint64_t val = strtoll(value, NULL, 0); gval = hb_value_int(val); } break; case P_REAL: { double val = strtod(value, NULL); gval = hb_value_double(val); } break; case P_STRING: { gval = hb_value_string(value); } break; case P_TRUE: { gval = hb_value_bool(1); } break; case P_FALSE: { gval = hb_value_bool(0); } break; default: { hb_error("Unhandled plist type %d", id); } break; } if (gval) { // Get the top of the data structure stack and if it's an array // or dict, add the current element if (current == NULL) { pd->plist = gval; pd->closed_top = 1; return; } gtype = hb_value_type(current); if (gtype == HB_VALUE_TYPE_ARRAY) { hb_value_array_append(current, gval); } else if (gtype == HB_VALUE_TYPE_DICT) { if (pd->key == NULL) { hb_error("No key for dictionary item"); hb_value_free(&gval); } else { hb_dict_set(current, pd->key, gval); } } else { hb_error("Invalid container type. This shouldn't happen"); } } if (queue_is_empty(pd->stack)) pd->closed_top = 1; }
static void start_element( void *ud, const xmlChar *xname, const xmlChar **attr_names) { char *name = (char*)xname; parse_data_t *pd = (parse_data_t*)ud; union { int id; void * pid; } id; int ii; // Check to see if the first element found has been closed // If so, ignore any junk following it. if (pd->closed_top) return; for (ii = 0; ii < TAG_MAP_SZ; ii++) { if (strcmp(name, tag_map[ii].tag) == 0) { id.id = tag_map[ii].id; break; } } if (ii == TAG_MAP_SZ) { hb_error("Unrecognized start tag (%s)", name); return; } if (pd->value) { free(pd->value); pd->value = NULL; } queue_push_head(pd->tag_stack, id.pid); hb_value_type_t gtype = 0; hb_value_t *gval = NULL; hb_value_t *current = queue_peek_head(pd->stack); switch (id.id) { case P_PLIST: { // Ignore } break; case P_KEY: { if (pd->key) free(pd->key); pd->key = NULL; } break; case P_DICT: { gval = hb_dict_init(); queue_push_head(pd->stack, gval); } break; case P_ARRAY: { gval = hb_value_array_init(); queue_push_head(pd->stack, gval); } break; case P_INTEGER: { } break; case P_REAL: { } break; case P_STRING: { } break; case P_DATE: { } break; case P_TRUE: { } break; case P_FALSE: { } break; case P_DATA: { } break; } // Add the element to the current container if (gval) { // There's an element to add if (current == NULL) { pd->plist = gval; return; } gtype = hb_value_type(current); if (gtype == HB_VALUE_TYPE_ARRAY) { hb_value_array_append(current, gval); } else if (gtype == HB_VALUE_TYPE_DICT) { if (pd->key == NULL) { hb_error("No key for dictionary item"); hb_value_free(&gval); } else { hb_dict_set(current, pd->key, gval); } } else { hb_error("Invalid container type. This shouldn't happen"); } } }