BLE fota Lamp Demo
Dependencies: mbed-rtos mbed mbed_fota_fan_control
Fork of mbed_fota_fan_control by
ext_fota/SerialManager.cpp@2:dba344c91bce, 2015-06-22 (annotated)
- Committer:
- dudnwjs
- Date:
- Mon Jun 22 09:43:13 2015 +0000
- Revision:
- 2:dba344c91bce
- Child:
- 3:1e70387e1337
Add Serial Manager;
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 | 2:dba344c91bce | 41 | hostpc->printf("SerialManager 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 | 2:dba344c91bce | 48 | hostpc->printf("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 | |
dudnwjs | 2:dba344c91bce | 69 | if(device->readable()) |
dudnwjs | 2:dba344c91bce | 70 | { |
dudnwjs | 2:dba344c91bce | 71 | tmp = device->getc(); |
dudnwjs | 2:dba344c91bce | 72 | switch(bReceiveState) |
dudnwjs | 2:dba344c91bce | 73 | { |
dudnwjs | 2:dba344c91bce | 74 | case 0: |
dudnwjs | 2:dba344c91bce | 75 | if( tmp == FE_MSG_PACKET_TYPE ) |
dudnwjs | 2:dba344c91bce | 76 | { |
dudnwjs | 2:dba344c91bce | 77 | bReceiveState = 1; |
dudnwjs | 2:dba344c91bce | 78 | wDataLength = 0; |
dudnwjs | 2:dba344c91bce | 79 | wReceivePos = 0; |
dudnwjs | 2:dba344c91bce | 80 | bHdrBytesRead = 0; |
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 | 2:dba344c91bce | 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 | 2:dba344c91bce | 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 | 2:dba344c91bce | 121 | hostpc->printf("\n[Receiver] Over SIZE: %d ", 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 | 2:dba344c91bce | 126 | hostpc->printf("\n[Receiver] Zero SIZE: %d ", 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 | 2:dba344c91bce | 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 | 2:dba344c91bce | 149 | hostpc->printf("\n[Receiver] Rcv Data SIZE: %d ", wDataLength); |
dudnwjs | 2:dba344c91bce | 150 | } |
dudnwjs | 2:dba344c91bce | 151 | break; |
dudnwjs | 2:dba344c91bce | 152 | default: |
dudnwjs | 2:dba344c91bce | 153 | if(print_flag == 1) |
dudnwjs | 2:dba344c91bce | 154 | hostpc->printf("ERROR STRAGE STATE\n"); |
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 | 2:dba344c91bce | 162 | }//namespace |