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); } }
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; }
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; }
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; }