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