⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app.c

📁 基于cc1000和avr128处理器的指定路由多跳网络的试验程序
💻 C
字号:
/*
****************************************************************************
*              宁波中科集成电路设计中心  版权所有 Copyright 2005
*						http:\\www.nbicc.com
*文件名:  route.c
*程序员:  蒋文丰
*主要内容 
*更新时间: 2005年11月17日
*如有问题或BUG,请登录www.wsn.net.cn 提问或用邮件和作者联系
****************************************************************************
*/


#include "type.h"
#include "message.h"
#include "global.h"
#include "sensor.h"
#include "timer.h"
#include "os.h"
#include "uartDebug.h"
#include "app.h"
#include "led.h"
#include "mac.h"
static result_t ROUTEGetADCDataStart(void);
static void SendSensorData(uint16_t localsensordata);
static void routehandlepkt(uint8_t type, OSMACMsgPtr receivemsg) ;
static void routeReceiveSENSOR(OSMACMsgPtr receivemsg) ;
static void sendreceivesensor(void);  //转发传感数据

enum sensortype {
  LIGHT = 1,
  TEMP = 2
};

/******************全局变量声明*************************************  */


OSMACMsg routesensor;
OSMACMsgPtr routesensormsgptr;
uint16_t sensorseq;   //应用层数据包号
uint16_t networkseq;  //网络层数据包号
uint8_t  LifeTime;//作为保存接收到的数据包的生存时间的相当于缓存的数据变量
SensorMsg forwardsensor;
OSMACMsg uartMsg;
//网络层的包冗余剔除



/******************************************************* 
          全局函数声明
 ******************************************  */
result_t Init(void) {
	////下面是配置节点的一些数据,需要在节点初始化之前定义,因为
	//有些数据是在初始化的时候直接赋值给相关的寄存器
    POWERLEVEL = 0XFF; //射频的功率
    
    OS_LOCAL_ADDRESS = 3; //本地节点的地址
    OS_BCAST_ADDR = 0xEE;  //广播地址

    ACK_ON = 0;  //使用ack机制,1表示开启,0表示关闭
    ACKTIME = 4;  //ack等待时间=ACKTIME*48ms
    //选择4的理由是和发送成功时的一样的,尽量假设数据包成功,ACK不成功

	//////////
    LedInit();
	LedRedOff();
	LedGreenOff();
	LedYellowOff();
    
	SensorPhoOStdControlInit();
	TimerStdControlInit();
	// 初始化UART debugging
	uartDebug_init();
	MACInit();

	sensorseq = 0;
	networkseq = 0;

	routesensormsgptr = &routesensor;
    
    return SUCCESS;
}

result_t Start(void) { 
    
	SensorPhoOStdControlStart();
	ROUTEGetADCDataStart();
	return SUCCESS;
		
}

static result_t ROUTEGetADCDataStart(void) {
    
    TimerTimerStart(1, TIMER_REPEAT, 2000); //定时采集数据
	return SUCCESS;
}


result_t Timer0_0_Fired(void) {
	// 添加时间事件
	return SUCCESS;
}


result_t Timer0_1_Fired(void) {
    //采集光数据
    SensorExternalPhotoADCGetData();
	return SUCCESS;
}


result_t SensordataReady(uint16_t data) {
    //采集好数据以后,执行发送数据任务 
    if( 3 == OS_LOCAL_ADDRESS ) {
    SendSensorData(data);
	}
	return SUCCESS;
}


/*************************************************************************
*功能描述:将ADC采样得到的数据采用单播的方式发送出去
*参数说明:ADC采样数据
*返回值:  
**************************************************************************/
static void SendSensorData(uint16_t localsensordata) {
	//发送采集到的数据,暂为空
	SensorMsg *sensordata;
	SHopMsgPtr sHopMsg;
    uint8_t i;
    uint8_t length;
	
		sHopMsg = (SHopMsgPtr )routesensormsgptr->data;
		sHopMsg->type = SENSOR;
		sHopMsg->seq = networkseq;
		sHopMsg->lifetime = 0;// 本程序中无效的字段统一初始化为0
		
		sensordata = (SensorMsg *) (sHopMsg->data);
		sensordata->type = LIGHT; //传感数据为光数据 
	    sensordata->sensorseq = sensorseq;
		sensordata->reading = localsensordata;
		sensordata->src = OS_LOCAL_ADDRESS;
		
		for (i = 0; i < MAXPASS; i++) {
	    	sensordata->passnode[i] = 0;  		
		}
        
	    sensorseq++;//应用层数据包号自增
		sensordata->passnode[0] = OS_LOCAL_ADDRESS;
		
		length = SHOP_HEADER_LEN + sizeof(SensorMsg);

		MACUnicastMsg(routesensormsgptr, length, 2,1);		
}

/*************************************************************************
*功能描述:mac层传输数据完成后的回调函数,通知上层数据包发送完成
*参数说明:被发送的数据包的指针
*返回值:  1
**************************************************************************/
result_t TransmitDone(OSMACMsgPtr msg) {
    networkseq++;
	LedYellowToggle();
	return 1;
}

/*************************************************************************
*功能描述:mac层传输数据失败后的回调函数,通知上层数据包发送失败
*参数说明:被发送的数据包的指针
*返回值:  
**************************************************************************/
void SendFail(OSMACMsgPtr receivemsg){
    ///////////////////
}

/*************************************************************************
*功能描述:mac层接收到一个完整的数据包后的回调函数,通知上层接收到一个数据包
*参数说明:被正确接收的数据包的指针
*返回值:  1
**************************************************************************/
result_t ReceiveDone(OSMACMsgPtr packet) {  //与底层接口函数
    uint8_t type;
	SHopMsgPtr sHopMsg;
	sHopMsg = (SHopMsgPtr )packet->data;
    
 		type = sHopMsg->type;
		if( packet->group == 0x01) {
		routehandlepkt(type, packet);
		     }

    return 1;   
}

/*************************************************************************
*功能描述:在网络层的概念上区分区分接收到的数据包,以区分不同的处理方式
*参数说明:网络层上的数据包类型,被接收的数据包的指针
*返回值:  
**************************************************************************/
static void routehandlepkt(uint8_t type, OSMACMsgPtr receivemsg) {
		
    switch (type) {
    case BROADCAST:  //网络控制包,比如路由包等等
    break;

	case SENSOR:   //数据包
    routeReceiveSENSOR(receivemsg);
	break;
    }
}

/*************************************************************************
*功能描述:非sink节点缓存接收到的数据,并调用发送任务;
           sink节点将数据包发往串口;
*参数说明:被发送的数据包的指针
*返回值:  
**************************************************************************/
static void routeReceiveSENSOR(OSMACMsgPtr receivemsg) {
	SHopMsgPtr sHopMsg;
	SensorMsg *sensordata;
    uint8_t i;
	uint8_t j;

	uartMsg = *receivemsg;
  
	if (OS_LOCAL_ADDRESS != SINKNODE) {
		    sHopMsg = (SHopMsgPtr )receivemsg->data;
		    sensordata = (SensorMsg *) (sHopMsg->data);
			LifeTime = sHopMsg->lifetime;
			
			
            forwardsensor.type = sensordata->type;		
			forwardsensor.sensorseq = sensordata->sensorseq;
			forwardsensor.reading = sensordata->reading;
			forwardsensor.src = sensordata->src;
			
			for (i = 0; i < MAXPASS; i++) {
				forwardsensor.passnode[i] = sensordata->passnode[i];
			}
			j = 0;
			for (i = 0; i < MAXPASS; i++) {
				if (sensordata->passnode[i] != 0) {
					j = j+1;
				} else {
					break;
				}
			}
			if( j < MAXPASS ){ //越界检查
			forwardsensor.passnode[j] = OS_LOCAL_ADDRESS;
			}
			LedGreenToggle();
			OSPostTask(sendreceivesensor);	
	} else {
		//如果是SINK节点,则写到后台
		LedRedToggle(); //红灯亮,表示基站接收到传感数据包
		uartDebug_txPacket(&uartMsg);
	}
}

/*************************************************************************
*功能描述:发送接收并缓存的数据包
*参数说明:
*返回值: 
**************************************************************************/
static void sendreceivesensor(void) {
    SHopMsgPtr sHopMsg;
  	SensorMsg *sensordata;
    uint8_t length;

    sHopMsg = (SHopMsgPtr )routesensormsgptr->data;
	sHopMsg->type = SENSOR;
	sHopMsg->seq = networkseq;
	sHopMsg->lifetime = LifeTime;
    sensordata = (SensorMsg *) (sHopMsg->data);
	*sensordata = forwardsensor;

	length = SHOP_HEADER_LEN + sizeof(SensorMsg);
	MACUnicastMsg(routesensormsgptr, length, 1,1);
}

/*************************************************************************
*功能描述:检查是否有可用的缓存区,这是因为所有的节点在网络中有路由器的功能,
           在采用ACK方式下要检查接收到的数据是否有缓存区,没有的话就要发送NACK
		   消息(在我们这里没有使用该函数的功能)
*参数说明:
*返回值:  1
**************************************************************************/
result_t GetFreeQueueLength(void){
    return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -