utl.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2003-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * This file contains utility functions.
  21. *
  22. ******************************************************************************/
  23. #include <stddef.h>
  24. #include "bta/utl.h"
  25. #include "stack/btm_api.h"
  26. #include "osi/allocator.h"
  27. /*******************************************************************************
  28. **
  29. ** Function utl_str2int
  30. **
  31. ** Description This utility function converts a character string to an
  32. ** integer. Acceptable values in string are 0-9. If invalid
  33. ** string or string value too large, -1 is returned. Leading
  34. ** spaces are skipped.
  35. **
  36. **
  37. ** Returns Integer value or -1 on error.
  38. **
  39. *******************************************************************************/
  40. INT16 utl_str2int(const char *p_s)
  41. {
  42. INT32 val = 0;
  43. for (; *p_s == ' ' && *p_s != 0; p_s++);
  44. if (*p_s == 0) {
  45. return -1;
  46. }
  47. for (;;) {
  48. if ((*p_s < '0') || (*p_s > '9')) {
  49. return -1;
  50. }
  51. val += (INT32) (*p_s++ - '0');
  52. if (val > 32767) {
  53. return -1;
  54. }
  55. if (*p_s == 0) {
  56. return (INT16) val;
  57. } else {
  58. val *= 10;
  59. }
  60. }
  61. }
  62. /*******************************************************************************
  63. **
  64. ** Function utl_strucmp
  65. **
  66. ** Description This utility function compares two strings in uppercase.
  67. ** String p_s must be uppercase. String p_t is converted to
  68. ** uppercase if lowercase. If p_s ends first, the substring
  69. ** match is counted as a match.
  70. **
  71. **
  72. ** Returns 0 if strings match, nonzero otherwise.
  73. **
  74. *******************************************************************************/
  75. int utl_strucmp(const char *p_s, const char *p_t)
  76. {
  77. char c;
  78. while (*p_s && *p_t) {
  79. c = *p_t++;
  80. if (c >= 'a' && c <= 'z') {
  81. c -= 0x20;
  82. }
  83. if (*p_s++ != c) {
  84. return -1;
  85. }
  86. }
  87. /* if p_t hit null first, no match */
  88. if (*p_t == 0 && *p_s != 0) {
  89. return 1;
  90. }
  91. /* else p_s hit null first, count as match */
  92. else {
  93. return 0;
  94. }
  95. }
  96. /*******************************************************************************
  97. **
  98. ** Function utl_itoa
  99. **
  100. ** Description This utility function converts a UINT16 to a string. The
  101. ** string is NULL-terminated. The length of the string is
  102. ** returned;
  103. **
  104. **
  105. ** Returns Length of string.
  106. **
  107. *******************************************************************************/
  108. UINT8 utl_itoa(UINT16 i, char *p_s)
  109. {
  110. UINT16 j, k;
  111. char *p = p_s;
  112. BOOLEAN fill = FALSE;
  113. if (i == 0) {
  114. /* take care of zero case */
  115. *p++ = '0';
  116. } else {
  117. for (j = 10000; j > 0; j /= 10) {
  118. k = i / j;
  119. i %= j;
  120. if (k > 0 || fill) {
  121. *p++ = k + '0';
  122. fill = TRUE;
  123. }
  124. }
  125. }
  126. *p = 0;
  127. return (UINT8) (p - p_s);
  128. }
  129. /*******************************************************************************
  130. **
  131. ** Function utl_freebuf
  132. **
  133. ** Description This function calls osi_free to free the buffer passed
  134. ** in, if buffer pointer is not NULL, and also initializes
  135. ** buffer pointer to NULL.
  136. **
  137. **
  138. ** Returns Nothing.
  139. **
  140. *******************************************************************************/
  141. void utl_freebuf(void **p)
  142. {
  143. if (*p != NULL) {
  144. osi_free(*p);
  145. *p = NULL;
  146. }
  147. }
  148. /*******************************************************************************
  149. **
  150. ** Function utl_set_device_class
  151. **
  152. ** Description This function updates the local Device Class.
  153. **
  154. ** Parameters:
  155. ** p_cod - Pointer to the device class to set to
  156. **
  157. ** cmd - the fields of the device class to update.
  158. ** BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, minor class
  159. ** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input
  160. ** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input
  161. ** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class
  162. ** BTA_UTL_INIT_COD - overwrite major, minor, and service class
  163. **
  164. ** Returns TRUE if successful, Otherwise FALSE
  165. **
  166. *******************************************************************************/
  167. BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
  168. {
  169. UINT8 *dev;
  170. UINT16 service;
  171. UINT8 minor, major;
  172. DEV_CLASS dev_class;
  173. dev = BTM_ReadDeviceClass();
  174. BTM_COD_SERVICE_CLASS( service, dev );
  175. BTM_COD_MINOR_CLASS(minor, dev );
  176. BTM_COD_MAJOR_CLASS(major, dev );
  177. switch (cmd) {
  178. case BTA_UTL_SET_COD_MAJOR_MINOR:
  179. minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
  180. major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
  181. break;
  182. case BTA_UTL_SET_COD_SERVICE_CLASS:
  183. /* clear out the bits that is not SERVICE_CLASS bits */
  184. p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
  185. service = service | p_cod->service;
  186. break;
  187. case BTA_UTL_CLR_COD_SERVICE_CLASS:
  188. p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
  189. service = service & (~p_cod->service);
  190. break;
  191. case BTA_UTL_SET_COD_ALL:
  192. minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
  193. major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
  194. p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
  195. service = service | p_cod->service;
  196. break;
  197. case BTA_UTL_INIT_COD:
  198. minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
  199. major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
  200. service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
  201. break;
  202. default:
  203. return FALSE;
  204. }
  205. /* convert the fields into the device class type */
  206. FIELDS_TO_COD(dev_class, minor, major, service);
  207. if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS) {
  208. return TRUE;
  209. }
  210. return FALSE;
  211. }
  212. /*******************************************************************************
  213. **
  214. ** Function utl_get_device_class
  215. **
  216. ** Description This function get the local Device Class.
  217. **
  218. ** Parameters:
  219. ** p_cod - Pointer to the device class to get to
  220. **
  221. **
  222. ** Returns TRUE if successful, Otherwise FALSE
  223. **
  224. *******************************************************************************/
  225. BOOLEAN utl_get_device_class(tBTA_UTL_COD *p_cod)
  226. {
  227. UINT8 *dev;
  228. UINT16 service;
  229. UINT8 minor, major;
  230. dev = BTM_ReadDeviceClass();
  231. BTM_COD_SERVICE_CLASS( service, dev );
  232. BTM_COD_MINOR_CLASS(minor, dev );
  233. BTM_COD_MAJOR_CLASS(major, dev );
  234. p_cod->minor = minor;
  235. p_cod->major = major;
  236. p_cod->service = service;
  237. return TRUE;
  238. }
  239. /*******************************************************************************
  240. **
  241. ** Function utl_isintstr
  242. **
  243. ** Description This utility function checks if the given string is an
  244. ** integer string or not
  245. **
  246. **
  247. ** Returns TRUE if successful, Otherwise FALSE
  248. **
  249. *******************************************************************************/
  250. BOOLEAN utl_isintstr(const char *p_s)
  251. {
  252. UINT16 i = 0;
  253. for (i = 0; p_s[i] != 0; i++) {
  254. if (((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) {
  255. return FALSE;
  256. }
  257. }
  258. return TRUE;
  259. }
  260. /*******************************************************************************
  261. **
  262. ** Function utl_isdialstr
  263. **
  264. ** Description This utility function checks if the given string contains
  265. ** only dial digits or not
  266. **
  267. **
  268. ** Returns TRUE if successful, Otherwise FALSE
  269. **
  270. *******************************************************************************/
  271. BOOLEAN utl_isdialstr(const char *p_s)
  272. {
  273. UINT16 i = 0;
  274. for (i = 0; p_s[i] != 0; i++) {
  275. if (!(((p_s[i] >= '0') && (p_s[i] <= '9'))
  276. || (p_s[i] == '*') || (p_s[i] == '+') || (p_s[i] == '#') || (p_s[i] == ';')
  277. || ((p_s[i] >= 'A') && (p_s[i] <= 'C'))
  278. || ((p_s[i] == 'p') || (p_s[i] == 'P')
  279. || (p_s[i] == 'w') || (p_s[i] == 'W')))) {
  280. return FALSE;
  281. }
  282. }
  283. return TRUE;
  284. }