Exemple #1
0
    def test_get_variable_width_object_from_serial_port(self):
        pn = MockSerialPortNode()

        #pn.debug = 5

        # Initialize some data to be 'read' from our mock serial port node.
        for x in chr(0x03) + chr(0xAA) + chr(0x03) + chr(0xFF):
            pn.addbuffer(ord(x))

        #print pn.get_buffer_str()
        
        conn = gc.FrameworkSerialPortWrapper(pn)

        lines = "" \
                "# Variable Buffer 1 Class\n" \
                "class testvbuffer1 {\n" \
                "   vbuffer1 vbuffer01;\n" \
                "}\n"


        lines = lines.split('\n')

        pc = ProtoCompiler()

        pc.parseLines(lines)

        c = pc.emitCode()

        pyfilename = os.path.join(properties.TEMP_DIR, "testvbuffer1.py")

        f = open(pyfilename, 'w')
        f.write(c)
        f.close()

        # Now try to import it.
        import testvbuffer1

        request_obj = testvbuffer1.testvbuffer1()
        response_obj = testvbuffer1.testvbuffer1()

        req_vbuffer_obj = request_obj.findChildByName('vbuffer01')
        #
        resp_vbuffer_obj = response_obj.findChildByName('vbuffer01')
        
        lh = gdlh.SimpleLineHandler(conn)

        req_vbuffer_obj.setValue("abc")

        res = lh.send_request_with_response(request_obj, response_obj, 30)

        assert res == 1, "send_request_with_response() should have returned 1."

        assert resp_vbuffer_obj.getValue() == chr(0xAA) + chr(0x03) + chr(0xFF), "Did not get expected value."
        
        os.remove(pyfilename)
Exemple #2
0
    def test_get_fixed_width_object_from_serial_port(self):
        pn = MockSerialPortNode()

        #pn.debug = 5

        # Initialize some data to be 'read' from our mock serial port node.
        for x in chr(0xAA) + chr(0x03) + chr(0xFF):
            pn.addbuffer(ord(x))

        #print pn.get_buffer_str()
        
        conn = gc.FrameworkSerialPortWrapper(pn)

        lines = "" \
                "# Single byte Class\n" \
                "class singlebyte {\n" \
                "   uint8 preamble = 0xAA;\n" \
                "   uint8 data;\n" \
                "   uint8 postamble = 0xFF;\n" \
                "}\n"

        lines = lines.split('\n')

        pc = ProtoCompiler()

        pc.parseLines(lines)

        c = pc.emitCode()

        pyfilename = os.path.join(properties.TEMP_DIR, "singlebyte.py")

        f = open(pyfilename, 'w')
        f.write(c)
        f.close()

        # Now try to import it.
        import singlebyte

        request_obj = singlebyte.singlebyte()
        response_obj = singlebyte.singlebyte()

        req_data_obj = request_obj.findChildByName('data')
        #
        resp_data_obj = response_obj.findChildByName('data')
        
        lh = gdlh.SimpleLineHandler(conn)

        req_data_obj.setValue(0x01)

        res = lh.send_request_with_response(request_obj, response_obj, 30)

        #print res
        
        os.remove(pyfilename)
Exemple #3
0
    def _test_case_dbuffer(self):
        lines = "" \
                "# Dynamic Buffer Class\n" \
                "class testdbuffer {\n" \
                "   dbuffer dbuffer01;\n" \
                "}\n"

        pc = ProtoCompiler()

        x = protocompiler.DynamicBufferItem('dbuffer', 'mydbuffer', None)

        fname = os.path.join(properties.TEMP_DIR, 'dbuffer.class')

        f = open(fname, 'w')
        f.write(lines)
        f.close()

        #print lines

        pc.parseFile(fname)

        assert pc.classnames == ['testdbuffer'
                                 ], "Classnames != ['testdbuffer']"

        my_class = pc._getClass('testdbuffer')

        #print 'Got class of %s.' % str(my_class)

        assert my_class.isPackCompatible(
        ) == 0, "Class should be not pack compatible."
        assert my_class.isFixedWidth() == 0, "Width should not be fixed."
        assert my_class.getWidth(
        ) == 0, "Width should be 0, got %d." % my_class.getWidth()

        c = pc.emitCode()

        #print "Got code of:"
        #print c

        pyfilename = os.path.join(properties.TEMP_DIR, "testdbuffer.py")

        f = open(pyfilename, 'w')
        f.write(c)
        f.close()

        # Now try to import it.
        import testdbuffer

        obj = testdbuffer.testdbuffer()

        #print 'Got object of: %s' % str(obj)
        #print 'Dir of new object: %s' % str(dir(obj))

        assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0"
        assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0"
        assert obj.getWidth(
        ) == 0, "Width should be 0, got %d" % obj.getWidth()
        assert obj.getNumItems(
        ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()

        # Object has only one item, run some tests against that item
        item_obj = obj.items[0]
        #print 'str', item_obj
        #print 'dir', dir (item_obj)

        #width = item_obj.getWidth()
        #assert width == 2, "width should be 2, got %s" % str(width)

        os.remove(pyfilename)
Exemple #4
0
    def test_case_vbuffer1(self):
        lines = "" \
                "# Variable Buffer 1 Class\n" \
                "class testvbuffer1 {\n" \
                "   vbuffer1 vbuffer01;\n" \
                "}\n"

        pc = ProtoCompiler()

        fname = os.path.join(properties.TEMP_DIR, 'vbuffer1.class')

        f = open(fname, 'w')
        f.write(lines)
        f.close()

        pc.parseFile(fname)

        assert pc.classnames == ['testvbuffer1'
                                 ], "Classnames != ['testvbuffer1']"

        my_class = pc._getClass('testvbuffer1')

        #print 'Got class of %s.' % str(my_class)

        assert my_class.isPackCompatible(
        ) == 0, "Class should be not pack compatible."
        assert my_class.isFixedWidth() == 0, "Width should not be fixed."
        assert my_class.getWidth(
        ) == 0, "Width should be 0, got %d." % my_class.getWidth()

        c = pc.emitCode()

        pyfilename = os.path.join(properties.TEMP_DIR, "testvbuffer1.py")

        f = open(pyfilename, 'w')
        f.write(c)
        f.close()

        pyfilename = os.path.join('/tmp', "testvbuffer1.py")

        f = open(pyfilename, 'w')
        f.write(c)
        f.close()

        # Now try to import it.
        import testvbuffer1

        obj = testvbuffer1.testvbuffer1()

        assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0"
        assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0"
        assert obj.getWidth(
        ) == 0, "Width should be 0, got %d" % obj.getWidth()
        assert obj.getNumItems(
        ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()

        # Object has only one item, run some tests against that item
        item_obj = obj.items[0]

        width = item_obj.getWidth()
        assert width == 1, "width should be 1, got %s" % str(width)
Exemple #5
0
    def test_case_allints(self):
        lines = "" \
                "# All Ints Class\n" \
                "class allints {\n" \
                "   uint8    data01;\n" \
                "   beuint8  data02;\n" \
                "   int8     data03;\n" \
                "   uint8    data04;\n" \
                "" \
                "   uint16   data05;\n" \
                "   beuint16 data06;\n" \
                "   int16    data07;\n" \
                "   uint16   data08;\n" \
                "" \
                "   uint32   data09;\n" \
                "   beuint32 data10;\n" \
                "   int32    data11;\n" \
                "   uint32   data12;\n" \
                "" \
                "   uint64   data13;\n" \
                "   beuint64 data14;\n" \
                "   int64    data15;\n" \
                "   uint64   data16;\n" \
                "}\n"

        pc = ProtoCompiler()

        fname = os.path.join(properties.TEMP_DIR, 'all_ints.class')

        f = open(fname, 'w')
        f.write(lines)
        f.close()

        pc.parseFile(fname)

        assert pc.classnames == ['allints'], "Classnames != ['allints']"

        my_class = pc._getClass('allints')

        #print 'Got class of %s.' % str(my_class)

        assert my_class.isPackCompatible(
        ) == 1, "Class should be pack compatible."
        exp_spec = '<B>B<b<B<H>H<h<H<L>L<l<L<Q>Q<q<Q'
        assert my_class.packSpec(
        ) == exp_spec, "Class spec should be '%s', got '%s'." % (
            exp_spec, my_class.packSpec())
        assert my_class.isFixedWidth() == 1, "Width should be fixed."
        assert my_class.getWidth(
        ) == 60, "Width should be 60, got %d." % my_class.getWidth()

        c = pc.emitCode()

        pyfilename = os.path.join(properties.TEMP_DIR, "allints.py")

        f = open(pyfilename, 'w')
        f.write(c)
        f.close()

        # Now try to import it.
        import allints

        obj = allints.allints()

        #print 'Dir of new object: %s' % str(dir(obj))

        assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1"
        assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1"
        assert obj.packSpec() == exp_spec, "packSpec() should be %s" % exp_spec
        assert obj.getWidth(
        ) == 60, "Width should be 60, got %d" % obj.getWidth()
        assert obj.getNumItems(
        ) == 16, "NumItems should be 16, got %d" % obj.getNumItems()
Exemple #6
0
    def test_case_multiple_classes(self):
        lines = "" \
                "# Single byte Class\n" \
                "class singlebyte {\n" \
                "   uint8 preamble = 0xAA;\n" \
                "   uint8 data;\n" \
                "   uint8 postamble = 0xFF;\n" \
                "}\n" \
                "\n" \
                "class doublebyte {\n" \
                "   uint8 preamble = 0xAA;\n" \
                "   uint8 data1;\n" \
                "   uint8 data2;\n" \
                "   uint8 postamble = 0xFF;\n" \
                "}\n"

        pc = ProtoCompiler()

        fname = os.path.join(properties.TEMP_DIR, 'mutiples.class')

        f = open(fname, 'w')
        f.write(lines)
        f.close()

        pc.parseFile(fname)

        #print pc.classnames

        assert pc.classnames == [
            'singlebyte', 'doublebyte'
        ], "Classnames != ['singlebyte', 'doublebyte']"

        my_class = pc._getClass('singlebyte')

        #print 'Got class of %s.' % str(my_class)

        assert my_class.isPackCompatible(
        ) == 1, "Class should be pack compatible."
        exp_spec = '<B<B<B'
        assert my_class.packSpec(
        ) == exp_spec, "Class spec should be '%s', got '%s'." % (
            exp_spec, my_class.packSpec())
        assert my_class.isFixedWidth() == 1, "Width should be fixed."
        assert my_class.getWidth(
        ) == 3, "Width should be 3, got %d." % my_class.getWidth()

        my_class = pc._getClass('doublebyte')

        assert my_class.isPackCompatible(
        ) == 1, "Class should be pack compatible."
        exp_spec = '<B<B<B<B'
        assert my_class.packSpec(
        ) == exp_spec, "Class spec should be '%s', got '%s'." % (
            exp_spec, my_class.packSpec())
        assert my_class.isFixedWidth() == 1, "Width should be fixed."
        assert my_class.getWidth(
        ) == 4, "Width should be 4, got %d." % my_class.getWidth()

        c = pc.emitCode()

        pyfilename = os.path.join(properties.TEMP_DIR, "multiples.py")

        f = open(pyfilename, 'w')
        f.write(c)
        f.close()

        # Now try to import it.
        import multiples

        obj = multiples.singlebyte()

        assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1"
        assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1"
        assert obj.packSpec() == "<B<B<B", "packSpec() should be <B<B<B"
        assert obj.getWidth(
        ) == 3, "Width should be 3, got %d" % obj.getWidth()
        assert obj.getNumItems(
        ) == 3, "NumItems should be 3, got %d" % obj.getNumItems()

        obj = multiples.doublebyte()

        assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1"
        assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1"
        assert obj.packSpec() == "<B<B<B<B", "packSpec() should be <B<B<B<B"
        assert obj.getWidth(
        ) == 4, "Width should be 4, got %d" % obj.getWidth()
        assert obj.getNumItems(
        ) == 4, "NumItems should be 4, got %d" % obj.getNumItems()