![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
UDPversion
Dependencies: XBee mbed NetServicesMin
Diff: common/data_models.cpp
- Revision:
- 0:84a3b029656e
- Child:
- 1:3a46d2725374
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/data_models.cpp Sat Apr 14 03:53:45 2012 +0000 @@ -0,0 +1,178 @@ +#include "types.h" +#include "data_models.h" +#include "EthernetNetIf.h" + +extern Serial pc; +void initPayload(Payload *_payload) +{ + _payload->size=sizeof(Payload); + _payload->dummy = 0; + _payload->ftype = Ftype_Type_B; + + Frame *frame=&_payload->frame; + + for(int i=0 ; i < sizeof(frame->id.personalId) ; i++) frame->id.personalId[i]=0; + + frame->message.frameNumber=0xFFFF; + frame->message.device=SERVER; + frame->message.command=PING; + + for(int i=0 ; i < sizeof(_payload->frame.message.data) ; i++) _payload->frame.message.data[i]=0; +} + +void payloadToNetwork(Payload *_payload) +{ + Frame *frame=&_payload->frame; + _payload->size = htons(_payload->size); +// frame->id.head = htons(frame->id.head); + frame->message.totalSize = htons(frame->message.totalSize); + frame->message.frameNumber = htons(frame->message.frameNumber); + frame->message.sum = htons(frame->message.sum); + + if(frame->message.command == PING) { + + if(frame->message.device == SERVER) + { + + PrefServer *data; + data = (PrefServer*)frame->message.data; + data->serverPort = htons(data->serverPort); + data->destinationPort = htons(data->destinationPort); + data->xbeeAdress = htonl(data->xbeeAdress); + + } + else if(frame->message.device == LED_SENDER) + { + PrefSender *data; + data = (PrefSender*)frame->message.data; + data->xbeeAdress = htonl(data->xbeeAdress); + data->currentFrameNumber = htons(data->currentFrameNumber); + } + else if(frame->message.device == LED_RECIEVER) + { + PrefReciever *data; + data = (PrefReciever*)frame->message.data; + data->xbeeAdress = htonl(data->xbeeAdress); + } + } + +} +void networkToPayload(Payload *_payload) +{ + Frame *frame=&_payload->frame; + _payload->size = ntohs(_payload->size); +// frame->id.head = ntohs(frame->id.head); + frame->message.totalSize = ntohs(frame->message.totalSize); + frame->message.frameNumber = ntohs(frame->message.frameNumber); + frame->message.sum = ntohs(frame->message.sum); + + if(frame->message.command == PING){ + + if(frame->message.device == SERVER) + { + PrefServer *data; + data = (PrefServer*)frame->message.data; + data->serverPort = ntohs(data->serverPort); + data->destinationPort = ntohs(data->destinationPort); + data->xbeeAdress = ntohl(data->xbeeAdress); + } + else if(frame->message.device == LED_SENDER) + { + PrefSender *data; + data = (PrefSender*)frame->message.data; + data->xbeeAdress = ntohl(data->xbeeAdress); + data->currentFrameNumber = ntohs(data->currentFrameNumber); + } + else if(frame->message.device == LED_RECIEVER) + { + PrefReciever *data; + data = (PrefReciever*)frame->message.data; + data->xbeeAdress = ntohl(data->xbeeAdress); + } + } +} + + + +void initTestPayloadData(Payload *_payload) +{ + Frame *frame=&_payload->frame; + + initPayload(_payload); + _payload->size=sizeof(Payload); + +// frame->id.head=0x0000; +// uint8_t tmpid[14]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44}; + uint8_t tmpid[16]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45,0x46}; + for(int i=0;i<sizeof(tmpid);i++) frame->id.personalId[i]=tmpid[i]; + + + frame->message.frameNumber=1; + frame->message.device=LED_RECIEVER; + frame->message.command=RECIEVE; + + uint8_t tmpdata[40]={ + 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}; + for(int i=0;i<sizeof(tmpdata);i++) frame->message.data[i]=tmpdata[i]; + + frame->message.totalSize= + sizeof(frame->message.totalSize) + +sizeof(frame->message.frameNumber) + +sizeof(frame->message.device) + +sizeof(frame->message.command) + +sizeof(frame->message.data); + + uint16_t sum = culcSum(_payload); + + frame->message.sum = sum; + +} + +uint16_t culcSum(Payload *_payload){ + + uint16_t tmpSum = _payload->ftype; + uint8_t *data = (uint8_t *)&_payload->frame; + + for(uint8_t i=0 ; i<sizeof(Frame)-2 ; i++){ + tmpSum += data[i]; + } + return tmpSum; +} + +void dumpPayload(Payload *_payload) +{ + Frame *frame=&_payload->frame; + pc.printf("\r\n======== dump!!\r\n"); + pc.printf("payload\nsize:%04X\r\n ",_payload->size); + pc.printf("Type:%0X\r\n ",_payload->ftype); + +// pc.printf("Payload id_head:%04X\r\n personal id:",_payload->frame.id.head); + pc.printf("personal id:"); + for(int i=0 ; i < sizeof(frame->id.personalId) ; i++){ + pc.printf("%02X:%c ",frame->id.personalId[i],frame->id.personalId[i]); + } + + pc.printf("\r\n"); + + pc.printf("total size:%d , frame number:%d , divece:%0X , command:%02X , sum:%04X\r\n" + ,frame->message.totalSize + ,frame->message.frameNumber + ,frame->message.device + ,frame->message.command + ,frame->message.sum); + + pc.printf("message data:\r\n"); + for(int i=0 ; i < sizeof(frame->message.data) ; i++){ + pc.printf("%d:%02X ",i,_payload->frame.message.data[i]); + } + + pc.printf("\r\n--- RAW DATA -------\r\n"); + for(int i=0 ; i < sizeof(Payload) ; i++){ + pc.printf("%d:%02X ",i,((uint8_t*)_payload)[i]); + } + pc.printf("\r\n"); + +} \ No newline at end of file