sevencore fota solution development project
ext_fota/SerialManager.cpp@6:8dd20294b2aa, 2015-07-17 (annotated)
- Committer:
- dudnwjs
- Date:
- Fri Jul 17 14:04:12 2015 +0000
- Revision:
- 6:8dd20294b2aa
- Parent:
- 5:e11b23f9aacc
- Child:
- 9:fcf91f563147
EXT DEMO FILE OPEN COMPLETE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudnwjs | 2:dba344c91bce | 1 | #include "SerialManager.h" |
dudnwjs | 2:dba344c91bce | 2 | |
dudnwjs | 2:dba344c91bce | 3 | namespace sevencore_fota{ |
dudnwjs | 2:dba344c91bce | 4 | |
dudnwjs | 2:dba344c91bce | 5 | |
dudnwjs | 2:dba344c91bce | 6 | SerialManager::SerialManager(Serial *_device) |
dudnwjs | 2:dba344c91bce | 7 | { |
dudnwjs | 2:dba344c91bce | 8 | print_flag = 0; |
dudnwjs | 2:dba344c91bce | 9 | device = _device; |
dudnwjs | 2:dba344c91bce | 10 | FE_MSG_PACKET_TYPE = 0x05; |
dudnwjs | 2:dba344c91bce | 11 | MAX_PACKET_LENGTH = 350; |
dudnwjs | 2:dba344c91bce | 12 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 13 | wDataLength = 0; |
dudnwjs | 2:dba344c91bce | 14 | wReceivePos = 0; |
dudnwjs | 2:dba344c91bce | 15 | bHdrBytesRead = 0; |
dudnwjs | 2:dba344c91bce | 16 | memset(bReceiveElementArr,0,512); |
dudnwjs | 2:dba344c91bce | 17 | } |
dudnwjs | 2:dba344c91bce | 18 | |
dudnwjs | 2:dba344c91bce | 19 | SerialManager::SerialManager(Serial *_device, Serial *_hostpc) |
dudnwjs | 2:dba344c91bce | 20 | { |
dudnwjs | 2:dba344c91bce | 21 | print_flag = 1; |
dudnwjs | 2:dba344c91bce | 22 | device = _device; |
dudnwjs | 2:dba344c91bce | 23 | hostpc = _hostpc; |
dudnwjs | 2:dba344c91bce | 24 | FE_MSG_PACKET_TYPE = 0x05; |
dudnwjs | 2:dba344c91bce | 25 | MAX_PACKET_LENGTH = 350; |
dudnwjs | 2:dba344c91bce | 26 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 27 | wDataLength = 0; |
dudnwjs | 2:dba344c91bce | 28 | wReceivePos = 0; |
dudnwjs | 2:dba344c91bce | 29 | bHdrBytesRead = 0; |
dudnwjs | 2:dba344c91bce | 30 | memset(bReceiveElementArr,0,512); |
dudnwjs | 2:dba344c91bce | 31 | PrintSerialManager(); |
dudnwjs | 2:dba344c91bce | 32 | } |
dudnwjs | 2:dba344c91bce | 33 | |
dudnwjs | 2:dba344c91bce | 34 | SerialManager::~SerialManager(void) |
dudnwjs | 2:dba344c91bce | 35 | { |
dudnwjs | 2:dba344c91bce | 36 | } |
dudnwjs | 2:dba344c91bce | 37 | |
dudnwjs | 2:dba344c91bce | 38 | void SerialManager::PrintSerialManager(void) |
dudnwjs | 2:dba344c91bce | 39 | { |
dudnwjs | 2:dba344c91bce | 40 | if(print_flag == 1) |
dudnwjs | 5:e11b23f9aacc | 41 | hostpc->printf("\nSerialManager Start!!\n"); |
dudnwjs | 2:dba344c91bce | 42 | } |
dudnwjs | 2:dba344c91bce | 43 | |
dudnwjs | 2:dba344c91bce | 44 | int SerialManager::SendToSerial(uint8_t *data,unsigned short size) |
dudnwjs | 2:dba344c91bce | 45 | { |
dudnwjs | 2:dba344c91bce | 46 | int cnt = 0; |
dudnwjs | 2:dba344c91bce | 47 | if(print_flag == 1) |
dudnwjs | 5:e11b23f9aacc | 48 | hostpc->printf("\n>>> send size = %hu\n",size); |
dudnwjs | 2:dba344c91bce | 49 | |
dudnwjs | 2:dba344c91bce | 50 | for(int i=0; i < size; i++) |
dudnwjs | 2:dba344c91bce | 51 | { |
dudnwjs | 2:dba344c91bce | 52 | if( device->writeable()) |
dudnwjs | 2:dba344c91bce | 53 | { |
dudnwjs | 2:dba344c91bce | 54 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 55 | hostpc->printf("%02X ", data[i]); |
dudnwjs | 2:dba344c91bce | 56 | device->putc(data[i]); |
dudnwjs | 2:dba344c91bce | 57 | cnt++; |
dudnwjs | 2:dba344c91bce | 58 | } |
dudnwjs | 2:dba344c91bce | 59 | } |
dudnwjs | 2:dba344c91bce | 60 | return cnt; |
dudnwjs | 2:dba344c91bce | 61 | } |
dudnwjs | 2:dba344c91bce | 62 | |
dudnwjs | 2:dba344c91bce | 63 | // return boolean : msg_size (receive complete) or -1 (receive not complete) |
dudnwjs | 2:dba344c91bce | 64 | int SerialManager::ReceiveToSerial(unsigned char *receive_msg) |
dudnwjs | 2:dba344c91bce | 65 | { |
dudnwjs | 2:dba344c91bce | 66 | int ret = -1; |
dudnwjs | 2:dba344c91bce | 67 | unsigned char tmp; |
dudnwjs | 2:dba344c91bce | 68 | if(device->readable()) |
dudnwjs | 2:dba344c91bce | 69 | { |
dudnwjs | 2:dba344c91bce | 70 | tmp = device->getc(); |
dudnwjs | 2:dba344c91bce | 71 | switch(bReceiveState) |
dudnwjs | 2:dba344c91bce | 72 | { |
dudnwjs | 2:dba344c91bce | 73 | case 0: |
dudnwjs | 2:dba344c91bce | 74 | if( tmp == FE_MSG_PACKET_TYPE ) |
dudnwjs | 2:dba344c91bce | 75 | { |
dudnwjs | 2:dba344c91bce | 76 | bReceiveState = 1; |
dudnwjs | 2:dba344c91bce | 77 | wDataLength = 0; |
dudnwjs | 2:dba344c91bce | 78 | wReceivePos = 0; |
dudnwjs | 2:dba344c91bce | 79 | bHdrBytesRead = 0; |
dudnwjs | 5:e11b23f9aacc | 80 | memset(bReceiveElementArr,0,512); |
dudnwjs | 2:dba344c91bce | 81 | |
dudnwjs | 2:dba344c91bce | 82 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 83 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 84 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 85 | hostpc->printf("\n[Receiver] Packet Type |: %02X \n", tmp); |
dudnwjs | 2:dba344c91bce | 86 | }else |
dudnwjs | 2:dba344c91bce | 87 | { |
dudnwjs | 2:dba344c91bce | 88 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 89 | hostpc->printf("\n[Receiver] Packet Type Error |: %02X \n", tmp); |
dudnwjs | 2:dba344c91bce | 90 | } |
dudnwjs | 2:dba344c91bce | 91 | break; |
dudnwjs | 2:dba344c91bce | 92 | case 1: |
dudnwjs | 2:dba344c91bce | 93 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 94 | hostpc->printf("R-%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 95 | bHdrBytesRead++; |
dudnwjs | 2:dba344c91bce | 96 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 97 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 98 | if( bHdrBytesRead == 6 ) |
dudnwjs | 2:dba344c91bce | 99 | bReceiveState = 2; |
dudnwjs | 2:dba344c91bce | 100 | break; |
dudnwjs | 2:dba344c91bce | 101 | case 2: |
dudnwjs | 2:dba344c91bce | 102 | if(print_flag == 1) |
dudnwjs | 3:1e70387e1337 | 103 | hostpc->printf("R+%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 104 | wDataLength += tmp; |
dudnwjs | 2:dba344c91bce | 105 | if( wDataLength > MAX_PACKET_LENGTH ) |
dudnwjs | 2:dba344c91bce | 106 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 107 | else |
dudnwjs | 2:dba344c91bce | 108 | { |
dudnwjs | 2:dba344c91bce | 109 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 110 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 111 | bReceiveState = 3; |
dudnwjs | 2:dba344c91bce | 112 | } |
dudnwjs | 2:dba344c91bce | 113 | break; |
dudnwjs | 2:dba344c91bce | 114 | case 3: |
dudnwjs | 2:dba344c91bce | 115 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 116 | hostpc->printf("R=%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 117 | wDataLength += (unsigned short) (tmp*256); |
dudnwjs | 2:dba344c91bce | 118 | if( wDataLength > MAX_PACKET_LENGTH ) |
dudnwjs | 2:dba344c91bce | 119 | { |
dudnwjs | 2:dba344c91bce | 120 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 121 | hostpc->printf("\n[Receiver] Over SIZE: %d \n", wDataLength); |
dudnwjs | 2:dba344c91bce | 122 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 123 | }else if (wDataLength == 0) |
dudnwjs | 2:dba344c91bce | 124 | { |
dudnwjs | 2:dba344c91bce | 125 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 126 | hostpc->printf("\n[Receiver] Zero SIZE: %d \n", wDataLength); |
dudnwjs | 2:dba344c91bce | 127 | memcpy(receive_msg,bReceiveElementArr,wReceivePos); |
dudnwjs | 2:dba344c91bce | 128 | ret = wReceivePos; |
dudnwjs | 2:dba344c91bce | 129 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 130 | }else |
dudnwjs | 2:dba344c91bce | 131 | { |
dudnwjs | 2:dba344c91bce | 132 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 133 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 134 | bReceiveState = 4; |
dudnwjs | 2:dba344c91bce | 135 | } |
dudnwjs | 2:dba344c91bce | 136 | break; |
dudnwjs | 2:dba344c91bce | 137 | case 4: |
dudnwjs | 2:dba344c91bce | 138 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 139 | hostpc->printf("R~%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 140 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 141 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 142 | // 9 = 1(first byte = FE_MSG_PACKET_TYPE) + 2(type) +2(dstid) +2(srcid) +2(length size) |
dudnwjs | 2:dba344c91bce | 143 | if(wReceivePos == wDataLength + 9) |
dudnwjs | 2:dba344c91bce | 144 | { |
dudnwjs | 2:dba344c91bce | 145 | memcpy(receive_msg,bReceiveElementArr,wReceivePos); |
dudnwjs | 2:dba344c91bce | 146 | ret = wReceivePos; |
dudnwjs | 2:dba344c91bce | 147 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 148 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 149 | hostpc->printf("\n[Receiver] Rcv Data SIZE: %d \n", wDataLength); |
dudnwjs | 2:dba344c91bce | 150 | } |
dudnwjs | 2:dba344c91bce | 151 | break; |
dudnwjs | 2:dba344c91bce | 152 | default: |
dudnwjs | 2:dba344c91bce | 153 | if(print_flag == 1) |
dudnwjs | 5:e11b23f9aacc | 154 | hostpc->printf("\nERROR STRAGE STATE R+%02X \n",tmp); |
dudnwjs | 2:dba344c91bce | 155 | break; |
dudnwjs | 2:dba344c91bce | 156 | } |
dudnwjs | 2:dba344c91bce | 157 | } |
dudnwjs | 2:dba344c91bce | 158 | |
dudnwjs | 2:dba344c91bce | 159 | return ret; |
dudnwjs | 2:dba344c91bce | 160 | } |
dudnwjs | 2:dba344c91bce | 161 | |
dudnwjs | 6:8dd20294b2aa | 162 | void SerialManager::DataReceive(uint8_t *databuf, unsigned short size) |
dudnwjs | 6:8dd20294b2aa | 163 | { |
dudnwjs | 6:8dd20294b2aa | 164 | int i; |
dudnwjs | 6:8dd20294b2aa | 165 | |
dudnwjs | 6:8dd20294b2aa | 166 | for(i=0;i<size;i++) |
dudnwjs | 6:8dd20294b2aa | 167 | { |
dudnwjs | 6:8dd20294b2aa | 168 | databuf[i] = (uint8_t)device->getc(); |
dudnwjs | 6:8dd20294b2aa | 169 | } |
dudnwjs | 6:8dd20294b2aa | 170 | } |
dudnwjs | 6:8dd20294b2aa | 171 | |
dudnwjs | 5:e11b23f9aacc | 172 | void SerialManager::ReceiveToSerialTest(void) |
dudnwjs | 5:e11b23f9aacc | 173 | { |
dudnwjs | 5:e11b23f9aacc | 174 | unsigned char tmp; |
dudnwjs | 5:e11b23f9aacc | 175 | int pos = 0; |
dudnwjs | 6:8dd20294b2aa | 176 | char buff[1024]; |
dudnwjs | 6:8dd20294b2aa | 177 | memset(buff,'K',1023); |
dudnwjs | 6:8dd20294b2aa | 178 | buff[1023] = '\0'; |
dudnwjs | 5:e11b23f9aacc | 179 | hostpc->printf("\n pos = %d FIRST <%s> \n",pos,buff); |
dudnwjs | 5:e11b23f9aacc | 180 | tmp = device->getc(); |
dudnwjs | 6:8dd20294b2aa | 181 | if( tmp == 0x80 ){ |
dudnwjs | 6:8dd20294b2aa | 182 | tmp = device->getc(); |
dudnwjs | 6:8dd20294b2aa | 183 | if( tmp == 0x46 ) |
dudnwjs | 6:8dd20294b2aa | 184 | { |
dudnwjs | 6:8dd20294b2aa | 185 | hostpc->printf("\n FIRMWARE DATA CONFIRM!! \n"); |
dudnwjs | 6:8dd20294b2aa | 186 | } |
dudnwjs | 6:8dd20294b2aa | 187 | } |
dudnwjs | 6:8dd20294b2aa | 188 | tmp = device->getc();//reserved value |
dudnwjs | 6:8dd20294b2aa | 189 | tmp = device->getc();//reserved value |
dudnwjs | 6:8dd20294b2aa | 190 | tmp = device->getc();//reserved value |
dudnwjs | 5:e11b23f9aacc | 191 | while(1) |
dudnwjs | 5:e11b23f9aacc | 192 | { |
dudnwjs | 5:e11b23f9aacc | 193 | if(device->readable()) |
dudnwjs | 5:e11b23f9aacc | 194 | { |
dudnwjs | 5:e11b23f9aacc | 195 | tmp = device->getc(); |
dudnwjs | 6:8dd20294b2aa | 196 | //hostpc->printf("%d ==> 0x%X\n",pos,tmp); |
dudnwjs | 5:e11b23f9aacc | 197 | buff[pos] = tmp; |
dudnwjs | 6:8dd20294b2aa | 198 | pos++; |
dudnwjs | 6:8dd20294b2aa | 199 | if( pos%512 == 0 ){ |
dudnwjs | 6:8dd20294b2aa | 200 | buff[1023] = '\0'; |
dudnwjs | 6:8dd20294b2aa | 201 | hostpc->printf("\n pos = %d CHECK <%s> \n",pos,buff); |
dudnwjs | 5:e11b23f9aacc | 202 | } |
dudnwjs | 6:8dd20294b2aa | 203 | if( pos == 1024 ){ |
dudnwjs | 6:8dd20294b2aa | 204 | pos = 0; |
dudnwjs | 6:8dd20294b2aa | 205 | memset(buff,'Z',1023); |
dudnwjs | 6:8dd20294b2aa | 206 | buff[1023] = '\0'; |
dudnwjs | 5:e11b23f9aacc | 207 | } |
dudnwjs | 5:e11b23f9aacc | 208 | } |
dudnwjs | 5:e11b23f9aacc | 209 | |
dudnwjs | 5:e11b23f9aacc | 210 | } |
dudnwjs | 6:8dd20294b2aa | 211 | |
dudnwjs | 6:8dd20294b2aa | 212 | |
dudnwjs | 5:e11b23f9aacc | 213 | } |
dudnwjs | 5:e11b23f9aacc | 214 | |
dudnwjs | 2:dba344c91bce | 215 | }//namespace |