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

Committer:
ansond
Date:
Mon Mar 17 16:56:41 2014 +0000
Revision:
111:dac439bf902b
Parent:
81:9e902c2b855e
Child:
112:1fb53d4729af
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:4c9bfcb3e759 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:4c9bfcb3e759 2 *
ansond 0:4c9bfcb3e759 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:4c9bfcb3e759 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:4c9bfcb3e759 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:4c9bfcb3e759 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:4c9bfcb3e759 7 * furnished to do so, subject to the following conditions:
ansond 0:4c9bfcb3e759 8 *
ansond 0:4c9bfcb3e759 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:4c9bfcb3e759 10 * substantial portions of the Software.
ansond 0:4c9bfcb3e759 11 *
ansond 0:4c9bfcb3e759 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:4c9bfcb3e759 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:4c9bfcb3e759 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:4c9bfcb3e759 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:4c9bfcb3e759 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:4c9bfcb3e759 17 */
ansond 0:4c9bfcb3e759 18
ansond 0:4c9bfcb3e759 19 #include "MBEDEndpoint.h"
ansond 0:4c9bfcb3e759 20
ansond 18:bc165829bb88 21 #include <string.h>
ansond 18:bc165829bb88 22 #include <stdlib.h>
ansond 18:bc165829bb88 23
ansond 0:4c9bfcb3e759 24 // Ethernet Interface
ansond 0:4c9bfcb3e759 25 EthernetInterface ethernet;
ansond 0:4c9bfcb3e759 26
ansond 111:dac439bf902b 27 // Mutex for the network as RTOS doesnt play well with threads...
ansond 111:dac439bf902b 28 Mutex *network_mutex = NULL;
ansond 111:dac439bf902b 29
ansond 0:4c9bfcb3e759 30 // Serial Console Support
ansond 68:e6431dfe2f30 31 Serial pc(USBTX, USBRX);
ansond 0:4c9bfcb3e759 32
ansond 0:4c9bfcb3e759 33 // LCD Support
ansond 0:4c9bfcb3e759 34 #include "C12832_lcd.h"
ansond 0:4c9bfcb3e759 35 C12832_LCD lcd;
ansond 0:4c9bfcb3e759 36
ansond 0:4c9bfcb3e759 37 // Instances for the Endpoint
ansond 0:4c9bfcb3e759 38 ErrorHandler *error_handler = NULL;
ansond 0:4c9bfcb3e759 39 MBEDEndpoint *endpoint = NULL;
ansond 18:bc165829bb88 40
ansond 77:a60fd4a86397 41 #ifdef MAC_ADDRESS
ansond 77:a60fd4a86397 42 extern "C" void mbed_mac_address(char *s);
ansond 77:a60fd4a86397 43 char mac[6] = {MAC_ADDRESS};
ansond 77:a60fd4a86397 44 char fmt_mac[RESOURCE_VALUE_LEN+1];
ansond 77:a60fd4a86397 45 #endif
ansond 77:a60fd4a86397 46
ansond 0:4c9bfcb3e759 47 // ErrorHandler (logger)
ansond 20:f2dbbd852e08 48 ErrorHandler *logger() { return error_handler; }
ansond 20:f2dbbd852e08 49
ansond 20:f2dbbd852e08 50 // translate a closedown code to a string
ansond 20:f2dbbd852e08 51 char str_code[16];
ansond 20:f2dbbd852e08 52 char *strCode(int code) {
ansond 20:f2dbbd852e08 53 memset(str_code,0,16);
ansond 20:f2dbbd852e08 54 if (code == 1) strcpy(str_code,"(USER)");
ansond 20:f2dbbd852e08 55 if (code == 2) strcpy(str_code,"(ERROR)");
ansond 20:f2dbbd852e08 56 return str_code;
ansond 20:f2dbbd852e08 57 }
ansond 0:4c9bfcb3e759 58
ansond 0:4c9bfcb3e759 59 // close down the application and exit
ansond 20:f2dbbd852e08 60 void closedown(int code) {
ansond 0:4c9bfcb3e759 61 if (endpoint != NULL) {
ansond 70:055ebf51f6ad 62 error_handler->releaseMutexes();
ansond 0:4c9bfcb3e759 63 error_handler->log("Closing down Endpoint...");
ansond 0:4c9bfcb3e759 64 delete endpoint;
ansond 0:4c9bfcb3e759 65 }
ansond 26:791d22d43cb4 66 if (error_handler != NULL) delete error_handler;
ansond 0:4c9bfcb3e759 67 pc.printf("Exiting...\r\n");
ansond 20:f2dbbd852e08 68 lcd.cls();
ansond 0:4c9bfcb3e759 69 lcd.locate(0,0);
ansond 20:f2dbbd852e08 70 lcd.printf("Endpoint Shutdown %s",strCode(code));
ansond 0:4c9bfcb3e759 71 exit(1);
ansond 0:4c9bfcb3e759 72 }
ansond 80:805143f36554 73
ansond 80:805143f36554 74 // strdup()
ansond 80:805143f36554 75 char *strdup(char *str) {
ansond 80:805143f36554 76 if (str != NULL) {
ansond 81:9e902c2b855e 77 char *cp = (char *)malloc(strlen(str)+1);
ansond 80:805143f36554 78 memset(cp,0,strlen(str)+1);
ansond 80:805143f36554 79 memcpy(cp,str,strlen(str));
ansond 80:805143f36554 80 return cp;
ansond 80:805143f36554 81 }
ansond 80:805143f36554 82 return NULL;
ansond 80:805143f36554 83 }
ansond 0:4c9bfcb3e759 84
ansond 80:805143f36554 85 // Main Entry
ansond 80:805143f36554 86 int main() {
ansond 111:dac439bf902b 87 network_mutex = new Mutex();
ansond 111:dac439bf902b 88 if (network_mutex != NULL) network_mutex->unlock();
ansond 77:a60fd4a86397 89 #ifdef MAC_ADDRESS
ansond 77:a60fd4a86397 90 mbed_mac_address(mac);
ansond 77:a60fd4a86397 91 memset(fmt_mac,0,RESOURCE_VALUE_LEN+1);
ansond 78:afb93ba1dbfd 92 sprintf(fmt_mac,"%02x:%02x:%02x:%02x:%02x:%02x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
ansond 77:a60fd4a86397 93 #endif
ansond 0:4c9bfcb3e759 94 error_handler = new ErrorHandler(&pc,&lcd);
ansond 0:4c9bfcb3e759 95 endpoint = new MBEDEndpoint(error_handler,&ethernet);
ansond 0:4c9bfcb3e759 96 if (endpoint != NULL) endpoint->run();
ansond 80:805143f36554 97 }