Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

main.cpp

Committer:
ansond
Date:
2014-03-04
Revision:
77:a60fd4a86397
Parent:
70:055ebf51f6ad
Child:
78:afb93ba1dbfd

File content as of revision 77:a60fd4a86397:

/* Copyright C2013 Doug Anson, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files the "Software", to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "MBEDEndpoint.h"
 
 #include <string.h>
 #include <stdlib.h>
 
 // Ethernet Interface
 EthernetInterface ethernet;
 
 // Serial Console Support
 Serial pc(USBTX, USBRX);
 
 // LCD Support
 #include "C12832_lcd.h"
 C12832_LCD lcd;
 
 // Instances for the Endpoint
 ErrorHandler *error_handler = NULL;
 MBEDEndpoint *endpoint = NULL;
 
 #ifdef MAC_ADDRESS
 extern "C" void mbed_mac_address(char *s);
 char mac[6] = {MAC_ADDRESS};
 char fmt_mac[RESOURCE_VALUE_LEN+1];
 #endif
 
 // ErrorHandler (logger)
 ErrorHandler *logger() { return error_handler; }    
 
 // translate a closedown code to a string
 char str_code[16];
 char *strCode(int code) {
     memset(str_code,0,16);
     if (code == 1) strcpy(str_code,"(USER)");
     if (code == 2) strcpy(str_code,"(ERROR)");
     return str_code;
 } 

 // close down the application and exit
 void closedown(int code) {
     if (endpoint != NULL) {
         error_handler->releaseMutexes();
         error_handler->log("Closing down Endpoint...");
         delete endpoint;
     }
     if (error_handler != NULL) delete error_handler;
     pc.printf("Exiting...\r\n");
     lcd.cls();
     lcd.locate(0,0);
     lcd.printf("Endpoint Shutdown %s",strCode(code));
     exit(1);
 }

// Main Entry
int main() {
#ifdef MAC_ADDRESS
    mbed_mac_address(mac);
    memset(fmt_mac,0,RESOURCE_VALUE_LEN+1);
    sprintf(fmt_mac,"%x:%x:%x:%x:%x:%x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
#endif
    error_handler = new ErrorHandler(&pc,&lcd);
    endpoint = new MBEDEndpoint(error_handler,&ethernet);
    if (endpoint != NULL) endpoint->run();
}