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: C027_Support C12832 StatusReporter LM75B c027_radios endpoint_core endpoint_nsp nsp_resources mbed mbed-rtos
MBEDEndpoint.cpp@0:dcced975fb52, 2014-02-25 (annotated)
- Committer:
- ansond
- Date:
- Tue Feb 25 16:56:16 2014 +0000
- Revision:
- 0:dcced975fb52
- Child:
- 1:9af5049aa866
initial checkin
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ansond | 0:dcced975fb52 | 1 | /* Copyright C2013 Doug Anson, MIT License |
| ansond | 0:dcced975fb52 | 2 | * |
| ansond | 0:dcced975fb52 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| ansond | 0:dcced975fb52 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
| ansond | 0:dcced975fb52 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| ansond | 0:dcced975fb52 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| ansond | 0:dcced975fb52 | 7 | * furnished to do so, subject to the following conditions: |
| ansond | 0:dcced975fb52 | 8 | * |
| ansond | 0:dcced975fb52 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| ansond | 0:dcced975fb52 | 10 | * substantial portions of the Software. |
| ansond | 0:dcced975fb52 | 11 | * |
| ansond | 0:dcced975fb52 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| ansond | 0:dcced975fb52 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| ansond | 0:dcced975fb52 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| ansond | 0:dcced975fb52 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| ansond | 0:dcced975fb52 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| ansond | 0:dcced975fb52 | 17 | */ |
| ansond | 0:dcced975fb52 | 18 | |
| ansond | 0:dcced975fb52 | 19 | #include "NSPTransport.h" |
| ansond | 0:dcced975fb52 | 20 | #include "MBEDEndpoint.h" |
| ansond | 0:dcced975fb52 | 21 | |
| ansond | 0:dcced975fb52 | 22 | // NSP Actions we can act on |
| ansond | 0:dcced975fb52 | 23 | #include "NSPLightDimmerAction.h" |
| ansond | 0:dcced975fb52 | 24 | #include "NSPLightSwitchAction.h" |
| ansond | 0:dcced975fb52 | 25 | |
| ansond | 0:dcced975fb52 | 26 | // default constructor |
| ansond | 0:dcced975fb52 | 27 | MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) { |
| ansond | 0:dcced975fb52 | 28 | bool success = true; |
| ansond | 0:dcced975fb52 | 29 | for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL; |
| ansond | 0:dcced975fb52 | 30 | this->m_error_handler = error_handler; |
| ansond | 0:dcced975fb52 | 31 | this->logger()->log(ENDPOINT_VERSION_ANNOUNCE); |
| ansond | 0:dcced975fb52 | 32 | if (success) this->logger()->turnLEDBlue(); |
| ansond | 0:dcced975fb52 | 33 | if (success) success = this->initializeEthernet(ethernet); |
| ansond | 0:dcced975fb52 | 34 | if (success) this->logger()->turnLEDYellow(); |
| ansond | 0:dcced975fb52 | 35 | if (success) success = this->initializeTransports(); |
| ansond | 0:dcced975fb52 | 36 | if (success) success = this->initializeLights(); |
| ansond | 0:dcced975fb52 | 37 | if (success) this->logger()->turnLEDGreen(); |
| ansond | 0:dcced975fb52 | 38 | if (!success) this->logger()->turnLEDRed(); |
| ansond | 0:dcced975fb52 | 39 | } |
| ansond | 0:dcced975fb52 | 40 | |
| ansond | 0:dcced975fb52 | 41 | // default destructor |
| ansond | 0:dcced975fb52 | 42 | MBEDEndpoint::~MBEDEndpoint() { |
| ansond | 0:dcced975fb52 | 43 | bool success = true; |
| ansond | 0:dcced975fb52 | 44 | if (success) this->logger()->turnLEDYellow(); |
| ansond | 0:dcced975fb52 | 45 | if (success) success = this->closeLights(); |
| ansond | 0:dcced975fb52 | 46 | if (success) success = this->closeTransports(); |
| ansond | 0:dcced975fb52 | 47 | if (success) success = this->closeEthernet(); |
| ansond | 0:dcced975fb52 | 48 | if (success) this->logger()->turnLEDBlue(); |
| ansond | 0:dcced975fb52 | 49 | } |
| ansond | 0:dcced975fb52 | 50 | |
| ansond | 0:dcced975fb52 | 51 | // initialize the Lights |
| ansond | 0:dcced975fb52 | 52 | bool MBEDEndpoint::initializeLights() { |
| ansond | 0:dcced975fb52 | 53 | this->logger()->log("Initializing Lights..."); |
| ansond | 0:dcced975fb52 | 54 | for(int i=0;i<NUM_LIGHTS;++i) { |
| ansond | 0:dcced975fb52 | 55 | this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this); |
| ansond | 0:dcced975fb52 | 56 | this->m_lights[i]->setDimmerAction(new NSPLightDimmerAction(this->logger(),this->m_lights[i])); |
| ansond | 0:dcced975fb52 | 57 | this->m_lights[i]->setSwitchAction(new NSPLightSwitchAction(this->logger(),this->m_lights[i])); |
| ansond | 0:dcced975fb52 | 58 | } |
| ansond | 0:dcced975fb52 | 59 | return true; |
| ansond | 0:dcced975fb52 | 60 | } |
| ansond | 0:dcced975fb52 | 61 | |
| ansond | 0:dcced975fb52 | 62 | // initialize our ResourceFactory |
| ansond | 0:dcced975fb52 | 63 | ResourceFactory *MBEDEndpoint::initResourceFactory() { return new NSPResourceFactory(this->logger()); } |
| ansond | 0:dcced975fb52 | 64 | |
| ansond | 0:dcced975fb52 | 65 | // initialize a specific transport |
| ansond | 0:dcced975fb52 | 66 | bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) { |
| ansond | 0:dcced975fb52 | 67 | bool success = false; |
| ansond | 0:dcced975fb52 | 68 | if (this->m_transports[index] == NULL) { |
| ansond | 0:dcced975fb52 | 69 | this->logger()->log("Initializing %s Transport...", key); |
| ansond | 0:dcced975fb52 | 70 | this->m_transports[index] = transport; |
| ansond | 0:dcced975fb52 | 71 | if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect(); |
| ansond | 0:dcced975fb52 | 72 | } |
| ansond | 0:dcced975fb52 | 73 | else { |
| ansond | 0:dcced975fb52 | 74 | this->logger()->log("%s already connected (OK)...", key); |
| ansond | 0:dcced975fb52 | 75 | success = true; |
| ansond | 0:dcced975fb52 | 76 | } |
| ansond | 0:dcced975fb52 | 77 | return success; |
| ansond | 0:dcced975fb52 | 78 | } |
| ansond | 0:dcced975fb52 | 79 | |
| ansond | 0:dcced975fb52 | 80 | // initialize our transports |
| ansond | 0:dcced975fb52 | 81 | bool MBEDEndpoint::initializeTransports() { |
| ansond | 0:dcced975fb52 | 82 | bool success = true; |
| ansond | 0:dcced975fb52 | 83 | |
| ansond | 0:dcced975fb52 | 84 | if (success == true) { |
| ansond | 0:dcced975fb52 | 85 | // NSP Initialization |
| ansond | 0:dcced975fb52 | 86 | success = this->initializeTransport(NSP_TRANSPORT,"NSP",new NSPTransport(this->m_error_handler)); |
| ansond | 0:dcced975fb52 | 87 | } |
| ansond | 0:dcced975fb52 | 88 | |
| ansond | 0:dcced975fb52 | 89 | return success; |
| ansond | 0:dcced975fb52 | 90 | } |
| ansond | 0:dcced975fb52 | 91 | |
| ansond | 0:dcced975fb52 | 92 | // initialize our Ethernet |
| ansond | 0:dcced975fb52 | 93 | bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) { |
| ansond | 0:dcced975fb52 | 94 | bool success = false; |
| ansond | 0:dcced975fb52 | 95 | this->m_ethernet = ethernet; |
| ansond | 0:dcced975fb52 | 96 | if (this->m_ethernet != NULL) { |
| ansond | 0:dcced975fb52 | 97 | this->logger()->log("Initializing Ethernet..."); |
| ansond | 0:dcced975fb52 | 98 | |
| ansond | 0:dcced975fb52 | 99 | // connect up ethernet |
| ansond | 0:dcced975fb52 | 100 | this->m_ethernet->init(); |
| ansond | 0:dcced975fb52 | 101 | this->m_ethernet->connect(); |
| ansond | 0:dcced975fb52 | 102 | |
| ansond | 0:dcced975fb52 | 103 | // display our IP address |
| ansond | 0:dcced975fb52 | 104 | char *ipaddr = this->m_ethernet->getIPAddress(); |
| ansond | 0:dcced975fb52 | 105 | if (ipaddr != NULL && strlen(ipaddr) > 0) { |
| ansond | 0:dcced975fb52 | 106 | this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress()); |
| ansond | 0:dcced975fb52 | 107 | success = true; |
| ansond | 0:dcced975fb52 | 108 | } |
| ansond | 0:dcced975fb52 | 109 | else { |
| ansond | 0:dcced975fb52 | 110 | this->logger()->log("Ethernet Not Connected..."); |
| ansond | 0:dcced975fb52 | 111 | success = false; |
| ansond | 0:dcced975fb52 | 112 | } |
| ansond | 0:dcced975fb52 | 113 | } |
| ansond | 0:dcced975fb52 | 114 | else { |
| ansond | 0:dcced975fb52 | 115 | this->logger()->log("No Ethernet instance found"); |
| ansond | 0:dcced975fb52 | 116 | success = false; |
| ansond | 0:dcced975fb52 | 117 | } |
| ansond | 0:dcced975fb52 | 118 | return success; |
| ansond | 0:dcced975fb52 | 119 | } |
| ansond | 0:dcced975fb52 | 120 | |
| ansond | 0:dcced975fb52 | 121 | // close down the Lights |
| ansond | 0:dcced975fb52 | 122 | bool MBEDEndpoint::closeLights() { |
| ansond | 0:dcced975fb52 | 123 | bool success = true; |
| ansond | 0:dcced975fb52 | 124 | this->logger()->log("Closing down Lights..."); |
| ansond | 0:dcced975fb52 | 125 | return success; |
| ansond | 0:dcced975fb52 | 126 | } |
| ansond | 0:dcced975fb52 | 127 | |
| ansond | 0:dcced975fb52 | 128 | // close a given transport |
| ansond | 0:dcced975fb52 | 129 | bool MBEDEndpoint::closeTransport(int index,char *key) { |
| ansond | 0:dcced975fb52 | 130 | this->logger()->log("Closing down %s Transport...", key); |
| ansond | 0:dcced975fb52 | 131 | if (this->m_transports[index] != NULL) delete this->m_transports[index]; |
| ansond | 0:dcced975fb52 | 132 | return true; |
| ansond | 0:dcced975fb52 | 133 | } |
| ansond | 0:dcced975fb52 | 134 | |
| ansond | 0:dcced975fb52 | 135 | // close down our transports |
| ansond | 0:dcced975fb52 | 136 | bool MBEDEndpoint::closeTransports() { |
| ansond | 0:dcced975fb52 | 137 | bool success = true; |
| ansond | 0:dcced975fb52 | 138 | |
| ansond | 0:dcced975fb52 | 139 | if (success) { |
| ansond | 0:dcced975fb52 | 140 | // close NSP |
| ansond | 0:dcced975fb52 | 141 | success = this->closeTransport(NSP_TRANSPORT,"NSP"); |
| ansond | 0:dcced975fb52 | 142 | } |
| ansond | 0:dcced975fb52 | 143 | |
| ansond | 0:dcced975fb52 | 144 | return success; |
| ansond | 0:dcced975fb52 | 145 | } |
| ansond | 0:dcced975fb52 | 146 | |
| ansond | 0:dcced975fb52 | 147 | // close down our Ethernet |
| ansond | 0:dcced975fb52 | 148 | bool MBEDEndpoint::closeEthernet() { |
| ansond | 0:dcced975fb52 | 149 | this->logger()->log("Closing down Ethernet..."); |
| ansond | 0:dcced975fb52 | 150 | if (this->m_ethernet != NULL) this->m_ethernet->disconnect(); |
| ansond | 0:dcced975fb52 | 151 | return true; |
| ansond | 0:dcced975fb52 | 152 | } |
| ansond | 0:dcced975fb52 | 153 | |
| ansond | 0:dcced975fb52 | 154 | // get our error handler |
| ansond | 0:dcced975fb52 | 155 | ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; } |
| ansond | 0:dcced975fb52 | 156 | |
| ansond | 0:dcced975fb52 | 157 | // main running loop |
| ansond | 0:dcced975fb52 | 158 | void MBEDEndpoint::run() { |
| ansond | 0:dcced975fb52 | 159 | this->logger()->log("Endpoint Main Loop"); |
| ansond | 0:dcced975fb52 | 160 | while(true) { |
| ansond | 0:dcced975fb52 | 161 | // sleep a bit |
| ansond | 0:dcced975fb52 | 162 | //this->logger()->log("Sleeping for a bit..."); |
| ansond | 0:dcced975fb52 | 163 | wait_ms(MAIN_LOOP_SLEEP); |
| ansond | 0:dcced975fb52 | 164 | |
| ansond | 0:dcced975fb52 | 165 | // check for events |
| ansond | 0:dcced975fb52 | 166 | //this->logger()->log("Processing Events..."); |
| ansond | 0:dcced975fb52 | 167 | for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess(); |
| ansond | 0:dcced975fb52 | 168 | |
| ansond | 0:dcced975fb52 | 169 | // check for exit |
| ansond | 0:dcced975fb52 | 170 | //this->logger()->log("Checking for exit..."); |
| ansond | 0:dcced975fb52 | 171 | this->logger()->checkForExit(); |
| ansond | 0:dcced975fb52 | 172 | } |
| ansond | 0:dcced975fb52 | 173 | } |
| ansond | 0:dcced975fb52 | 174 |