This commit is contained in:
JiXieShi
2024-11-16 23:59:15 +08:00
parent f722153536
commit 87859c7bb8
42 changed files with 2018 additions and 485 deletions
+146
View File
@@ -0,0 +1,146 @@
# Device SDK for C++
## 依赖
- libcurl
- nlohmann/json
- OpenSSL
### Windows
1. vcpkg 安装依赖:
```bash
vcpkg install curl:x64-windows
vcpkg install openssl:x64-windows
vcpkg install nlohmann-json:x64-windows
```
### Linux
1. Ubuntu/Debian:
```bash
sudo apt-get install libcurl4-openssl-dev libssl-dev nlohmann-json-dev
```
2. CentOS/RHEL:
```bash
sudo yum install libcurl-devel openssl-devel nlohmann-json-devel
```
## 编译
### Windows (Visual Studio)
1. 添加包含目录:
- $(vcpkg_root)/installed/x64-windows/include
2. 添加库目录:
- $(vcpkg_root)/installed/x64-windows/lib
3. 添加依赖库:
- libcurl
- libssl
- libcrypto
### Linux
```bash
g++ -o example example.cpp device_sdk.cpp -lcurl -lssl -lcrypto -I/usr/include/nlohmann
```
## 基本用法
### 初始化
```cpp
#include "device_sdk.h"
DeviceClient client("http://localhost:8080", "your-32-byte-encrypt-key-here123456");
```
### 设备注册
```cpp
// 不带授权码注册
DeviceInfo device;
device.uid = "device-001";
device.device_model = "test-model";
std::string error;
if (!client.registerDevice(device, error)) {
std::cerr << "注册失败: " << error << std::endl;
return;
}
// 带授权码注册
DeviceInfo deviceWithLicense;
deviceWithLicense.uid = "device-002";
deviceWithLicense.device_model = "test-model";
deviceWithLicense.license_code = "your-license-code";
if (!client.registerDevice(deviceWithLicense, error)) {
std::cerr << "注册失败: " << error << std::endl;
return;
}
```
### 设备验证
```cpp
ValidateResponse response;
std::string error;
if (!client.validateDevice("device-001", response, error)) {
std::cerr << "验证失败: " << error << std::endl;
return;
}
std::cout << "设备状态: " << response.status << std::endl;
std::cout << "授权类型: " << response.license_type << std::endl;
std::cout << "过期时间: " << response.expire_time << std::endl;
```
### 更新启动次数
```cpp
int count;
std::string error;
if (!client.updateStartCount("device-001", count, error)) {
std::cerr << "更新失败: " << error << std::endl;
return;
}
std::cout << "当前启动次数: " << count << std::endl;
```
### 绑定授权码
```cpp
std::string error;
if (!client.bindLicense("device-001", "license-code-123", error)) {
std::cerr << "绑定失败: " << error << std::endl;
return;
}
```
## 完整示例
```cpp
#include <iostream>
#include "device_sdk.h"
int main() {
try {
DeviceClient client("http://localhost:8080", "your-32-byte-encrypt-key-here123456");
std::string error;
// 注册设备
DeviceInfo device;
device.uid = "device-001";
device.device_model = "test-model";
if (!client.registerDevice(device, error)) {
std::cerr << "注册失败: " << error << std::endl;
return 1;
}
std::cout << "设备注册成功" << std::endl;
// 绑定授权码
if (!client.bindLicense("device-001", "license-code-123", error)) {
std::cerr << "绑定失败: " << error << std::endl;
return 1;
}
std::cout << "授权码绑定成功" << std::endl;
// 验证设备
ValidateResponse response;
if (!client.validateDevice("device-001", response, error)) {
std::cerr << "验证失败: " << error << std::endl;
return 1;
}
std::cout << "设备状态: " << response.status << std::endl;
std::cout << "授权类型: " << response.license_type << std::endl;
// 更新启动次数
int count;
if (!client.updateStartCount("device-001", count, error)) {
std::cerr << "更新失败: " << error << std::endl;
return 1;
}
std::cout << "当前启动次数: " << count << std::endl;
} catch (const std::exception& e) {
std::cerr << "发生错误: " << e.what() << std::endl;
return 1;
}
return 0;
}
```
+100
View File
@@ -0,0 +1,100 @@
#include "device_sdk.h"
#include <sstream>
#include <iomanip>
#include <vector>
DeviceClient::DeviceClient(const std::string& base_url, const std::string& encrypt_key)
: base_url_(base_url), encrypt_key_(encrypt_key) {
curl_global_init(CURL_GLOBAL_ALL);
curl_ = curl_easy_init();
}
DeviceClient::~DeviceClient() {
if (curl_) {
curl_easy_cleanup(curl_);
}
curl_global_cleanup();
}
bool DeviceClient::registerDevice(const DeviceInfo& device_info, std::string& error) {
json request;
request["uid"] = device_info.uid;
request["device_model"] = device_info.device_model;
if (!device_info.license_code.empty()) {
request["license_code"] = device_info.license_code;
}
std::string response;
if (!post(base_url_ + "/api/devices/register", request.dump(), response, error)) {
return false;
}
auto resp_json = json::parse(response);
if (resp_json["code"] != 0) {
error = resp_json["error"];
return false;
}
return true;
}
bool DeviceClient::validateDevice(const std::string& uid, ValidateResponse& response, std::string& error) {
std::string encrypted_response;
if (!get(base_url_ + "/api/devices/" + uid + "/validate", encrypted_response, error)) {
return false;
}
auto resp_json = json::parse(encrypted_response);
if (resp_json["code"] != 0) {
error = resp_json["error"];
return false;
}
std::string decrypted = decryptResponse(resp_json["data"]);
auto data = json::parse(decrypted);
response.status = data["status"];
response.license_type = data["license_type"];
response.expire_time = data["expire_time"];
response.start_count = data["start_count"];
response.max_uses = data["max_uses"];
response.timestamp = data["timestamp"];
return true;
}
bool DeviceClient::updateStartCount(const std::string& uid, int& count, std::string& error) {
std::string response;
if (!post(base_url_ + "/api/devices/" + uid + "/start", "", response, error)) {
return false;
}
auto resp_json = json::parse(response);
if (resp_json["code"] != 0) {
error = resp_json["error"];
return false;
}
count = resp_json["data"]["start_count"];
return true;
}
bool DeviceClient::bindLicense(const std::string& uid, const std::string& license_code, std::string& error) {
json request;
request["license_code"] = license_code;
std::string response;
if (!post(base_url_ + "/api/devices/" + uid + "/license", request.dump(), response, error)) {
return false;
}
auto resp_json = json::parse(response);
if (resp_json["code"] != 0) {
error = resp_json["error"];
return false;
}
return true;
}
// ... HTTP 请求和加密相关的辅助函数实现 ...
+61
View File
@@ -0,0 +1,61 @@
#ifndef DEVICE_SDK_H
#define DEVICE_SDK_H
#ifdef _WIN32
#include <windows.h>
#include <winhttp.h>
#else
#include <curl/curl.h>
#endif
#include <string>
#include <memory>
#include <ctime>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
struct DeviceInfo {
std::string uid;
std::string device_model;
std::string license_code; // 可选
};
struct ValidateResponse {
std::string status;
std::string license_type;
std::string expire_time;
int start_count;
int max_uses;
int64_t timestamp;
};
class DeviceClient {
public:
DeviceClient(const std::string& base_url, const std::string& encrypt_key);
~DeviceClient();
// 设备注册
bool registerDevice(const DeviceInfo& device_info, std::string& error);
// 设备验证
bool validateDevice(const std::string& uid, ValidateResponse& response, std::string& error);
// 更新启动次数
bool updateStartCount(const std::string& uid, int& count, std::string& error);
// 绑定授权码
bool bindLicense(const std::string& uid, const std::string& license_code, std::string& error);
private:
std::string base_url_;
std::string encrypt_key_;
CURL* curl_;
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp);
bool post(const std::string& url, const std::string& data, std::string& response, std::string& error);
bool get(const std::string& url, std::string& response, std::string& error);
std::string decryptResponse(const std::string& encrypted);
};
#endif // DEVICE_SDK_H