[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 |
|
---|
[5c65e61] | 29 | #include <char_dev_iface.h>
|
---|
[aa91105] | 30 | #include <errno.h>
|
---|
[c0e53ff] | 31 | #include <ipc/serial_ctl.h>
|
---|
[aa91105] | 32 | #include <loc.h>
|
---|
| 33 | #include <stdio.h>
|
---|
[c0e53ff] | 34 |
|
---|
| 35 | #define BUF_SIZE 1
|
---|
| 36 |
|
---|
[aa91105] | 37 | static void syntax_print(void)
|
---|
| 38 | {
|
---|
[0aa300d] | 39 | fprintf(stderr, "Usage: sportdmp [--baud=<baud>] [device_service]\n");
|
---|
[a281aec5] | 40 | }
|
---|
| 41 |
|
---|
[c0e53ff] | 42 | int main(int argc, char **argv)
|
---|
| 43 | {
|
---|
[a281aec5] | 44 | sysarg_t baud = 9600;
|
---|
[5ea75f0] | 45 | service_id_t svc_id;
|
---|
[69f5f19] | 46 |
|
---|
[0aa300d] | 47 | int arg = 1;
|
---|
[5ea75f0] | 48 | int rc;
|
---|
[69f5f19] | 49 |
|
---|
[0aa300d] | 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 | }
|
---|
[a281aec5] | 58 | char *endptr;
|
---|
[0aa300d] | 59 | baud = strtol(arg_str, &endptr, 10);
|
---|
[a281aec5] | 60 | if (*endptr != '\0') {
|
---|
| 61 | fprintf(stderr, "Invalid value for baud\n");
|
---|
| 62 | syntax_print();
|
---|
| 63 | return 1;
|
---|
| 64 | }
|
---|
[0aa300d] | 65 | arg++;
|
---|
[a281aec5] | 66 | }
|
---|
[69f5f19] | 67 |
|
---|
[0aa300d] | 68 | if (argc > arg) {
|
---|
[5ea75f0] | 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 | }
|
---|
[0aa300d] | 75 | arg++;
|
---|
[a281aec5] | 76 | }
|
---|
[5ea75f0] | 77 | else {
|
---|
| 78 | category_id_t serial_cat_id;
|
---|
[69f5f19] | 79 |
|
---|
[5ea75f0] | 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 | }
|
---|
[69f5f19] | 86 |
|
---|
[5ea75f0] | 87 | service_id_t *svc_ids;
|
---|
| 88 | size_t svc_count;
|
---|
[69f5f19] | 89 |
|
---|
| 90 | rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count);
|
---|
| 91 | if (rc != EOK) {
|
---|
[5ea75f0] | 92 | fprintf(stderr, "Failed getting list of services\n");
|
---|
| 93 | return 1;
|
---|
| 94 | }
|
---|
[69f5f19] | 95 |
|
---|
[5ea75f0] | 96 | if (svc_count == 0) {
|
---|
| 97 | fprintf(stderr, "No service in category 'serial'\n");
|
---|
| 98 | free(svc_ids);
|
---|
| 99 | return 1;
|
---|
| 100 | }
|
---|
[69f5f19] | 101 |
|
---|
[5ea75f0] | 102 | svc_id = svc_ids[0];
|
---|
| 103 | free(svc_ids);
|
---|
| 104 | }
|
---|
[69f5f19] | 105 |
|
---|
[0aa300d] | 106 | if (argc > arg) {
|
---|
| 107 | fprintf(stderr, "Too many arguments\n");
|
---|
[a281aec5] | 108 | syntax_print();
|
---|
| 109 | return 1;
|
---|
| 110 | }
|
---|
[69f5f19] | 111 |
|
---|
| 112 |
|
---|
[f9b2cb4c] | 113 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
---|
[c0e53ff] | 114 | IPC_FLAG_BLOCKING);
|
---|
| 115 | if (!sess) {
|
---|
[5ea75f0] | 116 | fprintf(stderr, "Failed connecting to service\n");
|
---|
[c0e53ff] | 117 | }
|
---|
[69f5f19] | 118 |
|
---|
[c0e53ff] | 119 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[a281aec5] | 120 | rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud,
|
---|
[c0e53ff] | 121 | SERIAL_NO_PARITY, 8, 1);
|
---|
| 122 | async_exchange_end(exch);
|
---|
[69f5f19] | 123 |
|
---|
[c0e53ff] | 124 | if (rc != EOK) {
|
---|
[aa91105] | 125 | fprintf(stderr, "Failed setting serial properties\n");
|
---|
[c0e53ff] | 126 | return 2;
|
---|
| 127 | }
|
---|
[69f5f19] | 128 |
|
---|
[c0e53ff] | 129 | uint8_t *buf = (uint8_t *) malloc(BUF_SIZE);
|
---|
| 130 | if (buf == NULL) {
|
---|
[aa91105] | 131 | fprintf(stderr, "Failed allocating buffer\n");
|
---|
[c0e53ff] | 132 | return 3;
|
---|
| 133 | }
|
---|
[69f5f19] | 134 |
|
---|
[c0e53ff] | 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++) {
|
---|
[4c73361] | 143 | printf("%02hhx ", buf[i]);
|
---|
[c0e53ff] | 144 | }
|
---|
[4c73361] | 145 | fflush(stdout);
|
---|
[c0e53ff] | 146 | }
|
---|
[69f5f19] | 147 |
|
---|
[c0e53ff] | 148 | free(buf);
|
---|
| 149 | return 0;
|
---|
| 150 | }
|
---|
[69f5f19] | 151 |
|
---|