1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* @author Ricardo Severino <rars@isep.ipp.pt>
* END DEVICE
*
*/
#include <Timer.h>
#include "printfUART.h"
#include "log_enable.h"
#ifdef APP_PRINTFS_ENABLED
#define lclPrintf printfUART
#define lclprintfUART_init printfUART_init
#else
#define lclPrintf(__format, __args...)
void lclprintfUART_init() {}
#endif
module end_deviceBasicC
{
uses {
interface Boot;
interface Leds;
interface NLDE_DATA;
//NLME NWK Management services
interface NLME_NETWORK_DISCOVERY;
interface NLME_JOIN;
interface NLME_LEAVE;
interface NLME_SYNC;
interface NLME_RESET;
interface NLME_GET;
interface NLME_SET;
//Timers
interface Timer<TMilli> as T_init;
interface Timer<TMilli> as KeepAliveTimer;
interface Timer<TMilli> as NetAssociationDeferredTimer;
#if defined(PLATFORM_TELOSB)
//user button
interface Get<button_state_t>;
interface Notify<button_state_t>;
#endif
}
}
implementation
{
// Depth by configuration
uint8_t myDepth;
//boolean variable definig if the device has joined to the PAN
uint8_t joined;
// Maximum number of join trials before restart from network discovery
uint8_t maxJoinTrials;
uint16_t myParentAddress;
task void KeepAlive();
task void KeepAlive()
{
uint8_t nsdu_pay[6];
nsdu_pay[0]=TOS_NODE_ID & 0x00FF;
nsdu_pay[1]='H';
nsdu_pay[2]='e';
nsdu_pay[3]='l';
nsdu_pay[4]='l';
nsdu_pay[5]='o';
// Send the message towards the coordinator
// (default network address: 0x0000)
call NLDE_DATA.request(0x0000, 6, nsdu_pay, 0, 1, 0x00, 0);
}
// This function initializes the variables.
void initVariables()
{
// Depth by configuration (initialize to default)
myDepth = DEF_DEVICE_DEPTH;
//boolean variable definig if the device has joined to the PAN
joined = 0x00;
// Maximum number of join trials before restart from network discovery
maxJoinTrials = MAX_JOIN_TRIALS;
}
event void Boot.booted()
{
printfUART_init();
initVariables();
#if defined(PLATFORM_TELOSB)
call Notify.enable();
#endif
// Start the application
call NLME_RESET.request();
}
/*****************************************************
****************NLDE EVENTS***************************
******************************************************/
/*************************NLDE_DATA*****************************/
event error_t NLDE_DATA.confirm(uint8_t NsduHandle, uint8_t Status)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLDE_DATA.confirm\n", "");
#endif
if (joined != 0x00)
call Leds.led1Toggle();
return SUCCESS;
}
event error_t NLDE_DATA.indication(
uint16_t SrcAddress,
uint8_t NsduLength,
uint8_t Nsdu[120],
uint16_t LinkQuality)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLDE_DATA.indication\n", "");
#endif
return SUCCESS;
}
/*****************************************************
****************NLME EVENTS***************************
******************************************************/
/*****************NLME_NETWORK_DISCOVERY**************************/
// This is not called anymore by the NKWP since it tries to associate
// directly to the parent and issuing a JOIN confirm, instead
event error_t NLME_NETWORK_DISCOVERY.confirm(uint8_t NetworkCount,networkdescriptor networkdescriptorlist[], uint8_t Status)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_NETWORK_DISCOVERY.confirm\n", "");
#endif
return SUCCESS;
}
/*************************NLME_JOIN*****************************/
event error_t NLME_JOIN.indication(uint16_t ShortAddress, uint32_t ExtendedAddress[], uint8_t CapabilityInformation, bool SecureJoin)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_JOIN.indication\n", "");
#endif
return SUCCESS;
}
event error_t NLME_JOIN.confirm(uint16_t PANId, uint8_t Status, uint16_t parentAddress)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_JOIN.confirm\n", "");
#endif
switch(Status)
{
case NWK_SUCCESS:
// Join procedure successful
joined = 0x01;
myParentAddress=parentAddress;
call KeepAliveTimer.startPeriodic(10000);
break;
case NWK_NOT_PERMITTED:
joined = 0x00;
//join failed
break;
case NWK_STARTUP_FAILURE:
joined = 0x00;
maxJoinTrials--;
if (maxJoinTrials == 0)
{
// Retry restarting from the network discovery phase
call T_init.startOneShot(5000);
}
else
{
// Retry after a few seconds
call NetAssociationDeferredTimer.startOneShot(JOIN_TIMER_RETRY);
}
break;
default:
//default procedure - join failed
joined = 0x00;
break;
}
return Status;
}
/*************************NLME_LEAVE****************************/
event error_t NLME_LEAVE.indication(uint64_t DeviceAddress)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_LEAVE.indication\n", "");
#endif
return SUCCESS;
}
event error_t NLME_LEAVE.confirm(uint64_t DeviceAddress, uint8_t Status)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_LEAVE.confirm\n", "");
#endif
joined=0x00;
return SUCCESS;
}
/*************************NLME_SYNC*****************************/
event error_t NLME_SYNC.indication()
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_SYNC.indication\n", "");
#endif
// We lost connection with our parent. Automatic rescan is done
// at the NWK layer, unless it is after a disassociation request
joined=0x00;
// Stop the keep alive timer, if it is still running
if (call KeepAliveTimer.isRunning())
call KeepAliveTimer.stop();
// Switch off all leds
call Leds.led0Off();
call Leds.led1Off();
call Leds.led2Off();
return SUCCESS;
}
event error_t NLME_SYNC.confirm(uint8_t Status)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_SYNC.confirm\n", "");
#endif
return SUCCESS;
}
/***************** NLME-SET ********************/
event error_t NLME_SET.confirm(uint8_t Status, uint8_t NIBAttribute)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_SET.confirm\n", "");
#endif
return SUCCESS;
}
/***************** NLME-GET ********************/
event error_t NLME_GET.confirm(uint8_t Status, uint8_t NIBAttribute, uint16_t NIBAttributeLength, uint16_t NIBAttributeValue)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_GET.confirm\n", "");
#endif
return SUCCESS;
}
event error_t NLME_RESET.confirm(uint8_t status)
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("NLME_RESET.confirm\n", "");
#endif
call T_init.startOneShot(2000);
return SUCCESS;
}
/*****************************************************
****************TIMER EVENTS***************************
******************************************************/
/*******************T_init**************************/
event void T_init.fired()
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("I'm NOT the coordinator\n", "");
#endif
call NLME_NETWORK_DISCOVERY.request(LOGICAL_CHANNEL, BEACON_ORDER);
return;
}
/*******************NetAssociationDeferredTimer**************************/
event void NetAssociationDeferredTimer.fired()
{
#if (LOG_LEVEL & TRACE_FUNC)
lclPrintf("go join as end device\n", "");
#endif
call NLME_JOIN.request(MAC_PANID, FALSE, FALSE, 0, 0, 0, 0, 0);
return;
}
/*******************KeepAlive**************************/
event void KeepAliveTimer.fired()
{
post KeepAlive();
}
#if defined(PLATFORM_TELOSB)
event void Notify.notify(button_state_t state)
{
if (state == BUTTON_PRESSED && joined)
{
call KeepAliveTimer.stop();
call NLME_LEAVE.request(0,0,0);
}
}
#endif
}
|