优化代码
parent
68a627d6af
commit
883b5d7e8b
Binary file not shown.
@ -0,0 +1,135 @@
|
||||
//
|
||||
// Created by Matthew on 2023/12/27.
|
||||
//
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#include "GPIOControl.h"
|
||||
|
||||
#ifdef _DEBUG
|
||||
#include <AndroidHelper.h>
|
||||
#endif
|
||||
|
||||
#define IOT_PARAM_WRITE 0xAE
|
||||
#define IOT_PARAM_READ 0xAF
|
||||
#define MAX_STRING_LEN 32
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int cmd;
|
||||
int value;
|
||||
int result;
|
||||
long value2;
|
||||
char str[MAX_STRING_LEN];
|
||||
}IOT_PARAM;
|
||||
|
||||
void GpioControl::setInt(int cmd, int value)
|
||||
{
|
||||
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
||||
IOT_PARAM param;
|
||||
param.cmd = cmd;
|
||||
param.value = value;
|
||||
// LOGE("set_int fd=%d,cmd=%d,value=%d\r\n",fd, cmd, value);
|
||||
if( fd > 0 )
|
||||
{
|
||||
int res = ioctl(fd, IOT_PARAM_WRITE, ¶m);
|
||||
// LOGE("set_int22 cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result);
|
||||
close(fd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int GpioControl::getInt(int cmd)
|
||||
{
|
||||
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
||||
// LOGE("get_int fd=%d,cmd=%d\r\n",fd, cmd);
|
||||
if( fd > 0 )
|
||||
{
|
||||
IOT_PARAM param;
|
||||
param.cmd = cmd;
|
||||
ioctl(fd, IOT_PARAM_READ, ¶m);
|
||||
#ifdef _DEBUG
|
||||
ALOGI("getInt cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result);
|
||||
#endif
|
||||
close(fd);
|
||||
return param.value;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void GpioControl::setLong(int cmd, long value)
|
||||
{
|
||||
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
||||
IOT_PARAM param;
|
||||
param.cmd = cmd;
|
||||
param.value2 = value;
|
||||
// LOGE("set_long fd=%d,cmd=%d,value2=%ld\r\n",fd, param.cmd, param.value2);
|
||||
|
||||
if( fd > 0 )
|
||||
{
|
||||
ioctl(fd, IOT_PARAM_WRITE, ¶m);
|
||||
// LOGE("set_long22 cmd=%d,value2=%ld,result=%d\r\n",param.cmd, param.value2, param.result);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
long GpioControl::getLong(int cmd)
|
||||
{
|
||||
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
||||
// LOGE("get_long fd=%d,cmd=%d\r\n",fd, cmd);
|
||||
if( fd > 0 )
|
||||
{
|
||||
IOT_PARAM param;
|
||||
param.cmd = cmd;
|
||||
ioctl(fd, IOT_PARAM_READ, ¶m);
|
||||
// LOGE("get_long22 cmd=%d,value2=%ld,result=%d\r\n",param.cmd, param.value2, param.result);
|
||||
close(fd);
|
||||
return param.value2;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void GpioControl::setString(int cmd, const std::string& value)
|
||||
{
|
||||
IOT_PARAM param;
|
||||
// char *pval = jstringToChars(env, value);
|
||||
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
||||
int len = MAX_STRING_LEN < value.size() ? MAX_STRING_LEN : value.size();
|
||||
|
||||
param.cmd = cmd;
|
||||
memset(param.str, 0, MAX_STRING_LEN);
|
||||
memcpy(param.str, value.c_str(), len);
|
||||
// LOGE("set_string fd=%d,cmd=%d,str=%s\r\n",fd, param.cmd, param.str);
|
||||
if( fd > 0 )
|
||||
{
|
||||
ioctl(fd, IOT_PARAM_WRITE, ¶m);
|
||||
// LOGE("set_string22 cmd=%d,str=%s,result=%d\r\n",param.cmd, param.str, param.result);
|
||||
close(fd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
std::string GpioControl::getString(int cmd)
|
||||
{
|
||||
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
||||
// LOGE("get_string fd=%d,cmd=%d\r\n",fd, cmd);
|
||||
if( fd > 0 )
|
||||
{
|
||||
IOT_PARAM param;
|
||||
param.cmd = cmd;
|
||||
ioctl(fd, IOT_PARAM_READ, ¶m);
|
||||
// LOGE("get_string22 cmd=%d,str=%s,result=%d\r\n",param.cmd, param.str, param.result);
|
||||
close(fd);
|
||||
return std::string(param.str);
|
||||
}
|
||||
return "";
|
||||
}
|
@ -0,0 +1,162 @@
|
||||
//
|
||||
// Created by Matthew on 2023/12/27.
|
||||
//
|
||||
|
||||
#ifndef MICROPHOTO_GPIOCONTROL_H
|
||||
#define MICROPHOTO_GPIOCONTROL_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#define CMD_GET_LIGHT_ADC 101
|
||||
#define CMD_SET_LIGHT_ADC 102
|
||||
#define CMD_GET_KEY_LOCKSTATE 103
|
||||
#define CMD_GET_BAT_ADC 104
|
||||
#define CMD_SET_FLASH_LED 105
|
||||
#define CMD_SET_NETWORK_STATE 106
|
||||
#define CMD_SET_OTG_STATE 107
|
||||
#define CMD_GET_OTG_STATE 108
|
||||
#define CMD_GET_CHARGING_VOL_STATE 110
|
||||
#define CMD_GET_CHARGING_SHUNT_VOLTAGE_STATE 111
|
||||
#define CMD_GET_CHARGING_BUS_VOLTAGE_STATE 112
|
||||
#define CMD_GET_CHARGING_POWER_STATE 113
|
||||
#define CMD_GET_CHARGING_CURRENT_STATE 114
|
||||
#define CMD_GET_BAT_VOL_STATE 115
|
||||
#define CMD_GET_BAT_SHUNT_VOLTAGE_STATE 116
|
||||
#define CMD_GET_BAT_BUS_VOLTAGE_STATE 117
|
||||
#define CMD_GET_BAT_POWER_STATE 118
|
||||
#define CMD_GET_BAT_CURRENT_STATE 119
|
||||
#define CMD_SET_485_STATE 121
|
||||
#define CMD_SET_SPI_MODE 123
|
||||
#define CMD_SET_SPI_BITS_PER_WORD 124
|
||||
#define CMD_SET_SPI_MAXSPEEDHZ 125
|
||||
#define CMD_SET_PWM_BEE_STATE 126
|
||||
#define CMD_SET_ALM_MODE 128
|
||||
#define CMD_SET_485_EN_STATE 131
|
||||
#define CMD_SET_CAM_3V3_EN_STATE 132
|
||||
#define CMD_SET_12V_EN_STATE 133
|
||||
#define CMD_SET_SYSTEM_RESET 202
|
||||
|
||||
class GpioControl
|
||||
{
|
||||
public:
|
||||
|
||||
static void setInt(int cmd, int value);
|
||||
static int getInt(int cmd);
|
||||
static void setLong(int cmd, long value);
|
||||
static long getLong(int cmd);
|
||||
static void setString(int cmd, const std::string& value);
|
||||
static std::string getString(int cmd);
|
||||
|
||||
static void setOtgState(bool on)
|
||||
{
|
||||
setInt(CMD_SET_OTG_STATE, on ? 1 : 0);
|
||||
}
|
||||
|
||||
static bool getOtgState()
|
||||
{
|
||||
return getInt(CMD_SET_OTG_STATE) != 0;
|
||||
}
|
||||
|
||||
static void setCam3V3Enable(bool enabled)
|
||||
{
|
||||
setInt(CMD_SET_CAM_3V3_EN_STATE, enabled ? 1 : 0);
|
||||
}
|
||||
|
||||
static void reboot()
|
||||
{
|
||||
setInt(CMD_SET_SYSTEM_RESET, 1);
|
||||
}
|
||||
|
||||
static void setLightAdc(int i)
|
||||
{
|
||||
setInt(CMD_SET_LIGHT_ADC, i);
|
||||
}
|
||||
|
||||
static int getLightAdc()
|
||||
{
|
||||
return getInt(CMD_GET_LIGHT_ADC);
|
||||
}
|
||||
|
||||
static int getChargingVoltage()
|
||||
{
|
||||
return getInt(CMD_GET_CHARGING_VOL_STATE);
|
||||
}
|
||||
|
||||
static int getChargingShuntVoltage()
|
||||
{
|
||||
return getInt(CMD_GET_CHARGING_SHUNT_VOLTAGE_STATE);
|
||||
}
|
||||
|
||||
static int getChargingBusVoltage() {
|
||||
return getInt(CMD_GET_CHARGING_BUS_VOLTAGE_STATE);
|
||||
}
|
||||
|
||||
static int getChargingPower() {
|
||||
return getInt(CMD_GET_CHARGING_POWER_STATE);
|
||||
}
|
||||
|
||||
static int getChargingCurrent() {
|
||||
return getInt(CMD_GET_CHARGING_CURRENT_STATE);
|
||||
}
|
||||
|
||||
static int getBatteryVoltage() {
|
||||
return getInt(CMD_GET_BAT_VOL_STATE);
|
||||
}
|
||||
|
||||
static int getBatteryShuntVoltage() {
|
||||
return getInt(CMD_GET_BAT_SHUNT_VOLTAGE_STATE);
|
||||
}
|
||||
|
||||
static int getBatteryBusVoltage() {
|
||||
return getInt(CMD_GET_BAT_BUS_VOLTAGE_STATE);
|
||||
}
|
||||
|
||||
static int getBatteryPower() {
|
||||
return getInt(CMD_GET_BAT_POWER_STATE);
|
||||
}
|
||||
|
||||
static int getBatteryCurrent() {
|
||||
return getInt(CMD_GET_BAT_CURRENT_STATE);
|
||||
}
|
||||
|
||||
static void set485WriteMode() {
|
||||
setInt(CMD_SET_485_STATE, 1);
|
||||
}
|
||||
|
||||
static void set485ReadMode() {
|
||||
setInt(CMD_SET_485_STATE, 0);
|
||||
}
|
||||
|
||||
static void setSpiMode(int i) {
|
||||
setInt(CMD_SET_SPI_MODE, i);
|
||||
}
|
||||
|
||||
static void setSpiBitsPerWord(int i) {
|
||||
setInt(CMD_SET_SPI_BITS_PER_WORD, i);
|
||||
}
|
||||
|
||||
static void setSpiMaxSpeedHz(long j) {
|
||||
setLong(CMD_SET_SPI_MAXSPEEDHZ, j);
|
||||
}
|
||||
|
||||
static void setBeeOn(bool z) {
|
||||
setInt(CMD_SET_PWM_BEE_STATE, z ? 1 : 0);
|
||||
}
|
||||
|
||||
static void setJidianqiState(bool z) {
|
||||
setInt(CMD_SET_ALM_MODE, z ? 1 : 0);
|
||||
}
|
||||
|
||||
static void setRS485Enable(bool z) {
|
||||
setInt(CMD_SET_485_EN_STATE, z ? 1 : 0);
|
||||
}
|
||||
|
||||
|
||||
static void set12VEnable(bool z) {
|
||||
setInt(CMD_SET_12V_EN_STATE, z ? 1 : 0);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //MICROPHOTO_GPIOCONTROL_H
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,60 @@
|
||||
#ifndef __NRSECPORT_H__
|
||||
#define __NRSECPORT_H__
|
||||
|
||||
#include "SpiLib.h"
|
||||
|
||||
|
||||
#define CMD_HEAD_SIZE 5
|
||||
using namespace std;
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
class NrsecPort : public SpiLIb {
|
||||
public:
|
||||
|
||||
NrsecPort();
|
||||
~NrsecPort();
|
||||
|
||||
bool Open(const char* path);
|
||||
|
||||
int Spirandom();
|
||||
std::string Version();
|
||||
int Indentify(unsigned char *to_idt, unsigned char *out_idt);
|
||||
int SM2keypair(int index);
|
||||
int SM2ExportPublicKey(int index, unsigned char result[]);
|
||||
int SM2ExportPrivateKey(int index, unsigned char result[]);
|
||||
int SM2InportPublicKey(int index, const unsigned char new_key[]);
|
||||
int SM2InportPrivateKey(int index, const unsigned char new_key[]);
|
||||
int SM3Hash(unsigned char *to_hash, int len, unsigned char *out_hash);
|
||||
int sm3hash_tosm2(unsigned char *in, int inl, unsigned char *out, unsigned char *pubkey, unsigned char
|
||||
*pucID, int idl);
|
||||
int SM2Sign(int index, const unsigned char *to_sign, unsigned char *out_sign);
|
||||
int SM2VerifySign(int index, unsigned char *hash, unsigned char *vs);
|
||||
int SM2encrypt(int index, unsigned char *to_encrypt, unsigned char *out_encrypt);
|
||||
int SM2decoder(int index, unsigned char *to_decoder, unsigned char *out_decoder);
|
||||
int SM2cert(int type, int index, string cert, unsigned char *out_cert);
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
unsigned char get_crc7(const unsigned char *buff, int len);
|
||||
void SendCMD(uint8_t *cmd, uint8_t *rxbuf);
|
||||
void RcvINS(uint8_t *txbuf, uint8_t *buf, uint8_t ins);
|
||||
void RcvLEN(uint8_t *txbuf, uint8_t *buf, uint8_t len);
|
||||
void RcvData(uint8_t *txbuf, uint8_t *buf);
|
||||
void RcvData(uint8_t *txbuf, uint8_t *buf, int len);
|
||||
void RcvSW(uint8_t *txbuf, uint8_t *buf, uint8_t sw);
|
||||
void SendEnd(uint8_t *txbuf, uint8_t *buf);
|
||||
void SendId(uint8_t *txbuf, uint8_t *buf, uint8_t id);
|
||||
void SendData(uint8_t *data, uint8_t *rxbuf, int data_size);
|
||||
|
||||
private:
|
||||
std::string m_path;
|
||||
int m_fd;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // __NRSECPORT_H__
|
File diff suppressed because it is too large
Load Diff
@ -1,62 +0,0 @@
|
||||
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <android/log.h>
|
||||
#include <string>
|
||||
|
||||
#include "SpiPort.h"
|
||||
|
||||
SpiPort::SpiPort(const std::string& path) : m_path(path), m_fd(0)
|
||||
{
|
||||
}
|
||||
|
||||
SpiPort::~SpiPort()
|
||||
{
|
||||
if (m_fd > 0)
|
||||
{
|
||||
close(m_fd);
|
||||
}
|
||||
}
|
||||
|
||||
bool SpiPort::Open()
|
||||
{
|
||||
if(m_fd <= 0) m_fd = open(m_path.c_str(), O_RDWR/*|O_NDELAY|O_NOCTTY*/);
|
||||
if(m_fd <= 0 ) {
|
||||
int err = errno;
|
||||
m_log = "open spi Error errno=" + std::to_string(err);
|
||||
__android_log_print(ANDROID_LOG_INFO, "SPi", "open spi Error errno=%d", err);
|
||||
return false;
|
||||
}
|
||||
else __android_log_print(ANDROID_LOG_INFO, "SPi", "open spi Success m_fd=%d",m_fd);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// write
|
||||
int SpiPort::Write(unsigned char *buf, int len)
|
||||
{
|
||||
return write(m_fd, buf, len);
|
||||
}
|
||||
|
||||
// read
|
||||
int SpiPort::Read(unsigned char *buf, int len)
|
||||
{
|
||||
return read(m_fd, buf, len);
|
||||
}
|
||||
|
||||
//close
|
||||
bool SpiPort::Close()
|
||||
{
|
||||
if(m_fd > 0)
|
||||
{
|
||||
close(m_fd);
|
||||
m_fd = 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
#ifndef _SPIPORT_H_
|
||||
#define _SPIPORT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
class SpiPort
|
||||
{
|
||||
public:
|
||||
|
||||
SpiPort(const std::string& path);
|
||||
~SpiPort();
|
||||
|
||||
bool Open();
|
||||
int Write(unsigned char *buf, int len);
|
||||
int Read(unsigned char *buf, int len);
|
||||
|
||||
bool Close();
|
||||
|
||||
std::string GetLog() const
|
||||
{
|
||||
return m_log;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::string m_path;
|
||||
std::string m_log;
|
||||
int m_fd;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue