[01a7aa1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup nterm
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Network serial terminal emulator.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[3e6a98c5] | 35 | #include <stdbool.h>
|
---|
[01a7aa1] | 36 | #include <errno.h>
|
---|
| 37 | #include <io/console.h>
|
---|
| 38 | #include <stdio.h>
|
---|
[1d6dd2a] | 39 | #include <str.h>
|
---|
[01a7aa1] | 40 |
|
---|
| 41 | #include "conn.h"
|
---|
| 42 | #include "nterm.h"
|
---|
| 43 |
|
---|
| 44 | #define NAME "nterm"
|
---|
| 45 |
|
---|
| 46 | static console_ctrl_t *con;
|
---|
| 47 | static bool done;
|
---|
| 48 |
|
---|
| 49 | static void key_handle_ctrl(kbd_event_t *ev)
|
---|
| 50 | {
|
---|
| 51 | switch (ev->key) {
|
---|
| 52 | case KC_Q:
|
---|
| 53 | done = true;
|
---|
| 54 | break;
|
---|
| 55 | default:
|
---|
| 56 | break;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[28a5ebd] | 60 | static void send_char(char32_t c)
|
---|
[01a7aa1] | 61 | {
|
---|
| 62 | char cbuf[STR_BOUNDS(1)];
|
---|
| 63 | size_t offs;
|
---|
[b7fd2a0] | 64 | errno_t rc;
|
---|
[01a7aa1] | 65 |
|
---|
| 66 | offs = 0;
|
---|
| 67 | chr_encode(c, cbuf, &offs, STR_BOUNDS(1));
|
---|
| 68 |
|
---|
| 69 | rc = conn_send(cbuf, offs);
|
---|
| 70 | if (rc != EOK) {
|
---|
[7c912b6] | 71 | printf("[Failed sending data]\n");
|
---|
[01a7aa1] | 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | static void key_handle_unmod(kbd_event_t *ev)
|
---|
| 76 | {
|
---|
| 77 | switch (ev->key) {
|
---|
| 78 | case KC_ENTER:
|
---|
| 79 | send_char('\n');
|
---|
| 80 | break;
|
---|
| 81 | default:
|
---|
[84c86819] | 82 | if (ev->c >= 32 || ev->c == '\t' || ev->c == '\b') {
|
---|
[01a7aa1] | 83 | send_char(ev->c);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | static void key_handle(kbd_event_t *ev)
|
---|
| 89 | {
|
---|
| 90 | if ((ev->mods & KM_ALT) == 0 &&
|
---|
| 91 | (ev->mods & KM_SHIFT) == 0 &&
|
---|
| 92 | (ev->mods & KM_CTRL) != 0) {
|
---|
| 93 | key_handle_ctrl(ev);
|
---|
| 94 | } else if ((ev->mods & (KM_CTRL | KM_ALT)) == 0) {
|
---|
| 95 | key_handle_unmod(ev);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | void nterm_received(void *data, size_t size)
|
---|
| 100 | {
|
---|
[84c86819] | 101 | fwrite(data, size, 1, stdout);
|
---|
[7c912b6] | 102 | fflush(stdout);
|
---|
[01a7aa1] | 103 | }
|
---|
| 104 |
|
---|
| 105 | static void print_syntax(void)
|
---|
| 106 | {
|
---|
[a62ceaf] | 107 | printf("syntax: nterm <host>:<port>\n");
|
---|
[01a7aa1] | 108 | }
|
---|
| 109 |
|
---|
| 110 | int main(int argc, char *argv[])
|
---|
| 111 | {
|
---|
[07b7c48] | 112 | cons_event_t ev;
|
---|
[b7fd2a0] | 113 | errno_t rc;
|
---|
[01a7aa1] | 114 |
|
---|
[a62ceaf] | 115 | if (argc != 2) {
|
---|
[01a7aa1] | 116 | print_syntax();
|
---|
| 117 | return 1;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[a62ceaf] | 120 | rc = conn_open(argv[1]);
|
---|
[01a7aa1] | 121 | if (rc != EOK) {
|
---|
| 122 | printf("Error connecting.\n");
|
---|
| 123 | return 1;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | printf("Connection established.\n");
|
---|
| 127 |
|
---|
| 128 | con = console_init(stdin, stdout);
|
---|
| 129 |
|
---|
| 130 | done = false;
|
---|
| 131 | while (!done) {
|
---|
[87822ce] | 132 | rc = console_get_event(con, &ev);
|
---|
| 133 | if (rc != EOK)
|
---|
| 134 | break;
|
---|
| 135 |
|
---|
[07b7c48] | 136 | if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS)
|
---|
| 137 | key_handle(&ev.ev.key);
|
---|
[01a7aa1] | 138 | }
|
---|
| 139 |
|
---|
| 140 | return 0;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /** @}
|
---|
| 144 | */
|
---|