/** * \file task.c * * \brief Implementation of Tasks for LoRa Demo Application on MiWi P2P * * Copyright (c) 2019 Microchip Technology Inc. and its subsidiaries. * * \asf_license_start * * \page License * * Subject to your compliance with these terms, you may use Microchip * software and any derivatives exclusively with Microchip products. * It is your responsibility to comply with third party license terms applicable * to your use of third party software (including open source software) that * may accompany Microchip software. * * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. * * \asf_license_stop * */ /***********************Headers***************************************/ #include "task.h" #include "miwi_api.h" #include "p2p_demo.h" #include "asf.h" #if defined(ENABLE_SLEEP_FEATURE) #include "sleep_mgr.h" #endif #include "sw_timer.h" #include "phy.h" /************************** VARIABLES ************************************/ #define LIGHT 0x01 #define SWITCH 0x02 // Software Timer used for Tx timeout uint8_t TxTimerId = 0; #if defined(PERIODIC_TX) uint8_t PeriodicTxTimerId = 0xFF ; #endif /*************************************************************************/ // AdditionalNodeID variable array defines the additional // information to identify a device on a PAN. This array // will be transmitted when initiate the connection between // the two devices. This variable array will be stored in // the Connection Entry structure of the partner device. The // size of this array is ADDITIONAL_NODE_ID_SIZE, defined in // miwi_config.h. // In this demo, this variable array is set to be empty. /*************************************************************************/ #if ADDITIONAL_NODE_ID_SIZE > 0 uint8_t AdditionalNodeID[ADDITIONAL_NODE_ID_SIZE] = {LIGHT}; #endif /*************************************************************************/ // The variable myChannel defines the channel that the device // is operate on. This variable will be only effective if energy scan // (ENABLE_ED_SCAN) is not turned on. Once the energy scan is turned // on, the operating channel will be one of the channels available with // least amount of energy (or noise). /*************************************************************************/ uint8_t myChannel = 1; /* Range for default configuration: 1 to 10 Note: TX Power and PHY Mode Setting needs to be modified as per the recommendation from Data Sheet for European band (ie.,Channel 0)*/ static void Connection_Confirm(miwi_status_t status); #ifdef ENABLE_ACTIVE_SCAN void Scan_Confirm(uint8_t ActiveScanResultCount, void* PtrActiveScanResults); #endif // #ifdef ENABLE_ACTIVE_SCAN bool freezer_feature(void) { MIWI_TICK tick1, tick2; uint8_t switch_val; tick1.Val = MiWi_TickGet(); while(1) { tick2.Val = MiWi_TickGet(); if(MiWi_TickGetDiff(tick2, tick1) > (ONE_SECOND * 4)) break; switch_val = ButtonPressed (); if(switch_val == 1) { #if defined (ENABLE_LCD) LCDDisplay((char *)"Restoring Network !!", 0, false); delay_ms(1000); #endif return true; } else { return false; } } return false; } #ifdef ENABLE_ACTIVE_SCAN void Scan_Confirm(uint8_t ActiveScanResultCount, void* PtrActiveScanResults) { ACTIVE_SCAN_RESULT* ActiveScanResult = (ACTIVE_SCAN_RESULT*) PtrActiveScanResults; #if defined (ENABLE_CONSOLE) for(uint8_t count =0; count <= ActiveScanResultCount-1; count++ ) { printf("\nID\tDevice Address \t\tCapability \tPAN ID \tChannel \n"); printf("%02x",count+1); printf("\t"); for(uint8_t j = 0; j < 8; j++) { if( j < MY_ADDRESS_LENGTH ) { printf("%02x",ActiveScanResult[count].Address[MY_ADDRESS_LENGTH-1-j] ); } else { printf(" "); } } printf("\t"); printf("%02x",(ActiveScanResult[count].Capability.Val)); printf("\t\t"); printf("%02x",(ActiveScanResult[count].PANID.Val)); printf("\t"); printf("%02x",ActiveScanResult[count].Channel); printf("\r\n"); } printf("\r\n Scan Operation Status: %d device found",ActiveScanResultCount); #endif // #if defined (ENABLE_CONSOLE) } #endif // #ifdef ENABLE_ACTIVE_SCAN static void Connection_Confirm(miwi_status_t status) { #if defined (ENABLE_CONSOLE) printf("\r\nConnect Operation Status: %d\n", status) ; #endif // #if defined (ENABLE_CONSOLE) if ((SUCCESS == status) || (ALREADY_EXISTS == status)) { #if defined (ENABLE_CONSOLE) printf("\r\nStarted Wireless Communication on Channel %u\r\n", currentChannel) ; //DumpConnection(0xFF) ; #endif // #if defined (ENABLE_CONSOLE) #if defined(PERIODIC_TX) SwTimerCreate(&PeriodicTxTimerId) ; SwTimerStart (PeriodicTxTimerId, MS_TO_US(1000), 0/*SW_TIMEOUT_RELATIVE*/, (void *)PeriodicTxCallback, NULL) ; #endif // #if defined(PERIODIC_TX) } } bool Initialize_Demo(bool freezer_enable) { uint8_t i; bool invalidIEEEAddrFlag = false; uint64_t invalidIEEEAddr; MiApp_SubscribeDataIndicationCallback(ReceivedDataIndication); #ifdef ENABLE_SLEEP_FEATURE sm_init(); #endif if (freezer_enable) { MiApp_ProtocolInit(NULL, NULL); srand(PHY_RandomReq()); /* Check if a valid IEEE address is available. 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF is presumed to be invalid */ /* Check if IEEE address is 0x0000000000000000 */ memset((uint8_t *)&invalidIEEEAddr, 0x00, LONG_ADDR_LEN); if (0 == memcmp((uint8_t *)&invalidIEEEAddr, (uint8_t *)&myLongAddress, LONG_ADDR_LEN)) { invalidIEEEAddrFlag = true; } /* Check if IEEE address is 0xFFFFFFFFFFFFFFFF */ memset((uint8_t *)&invalidIEEEAddr, 0xFF, LONG_ADDR_LEN); if (0 == memcmp((uint8_t *)&invalidIEEEAddr, (uint8_t *)&myLongAddress, LONG_ADDR_LEN)) { invalidIEEEAddrFlag = true; } if (invalidIEEEAddrFlag) { /* * In case no valid IEEE address is available, a random * IEEE address will be generated to be able to run the * applications for demonstration purposes. * In production code this can be omitted. */ uint8_t* peui64 = (uint8_t *)&myLongAddress; for(i = 0; i