@@ -80,7 +80,7 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
8080 mp_raise_ValueError (translate ("Address type out of range" ));
8181 }
8282
83- common_hal_bleio_address_construct (self , buf_info .buf , buf_info . len , address_type );
83+ common_hal_bleio_address_construct (self , buf_info .buf , address_type );
8484
8585 return MP_OBJ_FROM_PTR (self );
8686}
@@ -101,6 +101,13 @@ STATIC mp_obj_t bleio_address_get_address_bytes(mp_obj_t self_in) {
101101}
102102MP_DEFINE_CONST_FUN_OBJ_1 (bleio_address_get_address_bytes_obj , bleio_address_get_address_bytes );
103103
104+ const mp_obj_property_t bleio_address_address_bytes_obj = {
105+ .base .type = & mp_type_property ,
106+ .proxy = {(mp_obj_t )& bleio_address_get_address_bytes_obj ,
107+ (mp_obj_t )& mp_const_none_obj ,
108+ (mp_obj_t )& mp_const_none_obj },
109+ };
110+
104111//| .. attribute:: type
105112//|
106113//| The address type (read-only). One of these integers:
@@ -124,9 +131,47 @@ const mp_obj_property_t bleio_address_type_obj = {
124131 (mp_obj_t )& mp_const_none_obj },
125132};
126133
134+ //| .. method:: __eq__(other)
135+ //|
136+ //| Two Address objects are equal if their addresses and address types are equal.
137+ //|
138+ STATIC mp_obj_t bleio_address_binary_op (mp_binary_op_t op , mp_obj_t lhs_in , mp_obj_t rhs_in ) {
139+ switch (op ) {
140+ // Two Addresses are equal if their address bytes and address_type are equal
141+ case MP_BINARY_OP_EQUAL :
142+ if (MP_OBJ_IS_TYPE (rhs_in , & bleio_address_type )) {
143+ bleio_address_obj_t * lhs = MP_OBJ_TO_PTR (lhs_in );
144+ bleio_address_obj_t * rhs = MP_OBJ_TO_PTR (rhs_in );
145+ return mp_obj_new_bool (
146+ mp_obj_equal (common_hal_bleio_address_get_address_bytes (lhs ),
147+ common_hal_bleio_address_get_address_bytes (rhs )) &&
148+ common_hal_bleio_address_get_type (lhs ) ==
149+ common_hal_bleio_address_get_type (rhs ));
150+
151+ } else {
152+ return mp_const_false ;
153+ }
154+
155+ default :
156+ return MP_OBJ_NULL ; // op not supported
157+ }
158+ }
159+
160+ STATIC void bleio_address_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
161+ bleio_address_obj_t * self = MP_OBJ_TO_PTR (self_in );
162+ mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes (self );
163+
164+ mp_buffer_info_t buf_info ;
165+ mp_get_buffer_raise (address_bytes , & buf_info , MP_BUFFER_READ );
166+ const uint8_t * buf = (uint8_t * ) buf_info .buf ;
167+ mp_printf (print ,
168+ "%02x:%02x:%02x:%02x:%02x:%02x" ,
169+ buf [5 ], buf [4 ], buf [3 ], buf [2 ], buf [1 ], buf [0 ]);
170+ }
171+
127172STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table [] = {
128- { MP_ROM_QSTR (MP_QSTR_address_bytes ), MP_ROM_PTR (& bleio_address_get_address_bytes_obj ) },
129- { MP_ROM_QSTR (MP_QSTR_type ), MP_ROM_PTR (& bleio_address_get_type_obj ) },
173+ { MP_ROM_QSTR (MP_QSTR_address_bytes ), MP_ROM_PTR (& bleio_address_address_bytes_obj ) },
174+ { MP_ROM_QSTR (MP_QSTR_type ), MP_ROM_PTR (& bleio_address_type_obj ) },
130175 // These match the BLE_GAP_ADDR_TYPES values used by the nRF library.
131176 { MP_ROM_QSTR (MP_QSTR_PUBLIC ), MP_OBJ_NEW_SMALL_INT (0 ) },
132177 { MP_ROM_QSTR (MP_QSTR_RANDOM_STATIC ), MP_OBJ_NEW_SMALL_INT (1 ) },
@@ -141,5 +186,7 @@ const mp_obj_type_t bleio_address_type = {
141186 { & mp_type_type },
142187 .name = MP_QSTR_Address ,
143188 .make_new = bleio_address_make_new ,
189+ .print = bleio_address_print ,
190+ .binary_op = bleio_address_binary_op ,
144191 .locals_dict = (mp_obj_dict_t * )& bleio_address_locals_dict
145192};
0 commit comments