def test_stop_before_start(self): def read_func(): pass def write_func(): pass hdlc_c = HDLController(read_func, write_func) hdlc_c.stop()
def test_send_frame_and_receive_nack(self): def read_func(): return frame_data('', FRAME_NACK, 0) def write_func(data): write_func.data = data write_func.data = None hdlc_c = HDLController(read_func, write_func) hdlc_c.send('test') while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test', FRAME_DATA, 0)) self.assertEqual(hdlc_c.get_senders_number(), 1) hdlc_c.start() sleep(1) self.assertEqual(hdlc_c.get_senders_number(), 1) hdlc_c.stop()
def test_receive_three_frames(self): def read_func(): data = frame_data('test' + str(read_func.i), FRAME_DATA, read_func.i) read_func.i += 1 return data def write_func(data): pass read_func.i = 1 hdlc_c = HDLController(read_func, write_func) hdlc_c.start() self.assertEqual(hdlc_c.get_data(), b'test1') self.assertEqual(hdlc_c.get_data(), b'test2') self.assertEqual(hdlc_c.get_data(), b'test3') hdlc_c.stop()
def test_receive_one_frame(self): def read_func(): return frame_data('test', FRAME_DATA, 0) def write_func(data): write_func.data = data write_func.data = None hdlc_c = HDLController(read_func, write_func) hdlc_c.start() self.assertEqual(hdlc_c.get_data(), b'test') self.assertEqual(write_func.data, frame_data('', FRAME_ACK, 1)) hdlc_c.stop()
def test_send_one_frame(self): def read_func(): pass def write_func(data): write_func.data = data write_func.data = None hdlc_c = HDLController(read_func, write_func) hdlc_c.send('test') while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test', FRAME_DATA, 0)) self.assertEqual(hdlc_c.get_senders_number(), 1) hdlc_c.stop()
def test_send_three_frames_and_wait_timeout(self): def read_func(): pass def write_func(data): write_func.data = data write_func.data = None hdlc_c = HDLController(read_func, write_func, sending_timeout=5) hdlc_c.send('test1') while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test1', FRAME_DATA, 0)) self.assertEqual(hdlc_c.get_senders_number(), 1) write_func.data = None sleep(1) hdlc_c.send('test2') while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test2', FRAME_DATA, 1)) self.assertEqual(hdlc_c.get_senders_number(), 2) write_func.data = None sleep(1) hdlc_c.send('test3') while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test3', FRAME_DATA, 2)) self.assertEqual(hdlc_c.get_senders_number(), 3) write_func.data = None while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test1', FRAME_DATA, 0)) self.assertEqual(hdlc_c.get_senders_number(), 3) write_func.data = None while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test2', FRAME_DATA, 1)) self.assertEqual(hdlc_c.get_senders_number(), 3) write_func.data = None while write_func.data == None: pass self.assertEqual(write_func.data, frame_data('test3', FRAME_DATA, 2)) self.assertEqual(hdlc_c.get_senders_number(), 3) hdlc_c.stop()
# Serial port configuration ser = serial.Serial() ser.port = getenv('LAYS_SERIAL_PORT', '/dev/ttyACM0') ser.baudrate = int(getenv('LAYS_SERIAL_BAUDRATE', '9600')) ser.timeout = int(getenv('LAYS_SERIAL_TIMEOUT', '0')) # Device resources device_resources = dict() device_resources['resources'] = list() def read_serial(): return ser.read(ser.inWaiting()) # HDLC controller hdlc_c = HDLController(read_serial, ser.write) def device_request(ch, method, properties, body): message = json.loads(body.decode()) try: address = message['address'] action = message['action'] if action == 'read': stdout.write('[*] New read request @{0}\n'.format(address)) elif action == 'write': value = message['value'] stdout.write('[*] New write request @{0} : {1}\n'.format(address, value)) except KeyError: stderr.write('[x] Bad request :\n\t=> {0}\n'.format(message))