Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Feature/ble gap device name set #189

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
91 changes: 89 additions & 2 deletions pc_ble_driver_py/ble_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,63 @@ def to_c(self):

return scan_params

class BLEGapConnSecMode(object):
def __init__(self, sm=0, lv=0):
self.sm = sm
self.lv = lv

def set_no_access(self):
"""BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS"""
self.sm = 0
self.lv = 0

def set_open(self):
"""BLE_GAP_CONN_SEC_MODE_SET_OPEN"""
self.sm = 1
self.lv = 1

def set_enc_no_mitm(self):
"""BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM"""
self.sm = 1
self.lv = 2

def set_enc_with_mitm(self):
"""BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM"""
self.sm = 1
self.lv = 3

def set_lesc_enc_with_mitm(self):
"""BLE_GAP_CONN_SEC_MODE_SET_LESC_ENC_WITH_MITM"""
self.sm = 1
self.lv = 4

def set_signed_no_mitm(self):
"""BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM"""
self.sm = 2
self.lv = 1
def set_signed_with_mitm(self):
"""BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM"""
self.sm = 2
self.lv = 2

@classmethod
def from_c(cls, sec_mode):
return cls(
sm=sec_mode.sm,
lv=sec_mode.lv,
)

def to_c(self):
sec_mode = driver.ble_gap_conn_sec_mode_t()
sec_mode.sm = self.sm
sec_mode.lv = self.lv

return sec_mode

def __str__(self):
return "sm({0.ssm}) lv({0.lv}))".format(
self
)

class BLEGapConnSec(object):
def __init__(self, sec_mode, level, encr_key_size):
Expand Down Expand Up @@ -1289,15 +1346,22 @@ def to_c(self):

class BLEGattsAttrMD(object):
def __init__(self, vloc=driver.BLE_GATTS_VLOC_STACK,
rd_auth=False, wr_auth=False, vlen=1):
rd_auth=False, wr_auth=False,
read_perm=None, write_perm=None, vlen=1):
self.vloc = vloc
self.rd_auth = rd_auth
self.wr_auth = wr_auth
self.read_perm = read_perm
self.write_perm = write_perm
self.vlen = vlen

def to_c(self):
attr_md = driver.ble_gatts_attr_md_t()
attr_md.vloc = self.vloc
if self.read_perm:
attr_md.read_perm = self.read_perm.to_c()
if self.write_perm:
attr_md.write_perm = self.write_perm.to_c()
attr_md.rd_auth = int(self.rd_auth)
attr_md.wr_auth = int(self.wr_auth)
attr_md.vlen = self.vlen
Expand Down Expand Up @@ -1387,8 +1451,11 @@ def to_c(self):
char_md = driver.ble_gatts_char_md_t()
char_md.char_props = self.char_props.to_c()
if self.user_desc:
char_md.p_char_user_desc = util.list_to_char_array(self.user_desc)
user_desc_array=util.list_to_uint8_array(self.user_desc)
user_desc_array_cast = user_desc_array.cast()
char_md.p_char_user_desc = user_desc_array_cast
char_md.char_user_desc_size = len(self.user_desc)
char_md.char_user_desc_max_size = len(self.user_desc)
if self.pf:
char_md.p_char_pf = self.pf.to_c()
if self.desc_md:
Expand Down Expand Up @@ -2211,6 +2278,26 @@ def ble_gap_adv_data_set(self, adv_data=BLEAdvData(), scan_data=BLEAdvData()):
return driver.sd_ble_gap_adv_data_set(
self.rpc_adapter, p_adv_data, adv_data_len, p_scan_data, scan_data_len
)

@NordicSemiErrorCheck
@wrapt.synchronized(api_lock)
def ble_gap_device_name_set(self, name, device_name_read_only=True):
write_perm=BLEGapConnSecMode()
if device_name_read_only:
write_perm.sm = 0
write_perm.lv = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be .set_no_access()

else:
write_perm.sm = 1
write_perm.lv = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_perm.set_open()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! Now using the BLEGapConnSecMode methods for setting write permissions for the name.

Also trying to write a test for ble_gap_device_name_set()

p_write_perm = write_perm.to_c()
p_dev_name = name
p_dev_name = util.list_to_uint8_array([ord(x) for x in name])
p_dev_name_cast = p_dev_name.cast()
name_length = len(name)

return driver.sd_ble_gap_device_name_set(
self.rpc_adapter, p_write_perm, p_dev_name_cast, name_length
)

@NordicSemiErrorCheck
@wrapt.synchronized(api_lock)
Expand Down