def push_message(self, name, file_name='0123', transparent=0, retry=1, charset=1): """Push a message to a folder of the MSE""" logger.info("Requesting push_message with parameters %s", str(locals())) data = { "Transparent": headers.Transparent(transparent), "Retry": headers.Retry(retry), "Charset": headers.Charset(charset) } file_path = sys.path[ 0] + os.sep + 'test' + os.sep + 'data' + os.sep + file_name application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-bt/message")] if application_parameters.data: header_list.append(application_parameters) with open(file_path, 'r') as f: file_data = f.readlines() response = self.put(name, ''.join(file_data), header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error("push bMessage to %s fail'. reason = %s", name, response) return return response logger.error(" Read bMessage data fail") return
def get_messages_listing(self, name, max_list_count=1024, list_startoffset=0, filter_messageType=0, filter_readStatus=0, new_message=0): """Retrieves messages listing object from current folder""" logger.info("Requesting get_messages_listing with parameters %s", str(locals())) data = { "MaxListCount": headers.MaxListCount(max_list_count), "ListStartOffset": headers.ListStartOffset(list_startoffset), "FilterMessageType": headers.FilterMessageType(filter_messageType), "FilterReadStatus": headers.FilterReadStatus(filter_readStatus), "NewMessage": headers.NewMessage(new_message) } application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-bt/MAP-msg-listing")] if application_parameters.data: header_list.append(application_parameters) response = self.get(name, header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error( "get_messages_listing failed for bMessage '%s'. reason = %s", name, response) return return response
def _respond_phonebook_size(self, socket, phonebook_size): # MaxListCount = 0 signifies to the PSE that the PCE wants to know the number of used # indexes in the phone book of interest. # When MaxListCount = 0, the PSE shall ignore all other application parameters that may # be present in the request. The response shall include the PhonebookSize application # parameter (see Section 5.1.4.5). The response shall not contain any Body header logger.debug( "MaxListCount is 0, so responding with PhonebookSize = {}".format( phonebook_size)) response_dict = { 'PhonebookSize': headers.PhonebookSize(phonebook_size) } self.send_response(socket, responses.Success(), [headers.App_Parameters(response_dict)])
def get_folder_listing(self, max_list_count=1024, list_startoffset=0): """Retrieves folders list from current folder""" logger.info("Requesting get_folder_listing with appl parameters %s", str(locals())) data = { "MaxListCount": headers.MaxListCount(max_list_count), "ListStartOffset": headers.ListStartOffset(list_startoffset) } application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-obex/folder-listing")] if application_parameters.data: header_list.append(application_parameters) response = self.get(header_list=header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error("get_folder_listing failed. reason = %s", response) return return response
def set_msg_status(self, name='', status_indicator=1, status_value=''): '''Modify the status of a message on the MSE.''' logger.info("Requesting set_msg_status with parameters %s", str(locals())) data = { "StatusIndicator": headers.StatusIndicator(status_indicator), "StatusValue": headers.StatusValue(status_value) } application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-bt/messageStatus")] if application_parameters.data: header_list.append(application_parameters) response = self.put(name, '01', header_list=header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error( "Modify the status to %s of message %s fail'. reason = %s", name, status_value, response) return return response
def get_message(self, name, attachment=1, charset=1): """Retrieves a specific message from the MSE device""" logger.info("Requesting get_message with parameters %s", str(locals())) data = { "Attachment": headers.Attachment(attachment), "Charset": headers.Charset(charset) } application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-bt/message")] if application_parameters.data: header_list.append(application_parameters) response = self.get(name, header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error( "get_messages_listing failed for bMessage '%s'. reason = %s", name, response) return return response
def _pull_phonebook(self, socket, request, decoded_header): mch_size = 0 # mch_size for phonebook folder (Don't optimize) abs_name = self.vfolder.join(self.vfolder.curdir, decoded_header["Name"]) logger.info("Absolute path of requested phonebook object = %s", abs_name) app_params = self._decode_app_params( decoded_header.get("App_Parameters", {})) if not self.vfolder.isfile(abs_name): logger.error("Requested phonebook file doesn't exists") self.send_response(socket, responses.Not_Found()) else: phonebook_size = self.vfolder.count(os.path.splitext(abs_name)[0]) vcard_list = self.vfolder.listdir(os.path.splitext(abs_name)[0]) if app_params["MaxListCount"] == 0: self._respond_phonebook_size(socket, phonebook_size) return data = "" res_vcard_list = self._limit_phonebook( vcard_list, app_params["MaxListCount"], app_params["ListStartOffset"]) # "NewMissedCalls": This application parameter shall be used in the response when and only when the # phone book object is mch. It indicates the number of missed calls that have been # received on the PSE since the last PullPhoneBook request on the mch folder, at the # point of the request. if "mch" in abs_name: response_dict = { 'NewMissedCalls': headers.NewMissedCalls(phonebook_size - mch_size) } mch_size = phonebook_size else: response_dict = {} for item in res_vcard_list: filtered_data = self._filter_attributes( app_params["Filter"], item, app_params["Format"]) data += VCard(filtered_data, parsed=True).serialize(app_params["Format"]) logger.debug("Sending response success with following data") logger.debug("phonebook data: \r\n%s", data) # Sends the chunked response # TODO: This needs to be handled properly in pyobex: server.py: send_response bytes_transferred = 0 datasize = len(data) # ideally max data length per packet should be as follows # max_datalen = self._max_length() - Message().minimum_length # but because of some unknown reasons we could only able to transmit ~700 bytes # TODO: figure out exactly what is the reason max_datalen = 700 if datasize < max_datalen: data_last_chunk = data else: while bytes_transferred < datasize: data_chunk = data[bytes_transferred:(bytes_transferred + max_datalen)] header_list = [ headers.App_Parameters(response_dict), headers.Body(data_chunk) ] # 'continue' response and process the subsequent requests self.send_response(socket, responses.Continue(), header_list) while True: request = self.request_handler.decode(self.connection) if not isinstance(request, requests.Get_Final): self.process_request(self.connection, request) continue else: break bytes_transferred += max_datalen data_last_chunk = "" header_list = [ headers.App_Parameters(response_dict), headers.End_Of_Body(data_last_chunk) ] self.send_response(socket, responses.Success(), header_list)
def _pull_vcard_listing(self, socket, request, decoded_header): mch_size = 0 # mch_size for phonebook folder (Don't optimize) abs_name = self.vfolder.join(self.vfolder.curdir, decoded_header["Name"]) logger.info("Absolute path of requested vcard_listing object = %s", abs_name) app_params = self._decode_app_params( decoded_header.get("App_Parameters", {})) if not self.vfolder.isdir(abs_name): logger.error("Requested vcard-listing dir doesn't exists") self.send_response(socket, responses.Not_Found()) else: phonebook_size = self.vfolder.count(abs_name) search_query = self._get_search_query( app_params["SearchAttribute"], app_params["SearchValue"]) # TODO: sorting based on order not works, since handle is not part of db record. it is not proper # make the "handle" as part of db record sort_key = self._get_sort_key(app_params["Order"]) vcard_list = self.vfolder.listdir(abs_name, query=search_query) vcard_list = self._sort_vcard_list(vcard_list, sort_key) if app_params["MaxListCount"] == 0: self._respond_phonebook_size(socket, phonebook_size) return data = "" res_vcard_list = self._limit_phonebook( vcard_list, app_params["MaxListCount"], app_params["ListStartOffset"]) res_vcard_list_range = range( app_params["ListStartOffset"], min((app_params["ListStartOffset"] + app_params["MaxListCount"]), 65535)) # "NewMissedCalls": This application parameter shall be used in the response when and only when the # phone book object is mch. It indicates the number of missed calls that have been # received on the PSE since the last PullPhoneBook request on the mch folder, at the # point of the request. if "mch" in abs_name: response_dict = { 'NewMissedCalls': headers.NewMissedCalls(phonebook_size - mch_size) } mch_size = phonebook_size else: response_dict = {} vcard_listing_object_tmpl = ( '<?xml version="1.0"?>\r\n' '<!DOCTYPE vcard-listing SYSTEM "vcard-listing.dtd">\r\n' '<vCard-listing version="1.0">\r\n' '{cards}' '</vCard-listing>\r\n') card_tag_tmpl = '<card handle="{handle}" name="{name}"/>\r\n' cards = "" # TODO: As per spec the handles should be hex?? for index, vcard in zip(res_vcard_list_range, res_vcard_list): cards += card_tag_tmpl.format(handle="{}.vcf".format(index), name=self._get_param_values( vcard, "N")) data = vcard_listing_object_tmpl.format(cards=cards) logger.debug("Sending response success with following data") logger.debug("vcard-listing data: \r\n%s", data) self.send_response(socket, responses.Success(), [ headers.End_Of_Body(data), headers.App_Parameters(response_dict) ])