sevencore fota solution development project
ext_fota/SerialManager.cpp@9:fcf91f563147, 2015-08-24 (annotated)
- Committer:
- dudnwjs
- Date:
- Mon Aug 24 02:29:16 2015 +0000
- Revision:
- 9:fcf91f563147
- Parent:
- 6:8dd20294b2aa
Add comment for form doxygen;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudnwjs | 9:fcf91f563147 | 1 | /** |
dudnwjs | 9:fcf91f563147 | 2 | * @file SerialManager.cpp |
dudnwjs | 9:fcf91f563147 | 3 | * @brief Serial Communication Management |
dudnwjs | 9:fcf91f563147 | 4 | * Copyright 2015 SEVENCORE Co., Ltd. |
dudnwjs | 9:fcf91f563147 | 5 | * |
dudnwjs | 9:fcf91f563147 | 6 | * @author HyeongJun Kim |
dudnwjs | 9:fcf91f563147 | 7 | * @version 1.0.0 |
dudnwjs | 9:fcf91f563147 | 8 | * @date 2015-08-19 |
dudnwjs | 9:fcf91f563147 | 9 | */ |
dudnwjs | 2:dba344c91bce | 10 | #include "SerialManager.h" |
dudnwjs | 9:fcf91f563147 | 11 | /** |
dudnwjs | 9:fcf91f563147 | 12 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 13 | * @addtogroup ext_fota module |
dudnwjs | 9:fcf91f563147 | 14 | * @brief Serial Manager Class Method Definition. |
dudnwjs | 9:fcf91f563147 | 15 | * |
dudnwjs | 9:fcf91f563147 | 16 | * @{ |
dudnwjs | 9:fcf91f563147 | 17 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 18 | */ |
dudnwjs | 2:dba344c91bce | 19 | namespace sevencore_fota{ |
dudnwjs | 2:dba344c91bce | 20 | |
dudnwjs | 9:fcf91f563147 | 21 | /** |
dudnwjs | 9:fcf91f563147 | 22 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 23 | * @brief Serial Manager constructor only connected Device. |
dudnwjs | 9:fcf91f563147 | 24 | * @param[in] mbed serial class reference to device. |
dudnwjs | 9:fcf91f563147 | 25 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 26 | */ |
dudnwjs | 2:dba344c91bce | 27 | SerialManager::SerialManager(Serial *_device) |
dudnwjs | 2:dba344c91bce | 28 | { |
dudnwjs | 2:dba344c91bce | 29 | print_flag = 0; |
dudnwjs | 2:dba344c91bce | 30 | device = _device; |
dudnwjs | 2:dba344c91bce | 31 | FE_MSG_PACKET_TYPE = 0x05; |
dudnwjs | 2:dba344c91bce | 32 | MAX_PACKET_LENGTH = 350; |
dudnwjs | 2:dba344c91bce | 33 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 34 | wDataLength = 0; |
dudnwjs | 2:dba344c91bce | 35 | wReceivePos = 0; |
dudnwjs | 2:dba344c91bce | 36 | bHdrBytesRead = 0; |
dudnwjs | 2:dba344c91bce | 37 | memset(bReceiveElementArr,0,512); |
dudnwjs | 2:dba344c91bce | 38 | } |
dudnwjs | 9:fcf91f563147 | 39 | /** |
dudnwjs | 9:fcf91f563147 | 40 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 41 | * @brief Serial Manager constructor with connected Device & HostPC. |
dudnwjs | 9:fcf91f563147 | 42 | * @param[in] mbed serial class reference to device. |
dudnwjs | 9:fcf91f563147 | 43 | * @param[in] mbed serial class reference to hostpc. |
dudnwjs | 9:fcf91f563147 | 44 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 45 | */ |
dudnwjs | 2:dba344c91bce | 46 | SerialManager::SerialManager(Serial *_device, Serial *_hostpc) |
dudnwjs | 2:dba344c91bce | 47 | { |
dudnwjs | 2:dba344c91bce | 48 | print_flag = 1; |
dudnwjs | 2:dba344c91bce | 49 | device = _device; |
dudnwjs | 2:dba344c91bce | 50 | hostpc = _hostpc; |
dudnwjs | 2:dba344c91bce | 51 | FE_MSG_PACKET_TYPE = 0x05; |
dudnwjs | 2:dba344c91bce | 52 | MAX_PACKET_LENGTH = 350; |
dudnwjs | 2:dba344c91bce | 53 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 54 | wDataLength = 0; |
dudnwjs | 2:dba344c91bce | 55 | wReceivePos = 0; |
dudnwjs | 2:dba344c91bce | 56 | bHdrBytesRead = 0; |
dudnwjs | 2:dba344c91bce | 57 | memset(bReceiveElementArr,0,512); |
dudnwjs | 2:dba344c91bce | 58 | PrintSerialManager(); |
dudnwjs | 2:dba344c91bce | 59 | } |
dudnwjs | 9:fcf91f563147 | 60 | /** |
dudnwjs | 9:fcf91f563147 | 61 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 62 | * @brief Serial Manager destructor. |
dudnwjs | 9:fcf91f563147 | 63 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 64 | */ |
dudnwjs | 2:dba344c91bce | 65 | SerialManager::~SerialManager(void) |
dudnwjs | 2:dba344c91bce | 66 | { |
dudnwjs | 2:dba344c91bce | 67 | } |
dudnwjs | 9:fcf91f563147 | 68 | /** |
dudnwjs | 9:fcf91f563147 | 69 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 70 | * @brief Serial Manager Start title print function. |
dudnwjs | 9:fcf91f563147 | 71 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 72 | */ |
dudnwjs | 2:dba344c91bce | 73 | void SerialManager::PrintSerialManager(void) |
dudnwjs | 2:dba344c91bce | 74 | { |
dudnwjs | 2:dba344c91bce | 75 | if(print_flag == 1) |
dudnwjs | 5:e11b23f9aacc | 76 | hostpc->printf("\nSerialManager Start!!\n"); |
dudnwjs | 2:dba344c91bce | 77 | } |
dudnwjs | 9:fcf91f563147 | 78 | /** |
dudnwjs | 9:fcf91f563147 | 79 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 80 | * @brief Send ble message to serial port. |
dudnwjs | 9:fcf91f563147 | 81 | * @param[in] Sending data pointer. |
dudnwjs | 9:fcf91f563147 | 82 | * @param[in] Sending data size. |
dudnwjs | 9:fcf91f563147 | 83 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 84 | */ |
dudnwjs | 2:dba344c91bce | 85 | int SerialManager::SendToSerial(uint8_t *data,unsigned short size) |
dudnwjs | 2:dba344c91bce | 86 | { |
dudnwjs | 2:dba344c91bce | 87 | int cnt = 0; |
dudnwjs | 2:dba344c91bce | 88 | if(print_flag == 1) |
dudnwjs | 5:e11b23f9aacc | 89 | hostpc->printf("\n>>> send size = %hu\n",size); |
dudnwjs | 2:dba344c91bce | 90 | |
dudnwjs | 2:dba344c91bce | 91 | for(int i=0; i < size; i++) |
dudnwjs | 2:dba344c91bce | 92 | { |
dudnwjs | 2:dba344c91bce | 93 | if( device->writeable()) |
dudnwjs | 2:dba344c91bce | 94 | { |
dudnwjs | 2:dba344c91bce | 95 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 96 | hostpc->printf("%02X ", data[i]); |
dudnwjs | 2:dba344c91bce | 97 | device->putc(data[i]); |
dudnwjs | 2:dba344c91bce | 98 | cnt++; |
dudnwjs | 2:dba344c91bce | 99 | } |
dudnwjs | 2:dba344c91bce | 100 | } |
dudnwjs | 2:dba344c91bce | 101 | return cnt; |
dudnwjs | 2:dba344c91bce | 102 | } |
dudnwjs | 9:fcf91f563147 | 103 | /** |
dudnwjs | 9:fcf91f563147 | 104 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 105 | * @brief Receive ble message from serial port. |
dudnwjs | 9:fcf91f563147 | 106 | * @param[in] Storing data pointer. |
dudnwjs | 9:fcf91f563147 | 107 | * @return Boolean - msg_size (receive complete) or -1 (receive not complete) |
dudnwjs | 9:fcf91f563147 | 108 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 109 | */ |
dudnwjs | 2:dba344c91bce | 110 | int SerialManager::ReceiveToSerial(unsigned char *receive_msg) |
dudnwjs | 2:dba344c91bce | 111 | { |
dudnwjs | 2:dba344c91bce | 112 | int ret = -1; |
dudnwjs | 2:dba344c91bce | 113 | unsigned char tmp; |
dudnwjs | 2:dba344c91bce | 114 | if(device->readable()) |
dudnwjs | 2:dba344c91bce | 115 | { |
dudnwjs | 2:dba344c91bce | 116 | tmp = device->getc(); |
dudnwjs | 2:dba344c91bce | 117 | switch(bReceiveState) |
dudnwjs | 2:dba344c91bce | 118 | { |
dudnwjs | 2:dba344c91bce | 119 | case 0: |
dudnwjs | 2:dba344c91bce | 120 | if( tmp == FE_MSG_PACKET_TYPE ) |
dudnwjs | 2:dba344c91bce | 121 | { |
dudnwjs | 2:dba344c91bce | 122 | bReceiveState = 1; |
dudnwjs | 2:dba344c91bce | 123 | wDataLength = 0; |
dudnwjs | 2:dba344c91bce | 124 | wReceivePos = 0; |
dudnwjs | 2:dba344c91bce | 125 | bHdrBytesRead = 0; |
dudnwjs | 5:e11b23f9aacc | 126 | memset(bReceiveElementArr,0,512); |
dudnwjs | 2:dba344c91bce | 127 | |
dudnwjs | 2:dba344c91bce | 128 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 129 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 130 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 131 | hostpc->printf("\n[Receiver] Packet Type |: %02X \n", tmp); |
dudnwjs | 2:dba344c91bce | 132 | }else |
dudnwjs | 2:dba344c91bce | 133 | { |
dudnwjs | 2:dba344c91bce | 134 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 135 | hostpc->printf("\n[Receiver] Packet Type Error |: %02X \n", tmp); |
dudnwjs | 2:dba344c91bce | 136 | } |
dudnwjs | 2:dba344c91bce | 137 | break; |
dudnwjs | 2:dba344c91bce | 138 | case 1: |
dudnwjs | 2:dba344c91bce | 139 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 140 | hostpc->printf("R-%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 141 | bHdrBytesRead++; |
dudnwjs | 2:dba344c91bce | 142 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 143 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 144 | if( bHdrBytesRead == 6 ) |
dudnwjs | 2:dba344c91bce | 145 | bReceiveState = 2; |
dudnwjs | 2:dba344c91bce | 146 | break; |
dudnwjs | 2:dba344c91bce | 147 | case 2: |
dudnwjs | 2:dba344c91bce | 148 | if(print_flag == 1) |
dudnwjs | 3:1e70387e1337 | 149 | hostpc->printf("R+%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 150 | wDataLength += tmp; |
dudnwjs | 2:dba344c91bce | 151 | if( wDataLength > MAX_PACKET_LENGTH ) |
dudnwjs | 2:dba344c91bce | 152 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 153 | else |
dudnwjs | 2:dba344c91bce | 154 | { |
dudnwjs | 2:dba344c91bce | 155 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 156 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 157 | bReceiveState = 3; |
dudnwjs | 2:dba344c91bce | 158 | } |
dudnwjs | 2:dba344c91bce | 159 | break; |
dudnwjs | 2:dba344c91bce | 160 | case 3: |
dudnwjs | 2:dba344c91bce | 161 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 162 | hostpc->printf("R=%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 163 | wDataLength += (unsigned short) (tmp*256); |
dudnwjs | 2:dba344c91bce | 164 | if( wDataLength > MAX_PACKET_LENGTH ) |
dudnwjs | 2:dba344c91bce | 165 | { |
dudnwjs | 2:dba344c91bce | 166 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 167 | hostpc->printf("\n[Receiver] Over SIZE: %d \n", wDataLength); |
dudnwjs | 2:dba344c91bce | 168 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 169 | }else if (wDataLength == 0) |
dudnwjs | 2:dba344c91bce | 170 | { |
dudnwjs | 2:dba344c91bce | 171 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 172 | hostpc->printf("\n[Receiver] Zero SIZE: %d \n", wDataLength); |
dudnwjs | 2:dba344c91bce | 173 | memcpy(receive_msg,bReceiveElementArr,wReceivePos); |
dudnwjs | 2:dba344c91bce | 174 | ret = wReceivePos; |
dudnwjs | 2:dba344c91bce | 175 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 176 | }else |
dudnwjs | 2:dba344c91bce | 177 | { |
dudnwjs | 2:dba344c91bce | 178 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 179 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 180 | bReceiveState = 4; |
dudnwjs | 2:dba344c91bce | 181 | } |
dudnwjs | 2:dba344c91bce | 182 | break; |
dudnwjs | 2:dba344c91bce | 183 | case 4: |
dudnwjs | 2:dba344c91bce | 184 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 185 | hostpc->printf("R~%02X ",tmp); |
dudnwjs | 2:dba344c91bce | 186 | bReceiveElementArr[wReceivePos] = tmp; |
dudnwjs | 2:dba344c91bce | 187 | wReceivePos++; |
dudnwjs | 2:dba344c91bce | 188 | // 9 = 1(first byte = FE_MSG_PACKET_TYPE) + 2(type) +2(dstid) +2(srcid) +2(length size) |
dudnwjs | 2:dba344c91bce | 189 | if(wReceivePos == wDataLength + 9) |
dudnwjs | 2:dba344c91bce | 190 | { |
dudnwjs | 2:dba344c91bce | 191 | memcpy(receive_msg,bReceiveElementArr,wReceivePos); |
dudnwjs | 2:dba344c91bce | 192 | ret = wReceivePos; |
dudnwjs | 2:dba344c91bce | 193 | bReceiveState = 0; |
dudnwjs | 2:dba344c91bce | 194 | if( print_flag == 1 ) |
dudnwjs | 3:1e70387e1337 | 195 | hostpc->printf("\n[Receiver] Rcv Data SIZE: %d \n", wDataLength); |
dudnwjs | 2:dba344c91bce | 196 | } |
dudnwjs | 2:dba344c91bce | 197 | break; |
dudnwjs | 2:dba344c91bce | 198 | default: |
dudnwjs | 2:dba344c91bce | 199 | if(print_flag == 1) |
dudnwjs | 5:e11b23f9aacc | 200 | hostpc->printf("\nERROR STRAGE STATE R+%02X \n",tmp); |
dudnwjs | 2:dba344c91bce | 201 | break; |
dudnwjs | 2:dba344c91bce | 202 | } |
dudnwjs | 2:dba344c91bce | 203 | } |
dudnwjs | 2:dba344c91bce | 204 | |
dudnwjs | 2:dba344c91bce | 205 | return ret; |
dudnwjs | 2:dba344c91bce | 206 | } |
dudnwjs | 9:fcf91f563147 | 207 | /** |
dudnwjs | 9:fcf91f563147 | 208 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 209 | * @brief Receive data for size. |
dudnwjs | 9:fcf91f563147 | 210 | * @param[in] Storing data pointer. |
dudnwjs | 9:fcf91f563147 | 211 | * @param[in] transmision data size. |
dudnwjs | 9:fcf91f563147 | 212 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 213 | */ |
dudnwjs | 6:8dd20294b2aa | 214 | void SerialManager::DataReceive(uint8_t *databuf, unsigned short size) |
dudnwjs | 6:8dd20294b2aa | 215 | { |
dudnwjs | 6:8dd20294b2aa | 216 | int i; |
dudnwjs | 6:8dd20294b2aa | 217 | |
dudnwjs | 6:8dd20294b2aa | 218 | for(i=0;i<size;i++) |
dudnwjs | 6:8dd20294b2aa | 219 | { |
dudnwjs | 6:8dd20294b2aa | 220 | databuf[i] = (uint8_t)device->getc(); |
dudnwjs | 6:8dd20294b2aa | 221 | } |
dudnwjs | 6:8dd20294b2aa | 222 | } |
dudnwjs | 9:fcf91f563147 | 223 | /** |
dudnwjs | 9:fcf91f563147 | 224 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 225 | * @brief Receive data test function. |
dudnwjs | 9:fcf91f563147 | 226 | **************************************************************************************** |
dudnwjs | 9:fcf91f563147 | 227 | */ |
dudnwjs | 5:e11b23f9aacc | 228 | void SerialManager::ReceiveToSerialTest(void) |
dudnwjs | 5:e11b23f9aacc | 229 | { |
dudnwjs | 5:e11b23f9aacc | 230 | unsigned char tmp; |
dudnwjs | 5:e11b23f9aacc | 231 | int pos = 0; |
dudnwjs | 6:8dd20294b2aa | 232 | char buff[1024]; |
dudnwjs | 6:8dd20294b2aa | 233 | memset(buff,'K',1023); |
dudnwjs | 6:8dd20294b2aa | 234 | buff[1023] = '\0'; |
dudnwjs | 5:e11b23f9aacc | 235 | hostpc->printf("\n pos = %d FIRST <%s> \n",pos,buff); |
dudnwjs | 5:e11b23f9aacc | 236 | tmp = device->getc(); |
dudnwjs | 6:8dd20294b2aa | 237 | if( tmp == 0x80 ){ |
dudnwjs | 6:8dd20294b2aa | 238 | tmp = device->getc(); |
dudnwjs | 6:8dd20294b2aa | 239 | if( tmp == 0x46 ) |
dudnwjs | 6:8dd20294b2aa | 240 | { |
dudnwjs | 6:8dd20294b2aa | 241 | hostpc->printf("\n FIRMWARE DATA CONFIRM!! \n"); |
dudnwjs | 6:8dd20294b2aa | 242 | } |
dudnwjs | 6:8dd20294b2aa | 243 | } |
dudnwjs | 6:8dd20294b2aa | 244 | tmp = device->getc();//reserved value |
dudnwjs | 6:8dd20294b2aa | 245 | tmp = device->getc();//reserved value |
dudnwjs | 6:8dd20294b2aa | 246 | tmp = device->getc();//reserved value |
dudnwjs | 5:e11b23f9aacc | 247 | while(1) |
dudnwjs | 5:e11b23f9aacc | 248 | { |
dudnwjs | 5:e11b23f9aacc | 249 | if(device->readable()) |
dudnwjs | 5:e11b23f9aacc | 250 | { |
dudnwjs | 5:e11b23f9aacc | 251 | tmp = device->getc(); |
dudnwjs | 6:8dd20294b2aa | 252 | //hostpc->printf("%d ==> 0x%X\n",pos,tmp); |
dudnwjs | 5:e11b23f9aacc | 253 | buff[pos] = tmp; |
dudnwjs | 6:8dd20294b2aa | 254 | pos++; |
dudnwjs | 6:8dd20294b2aa | 255 | if( pos%512 == 0 ){ |
dudnwjs | 6:8dd20294b2aa | 256 | buff[1023] = '\0'; |
dudnwjs | 6:8dd20294b2aa | 257 | hostpc->printf("\n pos = %d CHECK <%s> \n",pos,buff); |
dudnwjs | 5:e11b23f9aacc | 258 | } |
dudnwjs | 6:8dd20294b2aa | 259 | if( pos == 1024 ){ |
dudnwjs | 6:8dd20294b2aa | 260 | pos = 0; |
dudnwjs | 6:8dd20294b2aa | 261 | memset(buff,'Z',1023); |
dudnwjs | 6:8dd20294b2aa | 262 | buff[1023] = '\0'; |
dudnwjs | 5:e11b23f9aacc | 263 | } |
dudnwjs | 5:e11b23f9aacc | 264 | } |
dudnwjs | 5:e11b23f9aacc | 265 | |
dudnwjs | 5:e11b23f9aacc | 266 | } |
dudnwjs | 6:8dd20294b2aa | 267 | |
dudnwjs | 6:8dd20294b2aa | 268 | |
dudnwjs | 5:e11b23f9aacc | 269 | } |
dudnwjs | 5:e11b23f9aacc | 270 | |
dudnwjs | 2:dba344c91bce | 271 | }//namespace |
dudnwjs | 9:fcf91f563147 | 272 | /// @} ext_fota module |