430 lines
13 KiB
C
430 lines
13 KiB
C
/*----------------------------------------------------------------------------
|
||
* Copyright (c) Fenda Technologies Co., Ltd. 2021. All rights reserved.
|
||
*
|
||
* Description: sql_bt.c EDR与BLE信息都在这管理
|
||
*
|
||
* Author: saimen
|
||
*
|
||
* Create: 2024-06-08
|
||
*--------------------------------------------------------------------------*/
|
||
//所有的 SQL 语法都必须以关键字(也称命令)开头,比如 SELECT、INSERT、UPDATE、DELETE、ALTER、DROP、CREATE、USE、SHOW 等。
|
||
#include <string.h>
|
||
#include "sys_typedef.h"
|
||
#include "sys_config.h"
|
||
#include "linux/crc32.h"
|
||
#include "sql_bt.h"
|
||
|
||
/**********************************************************************************************************************
|
||
* DEFINE
|
||
*/
|
||
#define ENABLE_STATIC_PRINT true
|
||
#define static_print_error(...) sys_sql_log_e(__VA_ARGS__) //错误信息打印一般常开
|
||
#define static_print_warn(...) sys_sql_log_w(__VA_ARGS__) //警告信息打印一般常开
|
||
#if ENABLE_STATIC_PRINT
|
||
#define static_print_debug(...) sys_sql_log_d(__VA_ARGS__)
|
||
#else
|
||
#define static_print_debug(...)
|
||
#endif
|
||
|
||
|
||
#define FILE_VERSION_SETTING (1)
|
||
#define FILE_PATH_SETTING "/system/sql_bt.bin"
|
||
#define QRCODE_MAX_DATA_LEN 256
|
||
#define QRCODE_MAX_NUMER 11
|
||
#define GAME_MAX_NUMER 2
|
||
|
||
|
||
//该结构体用于存储二维码数据
|
||
typedef struct {
|
||
bool exist; //二维码是否存在 false:不存在 true:存在
|
||
qrcode_type_t type; //二维码类型
|
||
uint8_t data[QRCODE_MAX_DATA_LEN]; //二维码数据
|
||
uint16_t len; //二维码长度
|
||
} qrcode_info_t; //二维码信息
|
||
|
||
typedef struct {
|
||
game_type_t game_type; //游戏类型
|
||
uint32_t game_score; //历史最高得分
|
||
}game_info_t; //游戏信息
|
||
|
||
#pragma pack(4) //alignment 4 bytes
|
||
typedef struct {
|
||
uint32_t init_flag:8; //标记模块是否初始化。=0 无效数据;=1 正常数据;=2 恢复数据。
|
||
uint32_t version:8; //结构体版本
|
||
|
||
uint8_t binded; //设备绑定 0:未绑定 1:已绑定
|
||
uint8_t en_disconn_reminder; //蓝牙断开提醒
|
||
bool start_complete; //启动完成标志 false:未完成 ture:完成
|
||
bool bt_swithch_mode; //蓝牙开关模式 false:关闭 ture:打开
|
||
bool is_payment_into_wechat; //是否从交友中进入钱包-微信支付
|
||
bool is_payment_into_qq; //是否从交友中进入名片-QQ
|
||
uint8_t mac_addr[MAC_ADDR_LEN]; //蓝牙MAC地址
|
||
char ble_name[BLE_NAME_LEN]; //BLE名称
|
||
char phone_name[16]; //手机型号
|
||
breakpoint_info_t breakpoint;
|
||
qrcode_info_t qrcode_info[QRCODE_MAX_NUMER]; //二维码信息
|
||
game_info_t game_info[GAME_MAX_NUMER]; //游戏得分信息
|
||
uint8_t headset_lastconnect_addr[MAC_ADDR_LEN]; //蓝牙连接的上一个耳机设备地址
|
||
//uint8_t revs[1]; //保证8字节对齐
|
||
uint32_t crc32;
|
||
} sql_bt_t;
|
||
#pragma pack()
|
||
|
||
static sql_bt_t g_sql_bt; //设置实例
|
||
static bool g_bt_need_save = false;
|
||
|
||
/**
|
||
* @brief 重置系统设置数据.
|
||
* @retval int|返回结果,0=成功.
|
||
*/
|
||
int sql_bt_restore(void)
|
||
{
|
||
int ret = RET_SUCCESS;
|
||
static_print_warn("sql_bt_restore()\r\n");
|
||
// 设置的默认值
|
||
memset((uint8_t*)&g_sql_bt,0,sizeof(sql_bt_t));
|
||
g_sql_bt.version = FILE_VERSION_SETTING;
|
||
g_sql_bt.init_flag = 1;
|
||
|
||
g_sql_bt.bt_swithch_mode = true;
|
||
g_sql_bt.binded = 0;
|
||
memcpy_s(g_sql_bt.ble_name,BLE_NAME_LEN,TJD_PCB_NAME""TJD_BLE_NAME_PREFIX,sizeof(TJD_PCB_NAME""TJD_BLE_NAME_PREFIX));
|
||
|
||
g_bt_need_save = false;
|
||
return ret;
|
||
}
|
||
|
||
/**********************************************************************************************************************
|
||
* 固定接口
|
||
*/
|
||
/**
|
||
* @brief 初始化系统设置数据.
|
||
* @retval int|返回结果,0=成功.
|
||
*/
|
||
int sql_bt_init(void)
|
||
{
|
||
int ret = RET_SUCCESS;
|
||
uint32_t ui32_crc;
|
||
static_print_warn("[%s],sizeof(sql_bt_t) = %d\r\n", FILE_PATH_SETTING, sizeof(sql_bt_t));
|
||
//ui32_crc = crc32_ex(0,(uint8_t*)&g_sql_bt, sizeof(sql_bt_t) - 4);
|
||
ui32_crc = Crc32Part(0,(const char *)&g_sql_bt, sizeof(sql_bt_t) - 4);
|
||
if(ui32_crc != g_sql_bt.crc32 || g_sql_bt.version != FILE_VERSION_SETTING)
|
||
{
|
||
sql_bt_restore();
|
||
g_bt_need_save = true;
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
void* sql_bt_get_info(uint32_t *len, char **path)
|
||
{
|
||
g_sql_bt.crc32 = Crc32Part(0,(const char *)&g_sql_bt, sizeof(g_sql_bt) - 4);
|
||
*len = sizeof(g_sql_bt);
|
||
*path = (char*)FILE_PATH_SETTING;
|
||
return &g_sql_bt;
|
||
}
|
||
|
||
void sql_bt_set_save_flag(uint8_t value)
|
||
{
|
||
g_bt_need_save = value;
|
||
}
|
||
|
||
uint8_t sql_bt_get_save_flag(void)
|
||
{
|
||
return g_bt_need_save;
|
||
}
|
||
|
||
/**********************************************************************************************************************
|
||
* SQL Server API
|
||
*/
|
||
void sql_bt_set_bt_switch_mode(bool mode)
|
||
{
|
||
static_print_debug("settings:set bt switch mode");
|
||
if(g_sql_bt.bt_swithch_mode != mode) {
|
||
g_sql_bt.bt_swithch_mode = mode;
|
||
g_bt_need_save = true;
|
||
}
|
||
}
|
||
|
||
bool sql_bt_get_bt_switch_mode(void)
|
||
{
|
||
return g_sql_bt.bt_swithch_mode;
|
||
}
|
||
|
||
void sql_bt_set_is_payment_into_wechat(bool mode)
|
||
{
|
||
static_print_debug("settings:set is_payment_into_wechat");
|
||
if(g_sql_bt.is_payment_into_wechat != mode) {
|
||
g_sql_bt.is_payment_into_wechat = mode;
|
||
g_bt_need_save = true;
|
||
}
|
||
}
|
||
|
||
bool sql_bt_get_is_payment_into_wechat(void)
|
||
{
|
||
return g_sql_bt.is_payment_into_wechat;
|
||
}
|
||
|
||
void sql_bt_set_is_payment_into_qq(bool mode)
|
||
{
|
||
static_print_debug("settings:set is_payment_into_qq");
|
||
if(g_sql_bt.is_payment_into_qq != mode) {
|
||
g_sql_bt.is_payment_into_qq = mode;
|
||
g_bt_need_save = true;
|
||
}
|
||
}
|
||
|
||
bool sql_bt_get_is_payment_into_qq(void)
|
||
{
|
||
return g_sql_bt.is_payment_into_qq;
|
||
}
|
||
|
||
void sql_bt_set_bind(uint8_t isBinded)
|
||
{
|
||
static_print_debug("settings:set bind\r\n");
|
||
if(g_sql_bt.binded != isBinded) {
|
||
g_sql_bt.binded = isBinded;
|
||
g_bt_need_save = true;
|
||
}
|
||
}
|
||
|
||
uint8_t sql_bt_get_bind(void)
|
||
{
|
||
return g_sql_bt.binded;
|
||
}
|
||
|
||
void sql_bt_set_switch_disconn_reminder(bool enable)
|
||
{
|
||
static_print_debug("settings:set ble disconn notify\r\n");
|
||
if(g_sql_bt.en_disconn_reminder != enable) {
|
||
g_sql_bt.en_disconn_reminder = enable;
|
||
g_bt_need_save = true;
|
||
}
|
||
}
|
||
|
||
uint8_t sql_bt_get_switch_disconn_reminder(void)
|
||
{
|
||
return g_sql_bt.en_disconn_reminder;
|
||
}
|
||
|
||
int8_t sql_bt_set_mac_addr(const uint8_t* address, uint8_t len)
|
||
{
|
||
// char* addr = (char*)address;
|
||
if (len > MAC_ADDR_LEN) {
|
||
static_print_debug("mac addr len is override");
|
||
return -1;
|
||
}
|
||
if(memcmp((g_sql_bt.mac_addr), address, len) != 0) {
|
||
memcpy_s((g_sql_bt.mac_addr), len, address, len);
|
||
g_bt_need_save = true;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
uint8_t * sql_bt_get_mac_addr(void)
|
||
{
|
||
return g_sql_bt.mac_addr;
|
||
}
|
||
|
||
int8_t sql_bt_set_ble_name(const uint8_t* ble_name, uint8_t len)
|
||
{
|
||
char* addr = (char*)ble_name;
|
||
if (len > BLE_NAME_LEN) {
|
||
static_print_debug("ble name len is override");
|
||
return -1;
|
||
}
|
||
if(strncmp((const char *)(g_sql_bt.ble_name), addr, len) != 0) {
|
||
strncpy((char *)(g_sql_bt.ble_name), addr, len);
|
||
g_bt_need_save = true;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
void sql_bt_get_ble_name(char * name)
|
||
{
|
||
strncpy_s(name, BLE_NAME_LEN , g_sql_bt.ble_name, strlen(g_sql_bt.ble_name));
|
||
}
|
||
|
||
int8_t sql_bt_set_phone_name(const uint8_t* address, uint8_t len)
|
||
{
|
||
char* addr = (char*)address;
|
||
|
||
static_print_debug("settings:set phone name");
|
||
if (len > 24) {
|
||
static_print_debug("phone name len is override");
|
||
return -1;
|
||
}
|
||
if(strncmp(g_sql_bt.phone_name, addr, len) != 0) {
|
||
strncpy(g_sql_bt.phone_name, addr, len);
|
||
g_bt_need_save = true;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
char* sql_bt_get_phone_name(void)
|
||
{
|
||
return g_sql_bt.phone_name;
|
||
}
|
||
|
||
bool sql_bt_get_qrcode_show_flag(void)
|
||
{
|
||
//遍历qrcode_info数组,判断是否存在有效二维码
|
||
for(int i = 0; i < QRCODE_MAX_NUMER; i++) {
|
||
if(i == 0 || i == 2) continue;
|
||
if(g_sql_bt.qrcode_info[i].exist == true) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool sql_bt_get_wallet_qrcode_show_flag(void)
|
||
{
|
||
//遍历qrcode_info数组,判断是否存在有效二维码
|
||
if(g_sql_bt.qrcode_info[0].exist == true || g_sql_bt.qrcode_info[2].exist == true) {
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
int8_t sql_bt_set_qrcode_exist(bool exist, qrcode_type_t type)
|
||
{
|
||
static_print_debug("settings:set qrcode exist");
|
||
if(type >= QRCODE_MAX) {
|
||
static_print_debug("qrcode type is invalid");
|
||
return false;
|
||
}
|
||
g_sql_bt.qrcode_info[type].exist = exist;
|
||
g_sql_bt.qrcode_info[type].type = type;
|
||
g_bt_need_save = true;
|
||
|
||
return RET_SUCCESS;
|
||
}
|
||
|
||
bool sql_bt_get_qrcode_exist(qrcode_type_t type)
|
||
{
|
||
if(type >= QRCODE_MAX) {
|
||
static_print_debug("qrcode type is invalid");
|
||
return false;
|
||
}
|
||
return g_sql_bt.qrcode_info[type].exist;
|
||
}
|
||
|
||
int8_t sql_bt_set_qrcode_info(qrcode_type_t type, const uint8_t *info, uint16_t len)
|
||
{
|
||
if(len > QRCODE_MAX_DATA_LEN) {
|
||
static_print_debug("qrcode len is override");
|
||
return -1;
|
||
}
|
||
g_sql_bt.qrcode_info[type].len = len;
|
||
memcpy((char*)g_sql_bt.qrcode_info[type].data, info, len);
|
||
return RET_SUCCESS;
|
||
}
|
||
|
||
int8_t sql_bt_get_qrcode_info(qrcode_type_t type, uint8_t *info, uint16_t *len)
|
||
{
|
||
if(type >= QRCODE_MAX) {
|
||
static_print_debug("qrcode type is invalid");
|
||
return -1;
|
||
}
|
||
if(g_sql_bt.qrcode_info[type].exist == false) {
|
||
static_print_debug("qrcode is not exist");
|
||
return -1;
|
||
}
|
||
*len = g_sql_bt.qrcode_info[type].len;
|
||
memcpy(info, (char*)g_sql_bt.qrcode_info[type].data, *len);
|
||
|
||
return RET_SUCCESS;
|
||
}
|
||
|
||
int8_t sql_bt_set_start_complete_flag(bool flag)
|
||
{
|
||
if(g_sql_bt.start_complete != flag) {
|
||
g_sql_bt.start_complete = flag;
|
||
g_bt_need_save = true;
|
||
return RET_PROCESS;
|
||
}
|
||
return RET_SUCCESS;
|
||
}
|
||
|
||
bool sql_bt_get_start_complete_flag(void)
|
||
{
|
||
return g_sql_bt.start_complete;
|
||
}
|
||
|
||
int8_t sql_bt_set_headset_lastconnect_addr(const uint8_t* address, uint8_t len)
|
||
{
|
||
char* addr = (char*)address;
|
||
if (len > MAC_ADDR_LEN) {
|
||
static_print_debug("headset lastconnect addr len is override");
|
||
return -1;
|
||
}
|
||
if(strncmp((const char *)(g_sql_bt.headset_lastconnect_addr), addr, len) != 0) {
|
||
memcpy((char *)(g_sql_bt.headset_lastconnect_addr), addr, len);
|
||
g_bt_need_save = true;
|
||
}
|
||
return RET_SUCCESS;
|
||
}
|
||
|
||
bool sql_bt_get_headset_lastconnect_addr(uint8_t *address)
|
||
{
|
||
if(address == NULL) {
|
||
return false;
|
||
}
|
||
for(int i = 0; i < MAC_ADDR_LEN; i++) {
|
||
if(g_sql_bt.headset_lastconnect_addr[i] != 0){
|
||
memcpy(address, g_sql_bt.headset_lastconnect_addr, MAC_ADDR_LEN);
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
uint32_t sql_bt_get_game_score(game_type_t type)
|
||
{
|
||
if(type >= GAME_MAX_NUMER) {
|
||
static_print_debug("game type is invalid");
|
||
return 0;
|
||
}
|
||
return g_sql_bt.game_info[type].game_score;
|
||
}
|
||
|
||
bool sql_bt_set_game_score(game_type_t type, uint32_t *score)
|
||
{
|
||
if(type >= GAME_MAX_NUMER) {
|
||
static_print_debug("game type is invalid");
|
||
return false;
|
||
}
|
||
if(g_sql_bt.game_info[type].game_score < *score) {
|
||
g_sql_bt.game_info[type].game_score = *score;
|
||
g_bt_need_save = true;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool sql_bt_set_breakpoint_info(uint32_t offset_byte, uint32_t crc_32, const char* file_name)
|
||
{
|
||
if(g_sql_bt.breakpoint.offset_byte != offset_byte || g_sql_bt.breakpoint.crc_32 != crc_32 || strncmp(g_sql_bt.breakpoint.file_name, file_name, 32) != 0) {
|
||
g_sql_bt.breakpoint.offset_byte = offset_byte;
|
||
g_sql_bt.breakpoint.crc_32 = crc_32;
|
||
strncpy(g_sql_bt.breakpoint.file_name, file_name, strlen(file_name));
|
||
g_bt_need_save = true;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool sql_bt_get_breakpoint_info(uint32_t *offset_byte, uint32_t *crc_32, char* file_name)
|
||
{
|
||
if(g_sql_bt.breakpoint.offset_byte == 0 || g_sql_bt.breakpoint.crc_32 == 0 || strlen(g_sql_bt.breakpoint.file_name) == 0 ||
|
||
offset_byte == NULL || crc_32 == NULL || file_name == NULL) {
|
||
return false;
|
||
}
|
||
*offset_byte = g_sql_bt.breakpoint.offset_byte;
|
||
*crc_32 = g_sql_bt.breakpoint.crc_32;
|
||
strncpy(file_name, g_sql_bt.breakpoint.file_name, strlen(g_sql_bt.breakpoint.file_name));
|
||
return true;
|
||
}
|
||
|