/*---------------------------------------------------------------------------*/ static unsigned getreg(enum cc2420_register regname) { unsigned reg; CC2420_READ_REG(regname, reg); return reg; }
/*---------------------------------------------------------------------------*/ void cc2420_aes_cipher(uint8_t *data, int len, int key_index) { int i; uint16_t secctrl0; CC2420_READ_REG(CC2420_SECCTRL0, secctrl0); secctrl0 &= ~(CC2420_SECCTRL0_SAKEYSEL0 | CC2420_SECCTRL0_SAKEYSEL1); switch(key_index) { case 0: secctrl0 |= CC2420_SECCTRL0_SAKEYSEL0; break; case 1: secctrl0 |= CC2420_SECCTRL0_SAKEYSEL1; break; } CC2420_WRITE_REG(CC2420_SECCTRL0, secctrl0); for(i = 0; i < len; i = i + MAX_DATALEN) { cipher16(data + i, MIN(len - i, MAX_DATALEN)); } }
PROCESS_THREAD(scanning, ev, data) { PROCESS_BEGIN(); // Initial operations leds_off(LEDS_ALL); watchdog_stop(); // Avoiding wrong RSSI readings unsigned temp; CC2420_READ_REG(CC2420_AGCTST1, temp); CC2420_WRITE_REG(CC2420_AGCTST1, (temp + (1 << 8) + (1 << 13))); // Selecting the channel SPI_SETCHANNEL_SUPERFAST(357+((CHANNEL-11)*5)); // Avoiding the initial wrong readings by discarding the wrong readings CC2420_SPI_ENABLE(); unsigned long k=0; for (k=0; k<=15; k++) {MY_FASTSPI_GETRSSI(temp);} CC2420_SPI_DISABLE(); static struct etimer et; while(1){ #if VERBOSE printf("#START (dBm: occurrencies)\n"); #endif // Resetting everything for(k=0;k<BUFFER_SIZE;k++){ buffer0[k] = 0; } dint(); // Disable interrupts boost_cpu(); // Temporarily boost CPU speed CC2420_SPI_ENABLE(); // Enable SPI // Actual scanning static signed char rssi; for(k=0; k<MAX_VALUE; k++){ MY_FASTSPI_GETRSSI(rssi); buffer0[rssi+55]++; } CC2420_SPI_DISABLE(); // Disable SPI restore_cpu(); // Restore CPU speed eint(); // Re-enable interrupts // Printing the stored values in compressed form unsigned long sum_cca = 0; unsigned long max = 0, max_value = 0; for(temp=0; temp<BUFFER_SIZE; temp++) { sum_cca += (temp * buffer0[temp]); if(buffer0[temp] > max){ max = buffer0[temp]; max_value = temp; } } // Printing the results of the CCA float f_cca = (((float) sum_cca*1.0000) / MAX_VALUE)-100.0000; #if VERBOSE printf("Average noise: %ld.%04u\nStatistic Mode noise: %ld\n", (long) f_cca, (unsigned)((f_cca-floor(f_cca))*10000), max_value-100); #else printf("%ld.%04u\n", (long) f_cca, (unsigned)((f_cca-floor(f_cca))*10000)); #endif #if VERBOSE printf("#END\n"); #endif // Waiting for timer etimer_set(&et, PERIOD_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); } PROCESS_WAIT_EVENT(); PROCESS_END(); }
PROCESS_THREAD(scanning, ev, data) { PROCESS_BEGIN(); // Initial operations leds_off(LEDS_ALL); watchdog_stop(); // Avoiding wrong RSSI readings unsigned temp; CC2420_READ_REG(CC2420_AGCTST1, temp); CC2420_WRITE_REG(CC2420_AGCTST1, (temp + (1 << 8) + (1 << 13))); // Selecting the channel SPI_SETCHANNEL_SUPERFAST(357+((CHANNEL-11)*5)); // Avoiding the initial wrong readings by discarding the wrong readings CC2420_SPI_ENABLE(); int k=0; for (k=0; k<=15; k++) { MY_FASTSPI_GETRSSI(temp); } CC2420_SPI_DISABLE(); static struct etimer et; while(1) { // Resetting everything for(k=0; k<(BUFFER_SIZE/2); k++) { buffer1[k] = 0; buffer0[k] = 0; } printf("#START [dBm: occurrencies]\n"); dint(); // Disable interrupts boost_cpu(); // Temporarily boost CPU speed CC2420_SPI_ENABLE(); // Enable SPI // Actual scanning static signed char rssi; int current = 0; int previous = 0; int cnt = 1; for(k=0; k<(BUFFER_SIZE/2);) { // Sample the RSSI fast MY_FASTSPI_GETRSSI(rssi); current = rssi + 55; if((current == previous)&&(cnt<255)) { cnt++; } else { buffer0[k] = previous; buffer1[k++] = cnt; cnt = 1; previous = current; } } CC2420_SPI_DISABLE(); // Disable SPI restore_cpu(); // Restore CPU speed eint(); // Re-enable interrupts // Printing the stored values in compressed form for(temp=0; temp<(BUFFER_SIZE/2); temp++) { printf("%d: %d\n",buffer0[temp] - 100, buffer1[temp]); clock_delay(30000); } printf("#END\n"); // Waiting for timer etimer_set(&et, PERIOD_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); } PROCESS_WAIT_EVENT(); PROCESS_END(); }