def __init__(self, bdaddr, port = 2): self.s = BluetoothSocket() self.s.connect((bdaddr, port)) self.GPIOB = PyIOGPIO(self, GPIO_PORTB) self.GPIOC = PyIOGPIO(self, GPIO_PORTC) self.GPIOD = PyIOGPIO(self, GPIO_PORTD)
def reconnect(self): self.s = BluetoothSocket() self.s.connect((self.addr, self.port)) # handle "connected" packet pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_CONNECTED: raise Exception('Unexpected packet %s' % pkt) self.firmware = pkt.payload
class I2CM(object): def __init__(self, bdaddr, port=2): self.s = BluetoothSocket() self.s.connect((bdaddr, port)) def read(self, addr, reg): addr = addr | 1 self.s.send(chr(addr) + chr(reg)) return ord(self.s.recv(1)) def write(self, addr, reg, val): addr = addr & 0xfe self.s.send(chr(addr) + chr(reg) + chr(val)) def close(self): self.s.close()
class PyIO(object): def __init__(self, bdaddr, port = 2): self.s = BluetoothSocket() self.s.connect((bdaddr, port)) self.GPIOB = PyIOGPIO(self, GPIO_PORTB) self.GPIOC = PyIOGPIO(self, GPIO_PORTC) self.GPIOD = PyIOGPIO(self, GPIO_PORTD) def __cmd__(self, op, arg = 0x00, data = 0x00, read = False): if read: self.s.send(chr((op << 3)|arg)) return ord(self.s.recv(1)) else: self.s.send(chr(0x80|(op << 3)|arg)+chr(data)) def close(self): self.s.close()
def connect(self): """ this function is used to establish connection with the wiimote return None if cannot be done """ c_sock = BluetoothSocket(L2CAP) r_sock = BluetoothSocket(L2CAP) try: c_sock.connect((self.mac, 0x11)) # 0x11 : control channel r_sock.connect((self.mac, 0x13)) # 0x13 : reception channel r_sock.settimeout(self.cfg.SOCK_TIMEOUT_DURATION) except BluetoothError, be: self.logger.error("Failed connecting: %s" % be) c_sock.close() r_sock.close() c_sock = None r_sock = None
def __init__(self, bdaddr='00:80:37:14:49:42', port=3): self.s = BluetoothSocket() self.s.connect((bdaddr, port))
class NolleDriver: def __init__(self, bdaddr='00:80:37:14:49:42', port=3): self.s = BluetoothSocket() self.s.connect((bdaddr, port)) """ Perform a hard reset on the target and either enter the BSL or normal execution """ def reset(self, enter_bsl=False): if enter_bsl: self.s.send('b') else: self.s.send('x') r = self.s.recv(1) if r != 'k': raise Exception('Unexpected response') """ Write data to the BSL on the serial port """ def write(self, data): while len(data) > 0: to_send = min(255, len(data)) d = data[:to_send] data = data[to_send:] self.s.send('w' + chr(len(d)) + d) r = self.s.recv(1) if r != 'k': raise Exception('Unexpected response') """ Perform a blocking read on the serial port from the BSL """ def read(self, length): # TODO: We should really have some timeout here... buf = '' while len(buf) < length: self.s.send('r' + chr(length - len(buf))) l = ord(self.s.recv(1)) if l > 0: buf = buf + self.s.recv(l) time.sleep(0) return buf """ Read the calibration block from IRMA """ def read_calibration(self): self.s.send('c?') r = self.s.recv(1) if r == 'k': return self.s.recv(10) elif r == 'n': return None else: raise Exception('Unexpected response') """ Write or delete the calibration block on IRMA """ def write_calibration(self, cal): if cal is None: # Delete calibration self.s.send('cx') else: if len(cal) != 10: raise Exception('Wrong length of calibration block') self.s.send('c' + cal) if self.s.recv(1) != 'k': raise Exception('Unexpected response')
def __init__(self, bdaddr, port=2): self.s = BluetoothSocket() self.s.connect((bdaddr, port))
#!/usr/bin/python import sys import time from random import random from bluetooth.bluez import BluetoothSocket from bluetooth.btcommon import BluetoothError addr = '00:80:37:14:49:42' port = 2 print '*** Connecting to %s-%d...' % (addr, port), sys.stdout.flush() try: s = BluetoothSocket() s.connect((addr, port)) except BluetoothError as e: print 'failed: %s' % e sys.exit(1) print 'OK!\n' idx = 0 while True: frame = [] for i in xrange(15): frame = frame + [int(random() * 256)] frame = ''.join(map(chr, map(lambda x: x | 0x80, frame))) s.send('b' + frame + '\n') time.sleep(.05)
class Monitor(object): RAMONA_UUID = "ea5cf8a3-833d-4d61-bfec-9cf6f4230ac4" def find_devices(self): return find_service(uuid=self.RAMONA_UUID) def connect(self, addr, port=1): (self.addr, self.port) = (addr, port) self.s = BluetoothSocket() self.s.connect((addr, port)) # handle "connected" packet pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_CONNECTED: raise Exception('Unexpected packet %s' % pkt) self.firmware = pkt.payload.split("\0") def reconnect(self): self.s = BluetoothSocket() self.s.connect((self.addr, self.port)) # handle "connected" packet pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_CONNECTED: raise Exception('Unexpected packet %s' % pkt) self.firmware = pkt.payload def read_mem(self, addr, length): data = '' while len(data) < length: to_read = min(length, 64) pkt = MonitorPacket() pkt.cmd = pkt.MON_READMEM pkt.payload = struct.pack('<II', addr + len(data), to_read) pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_READMEM: print 'Unexpected packet ', pkt sys.exit(1) data = data + pkt.payload[8:] return map(ord, data) def write_mem(self, addr, data): pkt = MonitorPacket() pkt.cmd = pkt.MON_WRITEMEM pkt.payload = struct.pack('<II', addr, len(data)) + data pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_WRITEMEM: print 'Unexpected packet ', pkt sys.exit(1) def write_flash(self, addr, data): pkt = MonitorPacket() pkt.cmd = pkt.MON_WRITEFLASH pkt.payload = struct.pack('<II', addr, len(data)) + data pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_WRITEFLASH: print 'Unexpected packet ', pkt sys.exit(1) def erase_flash(self, addr, length): pkt = MonitorPacket() pkt.cmd = pkt.MON_ERASEFLASH pkt.payload = struct.pack('<II', addr, length) pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_ERASEFLASH: print 'Unexpected packet ', pkt sys.exit(1) def reboot(self): pkt = MonitorPacket() pkt.cmd = pkt.MON_REBOOT pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_REBOOT: print 'Unexpected packet ', pkt sys.exit(1) self.s.close() def read_reg(self, addr): pkt = MonitorPacket() pkt.cmd = pkt.MON_READREG pkt.payload = struct.pack('<I', addr) pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_READREG: print 'Unexpected packet ', pkt sys.exit(1) (addr, data) = struct.unpack('<II', pkt.payload) return data def write_reg(self, addr, data): pkt = MonitorPacket() pkt.cmd = pkt.MON_WRITEREG pkt.payload = struct.pack('<II', addr, data) pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_WRITEREG: print 'Unexpected packet ', pkt sys.exit(1) def call(self, addr, r0=0, r1=0, r2=0, r3=0): pkt = MonitorPacket() pkt.cmd = pkt.MON_CALL pkt.payload = struct.pack('<IIIII', addr, r0, r1, r2, r3) pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_CALL: print 'Unexpected packet ', pkt sys.exit(1) (addr, r0) = struct.unpack('<II', pkt.payload) return r0 def plugin(self, state): pkt = MonitorPacket() pkt.cmd = pkt.MON_PLUGIN pkt.payload = struct.pack('<I', state) pkt.to_socket(self.s) pkt = MonitorPacket() pkt.from_socket(self.s) if pkt.cmd != pkt.MON_PLUGIN: print 'Unexpected packet ', pkt sys.exit(1) (state, ) = struct.unpack('<I', pkt.payload) return state
def connect(self): """ this function is used to establish connection with the wiimote return None if cannot be done """ c_sock = BluetoothSocket(L2CAP) r_sock = BluetoothSocket(L2CAP) try : c_sock.connect((self.mac, 0x11)) # 0x11 : control channel r_sock.connect((self.mac, 0x13)) # 0x13 : reception channel r_sock.settimeout(self.cfg.SOCK_TIMEOUT_DURATION) except BluetoothError, be: self.logger.error("Failed connecting: %s" % be) c_sock.close() r_sock.close() c_sock = None r_sock = None
#!/usr/bin/python import sys import select import tty import termios from bluetooth.bluez import BluetoothSocket s = BluetoothSocket() s.connect(('00:80:37:14:49:AB', 1)) old_settings = termios.tcgetattr(sys.stdin) try: tty.setcbreak(sys.stdin.fileno()) while True: (inp, _, _) = select.select([sys.stdin, s], [], []) # Something from the serial port? if s in inp: d = s.recv(1) sys.stdout.write(d) sys.stdout.flush() # Something from the local terminal? if sys.stdin in inp: d = sys.stdin.read(1) s.send(d) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)