mcr20_wireless_uart_FRDMk64f _FRDMcR20A_uart_test
Dependencies: fsl_phy_mcr20a fsl_smac mbed-rtos mbed
Fork of mcr20_wireless_uart by
main.cpp@3:a38ad504a18c, 2015-03-05 (annotated)
- Committer:
- sam_grove
- Date:
- Thu Mar 05 16:37:54 2015 +0000
- Revision:
- 3:a38ad504a18c
- Parent:
- 2:3e7685cfb2a7
- Child:
- 15:990a8b5664e1
Delete multi use functions and update mbed library - exporters for IAR now working again
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); |
sam_grove | 2:3e7685cfb2a7 | 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(); |
sam_grove | 2:3e7685cfb2a7 | 340 | randLIB_seed_random(); |
sam_grove | 0:01fb291427ce | 341 | |
sam_grove | 2:3e7685cfb2a7 | 342 | debug("Init ARM Timer...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 343 | arm_timer_init();//Init Timer |
sam_grove | 2:3e7685cfb2a7 | 344 | //Init nanostack & Event OS |
sam_grove | 2:3e7685cfb2a7 | 345 | debug("Init Net Core...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 346 | net_init_core(); |
sam_grove | 2:3e7685cfb2a7 | 347 | debug("Create Tasklets...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 348 | main_tasklet_id = arm_ns_tasklet_create(&tasklet_main); |
sam_grove | 2:3e7685cfb2a7 | 349 | if(main_tasklet_id < 0) |
sam_grove | 2:3e7685cfb2a7 | 350 | { |
sam_grove | 2:3e7685cfb2a7 | 351 | //Tasklet cerate fail |
sam_grove | 2:3e7685cfb2a7 | 352 | error("Tasklet create fail.."); |
sam_grove | 2:3e7685cfb2a7 | 353 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 354 | } |
sam_grove | 2:3e7685cfb2a7 | 355 | debug("Event Dispatch\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 356 | event_dispatch(); |
sam_grove | 2:3e7685cfb2a7 | 357 | } |
sam_grove | 2:3e7685cfb2a7 | 358 | |
sam_grove | 2:3e7685cfb2a7 | 359 | /** |
sam_grove | 2:3e7685cfb2a7 | 360 | * @brief This function handles Hard Fault exception. |
sam_grove | 2:3e7685cfb2a7 | 361 | * @param None |
sam_grove | 2:3e7685cfb2a7 | 362 | * @retval None |
sam_grove | 2:3e7685cfb2a7 | 363 | */ |
sam_grove | 2:3e7685cfb2a7 | 364 | void HardFault_Handler(void) |
sam_grove | 2:3e7685cfb2a7 | 365 | { |
sam_grove | 2:3e7685cfb2a7 | 366 | /* Go to infinite loop when Hard Fault exception occurs */ |
sam_grove | 2:3e7685cfb2a7 | 367 | while (1) |
sam_grove | 2:3e7685cfb2a7 | 368 | { |
sam_grove | 2:3e7685cfb2a7 | 369 | } |
sam_grove | 2:3e7685cfb2a7 | 370 | } |
sam_grove | 2:3e7685cfb2a7 | 371 | void app_heap_error_handler(heap_fail_t event) |
sam_grove | 2:3e7685cfb2a7 | 372 | { |
sam_grove | 2:3e7685cfb2a7 | 373 | switch (event) |
sam_grove | 2:3e7685cfb2a7 | 374 | { |
sam_grove | 2:3e7685cfb2a7 | 375 | case NS_DYN_MEM_NULL_FREE: |
sam_grove | 2:3e7685cfb2a7 | 376 | break; |
sam_grove | 2:3e7685cfb2a7 | 377 | case NS_DYN_MEM_DOUBLE_FREE: |
sam_grove | 2:3e7685cfb2a7 | 378 | break; |
sam_grove | 2:3e7685cfb2a7 | 379 | |
sam_grove | 2:3e7685cfb2a7 | 380 | case NS_DYN_MEM_ALLOCATE_SIZE_NOT_VALID: |
sam_grove | 2:3e7685cfb2a7 | 381 | break; |
sam_grove | 2:3e7685cfb2a7 | 382 | case NS_DYN_MEM_POINTER_NOT_VALID: |
sam_grove | 2:3e7685cfb2a7 | 383 | break; |
sam_grove | 2:3e7685cfb2a7 | 384 | |
sam_grove | 2:3e7685cfb2a7 | 385 | case NS_DYN_MEM_HEAP_SECTOR_CORRUPTED: |
sam_grove | 2:3e7685cfb2a7 | 386 | break; |
sam_grove | 2:3e7685cfb2a7 | 387 | |
sam_grove | 2:3e7685cfb2a7 | 388 | case NS_DYN_MEM_HEAP_SECTOR_UNITIALIZED: |
sam_grove | 2:3e7685cfb2a7 | 389 | break; |
sam_grove | 2:3e7685cfb2a7 | 390 | |
sam_grove | 2:3e7685cfb2a7 | 391 | default: |
sam_grove | 2:3e7685cfb2a7 | 392 | |
sam_grove | 2:3e7685cfb2a7 | 393 | break; |
sam_grove | 2:3e7685cfb2a7 | 394 | } |
sam_grove | 2:3e7685cfb2a7 | 395 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 396 | } |
sam_grove | 2:3e7685cfb2a7 | 397 | |
sam_grove | 2:3e7685cfb2a7 | 398 | |
sam_grove | 2:3e7685cfb2a7 | 399 | /** |
sam_grove | 2:3e7685cfb2a7 | 400 | * \brief Network state event handler. |
sam_grove | 2:3e7685cfb2a7 | 401 | * \param event show network start response or current network state. |
sam_grove | 2:3e7685cfb2a7 | 402 | * |
sam_grove | 2:3e7685cfb2a7 | 403 | * - NET_READY: Save NVK peristant data to NVM and Net role |
sam_grove | 2:3e7685cfb2a7 | 404 | * - NET_NO_BEACON: Link Layer Active Scan Fail, Stack is Already at Idle state |
sam_grove | 2:3e7685cfb2a7 | 405 | * - NET_NO_ND_ROUTER: No ND Router at current Channel Stack is Already at Idle state |
sam_grove | 2:3e7685cfb2a7 | 406 | * - NET_BORDER_ROUTER_LOST: Connection to Access point is lost wait for Scan Result |
sam_grove | 2:3e7685cfb2a7 | 407 | * - NET_PARENT_POLL_FAIL: Host should run net start without any PAN-id filter and all channels |
sam_grove | 2:3e7685cfb2a7 | 408 | * - NET_PANA_SERVER_AUTH_FAIL: Pana Authentication fail, Stack is Already at Idle state |
sam_grove | 2:3e7685cfb2a7 | 409 | */ |
sam_grove | 2:3e7685cfb2a7 | 410 | void app_parse_network_event(arm_event_s *event ) |
sam_grove | 2:3e7685cfb2a7 | 411 | { |
sam_grove | 2:3e7685cfb2a7 | 412 | arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e)event->event_data; |
sam_grove | 2:3e7685cfb2a7 | 413 | switch (status) |
sam_grove | 2:3e7685cfb2a7 | 414 | { |
sam_grove | 2:3e7685cfb2a7 | 415 | case ARM_NWK_BOOTSTRAP_READY: |
sam_grove | 2:3e7685cfb2a7 | 416 | /* NEtwork is ready and node is connected to Access Point */ |
sam_grove | 2:3e7685cfb2a7 | 417 | if(access_point_status==0) |
sam_grove | 2:3e7685cfb2a7 | 418 | { |
sam_grove | 2:3e7685cfb2a7 | 419 | uint8_t temp_ipv6[16]; |
sam_grove | 2:3e7685cfb2a7 | 420 | debug("Network Connection Ready\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 421 | access_point_status=1; |
sam_grove | 2:3e7685cfb2a7 | 422 | //Read Address |
sam_grove | 2:3e7685cfb2a7 | 423 | |
sam_grove | 2:3e7685cfb2a7 | 424 | if( arm_nwk_nd_address_read(net_6lowpan_id,&app_nd_address_info) != 0) |
sam_grove | 2:3e7685cfb2a7 | 425 | { |
sam_grove | 2:3e7685cfb2a7 | 426 | debug("ND Address read fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 427 | } |
sam_grove | 2:3e7685cfb2a7 | 428 | else |
sam_grove | 2:3e7685cfb2a7 | 429 | { |
sam_grove | 2:3e7685cfb2a7 | 430 | debug("ND Access Point: "); |
sam_grove | 2:3e7685cfb2a7 | 431 | printf_ipv6_address(app_nd_address_info.border_router); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 432 | |
sam_grove | 2:3e7685cfb2a7 | 433 | debug("ND Prefix 64: "); |
sam_grove | 2:3e7685cfb2a7 | 434 | printf_array(app_nd_address_info.prefix, 8); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 435 | |
sam_grove | 2:3e7685cfb2a7 | 436 | if(arm_net_address_get(net_6lowpan_id,ADDR_IPV6_GP,temp_ipv6) == 0) |
sam_grove | 2:3e7685cfb2a7 | 437 | { |
sam_grove | 2:3e7685cfb2a7 | 438 | debug("GP IPv6: "); |
sam_grove | 2:3e7685cfb2a7 | 439 | printf_ipv6_address(temp_ipv6); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 440 | } |
sam_grove | 2:3e7685cfb2a7 | 441 | } |
sam_grove | 2:3e7685cfb2a7 | 442 | |
sam_grove | 2:3e7685cfb2a7 | 443 | if( arm_nwk_mac_address_read(net_6lowpan_id,&app_link_address_info) != 0) |
sam_grove | 2:3e7685cfb2a7 | 444 | { |
sam_grove | 2:3e7685cfb2a7 | 445 | debug("MAC Address read fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 446 | } |
sam_grove | 2:3e7685cfb2a7 | 447 | else |
sam_grove | 2:3e7685cfb2a7 | 448 | { |
sam_grove | 2:3e7685cfb2a7 | 449 | uint8_t temp[2]; |
sam_grove | 2:3e7685cfb2a7 | 450 | common_write_16_bit(app_link_address_info.mac_short,temp); |
sam_grove | 2:3e7685cfb2a7 | 451 | debug("MAC 16-bit: "); |
sam_grove | 2:3e7685cfb2a7 | 452 | printf_array(temp, 2); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 453 | common_write_16_bit(app_link_address_info.PANId,temp); |
sam_grove | 2:3e7685cfb2a7 | 454 | debug("PAN-ID: "); |
sam_grove | 2:3e7685cfb2a7 | 455 | printf_array(temp, 2); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 456 | debug("MAC 64-bit: "); |
sam_grove | 2:3e7685cfb2a7 | 457 | printf_array(app_link_address_info.long_euid64, 8); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 458 | debug("EUID64(Based on MAC 64-bit address): "); |
sam_grove | 2:3e7685cfb2a7 | 459 | printf_array(app_link_address_info.euid64, 8); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 460 | } |
sam_grove | 2:3e7685cfb2a7 | 461 | } |
sam_grove | 2:3e7685cfb2a7 | 462 | break; |
sam_grove | 2:3e7685cfb2a7 | 463 | |
sam_grove | 2:3e7685cfb2a7 | 464 | case ARM_NWK_NWK_SCAN_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 465 | /* Link Layer Active Scan Fail, Stack is Already at Idle state */ |
sam_grove | 2:3e7685cfb2a7 | 466 | debug("Link Layer Scan Fail: No Beacons\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 467 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 468 | //dnssd_disable(1); |
sam_grove | 2:3e7685cfb2a7 | 469 | break; |
sam_grove | 2:3e7685cfb2a7 | 470 | |
sam_grove | 2:3e7685cfb2a7 | 471 | case ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 472 | /* No ND Router at current Channel Stack is Already at Idle state */ |
sam_grove | 2:3e7685cfb2a7 | 473 | debug("ND Scan/ GP REG fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 474 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 475 | //dnssd_disable(1); |
sam_grove | 2:3e7685cfb2a7 | 476 | break; |
sam_grove | 2:3e7685cfb2a7 | 477 | |
sam_grove | 2:3e7685cfb2a7 | 478 | case ARM_NWK_NWK_CONNECTION_DOWN: |
sam_grove | 2:3e7685cfb2a7 | 479 | /* Connection to Access point is lost wait for Scan Result */ |
sam_grove | 2:3e7685cfb2a7 | 480 | debug("ND/RPL scan new network\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 481 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 482 | break; |
sam_grove | 2:3e7685cfb2a7 | 483 | |
sam_grove | 2:3e7685cfb2a7 | 484 | case ARM_NWK_NWK_PARENT_POLL_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 485 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 486 | break; |
sam_grove | 2:3e7685cfb2a7 | 487 | |
sam_grove | 2:3e7685cfb2a7 | 488 | case ARM_NWK_AUHTENTICATION_FAIL: |
sam_grove | 2:3e7685cfb2a7 | 489 | debug("Network authentication fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 490 | access_point_status=0; |
sam_grove | 2:3e7685cfb2a7 | 491 | break; |
sam_grove | 2:3e7685cfb2a7 | 492 | |
sam_grove | 2:3e7685cfb2a7 | 493 | default: |
sam_grove | 2:3e7685cfb2a7 | 494 | debug_hex(status); //REVIEW |
sam_grove | 2:3e7685cfb2a7 | 495 | debug("Unknow event"); |
sam_grove | 2:3e7685cfb2a7 | 496 | break; |
sam_grove | 2:3e7685cfb2a7 | 497 | } |
sam_grove | 2:3e7685cfb2a7 | 498 | |
sam_grove | 2:3e7685cfb2a7 | 499 | if(access_point_status == 0) |
sam_grove | 2:3e7685cfb2a7 | 500 | { |
sam_grove | 2:3e7685cfb2a7 | 501 | //Set Timer for new Trig |
sam_grove | 2:3e7685cfb2a7 | 502 | timer_sys_event(RETRY_TIMER, 10000); |
sam_grove | 2:3e7685cfb2a7 | 503 | } |
sam_grove | 2:3e7685cfb2a7 | 504 | } |
sam_grove | 2:3e7685cfb2a7 | 505 | |
sam_grove | 2:3e7685cfb2a7 | 506 | |
sam_grove | 2:3e7685cfb2a7 | 507 | /** |
sam_grove | 2:3e7685cfb2a7 | 508 | * \brief Handler for events sent to the application. |
sam_grove | 2:3e7685cfb2a7 | 509 | * \param event received event. |
sam_grove | 2:3e7685cfb2a7 | 510 | * |
sam_grove | 2:3e7685cfb2a7 | 511 | * - EV_NETWORK event, Network Event state event handler |
sam_grove | 2:3e7685cfb2a7 | 512 | * - EV_INIT, Set Certificate Chain list, init multicast, Start net start if NVM have session |
sam_grove | 2:3e7685cfb2a7 | 513 | * - EV_DEBUG, Terminal handler |
sam_grove | 2:3e7685cfb2a7 | 514 | */ |
sam_grove | 2:3e7685cfb2a7 | 515 | void tasklet_main(arm_event_s *event) |
sam_grove | 2:3e7685cfb2a7 | 516 | { |
sam_grove | 2:3e7685cfb2a7 | 517 | if(event->sender == 0) |
sam_grove | 2:3e7685cfb2a7 | 518 | { |
sam_grove | 2:3e7685cfb2a7 | 519 | arm_library_event_type_e event_type; |
sam_grove | 2:3e7685cfb2a7 | 520 | event_type = (arm_library_event_type_e)event->event_type; |
sam_grove | 2:3e7685cfb2a7 | 521 | |
sam_grove | 2:3e7685cfb2a7 | 522 | switch(event_type) |
sam_grove | 2:3e7685cfb2a7 | 523 | { |
sam_grove | 2:3e7685cfb2a7 | 524 | case ARM_LIB_NWK_INTERFACE_EVENT: |
sam_grove | 2:3e7685cfb2a7 | 525 | /* Network Event state event handler */ |
sam_grove | 2:3e7685cfb2a7 | 526 | debug("Event: ARM_LIB_NWK_INTERFACE\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 527 | app_parse_network_event(event); |
sam_grove | 2:3e7685cfb2a7 | 528 | break; |
sam_grove | 2:3e7685cfb2a7 | 529 | |
sam_grove | 2:3e7685cfb2a7 | 530 | case ARM_LIB_TASKLET_INIT_EVENT: |
sam_grove | 2:3e7685cfb2a7 | 531 | /*Init event from stack at start-up*/ |
sam_grove | 2:3e7685cfb2a7 | 532 | debug("Event: ARM_LIB_TASKLET_INIT\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 533 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 534 | timer_sys_event(ACCELEROMETER_TIMER, ACCEL_SAMPLE_TIME); |
sam_grove | 2:3e7685cfb2a7 | 535 | #endif |
sam_grove | 2:3e7685cfb2a7 | 536 | multicast_set_parameters(10,0,20,3,75 ); |
sam_grove | 2:3e7685cfb2a7 | 537 | |
sam_grove | 2:3e7685cfb2a7 | 538 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 539 | /* open a socket to support control for REMOTE_NODE */ |
sam_grove | 2:3e7685cfb2a7 | 540 | ctrl_udp_socket = socket_open(SOCKET_UDP, SEND_CTRL_UDP_PORT, empty_udp_receive); |
sam_grove | 2:3e7685cfb2a7 | 541 | #endif |
sam_grove | 2:3e7685cfb2a7 | 542 | |
sam_grove | 2:3e7685cfb2a7 | 543 | net_6lowpan_id = arm_nwk_interface_init(NET_INTERFACE_RF_6LOWPAN, rf_phy_device_register_id, "6LoWPAN_BORDER_ROUTER"); |
sam_grove | 2:3e7685cfb2a7 | 544 | if(net_6lowpan_id < 0) |
sam_grove | 2:3e7685cfb2a7 | 545 | { |
sam_grove | 2:3e7685cfb2a7 | 546 | debug("Interface Generate Fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 547 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 548 | } |
sam_grove | 2:3e7685cfb2a7 | 549 | else |
sam_grove | 2:3e7685cfb2a7 | 550 | { |
sam_grove | 2:3e7685cfb2a7 | 551 | //SET Bootsrap |
sam_grove | 2:3e7685cfb2a7 | 552 | 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 | 553 | { |
sam_grove | 2:3e7685cfb2a7 | 554 | //Bootsrap SET fail |
sam_grove | 2:3e7685cfb2a7 | 555 | debug("Bootstrap Fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 556 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 557 | } |
sam_grove | 2:3e7685cfb2a7 | 558 | else |
sam_grove | 2:3e7685cfb2a7 | 559 | { |
sam_grove | 2:3e7685cfb2a7 | 560 | int8_t retval = -1; |
sam_grove | 2:3e7685cfb2a7 | 561 | 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 | 562 | arm_nwk_link_layer_security_mode(net_6lowpan_id, NET_SEC_MODE_NO_LINK_SECURITY, 0, 0); |
sam_grove | 2:3e7685cfb2a7 | 563 | arm_nwk_6lowpan_link_scan_paramameter_set(rf_phy_device_register_id, channel_list, 5); |
sam_grove | 2:3e7685cfb2a7 | 564 | retval = arm_nwk_interface_up(net_6lowpan_id); |
sam_grove | 2:3e7685cfb2a7 | 565 | if(retval != 0) |
sam_grove | 2:3e7685cfb2a7 | 566 | { |
sam_grove | 2:3e7685cfb2a7 | 567 | //6Lowpan Bootsrap start fail |
sam_grove | 2:3e7685cfb2a7 | 568 | debug("6LowPAN Bootstrap start Fail\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 569 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 570 | } |
sam_grove | 2:3e7685cfb2a7 | 571 | else |
sam_grove | 2:3e7685cfb2a7 | 572 | { |
sam_grove | 2:3e7685cfb2a7 | 573 | //6Lowpan Bootsrap start OK |
sam_grove | 2:3e7685cfb2a7 | 574 | debug("6LowPAN Bootstrap Start OK\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 575 | } |
sam_grove | 2:3e7685cfb2a7 | 576 | // open sockets |
sam_grove | 2:3e7685cfb2a7 | 577 | coap_udp_socket = socket_open(SOCKET_UDP, AUDP_SOCKET_PORT, NSDL_receive_socket); |
sam_grove | 2:3e7685cfb2a7 | 578 | // Start NSDL |
sam_grove | 2:3e7685cfb2a7 | 579 | nsdl_run(); |
sam_grove | 2:3e7685cfb2a7 | 580 | } |
sam_grove | 2:3e7685cfb2a7 | 581 | timer_sys_event(NSP_REGISTRATION_TIMER, RD_UPDATE_PERIOD); |
sam_grove | 0:01fb291427ce | 582 | } |
sam_grove | 2:3e7685cfb2a7 | 583 | break; |
sam_grove | 2:3e7685cfb2a7 | 584 | |
sam_grove | 2:3e7685cfb2a7 | 585 | case ARM_LIB_SYSTEM_TIMER_EVENT: |
sam_grove | 2:3e7685cfb2a7 | 586 | timer_sys_event_cancel(event->event_id); |
sam_grove | 2:3e7685cfb2a7 | 587 | if (event->event_id == NSP_REGISTRATION_TIMER) |
sam_grove | 2:3e7685cfb2a7 | 588 | { |
sam_grove | 2:3e7685cfb2a7 | 589 | printf("Time to register...\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 590 | NSP_registration(); |
sam_grove | 2:3e7685cfb2a7 | 591 | #if BATTERY |
sam_grove | 2:3e7685cfb2a7 | 592 | batteryLevel -= 10; |
sam_grove | 2:3e7685cfb2a7 | 593 | if (batteryLevel == 0) |
sam_grove | 2:3e7685cfb2a7 | 594 | batteryLevel = 100; |
sam_grove | 2:3e7685cfb2a7 | 595 | #endif |
sam_grove | 2:3e7685cfb2a7 | 596 | timer_sys_event(NSP_REGISTRATION_TIMER, RD_UPDATE_PERIOD); |
sam_grove | 2:3e7685cfb2a7 | 597 | } |
sam_grove | 2:3e7685cfb2a7 | 598 | else if (event->event_id == RETRY_TIMER) |
sam_grove | 2:3e7685cfb2a7 | 599 | { |
sam_grove | 2:3e7685cfb2a7 | 600 | debug("Event: ARM_LIB_SYSTEM_TIMER (event_id = 1)\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 601 | int8_t retval = -1; |
sam_grove | 2:3e7685cfb2a7 | 602 | retval = arm_nwk_interface_up(net_6lowpan_id); |
sam_grove | 2:3e7685cfb2a7 | 603 | if(retval != 0) |
sam_grove | 2:3e7685cfb2a7 | 604 | { |
sam_grove | 2:3e7685cfb2a7 | 605 | //6Lowpan Bootsrap start fail |
sam_grove | 2:3e7685cfb2a7 | 606 | debug("6LowPAN Bootstrap Start Failure\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 607 | while(1); |
sam_grove | 2:3e7685cfb2a7 | 608 | } |
sam_grove | 2:3e7685cfb2a7 | 609 | else |
sam_grove | 2:3e7685cfb2a7 | 610 | { |
sam_grove | 2:3e7685cfb2a7 | 611 | //6Lowpan Bootsrap start OK |
sam_grove | 2:3e7685cfb2a7 | 612 | debug("6LowPAN Bootstrap Start OK\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 613 | } |
sam_grove | 2:3e7685cfb2a7 | 614 | } |
sam_grove | 2:3e7685cfb2a7 | 615 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 616 | else if (event->event_id == ACCELEROMETER_TIMER) |
sam_grove | 2:3e7685cfb2a7 | 617 | { |
sam_grove | 2:3e7685cfb2a7 | 618 | if (accel.getAccX() < -0.85f && accel_enable) |
sam_grove | 2:3e7685cfb2a7 | 619 | { |
sam_grove | 2:3e7685cfb2a7 | 620 | accel_enable = 0; |
sam_grove | 2:3e7685cfb2a7 | 621 | update_LED(); |
sam_grove | 2:3e7685cfb2a7 | 622 | } |
sam_grove | 2:3e7685cfb2a7 | 623 | if (accel.getAccX() > -0.35f) |
sam_grove | 2:3e7685cfb2a7 | 624 | accel_enable = 1; |
sam_grove | 2:3e7685cfb2a7 | 625 | |
sam_grove | 2:3e7685cfb2a7 | 626 | timer_sys_event(ACCELEROMETER_TIMER, ACCEL_SAMPLE_TIME); |
sam_grove | 2:3e7685cfb2a7 | 627 | } |
sam_grove | 2:3e7685cfb2a7 | 628 | #endif |
sam_grove | 3:a38ad504a18c | 629 | |
sam_grove | 2:3e7685cfb2a7 | 630 | break; |
sam_grove | 2:3e7685cfb2a7 | 631 | |
sam_grove | 2:3e7685cfb2a7 | 632 | default: |
sam_grove | 2:3e7685cfb2a7 | 633 | break; |
sam_grove | 2:3e7685cfb2a7 | 634 | } |
sam_grove | 2:3e7685cfb2a7 | 635 | } |
sam_grove | 2:3e7685cfb2a7 | 636 | } |
sam_grove | 2:3e7685cfb2a7 | 637 | |
sam_grove | 2:3e7685cfb2a7 | 638 | void NSDL_receive_socket(void * cb) |
sam_grove | 2:3e7685cfb2a7 | 639 | { |
sam_grove | 2:3e7685cfb2a7 | 640 | socket_callback_t * cb_res =0; |
sam_grove | 2:3e7685cfb2a7 | 641 | int16_t length; |
sam_grove | 2:3e7685cfb2a7 | 642 | cb_res = (socket_callback_t *) cb; |
sam_grove | 2:3e7685cfb2a7 | 643 | uint8_t *payload; |
sam_grove | 2:3e7685cfb2a7 | 644 | |
sam_grove | 2:3e7685cfb2a7 | 645 | if(cb_res->event_type == SOCKET_DATA) |
sam_grove | 2:3e7685cfb2a7 | 646 | { |
sam_grove | 2:3e7685cfb2a7 | 647 | debug("LINK LQI:"); |
sam_grove | 2:3e7685cfb2a7 | 648 | debug_hex(cb_res->LINK_LQI); |
sam_grove | 2:3e7685cfb2a7 | 649 | create_lqi_resource(cb_res->LINK_LQI); |
sam_grove | 2:3e7685cfb2a7 | 650 | debug("\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 651 | |
sam_grove | 2:3e7685cfb2a7 | 652 | if ( cb_res->d_len > 0) |
sam_grove | 2:3e7685cfb2a7 | 653 | { |
sam_grove | 2:3e7685cfb2a7 | 654 | payload = (uint8_t *) own_alloc(cb_res->d_len); |
sam_grove | 2:3e7685cfb2a7 | 655 | if(payload) |
sam_grove | 2:3e7685cfb2a7 | 656 | { |
sam_grove | 2:3e7685cfb2a7 | 657 | //Read data to the RX buffer |
sam_grove | 2:3e7685cfb2a7 | 658 | length = socket_read(cb_res->socket_id, &app_src, payload, cb_res->d_len); //replace rx_buffer payload |
sam_grove | 2:3e7685cfb2a7 | 659 | if(length) |
sam_grove | 2:3e7685cfb2a7 | 660 | { |
sam_grove | 2:3e7685cfb2a7 | 661 | if(cb_res->socket_id == coap_udp_socket) |
sam_grove | 2:3e7685cfb2a7 | 662 | { |
sam_grove | 2:3e7685cfb2a7 | 663 | // Handles data received in UDP socket |
sam_grove | 2:3e7685cfb2a7 | 664 | // Call application protocol parser. |
sam_grove | 2:3e7685cfb2a7 | 665 | sn_addr_s.type = SN_NSDL_ADDRESS_TYPE_IPV6; |
sam_grove | 2:3e7685cfb2a7 | 666 | sn_addr_s.addr_len = 16; |
sam_grove | 2:3e7685cfb2a7 | 667 | sn_addr_s.port = app_src.identifier; |
sam_grove | 2:3e7685cfb2a7 | 668 | sn_addr_s.addr_ptr = app_src.address; |
sam_grove | 2:3e7685cfb2a7 | 669 | printf("Data 1\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 670 | if(sn_nsdl_process_coap(payload, length, &sn_addr_s)) // 0= ok, -1=failure |
sam_grove | 2:3e7685cfb2a7 | 671 | { |
sam_grove | 2:3e7685cfb2a7 | 672 | debug("Error processing CoAP packet\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 673 | } |
sam_grove | 2:3e7685cfb2a7 | 674 | printf("Data 4\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 675 | } |
sam_grove | 2:3e7685cfb2a7 | 676 | } |
sam_grove | 2:3e7685cfb2a7 | 677 | own_free(payload); |
sam_grove | 2:3e7685cfb2a7 | 678 | } |
sam_grove | 2:3e7685cfb2a7 | 679 | } |
sam_grove | 2:3e7685cfb2a7 | 680 | } |
sam_grove | 2:3e7685cfb2a7 | 681 | #if 1 // enabled for debug |
sam_grove | 2:3e7685cfb2a7 | 682 | else if(cb_res->event_type == SOCKET_TX_DONE) |
sam_grove | 2:3e7685cfb2a7 | 683 | { |
sam_grove | 2:3e7685cfb2a7 | 684 | //debug("*"); |
sam_grove | 2:3e7685cfb2a7 | 685 | } |
sam_grove | 2:3e7685cfb2a7 | 686 | else if(cb_res->event_type == SOCKET_NO_ROUTE) |
sam_grove | 2:3e7685cfb2a7 | 687 | { |
sam_grove | 2:3e7685cfb2a7 | 688 | debug("SOCKET_NO_ROUTE\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 689 | } |
sam_grove | 2:3e7685cfb2a7 | 690 | else if(cb_res->event_type == SOCKET_TX_FAIL) |
sam_grove | 2:3e7685cfb2a7 | 691 | { |
sam_grove | 2:3e7685cfb2a7 | 692 | debug("SOCKET_TX_FAIL\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 693 | } |
sam_grove | 2:3e7685cfb2a7 | 694 | #endif |
sam_grove | 2:3e7685cfb2a7 | 695 | } |
sam_grove | 2:3e7685cfb2a7 | 696 | |
sam_grove | 2:3e7685cfb2a7 | 697 | #if NODE_CONTROLLER |
sam_grove | 2:3e7685cfb2a7 | 698 | void ctrl_udp_send(uint8_t cmd) |
sam_grove | 2:3e7685cfb2a7 | 699 | { |
sam_grove | 2:3e7685cfb2a7 | 700 | uint8_t * payload = 0; |
sam_grove | 2:3e7685cfb2a7 | 701 | ns_address_t address; |
sam_grove | 2:3e7685cfb2a7 | 702 | /*SET UDP echo Port*/ |
sam_grove | 2:3e7685cfb2a7 | 703 | address.identifier = RECV_CTRL_UDP_PORT; |
sam_grove | 2:3e7685cfb2a7 | 704 | |
sam_grove | 2:3e7685cfb2a7 | 705 | uint8_t node_address[16] = REMOTE_NODE; |
sam_grove | 2:3e7685cfb2a7 | 706 | |
sam_grove | 2:3e7685cfb2a7 | 707 | payload = (uint8_t *) ns_dyn_mem_alloc(1); |
sam_grove | 2:3e7685cfb2a7 | 708 | if(payload) |
sam_grove | 2:3e7685cfb2a7 | 709 | { |
sam_grove | 2:3e7685cfb2a7 | 710 | uint8_t *ptr = payload; |
sam_grove | 2:3e7685cfb2a7 | 711 | |
sam_grove | 2:3e7685cfb2a7 | 712 | memcpy(address.address,node_address,16); |
sam_grove | 2:3e7685cfb2a7 | 713 | address.type = ADDRESS_IPV6; |
sam_grove | 2:3e7685cfb2a7 | 714 | *ptr = cmd; |
sam_grove | 2:3e7685cfb2a7 | 715 | if(socket_sendto(ctrl_udp_socket, &address, payload, (1)) != 0) |
sam_grove | 2:3e7685cfb2a7 | 716 | { |
sam_grove | 2:3e7685cfb2a7 | 717 | debug("Ctrl UDP Failed\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 718 | } |
sam_grove | 2:3e7685cfb2a7 | 719 | else |
sam_grove | 2:3e7685cfb2a7 | 720 | { |
sam_grove | 2:3e7685cfb2a7 | 721 | debug("Ctrl UDP Ok\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 722 | } |
sam_grove | 2:3e7685cfb2a7 | 723 | ns_dyn_mem_free(payload); |
sam_grove | 2:3e7685cfb2a7 | 724 | } |
sam_grove | 2:3e7685cfb2a7 | 725 | else |
sam_grove | 2:3e7685cfb2a7 | 726 | { |
sam_grove | 2:3e7685cfb2a7 | 727 | debug("No Heap for Ctrl UDP\r\n"); |
sam_grove | 2:3e7685cfb2a7 | 728 | } |
sam_grove | 2:3e7685cfb2a7 | 729 | } |
sam_grove | 2:3e7685cfb2a7 | 730 | // we don't expect to receive data |
sam_grove | 2:3e7685cfb2a7 | 731 | void empty_udp_receive(void * cb) { |
sam_grove | 2:3e7685cfb2a7 | 732 | |
sam_grove | 2:3e7685cfb2a7 | 733 | } |
sam_grove | 2:3e7685cfb2a7 | 734 | #endif |
sam_grove | 2:3e7685cfb2a7 | 735 |