|
|
|
@ -15,7 +15,7 @@ int Bluetooth::listen(int maxSize, int timer, void(*func)(void), int time){
|
|
|
|
|
unsigned char conf[6] = {0, 5, 0, 0, 0, 0};
|
|
|
|
|
max = maxSize;
|
|
|
|
|
this->ptr = (char*)malloc(maxSize);
|
|
|
|
|
this->ptr1ko = (char*)malloc(1000);
|
|
|
|
|
this->ptr1ko = (char*)malloc(SIZE_DATA_RX);
|
|
|
|
|
if(this->ptr == NULL || this->ptr1ko == NULL) return NOT_ENOUGH_RAM;
|
|
|
|
|
this->timer = Timer_Install(timer, func, time);
|
|
|
|
|
if(Serial_Open(conf) == 3) return SERIAL_ALREADY_OPEN;
|
|
|
|
@ -48,37 +48,27 @@ int Bluetooth::sendMessage(Message *message){
|
|
|
|
|
unsigned short tempShort, split;
|
|
|
|
|
char *ptr2;
|
|
|
|
|
unsigned char dateEx[] = "YYYY-MM-DD-HH-MM-SS";
|
|
|
|
|
split = (unsigned short)(message->getLength()/SIZE_DATA + (message->getLength()%SIZE_DATA == 0 ? 0:1));
|
|
|
|
|
split = (unsigned short)(message->getLength()/SIZE_DATA_TX + (message->getLength()%SIZE_DATA_TX == 0 ? 0:1));
|
|
|
|
|
if((ptr2 = (char*)malloc(SIZE_TX)) == NULL) return NOT_ENOUGH_RAM;
|
|
|
|
|
Timer_Stop(this->timer);
|
|
|
|
|
for(unsigned short i=0; i<split; i++){
|
|
|
|
|
Serial_ClearTransmitBuffer();
|
|
|
|
|
memcpy(ptr2,dateEx, 19);
|
|
|
|
|
if(message->getType() != NULL){
|
|
|
|
|
memcpy(ptr2+19, message->getType(), 3);
|
|
|
|
|
}else{
|
|
|
|
|
memcpy(ptr2+19,"RQT",3);
|
|
|
|
|
}
|
|
|
|
|
*(ptr2+TYPE_POS) = (message->getType() != NULL) ? message->getType() : RQT;
|
|
|
|
|
if(message->getID() != -1){
|
|
|
|
|
tempInt = message->getID();
|
|
|
|
|
memcpy(ptr2+22, &tempInt, sizeof(int));
|
|
|
|
|
}else{
|
|
|
|
|
memcpy(ptr2+22, &this->caltoID, sizeof(int));
|
|
|
|
|
}
|
|
|
|
|
if(message->getSender() != NULL){
|
|
|
|
|
memcpy(ptr2+26, message->getSender(), 10);
|
|
|
|
|
}else{
|
|
|
|
|
memcpy(ptr2+26, this->sender, 10);
|
|
|
|
|
}
|
|
|
|
|
memcpy(ptr2+ID_POS, &tempInt, sizeof(int));
|
|
|
|
|
}else memcpy(ptr2+ID_POS, &this->caltoID, sizeof(int));
|
|
|
|
|
memcpy(ptr2+SENDER_POS, (message->getSender() != NULL) ? message->getSender() : this->sender, 10);
|
|
|
|
|
tempInt = message->getLength();
|
|
|
|
|
memcpy(ptr2+36, &tempInt, sizeof(int));
|
|
|
|
|
tempInt = (i+1)*SIZE_DATA > message->getLength() ? message->getLength()-i*SIZE_DATA : SIZE_DATA;
|
|
|
|
|
memcpy(ptr2+40, &tempInt, sizeof(int));
|
|
|
|
|
memcpy(ptr2+MESSAGE_LENGTH_POS, &tempInt, sizeof(int));
|
|
|
|
|
tempInt = (i+1)*SIZE_DATA_TX > message->getLength() ? message->getLength()-i*SIZE_DATA_TX : SIZE_DATA_TX;
|
|
|
|
|
memcpy(ptr2+PACKET_LENGTH_POS, &tempInt, sizeof(int));
|
|
|
|
|
tempShort = i+1;
|
|
|
|
|
memcpy(ptr2+44, &tempShort, sizeof(unsigned short));
|
|
|
|
|
memcpy(ptr2+46, &split, sizeof(unsigned short));
|
|
|
|
|
memcpy(ptr2+48, (char*)message->getMessage()+i*SIZE_DATA, tempInt);
|
|
|
|
|
Serial_WriteBytes((unsigned char*)ptr2,48+tempInt);
|
|
|
|
|
memcpy(ptr2+PART_POS, &tempShort, sizeof(unsigned short));
|
|
|
|
|
memcpy(ptr2+NUMBER_PART_POS, &split, sizeof(unsigned short));
|
|
|
|
|
memcpy(ptr2+SIZE_HEADER, (char*)message->getMessage()+i*SIZE_DATA_TX, tempInt);
|
|
|
|
|
Serial_WriteBytes((unsigned char*)ptr2,SIZE_HEADER+tempInt);
|
|
|
|
|
RTCTime = RTC_GetTicks();
|
|
|
|
|
while(Serial_GetTxBufferFreeCapacity() < SIZE_TX){
|
|
|
|
|
if((RTC_GetTicks() - RTCTime)/RTC > 300){
|
|
|
|
@ -114,76 +104,77 @@ Message& Bluetooth::getLastMessage(){
|
|
|
|
|
void Bluetooth::receptMessage(){
|
|
|
|
|
int size, errorID;
|
|
|
|
|
int RTCTime;
|
|
|
|
|
int messageLength = 48;
|
|
|
|
|
int typeCase;
|
|
|
|
|
int messageLength = SIZE_HEADER;
|
|
|
|
|
char buffer[50];
|
|
|
|
|
int lengthPackets[2];
|
|
|
|
|
unsigned short partsNumber[2] = {(unsigned short)0,(unsigned short)1};
|
|
|
|
|
const char type[7][4] = {"RQT","ASW","ACK","MIS","FUL","ERR","END"};
|
|
|
|
|
const char err[1][4] = {"ERR"};
|
|
|
|
|
char ptr2[3];
|
|
|
|
|
unsigned int messageID;
|
|
|
|
|
Message sendBack;
|
|
|
|
|
if((this->secondTry = Serial_GetRxBufferSize()) == 1000 || (int)this->secondTry==Serial_GetRxBufferSize()){
|
|
|
|
|
Serial_ReadBytes((unsigned char*)this->ptr1ko, 1000,(short*)&size);
|
|
|
|
|
memcpy(ptr2, this->ptr1ko+19, 3);
|
|
|
|
|
typeCase = tabcmp((const char**)type, 7, ptr2);
|
|
|
|
|
switch(typeCase){
|
|
|
|
|
if((this->secondTry = Serial_GetRxBufferSize()) == SIZE_DATA_RX || ((int)this->secondTry==Serial_GetRxBufferSize() && this->secondTry != 0)){
|
|
|
|
|
Serial_ReadBytes((unsigned char*)this->ptr1ko, SIZE_DATA_RX,(short*)&size);
|
|
|
|
|
this->secondTry = 0;
|
|
|
|
|
switch((unsigned char)*(this->ptr1ko+TYPE_POS)){
|
|
|
|
|
case ACK:
|
|
|
|
|
if(isWaitingAck){
|
|
|
|
|
this->lastType = ACK;
|
|
|
|
|
memcpy(&this->lastID, (unsigned int*)this->ptr1ko+48, sizeof(unsigned int));
|
|
|
|
|
memcpy(&this->lastID, (unsigned int*)this->ptr1ko+SIZE_HEADER, sizeof(unsigned int));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
RETURN
|
|
|
|
|
case SYN:
|
|
|
|
|
this->caltoID = rand();
|
|
|
|
|
sendBack.init(SYN+ACK, this->caltoID, this->sender, "test");
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
RETURN
|
|
|
|
|
case RQT:
|
|
|
|
|
case END:
|
|
|
|
|
case ASW:
|
|
|
|
|
// if allowReception. else, send back a FUL packet to the device
|
|
|
|
|
if(this->allowReception){
|
|
|
|
|
if(size >= 26) memcpy(&messageID, (unsigned int*)this->ptr1ko+22, sizeof(unsigned int));
|
|
|
|
|
if(size < 48){
|
|
|
|
|
if(size >= 26)sendBack.init("HED", this->caltoID, this->sender, (char*)messageID);
|
|
|
|
|
else sendBack.init("HED", this->caltoID, this->sender, "\0");
|
|
|
|
|
if(size >= ID_POS + 4) memcpy(&messageID, (unsigned int*)this->ptr1ko+ID_POS, sizeof(unsigned int));
|
|
|
|
|
if(size < SIZE_HEADER){
|
|
|
|
|
if(size >= ID_POS + 4)sendBack.init(HED, this->caltoID, this->sender, (char*)messageID);
|
|
|
|
|
else sendBack.init(HED, this->caltoID, this->sender, "\0");
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
RETURN
|
|
|
|
|
}
|
|
|
|
|
memcpy(this->sender, this->ptr1ko+26, 10);
|
|
|
|
|
memcpy(this->sender, this->ptr1ko+ID_POS + 4, 10);
|
|
|
|
|
for(unsigned short i=0; i<partsNumber[1]; i++){
|
|
|
|
|
memcpy(&messageID, (unsigned int*)this->ptr1ko+22, sizeof(unsigned int));
|
|
|
|
|
memcpy(&messageID, (unsigned int*)this->ptr1ko+ID_POS, sizeof(unsigned int));
|
|
|
|
|
if(messageID != this->androidID){
|
|
|
|
|
sprintf(buffer,"INCORRECT ID %d-%d",messageID,this->androidID);
|
|
|
|
|
sendBack.init("ERR", messageID, this->sender, buffer);
|
|
|
|
|
sendBack.init(ERR, messageID, this->sender, buffer);
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
RETURN
|
|
|
|
|
}
|
|
|
|
|
memcpy(lengthPackets, this->ptr1ko+36, sizeof(lengthPackets));
|
|
|
|
|
memcpy(lengthPackets, this->ptr1ko+PACKET_LENGTH_POS, sizeof(lengthPackets));
|
|
|
|
|
sprintf(buffer,"%d", i+1);
|
|
|
|
|
if(partsNumber[0] != i+1){
|
|
|
|
|
if(partsNumber[0] > i+1){
|
|
|
|
|
sendBack.init("MIS", messageID, this->sender, buffer);
|
|
|
|
|
sendBack.init(ERR, messageID, this->sender, buffer);
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
}else{
|
|
|
|
|
sendBack.init("ACK", messageID, this->sender, buffer);
|
|
|
|
|
sendBack.init(ACK, messageID, this->sender, buffer);
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
}
|
|
|
|
|
}else if(lengthPackets[1] != size-48){
|
|
|
|
|
}else if(lengthPackets[1] != size-SIZE_HEADER){
|
|
|
|
|
sprintf(buffer, "INCOMPLETE PACKET SIZE %d", this->androidID);
|
|
|
|
|
sendBack.init("ERR", messageID, this->sender, buffer);
|
|
|
|
|
sendBack.init(ERR, messageID, this->sender, buffer);
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
RETURN
|
|
|
|
|
}else{
|
|
|
|
|
messageLength+=size-48;
|
|
|
|
|
memcpy(this->ptr+48+i*952, this->ptr1ko+48, 952);
|
|
|
|
|
sendBack.init("ACK", messageID, this->sender, buffer);
|
|
|
|
|
messageLength+=size-SIZE_HEADER;
|
|
|
|
|
memcpy(this->ptr+SIZE_HEADER+i*SIZE_DATA_RX, this->ptr1ko+SIZE_HEADER, SIZE_DATA_RX);
|
|
|
|
|
sendBack.init(ACK, messageID, this->sender, buffer);
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
if(messageLength-48 == lengthPackets[0] && partsNumber[0] == partsNumber[1]){
|
|
|
|
|
if(messageLength-SIZE_HEADER == lengthPackets[0] && partsNumber[0] == partsNumber[1]){
|
|
|
|
|
if(!(errorID = msg.setTotalMessage(this->ptr))){
|
|
|
|
|
this->unread = 1;
|
|
|
|
|
this->allowReception = 0;
|
|
|
|
|
this->androidID++;
|
|
|
|
|
RETURN
|
|
|
|
|
}else{
|
|
|
|
|
sendBack.init("ERR", messageID, this->sender, (char*)&err[errorID]);
|
|
|
|
|
sendBack.init(ERR, messageID, this->sender, (char*)&err[errorID]);
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
RETURN
|
|
|
|
|
}
|
|
|
|
@ -191,16 +182,16 @@ void Bluetooth::receptMessage(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
RTCTime = RTC_GetTicks();
|
|
|
|
|
while(Serial_GetRxBufferSize() != (lengthPackets[0]-messageLength-48>1000 ? 1000 : lengthPackets[0]-messageLength-48)){
|
|
|
|
|
while(Serial_GetRxBufferSize() != (lengthPackets[0]-messageLength-SIZE_HEADER>SIZE_DATA_RX ? SIZE_DATA_RX : lengthPackets[0]-messageLength-SIZE_HEADER)){
|
|
|
|
|
if((RTC_GetTicks() - RTCTime)/RTC > 300){
|
|
|
|
|
RETURN
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
memcpy(partsNumber, this->ptr1ko+44, sizeof(partsNumber));
|
|
|
|
|
memcpy(partsNumber, this->ptr1ko+PART_POS, sizeof(partsNumber));
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
sprintf(buffer, "%d",this->androidID);
|
|
|
|
|
sendBack.init("FUL", this->caltoID, this->sender, buffer);
|
|
|
|
|
sendBack.init(FUL, this->caltoID, this->sender, buffer);
|
|
|
|
|
sendMessage(&sendBack);
|
|
|
|
|
RETURN
|
|
|
|
|
}
|
|
|
|
|