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:
Sat Mar 14 00:14:40 2015 +0900
Revision:
7:6cfe0638b957
Parent:
6:79ecd4e53456
Child:
8:6635ca3b5a5c
add websocket data frame handling.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
flatbird 0:f7596ed7ab5c 1 #include "WebSocketServer.h"
flatbird 6:79ecd4e53456 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 7:6cfe0638b957 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 7:6cfe0638b957 33 int ret = mTCPSocketServer.accept(mConnection);
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 7:6cfe0638b957 38 mConnection.set_blocking(true);
flatbird 5:ce3f1dd90068 39
flatbird 7:6cfe0638b957 40 while (mConnection.is_connected()) {
flatbird 7:6cfe0638b957 41 ret = mConnection.receive(buf, sizeof(buf) - 1);
flatbird 5:ce3f1dd90068 42 if (ret == 0) {
flatbird 6:79ecd4e53456 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 7:6cfe0638b957 51 if (this->handleHTTP(buf, ret)) {
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 7:6cfe0638b957 64 mConnection.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 7:6cfe0638b957 69 mHandler = handler; // support only one handler now
flatbird 1:84af7a219830 70 return true;
flatbird 1:84af7a219830 71 }
flatbird 1:84af7a219830 72
flatbird 5:ce3f1dd90068 73 #define UPGRADE_WEBSOCKET "Upgrade: websocket"
flatbird 5:ce3f1dd90068 74 #define SEC_WEBSOCKET_KEY "Sec-WebSocket-Key:"
flatbird 5:ce3f1dd90068 75 #define MAGIC_NUMBER "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
flatbird 5:ce3f1dd90068 76
flatbird 7:6cfe0638b957 77 bool WebSocketServer::handleHTTP(char* buf, int size) {
flatbird 1:84af7a219830 78 char* line = &buf[0];
flatbird 5:ce3f1dd90068 79 char key[128];
flatbird 5:ce3f1dd90068 80 bool isUpgradeWebSocket = false;
flatbird 5:ce3f1dd90068 81 bool isSecWebSocketKeyFound = false;
flatbird 1:84af7a219830 82
flatbird 1:84af7a219830 83 for (int i = 0; i < size; i++) {
flatbird 1:84af7a219830 84 if (buf[i] == '\r' && i+1 < size && buf[i+1] == '\n') {
flatbird 1:84af7a219830 85 buf[i] = '\0';
flatbird 1:84af7a219830 86 if (strlen(buf) <= 0) {
flatbird 1:84af7a219830 87 break;
flatbird 1:84af7a219830 88 }
flatbird 1:84af7a219830 89 printf("%s\r\n", line);
flatbird 5:ce3f1dd90068 90 if (line == &buf[0]) {
flatbird 6:79ecd4e53456 91 char* method = strtok(buf, " ");
flatbird 6:79ecd4e53456 92 char* path = strtok(NULL, " ");
flatbird 6:79ecd4e53456 93 char* version = strtok(NULL, " ");
flatbird 5:ce3f1dd90068 94 printf("[%s] [%s] [%s]\r\n", method, path, version);
flatbird 5:ce3f1dd90068 95
flatbird 5:ce3f1dd90068 96 } else if (strncmp(line, UPGRADE_WEBSOCKET, strlen(UPGRADE_WEBSOCKET)) == 0) {
flatbird 5:ce3f1dd90068 97 isUpgradeWebSocket = true;
flatbird 5:ce3f1dd90068 98 printf("%s found\r\n", UPGRADE_WEBSOCKET);
flatbird 5:ce3f1dd90068 99 } else if (strncmp(line, SEC_WEBSOCKET_KEY, strlen(SEC_WEBSOCKET_KEY)) == 0) {
flatbird 5:ce3f1dd90068 100 printf("%s found\r\n", SEC_WEBSOCKET_KEY);
flatbird 5:ce3f1dd90068 101 isSecWebSocketKeyFound = true;
flatbird 5:ce3f1dd90068 102 char* ptr = line + strlen(SEC_WEBSOCKET_KEY);
flatbird 5:ce3f1dd90068 103 while (*ptr == ' ') ++ptr;
flatbird 6:79ecd4e53456 104 strcpy(key, ptr);
flatbird 5:ce3f1dd90068 105 printf("key=%s\r\n", key);
flatbird 5:ce3f1dd90068 106 }
flatbird 1:84af7a219830 107 i += 2;
flatbird 1:84af7a219830 108 line = &buf[i];
flatbird 1:84af7a219830 109 }
flatbird 1:84af7a219830 110 }
flatbird 1:84af7a219830 111
flatbird 5:ce3f1dd90068 112 if (isUpgradeWebSocket && isSecWebSocketKeyFound) {
flatbird 7:6cfe0638b957 113 this->sendUpgradeResponse(key);
flatbird 5:ce3f1dd90068 114 return true;
flatbird 5:ce3f1dd90068 115 }
flatbird 5:ce3f1dd90068 116
flatbird 5:ce3f1dd90068 117 return false;
flatbird 5:ce3f1dd90068 118 }
flatbird 5:ce3f1dd90068 119
flatbird 7:6cfe0638b957 120 #define OP_CONT 0x0
flatbird 7:6cfe0638b957 121 #define OP_TEXT 0x1
flatbird 7:6cfe0638b957 122 #define OP_BINARY 0x2
flatbird 7:6cfe0638b957 123 #define OP_CLOSE 0x8
flatbird 7:6cfe0638b957 124 #define OP_PING 0x9
flatbird 7:6cfe0638b957 125 #define OP_PONG 0xA
flatbird 7:6cfe0638b957 126
flatbird 5:ce3f1dd90068 127 bool WebSocketServer::handleWebSocket(char* buf, int size) {
flatbird 7:6cfe0638b957 128 uint8_t* ptr = (uint8_t*)buf;
flatbird 7:6cfe0638b957 129
flatbird 7:6cfe0638b957 130 bool fin = (*ptr & 0x80) == 0x80;
flatbird 7:6cfe0638b957 131 uint8_t opcode = *ptr & 0xF;
flatbird 7:6cfe0638b957 132
flatbird 7:6cfe0638b957 133 printf("opcode=%d\r\n", opcode);
flatbird 7:6cfe0638b957 134 if (opcode == OP_PING) {
flatbird 7:6cfe0638b957 135 *ptr = ((*ptr & 0xF0) | OP_PONG);
flatbird 7:6cfe0638b957 136 mConnection.send_all(buf, size);
flatbird 7:6cfe0638b957 137 return true;
flatbird 7:6cfe0638b957 138 }
flatbird 7:6cfe0638b957 139 if (opcode == OP_CLOSE) {
flatbird 7:6cfe0638b957 140 return false;
flatbird 7:6cfe0638b957 141 }
flatbird 7:6cfe0638b957 142 ptr++;
flatbird 7:6cfe0638b957 143
flatbird 7:6cfe0638b957 144 mPrevFin = fin;
flatbird 7:6cfe0638b957 145 if (!fin || !mPrevFin) {
flatbird 7:6cfe0638b957 146 printf("WARN: Data consists of multiple frame not supported\r\n");
flatbird 7:6cfe0638b957 147 return true; // not an error, just discard it
flatbird 7:6cfe0638b957 148 }
flatbird 7:6cfe0638b957 149
flatbird 7:6cfe0638b957 150 bool mask = (*ptr & 0x80) == 1;
flatbird 7:6cfe0638b957 151 uint8_t len = *ptr & 0x7F;
flatbird 7:6cfe0638b957 152 ptr++;
flatbird 7:6cfe0638b957 153
flatbird 7:6cfe0638b957 154 if (len > 125) {
flatbird 7:6cfe0638b957 155 printf("WARN: Extended payload lenght not supported\r\n");
flatbird 7:6cfe0638b957 156 return true; // not an error, just discard it
flatbird 7:6cfe0638b957 157 }
flatbird 7:6cfe0638b957 158
flatbird 7:6cfe0638b957 159 char* data;
flatbird 7:6cfe0638b957 160 if (mask) {
flatbird 7:6cfe0638b957 161 char* maskingKey = (char*)ptr;
flatbird 7:6cfe0638b957 162 data = (char*)(ptr + 4);
flatbird 7:6cfe0638b957 163 for (int i = 0; i < len; i++) {
flatbird 7:6cfe0638b957 164 data[i] = data[i] ^ maskingKey[(i % 4)];
flatbird 7:6cfe0638b957 165 }
flatbird 7:6cfe0638b957 166 } else {
flatbird 7:6cfe0638b957 167 data = (char*)ptr;
flatbird 7:6cfe0638b957 168 }
flatbird 7:6cfe0638b957 169 if (mHandler) {
flatbird 7:6cfe0638b957 170 if (opcode == OP_TEXT) {
flatbird 7:6cfe0638b957 171 data[len] = '\0';
flatbird 7:6cfe0638b957 172 mHandler->onMessage(data);
flatbird 7:6cfe0638b957 173 } else if (opcode == OP_BINARY) {
flatbird 7:6cfe0638b957 174 mHandler->onMessage(data, len);
flatbird 7:6cfe0638b957 175 }
flatbird 7:6cfe0638b957 176 }
flatbird 1:84af7a219830 177 return true;
flatbird 1:84af7a219830 178 }
flatbird 1:84af7a219830 179
flatbird 5:ce3f1dd90068 180 static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
flatbird 5:ce3f1dd90068 181 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
flatbird 5:ce3f1dd90068 182 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
flatbird 5:ce3f1dd90068 183 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
flatbird 5:ce3f1dd90068 184 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
flatbird 5:ce3f1dd90068 185 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
flatbird 5:ce3f1dd90068 186 'w', 'x', 'y', 'z', '0', '1', '2', '3',
flatbird 5:ce3f1dd90068 187 '4', '5', '6', '7', '8', '9', '+', '/'};
flatbird 5:ce3f1dd90068 188 static int mod_table[] = {0, 2, 1};
flatbird 5:ce3f1dd90068 189
flatbird 5:ce3f1dd90068 190 char *base64_encode(const unsigned char *data,
flatbird 5:ce3f1dd90068 191 size_t input_length,
flatbird 5:ce3f1dd90068 192 // size_t *output_length
flatbird 5:ce3f1dd90068 193 char* encoded_data,
flatbird 5:ce3f1dd90068 194 size_t buffer_length) {
flatbird 5:ce3f1dd90068 195
flatbird 5:ce3f1dd90068 196 // *output_length = 4 * ((input_length + 2) / 3);
flatbird 5:ce3f1dd90068 197 size_t output_length = 4 * ((input_length + 2) / 3);
flatbird 5:ce3f1dd90068 198 if (buffer_length - 1 < output_length) {
flatbird 5:ce3f1dd90068 199 return NULL;
flatbird 5:ce3f1dd90068 200 }
flatbird 5:ce3f1dd90068 201
flatbird 5:ce3f1dd90068 202 // char *encoded_data = malloc(*output_length);
flatbird 5:ce3f1dd90068 203 // if (encoded_data == NULL) return NULL;
flatbird 5:ce3f1dd90068 204
flatbird 5:ce3f1dd90068 205 for (int i = 0, j = 0; i < input_length;) {
flatbird 5:ce3f1dd90068 206
flatbird 5:ce3f1dd90068 207 uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
flatbird 5:ce3f1dd90068 208 uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
flatbird 5:ce3f1dd90068 209 uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
flatbird 5:ce3f1dd90068 210
flatbird 5:ce3f1dd90068 211 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
flatbird 5:ce3f1dd90068 212
flatbird 5:ce3f1dd90068 213 encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 214 encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 215 encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 216 encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
flatbird 5:ce3f1dd90068 217 }
flatbird 5:ce3f1dd90068 218
flatbird 5:ce3f1dd90068 219 for (int i = 0; i < mod_table[input_length % 3]; i++)
flatbird 5:ce3f1dd90068 220 // encoded_data[*output_length - 1 - i] = '=';
flatbird 5:ce3f1dd90068 221 encoded_data[output_length - 1 - i] = '=';
flatbird 5:ce3f1dd90068 222 encoded_data[output_length] = '\0';
flatbird 5:ce3f1dd90068 223
flatbird 5:ce3f1dd90068 224 return encoded_data;
flatbird 1:84af7a219830 225 }
flatbird 5:ce3f1dd90068 226
flatbird 7:6cfe0638b957 227 bool WebSocketServer::sendUpgradeResponse(char* key) {
flatbird 5:ce3f1dd90068 228 char buf[128];
flatbird 5:ce3f1dd90068 229
flatbird 5:ce3f1dd90068 230 if (strlen(key) + sizeof(MAGIC_NUMBER) > sizeof(buf)) {
flatbird 5:ce3f1dd90068 231 return false;
flatbird 5:ce3f1dd90068 232 }
flatbird 5:ce3f1dd90068 233 strcpy(buf, key);
flatbird 5:ce3f1dd90068 234 strcat(buf, MAGIC_NUMBER);
flatbird 5:ce3f1dd90068 235
flatbird 5:ce3f1dd90068 236 unsigned char hash[20];
flatbird 5:ce3f1dd90068 237 char encoded[30];
flatbird 6:79ecd4e53456 238 sha1(buf, strlen(buf), (char*)hash);
flatbird 5:ce3f1dd90068 239 base64_encode(hash, 20, encoded, sizeof(encoded));
flatbird 5:ce3f1dd90068 240
flatbird 5:ce3f1dd90068 241 char resp[] = "HTTP/1.1 101 Switching Protocols\r\n" \
flatbird 5:ce3f1dd90068 242 "Upgrade: websocket\r\n" \
flatbird 5:ce3f1dd90068 243 "Connection: Upgrade\r\n" \
flatbird 5:ce3f1dd90068 244 "Sec-WebSocket-Accept: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n\r\n";
flatbird 5:ce3f1dd90068 245 char* ptr = strstr(resp, "XXXXX");
flatbird 5:ce3f1dd90068 246 strcpy(ptr, encoded);
flatbird 5:ce3f1dd90068 247 strcpy(ptr+strlen(encoded), "\r\n\r\n");
flatbird 5:ce3f1dd90068 248
flatbird 7:6cfe0638b957 249 int ret = mConnection.send_all(resp, strlen(resp));
flatbird 5:ce3f1dd90068 250 if (ret < 0) {
flatbird 5:ce3f1dd90068 251 printf("ERROR: Failed to send response\r\n");
flatbird 5:ce3f1dd90068 252 return false;
flatbird 5:ce3f1dd90068 253 }
flatbird 5:ce3f1dd90068 254
flatbird 5:ce3f1dd90068 255 return true;
flatbird 5:ce3f1dd90068 256 }