Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 826e233

Browse files
geza-pycompeter-pycom
authored andcommitted
Add API create_128bit_le_uuid_from_string()
1 parent 3f690a5 commit 826e233

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

esp32/mods/modpycom.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,53 @@ STATIC mp_obj_t mod_pycom_sigfox_info (size_t n_args, const mp_obj_t *pos_args,
10131013
}
10141014
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_pycom_sigfox_info_obj, 0, mod_pycom_sigfox_info);
10151015

1016+
// This function creates a 128 bit long UUID stored in a byte array in Little Endian order from an input String
1017+
STATIC mp_obj_t create_128bit_le_uuid_from_string(mp_obj_t uuid_in) {
1018+
1019+
size_t length;
1020+
uint8_t new_uuid[16];
1021+
uint8_t i, j;
1022+
1023+
const char* uuid_char_in = mp_obj_str_get_data(uuid_in, &length);
1024+
// 1 character is stored on 1 byte because we received a String
1025+
// For 128 bit UUID maximum 32 characters long String can be accepted
1026+
if (length > 32) {
1027+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Input string must not be longer than 32 characters!"));
1028+
}
1029+
1030+
// Pre-fill the whole array with 0 because the remaining/not given digits will be 0
1031+
char uuid_char[32] = {0};
1032+
memcpy(uuid_char, uuid_char_in, length);
1033+
1034+
for(i = 0, j = 0; i < 32; i = i+2) {
1035+
1036+
uint8_t lower_nibble = 0;
1037+
uint8_t upper_nibble = 0;
1038+
1039+
if(uuid_char[i] > 0) {
1040+
upper_nibble = hex_from_char(uuid_char[i]);
1041+
}
1042+
1043+
if(uuid_char[i+1] > 0) {
1044+
lower_nibble = hex_from_char(uuid_char[i+1]);
1045+
}
1046+
1047+
if(lower_nibble == 16 || upper_nibble == 16) {
1048+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "UUID must only contain hexadecimal digits!"));
1049+
}
1050+
1051+
// Pack together the 4 bits digits into 1 byte
1052+
// Convert to Little Endian order because we expect that the digits of the input String follows the Natural Byte (Big Endian) order
1053+
new_uuid[15-j] = lower_nibble | (upper_nibble << 4);
1054+
j++;
1055+
}
1056+
1057+
mp_obj_t new_uuid_mp = mp_obj_new_bytearray(16, new_uuid);
1058+
return new_uuid_mp;
1059+
1060+
}
1061+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(create_128bit_le_uuid_from_string_obj, create_128bit_le_uuid_from_string);
1062+
10161063
STATIC const mp_map_elem_t pycom_module_globals_table[] = {
10171064
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pycom) },
10181065
{ MP_OBJ_NEW_QSTR(MP_QSTR_heartbeat), (mp_obj_t)&mod_pycom_heartbeat_obj },
@@ -1039,6 +1086,8 @@ STATIC const mp_map_elem_t pycom_module_globals_table[] = {
10391086
{ MP_OBJ_NEW_QSTR(MP_QSTR_wifi_pwd_sta), (mp_obj_t)&mod_pycom_wifi_pwd_sta_obj },
10401087
{ MP_OBJ_NEW_QSTR(MP_QSTR_wifi_pwd_ap), (mp_obj_t)&mod_pycom_wifi_pwd_ap_obj },
10411088
{ MP_OBJ_NEW_QSTR(MP_QSTR_wifi_mode_on_boot), (mp_obj_t)&mod_pycom_wifi_mode_obj },
1089+
{ MP_OBJ_NEW_QSTR(MP_QSTR_create_128bit_le_uuid_from_string), (mp_obj_t)&create_128bit_le_uuid_from_string_obj },
1090+
10421091

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy