Beispiel #1
0
int main(void)
{

    /* Initialize CSP
     * Not interfacing to any external devices, so we don't need to register
     * a route
     */
    printf("Initializing CSP\n");

    csp_buffer_init(5, 256);
    csp_init(MY_ADDRESS);
    csp_route_start_task(500, 1);

    /* Initialize example threads */
    printf("Starting example tasks\n");

    csp_thread_handle_t handle_server;
    csp_thread_handle_t handle_client;

    csp_thread_create(csp_server, "CSPSRV", 1000, NULL, 0, &handle_server);
    csp_thread_create(csp_client, "CSPCLI", 1000, NULL, 0, &handle_client);

    while (1)
    {
        csp_sleep_ms(100000);
    }

    return 0;
}
Beispiel #2
0
int can_mbox_init(can_iface_ctx_t *iface_ctx) {

    int i;
    mbox_t *m, *mbox;

    mbox = iface_ctx->mbox;

    for (i = 0; i < MBOX_NUM; i++) {
        m = &mbox[i];
        m->state = MBOX_FREE;
        m->csp_can_socket = &iface_ctx->csp_can_socket;
        m->mbox_pool_sem = &iface_ctx->mbox_pool_sem;

        /* Init signal semaphore */
        if (csp_bin_sem_create(&(m->signal_sem)) != CSP_SEMAPHORE_OK) {
            csp_log_error("sem create");
            return -1;
        } else {
            /* Take signal semaphore so the thread waits for tx data */
            csp_bin_sem_wait(&(m->signal_sem), CSP_MAX_DELAY);
        }

        /* Create mailbox */
        if(csp_thread_create(mbox_tx_thread, (signed char *)"mbox_tx", 1024, (void *)m, 3,  &m->thread) != CSP_ERR_NONE) { //TODO: Adjust priority
            csp_log_error("thread creation");
            return -1;
        }
    }

    /* Init mailbox pool semaphore */
    csp_bin_sem_create(&iface_ctx->mbox_pool_sem);
    return 0;

}
Beispiel #3
0
int main(int argc, char * argv[]) {

    /* Init buffer system with 10 packets of maximum 300 bytes each */
    csp_buffer_init(2, 800);

    /* Init CSP with address MY_ADDRESS */
    csp_init(MY_ADDRESS);

    /* Start router task with 500 word stack, OS task priority 1 */
    csp_route_start_task(500, 1);

    /* Enable debug output from CSP */
    if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) {
        printf("Debug eneabled\r\n");
        csp_debug_toggle_level(4);

        printf("Conn table\r\n");
        csp_conn_print_table();

        printf("Route table\r\n");
        csp_route_print_table();

        printf("Interfaces\r\n");
        csp_route_print_interfaces();

    }
    
	int iret1, iret2;

    csp_thread_handle_t handle_server;
    iret1 = csp_thread_create(task_server, (signed char *) "SERVER", 1000, NULL, 0, &handle_server);

    csp_thread_handle_t handle_client;
    iret2 = csp_thread_create(task_client, (signed char *) "CLIENT", 1000, NULL, 0, &handle_client);

	pthread_join(handle_client, NULL);
	printf("Client thread returns: %d\n", iret2);
	pthread_join(handle_server, NULL);
	printf("Server thread returns: %d\n", iret1);


    return 0;

}
Beispiel #4
0
int csp_route_start_task(unsigned int task_stack_size, unsigned int priority) {

    int ret = csp_thread_create(csp_task_router, (signed char *) "RTE", task_stack_size, NULL, priority, &handle_router);

    if (ret != 0) {
        csp_log_error("Failed to start router task\n");
        return CSP_ERR_NOMEM;
    }

    return CSP_ERR_NONE;

}
Beispiel #5
0
int main(int argc, char * argv[]) {
	printf("Initialising CSP\r\n");
	csp_buffer_init(10, 300);
	csp_init(MY_ADDRESS);
	csp_route_start_task(500, 1);
	printf(argv[1]);

	printf("Starting server task\r\n");
	csp_thread_handle_t handle_server;
	csp_thread_create(task_server, (signed char *) "SERVER", 1000, NULL, 0, &handle_server);

	printf("Starting client task\r\n");
//	csp_thread_handle_t handle_client;
//	csp_thread_create(task_client, (signed char *) "CLIENT", 1000, NULL, 0, &handle_client);

/*	while(1) {
		csp_sleep_ms(1000);
		csp_packet_t *packet;
		csp_conn_t *conn;
		packet = csp_buffer_get(10);
		if (packet == NULL) {
			printf("Failed to get buffer element\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\n");
			csp_buffer_free(packet);
			return CSP_TASK_RETURN;
		}

		char *msg = "Hello world";
		msg = argc;
		strcpy((char *) packet->data, msg);

		packet->length = strlen(msg);

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

		csp_close(conn);
	}*/
	sendpacket();
	while(1){
		csp_sleep_ms(100000);
	}
	return 0;
}
Beispiel #6
0
int csp_can_init(uint8_t mode, struct csp_can_config *conf) {

	int ret;
	uint32_t mask;

	/* Initialize packet buffer */
	if (pbuf_init() != 0) {
		csp_log_error("Failed to initialize CAN packet buffers\r\n");
		return CSP_ERR_NOMEM;
	}

	/* Initialize CFP identifier */
	if (id_init() != 0) {
		csp_log_error("Failed to initialize CAN identification number\r\n");
		return CSP_ERR_NOMEM;
	}

	if (mode == CSP_CAN_MASKED) {
		mask = CFP_MAKE_DST((1 << CFP_HOST_SIZE) - 1);
	} else if (mode == CSP_CAN_PROMISC) {
		mask = 0;
		csp_if_can.promisc = 1;
	} else {
		csp_log_error("Unknown CAN mode\r\n");
		return CSP_ERR_INVAL;
	}

	can_rx_queue = csp_queue_create(CSP_CAN_RX_QUEUE_SIZE, sizeof(can_frame_t));
	if (can_rx_queue == NULL) {
		csp_log_error("Failed to create CAN RX queue\r\n");
		return CSP_ERR_NOMEM;
	}

	ret = csp_thread_create(csp_can_rx_task, (signed char *) "CANRX",2048, NULL, 3, &can_rx_task);
	if (ret != 0) {
		csp_log_error("Failed to init CAN RX task\r\n");
		return CSP_ERR_NOMEM;
	}

	/* Initialize CAN driver */
	if (can_init(CFP_MAKE_DST(my_address), mask, csp_tx_callback, csp_rx_callback, conf) != 0) {
		csp_log_error("Failed to initialize CAN driver\r\n");
		return CSP_ERR_DRIVER;
	}

	/* Regsiter interface */
	csp_route_add_if(&csp_if_can);

	return CSP_ERR_NONE;

}
Beispiel #7
0
int main() {
	csp_buffer_init(10, 300);
	csp_init(MY_ADDRESS);
	csp_route_start_task(500,1);

	printf("Starting client task\r\n");
	csp_thread_handle_t handle_client;
	csp_thread_create(task_client, (signed char *) "CLIENT", 1000, NULL, 0, &handle_client);
	
	while(1) {
		csp_sleep_ms(100000);
	}		
	return 0;
}
Beispiel #8
0
int csp_bridge_start(unsigned int task_stack_size, unsigned int task_priority, csp_iface_t * _if_a, csp_iface_t * _if_b) {

	/* Set static references to A/B side of bridge */
	if_a = _if_a;
	if_b = _if_b;

	static csp_thread_handle_t handle;
	int ret = csp_thread_create(csp_bridge, "BRIDGE", task_stack_size, NULL, task_priority, &handle);

	if (ret != 0) {
		csp_log_error("Failed to start task");
		return CSP_ERR_NOMEM;
	}

	return CSP_ERR_NONE;

}
Beispiel #9
0
int can_init(csp_iface_t *csp_if_can, uint32_t id, uint32_t mask, can_tx_callback_t atxcb, can_rx_callback_t arxcb, struct csp_can_config *conf) {

    struct ifreq ifr;
    struct sockaddr_can addr;
    csp_thread_handle_t rx_thread;
    int *can_socket;
    can_iface_ctx_t *iface_ctx;

    iface_ctx = get_available_interface_ctx();
    if (iface_ctx == NULL) {
        return -1;
    }

    csp_if_can->driver = (void *)iface_ctx;
    iface_ctx->csp_can_socket.csp_if_can = csp_if_can;
    can_socket = &iface_ctx->csp_can_socket.can_socket;

    csp_assert(conf && conf->ifc);

    txcb = atxcb;
    rxcb = arxcb;

    /* Create socket */
    if ((*can_socket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
        csp_log_error("socket: %s", strerror(errno));
        return -1;
    }

    /* Locate interface */
    strncpy(ifr.ifr_name, conf->ifc, IFNAMSIZ - 1);
    if (ioctl(*can_socket, SIOCGIFINDEX, &ifr) < 0) {
        csp_log_error("ioctl: %s", strerror(errno));
        return -1;
    }

    /* Bind the socket to CAN interface */
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
    if (bind(*can_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        csp_log_error("bind: %s", strerror(errno));
        return -1;
    }

    /* Set promiscuous mode */
    if (mask) {
        struct can_filter filter;
        filter.can_id   = id;
        filter.can_mask = mask;
        if (setsockopt(*can_socket, SOL_CAN_RAW, CAN_RAW_FILTER, &filter, sizeof(filter)) < 0) {
            csp_log_error("setsockopt: %s", strerror(errno));
            return -1;
        }
    }

    /* Set read timeout */
    {
        struct timeval tv = {
            .tv_sec = 0,
            .tv_usec = 1000,
        };
        if (setsockopt(*can_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
            csp_log_error("setsockopt: %s", strerror(errno));
            return -1;
        }
    }

    /* Create receive thread */
    if(csp_thread_create(mbox_rx_thread, (signed char *)"mbox_rx", 1024, (void *)&iface_ctx->csp_can_socket, 3,  &rx_thread) != CSP_ERR_NONE) { //TODO: Adjust priority
        csp_log_error("thread creation");
        return -1;
    }

    /* Create mailbox pool */
    if (can_mbox_init(iface_ctx) != 0) {
        csp_log_error("Failed to create tx thread pool");
        return -1;
    }

    return 0;

}