2016-09-26 13:37:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2012 Broadcom Corporation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* This file contains utility functions.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#include <stddef.h>
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "bta/utl.h"
|
|
|
|
#include "stack/btm_api.h"
|
|
|
|
#include "osi/allocator.h"
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_str2int
|
|
|
|
**
|
|
|
|
** Description This utility function converts a character string to an
|
|
|
|
** integer. Acceptable values in string are 0-9. If invalid
|
|
|
|
** string or string value too large, -1 is returned. Leading
|
|
|
|
** spaces are skipped.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Returns Integer value or -1 on error.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
INT16 utl_str2int(const char *p_s)
|
|
|
|
{
|
|
|
|
INT32 val = 0;
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
for (; *p_s == ' ' && *p_s != 0; p_s++);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (*p_s == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
for (;;) {
|
|
|
|
if ((*p_s < '0') || (*p_s > '9')) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
val += (INT32) (*p_s++ - '0');
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (val > 32767) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (*p_s == 0) {
|
2016-09-26 13:37:39 +00:00
|
|
|
return (INT16) val;
|
2016-11-24 18:10:15 +00:00
|
|
|
} else {
|
2016-09-26 13:37:39 +00:00
|
|
|
val *= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_strucmp
|
|
|
|
**
|
|
|
|
** Description This utility function compares two strings in uppercase.
|
|
|
|
** String p_s must be uppercase. String p_t is converted to
|
|
|
|
** uppercase if lowercase. If p_s ends first, the substring
|
|
|
|
** match is counted as a match.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Returns 0 if strings match, nonzero otherwise.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
int utl_strucmp(const char *p_s, const char *p_t)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
while (*p_s && *p_t) {
|
2016-09-26 13:37:39 +00:00
|
|
|
c = *p_t++;
|
2016-11-24 18:10:15 +00:00
|
|
|
if (c >= 'a' && c <= 'z') {
|
2016-09-26 13:37:39 +00:00
|
|
|
c -= 0x20;
|
|
|
|
}
|
2016-11-24 18:10:15 +00:00
|
|
|
if (*p_s++ != c) {
|
2016-09-26 13:37:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if p_t hit null first, no match */
|
2016-11-24 18:10:15 +00:00
|
|
|
if (*p_t == 0 && *p_s != 0) {
|
2016-09-26 13:37:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* else p_s hit null first, count as match */
|
2016-11-24 18:10:15 +00:00
|
|
|
else {
|
2016-09-26 13:37:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_itoa
|
|
|
|
**
|
|
|
|
** Description This utility function converts a UINT16 to a string. The
|
|
|
|
** string is NULL-terminated. The length of the string is
|
|
|
|
** returned;
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Returns Length of string.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
UINT8 utl_itoa(UINT16 i, char *p_s)
|
|
|
|
{
|
|
|
|
UINT16 j, k;
|
|
|
|
char *p = p_s;
|
|
|
|
BOOLEAN fill = FALSE;
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (i == 0) {
|
2016-09-26 13:37:39 +00:00
|
|
|
/* take care of zero case */
|
|
|
|
*p++ = '0';
|
2016-11-24 18:10:15 +00:00
|
|
|
} else {
|
|
|
|
for (j = 10000; j > 0; j /= 10) {
|
2016-09-26 13:37:39 +00:00
|
|
|
k = i / j;
|
|
|
|
i %= j;
|
2016-11-24 18:10:15 +00:00
|
|
|
if (k > 0 || fill) {
|
|
|
|
*p++ = k + '0';
|
|
|
|
fill = TRUE;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return (UINT8) (p - p_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_freebuf
|
|
|
|
**
|
2017-08-17 13:13:45 +00:00
|
|
|
** Description This function calls osi_free to free the buffer passed
|
2016-09-26 13:37:39 +00:00
|
|
|
** in, if buffer pointer is not NULL, and also initializes
|
|
|
|
** buffer pointer to NULL.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Returns Nothing.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
void utl_freebuf(void **p)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
if (*p != NULL) {
|
2017-08-17 13:13:45 +00:00
|
|
|
osi_free(*p);
|
2016-09-26 13:37:39 +00:00
|
|
|
*p = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_set_device_class
|
|
|
|
**
|
|
|
|
** Description This function updates the local Device Class.
|
|
|
|
**
|
|
|
|
** Parameters:
|
|
|
|
** p_cod - Pointer to the device class to set to
|
|
|
|
**
|
|
|
|
** cmd - the fields of the device class to update.
|
|
|
|
** BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, minor class
|
|
|
|
** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input
|
|
|
|
** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input
|
|
|
|
** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class
|
|
|
|
** BTA_UTL_INIT_COD - overwrite major, minor, and service class
|
|
|
|
**
|
|
|
|
** Returns TRUE if successful, Otherwise FALSE
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
|
|
|
|
{
|
|
|
|
UINT8 *dev;
|
|
|
|
UINT16 service;
|
|
|
|
UINT8 minor, major;
|
|
|
|
DEV_CLASS dev_class;
|
|
|
|
|
|
|
|
dev = BTM_ReadDeviceClass();
|
|
|
|
BTM_COD_SERVICE_CLASS( service, dev );
|
|
|
|
BTM_COD_MINOR_CLASS(minor, dev );
|
|
|
|
BTM_COD_MAJOR_CLASS(major, dev );
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
switch (cmd) {
|
2016-09-26 13:37:39 +00:00
|
|
|
case BTA_UTL_SET_COD_MAJOR_MINOR:
|
|
|
|
minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
|
|
|
|
major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTA_UTL_SET_COD_SERVICE_CLASS:
|
|
|
|
/* clear out the bits that is not SERVICE_CLASS bits */
|
|
|
|
p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
|
|
|
|
service = service | p_cod->service;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTA_UTL_CLR_COD_SERVICE_CLASS:
|
|
|
|
p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
|
|
|
|
service = service & (~p_cod->service);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTA_UTL_SET_COD_ALL:
|
|
|
|
minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
|
|
|
|
major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
|
|
|
|
p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
|
|
|
|
service = service | p_cod->service;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTA_UTL_INIT_COD:
|
|
|
|
minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
|
|
|
|
major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
|
|
|
|
service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert the fields into the device class type */
|
|
|
|
FIELDS_TO_COD(dev_class, minor, major, service);
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS) {
|
2016-09-26 13:37:39 +00:00
|
|
|
return TRUE;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-04-09 11:25:26 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_get_device_class
|
|
|
|
**
|
|
|
|
** Description This function get the local Device Class.
|
|
|
|
**
|
|
|
|
** Parameters:
|
|
|
|
** p_cod - Pointer to the device class to get to
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Returns TRUE if successful, Otherwise FALSE
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
BOOLEAN utl_get_device_class(tBTA_UTL_COD *p_cod)
|
|
|
|
{
|
|
|
|
UINT8 *dev;
|
|
|
|
UINT16 service;
|
|
|
|
UINT8 minor, major;
|
|
|
|
|
|
|
|
dev = BTM_ReadDeviceClass();
|
|
|
|
BTM_COD_SERVICE_CLASS( service, dev );
|
|
|
|
BTM_COD_MINOR_CLASS(minor, dev );
|
|
|
|
BTM_COD_MAJOR_CLASS(major, dev );
|
|
|
|
|
|
|
|
p_cod->minor = minor;
|
|
|
|
p_cod->major = major;
|
|
|
|
p_cod->service = service;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:37:39 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_isintstr
|
|
|
|
**
|
|
|
|
** Description This utility function checks if the given string is an
|
|
|
|
** integer string or not
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Returns TRUE if successful, Otherwise FALSE
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
BOOLEAN utl_isintstr(const char *p_s)
|
|
|
|
{
|
|
|
|
UINT16 i = 0;
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
for (i = 0; p_s[i] != 0; i++) {
|
|
|
|
if (((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) {
|
2016-09-26 13:37:39 +00:00
|
|
|
return FALSE;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function utl_isdialstr
|
|
|
|
**
|
|
|
|
** Description This utility function checks if the given string contains
|
|
|
|
** only dial digits or not
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Returns TRUE if successful, Otherwise FALSE
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
BOOLEAN utl_isdialstr(const char *p_s)
|
|
|
|
{
|
|
|
|
UINT16 i = 0;
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
for (i = 0; p_s[i] != 0; i++) {
|
|
|
|
if (!(((p_s[i] >= '0') && (p_s[i] <= '9'))
|
|
|
|
|| (p_s[i] == '*') || (p_s[i] == '+') || (p_s[i] == '#') || (p_s[i] == ';')
|
|
|
|
|| ((p_s[i] >= 'A') && (p_s[i] <= 'C'))
|
|
|
|
|| ((p_s[i] == 'p') || (p_s[i] == 'P')
|
|
|
|
|| (p_s[i] == 'w') || (p_s[i] == 'W')))) {
|
2016-09-26 13:37:39 +00:00
|
|
|
return FALSE;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|