oi_utils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 The Android Open Source Project
  4. * Copyright 2002 - 2004 Open Interface North America, Inc. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. ******************************************************************************/
  19. #ifndef _OI_UTILS_H
  20. #define _OI_UTILS_H
  21. /**
  22. * @file
  23. *
  24. * This file provides the interface for utility functions.
  25. * Among the utilities are strlen (string length), strcmp (string compare), and
  26. * other string manipulation functions. These are provided for those plaforms
  27. * where this functionality is not available in stdlib.
  28. */
  29. /**********************************************************************************
  30. $Revision: #1 $
  31. ***********************************************************************************/
  32. #include <stdarg.h>
  33. #include "oi_common.h"
  34. #include "oi_string.h"
  35. #include "oi_bt_spec.h"
  36. /** \addtogroup Misc Miscellaneous APIs */
  37. /**@{*/
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /**
  42. * Opaque type for a callback function handle. See OI_ScheduleCallbackFunction()
  43. */
  44. typedef OI_UINT32 OI_CALLBACK_HANDLE;
  45. /**
  46. * Function prototype for a timed procedure callback.
  47. *
  48. * @param arg Value that was passed into the OI_ScheduleCallback() function
  49. *
  50. */
  51. typedef void (*OI_SCHEDULED_CALLBACK)(void *arg);
  52. /**
  53. * Registers a function to be called when a timeout expires. This API uses BLUEmagic's internal
  54. * function dispatch mechanism, so applications that make extensive use of this facility may need to
  55. * increase the value of DispatchTableSize in the configuration block for the dispatcher (see
  56. * oi_bt_stack_config.h).
  57. *
  58. * @param callbackFunction The function that will be called when the timeout expires
  59. *
  60. * @param arg Value that will be returned as the parameter to the callback function.
  61. *
  62. * @param timeout A timeout expressed in OI_INTERVALs (tenths of seconds). This can be
  63. * zero in which case the callback function will be called as soon as
  64. * possible.
  65. *
  66. * @param handle NULL or a pointer receive the callback handle.
  67. *
  68. * @return OI_OK if the function was reqistered, or an error status.
  69. */
  70. OI_STATUS OI_ScheduleCallbackFunction(OI_SCHEDULED_CALLBACK callbackFunction,
  71. void *arg,
  72. OI_INTERVAL timeout,
  73. OI_CALLBACK_HANDLE *handle);
  74. /**
  75. * Cancels a function registered with OI_ScheduleCallbackFunction() before its timer expires.
  76. *
  77. * @param handle handle returned by OI_ScheduleCallbackFunction().
  78. *
  79. * @return OI_OK if the function was cancelled, or an error status.
  80. */
  81. OI_STATUS OI_CancelCallbackFunction(OI_CALLBACK_HANDLE handle);
  82. /**
  83. * Registers a function to be called when a timeout expires. This version does not return a handle
  84. * so can only be canceled by calling OI_CancelCallback().
  85. *
  86. * @param callbackFunction The function that will be called when the timeout expires
  87. *
  88. * @param arg Value that will be returned as the parameter to the callback function.
  89. *
  90. * @param timeout A timeout expressed in OI_INTERVALs (tenths of seconds). This can be
  91. * zero in which case the callback function will be called as soon as
  92. * possible.
  93. *
  94. * @return OI_OK if the function was reqistered, or an error status.
  95. */
  96. #define OI_ScheduleCallback(f, a, t) OI_ScheduleCallbackFunction(f, a, t, NULL);
  97. /**
  98. * Cancels a function registered with OI_ScheduleCallback() before its timer expires. This
  99. * function will cancel the first entry matches the indicated callback function pointer.
  100. *
  101. * @param callbackFunction The function that was originally registered
  102. *
  103. * @return OI_OK if the function was cancelled, or an error status.
  104. */
  105. OI_STATUS OI_CancelCallback(OI_SCHEDULED_CALLBACK callbackFunction);
  106. /**
  107. * Parse a Bluetooth device address from the specified string.
  108. *
  109. * @param str the string to parse
  110. * @param addr the parsed address, if successful
  111. *
  112. * @return TRUE if an address was successfully parsed, FALSE otherwise
  113. */
  114. OI_BOOL OI_ParseBdAddr(const OI_CHAR *str,
  115. OI_BD_ADDR *addr) ;
  116. /**
  117. * Printf function for platforms which have no stdio or printf available.
  118. * OI_Printf supports the basic formatting types, with the exception of
  119. * floating point types. Additionally, OI_Printf supports several formats
  120. * specific to BLUEmagic 3.0 software:
  121. *
  122. * \%! prints the string for an #OI_STATUS value.
  123. * @code OI_Printf("There was an error %!", status); @endcode
  124. *
  125. * \%@ prints a hex dump of a buffer.
  126. * Requires a pointer to the buffer and a signed integer length
  127. * (0 for default length). If the buffer is large, only an excerpt will
  128. * be printed.
  129. * @code OI_Printf("Contents of buffer %@", buffer, sizeof(buffer)); @endcode
  130. *
  131. * \%: prints a Bluetooth address in the form "HH:HH:HH:HH:HH:HH".
  132. * Requires a pointer to an #OI_BD_ADDR.
  133. * @code OI_Printf("Bluetooth address %:", &bdaddr); @endcode
  134. *
  135. * \%^ decodes and prints a data element as formatted XML.
  136. * Requires a pointer to an #OI_DATAELEM.
  137. * @code OI_Printf("Service attribute list is:\n%^", &attributes); @endcode
  138. *
  139. * \%/ prints the base file name of a path, that is, the final substring
  140. * following a '/' or '\\' character. Requires a pointer to a null
  141. * terminated string.
  142. * @code OI_Printf("File %/", "c:\\dir1\\dir2\\file.txt"); @endcode
  143. *
  144. * \%~ prints a string, escaping characters as needed to display it in
  145. * ASCII. Requires a pointer to an #OI_PSTR and an #OI_UNICODE_ENCODING
  146. * parameter.
  147. * @code OI_Printf("Identifier %~", &id, OI_UNICODE_UTF16_BE); @endcode
  148. *
  149. * \%[ inserts an ANSI color escape sequence. Requires a single character
  150. * identifying the color to select. Colors are red (r/R), green (g/G),
  151. * blue (b/B), yellow (y/Y), cyan (c/C), magenta (m/M), white (W),
  152. * light-gray (l/L), dark-gray (d/D), and black (0). The lower case is
  153. * dim, the upper case is bright (except in the case of light-gray and
  154. * dark-gray, where bright and dim are identical). Any other value will
  155. * select the default color.
  156. * @code OI_Printf("%[red text %[black %[normal\n", 'r', '0', 0); @endcode
  157. *
  158. * \%a same as \%s, except '\\r' and '\\n' are output as "<cr>" and "<lf>".
  159. * \%?a is valid, but \%la is not.
  160. *
  161. * \%b prints an integer in base 2.
  162. * @code OI_Printf("Bits are %b", I); @endcode
  163. *
  164. * \%lb prints a long integer in base 2.
  165. *
  166. * \%?b prints the least significant N bits of an integer (or long integer)
  167. * in base 2. Requires the integer and a length N.
  168. * @code OI_Printf("Bottom 4 bits are: %?b", I, 4); @endcode
  169. *
  170. * \%B prints an integer as boolean text, "TRUE" or "FALSE".
  171. * @code OI_Printf("The value 0 is %B, the value 1 is %B", 0, 1); @endcode
  172. *
  173. * \%?s prints a substring up to a specified maximum length.
  174. * Requires a pointer to a string and a length parameter.
  175. * @code OI_Printf("String prefix is %?s", str, 3); @endcode
  176. *
  177. * \%ls same as \%S.
  178. *
  179. * \%S prints a UTF16 string as UTF8 (plain ASCII, plus 8-bit char sequences
  180. * where needed). Requires a pointer to #OI_CHAR16. \%?S is valid. The
  181. * length parameter is in OI_CHAR16 characters.
  182. *
  183. * \%T prints time, formatted as "secs.msecs".
  184. * Requires pointer to #OI_TIME struct, NULL pointer prints current time.
  185. * @code OI_Printf("The time now is %T", NULL); @endcode
  186. *
  187. * @param format The format string
  188. *
  189. */
  190. void OI_Printf(const OI_CHAR *format, ...);
  191. /**
  192. * Var-args version OI_Printf
  193. *
  194. * @param format Same as for OI_Printf.
  195. *
  196. * @param argp Var-args list.
  197. */
  198. void OI_VPrintf(const OI_CHAR *format, va_list argp);
  199. /**
  200. * Writes a formatted string to a buffer. This function supports the same format specifiers as
  201. * OI_Printf().
  202. *
  203. * @param buffer Destination buffer for the formatted string.
  204. *
  205. * @param bufLen The length of the destination buffer.
  206. *
  207. * @param format The format string
  208. *
  209. * @return Number of characters written or -1 in the case of an error.
  210. */
  211. OI_INT32 OI_SNPrintf(OI_CHAR *buffer,
  212. OI_UINT16 bufLen,
  213. const OI_CHAR *format, ...);
  214. /**
  215. * Var-args version OI_SNPrintf
  216. *
  217. * @param buffer Destination buffer for the formatted string.
  218. *
  219. * @param bufLen The length of the destination buffer.
  220. *
  221. * @param format The format string
  222. *
  223. * @param argp Var-args list.
  224. *
  225. * @return Number of characters written or -1 in the case of an error.
  226. */
  227. OI_INT32 OI_VSNPrintf(OI_CHAR *buffer,
  228. OI_UINT16 bufLen,
  229. const OI_CHAR *format, va_list argp);
  230. /**
  231. * Convert a string to an integer.
  232. *
  233. * @param str the string to parse
  234. *
  235. * @return the integer value of the string or 0 if the string could not be parsed
  236. */
  237. OI_INT OI_atoi(const OI_CHAR *str);
  238. /**
  239. * Parse a signed integer in a string.
  240. *
  241. * Skips leading whitespace (space and tabs only) and parses a decimal or hex string. Hex string
  242. * must be prefixed by "0x". Returns pointer to first character following the integer. Returns the
  243. * pointer passed in if the string does not describe an integer.
  244. *
  245. * @param str String to parse.
  246. *
  247. * @param val Pointer to receive the parsed integer value.
  248. *
  249. * @return A pointer to the first character following the integer or the pointer passed in.
  250. */
  251. const OI_CHAR *OI_ScanInt(const OI_CHAR *str,
  252. OI_INT32 *val);
  253. /**
  254. * Parse an unsigned integer in a string.
  255. *
  256. * Skips leading whitespace (space and tabs only) and parses a decimal or hex string. Hex string
  257. * must be prefixed by "0x". Returns pointer to first character following the integer. Returns the
  258. * pointer passed in if the string does not describe an integer.
  259. *
  260. * @param str String to parse.
  261. *
  262. * @param val Pointer to receive the parsed unsigned integer value.
  263. *
  264. * @return A pointer to the first character following the unsigned integer or the pointer passed in.
  265. */
  266. const OI_CHAR *OI_ScanUInt(const OI_CHAR *str,
  267. OI_UINT32 *val);
  268. /**
  269. * Parse a whitespace delimited substring out of a string.
  270. *
  271. * @param str Input string to parse.
  272. * @param outStr Buffer to return the substring
  273. * @param len Length of outStr
  274. *
  275. *
  276. * @return A pointer to the first character following the substring or the pointer passed in.
  277. */
  278. const OI_CHAR *OI_ScanStr(const OI_CHAR *str,
  279. OI_CHAR *outStr,
  280. OI_UINT16 len);
  281. /**
  282. * Parse a string for one of a set of alternative value. Skips leading whitespace (space and tabs
  283. * only) and parses text matching one of the alternative strings. Returns pointer to first character
  284. * following the matched text.
  285. *
  286. * @param str String to parse.
  287. *
  288. * @param alts Alternative matching strings separated by '|'
  289. *
  290. * @param index Pointer to receive the index of the matching alternative, return value is -1 if
  291. * there is no match.
  292. *
  293. * @return A pointer to the first character following the matched value or the pointer passed in
  294. * if there was no matching text.
  295. */
  296. const OI_CHAR *OI_ScanAlt(const OI_CHAR *str,
  297. const OI_CHAR *alts,
  298. OI_INT *index);
  299. /**
  300. * Parse a string for a BD Addr. Skips leading whitespace (space and tabs only) and parses a
  301. * Bluetooth device address with nibbles optionally separated by colons. Return pointet to first
  302. * character following the BD Addr.
  303. *
  304. * @param str String to parse.
  305. *
  306. * @param addr Pointer to receive the Bluetooth device address
  307. *
  308. * @return A pointer to the first character following the BD Addr or the pointer passed in.
  309. */
  310. const OI_CHAR *OI_ScanBdAddr(const OI_CHAR *str,
  311. OI_BD_ADDR *addr);
  312. /** Get a character from a digit integer value (0 - 9). */
  313. #define OI_DigitToChar(d) ((d) + '0')
  314. /**
  315. * Determine Maximum and Minimum between two arguments.
  316. *
  317. * @param a 1st value
  318. * @param b 2nd value
  319. *
  320. * @return the max or min value between a & b
  321. */
  322. #define OI_MAX(a, b) (((a) < (b)) ? (b) : (a) )
  323. #define OI_MIN(a, b) (((a) > (b)) ? (b) : (a) )
  324. /**
  325. * Compare two BD_ADDRs
  326. * SAME_BD_ADDR - Boolean: TRUE if they are the same address
  327. */
  328. #define SAME_BD_ADDR(x, y) (0 == OI_MemCmp((x),(y),OI_BD_ADDR_BYTE_SIZE) )
  329. #ifdef __cplusplus
  330. }
  331. #endif
  332. /**@}*/
  333. #endif /* _OI_UTILS_H */