def run(self, getnames, duration): if self._inquiring: raise _lightbluecommon.BluetoothError( "Another inquiry in progress") # set inquiry attributes self._inquiry.updatenames = getnames self._inquiry.length = duration # start the inquiry err = self._inquiry.start() if err != _macutil.kIOReturnSuccess: raise _lightbluecommon.BluetoothError( err, "Error starting device inquiry") # if error occurs during inquiry, set _inquiryerr to the error code self._inquiryerr = _macutil.kIOReturnSuccess # wait until the inquiry is complete self._inquiring = True _macutil.waituntil(lambda: not self._inquiring) # if error occured during inquiry, raise exception if self._inquiryerr != _macutil.kIOReturnSuccess: raise _lightbluecommon.BluetoothError(self._inquiryerr, "Error during device inquiry")
def connect(self, headers={}): if self.__client is not None: raise OBEXError(_kOBEXSessionAlreadyConnectedError, "session is already connected") if not BBLocalDevice.isPoweredOn(): raise OBEXError(_kOBEXSessionNoTransportError, "Bluetooth device not available") self.__delegate = _BBOBEXClientDelegate.alloc().initWithCallback_( self._finishedrequest) self.__client = BBBluetoothOBEXClient.alloc().initWithRemoteDeviceAddress_channelID_delegate_( _macutil.btaddrtochars(self.__serveraddr[0]), self.__serveraddr[1], self.__delegate) if self.__obexsession is not None: self.__client.performSelector_withObject_("setOBEXSession:", self.__obexsession) self.__reset() headerset = _headersdicttoset(headers) r = self.__client.sendConnectRequestWithHeaders_(headerset) if r != _kOBEXSuccess: self.__closetransport() raise OBEXError(r, "error starting Connect request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: self.__closetransport() raise OBEXError(self.__error, "error during Connect request (%s)" % errdesc(self.__error)) resp = self.__getresponse() if resp.code != _obexcommon.OK: self.__closetransport() return resp
def connect(self, headers={}): if self.__client is None: if not BBLocalDevice.isPoweredOn(): raise OBEXError(_kOBEXSessionNoTransportError, "Bluetooth device not available") self.__delegate = _BBOBEXClientDelegate.alloc().initWithCallback_( self._finishedrequest) self.__client = BBBluetoothOBEXClient.alloc( ).initWithRemoteDeviceAddress_channelID_delegate_( _macutil.createbtdevaddr(self.__serveraddr[0]), self.__serveraddr[1], self.__delegate) if self.__obexsession is not None: self.__client.performSelector_withObject_( "setOBEXSession:", self.__obexsession) self.__reset() headerset = _headersdicttoset(headers) r = self.__client.sendConnectRequestWithHeaders_(headerset) if r != _kOBEXSuccess: self.__closetransport() raise OBEXError(r, "error starting Connect request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: self.__closetransport() raise OBEXError( self.__error, "error during Connect request (%s)" % errdesc(self.__error)) resp = self.__getresponse() if resp.code != _obexcommon.OK: self.__closetransport() return resp
def setpath(self, headers, cdtoparent=False, createdirs=False): self.__checkconnected() self.__reset() headerset = _headersdicttoset(headers) r = self.__client.sendSetPathRequestWithHeaders_changeToParentDirectoryFirst_createDirectoriesIfNeeded_(headerset, cdtoparent, createdirs) if r != _kOBEXSuccess: raise OBEXError(r, "error starting SetPath request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: raise OBEXError(self.__error, "error during SetPath request (%s)" % errdesc(self.__error)) return self.__getresponse()
def delete(self, headers): self.__checkconnected() self.__reset() headerset = _headersdicttoset(headers) r = self.__client.sendPutRequestWithHeaders_readFromStream_(headerset, None) if r != _kOBEXSuccess: raise OBEXError(r, "error starting Delete request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: raise OBEXError(self.__error, "error during Delete request (%s)" % errdesc(self.__error)) return self.__getresponse()
def disconnect(self, headers={}): self.__checkconnected() self.__reset() try: headerset = _headersdicttoset(headers) r = self.__client.sendDisconnectRequestWithHeaders_(headerset) if r != _kOBEXSuccess: raise OBEXError(r, "error starting Disconnect request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: raise OBEXError(self.__error, "error during Disconnect request (%s)" % errdesc(self.__error)) finally: # close channel regardless of disconnect result self.__closetransport() return self.__getresponse()
def disconnect(self, headers={}): self.__checkconnected() self.__reset() try: headerset = _headersdicttoset(headers) r = self.__client.sendDisconnectRequestWithHeaders_(headerset) if r != _kOBEXSuccess: raise OBEXError( r, "error starting Disconnect request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: raise OBEXError( self.__error, "error during Disconnect request (%s)" % errdesc(self.__error)) finally: # close channel regardless of disconnect result self.__closetransport() return self.__getresponse()
def get(self, headers, fileobj): if not hasattr(fileobj, "write"): raise TypeError("file-like object must have write() method") self.__checkconnected() self.__reset() headerset = _headersdicttoset(headers) delegate = _macutil.BBFileLikeObjectWriter.alloc().initWithFileLikeObject_(fileobj) outstream = BBStreamingOutputStream.alloc().initWithDelegate_(delegate) outstream.open() r = self.__client.sendGetRequestWithHeaders_writeToStream_( headerset, outstream) if r != _kOBEXSuccess: raise OBEXError(r, "error starting Get request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: raise OBEXError(self.__error, "error during Get request (%s)" % errdesc(self.__error)) return self.__getresponse()
def put(self, headers, fileobj): if not hasattr(fileobj, "read"): raise TypeError("file-like object must have read() method") self.__checkconnected() self.__reset() headerset = _headersdicttoset(headers) self.fileobj = fileobj self.__fileobjdelegate = _macutil.BBFileLikeObjectReader.alloc().initWithFileLikeObject_(fileobj) self.instream = BBStreamingInputStream.alloc().initWithDelegate_(self.__fileobjdelegate) self.instream.open() r = self.__client.sendPutRequestWithHeaders_readFromStream_( headerset, self.instream) if r != _kOBEXSuccess: raise OBEXError(r, "error starting Put request (%s)" % errdesc(r)) _macutil.waituntil(self._done) if self.__error != _kOBEXSuccess: raise OBEXError(self.__error, "error during Put request (%s)" % errdesc(self.__error)) return self.__getresponse()
def run(self): self.__server.run() # wait until client sends a file, or an error occurs _macutil.waituntil(lambda: self.__gotfile or self.__error is not None) # wait briefly for a disconnect request (client may have decided to just # close the connection without sending a disconnect request) if self.__error is None: ok = _macutil.waituntil(lambda: self.__gotdisconnect, 3) if ok: _macutil.waituntil(lambda: self.__disconnected) # only raise OBEXError if file was not received if not self.__gotfile: if self.__error is not None: raise OBEXError(self.__error[0], self.__error[1]) # if client connected but didn't send PUT raise OBEXError(_kOBEXGeneralError, "client did not send a file")
def query(self, device, timeout=10.0): # do SDP query err = device.performSDPQuery_(self) if err != _macutil.kIOReturnSuccess: raise _lightbluecommon.BluetoothError(err, self._errmsg(device)) # performSDPQuery_ is async, so block-wait self._queryresult = None if not _macutil.waituntil(lambda: self._queryresult is not None, timeout): raise _lightbluecommon.BluetoothError("Timed out getting services for %s" % device.getNameOrAddress()) # query is now complete if self._queryresult != _macutil.kIOReturnSuccess: raise _lightbluecommon.BluetoothError(self._queryresult, self._errmsg(device))
def start(self): result = self.pair.start() if result != _macutil.kIOReturnSuccess: return (None, result) self.pairResult = None self.error = None timeout = 10.0 if not _macutil.waituntil(lambda: self.pairResult is not None or self.error is not None, timeout): self.error = -99 print "Timed out pairing with %s" % self.pair.device().addressString() self.pair.setDelegate_(None) self.pair = None self.device = None return (self.pairResult, self.error)
def query(self, device, timeout=10.0): # do SDP query err = device.performSDPQuery_(self) if err != _macutil.kIOReturnSuccess: raise _lightbluecommon.BluetoothError(err, self._errmsg(device)) # performSDPQuery_ is async, so block-wait self._queryresult = None if not _macutil.waituntil(lambda: self._queryresult is not None, timeout): raise _lightbluecommon.BluetoothError( "Timed out getting services for %s" % \ device.getNameOrAddress()) # query is now complete if self._queryresult != _macutil.kIOReturnSuccess: raise _lightbluecommon.BluetoothError( self._queryresult, self._errmsg(device))
def __waituntil(self, stopwaiting, timeoutmsg): """ Waits until stopwaiting() returns True, or until the wait times out (according to the self.__timeout value). This is to make a function wait until a buffer has been filled. i.e. stopwaiting() should return True when the buffer is no longer empty. """ if not stopwaiting(): if self.__timeout == 0: # in non-blocking mode (immediate timeout) # push event loop to really be sure there is no data available _macutil.looponce() if not stopwaiting(): # trying to perform operation now would block raise _socket.error(errno.EAGAIN, os.strerror(errno.EAGAIN)) else: # block and wait until we get data, or time out if not _macutil.waituntil(stopwaiting, self.__timeout): raise _socket.timeout(timeoutmsg)
# Copyright (c) 2009 Bea Lam. All rights reserved.