📄 macfunctionemulationdefinition.c
字号:
#include <winsock2.h>
#include <stdio.h>
#include <process.h>
#include<swsZigbeeNWK_headers.h>
#define DATA "我是老鼠,我是老鼠,听到请回答.."
#pragma comment(lib, "wsock32.lib")
#define DEFAULT_PORT 4113 //4113is one of my child device address
#define TRUE 1
//unsigned _stdcall client(void *p);
void mcpsDataRequest(BYTE addrModes, WORD srcPanId, ADDRESS *pSrcAddr, WORD destPanId, ADDRESS *pDestAddr, UINT8 msduLength, BYTE *pMsdu, BYTE msduHandle, BYTE txOptions)
{
/*
unsigned long thd;
unsigned tid;
unsigned long timeOut;
timeOut=20000;//使用这个变量来记录传输一个数据的超时值。
thd=_beginthreadex(NULL,0,client,0,0,&tid);
Sleep(timeOut);
*/
WORD wVersionRequested;
WSADATA wsaData;
int err;
SOCKET sock;
struct sockaddr_in server;
struct hostent *hp ;
int i;
char sendNwkData[97]; //此处定义的是网络层要传出的数据的最大长度
char *trueSendNwkData=NULL;
//最先执行
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable WinSock DLL. */
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
printf("The socket number is :%ld\n ",sock);
if (sock ==INVALID_SOCKET)
{
perror("opening stream socket");
exit(1);
}
//来自应用层的数据经过网络层的封装后将最终的数据存放在pMsdu中
//初始化通过socket要传送出去的来自网络层的数据
// printf("The length of msduLength is %d \n", msduLength);
printf("将要传出去的数据:!\n");
for(i=0;i<msduLength;i++)
{
sendNwkData[i]=(char)pMsdu[i];
printf("%d ", sendNwkData[i]);
}
printf("I am running the mcpsDataRequest() function!\n");
server.sin_family = AF_INET;
hp = gethostbyname("127.0.0.1");
if (hp == 0) {
fprintf(stderr, "%s: unknown host \n", "127.0.0.1");
exit(2);
}
memcpy((char*)&server.sin_addr, (char*)hp->h_addr, hp->h_length);
server.sin_port = htons(DEFAULT_PORT);
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
perror("connecting stream socket");
exit(3);
}
printf("are you here?\n");
/*此处主要对要被传送出去的数据进行操作*/
printf("are you here?\n");
memcpy(sendNwkData,(char*)pMsdu,msduLength);
trueSendNwkData=(char*)malloc((msduLength+1)*sizeof(char));
memset(trueSendNwkData,-1,msduLength+1);
if (send(sock, (const char*)pMsdu, msduLength,0) < 0)
perror("sending on stream socket");
closesocket(sock);
printf("I have send out some command\n");
exit(0);
//发送数据应该有一个时间限制,即在某一定时间间隔内将数据发送出去,所以这里应该尽量使用
//一个sleep函数而不是一个 while(1);永真循环
}
/*
unsigned _stdcall client(void *q)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
BYTE sendData[4];
SOCKET sock;
struct sockaddr_in server;
struct hostent *hp ;
// char buf[1024];
char data[5];
char *p=NULL;
struct test testData;
//p=data;
sendData[0]=0x12;
sendData[1]=0x34;
sendData[2]=0x56;
sendData[3]=0x78;
testData.a='a';
testData.b='b';
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/ * Tell the user that we could not find a usable * /
/ * WinSock DLL. * /
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock ==INVALID_SOCKET) {
perror("opening stream socket");
exit(1);
}
server.sin_family = AF_INET;
hp = gethostbyname("127.0.0.1");
if (hp == 0) {
fprintf(stderr, "%s: unknown host \n", "127.0.0.1");
exit(2);
}
memcpy((char*)&server.sin_addr, (char*)hp->h_addr, hp->h_length);
server.sin_port = htons(DEFAULT_PORT);
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
perror("connecting stream socket");
exit(3);
}
printf("are you here?\n");
/ *此处主要对要被传送出去的数据进行操作* /
memcpy(data,sendData,sizeof(data));
// memcpy((char *)&testData.a,sendData,sizeof(sendData));
printf("are you here?\n");
// memcpy(s2,&testData,sizeof(struct test));
// p=&data[0];
if (send(sock, DATA, sizeof(DATA),0) < 0)
// if (send(sock, data, sizeof(data),0) < 0)
// if (send(sock, (const char*)p, sizeof(p),0) < 0)
// if (send(sock, (const char*)s2, sizeof(struct test),0) < 0)
perror("sending on stream socket");
printf("the length of the data:%d",sizeof(p));
closesocket(sock);
printf("I have send out some command\n");
exit(0);
}
*/
//-------------------------------------------------------------------------------------------------------
// MAC_ENUM mlmeScanRequest(BYTE scanType, DWORD scanChannels, UINT8 scanDuration)
//
// DESCRIPTION:
// Scan through the selected channels (energy, active, passive and orphan scanning supported).
// Important:
// - The maximum number of results returned for active and passive scans is
// defined by the MAC_OPT_MAX_PAN_DESCRIPTORS (>= 1) mac option
// - This function will not exit before the scan is completed.
//
// PARAMETERS:
// BYTE scanType
// ENERGY_SCAN, ACTIVE_SCAN, PASSIVE_SCAN or ORPHAN_SCAN
// DWORD scanChannels
// The channel index mask (0x07FFF800 are the legal values for 2.4 GHz channels)
// UINT8 scanDuration
// The scan duration defines the time spent scanning each channel, defined as:
// (aBaseSuperframeDuration * (2 ^^ scanDuration + 1)) symbol periods
// = (60 * 16 * (2^^scanDuration+1)) symbol periods
// E.g., scanning all 16 channels with Scanduration 5 takes 8.11 seconds
// MAC_SCAN_RESULT *pScanResult
// The pointer to the MAC_SCAN_RESULT struct (defined by the higher layer) where
// the MAC sublayer shall store the scan result.
//
// RETURN VALUE:
// MAC_ENUM
// INVALID_PARAMETER, SUCCESS or NO_BEACON
//-------------------------------------------------------------------------------------------------------
MAC_ENUM mlmeScanRequest(BYTE scanType, DWORD scanChannels, UINT8 scanDuration, MAC_SCAN_RESULT *pScanResult)
{
//在这个函数中完成对‘值-结果’参数pScanResult的赋值操作
if(pScanResult->scanType==ENERGY_SCAN)
{
pScanResult->resultListSize=5;
pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[0]=12;
pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[1]=13;
pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[2]=14;
pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[3]=15;
pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[4]=16;
pScanResult->unscannedChannels=0x8fff000;
}
else if(pScanResult->scanType==ACTIVE_SCAN)
{
pScanResult->resultListSize=2;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordAddrMode=AM_SHORT_16;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordAddress.Short=0x1011;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordPanId=0x0000;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].linkQuality=5;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].logicalChannel=0x12;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordAddrMode=AM_SHORT_16;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordAddress.Short=0x1012;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordPanId=0x0001;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].linkQuality=5;
pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].logicalChannel=0x13;
pScanResult->unscannedChannels=0x8fff000;
}
else if(pScanResult->scanType==ORPHAN_SCAN)
{
printf("\nThe MAC layer is performing ORPHAN_SCAN!\n");
}
return MAC_SUCCESS;
}
//-------------------------------------------------------------------------------------------------------
// MAC_ENUM mlmeResetRequest(ZBOOL setDefaultPIB)
//
// DESCRIPTION:
// Reset the MAC and PHY layers, including CC2420, the microcontroller, and all state variables.
// NOTE: The initialization and power-up sequence must be performed according to
// the MAC documentation prior to calling mlmeResetRequest or other
// MAC primitives
//
// PARAMETERS:
// ZBOOL setDefaultPIB
// Reset the PHY and MAC PIBs
//
// RETURN VALUE:
// MAC_ENUM
// Always SUCCESS
//-------------------------------------------------------------------------------------------------------
MAC_ENUM mlmeResetRequest(ZBOOL setDefaultPIB)
{
//在这个函数中主要完成设备的PIB中的一些属性的赋值操作
mpib.macAckWaitDuration = 54;
mpib.macAssociationPermit = FALSE;
mpib.macAutoRequest = TRUE;
mpib.macBattLifeExt = FALSE;
mpib.macBattLifeExtPeriods = 6;
mpib.pMacBeaconPayload = NULL;
mpib.macBeaconPayloadLength = 0;
mpib.macBeaconOrder = 15;
mpib.macBeaconTxTime = 0;
mpib.macBSN = 0;
mpib.macCoordShortAddress = 0xFFFF;
mpib.macDSN = 1;
mpib.macGTSPermit = TRUE;
mpib.macMaxCsmaBackoffs = 4;
mpib.macMinBE = 3;
mpib.macPANId = 0xFFFF;
mpib.macPromiscuousMode = FALSE;
mpib.macRxOnWhenIdle = FALSE;
mpib.macShortAddress = 0xFFFF;
mpib.macSuperframeOrder = 15;
mpib.macTransactionPersistenceTime = 0x01F4;
mpib.macCoordExtendedAddress = 0xFFFF;
mpib.macAssocatedPanCordinator = TRUE;
#if MAC_OPT_ACL_SIZE > 0
mpib.ppMacACLEntryDescriptorSet = NULL;
mpib.macACLEntryDescriptorSetSize = 0;
#endif
#if MAC_OPT_SECURITY
mpib.macDefaultSecurity = FALSE;
mpib.macDefaultSecurityMaterialLength = 0x15;
mpib.pMacDefaultSecurityMaterial = NULL;
mpib.macDefaultSecuritySuite = 0;
#endif
#if ((MAC_OPT_SECURITY) || (MAC_OPT_ACL_SIZE>0))
mpib.macSecurityMode = 0;
#endif
return MAC_SUCCESS;
}
//-------------------------------------------------------------------------------------------------------
// void mlmeSyncRequest(UINT8 logicalChannel, ZBOOL trackBeacon)
//
// DESCRIPTION:
// Switch to the selected channel, locate a single beacon, and start or stop tracking beacons
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -