diff options
-rw-r--r-- | include/bluetooth.h | 1 | ||||
-rw-r--r-- | src/bluetooth.c | 35 |
2 files changed, 36 insertions, 0 deletions
diff --git a/include/bluetooth.h b/include/bluetooth.h index 1362ed82..1d377781 100644 --- a/include/bluetooth.h +++ b/include/bluetooth.h @@ -125,6 +125,7 @@ char *batostr(const bdaddr_t *ba); int ba2str(const bdaddr_t *ba, char *str); int str2ba(const char *str, bdaddr_t *ba); int ba2oui(const bdaddr_t *ba, char *oui); +int bachk(const char *str); int baprintf(const char *format, ...); int bafprintf(FILE *stream, const char *format, ...); diff --git a/src/bluetooth.c b/src/bluetooth.c index f0f800a5..676e468e 100644 --- a/src/bluetooth.c +++ b/src/bluetooth.c @@ -29,6 +29,7 @@ #include <stdio.h> #include <errno.h> +#include <ctype.h> #include <stdarg.h> #include <stdlib.h> #include <malloc.h> @@ -110,6 +111,40 @@ int ba2oui(const bdaddr_t *ba, char *str) return sprintf(str, "%2.2X-%2.2X-%2.2X", b[0], b[1], b[2]); } +int bachk(const char *str) +{ + char tmp[18], *ptr = tmp; + + if (!str) + return -1; + + if (strlen(str) != 17) + return -1; + + memcpy(tmp, str, 18); + + while (*ptr) { + *ptr = toupper(*ptr); + if (*ptr < '0'|| (*ptr > '9' && *ptr < 'A') || *ptr > 'F') + return -1; + ptr++; + + *ptr = toupper(*ptr); + if (*ptr < '0'|| (*ptr > '9' && *ptr < 'A') || *ptr > 'F') + return -1; + ptr++; + + *ptr = toupper(*ptr); + if (*ptr == 0) + break; + if (*ptr != ':') + return -1; + ptr++; + } + + return 0; +} + int baprintf(const char *format, ...) { va_list ap; |