Jake B. / Mbed 2 deprecated MakerBotServer

Dependencies:   IAP NTPClient RTC mbed-rtos mbed Socket lwip-sys lwip BurstSPI

Fork of LPC1768_Mini-DK by Frank Vannieuwkerke

Committer:
jakeb
Date:
Fri Aug 23 21:45:08 2013 +0000
Revision:
15:688b3e3958fd
Initial commit of software v0.2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakeb 15:688b3e3958fd 1 // Copyright (c) 2013, jake (at) allaboutjake (dot) com
jakeb 15:688b3e3958fd 2 // All rights reserved.
jakeb 15:688b3e3958fd 3 //
jakeb 15:688b3e3958fd 4 // Redistribution and use in source and binary forms, with or without
jakeb 15:688b3e3958fd 5 // modification, are permitted provided that the following conditions are met:
jakeb 15:688b3e3958fd 6 // * Redistributions of source code must retain the above copyright
jakeb 15:688b3e3958fd 7 // notice, this list of conditions and the following disclaimer.
jakeb 15:688b3e3958fd 8 // * Redistributions in binary form must reproduce the above copyright
jakeb 15:688b3e3958fd 9 // notice, this list of conditions and the following disclaimer in the
jakeb 15:688b3e3958fd 10 // documentation and/or other materials provided with the distribution.
jakeb 15:688b3e3958fd 11 // * The name of the author and/or copyright holder nor the
jakeb 15:688b3e3958fd 12 // names of its contributors may be used to endorse or promote products
jakeb 15:688b3e3958fd 13 // derived from this software without specific prior written permission.
jakeb 15:688b3e3958fd 14 //
jakeb 15:688b3e3958fd 15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
jakeb 15:688b3e3958fd 16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
jakeb 15:688b3e3958fd 17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
jakeb 15:688b3e3958fd 18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, AUTHOR, OR ANY CONTRIBUTORS
jakeb 15:688b3e3958fd 19 // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
jakeb 15:688b3e3958fd 20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
jakeb 15:688b3e3958fd 21 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
jakeb 15:688b3e3958fd 22 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
jakeb 15:688b3e3958fd 23 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
jakeb 15:688b3e3958fd 24 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jakeb 15:688b3e3958fd 25
jakeb 15:688b3e3958fd 26 // DESCRIPTION OF FILE:
jakeb 15:688b3e3958fd 27 //
jakeb 15:688b3e3958fd 28 // This file contains a very simple readline function used by the http and telnet
jakeb 15:688b3e3958fd 29 // server threads.
jakeb 15:688b3e3958fd 30 //
jakeb 15:688b3e3958fd 31
jakeb 15:688b3e3958fd 32 #include "SimpleSocket.h"
jakeb 15:688b3e3958fd 33 #include "mbed.h"
jakeb 15:688b3e3958fd 34 #include "rtos.h"
jakeb 15:688b3e3958fd 35
jakeb 15:688b3e3958fd 36 //Simple readline routine for use in the command prompt.
jakeb 15:688b3e3958fd 37 int readline(ClientSocket* socket, char* buffer, int max_length) {
jakeb 15:688b3e3958fd 38 int curPos = 0;
jakeb 15:688b3e3958fd 39
jakeb 15:688b3e3958fd 40 while (1) {
jakeb 15:688b3e3958fd 41 int c;
jakeb 15:688b3e3958fd 42
jakeb 15:688b3e3958fd 43 //Wait for a character
jakeb 15:688b3e3958fd 44 while ((c=socket->read()) < 0) {
jakeb 15:688b3e3958fd 45 if (!socket->connected()) return -1;
jakeb 15:688b3e3958fd 46 Thread::yield();
jakeb 15:688b3e3958fd 47 }
jakeb 15:688b3e3958fd 48
jakeb 15:688b3e3958fd 49 switch (c) {
jakeb 15:688b3e3958fd 50 case '\r':
jakeb 15:688b3e3958fd 51 continue;
jakeb 15:688b3e3958fd 52
jakeb 15:688b3e3958fd 53 case '\b':
jakeb 15:688b3e3958fd 54 if (curPos > 0) curPos--;
jakeb 15:688b3e3958fd 55 break;
jakeb 15:688b3e3958fd 56
jakeb 15:688b3e3958fd 57 default:
jakeb 15:688b3e3958fd 58 buffer[curPos]= (char)(c & 0xFF);
jakeb 15:688b3e3958fd 59 curPos++;
jakeb 15:688b3e3958fd 60 if (curPos < max_length)
jakeb 15:688b3e3958fd 61 break;
jakeb 15:688b3e3958fd 62
jakeb 15:688b3e3958fd 63 //FALLTHROUGH
jakeb 15:688b3e3958fd 64 case '\n':
jakeb 15:688b3e3958fd 65 buffer[curPos]=0x00; //string terminator
jakeb 15:688b3e3958fd 66 return curPos;
jakeb 15:688b3e3958fd 67 }
jakeb 15:688b3e3958fd 68 }
jakeb 15:688b3e3958fd 69 }
jakeb 15:688b3e3958fd 70