NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Committer:
donatien
Date:
Thu Aug 05 15:01:33 2010 +0000
Revision:
11:da4498f591ee
Parent:
4:fd826cad83c0

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:632c9925f013 1
donatien 0:632c9925f013 2 /*
donatien 0:632c9925f013 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
donatien 0:632c9925f013 4
donatien 0:632c9925f013 5 Permission is hereby granted, free of charge, to any person obtaining a copy
donatien 0:632c9925f013 6 of this software and associated documentation files (the "Software"), to deal
donatien 0:632c9925f013 7 in the Software without restriction, including without limitation the rights
donatien 0:632c9925f013 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
donatien 0:632c9925f013 9 copies of the Software, and to permit persons to whom the Software is
donatien 0:632c9925f013 10 furnished to do so, subject to the following conditions:
donatien 0:632c9925f013 11
donatien 0:632c9925f013 12 The above copyright notice and this permission notice shall be included in
donatien 0:632c9925f013 13 all copies or substantial portions of the Software.
donatien 0:632c9925f013 14
donatien 0:632c9925f013 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
donatien 0:632c9925f013 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
donatien 0:632c9925f013 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
donatien 0:632c9925f013 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
donatien 0:632c9925f013 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 0:632c9925f013 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
donatien 0:632c9925f013 21 THE SOFTWARE.
donatien 0:632c9925f013 22 */
donatien 0:632c9925f013 23
donatien 0:632c9925f013 24 #ifndef ATIF_H
donatien 0:632c9925f013 25 #define ATIF_H
donatien 0:632c9925f013 26
donatien 0:632c9925f013 27 #include "netCfg.h"
donatien 0:632c9925f013 28
donatien 0:632c9925f013 29 #include "mbed.h"
donatien 0:632c9925f013 30 #include "drv/serial/buf/SerialBuf.h"
donatien 0:632c9925f013 31 #include <list>
donatien 0:632c9925f013 32 using std::list;
donatien 0:632c9925f013 33
donatien 0:632c9925f013 34 //class Serial; //Pb w forward decl
donatien 0:632c9925f013 35
donatien 0:632c9925f013 36 enum ATErr
donatien 0:632c9925f013 37 {
donatien 0:632c9925f013 38 __AT_MIN = -0xFFFF,
donatien 0:632c9925f013 39 AT_CLOSED,
donatien 0:632c9925f013 40 AT_NOANSWER,
donatien 0:632c9925f013 41 AT_ERROR,
donatien 0:632c9925f013 42 AT_TIMEOUT,
donatien 0:632c9925f013 43 AT_BUSY,
donatien 0:632c9925f013 44 AT_PARSE,
donatien 0:632c9925f013 45 AT_INCOMPLETE,
donatien 0:632c9925f013 46 AT_OK = 0
donatien 0:632c9925f013 47 };
donatien 0:632c9925f013 48
donatien 0:632c9925f013 49 class ATIf : public SerialBuf
donatien 0:632c9925f013 50 {
donatien 0:632c9925f013 51 public:
donatien 0:632c9925f013 52
donatien 0:632c9925f013 53 ATIf();
donatien 0:632c9925f013 54 virtual ~ATIf();
donatien 4:fd826cad83c0 55
donatien 4:fd826cad83c0 56 #if 0
donatien 0:632c9925f013 57 template<class T>
donatien 0:632c9925f013 58 //void attachSignal( const char* sigName, T* pItem, bool (T::*pMethod)(ATIf*, bool, bool*) ); //Attach Signal ("Unsollicited response code" in Telit_AT_Reference_Guide.pdf) to an handler fn
donatien 0:632c9925f013 59 //Linker bug : Must be defined here :(
donatien 0:632c9925f013 60 void attachSignal( const char* sigName, T* pItem, bool (T::*pMethod)(ATIf*, bool, bool*) ) //Attach Signal ("Unsollicited response code" in Telit_AT_Reference_Guide.pdf) to an handler fn
donatien 0:632c9925f013 61 {
donatien 0:632c9925f013 62 ATSigHandler sig(sigName, (ATSigHandler::CDummy*)pItem, (bool (ATSigHandler::CDummy::*)(ATIf*, bool, bool*))pMethod);
donatien 0:632c9925f013 63 m_signals.push_back(sig);
donatien 0:632c9925f013 64 }
donatien 0:632c9925f013 65 void detachSignal( const char* sigName );
donatien 4:fd826cad83c0 66 #endif
donatien 0:632c9925f013 67
donatien 0:632c9925f013 68 ATErr open(Serial* pSerial); //Deactivate echo, etc
donatien 0:632c9925f013 69 #if NET_USB_SERIAL
donatien 0:632c9925f013 70 ATErr open(UsbSerial* pUsbSerial); //Deactivate echo, etc
donatien 0:632c9925f013 71 #endif
donatien 0:632c9925f013 72 ATErr close(); //Release port
donatien 0:632c9925f013 73
donatien 0:632c9925f013 74 int printf(const char* format, ... );
donatien 0:632c9925f013 75 int scanf(const char* format, ... );
donatien 0:632c9925f013 76 void setTimeout(int timeout); //used by scanf
donatien 0:632c9925f013 77 void setLineMode(bool lineMode); //Switch btw line & raw fns
donatien 4:fd826cad83c0 78 //void setSignals(bool signalsEnable);
donatien 0:632c9925f013 79 ATErr flushBuffer(); //Discard input buffer
donatien 0:632c9925f013 80 ATErr flushLine(int timeout); //Discard input buffer until CRLF is encountered
donatien 0:632c9925f013 81
donatien 0:632c9925f013 82 protected:
donatien 4:fd826cad83c0 83 //virtual bool onRead(); //Inherited from SerialBuf, return true if data is incomplete
donatien 0:632c9925f013 84 ATErr rawOpen(Serial* pSerial, int baudrate); //Simple open function for similar non-conforming protocols
donatien 0:632c9925f013 85
donatien 0:632c9925f013 86 public:
donatien 0:632c9925f013 87 /* ATErr command(const char* cmd, char* result, int resultLen, int timeout); */ //Kinda useless
donatien 0:632c9925f013 88 ATErr write(const char* cmd, bool lineMode = false);
donatien 0:632c9925f013 89 ATErr read(char* result, int resultMaxLen, int timeout, bool lineMode = false, int resultMinLen = 0);
donatien 0:632c9925f013 90 bool isOpen();
donatien 0:632c9925f013 91 ATErr checkOK(); //Helper fn to quickly check that OK has been returned
donatien 0:632c9925f013 92
donatien 0:632c9925f013 93 private:
donatien 0:632c9925f013 94 int readLine(char* line, int maxLen, int timeout); //Read a single line from serial port
donatien 0:632c9925f013 95 int writeLine(const char* line); //Write a single line to serial port
donatien 0:632c9925f013 96
donatien 0:632c9925f013 97 int readRaw(char* str, int maxLen, int timeout = 0, int minLen = 0); //Read from serial port in buf
donatien 0:632c9925f013 98 int writeRaw(const char* str); //Write directly to serial port
donatien 0:632c9925f013 99
donatien 0:632c9925f013 100 volatile int m_readTimeout;
donatien 0:632c9925f013 101 volatile bool m_lineMode;
donatien 4:fd826cad83c0 102 //bool m_signalsEnable;
donatien 0:632c9925f013 103 bool m_isOpen;
donatien 0:632c9925f013 104
donatien 0:632c9925f013 105 char* m_tmpBuf;
donatien 4:fd826cad83c0 106
donatien 4:fd826cad83c0 107 #if 0
donatien 0:632c9925f013 108 class ATSigHandler
donatien 0:632c9925f013 109 {
donatien 0:632c9925f013 110 class CDummy;
donatien 0:632c9925f013 111 public:
donatien 0:632c9925f013 112 ATSigHandler(const char* name, CDummy* cbObj, bool (CDummy::*cbMeth)(ATIf* pIf, bool beg, bool* pMoreData)) : m_cbObj(cbObj), m_cbMeth(cbMeth), m_name(name)
donatien 0:632c9925f013 113 {}
donatien 0:632c9925f013 114 protected:
donatien 0:632c9925f013 115 CDummy* m_cbObj;
donatien 0:632c9925f013 116 bool (CDummy::*m_cbMeth)(ATIf* pIf, bool beg, bool* pMoreData); //*pMoreData set to true if needs to read next line, beg = true if beginning of new code
donatien 0:632c9925f013 117 const char* m_name;
donatien 0:632c9925f013 118
donatien 0:632c9925f013 119 friend class ATIf;
donatien 0:632c9925f013 120 };
donatien 0:632c9925f013 121
donatien 0:632c9925f013 122 volatile ATSigHandler* m_pCurrentSignal; //Signal that asked more data
donatien 0:632c9925f013 123
donatien 0:632c9925f013 124 bool handleSignal(); //Returns true if signal has been handled
donatien 0:632c9925f013 125 list<ATSigHandler> m_signals;
donatien 4:fd826cad83c0 126 #endif
donatien 0:632c9925f013 127
donatien 0:632c9925f013 128 };
donatien 0:632c9925f013 129
donatien 0:632c9925f013 130 #endif