-
Notifications
You must be signed in to change notification settings - Fork 1
Add emulated serial port #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
499b0d4
Add module to emulate serial port via TCP socket.
giomba ee7d9b8
Refactor SIO/2 to export channels defines.
giomba 69585e3
Add interface to attach serial peripherals to SIO/2.
giomba ba3e67c
Add command to open/close virtual serial port.
giomba 4e144b5
Add virtual serial port module to emulator build.
giomba 7e0dda9
Implement TX-ready channel flag in SIO/2.
giomba e0db61c
Remove TODO comments for virtual serial port emulation.
giomba cb714b7
Improve logging messages in SIO/2 module.
giomba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| #include "serial.h" | ||
|
|
||
| #include "fifo.h" | ||
| #include "sio2.h" | ||
| #include "time.h" | ||
|
|
||
| #include <ctype.h> | ||
| #include <errno.h> | ||
| #include <netinet/in.h> | ||
| #include <string.h> | ||
| #include <sys/socket.h> | ||
| #include <sys/time.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define LOG_LEVEL LOG_LVL_INFO | ||
| #include "log.h" | ||
|
|
||
| #define SERIAL_TCP_PORT (0xCEDB) | ||
| #define SERIAL_NETWORK_BUFFER_SIZE 64U | ||
|
|
||
| DECLARE_FIFO_TYPE(char, SerialFifo, 64); | ||
| static int sockfd = -1; | ||
| static int connfd = -1; | ||
| static SerialFifo tx_fifo; | ||
| static SerialFifo rx_fifo; | ||
|
|
||
| static bool serial_getChar(uint8_t *c) { | ||
| if (FIFO_ISEMPTY(&rx_fifo)) | ||
| return false; | ||
|
|
||
| *c = (uint8_t)FIFO_POP(&rx_fifo); | ||
| return true; | ||
| } | ||
|
|
||
| static bool serial_putChar(uint8_t c) { | ||
| if (FIFO_ISFULL(&tx_fifo)) | ||
| return false; | ||
|
|
||
| LOG_DEBUG("serial: transmitting: %02x (%c)\n", (unsigned int)c, | ||
| isprint(c) ? c : ' '); | ||
|
|
||
| FIFO_PUSH(&tx_fifo, (char)c); | ||
| return true; | ||
| } | ||
|
|
||
| static void serial_poll(void) { | ||
| struct timeval timeout; | ||
| timeout.tv_sec = 0; | ||
| timeout.tv_usec = 0; | ||
|
|
||
| if (connfd == -1) { | ||
| fd_set accept_set; | ||
| FD_ZERO(&accept_set); | ||
| FD_SET(sockfd, &accept_set); | ||
| int ret = select(sockfd + 1, &accept_set, NULL, NULL, &timeout); | ||
| if (ret == -1) { | ||
| LOG_ERR( | ||
| "serial: error during select while accepting new client: %s\n", | ||
| strerror(errno)); | ||
| return; | ||
| } | ||
| if (ret == 0) // timeout | ||
| return; | ||
|
|
||
| connfd = accept(sockfd, NULL, NULL); | ||
| if (connfd != -1) { | ||
| LOG_INFO("serial: accept client\n"); | ||
| } | ||
| } else { | ||
| int ret = -1; | ||
| fd_set read_set; | ||
| fd_set write_set; | ||
| FD_ZERO(&read_set); | ||
| FD_ZERO(&write_set); | ||
| FD_SET(connfd, &read_set); | ||
| FD_SET(connfd, &write_set); | ||
|
|
||
| ret = select(connfd + 1, &read_set, &write_set, NULL, &timeout); | ||
| if (ret == -1) { | ||
| LOG_ERR("serial: select error while reading from client: %s\n", | ||
| strerror(errno)); | ||
| close(connfd); | ||
| connfd = -1; | ||
| return; | ||
| } | ||
| if (ret == 0) // timeout | ||
| return; | ||
|
|
||
| // check file descriptors ready for read | ||
| if (FD_ISSET(connfd, &read_set)) { | ||
| char buffer[SERIAL_NETWORK_BUFFER_SIZE]; | ||
| const size_t to_receive = MIN((size_t)SERIAL_NETWORK_BUFFER_SIZE, | ||
| (size_t)FIFO_FREE(&rx_fifo)); | ||
| if (to_receive > 0) { | ||
| ssize_t ret = recv(connfd, buffer, to_receive, 0); | ||
| if (ret == -1) { | ||
| LOG_ERR( | ||
| "serial: recv error while reading from client: %s\n", | ||
| strerror(errno)); | ||
| LOG_ERR("serial: connection reset\n"); | ||
| close(connfd); | ||
| connfd = -1; | ||
| return; | ||
| } | ||
| if (ret == 0) { | ||
| // client disconnection | ||
| close(connfd); | ||
| connfd = -1; | ||
| LOG_INFO("serial: client disconnected\n"); | ||
| return; | ||
| } | ||
| // data available | ||
| for (ssize_t i = 0; i < ret && !FIFO_ISFULL(&rx_fifo); ++i) | ||
| FIFO_PUSH(&rx_fifo, buffer[i]); | ||
| } | ||
| } | ||
|
|
||
| // check file descriptors ready for write | ||
| if (FD_ISSET(connfd, &write_set)) { | ||
| char buffer[SERIAL_NETWORK_BUFFER_SIZE]; | ||
| size_t n = 0; | ||
| while (n < SERIAL_NETWORK_BUFFER_SIZE && !FIFO_ISEMPTY(&tx_fifo)) | ||
| buffer[n++] = FIFO_POP(&tx_fifo); | ||
| ssize_t ret = send(connfd, buffer, n, 0); | ||
|
|
||
| if (ret == -1) { | ||
| LOG_ERR("serial: send error while writing to client: %s\n", | ||
| strerror(errno)); | ||
| LOG_ERR("serial: connection reset\n"); | ||
| close(connfd); | ||
| connfd = -1; | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool serial_open(uint16_t port) { | ||
| if (sockfd >= 0) { | ||
| LOG_INFO("serial: port already open\n"); | ||
| return false; | ||
| } | ||
|
|
||
| sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
| if (sockfd == -1) { | ||
| LOG_ERR("serial: unable to socket(): %s\n", strerror(errno)); | ||
| return false; | ||
| } | ||
|
|
||
| if (port == 0) | ||
| port = SERIAL_TCP_PORT; | ||
|
|
||
| struct sockaddr_in server_addr; | ||
| memset(&server_addr, 0, sizeof(server_addr)); | ||
| server_addr.sin_family = AF_INET; | ||
| server_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
| server_addr.sin_port = htons(port); | ||
|
|
||
| if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){true}, | ||
| sizeof(int)) != 0) { | ||
| LOG_ERR("serial: unable to setsockopt(): %s\n", strerror(errno)); | ||
| return false; | ||
| } | ||
|
|
||
| if (bind(sockfd, (const struct sockaddr *)&server_addr, | ||
| sizeof(server_addr)) != 0) { | ||
| LOG_ERR("serial: unable to bind(): %s\n", strerror(errno)); | ||
| return false; | ||
| } | ||
|
|
||
| if (listen(sockfd, 1) != 0) { | ||
| LOG_ERR("serial: unable to listen(): %s\n", strerror(errno)); | ||
| return false; | ||
| } | ||
|
|
||
| FIFO_INIT(&tx_fifo); | ||
| FIFO_INIT(&rx_fifo); | ||
|
|
||
| sio2_attachPeripheral(SIO_CHANNEL_A, serial_getChar, serial_putChar); | ||
|
|
||
| LOG_INFO("serial: open ok\n"); | ||
| return true; | ||
| } | ||
|
|
||
| void serial_close(void) { | ||
| sio2_detachPeripheral(SIO_CHANNEL_A); | ||
|
|
||
| if (connfd != -1) | ||
| close(connfd); | ||
|
|
||
| if (sockfd != -1) | ||
| close(sockfd); | ||
|
|
||
| LOG_INFO("serial: close ok\n"); | ||
| } | ||
|
|
||
| static void serial_cleanup(void) { | ||
| serial_close(); | ||
| } | ||
|
|
||
| void serial_init(CEDAModule *mod) { | ||
| memset(mod, 0, sizeof(*mod)); | ||
| mod->init = serial_init; | ||
| mod->poll = serial_poll; | ||
| mod->cleanup = serial_cleanup; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #ifndef CEDA_SERIAL_PORT_H | ||
| #define CEDA_SERIAL_PORT_H | ||
|
|
||
| #include "module.h" | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
|
|
||
| void serial_init(CEDAModule *mod); | ||
|
|
||
| bool serial_open(uint16_t port); | ||
| void serial_close(void); | ||
|
|
||
| #endif // CEDA_SERIAL_PORT_H |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC you never check for return value
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This module implements the
CEDAModuleinterface (seesrc/module.h), which means that this function is polled in the main loop, so the check is performed during next loop iteration, and (if I did not get something wrong) nothing will break.Anyhow, I'm always printing the "accept client" message, which may actually be wrong. Fixed.