| 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 <char_dev_iface.h> | 
|---|
| 30 | #include <errno.h> | 
|---|
| 31 | #include <ipc/serial_ctl.h> | 
|---|
| 32 | #include <loc.h> | 
|---|
| 33 | #include <stdio.h> | 
|---|
| 34 |  | 
|---|
| 35 | #define BUF_SIZE 1 | 
|---|
| 36 |  | 
|---|
| 37 | static void syntax_print(void) | 
|---|
| 38 | { | 
|---|
| 39 | fprintf(stderr, "Usage: sportdmp [--baud=<baud>] [device_service]\n"); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | int main(int argc, char **argv) | 
|---|
| 43 | { | 
|---|
| 44 | sysarg_t baud = 9600; | 
|---|
| 45 | service_id_t svc_id; | 
|---|
| 46 |  | 
|---|
| 47 | int arg = 1; | 
|---|
| 48 | int rc; | 
|---|
| 49 |  | 
|---|
| 50 | if (argc > arg && str_test_prefix(argv[arg], "--baud=")) { | 
|---|
| 51 | size_t arg_offset = str_lsize(argv[arg], 7); | 
|---|
| 52 | char* arg_str = argv[arg] + arg_offset; | 
|---|
| 53 | if (str_length(arg_str) == 0) { | 
|---|
| 54 | fprintf(stderr, "--baud requires an argument\n"); | 
|---|
| 55 | syntax_print(); | 
|---|
| 56 | return 1; | 
|---|
| 57 | } | 
|---|
| 58 | char *endptr; | 
|---|
| 59 | baud = strtol(arg_str, &endptr, 10); | 
|---|
| 60 | if (*endptr != '\0') { | 
|---|
| 61 | fprintf(stderr, "Invalid value for baud\n"); | 
|---|
| 62 | syntax_print(); | 
|---|
| 63 | return 1; | 
|---|
| 64 | } | 
|---|
| 65 | arg++; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | if (argc > arg) { | 
|---|
| 69 | rc = loc_service_get_id(argv[arg], &svc_id, 0); | 
|---|
| 70 | if (rc != EOK) { | 
|---|
| 71 | fprintf(stderr, "Cannot find device service %s\n", | 
|---|
| 72 | argv[arg]); | 
|---|
| 73 | return 1; | 
|---|
| 74 | } | 
|---|
| 75 | arg++; | 
|---|
| 76 | } | 
|---|
| 77 | else { | 
|---|
| 78 | category_id_t serial_cat_id; | 
|---|
| 79 |  | 
|---|
| 80 | rc = loc_category_get_id("serial", &serial_cat_id, 0); | 
|---|
| 81 | if (rc != EOK) { | 
|---|
| 82 | fprintf(stderr, "Failed getting id of category " | 
|---|
| 83 | "'serial'\n"); | 
|---|
| 84 | return 1; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | service_id_t *svc_ids; | 
|---|
| 88 | size_t svc_count; | 
|---|
| 89 |  | 
|---|
| 90 | rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count); | 
|---|
| 91 | if (rc != EOK) { | 
|---|
| 92 | fprintf(stderr, "Failed getting list of services\n"); | 
|---|
| 93 | return 1; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | if (svc_count == 0) { | 
|---|
| 97 | fprintf(stderr, "No service in category 'serial'\n"); | 
|---|
| 98 | free(svc_ids); | 
|---|
| 99 | return 1; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | svc_id = svc_ids[0]; | 
|---|
| 103 | free(svc_ids); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | if (argc > arg) { | 
|---|
| 107 | fprintf(stderr, "Too many arguments\n"); | 
|---|
| 108 | syntax_print(); | 
|---|
| 109 | return 1; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF, | 
|---|
| 114 | IPC_FLAG_BLOCKING); | 
|---|
| 115 | if (!sess) { | 
|---|
| 116 | fprintf(stderr, "Failed connecting to service\n"); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | async_exch_t *exch = async_exchange_begin(sess); | 
|---|
| 120 | rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud, | 
|---|
| 121 | SERIAL_NO_PARITY, 8, 1); | 
|---|
| 122 | async_exchange_end(exch); | 
|---|
| 123 |  | 
|---|
| 124 | if (rc != EOK) { | 
|---|
| 125 | fprintf(stderr, "Failed setting serial properties\n"); | 
|---|
| 126 | return 2; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | uint8_t *buf = (uint8_t *) malloc(BUF_SIZE); | 
|---|
| 130 | if (buf == NULL) { | 
|---|
| 131 | fprintf(stderr, "Failed allocating buffer\n"); | 
|---|
| 132 | return 3; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | while (true) { | 
|---|
| 136 | ssize_t read = char_dev_read(sess, buf, BUF_SIZE); | 
|---|
| 137 | if (read < 0) { | 
|---|
| 138 | fprintf(stderr, "Failed reading from serial device\n"); | 
|---|
| 139 | break; | 
|---|
| 140 | } | 
|---|
| 141 | ssize_t i; | 
|---|
| 142 | for (i = 0; i < read; i++) { | 
|---|
| 143 | printf("%02hhx ", buf[i]); | 
|---|
| 144 | } | 
|---|
| 145 | fflush(stdout); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | free(buf); | 
|---|
| 149 | return 0; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|