Initial commit
Dependencies: Chainable_RGB_LED EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib
Fork of SensorNotifyLED by
main.cpp@0:950566b0e099, 2014-10-30 (annotated)
- Committer:
- michaeljkoster
- Date:
- Thu Oct 30 21:44:12 2014 +0000
- Revision:
- 0:950566b0e099
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:950566b0e099 | 1 | #include "mbed.h" |
michaeljkoster | 0:950566b0e099 | 2 | #include "EthernetInterface.h" |
michaeljkoster | 0:950566b0e099 | 3 | #include "nsdl_support.h" |
michaeljkoster | 0:950566b0e099 | 4 | #include "dbg.h" |
michaeljkoster | 0:950566b0e099 | 5 | #include "IAP.h" |
michaeljkoster | 0:950566b0e099 | 6 | |
michaeljkoster | 0:950566b0e099 | 7 | // Include resources |
michaeljkoster | 0:950566b0e099 | 8 | #include "LEDbar.h" |
michaeljkoster | 0:950566b0e099 | 9 | #include "IPSO_illuminance.h" |
michaeljkoster | 0:950566b0e099 | 10 | #include "gas_sensor.h" |
michaeljkoster | 0:950566b0e099 | 11 | #include "IPSO_presence.h" |
michaeljkoster | 0:950566b0e099 | 12 | #include "LED_array.h" |
michaeljkoster | 0:950566b0e099 | 13 | |
michaeljkoster | 0:950566b0e099 | 14 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:950566b0e099 | 15 | |
michaeljkoster | 0:950566b0e099 | 16 | // **************************************************************************** |
michaeljkoster | 0:950566b0e099 | 17 | // Configuration section |
michaeljkoster | 0:950566b0e099 | 18 | |
michaeljkoster | 0:950566b0e099 | 19 | // Ethernet configuration |
michaeljkoster | 0:950566b0e099 | 20 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:950566b0e099 | 21 | #define DHCP |
michaeljkoster | 0:950566b0e099 | 22 | |
michaeljkoster | 0:950566b0e099 | 23 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 0:950566b0e099 | 24 | #define IP "10.0.0.199" |
michaeljkoster | 0:950566b0e099 | 25 | #define MASK "255.255.255.0" |
michaeljkoster | 0:950566b0e099 | 26 | #define GW "10.0.0.1" |
michaeljkoster | 0:950566b0e099 | 27 | |
michaeljkoster | 0:950566b0e099 | 28 | extern "C" void mbed_mac_address(char *mac) |
michaeljkoster | 0:950566b0e099 | 29 | { |
michaeljkoster | 0:950566b0e099 | 30 | static char buf[64] = {0}; |
michaeljkoster | 0:950566b0e099 | 31 | IAP iap; |
michaeljkoster | 0:950566b0e099 | 32 | int32_t *block = iap.read_serial(); |
michaeljkoster | 0:950566b0e099 | 33 | uint32_t serial_number[5] = {0}; |
michaeljkoster | 0:950566b0e099 | 34 | |
michaeljkoster | 0:950566b0e099 | 35 | memset(buf, 0, sizeof(buf)); |
michaeljkoster | 0:950566b0e099 | 36 | serial_number[0] = *(block); |
michaeljkoster | 0:950566b0e099 | 37 | serial_number[1] = *(block+1); |
michaeljkoster | 0:950566b0e099 | 38 | // we only want bottom 16 bits of word1 (MAC bits 32-47) |
michaeljkoster | 0:950566b0e099 | 39 | // and bit 9 forced to 1, bit 8 forced to 0 |
michaeljkoster | 0:950566b0e099 | 40 | // Locally administered MAC, reduced conflicts |
michaeljkoster | 0:950566b0e099 | 41 | // http://en.wikipedia.org/wiki/MAC_address |
michaeljkoster | 0:950566b0e099 | 42 | //serial_number[0] |= 0x00000200; |
michaeljkoster | 0:950566b0e099 | 43 | //serial_number[0] &= 0x0000FEFF; |
michaeljkoster | 0:950566b0e099 | 44 | memcpy(mac, (uint8_t*) &serial_number[0], 6); |
michaeljkoster | 0:950566b0e099 | 45 | mac[0] |= 0x02; |
michaeljkoster | 0:950566b0e099 | 46 | mac[0] &= 0xFE; |
michaeljkoster | 0:950566b0e099 | 47 | mac[5] |= 0x02; |
michaeljkoster | 0:950566b0e099 | 48 | mac[5] &= 0xFE; |
michaeljkoster | 0:950566b0e099 | 49 | |
michaeljkoster | 0:950566b0e099 | 50 | // snprintf(buf, 16, "%4X%08X", serial_number[0], serial_number[1]); |
michaeljkoster | 0:950566b0e099 | 51 | } |
michaeljkoster | 0:950566b0e099 | 52 | |
michaeljkoster | 0:950566b0e099 | 53 | |
michaeljkoster | 0:950566b0e099 | 54 | // NSP configuration |
michaeljkoster | 0:950566b0e099 | 55 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:950566b0e099 | 56 | //static const char* NSP_ADDRESS = "23.102.162.118"; // coen296.cloudapp.net |
michaeljkoster | 0:950566b0e099 | 57 | static const char* NSP_ADDRESS = "191.239.5.150"; // barista2.cloudapp.net |
michaeljkoster | 0:950566b0e099 | 58 | static const int NSP_PORT = 5683; |
michaeljkoster | 0:950566b0e099 | 59 | char endpoint_name[24] = "mbedDEMO-"; |
michaeljkoster | 0:950566b0e099 | 60 | uint8_t ep_type[] = {"DEMO"}; |
michaeljkoster | 0:950566b0e099 | 61 | uint8_t lifetime_ptr[] = {"60"}; |
michaeljkoster | 0:950566b0e099 | 62 | |
michaeljkoster | 0:950566b0e099 | 63 | // **************************************************************************** |
michaeljkoster | 0:950566b0e099 | 64 | // Ethernet initialization |
michaeljkoster | 0:950566b0e099 | 65 | |
michaeljkoster | 0:950566b0e099 | 66 | EthernetInterface eth; |
michaeljkoster | 0:950566b0e099 | 67 | static void ethernet_init() |
michaeljkoster | 0:950566b0e099 | 68 | { |
michaeljkoster | 0:950566b0e099 | 69 | |
michaeljkoster | 0:950566b0e099 | 70 | /* Initialize network */ |
michaeljkoster | 0:950566b0e099 | 71 | #ifdef DHCP |
michaeljkoster | 0:950566b0e099 | 72 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:950566b0e099 | 73 | eth.init(); |
michaeljkoster | 0:950566b0e099 | 74 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:950566b0e099 | 75 | #else |
michaeljkoster | 0:950566b0e099 | 76 | eth.init(IP, MASK, GW); |
michaeljkoster | 0:950566b0e099 | 77 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:950566b0e099 | 78 | #endif |
michaeljkoster | 0:950566b0e099 | 79 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:950566b0e099 | 80 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:950566b0e099 | 81 | |
michaeljkoster | 0:950566b0e099 | 82 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:950566b0e099 | 83 | } |
michaeljkoster | 0:950566b0e099 | 84 | |
michaeljkoster | 0:950566b0e099 | 85 | // **************************************************************************** |
michaeljkoster | 0:950566b0e099 | 86 | // NSP initialization |
michaeljkoster | 0:950566b0e099 | 87 | |
michaeljkoster | 0:950566b0e099 | 88 | UDPSocket server; |
michaeljkoster | 0:950566b0e099 | 89 | Endpoint nsp; |
michaeljkoster | 0:950566b0e099 | 90 | |
michaeljkoster | 0:950566b0e099 | 91 | char macbytes[6] = {0}; |
michaeljkoster | 0:950566b0e099 | 92 | char MAC[20]; |
michaeljkoster | 0:950566b0e099 | 93 | |
michaeljkoster | 0:950566b0e099 | 94 | static void nsp_init() |
michaeljkoster | 0:950566b0e099 | 95 | { |
michaeljkoster | 0:950566b0e099 | 96 | server.init(); |
michaeljkoster | 0:950566b0e099 | 97 | server.bind(NSP_PORT); |
michaeljkoster | 0:950566b0e099 | 98 | |
michaeljkoster | 0:950566b0e099 | 99 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:950566b0e099 | 100 | |
michaeljkoster | 0:950566b0e099 | 101 | mbed_mac_address(macbytes); |
michaeljkoster | 0:950566b0e099 | 102 | |
michaeljkoster | 0:950566b0e099 | 103 | sprintf(MAC, "%02X%02X%02X%02X%02X%02X", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]); |
michaeljkoster | 0:950566b0e099 | 104 | pc.printf("MAC: %s\r\n", MAC); |
michaeljkoster | 0:950566b0e099 | 105 | |
michaeljkoster | 0:950566b0e099 | 106 | strncat(endpoint_name, MAC, 12); |
michaeljkoster | 0:950566b0e099 | 107 | |
michaeljkoster | 0:950566b0e099 | 108 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:950566b0e099 | 109 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:950566b0e099 | 110 | |
michaeljkoster | 0:950566b0e099 | 111 | } |
michaeljkoster | 0:950566b0e099 | 112 | |
michaeljkoster | 0:950566b0e099 | 113 | // **************************************************************************** |
michaeljkoster | 0:950566b0e099 | 114 | // Resource creation |
michaeljkoster | 0:950566b0e099 | 115 | |
michaeljkoster | 0:950566b0e099 | 116 | static int create_resources() |
michaeljkoster | 0:950566b0e099 | 117 | { |
michaeljkoster | 0:950566b0e099 | 118 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:950566b0e099 | 119 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:950566b0e099 | 120 | |
michaeljkoster | 0:950566b0e099 | 121 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:950566b0e099 | 122 | |
michaeljkoster | 0:950566b0e099 | 123 | /* Create resources */ |
michaeljkoster | 0:950566b0e099 | 124 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:950566b0e099 | 125 | if(!resource_ptr) |
michaeljkoster | 0:950566b0e099 | 126 | return 0; |
michaeljkoster | 0:950566b0e099 | 127 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:950566b0e099 | 128 | |
michaeljkoster | 0:950566b0e099 | 129 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:950566b0e099 | 130 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:950566b0e099 | 131 | { |
michaeljkoster | 0:950566b0e099 | 132 | nsdl_free(resource_ptr); |
michaeljkoster | 0:950566b0e099 | 133 | return 0; |
michaeljkoster | 0:950566b0e099 | 134 | } |
michaeljkoster | 0:950566b0e099 | 135 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:950566b0e099 | 136 | |
michaeljkoster | 0:950566b0e099 | 137 | // Static resources |
michaeljkoster | 0:950566b0e099 | 138 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1); |
michaeljkoster | 0:950566b0e099 | 139 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1); |
michaeljkoster | 0:950566b0e099 | 140 | |
michaeljkoster | 0:950566b0e099 | 141 | // Dynamic resources |
michaeljkoster | 0:950566b0e099 | 142 | create_LEDbar_resource(resource_ptr); |
michaeljkoster | 0:950566b0e099 | 143 | create_IPSO_illuminance_resource(resource_ptr); |
michaeljkoster | 0:950566b0e099 | 144 | create_gas_sensor_resource(resource_ptr); |
michaeljkoster | 0:950566b0e099 | 145 | create_IPSO_presence_resource(resource_ptr); |
michaeljkoster | 0:950566b0e099 | 146 | create_LED_array_resource(resource_ptr); |
michaeljkoster | 0:950566b0e099 | 147 | |
michaeljkoster | 0:950566b0e099 | 148 | /* Register with NSP */ |
michaeljkoster | 0:950566b0e099 | 149 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:950566b0e099 | 150 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:950566b0e099 | 151 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:950566b0e099 | 152 | else |
michaeljkoster | 0:950566b0e099 | 153 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:950566b0e099 | 154 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:950566b0e099 | 155 | |
michaeljkoster | 0:950566b0e099 | 156 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:950566b0e099 | 157 | nsdl_free(resource_ptr); |
michaeljkoster | 0:950566b0e099 | 158 | return 1; |
michaeljkoster | 0:950566b0e099 | 159 | } |
michaeljkoster | 0:950566b0e099 | 160 | |
michaeljkoster | 0:950566b0e099 | 161 | // **************************************************************************** |
michaeljkoster | 0:950566b0e099 | 162 | // Program entry point |
michaeljkoster | 0:950566b0e099 | 163 | |
michaeljkoster | 0:950566b0e099 | 164 | int main() |
michaeljkoster | 0:950566b0e099 | 165 | { |
michaeljkoster | 0:950566b0e099 | 166 | |
michaeljkoster | 0:950566b0e099 | 167 | NSDL_DEBUG("Hello mDS Demo Endpoint Application\n"); |
michaeljkoster | 0:950566b0e099 | 168 | |
michaeljkoster | 0:950566b0e099 | 169 | // Initialize Ethernet interface first |
michaeljkoster | 0:950566b0e099 | 170 | ethernet_init(); |
michaeljkoster | 0:950566b0e099 | 171 | |
michaeljkoster | 0:950566b0e099 | 172 | // Initialize NSP node |
michaeljkoster | 0:950566b0e099 | 173 | nsp_init(); |
michaeljkoster | 0:950566b0e099 | 174 | |
michaeljkoster | 0:950566b0e099 | 175 | // Initialize NSDL stack |
michaeljkoster | 0:950566b0e099 | 176 | nsdl_init(); |
michaeljkoster | 0:950566b0e099 | 177 | |
michaeljkoster | 0:950566b0e099 | 178 | // Create NSDL resources |
michaeljkoster | 0:950566b0e099 | 179 | create_resources(); |
michaeljkoster | 0:950566b0e099 | 180 | |
michaeljkoster | 0:950566b0e099 | 181 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:950566b0e099 | 182 | nsdl_event_loop(); |
michaeljkoster | 0:950566b0e099 | 183 | } |