#include <sys/queue.h>
#include <sys/utsname.h>
#include <bluetooth.h>
#include <sdp.h>
#include <string.h>
#include "profile.h"
#include "provider.h"
profile_p
profile_get_descriptor(uint16_t uuid)
{
extern profile_t dun_profile_descriptor;
extern profile_t ftrn_profile_descriptor;
extern profile_t hf_profile_descriptor;
extern profile_t hset_profile_descriptor;
extern profile_t irmc_profile_descriptor;
extern profile_t irmc_command_profile_descriptor;
extern profile_t lan_profile_descriptor;
extern profile_t opush_profile_descriptor;
extern profile_t sp_profile_descriptor;
static const profile_p profiles[] = {
&dun_profile_descriptor,
&ftrn_profile_descriptor,
&hf_profile_descriptor,
&hset_profile_descriptor,
&irmc_profile_descriptor,
&irmc_command_profile_descriptor,
&lan_profile_descriptor,
&opush_profile_descriptor,
&sp_profile_descriptor
};
int32_t i;
for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++)
if (profiles[i]->uuid[0] == uuid)
return (profiles[i]);
return (NULL);
}
profile_attr_create_p
profile_get_attr(const profile_p profile, uint16_t attr)
{
attr_t const *ad = (attr_t const *) profile->attrs;
for (; ad->create != NULL; ad ++)
if (ad->attr == attr)
return (ad->create);
return (NULL);
}
int32_t
common_profile_create_service_record_handle(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
if (buf + 5 > eob)
return (-1);
SDP_PUT8(SDP_DATA_UINT32, buf);
SDP_PUT32(((provider_t const *) data)->handle, buf);
return (5);
}
int32_t
common_profile_create_service_class_id_list(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
int32_t len = 3 * (datalen >>= 1);
if (len <= 0 || len > 0xff || buf + 2 + len > eob)
return (-1);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(len, buf);
for (; datalen > 0; datalen --) {
SDP_PUT8(SDP_DATA_UUID16, buf);
SDP_PUT16(*((uint16_t const *)data), buf);
data += sizeof(uint16_t);
}
return (2 + len);
}
int32_t
common_profile_create_bluetooth_profile_descriptor_list(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
int32_t len = 8 * (datalen >>= 2);
if (len <= 0 || len > 0xff || buf + 2 + len > eob)
return (-1);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(len, buf);
for (; datalen > 0; datalen --) {
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(6, buf);
SDP_PUT8(SDP_DATA_UUID16, buf);
SDP_PUT16(*((uint16_t const *)data), buf);
data += sizeof(uint16_t);
SDP_PUT8(SDP_DATA_UINT16, buf);
SDP_PUT16(*((uint16_t const *)data), buf);
data += sizeof(uint16_t);
}
return (2 + len);
}
int32_t
common_profile_create_language_base_attribute_id_list(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
if (buf + 11 > eob)
return (-1);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(9, buf);
SDP_PUT8(SDP_DATA_UINT16, buf);
SDP_PUT16(((0x65 << 8) | 0x6e), buf);
SDP_PUT8(SDP_DATA_UINT16, buf);
SDP_PUT16(106, buf);
SDP_PUT8(SDP_DATA_UINT16, buf);
SDP_PUT16(SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID, buf);
return (11);
}
int32_t
common_profile_create_service_provider_name(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
struct utsname u;
char const *name;
if (uname(&u) < 0)
name = "Unknown";
else
name = u.sysname;
return (common_profile_create_string8(buf, eob,
(uint8_t const *)name, strlen(name)));
}
int32_t
common_profile_create_string8(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
if (datalen == 0 || datalen > 0xff || buf + 2 + datalen > eob)
return (-1);
SDP_PUT8(SDP_DATA_STR8, buf);
SDP_PUT8(datalen, buf);
memcpy(buf, data, datalen);
return (2 + datalen);
}
int32_t
rfcomm_profile_create_protocol_descriptor_list(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
if (datalen != 1 || buf + 14 > eob)
return (-1);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(12, buf);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(3, buf);
SDP_PUT8(SDP_DATA_UUID16, buf);
SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(5, buf);
SDP_PUT8(SDP_DATA_UUID16, buf);
SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
SDP_PUT8(SDP_DATA_UINT8, buf);
SDP_PUT8(*data, buf);
return (14);
}
int32_t
obex_profile_create_protocol_descriptor_list(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
if (datalen != 1 || buf + 19 > eob)
return (-1);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(17, buf);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(3, buf);
SDP_PUT8(SDP_DATA_UUID16, buf);
SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(5, buf);
SDP_PUT8(SDP_DATA_UUID16, buf);
SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
SDP_PUT8(SDP_DATA_UINT8, buf);
SDP_PUT8(*data, buf);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(3, buf);
SDP_PUT8(SDP_DATA_UUID16, buf);
SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf);
return (19);
}
int32_t
obex_profile_create_supported_formats_list(
uint8_t *buf, uint8_t const * const eob,
uint8_t const *data, uint32_t datalen)
{
int32_t len = 2 * datalen;
if (len <= 0 || len > 0xff || buf + 2 + len > eob)
return (-1);
SDP_PUT8(SDP_DATA_SEQ8, buf);
SDP_PUT8(len, buf);
for (; datalen > 0; datalen --) {
SDP_PUT8(SDP_DATA_UINT8, buf);
SDP_PUT8(*data++, buf);
}
return (2 + len);
}
int32_t
common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen)
{
if (data[0] < 1 || data[0] > 30)
return (0);
return (1);
}
int32_t
obex_profile_data_valid(uint8_t const *data, uint32_t datalen)
{
sdp_opush_profile_t const *opush = (sdp_opush_profile_t const *) data;
if (opush->server_channel < 1 ||
opush->server_channel > 30 ||
opush->supported_formats_size == 0 ||
opush->supported_formats_size > sizeof(opush->supported_formats))
return (0);
return (1);
}