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@157:33db0c5e0bd8, 2014-05-13 (annotated)
- Committer:
- ansond
- Date:
- Tue May 13 20:27:11 2014 +0000
- Revision:
- 157:33db0c5e0bd8
- Parent:
- 155:582462821bd7
- Child:
- 159:329e3271c87e
added nvic hard fault handler
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 | 138:19502bc20369 | 24 | #ifndef CELLULAR_NETWORK |
ansond | 138:19502bc20369 | 25 | // Ethernet Interface |
ansond | 139:89cd0657b3c3 | 26 | #include "EthernetInterface.h" |
ansond | 138:19502bc20369 | 27 | EthernetInterface ethernet; |
ansond | 138:19502bc20369 | 28 | #endif |
ansond | 137:adc39c42b169 | 29 | |
ansond | 111:dac439bf902b | 30 | // Mutex for the network as RTOS doesnt play well with threads... |
ansond | 111:dac439bf902b | 31 | Mutex *network_mutex = NULL; |
ansond | 111:dac439bf902b | 32 | |
ansond | 130:630d05daed77 | 33 | #ifdef _ENDPOINT_UBLOX_PLATFORM |
ansond | 130:630d05daed77 | 34 | // Serial Console Support |
ansond | 150:b898b05e9e23 | 35 | Serial pc(USBTX, USBRX); |
ansond | 130:630d05daed77 | 36 | |
ansond | 130:630d05daed77 | 37 | // LCD Support |
ansond | 130:630d05daed77 | 38 | #include "C12832.h" |
ansond | 131:c1ecc5fcf3c6 | 39 | C12832 lcd(D11, D13, D12, D7, D10); |
ansond | 130:630d05daed77 | 40 | #endif |
ansond | 130:630d05daed77 | 41 | |
ansond | 130:630d05daed77 | 42 | #ifdef _ENDPOINT_NXP_PLATFORM |
ansond | 130:630d05daed77 | 43 | // Serial Console Support |
ansond | 130:630d05daed77 | 44 | Serial pc(USBTX, USBRX); |
ansond | 130:630d05daed77 | 45 | |
ansond | 130:630d05daed77 | 46 | // LCD Support |
ansond | 130:630d05daed77 | 47 | #include "C12832_lcd.h" |
ansond | 130:630d05daed77 | 48 | C12832_LCD lcd; |
ansond | 130:630d05daed77 | 49 | #endif |
ansond | 130:630d05daed77 | 50 | |
ansond | 130:630d05daed77 | 51 | #ifdef _ENDPOINT_FREEDOM_PLATFORM |
ansond | 130:630d05daed77 | 52 | // Serial Console Support |
ansond | 130:630d05daed77 | 53 | Serial pc(USBTX, USBRX); |
ansond | 130:630d05daed77 | 54 | |
ansond | 130:630d05daed77 | 55 | // LCD Support |
ansond | 130:630d05daed77 | 56 | #include "C12832_lcd.h" |
ansond | 130:630d05daed77 | 57 | C12832_LCD lcd; |
ansond | 130:630d05daed77 | 58 | #endif |
ansond | 0:4c9bfcb3e759 | 59 | |
ansond | 0:4c9bfcb3e759 | 60 | // Instances for the Endpoint |
ansond | 0:4c9bfcb3e759 | 61 | ErrorHandler *error_handler = NULL; |
ansond | 0:4c9bfcb3e759 | 62 | MBEDEndpoint *endpoint = NULL; |
ansond | 18:bc165829bb88 | 63 | |
ansond | 77:a60fd4a86397 | 64 | #ifdef MAC_ADDRESS |
ansond | 77:a60fd4a86397 | 65 | extern "C" void mbed_mac_address(char *s); |
ansond | 77:a60fd4a86397 | 66 | char mac[6] = {MAC_ADDRESS}; |
ansond | 77:a60fd4a86397 | 67 | char fmt_mac[RESOURCE_VALUE_LEN+1]; |
ansond | 77:a60fd4a86397 | 68 | #endif |
ansond | 77:a60fd4a86397 | 69 | |
ansond | 0:4c9bfcb3e759 | 70 | // ErrorHandler (logger) |
ansond | 20:f2dbbd852e08 | 71 | ErrorHandler *logger() { return error_handler; } |
ansond | 20:f2dbbd852e08 | 72 | |
ansond | 20:f2dbbd852e08 | 73 | // translate a closedown code to a string |
ansond | 20:f2dbbd852e08 | 74 | char str_code[16]; |
ansond | 20:f2dbbd852e08 | 75 | char *strCode(int code) { |
ansond | 20:f2dbbd852e08 | 76 | memset(str_code,0,16); |
ansond | 20:f2dbbd852e08 | 77 | if (code == 1) strcpy(str_code,"(USER)"); |
ansond | 20:f2dbbd852e08 | 78 | if (code == 2) strcpy(str_code,"(ERROR)"); |
ansond | 20:f2dbbd852e08 | 79 | return str_code; |
ansond | 20:f2dbbd852e08 | 80 | } |
ansond | 0:4c9bfcb3e759 | 81 | |
ansond | 0:4c9bfcb3e759 | 82 | // close down the application and exit |
ansond | 20:f2dbbd852e08 | 83 | void closedown(int code) { |
ansond | 0:4c9bfcb3e759 | 84 | if (endpoint != NULL) { |
ansond | 70:055ebf51f6ad | 85 | error_handler->releaseMutexes(); |
ansond | 0:4c9bfcb3e759 | 86 | error_handler->log("Closing down Endpoint..."); |
ansond | 0:4c9bfcb3e759 | 87 | delete endpoint; |
ansond | 0:4c9bfcb3e759 | 88 | } |
ansond | 26:791d22d43cb4 | 89 | if (error_handler != NULL) delete error_handler; |
ansond | 149:7fde829ed672 | 90 | #ifndef _ENDPOINT_UBLOX_PLATFORM |
ansond | 0:4c9bfcb3e759 | 91 | pc.printf("Exiting...\r\n"); |
ansond | 149:7fde829ed672 | 92 | #endif |
ansond | 20:f2dbbd852e08 | 93 | lcd.cls(); |
ansond | 0:4c9bfcb3e759 | 94 | lcd.locate(0,0); |
ansond | 20:f2dbbd852e08 | 95 | lcd.printf("Endpoint Shutdown %s",strCode(code)); |
ansond | 0:4c9bfcb3e759 | 96 | exit(1); |
ansond | 0:4c9bfcb3e759 | 97 | } |
ansond | 80:805143f36554 | 98 | |
ansond | 80:805143f36554 | 99 | // strdup() |
ansond | 80:805143f36554 | 100 | char *strdup(char *str) { |
ansond | 80:805143f36554 | 101 | if (str != NULL) { |
ansond | 81:9e902c2b855e | 102 | char *cp = (char *)malloc(strlen(str)+1); |
ansond | 80:805143f36554 | 103 | memset(cp,0,strlen(str)+1); |
ansond | 80:805143f36554 | 104 | memcpy(cp,str,strlen(str)); |
ansond | 80:805143f36554 | 105 | return cp; |
ansond | 80:805143f36554 | 106 | } |
ansond | 80:805143f36554 | 107 | return NULL; |
ansond | 80:805143f36554 | 108 | } |
ansond | 151:d6500663ed8c | 109 | |
ansond | 154:6e60f310ab78 | 110 | #ifdef CELLULAR_NETWORK |
ansond | 154:6e60f310ab78 | 111 | int _ublox_cell_connect_status = 0; |
ansond | 154:6e60f310ab78 | 112 | int getUbloxConnectStatus() { return _ublox_cell_connect_status; } |
ansond | 155:582462821bd7 | 113 | C027 c027; |
ansond | 155:582462821bd7 | 114 | |
ansond | 155:582462821bd7 | 115 | /* |
ansond | 155:582462821bd7 | 116 | #include "Websocket.h" |
ansond | 155:582462821bd7 | 117 | |
ansond | 155:582462821bd7 | 118 | void ublox_test_modem(int index) { |
ansond | 155:582462821bd7 | 119 | // See the output on http://sockets.mbed.org/demo/viewer |
ansond | 155:582462821bd7 | 120 | char *url = "ws://sockets.mbed.org:443/ws/demo/rw"; |
ansond | 155:582462821bd7 | 121 | Websocket ws(url); |
ansond | 155:582462821bd7 | 122 | |
ansond | 155:582462821bd7 | 123 | pc.printf("connecting to websocket: %s\r\n",url); |
ansond | 155:582462821bd7 | 124 | bool c = ws.connect(); |
ansond | 155:582462821bd7 | 125 | pc.printf("Connect result: %s\n", c?"OK":"Failed"); |
ansond | 155:582462821bd7 | 126 | |
ansond | 155:582462821bd7 | 127 | char msg[512]; |
ansond | 155:582462821bd7 | 128 | memset(msg,0,512); |
ansond | 155:582462821bd7 | 129 | sprintf(msg,"testing mbed websockets...123...%d",index); |
ansond | 155:582462821bd7 | 130 | |
ansond | 155:582462821bd7 | 131 | pc.printf("Sending data...\r\n"); |
ansond | 155:582462821bd7 | 132 | ws.send(msg); |
ansond | 155:582462821bd7 | 133 | wait(0.5f); |
ansond | 155:582462821bd7 | 134 | memset(msg,0,512); |
ansond | 155:582462821bd7 | 135 | |
ansond | 155:582462821bd7 | 136 | pc.printf("Receiving data...\r\n"); |
ansond | 155:582462821bd7 | 137 | if (ws.read(msg)) |
ansond | 155:582462821bd7 | 138 | pc.printf("Websocket Receive: [%s]\r\n", msg); |
ansond | 155:582462821bd7 | 139 | else |
ansond | 155:582462821bd7 | 140 | pc.printf("Error in reading from websocket!\r\n"); |
ansond | 155:582462821bd7 | 141 | |
ansond | 155:582462821bd7 | 142 | pc.printf("closing websockets."); |
ansond | 155:582462821bd7 | 143 | ws.close(); |
ansond | 155:582462821bd7 | 144 | } |
ansond | 155:582462821bd7 | 145 | */ |
ansond | 155:582462821bd7 | 146 | |
ansond | 154:6e60f310ab78 | 147 | #endif |
ansond | 154:6e60f310ab78 | 148 | |
ansond | 157:33db0c5e0bd8 | 149 | // HARD RESET - ugly but necessary - we seem to develop memory holes.... :( |
ansond | 157:33db0c5e0bd8 | 150 | extern "C" void HardFault_Handler() { NVIC_SystemReset(); } |
ansond | 157:33db0c5e0bd8 | 151 | |
ansond | 151:d6500663ed8c | 152 | // main entry point |
ansond | 152:659a8ebcbe93 | 153 | int main() { |
ansond | 147:f370b21f696e | 154 | #ifdef CELLULAR_NETWORK |
ansond | 154:6e60f310ab78 | 155 | c027.mdmUsbEnable(true); |
ansond | 154:6e60f310ab78 | 156 | c027.mdmPower(true); |
ansond | 154:6e60f310ab78 | 157 | c027.mdmReset(); |
ansond | 154:6e60f310ab78 | 158 | c027.mdmWakeup(); |
ansond | 152:659a8ebcbe93 | 159 | UBLOX_MODEM modem(NC,true,1); |
ansond | 154:6e60f310ab78 | 160 | modem.power(true); |
ansond | 155:582462821bd7 | 161 | Thread::wait(3000); |
ansond | 154:6e60f310ab78 | 162 | _ublox_cell_connect_status = modem.connect("internet"); |
ansond | 154:6e60f310ab78 | 163 | if (_ublox_cell_connect_status) pc.printf("Unable to connect cell modem\r\n"); |
ansond | 154:6e60f310ab78 | 164 | else pc.printf("Cell modem connected!\r\n"); |
ansond | 155:582462821bd7 | 165 | //ublox_test_modem(1); |
ansond | 155:582462821bd7 | 166 | //ublox_test_modem(2); |
ansond | 155:582462821bd7 | 167 | //ublox_test_modem(3); |
ansond | 147:f370b21f696e | 168 | #endif |
ansond | 119:c620eb98af8b | 169 | #ifdef NETWORK_MUTEX |
ansond | 111:dac439bf902b | 170 | network_mutex = new Mutex(); |
ansond | 111:dac439bf902b | 171 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 119:c620eb98af8b | 172 | #endif |
ansond | 77:a60fd4a86397 | 173 | #ifdef MAC_ADDRESS |
ansond | 77:a60fd4a86397 | 174 | mbed_mac_address(mac); |
ansond | 77:a60fd4a86397 | 175 | memset(fmt_mac,0,RESOURCE_VALUE_LEN+1); |
ansond | 78:afb93ba1dbfd | 176 | sprintf(fmt_mac,"%02x:%02x:%02x:%02x:%02x:%02x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); |
ansond | 77:a60fd4a86397 | 177 | #endif |
ansond | 0:4c9bfcb3e759 | 178 | error_handler = new ErrorHandler(&pc,&lcd); |
ansond | 152:659a8ebcbe93 | 179 | #ifdef CELLULAR_NETWORK |
ansond | 152:659a8ebcbe93 | 180 | endpoint = new MBEDEndpoint(error_handler,&modem,&c027); |
ansond | 152:659a8ebcbe93 | 181 | #else |
ansond | 148:0b7e6632acbe | 182 | endpoint = new MBEDEndpoint(error_handler,ðernet,NULL); |
ansond | 138:19502bc20369 | 183 | #endif |
ansond | 0:4c9bfcb3e759 | 184 | if (endpoint != NULL) endpoint->run(); |
ansond | 80:805143f36554 | 185 | } |