This commit is contained in:
2025-10-26 11:30:13 +08:00
parent e43078aa84
commit 08bdd1664e
23 changed files with 63 additions and 99 deletions

Binary file not shown.

View File

@@ -22,10 +22,12 @@ const string envPath = "/home/orangepi/RKApp/InitAuth/conf/.env";
// MQTT相关配置
const string mqtt_url = "tcp://192.168.12.1:1883";
const string clientId = "RK3588_SubTest";
const string Topic = "/bsd_camera/cmd";
const string TopicRecv = "/bsd_camera/cmd"; // 接收手机传来的信息
const string TopicSend = "/bsd_camera/init"; // 发送的话题
const int Qos = 1;
atomic<bool> isRunning(true);
atomic<bool> Deactivate(false); // 激活标志
mqtt::async_client client(mqtt_url, clientId);
@@ -45,12 +47,17 @@ void messageCallback(mqtt::const_message_ptr msg);
// 检测设备是否已进行初始化
bool checkUUID();
// 退出程序,开始进行设备验证
void StartCheck();
int main()
{
if (checkUUID())
Deactivate = checkUUID(); // 判断是否已被激活
if (Deactivate)
{
// 设备已被激活,调用验证程序,退出本程序
cout << "设备已激活,开始验证" << endl;
StartCheck();
return 0;
}
@@ -64,6 +71,14 @@ int main()
return 0;
}
// 退出程序,开始进行设备验证
void StartCheck()
{
isRunning = false; // 退出程序
string cmd = "../../softWareInit/bin/wifi ";
system(cmd.c_str());
}
// 检测设备是否已经激活
bool checkUUID()
{
@@ -98,19 +113,21 @@ void messageCallback(mqtt::const_message_ptr msg)
{
// 接受App传来的密文,并保存在配置文件中
auto buffer = msg->to_string();
cout << "msg: " << buffer << endl;
// cout << "Topic:" << msg->get_topic() << ",msg:" << buffer << endl;
if (buffer.find("Activate") != string::npos)
{
// 接受请求,发送SIM卡号
string ICCID = GetSimICCID();
client.publish(Topic, ICCID, Qos, false); // 不保存
string deviceId = format("{\"deviceUid\":\"{}\"}", ICCID);
// cout << "ICCID:" << deviceId << endl;
client.publish(TopicSend, deviceId, Qos, false); // 不保存
}
else if (buffer.find("ServerPwd") != string::npos)
{
// 接受UUID,保存至配置文件中,退出程序,调用设备验证程序
auto res = nlohmann::json::parse(buffer); // 准备解析接受到的秘钥
auto pwd = res["Data"];
cout << pwd << endl;
// cout << pwd << endl;
// 写入文件
ReadFile rf(envPath);
@@ -127,16 +144,23 @@ void messageCallback(mqtt::const_message_ptr msg)
{
WriteFile wf(envPath);
string out;
out.resize(1024);
out.reserve(1024);
for (size_t i = 0; i < lines.size(); ++i)
{
out += lines[i];
if (i + 1 < lines.size())
out += "\n";
}
wf.overwriteText(out); })
wf.overwriteText(out);
Deactivate =true; })
.detach();
}
if (Deactivate)
{ // 已激活
StartCheck();
}
}
// 连接成功的回调
@@ -150,8 +174,8 @@ void mqttInit()
client.set_message_callback(messageCallback);
client.set_connected_handler(connectCallback);
client.connect()->wait(); // 进行连接
client.subscribe(Topic, Qos)->wait(); // 订阅话题
client.connect()->wait(); // 进行连接
client.subscribe(TopicRecv, Qos)->wait(); // 订阅话题
}
// 获取SIM卡号

View File

@@ -2,7 +2,7 @@
UUID:null
#以下配置用于获取云端发送秘钥
ServerPwd:null
ServerPwd:"17227ca72f30f8bca8158bb78f25b43e"
#以下配置存储报警输出高低电平
outSignal:low

View File

@@ -1,8 +1,9 @@
/*
:
1.4G模块,
2.RTSP推流
3.,,
:
1.RTSP推流
2.,
3.mqtt,App进行数据交互
*/
#include <iostream>
@@ -73,7 +74,7 @@ string GetSimICCID(const string &tty = "/dev/ttyUSB2");
// 发送cpu序列号和SIM卡号
void SendCardInfo(mqtt::async_client &client);
// 进行验证
// 开始进行设备验证
bool verification();
// 解析接收的数据
@@ -106,39 +107,33 @@ void mqttInit();
// 接收消息回调
void getMsgCallback(mqtt::const_message_ptr msg);
/*
parames:
argv[1] - SSID of the WiFi network to connect to
argv[2] - Password for the WiFi network (if applicable)
*/
int main(int argc, char *argv[])
{
// 开启4G模块
StartNet();
this_thread::sleep_for(chrono::seconds(2));
// StartNet();
// this_thread::sleep_for(chrono::seconds(2));
// 关闭全部信号
blockAllSignals();
signal(SIGINT, Exit); // 捕获Ctrl+C信号
// 初始化mqtt服务器
// 初始化mqtt
mqttInit();
// // 开启服务器
// 如果没有进行过初始化,发送物联网卡信息进行认证
if (ConfirmInit() == false)
{
SendCardInfo(client);
}
else
{
if (verification() == false)
{
cerr << "验证失败" << endl;
return 0;
}
}
// // // 开启服务器
// // 如果没有进行过初始化,发送物联网卡信息进行认证
// if (ConfirmInit() == false)
// {
// SendCardInfo(client);
// }
// else
// {
// if (verification() == false)
// {
// cerr << "验证失败" << endl;
// return 0;
// }
// }
// 开启服务
thread(StartService).detach();
@@ -325,68 +320,13 @@ void StartNet()
// 进行验证
bool verification()
{
// 读取文件密文
ReadFile *rf = new ReadFile(passwd);
bool flag = false;
auto pass = rf->ReadLines();
// 若为空:改写初始化标志位false,重认证
if (pass.empty())
{
WriteFile *wf = new WriteFile(filepath);
wf->overwriteAtPos("no", wf->countBytesPattern("InitOrNot:", true), 3);
return 0;
}
bool flag = false; // 是否通过验证
for (auto &ii : pass)
{
// 不为空,线上发送密文开始认证
auto pos = format(R"({{"cardNo":"{}"}})", ii);
// 获取ICCID
string ICCID = GetSimICCID();
auto res = NetRequest::QuickPostJson(url, pos);
auto res = NetRequest::QuickPostJson(url,ICCID);
int code = -1;
if (res)
{
try
{
code = nlohmann::json::parse(res->body).value("code", -1);
}
catch (const std::exception &e)
{
std::cerr << "JSON解析失败: " << e.what() << std::endl;
}
}
if (res && code == 200)
{
cout << "线上:认证成功" << endl;
flag = true;
}
else
{
try
{
// 进行线下认证
// 获取cpu序列号和SIM卡号
string CardID = GetCardInfo();
string SIMID = GetSimICCID();
string info = CardID + SIMID;
// 进行MD5加密
string md5 = MD5(info);
cout << md5 << endl;
if (md5.compare(ii) == 0)
{
cout << "线下:认证成功" << endl;
flag = true;
}
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
}
}
cout << "结束" << endl;
return flag;
}