#define LOG_TAG "tbox_test"
#include <cutils/log.h>
#include <iostream>
#include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
#include "GB32960MessageData.h"
#include "PlatformLoginMessage.h"
#include "VehicleLoginMessage.h"
#include "GB32960MessageParser.h"
#include "VehicleLogoutMessage.h"
#include "RealTimeDataMessage.h"
#include "DriveMotorDataMessage.h"
#include "rechargeable_energy_temperature_message.h"
#include "ExtremeDataMessage.h"
#include "FaultDataMessage.h"
#include "LocationDataMessage.h"
#include "VehicleDataMessage.h"
#include "battery_voltage_message.h"
#include "CustomInfoMessage.h"
#include "PropertyManager.h"
#include <bitset>
#include <iomanip>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <unordered_map>
#include <sstream>
#include <algorithm>
#include <fstream>
#include <chrono>
#include <csignal>
#include <iomanip>
#include <netdb.h>
#include <types.h>
using namespace android;
using namespace android::hardware;
using namespace android::hardware::automotive::vehicle::V2_0;
using ::android::sp;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_handle;
#define LOG_NDEBUG 1
#define MAX_BUFFER_SIZE 1024
enum class MachineState {
Login,
Realtime,
Logout,
Heartbeat,
aircondcontrol,
end
};
uint16_t MachineLoginserialNumber;
AirconditioningMessage::aircondstr gAircondstr;
AirconditioningMessage::aircondstr gAircondstrcb;
void updateaircond(int prop,int value) {
std::cout << "yyy :updateaircond " << prop << " updated to: " << value << std::endl;
switch (prop) {
case 354419984: //toInt(VehicleProperty::HVAC_POWER_ON): // 354419984:
gAircondstrcb.acPowerSwitch = value;
break;
case 354419976: //toInt(VehicleProperty::HVAC_RECIRC_ON): //354419976:
gAircondstrcb.recircMode = value;
break;
case 356517121: //toInt(VehicleProperty::HVAC_FAN_DIRECTION): // 356517121:
gAircondstrcb.fanDirection = value;
break;
case 354419973: //toInt(VehicleProperty::HVAC_AC_ON): //354419973:
gAircondstrcb.acSwitch = value;
break;
case 287311126: //toInt(VehicleProperty::HVAC_PTC_HEAT_ON): // 287311126
gAircondstrcb.ptcHeatSwitch = value;
break;
case 320865540: //toInt(VehicleProperty::HVAC_DEFROSTER): //320865540:
gAircondstrcb.defrostSwitch = value;
break;
case 356517120: //toInt(VehicleProperty::HVAC_FAN_SPEED): //356517120:
gAircondstrcb.fanSpeed = value;
break;
}
}
class TboxVehicleCallback : public IVehicleCallback {
public:
Return<void> onPropertyEvent(const hidl_vec<VehiclePropValue>& values) override {
updateaircond(values[0].prop,std::stoi(toString(values[0].value.int32Values[0])));
std::cout << "yyy : Property " << values[0].prop << " updated to: ";
for (const auto& value : values[0].value.int32Values) {
std::cout << value <<" ";
}
std::cout << std::endl;
return Void();
}
Return<void> onPropertySet(const VehiclePropValue& /* value */) override {
return Return<void>();
}
Return<void> onPropertySetError(StatusCode /* errorCode */, int32_t /* propId */,
int32_t /* areaId */) override {
return Return<void>();
}
};
// 发送队列管理
class SendQueue {
private:
std::queue<std::vector<uint8_t>> queue_;
std::mutex mutex_;
std::condition_variable cv_;
public:
void enqueue(const std::vector<uint8_t>& message) {
// std::cout << "SendQueue enqueue" << std::endl;
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(message);
cv_.notify_one();
}
std::vector<uint8_t> dequeue() {
// std::cout << "SendQueue dequeue" << std::endl;
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock, [this] { return !queue_.empty(); });
std::vector<uint8_t> message = queue_.front();
queue_.pop();
return message;
}
};
// 接收队列管理
class ReceiveQueue {
private:
std::queue<std::vector<uint8_t>> queue_;
std::mutex mutex_;
std::condition_variable cv_;
public:
void enqueue(const std::vector<uint8_t>& message) {
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(message);
cv_.notify_one();
}
std::vector<uint8_t> dequeue() {
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock, [this] { return !queue_.empty(); });
std::vector<uint8_t> message = queue_.front();
queue_.pop();
return message;
}
};
// 信息生产者
class MessageProducer {
static MessageProducer* instance;
private:
MachineState currentState_;
public:
PropertyManager manager;
MessageProducer() : currentState_(MachineState::Login) {
instance = this;
}
int logoutFlag = 0;
std::vector<uint8_t> produceMessage() {
std::vector<uint8_t> message;
switch (currentState_) {
case MachineState::Login:
// 生成车辆登入的二进制信息
message = generateLoginMessage();
break;
case MachineState::Logout:
// 生成车辆登出的二进制信息
message = generateLogoutMessage();
break;
case MachineState::Realtime:
// 生成实时信息上报的二进制信息
message = generateRealtimeMessage();
break;
case MachineState::Heartbeat:
// 生成心跳报文
message = generateHeartbeatMessage();
break;
case MachineState::aircondcontrol:
// 生成空调报文
message = aircondcontrolMessage();
break;
default:
break;
}
return message;
}
void setStateLogin() {
currentState_ = MachineState::Login;
}
void setStateLogout() {
currentState_ = MachineState::Logout;
}
void setStateRealtime() {
currentState_ = MachineState::Realtime;
}
void setStateHeartbeat() {
currentState_ = MachineState::Heartbeat;
}
void setStateAircond() {
currentState_ = MachineState::aircondcontrol;
}
void setStateEnd() {
currentState_ = MachineState::end;
}
MachineState getCurrentState() const {
std::cout << "yyy:getcurrentState ";
switch (currentState_) {
case MachineState::Login:
std::cout << "Login";
break;
case MachineState::Realtime:
std::cout << "Realtime";
break;
case MachineState::Logout:
std::cout << "Logout";
break;
case MachineState::Heartbeat:
std::cout << "Heartbeat";
break;
case MachineState::aircondcontrol:
std::cout << "aircondcontrol";
break;
case MachineState::end:
std::cout << "end";
break;
}
std::cout << std::endl;
return currentState_;
}
static void killsignal(int signum) {
std::cout << "signum is:" << signum << std::endl;
instance->setStateLogout();
}
void getaircondconfig(sp<IVehicle> vehicle,sp<TboxVehicleCallback> callback) {
std::cout << "yyy:setaircondconfig" << std::endl;
hidl_vec<SubscribeOptions> options = {
SubscribeOptions{.propId = static_cast<int>(VehicleProperty::HVAC_POWER_ON), 10, .flags = SubscribeFlags::EVENTS_FROM_CAR},
SubscribeOpt