posilani dat

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

Committer:
PavelKumpan
Date:
Tue May 23 18:42:14 2017 +0000
Revision:
26:5674b8978551
Parent:
7:805c16a071d0
Recreated communication protocol.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jkaderka 7:805c16a071d0 1 /* Copyright (C) 2012 mbed.org, MIT License
jkaderka 7:805c16a071d0 2 *
jkaderka 7:805c16a071d0 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jkaderka 7:805c16a071d0 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jkaderka 7:805c16a071d0 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jkaderka 7:805c16a071d0 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jkaderka 7:805c16a071d0 7 * furnished to do so, subject to the following conditions:
jkaderka 7:805c16a071d0 8 *
jkaderka 7:805c16a071d0 9 * The above copyright notice and this permission notice shall be included in all copies or
jkaderka 7:805c16a071d0 10 * substantial portions of the Software.
jkaderka 7:805c16a071d0 11 *
jkaderka 7:805c16a071d0 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jkaderka 7:805c16a071d0 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jkaderka 7:805c16a071d0 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jkaderka 7:805c16a071d0 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jkaderka 7:805c16a071d0 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jkaderka 7:805c16a071d0 17 */
jkaderka 7:805c16a071d0 18
jkaderka 7:805c16a071d0 19 #include "TCPSocketServer.h"
jkaderka 7:805c16a071d0 20 #include <string>
jkaderka 7:805c16a071d0 21
jkaderka 7:805c16a071d0 22 TCPSocketServer::TCPSocketServer() {}
jkaderka 7:805c16a071d0 23
jkaderka 7:805c16a071d0 24 // Server initialization
jkaderka 7:805c16a071d0 25 int TCPSocketServer::bind(int port) {
jkaderka 7:805c16a071d0 26 char cmd[20];
jkaderka 7:805c16a071d0 27
jkaderka 7:805c16a071d0 28 // set TCP protocol
jkaderka 7:805c16a071d0 29 wifi->setProtocol(TCP);
jkaderka 7:805c16a071d0 30
jkaderka 7:805c16a071d0 31 // set local port
jkaderka 7:805c16a071d0 32 sprintf(cmd, "set i l %d\r", port);
jkaderka 7:805c16a071d0 33 if (!wifi->sendCommand(cmd, "AOK"))
jkaderka 7:805c16a071d0 34 return -1;
jkaderka 7:805c16a071d0 35
jkaderka 7:805c16a071d0 36 // save
jkaderka 7:805c16a071d0 37 if (!wifi->sendCommand("save\r", "Stor"))
jkaderka 7:805c16a071d0 38 return -1;
jkaderka 7:805c16a071d0 39
jkaderka 7:805c16a071d0 40 // reboot
jkaderka 7:805c16a071d0 41 wifi->reboot();
jkaderka 7:805c16a071d0 42
jkaderka 7:805c16a071d0 43 // connect the network
jkaderka 7:805c16a071d0 44 if (wifi->isDHCP()) {
jkaderka 7:805c16a071d0 45 if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000))
jkaderka 7:805c16a071d0 46 return -1;
jkaderka 7:805c16a071d0 47 } else {
jkaderka 7:805c16a071d0 48 if (!wifi->sendCommand("join\r", "Associated", NULL, 10000))
jkaderka 7:805c16a071d0 49 return -1;
jkaderka 7:805c16a071d0 50 }
jkaderka 7:805c16a071d0 51
jkaderka 7:805c16a071d0 52 // exit
jkaderka 7:805c16a071d0 53 wifi->exit();
jkaderka 7:805c16a071d0 54
jkaderka 7:805c16a071d0 55 wait(0.2);
jkaderka 7:805c16a071d0 56 wifi->flush();
jkaderka 7:805c16a071d0 57 return 0;
jkaderka 7:805c16a071d0 58 }
jkaderka 7:805c16a071d0 59
jkaderka 7:805c16a071d0 60 int TCPSocketServer::listen(int backlog) {
jkaderka 7:805c16a071d0 61 if (backlog != 1)
jkaderka 7:805c16a071d0 62 return -1;
jkaderka 7:805c16a071d0 63 return 0;
jkaderka 7:805c16a071d0 64 }
jkaderka 7:805c16a071d0 65
jkaderka 7:805c16a071d0 66
jkaderka 7:805c16a071d0 67 int TCPSocketServer::accept(TCPSocketConnection& connection) {
jkaderka 7:805c16a071d0 68 int nb_available = 0, pos = 0;
jkaderka 7:805c16a071d0 69 char c;
jkaderka 7:805c16a071d0 70 string str;
jkaderka 7:805c16a071d0 71 bool o_find = false;
jkaderka 7:805c16a071d0 72 while (1) {
jkaderka 7:805c16a071d0 73 while(!wifi->readable());
jkaderka 7:805c16a071d0 74 nb_available = wifi->readable();
jkaderka 7:805c16a071d0 75 for (int i = 0; i < nb_available; i++) {
jkaderka 7:805c16a071d0 76 c = wifi->getc();
jkaderka 7:805c16a071d0 77 if (c == '*') {
jkaderka 7:805c16a071d0 78 o_find = true;
jkaderka 7:805c16a071d0 79 }
jkaderka 7:805c16a071d0 80 if (o_find && c != '\r' && c != '\n') {
jkaderka 7:805c16a071d0 81 str += c;
jkaderka 7:805c16a071d0 82 pos = str.find("*OPEN*");
jkaderka 7:805c16a071d0 83 if (pos != string::npos) {
jkaderka 7:805c16a071d0 84 wifi->flush();
jkaderka 7:805c16a071d0 85 return 0;
jkaderka 7:805c16a071d0 86 }
jkaderka 7:805c16a071d0 87 }
jkaderka 7:805c16a071d0 88 }
jkaderka 7:805c16a071d0 89 }
jkaderka 7:805c16a071d0 90 }