⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdptool.c

📁 这是Linux环境下的蓝牙源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
		if (member_name) {			printf("%.*s%s (Integer) : 0x%x\n",				indent, indent_spaces, member_name, sdpdata->val.uint32);		} else {			printf("%.*sInteger : 0x%x\n", indent, indent_spaces,				sdpdata->val.uint32);		}		break;	case SDP_UUID16:	case SDP_UUID32:	case SDP_UUID128:		//printf("%.*sUUID\n", indent, indent_spaces);		sdp_uuid_printf(&sdpdata->val.uuid, context, indent);		break;	case SDP_TEXT_STR8:	case SDP_TEXT_STR16:	case SDP_TEXT_STR32:		if (sdpdata->unitSize > strlen(sdpdata->val.str)) {			int i;			printf("%.*sData :", indent, indent_spaces);			for (i = 0; i < sdpdata->unitSize; i++)				printf(" %02x", (unsigned char) sdpdata->val.str[i]);			printf("\n");		} else			printf("%.*sText : \"%s\"\n", indent, indent_spaces, sdpdata->val.str);		break;	case SDP_URL_STR8:	case SDP_URL_STR16:	case SDP_URL_STR32:		printf("%.*sURL : %s\n", indent, indent_spaces, sdpdata->val.str);		break;	case SDP_SEQ8:	case SDP_SEQ16:	case SDP_SEQ32:		printf("%.*sData Sequence\n", indent, indent_spaces);		printf_dataseq(sdpdata->val.dataseq, context, indent);		break;	case SDP_ALT8:	case SDP_ALT16:	case SDP_ALT32:		printf("%.*sData Sequence Alternates\n", indent, indent_spaces);		printf_dataseq(sdpdata->val.dataseq, context, indent);		break;	}}/* * Parse a single attribute. */static void print_tree_attr_func(void *value, void *userData){	sdp_data_t *sdpdata = NULL;	uint16_t attrId;	struct service_context *service = (struct service_context *) userData;	struct attrib_context context;	struct attrib_def *attrDef = NULL;	int i;	sdpdata = (sdp_data_t *)value;	attrId = sdpdata->attrId;	/* Search amongst the generic attributes */	for (i = 0; i < attrib_max; i++)		if (attrib_names[i].num == attrId) {			attrDef = &attrib_names[i];			break;		}	/* Search amongst the specific attributes of this service */	if ((attrDef == NULL) && (service->service != NULL) &&				(service->service->attribs != NULL)) {		struct attrib_def *svc_attribs = service->service->attribs;		int		svc_attrib_max = service->service->attrib_max;		for (i = 0; i < svc_attrib_max; i++)			if (svc_attribs[i].num == attrId) {				attrDef = &svc_attribs[i];				break;			}	}	if (attrDef)		printf("Attribute Identifier : 0x%x - %s\n", attrId, attrDef->name);	else		printf("Attribute Identifier : 0x%x\n", attrId);	/* Build context */	context.service = service->service;	context.attrib = attrDef;	context.member_index = 0;	/* Parse attribute members */	if (sdpdata)		sdp_data_printf(sdpdata, &context, 2);	else		printf("  NULL value\n");	/* Update service */	service->service = context.service;}/* * Main entry point of this library. Parse a SDP record. * We assume the record has already been read, parsed and cached * locally. Jean II */static void print_tree_attr(sdp_record_t *rec){	if (rec && rec->attrlist) {		struct service_context service = { NULL };		sdp_list_foreach(rec->attrlist, print_tree_attr_func, &service);	}}static void print_raw_data(sdp_data_t *data, int indent){	struct uuid_def *def;	int i, hex;	if (!data)		return;	for (i = 0; i < indent; i++)		printf("\t");	switch (data->dtd) {	case SDP_DATA_NIL:		printf("NIL\n");		break;	case SDP_BOOL:		printf("Bool %s\n", data->val.uint8 ? "True" : "False");		break;	case SDP_UINT8:		printf("UINT8 0x%02x\n", data->val.uint8);		break;	case SDP_UINT16:		printf("UINT16 0x%04x\n", data->val.uint16);		break;	case SDP_UINT32:		printf("UINT32 0x%08x\n", data->val.uint32);		break;	case SDP_UINT64:		printf("UINT64 0x%016jx\n", data->val.uint64);		break;	case SDP_UINT128:		printf("UINT128 ...\n");		break;	case SDP_INT8:		printf("INT8 %d\n", data->val.int8);		break;	case SDP_INT16:		printf("INT16 %d\n", data->val.int16);		break;	case SDP_INT32:		printf("INT32 %d\n", data->val.int32);		break;	case SDP_INT64:		printf("INT64 %jd\n", data->val.int64);		break;	case SDP_INT128:		printf("INT128 ...\n");		break;	case SDP_UUID16:	case SDP_UUID32:	case SDP_UUID128:		switch (data->val.uuid.type) {		case SDP_UUID16:			def = NULL;			for (i = 0; i < uuid16_max; i++)				if (uuid16_names[i].num == data->val.uuid.value.uuid16) {					def = &uuid16_names[i];					break;				}			if (def)				printf("UUID16 0x%04x - %s\n", data->val.uuid.value.uuid16, def->name);			else				printf("UUID16 0x%04x\n", data->val.uuid.value.uuid16);			break;		case SDP_UUID32:			def = NULL;			if (!(data->val.uuid.value.uuid32 & 0xffff0000)) {				uint16_t value = data->val.uuid.value.uuid32;				for (i = 0; i < uuid16_max; i++)					if (uuid16_names[i].num == value) {						def = &uuid16_names[i];						break;					}			}			if (def)				printf("UUID32 0x%08x - %s\n", data->val.uuid.value.uuid32, def->name);			else				printf("UUID32 0x%08x\n", data->val.uuid.value.uuid32);			break;		case SDP_UUID128:			printf("UUID128 ");			for (i = 0; i < 16; i++) {				switch (i) {				case 4:				case 6:				case 8:				case 10:					printf("-");					break;				}				printf("%02x", (unsigned char ) data->val.uuid.value.uuid128.data[i]);			}			printf("\n");			break;		default:			printf("UUID type 0x%02x\n", data->val.uuid.type);			break;		}		break;	case SDP_TEXT_STR8:	case SDP_TEXT_STR16:	case SDP_TEXT_STR32:		hex = 0;		for (i = 0; i < data->unitSize; i++) {			if (i == (data->unitSize - 1) && data->val.str[i] == '\0')				break;			if (!isprint(data->val.str[i])) {				hex = 1;				break;			}		}		if (hex) {			printf("Data");			for (i = 0; i < data->unitSize; i++)				printf(" %02x", (unsigned char) data->val.str[i]);		} else {			printf("String ");			for (i = 0; i < data->unitSize; i++)				printf("%c", data->val.str[i]);		}		printf("\n");		break;	case SDP_URL_STR8:	case SDP_URL_STR16:	case SDP_URL_STR32:		printf("URL %s\n", data->val.str);		break;	case SDP_SEQ8:	case SDP_SEQ16:	case SDP_SEQ32:		printf("Sequence\n");		print_raw_data(data->val.dataseq, indent + 1);		break;	case SDP_ALT8:	case SDP_ALT16:	case SDP_ALT32:		printf("Alternate\n");		print_raw_data(data->val.dataseq, indent + 1);		break;	default:		printf("Unknown type 0x%02x\n", data->dtd);		break;	}	print_raw_data(data->next, indent);}static void print_raw_attr_func(void *value, void *userData){	sdp_data_t *data = (sdp_data_t *) value;	struct attrib_def *def = NULL;	int i;	/* Search amongst the generic attributes */	for (i = 0; i < attrib_max; i++)		if (attrib_names[i].num == data->attrId) {			def = &attrib_names[i];			break;		}	if (def)		printf("\tAttribute 0x%04x - %s\n", data->attrId, def->name);	else		printf("\tAttribute 0x%04x\n", data->attrId);	if (data)		print_raw_data(data, 2);	else		printf("  NULL value\n");}static void print_raw_attr(sdp_record_t *rec){	if (rec && rec->attrlist) {		printf("Sequence\n");		sdp_list_foreach(rec->attrlist, print_raw_attr_func, 0);	}}/* * Set attributes with single values in SDP record * Jean II */static int set_attrib(sdp_session_t *sess, uint32_t handle, uint16_t attrib, char *value) {	sdp_list_t *attrid_list;	uint32_t range = 0x0000ffff;	sdp_record_t *rec;	int ret;	/* Get the old SDP record */	attrid_list = sdp_list_append(NULL, &range);	rec = sdp_service_attr_req(sess, handle, SDP_ATTR_REQ_RANGE, attrid_list);	sdp_list_free(attrid_list, NULL);	if (!rec) {		printf("Service get request failed.\n");		return -1;	}	/* Check the type of attribute */	if (!strncasecmp(value, "u0x", 3)) {		/* UUID16 */		uint16_t value_int = 0;		uuid_t value_uuid;		value_int = strtoul(value + 3, NULL, 16);		sdp_uuid16_create(&value_uuid, value_int);		printf("Adding attrib 0x%X uuid16 0x%X to record 0x%X\n",			attrib, value_int, handle);		sdp_attr_add_new(rec, attrib, SDP_UUID16, &value_uuid.value.uuid16);	} else if (!strncasecmp(value, "0x", 2)) {		/* Int */		uint32_t value_int;  		value_int = strtoul(value + 2, NULL, 16);		printf("Adding attrib 0x%X int 0x%X to record 0x%X\n",			attrib, value_int, handle);		sdp_attr_add_new(rec, attrib, SDP_UINT32, &value_int);	} else {		/* String */		printf("Adding attrib 0x%X string \"%s\" to record 0x%X\n",			attrib, value, handle);		/* Add/Update our attribute to the record */		sdp_attr_add_new(rec, attrib, SDP_TEXT_STR8, value);	}	/* Update on the server */	ret = sdp_device_record_update(sess, &interface, rec);	if (ret < 0)		printf("Service Record update failed (%d).\n", errno);	sdp_record_free(rec);	return ret;}static struct option set_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *set_help = 	"Usage:\n"	"\tget record_handle attrib_id attrib_value\n";/* * Add an attribute to an existing SDP record on the local SDP server */static int cmd_setattr(int argc, char **argv){	int opt, status;	uint32_t handle;	uint16_t attrib;	sdp_session_t *sess;	for_each_opt(opt, set_options, NULL) {		switch(opt) {		default:			printf(set_help);			return -1;		}	}	argc -= optind;	argv += optind;	if (argc < 3) {		printf(set_help);		return -1;	}	/* Convert command line args */	handle = strtoul(argv[0], NULL, 16);	attrib = strtoul(argv[1], NULL, 16);	/* Do it */	sess = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);	if (!sess)		return -1;	status = set_attrib(sess, handle, attrib, argv[2]);	sdp_close(sess);	return status;}/* * We do only simple data sequences. Sequence of sequences is a pain ;-) * Jean II */static int set_attribseq(sdp_session_t *session, uint32_t handle, uint16_t attrib, int argc, char **argv){	sdp_list_t *attrid_list;	uint32_t range = 0x0000ffff;	sdp_record_t *rec;	sdp_data_t *pSequenceHolder = NULL;	void **dtdArray;	void **valueArray;	void **allocArray;	uint8_t uuid16 = SDP_UUID16;	uint8_t uint32 = SDP_UINT32;	uint8_t str8 = SDP_TEXT_STR8;	int i, ret = 0;	/* Get the old SDP record */	attrid_list = sdp_list_append(NULL, &range);	rec = sdp_service_attr_req(session, handle, SDP_ATTR_REQ_RANGE, attrid_list);	sdp_list_free(attrid_list, NULL);	if (!rec) {		printf("Service get request failed.\n");		return -1;	}	/* Create arrays */	dtdArray = (void **)malloc(argc * sizeof(void *));	valueArray = (void **)malloc(argc * sizeof(void *));	allocArray = (void **)malloc(argc * sizeof(void *));	/* Loop on all args, add them in arrays */	for (i = 0; i < argc; i++) {		/* Check the type of attribute */		if (!strncasecmp(argv[i], "u0x", 3)) {			/* UUID16 */			uint16_t value_int = strtoul((argv[i]) + 3, NULL, 16);			uuid_t *value_uuid = (uuid_t *) malloc(sizeof(uuid_t));			allocArray[i] = value_uuid;			sdp_uuid16_create(value_uuid, value_int);			printf("Adding uuid16 0x%X to record 0x%X\n", value_int, handle);			dtdArray[i] = &uuid16;			valueArray[i] = &value_uuid->value.uuid16;		} else if (!strncasecmp(argv[i], "0x", 2)) {			/* Int */			uint32_t *value_int = (uint32_t *) malloc(sizeof(int));			allocArray[i] = value_int;			*value_int = strtoul((argv[i]) + 2, NULL, 16);			printf("Adding int 0x%X to record 0x%X\n", *value_int, handle);			dtdArray[i] = &uint32;			valueArray[i] = value_int;		} else {			/* String */			printf("Adding string \"%s\" to record 0x%X\n", argv[i], handle);			dtdArray[i] = &str8;			valueArray[i] = argv[i];		}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -