forked from nrfconnect/sdk-wi-fiquicktrack-controlappc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqt_client.c
159 lines (141 loc) · 6.53 KB
/
qt_client.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
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Copyright (c) 2023 Wi-Fi Alliance */
/* Permission to use, copy, modify, and/or distribute this software for any */
/* purpose with or without fee is hereby granted, provided that the above */
/* copyright notice and this permission notice appear in all copies. */
/* THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL */
/* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL */
/* THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR */
/* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING */
/* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF */
/* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT */
/* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS */
/* SOFTWARE. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef CONFIG_ZEPHYR
#include <zephyr/net/socket.h>
#include <zephyr/net/net_ip.h>
#include <zephyr/shell/shell.h>
#else
#include <unistd.h>
#include <netinet/in.h>
#endif
#include <errno.h>
#include "vendor_specific.h"
#include "eloop.h"
#include "indigo_api.h"
#include "utils.h"
struct sockaddr_in *tool_addr; // For HTTP Post
/* Callback function of the QuickTrack API. */
static void control_receive_message(int sock, void *eloop_ctx, void *sock_ctx) {
int ret; // return code
int fromlen, len; // structure size and received length
struct sockaddr_storage from; // source address of the message
char buffer[BUFFER_LEN]; // buffer to receive the message
struct packet_wrapper req, resp; // packet wrapper for the received message and response
struct indigo_api *api = NULL; // used for API search, validation and handler call
(void) eloop_ctx;
(void) sock_ctx;
/* Receive request */
fromlen = sizeof(from);
len = recvfrom(sock, buffer, BUFFER_LEN, 0, (struct sockaddr *) &from, (socklen_t*)&fromlen);
if (len < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Server: Failed to receive the packet");
return ;
} else {
indigo_logger(LOG_LEVEL_DEBUG, "Server: Receive the packet");
}
tool_addr = (struct sockaddr_in *)&from;
/* Parse request to HDR and TLV. Response NACK if parser fails. Otherwises, ACK. */
memset(&req, 0, sizeof(struct packet_wrapper));
memset(&resp, 0, sizeof(struct packet_wrapper));
ret = parse_packet(&req, buffer, len);
if (ret == 0) {
indigo_logger(LOG_LEVEL_DEBUG, "Server: Parsed packet successfully");
} else {
indigo_logger(LOG_LEVEL_ERROR, "Server: Failed to parse the packet");
fill_wrapper_ack(&resp, req.hdr.seq, 0x31, "Unable to parse the packet");
len = assemble_packet(buffer, BUFFER_LEN, &resp);
sendto(sock, (const char *)buffer, len, MSG_CONFIRM, (const struct sockaddr *) &from, fromlen);
goto done;
}
/* Find API by ID. If API is not supported, assemble NACK. */
api = get_api_by_id(req.hdr.type);
if (api) {
indigo_logger(LOG_LEVEL_DEBUG, "API %s: Found handler", api->name);
} else {
indigo_logger(LOG_LEVEL_ERROR, "API Unknown (0x%04x): No registered handler", req.hdr.type);
fill_wrapper_ack(&resp, req.hdr.seq, 0x31, "Unable to find the API handler");
len = assemble_packet(buffer, BUFFER_LEN, &resp);
sendto(sock, (const char *)buffer, len, MSG_CONFIRM, (const struct sockaddr *) &from, fromlen);
goto done;
}
/* Verify. Optional. If validation is failed, then return NACK. */
if (api->verify == NULL || (api->verify && api->verify(&req, &resp) == 0)) {
indigo_logger(LOG_LEVEL_INFO, "API %s: Return ACK", api->name);
fill_wrapper_ack(&resp, req.hdr.seq, 0x30, "ACK: Command received");
len = assemble_packet(buffer, BUFFER_LEN, &resp);
sendto(sock, (const char *)buffer, len, MSG_CONFIRM, (const struct sockaddr *) &from, fromlen);
free_packet_wrapper(&resp);
} else {
indigo_logger(LOG_LEVEL_ERROR, "API %s: Failed to verify and return NACK", api->name);
fill_wrapper_ack(&resp, req.hdr.seq, 1, "Unable to find the API handler");
len = assemble_packet(buffer, BUFFER_LEN, &resp);
sendto(sock, (const char *)buffer, len, MSG_CONFIRM, (const struct sockaddr *) &from, fromlen);
goto done;
}
/* Optional, use timer to handle the execution */
/* Handle & Response. Call API handle(), assemble packet by response wrapper and send back to source address. */
if (api->handle && api->handle(&req, &resp) == 0) {
indigo_logger(LOG_LEVEL_INFO, "API %s: Return execution result", api->name);
len = assemble_packet(buffer, BUFFER_LEN, &resp);
sendto(sock, (const char *)buffer, len, MSG_CONFIRM, (const struct sockaddr *) &from, fromlen);
#ifdef CONFIG_ZEPHYR
if(!strcmp(api->name, "DEVICE_RESET")) {
k_msleep(CONFIG_WFA_QT_REBOOT_TIMEOUT_MS);
shell_execute_cmd(NULL, "kernel reboot cold");
}
#endif
} else {
indigo_logger(LOG_LEVEL_DEBUG, "API %s (0x%04x): No handle function", api ? api->name : "Unknown", req.hdr.type);
}
done:
/* Clean up resource */
free_packet_wrapper(&req);
free_packet_wrapper(&resp);
indigo_logger(LOG_LEVEL_DEBUG, "API %s: Complete", api ? api->name : "Unknown");
}
/* Initiate the service port. */
int control_socket_init(int port) {
int s = -1;
char cmd[S_BUFFER_LEN];
struct sockaddr_in addr;
/* Open UDP socket */
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to open server socket: %s", strerror(errno));
return -1;
}
/* Bind specific port */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to bind server socket: %s", strerror(errno));
if (errno == EADDRINUSE) {
sprintf(cmd, "netstat -lunatp | grep %d", port);
system(cmd);
}
close(s);
return -1;
}
/* Register to eloop and ready for the socket event */
if (qt_eloop_register_read_sock(s, control_receive_message, NULL, NULL)) {
indigo_logger(LOG_LEVEL_ERROR, "Failed to initiate ControlAppC");
close(s);
return -1;
}
return s;
}