Thread UART
Dependencies: fsl_phy_mcr20a fsl_smac mbed-rtos mbed
Fork of mcr20_wireless_uart by
main.cpp@15:990a8b5664e1, 2015-03-15 (annotated)
- Committer:
- FSL\B36402
- Date:
- Sun Mar 15 00:56:28 2015 -0500
- Revision:
- 15:990a8b5664e1
- Parent:
- 3:a38ad504a18c
- Child:
- 17:52cfd7db8da3
Integrated PHY version from the official K64F+MCR20A package
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:01fb291427ce | 1 | #include "mbed.h" |
sam_grove | 0:01fb291427ce | 2 | |
sam_grove | 2:3e7685cfb2a7 | 3 | #include "driverDebug.h" |
sam_grove | 2:3e7685cfb2a7 | 4 | #include "socket_api.h" |
sam_grove | 2:3e7685cfb2a7 | 5 | #include "net_interface.h" |
sam_grove | 2:3e7685cfb2a7 | 6 | #include "system_event.h" |
sam_grove | 2:3e7685cfb2a7 | 7 | #include "string.h" |
sam_grove | 2:3e7685cfb2a7 | 8 | #include "net_nwk_scan.h" |
sam_grove | 2:3e7685cfb2a7 | 9 | #include "socket_security.h" |
sam_grove | 2:3e7685cfb2a7 | 10 | #include "nwk_stats_api.h" |
sam_grove | 2:3e7685cfb2a7 | 11 | #include "multicast_api.h" |
sam_grove | 2:3e7685cfb2a7 | 12 | #include "nsdynmemLIB.h" |
sam_grove | 2:3e7685cfb2a7 | 13 | #include "randLIB.h" |
sam_grove | 2:3e7685cfb2a7 | 14 | #include "arm_hal_timer.h" |
sam_grove | 2:3e7685cfb2a7 | 15 | #include "common_functions.h" |
sam_grove | 2:3e7685cfb2a7 | 16 | #include "driverRFPhy.h" |
sam_grove | 2:3e7685cfb2a7 | 17 | #include "driverAtmelRFInterface.h" |
sam_grove | 2:3e7685cfb2a7 | 18 | #include "nsdl_support.h" |
sam_grove | 2:3e7685cfb2a7 | 19 | #include "node_cfg.h" |
sam_grove | 2:3e7685cfb2a7 | 20 | |
sam_grove | 2:3e7685cfb2a7 | 21 | /*** DEBUG ******/ |
sam_grove | 2:3e7685cfb2a7 | 22 | #define DEBUG 1 |
sam_grove | 2:3e7685cfb2a7 | 23 | #if DEBUG |
sam_grove | 2:3e7685cfb2a7 | 24 | #define debug printf |
sam_grove | 2:3e7685cfb2a7 | 25 | #else |
sam_grove | 2:3e7685cfb2a7 | 26 | #define debug(...) "" |
sam_grove | 2:3e7685cfb2a7 | 27 | #endif // DEBUG |
sam_grove | 2:3e7685cfb2a7 | 28 | |
sam_grove | 2:3e7685cfb2a7 | 29 | /******* Hardware Definition **********/ |
sam_grove | 2:3e7685cfb2a7 | 30 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 31 | DigitalOut ledR(PTB22); // RGB - Red LED |
sam_grove | 2:3e7685cfb2a7 | 32 | DigitalOut ledG(PTE26); // RGB - Green LED |
sam_grove | 2:3e7685cfb2a7 | 33 | DigitalOut ledB(PTB21); // RGB - Blue LED |
sam_grove | 2:3e7685cfb2a7 | 34 | InterruptIn TAP_INT(PTC6); // INT1 from FXOS8700Q |
sam_grove | 2:3e7685cfb2a7 | 35 | #endif |
sam_grove | 2:3e7685cfb2a7 | 36 | |
sam_grove | 2:3e7685cfb2a7 | 37 | /******* Timers Definitions ************/ |
sam_grove | 2:3e7685cfb2a7 | 38 | #define RETRY_TIMER 1 |
sam_grove | 2:3e7685cfb2a7 | 39 | #define NSP_REGISTRATION_TIMER 2 |
sam_grove | 2:3e7685cfb2a7 | 40 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 41 | #define ACCELEROMETER_TIMER 3 |
sam_grove | 2:3e7685cfb2a7 | 42 | #endif |
sam_grove | 2:3e7685cfb2a7 | 43 | #if NODE_HOME |
sam_grove | 2:3e7685cfb2a7 | 44 | #define DOOR_CHECK_TIMER 4 |
sam_grove | 2:3e7685cfb2a7 | 45 | #endif |
sam_grove | 2:3e7685cfb2a7 | 46 | |
sam_grove | 2:3e7685cfb2a7 | 47 | void app_heap_error_handler(heap_fail_t event); |
sam_grove | 2:3e7685cfb2a7 | 48 | void tasklet_main(arm_event_s *event); |
sam_grove | 2:3e7685cfb2a7 | 49 | void button_main(arm_event_s *event); |
sam_grove | 2:3e7685cfb2a7 | 50 | |
sam_grove | 2:3e7685cfb2a7 | 51 | #define APP_DEV_HEAP_SIZE 6000 |
sam_grove | 2:3e7685cfb2a7 | 52 | static uint8_t app_defined_stack_heap[APP_DEV_HEAP_SIZE]; |
sam_grove | 2:3e7685cfb2a7 | 53 | static int8_t main_tasklet_id = -1; |
sam_grove | 2:3e7685cfb2a7 | 54 | |
sam_grove | 2:3e7685cfb2a7 | 55 | static const uint8_t app_MAC_address[8] = NODE_MAC_ADDRESS; |
sam_grove | 2:3e7685cfb2a7 | 56 | static int8_t rf_phy_device_register_id = -1; |
sam_grove | 2:3e7685cfb2a7 | 57 | static int8_t net_6lowpan_id = -1; |
sam_grove | 2:3e7685cfb2a7 | 58 | static uint32_t channel_list = 0x07FFF800; // All channels |
sam_grove | 2:3e7685cfb2a7 | 59 | |
sam_grove | 2:3e7685cfb2a7 | 60 | int8_t coap_udp_socket = -1; |
sam_grove | 2:3e7685cfb2a7 | 61 | |
sam_grove | 3:a38ad504a18c | 62 | #define AUDP_SOCKET_PORT 61630 |
sam_grove | 2:3e7685cfb2a7 | 63 | #define RECV_CTRL_UDP_PORT 1050 // receive |
sam_grove | 2:3e7685cfb2a7 | 64 | #define SEND_CTRL_UDP_PORT 1060 // send |
sam_grove | 2:3e7685cfb2a7 | 65 | |
sam_grove | 2:3e7685cfb2a7 | 66 | /** Used for Receive Data source Address store*/ |
sam_grove | 3:a38ad504a18c | 67 | static ns_address_t app_src; |
sam_grove | 2:3e7685cfb2a7 | 68 | static sn_nsdl_addr_s sn_addr_s; |
sam_grove | 2:3e7685cfb2a7 | 69 | |
sam_grove | 2:3e7685cfb2a7 | 70 | static int access_point_status = 0; |
sam_grove | 2:3e7685cfb2a7 | 71 | |
sam_grove | 2:3e7685cfb2a7 | 72 | link_layer_setups_s app_link_info; |
sam_grove | 2:3e7685cfb2a7 | 73 | link_layer_address_s app_link_address_info; |
sam_grove | 2:3e7685cfb2a7 | 74 | network_layer_address_s app_nd_address_info; |
sam_grove | 2:3e7685cfb2a7 | 75 | |
sam_grove | 2:3e7685cfb2a7 | 76 | /* Prototype functions */ |
sam_grove | 2:3e7685cfb2a7 | 77 | void NSDL_receive_socket(void *cb); |
sam_grove | 2:3e7685cfb2a7 | 78 | void NSP_registration(void); |
sam_grove | 2:3e7685cfb2a7 | 79 | |
sam_grove | 2:3e7685cfb2a7 | 80 | extern void create_lqi_resource(uint8_t); |
sam_grove | 2:3e7685cfb2a7 | 81 | void* own_alloc(uint16_t size); |
sam_grove | 2:3e7685cfb2a7 | 82 | void own_free(void *ptr); |
sam_grove | 2:3e7685cfb2a7 | 83 | |
sam_grove | 2:3e7685cfb2a7 | 84 | /*************** Application variables ***********************/ |
sam_grove | 2:3e7685cfb2a7 | 85 | uint8_t batteryLevel = 100; |
sam_grove | 2:3e7685cfb2a7 | 86 | enum LED {NONE, RED, GREEN, BLUE}; |
sam_grove | 2:3e7685cfb2a7 | 87 | int ledState = NONE; |
sam_grove | 2:3e7685cfb2a7 | 88 | |
sam_grove | 2:3e7685cfb2a7 | 89 | |
sam_grove | 2:3e7685cfb2a7 | 90 | /* #defines, definitions and declarations according node profile */ |
sam_grove | 2:3e7685cfb2a7 | 91 | |
sam_grove | 2:3e7685cfb2a7 | 92 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 93 | int8_t ctrl_udp_socket = -1; |
sam_grove | 2:3e7685cfb2a7 | 94 | void empty_udp_receive(void * cb); |
sam_grove | 2:3e7685cfb2a7 | 95 | void ctrl_udp_send(uint8_t cmd); |
sam_grove | 2:3e7685cfb2a7 | 96 | |
sam_grove | 2:3e7685cfb2a7 | 97 | #include "FXOS8700Q_TD.h" |
sam_grove | 2:3e7685cfb2a7 | 98 | |
sam_grove | 2:3e7685cfb2a7 | 99 | #define ACCEL_SAMPLE_TIME 200 |
sam_grove | 2:3e7685cfb2a7 | 100 | FXOS8700Q accel( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
sam_grove | 2:3e7685cfb2a7 | 101 | bool accel_enable = 1; |
sam_grove | 2:3e7685cfb2a7 | 102 | |
sam_grove | 2:3e7685cfb2a7 | 103 | void tap_detector_handler(void) { |
sam_grove | 2:3e7685cfb2a7 | 104 | printf("Tap Detected!\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 105 | |
sam_grove | 2:3e7685cfb2a7 | 106 | switch(ledState) { |
sam_grove | 2:3e7685cfb2a7 | 107 | case NONE: |
sam_grove | 2:3e7685cfb2a7 | 108 | break; |
sam_grove | 2:3e7685cfb2a7 | 109 | case RED: |
sam_grove | 2:3e7685cfb2a7 | 110 | ctrl_udp_send('R'); |
sam_grove | 2:3e7685cfb2a7 | 111 | break; |
sam_grove | 2:3e7685cfb2a7 | 112 | case GREEN: |
sam_grove | 2:3e7685cfb2a7 | 113 | ctrl_udp_send('G'); |
sam_grove | 2:3e7685cfb2a7 | 114 | break; |
sam_grove | 2:3e7685cfb2a7 | 115 | case BLUE: |
sam_grove | 2:3e7685cfb2a7 | 116 | ctrl_udp_send('B'); |
sam_grove | 2:3e7685cfb2a7 | 117 | break; |
sam_grove | 2:3e7685cfb2a7 | 118 | } |
sam_grove | 2:3e7685cfb2a7 | 119 | } |
sam_grove | 2:3e7685cfb2a7 | 120 | |
sam_grove | 2:3e7685cfb2a7 | 121 | void update_LED(void) { |
sam_grove | 2:3e7685cfb2a7 | 122 | |
sam_grove | 2:3e7685cfb2a7 | 123 | switch(ledState) { |
sam_grove | 2:3e7685cfb2a7 | 124 | case NONE: |
sam_grove | 2:3e7685cfb2a7 | 125 | ledState = GREEN; |
sam_grove | 2:3e7685cfb2a7 | 126 | ledR = 1; |
sam_grove | 2:3e7685cfb2a7 | 127 | ledG = 0; |
sam_grove | 2:3e7685cfb2a7 | 128 | ledB = 1; |
sam_grove | 2:3e7685cfb2a7 | 129 | break; |
sam_grove | 2:3e7685cfb2a7 | 130 | |
sam_grove | 2:3e7685cfb2a7 | 131 | case GREEN: |
sam_grove | 2:3e7685cfb2a7 | 132 | ledState = RED; |
sam_grove | 2:3e7685cfb2a7 | 133 | ledR = 0; |
sam_grove | 2:3e7685cfb2a7 | 134 | ledG = 1; |
sam_grove | 2:3e7685cfb2a7 | 135 | ledB = 1; |
sam_grove | 2:3e7685cfb2a7 | 136 | break; |
sam_grove | 2:3e7685cfb2a7 | 137 | |
sam_grove | 2:3e7685cfb2a7 | 138 | case RED: |
sam_grove | 2:3e7685cfb2a7 | 139 | ledState = BLUE; |
sam_grove | 2:3e7685cfb2a7 | 140 | ledR = 1; |
sam_grove | 2:3e7685cfb2a7 | 141 | ledG = 1; |
sam_grove | 2:3e7685cfb2a7 | 142 | ledB = 0; |
sam_grove | 2:3e7685cfb2a7 | 143 | break; |
sam_grove | 2:3e7685cfb2a7 | 144 | |
sam_grove | 2:3e7685cfb2a7 | 145 | case BLUE: |
sam_grove | 2:3e7685cfb2a7 | 146 | ledState = NONE; |
sam_grove | 2:3e7685cfb2a7 | 147 | ledR = 1; |
sam_grove | 2:3e7685cfb2a7 | 148 | ledG = 1; |
sam_grove | 2:3e7685cfb2a7 | 149 | ledB = 1; |
sam_grove | 2:3e7685cfb2a7 | 150 | break; |
sam_grove | 2:3e7685cfb2a7 | 151 | } |
sam_grove | 2:3e7685cfb2a7 | 152 | } |
sam_grove | 2:3e7685cfb2a7 | 153 | |
sam_grove | 2:3e7685cfb2a7 | 154 | #endif |
sam_grove | 0:01fb291427ce | 155 | |
sam_grove | 2:3e7685cfb2a7 | 156 | #if 1 |
sam_grove | 2:3e7685cfb2a7 | 157 | void printf_array(uint8_t *ptr , uint16_t len) |
sam_grove | 2:3e7685cfb2a7 | 158 | { |
sam_grove | 2:3e7685cfb2a7 | 159 | uint16_t i; |
sam_grove | 2:3e7685cfb2a7 | 160 | for(i=0; i<len; i++) |
sam_grove | 2:3e7685cfb2a7 | 161 | { |
sam_grove | 2:3e7685cfb2a7 | 162 | if(i) |
sam_grove | 2:3e7685cfb2a7 | 163 | { |
sam_grove | 2:3e7685cfb2a7 | 164 | if(i%16== 0) |
sam_grove | 2:3e7685cfb2a7 | 165 | { |
sam_grove | 2:3e7685cfb2a7 | 166 | debug("\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 167 | if(len > 64) |
sam_grove | 2:3e7685cfb2a7 | 168 | { |
sam_grove | 2:3e7685cfb2a7 | 169 | uint8_t x =254; |
sam_grove | 2:3e7685cfb2a7 | 170 | while(x--); |
sam_grove | 2:3e7685cfb2a7 | 171 | } |
sam_grove | 2:3e7685cfb2a7 | 172 | } |
sam_grove | 2:3e7685cfb2a7 | 173 | else |
sam_grove | 2:3e7685cfb2a7 | 174 | { |
sam_grove | 2:3e7685cfb2a7 | 175 | debug(":"); |
sam_grove | 2:3e7685cfb2a7 | 176 | } |
sam_grove | 2:3e7685cfb2a7 | 177 | } |
sam_grove | 2:3e7685cfb2a7 | 178 | debug_hex(*ptr++); |
sam_grove | 2:3e7685cfb2a7 | 179 | } |
sam_grove | 2:3e7685cfb2a7 | 180 | debug("\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 181 | } |
sam_grove | 2:3e7685cfb2a7 | 182 | |
sam_grove | 2:3e7685cfb2a7 | 183 | |
sam_grove | 2:3e7685cfb2a7 | 184 | |
sam_grove | 2:3e7685cfb2a7 | 185 | void debug_integer(uint8_t width, uint8_t base, int16_t n) |
sam_grove | 2:3e7685cfb2a7 | 186 | { |
sam_grove | 2:3e7685cfb2a7 | 187 | uint8_t bfr[8]; |
sam_grove | 2:3e7685cfb2a7 | 188 | uint8_t *ptr = bfr; |
sam_grove | 2:3e7685cfb2a7 | 189 | uint8_t ctr = 0; |
sam_grove | 2:3e7685cfb2a7 | 190 | |
sam_grove | 2:3e7685cfb2a7 | 191 | if (width > 7) width = 7; |
sam_grove | 2:3e7685cfb2a7 | 192 | |
sam_grove | 2:3e7685cfb2a7 | 193 | ptr += width; |
sam_grove | 2:3e7685cfb2a7 | 194 | *ptr-- = 0; |
sam_grove | 2:3e7685cfb2a7 | 195 | |
sam_grove | 2:3e7685cfb2a7 | 196 | if (base == 16) |
sam_grove | 2:3e7685cfb2a7 | 197 | { |
sam_grove | 2:3e7685cfb2a7 | 198 | do |
sam_grove | 2:3e7685cfb2a7 | 199 | { |
sam_grove | 2:3e7685cfb2a7 | 200 | *ptr = n & 0x0F; |
sam_grove | 2:3e7685cfb2a7 | 201 | if (*ptr < 10) *ptr += '0'; |
sam_grove | 2:3e7685cfb2a7 | 202 | else *ptr += ('A'-10); |
sam_grove | 2:3e7685cfb2a7 | 203 | ptr--; |
sam_grove | 2:3e7685cfb2a7 | 204 | n >>= 4; |
sam_grove | 2:3e7685cfb2a7 | 205 | ctr++; |
sam_grove | 2:3e7685cfb2a7 | 206 | }while((ctr & 1) || (ctr < width)); |
sam_grove | 2:3e7685cfb2a7 | 207 | } |
sam_grove | 2:3e7685cfb2a7 | 208 | else |
sam_grove | 2:3e7685cfb2a7 | 209 | { |
sam_grove | 2:3e7685cfb2a7 | 210 | uint8_t negative = 0; |
sam_grove | 2:3e7685cfb2a7 | 211 | if (n < 0) |
sam_grove | 2:3e7685cfb2a7 | 212 | { negative = 1; |
sam_grove | 2:3e7685cfb2a7 | 213 | n = -n; |
sam_grove | 2:3e7685cfb2a7 | 214 | } |
sam_grove | 2:3e7685cfb2a7 | 215 | ctr++; |
sam_grove | 2:3e7685cfb2a7 | 216 | do |
sam_grove | 2:3e7685cfb2a7 | 217 | { |
sam_grove | 2:3e7685cfb2a7 | 218 | *ptr-- = (n % 10) + '0'; |
sam_grove | 2:3e7685cfb2a7 | 219 | n /= 10; |
sam_grove | 2:3e7685cfb2a7 | 220 | ctr++; |
sam_grove | 2:3e7685cfb2a7 | 221 | }while ((ctr < width) && n); |
sam_grove | 2:3e7685cfb2a7 | 222 | if (negative) |
sam_grove | 2:3e7685cfb2a7 | 223 | { |
sam_grove | 2:3e7685cfb2a7 | 224 | *ptr-- = '-'; |
sam_grove | 2:3e7685cfb2a7 | 225 | } |
sam_grove | 2:3e7685cfb2a7 | 226 | else |
sam_grove | 2:3e7685cfb2a7 | 227 | { |
sam_grove | 2:3e7685cfb2a7 | 228 | *ptr-- = ' '; |
sam_grove | 2:3e7685cfb2a7 | 229 | } |
sam_grove | 2:3e7685cfb2a7 | 230 | } |
sam_grove | 2:3e7685cfb2a7 | 231 | ptr++; |
sam_grove | 2:3e7685cfb2a7 | 232 | //debug_send(ptr); |
sam_grove | 2:3e7685cfb2a7 | 233 | debug((const char *)ptr); |
sam_grove | 2:3e7685cfb2a7 | 234 | } |
sam_grove | 2:3e7685cfb2a7 | 235 | |
sam_grove | 2:3e7685cfb2a7 | 236 | |
sam_grove | 2:3e7685cfb2a7 | 237 | void printf_ipv6_address(uint8_t *addr_ptr) |
sam_grove | 0:01fb291427ce | 238 | { |
sam_grove | 2:3e7685cfb2a7 | 239 | if(addr_ptr) |
sam_grove | 2:3e7685cfb2a7 | 240 | { |
sam_grove | 2:3e7685cfb2a7 | 241 | uint8_t i, d_colon = 0; |
sam_grove | 2:3e7685cfb2a7 | 242 | uint16_t current_value = 0, last_value = 0; |
sam_grove | 2:3e7685cfb2a7 | 243 | |
sam_grove | 2:3e7685cfb2a7 | 244 | for(i=0; i< 16;i += 2) |
sam_grove | 2:3e7685cfb2a7 | 245 | { |
sam_grove | 2:3e7685cfb2a7 | 246 | current_value = (*addr_ptr++ << 8); |
sam_grove | 2:3e7685cfb2a7 | 247 | current_value += *addr_ptr++; |
sam_grove | 2:3e7685cfb2a7 | 248 | |
sam_grove | 2:3e7685cfb2a7 | 249 | if(i == 0) |
sam_grove | 2:3e7685cfb2a7 | 250 | { |
sam_grove | 2:3e7685cfb2a7 | 251 | last_value = current_value; |
sam_grove | 2:3e7685cfb2a7 | 252 | debug_hex(current_value >> 8); |
sam_grove | 2:3e7685cfb2a7 | 253 | debug_hex(current_value ); |
sam_grove | 2:3e7685cfb2a7 | 254 | debug(":"); |
sam_grove | 2:3e7685cfb2a7 | 255 | } |
sam_grove | 2:3e7685cfb2a7 | 256 | else |
sam_grove | 2:3e7685cfb2a7 | 257 | { |
sam_grove | 2:3e7685cfb2a7 | 258 | if(current_value == 0) |
sam_grove | 2:3e7685cfb2a7 | 259 | { |
sam_grove | 2:3e7685cfb2a7 | 260 | if(i== 14) |
sam_grove | 2:3e7685cfb2a7 | 261 | { |
sam_grove | 2:3e7685cfb2a7 | 262 | debug(":"); |
sam_grove | 2:3e7685cfb2a7 | 263 | //debug_put('0'); |
sam_grove | 2:3e7685cfb2a7 | 264 | debug("0"); |
sam_grove | 2:3e7685cfb2a7 | 265 | } |
sam_grove | 2:3e7685cfb2a7 | 266 | else |
sam_grove | 2:3e7685cfb2a7 | 267 | { |
sam_grove | 2:3e7685cfb2a7 | 268 | if(last_value == 0) |
sam_grove | 2:3e7685cfb2a7 | 269 | { |
sam_grove | 2:3e7685cfb2a7 | 270 | if(d_colon == 0) |
sam_grove | 2:3e7685cfb2a7 | 271 | { |
sam_grove | 2:3e7685cfb2a7 | 272 | d_colon=1; |
sam_grove | 2:3e7685cfb2a7 | 273 | } |
sam_grove | 2:3e7685cfb2a7 | 274 | } |
sam_grove | 2:3e7685cfb2a7 | 275 | else |
sam_grove | 2:3e7685cfb2a7 | 276 | { |
sam_grove | 2:3e7685cfb2a7 | 277 | if(d_colon == 2) |
sam_grove | 2:3e7685cfb2a7 | 278 | { |
sam_grove | 2:3e7685cfb2a7 | 279 | //debug_put('0'); |
sam_grove | 2:3e7685cfb2a7 | 280 | debug("0"); |
sam_grove | 2:3e7685cfb2a7 | 281 | debug(":"); |
sam_grove | 2:3e7685cfb2a7 | 282 | } |
sam_grove | 2:3e7685cfb2a7 | 283 | } |
sam_grove | 2:3e7685cfb2a7 | 284 | } |
sam_grove | 2:3e7685cfb2a7 | 285 | } |
sam_grove | 2:3e7685cfb2a7 | 286 | else |
sam_grove | 2:3e7685cfb2a7 | 287 | { |
sam_grove | 2:3e7685cfb2a7 | 288 | if(last_value == 0) |
sam_grove | 2:3e7685cfb2a7 | 289 | { |
sam_grove | 2:3e7685cfb2a7 | 290 | if(d_colon == 1) |
sam_grove | 2:3e7685cfb2a7 | 291 | { |
sam_grove | 2:3e7685cfb2a7 | 292 | debug(":"); |
sam_grove | 2:3e7685cfb2a7 | 293 | d_colon = 2; |
sam_grove | 2:3e7685cfb2a7 | 294 | } |
sam_grove | 2:3e7685cfb2a7 | 295 | else |
sam_grove | 2:3e7685cfb2a7 | 296 | { |
sam_grove | 2:3e7685cfb2a7 | 297 | //debug_put('0'); |
sam_grove | 2:3e7685cfb2a7 | 298 | debug("0"); |
sam_grove | 2:3e7685cfb2a7 | 299 | debug(":"); |
sam_grove | 2:3e7685cfb2a7 | 300 | } |
sam_grove | 2:3e7685cfb2a7 | 301 | } |
sam_grove | 2:3e7685cfb2a7 | 302 | if(current_value > 0x00ff) |
sam_grove | 2:3e7685cfb2a7 | 303 | { |
sam_grove | 2:3e7685cfb2a7 | 304 | debug_hex(current_value >> 8); |
sam_grove | 2:3e7685cfb2a7 | 305 | } |
sam_grove | 2:3e7685cfb2a7 | 306 | debug_hex(current_value ); |
sam_grove | 2:3e7685cfb2a7 | 307 | if(i< 14) |
sam_grove | 2:3e7685cfb2a7 | 308 | { |
sam_grove | 2:3e7685cfb2a7 | 309 | debug(":"); |
sam_grove | 2:3e7685cfb2a7 | 310 | } |
sam_grove | 2:3e7685cfb2a7 | 311 | } |
sam_grove | 2:3e7685cfb2a7 | 312 | last_value = current_value; |
sam_grove | 2:3e7685cfb2a7 | 313 | } |
sam_grove | 2:3e7685cfb2a7 | 314 | } |
sam_grove | 2:3e7685cfb2a7 | 315 | } |
sam_grove | 2:3e7685cfb2a7 | 316 | else |
sam_grove | 2:3e7685cfb2a7 | 317 | { |
sam_grove | 2:3e7685cfb2a7 | 318 | debug("Address Print: pointer NULL"); |
sam_grove | 2:3e7685cfb2a7 | 319 | } |
sam_grove | 2:3e7685cfb2a7 | 320 | debug("\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 321 | } |
sam_grove | 2:3e7685cfb2a7 | 322 | #endif |
sam_grove | 2:3e7685cfb2a7 | 323 | |
sam_grove | 2:3e7685cfb2a7 | 324 | int main() |
sam_grove | 2:3e7685cfb2a7 | 325 | { |
sam_grove | 2:3e7685cfb2a7 | 326 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 327 | ledR = 1; |
sam_grove | 2:3e7685cfb2a7 | 328 | ledG = 1; |
sam_grove | 2:3e7685cfb2a7 | 329 | ledB = 1; |
sam_grove | 2:3e7685cfb2a7 | 330 | TAP_INT.rise(&tap_detector_handler); |
sam_grove | 2:3e7685cfb2a7 | 331 | #endif |
sam_grove | 2:3e7685cfb2a7 | 332 | |
sam_grove | 2:3e7685cfb2a7 | 333 | debug("\r\nApplication Start\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 334 | ns_dyn_mem_init(app_defined_stack_heap, APP_DEV_HEAP_SIZE, app_heap_error_handler,0); |
sam_grove | 2:3e7685cfb2a7 | 335 | rf_set_mac_address(app_MAC_address); |
FSL\B36402 |
15:990a8b5664e1 | 336 | |
sam_grove | 2:3e7685cfb2a7 | 337 | // init RF interface |
sam_grove | 2:3e7685cfb2a7 | 338 | debug("Init RF Interface...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 339 | rf_phy_device_register_id = rf_device_register(); |
FSL\B36402 |
15:990a8b5664e1 | 340 | |
sam_grove | 2:3e7685cfb2a7 | 341 | randLIB_seed_random(); |
sam_grove | 0:01fb291427ce | 342 | |
sam_grove | 2:3e7685cfb2a7 | 343 | debug("Init ARM Timer...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 344 | arm_timer_init();//Init Timer |
FSL\B36402 |
15:990a8b5664e1 | 345 | |
sam_grove | 2:3e7685cfb2a7 | 346 | //Init nanostack & Event OS |
sam_grove | 2:3e7685cfb2a7 | 347 | debug("Init Net Core...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 348 | net_init_core(); |
sam_grove | 2:3e7685cfb2a7 | 349 | debug("Create Tasklets...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 350 | main_tasklet_id = arm_ns_tasklet_create(&tasklet_main); |
sam_grove | 2:3e7685cfb2a7 | 351 | if(main_tasklet_id < 0) |
sam_grove | 2:3e7685cfb2a7 | 352 | { |
sam_grove | 2:3e7685cfb2a7 | 353 | //Tasklet cerate fail |
sam_grove | 2:3e7685cfb2a7 | 354 | error("Tasklet create fail.."); |
sam_grove | 2:3e7685cfb2a7 | 355 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 356 | } |
FSL\B36402 |
15:990a8b5664e1 | 357 | |
sam_grove | 2:3e7685cfb2a7 | 358 | debug("Event Dispatch\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 359 | event_dispatch(); |
sam_grove | 2:3e7685cfb2a7 | 360 | } |
sam_grove | 2:3e7685cfb2a7 | 361 | |
sam_grove | 2:3e7685cfb2a7 | 362 | /** |
sam_grove | 2:3e7685cfb2a7 | 363 | * @brief This function handles Hard Fault exception. |
sam_grove | 2:3e7685cfb2a7 | 364 | * @param None |
sam_grove | 2:3e7685cfb2a7 | 365 | * @retval None |
sam_grove | 2:3e7685cfb2a7 | 366 | */ |
sam_grove | 2:3e7685cfb2a7 | 367 | void HardFault_Handler(void) |
sam_grove | 2:3e7685cfb2a7 | 368 | { |
sam_grove | 2:3e7685cfb2a7 | 369 | /* Go to infinite loop when Hard Fault exception occurs */ |
sam_grove | 2:3e7685cfb2a7 | 370 | while (1) |
sam_grove | 2:3e7685cfb2a7 | 371 | { |
sam_grove | 2:3e7685cfb2a7 | 372 | } |
sam_grove | 2:3e7685cfb2a7 | 373 | } |
sam_grove | 2:3e7685cfb2a7 | 374 | void app_heap_error_handler(heap_fail_t event) |
sam_grove | 2:3e7685cfb2a7 | 375 | { |
sam_grove | 2:3e7685cfb2a7 | 376 | switch (event) |
sam_grove | 2:3e7685cfb2a7 | 377 | { |
sam_grove | 2:3e7685cfb2a7 | 378 | case NS_DYN_MEM_NULL_FREE: |
sam_grove | 2:3e7685cfb2a7 | 379 | break; |
sam_grove | 2:3e7685cfb2a7 | 380 | case NS_DYN_MEM_DOUBLE_FREE: |
sam_grove | 2:3e7685cfb2a7 | 381 | break; |
sam_grove | 2:3e7685cfb2a7 | 382 | |
sam_grove | 2:3e7685cfb2a7 | 383 | case NS_DYN_MEM_ALLOCATE_SIZE_NOT_VALID: |
sam_grove | 2:3e7685cfb2a7 | 384 | break; |
sam_grove | 2:3e7685cfb2a7 | 385 | case NS_DYN_MEM_POINTER_NOT_VALID: |
sam_grove | 2:3e7685cfb2a7 | 386 | break; |
sam_grove | 2:3e7685cfb2a7 | 387 | |
sam_grove | 2:3e7685cfb2a7 | 388 | case NS_DYN_MEM_HEAP_SECTOR_CORRUPTED: |
sam_grove | 2:3e7685cfb2a7 | 389 | break; |
sam_grove | 2:3e7685cfb2a7 | 390 | |
sam_grove | 2:3e7685cfb2a7 | 391 | case NS_DYN_MEM_HEAP_SECTOR_UNITIALIZED: |
sam_grove | 2:3e7685cfb2a7 | 392 | break; |
sam_grove | 2:3e7685cfb2a7 | 393 | |
sam_grove | 2:3e7685cfb2a7 | 394 | default: |
sam_grove | 2:3e7685cfb2a7 | 395 | |
sam_grove | 2:3e7685cfb2a7 | 396 | break; |
sam_grove | 2:3e7685cfb2a7 | 397 | } |
sam_grove | 2:3e7685cfb2a7 | 398 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 399 | } |
sam_grove | 2:3e7685cfb2a7 | 400 | |
sam_grove | 2:3e7685cfb2a7 | 401 | |
sam_grove | 2:3e7685cfb2a7 | 402 | /** |
sam_grove | 2:3e7685cfb2a7 | 403 | * \brief Network state event handler. |
sam_grove | 2:3e7685cfb2a7 | 404 | * \param event show network start response or current network state. |
sam_grove | 2:3e7685cfb2a7 | 405 | * |
sam_grove | 2:3e7685cfb2a7 | 406 | * - NET_READY: Save NVK peristant data to NVM and Net role |
sam_grove | 2:3e7685cfb2a7 | 407 | * - NET_NO_BEACON: Link Layer Active Scan Fail, Stack is Already at Idle state |
sam_grove | 2:3e7685cfb2a7 | 408 | * - NET_NO_ND_ROUTER: No ND Router at current Channel Stack is Already at Idle state |
sam_grove | 2:3e7685cfb2a7 | 409 | * - NET_BORDER_ROUTER_LOST: Connection to Access point is lost wait for Scan Result |
sam_grove | 2:3e7685cfb2a7 | 410 | * - NET_PARENT_POLL_FAIL: Host should run net start without any PAN-id filter and all channels |
sam_grove | 2:3e7685cfb2a7 | 411 | * - NET_PANA_SERVER_AUTH_FAIL: Pana Authentication fail, Stack is Already at Idle state |
sam_grove | 2:3e7685cfb2a7 | 412 | */ |
sam_grove | 2:3e7685cfb2a7 | 413 | void app_parse_network_event(arm_event_s *event ) |
sam_grove | 2:3e7685cfb2a7 | 414 | { |
sam_grove | 2:3e7685cfb2a7 | 415 | arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e)event->event_data; |
sam_grove | 2:3e7685cfb2a7 | 416 | switch (status) |
sam_grove | 2:3e7685cfb2a7 | 417 | { |
sam_grove | 2:3e7685cfb2a7 | 418 | case ARM_NWK_BOOTSTRAP_READY: |
sam_grove | 2:3e7685cfb2a7 | 419 | /* NEtwork is ready and node is connected to Access Point */ |
sam_grove | 2:3e7685cfb2a7 | 420 | if(access_point_status==0) |
sam_grove | 2:3e7685cfb2a7 | 421 | { |
sam_grove | 2:3e7685cfb2a7 | 422 | uint8_t temp_ipv6[16]; |
sam_grove | 2:3e7685cfb2a7 | 423 | debug("Network Connection Ready\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 424 | access_point_status=1; |
sam_grove | 2:3e7685cfb2a7 | 425 | //Read Address |
sam_grove | 2:3e7685cfb2a7 | 426 | |
sam_grove | 2:3e7685cfb2a7 | 427 | if( arm_nwk_nd_address_read(net_6lowpan_id,&app_nd_address_info) != 0) |
sam_grove | 2:3e7685cfb2a7 | 428 | { |
sam_grove | 2:3e7685cfb2a7 | 429 | debug("ND Address read fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 430 | } |
sam_grove | 2:3e7685cfb2a7 | 431 | else |
sam_grove | 2:3e7685cfb2a7 | 432 | { |
sam_grove | 2:3e7685cfb2a7 | 433 | debug("ND Access Point: "); |
sam_grove | 2:3e7685cfb2a7 | 434 | printf_ipv6_address(app_nd_address_info.border_router); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 435 | |
sam_grove | 2:3e7685cfb2a7 | 436 | debug("ND Prefix 64: "); |
sam_grove | 2:3e7685cfb2a7 | 437 | printf_array(app_nd_address_info.prefix, 8); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 438 | |
sam_grove | 2:3e7685cfb2a7 | 439 | if(arm_net_address_get(net_6lowpan_id,ADDR_IPV6_GP,temp_ipv6) == 0) |
sam_grove | 2:3e7685cfb2a7 | 440 | { |
sam_grove | 2:3e7685cfb2a7 | 441 | debug("GP IPv6: "); |
sam_grove | 2:3e7685cfb2a7 | 442 | printf_ipv6_address(temp_ipv6); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 443 | } |
sam_grove | 2:3e7685cfb2a7 | 444 | } |
sam_grove | 2:3e7685cfb2a7 | 445 | |
sam_grove | 2:3e7685cfb2a7 | 446 | if( arm_nwk_mac_address_read(net_6lowpan_id,&app_link_address_info) != 0) |
sam_grove | 2:3e7685cfb2a7 | 447 | { |
sam_grove | 2:3e7685cfb2a7 | 448 | debug("MAC Address read fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 449 | } |
sam_grove | 2:3e7685cfb2a7 | 450 | else |
sam_grove | 2:3e7685cfb2a7 | 451 | { |
sam_grove | 2:3e7685cfb2a7 | 452 | uint8_t temp[2]; |
sam_grove | 2:3e7685cfb2a7 | 453 | common_write_16_bit(app_link_address_info.mac_short,temp); |
sam_grove | 2:3e7685cfb2a7 | 454 | debug("MAC 16-bit: "); |
sam_grove | 2:3e7685cfb2a7 | 455 | printf_array(temp, 2); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 456 | common_write_16_bit(app_link_address_info.PANId,temp); |
sam_grove | 2:3e7685cfb2a7 | 457 | debug("PAN-ID: "); |
sam_grove | 2:3e7685cfb2a7 | 458 | printf_array(temp, 2); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 459 | debug("MAC 64-bit: "); |
sam_grove | 2:3e7685cfb2a7 | 460 | printf_array(app_link_address_info.long_euid64, 8); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 461 | debug("EUID64(Based on MAC 64-bit address): "); |
sam_grove | 2:3e7685cfb2a7 | 462 | printf_array(app_link_address_info.euid64, 8); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 463 | } |
sam_grove | 2:3e7685cfb2a7 | 464 | } |
sam_grove | 2:3e7685cfb2a7 | 465 | break; |
sam_grove | 2:3e7685cfb2a7 | 466 | |
sam_grove | 2:3e7685cfb2a7 | 467 | case ARM_NWK_NWK_SCAN_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 468 | /* Link Layer Active Scan Fail, Stack is Already at Idle state */ |
sam_grove | 2:3e7685cfb2a7 | 469 | debug("Link Layer Scan Fail: No Beacons\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 470 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 471 | //dnssd_disable(1); |
sam_grove | 2:3e7685cfb2a7 | 472 | break; |
sam_grove | 2:3e7685cfb2a7 | 473 | |
sam_grove | 2:3e7685cfb2a7 | 474 | case ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 475 | /* No ND Router at current Channel Stack is Already at Idle state */ |
sam_grove | 2:3e7685cfb2a7 | 476 | debug("ND Scan/ GP REG fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 477 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 478 | //dnssd_disable(1); |
sam_grove | 2:3e7685cfb2a7 | 479 | break; |
sam_grove | 2:3e7685cfb2a7 | 480 | |
sam_grove | 2:3e7685cfb2a7 | 481 | case ARM_NWK_NWK_CONNECTION_DOWN: |
sam_grove | 2:3e7685cfb2a7 | 482 | /* Connection to Access point is lost wait for Scan Result */ |
sam_grove | 2:3e7685cfb2a7 | 483 | debug("ND/RPL scan new network\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 484 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 485 | break; |
sam_grove | 2:3e7685cfb2a7 | 486 | |
sam_grove | 2:3e7685cfb2a7 | 487 | case ARM_NWK_NWK_PARENT_POLL_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 488 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 489 | break; |
sam_grove | 2:3e7685cfb2a7 | 490 | |
sam_grove | 2:3e7685cfb2a7 | 491 | case ARM_NWK_AUHTENTICATION_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 492 | debug("Network authentication fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 493 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 494 | break; |
sam_grove | 2:3e7685cfb2a7 | 495 | |
sam_grove | 2:3e7685cfb2a7 | 496 | default: |
sam_grove | 2:3e7685cfb2a7 | 497 | debug_hex(status); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 498 | debug("Unknow event"); |
sam_grove | 2:3e7685cfb2a7 | 499 | break; |
sam_grove | 2:3e7685cfb2a7 | 500 | } |
sam_grove | 2:3e7685cfb2a7 | 501 | |
sam_grove | 2:3e7685cfb2a7 | 502 | if(access_point_status == 0) |
sam_grove | 2:3e7685cfb2a7 | 503 | { |
sam_grove | 2:3e7685cfb2a7 | 504 | //Set Timer for new Trig |
sam_grove | 2:3e7685cfb2a7 | 505 | timer_sys_event(RETRY_TIMER, 10000); |
sam_grove | 2:3e7685cfb2a7 | 506 | } |
sam_grove | 2:3e7685cfb2a7 | 507 | } |
sam_grove | 2:3e7685cfb2a7 | 508 | |
sam_grove | 2:3e7685cfb2a7 | 509 | |
sam_grove | 2:3e7685cfb2a7 | 510 | /** |
sam_grove | 2:3e7685cfb2a7 | 511 | * \brief Handler for events sent to the application. |
sam_grove | 2:3e7685cfb2a7 | 512 | * \param event received event. |
sam_grove | 2:3e7685cfb2a7 | 513 | * |
sam_grove | 2:3e7685cfb2a7 | 514 | * - EV_NETWORK event, Network Event state event handler |
sam_grove | 2:3e7685cfb2a7 | 515 | * - EV_INIT, Set Certificate Chain list, init multicast, Start net start if NVM have session |
sam_grove | 2:3e7685cfb2a7 | 516 | * - EV_DEBUG, Terminal handler |
sam_grove | 2:3e7685cfb2a7 | 517 | */ |
sam_grove | 2:3e7685cfb2a7 | 518 | void tasklet_main(arm_event_s *event) |
sam_grove | 2:3e7685cfb2a7 | 519 | { |
sam_grove | 2:3e7685cfb2a7 | 520 | if(event->sender == 0) |
sam_grove | 2:3e7685cfb2a7 | 521 | { |
sam_grove | 2:3e7685cfb2a7 | 522 | arm_library_event_type_e event_type; |
sam_grove | 2:3e7685cfb2a7 | 523 | event_type = (arm_library_event_type_e)event->event_type; |
sam_grove | 2:3e7685cfb2a7 | 524 | |
sam_grove | 2:3e7685cfb2a7 | 525 | switch(event_type) |
sam_grove | 2:3e7685cfb2a7 | 526 | { |
sam_grove | 2:3e7685cfb2a7 | 527 | case ARM_LIB_NWK_INTERFACE_EVENT: |
sam_grove | 2:3e7685cfb2a7 | 528 | /* Network Event state event handler */ |
sam_grove | 2:3e7685cfb2a7 | 529 | debug("Event: ARM_LIB_NWK_INTERFACE\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 530 | app_parse_network_event(event); |
sam_grove | 2:3e7685cfb2a7 | 531 | break; |
sam_grove | 2:3e7685cfb2a7 | 532 | |
sam_grove | 2:3e7685cfb2a7 | 533 | case ARM_LIB_TASKLET_INIT_EVENT: |
sam_grove | 2:3e7685cfb2a7 | 534 | /*Init event from stack at start-up*/ |
sam_grove | 2:3e7685cfb2a7 | 535 | debug("Event: ARM_LIB_TASKLET_INIT\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 536 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 537 | timer_sys_event(ACCELEROMETER_TIMER, ACCEL_SAMPLE_TIME); |
sam_grove | 2:3e7685cfb2a7 | 538 | #endif |
sam_grove | 2:3e7685cfb2a7 | 539 | multicast_set_parameters(10,0,20,3,75 ); |
sam_grove | 2:3e7685cfb2a7 | 540 | |
sam_grove | 2:3e7685cfb2a7 | 541 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 542 | /* open a socket to support control for REMOTE_NODE */ |
sam_grove | 2:3e7685cfb2a7 | 543 | ctrl_udp_socket = socket_open(SOCKET_UDP, SEND_CTRL_UDP_PORT, empty_udp_receive); |
sam_grove | 2:3e7685cfb2a7 | 544 | #endif |
sam_grove | 2:3e7685cfb2a7 | 545 | |
sam_grove | 2:3e7685cfb2a7 | 546 | net_6lowpan_id = arm_nwk_interface_init(NET_INTERFACE_RF_6LOWPAN, rf_phy_device_register_id, "6LoWPAN_BORDER_ROUTER"); |
sam_grove | 2:3e7685cfb2a7 | 547 | if(net_6lowpan_id < 0) |
sam_grove | 2:3e7685cfb2a7 | 548 | { |
sam_grove | 2:3e7685cfb2a7 | 549 | debug("Interface Generate Fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 550 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 551 | } |
sam_grove | 2:3e7685cfb2a7 | 552 | else |
sam_grove | 2:3e7685cfb2a7 | 553 | { |
sam_grove | 2:3e7685cfb2a7 | 554 | //SET Bootsrap |
sam_grove | 2:3e7685cfb2a7 | 555 | if(arm_nwk_interface_configure_6lowpan_bootstrap_set(net_6lowpan_id, NET_6LOWPAN_HOST, 1) != 0) // Last parameter enables MLE protocol |
sam_grove | 2:3e7685cfb2a7 | 556 | { |
sam_grove | 2:3e7685cfb2a7 | 557 | //Bootsrap SET fail |
sam_grove | 2:3e7685cfb2a7 | 558 | debug("Bootstrap Fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 559 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 560 | } |
sam_grove | 2:3e7685cfb2a7 | 561 | else |
sam_grove | 2:3e7685cfb2a7 | 562 | { |
sam_grove | 2:3e7685cfb2a7 | 563 | int8_t retval = -1; |
sam_grove | 2:3e7685cfb2a7 | 564 | arm_nwk_6lowpan_gp_address_mode(net_6lowpan_id, NET_6LOWPAN_GP16_ADDRESS, NODE_SHORT_ADDRESS, 1); // 5 = short address for link-layer // 1 = generate automatically if duplicate address is encountered |
sam_grove | 2:3e7685cfb2a7 | 565 | arm_nwk_link_layer_security_mode(net_6lowpan_id, NET_SEC_MODE_NO_LINK_SECURITY, 0, 0); |
sam_grove | 2:3e7685cfb2a7 | 566 | arm_nwk_6lowpan_link_scan_paramameter_set(rf_phy_device_register_id, channel_list, 5); |
sam_grove | 2:3e7685cfb2a7 | 567 | retval = arm_nwk_interface_up(net_6lowpan_id); |
sam_grove | 2:3e7685cfb2a7 | 568 | if(retval != 0) |
sam_grove | 2:3e7685cfb2a7 | 569 | { |
sam_grove | 2:3e7685cfb2a7 | 570 | //6Lowpan Bootsrap start fail |
sam_grove | 2:3e7685cfb2a7 | 571 | debug("6LowPAN Bootstrap start Fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 572 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 573 | } |
sam_grove | 2:3e7685cfb2a7 | 574 | else |
sam_grove | 2:3e7685cfb2a7 | 575 | { |
sam_grove | 2:3e7685cfb2a7 | 576 | //6Lowpan Bootsrap start OK |
sam_grove | 2:3e7685cfb2a7 | 577 | debug("6LowPAN Bootstrap Start OK\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 578 | } |
sam_grove | 2:3e7685cfb2a7 | 579 | // open sockets |
sam_grove | 2:3e7685cfb2a7 | 580 | coap_udp_socket = socket_open(SOCKET_UDP, AUDP_SOCKET_PORT, NSDL_receive_socket); |
sam_grove | 2:3e7685cfb2a7 | 581 | // Start NSDL |
sam_grove | 2:3e7685cfb2a7 | 582 | nsdl_run(); |
sam_grove | 2:3e7685cfb2a7 | 583 | } |
sam_grove | 2:3e7685cfb2a7 | 584 | timer_sys_event(NSP_REGISTRATION_TIMER, RD_UPDATE_PERIOD); |
sam_grove | 0:01fb291427ce | 585 | } |
sam_grove | 2:3e7685cfb2a7 | 586 | break; |
sam_grove | 2:3e7685cfb2a7 | 587 | |
sam_grove | 2:3e7685cfb2a7 | 588 | case ARM_LIB_SYSTEM_TIMER_EVENT: |
sam_grove | 2:3e7685cfb2a7 | 589 | timer_sys_event_cancel(event->event_id); |
sam_grove | 2:3e7685cfb2a7 | 590 | if (event->event_id == NSP_REGISTRATION_TIMER) |
sam_grove | 2:3e7685cfb2a7 | 591 | { |
sam_grove | 2:3e7685cfb2a7 | 592 | printf("Time to register...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 593 | NSP_registration(); |
sam_grove | 2:3e7685cfb2a7 | 594 | #if BATTERY |
sam_grove | 2:3e7685cfb2a7 | 595 | batteryLevel -= 10; |
sam_grove | 2:3e7685cfb2a7 | 596 | if (batteryLevel == 0) |
sam_grove | 2:3e7685cfb2a7 | 597 | batteryLevel = 100; |
sam_grove | 2:3e7685cfb2a7 | 598 | #endif |
sam_grove | 2:3e7685cfb2a7 | 599 | timer_sys_event(NSP_REGISTRATION_TIMER, RD_UPDATE_PERIOD); |
sam_grove | 2:3e7685cfb2a7 | 600 | } |
sam_grove | 2:3e7685cfb2a7 | 601 | else if (event->event_id == RETRY_TIMER) |
sam_grove | 2:3e7685cfb2a7 | 602 | { |
sam_grove | 2:3e7685cfb2a7 | 603 | debug("Event: ARM_LIB_SYSTEM_TIMER (event_id = 1)\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 604 | int8_t retval = -1; |
sam_grove | 2:3e7685cfb2a7 | 605 | retval = arm_nwk_interface_up(net_6lowpan_id); |
sam_grove | 2:3e7685cfb2a7 | 606 | if(retval != 0) |
sam_grove | 2:3e7685cfb2a7 | 607 | { |
sam_grove | 2:3e7685cfb2a7 | 608 | //6Lowpan Bootsrap start fail |
sam_grove | 2:3e7685cfb2a7 | 609 | debug("6LowPAN Bootstrap Start Failure\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 610 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 611 | } |
sam_grove | 2:3e7685cfb2a7 | 612 | else |
sam_grove | 2:3e7685cfb2a7 | 613 | { |
sam_grove | 2:3e7685cfb2a7 | 614 | //6Lowpan Bootsrap start OK |
sam_grove | 2:3e7685cfb2a7 | 615 | debug("6LowPAN Bootstrap Start OK\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 616 | } |
sam_grove | 2:3e7685cfb2a7 | 617 | } |
sam_grove | 2:3e7685cfb2a7 | 618 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 619 | else if (event->event_id == ACCELEROMETER_TIMER) |
sam_grove | 2:3e7685cfb2a7 | 620 | { |
sam_grove | 2:3e7685cfb2a7 | 621 | if (accel.getAccX() < -0.85f && accel_enable) |
sam_grove | 2:3e7685cfb2a7 | 622 | { |
sam_grove | 2:3e7685cfb2a7 | 623 | accel_enable = 0; |
sam_grove | 2:3e7685cfb2a7 | 624 | update_LED(); |
sam_grove | 2:3e7685cfb2a7 | 625 | } |
sam_grove | 2:3e7685cfb2a7 | 626 | if (accel.getAccX() > -0.35f) |
sam_grove | 2:3e7685cfb2a7 | 627 | accel_enable = 1; |
sam_grove | 2:3e7685cfb2a7 | 628 | |
sam_grove | 2:3e7685cfb2a7 | 629 | timer_sys_event(ACCELEROMETER_TIMER, ACCEL_SAMPLE_TIME); |
sam_grove | 2:3e7685cfb2a7 | 630 | } |
sam_grove | 2:3e7685cfb2a7 | 631 | #endif |
sam_grove | 3:a38ad504a18c | 632 | |
sam_grove | 2:3e7685cfb2a7 | 633 | break; |
sam_grove | 2:3e7685cfb2a7 | 634 | |
sam_grove | 2:3e7685cfb2a7 | 635 | default: |
sam_grove | 2:3e7685cfb2a7 | 636 | break; |
sam_grove | 2:3e7685cfb2a7 | 637 | } |
sam_grove | 2:3e7685cfb2a7 | 638 | } |
sam_grove | 2:3e7685cfb2a7 | 639 | } |
sam_grove | 2:3e7685cfb2a7 | 640 | |
sam_grove | 2:3e7685cfb2a7 | 641 | void NSDL_receive_socket(void * cb) |
sam_grove | 2:3e7685cfb2a7 | 642 | { |
sam_grove | 2:3e7685cfb2a7 | 643 | socket_callback_t * cb_res =0; |
sam_grove | 2:3e7685cfb2a7 | 644 | int16_t length; |
sam_grove | 2:3e7685cfb2a7 | 645 | cb_res = (socket_callback_t *) cb; |
sam_grove | 2:3e7685cfb2a7 | 646 | uint8_t *payload; |
sam_grove | 2:3e7685cfb2a7 | 647 | |
sam_grove | 2:3e7685cfb2a7 | 648 | if(cb_res->event_type == SOCKET_DATA) |
sam_grove | 2:3e7685cfb2a7 | 649 | { |
sam_grove | 2:3e7685cfb2a7 | 650 | debug("LINK LQI:"); |
sam_grove | 2:3e7685cfb2a7 | 651 | debug_hex(cb_res->LINK_LQI); |
sam_grove | 2:3e7685cfb2a7 | 652 | create_lqi_resource(cb_res->LINK_LQI); |
sam_grove | 2:3e7685cfb2a7 | 653 | debug("\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 654 | |
sam_grove | 2:3e7685cfb2a7 | 655 | if ( cb_res->d_len > 0) |
sam_grove | 2:3e7685cfb2a7 | 656 | { |
sam_grove | 2:3e7685cfb2a7 | 657 | payload = (uint8_t *) own_alloc(cb_res->d_len); |
sam_grove | 2:3e7685cfb2a7 | 658 | if(payload) |
sam_grove | 2:3e7685cfb2a7 | 659 | { |
sam_grove | 2:3e7685cfb2a7 | 660 | //Read data to the RX buffer |
sam_grove | 2:3e7685cfb2a7 | 661 | length = socket_read(cb_res->socket_id, &app_src, payload, cb_res->d_len); //replace rx_buffer payload |
sam_grove | 2:3e7685cfb2a7 | 662 | if(length) |
sam_grove | 2:3e7685cfb2a7 | 663 | { |
sam_grove | 2:3e7685cfb2a7 | 664 | if(cb_res->socket_id == coap_udp_socket) |
sam_grove | 2:3e7685cfb2a7 | 665 | { |
sam_grove | 2:3e7685cfb2a7 | 666 | // Handles data received in UDP socket |
sam_grove | 2:3e7685cfb2a7 | 667 | // Call application protocol parser. |
sam_grove | 2:3e7685cfb2a7 | 668 | sn_addr_s.type = SN_NSDL_ADDRESS_TYPE_IPV6; |
sam_grove | 2:3e7685cfb2a7 | 669 | sn_addr_s.addr_len = 16; |
sam_grove | 2:3e7685cfb2a7 | 670 | sn_addr_s.port = app_src.identifier; |
sam_grove | 2:3e7685cfb2a7 | 671 | sn_addr_s.addr_ptr = app_src.address; |
sam_grove | 2:3e7685cfb2a7 | 672 | printf("Data 1\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 673 | if(sn_nsdl_process_coap(payload, length, &sn_addr_s)) // 0= ok, -1=failure |
sam_grove | 2:3e7685cfb2a7 | 674 | { |
sam_grove | 2:3e7685cfb2a7 | 675 | debug("Error processing CoAP packet\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 676 | } |
sam_grove | 2:3e7685cfb2a7 | 677 | printf("Data 4\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 678 | } |
sam_grove | 2:3e7685cfb2a7 | 679 | } |
sam_grove | 2:3e7685cfb2a7 | 680 | own_free(payload); |
sam_grove | 2:3e7685cfb2a7 | 681 | } |
sam_grove | 2:3e7685cfb2a7 | 682 | } |
sam_grove | 2:3e7685cfb2a7 | 683 | } |
sam_grove | 2:3e7685cfb2a7 | 684 | #if 1 // enabled for debug |
sam_grove | 2:3e7685cfb2a7 | 685 | else if(cb_res->event_type == SOCKET_TX_DONE) |
sam_grove | 2:3e7685cfb2a7 | 686 | { |
sam_grove | 2:3e7685cfb2a7 | 687 | //debug("*"); |
sam_grove | 2:3e7685cfb2a7 | 688 | } |
sam_grove | 2:3e7685cfb2a7 | 689 | else if(cb_res->event_type == SOCKET_NO_ROUTE) |
sam_grove | 2:3e7685cfb2a7 | 690 | { |
sam_grove | 2:3e7685cfb2a7 | 691 | debug("SOCKET_NO_ROUTE\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 692 | } |
sam_grove | 2:3e7685cfb2a7 | 693 | else if(cb_res->event_type == SOCKET_TX_FAIL) |
sam_grove | 2:3e7685cfb2a7 | 694 | { |
sam_grove | 2:3e7685cfb2a7 | 695 | debug("SOCKET_TX_FAIL\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 696 | } |
sam_grove | 2:3e7685cfb2a7 | 697 | #endif |
sam_grove | 2:3e7685cfb2a7 | 698 | } |
sam_grove | 2:3e7685cfb2a7 | 699 | |
sam_grove | 2:3e7685cfb2a7 | 700 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 701 | void ctrl_udp_send(uint8_t cmd) |
sam_grove | 2:3e7685cfb2a7 | 702 | { |
sam_grove | 2:3e7685cfb2a7 | 703 | uint8_t * payload = 0; |
sam_grove | 2:3e7685cfb2a7 | 704 | ns_address_t address; |
sam_grove | 2:3e7685cfb2a7 | 705 | /*SET UDP echo Port*/ |
sam_grove | 2:3e7685cfb2a7 | 706 | address.identifier = RECV_CTRL_UDP_PORT; |
sam_grove | 2:3e7685cfb2a7 | 707 | |
sam_grove | 2:3e7685cfb2a7 | 708 | uint8_t node_address[16] = REMOTE_NODE; |
sam_grove | 2:3e7685cfb2a7 | 709 | |
sam_grove | 2:3e7685cfb2a7 | 710 | payload = (uint8_t *) ns_dyn_mem_alloc(1); |
sam_grove | 2:3e7685cfb2a7 | 711 | if(payload) |
sam_grove | 2:3e7685cfb2a7 | 712 | { |
sam_grove | 2:3e7685cfb2a7 | 713 | uint8_t *ptr = payload; |
sam_grove | 2:3e7685cfb2a7 | 714 | |
sam_grove | 2:3e7685cfb2a7 | 715 | memcpy(address.address,node_address,16); |
sam_grove | 2:3e7685cfb2a7 | 716 | address.type = ADDRESS_IPV6; |
sam_grove | 2:3e7685cfb2a7 | 717 | *ptr = cmd; |
sam_grove | 2:3e7685cfb2a7 | 718 | if(socket_sendto(ctrl_udp_socket, &address, payload, (1)) != 0) |
sam_grove | 2:3e7685cfb2a7 | 719 | { |
sam_grove | 2:3e7685cfb2a7 | 720 | debug("Ctrl UDP Failed\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 721 | } |
sam_grove | 2:3e7685cfb2a7 | 722 | else |
sam_grove | 2:3e7685cfb2a7 | 723 | { |
sam_grove | 2:3e7685cfb2a7 | 724 | debug("Ctrl UDP Ok\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 725 | } |
sam_grove | 2:3e7685cfb2a7 | 726 | ns_dyn_mem_free(payload); |
sam_grove | 2:3e7685cfb2a7 | 727 | } |
sam_grove | 2:3e7685cfb2a7 | 728 | else |
sam_grove | 2:3e7685cfb2a7 | 729 | { |
sam_grove | 2:3e7685cfb2a7 | 730 | debug("No Heap for Ctrl UDP\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 731 | } |
sam_grove | 2:3e7685cfb2a7 | 732 | } |
sam_grove | 2:3e7685cfb2a7 | 733 | // we don't expect to receive data |
sam_grove | 2:3e7685cfb2a7 | 734 | void empty_udp_receive(void * cb) { |
sam_grove | 2:3e7685cfb2a7 | 735 | |
sam_grove | 2:3e7685cfb2a7 | 736 | } |
sam_grove | 2:3e7685cfb2a7 | 737 | #endif |
sam_grove | 2:3e7685cfb2a7 | 738 |