![](/media/cache/profiles/DG_Mugshot.png.50x50_q85.jpg)
RFID tracking with mbed & RS-EDP reference design
Dependencies: RWDModule mbed SDCard
log/logger.cpp@0:fd63457452f4, 2010-07-28 (annotated)
- Committer:
- donatien
- Date:
- Wed Jul 28 11:02:36 2010 +0000
- Revision:
- 0:fd63457452f4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:fd63457452f4 | 1 | /* |
donatien | 0:fd63457452f4 | 2 | Copyright (c) 2010 ARM Limited |
donatien | 0:fd63457452f4 | 3 | |
donatien | 0:fd63457452f4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 0:fd63457452f4 | 5 | of this software and associated documentation files (the "Software"), to deal |
donatien | 0:fd63457452f4 | 6 | in the Software without restriction, including without limitation the rights |
donatien | 0:fd63457452f4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 0:fd63457452f4 | 8 | copies of the Software, and to permit persons to whom the Software is |
donatien | 0:fd63457452f4 | 9 | furnished to do so, subject to the following conditions: |
donatien | 0:fd63457452f4 | 10 | |
donatien | 0:fd63457452f4 | 11 | The above copyright notice and this permission notice shall be included in |
donatien | 0:fd63457452f4 | 12 | all copies or substantial portions of the Software. |
donatien | 0:fd63457452f4 | 13 | |
donatien | 0:fd63457452f4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:fd63457452f4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:fd63457452f4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:fd63457452f4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:fd63457452f4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:fd63457452f4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 0:fd63457452f4 | 20 | THE SOFTWARE. |
donatien | 0:fd63457452f4 | 21 | */ |
donatien | 0:fd63457452f4 | 22 | |
donatien | 0:fd63457452f4 | 23 | #include "mbed.h" |
donatien | 0:fd63457452f4 | 24 | #include "logger.h" |
donatien | 0:fd63457452f4 | 25 | |
donatien | 0:fd63457452f4 | 26 | #include "MySQLClient.h" |
donatien | 0:fd63457452f4 | 27 | |
donatien | 0:fd63457452f4 | 28 | #define MAX_ELEMENTS_IN_QUEUE 50 |
donatien | 0:fd63457452f4 | 29 | |
donatien | 0:fd63457452f4 | 30 | |
donatien | 0:fd63457452f4 | 31 | Logger::Logger(const string& table) : m_lSQLReq(MAX_ELEMENTS_IN_QUEUE), m_fd(NULL), m_sqlClient(), m_sqlLastResult(MYSQL_PROCESSING), |
donatien | 0:fd63457452f4 | 32 | m_sqlConnecting(false), m_sqlConnected(false), m_sqlConnectionError(false), m_sqlCommand(false) |
donatien | 0:fd63457452f4 | 33 | { |
donatien | 0:fd63457452f4 | 34 | m_table = table; |
donatien | 0:fd63457452f4 | 35 | } |
donatien | 0:fd63457452f4 | 36 | |
donatien | 0:fd63457452f4 | 37 | Logger::~Logger() |
donatien | 0:fd63457452f4 | 38 | { |
donatien | 0:fd63457452f4 | 39 | |
donatien | 0:fd63457452f4 | 40 | } |
donatien | 0:fd63457452f4 | 41 | |
donatien | 0:fd63457452f4 | 42 | //Open file for logging |
donatien | 0:fd63457452f4 | 43 | int Logger::fileOpen(const char* filename) |
donatien | 0:fd63457452f4 | 44 | { |
donatien | 0:fd63457452f4 | 45 | if(m_fd) |
donatien | 0:fd63457452f4 | 46 | return -1; //File already opened |
donatien | 0:fd63457452f4 | 47 | m_fd = fopen(filename, "a"); |
donatien | 0:fd63457452f4 | 48 | if(!m_fd) //Try again in "w" mode (creates the file if not existent) |
donatien | 0:fd63457452f4 | 49 | m_fd = fopen(filename, "w"); |
donatien | 0:fd63457452f4 | 50 | if(!m_fd) |
donatien | 0:fd63457452f4 | 51 | return -1; //Could not open file |
donatien | 0:fd63457452f4 | 52 | return 0; |
donatien | 0:fd63457452f4 | 53 | } |
donatien | 0:fd63457452f4 | 54 | |
donatien | 0:fd63457452f4 | 55 | //Close log file |
donatien | 0:fd63457452f4 | 56 | int Logger::fileClose() |
donatien | 0:fd63457452f4 | 57 | { |
donatien | 0:fd63457452f4 | 58 | if(!m_fd) |
donatien | 0:fd63457452f4 | 59 | return -1; //Nothing to close |
donatien | 0:fd63457452f4 | 60 | fclose(m_fd); |
donatien | 0:fd63457452f4 | 61 | m_fd = NULL; |
donatien | 0:fd63457452f4 | 62 | return 0; |
donatien | 0:fd63457452f4 | 63 | } |
donatien | 0:fd63457452f4 | 64 | |
donatien | 0:fd63457452f4 | 65 | //Try to connect to server |
donatien | 0:fd63457452f4 | 66 | int Logger::sqlOpen(Host& host, const string& user, const string& password, const string& db) |
donatien | 0:fd63457452f4 | 67 | { |
donatien | 0:fd63457452f4 | 68 | m_sqlClient.open(host, user, password, db, this, &Logger::onMySQLResult); //Non blocking |
donatien | 0:fd63457452f4 | 69 | //The onMySQLResult() method will be called on result of this command |
donatien | 0:fd63457452f4 | 70 | m_sqlConnecting = true; |
donatien | 0:fd63457452f4 | 71 | m_sqlConnectionError = false; |
donatien | 0:fd63457452f4 | 72 | return 0; |
donatien | 0:fd63457452f4 | 73 | } |
donatien | 0:fd63457452f4 | 74 | |
donatien | 0:fd63457452f4 | 75 | //Close server connection |
donatien | 0:fd63457452f4 | 76 | int Logger::sqlClose() |
donatien | 0:fd63457452f4 | 77 | { |
donatien | 0:fd63457452f4 | 78 | m_sqlClient.exit(); |
donatien | 0:fd63457452f4 | 79 | m_sqlConnected = false; |
donatien | 0:fd63457452f4 | 80 | return 0; |
donatien | 0:fd63457452f4 | 81 | } |
donatien | 0:fd63457452f4 | 82 | |
donatien | 0:fd63457452f4 | 83 | //Log some data |
donatien | 0:fd63457452f4 | 84 | int Logger::log(LogInfo* pInfo) //Returns 0 on success, -1 on failure |
donatien | 0:fd63457452f4 | 85 | { |
donatien | 0:fd63457452f4 | 86 | |
donatien | 0:fd63457452f4 | 87 | if(m_fd) //If file is open, append record to file in CSV format |
donatien | 0:fd63457452f4 | 88 | { |
donatien | 0:fd63457452f4 | 89 | fputs(pInfo->toCSV().c_str(), m_fd); |
donatien | 0:fd63457452f4 | 90 | fputs("\r\n", m_fd); //Append new line |
donatien | 0:fd63457452f4 | 91 | } |
donatien | 0:fd63457452f4 | 92 | if(m_lSQLReq.size()<MAX_ELEMENTS_IN_QUEUE) //Check if there is (arbitrary) space in queue |
donatien | 0:fd63457452f4 | 93 | { |
donatien | 0:fd63457452f4 | 94 | m_lSQLReq.push(pInfo->toSQL(m_table)); //Put record in queue for next network push |
donatien | 0:fd63457452f4 | 95 | return 0; //Request queued |
donatien | 0:fd63457452f4 | 96 | } |
donatien | 0:fd63457452f4 | 97 | else |
donatien | 0:fd63457452f4 | 98 | { |
donatien | 0:fd63457452f4 | 99 | return -1; //Error |
donatien | 0:fd63457452f4 | 100 | } |
donatien | 0:fd63457452f4 | 101 | } |
donatien | 0:fd63457452f4 | 102 | |
donatien | 0:fd63457452f4 | 103 | void Logger::service() |
donatien | 0:fd63457452f4 | 104 | { |
donatien | 0:fd63457452f4 | 105 | Net::poll(); //Poll Networking stack |
donatien | 0:fd63457452f4 | 106 | if(m_sqlConnected) //Are we connected to the server? |
donatien | 0:fd63457452f4 | 107 | { |
donatien | 0:fd63457452f4 | 108 | //Can serve SQL requests |
donatien | 0:fd63457452f4 | 109 | if(m_sqlCommand && m_sqlLastResult!=MYSQL_PROCESSING) //Previous command completed? |
donatien | 0:fd63457452f4 | 110 | { |
donatien | 0:fd63457452f4 | 111 | if(m_sqlLastResult==MYSQL_OK) //Has previous command completed with success? |
donatien | 0:fd63457452f4 | 112 | { |
donatien | 0:fd63457452f4 | 113 | if(!m_lSQLReq.empty()) |
donatien | 0:fd63457452f4 | 114 | { |
donatien | 0:fd63457452f4 | 115 | m_lSQLReq.pop(); //Ok, we can dequeue previous element |
donatien | 0:fd63457452f4 | 116 | } |
donatien | 0:fd63457452f4 | 117 | } |
donatien | 0:fd63457452f4 | 118 | m_sqlCommand = false; //No command being executed |
donatien | 0:fd63457452f4 | 119 | } |
donatien | 0:fd63457452f4 | 120 | if(!m_sqlCommand) //Ready for a command? |
donatien | 0:fd63457452f4 | 121 | { |
donatien | 0:fd63457452f4 | 122 | if(!m_lSQLReq.empty()) //Anything in the queue? |
donatien | 0:fd63457452f4 | 123 | { |
donatien | 0:fd63457452f4 | 124 | //Execute the next SQL request |
donatien | 0:fd63457452f4 | 125 | m_sqlLastResult = m_sqlClient.sql(m_lSQLReq.front()); //MYSQL_PROCESSING |
donatien | 0:fd63457452f4 | 126 | m_sqlCommand = true; |
donatien | 0:fd63457452f4 | 127 | } |
donatien | 0:fd63457452f4 | 128 | } |
donatien | 0:fd63457452f4 | 129 | } |
donatien | 0:fd63457452f4 | 130 | } |
donatien | 0:fd63457452f4 | 131 | |
donatien | 0:fd63457452f4 | 132 | bool Logger::isEmpty() |
donatien | 0:fd63457452f4 | 133 | { |
donatien | 0:fd63457452f4 | 134 | return m_lSQLReq.empty(); |
donatien | 0:fd63457452f4 | 135 | } |
donatien | 0:fd63457452f4 | 136 | |
donatien | 0:fd63457452f4 | 137 | bool Logger::isConnecting() |
donatien | 0:fd63457452f4 | 138 | { |
donatien | 0:fd63457452f4 | 139 | return m_sqlConnecting; |
donatien | 0:fd63457452f4 | 140 | } |
donatien | 0:fd63457452f4 | 141 | |
donatien | 0:fd63457452f4 | 142 | bool Logger::isConnected() |
donatien | 0:fd63457452f4 | 143 | { |
donatien | 0:fd63457452f4 | 144 | return m_sqlConnected; |
donatien | 0:fd63457452f4 | 145 | } |
donatien | 0:fd63457452f4 | 146 | |
donatien | 0:fd63457452f4 | 147 | bool Logger::connectionError() |
donatien | 0:fd63457452f4 | 148 | { |
donatien | 0:fd63457452f4 | 149 | return m_sqlConnectionError; |
donatien | 0:fd63457452f4 | 150 | } |
donatien | 0:fd63457452f4 | 151 | |
donatien | 0:fd63457452f4 | 152 | MySQLResult Logger::getLastResult() //Returns last command's result |
donatien | 0:fd63457452f4 | 153 | { |
donatien | 0:fd63457452f4 | 154 | return m_sqlLastResult; |
donatien | 0:fd63457452f4 | 155 | } |
donatien | 0:fd63457452f4 | 156 | |
donatien | 0:fd63457452f4 | 157 | void Logger::onMySQLResult(MySQLResult r) //Callback from MySQL client |
donatien | 0:fd63457452f4 | 158 | { |
donatien | 0:fd63457452f4 | 159 | //Update status |
donatien | 0:fd63457452f4 | 160 | m_sqlLastResult = r; |
donatien | 0:fd63457452f4 | 161 | if(m_sqlConnecting) //Check if we're connecting, if so this result is the result of the connection |
donatien | 0:fd63457452f4 | 162 | { |
donatien | 0:fd63457452f4 | 163 | m_sqlConnected = (r==0)?true:false; //If no error, we're connected |
donatien | 0:fd63457452f4 | 164 | m_sqlConnecting = false; |
donatien | 0:fd63457452f4 | 165 | if(r!=0) |
donatien | 0:fd63457452f4 | 166 | m_sqlConnectionError = true; //Else there was an error during connection |
donatien | 0:fd63457452f4 | 167 | } |
donatien | 0:fd63457452f4 | 168 | if( (r==MYSQL_DNS) || (r==MYSQL_PRTCL) || (r==MYSQL_AUTHFAILED) || (r==MYSQL_TIMEOUT) || (r==MYSQL_CONN) ) //Was this a fatal error? |
donatien | 0:fd63457452f4 | 169 | { |
donatien | 0:fd63457452f4 | 170 | //Connection has been closed, we must reconnect |
donatien | 0:fd63457452f4 | 171 | m_sqlConnected = false; |
donatien | 0:fd63457452f4 | 172 | m_sqlConnectionError = true; |
donatien | 0:fd63457452f4 | 173 | } |
donatien | 0:fd63457452f4 | 174 | } |