def ATTWriteReqHandler(self, dictionary, func): ''' handle write req, Limitation: no signature and command type checking ''' def _sendATTWriteRsp(conn_handle): print("COMMAND: ATT_WriteRsp") print(utils.printOutput(self.ble_builder.send("fd13", conn_handle = conn_handle))) def _handleIndication(conn_handle, handle, start_handle, end_handle): #to-do refactor for better usability print("COMMAND: ATT_HandleValueIndication") handle = handle value = start_handle+end_handle # print(utils.printOutput(self.ble_builder.send("fd1d", conn_handle = conn_handle, handle = handle, value = value))) conn_handle = dictionary['conn_handle'][0] handle = dictionary['handle'] value = dictionary['value'] value_str = value[1] handle_raw = handle[0] handle_str = handle[1] _sendATTWriteRsp(conn_handle) #get data from gatt table if handle_str < self.preserved_handle_eom: #attampt to write preserved services if handle_str == "0009" and value_str == "0002": #ask for service changes, tempararily fix to return non-reserved handles start_handle = utils.stringToSByteInHex(utils.stringMinusInHex(self.preserved_handle_eom, -1))[::-1] end_handle = utils.stringToSByteInHex(self.handles[-1])[::-1] char_handle = utils.hexMinusInHex(handle_raw, 1) _handleIndication(conn_handle, char_handle, start_handle, end_handle) else: #external services, tempararily fix to trigger call notification call back or do nothing if value_str == "0001": #Notification sql = sqls.sql_select_uuid_by_handle % utils.stringMinusInHex(handle_str, 1) data = self._execute_sql(sql) uuid = data[0][0] char_handle = utils.hexMinusInHex(handle_raw, 1) func(uuid, char_handle, conn_handle)
def _get_pdu( data): eof = "\xff\xff\xff\xff" pdu = "" if data: try: for i in range(len(data)): if data[i][0] == "0014": pass temp_start = utils.stringToSByteInHex(data[i][0])[::-1] #start handle temp_value = utils.stringToSByteInHex(data[i][1].replace(":","")) #value #get end handle if i + 1 > range(len(data))[-1]: temp_end = utils.stringToSByteInHex("001b")[::-1] #this is the last service, end handle is the last one of all handles else: temp_end = utils.stringToSByteInHex(utils.stringMinusInHex(data[i+1][0],1))[::-1] #this is not the last service then the end handle is the handle of next service minus one if len(temp_value) == 16: #this is 128bit uuid, then only return one service info pdu = temp_start + temp_end + temp_value break else: if len(pdu) + 4 + len(temp_value) > 12 : #if total pdu exceeds the maxmium length, do not append, only two services returned each time break elif i + 1 <= range(len(data))[-1]: next_value = utils.stringToSByteInHex(data[i+1][1].replace(":","")) if len(next_value) == 16: if pdu: pdu = pdu + temp_start + temp_end + temp_value break else: pdu = temp_start + temp_end + temp_value break else: pdu = pdu + temp_start + temp_end + temp_value else: pdu = pdu + temp_start + temp_end + temp_value #append pdu except Exception, e: print e pdu = eof