yanzheng
This commit is contained in:
Binary file not shown.
@@ -22,10 +22,12 @@ const string envPath = "/home/orangepi/RKApp/InitAuth/conf/.env";
|
|||||||
// MQTT相关配置
|
// MQTT相关配置
|
||||||
const string mqtt_url = "tcp://192.168.12.1:1883";
|
const string mqtt_url = "tcp://192.168.12.1:1883";
|
||||||
const string clientId = "RK3588_SubTest";
|
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;
|
const int Qos = 1;
|
||||||
|
|
||||||
atomic<bool> isRunning(true);
|
atomic<bool> isRunning(true);
|
||||||
|
atomic<bool> Deactivate(false); // 激活标志
|
||||||
|
|
||||||
mqtt::async_client client(mqtt_url, clientId);
|
mqtt::async_client client(mqtt_url, clientId);
|
||||||
|
|
||||||
@@ -45,12 +47,17 @@ void messageCallback(mqtt::const_message_ptr msg);
|
|||||||
// 检测设备是否已进行初始化
|
// 检测设备是否已进行初始化
|
||||||
bool checkUUID();
|
bool checkUUID();
|
||||||
|
|
||||||
|
// 退出程序,开始进行设备验证
|
||||||
|
void StartCheck();
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
if (checkUUID())
|
Deactivate = checkUUID(); // 判断是否已被激活
|
||||||
|
if (Deactivate)
|
||||||
{
|
{
|
||||||
// 设备已被激活,调用验证程序,退出本程序
|
// 设备已被激活,调用验证程序,退出本程序
|
||||||
|
cout << "设备已激活,开始验证" << endl;
|
||||||
|
StartCheck();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +71,14 @@ int main()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 退出程序,开始进行设备验证
|
||||||
|
void StartCheck()
|
||||||
|
{
|
||||||
|
isRunning = false; // 退出程序
|
||||||
|
string cmd = "../../softWareInit/bin/wifi ";
|
||||||
|
system(cmd.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
// 检测设备是否已经激活
|
// 检测设备是否已经激活
|
||||||
bool checkUUID()
|
bool checkUUID()
|
||||||
{
|
{
|
||||||
@@ -98,19 +113,21 @@ void messageCallback(mqtt::const_message_ptr msg)
|
|||||||
{
|
{
|
||||||
// 接受App传来的密文,并保存在配置文件中
|
// 接受App传来的密文,并保存在配置文件中
|
||||||
auto buffer = msg->to_string();
|
auto buffer = msg->to_string();
|
||||||
cout << "msg: " << buffer << endl;
|
// cout << "Topic:" << msg->get_topic() << ",msg:" << buffer << endl;
|
||||||
if (buffer.find("Activate") != string::npos)
|
if (buffer.find("Activate") != string::npos)
|
||||||
{
|
{
|
||||||
// 接受请求,发送SIM卡号
|
// 接受请求,发送SIM卡号
|
||||||
string ICCID = GetSimICCID();
|
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)
|
else if (buffer.find("ServerPwd") != string::npos)
|
||||||
{
|
{
|
||||||
// 接受UUID,保存至配置文件中,退出程序,调用设备验证程序
|
// 接受UUID,保存至配置文件中,退出程序,调用设备验证程序
|
||||||
auto res = nlohmann::json::parse(buffer); // 准备解析接受到的秘钥
|
auto res = nlohmann::json::parse(buffer); // 准备解析接受到的秘钥
|
||||||
auto pwd = res["Data"];
|
auto pwd = res["Data"];
|
||||||
cout << pwd << endl;
|
// cout << pwd << endl;
|
||||||
// 写入文件
|
// 写入文件
|
||||||
ReadFile rf(envPath);
|
ReadFile rf(envPath);
|
||||||
|
|
||||||
@@ -127,16 +144,23 @@ void messageCallback(mqtt::const_message_ptr msg)
|
|||||||
{
|
{
|
||||||
WriteFile wf(envPath);
|
WriteFile wf(envPath);
|
||||||
string out;
|
string out;
|
||||||
out.resize(1024);
|
out.reserve(1024);
|
||||||
for (size_t i = 0; i < lines.size(); ++i)
|
for (size_t i = 0; i < lines.size(); ++i)
|
||||||
{
|
{
|
||||||
out += lines[i];
|
out += lines[i];
|
||||||
if (i + 1 < lines.size())
|
if (i + 1 < lines.size())
|
||||||
out += "\n";
|
out += "\n";
|
||||||
}
|
}
|
||||||
wf.overwriteText(out); })
|
wf.overwriteText(out);
|
||||||
|
|
||||||
|
Deactivate =true; })
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Deactivate)
|
||||||
|
{ // 已激活
|
||||||
|
StartCheck();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 连接成功的回调
|
// 连接成功的回调
|
||||||
@@ -150,8 +174,8 @@ void mqttInit()
|
|||||||
client.set_message_callback(messageCallback);
|
client.set_message_callback(messageCallback);
|
||||||
client.set_connected_handler(connectCallback);
|
client.set_connected_handler(connectCallback);
|
||||||
|
|
||||||
client.connect()->wait(); // 进行连接
|
client.connect()->wait(); // 进行连接
|
||||||
client.subscribe(Topic, Qos)->wait(); // 订阅话题
|
client.subscribe(TopicRecv, Qos)->wait(); // 订阅话题
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取SIM卡号
|
// 获取SIM卡号
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
UUID:null
|
UUID:null
|
||||||
|
|
||||||
#以下配置用于获取云端发送秘钥
|
#以下配置用于获取云端发送秘钥
|
||||||
ServerPwd:null
|
ServerPwd:"17227ca72f30f8bca8158bb78f25b43e"
|
||||||
|
|
||||||
#以下配置存储报警输出高低电平
|
#以下配置存储报警输出高低电平
|
||||||
outSignal:low
|
outSignal:low
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
本程序提供以下功能:
|
本程序用于设备验证
|
||||||
1.初始化4G模块,提供网络功能
|
验证通过执行以下功能:
|
||||||
2.进行RTSP推流
|
1.启动RTSP推流
|
||||||
3.进行设备验证,通过后,启动系统各项服务
|
2.启动系统各项服务,用于和双目交互
|
||||||
|
3.启动mqtt,与App进行数据交互
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -73,7 +74,7 @@ string GetSimICCID(const string &tty = "/dev/ttyUSB2");
|
|||||||
// 发送cpu序列号和SIM卡号
|
// 发送cpu序列号和SIM卡号
|
||||||
void SendCardInfo(mqtt::async_client &client);
|
void SendCardInfo(mqtt::async_client &client);
|
||||||
|
|
||||||
// 进行验证
|
// 开始进行设备验证
|
||||||
bool verification();
|
bool verification();
|
||||||
|
|
||||||
// 解析接收的数据
|
// 解析接收的数据
|
||||||
@@ -106,39 +107,33 @@ void mqttInit();
|
|||||||
// 接收消息回调
|
// 接收消息回调
|
||||||
void getMsgCallback(mqtt::const_message_ptr msg);
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
// 开启4G模块
|
// 开启4G模块
|
||||||
StartNet();
|
// StartNet();
|
||||||
|
// this_thread::sleep_for(chrono::seconds(2));
|
||||||
this_thread::sleep_for(chrono::seconds(2));
|
|
||||||
|
|
||||||
// 关闭全部信号
|
// 关闭全部信号
|
||||||
blockAllSignals();
|
blockAllSignals();
|
||||||
signal(SIGINT, Exit); // 捕获Ctrl+C信号
|
signal(SIGINT, Exit); // 捕获Ctrl+C信号
|
||||||
|
|
||||||
// 初始化mqtt服务器
|
// 初始化mqtt
|
||||||
mqttInit();
|
mqttInit();
|
||||||
|
|
||||||
// // 开启服务器
|
// // // 开启服务器
|
||||||
// 如果没有进行过初始化,发送物联网卡信息进行认证
|
// // 如果没有进行过初始化,发送物联网卡信息进行认证
|
||||||
if (ConfirmInit() == false)
|
// if (ConfirmInit() == false)
|
||||||
{
|
// {
|
||||||
SendCardInfo(client);
|
// SendCardInfo(client);
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
if (verification() == false)
|
// if (verification() == false)
|
||||||
{
|
// {
|
||||||
cerr << "验证失败" << endl;
|
// cerr << "验证失败" << endl;
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 开启服务
|
// 开启服务
|
||||||
thread(StartService).detach();
|
thread(StartService).detach();
|
||||||
@@ -325,68 +320,13 @@ void StartNet()
|
|||||||
// 进行验证
|
// 进行验证
|
||||||
bool verification()
|
bool verification()
|
||||||
{
|
{
|
||||||
// 读取文件密文
|
bool flag = false; // 是否通过验证
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &ii : pass)
|
// 获取ICCID
|
||||||
{
|
string ICCID = GetSimICCID();
|
||||||
// 不为空,线上发送密文开始认证
|
|
||||||
auto pos = format(R"({{"cardNo":"{}"}})", ii);
|
|
||||||
|
|
||||||
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;
|
return flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user