-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorvibosetup.c
147 lines (126 loc) · 4 KB
/
orvibosetup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* orvibosetup - A simple program to setup an Orvibo S20 wiFi plug.
*
* Copyright 2020, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* orvibosetup.c - Setup an Orvibo S20 WiFi plug or switch.
*
* SYNOPSYS:
*
* orvibosetup ssid
*
*/
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
static int OrviboSocket = -1;
static struct sockaddr_in OrviboBroadcast;
static void orvibo_socket (void) {
static int OrviboPort = 48899;
OrviboBroadcast.sin_family = AF_INET;
OrviboBroadcast.sin_port = htons(OrviboPort);
OrviboBroadcast.sin_addr.s_addr = INADDR_BROADCAST;
OrviboSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (OrviboSocket < 0) {
printf ("cannot open UDP socket: %s\n", strerror(errno));
exit(1);
}
int value = 1;
if (setsockopt(OrviboSocket, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)) < 0) {
printf ("cannot broadcast: %s\n", strerror(errno));
exit(1);
}
printf ("UDP socket is ready.\n");
}
static unsigned char hex2bin(char data) {
if (data >= '0' && data <= '9')
return data - '0';
if (data >= 'a' && data <= 'f')
return data - 'a' + 10;
if (data >= 'A' && data <= 'F')
return data - 'A' + 10;
return 0;
}
static char bin2hex (unsigned char d) {
d &= 0x0f;
if (d >= 0 && d <= 9) return '0' + d;
if (d >= 10 && d <= 15) return ('a' - 10) + d;
return '0';
}
static void orvibo_send (const char *d, const char *private) {
struct sockaddr_in a;
int sent = sendto (OrviboSocket, d, strlen(d), 0,
(struct sockaddr *)(&OrviboBroadcast),
sizeof(struct sockaddr_in));
if (sent < 0) {
printf ("** sendto() error: %s", strerror(errno));
exit(1);
}
if (!private)
printf ("Sending %s\n", d);
else {
char privacy[256];
strncpy (privacy, d, sizeof(privacy));
char *p = strstr (privacy, private);
int i = strlen(private);
while (--i>=0) *(p++) = '*';
printf ("Sending %s\n", privacy);
}
}
static void orvibo_receive (void) {
char data[128];
struct sockaddr_in addr;
int addrlen = sizeof(addr);
int size = recvfrom (OrviboSocket, data, sizeof(data), 0,
(struct sockaddr *)(&addr), &addrlen);
if (size <= 0) {
printf ("** recvfrom() error: %s", strerror(errno));
return;
}
printf ("Received: %s\n", data);
}
int main (int argc, char **argv) {
char buffer[256];
if (argc != 2) {
fprintf (stderr, "Invalid parameters: need SSID.\n");
exit(1);
}
snprintf (buffer, sizeof(buffer), "WiFi password for %s? ", argv[1]);
char *password = strdup(getpass(buffer));
fflush(stdout);
orvibo_socket ();
orvibo_send ("HF-A11ASSISTHREAD", 0);
orvibo_receive();
orvibo_send ("+ok", 0);
snprintf (buffer, sizeof(buffer), "AT+WSSSID=%s\r", argv[1]);
orvibo_send (buffer, 0);
orvibo_receive();
snprintf (buffer, sizeof(buffer), "AT+WSKEY=WPA2PSK,AES,%s\r", password);
orvibo_send (buffer, password);
orvibo_receive();
orvibo_send ("AT+WMODE=STA\r", 0);
orvibo_receive();
orvibo_send ("AT+Z\r", 0);
return 0;
}