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