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)
def receive(self): parser = CommandParser() # if sock not in locals(): sock = libcsp.socket() libcsp.bind(sock, libcsp.CSP_ANY) libcsp.listen(sock, 5) # Exit the loop gracefully (ie. CTRL+C) if flag.exit(): print('Exiting receiving loop') flag.reset() return # wait for incoming connection while True: print('WAIT FOR CONNECTION ... (CTRL+C to stop)') conn = libcsp.accept(sock, 1000) # or 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: print('packet is None; no more packets') break # print the packet's data data = bytearray(libcsp.packet_get_data(packet)) length = libcsp.packet_get_length(packet) print(length) print(data) rxData = parser.parseReturnValue( libcsp.conn_src(conn), libcsp.conn_dst(conn), libcsp.conn_dport(conn), data, length) if rxData is None: print('ERROR: bad response data') print(rxData)
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)