@@ -70,20 +70,51 @@ STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, co
7070 common_hal_bleio_uuid_construct (self , uuid16 , NULL );
7171
7272 } else {
73- mp_buffer_info_t bufinfo ;
74- if (!mp_get_buffer (value , & bufinfo , MP_BUFFER_READ )) {
75- mp_raise_ValueError (translate ("UUID value is not int or byte buffer" ));
76- }
73+ if (MP_OBJ_IS_STR (value )) {
74+ // 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
75+ GET_STR_DATA_LEN (value , chars , len );
76+ char hex [32 ];
77+ // Validate length, hyphens, and hex digits.
78+ bool good_uuid =
79+ len == 36 && chars [8 ] == '-' && chars [13 ] == '-' && chars [18 ] == '-' && chars [23 ] == '-' ;
80+ if (good_uuid ) {
81+ size_t hex_idx = 0 ;
82+ for (int i = 0 ; i < len ; i ++ ) {
83+ if (unichar_isxdigit (chars [i ])) {
84+ hex [hex_idx ] = chars [i ];
85+ hex_idx ++ ;
86+ }
87+ }
88+ good_uuid = hex_idx == 32 ;
89+ }
90+ if (!good_uuid ) {
91+ mp_raise_ValueError (translate ("UUID string not 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'" ));
92+ }
93+
94+ size_t hex_idx = 0 ;
95+ for (int i = 15 ; i >= 0 ; i -- ) {
96+ uuid128 [i ] = (unichar_xdigit_value (hex [hex_idx ]) << 4 ) | unichar_xdigit_value (hex [hex_idx + 1 ]);
97+ hex_idx += 2 ;
98+ }
99+ } else {
100+ // Last possibility is that it's a buf.
101+ mp_buffer_info_t bufinfo ;
102+ if (!mp_get_buffer (value , & bufinfo , MP_BUFFER_READ )) {
103+ mp_raise_ValueError (translate ("UUID value is not str, int or byte buffer" ));
104+ }
77105
78- if (bufinfo .len != 16 ) {
79- mp_raise_ValueError (translate ("Byte buffer must be 16 bytes." ));
106+ if (bufinfo .len != 16 ) {
107+ mp_raise_ValueError (translate ("Byte buffer must be 16 bytes." ));
108+ }
109+
110+ memcpy (uuid128 , bufinfo .buf , 16 );
80111 }
81112
82- memcpy ( uuid128 , bufinfo . buf , 16 );
113+ // Str and bytes both get constructed the same way here.
83114 uint32_t uuid16 = (uuid128 [13 ] << 8 ) | uuid128 [12 ];
84115 uuid128 [12 ] = 0 ;
85116 uuid128 [13 ] = 0 ;
86- common_hal_bleio_uuid_construct (self , uuid16 , bufinfo . buf );
117+ common_hal_bleio_uuid_construct (self , uuid16 , uuid128 );
87118 }
88119
89120 return MP_OBJ_FROM_PTR (self );
@@ -200,9 +231,32 @@ STATIC mp_obj_t bleio_uuid_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_
200231 }
201232}
202233
234+ void bleio_uuid_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
235+ bleio_uuid_obj_t * self = MP_OBJ_TO_PTR (self_in );
236+ uint32_t size = common_hal_bleio_uuid_get_size (self );
237+ if (size == 16 ) {
238+ mp_printf (print , "UUID(0x%04x)" , common_hal_bleio_uuid_get_uuid16 (self ));
239+ } else {
240+ uint8_t uuid128 [16 ];
241+ (void ) common_hal_bleio_uuid_get_uuid128 (self , uuid128 );
242+ mp_printf (print , "UUID('"
243+ "%02x%02x%02x%02x-"
244+ "%02x%02x-"
245+ "%02x%02x-"
246+ "%02x%02x-"
247+ "%02x%02x%02x%02x%02x%02x')" ,
248+ uuid128 [15 ], uuid128 [14 ], uuid128 [13 ], uuid128 [12 ],
249+ uuid128 [11 ], uuid128 [10 ],
250+ uuid128 [9 ], uuid128 [8 ],
251+ uuid128 [7 ], uuid128 [6 ],
252+ uuid128 [5 ], uuid128 [4 ], uuid128 [3 ], uuid128 [2 ], uuid128 [1 ], uuid128 [0 ]);
253+ }
254+ }
255+
203256const mp_obj_type_t bleio_uuid_type = {
204257 { & mp_type_type },
205258 .name = MP_QSTR_UUID ,
259+ .print = bleio_uuid_print ,
206260 .make_new = bleio_uuid_make_new ,
207261 .unary_op = bleio_uuid_unary_op ,
208262 .binary_op = bleio_uuid_binary_op ,
0 commit comments