less hardcoding and flags rework
This commit is contained in:
parent
a14607a9e5
commit
f0e28d39de
|
@ -0,0 +1,5 @@
|
|||
#ignore output file
|
||||
*.o
|
||||
#ignore addin files except addin.ld
|
||||
addin.*
|
||||
!addin.ld
|
1
BTKOM
1
BTKOM
|
@ -1 +0,0 @@
|
|||
Subproject commit 73ef1c56c82c5df134ad8ca82d7a5cd8ed34613a
|
2
addin.ld
2
addin.ld
|
@ -14,6 +14,8 @@ SECTIONS
|
|||
.rodata : {
|
||||
*(.rodata)
|
||||
*(.rodata.str1.4)
|
||||
*(.eh_frame)
|
||||
*(.ctors)
|
||||
_romdata = . ; /* symbol for initialization data */
|
||||
} > rom
|
||||
.bss : {
|
||||
|
|
|
@ -31,8 +31,8 @@ This include : Graph 35+, 75, 85 & 95.
|
|||
This library has been written for the following bluetooth module : HC-05, HC-06
|
||||
RAM can contain approximately 50KO of data. Each message can't do more than 32KO
|
||||
You can't send more than 1ko -including header- from your smartphone to the calculator. In fact, the reception buffer has a size of 1ko max.
|
||||
Without the header : 1000 - 48 = 952o
|
||||
for sending : 256-48 = 208
|
||||
Without the header : 1000 - 46 = 954o
|
||||
for sending : 256-46 = 210
|
||||
If the 32 ko buffer is full, it will return a FUL packet.
|
||||
*/
|
||||
|
||||
|
@ -88,7 +88,7 @@ private:
|
|||
unsigned int androidID;
|
||||
unsigned int secondTry;
|
||||
// for ACK
|
||||
unsigned int lastType;
|
||||
char lastType;
|
||||
unsigned int lastID;
|
||||
bool isWaitingAck;
|
||||
|
||||
|
@ -97,6 +97,4 @@ private:
|
|||
|
||||
};
|
||||
|
||||
#define RETURN Timer_Start(this->timer);return;
|
||||
#define SIZE_TX 255
|
||||
#define SIZE_DATA (SIZE_TX-48)
|
||||
#define RETURN Timer_Start(this->timer);return;
|
|
@ -26,11 +26,26 @@
|
|||
|
||||
#define RTC 0.128
|
||||
|
||||
#define RQT 0
|
||||
#define ASW 1
|
||||
#define ACK 2
|
||||
#define MIS 3
|
||||
#define FUL 4
|
||||
#define ERR 5
|
||||
#define END 6
|
||||
#define SYN 7
|
||||
#define SIZE_TX 255
|
||||
#define SIZE_RX 1000
|
||||
#define SIZE_HEADER 46
|
||||
#define SIZE_DATA_TX (SIZE_TX-SIZE_HEADER)
|
||||
#define SIZE_DATA_RX (SIZE_RX-SIZE_HEADER)
|
||||
|
||||
#define RQT 1
|
||||
#define ASW 2
|
||||
#define ACK 4
|
||||
#define HED 8
|
||||
#define FUL 16
|
||||
#define ERR 32
|
||||
#define END 64
|
||||
#define SYN 128
|
||||
|
||||
#define TIME_POS 0
|
||||
#define TYPE_POS (TIME_POS + 19)
|
||||
#define ID_POS (TYPE_POS + 1)
|
||||
#define SENDER_POS (ID_POS + 4)
|
||||
#define MESSAGE_LENGTH_POS (SENDER_POS + 10)
|
||||
#define PACKET_LENGTH_POS (MESSAGE_LENGTH_POS + 4)
|
||||
#define PART_POS (PACKET_LENGTH_POS + 4)
|
||||
#define NUMBER_PART_POS (PART_POS + 2 )
|
|
@ -26,14 +26,14 @@ extern "C"{
|
|||
How does Bluetooth header have to look like in a Message:
|
||||
|
||||
- (1) time : 19 bytes (YYYY-MM-DD-HH-MM-SS)
|
||||
- (2) type : 3 bytes (3*char)
|
||||
- (2) type : 1 byte (char)
|
||||
- (3) ID : 4 bytes (unsigned int)
|
||||
- (4) sender : 10 bytes (10*char)
|
||||
- (5) total length of the message (in bytes), headers not included : 4 bytes (unsigned int)
|
||||
- (6) total length of this packet (in bytes), headers not included : 4 bytes (unsigned int)
|
||||
- (7) part of the message / number of part : 2 bytes (short unsigned int) / 2 bytes (short unsigned int)
|
||||
|
||||
Total : 48 bytes.
|
||||
Total : 46 bytes.
|
||||
Length must contain the null byte ! Null byte must be at the end of the message.
|
||||
|
||||
(2) type can be :
|
||||
|
@ -43,10 +43,9 @@ Length must contain the null byte ! Null byte must be at the end of the message.
|
|||
- MIS : missing packet
|
||||
- FUL : full reception buffer or RAM buffer or precedent message unread. It sends the ID of last message read.
|
||||
- ERR : error. see error code in message content.
|
||||
- HED : incorrect header
|
||||
- HED : incorrect header.
|
||||
- END : terminate the connection.
|
||||
|
||||
Those are classic types. You can also set your own type (3 characters).
|
||||
- SYN : establishing connection.
|
||||
|
||||
Practical informations and example :
|
||||
|
||||
|
@ -56,7 +55,7 @@ another location before accepting new message.
|
|||
|
||||
- To read message : ptr = Obj.getMessage();
|
||||
|
||||
- To save message : length = Obj.getLength()+48\
|
||||
- To save message : length = Obj.getLength()+46\
|
||||
ptr = malloc(length);\
|
||||
memcpy(ptr, Obj.msg, length);\
|
||||
Message Obj2;\
|
||||
|
@ -71,10 +70,10 @@ public:
|
|||
Message();
|
||||
|
||||
// init message attribute. opt=1 to send the message directly (if possible)
|
||||
int init(const char* type, int ID, const char* sender,const char* message);
|
||||
int init(const unsigned char type, int ID, const char* sender,const char* message);
|
||||
|
||||
// set headers informations.
|
||||
int setType(const char *typeH);
|
||||
int setType(const unsigned char typeH);
|
||||
int setID(unsigned int ID);
|
||||
int setSender(const char *sender);
|
||||
|
||||
|
@ -89,7 +88,7 @@ public:
|
|||
|
||||
// header informations :
|
||||
char* getTime();
|
||||
char* getType();
|
||||
unsigned char getType();
|
||||
char* getSender();
|
||||
int getID();
|
||||
int getLength();
|
||||
|
@ -100,7 +99,7 @@ public:
|
|||
|
||||
private:
|
||||
|
||||
char type[3];
|
||||
char type;
|
||||
char sender[10];
|
||||
int ID;
|
||||
int isType;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
Bluetooth bluetooth;
|
||||
|
||||
void listen(){
|
||||
//bluetooth.receptMessage();
|
||||
bluetooth.receptMessage();
|
||||
}
|
||||
|
||||
int init(){
|
||||
|
@ -15,7 +15,7 @@ int init(){
|
|||
return !err;
|
||||
}
|
||||
|
||||
int sendMessage(const char* type, const char* message){
|
||||
int sendMessage(const unsigned char type, const char* message){
|
||||
int err = 0;
|
||||
Message toSend;
|
||||
toSend.setType(type);
|
||||
|
@ -39,7 +39,7 @@ int main(){
|
|||
ML_clear_screen();
|
||||
//ML_clear_vram();
|
||||
if(init()){
|
||||
sendMessage((const char*)"ACK",(const char*) buffer);
|
||||
sendMessage(RQT,(const char*) buffer);
|
||||
}
|
||||
//ML_display_vram();
|
||||
//bluetooth.stop();
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ Message::Message(){
|
|||
this->msg = (char*)NULL;
|
||||
}
|
||||
|
||||
int Message::init(const char* type, int ID, const char* sender,const char* message){
|
||||
int Message::init(const unsigned char type, int ID, const char* sender,const char* message){
|
||||
if(setType(type)) return INVALID_TYPE;
|
||||
if(setMessage(message)) return NOT_ENOUGH_RAM;
|
||||
setID((unsigned int)ID);
|
||||
|
@ -16,8 +16,8 @@ int Message::init(const char* type, int ID, const char* sender,const char* messa
|
|||
return 0;
|
||||
}
|
||||
|
||||
int Message::setType(const char *typeH){
|
||||
memcpy(this->type, typeH, 3);
|
||||
int Message::setType(const unsigned char typeH){
|
||||
this->type = typeH;
|
||||
this->isType = 1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -36,10 +36,10 @@ int Message::setSender(const char *sender){
|
|||
|
||||
int Message::setMessage(const char *message){
|
||||
int length;
|
||||
length = strlen(message) + 49;
|
||||
length = strlen(message) + SIZE_HEADER + 1;
|
||||
this->msg = (char*)realloc(this->msg, length);
|
||||
if(this->msg == NULL) return NOT_ENOUGH_RAM;
|
||||
memcpy(this->msg+48, message, length-48);
|
||||
memcpy(this->msg+SIZE_HEADER, message, length-SIZE_HEADER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -57,19 +57,19 @@ char* Message::getTime(){
|
|||
}
|
||||
}
|
||||
|
||||
char* Message::getType(){
|
||||
unsigned char Message::getType(){
|
||||
if(this->isHeader){
|
||||
return this->msg+19;
|
||||
return *(this->msg+TYPE_POS);
|
||||
}else if(this->isType){
|
||||
return this->type;
|
||||
}else{
|
||||
return (char*)NULL;
|
||||
return RQT;
|
||||
}
|
||||
}
|
||||
|
||||
int Message::getID(){
|
||||
if(this->isHeader){
|
||||
return (unsigned int)this->msg+22;
|
||||
return (unsigned int)this->msg+ID_POS;
|
||||
}else if(this->isID){
|
||||
return this->ID;
|
||||
}else{
|
||||
|
@ -79,7 +79,7 @@ int Message::getID(){
|
|||
|
||||
char* Message::getSender(){
|
||||
if(this->isHeader){
|
||||
return this->msg+26;
|
||||
return this->msg+SENDER_POS;
|
||||
}else if(this->isSender){
|
||||
return this->sender;
|
||||
}else{
|
||||
|
@ -89,9 +89,9 @@ char* Message::getSender(){
|
|||
|
||||
int Message::getLength(){
|
||||
if(this->msg == NULL) return -1;
|
||||
return strlen(this->msg+48)+1;
|
||||
return strlen(this->msg+SIZE_HEADER)+1;
|
||||
}
|
||||
|
||||
unsigned char* Message::getMessage(){
|
||||
return (unsigned char*)this->msg+48;
|
||||
return (unsigned char*)this->msg+SIZE_HEADER;
|
||||
}
|
Loading…
Reference in New Issue