Simple WebSocket server to control a tank.

Dependencies:   SNICInterface_mod WebSocketServer mbed-rtos mbed PowerControl C12832

au Firefox OS WoTハッカソン on ホワイトデーで使用した、タンクを動かすプログラムです。 ゲームパッドでタンクを操縦します。

ゲームパッドは PC に接続し、ブラウザ上の Web アプリから Gamepad API で入力を取得します。 取得した入力データは WebSocket で mbed 上の WebSocket サーバに送信します。

WebSocket サーバのコードはライブラリ化したので、他のプログラムでもインポートして使えます。

使用した機材

  • LPC1768
  • Murata TypeYD
  • LPC1768 用アプリケーションボード
  • TAMIYA トラック&ホイールセット
  • TAMIYA ダブルキヤボックス(左右独立4速タイプ)
  • TAMIYA ユニバーサルプレート
  • TOSHIBA TA7291P x 2
  • その他、モバイルバッテリー、電池ボックス等

左右のモータードライバにそれぞれ LPC1768 の p12, p13 と p14, p15 のピンを割り当てていますが、必要に応じてコードを変更してください。

コントローラー側(Webアプリ)

https://github.com/chikoski/wotxwot-control

Firefox ブラウザで動作確認しています(他のブラウザでは動かないかも)。 ゲームパッドの左右のスティックの前後の操作が左右それぞれのモータの前転・後転に対応しています。

動いているところの動画

https://www.facebook.com/video.php?v=456620974491805

ハッカソンでは ARM 賞をいただきました!

(参考) au Firefox OS WoTハッカソン on ホワイトデー

http://au-fx.kddi.com/event/20150314/wot_hackathon0314.html

Committer:
flatbird
Date:
Fri Mar 13 20:27:06 2015 +0900
Revision:
5:ce3f1dd90068
Parent:
1:84af7a219830
Child:
6:79ecd4e53456
updated WebSocketServer.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
flatbird 0:f7596ed7ab5c 1 #include "WebSocketServer.h"
flatbird 5:ce3f1dd90068 2 // #include "sha1.h"
flatbird 0:f7596ed7ab5c 3
flatbird 0:f7596ed7ab5c 4 WebSocketServer::WebSocketServer() {
flatbird 0:f7596ed7ab5c 5 }
flatbird 0:f7596ed7ab5c 6
flatbird 0:f7596ed7ab5c 7 WebSocketServer::~WebSocketServer() {
flatbird 0:f7596ed7ab5c 8 }
flatbird 0:f7596ed7ab5c 9
flatbird 1:84af7a219830 10 bool WebSocketServer::init(int port) {
flatbird 1:84af7a219830 11 mTCPSocketServer.set_blocking(true);
flatbird 1:84af7a219830 12
flatbird 1:84af7a219830 13 int ret = mTCPSocketServer.bind(port);
flatbird 1:84af7a219830 14 if (ret != 0) {
flatbird 1:84af7a219830 15 printf("ERROR: Failed to bind %d\r\n", ret);
flatbird 1:84af7a219830 16 return false;
flatbird 1:84af7a219830 17 }
flatbird 1:84af7a219830 18 ret = mTCPSocketServer.listen();
flatbird 1:84af7a219830 19 if (ret != 0) {
flatbird 1:84af7a219830 20 printf("ERROR: Failed to listen %d\r\n", ret);
flatbird 1:84af7a219830 21 return false;
flatbird 1:84af7a219830 22 }
flatbird 1:84af7a219830 23
flatbird 1:84af7a219830 24 return true;
flatbird 1:84af7a219830 25 }
flatbird 1:84af7a219830 26
flatbird 1:84af7a219830 27 void WebSocketServer::run() {
flatbird 1:84af7a219830 28 TCPSocketConnection connection;
flatbird 1:84af7a219830 29 char buf[1024];
flatbird 1:84af7a219830 30
flatbird 1:84af7a219830 31 while (true) {
flatbird 1:84af7a219830 32 bool isWebSocket = false;
flatbird 1:84af7a219830 33 int ret = mTCPSocketServer.accept(connection);
flatbird 1:84af7a219830 34 if (ret != 0) {
flatbird 1:84af7a219830 35 continue;
flatbird 1:84af7a219830 36 }
flatbird 1:84af7a219830 37 printf("New connection\r\n");
flatbird 5:ce3f1dd90068 38 connection.set_blocking(true);
flatbird 5:ce3f1dd90068 39
flatbird 1:84af7a219830 40 while (connection.is_connected()) {
flatbird 1:84af7a219830 41 ret = connection.receive(buf, sizeof(buf) - 1);
flatbird 5:ce3f1dd90068 42 if (ret == 0) {
flatbird 5:ce3f1dd90068 43 printf("No data to receive\r\n");
flatbird 5:ce3f1dd90068 44 continue;
flatbird 5:ce3f1dd90068 45 }
flatbird 5:ce3f1dd90068 46 if (ret < 0) {
flatbird 5:ce3f1dd90068 47 printf("ERROR: Failed to receive %d\r\n", ret);
flatbird 1:84af7a219830 48 break;
flatbird 1:84af7a219830 49 }
flatbird 1:84af7a219830 50 if (!isWebSocket) {
flatbird 5:ce3f1dd90068 51 if (this->handleHTTP(buf, ret, connection)) {
flatbird 1:84af7a219830 52 isWebSocket = true;
flatbird 1:84af7a219830 53 } else {
flatbird 1:84af7a219830 54 printf("ERROR: Non websocket\r\n");
flatbird 1:84af7a219830 55 break;
flatbird 1:84af7a219830 56 }
flatbird 1:84af7a219830 57 } else {
flatbird 1:84af7a219830 58 if (!this->handleWebSocket(buf, ret)) {
flatbird 1:84af7a219830 59 break;
flatbird 1:84af7a219830 60 }
flatbird 1:84af7a219830 61 }
flatbird 1:84af7a219830 62 }
flatbird 5:ce3f1dd90068 63 printf("closed\r\n");
flatbird 1:84af7a219830 64 connection.close();
flatbird 1:84af7a219830 65 }
flatbird 1:84af7a219830 66 }
flatbird 1:84af7a219830 67
flatbird 1:84af7a219830 68 bool WebSocketServer::setHandler(const char* path, WebSocketHandler* handler) {
flatbird 1:84af7a219830 69 return true;
flatbird 1:84af7a219830 70 }
flatbird 1:84af7a219830 71
flatbird 5:ce3f1dd90068 72 #define UPGRADE_WEBSOCKET "Upgrade: websocket"
flatbird 5:ce3f1dd90068 73 #define SEC_WEBSOCKET_KEY "Sec-WebSocket-Key:"
flatbird 5:ce3f1dd90068 74 #define MAGIC_NUMBER "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
flatbird 5:ce3f1dd90068 75
flatbird 5:ce3f1dd90068 76 bool WebSocketServer::handleHTTP(char* buf, int size, TCPSocketConnection& connection) {
flatbird 1:84af7a219830 77 char* line = &buf[0];
flatbird 5:ce3f1dd90068 78 char key[128];
flatbird 5:ce3f1dd90068 79 bool isUpgradeWebSocket = false;
flatbird 5:ce3f1dd90068 80 bool isSecWebSocketKeyFound = false;
flatbird 1:84af7a219830 81
flatbird 1:84af7a219830 82 for (int i = 0; i < size; i++) {
flatbird 1:84af7a219830 83 if (buf[i] == '\r' && i+1 < size && buf[i+1] == '\n') {
flatbird 1:84af7a219830 84 buf[i] = '\0';
flatbird 1:84af7a219830 85 if (strlen(buf) <= 0) {
flatbird 1:84af7a219830 86 break;
flatbird 1:84af7a219830 87 }
flatbird 1:84af7a219830 88 printf("%s\r\n", line);
flatbird 5:ce3f1dd90068 89 if (line == &buf[0]) {
flatbird 5:ce3f1dd90068 90 char* method = strtok(buf, ' ');
flatbird 5:ce3f1dd90068 91 char* path = strtok(NULL, ' ');
flatbird 5:ce3f1dd90068 92 char* version = strtok(NULL, ' ');
flatbird 5:ce3f1dd90068 93 printf("[%s] [%s] [%s]\r\n", method, path, version);
flatbird 5:ce3f1dd90068 94
flatbird 5:ce3f1dd90068 95 } else if (strncmp(line, UPGRADE_WEBSOCKET, strlen(UPGRADE_WEBSOCKET)) == 0) {
flatbird 5:ce3f1dd90068 96 isUpgradeWebSocket = true;
flatbird 5:ce3f1dd90068 97 printf("%s found\r\n", UPGRADE_WEBSOCKET);
flatbird 5:ce3f1dd90068 98 } else if (strncmp(line, SEC_WEBSOCKET_KEY, strlen(SEC_WEBSOCKET_KEY)) == 0) {
flatbird 5:ce3f1dd90068 99 printf("%s found\r\n", SEC_WEBSOCKET_KEY);
flatbird 5:ce3f1dd90068 100 isSecWebSocketKeyFound = true;
flatbird 5:ce3f1dd90068 101 char* ptr = line + strlen(SEC_WEBSOCKET_KEY);
flatbird 5:ce3f1dd90068 102 while (*ptr == ' ') ++ptr;
flatbird 5:ce3f1dd90068 103 strcpy(ptr, key);
flatbird 5:ce3f1dd90068 104 printf("key=%s\r\n", key);
flatbird 5:ce3f1dd90068 105 }
flatbird 1:84af7a219830 106 i += 2;
flatbird 1:84af7a219830 107 line = &buf[i];
flatbird 1:84af7a219830 108 }
flatbird 1:84af7a219830 109 }
flatbird 1:84af7a219830 110
flatbird 5:ce3f1dd90068 111 if (isUpgradeWebSocket && isSecWebSocketKeyFound) {
flatbird 5:ce3f1dd90068 112 this->sendUpgradeResponse(key, connection);
flatbird 5:ce3f1dd90068 113 return true;
flatbird 5:ce3f1dd90068 114 }
flatbird 5:ce3f1dd90068 115
flatbird 5:ce3f1dd90068 116 return false;
flatbird 5:ce3f1dd90068 117 }
flatbird 5:ce3f1dd90068 118
flatbird 5:ce3f1dd90068 119 bool WebSocketServer::handleWebSocket(char* buf, int size) {
flatbird 5:ce3f1dd90068 120 buf[size] = '\0';
flatbird 5:ce3f1dd90068 121 printf("%s\r\n", buf);
flatbird 1:84af7a219830 122 return true;
flatbird 1:84af7a219830 123 }
flatbird 1:84af7a219830 124
flatbird 5:ce3f1dd90068 125 static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
flatbird 5:ce3f1dd90068 126 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
flatbird 5:ce3f1dd90068 127 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
flatbird 5:ce3f1dd90068 128 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
flatbird 5:ce3f1dd90068 129 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
flatbird 5:ce3f1dd90068 130 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
flatbird 5:ce3f1dd90068 131 'w', 'x', 'y', 'z', '0', '1', '2', '3',
flatbird 5:ce3f1dd90068 132 '4', '5', '6', '7', '8', '9', '+', '/'};
flatbird 5:ce3f1dd90068 133 static int mod_table[] = {0, 2, 1};
flatbird 5:ce3f1dd90068 134
flatbird 5:ce3f1dd90068 135 char *base64_encode(const unsigned char *data,
flatbird 5:ce3f1dd90068 136 size_t input_length,
flatbird 5:ce3f1dd90068 137 // size_t *output_length
flatbird 5:ce3f1dd90068 138 char* encoded_data,
flatbird 5:ce3f1dd90068 139 size_t buffer_length) {
flatbird 5:ce3f1dd90068 140
flatbird 5:ce3f1dd90068 141 // *output_length = 4 * ((input_length + 2) / 3);
flatbird 5:ce3f1dd90068 142 size_t output_length = 4 * ((input_length + 2) / 3);
flatbird 5:ce3f1dd90068 143 if (buffer_length - 1 < output_length) {
flatbird 5:ce3f1dd90068 144 return NULL;
flatbird 5:ce3f1dd90068 145 }
flatbird 5:ce3f1dd90068 146
flatbird 5:ce3f1dd90068 147 // char *encoded_data = malloc(*output_length);
flatbird 5:ce3f1dd90068 148 // if (encoded_data == NULL) return NULL;
flatbird 5:ce3f1dd90068 149
flatbird 5:ce3f1dd90068 150 for (int i = 0, j = 0; i < input_length;) {
flatbird 5:ce3f1dd90068 151
flatbird 5:ce3f1dd90068 152 uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
flatbird 5:ce3f1dd90068 153 uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
flatbird 5:ce3f1dd90068 154 uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
flatbird 5:ce3f1dd90068 155
flatbird 5:ce3f1dd90068 156 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
flatbird 5:ce3f1dd90068 157
flatbird 5:ce3f1dd90068 158 encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 159 encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 160 encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 161 encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 162 }
flatbird 5:ce3f1dd90068 163
flatbird 5:ce3f1dd90068 164 for (int i = 0; i < mod_table[input_length % 3]; i++)
flatbird 5:ce3f1dd90068 165 // encoded_data[*output_length - 1 - i] = '=';
flatbird 5:ce3f1dd90068 166 encoded_data[output_length - 1 - i] = '=';
flatbird 5:ce3f1dd90068 167 encoded_data[output_length] = '\0';
flatbird 5:ce3f1dd90068 168
flatbird 5:ce3f1dd90068 169 return encoded_data;
flatbird 1:84af7a219830 170 }
flatbird 5:ce3f1dd90068 171
flatbird 5:ce3f1dd90068 172 bool WebSocketServer::sendUpgradeResponse(char* key, TCPSocketConnection& connection) {
flatbird 5:ce3f1dd90068 173 char buf[128];
flatbird 5:ce3f1dd90068 174
flatbird 5:ce3f1dd90068 175 if (strlen(key) + sizeof(MAGIC_NUMBER) > sizeof(buf)) {
flatbird 5:ce3f1dd90068 176 return false;
flatbird 5:ce3f1dd90068 177 }
flatbird 5:ce3f1dd90068 178 strcpy(buf, key);
flatbird 5:ce3f1dd90068 179 strcat(buf, MAGIC_NUMBER);
flatbird 5:ce3f1dd90068 180
flatbird 5:ce3f1dd90068 181 unsigned char hash[20];
flatbird 5:ce3f1dd90068 182 char encoded[30];
flatbird 5:ce3f1dd90068 183 sha1((unsigned char*)buf, strlen(buf), hash);
flatbird 5:ce3f1dd90068 184 base64_encode(hash, 20, encoded, sizeof(encoded));
flatbird 5:ce3f1dd90068 185
flatbird 5:ce3f1dd90068 186 char resp[] = "HTTP/1.1 101 Switching Protocols\r\n" \
flatbird 5:ce3f1dd90068 187 "Upgrade: websocket\r\n" \
flatbird 5:ce3f1dd90068 188 "Connection: Upgrade\r\n" \
flatbird 5:ce3f1dd90068 189 "Sec-WebSocket-Accept: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n\r\n";
flatbird 5:ce3f1dd90068 190 char* ptr = strstr(resp, "XXXXX");
flatbird 5:ce3f1dd90068 191 strcpy(ptr, encoded);
flatbird 5:ce3f1dd90068 192 strcpy(ptr+strlen(encoded), "\r\n\r\n");
flatbird 5:ce3f1dd90068 193
flatbird 5:ce3f1dd90068 194 int ret = connection.send_all(resp, strlen(resp));
flatbird 5:ce3f1dd90068 195 if (ret < 0) {
flatbird 5:ce3f1dd90068 196 printf("ERROR: Failed to send response\r\n");
flatbird 5:ce3f1dd90068 197 return false;
flatbird 5:ce3f1dd90068 198 }
flatbird 5:ce3f1dd90068 199
flatbird 5:ce3f1dd90068 200 return true;
flatbird 5:ce3f1dd90068 201 }