subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

ble.c (2231B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2024 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #ifdef HAVE_CONFIG_H
     23 #include "config.h"
     24 #endif
     25 
     26 #include <string.h>
     27 
     28 #include <libdivecomputer/ble.h>
     29 
     30 #include "platform.h"
     31 
     32 char *
     33 dc_ble_uuid2str (const dc_ble_uuid_t uuid, char *str, size_t size)
     34 {
     35 	if (str == NULL || size < DC_BLE_UUID_SIZE)
     36 		return NULL;
     37 
     38 	int n = dc_platform_snprintf(str, size,
     39 		"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
     40 		uuid[0], uuid[1], uuid[2], uuid[3],
     41 		uuid[4], uuid[5],
     42 		uuid[6], uuid[7],
     43 		uuid[8], uuid[9],
     44 		uuid[10], uuid[11], uuid[12],
     45 		uuid[13], uuid[14], uuid[15]);
     46 	if (n < 0 || (size_t) n >= size)
     47 		return NULL;
     48 
     49 	return str;
     50 }
     51 
     52 int
     53 dc_ble_str2uuid (const char *str, dc_ble_uuid_t uuid)
     54 {
     55 	dc_ble_uuid_t tmp = {0};
     56 
     57 	if (str == NULL || uuid == NULL)
     58 		return 0;
     59 
     60 	unsigned int i = 0;
     61 	unsigned char c = 0;
     62 	while ((c = *str++) != '\0') {
     63 		if (c == '-') {
     64 			if (i != 8 && i != 12 && i != 16 && i != 20) {
     65 				return 0; /* Invalid character! */
     66 			}
     67 			continue;
     68 		} else if (c >= '0' && c <= '9') {
     69 			c -= '0';
     70 		} else if (c >= 'A' && c <= 'F') {
     71 			c -= 'A' - 10;
     72 		} else if (c >= 'a' && c <= 'f') {
     73 			c -= 'a' - 10;
     74 		} else {
     75 			return 0; /* Invalid character! */
     76 		}
     77 
     78 		if ((i & 1) == 0) {
     79 			c <<= 4;
     80 		}
     81 
     82 		if (i >= 2 * sizeof(tmp)) {
     83 			return 0; /* Too many characters! */
     84 		}
     85 
     86 		tmp[i / 2] |= c;
     87 		i++;
     88 	}
     89 
     90 	if (i != 2 * sizeof(tmp)) {
     91 		return 0; /* Not enough characters! */
     92 	}
     93 
     94 	memcpy (uuid, tmp, sizeof(tmp));
     95 
     96 	return 1;
     97 }