diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 07f53d0..16600ba 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -21,7 +21,7 @@ add_library( # Sets the name of the library. SHARED # Provides a relative path to your source file(s). - SpiLib.cpp + SpiPort.cpp NrsecPort.cpp GPIOControl.cpp native-lib.cpp) diff --git a/app/src/main/cpp/NrsecPort.cpp b/app/src/main/cpp/NrsecPort.cpp index 4d1e839..4187768 100644 --- a/app/src/main/cpp/NrsecPort.cpp +++ b/app/src/main/cpp/NrsecPort.cpp @@ -6,54 +6,29 @@ #define TAG_SPI "SPI" -const unsigned char EK_CMD[5] = { 0x80,0xd4,0x01,0x00,0x10 }; -const unsigned char AK_CMD[5] = { 0x80,0xd4,0x02,0x00,0x10 }; -const unsigned char IV_CMD[5] = { 0x80,0xd4,0x04,0x00,0x10 }; -unsigned char SM1encrpt_CMD[5] = { 0xa0,0xe0,0x80,0xff,0xff }; -unsigned char SM1decoder_CMD[5] = { 0xa0,0xe0,0x81,0xff,0xff }; -unsigned char SM2Keypair_CMD[5] = { 0x80,0xb2,0x00,0xff,0x00 }; -unsigned char SM2OutPub_CMD[5] = { 0x80,0xb8,0x01,0xff,0x40 }; -unsigned char SM2OutPri_CMD[5] = { 0x80,0xb8,0x02,0xff,0x20 }; -unsigned char SM2InPub_CMD[5] = { 0x80,0xba,0x01,0xff,0x40 }; -unsigned char SM2InPri_CMD[5] = { 0x80,0xba,0x02,0xff,0x20 }; -unsigned char SM3Hash_CMD[5] = { 0x80,0xb5,0x00,0xff,0xff }; -unsigned char SM2Sign_CMD[5] = { 0x80,0xb4,0x00,0xff,0x20 }; -unsigned char SM2VerifySign_CMD[5] = { 0x80,0xb6,0x00,0xff,0x60 }; -unsigned char SM2encrypt_CMD[5] = { 0x80,0xb3,0x01,0xff,0x20 }; -unsigned char SM2decoder_CMD[5] = { 0x80,0xb3,0x81,0xff,0x80 }; -unsigned char SM2cert_CMD[5] = { 0x80,0xb7,0xff,0xff,0xff }; -unsigned char Random_CMD[5] = { 0x00,0x84,0x00,0x00,0xff }; -const unsigned char Version_CMD[5] = { 0x00,0x5b,0x00,0x00,0x40 }; -const unsigned char Indentify_CMD[5] = { 0x80,0xb3,0x01,0x04,0x20 }; - -NrsecPort::NrsecPort() +const uint8_t EK_CMD[5] = { 0x80,0xd4,0x01,0x00,0x10 }; +const uint8_t AK_CMD[5] = { 0x80,0xd4,0x02,0x00,0x10 }; +const uint8_t IV_CMD[5] = { 0x80,0xd4,0x04,0x00,0x10 }; +uint8_t SM1encrpt_CMD[5] = { 0xa0,0xe0,0x80,0xff,0xff }; +uint8_t SM1decoder_CMD[5] = { 0xa0,0xe0,0x81,0xff,0xff }; +uint8_t SM2Keypair_CMD[5] = { 0x80,0xb2,0x00,0xff,0x00 }; +uint8_t SM2OutPub_CMD[5] = { 0x80,0xb8,0x01,0xff,0x40 }; +uint8_t SM2OutPri_CMD[5] = { 0x80,0xb8,0x02,0xff,0x20 }; +uint8_t SM2InPub_CMD[5] = { 0x80,0xba,0x01,0xff,0x40 }; +uint8_t SM2InPri_CMD[5] = { 0x80,0xba,0x02,0xff,0x20 }; +uint8_t SM3Hash_CMD[5] = { 0x80,0xb5,0x00,0xff,0xff }; +uint8_t SM2Sign_CMD[5] = { 0x80,0xb4,0x00,0xff,0x20 }; +uint8_t SM2VerifySign_CMD[5] = { 0x80,0xb6,0x00,0xff,0x60 }; +uint8_t SM2encrypt_CMD[5] = { 0x80,0xb3,0x01,0xff,0x20 }; +uint8_t SM2decoder_CMD[5] = { 0x80,0xb3,0x81,0xff,0x80 }; +uint8_t SM2cert_CMD[5] = { 0x80,0xb7,0xff,0xff,0xff }; +uint8_t Random_CMD[5] = { 0x00,0x84,0x00,0x00,0xff }; +const uint8_t Version_CMD[5] = { 0x00,0x5b,0x00,0x00,0x40 }; +const uint8_t Indentify_CMD[5] = { 0x80,0xb3,0x01,0x04,0x20 }; + +uint8_t NrsecPort::CalcCRC7(const uint8_t *buff, int len) { - m_fd = 0; -} -bool NrsecPort::Open(const char *path) -{ - m_path = path; - m_fd = open(path, O_RDWR); - if (m_fd < 0) { - perror("open"); - return false; - } - spi_master_init(path, m_fd); - - return true; -} - -NrsecPort::~NrsecPort() -{ - if (m_fd > 0) - { - close(m_fd); - } -} - -unsigned char NrsecPort::get_crc7(const unsigned char *buff, int len) -{ - const static unsigned char crc7_table[256] = { + const static uint8_t crc7_table[256] = { 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, 0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26, @@ -88,7 +63,7 @@ unsigned char NrsecPort::get_crc7(const unsigned char *buff, int len) 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79 }; - unsigned char crc7_accum = 0; + uint8_t crc7_accum = 0; int i; for (i = 0; i < len; i++) { @@ -98,7 +73,7 @@ unsigned char NrsecPort::get_crc7(const unsigned char *buff, int len) return crc7_accum; } -void NrsecPort::SendCMD(uint8_t*cmd, uint8_t*rxbuf) +void NrsecPort::SendCMD(uint8_t* cmd, uint8_t* rxbuf) { int i = 0; int retval; @@ -115,7 +90,7 @@ void NrsecPort::SendCMD(uint8_t*cmd, uint8_t*rxbuf) /* send five command header */ for (i = 0; i < CMD_HEAD_SIZE; i++) { - retval = spi_transfer(m_fd, cmd + i, rxbuf + i, 1); + retval = spi_transfer(cmd + i, rxbuf + i, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "cmd[%d]=%x,rxbuf[%d]=%x", i, *(cmd + i), i, *(rxbuf + i)); delay(20); } @@ -134,7 +109,7 @@ INS: delay(20); while (cnt--) { - retval = spi_transfer(m_fd, txbuf, buf, 1); + retval = spi_transfer(txbuf, buf, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RcvINS txbuf=%x,buf=%x", *txbuf, *buf); if (*buf == ins) { @@ -146,14 +121,14 @@ INS: } } -void NrsecPort::RcvLEN(uint8_t*txbuf, uint8_t*buf, uint8_t len) +void NrsecPort::RcvLEN(uint8_t* txbuf, uint8_t* buf, uint8_t len) { int retval; /* receive length */ LEN: for (int i = 0; i < len; i++) { txbuf[0] = 0xaa; - retval = spi_transfer(m_fd, txbuf, buf + i, 1); + retval = spi_transfer(txbuf, buf + i, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RecvLEN txbuf=%x,rxbuf=%x", *txbuf, *(buf + i)); } } @@ -178,7 +153,7 @@ void NrsecPort::RcvData(uint8_t*txbuf, uint8_t*buf, int len) /* receive data and crc */ for (i = 0; i < len; i++) { - retval = spi_transfer(m_fd, txbuf, buf + i, 1); + retval = spi_transfer(txbuf, buf + i, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RcvData data[%d]=%x", i, *(buf + i)); } } @@ -196,7 +171,7 @@ SW90: delay(20); while (1) { - retval = spi_transfer(m_fd, txbuf, buf, 1); + retval = spi_transfer(txbuf, buf, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RecvSW txbuf=%x,buf=%x", *txbuf, *buf); @@ -206,14 +181,14 @@ SW90: } break; } - retval = spi_transfer(m_fd, txbuf, buf + 1, 1); + retval = spi_transfer(txbuf, buf + 1, 1); } void NrsecPort::SendEnd(uint8_t* txbuf, uint8_t* buf) { int retval; txbuf[0] = 0xaa; - retval = spi_transfer(m_fd, txbuf, buf, 1); + retval = spi_transfer(txbuf, buf, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendEnd txbuf=%x,rxbuf=%hhu", *txbuf, *buf); } @@ -221,7 +196,7 @@ void NrsecPort::SendId(uint8_t* txbuf, uint8_t* buf, uint8_t id) { int retval; txbuf[0] = id; - retval = spi_transfer(m_fd, txbuf, buf, 1); + retval = spi_transfer(txbuf, buf, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendID txbuf=%x,rxbuf=%hhu", *txbuf, *buf); } @@ -229,16 +204,16 @@ void NrsecPort::SendData(uint8_t* data, uint8_t* rxbuf, int data_size) { int i = 0; int retval; - unsigned char crc[1]; - crc[0] = get_crc7(data, data_size); + uint8_t crc[1]; + crc[0] = CalcCRC7(data, data_size); for (i = 0; i < data_size; i++) { - retval = spi_transfer(m_fd, data + i, rxbuf + i, 1); + retval = spi_transfer(data + i, rxbuf + i, 1); __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendData i=%d,txbuf=%x,rxbuf=%x", i, *(data + i), *(rxbuf + i)); delay(20); } - retval = spi_transfer(m_fd, crc, rxbuf, 1); + retval = spi_transfer(crc, rxbuf, 1); } @@ -246,8 +221,8 @@ int NrsecPort::Spirandom() { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -273,7 +248,7 @@ CMD_RESEND: RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90); //计算接收到数据的CRC - if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) + if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) { //CRC Error 命令重发,超过3次,结束 if (cnt < 3) @@ -308,8 +283,8 @@ std::string NrsecPort::Version() { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -339,7 +314,7 @@ CMD_RESEND: RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90); //计算接收到数据的CRC - if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) + if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) { //CRC Error 命令重发,超过3次,结束 if (cnt < 3) @@ -377,8 +352,8 @@ int NrsecPort::SM2keypair(int index)//产生密钥 { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -405,12 +380,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2ExportPublicKey(int index, unsigned char result[])//导出公钥 +int NrsecPort::SM2ExportPublicKey(int index, uint8_t result[])//导出公钥 { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -437,7 +412,7 @@ CMD_RESEND: RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90); //计算接收到数据的CRC - if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) + if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) { //CRC Error 命令重发,超过3次,结束 if (cnt < 3) @@ -464,12 +439,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2ExportPrivateKey(int index, unsigned char result[])//导出私钥 +int NrsecPort::SM2ExportPrivateKey(int index, uint8_t result[])//导出私钥 { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -498,7 +473,7 @@ CMD_RESEND: RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90); //计算接收到数据的CRC - if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) + if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1]) { //CRC Error 命令重发,超过3次,结束 if (cnt < 3) @@ -534,12 +509,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2InportPublicKey(int index, const unsigned char new_key[])//外部公钥导入存放在01 +int NrsecPort::SM2InportPublicKey(int index, const uint8_t new_key[])//外部公钥导入存放在01 { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -580,12 +555,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2InportPrivateKey(int index, const unsigned char new_key[])//导入私钥 没测试 +int NrsecPort::SM2InportPrivateKey(int index, const uint8_t new_key[])//导入私钥 没测试 { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -624,12 +599,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM3Hash(unsigned char *to_hash, int len, unsigned char *out_hash)//原始哈希 跑通了,没有验证 +int NrsecPort::SM3Hash(uint8_t *to_hash, int len, uint8_t *out_hash)//原始哈希 跑通了,没有验证 { int i; int cnt; - unsigned char txbuf[512]; - unsigned char rxbuf[512]; + uint8_t txbuf[512]; + uint8_t rxbuf[512]; int retval; int msglen; @@ -688,13 +663,13 @@ CMD_RESEND: return 0; } -int NrsecPort::sm3hash_tosm2(unsigned char *in, int inl, unsigned char *out, unsigned char *pubkey, unsigned char* pucID, int idl) +int NrsecPort::sm3hash_tosm2(uint8_t *in, int inl, uint8_t *out, uint8_t *pubkey, uint8_t* pucID, int idl) { int nRet, l; - unsigned char *Z = NULL; + uint8_t *Z = NULL; int entl = 0; - unsigned char tmpm[32]; - unsigned char abxy[32 * 4] = { + uint8_t tmpm[32]; + uint8_t abxy[32 * 4] = { 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, /* a */ 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, @@ -713,7 +688,7 @@ int NrsecPort::sm3hash_tosm2(unsigned char *in, int inl, unsigned char *out, uns 0xF0,0xA0 }; l = 2 + idl + 32 * 6; - Z = (unsigned char *)malloc(l); + Z = (uint8_t *)malloc(l); if (!Z) return -1; entl = idl * 8; @@ -729,7 +704,7 @@ int NrsecPort::sm3hash_tosm2(unsigned char *in, int inl, unsigned char *out, uns goto quit; free(Z); l = inl + 32; - Z = (unsigned char *)malloc(l); + Z = (uint8_t *)malloc(l); if (!Z) { nRet = -1; goto quit; @@ -743,12 +718,12 @@ quit: return nRet; }//用于签名的处理哈希值,用户标识01*16 -int NrsecPort::SM2Sign(int index, const unsigned char *to_sign, unsigned char *out_sign)//SM2签名 所使用的哈希值应该来源于sm3hash_tosm2 +int NrsecPort::SM2Sign(int index, const uint8_t *to_sign, uint8_t *out_sign)//SM2签名 所使用的哈希值应该来源于sm3hash_tosm2 { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -796,10 +771,10 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2VerifySign(int index, unsigned char *hash, unsigned char *vs)//SM2验签 +int NrsecPort::SM2VerifySign(int index, uint8_t *hash, uint8_t *vs)//SM2验签 { - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + uint8_t txbuf[256]; + uint8_t rxbuf[256]; int retval; int msglen; @@ -836,12 +811,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2encrypt(int index, unsigned char *to_encrypt, unsigned char *out_encrypt)//加密 +int NrsecPort::SM2encrypt(int index, uint8_t *to_encrypt, uint8_t *out_encrypt)//加密 { int i; int cnt; - unsigned char txbuf[512]; - unsigned char rxbuf[512]; + uint8_t txbuf[512]; + uint8_t rxbuf[512]; int retval; int msglen; @@ -889,12 +864,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2decoder(int index, unsigned char *to_decoder, unsigned char *out_decoder)//解密 +int NrsecPort::SM2decoder(int index, uint8_t *to_decoder, uint8_t *out_decoder)//解密 { int i; int cnt; - unsigned char txbuf[512]; - unsigned char rxbuf[512]; + uint8_t txbuf[512]; + uint8_t rxbuf[512]; int retval; int msglen; @@ -943,12 +918,12 @@ CMD_RESEND: return 0; } -int NrsecPort::SM2cert(int type, int index, string cert, unsigned char *out_cert)//证书 +int NrsecPort::SM2cert(int type, int index, string cert, uint8_t *out_cert)//证书 { int i; int cnt; - unsigned char txbuf[512]; - unsigned char rxbuf[1024]; + uint8_t txbuf[512]; + uint8_t rxbuf[1024]; int retval; int msglen; @@ -960,7 +935,7 @@ int NrsecPort::SM2cert(int type, int index, string cert, unsigned char *out_cert //printf("tx %1d bytes: ", msglen); int certlen = cert.length(); - unsigned char to_cert[certlen]; + uint8_t to_cert[certlen]; for (int x = 0; x < certlen; x++) { to_cert[x] = cert[x]; } @@ -1015,12 +990,12 @@ CMD_RESEND: return 0; } -int NrsecPort::Indentify(unsigned char *to_idt, unsigned char *out_idt)//安全认证 +int NrsecPort::Indentify(uint8_t *to_idt, uint8_t *out_idt)//安全认证 { int i; int cnt; - unsigned char txbuf[512]; - unsigned char rxbuf[512]; + uint8_t txbuf[512]; + uint8_t rxbuf[512]; int retval; int msglen; diff --git a/app/src/main/cpp/NrsecPort.h b/app/src/main/cpp/NrsecPort.h index 0006942..84cbed6 100644 --- a/app/src/main/cpp/NrsecPort.h +++ b/app/src/main/cpp/NrsecPort.h @@ -1,43 +1,35 @@ #ifndef __NRSECPORT_H__ #define __NRSECPORT_H__ -#include "SpiLib.h" +#include "SpiPort.h" #define CMD_HEAD_SIZE 5 using namespace std; -typedef unsigned char uint8_t; +typedef uint8_t uint8_t; -class NrsecPort : public SpiLIb { +class NrsecPort : public SpiPort { public: - NrsecPort(); - ~NrsecPort(); - - bool Open(const char* path); - int Spirandom(); std::string Version(); - int Indentify(unsigned char *to_idt, unsigned char *out_idt); + int Indentify(uint8_t *to_idt, uint8_t *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); - + int SM2ExportPublicKey(int index, uint8_t result[]); + int SM2ExportPrivateKey(int index, uint8_t result[]); + int SM2InportPublicKey(int index, const uint8_t new_key[]); + int SM2InportPrivateKey(int index, const uint8_t new_key[]); + int SM3Hash(uint8_t *to_hash, int len, uint8_t *out_hash); + int sm3hash_tosm2(uint8_t *in, int inl, uint8_t *out, uint8_t *pubkey, uint8_t *pucID, int idl); + int SM2Sign(int index, const uint8_t *to_sign, uint8_t *out_sign); + int SM2VerifySign(int index, uint8_t *hash, uint8_t * vs); + int SM2encrypt(int index, uint8_t *to_encrypt, uint8_t * out_encrypt); + int SM2decoder(int index, uint8_t *to_decoder, uint8_t *out_decoder); + int SM2cert(int type, int index, string cert, uint8_t *out_cert); protected: - - unsigned char get_crc7(const unsigned char *buff, int len); + uint8_t CalcCRC7(const uint8_t *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); @@ -48,9 +40,6 @@ protected: 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; }; diff --git a/app/src/main/cpp/SpiLib.h b/app/src/main/cpp/SpiLib.h deleted file mode 100644 index 2f60d38..0000000 --- a/app/src/main/cpp/SpiLib.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef TESTCOMM_SPILIB_H -#define TESTCOMM_SPILIB_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -using namespace std; - -typedef unsigned char u8; - -class SpiLIb { -public: - - int spi_transfer(int fd, unsigned char *txbuf, unsigned char *rxbuf, int bytes); - void spi_master_init(const char *name, int fd); - int delay(int x); -}; - - - - -#endif //TESTCOMM_SPILIB_H diff --git a/app/src/main/cpp/SpiLib.cpp b/app/src/main/cpp/SpiPort.cpp similarity index 54% rename from app/src/main/cpp/SpiLib.cpp rename to app/src/main/cpp/SpiPort.cpp index 591a455..00593d6 100644 --- a/app/src/main/cpp/SpiLib.cpp +++ b/app/src/main/cpp/SpiPort.cpp @@ -1,4 +1,4 @@ -#include "SpiLib.h" +#include "SpiPort.h" //typedef unsigned char u8; @@ -7,7 +7,38 @@ #define TAG_SPI "SPI" -int SpiLIb::spi_transfer(int fd, unsigned char *txbuf, unsigned char *rxbuf, int bytes) +SpiPort::SpiPort() : m_fd(0) +{ +} + +SpiPort::~SpiPort() +{ + Close(); +} + +bool SpiPort::Open(const char *path) +{ + m_path = path; + m_fd = open(path, O_RDWR); + if (m_fd < 0) { + perror("open"); + return false; + } + spi_master_init(); + + return true; +} + +void SpiPort::Close() +{ + if (m_fd > 0) + { + close(m_fd); + m_fd = 0; + } +} + +int SpiPort::spi_transfer(unsigned char *txbuf, unsigned char *rxbuf, int bytes) { struct spi_ioc_transfer xfer[2]; @@ -20,7 +51,7 @@ int SpiLIb::spi_transfer(int fd, unsigned char *txbuf, unsigned char *rxbuf, int xfer[0].len = bytes; xfer[0].delay_usecs = 2; - status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer); + status = ioctl(m_fd, SPI_IOC_MESSAGE(1), xfer); if (status < 0) { perror("SPI_IOC_MESSAGE"); @@ -30,7 +61,7 @@ int SpiLIb::spi_transfer(int fd, unsigned char *txbuf, unsigned char *rxbuf, int return status; } -void SpiLIb::spi_master_init(const char *name, int fd) +void SpiPort::spi_master_init() { __u8 mode = 3; __u8 lsb = 0; @@ -43,40 +74,40 @@ void SpiLIb::spi_master_init(const char *name, int fd) // SPI_IOC_WR_MODE int res = 0; - res = ioctl(fd, SPI_IOC_WR_MODE, &mode); - res = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); - res = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &lsb); + res = ioctl(m_fd, SPI_IOC_WR_MODE, &mode); + res = ioctl(m_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); + res = ioctl(m_fd, SPI_IOC_WR_LSB_FIRST, &lsb); - if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) + if (ioctl(m_fd, SPI_IOC_RD_MODE, &mode) < 0) { perror("SPI rd_mode"); return; } - if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) + if (ioctl(m_fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) { perror("SPI rd_lsb_fist"); return; } - if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) + if (ioctl(m_fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) { perror("SPI rd bits_per_word"); return; } - if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) + if (ioctl(m_fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) { perror("SPI rd max_speed_hz"); return; } - __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "%s: spi mode %d, %d bits %sper word, %d Hz max\n", name, mode, bits, lsb ? "(lsb first) " : "", speed); + __android_log_print(ANDROID_LOG_INFO, TAG_SPI, "%s: spi mode %d, %d bits %sper word, %d Hz max\n", m_path.c_str(), mode, bits, lsb ? "(lsb first) " : "", speed); //printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n", // name, mode, bits, lsb ? "(lsb first) " : "", speed); } -int SpiLIb::delay(int x) +int SpiPort::delay(int x) { #ifdef _WIN32 std::this_thread::sleep_for(std::chrono::milliseconds(x)); diff --git a/app/src/main/cpp/SpiPort.h b/app/src/main/cpp/SpiPort.h new file mode 100644 index 0000000..bf26d8f --- /dev/null +++ b/app/src/main/cpp/SpiPort.h @@ -0,0 +1,45 @@ +#ifndef TESTCOMM_SPIPORT_H +#define TESTCOMM_SPIPORT_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +using namespace std; + +typedef unsigned char u8; + +class SpiPort { +public: + + SpiPort(); + virtual ~SpiPort(); + + bool Open(const char* path); + void Close(); + + int spi_transfer(unsigned char *txbuf, unsigned char *rxbuf, int bytes); + void spi_master_init(); + static int delay(int x); + +protected: + std::string m_path; + int m_fd; + +}; + + + + +#endif //TESTCOMM_SPIPORT_H