📄 common.c
字号:
// *******************************************************************// common.c//// common functions for sensor sample app//// Copyright 2005 by Ember Corporation. All rights reserved. *80*// *******************************************************************#include "app/sensor/common.h"#ifdef SINK_APPextern int16u ticksSinceLastHeard[];#endif#ifdef SENSOR_APPvoid printDataMode(void);#endifvoid setSecurityKey(void){ EmberStatus status; int8u securityKey[EMBER_ENCRYPTION_KEY_SIZE] = {'e','m','b','e','r',' ','E','M','2','5','0',' ','c','h','i','p'}; // "ember EM250 chip"; int8u keySequenceNumber = 1; int8u i; // set the key status = emberSetEncryptionKey(securityKey, keySequenceNumber); // print error if necessary if (status != EMBER_SUCCESS) { emberSerialPrintf(APP_SERIAL, "ERROR: 0x%x setting security key\r\n", status); } else { emberSerialPrintf(APP_SERIAL, "INFO: security key set to:"); for (i=0; i<EMBER_ENCRYPTION_KEY_SIZE; i++) { emberSerialPrintf(APP_SERIAL, "%x ", securityKey[i]); emberSerialWaitSend(APP_SERIAL); } emberSerialPrintf(APP_SERIAL, "\r\n"); }}// *********************************// print info about this node// *********************************void printNodeInfo(void) { int8u channel; int8u power; int16u panId; channel = emberGetRadioChannel(); power = emberGetRadioPower(); panId = emberGetPanId();#ifdef SINK_APP emberSerialPrintf(APP_SERIAL, "sink ");#else emberSerialPrintf(APP_SERIAL, "sensor ");#endif emberSerialPrintf(APP_SERIAL, "eui ["); printEUI64(APP_SERIAL, (EmberEUI64*)emberGetEui64()); emberSerialPrintf(APP_SERIAL, "] short ID [%2x]\r\n", emberGetNodeId()); emberSerialWaitSend(APP_SERIAL); if (emberNetworkState() == EMBER_JOINED_NETWORK) { emberSerialPrintf(APP_SERIAL, "channel [0x%x], power [0x%x], panId [0x%2x]", channel, power, panId); } else { emberSerialPrintf(APP_SERIAL, "channel [N/A], power [N/A], panId [N/A]"); } emberSerialPrintf(APP_SERIAL, ", stack [%2x], app [2.60]\r\n", SOFTWARE_VERSION); emberSerialWaitSend(APP_SERIAL); emberSerialPrintf(APP_SERIAL, "security level [%x], secure join [%x]\r\n", EMBER_SECURITY_LEVEL, USE_KEY_WHEN_JOINING); emberSerialWaitSend(APP_SERIAL);#ifdef SENSOR_APP printDataMode();#endif#ifdef DEBUG emberSerialPrintf(APP_SERIAL, "DEBUG IS ON\r\n"); emberSerialWaitSend(APP_SERIAL);#endif}// ********************************// to print binding table// ********************************void printBindingTable(int8u tableSize) { int8u i; EmberStatus status; EmberBindingTableEntry result; int8u* id; int8u indexLow; int8u indexHigh;#ifdef SINK_APP emberSerialPrintf(APP_SERIAL, "index type local remote id age\r\n");#else emberSerialPrintf(APP_SERIAL, "index type local remote id\r\n");#endif for (i=0; i<tableSize; i++) { emberSerialWaitSend(APP_SERIAL); status = emberGetBinding(i, &result); indexLow = (i % 10) + 48; indexHigh = ((i - (i % 10))/10) + 48; id = result.identifier; emberSerialPrintf(APP_SERIAL, " %c%c | ", indexHigh, indexLow); if (EMBER_SUCCESS == status) { switch(result.type) { case EMBER_UNUSED_BINDING: emberSerialPrintf(APP_SERIAL, "unused.....|"); break; case EMBER_UNICAST_BINDING: emberSerialPrintf(APP_SERIAL, "unicast....|"); break; case EMBER_MULTICAST_BINDING: emberSerialPrintf(APP_SERIAL, "multicast..|"); break; case EMBER_AGGREGATION_BINDING: emberSerialPrintf(APP_SERIAL, "aggregation|"); break; default: emberSerialPrintf(APP_SERIAL, "?????????..|"); } emberSerialPrintf(APP_SERIAL, "0x%x 0x%x ", result.local, result.remote); emberSerialPrintf(APP_SERIAL, "%x %x %x %x %x %x %x %x ", id[0],id[1],id[2],id[3],id[4],id[5],id[6],id[7]);#ifdef SINK_APP emberSerialPrintf(APP_SERIAL, "| %2x \r\n", ticksSinceLastHeard[i]);#else emberSerialPrintf(APP_SERIAL, "\r\n");#endif } else { emberSerialPrintf(APP_SERIAL, " --- ERROR ---\r\n "); } } emberSerialWaitSend(APP_SERIAL);}// *********************************// utility for printing EUI64 addresses// *********************************void printEUI64(int8u port, EmberEUI64* eui) { int8u i; int8u* p = (int8u*)eui; for (i=8; i>0; i--) { emberSerialPrintf(port, "%x", p[i-1]); }}// *********************************// print the node's tokens// *********************************void printTokens() { tokTypeStackNodeData tokNodeData; tokTypeStackKeys tokKeys; int i; halCommonGetToken(&tokNodeData, TOKEN_STACK_NODE_DATA); halCommonGetToken(&tokKeys, TOKEN_STACK_KEYS); emberSerialPrintf(APP_SERIAL, "PAN_ID: 0x%2x\r\n", tokNodeData.panId); emberSerialPrintf(APP_SERIAL, "RADIO_TX_POWER: 0x%x\r\n", tokNodeData.radioTxPower); emberSerialWaitSend(APP_SERIAL); emberSerialPrintf(APP_SERIAL, "RADIO_FREQ_CHANNEL: 0x%x\r\n", tokNodeData.radioFreqChannel); emberSerialPrintf(APP_SERIAL, "STACK_PROFILE: 0x%x\r\n", tokNodeData.stackProfile); emberSerialWaitSend(APP_SERIAL); emberSerialPrintf(APP_SERIAL, "NODE_TYPE: 0x%x\r\n", tokNodeData.nodeType); emberSerialPrintf(APP_SERIAL, "NETWORK_KEY: 0x"); for (i=0;i<16;i++) { emberSerialPrintf(APP_SERIAL, "%x", tokKeys.networkKey[i]); emberSerialWaitSend(APP_SERIAL); } emberSerialPrintf(APP_SERIAL, "\r\n"); emberSerialWaitSend(APP_SERIAL); emberSerialPrintf(APP_SERIAL, "ACTIVE_KEY_SEQ_NUM: 0x%x\r\n", tokKeys.activeKeySeqNum); emberSerialPrintf(APP_SERIAL, "ZIGBEE_NODE_ID: 0x%2x\r\n", tokNodeData.zigbeeNodeId); emberSerialWaitSend(APP_SERIAL);}// *********************************************************************// the next set of variables are needed to support EM250 bootloader.#ifdef USE_BOOTLOADER_LIBEmberNodeId bootloadInProgressChildId;EmberEUI64 bootloadInProgressEui;void sendBootloaderLaunchMessage(EmberEUI64 targetEui);boolean parentLaunchBootload = FALSE;#endif // USE_BOOTLOADER_LIB#if defined(SENSOR_APP) || defined(SINK_APP)// *********************************************************************// The follwing section has to do with sending JIT messages to sleeping// children. This code is needed by devices that act as parents#define AF_FRAME_SIZE_USED 3// bitmasks to tell which children have or have not been sent a// partcular JIT message - one bitmask per message. This can support// up to 16 childrenint16u jitMaskSinkAdvertise;int16u jitMaskMulticastHello;// Message Buffers are allocated for each of the two message typesEmberMessageBuffer jitMessageSinkAdvertise = EMBER_NULL_MESSAGE_BUFFER;EmberMessageBuffer jitMessageMulticastHello = EMBER_NULL_MESSAGE_BUFFER;// all JIT message in this app use the same APS frame setup except// for the clusterID. The APS header is setup before it is needed to// save time. Only the clusterID is modified before a message is sentEmberApsFrame jitMessageApsFrame;// Called from appAddJitForAllChildren which is called when the parent// receives a message that it must turn into a JIT message.//// This fills in the Message Buffer for the JIT message type passed in// if it has not already been filled in. It also updates the data in the// MessageBuffer to the data passed in.void createAndStoreJitMessage(int8u messageType, EmberMessageBuffer* globalBuffer, int8u* data, int8u dataLength){ // make sure we don't put in too much data and overrun the packet buffer if ((dataLength + AF_FRAME_SIZE_USED) > PACKET_BUFFER_SIZE) { assert(FALSE); } // if this is the first time we are creating the JIT message then // setup the aps header and af frame. Also allocate a buffer if ((*globalBuffer) == EMBER_NULL_MESSAGE_BUFFER) { int8u afFrame[AF_FRAME_SIZE_USED]; emberSerialPrintf(APP_SERIAL, "EVENT: creating JIT msg 0x%x\r\n", messageType); emberSerialWaitSend(APP_SERIAL); jitMessageApsFrame.sourceEndpoint = ENDPOINT; jitMessageApsFrame.destinationEndpoint = ENDPOINT; // the cluster ID will change based on the message //jitMessageApsFrame.clusterId = clusterId; jitMessageApsFrame.profileId = PROFILE_ID; jitMessageApsFrame.options = EMBER_UNICAST_OPTION_POLL_RESPONSE; afFrame[0] = EMBER_AF_FRAME_CONTROL(EMBER_AF_FRAME_TYPE_MSG, 1); afFrame[1] = 0; // sequence number afFrame[2] = 1; // transaction length (*globalBuffer) = emberFillLinkedBuffers(afFrame, AF_FRAME_SIZE_USED + dataLength); if ((*globalBuffer) == EMBER_NULL_MESSAGE_BUFFER) { emberSerialPrintf(APP_SERIAL, "ERROR: no buffers to construct JIT message!\r\n"); return; } } // copy data to the globalBuffer used to create the JIT message, start // 3 bytes from the front (AF_FRAME_SIZE_USED) and copy dataLength bytes emberCopyToLinkedBuffers(data, (*globalBuffer), AF_FRAME_SIZE_USED, dataLength);}// Called when the parent receives a message that it must turn into// a JIT message.//// this sets the JIT message flag for all children, and sets// the bitmask that the application uses to make sure it doesn't// send the same JIT message to a child more than once.void appAddJitForAllChildren(int8u msgType, int8u* data, int8u dataLength){ int8u i; EmberStatus status; // store the message data and set the flag so that all // devices get the message if (msgType == MSG_SINK_ADVERTISE) { jitMaskSinkAdvertise = 0xFFFF; createAndStoreJitMessage(MSG_SINK_ADVERTISE, &jitMessageSinkAdvertise, data, dataLength); } if (msgType == MSG_MULTICAST_HELLO) { jitMaskMulticastHello = 0xFFFF; createAndStoreJitMessage(MSG_MULTICAST_HELLO, &jitMessageMulticastHello, data, dataLength); } // set the message flag for all children emberSerialPrintf(APP_SERIAL, "JIT: setting flag, for all children\r\n"); for (i=0; i<emberMaxChildCount(); i++) { status = emberSetMessageFlag(emberChildId(i)); if (status != EMBER_SUCCESS) { emberSerialPrintf(APP_SERIAL, "ERROR: set flag, child %2x, status %x\r\n", emberChildId(i), status); emberSerialWaitSend(APP_SERIAL); } }}// This is called from emberPollHandler when the polling node has it's// message flag set.//// This sends the JIT message to the child using an APS message. The// child should be awake because it has just polled and the parent should// have sent a mac ack with frame pending set to truevoid appSendJitToChild(EmberNodeId childId){ EmberStatus status; boolean sentSinkAdv = FALSE; // used for debugging (see below) // boolean sentMcastHello = FALSE; // EmberStatus sinkAdvStatus; // EmberStatus mcastHelloStatus; // get the child index from child ID. The index is used by the // application to determine if the child has already polled for // that message int8u childIndex = emberChildIndex(childId); // check if this child has already polled for the message if (BIT(childIndex) & jitMaskSinkAdvertise) { // send the JIT message as a unicast jitMessageApsFrame.clusterId = MSG_SINK_ADVERTISE; /*sinkAdvStatus = */ emberSendUnicast(childId, &jitMessageApsFrame, jitMessageSinkAdvertise); sentSinkAdv = TRUE; // clear the bit - invert the number that has the child index'th bit set (xor)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -