Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 4677 | trochtova | 1 | /* |
| 2 | * Copyright (c) 2009 Lenka Trochtova |
||
| 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 | /** @addtogroup shutters |
||
| 30 | * @brief closing/opening shutters |
||
| 31 | * @{ |
||
| 32 | */ |
||
| 33 | /** |
||
| 34 | * @file |
||
| 35 | */ |
||
| 36 | |||
| 37 | #include <stdio.h> |
||
| 38 | #include <ipc/ipc.h> |
||
| 39 | #include <sys/types.h> |
||
| 40 | #include <atomic.h> |
||
| 41 | #include <ipc/devmap.h> |
||
| 42 | #include <async.h> |
||
| 43 | #include <ipc/services.h> |
||
| 44 | #include <ipc/serial.h> |
||
| 45 | #include <as.h> |
||
| 46 | #include <sysinfo.h> |
||
| 47 | #include <errno.h> |
||
| 48 | |||
| 49 | |||
| 50 | #include <stdlib.h> |
||
| 51 | #include <unistd.h> |
||
| 52 | #include <fcntl.h> |
||
| 53 | #include <sys/stat.h> |
||
| 54 | |||
| 55 | #include <string.h> |
||
| 56 | |||
| 57 | #define NAME "shutters" |
||
| 58 | #define MYROOM 2 |
||
| 59 | #define UpSection1 25 |
||
| 60 | #define DwnSection1 26 |
||
| 61 | #define UpSection2 27 |
||
| 62 | #define DwnSection2 28 |
||
| 63 | |||
| 64 | static int device_get_handle(const char *name, dev_handle_t *handle); |
||
| 65 | static void print_usage(); |
||
| 66 | static void move_shutters(const char * serial_dev_name, char room, char cmd); |
||
| 67 | static char getcmd(bool wnd, bool up); |
||
| 68 | static bool is_com_dev(const char *dev_name); |
||
| 69 | |||
| 70 | static int device_get_handle(const char *name, dev_handle_t *handle) |
||
| 71 | { |
||
| 72 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0); |
||
| 73 | if (phone < 0) |
||
| 74 | return phone; |
||
| 75 | |||
| 76 | ipc_call_t answer; |
||
| 77 | aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0, &answer); |
||
| 78 | |||
| 79 | ipcarg_t retval = ipc_data_write_start(phone, name, str_length(name) + 1); |
||
| 80 | |||
| 81 | if (retval != EOK) { |
||
| 82 | async_wait_for(req, NULL); |
||
| 83 | ipc_hangup(phone); |
||
| 84 | return retval; |
||
| 85 | } |
||
| 86 | |||
| 87 | async_wait_for(req, &retval); |
||
| 88 | |||
| 89 | if (handle != NULL) |
||
| 90 | *handle = -1; |
||
| 91 | |||
| 92 | if (retval == EOK) { |
||
| 93 | if (handle != NULL) |
||
| 94 | *handle = (dev_handle_t) IPC_GET_ARG1(answer); |
||
| 95 | } |
||
| 96 | |||
| 97 | ipc_hangup(phone); |
||
| 98 | return retval; |
||
| 99 | } |
||
| 100 | |||
| 101 | static void print_usage() |
||
| 102 | { |
||
| 103 | printf("Usage: \n shutters comN shutter direction \n where 'comN' is a serial port, 'shutter' is either 'window' or 'door' and direction is 'up' or 'down'.\n"); |
||
| 104 | } |
||
| 105 | |||
| 106 | static void move_shutters(const char * serial_dev_name, char room, char cmd) |
||
| 107 | { |
||
| 108 | dev_handle_t serial_dev_handle = -1; |
||
| 109 | |||
| 110 | if (device_get_handle(serial_dev_name, &serial_dev_handle) != EOK) { |
||
| 111 | printf(NAME ": could not get the handle of %s.\n", serial_dev_name); |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | |||
| 115 | printf(NAME ": got the handle of %s.\n", serial_dev_name); |
||
| 116 | |||
| 117 | int dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, serial_dev_handle); |
||
| 118 | if(dev_phone < 0) { |
||
| 119 | printf(NAME ": could not connect to %s device.\n", serial_dev_name); |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | async_req_1_0(dev_phone, SERIAL_PUTCHAR, 0x55); |
||
| 124 | async_req_1_0(dev_phone, SERIAL_PUTCHAR, room); |
||
| 125 | async_req_1_0(dev_phone, SERIAL_PUTCHAR, cmd); |
||
| 126 | async_req_1_0(dev_phone, SERIAL_PUTCHAR, 240); |
||
| 127 | } |
||
| 128 | |||
| 129 | static char getcmd(bool wnd, bool up) |
||
| 130 | { |
||
| 131 | char cmd = 0; |
||
| 132 | if (wnd) { |
||
| 133 | if (up) { |
||
| 134 | cmd = UpSection2; |
||
| 135 | } |
||
| 136 | else { |
||
| 137 | cmd = DwnSection2; |
||
| 138 | } |
||
| 139 | } else { |
||
| 140 | if (up) { |
||
| 141 | cmd = UpSection1; |
||
| 142 | } |
||
| 143 | else { |
||
| 144 | cmd = DwnSection1; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | return cmd; |
||
| 148 | } |
||
| 149 | |||
| 150 | /* |
||
| 151 | * The name of a serial device must be between 'com0' and 'com9'. |
||
| 152 | */ |
||
| 153 | static bool is_com_dev(const char *dev_name) |
||
| 154 | { |
||
| 155 | if (str_length(dev_name) != 4) { |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (str_cmp("com0", dev_name) > 0) { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | if (str_cmp(dev_name, "com9") > 0) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | return true; |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | int main(int argc, char *argv[]) |
||
| 172 | { |
||
| 173 | if (argc != 4) { |
||
| 174 | printf(NAME ": incorrect number of arguments.\n"); |
||
| 175 | print_usage(); |
||
| 176 | return 0; |
||
| 177 | } |
||
| 178 | |||
| 179 | bool wnd, up; |
||
| 180 | const char *serial_dev = argv[1]; |
||
| 181 | |||
| 182 | if (!is_com_dev(serial_dev)) { |
||
| 183 | printf(NAME ": the first argument is not correct.\n"); |
||
| 184 | print_usage(); |
||
| 185 | return 0; |
||
| 186 | } |
||
| 187 | |||
| 188 | if (str_cmp(argv[2], "window") == 0) { |
||
| 189 | wnd = true; |
||
| 190 | } else if (str_cmp(argv[2], "door") == 0) { |
||
| 191 | wnd = false; |
||
| 192 | } else { |
||
| 193 | printf(NAME ": the second argument is not correct.\n"); |
||
| 194 | print_usage(); |
||
| 195 | return 0; |
||
| 196 | } |
||
| 197 | |||
| 198 | if (str_cmp(argv[2], "window") == 0) { |
||
| 199 | wnd = true; |
||
| 200 | } else if (str_cmp(argv[2], "door") == 0) { |
||
| 201 | wnd = false; |
||
| 202 | } else { |
||
| 203 | printf(NAME ": the third argument is not correct.\n"); |
||
| 204 | print_usage(); |
||
| 205 | return 0; |
||
| 206 | } |
||
| 207 | |||
| 208 | if (str_cmp(argv[3], "up") == 0) { |
||
| 209 | up = true; |
||
| 210 | } else if (str_cmp(argv[3], "down") == 0) { |
||
| 211 | up = false; |
||
| 212 | } else { |
||
| 213 | printf(NAME ": the second argument is not correct.\n"); |
||
| 214 | print_usage(); |
||
| 215 | return 0; |
||
| 216 | } |
||
| 217 | |||
| 218 | char cmd = getcmd(wnd, up); |
||
| 219 | move_shutters(serial_dev, MYROOM, cmd); |
||
| 220 | |||
| 221 | return 0; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** @} |
||
| 225 | */ |