| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Martin Sucha
|
|---|
| 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 | #include <errno.h>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <devman.h>
|
|---|
| 32 | #include <ipc/devman.h>
|
|---|
| 33 | #include <device/char_dev.h>
|
|---|
| 34 | #include <ipc/serial_ctl.h>
|
|---|
| 35 |
|
|---|
| 36 | #define BUF_SIZE 1
|
|---|
| 37 |
|
|---|
| 38 | static void syntax_print() {
|
|---|
| 39 | fprintf(stderr, "Usage: sportdmp <baud> <device_path>\n");
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | int main(int argc, char **argv)
|
|---|
| 43 | {
|
|---|
| 44 | const char* devpath = "/hw/pci0/00:01.0/com1/a";
|
|---|
| 45 | sysarg_t baud = 9600;
|
|---|
| 46 |
|
|---|
| 47 | if (argc > 1) {
|
|---|
| 48 | char *endptr;
|
|---|
| 49 | baud = strtol(argv[1], &endptr, 10);
|
|---|
| 50 | if (*endptr != '\0') {
|
|---|
| 51 | fprintf(stderr, "Invalid value for baud\n");
|
|---|
| 52 | syntax_print();
|
|---|
| 53 | return 1;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (argc > 2) {
|
|---|
| 58 | devpath = argv[2];
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | if (argc > 3) {
|
|---|
| 62 | syntax_print();
|
|---|
| 63 | return 1;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | devman_handle_t device;
|
|---|
| 67 | int rc = devman_fun_get_handle(devpath, &device, IPC_FLAG_BLOCKING);
|
|---|
| 68 | if (rc != EOK) {
|
|---|
| 69 | fprintf(stderr, "Cannot open device %s\n", devpath);
|
|---|
| 70 | return 1;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | async_sess_t *sess = devman_device_connect(EXCHANGE_SERIALIZE, device,
|
|---|
| 74 | IPC_FLAG_BLOCKING);
|
|---|
| 75 | if (!sess) {
|
|---|
| 76 | fprintf(stderr, "Cannot connect device\n");
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 80 | rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud,
|
|---|
| 81 | SERIAL_NO_PARITY, 8, 1);
|
|---|
| 82 | async_exchange_end(exch);
|
|---|
| 83 |
|
|---|
| 84 | if (rc != EOK) {
|
|---|
| 85 | fprintf(stderr, "Cannot set serial properties\n");
|
|---|
| 86 | return 2;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | uint8_t *buf = (uint8_t *) malloc(BUF_SIZE);
|
|---|
| 90 | if (buf == NULL) {
|
|---|
| 91 | fprintf(stderr, "Cannot allocate buffer\n");
|
|---|
| 92 | return 3;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | while (true) {
|
|---|
| 96 | ssize_t read = char_dev_read(sess, buf, BUF_SIZE);
|
|---|
| 97 | if (read < 0) {
|
|---|
| 98 | fprintf(stderr, "Failed reading from serial device\n");
|
|---|
| 99 | break;
|
|---|
| 100 | }
|
|---|
| 101 | ssize_t i;
|
|---|
| 102 | for (i = 0; i < read; i++) {
|
|---|
| 103 | if ((buf[i] >= 32) && (buf[i] < 128))
|
|---|
| 104 | putchar((wchar_t) buf[i]);
|
|---|
| 105 | else
|
|---|
| 106 | putchar('.');
|
|---|
| 107 | fflush(stdout);
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | free(buf);
|
|---|
| 112 | return 0;
|
|---|
| 113 | }
|
|---|