void dumpif(FILE *fd, struct interface *ifp) { if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) { fprintf(fd, "*** Packet history for interface %s ***\n", ifp->int_name); dumptrace(fd, "to", &ifp->int_output); dumptrace(fd, "from", &ifp->int_input); fprintf(fd, "*** end packet history ***\n"); } }
static FAR void *usbterm_listener(FAR void *parameter) { printf("usbterm_listener: Waiting for remote input\n"); for (;;) { /* Display the prompt string on the remote USB serial connection -- only * if we know that there is someone listening at the other end. The * remote side must initiate the the conversation. */ if (g_usbterm.peer) { fputs("\rusbterm> ", g_usbterm.outstream); fflush(g_usbterm.outstream); } /* Get the next line of input from the remote USB serial connection */ if (fgets(g_usbterm.inbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, g_usbterm.instream)) { /* If we receive anything, then we can be assured that there is someone * with the serial driver open on the remote host. */ g_usbterm.peer = true; /* Echo the line on the local stdout */ fputs(g_usbterm.inbuffer, stdout); /* Display the prompt string on stdout */ fputs("usbterm> ", stdout); fflush(stdout); } /* If USB tracing is enabled, then dump all collected trace data to stdout */ dumptrace(); } /* Won't get here */ return NULL; }
int conn_main(int argc, char *argv[]) #endif { int ret; /* If this program is implemented as the NSH 'msconn' command, then we need to * do a little error checking to assure that we are not being called re-entrantly. */ #ifdef CONFIG_NSH_BUILTIN_APPS /* Check if there is a non-NULL USB mass storage device handle (meaning that the * USB mass storage device is already configured). */ if (g_composite.cmphandle) { printf("conn_main: ERROR: Already connected\n"); return 1; } #endif #ifdef CONFIG_SYSTEM_COMPOSITE_DEBUGMM # ifdef CONFIG_CAN_PASS_STRUCTS g_composite.mmstart = mallinfo(); g_composite.mmprevious = g_composite.mmstart; # else (void)mallinfo(&g_composite.mmstart); memcpy(&g_composite.mmprevious, &g_composite.mmstart, sizeof(struct mallinfo)); # endif #endif /* Perform architecture-specific initialization */ printf("conn_main: Performing architecture-specific intialization\n"); ret = composite_archinitialize(); if (ret < 0) { printf("conn_main: composite_archinitialize failed: %d\n", -ret); return 1; } check_test_memory_usage("After composite_archinitialize()"); /* Initialize the USB composite device device */ g_composite.cmphandle = composite_initialize(); if (!g_composite.cmphandle) { printf("conn_main: composite_initialize failed\n"); return 1; } check_test_memory_usage("After composite_initialize()"); #if CONFIG_USBDEV_TRACE && CONFIG_USBDEV_TRACE_INITIALIDSET != 0 /* If USB tracing is enabled and tracing of initial USB events is specified, * then dump all collected trace data to stdout */ sleep(5); ret = dumptrace(); if (ret < 0) { goto errout; } #endif /* It this program was configued as an NSH command, then just exit now. * Also, if signals are not enabled (and, hence, sleep() is not supported. * then we have not real option but to exit now. */ #if !defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_DISABLE_SIGNALS) /* Otherwise, this thread will hang around and monitor the USB activity */ /* Open the serial driver */ ret = open_serial(); if (ret < 0) { goto errout; } /* Now looping */ for (;;) { /* Sleep for a bit */ fflush(stdout); sleep(5); /* Echo any serial data */ ret = echo_serial(); if (ret < 0) { goto errout; } /* Dump trace data */ # ifdef CONFIG_USBDEV_TRACE printf("\n" "conn_main: USB TRACE DATA:\n"); ret = dumptrace(); if (ret < 0) { goto errout; } check_test_memory_usage("After usbtrace_enumerate()"); # else printf("conn_main: Still alive\n"); # endif } #else printf("conn_main: Connected\n"); check_test_memory_usage("After composite device connection"); #endif /* Dump debug memory usage */ printf("conn_main: Exiting\n"); #if !defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_DISABLE_SIGNALS) close(g_composite.infd); close(g_composite.outfd); #endif #ifdef CONFIG_NSH_BUILTIN_APPS #endif final_memory_usage("Final memory usage"); return 0; errout: #if !defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_DISABLE_SIGNALS) close(g_composite.infd); close(g_composite.outfd); #endif composite_uninitialize(g_composite.cmphandle); final_memory_usage("Final memory usage"); return 1; }
static int open_serial(void) { int errcode; #ifdef CONFIG_USBDEV_TRACE int ret; #endif /* Open the USB serial device for writing (blocking) */ do { printf("open_serial: Opening USB serial driver\n"); g_composite.outfd = open(CONFIG_SYSTEM_COMPOSITE_SERDEV, O_WRONLY); if (g_composite.outfd < 0) { errcode = errno; printf("open_serial: ERROR: Failed to open %s for writing: %d\n", CONFIG_SYSTEM_COMPOSITE_SERDEV, errcode); /* ENOTCONN means that the USB device is not yet connected */ if (errcode == ENOTCONN) { printf("open_serial: Not connected. Wait and try again.\n"); sleep(5); } else { /* Give up on other errors */ printf("open_serial: Aborting\n"); return -errcode; } } /* If USB tracing is enabled, then dump all collected trace data to * stdout. */ #ifdef CONFIG_USBDEV_TRACE ret = dumptrace(); if (ret < 0) { return ret; } #endif } while (g_composite.outfd < 0); /* Open the USB serial device for reading (non-blocking) */ g_composite.infd = open(CONFIG_SYSTEM_COMPOSITE_SERDEV, O_RDONLY|O_NONBLOCK); if (g_composite.infd < 0) { errcode = errno; printf("open_serial: ERROR: Failed to open%s for reading: %d\n", CONFIG_SYSTEM_COMPOSITE_SERDEV, errcode); close(g_composite.outfd); return -errcode; } printf("open_serial: Successfully opened the serial driver\n"); return OK; }
int usbterm_main(int argc, char *argv[]) #endif { struct boardioc_usbdev_ctrl_s ctrl; FAR void *handle; pthread_attr_t attr; int ret; /* Initialize global data */ memset(&g_usbterm, 0, sizeof(struct usbterm_globals_s)); /* Initialization of the USB hardware may be performed by logic external to * this test. */ #ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT printf("usbterm_main: Performing external device initialization\n"); ret = usbterm_devinit(); if (ret != OK) { printf("usbterm_main: usbterm_devinit failed: %d\n", ret); goto errout; } #endif /* Initialize the USB serial driver */ printf("usbterm_main: Registering USB serial driver\n"); #ifdef CONFIG_CDCACM ctrl.usbdev = BOARDIOC_USBDEV_CDCACM; ctrl.action = BOARDIOC_USBDEV_CONNECT; ctrl.instance = 0; ctrl.handle = &handle; #else ctrl.usbdev = BOARDIOC_USBDEV_PL2303; ctrl.action = BOARDIOC_USBDEV_CONNECT; ctrl.instance = 0; ctrl.handle = &handle; #endif ret = boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl); if (ret < 0) { printf("usbterm_main: ERROR: Failed to create the USB serial device: %d\n", -ret); goto errout_with_devinit; } printf("usbterm_main: Successfully registered the serial driver\n"); #if defined(CONFIG_USBDEV_TRACE) && CONFIG_USBDEV_TRACE_INITIALIDSET != 0 /* If USB tracing is enabled and tracing of initial USB events is specified, * then dump all collected trace data to stdout */ sleep(5); dumptrace(); #endif /* Then, in any event, configure trace data collection as configured */ usbtrace_enable(TRACE_BITSET); /* Open the USB serial device for writing */ do { printf("usbterm_main: Opening USB serial driver\n"); g_usbterm.outstream = fopen(USBTERM_DEVNAME, "w"); if (g_usbterm.outstream == NULL) { int errcode = errno; printf("usbterm_main: ERROR: Failed to open " USBTERM_DEVNAME " for writing: %d\n", errcode); /* ENOTCONN means that the USB device is not yet connected */ if (errcode == ENOTCONN) { printf("usbterm_main: Not connected. Wait and try again.\n"); sleep(5); } else { /* Give up on other errors */ goto errout_with_devinit; } } /* If USB tracing is enabled, then dump all collected trace data to stdout */ dumptrace(); } while (g_usbterm.outstream == NULL); /* Open the USB serial device for reading. Since we are already connected, this * should not fail. */ g_usbterm.instream = fopen(USBTERM_DEVNAME, "r"); if (g_usbterm.instream == NULL) { printf("usbterm_main: ERROR: Failed to open " USBTERM_DEVNAME " for reading: %d\n", errno); goto errout_with_outstream; } printf("usbterm_main: Successfully opened the serial driver\n"); /* Start the USB term listener thread */ printf("usbterm_main: Starting the listener thread\n"); ret = pthread_attr_init(&attr); if (ret != OK) { printf("usbterm_main: pthread_attr_init failed: %d\n", ret); goto errout_with_streams; } ret = pthread_create(&g_usbterm.listener, &attr, usbterm_listener, (pthread_addr_t)0); if (ret != 0) { printf("usbterm_main: Error in thread creation: %d\n", ret); goto errout_with_streams; } /* Send messages and get responses -- forever */ printf("usbterm_main: Waiting for local input\n"); for (;;) { /* Display the prompt string on stdout */ fputs("usbterm> ", stdout); fflush(stdout); /* Get the next line of input */ #ifdef CONFIG_EXAMPLES_USBTERM_FGETS /* fgets returns NULL on end-of-file or any I/O error */ if (fgets(g_usbterm.outbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, stdin) == NULL) { printf("ERROR: fgets failed: %d\n", errno); return 1; } #else ret = readline(g_usbterm.outbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, stdin, stdout); /* Readline normally returns the number of characters read, * but will return EOF on end of file or if an error occurs. Either * will cause the session to terminate. */ if (ret == EOF) { printf("ERROR: readline failed: %d\n", ret); return 1; } #endif /* Is there anyone listening on the other end? */ else if (g_usbterm.peer) { /* Yes.. Send the line of input via USB */ fputs(g_usbterm.outbuffer, g_usbterm.outstream); /* Display the prompt string on the remote USB serial connection */ fputs("\rusbterm> ", g_usbterm.outstream); fflush(g_usbterm.outstream); } else { printf("Still waiting for remote peer. Please try again later.\n"); } /* If USB tracing is enabled, then dump all collected trace data to stdout */ dumptrace(); } /* Error exits */ errout_with_streams: fclose(g_usbterm.instream); errout_with_outstream: fclose(g_usbterm.outstream); errout_with_devinit: #ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT usbterm_devuninit(); errout: #endif printf("usbterm_main: Aborting\n"); return 1; }
int usbserial_main(int argc, char *argv[]) #endif { #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY int infd; #endif #ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY int outfd; #endif #ifdef COUNTER_NEEDED int count = 0; #endif ssize_t nbytes; #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY int i, j, k; #endif int ret; /* Initialize the USB serial driver */ printf("usbserial_main: Registering USB serial driver\n"); #ifdef CONFIG_CDCACM ret = cdcacm_initialize(0, NULL); #else ret = usbdev_serialinitialize(0); #endif if (ret < 0) { printf("usbserial_main: ERROR: Failed to create the USB serial device: %d\n", -ret); return 1; } printf("usbserial_main: Successfully registered the serial driver\n"); #if CONFIG_USBDEV_TRACE && CONFIG_USBDEV_TRACE_INITIALIDSET != 0 /* If USB tracing is enabled and tracing of initial USB events is specified, * then dump all collected trace data to stdout */ sleep(5); dumptrace(); #endif /* Then, in any event, configure trace data collection as configured */ usbtrace_enable(TRACE_BITSET); /* Open the USB serial device for writing (blocking) */ #ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY do { printf("usbserial_main: Opening USB serial driver\n"); outfd = open(USBSER_DEVNAME, O_WRONLY); if (outfd < 0) { int errcode = errno; printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for writing: %d\n", errcode); /* ENOTCONN means that the USB device is not yet connected */ if (errcode == ENOTCONN) { printf("usbserial_main: Not connected. Wait and try again.\n"); sleep(5); } else { /* Give up on other errors */ printf("usbserial_main: Aborting\n"); return 2; } } /* If USB tracing is enabled, then dump all collected trace data to stdout */ dumptrace(); } while (outfd < 0); #endif /* Open the USB serial device for reading (non-blocking) */ #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY #ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY infd = open(USBSER_DEVNAME, O_RDONLY|O_NONBLOCK); if (infd < 0) { printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for reading: %d\n", errno); close(outfd); return 3; } #else do { infd = open(USBSER_DEVNAME, O_RDONLY|O_NONBLOCK); if (infd < 0) { int errcode = errno; printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for reading: %d\n", errno); /* ENOTCONN means that the USB device is not yet connected */ if (errcode == ENOTCONN) { printf("usbserial_main: Not connected. Wait and try again.\n"); sleep(5); } else { /* Give up on other errors */ printf("usbserial_main: Aborting\n"); return 3; } } /* If USB tracing is enabled, then dump all collected trace data to stdout */ dumptrace(); } while (infd < 0); #endif #endif printf("usbserial_main: Successfully opened the serial driver\n"); /* Send messages and get responses -- forever */ for (;;) { /* Test IN (device-to-host) messages */ #ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY #if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) if (count < 8) { printf("usbserial_main: Saying hello\n"); nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg)); count++; } else { printf("usbserial_main: Reciting QEI's speech of 1588\n"); nbytes = write(outfd, g_longmsg, sizeof(g_longmsg)); count = 0; } #elif !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) printf("usbserial_main: Reciting QEI's speech of 1588\n"); nbytes = write(outfd, g_longmsg, sizeof(g_longmsg)); #else /* !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) */ printf("usbserial_main: Saying hello\n"); nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg)); #endif /* Test if the write was successful */ if (nbytes < 0) { printf("usbserial_main: ERROR: write failed: %d\n", errno); #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY close(infd); #endif close(outfd); return 4; } printf("usbserial_main: %ld bytes sent\n", (long)nbytes); #endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */ /* Test OUT (host-to-device) messages */ #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY /* Poll for incoming messages */ printf("usbserial_main: Polling for OUT messages\n"); for (i = 0; i < 5; i++) { memset(g_iobuffer, 'X', IOBUFFER_SIZE); nbytes = read(infd, g_iobuffer, IOBUFFER_SIZE); if (nbytes < 0) { int errorcode = errno; if (errorcode != EAGAIN) { printf("usbserial_main: ERROR: read failed: %d\n", errno); close(infd); #ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY close(outfd); #endif return 6; } } else { printf("usbserial_main: Received l%d bytes:\n", (long)nbytes); if (nbytes > 0) { for (j = 0; j < nbytes; j += 16) { printf("usbserial_main: %03x: ", j); for (k = 0; k < 16; k++) { if (k == 8) { printf(" "); } if (j+k < nbytes) { printf("%02x", g_iobuffer[j+k]); } else { printf(" "); } } printf(" "); for (k = 0; k < 16; k++) { if (k == 8) { printf(" "); } if (j+k < nbytes) { if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f) { printf("%c", g_iobuffer[j+k]); } else { printf("."); } } else { printf(" "); } } printf("\n"); } } } sleep(1); } #else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ printf("usbserial_main: Waiting\n"); sleep(5); #endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ /* If USB tracing is enabled, then dump all collected trace data to stdout */ dumptrace(); } /* Won't get here, but if we did this what we would have to do */ #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY close(infd); #endif #ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY close(outfd); #endif return 0; }