map_util: move code map messages parsing

This commit is contained in:
Milanka Ringwald 2019-03-22 15:12:58 +01:00
parent bab13b64a1
commit b6b303d9a0
6 changed files with 317 additions and 155 deletions

View File

@ -27,9 +27,21 @@ VPATH += ${BTSTACK_ROOT}/port/libusb
CFLAGS += $(shell pkg-config libusb-1.0 --cflags)
LDFLAGS += $(shell pkg-config libusb-1.0 --libs)
MAP = \
obex_iterator.c \
goep_client.c \
yxml.c \
btstack_util.c \
hci_dump.c \
map_util.c \
map_client.c \
map_server.c \
MAP_OBJ = $(MAP:.c=.o)
all: map_client_test
map_client_test: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} ${SDP_SERVER} md5.o obex_iterator.o goep_client.o yxml.o map_client.o map_server.o map_client_test.c
map_client_test: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} ${MAP_OBJ} map_client_test.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@

View File

@ -189,6 +189,10 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
uint8_t status;
uint16_t mtu;
int value_len;
char value[MAP_MAX_VALUE_LEN];
memset(value, 0, MAP_MAX_VALUE_LEN);
switch (packet_type){
case HCI_EVENT_PACKET:
switch (hci_event_packet_get_type(packet)) {
@ -237,7 +241,19 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
case MAP_SUBEVENT_OPERATION_COMPLETED:
printf("[+] Operation complete\n");
break;
case MAP_SUBEVENT_FOLDER_LISTING_ITEM:
value_len = btstack_min(map_subevent_folder_listing_item_get_name_len(packet), MAP_MAX_VALUE_LEN);
memcpy(value, map_subevent_folder_listing_item_get_name(packet), value_len);
printf("Folder \'%s\'\n", value);
break;
case MAP_SUBEVENT_MESSAGE_LISTING_ITEM:
memcpy((uint8_t *) message_handle, map_subevent_message_listing_item_get_handle(packet), MAP_MESSAGE_HANDLE_SIZE);
printf("Message handle: ");
printf_hexdump((uint8_t *) message_handle, MAP_MESSAGE_HANDLE_SIZE);
printf("\n");
break;
case MAP_SUBEVENT_PARSING_DONE:
break;
default:
break;
}

218
test/map_client/map_util.c Normal file
View File

@ -0,0 +1,218 @@
/*
* Copyright (C) 2019 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define __BTSTACK_FILE__ "map_util.c"
#include "btstack_config.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btstack_util.h"
#include "map_util.h"
#include "yxml.h"
#include "map.h"
#include "btstack_defines.h"
#include "btstack_util.h"
#include "btstack_debug.h"
#include "btstack_event.h"
static yxml_t xml_parser;
static uint8_t xml_buffer[50];
void map_message_str_to_handle(const char * value, map_message_handle_t msg_handle){
int i;
for (i = 0; i < MAP_MESSAGE_HANDLE_SIZE; i++) {
uint8_t upper_nibble = nibble_for_char(*value++);
uint8_t lower_nibble = nibble_for_char(*value++);
msg_handle[i] = (upper_nibble << 4) | lower_nibble;
}
}
void map_message_handle_to_str(char * p, const map_message_handle_t msg_handle){
int i;
for (i = 0; i < MAP_MESSAGE_HANDLE_SIZE ; i++) {
uint8_t byte = msg_handle[i];
*p++ = char_for_nibble(byte >> 4);
*p++ = char_for_nibble(byte & 0x0F);
}
*p = 0;
}
static void map_client_emit_folder_listing_item_event(btstack_packet_handler_t callback, uint16_t cid, uint8_t * folder_name, uint16_t folder_name_len){
uint8_t event[7 + MAP_MAX_VALUE_LEN];
uint16_t pos = 0;
event[pos++] = HCI_EVENT_MAP_META;
pos++; // skip len
event[pos++] = MAP_SUBEVENT_FOLDER_LISTING_ITEM;
little_endian_store_16(event,pos,cid);
pos+=2;
uint16_t value_len = btstack_min(folder_name_len, sizeof(event) - pos);
little_endian_store_16(event, pos, value_len);
pos += 2;
memcpy(event+pos, folder_name, value_len);
pos += value_len;
event[1] = pos - 2;
if (pos > sizeof(event)) log_error("map_client_emit_folder_listing_item_event size %u", pos);
(*callback)(HCI_EVENT_PACKET, cid, &event[0], pos);
}
static void map_client_emit_message_listing_item_event(btstack_packet_handler_t callback, uint16_t cid, map_message_handle_t message_handle){
uint8_t event[7 + MAP_MESSAGE_HANDLE_SIZE];
uint16_t pos = 0;
event[pos++] = HCI_EVENT_MAP_META;
pos++; // skip len
event[pos++] = MAP_SUBEVENT_MESSAGE_LISTING_ITEM;
little_endian_store_16(event,pos,cid);
pos+=2;
memcpy(event+pos, message_handle, MAP_MESSAGE_HANDLE_SIZE);
pos += MAP_MESSAGE_HANDLE_SIZE;
event[1] = pos - 2;
if (pos > sizeof(event)) log_error("map_client_emit_message_listing_item_event size %u", pos);
(*callback)(HCI_EVENT_PACKET, cid, &event[0], pos);
}
static void map_client_emit_parsing_done_event(btstack_packet_handler_t callback, uint16_t cid){
uint8_t event[5];
int pos = 0;
event[pos++] = HCI_EVENT_MAP_META;
pos++; // skip len
event[pos++] = MAP_SUBEVENT_PARSING_DONE;
little_endian_store_16(event,pos,cid);
pos += 2;
event[1] = pos - 2;
if (pos != sizeof(event)) log_error("map_client_emit_parsing_done_event size %u", pos);
(*callback)(HCI_EVENT_PACKET, cid, &event[0], pos);
}
void map_client_parse_folder_listing(btstack_packet_handler_t callback, uint16_t cid, const uint8_t * data, uint16_t data_len){
int folder_found = 0;
int name_found = 0;
char name[MAP_MAX_VALUE_LEN];
yxml_init(&xml_parser, xml_buffer, sizeof(xml_buffer));
while (data_len--){
yxml_ret_t r = yxml_parse(&xml_parser, *data++);
switch (r){
case YXML_ELEMSTART:
folder_found = strcmp("folder", xml_parser.elem) == 0;
break;
case YXML_ELEMEND:
if (folder_found){
map_client_emit_folder_listing_item_event(callback, cid, (uint8_t *) name, strlen(name));
}
folder_found = 0;
break;
case YXML_ATTRSTART:
if (!folder_found) break;
if (strcmp("name", xml_parser.attr) == 0){
name_found = 1;
name[0] = 0;
break;
}
break;
case YXML_ATTRVAL:
if (name_found) {
// "In UTF-8, characters from the U+0000..U+10FFFF range (the UTF-16 accessible range) are encoded using sequences of 1 to 4 octets."
if (strlen(name) + 4 + 1 >= sizeof(name)) break;
strcat(name, xml_parser.data);
break;
}
break;
case YXML_ATTREND:
name_found = 0;
break;
default:
break;
}
}
map_client_emit_parsing_done_event(callback, cid);
}
void map_client_parse_message_listing(btstack_packet_handler_t callback, uint16_t cid, const uint8_t * data, uint16_t data_len){
// now try parsing it
int message_found = 0;
int handle_found = 0;
char handle[MAP_MESSAGE_HANDLE_SIZE * 2];
map_message_handle_t msg_handle;
yxml_init(&xml_parser, xml_buffer, sizeof(xml_buffer));
while (data_len--){
yxml_ret_t r = yxml_parse(&xml_parser, *data++);
switch (r){
case YXML_ELEMSTART:
message_found = strcmp("msg", xml_parser.elem) == 0;
break;
case YXML_ELEMEND:
if (!message_found) break;
message_found = 0;
if (strlen(handle) != MAP_MESSAGE_HANDLE_SIZE * 2){
log_info("message handle string length != 16");
break;
}
map_message_str_to_handle(handle, msg_handle);
map_client_emit_message_listing_item_event(callback, cid, msg_handle);
break;
case YXML_ATTRSTART:
if (!message_found) break;
if (strcmp("handle", xml_parser.attr) == 0){
handle_found = 1;
handle[0] = 0;
break;
}
break;
case YXML_ATTRVAL:
if (handle_found) {
strcat(handle, xml_parser.data);
break;
}
break;
case YXML_ATTREND:
handle_found = 0;
break;
default:
break;
}
}
map_client_emit_parsing_done_event(callback, cid);
}

View File

@ -0,0 +1,63 @@
/*
* Copyright (C) 2019 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#ifndef __MAP_UTIL_H
#define __MAP_UTIL_H
#if defined __cplusplus
extern "C" {
#endif
#include "btstack_config.h"
#include <stdint.h>
#include "map.h"
/* API_START */
/* API_END */
// only for testing
void map_message_str_to_handle(const char * value, map_message_handle_t msg_handle);
void map_message_handle_to_str(char * p, const map_message_handle_t msg_handle);
void map_client_parse_folder_listing(btstack_packet_handler_t callback, uint16_t cid, const uint8_t * data, uint16_t data_len);
void map_client_parse_message_listing(btstack_packet_handler_t callback, uint16_t cid, const uint8_t * data, uint16_t data_len);
#if defined __cplusplus
}
#endif
#endif

View File

@ -22,7 +22,10 @@ COMMON = \
yxml.c \
btstack_util.c \
hci_dump.c \
map_util.c \
btstack_util.c \
COMMON_OBJ = $(COMMON:.c=.o)
all: map_xml_test

View File

@ -6,6 +6,7 @@
#include "btstack_util.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "map_util.h"
const static char * folders =
"<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"
@ -66,8 +67,6 @@ const static char * message =
#endif
/* xml parser */
static yxml_t xml_parser;
static uint8_t xml_buffer[50];
static int num_found_items;
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
@ -79,160 +78,12 @@ static void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int si
}
}
static void map_client_emit_folder_listing_item_event(btstack_packet_handler_t callback, uint16_t cid, uint8_t * folder_name, uint16_t folder_name_len){
uint8_t event[7 + MAP_MAX_VALUE_LEN];
int pos = 0;
event[pos++] = HCI_EVENT_MAP_META;
pos++; // skip len
event[pos++] = MAP_SUBEVENT_FOLDER_LISTING_ITEM;
little_endian_store_16(event,pos,cid);
pos+=2;
uint16_t value_len = btstack_min(folder_name_len, sizeof(event) - pos);
little_endian_store_16(event, pos, value_len);
pos += 2;
memcpy(event+pos, folder_name, value_len);
pos += value_len;
event[1] = pos - 2;
if (pos > sizeof(event)) log_error("map_client_emit_folder_listing_item_event size %u", pos);
(*callback)(HCI_EVENT_PACKET, cid, &event[0], pos);
}
static void map_client_emit_message_listing_item_event(btstack_packet_handler_t callback, uint16_t cid, map_message_handle_t message_handle){
uint8_t event[7 + MAP_MESSAGE_HANDLE_SIZE];
int pos = 0;
event[pos++] = HCI_EVENT_MAP_META;
pos++; // skip len
event[pos++] = MAP_SUBEVENT_MESSAGE_LISTING_ITEM;
little_endian_store_16(event,pos,cid);
pos+=2;
memcpy(event+pos, message_handle, MAP_MESSAGE_HANDLE_SIZE);
pos += MAP_MESSAGE_HANDLE_SIZE;
event[1] = pos - 2;
if (pos > sizeof(event)) log_error("map_client_emit_message_listing_item_event size %u", pos);
(*callback)(HCI_EVENT_PACKET, cid, &event[0], pos);
}
static void map_client_emit_parsing_done_event(btstack_packet_handler_t callback, uint16_t cid){
uint8_t event[5];
int pos = 0;
event[pos++] = HCI_EVENT_MAP_META;
pos++; // skip len
event[pos++] = MAP_SUBEVENT_PARSING_DONE;
little_endian_store_16(event,pos,cid);
pos += 2;
event[1] = pos - 2;
if (pos != sizeof(event)) log_error("map_client_emit_parsing_done_event size %u", pos);
(*callback)(HCI_EVENT_PACKET, cid, &event[0], pos);
}
static void map_client_parse_folder_listing(btstack_packet_handler_t callback, uint16_t cid, const uint8_t * data, uint16_t data_len){
int folder_found = 0;
int name_found = 0;
char name[MAP_MAX_VALUE_LEN];
while (data_len--){
yxml_ret_t r = yxml_parse(&xml_parser, *data++);
switch (r){
case YXML_ELEMSTART:
folder_found = strcmp("folder", xml_parser.elem) == 0;
break;
case YXML_ELEMEND:
if (folder_found){
map_client_emit_folder_listing_item_event(callback, cid, (uint8_t *) name, strlen(name));
}
folder_found = 0;
break;
case YXML_ATTRSTART:
if (!folder_found) break;
if (strcmp("name", xml_parser.attr) == 0){
name_found = 1;
name[0] = 0;
break;
}
break;
case YXML_ATTRVAL:
if (name_found) {
// "In UTF-8, characters from the U+0000..U+10FFFF range (the UTF-16 accessible range) are encoded using sequences of 1 to 4 octets."
if (strlen(name) + 4 + 1 >= sizeof(name)) break;
strcat(name, xml_parser.data);
break;
}
break;
case YXML_ATTREND:
name_found = 0;
break;
default:
break;
}
}
map_client_emit_parsing_done_event(callback, cid);
}
static void map_message_str_to_handle(const char * value, map_message_handle_t msg_handle){
int i;
for (i = 0; i < MAP_MESSAGE_HANDLE_SIZE; i++) {
uint8_t upper_nibble = nibble_for_char(*value++);
uint8_t lower_nibble = nibble_for_char(*value++);
msg_handle[i] = (upper_nibble << 4) | lower_nibble;
}
}
static void map_client_parse_message_listing(btstack_packet_handler_t callback, uint16_t cid, const uint8_t * data, uint16_t data_len){
// now try parsing it
int message_found = 0;
int handle_found = 0;
char handle[MAP_MESSAGE_HANDLE_SIZE * 2];
map_message_handle_t msg_handle;
while (data_len--){
yxml_ret_t r = yxml_parse(&xml_parser, *data++);
switch (r){
case YXML_ELEMSTART:
message_found = strcmp("msg", xml_parser.elem) == 0;
break;
case YXML_ELEMEND:
if (!message_found) break;
message_found = 0;
if (strlen(handle) != MAP_MESSAGE_HANDLE_SIZE * 2){
log_info("message handle string length != 16");
break;
}
map_message_str_to_handle(handle, msg_handle);
map_client_emit_message_listing_item_event(callback, cid, msg_handle);
break;
case YXML_ATTRSTART:
if (!message_found) break;
if (strcmp("handle", xml_parser.attr) == 0){
handle_found = 1;
handle[0] = 0;
break;
}
break;
case YXML_ATTRVAL:
if (handle_found) {
strcat(handle, xml_parser.data);
break;
}
break;
case YXML_ATTREND:
handle_found = 0;
break;
default:
break;
}
}
map_client_emit_parsing_done_event(callback, cid);
}
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return;
if (hci_event_packet_get_type(packet) != HCI_EVENT_MAP_META) return;
char value[MAP_MAX_VALUE_LEN];
int value_len;
char value[MAP_MAX_VALUE_LEN];
memset(value, 0, MAP_MAX_VALUE_LEN);
switch (hci_event_goep_meta_get_subevent_code(packet)){
@ -260,7 +111,6 @@ TEST_GROUP(MAP_XML){
map_callback = &packet_handler;
num_found_items = 0;
map_cid = 1;
yxml_init(&xml_parser, xml_buffer, sizeof(xml_buffer));
}
};