213 lines
6.5 KiB
C++
213 lines
6.5 KiB
C++
#include "libJelling/bluetooth.h"
|
|
|
|
Bluetooth::Bluetooth(){
|
|
this->sentPacketsTotalSize = 0;
|
|
this->receivedPacketsTotalSize = 0;
|
|
this->unread = 0;
|
|
this->allowReception = 1;
|
|
srand(RTC_GetTicks());
|
|
this->caltoID = rand();
|
|
this->secondTry = 0;
|
|
memcpy(this->sender, "HC-06\0", 6);
|
|
}
|
|
|
|
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);
|
|
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;
|
|
|
|
Timer_Start(this->timer);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int Bluetooth::stop(){
|
|
Serial_Close(1);
|
|
return 0;
|
|
}
|
|
|
|
int Bluetooth::setSender(const char *senderName){
|
|
if(strlen(senderName)+1 <= 10){
|
|
if(strlen(senderName) > 0){
|
|
memcpy(this->sender, senderName, strlen(senderName)+1);
|
|
}else{
|
|
return TOO_SHORT;
|
|
}
|
|
}else{
|
|
return TOO_LONG;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Bluetooth::sendMessage(Message *message){
|
|
int RTCTime, tempInt;
|
|
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));
|
|
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);
|
|
}
|
|
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);
|
|
}
|
|
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));
|
|
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);
|
|
RTCTime = RTC_GetTicks();
|
|
while(Serial_GetTxBufferFreeCapacity() < SIZE_TX){
|
|
if((RTC_GetTicks() - RTCTime)/RTC > 300){
|
|
free(ptr2);
|
|
Timer_Start(this->timer);
|
|
return UNKNOWN_ERROR;
|
|
}
|
|
}
|
|
}
|
|
|
|
RTCTime = RTC_GetTicks();
|
|
isWaitingAck = true;
|
|
while(this->lastID != this->caltoID){
|
|
if((RTC_GetTicks() - RTCTime)/RTC > 300){
|
|
free(ptr2);
|
|
Timer_Start(this->timer);
|
|
isWaitingAck = false;
|
|
return MISSING_ACK;
|
|
}
|
|
}
|
|
isWaitingAck = false;
|
|
free(ptr2);
|
|
this->caltoID++;
|
|
Timer_Start(this->timer);
|
|
return 0;
|
|
|
|
}
|
|
|
|
Message& Bluetooth::getLastMessage(){
|
|
return msg;
|
|
}
|
|
|
|
void Bluetooth::receptMessage(){
|
|
int size, errorID;
|
|
int RTCTime;
|
|
int messageLength = 48;
|
|
int typeCase;
|
|
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){
|
|
case ACK:
|
|
if(isWaitingAck){
|
|
this->lastType = ACK;
|
|
memcpy(&this->lastID, (unsigned int*)this->ptr1ko+48, sizeof(unsigned int));
|
|
}
|
|
break;
|
|
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");
|
|
sendMessage(&sendBack);
|
|
RETURN
|
|
}
|
|
memcpy(this->sender, this->ptr1ko+26, 10);
|
|
for(unsigned short i=0; i<partsNumber[1]; i++){
|
|
memcpy(&messageID, (unsigned int*)this->ptr1ko+22, sizeof(unsigned int));
|
|
if(messageID != this->androidID){
|
|
sprintf(buffer,"INCORRECT ID %d-%d",messageID,this->androidID);
|
|
sendBack.init("ERR", messageID, this->sender, buffer);
|
|
sendMessage(&sendBack);
|
|
RETURN
|
|
}
|
|
memcpy(lengthPackets, this->ptr1ko+36, sizeof(lengthPackets));
|
|
sprintf(buffer,"%d", i+1);
|
|
if(partsNumber[0] != i+1){
|
|
if(partsNumber[0] > i+1){
|
|
sendBack.init("MIS", messageID, this->sender, buffer);
|
|
sendMessage(&sendBack);
|
|
}else{
|
|
sendBack.init("ACK", messageID, this->sender, buffer);
|
|
sendMessage(&sendBack);
|
|
}
|
|
}else if(lengthPackets[1] != size-48){
|
|
sprintf(buffer, "INCOMPLETE PACKET SIZE %d", this->androidID);
|
|
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);
|
|
sendMessage(&sendBack);
|
|
if(messageLength-48 == 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]);
|
|
sendMessage(&sendBack);
|
|
RETURN
|
|
}
|
|
}
|
|
|
|
}
|
|
RTCTime = RTC_GetTicks();
|
|
while(Serial_GetRxBufferSize() != (lengthPackets[0]-messageLength-48>1000 ? 1000 : lengthPackets[0]-messageLength-48)){
|
|
if((RTC_GetTicks() - RTCTime)/RTC > 300){
|
|
RETURN
|
|
}
|
|
}
|
|
memcpy(partsNumber, this->ptr1ko+44, sizeof(partsNumber));
|
|
}
|
|
}else{
|
|
sprintf(buffer, "%d",this->androidID);
|
|
sendBack.init("FUL", this->caltoID, this->sender, buffer);
|
|
sendMessage(&sendBack);
|
|
RETURN
|
|
}
|
|
break;
|
|
}
|
|
}else if(Serial_GetRxBufferSize()>0){
|
|
this->secondTry = Serial_GetRxBufferSize();
|
|
RETURN
|
|
}
|
|
} |