| 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 |  | 
|---|
| 35 | #include <bool.h> | 
|---|
| 36 | #include <errno.h> | 
|---|
| 37 | #include <io/console.h> | 
|---|
| 38 | #include <stdio.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include "conn.h" | 
|---|
| 41 | #include "nterm.h" | 
|---|
| 42 |  | 
|---|
| 43 | #define NAME "nterm" | 
|---|
| 44 |  | 
|---|
| 45 | static console_ctrl_t *con; | 
|---|
| 46 | static bool done; | 
|---|
| 47 |  | 
|---|
| 48 | static void key_handle_ctrl(kbd_event_t *ev) | 
|---|
| 49 | { | 
|---|
| 50 | switch (ev->key) { | 
|---|
| 51 | case KC_Q: | 
|---|
| 52 | done = true; | 
|---|
| 53 | break; | 
|---|
| 54 | default: | 
|---|
| 55 | break; | 
|---|
| 56 | } | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | static void send_char(wchar_t c) | 
|---|
| 60 | { | 
|---|
| 61 | char cbuf[STR_BOUNDS(1)]; | 
|---|
| 62 | size_t offs; | 
|---|
| 63 | int rc; | 
|---|
| 64 |  | 
|---|
| 65 | offs = 0; | 
|---|
| 66 | chr_encode(c, cbuf, &offs, STR_BOUNDS(1)); | 
|---|
| 67 |  | 
|---|
| 68 | rc = conn_send(cbuf, offs); | 
|---|
| 69 | if (rc != EOK) { | 
|---|
| 70 | printf("[Failed sending data]\n"); | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | static void key_handle_unmod(kbd_event_t *ev) | 
|---|
| 75 | { | 
|---|
| 76 | switch (ev->key) { | 
|---|
| 77 | case KC_ENTER: | 
|---|
| 78 | send_char('\n'); | 
|---|
| 79 | break; | 
|---|
| 80 | default: | 
|---|
| 81 | if (ev->c >= 32 || ev->c == '\t' || ev->c == '\b') { | 
|---|
| 82 | send_char(ev->c); | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | static void key_handle(kbd_event_t *ev) | 
|---|
| 88 | { | 
|---|
| 89 | if ((ev->mods & KM_ALT) == 0 && | 
|---|
| 90 | (ev->mods & KM_SHIFT) == 0 && | 
|---|
| 91 | (ev->mods & KM_CTRL) != 0) { | 
|---|
| 92 | key_handle_ctrl(ev); | 
|---|
| 93 | } else if ((ev->mods & (KM_CTRL | KM_ALT)) == 0) { | 
|---|
| 94 | key_handle_unmod(ev); | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void nterm_received(void *data, size_t size) | 
|---|
| 99 | { | 
|---|
| 100 | fwrite(data, size, 1, stdout); | 
|---|
| 101 | fflush(stdout); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | static void print_syntax(void) | 
|---|
| 105 | { | 
|---|
| 106 | printf("syntax: nterm <ip-address> <port>\n"); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | int main(int argc, char *argv[]) | 
|---|
| 110 | { | 
|---|
| 111 | kbd_event_t ev; | 
|---|
| 112 | int rc; | 
|---|
| 113 |  | 
|---|
| 114 | if (argc != 3) { | 
|---|
| 115 | print_syntax(); | 
|---|
| 116 | return 1; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | rc = conn_open(argv[1], argv[2]); | 
|---|
| 120 | if (rc != EOK) { | 
|---|
| 121 | printf("Error connecting.\n"); | 
|---|
| 122 | return 1; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | printf("Connection established.\n"); | 
|---|
| 126 |  | 
|---|
| 127 | con = console_init(stdin, stdout); | 
|---|
| 128 |  | 
|---|
| 129 | done = false; | 
|---|
| 130 | while (!done) { | 
|---|
| 131 | console_get_kbd_event(con, &ev); | 
|---|
| 132 | if (ev.type == KEY_PRESS) | 
|---|
| 133 | key_handle(&ev); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | return 0; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | /** @} | 
|---|
| 140 | */ | 
|---|