11 changed files with 408 additions and 21 deletions
-
11src/iflytop/components/iflytop_front_end_service/iflytop_front_end_service.cpp
-
6src/iflytop/components/iflytop_front_end_service/iflytop_front_end_service.hpp
-
55src/iflytop/components/simple_udp/simple_udp.cpp
-
58src/iflytop/components/simple_udp/simple_udp.hpp
-
158src/iflytop/components/zcanreceiver/zcanhost.cpp
-
70src/iflytop/components/zcanreceiver/zcanhost.hpp
-
10src/iflytop/components/zcanreceiver/zcanreceiverhost.cpp
-
10src/iflytop/components/zcanreceiver/zcanreceiverhost.hpp
-
15src/iflytop/core/core.hpp
-
21src/iflytop/core/error/error_code.cpp
-
15src/iflytop/core/error/error_code.hpp
@ -0,0 +1,55 @@ |
|||||
|
#include "simple_udp.hpp"
|
||||
|
|
||||
|
|
||||
|
|
||||
|
using namespace std; |
||||
|
using namespace iflytop; |
||||
|
using namespace core; |
||||
|
|
||||
|
void SimpleUDP::initialize(int port) { |
||||
|
// 建立一个UDP的socket
|
||||
|
m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
||||
|
|
||||
|
if (m_socket == -1) { |
||||
|
logger->error("socket failed"); |
||||
|
return; |
||||
|
} |
||||
|
// 绑定地址信息
|
||||
|
|
||||
|
struct sockaddr_in servaddr; |
||||
|
memset(&servaddr, 0, sizeof(servaddr)); |
||||
|
servaddr.sin_family = AF_INET; |
||||
|
servaddr.sin_port = htons(port); |
||||
|
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
||||
|
|
||||
|
if (bind(m_socket, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { |
||||
|
logger->error("bind {} failed,", port); |
||||
|
return; |
||||
|
} |
||||
|
logger->info("bind {} success", port); |
||||
|
|
||||
|
m_thread.reset(new Thread("udpThread", [this]() { //
|
||||
|
struct sockaddr_in peeraddr; |
||||
|
socklen_t peerlen; |
||||
|
while (true) { |
||||
|
memset(m_rxbuf, 0, sizeof(m_rxbuf)); |
||||
|
// 网络节点的信息,用来保存客户端的网络信息
|
||||
|
// sockaddr_in clientAddr;
|
||||
|
memset(&peeraddr, 0, sizeof(sockaddr_in)); |
||||
|
int clientAddrLen = sizeof(sockaddr); |
||||
|
// 接收客户端发来的数据
|
||||
|
int ret = recvfrom(m_socket, m_rxbuf, sizeof(m_rxbuf), 0, (struct sockaddr *)&peeraddr, &peerlen); |
||||
|
// printf("Recv msg:%s from IP:[%s] Port:[%d]\n", m_rxbuf, inet_ntoa(m_context.clientAddr.sin_addr), ntohs(m_context.clientAddr.sin_port));
|
||||
|
// process_rx_cmd(m_rxbuf, ret);
|
||||
|
// sendto(m_socket, "Hello World!", strlen("Hello World!"), 0, (sockaddr *)&m_context.clientAddr, clientAddrLen);
|
||||
|
if (ret < 0) { |
||||
|
logger->error("recvfrom failed, {}", strerror(errno)); |
||||
|
continue; |
||||
|
} |
||||
|
if (m_on_recv) { |
||||
|
m_on_recv(&peeraddr, m_rxbuf, ret); |
||||
|
} |
||||
|
} |
||||
|
})); |
||||
|
return; |
||||
|
}; |
@ -0,0 +1,58 @@ |
|||||
|
//
|
||||
|
// Created by zwsd
|
||||
|
//
|
||||
|
|
||||
|
#pragma once
|
||||
|
#include <errno.h>
|
||||
|
#include <netinet/in.h>
|
||||
|
#include <stdio.h>
|
||||
|
#include <stdlib.h>
|
||||
|
#include <string.h>
|
||||
|
#include <sys/socket.h>
|
||||
|
#include <sys/types.h> /* See NOTES */
|
||||
|
#include <sys/types.h>
|
||||
|
#include <unistd.h>
|
||||
|
|
||||
|
#include <fstream>
|
||||
|
#include <iostream>
|
||||
|
#include <list>
|
||||
|
#include <map>
|
||||
|
#include <memory>
|
||||
|
#include <set>
|
||||
|
#include <sstream>
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
|
||||
|
#include "iflytop/core/core.hpp"
|
||||
|
/**
|
||||
|
* @brief |
||||
|
* |
||||
|
* service: SimpleUDP |
||||
|
* |
||||
|
* 监听事件: |
||||
|
* 依赖状态: |
||||
|
* 依赖服务: |
||||
|
* 作用: |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace iflytop { |
||||
|
using namespace std; |
||||
|
using namespace core; |
||||
|
class SimpleUDP : public enable_shared_from_this<SimpleUDP> { |
||||
|
ENABLE_LOGGER(SimpleUDP); |
||||
|
int m_socket = -1; |
||||
|
unique_ptr<Thread> m_thread; |
||||
|
char m_rxbuf[1024]; |
||||
|
|
||||
|
typedef function<void(struct sockaddr_in* from, char* data, size_t len)> on_recv_t; |
||||
|
|
||||
|
on_recv_t m_on_recv; |
||||
|
|
||||
|
public: |
||||
|
SimpleUDP(){}; |
||||
|
void initialize(int port); |
||||
|
void set_on_recv(on_recv_t on_recv) { m_on_recv = on_recv; } |
||||
|
void sendto(struct sockaddr_in* from, const char* data, size_t len) { ::sendto(m_socket, data, len, 0, (struct sockaddr*)from, sizeof(struct sockaddr_in)); } |
||||
|
}; |
||||
|
} // namespace iflytop
|
@ -0,0 +1,158 @@ |
|||||
|
#include "zcanhost.hpp"
|
||||
|
|
||||
|
using namespace iflytop; |
||||
|
using namespace core; |
||||
|
using namespace zcr; |
||||
|
|
||||
|
#define zint16p(x) ((int16_t*)(x))
|
||||
|
#define zint32p(x) ((int32_t*)(x))
|
||||
|
|
||||
|
void ZCanHost::initialize(string can_if_name, int baudrate, bool enablLoopback) { |
||||
|
m_zcanReceiverHost = make_shared<ZCanReceiverHost>(); |
||||
|
m_zcanReceiverHost->initialize(can_if_name, baudrate, enablLoopback); |
||||
|
} |
||||
|
|
||||
|
static void prase_cmd(char* input, int inputlen, int& argc, char* argv[]) { |
||||
|
bool findcomment = false; |
||||
|
for (size_t i = 0; input[i] == 0 || i < inputlen; i++) { |
||||
|
if (input[i] == ' ' || input[i] == '\r' || input[i] == '\n' || input[i] == '\t') { |
||||
|
input[i] = 0; |
||||
|
} |
||||
|
|
||||
|
if (input[i] == '#') { |
||||
|
findcomment = true; |
||||
|
} |
||||
|
if (findcomment) { |
||||
|
input[i] = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int j = 0; |
||||
|
for (size_t i = 0; input[i] == 0 || i < inputlen; i++) { |
||||
|
if (input[i] != 0 && j == 0) { |
||||
|
argv[argc++] = &input[i]; |
||||
|
j = 1; |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
if (input[i] == 0 && j == 1) { |
||||
|
j = 0; |
||||
|
continue; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bool ZCanHost::execcmd(string cmd, string& retval) { |
||||
|
int argc = 0; |
||||
|
char* argv[10] = {0}; |
||||
|
char cmdcache[1024] = {0}; |
||||
|
// context.rawcmd = cmd;
|
||||
|
strcpy(cmdcache, cmd.c_str()); |
||||
|
logger->info("do cmd:{}", cmdcache); |
||||
|
|
||||
|
for (size_t i = 0; i < cmd.size(); i++) { |
||||
|
if (cmdcache[i] == '#') { |
||||
|
cmdcache[i] = '\0'; |
||||
|
} |
||||
|
} |
||||
|
prase_cmd(cmdcache, strlen(cmdcache), argc, argv); |
||||
|
if (argc == 0) { |
||||
|
logger->error("cmd:{} prase error", cmd); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (m_cmdMap.find(string(argv[0])) != m_cmdMap.end()) { |
||||
|
return m_cmdMap.find(string(argv[0]))->second(argc, argv, retval); |
||||
|
} |
||||
|
logger->error("cmd:{} not found", argv[0]); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/*******************************************************************************
|
||||
|
* CMD * |
||||
|
*******************************************************************************/ |
||||
|
/**
|
||||
|
* @brief 0-1000 Command support |
||||
|
*/ |
||||
|
bool ZCanHost::ping(int board_id) { |
||||
|
shared_ptr<ZCanReceiverCMD> cmd = make_shared<ZCanReceiverCMD>(); |
||||
|
cmd->cmdid = 0; |
||||
|
cmd->subcmdid = 0; |
||||
|
cmd->data[0] = board_id; |
||||
|
cmd->len = 1; |
||||
|
auto rx = m_zcanReceiverHost->sendcmdblock(cmd, 100); |
||||
|
return rx != nullptr; |
||||
|
} |
||||
|
|
||||
|
int32_t ZCanHost::readio(int id, bool& value) { |
||||
|
shared_ptr<ZCanReceiverCMD> cmd = make_shared<ZCanReceiverCMD>(); |
||||
|
cmd->cmdid = 1; |
||||
|
cmd->subcmdid = 0; |
||||
|
cmd->data[0] = id; |
||||
|
cmd->len = 1; |
||||
|
auto rx = m_zcanReceiverHost->sendcmdblock(cmd, 100); |
||||
|
if (rx == nullptr) { |
||||
|
return err::zecode(err::knoack); |
||||
|
} |
||||
|
value = rx->data[1]; |
||||
|
return 0; |
||||
|
} |
||||
|
int32_t ZCanHost::writeio(int id, bool value) { |
||||
|
shared_ptr<ZCanReceiverCMD> cmd = make_shared<ZCanReceiverCMD>(); |
||||
|
cmd->cmdid = 2; |
||||
|
cmd->subcmdid = 0; |
||||
|
cmd->data[0] = id; |
||||
|
cmd->data[1] = value; |
||||
|
cmd->len = 2; |
||||
|
auto rx = m_zcanReceiverHost->sendcmdblock(cmd, 100); |
||||
|
if (rx == nullptr) { |
||||
|
return err::zecode(err::knoack); |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
int32_t ZCanHost::readadc(int id, int& value) { |
||||
|
shared_ptr<ZCanReceiverCMD> cmd = make_shared<ZCanReceiverCMD>(); |
||||
|
cmd->cmdid = 3; |
||||
|
cmd->subcmdid = 0; |
||||
|
cmd->data[0] = id; |
||||
|
cmd->len = 1; |
||||
|
auto rx = m_zcanReceiverHost->sendcmdblock(cmd, 100); |
||||
|
if (rx == nullptr) { |
||||
|
return err::zecode(err::knoack); |
||||
|
} |
||||
|
value = *(int32_t*)(&rx->data[2]); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// 1004
|
||||
|
bool ZCanHost::pumpctrl_c1004(int sensorid, int16_t acc, int16_t rpm) { |
||||
|
shared_ptr<ZCanReceiverCMD> cmd = make_shared<ZCanReceiverCMD>(); |
||||
|
cmd->cmdid = 1004; |
||||
|
cmd->subcmdid = 0; |
||||
|
cmd->data[0] = sensorid; |
||||
|
*zint16p(&cmd->data[2]) = acc; |
||||
|
*zint16p(&cmd->data[4]) = rpm; |
||||
|
cmd->len = 6; |
||||
|
auto rx = m_zcanReceiverHost->sendcmdblock(cmd, 100); |
||||
|
return rx != nullptr; |
||||
|
} |
||||
|
|
||||
|
/**
|
||||
|
* @brief 报警三色指示灯控制 |
||||
|
* |
||||
|
* @param sensorid |
||||
|
* @param r |
||||
|
* @param g |
||||
|
* @param b |
||||
|
* @param w |
||||
|
*/ |
||||
|
bool ZCanHost::warning_light_ctrl_c1002(uint8_t sensorid, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { return false; } |
||||
|
|
||||
|
/**
|
||||
|
* @brief 读取华成压力传感器数值 |
||||
|
* |
||||
|
* @param sensorid |
||||
|
* @param value |
||||
|
*/ |
||||
|
|
||||
|
bool ZCanHost::huacheng_pressure_sensor_read_c1005(int sensorid, huacheng_pressure_sensor_read_c1005_t& value) { return false; } |
@ -0,0 +1,70 @@ |
|||||
|
//
|
||||
|
// Created by zwsd
|
||||
|
//
|
||||
|
|
||||
|
#pragma once
|
||||
|
|
||||
|
#include "cmd.hpp"
|
||||
|
#include "iflytop/core/core.hpp"
|
||||
|
#include "zcanreceiverhost.hpp"
|
||||
|
|
||||
|
namespace iflytop { |
||||
|
|
||||
|
using namespace std; |
||||
|
using namespace zcr; |
||||
|
using namespace core; |
||||
|
|
||||
|
class ZCanHost { |
||||
|
ENABLE_LOGGER(ZCanHost); |
||||
|
|
||||
|
shared_ptr<ZCanReceiverHost> m_zcanReceiverHost; |
||||
|
|
||||
|
map<string, function<bool(int argc, char** argv, string& retval)>> m_cmdMap; |
||||
|
|
||||
|
public: |
||||
|
void initialize(string can_if_name, int baudrate, bool enablLoopback); |
||||
|
bool execcmd(string cmd, string& retval); |
||||
|
|
||||
|
/*******************************************************************************
|
||||
|
* CMD * |
||||
|
*******************************************************************************/ |
||||
|
/**
|
||||
|
* @brief 0-1000 Command support |
||||
|
*/ |
||||
|
bool ping(int board_id); |
||||
|
|
||||
|
int32_t readio(int id, bool& value); |
||||
|
int32_t writeio(int id, bool value); |
||||
|
int32_t readadc(int id, int& value); |
||||
|
|
||||
|
// 1004
|
||||
|
bool pumpctrl_c1004(int sensorid, int16_t acc, int16_t rpm); |
||||
|
|
||||
|
/**
|
||||
|
* @brief 报警三色指示灯控制 |
||||
|
* |
||||
|
* @param sensorid |
||||
|
* @param r |
||||
|
* @param g |
||||
|
* @param b |
||||
|
* @param w |
||||
|
*/ |
||||
|
bool warning_light_ctrl_c1002(uint8_t sensorid, uint8_t r, uint8_t g, uint8_t b, uint8_t w); |
||||
|
|
||||
|
/**
|
||||
|
* @brief 读取华成压力传感器数值 |
||||
|
* |
||||
|
* @param sensorid |
||||
|
* @param value |
||||
|
*/ |
||||
|
typedef struct { |
||||
|
uint8_t precision; // 0,1,2,3
|
||||
|
uint8_t unit; // 0-Mpa ,1-Kpa ,2-Pa ,3-Bar ,4-Mbar ,5-kg/cm2 ,6-psi ,7-mh2o ,8-mmh2o
|
||||
|
uint16_t value; // value, realvalue = value / 10^precision unit
|
||||
|
uint16_t zero; // 零点
|
||||
|
uint16_t full; // 满量程
|
||||
|
} huacheng_pressure_sensor_read_c1005_t; |
||||
|
bool huacheng_pressure_sensor_read_c1005(int sensorid, huacheng_pressure_sensor_read_c1005_t& value); |
||||
|
}; |
||||
|
|
||||
|
} // namespace iflytop
|
@ -1,8 +1,21 @@ |
|||||
#pragma once
|
#pragma once
|
||||
|
|
||||
|
#include <fstream>
|
||||
|
#include <functional>
|
||||
|
#include <iostream>
|
||||
|
#include <list>
|
||||
|
#include <map>
|
||||
|
#include <memory>
|
||||
|
#include <set>
|
||||
|
#include <sstream>
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
#include <mutex>
|
||||
|
|
||||
|
#include "iflytop/core/basic/nlohmann/json.hpp"
|
||||
#include "iflytop/core/basic/nod/nod.hpp"
|
#include "iflytop/core/basic/nod/nod.hpp"
|
||||
|
#include "iflytop/core/components/stringutils.hpp"
|
||||
#include "iflytop/core/components/timeutils.hpp"
|
#include "iflytop/core/components/timeutils.hpp"
|
||||
#include "iflytop/core/error/error_code.hpp"
|
#include "iflytop/core/error/error_code.hpp"
|
||||
#include "iflytop/core/spdlogfactory/logger.hpp"
|
#include "iflytop/core/spdlogfactory/logger.hpp"
|
||||
#include "iflytop/core/thread/thread.hpp"
|
#include "iflytop/core/thread/thread.hpp"
|
||||
#include "iflytop/core/components/stringutils.hpp"
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue