source: mainline/uspace/app/nterm/nterm.c@ 01a7aa1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 01a7aa1 was 01a7aa1, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Add a trivial network terminal application.

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
45static console_ctrl_t *con;
46static bool done;
47
48static 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
59static 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
74static 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') {
82 send_char(ev->c);
83 }
84 }
85}
86
87static 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
98void nterm_received(void *data, size_t size)
99{
100 uint8_t *dp = (uint8_t *)data;
101 size_t i;
102
103// printf("Received %zu bytes.\n", size);
104 for (i = 0; i < size; i++) {
105// printf("0x%02x\n", dp[i]);
106 putchar(dp[i]);
107 }
108}
109
110static void print_syntax(void)
111{
112 printf("syntax: nterm <ip-address> <port>\n");
113}
114
115int main(int argc, char *argv[])
116{
117 kbd_event_t ev;
118 int rc;
119
120 if (argc != 3) {
121 print_syntax();
122 return 1;
123 }
124
125 rc = conn_open(argv[1], argv[2]);
126 if (rc != EOK) {
127 printf("Error connecting.\n");
128 return 1;
129 }
130
131 printf("Connection established.\n");
132
133 con = console_init(stdin, stdout);
134
135 done = false;
136 while (!done) {
137 console_get_kbd_event(con, &ev);
138 if (ev.type == KEY_PRESS)
139 key_handle(&ev);
140 }
141
142 return 0;
143}
144
145/** @}
146 */
Note: See TracBrowser for help on using the repository browser.