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 11:06:11 2015 +0000
Revision:
9:774f408b9740
Parent:
8:6635ca3b5a5c
Child:
10:578778037efb
fixed

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