示例#1
0
void csp_ping_noreply(uint8_t node) {

	/* Prepare data */
	csp_packet_t * packet;
	packet = csp_buffer_get(1);

	/* Check malloc */
	if (packet == NULL)
		return;

	/* Open connection */
	csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, node, CSP_PING, 0, 0);
	if (conn == NULL) {
		csp_buffer_free(packet);
		return;
	}

	packet->data[0] = 0x55;
	packet->length = 1;

	printf("Ping ignore reply node %u.\r\n", node);

	/* Try to send frame */
	if (!csp_send(conn, packet, 0))
		csp_buffer_free(packet);

	csp_close(conn);

}
示例#2
0
int csp_ping(uint8_t node, uint32_t timeout, unsigned int size, uint8_t conn_options) {

	int i;
	uint32_t start, time, status = 0;

	/* Counter */
	start = csp_get_ms();

	/* Open connection */
	csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, node, CSP_PING, timeout, conn_options);
	if (conn == NULL)
		return -1;

	/* Prepare data */
	csp_packet_t * packet;
	packet = csp_buffer_get(size);
	if (packet == NULL)
		goto out;

	/* Set data to increasing numbers */
	packet->length = size;
	for (i = 0; i < size; i++)
		packet->data[i] = i;

	/* Try to send frame */
	if (!csp_send(conn, packet, 0))
		goto out;

	/* Read incoming frame */
	packet = csp_read(conn, timeout);
	if (packet == NULL)
		goto out;

	/* Ensure that the data was actually echoed */
	for (i = 0; i < size; i++)
		if (packet->data[i] != i)
			goto out;

	status = 1;

out:
	/* Clean up */
	if (packet != NULL)
		csp_buffer_free(packet);
	csp_close(conn);

	/* We have a reply */
	time = (csp_get_ms() - start);

	if (status) {
		return time;
	} else {
		return -1;
	}

}
示例#3
0
文件: csp_io.c 项目: janbre/NUTS
int csp_transaction(uint8_t prio, uint8_t dest, uint8_t port, uint32_t timeout, void * outbuf, int outlen, void * inbuf, int inlen) {

    csp_conn_t * conn = csp_connect(prio, dest, port, 0, 0);
    if (conn == NULL)
        return 0;

    int status = csp_transaction_persistent(conn, timeout, outbuf, outlen, inbuf, inlen);

    csp_close(conn);

    return status;

}
示例#4
0
void csp_server(void *p) {
    (void) p;

    /* Create socket without any socket options */
    csp_socket_t *sock = csp_socket(CSP_SO_NONE);

    /* Bind all ports to socket */
    csp_bind(sock, CSP_ANY);

    /* Create 10 connections backlog queue */
    csp_listen(sock, 10);

    /* Pointer to current connection and packet */
    csp_conn_t *conn;
    csp_packet_t *packet;

    /* Process incoming connections */
    while (1) {

        /* Wait for connection, 10000 ms timeout */
        if ((conn = csp_accept(sock, 10000)) == NULL)
            continue;

        /* Read packets. Timout is 100 ms */
        while ((packet = csp_read(conn, 100)) != NULL) {
            switch (csp_conn_dport(conn)) {
                case MY_PORT:
                    /* Process packet here */
                    blink(K_LED_GREEN);
                    csp_buffer_free(packet);
                    break;

                default:
                    /* Let the service handler reply pings, buffer use, etc. */
                    #ifdef TARGET_LIKE_MSP430
                    blink(K_LED_GREEN);
                    blink(K_LED_GREEN);
                    #else
                    blink(K_LED_BLUE);
                    #endif
                    csp_service_handler(conn, packet);
                    break;
            }
        }

        /* Close current connection, and handle next */
        csp_close(conn);

    }
}
示例#5
0
unsigned WINAPI task_server(void * parameters) {

	/* Create socket without any socket options */
	csp_socket_t *sock = csp_socket(CSP_SO_NONE);

	/* Bind all ports to socket */
	csp_bind(sock, CSP_ANY);

	/* Create 10 connections backlog queue */
	csp_listen(sock, 10);

	/* Pointer to current connection and packet */
	csp_conn_t *conn;
	csp_packet_t *packet;

	/* Process incoming connections */
	while (1) {

		/* Wait for connection, 10000 ms timeout */
		if ((conn = csp_accept(sock, 10000)) == NULL)
			continue;

		/* Read packets. Timout is 100 ms */
		while ((packet = csp_read(conn, 100)) != NULL) {
			switch (csp_conn_dport(conn)) {
			case MY_PORT:
				/* Process packet here */
				printf("Packet received on MY_PORT: %s\r\n", (char *) packet->data);
				csp_buffer_free(packet);

			default:
				/* Let the service handler reply pings, buffer use, etc. */
				csp_service_handler(conn, packet);
				break;
			}
		}

		/* Close current connection, and handle next */
		csp_close(conn);

	}

	return 0;
}
示例#6
0
void csp_ps(uint8_t node, uint32_t timeout) {

	/* Open connection */
	csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, node, CSP_PS, 0, 0);
	if (conn == NULL)
		return;

	/* Prepare data */
	csp_packet_t * packet;
	packet = csp_buffer_get(95);

	/* Check malloc */
	if (packet == NULL)
		goto out;

	packet->data[0] = 0x55;
	packet->length = 1;

	printf("PS node %u: ", node);

	/* Try to send frame */
	if (!csp_send(conn, packet, 0))
		goto out;

	/* Read incoming frame */
	packet = csp_read(conn, timeout);
	if (packet == NULL) {
		printf(" Timeout!\r\n");
		goto out;
	}

	/* We have a reply */
	printf("PS Length %u\r\n", packet->length);
	printf("%s\r\n", packet->data);

	/* Clean up */
out:
	if (packet != NULL)
		csp_buffer_free(packet);
	csp_close(conn);

}
示例#7
0
void csp_udp_new_packet(csp_conn_t * conn, csp_packet_t * packet) {

    /* Enqueue */
    if (csp_conn_enqueue_packet(conn, packet) < 0) {
        csp_log_error("Connection buffer queue full!\r\n");
        csp_buffer_free(packet);
        return;
    }

    /* Try to queue up the new connection pointer */
    if (conn->socket != NULL) {
        if (csp_queue_enqueue(conn->socket, &conn, 0) != CSP_QUEUE_OK) {
            csp_log_warn("Warning socket connection queue full\r\n");
            csp_close(conn);
            return;
        }

        /* Ensure that this connection will not be posted to this socket again */
        conn->socket = NULL;
    }

}
示例#8
0
文件: testlib.c 项目: janbre/NUTS
int sendpacket() {
	csp_packet_t *packet;
	csp_conn_t *conn;
	packet = csp_buffer_get(10);
	if (packet == NULL) {
		printf("failed to get packet in sendpacket()\n");
		return CSP_TASK_RETURN;
	}
	conn = csp_connect(CSP_PRIO_NORM, MY_ADDRESS, MY_PORT, 1000, CSP_O_NONE);
	if (conn == NULL) {
		printf("Connection failed in sendpacket()\n");
		return CSP_TASK_RETURN;
	}
	char *msg = "42!";
	strcpy((char *) packet->data, msg);
	packet->length = strlen(msg);
	if (!csp_send(conn, packet, 1000)) {
		printf("Sending packet from sendpacket() failed\n");
		csp_buffer_free(packet);
	}
	csp_close(conn);
	return;
}
示例#9
0
unsigned WINAPI task_client(void * parameters) {

	csp_packet_t * packet;
	csp_conn_t * conn;

	while (1) {

		/**
		 * Try ping
		 */

		Sleep(1000);

		int result = csp_ping(MY_ADDRESS, 100, 100, CSP_O_NONE);
		printf("Ping result %d [ms]\r\n", result);

		Sleep(1000);

		/**
		 * Try data packet to server
		 */

		/* Get packet buffer for data */
		packet = csp_buffer_get(100);
		if (packet == NULL) {
			/* Could not get buffer element */
			printf("Failed to get buffer element\n");
			return 0;
		}

		/* Connect to host HOST, port PORT with regular UDP-like protocol and 1000 ms timeout */
		conn = csp_connect(CSP_PRIO_NORM, MY_ADDRESS, MY_PORT, 1000, CSP_O_NONE);
		if (conn == NULL) {
			/* Connect failed */
			printf("Connection failed\n");
			/* Remember to free packet buffer */
			csp_buffer_free(packet);
			return 0;
		}

		/* Copy dummy data to packet */
		char *msg = "Hello World";
		strcpy((char *) packet->data, msg);

		/* Set packet length */
		packet->length = strlen(msg);

		/* Send packet */
		if (!csp_send(conn, packet, 1000)) {
			/* Send failed */
			printf("Send failed\n");
			csp_buffer_free(packet);
		}

		/* Close connection */
		csp_close(conn);

	}

	return 0;
}
示例#10
0
csp_conn_t * csp_connect(uint8_t prio, uint8_t dest, uint8_t dport, uint32_t timeout, uint32_t opts) {

    /* Generate identifier */
    csp_id_t incoming_id, outgoing_id;
    incoming_id.pri = prio;
    incoming_id.dst = my_address;
    incoming_id.src = dest;
    incoming_id.sport = dport;
    incoming_id.flags = 0;
    outgoing_id.pri = prio;
    outgoing_id.dst = dest;
    outgoing_id.src = my_address;
    outgoing_id.dport = dport;
    outgoing_id.flags = 0;

    /* Set connection options */
    if (opts & CSP_O_RDP) {
#ifdef CSP_USE_RDP
        incoming_id.flags |= CSP_FRDP;
        outgoing_id.flags |= CSP_FRDP;
#else
        csp_log_error("Attempt to create RDP connection, but CSP was compiled without RDP support\r\n");
        return NULL;
#endif
    }

    if (opts & CSP_O_HMAC) {
#ifdef CSP_USE_HMAC
        outgoing_id.flags |= CSP_FHMAC;
        incoming_id.flags |= CSP_FHMAC;
#else
        csp_log_error("Attempt to create HMAC authenticated connection, but CSP was compiled without HMAC support\r\n");
        return NULL;
#endif
    }

    if (opts & CSP_O_XTEA) {
#ifdef CSP_USE_XTEA
        outgoing_id.flags |= CSP_FXTEA;
        incoming_id.flags |= CSP_FXTEA;
#else
        csp_log_error("Attempt to create XTEA encrypted connection, but CSP was compiled without XTEA support\r\n");
        return NULL;
#endif
    }

    if (opts & CSP_O_CRC32) {
#ifdef CSP_USE_CRC32
        outgoing_id.flags |= CSP_FCRC32;
        incoming_id.flags |= CSP_FCRC32;
#else
        csp_log_error("Attempt to create CRC32 validated connection, but CSP was compiled without CRC32 support\r\n");
        return NULL;
#endif
    }

    /* Find an unused ephemeral port */
    csp_conn_t * conn;

    /* Wait for sport lock */
    if (csp_bin_sem_wait(&sport_lock, 1000) != CSP_SEMAPHORE_OK)
        return NULL;

    uint8_t start = sport;
    while (++sport != start) {
        if (sport > CSP_ID_PORT_MAX)
            sport = CSP_MAX_BIND_PORT + 1;

        outgoing_id.sport = sport;
        incoming_id.dport = sport;

        /* Match on destination port of _incoming_ identifier */
        conn = csp_conn_find(incoming_id.ext, CSP_ID_DPORT_MASK);

        /* Break if we found an unused ephemeral port */
        if (conn == NULL)
            break;
    }

    /* Post sport lock */
    csp_bin_sem_post(&sport_lock);

    /* If no available ephemeral port was found */
    if (sport == start)
        return NULL;

    /* Get storage for new connection */
    conn = csp_conn_new(incoming_id, outgoing_id);
    if (conn == NULL)
        return NULL;

    /* Set connection options */
    conn->opts = opts;

#ifdef CSP_USE_RDP
    /* Call Transport Layer connect */
    if (outgoing_id.flags & CSP_FRDP) {
        /* If the transport layer has failed to connect
         * deallocate connection structure again and return NULL */
        if (csp_rdp_connect(conn, timeout) != CSP_ERR_NONE) {
            csp_close(conn);
            return NULL;
        }
    }
#endif

    /* We have a successful connection */
    return conn;

}
示例#11
0
void csp_client(void *p) {
    (void) p;
    csp_packet_t * packet;
    csp_conn_t * conn;
    portBASE_TYPE status;
    int signal;

    /**
     * Try ping
     */

    csp_sleep_ms(200);

    #ifdef TARGET_LIKE_MSP430
    blink(K_LED_RED);
    #else
    blink(K_LED_ORANGE);
    #endif
    int result = csp_ping(MY_ADDRESS, 100, 100, CSP_O_NONE);
    if (result) {
        #ifdef TARGET_LIKE_MSP430
        blink(K_LED_RED);
        #else
        blink(K_LED_ORANGE);
        #endif
    }

    /**
     * Try data packet to server
     */

    while (1) {
        status = xQueueReceive(button_queue, &signal, portMAX_DELAY);
        if (status != pdTRUE) {
            continue;
        }

        /* Get packet buffer for data */
        packet = csp_buffer_get(100);
        if (packet == NULL) {
            /* Could not get buffer element */
            return;
        }

        /* Connect to host HOST, port PORT with regular UDP-like protocol and 1000 ms timeout */
        blink(K_LED_RED);
        conn = csp_connect(CSP_PRIO_NORM, MY_ADDRESS, MY_PORT, 1000, CSP_O_NONE);
        if (conn == NULL) {
            /* Connect failed */
            /* Remember to free packet buffer */
            csp_buffer_free(packet);
            return;
        }

        blink(K_LED_RED);
        /* Copy dummy data to packet */
        char *msg = "Hello World";
        strcpy((char *) packet->data, msg);

        /* Set packet length */
        packet->length = strlen(msg);

        /* Send packet */
        if (!csp_send(conn, packet, 1000)) {
            /* Send failed */
            csp_buffer_free(packet);
        }

        blink(K_LED_RED);
        /* Close connection */
        csp_close(conn);
    }
}
示例#12
0
int main(int argc, char *argv[]) {
    int me, other, type;
    char *message = "Testing CSP";
    csp_socket_t *sock = NULL;
    csp_conn_t *conn = NULL;
    csp_packet_t *packet = NULL;

    /* Run as either server or client */
    if (argc != 2) {
        printf("usage: server <server/client>\r\n");
        return -1;
    }

    /* Set type */
    if (strcmp(argv[1], "server") == 0) {
        me = 1;
        other = 2;
        type = TYPE_SERVER;
    } else if (strcmp(argv[1], "client") == 0) {
        me = 2;
        other = 1;
        type = TYPE_CLIENT;
    } else {
        printf("Invalid type. Must be either 'server' or 'client'\r\n");
        return -1;
    }

    /* Init CSP and CSP buffer system */
    if (csp_init(me) != CSP_ERR_NONE || csp_buffer_init(10, 300) != CSP_ERR_NONE) {
        printf("Failed to init CSP\r\n");
        return -1;
    }

    if( type == TYPE_SERVER ) {
        _beginthreadex(NULL, 0, pipe_listener, NULL, 0, 0);    
    } else {
        pipe = CreateFile(
            pipeName,
            GENERIC_READ | GENERIC_WRITE,
            0,
            NULL,
            OPEN_EXISTING,
            0,
            NULL);
        if( pipe == INVALID_HANDLE_VALUE ) {
            printError();
            return -1;
        }
    }

    /* Set default route and start router */
    csp_route_set(CSP_DEFAULT_ROUTE, &csp_if_fifo, CSP_NODE_MAC);
    csp_route_start_task(0, 0);

    /* Create socket and listen for incoming connections */
    if (type == TYPE_SERVER) {
        sock = csp_socket(CSP_SO_NONE);
        csp_bind(sock, PORT);
        csp_listen(sock, 5);
    }

    /* Super loop */
    while (1) {
        if (type == TYPE_SERVER) {
            /* Process incoming packet */
            conn = csp_accept(sock, 1000);
            if (conn) {
                packet = csp_read(conn, 0);
                if (packet)
                    printf("Received: %s\r\n", packet->data);
                csp_buffer_free(packet);
                csp_close(conn);
            }
        } else {
            /* Send a new packet */
            packet = csp_buffer_get(strlen(message));
            if (packet) {
                strcpy((char *) packet->data, message);
                packet->length = strlen(message);
                
                conn = csp_connect(CSP_PRIO_NORM, other, PORT, 1000, CSP_O_NONE);
                printf("Sending: %s\r\n", message);
                if (!conn || !csp_send(conn, packet, 1000))
                    return -1;
                csp_close(conn);
                Sleep(1000);
            }
        }
    }

    return 0;
}
示例#13
0
int main(int argc, char **argv) {

    int me, other, type;
    char *message = "Testing CSP", *rx_channel_name, *tx_channel_name;
    csp_socket_t *sock;
    csp_conn_t *conn;
    csp_packet_t *packet;

    /* Run as either server or client */
    if (argc != 2) {
        printf("usage: %s <server/client>\r\n", argv[0]);
        return -1;
    }

    /* Set type */
    if (strcmp(argv[1], "server") == 0) {
        me = 1;
        other = 2;
        tx_channel_name = "server_to_client";
        rx_channel_name = "client_to_server";
        type = TYPE_SERVER;
    } else if (strcmp(argv[1], "client") == 0) {
        me = 2;
        other = 1;
        tx_channel_name = "client_to_server";
        rx_channel_name = "server_to_client";
        type = TYPE_CLIENT;
    } else {
        printf("Invalid type. Must be either 'server' or 'client'\r\n");
        return -1;
    }

    /* Init CSP and CSP buffer system */
    if (csp_init(me) != CSP_ERR_NONE || csp_buffer_init(10, 300) != CSP_ERR_NONE) {
        printf("Failed to init CSP\r\n");
        return -1;
    }

    tx_channel = open(tx_channel_name, O_RDWR);
    if (tx_channel < 0) {
        printf("Failed to open TX channel\r\n");
        return -1;
    }

    rx_channel = open(rx_channel_name, O_RDWR);
    if (rx_channel < 0) {
        printf("Failed to open RX channel\r\n");
        return -1;
    }

    /* Start fifo RX task */
	pthread_create(&rx_thread, NULL, fifo_rx, NULL);

    /* Set default route and start router */
    csp_route_set(CSP_DEFAULT_ROUTE, &csp_if_fifo, CSP_NODE_MAC);
    csp_route_start_task(0, 0);

    /* Create socket and listen for incoming connections */
    if (type == TYPE_SERVER) {
        sock = csp_socket(CSP_SO_NONE);
        csp_bind(sock, PORT);
        csp_listen(sock, 5);
    }

    /* Super loop */
    while (1) {
        if (type == TYPE_SERVER) {
            /* Process incoming packet */
            conn = csp_accept(sock, 1000);
            if (conn) {
                packet = csp_read(conn, 0);
                if (packet)
                    printf("Received: %s\r\n", packet->data);
                csp_buffer_free(packet);
                csp_close(conn);
            }
        } else {
            /* Send a new packet */
            packet = csp_buffer_get(strlen(message));
            if (packet) {
                strcpy((char *) packet->data, message);
                packet->length = strlen(message);

                conn = csp_connect(CSP_PRIO_NORM, other, PORT, 1000, CSP_O_NONE);
                printf("Sending: %s\r\n", message);
                if (!conn || !csp_send(conn, packet, 1000))
                    return -1;
                csp_close(conn);
            }
            sleep(1);
        }
    }

    close(rx_channel);
    close(tx_channel);

    return 0;
}