@@ -1013,6 +1013,53 @@ STATIC mp_obj_t mod_pycom_sigfox_info (size_t n_args, const mp_obj_t *pos_args,
1013
1013
}
1014
1014
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (mod_pycom_sigfox_info_obj , 0 , mod_pycom_sigfox_info );
1015
1015
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
+
1016
1063
STATIC const mp_map_elem_t pycom_module_globals_table [] = {
1017
1064
{ MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR_pycom ) },
1018
1065
{ 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[] = {
1039
1086
{ MP_OBJ_NEW_QSTR (MP_QSTR_wifi_pwd_sta ), (mp_obj_t )& mod_pycom_wifi_pwd_sta_obj },
1040
1087
{ MP_OBJ_NEW_QSTR (MP_QSTR_wifi_pwd_ap ), (mp_obj_t )& mod_pycom_wifi_pwd_ap_obj },
1041
1088
{ 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
+
1042
1091
1043
1092
#if defined(FIPY ) || defined (LOPY4 ) || defined (SIPY )
1044
1093
{ MP_OBJ_NEW_QSTR (MP_QSTR_sigfox_info ), (mp_obj_t )& mod_pycom_sigfox_info_obj },
0 commit comments