![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
nakagawa kit emurator (add CR) BaudRate 115200,p20
Dependencies: mbed NetServicesMin
common/data_models.cpp@4:9daa308155d6, 2012-04-17 (annotated)
- Committer:
- recotana
- Date:
- Tue Apr 17 04:14:27 2012 +0000
- Revision:
- 4:9daa308155d6
- Parent:
- 3:51e64f5e42b8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
recotana | 0:e5342390978b | 1 | #include "types.h" |
recotana | 0:e5342390978b | 2 | #include "data_models.h" |
recotana | 0:e5342390978b | 3 | #include "EthernetNetIf.h" |
recotana | 0:e5342390978b | 4 | |
recotana | 3:51e64f5e42b8 | 5 | extern Serial pc; |
recotana | 3:51e64f5e42b8 | 6 | |
recotana | 0:e5342390978b | 7 | |
recotana | 0:e5342390978b | 8 | void initPayload(Payload *_payload) |
recotana | 0:e5342390978b | 9 | { |
recotana | 0:e5342390978b | 10 | _payload->size=sizeof(Payload); |
recotana | 0:e5342390978b | 11 | _payload->dummy = 0; |
recotana | 0:e5342390978b | 12 | _payload->ftype = Ftype_Type_B; |
recotana | 0:e5342390978b | 13 | |
recotana | 0:e5342390978b | 14 | Frame *frame=&_payload->frame; |
recotana | 0:e5342390978b | 15 | |
recotana | 0:e5342390978b | 16 | for(int i=0 ; i < sizeof(frame->id.personalId) ; i++) frame->id.personalId[i]=0; |
recotana | 0:e5342390978b | 17 | |
recotana | 3:51e64f5e42b8 | 18 | frame->message.frameNumber=0xFFFF; |
recotana | 3:51e64f5e42b8 | 19 | frame->message.device=SERVER; |
recotana | 3:51e64f5e42b8 | 20 | frame->message.command=PING; |
recotana | 0:e5342390978b | 21 | |
recotana | 0:e5342390978b | 22 | for(int i=0 ; i < sizeof(_payload->frame.message.data) ; i++) _payload->frame.message.data[i]=0; |
recotana | 0:e5342390978b | 23 | } |
recotana | 0:e5342390978b | 24 | |
recotana | 0:e5342390978b | 25 | void payloadToNetwork(Payload *_payload) |
recotana | 0:e5342390978b | 26 | { |
recotana | 0:e5342390978b | 27 | Frame *frame=&_payload->frame; |
recotana | 3:51e64f5e42b8 | 28 | _payload->size = htons(_payload->size); |
recotana | 3:51e64f5e42b8 | 29 | // frame->id.head = htons(frame->id.head); |
recotana | 0:e5342390978b | 30 | frame->message.totalSize = htons(frame->message.totalSize); |
recotana | 0:e5342390978b | 31 | frame->message.frameNumber = htons(frame->message.frameNumber); |
recotana | 3:51e64f5e42b8 | 32 | frame->message.sum = htons(frame->message.sum); |
recotana | 0:e5342390978b | 33 | |
recotana | 3:51e64f5e42b8 | 34 | if(frame->message.command == PING) { |
recotana | 3:51e64f5e42b8 | 35 | |
recotana | 3:51e64f5e42b8 | 36 | if(frame->message.device == SERVER) |
recotana | 3:51e64f5e42b8 | 37 | { |
recotana | 3:51e64f5e42b8 | 38 | |
recotana | 3:51e64f5e42b8 | 39 | PrefServer *data; |
recotana | 3:51e64f5e42b8 | 40 | data = (PrefServer*)frame->message.data; |
recotana | 3:51e64f5e42b8 | 41 | data->serverPort = htons(data->serverPort); |
recotana | 3:51e64f5e42b8 | 42 | data->destinationPort = htons(data->destinationPort); |
recotana | 3:51e64f5e42b8 | 43 | data->xbeeAdress = htonl(data->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 44 | |
recotana | 3:51e64f5e42b8 | 45 | } |
recotana | 3:51e64f5e42b8 | 46 | else if(frame->message.device == LED_SENDER) |
recotana | 3:51e64f5e42b8 | 47 | { |
recotana | 3:51e64f5e42b8 | 48 | PrefSender *data; |
recotana | 3:51e64f5e42b8 | 49 | data = (PrefSender*)frame->message.data; |
recotana | 3:51e64f5e42b8 | 50 | data->xbeeAdress = htonl(data->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 51 | data->currentFrameNumber = htons(data->currentFrameNumber); |
recotana | 3:51e64f5e42b8 | 52 | } |
recotana | 3:51e64f5e42b8 | 53 | else if(frame->message.device == LED_RECIEVER) |
recotana | 3:51e64f5e42b8 | 54 | { |
recotana | 3:51e64f5e42b8 | 55 | PrefReciever *data; |
recotana | 3:51e64f5e42b8 | 56 | data = (PrefReciever*)frame->message.data; |
recotana | 3:51e64f5e42b8 | 57 | data->xbeeAdress = htonl(data->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 58 | } |
recotana | 3:51e64f5e42b8 | 59 | } |
recotana | 3:51e64f5e42b8 | 60 | |
recotana | 0:e5342390978b | 61 | } |
recotana | 0:e5342390978b | 62 | void networkToPayload(Payload *_payload) |
recotana | 0:e5342390978b | 63 | { |
recotana | 0:e5342390978b | 64 | Frame *frame=&_payload->frame; |
recotana | 3:51e64f5e42b8 | 65 | _payload->size = ntohs(_payload->size); |
recotana | 3:51e64f5e42b8 | 66 | // frame->id.head = ntohs(frame->id.head); |
recotana | 0:e5342390978b | 67 | frame->message.totalSize = ntohs(frame->message.totalSize); |
recotana | 0:e5342390978b | 68 | frame->message.frameNumber = ntohs(frame->message.frameNumber); |
recotana | 3:51e64f5e42b8 | 69 | frame->message.sum = ntohs(frame->message.sum); |
recotana | 3:51e64f5e42b8 | 70 | |
recotana | 3:51e64f5e42b8 | 71 | if(frame->message.command == PING){ |
recotana | 3:51e64f5e42b8 | 72 | |
recotana | 3:51e64f5e42b8 | 73 | if(frame->message.device == SERVER) |
recotana | 3:51e64f5e42b8 | 74 | { |
recotana | 3:51e64f5e42b8 | 75 | PrefServer *data; |
recotana | 3:51e64f5e42b8 | 76 | data = (PrefServer*)frame->message.data; |
recotana | 3:51e64f5e42b8 | 77 | data->serverPort = ntohs(data->serverPort); |
recotana | 3:51e64f5e42b8 | 78 | data->destinationPort = ntohs(data->destinationPort); |
recotana | 3:51e64f5e42b8 | 79 | data->xbeeAdress = ntohl(data->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 80 | } |
recotana | 3:51e64f5e42b8 | 81 | else if(frame->message.device == LED_SENDER) |
recotana | 3:51e64f5e42b8 | 82 | { |
recotana | 3:51e64f5e42b8 | 83 | PrefSender *data; |
recotana | 3:51e64f5e42b8 | 84 | data = (PrefSender*)frame->message.data; |
recotana | 3:51e64f5e42b8 | 85 | data->xbeeAdress = ntohl(data->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 86 | data->currentFrameNumber = ntohs(data->currentFrameNumber); |
recotana | 3:51e64f5e42b8 | 87 | } |
recotana | 3:51e64f5e42b8 | 88 | else if(frame->message.device == LED_RECIEVER) |
recotana | 3:51e64f5e42b8 | 89 | { |
recotana | 3:51e64f5e42b8 | 90 | PrefReciever *data; |
recotana | 3:51e64f5e42b8 | 91 | data = (PrefReciever*)frame->message.data; |
recotana | 3:51e64f5e42b8 | 92 | data->xbeeAdress = ntohl(data->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 93 | } |
recotana | 3:51e64f5e42b8 | 94 | } |
recotana | 0:e5342390978b | 95 | } |
recotana | 0:e5342390978b | 96 | |
recotana | 3:51e64f5e42b8 | 97 | |
recotana | 3:51e64f5e42b8 | 98 | |
recotana | 0:e5342390978b | 99 | void initTestPayloadData(Payload *_payload) |
recotana | 0:e5342390978b | 100 | { |
recotana | 0:e5342390978b | 101 | Frame *frame=&_payload->frame; |
recotana | 0:e5342390978b | 102 | |
recotana | 0:e5342390978b | 103 | initPayload(_payload); |
recotana | 0:e5342390978b | 104 | _payload->size=sizeof(Payload); |
recotana | 0:e5342390978b | 105 | |
recotana | 3:51e64f5e42b8 | 106 | // frame->id.head=0x0000; |
recotana | 3:51e64f5e42b8 | 107 | // uint8_t tmpid[14]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44}; |
recotana | 3:51e64f5e42b8 | 108 | uint8_t tmpid[16]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45,0x46}; |
recotana | 0:e5342390978b | 109 | for(int i=0;i<sizeof(tmpid);i++) frame->id.personalId[i]=tmpid[i]; |
recotana | 0:e5342390978b | 110 | |
recotana | 0:e5342390978b | 111 | |
recotana | 0:e5342390978b | 112 | frame->message.frameNumber=1; |
recotana | 3:51e64f5e42b8 | 113 | frame->message.device=SERVER; |
recotana | 0:e5342390978b | 114 | frame->message.command=SEND_SINGLE; |
recotana | 0:e5342390978b | 115 | |
recotana | 3:51e64f5e42b8 | 116 | uint8_t tmpdata[40]={ |
recotana | 0:e5342390978b | 117 | 1,2,3,4,5,6,7,8,9,10, |
recotana | 0:e5342390978b | 118 | 11,12,13,14,15,16,17,18,19,20, |
recotana | 0:e5342390978b | 119 | 21,22,23,24,25,26,27,28,29,30, |
recotana | 3:51e64f5e42b8 | 120 | 31,32,33,34,35,36,37,38,39,40}; |
recotana | 0:e5342390978b | 121 | for(int i=0;i<sizeof(tmpdata);i++) frame->message.data[i]=tmpdata[i]; |
recotana | 0:e5342390978b | 122 | |
recotana | 0:e5342390978b | 123 | frame->message.totalSize= |
recotana | 0:e5342390978b | 124 | sizeof(frame->message.totalSize) |
recotana | 0:e5342390978b | 125 | +sizeof(frame->message.frameNumber) |
recotana | 0:e5342390978b | 126 | +sizeof(frame->message.device) |
recotana | 0:e5342390978b | 127 | +sizeof(frame->message.command) |
recotana | 0:e5342390978b | 128 | +sizeof(frame->message.data); |
recotana | 3:51e64f5e42b8 | 129 | |
recotana | 3:51e64f5e42b8 | 130 | uint16_t sum = culcSum(_payload); |
recotana | 3:51e64f5e42b8 | 131 | |
recotana | 3:51e64f5e42b8 | 132 | frame->message.sum = sum; |
recotana | 0:e5342390978b | 133 | |
recotana | 0:e5342390978b | 134 | } |
recotana | 0:e5342390978b | 135 | |
recotana | 3:51e64f5e42b8 | 136 | uint16_t culcSum(Payload *_payload){ |
recotana | 3:51e64f5e42b8 | 137 | |
recotana | 3:51e64f5e42b8 | 138 | uint16_t tmpSum = _payload->ftype; |
recotana | 3:51e64f5e42b8 | 139 | uint8_t *data = (uint8_t *)&_payload->frame; |
recotana | 3:51e64f5e42b8 | 140 | |
recotana | 3:51e64f5e42b8 | 141 | for(uint8_t i=0 ; i<sizeof(Frame)-2 ; i++){ |
recotana | 3:51e64f5e42b8 | 142 | tmpSum += data[i]; |
recotana | 3:51e64f5e42b8 | 143 | } |
recotana | 3:51e64f5e42b8 | 144 | return tmpSum; |
recotana | 3:51e64f5e42b8 | 145 | } |
recotana | 0:e5342390978b | 146 | |
recotana | 0:e5342390978b | 147 | void dumpPayload(Payload *_payload) |
recotana | 0:e5342390978b | 148 | { |
recotana | 0:e5342390978b | 149 | Frame *frame=&_payload->frame; |
recotana | 3:51e64f5e42b8 | 150 | pc.printf("\r\n======== dump!!\r\n"); |
recotana | 3:51e64f5e42b8 | 151 | pc.printf("payload\nsize:%04X\r\n ",_payload->size); |
recotana | 3:51e64f5e42b8 | 152 | pc.printf("Type:%0X\r\n ",_payload->ftype); |
recotana | 0:e5342390978b | 153 | |
recotana | 3:51e64f5e42b8 | 154 | // pc.printf("Payload id_head:%04X\r\n personal id:",_payload->frame.id.head); |
recotana | 3:51e64f5e42b8 | 155 | pc.printf("personal id:"); |
recotana | 0:e5342390978b | 156 | for(int i=0 ; i < sizeof(frame->id.personalId) ; i++){ |
recotana | 3:51e64f5e42b8 | 157 | pc.printf("%02X:%c ",frame->id.personalId[i],frame->id.personalId[i]); |
recotana | 0:e5342390978b | 158 | } |
recotana | 0:e5342390978b | 159 | |
recotana | 3:51e64f5e42b8 | 160 | pc.printf("\r\n"); |
recotana | 0:e5342390978b | 161 | |
recotana | 3:51e64f5e42b8 | 162 | pc.printf("total size:%d , frame number:%d , divece:%0X , command:%02X , sum:%04X\r\n" |
recotana | 0:e5342390978b | 163 | ,frame->message.totalSize |
recotana | 0:e5342390978b | 164 | ,frame->message.frameNumber |
recotana | 0:e5342390978b | 165 | ,frame->message.device |
recotana | 3:51e64f5e42b8 | 166 | ,frame->message.command |
recotana | 3:51e64f5e42b8 | 167 | ,frame->message.sum); |
recotana | 0:e5342390978b | 168 | |
recotana | 3:51e64f5e42b8 | 169 | pc.printf("message data:\r\n"); |
recotana | 0:e5342390978b | 170 | for(int i=0 ; i < sizeof(frame->message.data) ; i++){ |
recotana | 3:51e64f5e42b8 | 171 | pc.printf("%d:%02X ",i,_payload->frame.message.data[i]); |
recotana | 0:e5342390978b | 172 | } |
recotana | 0:e5342390978b | 173 | |
recotana | 3:51e64f5e42b8 | 174 | pc.printf("\r\n--- RAW DATA -------\r\n"); |
recotana | 0:e5342390978b | 175 | for(int i=0 ; i < sizeof(Payload) ; i++){ |
recotana | 3:51e64f5e42b8 | 176 | pc.printf("%d:%02X ",i,((uint8_t*)_payload)[i]); |
recotana | 0:e5342390978b | 177 | } |
recotana | 3:51e64f5e42b8 | 178 | pc.printf("\r\n"); |
recotana | 0:e5342390978b | 179 | |
recotana | 3:51e64f5e42b8 | 180 | } |
recotana | 3:51e64f5e42b8 | 181 | |
recotana | 3:51e64f5e42b8 | 182 | void dumpPrefSender(PrefSender *prf) |
recotana | 3:51e64f5e42b8 | 183 | { |
recotana | 3:51e64f5e42b8 | 184 | pc.printf("current frame number:%d\n",prf->currentFrameNumber); |
recotana | 3:51e64f5e42b8 | 185 | pc.printf("gainData gain:%d\n",prf->gainData.gain); |
recotana | 3:51e64f5e42b8 | 186 | pc.printf("gainData middleLevel:%d\n",prf->gainData.middleLevel); |
recotana | 3:51e64f5e42b8 | 187 | pc.printf("lightsw:%d\n",prf->lightsw); |
recotana | 3:51e64f5e42b8 | 188 | pc.printf("continueusMode:%d\n",prf->continueusMode); |
recotana | 3:51e64f5e42b8 | 189 | pc.printf("isSend:%d\n",prf->isSend); |
recotana | 3:51e64f5e42b8 | 190 | pc.printf("xbeeAdress:%X\n",prf->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 191 | |
recotana | 3:51e64f5e42b8 | 192 | } |
recotana | 3:51e64f5e42b8 | 193 | void dumpPrefReciever(PrefReciever *prf) |
recotana | 3:51e64f5e42b8 | 194 | { |
recotana | 3:51e64f5e42b8 | 195 | |
recotana | 3:51e64f5e42b8 | 196 | pc.printf("xbeeAdress:%X\n",prf->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 197 | |
recotana | 3:51e64f5e42b8 | 198 | } |
recotana | 3:51e64f5e42b8 | 199 | void dumpPrefServer(PrefServer *prf) |
recotana | 3:51e64f5e42b8 | 200 | { |
recotana | 3:51e64f5e42b8 | 201 | pc.printf("xbeeAdress:%X\n",prf->xbeeAdress); |
recotana | 3:51e64f5e42b8 | 202 | pc.printf("server ip:%d.%d.%d.%d\n",prf->serverAddress[0],prf->serverAddress[1],prf->serverAddress[2],prf->serverAddress[3]); |
recotana | 3:51e64f5e42b8 | 203 | pc.printf("subnet ip:%d.%d.%d.%d\n",prf->subnetMask[0],prf->subnetMask[1],prf->subnetMask[2],prf->subnetMask[3]); |
recotana | 3:51e64f5e42b8 | 204 | pc.printf("router ip:%d.%d.%d.%d\n",prf->routerAddress[0],prf->routerAddress[1],prf->routerAddress[2],prf->routerAddress[3]); |
recotana | 3:51e64f5e42b8 | 205 | |
recotana | 3:51e64f5e42b8 | 206 | pc.printf("Server Port:%d\n",prf->serverPort); |
recotana | 3:51e64f5e42b8 | 207 | |
recotana | 3:51e64f5e42b8 | 208 | pc.printf("Destination ip:%d.%d.%d.%d\n",prf->destinationAddress[0],prf->destinationAddress[1],prf->destinationAddress[2],prf->destinationAddress[3]); |
recotana | 3:51e64f5e42b8 | 209 | pc.printf("Destination Port:%d\n",prf->destinationPort); |
recotana | 3:51e64f5e42b8 | 210 | |
recotana | 0:e5342390978b | 211 | } |