예제 #1
0
 def __init__(self, opts):
     self.myAddr = apps['GND']
     self.parser = CommandParser()
     self.server_connection = defaultdict(dict)
     libcsp.init(self.myAddr, 'host', 'model', '1.2.3', 100, 300)
     if opts.interface == 'zmq':
         self.__zmq__(self.myAddr)
     elif opts.interface == 'uart':
         self.__uart__(opts.device)
     elif opts.interface == 'fifo':
         self.__fifo__()
     libcsp.route_start_task()
     time.sleep(0.2)  # allow router task startup
     self.rdp_timeout = opts.timeout  # 10 seconds
     libcsp.rdp_set_opt(4, self.rdp_timeout, 1000, 1, 250, 2)
                print("got packet, len=" + str(length) + ", data=" +
                      ''.join('{:02x}'.format(x) for x in data))
                # send reply
                data[0] = data[0] + 1
                reply = libcsp.buffer_get(1)
                libcsp.packet_set_data(reply, data)
                libcsp.sendto_reply(packet, reply, libcsp.CSP_O_NONE)

            else:
                # pass request on to service handler
                libcsp.service_handler(conn, packet)


if __name__ == "__main__":

    # init csp
    libcsp.init(27, "test_service", "bindings", "1.2.3", 10, 300)
    libcsp.zmqhub_init(27, "localhost")
    libcsp.rtable_set(0, 0, "ZMQHUB")
    libcsp.route_start_task()

    print("Hostname: %s" % libcsp.get_hostname())
    print("Model:    %s" % libcsp.get_model())
    print("Revision: %s" % libcsp.get_revision())

    print("Routes:")
    libcsp.print_routes()

    # start CSP server
    threading.Thread(target=csp_server).start()
    parser.add_argument("-c", "--can", help="Add CAN interface")
    parser.add_argument("-z", "--zmq", help="Add ZMQ interface")
    parser.add_argument("-s",
                        "--server-address",
                        type=int,
                        default=27,
                        help="Server address")
    parser.add_argument("-R", "--routing-table", help="Routing table")
    return parser.parse_args(sys.argv[1:])


if __name__ == "__main__":

    options = getOptions()

    libcsp.init(options.address, "host", "model", "1.2.3", 10, 300)

    if options.can:
        libcsp.can_socketcan_init(options.can)
    if options.zmq:
        libcsp.zmqhub_init(options.address, options.zmq)
        libcsp.rtable_load("0/0 ZMQHUB")
    if options.routing_table:
        libcsp.rtable_load(options.routing_table)

    libcsp.route_start_task()
    time.sleep(0.2)  # allow router task startup

    print("Connections:")
    libcsp.print_connections()