def csp_server():
    sock = libcsp.socket()
    libcsp.bind(sock, libcsp.CSP_ANY)
    libcsp.listen(sock, 5)
    while True:
        # wait for incoming connection
        conn = libcsp.accept(sock, libcsp.CSP_MAX_TIMEOUT)
        if not conn:
            continue

        print("connection: source=%i:%i, dest=%i:%i" %
              (libcsp.conn_src(conn), libcsp.conn_sport(conn),
               libcsp.conn_dst(conn), libcsp.conn_dport(conn)))

        while True:
            # Read all packets on the connection
            packet = libcsp.read(conn, 100)
            if packet is None:
                break

            if libcsp.conn_dport(conn) == 10:
                # print request
                data = bytearray(libcsp.packet_get_data(packet))
                length = libcsp.packet_get_length(packet)
                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)
Example #2
0
    def getInput(self, prompt=None, inVal=None):
        """ Take input (either prompt user or take input from funtion call)
        and parse the input to CSP packet information """
        if inVal is not None:
            try:
                command = self.parser.parseInputValue(inVal)
            except Exception as e:
                print(e + '\n')
                return
        elif prompt is not None:
            inStr = input(prompt)
            try:
                command = self.parser.parseInputValue(inStr)
            except Exception as e:
                print(e + '\n')
                return
        else:
            print('invalid call to getInput')
            return

        if command is None:
            print('Error: Command was not parsed')
            return

        toSend = libcsp.buffer_get(len(command['args']))
        if len(command['args']) > 0:
            libcsp.packet_set_data(toSend, command['args'])
        return command['dst'], command['dport'], toSend
def csp_server():
    # parameters: {options} - bit flag corresponding to socket options (see "include\csp\csp_types.h" lines 167-180)
    # creates new socket endpoint, returns socket or None
    # libcsp.listen(<~>,<~>) must proceed this function call somewhere
    sock = libcsp.socket()

    # bind port to socket
    # parameters: {socket}, {port} - (libcsp.CSP_ANY=255 meaning 'listen on all ports')
    libcsp.bind(sock, libcsp.CSP_ANY)

    # Set socket to listen for incoming connections
    # parameters: {socket}, {backlog - default=10 (max length of backlog queue, incoming connection need to be "accepted" using libcsp.accept())}
    libcsp.listen(sock, 5)
    while True:
        # wait for incoming connection
        # parameters: {socket} optional:{timeout}
        # returns the connection data or None
        conn = libcsp.accept(sock, libcsp.CSP_MAX_TIMEOUT)
        if not conn:
            continue

        # print connection source address/port and destination address/port
        print("connection: source=%i:%i, dest=%i:%i" %
              (libcsp.conn_src(conn), libcsp.conn_sport(conn),
               libcsp.conn_dst(conn), libcsp.conn_dport(conn)))

        while True:
            # Read incoming packets on the connection
            # parameters: {connection} optional:{timeout (default=500ms)}
            # returns the entire packet
            packet = libcsp.read(conn, 100)
            if packet is None:
                break

            # connection destination port
            if libcsp.conn_dport(conn) == 10:
                # extract the data payload from the packet
                # see "include\csp\csp_types.h" line 215-239 for packet structure
                data = bytearray(libcsp.packet_get_data(packet))

                # get length of the data (not the whole packet, just the data length)
                length = libcsp.packet_get_length(packet)
                print("got packet, len=" + str(length) + ", data=" +
                      ''.join('{:02x}'.format(x) for x in data))

                # send back "input data + 1"
                data[0] = data[0] + 1

                # free up a buffer to hold the reply
                # parameters: {buffer size (# of 4-byte doublewords)}
                reply = libcsp.buffer_get(1)

                # store the data into the reply buffer
                libcsp.packet_set_data(reply, data)

                # Send packet as a reply
                # uses the info (address/port) from the original packet to reply
                # parameters:
                # packet    - the incoming packet (request packet)
                # reply     - data buffer containing reply
                # options   - *optional* connection options (see "include\csp\csp_types.h" line 184-195)
                # timeout   - *optional* in ms (default=1000ms)
                libcsp.sendto_reply(packet, reply, libcsp.CSP_O_NONE)

            else:
                # pass request on to service handler if the given packet is a service-request
                # (ie: destination port is [0-6] see "include\csp\csp_types.h" line 47-55)
                # parameters: {connection} {packet}
                # will handle and send reply packets if necessary
                libcsp.service_handler(conn, packet)