-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpic-web.c
123 lines (111 loc) · 3.58 KB
/
rpic-web.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
/******************************************************************************
* Copyright (C) 2012 - 2013, Leonid Zolotarev
*
* Licensed under the terms of the BSD license, see file COPYING
* for details.
*
* Raspberry Pi Car Controller
*
* Source file for the rpic-web
*
*****************************************************************************/
#include <string.h>
#include <stdio.h>
#include "mongoose.h"
#include "rpic-lib.h"
#include "rpic-web.h"
#include "rpic-sys.h"
/*****************************************************************************/
static const char* rpic_web_reply =
"HTTP/1.1 200 OK\r\n"
"Cache: no-cache\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %d\r\n"
"\r\n";
static void rpic_web_send_data(struct mg_connection *conn, const char* data)
{
mg_printf(conn, rpic_web_reply, strlen(data));
mg_write(conn, data, strlen(data));
}
static int rpic_web_check_command(struct mg_connection *conn, const char* command)
{
if (command != NULL) {
if (strcmp(command, rpic_commands[RPIC_CMD_FWRD]) == 0) {
rpi_car_forward();
return RPIC_CMD_FWRD;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_BKWD]) == 0) {
rpi_car_backward();
return RPIC_CMD_BKWD;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_STOP]) == 0) {
rpi_car_stop();
return RPIC_CMD_STOP;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_LEFT]) == 0) {
rpi_car_left();
return RPIC_CMD_LEFT;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_RGHT]) == 0) {
rpi_car_right();
return RPIC_CMD_RGHT;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_DCL]) == 0) {
rpi_car_deccelerate();
return RPIC_CMD_DCL;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_ACL]) == 0) {
rpi_car_accelerate();
return RPIC_CMD_ACL;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_VER]) == 0) {
const char* data = rpi_car_dev_version();
if (data != NULL) {
rpic_web_send_data(conn, data);
}
return RPIC_CMD_VER;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_TIME]) == 0) {
const char* data = rpi_sys_time();
if (data != NULL) {
rpic_web_send_data(conn, data);
}
return RPIC_CMD_TIME;
}
else if (strcmp(command, rpic_commands[RPIC_CMD_UPTIME]) == 0) {
const char* data = rpi_sys_uptime();
if (data != NULL) {
rpic_web_send_data(conn, data);
}
return RPIC_CMD_UPTIME;
}
}
return RPIC_CMD_VOID;
}
static void *callback(enum mg_event event,
struct mg_connection *conn)
{
const struct mg_request_info *ri = mg_get_request_info(conn);
if (event == MG_NEW_REQUEST) {
if (rpic_web_check_command(conn, ri->query_string) != RPIC_CMD_VOID) {
printf("Received supported command from HTTP: %s\n", ri->query_string);
return "";
}
}
return NULL;
}
int main()
{
struct mg_context *ctx;
const char *options[] = {
"listening_ports", "3001",
"document_root", "rpicweb",
NULL
};
int dev = rpi_car_dev_open();
ctx = mg_start(&callback, NULL, options);
getchar(); // Wait until user hits "enter"
mg_stop(ctx);
rpi_car_dev_close(dev);
return 0;
}