Path: blob/main/usr.sbin/bluetooth/hccontrol/adv_data.c
104817 views
/*-1* adv_data.c2*3* SPDX-License-Identifier: BSD-2-Clause45* Copyright (c) 2020 Marc Veldman <[email protected]>6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29* $Id$30*/3132#include <sys/types.h>33#include <stdio.h>34#include <string.h>35#include <uuid.h>36#define L2CAP_SOCKET_CHECKED37#include <bluetooth.h>38#include "hccontrol.h"3940static char* const adv_data2str(int len, uint8_t* data, char* buffer,41int size);42static char* const adv_name2str(int len, uint8_t* advdata, char* buffer,43int size);44static char* const adv_uuid2str(int datalen, uint8_t* data, char* buffer,45int size);4647void dump_adv_data(int len, uint8_t* advdata)48{49int n=0;50fprintf(stdout, "\tADV Data: ");51for (n = 0; n < len+1; n++) {52fprintf(stdout, "%02x ", advdata[n]);53}54fprintf(stdout, "\n");55}5657void print_adv_data(int len, uint8_t* advdata)58{59int n=0;60while(n < len)61{62char buffer[2048];63uint8_t datalen = advdata[n];64uint8_t datatype = advdata[++n];65/* Skip type */66++n;67datalen--;68switch (datatype) {69case 0x01:70fprintf(stdout,71"\tFlags: %s\n",72adv_data2str(73datalen,74&advdata[n],75buffer,76sizeof(buffer)));77break;78case 0x02:79fprintf(stdout,80"\tIncomplete list of service"81" class UUIDs (16-bit): %s\n",82adv_data2str(83datalen,84&advdata[n],85buffer,86sizeof(buffer)));87break;88case 0x03:89fprintf(stdout,90"\tComplete list of service "91"class UUIDs (16-bit): %s\n",92adv_data2str(93datalen,94&advdata[n],95buffer,96sizeof(buffer)));97break;98case 0x07:99fprintf(stdout,100"\tComplete list of service "101"class UUIDs (128 bit): %s\n",102adv_uuid2str(103datalen,104&advdata[n],105buffer,106sizeof(buffer)));107break;108case 0x08:109fprintf(stdout,110"\tShortened local name: %s\n",111adv_name2str(112datalen,113&advdata[n],114buffer,115sizeof(buffer)));116break;117case 0x09:118fprintf(stdout,119"\tComplete local name: %s\n",120adv_name2str(121datalen,122&advdata[n],123buffer,124sizeof(buffer)));125break;126case 0x0a:127fprintf(stdout,128"\tTx Power level: %d dBm\n",129(int8_t)advdata[n]);130break;131case 0x0d:132fprintf(stdout,133"\tClass of device: %s\n",134adv_data2str(135datalen,136&advdata[n],137buffer,138sizeof(buffer)));139break;140case 0x16:141fprintf(stdout,142"\tService data: %s\n",143adv_data2str(144datalen,145&advdata[n],146buffer,147sizeof(buffer)));148break;149case 0x19:150fprintf(stdout,151"\tAppearance: %s\n",152adv_data2str(153datalen,154&advdata[n],155buffer,156sizeof(buffer)));157break;158case 0xff:159fprintf(stdout,160"\tManufacturer: %s\n",161hci_manufacturer2str(162advdata[n]|advdata[n+1]<<8));163fprintf(stdout,164"\tManufacturer specific data: %s\n",165adv_data2str(166datalen-2,167&advdata[n+2],168buffer,169sizeof(buffer)));170break;171default:172fprintf(stdout,173"\tUNKNOWN datatype: %02x data %s\n",174datatype,175adv_data2str(176datalen,177&advdata[n],178buffer,179sizeof(buffer)));180}181n += datalen;182}183}184185static char* const adv_data2str(int datalen, uint8_t* data, char* buffer,186int size)187{188int i = 0;189char tmpbuf[5];190191if (buffer == NULL)192return NULL;193194memset(buffer, 0, size);195196while(i < datalen) {197(void)snprintf(tmpbuf, sizeof(tmpbuf), "%02x ", data[i]);198/* Check if buffer is full */199if (strlcat(buffer, tmpbuf, size) > size)200break;201i++;202}203return buffer;204}205206static char* const adv_name2str(int datalen, uint8_t* data, char* buffer,207int size)208{209if (buffer == NULL)210return NULL;211212memset(buffer, 0, size);213214(void)strlcpy(buffer, (char*)data, datalen+1);215return buffer;216}217218static char* const adv_uuid2str(int datalen, uint8_t* data, char* buffer,219int size)220{221int i;222uuid_t uuid;223uint32_t ustatus;224char* tmpstr;225226if (buffer == NULL)227return NULL;228229memset(buffer, 0, size);230if (datalen < 16)231return buffer;232uuid.time_low = le32dec(data+12);233uuid.time_mid = le16dec(data+10);234uuid.time_hi_and_version = le16dec(data+8);235uuid.clock_seq_hi_and_reserved = data[7];236uuid.clock_seq_low = data[6];237for(i = 0; i < _UUID_NODE_LEN; i++){238uuid.node[i] = data[5 - i];239}240uuid_to_string(&uuid, &tmpstr, &ustatus);241if(ustatus == uuid_s_ok) {242strlcpy(buffer, tmpstr, size);243}244free(tmpstr);245246return buffer;247}248249250