주석에 보면 //이곳은 시리얼로부터 데이터를 수신했을 때 부분-->ListenfromSerial함수호출하여 사용하면 될까요?[센서네트워크]
#include
#include
#include
#include
#include "tdef.h"
#include "ttype.h"
//시리얼 통신 관련 헤더파일
#include
#include
#include
#define TOSH_DATA_LENGTH 33
#define BAUDRATE B57600
#define MODEMDEVICE "/dev/ttyS2"
#define TOSH_DATA_LENGTH 33
struct TinyOS_Serial_MSG
{
unsigned short PT;
unsigned char SRC;
unsigned char DST;
unsigned char ST;
unsigned char data[TOSH_DATA_LENGTH];
unsigned short crc;
}__attribute__ ((packed)) TinyOS_Serial_MSG;
//////// Values for receiving data from serial ////////
struct TinyOS_Serial_MSG RecvMsg;
unsigned char RecvBuff[50];
unsigned char Startflag;
unsigned char SevenD_flag;
int Rindex;
///////////////////////////////////////////////////////
//////// Values for transmitting data to serial ////////
struct TinyOS_Serial_MSG SendMsg;
unsigned char SendBuff[50];
unsigned char Sindex;
unsigned short CRC_Value;
unsigned char Type = 0x0A;
unsigned char GroupID = 0x7D;
///////////////////////////////////////////////////////
int fd;
void Parsing_Buff(unsigned char *RecvBuff, unsigned char RecvLen);
void SendMsgtoSerial(unsigned char *SendData, unsigned char SendLen);
//이곳은 시리얼로부터 데이터를 수신했을때~~~~~~~~~~~~
void Parsing_Buff(unsigned char *RecvBuff, unsigned char RecvLen)
{
// RecvBuff[0] and [1] are 0x7E and 0x42
/* typedef struct TOS_Msg{
unsigned short PT;
unsigned char SRC;
unsigned char DST;
unsigned char ST;
int8_t DATA[TOSH_DATA_LENGTH];
unsigned short crc;
} */
int i;
memcpy(&RecvMsg.PT, RecvBuff+2, 2);
RecvMsg.SRC = RecvBuff[4];
RecvMsg.DST = RecvBuff[5];
RecvMsg.ST = RecvBuff[6];
for ( i=0 ; i
RecvMsg.data[i] = RecvBuff[7+i];
}
}
unsigned short crcByte(unsigned short crc, unsigned char b)
{
unsigned char i;
crc = crc ^ b << 8;
i = 8;
do
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
while (--i);
return crc;
}
uint8_t
GetPacket_from_sensor(uHealthPDU *rPDU, uint8_t *RecvData) {
uHealthPDU *ptr=rPDU;
uint8_t i;
TinyOS_Serial_MSG *RecvMsg = (TinyOS_Serial_MSG *) RecvData;
uint8_t buffer[40];
int n;
/* memset((uint8_t *) rPDU, 0x0, sizeof(uHealthPDU)); */
if (fd > 0) {
memset(buffer, 0x0, 40);
n = read(fd, buffer, 40);
strncpy((uint8_t *) rPDU, buffer, n);
close(fd);
return (rPDU->PT);
}
else {
return(FAIL);
}
// ----------------------------------------
}
uHealthPDU *
MakePacket_to_sensor(uHealthPDU rPDU, uint8_t *Tokenset) {
uHealthPDU *ptr;
ptr = (uHealthPDU *) malloc(sizeof(uHealthPDU));
memset(ptr, 0x0, sizeof(uHealthPDU));
ptr->PT = CON_RES;
ptr->SRC = rPDU.DST;
ptr->DST = rPDU.SRC;
ptr->ST = rPDU.ST;
strcpy((uint8_t *) ptr+4, Tokenset);
return(ptr);
}
int
SendPacket_to_sensor(uHealthPDU *sPDU) {
// 테스트코드 ---------------------------
int fd, n;
fd = open("data/serial.out", O_WRONLY | O_CREAT, 0644);
if (fd > 0) {
n = write(fd, sPDU, strlen((uint8_t *) sPDU));
close(fd);
return (SUCCESS);
}
else {
return(FAIL);
}
// ----------------------------------------
}
int
FlushPacket(uHealthPDU *PDU) {
if (PDU != (uHealthPDU *) NULL)
free(PDU);
}
uint8_t
*DecrytionPacket(uHealthPDU rPDU, uint8_t *key) {
uint8_t len =0;
uint8_t str[2];
uint8_t *MedicalData;
str[0] = rPDU.data[2];
str[1] = 0x0;
len = atoi(str);
MedicalData = (uint8_t *) malloc (sizeof(uint8_t) * 30);
memset(MedicalData, 0x0, 30);
strncpy(MedicalData, &(rPDU.data[3]),len);
RC4( key, sizeof(key), MedicalData, len );
printf("CONNECTED | 수신데이터 복호화결과 :(%s)\n",MedicalData);
return ((uint8_t *) MedicalData);
}
int
SaveData_to_buffer(uint8_t *PureData) {
// 테스트코드 ---------------------------
int fd, n;
fd = open("data/pure.data", O_WRONLY | O_CREAT | O_APPEND , 0644);
if (fd > 0) {
n = write(fd, PureData, strlen((uint8_t *) PureData));
close(fd);
return (SUCCESS);
}
else {
return(FAIL);
}
// ----------------------------------------
}
uHealthPDU *
MakeTimeSessionPacket_to_sensor(uHealthPDU rPDU, uint8_t i, uint8_t k) {
uHealthPDU *ptr;
uint8_t buffer[3];
ptr = (uHealthPDU *) malloc(sizeof(uHealthPDU));
memset(buffer, 0x0, 3);
memset(ptr, 0x0, sizeof(uHealthPDU));
ptr->PT = TOKEN_REQ;
ptr->SRC = rPDU.DST;
ptr->DST = rPDU.SRC;
ptr->ST = rPDU.ST;
sprintf(buffer,"%d%d",i,k);
ptr->data[0] = buffer[0];
ptr->data[1] = buffer[1];
return(ptr);
}
int
Generate_RandomTokenID(uint8_t *ti,uint8_t *tk) {
*ti = 1+(int) (5.0*rand()/(RAND_MAX+1.0));
*tk = 1+(int) (5.0*rand()/(RAND_MAX+1.0));
}
uint8_t
VerifyChallengeKey(uHealthPDU rPDU, uint8_t *key) {
return(SUCCESS);
}
int
SetTimeoutValue(uint8_t *stype) {
return(SUCCESS);
}
댓글 달기