Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832_lcd EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed
MBEDEndpoint.cpp@7:f570eb3f38cd, 2014-02-26 (annotated)
- Committer:
- ansond
- Date:
- Wed Feb 26 18:59:08 2014 +0000
- Revision:
- 7:f570eb3f38cd
- Parent:
- 6:34c07e145caa
- Child:
- 8:45f9a920e82c
updates
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ansond | 0:ae2a45502448 | 1 | /* Copyright C2013 Doug Anson, MIT License |
| ansond | 0:ae2a45502448 | 2 | * |
| ansond | 0:ae2a45502448 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| ansond | 0:ae2a45502448 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
| ansond | 0:ae2a45502448 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| ansond | 0:ae2a45502448 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| ansond | 0:ae2a45502448 | 7 | * furnished to do so, subject to the following conditions: |
| ansond | 0:ae2a45502448 | 8 | * |
| ansond | 0:ae2a45502448 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| ansond | 0:ae2a45502448 | 10 | * substantial portions of the Software. |
| ansond | 0:ae2a45502448 | 11 | * |
| ansond | 0:ae2a45502448 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| ansond | 0:ae2a45502448 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| ansond | 0:ae2a45502448 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| ansond | 0:ae2a45502448 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| ansond | 0:ae2a45502448 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| ansond | 0:ae2a45502448 | 17 | */ |
| ansond | 0:ae2a45502448 | 18 | |
| ansond | 0:ae2a45502448 | 19 | #include "MQTTTransport.h" |
| ansond | 0:ae2a45502448 | 20 | #include "MBEDEndpoint.h" |
| ansond | 0:ae2a45502448 | 21 | |
| ansond | 0:ae2a45502448 | 22 | // MBED Light support |
| ansond | 0:ae2a45502448 | 23 | #include "MBEDLight.h" |
| ansond | 0:ae2a45502448 | 24 | |
| ansond | 2:90a84a216c58 | 25 | // Emulated Resource Factory |
| ansond | 2:90a84a216c58 | 26 | #include "EmulatedResourceFactory.h" |
| ansond | 2:90a84a216c58 | 27 | |
| ansond | 0:ae2a45502448 | 28 | // Emulated Actions we can act on |
| ansond | 0:ae2a45502448 | 29 | #include "EmulatedLightDimmerAction.h" |
| ansond | 0:ae2a45502448 | 30 | #include "EmulatedLightSwitchAction.h" |
| ansond | 7:f570eb3f38cd | 31 | |
| ansond | 7:f570eb3f38cd | 32 | // shutdown endpoint reference |
| ansond | 7:f570eb3f38cd | 33 | extern void closedown(int code); |
| ansond | 0:ae2a45502448 | 34 | |
| ansond | 0:ae2a45502448 | 35 | // default constructor |
| ansond | 0:ae2a45502448 | 36 | MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) { |
| ansond | 0:ae2a45502448 | 37 | bool success = true; |
| ansond | 0:ae2a45502448 | 38 | for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL; |
| ansond | 0:ae2a45502448 | 39 | this->m_error_handler = error_handler; |
| ansond | 0:ae2a45502448 | 40 | this->logger()->log(ENDPOINT_VERSION_ANNOUNCE); |
| ansond | 0:ae2a45502448 | 41 | if (success) this->logger()->turnLEDBlue(); |
| ansond | 0:ae2a45502448 | 42 | if (success) success = this->initializeEthernet(ethernet); |
| ansond | 0:ae2a45502448 | 43 | if (success) this->logger()->turnLEDYellow(); |
| ansond | 0:ae2a45502448 | 44 | if (success) success = this->initializeTransports(); |
| ansond | 0:ae2a45502448 | 45 | if (success) success = this->initializeLights(); |
| ansond | 0:ae2a45502448 | 46 | if (success) this->logger()->turnLEDGreen(); |
| ansond | 7:f570eb3f38cd | 47 | if (!success) closedown(2); |
| ansond | 0:ae2a45502448 | 48 | } |
| ansond | 0:ae2a45502448 | 49 | |
| ansond | 0:ae2a45502448 | 50 | // default destructor |
| ansond | 0:ae2a45502448 | 51 | MBEDEndpoint::~MBEDEndpoint() { |
| ansond | 0:ae2a45502448 | 52 | bool success = true; |
| ansond | 0:ae2a45502448 | 53 | if (success) this->logger()->turnLEDYellow(); |
| ansond | 0:ae2a45502448 | 54 | if (success) success = this->closeLights(); |
| ansond | 0:ae2a45502448 | 55 | if (success) success = this->closeTransports(); |
| ansond | 0:ae2a45502448 | 56 | if (success) success = this->closeEthernet(); |
| ansond | 0:ae2a45502448 | 57 | if (success) this->logger()->turnLEDBlue(); |
| ansond | 0:ae2a45502448 | 58 | } |
| ansond | 0:ae2a45502448 | 59 | |
| ansond | 0:ae2a45502448 | 60 | // initialize the Lights |
| ansond | 0:ae2a45502448 | 61 | bool MBEDEndpoint::initializeLights() { |
| ansond | 0:ae2a45502448 | 62 | this->logger()->log("Initializing Lights..."); |
| ansond | 0:ae2a45502448 | 63 | for(int i=0;i<NUM_LIGHTS;++i) { |
| ansond | 0:ae2a45502448 | 64 | this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this); |
| ansond | 0:ae2a45502448 | 65 | this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i])); |
| ansond | 0:ae2a45502448 | 66 | this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i])); |
| ansond | 0:ae2a45502448 | 67 | } |
| ansond | 0:ae2a45502448 | 68 | return true; |
| ansond | 0:ae2a45502448 | 69 | } |
| ansond | 0:ae2a45502448 | 70 | |
| ansond | 0:ae2a45502448 | 71 | // does the input name match any of our light resources? |
| ansond | 0:ae2a45502448 | 72 | int MBEDEndpoint::indexOfLight(char *name) { |
| ansond | 0:ae2a45502448 | 73 | bool found = false; |
| ansond | 0:ae2a45502448 | 74 | int index = -1; |
| ansond | 0:ae2a45502448 | 75 | |
| ansond | 0:ae2a45502448 | 76 | for(int i=0;i<NUM_LIGHTS && !found;++i) { |
| ansond | 0:ae2a45502448 | 77 | if (strcmp(this->m_lights[i]->getName(),name) == 0) { |
| ansond | 0:ae2a45502448 | 78 | found = true; |
| ansond | 0:ae2a45502448 | 79 | index = i; |
| ansond | 0:ae2a45502448 | 80 | } |
| ansond | 0:ae2a45502448 | 81 | } |
| ansond | 0:ae2a45502448 | 82 | |
| ansond | 0:ae2a45502448 | 83 | return index; |
| ansond | 0:ae2a45502448 | 84 | } |
| ansond | 0:ae2a45502448 | 85 | |
| ansond | 0:ae2a45502448 | 86 | // get a specific resources |
| ansond | 0:ae2a45502448 | 87 | ResourceFactory *MBEDEndpoint::getResources(int index) { |
| ansond | 0:ae2a45502448 | 88 | if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources(); |
| ansond | 0:ae2a45502448 | 89 | return NULL; |
| ansond | 0:ae2a45502448 | 90 | } |
| ansond | 0:ae2a45502448 | 91 | |
| ansond | 0:ae2a45502448 | 92 | // initialize our ResourceFactory |
| ansond | 2:90a84a216c58 | 93 | ResourceFactory *MBEDEndpoint::initResourceFactory() { return new EmulatedResourceFactory(this->logger()); } |
| ansond | 2:90a84a216c58 | 94 | |
| ansond | 2:90a84a216c58 | 95 | // get our endpoint name (first light name) |
| ansond | 6:34c07e145caa | 96 | char *MBEDEndpoint::getEndpointName() { return this->m_lights[0]->getName(); } |
| ansond | 0:ae2a45502448 | 97 | |
| ansond | 0:ae2a45502448 | 98 | // initialize a specific transport |
| ansond | 0:ae2a45502448 | 99 | bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) { |
| ansond | 0:ae2a45502448 | 100 | bool success = false; |
| ansond | 0:ae2a45502448 | 101 | if (this->m_transports[index] == NULL) { |
| ansond | 0:ae2a45502448 | 102 | this->logger()->log("Initializing %s Transport...", key); |
| ansond | 0:ae2a45502448 | 103 | this->m_transports[index] = transport; |
| ansond | 0:ae2a45502448 | 104 | if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect(); |
| ansond | 0:ae2a45502448 | 105 | } |
| ansond | 0:ae2a45502448 | 106 | else { |
| ansond | 0:ae2a45502448 | 107 | this->logger()->log("%s already connected (OK)...", key); |
| ansond | 0:ae2a45502448 | 108 | success = true; |
| ansond | 0:ae2a45502448 | 109 | } |
| ansond | 0:ae2a45502448 | 110 | return success; |
| ansond | 0:ae2a45502448 | 111 | } |
| ansond | 0:ae2a45502448 | 112 | |
| ansond | 0:ae2a45502448 | 113 | // initialize our transports |
| ansond | 0:ae2a45502448 | 114 | bool MBEDEndpoint::initializeTransports() { |
| ansond | 0:ae2a45502448 | 115 | bool success = true; |
| ansond | 0:ae2a45502448 | 116 | |
| ansond | 0:ae2a45502448 | 117 | if (success == true) { |
| ansond | 0:ae2a45502448 | 118 | // MQTT Initialization |
| ansond | 7:f570eb3f38cd | 119 | success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this)); |
| ansond | 0:ae2a45502448 | 120 | } |
| ansond | 0:ae2a45502448 | 121 | |
| ansond | 0:ae2a45502448 | 122 | if (success == true) { |
| ansond | 0:ae2a45502448 | 123 | // HTTP Initialization |
| ansond | 7:f570eb3f38cd | 124 | success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler,this)); |
| ansond | 0:ae2a45502448 | 125 | } |
| ansond | 0:ae2a45502448 | 126 | return success; |
| ansond | 0:ae2a45502448 | 127 | } |
| ansond | 0:ae2a45502448 | 128 | |
| ansond | 0:ae2a45502448 | 129 | // initialize our Ethernet |
| ansond | 0:ae2a45502448 | 130 | bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) { |
| ansond | 0:ae2a45502448 | 131 | bool success = false; |
| ansond | 0:ae2a45502448 | 132 | this->m_ethernet = ethernet; |
| ansond | 0:ae2a45502448 | 133 | if (this->m_ethernet != NULL) { |
| ansond | 0:ae2a45502448 | 134 | this->logger()->log("Initializing Ethernet..."); |
| ansond | 0:ae2a45502448 | 135 | |
| ansond | 0:ae2a45502448 | 136 | // connect up ethernet |
| ansond | 0:ae2a45502448 | 137 | this->m_ethernet->init(); |
| ansond | 0:ae2a45502448 | 138 | this->m_ethernet->connect(); |
| ansond | 0:ae2a45502448 | 139 | |
| ansond | 0:ae2a45502448 | 140 | // display our IP address |
| ansond | 0:ae2a45502448 | 141 | char *ipaddr = this->m_ethernet->getIPAddress(); |
| ansond | 0:ae2a45502448 | 142 | if (ipaddr != NULL && strlen(ipaddr) > 0) { |
| ansond | 0:ae2a45502448 | 143 | this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress()); |
| ansond | 0:ae2a45502448 | 144 | success = true; |
| ansond | 0:ae2a45502448 | 145 | } |
| ansond | 0:ae2a45502448 | 146 | else { |
| ansond | 0:ae2a45502448 | 147 | this->logger()->log("Ethernet Not Connected..."); |
| ansond | 0:ae2a45502448 | 148 | success = false; |
| ansond | 0:ae2a45502448 | 149 | } |
| ansond | 0:ae2a45502448 | 150 | } |
| ansond | 0:ae2a45502448 | 151 | else { |
| ansond | 0:ae2a45502448 | 152 | this->logger()->log("No Ethernet instance found"); |
| ansond | 0:ae2a45502448 | 153 | success = false; |
| ansond | 0:ae2a45502448 | 154 | } |
| ansond | 0:ae2a45502448 | 155 | return success; |
| ansond | 0:ae2a45502448 | 156 | } |
| ansond | 0:ae2a45502448 | 157 | |
| ansond | 0:ae2a45502448 | 158 | // load up our endpoint to the IOC |
| ansond | 0:ae2a45502448 | 159 | void MBEDEndpoint::loadEndpoint() { |
| ansond | 0:ae2a45502448 | 160 | } |
| ansond | 0:ae2a45502448 | 161 | |
| ansond | 0:ae2a45502448 | 162 | // update our endpoint with the IOC |
| ansond | 0:ae2a45502448 | 163 | void MBEDEndpoint::updateEndpoint() { |
| ansond | 0:ae2a45502448 | 164 | } |
| ansond | 0:ae2a45502448 | 165 | |
| ansond | 0:ae2a45502448 | 166 | |
| ansond | 0:ae2a45502448 | 167 | // close down the Lights |
| ansond | 0:ae2a45502448 | 168 | bool MBEDEndpoint::closeLights() { |
| ansond | 0:ae2a45502448 | 169 | bool success = true; |
| ansond | 0:ae2a45502448 | 170 | this->logger()->log("Closing down Lights..."); |
| ansond | 0:ae2a45502448 | 171 | return success; |
| ansond | 0:ae2a45502448 | 172 | } |
| ansond | 0:ae2a45502448 | 173 | |
| ansond | 0:ae2a45502448 | 174 | // close a given transport |
| ansond | 0:ae2a45502448 | 175 | bool MBEDEndpoint::closeTransport(int index,char *key) { |
| ansond | 0:ae2a45502448 | 176 | this->logger()->log("Closing down %s Transport...", key); |
| ansond | 0:ae2a45502448 | 177 | if (this->m_transports[index] != NULL) delete this->m_transports[index]; |
| ansond | 0:ae2a45502448 | 178 | return true; |
| ansond | 0:ae2a45502448 | 179 | } |
| ansond | 0:ae2a45502448 | 180 | |
| ansond | 0:ae2a45502448 | 181 | // close down our transports |
| ansond | 0:ae2a45502448 | 182 | bool MBEDEndpoint::closeTransports() { |
| ansond | 0:ae2a45502448 | 183 | bool success = true; |
| ansond | 0:ae2a45502448 | 184 | |
| ansond | 0:ae2a45502448 | 185 | if (success) { |
| ansond | 0:ae2a45502448 | 186 | // close MQTT |
| ansond | 0:ae2a45502448 | 187 | success = this->closeTransport(MQTT_TRANSPORT,"MQTT"); |
| ansond | 0:ae2a45502448 | 188 | } |
| ansond | 0:ae2a45502448 | 189 | |
| ansond | 0:ae2a45502448 | 190 | if (success) { |
| ansond | 0:ae2a45502448 | 191 | // close HTTP |
| ansond | 0:ae2a45502448 | 192 | success = this->closeTransport(HTTP_TRANSPORT,"HTTP"); |
| ansond | 0:ae2a45502448 | 193 | } |
| ansond | 0:ae2a45502448 | 194 | |
| ansond | 0:ae2a45502448 | 195 | return success; |
| ansond | 0:ae2a45502448 | 196 | } |
| ansond | 0:ae2a45502448 | 197 | |
| ansond | 0:ae2a45502448 | 198 | // close down our Ethernet |
| ansond | 0:ae2a45502448 | 199 | bool MBEDEndpoint::closeEthernet() { |
| ansond | 0:ae2a45502448 | 200 | this->logger()->log("Closing down Ethernet..."); |
| ansond | 0:ae2a45502448 | 201 | if (this->m_ethernet != NULL) this->m_ethernet->disconnect(); |
| ansond | 0:ae2a45502448 | 202 | return true; |
| ansond | 0:ae2a45502448 | 203 | } |
| ansond | 0:ae2a45502448 | 204 | |
| ansond | 0:ae2a45502448 | 205 | // get our error handler |
| ansond | 0:ae2a45502448 | 206 | ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; } |
| ansond | 0:ae2a45502448 | 207 | |
| ansond | 0:ae2a45502448 | 208 | // main running loop |
| ansond | 0:ae2a45502448 | 209 | void MBEDEndpoint::run() { |
| ansond | 0:ae2a45502448 | 210 | this->logger()->log("Endpoint Main Loop"); |
| ansond | 0:ae2a45502448 | 211 | while(true) { |
| ansond | 0:ae2a45502448 | 212 | // sleep a bit |
| ansond | 0:ae2a45502448 | 213 | //this->logger()->log("Sleeping for a bit..."); |
| ansond | 0:ae2a45502448 | 214 | wait_ms(MAIN_LOOP_SLEEP); |
| ansond | 0:ae2a45502448 | 215 | |
| ansond | 0:ae2a45502448 | 216 | // check for events |
| ansond | 0:ae2a45502448 | 217 | //this->logger()->log("Processing Events..."); |
| ansond | 0:ae2a45502448 | 218 | for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess(); |
| ansond | 0:ae2a45502448 | 219 | |
| ansond | 0:ae2a45502448 | 220 | // check for exit |
| ansond | 0:ae2a45502448 | 221 | //this->logger()->log("Checking for exit..."); |
| ansond | 0:ae2a45502448 | 222 | this->logger()->checkForExit(); |
| ansond | 0:ae2a45502448 | 223 | } |
| ansond | 0:ae2a45502448 | 224 | } |
| ansond | 0:ae2a45502448 | 225 |