BLE fota Lamp Demo

Dependencies:   mbed-rtos mbed mbed_fota_fan_control

Fork of mbed_fota_fan_control by KIM HyoengJun

Committer:
dudnwjs
Date:
Mon Jul 13 06:32:05 2015 +0000
Revision:
5:e11b23f9aacc
Parent:
3:1e70387e1337
Child:
6:8dd20294b2aa
Fota Service Add complete

Who changed what in which revision?

UserRevisionLine numberNew 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 5:e11b23f9aacc 162 void SerialManager::ReceiveToSerialTest(void)
dudnwjs 5:e11b23f9aacc 163 {
dudnwjs 5:e11b23f9aacc 164 unsigned char tmp;
dudnwjs 5:e11b23f9aacc 165 int pos = 0;
dudnwjs 5:e11b23f9aacc 166 char buff[128];
dudnwjs 5:e11b23f9aacc 167 memset(buff,'K',127);
dudnwjs 5:e11b23f9aacc 168 buff[127] = '\0';
dudnwjs 5:e11b23f9aacc 169 hostpc->printf("\n pos = %d FIRST <%s> \n",pos,buff);
dudnwjs 5:e11b23f9aacc 170 tmp = device->getc();
dudnwjs 5:e11b23f9aacc 171 tmp = device->getc();
dudnwjs 5:e11b23f9aacc 172 tmp = device->getc();
dudnwjs 5:e11b23f9aacc 173 tmp = device->getc();
dudnwjs 5:e11b23f9aacc 174 tmp = device->getc();
dudnwjs 5:e11b23f9aacc 175 while(1)
dudnwjs 5:e11b23f9aacc 176 {
dudnwjs 5:e11b23f9aacc 177 if(device->readable())
dudnwjs 5:e11b23f9aacc 178 {
dudnwjs 5:e11b23f9aacc 179 tmp = device->getc();
dudnwjs 5:e11b23f9aacc 180 hostpc->printf("%d ==> 0x%X\n",pos,tmp);
dudnwjs 5:e11b23f9aacc 181 buff[pos] = tmp;
dudnwjs 5:e11b23f9aacc 182 pos++;
dudnwjs 5:e11b23f9aacc 183 if( pos == 128 ){
dudnwjs 5:e11b23f9aacc 184 pos = 0;
dudnwjs 5:e11b23f9aacc 185 memset(buff,'Z',127);
dudnwjs 5:e11b23f9aacc 186 buff[127] = '\0';
dudnwjs 5:e11b23f9aacc 187 }
dudnwjs 5:e11b23f9aacc 188 if( pos%16 == 0 ){
dudnwjs 5:e11b23f9aacc 189 hostpc->printf("\n pos = %d ERROR <%s> \n",pos,buff);
dudnwjs 5:e11b23f9aacc 190 if(device->writeable()){
dudnwjs 5:e11b23f9aacc 191 hostpc->printf("writeable!!\n");
dudnwjs 5:e11b23f9aacc 192 device->putc(0x80);
dudnwjs 5:e11b23f9aacc 193 device->putc(0x44);
dudnwjs 5:e11b23f9aacc 194 }
dudnwjs 5:e11b23f9aacc 195 }
dudnwjs 5:e11b23f9aacc 196 }
dudnwjs 5:e11b23f9aacc 197
dudnwjs 5:e11b23f9aacc 198 }
dudnwjs 5:e11b23f9aacc 199 }
dudnwjs 5:e11b23f9aacc 200
dudnwjs 2:dba344c91bce 201 }//namespace