📄 sdp_server.c
字号:
if (is_err()) { D_MEM("<--- free%d 0x%8p", --malloc_dbg, tmp_hdl_list); free(tmp_hdl_list); return NULL; } if (tmp_hdl_list) { j = 0; while (tmp_hdl_list[j] != NO_REC_HDL) { rec_hdl_list[rec_hdl_cnt] = tmp_hdl_list[j]; D_REC("Found record handle : 0x%08x", rec_hdl_list[rec_hdl_cnt]); rec_hdl_cnt++; j++; } D_RHDL("Found %d record handles using get_more_rec_hdl\n", j); D_MEM("<--- free%d 0x%8p", --malloc_dbg, tmp_hdl_list); free(tmp_hdl_list); } } D_RHDL("Now __remove__ duplicated entries ?!?!?\n"); tmp_hdl_list = remove_duplicated_rec_hdl(rec_hdl_list, rec_hdl_cnt); return tmp_hdl_list;}/* * Searches for "ServiceClasses" entries in database */unsigned intget_record_handle(unsigned short service_class, int fd){ int record_handle = NO_REC_HDL; char *service_class_name = NULL; char *service_class_id = NULL; char tmp[12]; D_RHDL("Looking for service class %0x\n", service_class); /* Before we can get the recordhandle we have to find the name of the service class, so we can search for it in the database. When we search the service class id, have to be converted to characters. All service classes are stored as 16 bits UUIDs*/ /* Search for UUID as a string among ServiceClasses TAG */ sprintf(tmp, "0x%04x", service_class); service_class_name = get_from_xml(fd, "ServiceClasses", NULL, tmp); /* If we didn't find the service class we return here */ if (service_class_name == NULL) { D_RHDL("Didn't find ServiceClass name for uuid 0x%04x", service_class); return NO_REC_HDL; } D_RHDL("Found service class name %s\n", service_class_name); /* We found service class name, now search for record handle */ service_class_id = get_from_xml(fd, service_class_name, "ServiceRecordHandle", NULL); D_MEM("<--- free%d 0x%8p", --malloc_dbg, service_class_name); free(service_class_name); /* If we found the record handle we convert it to an unsigned int */ if (service_class_id != NULL) { D_RHDL("Found service_class_id : 0x%x\n", service_class_id); record_handle = strtoul(service_class_id, NULL, 16); D_RHDL("Service class id conv to rec hdl 0x%08x", record_handle); D_MEM("<--- free%d 0x%8p", --malloc_dbg, service_class_id); free(service_class_id); } return record_handle;}voidget_more_rec_hdl_start(void *data, const char *el, const char **attr){ unsigned int *tmp; rec_hdl_search_struct *s_hdl = (rec_hdl_search_struct*) data; if (attr[0] && strcmp(attr[0], "ServiceRecordHandle") == 0) { S_FNC("Found Record Handle %s", attr[1]); s_hdl->tmp_hdl = strtoul(attr[1], NULL, 16); S_FNC("Found Record Handle converted to int 0x%08x", s_hdl->tmp_hdl); } if ((s_hdl->tmp_hdl != NO_REC_HDL) && (strcmp(el, s_hdl->uuid) == 0)) { if ((s_hdl->hdl_list_len > 0) && (s_hdl->tmp_hdl == s_hdl->hdl_list[s_hdl->hdl_list_len - 1])) { S_FNC("%s is present in record handle 0x%08x, but already found", s_hdl->uuid, s_hdl->tmp_hdl); } else { S_FNC("%s is present in record handle 0x%08x", s_hdl->uuid, s_hdl->tmp_hdl); /* Store the poiter to the allready found record handles, and then if necessary allocate new space for both the old and the new record handles */ if (s_hdl->hdl_list_len >= s_hdl->hdl_list_max) { s_hdl->hdl_list_max += 16; tmp = realloc(s_hdl->hdl_list, s_hdl->hdl_list_max * sizeof(*s_hdl->hdl_list)); D_MEM("---> realloc%d %ld bytes at 0x%8p", malloc_dbg++, s_hdl->hdl_list_max * sizeof(*s_hdl->hdl_list), tmp); if (!tmp) { set_err(SDP_INSUFFICIENT_RESOURCES); s_hdl->hdl_list_max -= 16; return; } s_hdl->hdl_list = tmp; } s_hdl->hdl_list[s_hdl->hdl_list_len] = s_hdl->tmp_hdl; s_hdl->hdl_list_len++; } }}voidget_more_rec_hdl_end(void *data, const char *el){}/* * Searches for Attributes in database */unsigned int*get_more_rec_hdl(unsigned short service_class, int fd){ rec_hdl_search_struct s_hdl; char tmp_ch[12]; unsigned int *tmp; XML_Parser p; sprintf(tmp_ch, "0x%04x", service_class); /* Search for UUID among "Protocols" TAG */ s_hdl.uuid = get_from_xml(fd, "Protocols", NULL, tmp_ch); /* If we didn't find the service class among the protocols we look for it among the ServiceClasses */ if (s_hdl.uuid == NULL) { D_RHDL("Didn't find Protocol name for uuid 0x%04x among the protocols", service_class); s_hdl.uuid = get_from_xml(fd, "ServiceClasses", NULL, tmp_ch); /* If we still didn't find the sevice class, we return NULL */ if (s_hdl.uuid == NULL) { D_RHDL("Didn't find Protocol name for uuid 0x%04x", service_class); return NULL; } } s_hdl.hdl_list_len = 0; s_hdl.hdl_list_max = 16; s_hdl.hdl_list = malloc(s_hdl.hdl_list_max * sizeof(*s_hdl.hdl_list)); D_MEM("---> malloc%d %ld bytes at 0x%8p", malloc_dbg++, s_hdl.hdl_list_max * sizeof(*s_hdl.hdl_list), s_hdl.hdl_list); if (!s_hdl.hdl_list) { set_err(SDP_INSUFFICIENT_RESOURCES); D_MEM("<--- free%d 0x%8p", --malloc_dbg, s_hdl.uuid); free(s_hdl.uuid); return NULL; } p = XML_ParserCreate(NULL); XML_SetElementHandler(p, get_more_rec_hdl_start, get_more_rec_hdl_end); XML_SetUserData(p, (void*)&s_hdl); start_xml_parser(p, fd); XML_ParserFree(p); D_MEM("<--- free%d 0x%8p", --malloc_dbg, s_hdl.uuid); free(s_hdl.uuid); if (is_err()) { D_MEM("<--- free%d 0x%8p", --malloc_dbg, s_hdl.hdl_list); free(s_hdl.hdl_list); return NULL; } if (s_hdl.hdl_list_len >= s_hdl.hdl_list_max) { s_hdl.hdl_list_max++; tmp = realloc(s_hdl.hdl_list, s_hdl.hdl_list_max * sizeof(*s_hdl.hdl_list)); D_MEM("---> realloc%d %ld bytes at 0x%8p", malloc_dbg++, s_hdl.hdl_list_max * sizeof(*s_hdl.hdl_list), tmp); if (!tmp) { set_err(SDP_INSUFFICIENT_RESOURCES); D_MEM("<--- free%d 0x%8p", --malloc_dbg, s_hdl.hdl_list); free(s_hdl.hdl_list); return NULL; } s_hdl.hdl_list = tmp; } s_hdl.hdl_list[s_hdl.hdl_list_len] = NO_REC_HDL; return s_hdl.hdl_list;}char*get_attribute_range(int fd, unsigned int record_handle, unsigned int attr_id_code){ int *attr_lst, i = 1, pos = 0; char *tmp_ptr; char *return_sequence; /* FIXME: But for now 256 bytes will do */ return_sequence = malloc(256); D_MEM("---> malloc%d %d bytes at 0x%8p", malloc_dbg++, 256, return_sequence); if (!return_sequence) { set_err(SDP_INSUFFICIENT_RESOURCES); return NULL; } D_ATTR("A range of attributes was requested 0x%04x - 0x%04x", (attr_id_code >> 16), (attr_id_code & 0xffff)); /* Lists all attributes registerd in the database */ attr_lst = get_all_attributes(fd); if (!attr_lst) { D_MEM("<--- free%d 0x%8p", --malloc_dbg, return_sequence); free(return_sequence); return NULL; } return_sequence[pos++] = DES_HDR; return_sequence[pos++] = 0; /* Find the first attribute in the range */ while ((i < attr_lst[0]) && (attr_lst[i] < (attr_id_code >> 16))) { i++; } D_ATTR("attr_lst[0]: %d, attr_id_code: 0x%04x", attr_lst[0], attr_id_code); while ((i < attr_lst[0]) && (attr_lst[i] <= (attr_id_code & 0xffff))) { D_REC("attr_lst[%d]: 0x%04x", i, attr_lst[i]); /* we mask the attribute her so we don't by misstake send a range as input */ tmp_ptr = get_attribute_list(fd, record_handle, attr_lst[i] & 0xffff); if (tmp_ptr) { memcpy(return_sequence + pos, tmp_ptr + 2, tmp_ptr[1]); pos += tmp_ptr[1]; D_MEM("<--- free%d 0x%8p", --malloc_dbg, tmp_ptr); free(tmp_ptr); } else if (is_err()) { D_MEM("<--- free%d 0x%8p", --malloc_dbg, return_sequence); free(return_sequence); return NULL; } i++; } return_sequence[1] = pos - 2; D_MEM("<--- free%d 0x%8p", --malloc_dbg, attr_lst); free(attr_lst); PRINT_DATA(__FUNCTION__ ": return_sequence", return_sequence, return_sequence[1] + 2); if (return_sequence[1] == 0) { D_MEM("<--- free%d 0x%8p", --malloc_dbg, return_sequence); free(return_sequence); return NULL; } else { return return_sequence; }}void init_attribute_search_struct(sdp_attribute_search *search_struct){ int i; for (i = 0; i < 2; i++) { search_struct->des_len_pos[i] = 0; search_struct->des_len[i] = 0; search_struct->set_des_len[i] = 0; } search_struct->service_class_found = 0; search_struct->attribute_name_found = 0; search_struct->attr2get = 0; search_struct->attrlist_index = 0;}char*get_attribute_list(int fd, unsigned int record_handle, unsigned short attr_id_code){#define SEQ_LEN 256 XML_Parser p; char *return_sequence = NULL; char *char_tmp = NULL; char tmp[12]; int len; sdp_attribute_search search_struct; init_attribute_search_struct(&search_struct); D_ATTR("Searching for attribute 0x%04x for record handle 0x%08x", attr_id_code, record_handle); search_struct.attribute_id = attr_id_code; sprintf(tmp, "0x%04x", attr_id_code); search_struct.attribute_name = get_from_xml(fd, "AttributeIdentifierCodes", NULL, tmp); if (search_struct.attribute_name == NULL) { fprintf(stderr, __FUNCTION__ ": Didn't find service attribute id name for uuid 0x%04x\n", attr_id_code); return NULL; } D_ATTR("Found %s", search_struct.attribute_name); sprintf(tmp, "0x%08x", record_handle); search_struct.service_class = get_from_xml(fd, NULL, "ServiceRecordHandle", tmp); if (search_struct.service_class == NULL) { fprintf(stderr, __FUNCTION__ ": Didn't find service class name for RecordHandle 0x%08x\n", record_handle); set_err(SDP_INVALID_SERVICE_RECORD_HANDLE); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.attribute_name); free(search_struct.attribute_name); return NULL; } D_ATTR("Found %s", search_struct.service_class); p = XML_ParserCreate(NULL); XML_SetElementHandler(p, get_attribute_list_start, get_attribute_list_end); XML_SetUserData(p, (void*)&search_struct); XML_SetCharacterDataHandler(p, get_attribute_char_data); start_xml_parser(p, fd); XML_ParserFree(p); if (is_err()) { D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.attribute_name); free(search_struct.attribute_name); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.service_class); free(search_struct.service_class); return NULL; } if (search_struct.attrlist_index == 0) { D_ATTR("Didn't find the attribute values for the attribute %s", search_struct.attribute_name); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.attribute_name); free(search_struct.attribute_name); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.service_class); free(search_struct.service_class); return NULL; } char_tmp = get_values(search_struct.attrlist, search_struct.attrlist_index, fd); len = strlen(char_tmp) / 2; return_sequence = malloc(len + 2); D_MEM("---> malloc%d %d bytes at 0x%8p", malloc_dbg++, len + 2, return_sequence); if (!return_sequence) { set_err(SDP_INSUFFICIENT_RESOURCES); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.attribute_name); free(search_struct.attribute_name); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.service_class); free(search_struct.service_class); return NULL; } return_sequence[0] = 0x35; return_sequence[1] = len; char2hex(char_tmp, return_sequence + 2); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.attribute_name); free(search_struct.attribute_name); D_MEM("<--- free%d 0x%8p", --malloc_dbg, search_struct.service_class); free(search_struct.service_class); PRINT_DATA(__FUNCTION__ ": return value", return_sequence, len + 2); return return_sequence;}voidget_attribute_list_start(void *data, const char *el, const char **attr){ int i; sdp_attribute_search *search_struct; search_struct = (sdp_attribute_search*) data; /* If we find the service class we are looking for set the service_class_found parameter to TRUE */ if (strcmp(el, search_struct->service_class) == 0) { S_FNC("Found service class %s", el); search_struct->service_class_found++; } if (search_struct->service_class_found) { /* If we find the attribute name, we set the attribute_name_found parameter to TRUE */ if (strcmp(el, search_struct->attribute_name) == 0) { search_struct->attribute_name_found = 1; S_FNC("Found attribute name %s", el); /* We then inserts the attribute id code of the attribute type we have found */ sprintf(search_struct->attrlist, "0x09%04x", search_struct->attribute_id); search_struct->attrlist_index = strlen(search_struct->attrlist) + 1; /* Then we browses through the attributes */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -