Back to Low Power Networking sample application.
00001 00017 #ifdef _ENDDEVICE_ 00018 #include <lowpower.h> 00019 00020 /*********************************************************************************** 00021 Global variables 00022 ***********************************************************************************/ 00023 /*********************************************************************************** 00024 Local variables 00025 ***********************************************************************************/ 00026 static AppDataTransmissionState_t appDataTtransmissionState = APP_DATA_TRANSMISSION_IDLE_STATE; 00027 static APS_DataReq_t apsDataReq; // APS Data Request primitive (for application message sending) 00028 static ZDO_SleepReq_t zdoSleepReq; // Request parameters for stack sleep 00029 static ZDO_WakeUpReq_t zdoWakeUpReq; // Request parameters for stack awakening 00030 static AppMessageBuffer_t appMessageBuffer; // Application message buffer 00031 00032 00033 /*********************************************************************************** 00034 Static functions 00035 ***********************************************************************************/ 00036 static void ZDO_SleepConf(ZDO_SleepConf_t *conf); // Sleep confirmation handler 00037 static void ZDO_WakeUpConf(ZDO_WakeUpConf_t *conf); // Wake up confirmation handler 00038 static void APS_DataConf(APS_DataConf_t *conf); // Data transmission confirmation handler 00039 00040 static void temperaturesSensorHandler(bool result, int16_t temperature); 00041 static void openPeriphery(void); // Open LEDs and Temperature Sensor 00042 static void closePeriphery(void); // Close LEDs and Temperature Sensor 00043 static void sendMessage(void); // Send the application message 00044 static void prepareToSleep(void); 00045 00046 00047 /******************************************************************** 00048 Description: End device initialization routine 00049 Parameters: none 00050 Returns: none 00051 ********************************************************************/ 00052 void appEndDeviceInit(void) 00053 { 00054 // Prepare APS Data Request 00055 apsDataReq.dstAddrMode = APS_SHORT_ADDRESS; // Short addressing mode 00056 apsDataReq.dstAddress.shortAddress = 0; // Destination node short address 00057 apsDataReq.dstEndpoint = APP_ENDPOINT; // Destination endpoint 00058 apsDataReq.profileId = APP_PROFILE_ID; // Profile ID 00059 apsDataReq.clusterId = APP_CLUSTER_ID; // Destination cluster ID 00060 apsDataReq.srcEndpoint = APP_ENDPOINT; // Source endpoint 00061 apsDataReq.asduLength = sizeof (AppMessage_t); // ASDU size 00062 apsDataReq.asdu = (uint8_t *) &appMessageBuffer.message; // ASDU pointer as an application message 00063 apsDataReq.txOptions.acknowledgedTransmission = 1; // Acknowledged transmission enabled 00064 apsDataReq.radius = 0; // Default radius 00065 apsDataReq.APS_DataConf = APS_DataConf; // Confirm handler 00066 00067 BSP_OpenTemperatureSensor(); 00068 } 00069 00070 /******************************************************************** 00071 Description: Device common task handler 00072 Parameters: none 00073 Returns: none 00074 ********************************************************************/ 00075 void appEndDeviceTaskHandler(void) 00076 { 00077 switch (appDeviceState) // Actual device state when one joined network 00078 { 00079 case DEVICE_ACTIVE_IDLE_STATE: // Device ready to temperature measuring 00080 #ifdef _TEMPERATURE_SENSOR_ 00081 BSP_ReadTemperatureData(temperaturesSensorHandler); // Temperature measuring 00082 #else 00083 temperaturesSensorHandler(false, 0); 00084 #endif // _TEMPERATURE_SENSOR_ 00085 break; 00086 00087 case DEVICE_MESSAGE_SENDING_STATE: // Message sending state 00088 sendMessage(); // Application message sending 00089 break; 00090 00091 case DEVICE_SLEEP_PREPARE_STATE: // Prepare to sleep state 00092 if (appReadButtonsState() & BSP_KEY1) 00093 SYS_PostTask(APL_TASK_ID); // Still in current state. 00094 else 00095 prepareToSleep(); // Prepare to sleep 00096 break; 00097 00098 case DEVICE_AWAKENING_STATE: // Awakening state 00099 zdoWakeUpReq.ZDO_WakeUpConf = ZDO_WakeUpConf; // ZDO WakeUp confirm handler defining 00100 ZDO_WakeUpReq(&zdoWakeUpReq); // ZDO WakeUp Request sending 00101 break; 00102 00103 default: 00104 break; 00105 } 00106 } 00107 00108 /******************************************************************** 00109 Description: Data intication handler 00110 Parameters: ind - APS Data Indication primitive 00111 Returns: none 00112 ********************************************************************/ 00113 void appEndDeviceDataInd(APS_DataInd_t* ind) 00114 { 00115 ind = ind; // Warning prevention 00116 00117 // Data received indication 00118 appToggleLed(APP_RECEIVING_STATUS_LED); 00119 } 00120 00121 /******************************************************************** 00122 Description: Open LEDs and Sensor 00123 Parameters: none 00124 Returns: none 00125 ********************************************************************/ 00126 static void openPeriphery(void) 00127 { 00128 appOpenLeds(); 00129 00130 #ifdef _TEMPERATURE_SENSOR_ 00131 BSP_OpenTemperatureSensor(); 00132 #endif // _TEMPERATURE_SENSOR_ 00133 } 00134 00135 /******************************************************************** 00136 Description: Close LEDs and Sensor 00137 Parameters: none 00138 Returns: none 00139 *******************************************************************/ 00140 static void closePeriphery(void) 00141 { 00142 appOffLed(APP_NETWORK_STATUS_LED); 00143 appOffLed(APP_SENDING_STATUS_LED); 00144 appOffLed(APP_RECEIVING_STATUS_LED); 00145 appCloseLeds(); 00146 00147 BSP_CloseTemperatureSensor(); 00148 } 00149 00150 /******************************************************************** 00151 Description: Data sent handler 00152 Parameters: conf - APS Data Confirm primitive 00153 Returns: none 00154 ********************************************************************/ 00155 static void APS_DataConf(APS_DataConf_t *conf) 00156 { 00157 appDataTtransmissionState = APP_DATA_TRANSMISSION_IDLE_STATE; // Data transmission entity is idle 00158 00159 appOffLed(APP_SENDING_STATUS_LED); 00160 00161 if (APS_SUCCESS_STATUS == conf->status) // Data transmission was successfully performed 00162 { 00163 appDeviceState = DEVICE_SLEEP_PREPARE_STATE; // Switch device state to prepare for sleep 00164 } 00165 else 00166 { 00167 appDeviceState = DEVICE_ACTIVE_IDLE_STATE; // Data transmission wasn't successfully finished. Retry. 00168 } 00169 00170 SYS_PostTask(APL_TASK_ID); 00171 } 00172 00173 00174 /******************************************************************** 00175 Description: Temperature measured handler 00176 Parameters: result - measurement status (true - success, 0 - fail) 00177 temperature - value measured 00178 Returns: none 00179 ********************************************************************/ 00180 static void temperaturesSensorHandler(bool result, int16_t temperature) 00181 { 00182 // Temperature measured will be sent as an application message 00183 if (true == result) 00184 appMessageBuffer.message.temperature = temperature; 00185 else 00186 appMessageBuffer.message.temperature = 0; 00187 00188 appDeviceState = DEVICE_MESSAGE_SENDING_STATE; //Switch device state to application message sending 00189 SYS_PostTask(APL_TASK_ID); // Application task posting 00190 } 00191 00192 /******************************************************************** 00193 Description: Send the application message 00194 Parameters: none 00195 Returns: none 00196 ********************************************************************/ 00197 static void sendMessage(void) 00198 { 00199 if (APP_DATA_TRANSMISSION_IDLE_STATE == appDataTtransmissionState) // If previous data transmission was finished 00200 { 00201 appDataTtransmissionState = APP_DATA_TRANSMISSION_BUSY_STATE; // Data transmission entity is busy while sending not finished 00202 APS_DataReq(&apsDataReq); 00203 appOnLed(APP_SENDING_STATUS_LED); 00204 } 00205 } 00206 00207 00208 /******************************************************************** 00209 Description: ZDO Sleep Confirm handler 00210 Parameters: conf - ZDO Sleep Confirm primitive 00211 Returns: none 00212 ********************************************************************/ 00213 static void ZDO_SleepConf(ZDO_SleepConf_t *conf) 00214 { 00215 if (ZDO_SUCCESS_STATUS == conf->status) // Stack was slept successfully 00216 { 00217 closePeriphery(); // LEDs and Temperature Sensor closing 00218 appDeviceState = DEVICE_SLEEP_STATE; // Device actually slept 00219 } 00220 else 00221 SYS_PostTask(APL_TASK_ID); // Still in current state. 00222 // Application task posting for attempt repeat. 00223 } 00224 00225 /******************************************************************** 00226 Description: Prepare to sleep 00227 Parameters: none 00228 Returns: none 00229 ********************************************************************/ 00230 static void prepareToSleep(void) 00231 { 00232 zdoSleepReq.ZDO_SleepConf = ZDO_SleepConf; // Sleep Confirm handler defining 00233 ZDO_SleepReq(&zdoSleepReq); // Sleep Request sending 00234 } 00235 00236 /******************************************************************** 00237 Description: Device wakeup handler. 00238 Parameters: none 00239 Returns: none 00240 ********************************************************************/ 00241 static void wakeUpHandler(void) 00242 { 00243 appDeviceState = DEVICE_ACTIVE_IDLE_STATE; 00244 00245 openPeriphery(); 00246 00247 // Turn network indication on 00248 appOnLed(APP_NETWORK_STATUS_LED); 00249 00250 SYS_PostTask(APL_TASK_ID); 00251 } 00252 00253 /******************************************************************** 00254 Description: End device wake up indication 00255 00256 Parameters: none. 00257 00258 Returns: nothing. 00259 ********************************************************************/ 00260 void ZDO_WakeUpInd(void) 00261 { 00262 if (DEVICE_SLEEP_STATE == appDeviceState) 00263 wakeUpHandler(); 00264 } 00265 00266 /******************************************************************** 00267 Description: Wake up confirmation handler 00268 00269 Parameters: conf - confirmation parameters 00270 00271 Returns: nothing. 00272 ********************************************************************/ 00273 void ZDO_WakeUpConf(ZDO_WakeUpConf_t *conf) 00274 { 00275 if (ZDO_SUCCESS_STATUS == conf->status) 00276 wakeUpHandler(); 00277 else 00278 SYS_PostTask(APL_TASK_ID); 00279 } 00280 00281 #endif // _ENDDEVICE_ 00282 00283 // eof enddevice.c