108 lines
2.9 KiB
C++
108 lines
2.9 KiB
C++
/*
|
|
* AUTHOR : Xavier Bruni
|
|
*
|
|
* ----------------------------------------------------------------------------
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
* As long as you retain this notice you can do whatever you want with this
|
|
* stuff. If we meet some day, and you think this stuff is worth it, you can
|
|
* buy me a beer in return.
|
|
* ----------------------------------------------------------------------------
|
|
*/
|
|
|
|
#define LIB_JELLING
|
|
#define MESSAGE_H_INCLUDED
|
|
|
|
extern "C"{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "libfx.h"
|
|
#include "syscall.h"
|
|
}
|
|
|
|
#include "libJelling/libJelling.h"
|
|
|
|
/*
|
|
How does Bluetooth header have to look like in a Message:
|
|
|
|
- (1) time : 19 bytes (YYYY-MM-DD-HH-MM-SS)
|
|
- (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 : 46 bytes.
|
|
Length must contain the null byte ! Null byte must be at the end of the message.
|
|
|
|
(2) type can be :
|
|
- RQT : request
|
|
- ASW : answer to a request
|
|
- ACK : acknowledgment. packet received.
|
|
- 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.
|
|
- END : terminate the connection.
|
|
- SYN : establishing connection.
|
|
|
|
Practical informations and example :
|
|
|
|
This library has been build to save as much as memory as it can. To do that, Bluetooth class is sharing the same message ptr as the Message class.
|
|
The fact is that it will delete your precedent message on new message reception. If you want to avoid that, you can copy the message to
|
|
another location before accepting new message.
|
|
|
|
- To read message : ptr = Obj.getMessage();
|
|
|
|
- To save message : length = Obj.getLength()+46\
|
|
ptr = malloc(length);\
|
|
memcpy(ptr, Obj.msg, length);\
|
|
Message Obj2;\
|
|
Obj2.setTotalMessage(ptr);
|
|
|
|
*/
|
|
|
|
class Message{
|
|
|
|
public:
|
|
|
|
Message();
|
|
|
|
// init message attribute. opt=1 to send the message directly (if possible)
|
|
int init(const unsigned char type, int ID, const char* sender,const char* message);
|
|
|
|
// set headers informations.
|
|
int setType(const unsigned char typeH);
|
|
int setID(unsigned int ID);
|
|
int setSender(const char *sender);
|
|
|
|
// set message content.
|
|
int setMessage(const char *message);
|
|
|
|
// set message (headers )
|
|
int setTotalMessage(char *message);
|
|
|
|
// get message content. (ptr + 48)
|
|
unsigned char* getMessage();
|
|
|
|
// header informations :
|
|
char* getTime();
|
|
unsigned char getType();
|
|
char* getSender();
|
|
int getID();
|
|
int getLength();
|
|
|
|
// this is message address (with header)
|
|
char *msg;
|
|
int isHeader;
|
|
|
|
private:
|
|
|
|
char type;
|
|
char sender[10];
|
|
int ID;
|
|
int isType;
|
|
int isID;
|
|
int isSender;
|
|
}; |