-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSTM32_MySQL.h
More file actions
executable file
·144 lines (122 loc) · 3.4 KB
/
Copy pathSTM32_MySQL.h
File metadata and controls
executable file
·144 lines (122 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @file STM32_MySQL.h
* @author Mathieu LE DIMNA (mathieu-ledimna@outlook.com)
* @brief STM32-MySQL library using Mbed framework
* @version 2.0
* @date 2021-04-30
*
* @copyright Copyright (c) 2021
*
*/
#ifndef STM32_MYSQL_H
#define STM32_MYSQL_H
#include <iostream>
#include <vector>
#include <string>
#include "mbed.h"
#include "EthernetInterface.h"
#include "./Utils/Packets/Types/PacketsTypes.h"
#include "./Utils/SHA1/SHA1.h"
#include "./Utils/SQLVarTypes/SQLVarTypes.h"
/**
* @brief Used to send query over TCP socket.
* It MUST be equal to framework max TCP send buffer size
* otherwise TCP dialog will be interrupted by an overflow
* causing MYSQL query interruption and unintelligible
* request to server.
*/
#define SEND_SIZE (MBED_CONF_LWIP_TCP_MSS * 2)
/**
* @brief Used to recieve RECV_SIZE bytes from server.
* This does not limit the recieve size, incoming data
* will then be sored into mBuffer which is a dynamic
* size buffer.
*/
#define RECV_SIZE (SEND_SIZE)
typedef struct
{
char catalog[50] = {'\0'};
char schema[50] = {'\0'};
char table[50] = {'\0'};
char org_table[50] = {'\0'};
char name[50] = {'\0'};
char org_name[50] = {'\0'};
int fields_len = 0;
int character_set = 0;
int column_len = 0;
int type = 0;
int flags = 0;
int decimals = 0;
} TypeDef_ColumnDefinition;
/**
* @brief Stores the SELECT query result.
*
*/
typedef struct
{
char *Table_Name = NULL;
int Column_Count = 0;
int Row_Count = 0;
char **Column_Names = NULL;
char ***Row_Values = NULL;
} TypeDef_Table;
/**
* @brief Stores the SELECT query results.
*
*/
typedef struct
{
char *Database_Name = NULL;
TypeDef_Table *Table = NULL;
} TypeDef_Database;
/**
* @brief Stores the raw MySQL packet
*
* @param payload_length Size of the payload in bytes.
* @param sequence_id MySQL packet number in current dialog.
* @param payload MySQL query result of size `payload_length`.
*/
typedef struct
{
uint8_t payload_length[3];
uint8_t sequence_id;
uint8_t *payload;
} mysql_packet_t;
class MySQL
{
public:
MySQL(TCPSocket *pTCPSocket, const char *server_ip);
~MySQL(void);
bool connect(const char *user, const char *password);
bool disconnect();
bool query(const char *pQuery);
void printDatabase(void);
private:
// User-configured TCP socket attached to NetworkInterface
TCPSocket *mTcpSocket = NULL;
// MySQL Server IP
const char *mServerIP = NULL;
// mBuffer stores the raw bytes recieved from TCP socket
uint8_t *mBuffer = NULL;
uint32_t mBufferSize = 0;
// MySQL packets parsed from mBuffer
std::vector<MySQL_Packet*> mPacketsRecieved;
// Seed used to hash password through SHA-1
uint8_t mSeed[20] = {0};
// Stores the recieved table following a SELECT query
TypeDef_Database *mDatabase = NULL;
bool recieve(void);
nsapi_size_or_error_t write(char *message, uint16_t len);
int send_authentication_packet(const char *user, const char *password);
void parse_handshake_packet(void);
bool parse_textresultset(void);
void free_recieved_packets(void);
int getNewOffset(const uint8_t *packet, int offset);
int check_ok_packet(void);
int scramble_password(const char *password, uint8_t *pwd_hash);
void flush_packet(void);
void freeBuffer(void);
void freeDatabase(void);
void print_packets_types(void);
};
#endif