Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
main.cpp@175:d17460899bc9, 2014-08-28 (annotated)
- Committer:
- ansond
- Date:
- Thu Aug 28 21:08:34 2014 +0000
- Revision:
- 175:d17460899bc9
- Parent:
- 174:e8f11d1b5e3f
- Child:
- 176:3c86e0cf7e21
updates
Who changed what in which revision?
User | Revision | Line number | New 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 | 159:329e3271c87e | 24 | #ifdef CELLULAR_NETWORK |
ansond | 159:329e3271c87e | 25 | // MDMSerial |
ansond | 166:8e63cd2c003a | 26 | #ifdef NSP_CELLULAR_NETWORK |
ansond | 166:8e63cd2c003a | 27 | #include "mbed.h" |
ansond | 166:8e63cd2c003a | 28 | #include "rtos.h" |
ansond | 166:8e63cd2c003a | 29 | #endif |
ansond | 159:329e3271c87e | 30 | #include "MDM.h" |
ansond | 159:329e3271c87e | 31 | |
ansond | 159:329e3271c87e | 32 | // GPSParser |
ansond | 160:24337c83dd1d | 33 | #include "GPS.h" |
ansond | 159:329e3271c87e | 34 | #else |
ansond | 138:19502bc20369 | 35 | // Ethernet Interface |
ansond | 139:89cd0657b3c3 | 36 | #include "EthernetInterface.h" |
ansond | 138:19502bc20369 | 37 | EthernetInterface ethernet; |
ansond | 138:19502bc20369 | 38 | #endif |
ansond | 160:24337c83dd1d | 39 | |
ansond | 160:24337c83dd1d | 40 | #ifdef NETWORK_MUTEX |
ansond | 111:dac439bf902b | 41 | // Mutex for the network as RTOS doesnt play well with threads... |
ansond | 111:dac439bf902b | 42 | Mutex *network_mutex = NULL; |
ansond | 160:24337c83dd1d | 43 | #endif |
ansond | 111:dac439bf902b | 44 | |
ansond | 130:630d05daed77 | 45 | #ifdef _ENDPOINT_UBLOX_PLATFORM |
ansond | 130:630d05daed77 | 46 | // Serial Console Support |
ansond | 150:b898b05e9e23 | 47 | Serial pc(USBTX, USBRX); |
ansond | 130:630d05daed77 | 48 | |
ansond | 130:630d05daed77 | 49 | // LCD Support |
ansond | 130:630d05daed77 | 50 | #include "C12832.h" |
ansond | 131:c1ecc5fcf3c6 | 51 | C12832 lcd(D11, D13, D12, D7, D10); |
ansond | 130:630d05daed77 | 52 | #endif |
ansond | 130:630d05daed77 | 53 | |
ansond | 130:630d05daed77 | 54 | #ifdef _ENDPOINT_NXP_PLATFORM |
ansond | 130:630d05daed77 | 55 | // Serial Console Support |
ansond | 130:630d05daed77 | 56 | Serial pc(USBTX, USBRX); |
ansond | 130:630d05daed77 | 57 | |
ansond | 130:630d05daed77 | 58 | // LCD Support |
ansond | 130:630d05daed77 | 59 | #include "C12832_lcd.h" |
ansond | 130:630d05daed77 | 60 | C12832_LCD lcd; |
ansond | 130:630d05daed77 | 61 | #endif |
ansond | 130:630d05daed77 | 62 | |
ansond | 130:630d05daed77 | 63 | #ifdef _ENDPOINT_FREEDOM_PLATFORM |
ansond | 130:630d05daed77 | 64 | // Serial Console Support |
ansond | 130:630d05daed77 | 65 | Serial pc(USBTX, USBRX); |
ansond | 130:630d05daed77 | 66 | |
ansond | 130:630d05daed77 | 67 | // LCD Support |
ansond | 130:630d05daed77 | 68 | #include "C12832_lcd.h" |
ansond | 130:630d05daed77 | 69 | C12832_LCD lcd; |
ansond | 130:630d05daed77 | 70 | #endif |
ansond | 0:4c9bfcb3e759 | 71 | |
ansond | 0:4c9bfcb3e759 | 72 | // Instances for the Endpoint |
ansond | 0:4c9bfcb3e759 | 73 | ErrorHandler *error_handler = NULL; |
ansond | 0:4c9bfcb3e759 | 74 | MBEDEndpoint *endpoint = NULL; |
ansond | 18:bc165829bb88 | 75 | |
ansond | 174:e8f11d1b5e3f | 76 | #if SF_STATUS_REPORTING |
ansond | 174:e8f11d1b5e3f | 77 | // include Saleforce status reporting |
ansond | 174:e8f11d1b5e3f | 78 | #include "StatusReporter.h" |
ansond | 174:e8f11d1b5e3f | 79 | #endif |
ansond | 175:d17460899bc9 | 80 | void *sf_status_reporter = NULL; |
ansond | 174:e8f11d1b5e3f | 81 | |
ansond | 77:a60fd4a86397 | 82 | #ifdef MAC_ADDRESS |
ansond | 77:a60fd4a86397 | 83 | extern "C" void mbed_mac_address(char *s); |
ansond | 77:a60fd4a86397 | 84 | char mac[6] = {MAC_ADDRESS}; |
ansond | 77:a60fd4a86397 | 85 | char fmt_mac[RESOURCE_VALUE_LEN+1]; |
ansond | 77:a60fd4a86397 | 86 | #endif |
ansond | 77:a60fd4a86397 | 87 | |
ansond | 0:4c9bfcb3e759 | 88 | // ErrorHandler (logger) |
ansond | 20:f2dbbd852e08 | 89 | ErrorHandler *logger() { return error_handler; } |
ansond | 20:f2dbbd852e08 | 90 | |
ansond | 20:f2dbbd852e08 | 91 | // translate a closedown code to a string |
ansond | 20:f2dbbd852e08 | 92 | char str_code[16]; |
ansond | 20:f2dbbd852e08 | 93 | char *strCode(int code) { |
ansond | 20:f2dbbd852e08 | 94 | memset(str_code,0,16); |
ansond | 20:f2dbbd852e08 | 95 | if (code == 1) strcpy(str_code,"(USER)"); |
ansond | 20:f2dbbd852e08 | 96 | if (code == 2) strcpy(str_code,"(ERROR)"); |
ansond | 20:f2dbbd852e08 | 97 | return str_code; |
ansond | 20:f2dbbd852e08 | 98 | } |
ansond | 0:4c9bfcb3e759 | 99 | |
ansond | 0:4c9bfcb3e759 | 100 | // close down the application and exit |
ansond | 20:f2dbbd852e08 | 101 | void closedown(int code) { |
ansond | 0:4c9bfcb3e759 | 102 | if (endpoint != NULL) { |
ansond | 160:24337c83dd1d | 103 | #ifdef EH_USE_MUTEXES |
ansond | 70:055ebf51f6ad | 104 | error_handler->releaseMutexes(); |
ansond | 160:24337c83dd1d | 105 | #endif |
ansond | 0:4c9bfcb3e759 | 106 | error_handler->log("Closing down Endpoint..."); |
ansond | 0:4c9bfcb3e759 | 107 | delete endpoint; |
ansond | 0:4c9bfcb3e759 | 108 | } |
ansond | 26:791d22d43cb4 | 109 | if (error_handler != NULL) delete error_handler; |
ansond | 149:7fde829ed672 | 110 | #ifndef _ENDPOINT_UBLOX_PLATFORM |
ansond | 0:4c9bfcb3e759 | 111 | pc.printf("Exiting...\r\n"); |
ansond | 149:7fde829ed672 | 112 | #endif |
ansond | 20:f2dbbd852e08 | 113 | lcd.cls(); |
ansond | 0:4c9bfcb3e759 | 114 | lcd.locate(0,0); |
ansond | 20:f2dbbd852e08 | 115 | lcd.printf("Endpoint Shutdown %s",strCode(code)); |
ansond | 0:4c9bfcb3e759 | 116 | exit(1); |
ansond | 0:4c9bfcb3e759 | 117 | } |
ansond | 80:805143f36554 | 118 | |
ansond | 80:805143f36554 | 119 | // strdup() |
ansond | 80:805143f36554 | 120 | char *strdup(char *str) { |
ansond | 80:805143f36554 | 121 | if (str != NULL) { |
ansond | 81:9e902c2b855e | 122 | char *cp = (char *)malloc(strlen(str)+1); |
ansond | 80:805143f36554 | 123 | memset(cp,0,strlen(str)+1); |
ansond | 80:805143f36554 | 124 | memcpy(cp,str,strlen(str)); |
ansond | 80:805143f36554 | 125 | return cp; |
ansond | 80:805143f36554 | 126 | } |
ansond | 80:805143f36554 | 127 | return NULL; |
ansond | 80:805143f36554 | 128 | } |
ansond | 151:d6500663ed8c | 129 | |
ansond | 157:33db0c5e0bd8 | 130 | // HARD RESET - ugly but necessary - we seem to develop memory holes.... :( |
ansond | 157:33db0c5e0bd8 | 131 | extern "C" void HardFault_Handler() { NVIC_SystemReset(); } |
ansond | 157:33db0c5e0bd8 | 132 | |
ansond | 151:d6500663ed8c | 133 | // main entry point |
ansond | 152:659a8ebcbe93 | 134 | int main() { |
ansond | 119:c620eb98af8b | 135 | #ifdef NETWORK_MUTEX |
ansond | 111:dac439bf902b | 136 | network_mutex = new Mutex(); |
ansond | 111:dac439bf902b | 137 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 119:c620eb98af8b | 138 | #endif |
ansond | 77:a60fd4a86397 | 139 | #ifdef MAC_ADDRESS |
ansond | 77:a60fd4a86397 | 140 | mbed_mac_address(mac); |
ansond | 77:a60fd4a86397 | 141 | memset(fmt_mac,0,RESOURCE_VALUE_LEN+1); |
ansond | 78:afb93ba1dbfd | 142 | sprintf(fmt_mac,"%02x:%02x:%02x:%02x:%02x:%02x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); |
ansond | 77:a60fd4a86397 | 143 | #endif |
ansond | 0:4c9bfcb3e759 | 144 | error_handler = new ErrorHandler(&pc,&lcd); |
ansond | 174:e8f11d1b5e3f | 145 | |
ansond | 174:e8f11d1b5e3f | 146 | #if SF_STATUS_REPORTING |
ansond | 175:d17460899bc9 | 147 | sf_status_reporter = (void *)new StatusReporter(error_handler); |
ansond | 174:e8f11d1b5e3f | 148 | #endif |
ansond | 174:e8f11d1b5e3f | 149 | |
ansond | 152:659a8ebcbe93 | 150 | #ifdef CELLULAR_NETWORK |
ansond | 166:8e63cd2c003a | 151 | #ifdef NSP_CELLULAR_NETWORK |
ansond | 166:8e63cd2c003a | 152 | MDMRtos<MDMSerial> modem; |
ansond | 166:8e63cd2c003a | 153 | #else |
ansond | 166:8e63cd2c003a | 154 | MDMSerial modem; |
ansond | 166:8e63cd2c003a | 155 | #endif |
ansond | 174:e8f11d1b5e3f | 156 | endpoint = new MBEDEndpoint(error_handler,&modem,sf_status_reporter,NULL); |
ansond | 152:659a8ebcbe93 | 157 | #else |
ansond | 174:e8f11d1b5e3f | 158 | endpoint = new MBEDEndpoint(error_handler,ðernet,sf_status_reporter,NULL); |
ansond | 138:19502bc20369 | 159 | #endif |
ansond | 0:4c9bfcb3e759 | 160 | if (endpoint != NULL) endpoint->run(); |
ansond | 80:805143f36554 | 161 | } |