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