libJelling-calc/include/bluetooth.h

186 lines
5.5 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.
* ----------------------------------------------------------------------------
*/
extern "C"{
#include "MonochromeLib.h"
#include "syscall.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libfx.h"
}
/*
This bluetooth library is working with the 3-pin port of your calculator.
This library is compatible with SuperH-3 and SuperH-4 processor :
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
If the 32 ko buffer is full, it will return a FUL packet.
*/
/*
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)
- (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.
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.
Those are classic types. You can also set your own type (3 characters).
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()+48\
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 char* type, int ID, char* sender,const char* message);
// set the type of the message. You have not to worry about other things to set in the header. It is automatic.
int setType(const char *typeH);
// set message content.
int setMessage(const char *message);
int setTotalMessage(char *message);
// get message content. (ptr + 48)
unsigned char* getMessage();
// header informations :
char* getTime();
char* getType();
char* getSender();
int getLength();
// this is message address (with header)
char *msg;
int isHeader;
private:
char type[3];
int isType;
};
class Bluetooth{
public:
Bluetooth();
// listen for new message. This function is non blocking. maxSize is the maximum length of message to receive.
// it will return NOT_ENOUG_RAM if it can not allocate that much. call this function early in your code, you'll have more chance to have enough ram.
// There is no maximum restriction, but RAM is about 50KO. 32KO should be the maximum.
int listen(int maxSize, int timer, void(*)(void), int time);
// Send a message. There is no maximum size. Indead, you will be limited by the RAM size (approx. 50ko)
int sendMessage(Message *message);
// get last message.
Message& getLastMessage();
// set the sender name (for header)
int setSender(char *senderName);
// set it to 1 if you want to allow message reception. it will delete previous message on new reception !! save it if you would like to keep it !!
unsigned int allowReception;
unsigned int unread;
// point on the 32ko of data.
char* ptr;
// point to the 1ko to receive
char* ptr1ko;
// call by a timber every 25ms. It CAN NOT recept more than 1 ko in a row.
// use (3) and (4) to send bigger message from your device.
void receptMessage();
int timer;
private:
// total size of all send packet
int sentPacketsTotalSize;
// total szie of all received packets
int receivedPacketsTotalSize;
int max;
char sender[10];
unsigned int ID;
unsigned int secondTry;
// Keep the last message in memory
Message msg;
};
int tabcmp(const char *tab[], int size, char *string);
char *itoa(int integer);
#define RTC 15.625
#define RQT 0
#define ASW 1
#define ACK 2
#define MIS 3
#define FUL 4
#define ERR 5
#define END 6
// Error code :
#define TIMER_ERROR_INSTALL 1
#define SENDER_TOO_BIG 2
#define SENDER_EMPTY 3
#define SERIAL_ALREADY_OPEN 4
#define NOT_ENOUGH_RAM 5
#define MISSING_ACK 6
#define UNKNOWN_ERROR 7
#define INVALID_TYPE 8
#define END_OF_COMMUNICATION 9