forked from jkbenaim/hs100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.c
65 lines (54 loc) · 1.45 KB
/
handlers.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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "comms.h"
char *handler_associate(int argc, char *argv[])
{
const char *template =
"{\"netif\":{\"set_stainfo\":{\"ssid\":\"%s\",\"password\":"
"\"%s\",\"key_type\":%d}}}";
char *plug_addr = argv[1];
char *ssid = argv[3];
char *password = argv[4];
char *key_type = argv[5];
char *endptr, *msg, *response;
int key_type_num;
size_t len;
if (argc < 6) {
fprintf(stderr, "not enough arguments\n");
exit(1);
}
errno = 0;
key_type_num = (int)strtol(key_type, &endptr, 10);
if (errno || endptr == key_type) {
fprintf(stderr, "invalid key type: %s\n", key_type);
exit(1);
}
len = snprintf(NULL, 0, template, ssid, password,
key_type_num);
len++; /* snprintf does not count the null terminator */
msg = calloc(1, len);
snprintf(msg, len, template, ssid, password, key_type_num);
response = hs100_send(plug_addr, msg);
return response;
}
char *handler_set_server(int argc, char *argv[])
{
const char *template =
"{\"cnCloud\":{\"set_server_url\":{\"server\":\"%s\"}}}";
char *plug_addr = argv[1];
char *server = argv[3];
size_t len;
char *msg, *response;
if (argc < 4) {
fprintf(stderr, "not enough arguments\n");
exit(1);
}
len = snprintf(NULL, 0, template, server);
len++; /* snprintf does not count the null terminator */
msg = calloc(1, len);
snprintf(msg, len, template, server);
response = hs100_send(plug_addr, msg);
return response;
}