UDPversion

Dependencies:   XBee mbed NetServicesMin

Committer:
recotana
Date:
Sat Apr 14 03:53:45 2012 +0000
Revision:
0:84a3b029656e
Child:
1:3a46d2725374
UDPversion

Who changed what in which revision?

UserRevisionLine numberNew contents of line
recotana 0:84a3b029656e 1 #include "types.h"
recotana 0:84a3b029656e 2 #include "data_models.h"
recotana 0:84a3b029656e 3 #include "EthernetNetIf.h"
recotana 0:84a3b029656e 4
recotana 0:84a3b029656e 5 extern Serial pc;
recotana 0:84a3b029656e 6 void initPayload(Payload *_payload)
recotana 0:84a3b029656e 7 {
recotana 0:84a3b029656e 8 _payload->size=sizeof(Payload);
recotana 0:84a3b029656e 9 _payload->dummy = 0;
recotana 0:84a3b029656e 10 _payload->ftype = Ftype_Type_B;
recotana 0:84a3b029656e 11
recotana 0:84a3b029656e 12 Frame *frame=&_payload->frame;
recotana 0:84a3b029656e 13
recotana 0:84a3b029656e 14 for(int i=0 ; i < sizeof(frame->id.personalId) ; i++) frame->id.personalId[i]=0;
recotana 0:84a3b029656e 15
recotana 0:84a3b029656e 16 frame->message.frameNumber=0xFFFF;
recotana 0:84a3b029656e 17 frame->message.device=SERVER;
recotana 0:84a3b029656e 18 frame->message.command=PING;
recotana 0:84a3b029656e 19
recotana 0:84a3b029656e 20 for(int i=0 ; i < sizeof(_payload->frame.message.data) ; i++) _payload->frame.message.data[i]=0;
recotana 0:84a3b029656e 21 }
recotana 0:84a3b029656e 22
recotana 0:84a3b029656e 23 void payloadToNetwork(Payload *_payload)
recotana 0:84a3b029656e 24 {
recotana 0:84a3b029656e 25 Frame *frame=&_payload->frame;
recotana 0:84a3b029656e 26 _payload->size = htons(_payload->size);
recotana 0:84a3b029656e 27 // frame->id.head = htons(frame->id.head);
recotana 0:84a3b029656e 28 frame->message.totalSize = htons(frame->message.totalSize);
recotana 0:84a3b029656e 29 frame->message.frameNumber = htons(frame->message.frameNumber);
recotana 0:84a3b029656e 30 frame->message.sum = htons(frame->message.sum);
recotana 0:84a3b029656e 31
recotana 0:84a3b029656e 32 if(frame->message.command == PING) {
recotana 0:84a3b029656e 33
recotana 0:84a3b029656e 34 if(frame->message.device == SERVER)
recotana 0:84a3b029656e 35 {
recotana 0:84a3b029656e 36
recotana 0:84a3b029656e 37 PrefServer *data;
recotana 0:84a3b029656e 38 data = (PrefServer*)frame->message.data;
recotana 0:84a3b029656e 39 data->serverPort = htons(data->serverPort);
recotana 0:84a3b029656e 40 data->destinationPort = htons(data->destinationPort);
recotana 0:84a3b029656e 41 data->xbeeAdress = htonl(data->xbeeAdress);
recotana 0:84a3b029656e 42
recotana 0:84a3b029656e 43 }
recotana 0:84a3b029656e 44 else if(frame->message.device == LED_SENDER)
recotana 0:84a3b029656e 45 {
recotana 0:84a3b029656e 46 PrefSender *data;
recotana 0:84a3b029656e 47 data = (PrefSender*)frame->message.data;
recotana 0:84a3b029656e 48 data->xbeeAdress = htonl(data->xbeeAdress);
recotana 0:84a3b029656e 49 data->currentFrameNumber = htons(data->currentFrameNumber);
recotana 0:84a3b029656e 50 }
recotana 0:84a3b029656e 51 else if(frame->message.device == LED_RECIEVER)
recotana 0:84a3b029656e 52 {
recotana 0:84a3b029656e 53 PrefReciever *data;
recotana 0:84a3b029656e 54 data = (PrefReciever*)frame->message.data;
recotana 0:84a3b029656e 55 data->xbeeAdress = htonl(data->xbeeAdress);
recotana 0:84a3b029656e 56 }
recotana 0:84a3b029656e 57 }
recotana 0:84a3b029656e 58
recotana 0:84a3b029656e 59 }
recotana 0:84a3b029656e 60 void networkToPayload(Payload *_payload)
recotana 0:84a3b029656e 61 {
recotana 0:84a3b029656e 62 Frame *frame=&_payload->frame;
recotana 0:84a3b029656e 63 _payload->size = ntohs(_payload->size);
recotana 0:84a3b029656e 64 // frame->id.head = ntohs(frame->id.head);
recotana 0:84a3b029656e 65 frame->message.totalSize = ntohs(frame->message.totalSize);
recotana 0:84a3b029656e 66 frame->message.frameNumber = ntohs(frame->message.frameNumber);
recotana 0:84a3b029656e 67 frame->message.sum = ntohs(frame->message.sum);
recotana 0:84a3b029656e 68
recotana 0:84a3b029656e 69 if(frame->message.command == PING){
recotana 0:84a3b029656e 70
recotana 0:84a3b029656e 71 if(frame->message.device == SERVER)
recotana 0:84a3b029656e 72 {
recotana 0:84a3b029656e 73 PrefServer *data;
recotana 0:84a3b029656e 74 data = (PrefServer*)frame->message.data;
recotana 0:84a3b029656e 75 data->serverPort = ntohs(data->serverPort);
recotana 0:84a3b029656e 76 data->destinationPort = ntohs(data->destinationPort);
recotana 0:84a3b029656e 77 data->xbeeAdress = ntohl(data->xbeeAdress);
recotana 0:84a3b029656e 78 }
recotana 0:84a3b029656e 79 else if(frame->message.device == LED_SENDER)
recotana 0:84a3b029656e 80 {
recotana 0:84a3b029656e 81 PrefSender *data;
recotana 0:84a3b029656e 82 data = (PrefSender*)frame->message.data;
recotana 0:84a3b029656e 83 data->xbeeAdress = ntohl(data->xbeeAdress);
recotana 0:84a3b029656e 84 data->currentFrameNumber = ntohs(data->currentFrameNumber);
recotana 0:84a3b029656e 85 }
recotana 0:84a3b029656e 86 else if(frame->message.device == LED_RECIEVER)
recotana 0:84a3b029656e 87 {
recotana 0:84a3b029656e 88 PrefReciever *data;
recotana 0:84a3b029656e 89 data = (PrefReciever*)frame->message.data;
recotana 0:84a3b029656e 90 data->xbeeAdress = ntohl(data->xbeeAdress);
recotana 0:84a3b029656e 91 }
recotana 0:84a3b029656e 92 }
recotana 0:84a3b029656e 93 }
recotana 0:84a3b029656e 94
recotana 0:84a3b029656e 95
recotana 0:84a3b029656e 96
recotana 0:84a3b029656e 97 void initTestPayloadData(Payload *_payload)
recotana 0:84a3b029656e 98 {
recotana 0:84a3b029656e 99 Frame *frame=&_payload->frame;
recotana 0:84a3b029656e 100
recotana 0:84a3b029656e 101 initPayload(_payload);
recotana 0:84a3b029656e 102 _payload->size=sizeof(Payload);
recotana 0:84a3b029656e 103
recotana 0:84a3b029656e 104 // frame->id.head=0x0000;
recotana 0:84a3b029656e 105 // uint8_t tmpid[14]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44};
recotana 0:84a3b029656e 106 uint8_t tmpid[16]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45,0x46};
recotana 0:84a3b029656e 107 for(int i=0;i<sizeof(tmpid);i++) frame->id.personalId[i]=tmpid[i];
recotana 0:84a3b029656e 108
recotana 0:84a3b029656e 109
recotana 0:84a3b029656e 110 frame->message.frameNumber=1;
recotana 0:84a3b029656e 111 frame->message.device=LED_RECIEVER;
recotana 0:84a3b029656e 112 frame->message.command=RECIEVE;
recotana 0:84a3b029656e 113
recotana 0:84a3b029656e 114 uint8_t tmpdata[40]={
recotana 0:84a3b029656e 115 1,2,3,4,5,6,7,8,9,10,
recotana 0:84a3b029656e 116 11,12,13,14,15,16,17,18,19,20,
recotana 0:84a3b029656e 117 21,22,23,24,25,26,27,28,29,30,
recotana 0:84a3b029656e 118 31,32,33,34,35,36,37,38,39,40};
recotana 0:84a3b029656e 119 for(int i=0;i<sizeof(tmpdata);i++) frame->message.data[i]=tmpdata[i];
recotana 0:84a3b029656e 120
recotana 0:84a3b029656e 121 frame->message.totalSize=
recotana 0:84a3b029656e 122 sizeof(frame->message.totalSize)
recotana 0:84a3b029656e 123 +sizeof(frame->message.frameNumber)
recotana 0:84a3b029656e 124 +sizeof(frame->message.device)
recotana 0:84a3b029656e 125 +sizeof(frame->message.command)
recotana 0:84a3b029656e 126 +sizeof(frame->message.data);
recotana 0:84a3b029656e 127
recotana 0:84a3b029656e 128 uint16_t sum = culcSum(_payload);
recotana 0:84a3b029656e 129
recotana 0:84a3b029656e 130 frame->message.sum = sum;
recotana 0:84a3b029656e 131
recotana 0:84a3b029656e 132 }
recotana 0:84a3b029656e 133
recotana 0:84a3b029656e 134 uint16_t culcSum(Payload *_payload){
recotana 0:84a3b029656e 135
recotana 0:84a3b029656e 136 uint16_t tmpSum = _payload->ftype;
recotana 0:84a3b029656e 137 uint8_t *data = (uint8_t *)&_payload->frame;
recotana 0:84a3b029656e 138
recotana 0:84a3b029656e 139 for(uint8_t i=0 ; i<sizeof(Frame)-2 ; i++){
recotana 0:84a3b029656e 140 tmpSum += data[i];
recotana 0:84a3b029656e 141 }
recotana 0:84a3b029656e 142 return tmpSum;
recotana 0:84a3b029656e 143 }
recotana 0:84a3b029656e 144
recotana 0:84a3b029656e 145 void dumpPayload(Payload *_payload)
recotana 0:84a3b029656e 146 {
recotana 0:84a3b029656e 147 Frame *frame=&_payload->frame;
recotana 0:84a3b029656e 148 pc.printf("\r\n======== dump!!\r\n");
recotana 0:84a3b029656e 149 pc.printf("payload\nsize:%04X\r\n ",_payload->size);
recotana 0:84a3b029656e 150 pc.printf("Type:%0X\r\n ",_payload->ftype);
recotana 0:84a3b029656e 151
recotana 0:84a3b029656e 152 // pc.printf("Payload id_head:%04X\r\n personal id:",_payload->frame.id.head);
recotana 0:84a3b029656e 153 pc.printf("personal id:");
recotana 0:84a3b029656e 154 for(int i=0 ; i < sizeof(frame->id.personalId) ; i++){
recotana 0:84a3b029656e 155 pc.printf("%02X:%c ",frame->id.personalId[i],frame->id.personalId[i]);
recotana 0:84a3b029656e 156 }
recotana 0:84a3b029656e 157
recotana 0:84a3b029656e 158 pc.printf("\r\n");
recotana 0:84a3b029656e 159
recotana 0:84a3b029656e 160 pc.printf("total size:%d , frame number:%d , divece:%0X , command:%02X , sum:%04X\r\n"
recotana 0:84a3b029656e 161 ,frame->message.totalSize
recotana 0:84a3b029656e 162 ,frame->message.frameNumber
recotana 0:84a3b029656e 163 ,frame->message.device
recotana 0:84a3b029656e 164 ,frame->message.command
recotana 0:84a3b029656e 165 ,frame->message.sum);
recotana 0:84a3b029656e 166
recotana 0:84a3b029656e 167 pc.printf("message data:\r\n");
recotana 0:84a3b029656e 168 for(int i=0 ; i < sizeof(frame->message.data) ; i++){
recotana 0:84a3b029656e 169 pc.printf("%d:%02X ",i,_payload->frame.message.data[i]);
recotana 0:84a3b029656e 170 }
recotana 0:84a3b029656e 171
recotana 0:84a3b029656e 172 pc.printf("\r\n--- RAW DATA -------\r\n");
recotana 0:84a3b029656e 173 for(int i=0 ; i < sizeof(Payload) ; i++){
recotana 0:84a3b029656e 174 pc.printf("%d:%02X ",i,((uint8_t*)_payload)[i]);
recotana 0:84a3b029656e 175 }
recotana 0:84a3b029656e 176 pc.printf("\r\n");
recotana 0:84a3b029656e 177
recotana 0:84a3b029656e 178 }