Thread UART

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by samuel belete

Committer:
sam_grove
Date:
Thu Mar 05 15:47:08 2015 +0000
Revision:
2:3e7685cfb2a7
Parent:
1:6055c6873f85
Child:
3:a38ad504a18c
Updating to techcon demo program

Who changed what in which revision?

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