1 /***************************************************************************** |
|
2 * Copyright (C) 2014 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
7 * |
|
8 * This is free software; you can redistribute it and/or modify it |
|
9 * under the terms of the GNU General Public License as published by the |
|
10 * Free Software Foundation; either version 2, or (at your option) any |
|
11 * later version. |
|
12 * |
|
13 * mbsePi-apps is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with EC-65K; see the file COPYING. If not, write to the Free |
|
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
21 *****************************************************************************/ |
|
22 |
|
23 #include "rc433.h" |
|
24 |
|
25 #ifdef HAVE_WIRINGPI_H |
|
26 |
|
27 void help(void) |
|
28 { |
|
29 fprintf(stdout, "mbsePi-apps send v%s\n\n", VERSION); |
|
30 fprintf(stdout, "Usage: send [-t type ] [-f family ] [-g group ] [-d device] [-s state ] [-h]\n"); |
|
31 fprintf(stdout, " send -t A -g 00001 -d 00001 -s 1\n"); |
|
32 fprintf(stdout, " send -t B -g 2 -d 2 -s 0\n"); |
|
33 fprintf(stdout, " send -t C -f c -g 3 -d 3 -s 1\n"); |
|
34 fprintf(stdout, "\n"); |
|
35 fprintf(stdout, " send -t E -g A -d 2 -s 0\n"); |
|
36 } |
|
37 |
|
38 |
|
39 |
|
40 int isAcode(char *s) |
|
41 { |
|
42 int i; |
|
43 |
|
44 if (strlen(s) != 5) |
|
45 return FALSE; |
|
46 for (i = 0; i < 5; i++) { |
|
47 if ((s[i] < '0') || (s[i] > '1')) |
|
48 return FALSE; |
|
49 } |
|
50 |
|
51 return TRUE; |
|
52 } |
|
53 |
|
54 |
|
55 |
|
56 /* |
|
57 * A - char group, char device (2 x 5 binary) |
|
58 * B - int system, int unit |
|
59 * C - char family, int group, int device |
|
60 * D - char group, int device |
|
61 */ |
|
62 |
|
63 int main(int argc, char *argv[]) { |
|
64 char buf[127], *endptr; |
|
65 int c; |
|
66 char cType = '0', *sGroup = NULL, *sDevice = NULL, cFamily = '0', cGroup = '0'; |
|
67 int iState = -1, iGroup = -1, iDevice = -1; |
|
68 |
|
69 while (1) { |
|
70 int option_index = 0; |
|
71 static struct option long_options[] = { |
|
72 {"device", 1, 0, 'd'}, |
|
73 {"family", 1, 0, 'f'}, |
|
74 {"group", 1, 0, 'g'}, |
|
75 {"help", 0, 0, 'h'}, |
|
76 {"state", 1, 0, 's'}, |
|
77 {"type", 1, 0, 't'}, |
|
78 {0, 0, 0, 0} |
|
79 }; |
|
80 |
|
81 c = getopt_long(argc, argv, "d:f:g:hs:t:", long_options, &option_index); |
|
82 if (c == -1) |
|
83 break; |
|
84 |
|
85 switch (c) { |
|
86 case 'd': snprintf(buf, 127, "%s", optarg); |
|
87 if (cType == 'A') { |
|
88 sDevice = xstrcpy(buf); |
|
89 } else { |
|
90 iDevice = strtol(buf, &endptr, 10); |
|
91 } |
|
92 break; |
|
93 case 'f': snprintf(buf, 127, "%s", optarg); |
|
94 if (cType == 'C') { |
|
95 cFamily = tolower(buf[0]); |
|
96 } |
|
97 break; |
|
98 case 'g': snprintf(buf, 127, "%s", optarg); |
|
99 if (cType == 'A') { |
|
100 sGroup = xstrcpy(buf); |
|
101 } else if (cType == 'E') { |
|
102 cGroup = toupper(buf[0]); |
|
103 } else { |
|
104 iGroup = strtol(buf, &endptr, 10); |
|
105 } |
|
106 break; |
|
107 case 'h': help(); |
|
108 return 1; |
|
109 case 's': snprintf(buf, 127, "%s", optarg); |
|
110 iState = strtol(buf, &endptr, 10); |
|
111 break; |
|
112 case 't': snprintf(buf, 127, "%s", optarg); |
|
113 cType = toupper(buf[0]); |
|
114 break; |
|
115 } |
|
116 } |
|
117 |
|
118 /* |
|
119 * Check all option and combinations |
|
120 */ |
|
121 if ((cType < 'A') || (cType > 'E')) { |
|
122 fprintf(stderr, "Type must be A, B, C or E\n"); |
|
123 return 1; |
|
124 } |
|
125 if ((iState < 0) || (iState > 1)) { |
|
126 fprintf(stderr, "State must be 0 or 1\n"); |
|
127 return 1; |
|
128 } |
|
129 if (cType == 'A') { |
|
130 if (sGroup == NULL) { |
|
131 fprintf(stderr, "Group not set\n"); |
|
132 return 1; |
|
133 } |
|
134 if (sDevice == NULL) { |
|
135 fprintf(stderr, "Device not set\n"); |
|
136 return 1; |
|
137 } |
|
138 if (isAcode(sGroup) == FALSE) { |
|
139 fprintf(stderr, "Group must be 5 binary digits like 10000\n"); |
|
140 return 1; |
|
141 } |
|
142 if (isAcode(sDevice) == FALSE) { |
|
143 fprintf(stderr, "Device must be 5 binary digits like 10000\n"); |
|
144 return 1; |
|
145 } |
|
146 } else { |
|
147 if (iDevice == -1) { |
|
148 fprintf(stderr, "Device not set\n"); |
|
149 return 1; |
|
150 } |
|
151 } |
|
152 if ((cType == 'B') || (cType == 'C')) { |
|
153 if (iGroup == -1) { |
|
154 fprintf(stderr, "Group not set\n"); |
|
155 return 1; |
|
156 } |
|
157 if ((iGroup < 1) || (iGroup > 4)) { |
|
158 fprintf(stderr, "Group must be 1..4\n"); |
|
159 return 1; |
|
160 } |
|
161 if ((iDevice < 1) || (iDevice > 4)) { |
|
162 fprintf(stderr, "Device must be 1..4\n"); |
|
163 return 1; |
|
164 } |
|
165 } |
|
166 if (cType == 'E') { |
|
167 if ((iDevice < 1) || (iDevice > 3)) { |
|
168 fprintf(stderr, "Device must be 1..3\n"); |
|
169 return 1; |
|
170 } |
|
171 if ((cGroup < 'A') || (cGroup > 'D')) { |
|
172 fprintf(stderr, "Group must be A..D\n"); |
|
173 return 1; |
|
174 } |
|
175 } |
|
176 if (cType == 'C') { |
|
177 if ((cFamily < 'a') || (cFamily > 'f')) { |
|
178 fprintf(stderr, "Family must be a..f\n"); |
|
179 return 1; |
|
180 } |
|
181 } |
|
182 |
|
183 /* |
|
184 output PIN is hardcoded for testing purposes |
|
185 see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ |
|
186 for pin mapping of the raspberry pi GPIO connector |
|
187 */ |
|
188 int PIN = 0; |
|
189 |
|
190 if (wiringPiSetup () ) |
|
191 return 1; |
|
192 |
|
193 enableTransmit(PIN); |
|
194 switch (cType) { |
|
195 case 'A': |
|
196 printf("Type A sending group[%s] device[%s] command[%d]\n", sGroup, sDevice, iState); |
|
197 toggleTypeA(sGroup, sDevice, iState); |
|
198 break; |
|
199 case 'B': |
|
200 printf("Type B sending group[%d] device[%i] command[%i]\n", iGroup, iDevice, iState); |
|
201 toggleTypeB(iGroup, iDevice, iState); |
|
202 break; |
|
203 case 'C': |
|
204 printf("Type C sending family[%c] group[%d] device[%i] command[%i]\n", cFamily, iGroup, iDevice, iState); |
|
205 toggleTypeC(cFamily, iGroup, iDevice, iState); |
|
206 break; |
|
207 case 'E': |
|
208 printf("Type E sending group[%c] device[%i] command[%i]\n", cGroup, iDevice, iState); |
|
209 toggleTypeE(cGroup, iDevice, iState); |
|
210 break; |
|
211 } |
|
212 disableTransmit(); |
|
213 |
|
214 return 0; |
|
215 } |
|
216 |
|
217 #else |
|
218 |
|
219 int main(int argc, char *argv[]) { |
|
220 fprintf(stderr, "This program does nothing without the wiringPi library\n"); |
|
221 return 0; |
|
222 } |
|
223 |
|
224 #endif |
|
225 |
|