Skip to content

Commit 96f1673

Browse files
geza-pycompeter-pycom
authored andcommitted
Add API create_128bit_le_uuid_from_string()
1 parent 9724ddc commit 96f1673

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

esp32/mods/modpycom.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,52 @@ STATIC mp_obj_t mod_pycom_ml_run_model (mp_obj_t data) {
11541154
}
11551155
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_ml_run_model_obj, mod_pycom_ml_run_model);
11561156

1157+
// This function creates a 128 bit long UUID stored in a byte array in Little Endian order from an input String
1158+
STATIC mp_obj_t create_128bit_le_uuid_from_string(mp_obj_t uuid_in) {
1159+
1160+
size_t length;
1161+
uint8_t new_uuid[16];
1162+
uint8_t i, j;
1163+
1164+
const char* uuid_char_in = mp_obj_str_get_data(uuid_in, &length);
1165+
// 1 character is stored on 1 byte because we received a String
1166+
// For 128 bit UUID maximum 32 characters long String can be accepted
1167+
if (length > 32) {
1168+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Input string must not be longer than 32 characters!"));
1169+
}
1170+
1171+
// Pre-fill the whole array with 0 because the remaining/not given digits will be 0
1172+
char uuid_char[32] = {0};
1173+
memcpy(uuid_char, uuid_char_in, length);
1174+
1175+
for(i = 0, j = 0; i < 32; i = i+2) {
1176+
1177+
uint8_t lower_nibble = 0;
1178+
uint8_t upper_nibble = 0;
1179+
1180+
if(uuid_char[i] > 0) {
1181+
upper_nibble = hex_from_char(uuid_char[i]);
1182+
}
1183+
1184+
if(uuid_char[i+1] > 0) {
1185+
lower_nibble = hex_from_char(uuid_char[i+1]);
1186+
}
1187+
1188+
if(lower_nibble == 16 || upper_nibble == 16) {
1189+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "UUID must only contain hexadecimal digits!"));
1190+
}
1191+
1192+
// Pack together the 4 bits digits into 1 byte
1193+
// Convert to Little Endian order because we expect that the digits of the input String follows the Natural Byte (Big Endian) order
1194+
new_uuid[15-j] = lower_nibble | (upper_nibble << 4);
1195+
j++;
1196+
}
1197+
1198+
mp_obj_t new_uuid_mp = mp_obj_new_bytearray(16, new_uuid);
1199+
return new_uuid_mp;
1200+
1201+
}
1202+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(create_128bit_le_uuid_from_string_obj, create_128bit_le_uuid_from_string);
11571203

11581204
STATIC const mp_map_elem_t pycom_module_globals_table[] = {
11591205
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pycom) },
@@ -1184,6 +1230,8 @@ STATIC const mp_map_elem_t pycom_module_globals_table[] = {
11841230
{ MP_OBJ_NEW_QSTR(MP_QSTR_wifi_mode_on_boot), (mp_obj_t)&mod_pycom_wifi_mode_obj },
11851231
{ MP_OBJ_NEW_QSTR(MP_QSTR_ml_new_model), (mp_obj_t)&mod_pycom_ml_new_model_obj },
11861232
{ MP_OBJ_NEW_QSTR(MP_QSTR_ml_run_model), (mp_obj_t)&mod_pycom_ml_run_model_obj },
1233+
{ MP_OBJ_NEW_QSTR(MP_QSTR_create_128bit_le_uuid_from_string), (mp_obj_t)&create_128bit_le_uuid_from_string_obj },
1234+
11871235

11881236
#if defined(FIPY) || defined(LOPY4) || defined(SIPY)
11891237
{ MP_OBJ_NEW_QSTR(MP_QSTR_sigfox_info), (mp_obj_t)&mod_pycom_sigfox_info_obj },

0 commit comments

Comments
 (0)