Skip to content

Commit

Permalink
WIP: Support utf8 text coming from Gadgetbridge.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Oct 30, 2023
1 parent 4a4bacb commit ec0bc39
Show file tree
Hide file tree
Showing 3 changed files with 1,198 additions and 4 deletions.
165 changes: 165 additions & 0 deletions app/src/ble/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,175 @@ static int32_t extract_value_int32(char *key, char *data)
return id;
}

static uint8_t basic_latin_utf16_to_utf8_table[128][3] = {
{0x80, 0xc2, 0x80},
{0x81, 0xc2, 0x81},
{0x82, 0xc2, 0x82},
{0x83, 0xc2, 0x83},
{0x84, 0xc2, 0x84},
{0x85, 0xc2, 0x85},
{0x86, 0xc2, 0x86},
{0x87, 0xc2, 0x87},
{0x88, 0xc2, 0x88},
{0x89, 0xc2, 0x89},
{0x8A, 0xc2, 0x8a},
{0x8B, 0xc2, 0x8b},
{0x8C, 0xc2, 0x8c},
{0x8D, 0xc2, 0x8d},
{0x8E, 0xc2, 0x8e},
{0x8F, 0xc2, 0x8f},
{0x90, 0xc2, 0x90},
{0x91, 0xc2, 0x91},
{0x92, 0xc2, 0x92},
{0x93, 0xc2, 0x93},
{0x94, 0xc2, 0x94},
{0x95, 0xc2, 0x95},
{0x96, 0xc2, 0x96},
{0x97, 0xc2, 0x97},
{0x98, 0xc2, 0x98},
{0x99, 0xc2, 0x99},
{0x9A, 0xc2, 0x9a},
{0x9B, 0xc2, 0x9b},
{0x9C, 0xc2, 0x9c},
{0x9D, 0xc2, 0x9d},
{0x9E, 0xc2, 0x9e},
{0x9F, 0xc2, 0x9f},
{0xA0, 0xc2, 0xa0},
{0xA1, 0xc2, 0xa1},
{0xA2, 0xc2, 0xa2},
{0xA3, 0xc2, 0xa3},
{0xA4, 0xc2, 0xa4},
{0xA5, 0xc2, 0xa5},
{0xA6, 0xc2, 0xa6},
{0xA7, 0xc2, 0xa7},
{0xA8, 0xc2, 0xa8},
{0xA9, 0xc2, 0xa9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xE1, 0xc3, 0xa1},
{0xE2, 0xc3, 0xa2},
{0xE3, 0xc3, 0xa3},
{0xE4, 0xc3, 0xa4},
{0xE5, 0xc3, 0xa5},
{0xE6, 0xc3, 0xa6},
{0xE7, 0xc3, 0xa7},
{0xE8, 0xc3, 0xa8},
{0xE9, 0xc3, 0xa9},
{0xEA, 0xc3, 0xaa},
{0xEB, 0xc3, 0xab},
{0xEC, 0xc3, 0xac},
{0xED, 0xc3, 0xad},
{0xEE, 0xc3, 0xae},
{0xEF, 0xc3, 0xaf},
{0xF0, 0xc3, 0xb0},
{0xF1, 0xc3, 0xb1},
{0xF2, 0xc3, 0xb2},
{0xF3, 0xc3, 0xb3},
{0xF4, 0xc3, 0xb4},
{0xF5, 0xc3, 0xb5},
{0xF6, 0xc3, 0xb6},
{0xF7, 0xc3, 0xb7},
{0xF8, 0xc3, 0xb8},
{0xF9, 0xc3, 0xb9},
{0xFA, 0xc3, 0xba},
{0xFB, 0xc3, 0xbb},
{0xFC, 0xc3, 0xbc},
{0xFD, 0xc3, 0xbd},
{0xFE, 0xc3, 0xbe},
{0xFF, 0xc3, 0xbf},
};

static char* parse_and_convert_to_utf8(char* data, int len)
{
int i = 0, j = 0;
char output[100];
memset(output, 0, sizeof(output));
while (data[i] != '\0' && i < len - 3) {
if (data[i] == '\\' && data[i+1] == 'x') {
char hex[3] = {data[i+2], data[i+3], '\0'};
int value = strtol(hex, NULL, 16);
if (value < 0x80) {
data[j] = value;
} else {
data[j] = basic_latin_utf16_to_utf8_table[value - 0x80][1];
j++;
data[j] = basic_latin_utf16_to_utf8_table[value - 0x80][2];
j++;
}
i += 4;
} else {
char tmp = data[i];
data[j] = tmp;
//data[j] = data[i];
i++;
j++;
}
}

data[j] = '\0';
return data;
}

static int parse_notify(char *data, int len)
{
ble_comm_cb_data_t cb;
memset(&cb, 0, sizeof(cb));
char * asdf = "w\xc4q";

data = parse_and_convert_to_utf8(data, len);

cb.type = BLE_COMM_DATA_TYPE_NOTIFY;
cb.data.notify.id = extract_value_uint32("\"id\":", data);
Expand Down
Loading

0 comments on commit ec0bc39

Please sign in to comment.