Deokgyu Yang | 2c7e86b | 2020-04-28 10:56:11 +0900 | [diff] [blame] | 1 | /* |
| 2 | * runRemote.c: |
| 3 | * Run the remote commands passed over the network link. |
| 4 | * |
| 5 | * Copyright (c) 2012-2017 Gordon Henderson |
| 6 | *********************************************************************** |
| 7 | * This file is part of wiringPi: |
| 8 | * https://projects.drogon.net/raspberry-pi/wiringpi/ |
| 9 | * |
| 10 | * wiringPi is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * wiringPi is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public License |
| 21 | * along with wiringPi. If not, see <http://www.gnu.org/licenses/>. |
| 22 | *********************************************************************** |
| 23 | */ |
| 24 | |
| 25 | #include <arpa/inet.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stdint.h> |
| 29 | #include <unistd.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
| 32 | //#include <stdarg.h> |
| 33 | |
| 34 | #include <wiringPi.h> |
| 35 | #include <wpiExtensions.h> |
| 36 | |
| 37 | #include "drcNetCmd.h" |
| 38 | #include "network.h" |
| 39 | #include "runRemote.h" |
| 40 | |
| 41 | |
| 42 | |
| 43 | int noLocalPins = FALSE ; |
| 44 | |
| 45 | |
| 46 | void runRemoteCommands (int fd) |
| 47 | { |
| 48 | register uint32_t pin ; |
| 49 | int len ; |
| 50 | struct drcNetComStruct cmd ; |
| 51 | |
| 52 | len = sizeof (struct drcNetComStruct) ; |
| 53 | |
| 54 | if (setsockopt (fd, SOL_SOCKET, SO_RCVLOWAT, (void *)&len, sizeof (len)) < 0) |
| 55 | return ; |
| 56 | |
| 57 | for (;;) |
| 58 | { |
| 59 | if (recv (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) // Probably remote hangup |
| 60 | return ; |
| 61 | |
| 62 | pin = cmd.pin ; |
| 63 | if (noLocalPins && ((pin & PI_GPIO_MASK) == 0)) |
| 64 | { |
| 65 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 66 | return ; |
| 67 | continue ; |
| 68 | } |
| 69 | |
| 70 | switch (cmd.cmd) |
| 71 | { |
| 72 | case DRCN_PIN_MODE: |
| 73 | pinMode (pin, cmd.data) ; |
| 74 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 75 | return ; |
| 76 | break ; |
| 77 | |
| 78 | case DRCN_PULL_UP_DN: |
| 79 | pullUpDnControl (pin, cmd.data) ; |
| 80 | break ; |
| 81 | |
| 82 | case DRCN_PWM_WRITE: |
| 83 | pwmWrite (pin, cmd.data) ; |
| 84 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 85 | return ; |
| 86 | break ; |
| 87 | |
| 88 | case DRCN_DIGITAL_WRITE: |
| 89 | digitalWrite (pin, cmd.data) ; |
| 90 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 91 | return ; |
| 92 | break ; |
| 93 | |
| 94 | case DRCN_DIGITAL_WRITE8: |
| 95 | //digitalWrite8 (pin, cmd.data) ; |
| 96 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 97 | return ; |
| 98 | break ; |
| 99 | |
| 100 | case DRCN_DIGITAL_READ: |
| 101 | cmd.data = digitalRead (pin) ; |
| 102 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 103 | return ; |
| 104 | break ; |
| 105 | |
| 106 | case DRCN_DIGITAL_READ8: |
| 107 | //cmd.data = digitalRead8 (pin) ; |
| 108 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 109 | return ; |
| 110 | break ; |
| 111 | |
| 112 | case DRCN_ANALOG_WRITE: |
| 113 | analogWrite (pin, cmd.data) ; |
| 114 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 115 | return ; |
| 116 | break ; |
| 117 | |
| 118 | case DRCN_ANALOG_READ: |
| 119 | cmd.data = analogRead (pin) ; |
| 120 | if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) |
| 121 | return ; |
| 122 | break ; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |